|
63 | 63 | import com.sun.jna.Memory;
|
64 | 64 | import com.sun.jna.Native;
|
65 | 65 | import com.sun.jna.Pointer;
|
| 66 | +import com.sun.jna.platform.win32.Advapi32Util.Account; |
66 | 67 | import com.sun.jna.platform.win32.WinBase.FE_EXPORT_FUNC;
|
67 | 68 | import com.sun.jna.platform.win32.WinBase.FE_IMPORT_FUNC;
|
68 | 69 | import com.sun.jna.platform.win32.WinBase.FILETIME;
|
|
86 | 87 | import com.sun.jna.platform.win32.WinNT.SECURITY_IMPERSONATION_LEVEL;
|
87 | 88 | import com.sun.jna.platform.win32.WinNT.SID_AND_ATTRIBUTES;
|
88 | 89 | import com.sun.jna.platform.win32.WinNT.SID_NAME_USE;
|
| 90 | +import com.sun.jna.platform.win32.WinNT.TOKEN_PRIMARY_GROUP; |
89 | 91 | import com.sun.jna.platform.win32.WinNT.TOKEN_TYPE;
|
90 | 92 | import com.sun.jna.platform.win32.WinReg.HKEY;
|
91 | 93 | import com.sun.jna.platform.win32.WinReg.HKEYByReference;
|
@@ -475,6 +477,45 @@ public static Account[] getTokenGroups(HANDLE hToken) {
|
475 | 477 | return userGroups.toArray(new Account[0]);
|
476 | 478 | }
|
477 | 479 |
|
| 480 | + /** |
| 481 | + * This function returns the primary group associated with a security token, |
| 482 | + * such as a user token. |
| 483 | + * |
| 484 | + * @param hToken |
| 485 | + * Token. |
| 486 | + * @return Token primary group. |
| 487 | + */ |
| 488 | + public static Account getTokenPrimaryGroup(HANDLE hToken) { |
| 489 | + // get token group information size |
| 490 | + IntByReference tokenInformationLength = new IntByReference(); |
| 491 | + if (Advapi32.INSTANCE.GetTokenInformation(hToken, WinNT.TOKEN_INFORMATION_CLASS.TokenPrimaryGroup, null, 0, |
| 492 | + tokenInformationLength)) { |
| 493 | + throw new RuntimeException("Expected GetTokenInformation to fail with ERROR_INSUFFICIENT_BUFFER"); |
| 494 | + } |
| 495 | + int rc = Kernel32.INSTANCE.GetLastError(); |
| 496 | + if (rc != W32Errors.ERROR_INSUFFICIENT_BUFFER) { |
| 497 | + throw new Win32Exception(rc); |
| 498 | + } |
| 499 | + // get token group information |
| 500 | + WinNT.TOKEN_PRIMARY_GROUP primaryGroup = new WinNT.TOKEN_PRIMARY_GROUP(tokenInformationLength.getValue()); |
| 501 | + if (!Advapi32.INSTANCE.GetTokenInformation(hToken, WinNT.TOKEN_INFORMATION_CLASS.TokenPrimaryGroup, |
| 502 | + primaryGroup, tokenInformationLength.getValue(), tokenInformationLength)) { |
| 503 | + throw new Win32Exception(Kernel32.INSTANCE.GetLastError()); |
| 504 | + } |
| 505 | + Account group; |
| 506 | + try { |
| 507 | + group = Advapi32Util.getAccountBySid(primaryGroup.PrimaryGroup); |
| 508 | + } catch (Exception e) { |
| 509 | + group = new Account(); |
| 510 | + group.sid = primaryGroup.PrimaryGroup.getBytes(); |
| 511 | + group.sidString = Advapi32Util.convertSidToStringSid(primaryGroup.PrimaryGroup); |
| 512 | + group.name = group.sidString; |
| 513 | + group.fqn = group.sidString; |
| 514 | + group.accountType = SID_NAME_USE.SidTypeGroup; |
| 515 | + } |
| 516 | + return group; |
| 517 | + } |
| 518 | + |
478 | 519 | /**
|
479 | 520 | * This function returns the information about the user who owns a security
|
480 | 521 | * token,
|
|
0 commit comments