Search in sources :

Example 16 with Memory

use of com.sun.jna.Memory 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;
        }
    }
}
Also used : BOOLByReference(com.sun.jna.platform.win32.WinDef.BOOLByReference) PRIVILEGE_SET(com.sun.jna.platform.win32.WinNT.PRIVILEGE_SET) GENERIC_MAPPING(com.sun.jna.platform.win32.WinNT.GENERIC_MAPPING) DWORDByReference(com.sun.jna.platform.win32.WinDef.DWORDByReference) Memory(com.sun.jna.Memory) DWORD(com.sun.jna.platform.win32.WinDef.DWORD) HANDLEByReference(com.sun.jna.platform.win32.WinNT.HANDLEByReference) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Example 17 with Memory

use of com.sun.jna.Memory in project jna by java-native-access.

the class Advapi32Util method getSecurityDescriptorForFile.

private static Memory getSecurityDescriptorForFile(final String absoluteFilePath) {
    final int infoType = OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION;
    final IntByReference lpnSize = new IntByReference();
    boolean succeeded = Advapi32.INSTANCE.GetFileSecurity(absoluteFilePath, infoType, null, 0, lpnSize);
    if (!succeeded) {
        final int lastError = Kernel32.INSTANCE.GetLastError();
        if (W32Errors.ERROR_INSUFFICIENT_BUFFER != lastError) {
            throw new Win32Exception(lastError);
        }
    }
    final int nLength = lpnSize.getValue();
    final Memory securityDescriptorMemoryPointer = new Memory(nLength);
    succeeded = Advapi32.INSTANCE.GetFileSecurity(absoluteFilePath, infoType, securityDescriptorMemoryPointer, nLength, lpnSize);
    if (!succeeded) {
        securityDescriptorMemoryPointer.clear();
        throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
    }
    return securityDescriptorMemoryPointer;
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) Memory(com.sun.jna.Memory)

Example 18 with Memory

use of com.sun.jna.Memory in project jna by java-native-access.

the class XAttrUtil method encodeString.

protected static Memory encodeString(String s) {
    // create NULL-terminated UTF-8 String
    byte[] bb = s.getBytes(Charset.forName("UTF-8"));
    Memory valueBuffer = new Memory(bb.length + 1);
    valueBuffer.write(0, bb, 0, bb.length);
    valueBuffer.setByte(valueBuffer.size() - 1, (byte) 0);
    return valueBuffer;
}
Also used : Memory(com.sun.jna.Memory)

Example 19 with Memory

use of com.sun.jna.Memory in project jna by java-native-access.

the class DdemlTest method testStringHandling.

@Test
public void testStringHandling() {
    DdeCallback callback = new Ddeml.DdeCallback() {

        public WinDef.PVOID ddeCallback(int wType, int wFmt, Ddeml.HCONV hConv, Ddeml.HSZ hsz1, Ddeml.HSZ hsz2, Ddeml.HDDEDATA hData, BaseTSD.ULONG_PTR lData1, BaseTSD.ULONG_PTR lData2) {
            return new PVOID();
        }
    };
    DWORDByReference pidInst = new DWORDByReference();
    int initResult = Ddeml.INSTANCE.DdeInitialize(pidInst, callback, Ddeml.APPCMD_CLIENTONLY, 0);
    assertEquals(Ddeml.DMLERR_NO_ERROR, initResult);
    HSZ handle = Ddeml.INSTANCE.DdeCreateStringHandle(pidInst.getValue().intValue(), "Test", Ddeml.CP_WINUNICODE);
    assertNotNull(handle);
    // String in DDE can not exceed 255 Chars
    Memory mem = new Memory(256 * 2);
    Ddeml.INSTANCE.DdeQueryString(pidInst.getValue().intValue(), handle, mem, 256, Ddeml.CP_WINUNICODE);
    assertEquals("Test", mem.getWideString(0));
    synchronized (mem) {
    }
    assertTrue(Ddeml.INSTANCE.DdeFreeStringHandle(pidInst.getValue().intValue(), handle));
    // Test overlong creation -- according to documentation this must fail
    StringBuilder testString = new StringBuilder();
    for (int i = 0; i < 30; i++) {
        testString.append("0123456789");
    }
    HSZ handle2 = Ddeml.INSTANCE.DdeCreateStringHandle(pidInst.getValue().intValue(), testString.toString(), Ddeml.CP_WINUNICODE);
    assertNull(handle2);
    boolean uninitResult = Ddeml.INSTANCE.DdeUninitialize(pidInst.getValue().intValue());
    assertTrue(uninitResult);
}
Also used : HDDEDATA(com.sun.jna.platform.win32.Ddeml.HDDEDATA) DWORDByReference(com.sun.jna.platform.win32.WinDef.DWORDByReference) Memory(com.sun.jna.Memory) PVOID(com.sun.jna.platform.win32.WinDef.PVOID) HSZ(com.sun.jna.platform.win32.Ddeml.HSZ) DdeCallback(com.sun.jna.platform.win32.Ddeml.DdeCallback) Test(org.junit.Test)

Example 20 with Memory

use of com.sun.jna.Memory in project jna by java-native-access.

the class DdemlTest method testMemoryHandling.

@Test
public void testMemoryHandling() {
    DdeCallback callback = new Ddeml.DdeCallback() {

        public WinDef.PVOID ddeCallback(int wType, int wFmt, Ddeml.HCONV hConv, Ddeml.HSZ hsz1, Ddeml.HSZ hsz2, Ddeml.HDDEDATA hData, BaseTSD.ULONG_PTR lData1, BaseTSD.ULONG_PTR lData2) {
            return new PVOID();
        }
    };
    DWORDByReference pidInst = new DWORDByReference();
    int initResult = Ddeml.INSTANCE.DdeInitialize(pidInst, callback, Ddeml.APPCMD_CLIENTONLY, 0);
    assertEquals(Ddeml.DMLERR_NO_ERROR, initResult);
    // Acquire dummy handle
    HSZ hsz = Ddeml.INSTANCE.DdeCreateStringHandle(pidInst.getValue().intValue(), "Dummy", Ddeml.CP_WINUNICODE);
    String testStringPart1 = "Hallo ";
    String testStringPart2 = "Welt";
    // Create Handle
    // String in DDE can not exceed 255 Chars
    Memory mem = new Memory(256 * 2);
    mem.setWideString(0, testStringPart1);
    HDDEDATA data = Ddeml.INSTANCE.DdeCreateDataHandle(pidInst.getValue().intValue(), mem, testStringPart1.length() * 2, 0, hsz, WinUser.CF_UNICODETEXT, Ddeml.HDATA_APPOWNED);
    mem.setWideString(0, testStringPart2);
    Ddeml.INSTANCE.DdeAddData(data, mem, (testStringPart2.length() + 1) * 2, testStringPart1.length() * 2);
    DWORDByReference dataSize = new DWORDByReference();
    Pointer resultPointer = Ddeml.INSTANCE.DdeAccessData(data, dataSize);
    assertEquals((testStringPart1.length() + testStringPart2.length() + 1) * 2, dataSize.getValue().intValue());
    assertEquals(testStringPart1 + testStringPart2, resultPointer.getWideString(0));
    boolean result = Ddeml.INSTANCE.DdeUnaccessData(data);
    int readSize = Ddeml.INSTANCE.DdeGetData(data, mem, (int) mem.size(), 0);
    assertEquals((testStringPart1.length() + testStringPart2.length() + 1) * 2, readSize);
    assertEquals(testStringPart1 + testStringPart2, mem.getWideString(0));
    assertTrue(result);
    result = Ddeml.INSTANCE.DdeFreeDataHandle(data);
    assertTrue(result);
    synchronized (mem) {
    }
    result = Ddeml.INSTANCE.DdeUninitialize(pidInst.getValue().intValue());
    assertTrue(result);
}
Also used : DWORDByReference(com.sun.jna.platform.win32.WinDef.DWORDByReference) Memory(com.sun.jna.Memory) Pointer(com.sun.jna.Pointer) PVOID(com.sun.jna.platform.win32.WinDef.PVOID) HSZ(com.sun.jna.platform.win32.Ddeml.HSZ) DdeCallback(com.sun.jna.platform.win32.Ddeml.DdeCallback) HDDEDATA(com.sun.jna.platform.win32.Ddeml.HDDEDATA) Test(org.junit.Test)

Aggregations

Memory (com.sun.jna.Memory)64 IntByReference (com.sun.jna.ptr.IntByReference)33 HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)17 Pointer (com.sun.jna.Pointer)15 File (java.io.File)15 DWORD (com.sun.jna.platform.win32.WinDef.DWORD)9 PointerByReference (com.sun.jna.ptr.PointerByReference)8 DWORDByReference (com.sun.jna.platform.win32.WinDef.DWORDByReference)6 PSID (com.sun.jna.platform.win32.WinNT.PSID)6 Test (org.junit.Test)6 HDDEDATA (com.sun.jna.platform.win32.Ddeml.HDDEDATA)5 HSZ (com.sun.jna.platform.win32.Ddeml.HSZ)5 ACL (com.sun.jna.platform.win32.WinNT.ACL)5 EVT_HANDLE (com.sun.jna.platform.win32.Winevt.EVT_HANDLE)5 ACCESS_ALLOWED_ACE (com.sun.jna.platform.win32.WinNT.ACCESS_ALLOWED_ACE)4 HCONV (com.sun.jna.platform.win32.Ddeml.HCONV)3 ConnectHandler (com.sun.jna.platform.win32.DdemlUtil.ConnectHandler)3 IDdeConnection (com.sun.jna.platform.win32.DdemlUtil.IDdeConnection)3 StandaloneDdeClient (com.sun.jna.platform.win32.DdemlUtil.StandaloneDdeClient)3 WIN32_FIND_DATA (com.sun.jna.platform.win32.WinBase.WIN32_FIND_DATA)3