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