jdk/src/windows/native/com/sun/security/auth/module/nt.c
author coffeys
Mon, 05 Sep 2011 11:28:23 +0100
changeset 10429 d274e775b258
parent 5506 202f599c92aa
child 22648 7bcdb55f70ff
permissions -rw-r--r--
7049079: NTSYSTEM CLASS IS LEAKING WINDOWS TOKENS Reviewed-by: weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10429
d274e775b258 7049079: NTSYSTEM CLASS IS LEAKING WINDOWS TOKENS
coffeys
parents: 5506
diff changeset
     2
 * Copyright (c) 2000, 2011, 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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include <jni.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include "com_sun_security_auth_module_NTSystem.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include <windows.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include <stdio.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include <wchar.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include <ntsecapi.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include <lmerr.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
static BOOL debug = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
BOOL getToken(PHANDLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
BOOL getUser(HANDLE tokenHandle, LPTSTR *userName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        LPTSTR *domainName, LPTSTR *userSid, LPTSTR *domainSid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
BOOL getPrimaryGroup(HANDLE tokenHandle, LPTSTR *primaryGroup);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
BOOL getGroups(HANDLE tokenHandle, PDWORD numGroups, LPTSTR **groups);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
BOOL getImpersonationToken(PHANDLE impersonationToken);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
BOOL getTextualSid(PSID pSid, LPTSTR TextualSid, LPDWORD lpdwBufferLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
void DisplayErrorText(DWORD dwLastError);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
10429
d274e775b258 7049079: NTSYSTEM CLASS IS LEAKING WINDOWS TOKENS
coffeys
parents: 5506
diff changeset
    46
JNIEXPORT jlong JNICALL
d274e775b258 7049079: NTSYSTEM CLASS IS LEAKING WINDOWS TOKENS
coffeys
parents: 5506
diff changeset
    47
Java_com_sun_security_auth_module_NTSystem_getImpersonationToken0
d274e775b258 7049079: NTSYSTEM CLASS IS LEAKING WINDOWS TOKENS
coffeys
parents: 5506
diff changeset
    48
        (JNIEnv *env, jobject obj) {
d274e775b258 7049079: NTSYSTEM CLASS IS LEAKING WINDOWS TOKENS
coffeys
parents: 5506
diff changeset
    49
    HANDLE impersonationToken = 0;      // impersonation token
d274e775b258 7049079: NTSYSTEM CLASS IS LEAKING WINDOWS TOKENS
coffeys
parents: 5506
diff changeset
    50
    if (debug) {
d274e775b258 7049079: NTSYSTEM CLASS IS LEAKING WINDOWS TOKENS
coffeys
parents: 5506
diff changeset
    51
        printf("getting impersonation token\n");
d274e775b258 7049079: NTSYSTEM CLASS IS LEAKING WINDOWS TOKENS
coffeys
parents: 5506
diff changeset
    52
    }
d274e775b258 7049079: NTSYSTEM CLASS IS LEAKING WINDOWS TOKENS
coffeys
parents: 5506
diff changeset
    53
    if (getImpersonationToken(&impersonationToken) == FALSE) {
d274e775b258 7049079: NTSYSTEM CLASS IS LEAKING WINDOWS TOKENS
coffeys
parents: 5506
diff changeset
    54
        return 0;
d274e775b258 7049079: NTSYSTEM CLASS IS LEAKING WINDOWS TOKENS
coffeys
parents: 5506
diff changeset
    55
    }
d274e775b258 7049079: NTSYSTEM CLASS IS LEAKING WINDOWS TOKENS
coffeys
parents: 5506
diff changeset
    56
    return (jlong)impersonationToken;
d274e775b258 7049079: NTSYSTEM CLASS IS LEAKING WINDOWS TOKENS
coffeys
parents: 5506
diff changeset
    57
}
d274e775b258 7049079: NTSYSTEM CLASS IS LEAKING WINDOWS TOKENS
coffeys
parents: 5506
diff changeset
    58
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
Java_com_sun_security_auth_module_NTSystem_getCurrent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    (JNIEnv *env, jobject obj, jboolean debugNative) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    long i, j = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    HANDLE tokenHandle = INVALID_HANDLE_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    BOOL systemError = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    LPTSTR userName = NULL;             // user name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    LPTSTR userSid = NULL;              // user sid
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    LPTSTR domainName = NULL;           // domain name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    LPTSTR domainSid = NULL;            // domain sid
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    LPTSTR primaryGroup = NULL;         // primary group sid
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    DWORD numGroups = 0;                // num groups
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    LPTSTR *groups = NULL;              // groups array
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    long pIndex = -1;                   // index of primaryGroup in groups array
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    jfieldID fid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    jstring jstr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    jobjectArray jgroups;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    jclass stringClass = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    jclass cls = (*env)->GetObjectClass(env, obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    debug = debugNative;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    // get NT information first
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        printf("getting access token\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    if (getToken(&tokenHandle) == FALSE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        printf("getting user info\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    if (getUser
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        (tokenHandle, &userName, &domainName, &userSid, &domainSid) == FALSE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        printf("getting primary group\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    if (getPrimaryGroup(tokenHandle, &primaryGroup) == FALSE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        printf("getting supplementary groups\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    if (getGroups(tokenHandle, &numGroups, &groups) == FALSE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    // then set values into NTSystem
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    fid = (*env)->GetFieldID(env, cls, "userName", "Ljava/lang/String;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    if (fid == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        jclass newExcCls =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            (*env)->FindClass(env, "java/lang/IllegalArgumentException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if (newExcCls == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            // Unable to find exception class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            systemError = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            goto out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        (*env)->ThrowNew(env, newExcCls, "invalid field: userName");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    jstr = (*env)->NewStringUTF(env, userName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    (*env)->SetObjectField(env, obj, fid, jstr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    fid = (*env)->GetFieldID(env, cls, "userSID", "Ljava/lang/String;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    if (fid == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        jclass newExcCls =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            (*env)->FindClass(env, "java/lang/IllegalArgumentException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        if (newExcCls == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            systemError = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            goto out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        (*env)->ThrowNew(env, newExcCls, "invalid field: userSID");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    jstr = (*env)->NewStringUTF(env, userSid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    (*env)->SetObjectField(env, obj, fid, jstr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    fid = (*env)->GetFieldID(env, cls, "domain", "Ljava/lang/String;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    if (fid == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        jclass newExcCls =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            (*env)->FindClass(env, "java/lang/IllegalArgumentException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (newExcCls == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            systemError = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            goto out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        (*env)->ThrowNew(env, newExcCls, "invalid field: domain");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    jstr = (*env)->NewStringUTF(env, domainName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    (*env)->SetObjectField(env, obj, fid, jstr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    if (domainSid != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        fid = (*env)->GetFieldID(env, cls, "domainSID", "Ljava/lang/String;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if (fid == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            jclass newExcCls =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                (*env)->FindClass(env, "java/lang/IllegalArgumentException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            if (newExcCls == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                systemError = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                goto out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            (*env)->ThrowNew(env, newExcCls, "invalid field: domainSID");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        jstr = (*env)->NewStringUTF(env, domainSid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        (*env)->SetObjectField(env, obj, fid, jstr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    fid = (*env)->GetFieldID(env, cls, "primaryGroupID", "Ljava/lang/String;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    if (fid == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        jclass newExcCls =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            (*env)->FindClass(env, "java/lang/IllegalArgumentException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        if (newExcCls == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            systemError = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            goto out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        (*env)->ThrowNew(env, newExcCls, "invalid field: PrimaryGroupID");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    jstr = (*env)->NewStringUTF(env, primaryGroup);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    (*env)->SetObjectField(env, obj, fid, jstr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    // primary group may or may not be part of supplementary groups
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    for (i = 0; i < (long)numGroups; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        if (strcmp(primaryGroup, groups[i]) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            // found primary group in groups array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            pIndex = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    if (numGroups == 0 || (pIndex == 0 && numGroups == 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        // primary group is only group in groups array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            printf("no secondary groups\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        // the groups array is non-empty,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        // and may or may not contain the primary group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        fid = (*env)->GetFieldID(env, cls, "groupIDs", "[Ljava/lang/String;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        if (fid == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            jclass newExcCls =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                (*env)->FindClass(env, "java/lang/IllegalArgumentException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            if (newExcCls == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                systemError = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                goto out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            (*env)->ThrowNew(env, newExcCls, "invalid field: groupIDs");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        stringClass = (*env)->FindClass(env, "java/lang/String");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        if (stringClass == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            goto out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        if (pIndex == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            // primary group not in groups array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            jgroups = (*env)->NewObjectArray(env, numGroups, stringClass, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            // primary group in groups array -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            // allocate one less array entry and do not add into new array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            jgroups = (*env)->NewObjectArray(env, numGroups-1, stringClass, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        for (i = 0, j = 0; i < (long)numGroups; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            if (pIndex == i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                // continue if equal to primary group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            jstr = (*env)->NewStringUTF(env, groups[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            (*env)->SetObjectArrayElement(env, jgroups, j++, jstr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        (*env)->SetObjectField(env, obj, fid, jgroups);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
out:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    if (userName != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        HeapFree(GetProcessHeap(), 0, userName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    if (domainName != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        HeapFree(GetProcessHeap(), 0, domainName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    if (userSid != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        HeapFree(GetProcessHeap(), 0, userSid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    if (domainSid != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        HeapFree(GetProcessHeap(), 0, domainSid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    if (primaryGroup != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        HeapFree(GetProcessHeap(), 0, primaryGroup);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    if (groups != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        for (i = 0; i < (long)numGroups; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            if (groups[i] != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                HeapFree(GetProcessHeap(), 0, groups[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        HeapFree(GetProcessHeap(), 0, groups);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
10429
d274e775b258 7049079: NTSYSTEM CLASS IS LEAKING WINDOWS TOKENS
coffeys
parents: 5506
diff changeset
   265
    CloseHandle(tokenHandle);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    if (systemError && debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        printf("  [getCurrent] System Error: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        printf("unable to find IllegalArgumentException class\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
BOOL getToken(PHANDLE tokenHandle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    // first try the thread token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    if (OpenThreadToken(GetCurrentThread(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                        TOKEN_READ,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                        FALSE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                        tokenHandle) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            printf("  [getToken] OpenThreadToken error [%d]: ", GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            DisplayErrorText(GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        // next try the process token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        if (OpenProcessToken(GetCurrentProcess(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                        TOKEN_READ,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                        tokenHandle) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                printf("  [getToken] OpenProcessToken error [%d]: ",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                        GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                DisplayErrorText(GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        printf("  [getToken] got user access token\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    return TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
BOOL getUser(HANDLE tokenHandle, LPTSTR *userName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        LPTSTR *domainName, LPTSTR *userSid, LPTSTR *domainSid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    BOOL error = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    DWORD bufSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    DWORD buf2Size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    DWORD retBufSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    PTOKEN_USER tokenUserInfo = NULL;   // getTokenInformation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    SID_NAME_USE nameUse;               // LookupAccountSid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    PSID dSid = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    LPTSTR domainSidName = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    // get token information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    GetTokenInformation(tokenHandle,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                        TokenUser,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                        NULL,   // TokenInformation - if NULL get buffer size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                        0,      // since TokenInformation is NULL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                        &bufSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    tokenUserInfo = (PTOKEN_USER)HeapAlloc(GetProcessHeap(), 0, bufSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    if (GetTokenInformation(tokenHandle,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                        TokenUser,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                        tokenUserInfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                        bufSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                        &retBufSize) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            printf("  [getUser] GetTokenInformation error [%d]: ",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            DisplayErrorText(GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        error = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        goto out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        printf("  [getUser] Got TokenUser info\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    // get userName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    bufSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    buf2Size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    LookupAccountSid(NULL,      // local host
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                tokenUserInfo->User.Sid,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                &bufSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                &buf2Size,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                &nameUse);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    *userName = (LPTSTR)HeapAlloc(GetProcessHeap(), 0, bufSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    *domainName = (LPTSTR)HeapAlloc(GetProcessHeap(), 0, buf2Size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    if (LookupAccountSid(NULL,  // local host
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                tokenUserInfo->User.Sid,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                *userName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                &bufSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                *domainName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                &buf2Size,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                &nameUse) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            printf("  [getUser] LookupAccountSid error [%d]: ",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            DisplayErrorText(GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        error = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        goto out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        printf("  [getUser] userName: %s, domainName = %s\n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                *userName, *domainName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    bufSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    getTextualSid(tokenUserInfo->User.Sid, NULL, &bufSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    *userSid = (LPTSTR)HeapAlloc(GetProcessHeap(), 0, bufSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    getTextualSid(tokenUserInfo->User.Sid, *userSid, &bufSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        printf("  [getUser] userSid: %s\n", *userSid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    // get domainSid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    bufSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    buf2Size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    LookupAccountName(NULL,     // local host
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                *domainName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                &bufSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                &buf2Size,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                &nameUse);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    dSid = (PSID)HeapAlloc(GetProcessHeap(), 0, bufSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    domainSidName = (LPTSTR)HeapAlloc(GetProcessHeap(), 0, buf2Size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    if (LookupAccountName(NULL, // local host
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                *domainName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                dSid,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                &bufSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                domainSidName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                &buf2Size,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                &nameUse) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            printf("  [getUser] LookupAccountName error [%d]: ",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            DisplayErrorText(GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        // ok not to have a domain SID (no error)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        goto out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    bufSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    getTextualSid(dSid, NULL, &bufSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    *domainSid = (LPTSTR)HeapAlloc(GetProcessHeap(), 0, bufSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    getTextualSid(dSid, *domainSid, &bufSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        printf("  [getUser] domainSid: %s\n", *domainSid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
out:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    if (tokenUserInfo != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        HeapFree(GetProcessHeap(), 0, tokenUserInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    if (dSid != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        HeapFree(GetProcessHeap(), 0, dSid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    if (domainSidName != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        HeapFree(GetProcessHeap(), 0, domainSidName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    if (error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    return TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
BOOL getPrimaryGroup(HANDLE tokenHandle, LPTSTR *primaryGroup) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    BOOL error = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    DWORD bufSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    DWORD retBufSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    PTOKEN_PRIMARY_GROUP tokenGroupInfo = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    // get token information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    GetTokenInformation(tokenHandle,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                        TokenPrimaryGroup,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                        NULL,   // TokenInformation - if NULL get buffer size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                        0,      // since TokenInformation is NULL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                        &bufSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    tokenGroupInfo = (PTOKEN_PRIMARY_GROUP)HeapAlloc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                        (GetProcessHeap(), 0, bufSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    if (GetTokenInformation(tokenHandle,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                        TokenPrimaryGroup,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                        tokenGroupInfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                        bufSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                        &retBufSize) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            printf("  [getPrimaryGroup] GetTokenInformation error [%d]: ",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            DisplayErrorText(GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        error = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        goto out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        printf("  [getPrimaryGroup] Got TokenPrimaryGroup info\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    bufSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    getTextualSid(tokenGroupInfo->PrimaryGroup, NULL, &bufSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    *primaryGroup = (LPTSTR)HeapAlloc(GetProcessHeap(), 0, bufSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    getTextualSid(tokenGroupInfo->PrimaryGroup, *primaryGroup, &bufSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        printf("  [getPrimaryGroup] primaryGroup: %s\n", *primaryGroup);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
out:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    if (tokenGroupInfo != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        HeapFree(GetProcessHeap(), 0, tokenGroupInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    if (error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    return TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
BOOL getGroups(HANDLE tokenHandle, PDWORD numGroups, LPTSTR **groups) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    BOOL error = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    DWORD bufSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    DWORD retBufSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    long i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    PTOKEN_GROUPS tokenGroupInfo = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    // get token information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    GetTokenInformation(tokenHandle,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                        TokenGroups,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                        NULL,   // TokenInformation - if NULL get buffer size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                        0,      // since TokenInformation is NULL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                        &bufSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    tokenGroupInfo = (PTOKEN_GROUPS)HeapAlloc(GetProcessHeap(), 0, bufSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    if (GetTokenInformation(tokenHandle,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                        TokenGroups,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                        tokenGroupInfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                        bufSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                        &retBufSize) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            printf("  [getGroups] GetTokenInformation error [%d]: ",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            DisplayErrorText(GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        error = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        goto out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        printf("  [getGroups] Got TokenGroups info\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    if (tokenGroupInfo->GroupCount == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        // no groups
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        goto out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    // return group info
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    *numGroups = tokenGroupInfo->GroupCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    *groups = (LPTSTR *)HeapAlloc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                (GetProcessHeap(), 0, (*numGroups) * sizeof(LPTSTR));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    for (i = 0; i < (long)*numGroups; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        bufSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        getTextualSid(tokenGroupInfo->Groups[i].Sid, NULL, &bufSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        (*groups)[i] = (LPTSTR)HeapAlloc(GetProcessHeap(), 0, bufSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        getTextualSid(tokenGroupInfo->Groups[i].Sid, (*groups)[i], &bufSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            printf("  [getGroups] group %d: %s\n", i, (*groups)[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
out:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    if (tokenGroupInfo != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        HeapFree(GetProcessHeap(), 0, tokenGroupInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    if (error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    return TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
BOOL getImpersonationToken(PHANDLE impersonationToken) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    HANDLE dupToken;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    if (OpenThreadToken(GetCurrentThread(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                        TOKEN_DUPLICATE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                        FALSE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                        &dupToken) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        if (OpenProcessToken(GetCurrentProcess(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                                TOKEN_DUPLICATE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                                &dupToken) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                printf
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                    ("  [getImpersonationToken] OpenProcessToken error [%d]: ",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                    GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                DisplayErrorText(GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    if (DuplicateToken(dupToken,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                        SecurityImpersonation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                        impersonationToken) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
            printf("  [getImpersonationToken] DuplicateToken error [%d]: ",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            DisplayErrorText(GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    }
10429
d274e775b258 7049079: NTSYSTEM CLASS IS LEAKING WINDOWS TOKENS
coffeys
parents: 5506
diff changeset
   589
    CloseHandle(dupToken);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        printf("  [getImpersonationToken] token = %d\n", *impersonationToken);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    return TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
BOOL getTextualSid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    (PSID pSid,                 // binary SID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    LPTSTR TextualSid,          // buffer for Textual representation of SID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    LPDWORD lpdwBufferLen) {    // required/provided TextualSid buffersize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    PSID_IDENTIFIER_AUTHORITY psia;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    DWORD dwSubAuthorities;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    DWORD dwSidRev=SID_REVISION;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    DWORD dwCounter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    DWORD dwSidSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    // Validate the binary SID.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    if(!IsValidSid(pSid)) return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    // Get the identifier authority value from the SID.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    psia = GetSidIdentifierAuthority(pSid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    // Get the number of subauthorities in the SID.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    dwSubAuthorities = *GetSidSubAuthorityCount(pSid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    // Compute the buffer length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    // S-SID_REVISION- + IdentifierAuthority- + subauthorities- + NULL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    dwSidSize=(15 + 12 + (12 * dwSubAuthorities) + 1) * sizeof(TCHAR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    // Check input buffer length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    // If too small, indicate the proper size and set last error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    if (*lpdwBufferLen < dwSidSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        *lpdwBufferLen = dwSidSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        SetLastError(ERROR_INSUFFICIENT_BUFFER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    // Add 'S' prefix and revision number to the string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    dwSidSize=wsprintf(TextualSid, TEXT("S-%lu-"), dwSidRev );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    // Add SID identifier authority to the string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    if ((psia->Value[0] != 0) || (psia->Value[1] != 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        dwSidSize+=wsprintf(TextualSid + lstrlen(TextualSid),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                TEXT("0x%02hx%02hx%02hx%02hx%02hx%02hx"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                (USHORT)psia->Value[0],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                (USHORT)psia->Value[1],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                (USHORT)psia->Value[2],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                (USHORT)psia->Value[3],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                (USHORT)psia->Value[4],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                (USHORT)psia->Value[5]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        dwSidSize+=wsprintf(TextualSid + lstrlen(TextualSid),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                TEXT("%lu"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                (ULONG)(psia->Value[5]  )   +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                (ULONG)(psia->Value[4] <<  8)   +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                (ULONG)(psia->Value[3] << 16)   +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                (ULONG)(psia->Value[2] << 24)   );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    // Add SID subauthorities to the string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    for (dwCounter=0 ; dwCounter < dwSubAuthorities ; dwCounter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        dwSidSize+=wsprintf(TextualSid + dwSidSize, TEXT("-%lu"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                *GetSidSubAuthority(pSid, dwCounter) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    return TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
void DisplayErrorText(DWORD dwLastError) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    HMODULE hModule = NULL; // default to system source
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    LPSTR MessageBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
    DWORD dwBufferLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    DWORD dwFormatFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                        FORMAT_MESSAGE_IGNORE_INSERTS |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                        FORMAT_MESSAGE_FROM_SYSTEM ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    // If dwLastError is in the network range,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    //  load the message source.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    if(dwLastError >= NERR_BASE && dwLastError <= MAX_NERR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        hModule = LoadLibraryEx(TEXT("netmsg.dll"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                                NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                                LOAD_LIBRARY_AS_DATAFILE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        if(hModule != NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            dwFormatFlags |= FORMAT_MESSAGE_FROM_HMODULE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
    // Call FormatMessage() to allow for message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    //  text to be acquired from the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    //  or from the supplied module handle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
    if(dwBufferLength = FormatMessageA(dwFormatFlags,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                hModule, // module to get message from (NULL == system)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                dwLastError,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // default language
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                (LPSTR) &MessageBuffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                NULL)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        DWORD dwBytesWritten;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        // Output message string on stderr.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        WriteFile(GetStdHandle(STD_ERROR_HANDLE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                MessageBuffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                dwBufferLength,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                &dwBytesWritten,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        // Free the buffer allocated by the system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        LocalFree(MessageBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    // If we loaded a message source, unload it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
    if(hModule != NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        FreeLibrary(hModule);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
 * 1. comment out first two #includes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
 * 2. set 'debug' to TRUE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
 * 3. comment out 'getCurrent'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
 * 4. uncomment 'main'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
 * 5. cc -c nt.c
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
 * 6. link nt.obj user32.lib advapi32.lib /out:nt.exe
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
void main(int argc, char *argv[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
    long i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
    HANDLE tokenHandle = INVALID_HANDLE_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    LPTSTR userName = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
    LPTSTR userSid = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    LPTSTR domainName = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    LPTSTR domainSid = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
    LPTSTR primaryGroup = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
    DWORD numGroups = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
    LPTSTR *groups = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
    HANDLE impersonationToken = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
    printf("getting access token\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
    if (getToken(&tokenHandle) == FALSE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    printf("getting user info\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    if (getUser
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        (tokenHandle, &userName, &domainName, &userSid, &domainSid) == FALSE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
    printf("getting primary group\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
    if (getPrimaryGroup(tokenHandle, &primaryGroup) == FALSE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
    printf("getting supplementary groups\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
    if (getGroups(tokenHandle, &numGroups, &groups) == FALSE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
    printf("getting impersonation token\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
    if (getImpersonationToken(&impersonationToken) == FALSE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
    printf("userName = %s, userSid = %s, domainName = %s, domainSid = %s\n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        userName, userSid, domainName, domainSid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
    printf("primaryGroup = %s\n", primaryGroup);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    for (i = 0; i < numGroups; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        printf("Group[%d] = %s\n", i, groups[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
    printf("impersonationToken = %ld\n", impersonationToken);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
    if (userName != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        HeapFree(GetProcessHeap(), 0, userName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
    if (userSid != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        HeapFree(GetProcessHeap(), 0, userSid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
    if (domainName != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        HeapFree(GetProcessHeap(), 0, domainName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
    if (domainSid != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        HeapFree(GetProcessHeap(), 0, domainSid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
    if (primaryGroup != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        HeapFree(GetProcessHeap(), 0, primaryGroup);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
    if (groups != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        for (i = 0; i < numGroups; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            if (groups[i] != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                HeapFree(GetProcessHeap(), 0, groups[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        HeapFree(GetProcessHeap(), 0, groups);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
    }
10429
d274e775b258 7049079: NTSYSTEM CLASS IS LEAKING WINDOWS TOKENS
coffeys
parents: 5506
diff changeset
   800
    CloseHandle(impersonationToken);
d274e775b258 7049079: NTSYSTEM CLASS IS LEAKING WINDOWS TOKENS
coffeys
parents: 5506
diff changeset
   801
    CloseHandle(tokenHandle);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
 * extra main method for testing debug printing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
void main(int argc, char *argv[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
    if(argc != 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        fprintf(stderr,"Usage: %s <error number>\n", argv[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    DisplayErrorText(atoi(argv[1]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
*/