test/jdk/sun/management/windows/exerevokeall.c
author dtitov
Wed, 22 May 2019 09:06:24 -0700
changeset 54987 2ffbc00d87ae
parent 47216 test/jdk/sun/management/windows/revokeall.c@71c04702a3d5
permissions -rw-r--r--
8214545: sun/management/jmxremote/bootstrap tests hang in revokeall.exe on Windows Reviewed-by: dholmes, amenkov, bulasevich, dfuchs, erikj
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
54987
2ffbc00d87ae 8214545: sun/management/jmxremote/bootstrap tests hang in revokeall.exe on Windows
dtitov
parents: 47216
diff changeset
     2
 * Copyright (c) 2004, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
#include <stdio.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
#include <windows.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include <malloc.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
/*
54987
2ffbc00d87ae 8214545: sun/management/jmxremote/bootstrap tests hang in revokeall.exe on Windows
dtitov
parents: 47216
diff changeset
    30
 * Simple Windows utility to remove all non-owner access to a given file.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * Access mask to represent any file access
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#define ANY_ACCESS (FILE_GENERIC_READ | FILE_GENERIC_WRITE | FILE_GENERIC_EXECUTE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * Print error message to stderr
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
static void printLastError(const char* msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    int len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    char buf[128];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    DWORD errval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    buf[0] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    len = sizeof(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    errval = GetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    if (errval != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        int n = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                              NULL, errval,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                              0, buf, len, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        if (n > 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            /* Drop final '.', CR, LF */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            if (buf[n - 1] == '\n') n--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            if (buf[n - 1] == '\r') n--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            if (buf[n - 1] == '.') n--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            buf[n] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    if (strlen(buf) > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        fprintf(stderr, "revokeall %s: %s\n", msg, buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        fprintf(stderr, "revokeall %s\n", msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * Return a string that includes all the components of a given SID.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * See here for a description of the SID components :-
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/security/security/sid_components.asp
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
static char *getTextualSid(SID* sid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    SID_IDENTIFIER_AUTHORITY* sia;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    DWORD i, count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    DWORD len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    char* name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * Get the identifier authority and the number of sub-authorities
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    sia = GetSidIdentifierAuthority(sid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    count = *GetSidSubAuthorityCount(sid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * Allocate buffer for the string - buffer is :-
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * S-SID_REVISION- + identifierAuthority- + subauthorities- + NULL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    len=(15 + 12 + (12 * count) + 1) * sizeof(char);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    name = (char*)malloc(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    if (name == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    // S-SID_REVISION
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    sprintf(name, "S-%lu-", SID_REVISION );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    // Identifier authority
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    if ((sia->Value[0] != 0) || (sia->Value[1] != 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        sprintf(name + strlen(name), "0x%02hx%02hx%02hx%02hx%02hx%02hx",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                (USHORT)sia->Value[0],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                (USHORT)sia->Value[1],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                (USHORT)sia->Value[2],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                (USHORT)sia->Value[3],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                (USHORT)sia->Value[4],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                (USHORT)sia->Value[5]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        sprintf(name + strlen(name), "%lu",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                (ULONG)(sia->Value[5]      )   +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                (ULONG)(sia->Value[4] <<  8)   +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                (ULONG)(sia->Value[3] << 16)   +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                (ULONG)(sia->Value[2] << 24)   );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    // finally, the sub-authorities
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    for (i=0 ; i<count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        sprintf(name + strlen(name), "-%lu",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                *GetSidSubAuthority(sid, i) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * Returns a string to represent the given security identifier (SID).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * If the account is known to the local computer then the account
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * domain is returned. The format will be \\name or domain\\name depending
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 * on if the computer belongs to a domain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * If the account name is not known then the textual representation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * SID is returned -- eg: S-1-5-21-2818032319-470147023-1036452850-13037.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
static char *getSIDString(SID* sid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    char domain[255];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    char name[255];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    DWORD domainLen = sizeof(domain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    DWORD nameLen = sizeof(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    SID_NAME_USE use;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    if(!IsValidSid(sid)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        return strdup("<Invalid SID>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    if (LookupAccountSid(NULL, sid, name, &nameLen, domain, &domainLen, &use)) {
54987
2ffbc00d87ae 8214545: sun/management/jmxremote/bootstrap tests hang in revokeall.exe on Windows
dtitov
parents: 47216
diff changeset
   153
        size_t len = strlen(name) + strlen(domain) + 3;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        char* s = (char*)malloc(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        if (s != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            strcpy(s, domain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            strcat(s, "\\\\");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            strcat(s, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        return getTextualSid(sid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 * Returns 1 if the specified file is on a file system that supports
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
 * persistent ACLs (On NTFS file systems returns true, on FAT32 file systems
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
 * returns false), otherwise 0. Returns -1 if error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
static int isSecuritySupported(const char* path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    char* root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    char* p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    BOOL res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    DWORD dwMaxComponentLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    DWORD dwFlags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    char fsName[128];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    DWORD fsNameLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * Get root directory. For UNCs the slash after the share name is required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    root = strdup(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    if (*root == '\\') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
         * \\server\share\file ==> \\server\share\
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        int slashskip = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        p = root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        while ((*p == '\\') && (slashskip > 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            char* p2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            p++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            p2 = strchr(p, '\\');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            if ((p2 == NULL) || (*p2 != '\\')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                free(root);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                fprintf(stderr, "Malformed UNC");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            p = p2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            slashskip--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        if (slashskip != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            free(root);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            fprintf(stderr, "Malformed UNC");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        p++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        *p = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        p = strchr(root, '\\');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
         * Relative path so use current directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        if (p == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            free(root);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            root = malloc(255);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            if (GetCurrentDirectory(255, root) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                printLastError("GetCurrentDirectory failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            p = strchr(root, '\\');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            if (p == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                fprintf(stderr, "GetCurrentDirectory doesn't include drive letter!!!!\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        p++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        *p = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * Get the volume information - this gives us the file system file and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * also tells us if the file system supports persistent ACLs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    fsNameLength = sizeof(fsName)-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    res = GetVolumeInformation(root,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                               NULL,        // address of name of the volume, can be NULL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                               0,           // length of volume name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                               NULL,        // address of volume serial number, can be NULL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                               &dwMaxComponentLength,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                               &dwFlags,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                               fsName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                               fsNameLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    if (res == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        printLastError("GetVolumeInformation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        free(root);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    free(root);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    return (dwFlags & FS_PERSISTENT_ACLS) ? 1 : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
 * Returns the security descriptor for a file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
static SECURITY_DESCRIPTOR* getFileSecurityDescriptor(const char* path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    SECURITY_DESCRIPTOR* sd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    DWORD len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    SECURITY_INFORMATION info =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        OWNER_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    GetFileSecurity(path, info , 0, 0, &len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        printLastError("GetFileSecurity failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    sd = (SECURITY_DESCRIPTOR *)malloc(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    if (sd == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        fprintf(stderr, "Out of memory");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        if (!GetFileSecurity(path, info, sd, len, &len)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            printLastError("GetFileSecurity failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            free(sd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    return sd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
 * Revoke all access to the specific file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
static int revokeAll(const char* path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    SECURITY_DESCRIPTOR* sd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    SID* owner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    ACL *acl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    BOOL defaulted, present;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    ACL_SIZE_INFORMATION acl_size_info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    DWORD i, count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    char* str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * Get security descriptor for file; From security descriptor get the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * owner SID, and the DACL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    sd = getFileSecurityDescriptor(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    if (sd == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        return -1;      /* error already reported */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    if (!GetSecurityDescriptorOwner(sd, &owner, &defaulted)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        printLastError("GetSecurityDescriptorOwner failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    str = getSIDString(owner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    if (str != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        printf("owner: %s\n", str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        free(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    if (!GetSecurityDescriptorDacl(sd, &present, &acl, &defaulted)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        printLastError("GetSecurityDescriptorDacl failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    if (!present) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        fprintf(stderr, "Security descriptor does not contain a DACL");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * If DACL is NULL there is no access to the file - we are done
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    if (acl == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * Iterate over the ACEs. For each "allow" type check that the SID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * matches the owner - if not we remove the ACE from the ACL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    if (!GetAclInformation(acl, (void *) &acl_size_info, sizeof(acl_size_info),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                                  AclSizeInformation)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        printLastError("GetAclInformation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    count = acl_size_info.AceCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    while (count > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        void* ace;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        ACCESS_ALLOWED_ACE *access;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        SID* sid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        BOOL deleted;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        if (!GetAce(acl, i, &ace)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            printLastError("GetAce failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        if (((ACCESS_ALLOWED_ACE *)ace)->Header.AceType != ACCESS_ALLOWED_ACE_TYPE) {
54987
2ffbc00d87ae 8214545: sun/management/jmxremote/bootstrap tests hang in revokeall.exe on Windows
dtitov
parents: 47216
diff changeset
   354
            i++;
2ffbc00d87ae 8214545: sun/management/jmxremote/bootstrap tests hang in revokeall.exe on Windows
dtitov
parents: 47216
diff changeset
   355
            count--;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        access = (ACCESS_ALLOWED_ACE *)ace;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        sid = (SID *) &access->SidStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        deleted = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        if (!EqualSid(owner, sid)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
             * If the ACE allows any access then the file then we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
             * delete it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            if (access->Mask & ANY_ACCESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                str = getSIDString(sid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                if (str != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                    printf("remove ALLOW %s\n", str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                    free(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                if (DeleteAce(acl, i) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                    printLastError("DeleteAce failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                deleted = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        if (!deleted) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            str = getSIDString(sid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            if (str != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                printf("ALLOW %s (access mask=%x)\n", str, access->Mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                free(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            /* onto the next ACE */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        count--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * No changes - only owner has access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    if (i == acl_size_info.AceCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        printf("No changes.\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * Create security descriptor and set its DACL to the version
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * that we just edited
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    if (!InitializeSecurityDescriptor(sd, SECURITY_DESCRIPTOR_REVISION)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        printLastError("InitializeSecurityDescriptor failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    if (!SetSecurityDescriptorDacl(sd, present, acl, defaulted)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        printLastError("SetSecurityDescriptorDacl failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    if (!SetFileSecurity(path, DACL_SECURITY_INFORMATION, sd)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        printLastError("SetFileSecurity failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    printf("File updated.\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
 * Convert slashes in the pathname to backslashes if needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
static char* convert_path(const char* p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
   int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
   char* path = strdup(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
   while (p[i] != '\0') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
       if (p[i] == '/') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
           path[i] = '\\';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
       i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
   return path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
 * Usage: revokeall file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
int main( int argc, char *argv[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    int rc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    const char* path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    if (argc != 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        fprintf(stderr, "Usage: %s file\n", argv[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    path = convert_path(argv[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    printf("Revoking all non-owner access to %s\n", path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    rc = isSecuritySupported(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    if (rc != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        if (rc == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            printf("File security not supported on this file system\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        return rc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        return revokeAll(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
}