Search in sources :

Example 1 with GENERIC_MAPPING

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

the class Advapi32Test method testMapGenericAllMask.

public void testMapGenericAllMask() {
    final 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);
    final DWORDByReference rights = new DWORDByReference(new DWORD(GENERIC_ALL));
    Advapi32.INSTANCE.MapGenericMask(rights, mapping);
    assertEquals(FILE_ALL_ACCESS, rights.getValue().intValue());
    assertTrue(GENERIC_ALL != (rights.getValue().intValue() & GENERIC_ALL));
}
Also used : GENERIC_MAPPING(com.sun.jna.platform.win32.WinNT.GENERIC_MAPPING) DWORDByReference(com.sun.jna.platform.win32.WinDef.DWORDByReference) DWORD(com.sun.jna.platform.win32.WinDef.DWORD)

Example 2 with GENERIC_MAPPING

use of com.sun.jna.platform.win32.WinNT.GENERIC_MAPPING 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 3 with GENERIC_MAPPING

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

the class Advapi32Test method testAccessCheck.

public void testAccessCheck() {
    final 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);
    final Memory securityDescriptorMemoryPointer = new Memory(1);
    final PRIVILEGE_SET privileges = new PRIVILEGE_SET(1);
    privileges.PrivilegeCount = new DWORD(0);
    final DWORDByReference privilegeLength = new DWORDByReference(new DWORD(privileges.size()));
    final DWORDByReference grantedAccess = new DWORDByReference();
    final BOOLByReference result = new BOOLByReference();
    final boolean status = Advapi32.INSTANCE.AccessCheck(securityDescriptorMemoryPointer, null, new DWORD(FILE_GENERIC_READ), mapping, privileges, privilegeLength, grantedAccess, result);
    assertFalse(status);
    assertFalse(result.getValue().booleanValue());
    assertEquals(WinError.ERROR_INVALID_HANDLE, Kernel32.INSTANCE.GetLastError());
}
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)

Example 4 with GENERIC_MAPPING

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

the class Advapi32Test method testMapGenericExecuteMask.

public void testMapGenericExecuteMask() {
    final 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);
    final DWORDByReference rights = new DWORDByReference(new DWORD(GENERIC_EXECUTE));
    Advapi32.INSTANCE.MapGenericMask(rights, mapping);
    assertEquals(FILE_GENERIC_EXECUTE, rights.getValue().intValue());
    assertTrue(GENERIC_EXECUTE != (rights.getValue().intValue() & GENERIC_EXECUTE));
}
Also used : GENERIC_MAPPING(com.sun.jna.platform.win32.WinNT.GENERIC_MAPPING) DWORDByReference(com.sun.jna.platform.win32.WinDef.DWORDByReference) DWORD(com.sun.jna.platform.win32.WinDef.DWORD)

Example 5 with GENERIC_MAPPING

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

the class Advapi32Test method testMapGenericReadMask.

public void testMapGenericReadMask() {
    final 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);
    final DWORDByReference rights = new DWORDByReference(new DWORD(GENERIC_READ));
    Advapi32.INSTANCE.MapGenericMask(rights, mapping);
    assertEquals(FILE_GENERIC_READ, rights.getValue().intValue());
    assertTrue(GENERIC_READ != (rights.getValue().intValue() & GENERIC_READ));
}
Also used : GENERIC_MAPPING(com.sun.jna.platform.win32.WinNT.GENERIC_MAPPING) DWORDByReference(com.sun.jna.platform.win32.WinDef.DWORDByReference) DWORD(com.sun.jna.platform.win32.WinDef.DWORD)

Aggregations

DWORD (com.sun.jna.platform.win32.WinDef.DWORD)6 DWORDByReference (com.sun.jna.platform.win32.WinDef.DWORDByReference)6 GENERIC_MAPPING (com.sun.jna.platform.win32.WinNT.GENERIC_MAPPING)6 Memory (com.sun.jna.Memory)2 BOOLByReference (com.sun.jna.platform.win32.WinDef.BOOLByReference)2 PRIVILEGE_SET (com.sun.jna.platform.win32.WinNT.PRIVILEGE_SET)2 HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)1 HANDLEByReference (com.sun.jna.platform.win32.WinNT.HANDLEByReference)1