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