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