Search in sources :

Example 1 with ACCESS_ACEStructure

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

the class Advapi32Util method getFileSecurity.

public static ACCESS_ACEStructure[] getFileSecurity(String fileName, boolean compact) {
    int infoType = WinNT.DACL_SECURITY_INFORMATION;
    int nLength = 1024;
    boolean repeat = false;
    Memory memory = null;
    do {
        repeat = false;
        memory = new Memory(nLength);
        IntByReference lpnSize = new IntByReference();
        boolean succeded = Advapi32.INSTANCE.GetFileSecurity(fileName, infoType, memory, nLength, lpnSize);
        if (!succeded) {
            int lastError = Kernel32.INSTANCE.GetLastError();
            memory.clear();
            if (W32Errors.ERROR_INSUFFICIENT_BUFFER != lastError) {
                throw new Win32Exception(lastError);
            }
        }
        int lengthNeeded = lpnSize.getValue();
        if (nLength < lengthNeeded) {
            repeat = true;
            nLength = lengthNeeded;
            memory.clear();
        }
    } while (repeat);
    SECURITY_DESCRIPTOR_RELATIVE sdr = new WinNT.SECURITY_DESCRIPTOR_RELATIVE(memory);
    memory.clear();
    ACL dacl = sdr.getDiscretionaryACL();
    ACCESS_ACEStructure[] aceStructures = dacl.getACEStructures();
    if (compact) {
        Map<String, ACCESS_ACEStructure> aceMap = new HashMap<String, ACCESS_ACEStructure>();
        for (ACCESS_ACEStructure aceStructure : aceStructures) {
            boolean inherted = ((aceStructure.AceFlags & WinNT.VALID_INHERIT_FLAGS) != 0);
            String key = aceStructure.getSidString() + "/" + inherted + "/" + aceStructure.getClass().getName();
            ACCESS_ACEStructure aceStructure2 = aceMap.get(key);
            if (aceStructure2 != null) {
                int accessMask = aceStructure2.Mask;
                accessMask = accessMask | aceStructure.Mask;
                aceStructure2.Mask = accessMask;
            } else {
                aceMap.put(key, aceStructure);
            }
        }
        return aceMap.values().toArray(new ACCESS_ACEStructure[aceMap.size()]);
    }
    return aceStructures;
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) HashMap(java.util.HashMap) Memory(com.sun.jna.Memory) SECURITY_DESCRIPTOR_RELATIVE(com.sun.jna.platform.win32.WinNT.SECURITY_DESCRIPTOR_RELATIVE) ACCESS_ACEStructure(com.sun.jna.platform.win32.WinNT.ACCESS_ACEStructure) ACL(com.sun.jna.platform.win32.WinNT.ACL)

Aggregations

Memory (com.sun.jna.Memory)1 ACCESS_ACEStructure (com.sun.jna.platform.win32.WinNT.ACCESS_ACEStructure)1 ACL (com.sun.jna.platform.win32.WinNT.ACL)1 SECURITY_DESCRIPTOR_RELATIVE (com.sun.jna.platform.win32.WinNT.SECURITY_DESCRIPTOR_RELATIVE)1 IntByReference (com.sun.jna.ptr.IntByReference)1 HashMap (java.util.HashMap)1