Search in sources :

Example 11 with Kernel32

use of com.sun.jna.platform.win32.Kernel32 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));
}
Also used : SC_HANDLE(com.sun.jna.platform.win32.Winsvc.SC_HANDLE)

Example 12 with Kernel32

use of com.sun.jna.platform.win32.Kernel32 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);
    }
}
Also used : TOKEN_PRIVILEGES(com.sun.jna.platform.win32.WinNT.TOKEN_PRIVILEGES) TOKEN_PRIVILEGES(com.sun.jna.platform.win32.WinNT.TOKEN_PRIVILEGES) DWORD(com.sun.jna.platform.win32.WinDef.DWORD) PointerByReference(com.sun.jna.ptr.PointerByReference) HANDLEByReference(com.sun.jna.platform.win32.WinNT.HANDLEByReference) SC_HANDLE(com.sun.jna.platform.win32.Winsvc.SC_HANDLE) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE) File(java.io.File)

Example 13 with Kernel32

use of com.sun.jna.platform.win32.Kernel32 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();
}
Also used : DWORD(com.sun.jna.platform.win32.WinDef.DWORD) File(java.io.File)

Example 14 with Kernel32

use of com.sun.jna.platform.win32.Kernel32 in project jna by java-native-access.

the class Advapi32Test method testReadEventLog.

public void testReadEventLog() {
    HANDLE h = Advapi32.INSTANCE.OpenEventLog(null, "Application");
    IntByReference pnBytesRead = new IntByReference();
    IntByReference pnMinNumberOfBytesNeeded = new IntByReference();
    Memory buffer = new Memory(1);
    assertFalse(Advapi32.INSTANCE.ReadEventLog(h, WinNT.EVENTLOG_SEQUENTIAL_READ | WinNT.EVENTLOG_BACKWARDS_READ, 0, buffer, (int) buffer.size(), pnBytesRead, pnMinNumberOfBytesNeeded));
    assertEquals(W32Errors.ERROR_INSUFFICIENT_BUFFER, Kernel32.INSTANCE.GetLastError());
    assertTrue(pnMinNumberOfBytesNeeded.getValue() > 0);
    assertTrue(Advapi32.INSTANCE.CloseEventLog(h));
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) Memory(com.sun.jna.Memory) SC_HANDLE(com.sun.jna.platform.win32.Winsvc.SC_HANDLE) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Example 15 with Kernel32

use of com.sun.jna.platform.win32.Kernel32 in project jna by java-native-access.

the class Advapi32Test method testCreateProcessAsUser.

public void testCreateProcessAsUser() {
    HANDLEByReference hToken = new HANDLEByReference();
    HANDLE processHandle = Kernel32.INSTANCE.GetCurrentProcess();
    assertTrue(Advapi32.INSTANCE.OpenProcessToken(processHandle, WinNT.TOKEN_DUPLICATE | WinNT.TOKEN_QUERY, hToken));
    try {
        assertFalse(Advapi32.INSTANCE.CreateProcessAsUser(hToken.getValue(), null, "InvalidCmdLine.jna", null, null, false, 0, null, null, new WinBase.STARTUPINFO(), new WinBase.PROCESS_INFORMATION()));
        assertEquals(W32Errors.ERROR_FILE_NOT_FOUND, Kernel32.INSTANCE.GetLastError());
    } finally {
        Kernel32Util.closeHandleRef(hToken);
    }
}
Also used : PROCESS_INFORMATION(com.sun.jna.platform.win32.WinBase.PROCESS_INFORMATION) HANDLEByReference(com.sun.jna.platform.win32.WinNT.HANDLEByReference) STARTUPINFO(com.sun.jna.platform.win32.WinBase.STARTUPINFO) SC_HANDLE(com.sun.jna.platform.win32.Winsvc.SC_HANDLE) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Aggregations

HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)74 IntByReference (com.sun.jna.ptr.IntByReference)47 File (java.io.File)33 DWORD (com.sun.jna.platform.win32.WinDef.DWORD)30 SC_HANDLE (com.sun.jna.platform.win32.Winsvc.SC_HANDLE)27 HANDLEByReference (com.sun.jna.platform.win32.WinNT.HANDLEByReference)26 Memory (com.sun.jna.Memory)25 PointerByReference (com.sun.jna.ptr.PointerByReference)14 Pointer (com.sun.jna.Pointer)12 EVT_HANDLE (com.sun.jna.platform.win32.Winevt.EVT_HANDLE)7 Test (org.junit.Test)7 SYSTEMTIME (com.sun.jna.platform.win32.WinBase.SYSTEMTIME)6 ArrayList (java.util.ArrayList)6 FILETIME (com.sun.jna.platform.win32.WinBase.FILETIME)5 HMODULE (com.sun.jna.platform.win32.WinDef.HMODULE)5 PSID (com.sun.jna.platform.win32.WinNT.PSID)5 TOKEN_PRIVILEGES (com.sun.jna.platform.win32.WinNT.TOKEN_PRIVILEGES)5 WString (com.sun.jna.WString)4 DWORDByReference (com.sun.jna.platform.win32.WinDef.DWORDByReference)4 ShortByReference (com.sun.jna.ptr.ShortByReference)4