use of com.sun.jna.platform.win32.WinDef.BOOLByReference in project jna by java-native-access.
the class Advapi32Test method testSetGetSecurityDescriptorDacl.
public void testSetGetSecurityDescriptorDacl() throws IOException {
SECURITY_DESCRIPTOR sd = new SECURITY_DESCRIPTOR(64 * 1024);
assertTrue(Advapi32.INSTANCE.InitializeSecurityDescriptor(sd, WinNT.SECURITY_DESCRIPTOR_REVISION));
ACL pAcl;
int cbAcl = 0;
PSID pSid = new PSID(WinNT.SECURITY_MAX_SID_SIZE);
IntByReference cbSid = new IntByReference(WinNT.SECURITY_MAX_SID_SIZE);
assertTrue("Failed to create well-known SID", Advapi32.INSTANCE.CreateWellKnownSid(WELL_KNOWN_SID_TYPE.WinBuiltinAdministratorsSid, null, pSid, cbSid));
int sidLength = Advapi32.INSTANCE.GetLengthSid(pSid);
cbAcl = Native.getNativeSize(ACL.class, null);
cbAcl += Native.getNativeSize(ACCESS_ALLOWED_ACE.class, null);
cbAcl += (sidLength - DWORD.SIZE);
cbAcl = Advapi32Util.alignOnDWORD(cbAcl);
pAcl = new ACL(cbAcl);
assertTrue(Advapi32.INSTANCE.InitializeAcl(pAcl, cbAcl, WinNT.ACL_REVISION));
assertTrue(Advapi32.INSTANCE.AddAccessAllowedAce(pAcl, WinNT.ACL_REVISION, WinNT.STANDARD_RIGHTS_ALL, pSid));
assertTrue(Advapi32.INSTANCE.SetSecurityDescriptorDacl(sd, true, pAcl, false));
BOOLByReference lpbDaclPresent = new BOOLByReference();
BOOLByReference lpbDaclDefaulted = new BOOLByReference();
PACLByReference pDacl = new PACLByReference();
assertTrue(Advapi32.INSTANCE.GetSecurityDescriptorDacl(sd, lpbDaclPresent, pDacl, lpbDaclDefaulted));
ACL pAclGet = pDacl.getValue();
assertEquals(new BOOL(true), lpbDaclPresent.getValue());
assertEquals(new BOOL(false), lpbDaclDefaulted.getValue());
assertEquals(1, pAclGet.AceCount);
assertEquals(WinNT.ACL_REVISION, pAclGet.AclRevision);
}
use of com.sun.jna.platform.win32.WinDef.BOOLByReference in project jna by java-native-access.
the class Advapi32Test method testSetGetSecurityDescriptorOwner.
public void testSetGetSecurityDescriptorOwner() {
SECURITY_DESCRIPTOR sd = new SECURITY_DESCRIPTOR(64 * 1024);
assertTrue(Advapi32.INSTANCE.InitializeSecurityDescriptor(sd, WinNT.SECURITY_DESCRIPTOR_REVISION));
PSID pSidPut = new PSID(WinNT.SECURITY_MAX_SID_SIZE);
IntByReference cbSid = new IntByReference(WinNT.SECURITY_MAX_SID_SIZE);
assertTrue("Failed to create well-known SID", Advapi32.INSTANCE.CreateWellKnownSid(WELL_KNOWN_SID_TYPE.WinBuiltinAdministratorsSid, null, pSidPut, cbSid));
assertTrue(Advapi32.INSTANCE.SetSecurityDescriptorOwner(sd, pSidPut, true));
BOOLByReference lpbOwnerDefaulted = new BOOLByReference();
PSIDByReference prSd = new PSIDByReference();
assertTrue(Advapi32.INSTANCE.GetSecurityDescriptorOwner(sd, prSd, lpbOwnerDefaulted));
PSID pSidGet = prSd.getValue();
assertTrue(Advapi32.INSTANCE.EqualSid(pSidPut, pSidGet));
}
use of com.sun.jna.platform.win32.WinDef.BOOLByReference in project jna by java-native-access.
the class Advapi32Test method testSetGetSecurityDescriptorGroup.
public void testSetGetSecurityDescriptorGroup() {
SECURITY_DESCRIPTOR sd = new SECURITY_DESCRIPTOR(64 * 1024);
assertTrue(Advapi32.INSTANCE.InitializeSecurityDescriptor(sd, WinNT.SECURITY_DESCRIPTOR_REVISION));
PSID pSidPut = new PSID(WinNT.SECURITY_MAX_SID_SIZE);
IntByReference cbSid = new IntByReference(WinNT.SECURITY_MAX_SID_SIZE);
assertTrue("Failed to create well-known SID", Advapi32.INSTANCE.CreateWellKnownSid(WELL_KNOWN_SID_TYPE.WinBuiltinAdministratorsSid, null, pSidPut, cbSid));
assertTrue(Advapi32.INSTANCE.SetSecurityDescriptorGroup(sd, pSidPut, true));
BOOLByReference lpbOwnerDefaulted = new BOOLByReference();
PSIDByReference prSd = new PSIDByReference();
assertTrue(Advapi32.INSTANCE.GetSecurityDescriptorGroup(sd, prSd, lpbOwnerDefaulted));
PSID pSidGet = prSd.getValue();
assertTrue(Advapi32.INSTANCE.EqualSid(pSidPut, pSidGet));
}
use of com.sun.jna.platform.win32.WinDef.BOOLByReference in project jna by java-native-access.
the class Advapi32Util method accessCheck.
/**
* Checks if the current process has the given permission for the file.
* @param file the file to check
* @param permissionToCheck the permission to check for the file
* @return true if has access, otherwise false
*/
public static boolean accessCheck(File file, AccessCheckPermission permissionToCheck) {
Memory securityDescriptorMemoryPointer = getSecurityDescriptorForFile(file.getAbsolutePath().replace('/', '\\'));
HANDLEByReference openedAccessToken = new HANDLEByReference();
HANDLEByReference duplicatedToken = new HANDLEByReference();
Win32Exception err = null;
try {
int desireAccess = TOKEN_IMPERSONATE | TOKEN_QUERY | TOKEN_DUPLICATE | STANDARD_RIGHTS_READ;
HANDLE hProcess = Kernel32.INSTANCE.GetCurrentProcess();
if (!Advapi32.INSTANCE.OpenProcessToken(hProcess, desireAccess, openedAccessToken)) {
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}
if (!Advapi32.INSTANCE.DuplicateToken(openedAccessToken.getValue(), SECURITY_IMPERSONATION_LEVEL.SecurityImpersonation, duplicatedToken)) {
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}
GENERIC_MAPPING mapping = new GENERIC_MAPPING();
mapping.genericRead = new DWORD(FILE_GENERIC_READ);
mapping.genericWrite = new DWORD(FILE_GENERIC_WRITE);
mapping.genericExecute = new DWORD(FILE_GENERIC_EXECUTE);
mapping.genericAll = new DWORD(FILE_ALL_ACCESS);
DWORDByReference rights = new DWORDByReference(new DWORD(permissionToCheck.getCode()));
Advapi32.INSTANCE.MapGenericMask(rights, mapping);
PRIVILEGE_SET privileges = new PRIVILEGE_SET(1);
privileges.PrivilegeCount = new DWORD(0);
DWORDByReference privilegeLength = new DWORDByReference(new DWORD(privileges.size()));
DWORDByReference grantedAccess = new DWORDByReference();
BOOLByReference result = new BOOLByReference();
if (!Advapi32.INSTANCE.AccessCheck(securityDescriptorMemoryPointer, duplicatedToken.getValue(), rights.getValue(), mapping, privileges, privilegeLength, grantedAccess, result)) {
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}
return result.getValue().booleanValue();
} catch (Win32Exception e) {
err = e;
// re-throw so finally block executed
throw err;
} finally {
try {
Kernel32Util.closeHandleRefs(openedAccessToken, duplicatedToken);
} catch (Win32Exception e) {
if (err == null) {
err = e;
} else {
err.addSuppressed(e);
}
}
if (securityDescriptorMemoryPointer != null) {
securityDescriptorMemoryPointer.clear();
}
if (err != null) {
throw err;
}
}
}
use of com.sun.jna.platform.win32.WinDef.BOOLByReference in project jna by java-native-access.
the class Rasapi32Test method testRasGetEntryDialParams.
public void testRasGetEntryDialParams() {
RASDIALPARAMS.ByReference rasDialParams = new RASDIALPARAMS.ByReference();
System.arraycopy(rasDialParams.szEntryName, 0, "TEST".toCharArray(), 0, "TEST".length());
BOOLByReference lpfPassword = new BOOLByReference();
int err = Rasapi32.INSTANCE.RasGetEntryDialParams(null, rasDialParams, lpfPassword);
assertEquals(623, err);
}
Aggregations