/* * Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
long i, j = 0;
HANDLE tokenHandle = INVALID_HANDLE_VALUE;
LPTSTR userName = NULL; // user name
LPTSTR userSid = NULL; // user sid
LPTSTR domainName = NULL; // domain name
LPTSTR domainSid = NULL; // domain sid
LPTSTR primaryGroup = NULL; // primary group sid
DWORD numGroups = 0; // num groups
LPTSTR *groups = NULL; // groups array long pIndex = -1; // index of primaryGroup in groups array
// primary group may or may not be part of supplementary groups for (i = 0; i < (long)numGroups; i++) { if (strcmp(primaryGroup, groups[i]) == 0) { // found primary group in groups array
pIndex = i; break;
}
}
if (numGroups == 0 || (pIndex == 0 && numGroups == 1)) { // primary group is only group in groups array
if (debug) {
printf("no secondary groups\n");
}
} else {
// the groups array is non-empty, // and may or may not contain the primary group
stringClass = (*env)->FindClass(env, "java/lang/String"); if (stringClass == NULL) goto cleanup;
if (pIndex == -1) { // primary group not in groups array
jgroups = (*env)->NewObjectArray(env, numGroups, stringClass, 0);
} else { // primary group in groups array - // allocate one less array entry and do not add into new array
jgroups = (*env)->NewObjectArray(env, numGroups-1, stringClass, 0);
} if (jgroups == NULL) goto cleanup;
for (i = 0, j = 0; i < (long)numGroups; i++) { if (pIndex == i) { // continue if equal to primary group continue;
}
jstr = (*env)->NewStringUTF(env, groups[i]); if (jstr == NULL) goto cleanup;
(*env)->SetObjectArrayElement(env, jgroups, j++, jstr);
}
(*env)->SetObjectField(env, obj, fid, jgroups);
}
cleanup: if (userName != NULL) {
HeapFree(GetProcessHeap(), 0, userName);
} if (domainName != NULL) {
HeapFree(GetProcessHeap(), 0, domainName);
} if (userSid != NULL) {
HeapFree(GetProcessHeap(), 0, userSid);
} if (domainSid != NULL) {
HeapFree(GetProcessHeap(), 0, domainSid);
} if (primaryGroup != NULL) {
HeapFree(GetProcessHeap(), 0, primaryGroup);
} if (groups != NULL) { for (i = 0; i < (long)numGroups; i++) { if (groups[i] != NULL) {
HeapFree(GetProcessHeap(), 0, groups[i]);
}
}
HeapFree(GetProcessHeap(), 0, groups);
}
CloseHandle(tokenHandle);
return;
}
BOOL getToken(PHANDLE tokenHandle) {
// first try the thread token if (OpenThreadToken(GetCurrentThread(),
TOKEN_READ, FALSE,
tokenHandle) == 0) { if (debug) {
printf(" [getToken] OpenThreadToken error [%d]: ", GetLastError());
DisplayErrorText(GetLastError());
}
// next try the process token if (OpenProcessToken(GetCurrentProcess(),
TOKEN_READ,
tokenHandle) == 0) { if (debug) {
printf(" [getToken] OpenProcessToken error [%d]: ",
GetLastError());
DisplayErrorText(GetLastError());
} returnFALSE;
}
}
if (debug) {
printf(" [getToken] got user access token\n");
}
// get token information
GetTokenInformation(tokenHandle,
TokenUser,
NULL, // TokenInformation - if NULL get buffer size
0, // since TokenInformation is NULL
&bufSize);
// get token information
GetTokenInformation(tokenHandle,
TokenPrimaryGroup,
NULL, // TokenInformation - if NULL get buffer size
0, // since TokenInformation is NULL
&bufSize);
BOOL error = FALSE;
DWORD bufSize = 0;
DWORD retBufSize = 0; long i = 0;
PTOKEN_GROUPS tokenGroupInfo = NULL;
// get token information
GetTokenInformation(tokenHandle,
TokenGroups,
NULL, // TokenInformation - if NULL get buffer size
0, // since TokenInformation is NULL
&bufSize);
// Check input buffer length. // If too small, indicate the proper size and set last error. if (*lpdwBufferLen < dwSidSize) {
*lpdwBufferLen = dwSidSize;
SetLastError(ERROR_INSUFFICIENT_BUFFER); returnFALSE;
}
// Add 'S' prefix and revision number to the string.
dwSidSize=wsprintf(TextualSid, TEXT("S-%lu-"), dwSidRev );
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.