|
| 1 | +package org.scalasbt.ipcsocket; |
| 2 | + |
| 3 | +import com.sun.jna.platform.win32.WinNT; |
| 4 | +import com.sun.jna.platform.win32.WinNT.SECURITY_DESCRIPTOR; |
| 5 | +import com.sun.jna.platform.win32.WinNT.PSID; |
| 6 | +import com.sun.jna.platform.win32.WinNT.PSIDByReference; |
| 7 | +import com.sun.jna.platform.win32.WinNT.HANDLEByReference; |
| 8 | +import com.sun.jna.platform.win32.WinNT.HANDLE; |
| 9 | +import com.sun.jna.platform.win32.WinNT.SID_AND_ATTRIBUTES; |
| 10 | +import com.sun.jna.platform.win32.WinNT.ACL; |
| 11 | +import com.sun.jna.platform.win32.WinNT.ACCESS_ALLOWED_ACE; |
| 12 | +import com.sun.jna.platform.win32.WinDef.DWORD; |
| 13 | +import com.sun.jna.platform.win32.WinBase.SECURITY_ATTRIBUTES; |
| 14 | +import com.sun.jna.platform.win32.Advapi32; |
| 15 | +import com.sun.jna.platform.win32.Advapi32Util; |
| 16 | +import com.sun.jna.platform.win32.Kernel32; |
| 17 | +import com.sun.jna.platform.win32.Kernel32Util; |
| 18 | +import com.sun.jna.platform.win32.W32Errors; |
| 19 | +import com.sun.jna.ptr.IntByReference; |
| 20 | +import com.sun.jna.Native; |
| 21 | + |
| 22 | +public class Win32SecurityLibrary { |
| 23 | + private static final long SE_GROUP_LOGON_ID = 0xC0000000L; |
| 24 | + |
| 25 | + public static SECURITY_ATTRIBUTES createSecurityWithLogonDacl(int accessMask) { |
| 26 | + SECURITY_DESCRIPTOR sd = new SECURITY_DESCRIPTOR(64 * 1024); |
| 27 | + Advapi32.INSTANCE.InitializeSecurityDescriptor(sd, WinNT.SECURITY_DESCRIPTOR_REVISION); |
| 28 | + Native.getLastError(); |
| 29 | + |
| 30 | + ACL pAcl; |
| 31 | + int cbAcl = 0; |
| 32 | + PSIDByReference psid = new PSIDByReference(); |
| 33 | + getLogonSID(psid); |
| 34 | + int sidLength = Advapi32.INSTANCE.GetLengthSid(psid.getValue()); |
| 35 | + cbAcl = Native.getNativeSize(ACL.class, null); |
| 36 | + cbAcl += Native.getNativeSize(ACCESS_ALLOWED_ACE.class, null); |
| 37 | + cbAcl += (sidLength - DWORD.SIZE); |
| 38 | + cbAcl = Advapi32Util.alignOnDWORD(cbAcl); |
| 39 | + pAcl = new ACL(cbAcl); |
| 40 | + Advapi32.INSTANCE.InitializeAcl(pAcl, cbAcl, WinNT.ACL_REVISION); |
| 41 | + Native.getLastError(); |
| 42 | + Advapi32.INSTANCE.AddAccessAllowedAce(pAcl, WinNT.ACL_REVISION, accessMask, psid.getValue()); |
| 43 | + Native.getLastError(); |
| 44 | + Advapi32.INSTANCE.SetSecurityDescriptorDacl(sd, true, pAcl, false); |
| 45 | + Native.getLastError(); |
| 46 | + |
| 47 | + SECURITY_ATTRIBUTES sa = new SECURITY_ATTRIBUTES(); |
| 48 | + sa.dwLength = new DWORD(sd.size()); |
| 49 | + sa.lpSecurityDescriptor = sd.getPointer(); |
| 50 | + sa.bInheritHandle = false; |
| 51 | + |
| 52 | + return sa; |
| 53 | + } |
| 54 | + |
| 55 | + public static void getOwnerSID(PSIDByReference psid) { |
| 56 | + HANDLEByReference phToken = new HANDLEByReference(); |
| 57 | + try { |
| 58 | + Advapi32.INSTANCE.OpenProcessToken(Kernel32.INSTANCE.GetCurrentProcess(), WinNT.TOKEN_QUERY, phToken); |
| 59 | + Native.getLastError(); |
| 60 | + IntByReference tokenInformationLength = new IntByReference(); |
| 61 | + Advapi32.INSTANCE.GetTokenInformation(phToken.getValue(), |
| 62 | + WinNT.TOKEN_INFORMATION_CLASS.TokenOwner, null, 0, tokenInformationLength); |
| 63 | + WinNT.TOKEN_OWNER owner = new WinNT.TOKEN_OWNER(tokenInformationLength.getValue()); |
| 64 | + Advapi32.INSTANCE.GetTokenInformation(phToken.getValue(), |
| 65 | + WinNT.TOKEN_INFORMATION_CLASS.TokenOwner, owner, |
| 66 | + tokenInformationLength.getValue(), tokenInformationLength); |
| 67 | + Native.getLastError(); |
| 68 | + psid.setValue(owner.Owner); |
| 69 | + } finally { |
| 70 | + Kernel32Util.closeHandleRef(phToken); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + public static void getLogonSID(PSIDByReference psid) { |
| 75 | + HANDLEByReference phToken = new HANDLEByReference(); |
| 76 | + try { |
| 77 | + Advapi32.INSTANCE.OpenProcessToken(Kernel32.INSTANCE.GetCurrentProcess(), WinNT.TOKEN_QUERY, phToken); |
| 78 | + Native.getLastError(); |
| 79 | + IntByReference tokenInformationLength = new IntByReference(); |
| 80 | + Advapi32.INSTANCE.GetTokenInformation(phToken.getValue(), |
| 81 | + WinNT.TOKEN_INFORMATION_CLASS.TokenGroups, null, 0, tokenInformationLength); |
| 82 | + WinNT.TOKEN_GROUPS groups = new WinNT.TOKEN_GROUPS(tokenInformationLength.getValue()); |
| 83 | + Advapi32.INSTANCE.GetTokenInformation(phToken.getValue(), |
| 84 | + WinNT.TOKEN_INFORMATION_CLASS.TokenGroups, groups, |
| 85 | + tokenInformationLength.getValue(), tokenInformationLength); |
| 86 | + Native.getLastError(); |
| 87 | + |
| 88 | + for (SID_AND_ATTRIBUTES sidAndAttribute: groups.getGroups()) { |
| 89 | + if ((sidAndAttribute.Attributes & SE_GROUP_LOGON_ID) == SE_GROUP_LOGON_ID) { |
| 90 | + psid.setValue(sidAndAttribute.Sid); |
| 91 | + return; |
| 92 | + } |
| 93 | + } |
| 94 | + throw new RuntimeException("LogonSID was not found."); |
| 95 | + } finally { |
| 96 | + Kernel32Util.closeHandleRef(phToken); |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments