use of com.sun.jna.platform.win32.Advapi32 in project jna by java-native-access.
the class Advapi32Test method testOpenService.
public void testOpenService() {
assertNull(Advapi32.INSTANCE.OpenService(null, "eventlog", Winsvc.SERVICE_QUERY_CONFIG));
assertEquals(W32Errors.ERROR_INVALID_HANDLE, Kernel32.INSTANCE.GetLastError());
SC_HANDLE scmHandle = Advapi32.INSTANCE.OpenSCManager(null, null, Winsvc.SC_MANAGER_CONNECT);
assertNotNull(scmHandle);
SC_HANDLE serviceHandle = Advapi32.INSTANCE.OpenService(scmHandle, "eventlog", Winsvc.SERVICE_QUERY_CONFIG);
assertNotNull(serviceHandle);
assertTrue(Advapi32.INSTANCE.CloseServiceHandle(serviceHandle));
assertNull(Advapi32.INSTANCE.OpenService(scmHandle, "slashesArentValidChars/", Winsvc.SERVICE_QUERY_CONFIG));
assertEquals(W32Errors.ERROR_INVALID_NAME, Kernel32.INSTANCE.GetLastError());
assertNull(Advapi32.INSTANCE.OpenService(scmHandle, "serviceDoesNotExist", Winsvc.SERVICE_QUERY_CONFIG));
assertEquals(W32Errors.ERROR_SERVICE_DOES_NOT_EXIST, Kernel32.INSTANCE.GetLastError());
assertTrue(Advapi32.INSTANCE.CloseServiceHandle(scmHandle));
}
use of com.sun.jna.platform.win32.Advapi32 in project jna by java-native-access.
the class Advapi32Test method testEncryptionDisable.
public void testEncryptionDisable() throws Exception {
DWORDByReference lpStatus = new DWORDByReference();
// create a temp dir
String filePath = System.getProperty("java.io.tmpdir") + File.separator + System.nanoTime();
String DirPath = filePath;
File dir = new File(filePath);
dir.mkdir();
// check status
assertTrue(Advapi32.INSTANCE.FileEncryptionStatus(DirPath, lpStatus));
assertEquals(FILE_ENCRYPTABLE, lpStatus.getValue().intValue());
// disable encryption
assertTrue(Advapi32.INSTANCE.EncryptionDisable(DirPath, true));
assertTrue(Advapi32.INSTANCE.FileEncryptionStatus(DirPath, lpStatus));
assertEquals(FILE_DIR_DISALOWED, lpStatus.getValue().intValue());
// enable encryption
assertTrue(Advapi32.INSTANCE.EncryptionDisable(DirPath, false));
assertTrue(Advapi32.INSTANCE.FileEncryptionStatus(DirPath, lpStatus));
assertEquals(FILE_ENCRYPTABLE, lpStatus.getValue().intValue());
// clean up
for (File file : dir.listFiles()) {
file.delete();
}
dir.delete();
}
use of com.sun.jna.platform.win32.Advapi32 in project jna by java-native-access.
the class Advapi32Test method testGetNamedSecurityInfoForFileWithSACL.
public void testGetNamedSecurityInfoForFileWithSACL() throws Exception {
boolean impersontating = false;
WinNT.LUID pLuid = new WinNT.LUID();
assertTrue(Advapi32.INSTANCE.LookupPrivilegeValue(null, SE_SECURITY_NAME, pLuid));
HANDLEByReference phToken = new HANDLEByReference();
HANDLEByReference phTokenDuplicate = new HANDLEByReference();
try {
// open thread or process token, elevate
if (!Advapi32.INSTANCE.OpenThreadToken(Kernel32.INSTANCE.GetCurrentThread(), TOKEN_ADJUST_PRIVILEGES, false, phToken)) {
assertEquals(W32Errors.ERROR_NO_TOKEN, Kernel32.INSTANCE.GetLastError());
// OpenThreadToken may fail with W32Errors.ERROR_NO_TOKEN if current thread is anonymous. When this happens,
// we need to open the process token to duplicate it, then set our thread token.
assertTrue(Advapi32.INSTANCE.OpenProcessToken(Kernel32.INSTANCE.GetCurrentProcess(), TOKEN_DUPLICATE, phToken));
// Process token opened, now duplicate
assertTrue(Advapi32.INSTANCE.DuplicateTokenEx(phToken.getValue(), TOKEN_ADJUST_PRIVILEGES | TOKEN_IMPERSONATE, null, SECURITY_IMPERSONATION_LEVEL.SecurityImpersonation, TOKEN_TYPE.TokenImpersonation, phTokenDuplicate));
// And set thread token.
assertTrue(Advapi32.INSTANCE.SetThreadToken(null, phTokenDuplicate.getValue()));
impersontating = true;
}
// Which token to adjust depends on whether we had to impersonate or not.
HANDLE tokenAdjust = impersontating ? phTokenDuplicate.getValue() : phToken.getValue();
WinNT.TOKEN_PRIVILEGES tp = new WinNT.TOKEN_PRIVILEGES(1);
tp.Privileges[0] = new WinNT.LUID_AND_ATTRIBUTES(pLuid, new DWORD(WinNT.SE_PRIVILEGE_ENABLED));
assertTrue(Advapi32.INSTANCE.AdjustTokenPrivileges(tokenAdjust, false, tp, 0, null, null));
int infoType = OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION;
PointerByReference ppsidOwner = new PointerByReference();
PointerByReference ppsidGroup = new PointerByReference();
PointerByReference ppDacl = new PointerByReference();
PointerByReference ppSacl = new PointerByReference();
PointerByReference ppSecurityDescriptor = new PointerByReference();
File file = createTempFile();
String filePath = file.getAbsolutePath();
try {
try {
assertEquals("GetNamedSecurityInfo(" + filePath + ")", 0, Advapi32.INSTANCE.GetNamedSecurityInfo(filePath, AccCtrl.SE_OBJECT_TYPE.SE_FILE_OBJECT, infoType, ppsidOwner, ppsidGroup, ppDacl, ppSacl, ppSecurityDescriptor));
} finally {
file.delete();
}
} finally {
Kernel32Util.freeLocalMemory(ppSecurityDescriptor.getValue());
}
if (impersontating) {
Advapi32.INSTANCE.SetThreadToken(null, null);
} else {
tp.Privileges[0] = new WinNT.LUID_AND_ATTRIBUTES(pLuid, new DWORD(0));
Advapi32.INSTANCE.AdjustTokenPrivileges(tokenAdjust, false, tp, 0, null, null);
}
} finally {
Kernel32Util.closeHandleRefs(phToken, phTokenDuplicate);
}
}
use of com.sun.jna.platform.win32.Advapi32 in project jna by java-native-access.
the class Advapi32Test method testDecryptFile.
public void testDecryptFile() throws Exception {
// create an encrypted file
File file = createTempFile();
String lpFileName = file.getAbsolutePath();
assertTrue(Advapi32.INSTANCE.EncryptFile(lpFileName));
// decrypt a read only file
file.setWritable(false);
assertFalse(Advapi32.INSTANCE.DecryptFile(lpFileName, new DWORD(0)));
assertEquals(WinError.ERROR_FILE_READ_ONLY, Kernel32.INSTANCE.GetLastError());
// decrypt
file.setWritable(true);
assertTrue(Advapi32.INSTANCE.DecryptFile(lpFileName, new DWORD(0)));
file.delete();
}
use of com.sun.jna.platform.win32.Advapi32 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));
}
Aggregations