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