Search in sources :

Example 1 with SECURITY_DESCRIPTOR_RELATIVE

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

the class Advapi32Test method testMakeAbsoluteSD.

public void testMakeAbsoluteSD() throws Exception {
    SECURITY_DESCRIPTOR absolute = new SECURITY_DESCRIPTOR(64 * 1024);
    // Get a SD in self relative form
    int infoType = OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION;
    PointerByReference relativeByReference = new PointerByReference();
    File file = createTempFile();
    try {
        try {
            assertEquals("GetNamedSecurityInfo(" + file + ")", Advapi32.INSTANCE.GetNamedSecurityInfo(file.getAbsolutePath(), AccCtrl.SE_OBJECT_TYPE.SE_FILE_OBJECT, infoType, null, null, null, null, relativeByReference), 0);
            SECURITY_DESCRIPTOR_RELATIVE relative = new SECURITY_DESCRIPTOR_RELATIVE(relativeByReference.getValue());
            PSID pOwner = new PSID(WinNT.SECURITY_MAX_SID_SIZE);
            PSID pGroup = new PSID(WinNT.SECURITY_MAX_SID_SIZE);
            ACL pDacl = new ACL(ACL.MAX_ACL_SIZE);
            ACL pSacl = new ACL(ACL.MAX_ACL_SIZE);
            IntByReference lpdwBufferLength = new IntByReference(absolute.size());
            IntByReference lpdwDaclSize = new IntByReference(ACL.MAX_ACL_SIZE);
            IntByReference lpdwSaclSize = new IntByReference(ACL.MAX_ACL_SIZE);
            IntByReference lpdwOwnerSize = new IntByReference(WinNT.SECURITY_MAX_SID_SIZE);
            IntByReference lpdwPrimaryGroupSize = new IntByReference(WinNT.SECURITY_MAX_SID_SIZE);
            assertTrue(Advapi32.INSTANCE.MakeAbsoluteSD(relative, absolute, lpdwBufferLength, pDacl, lpdwDaclSize, pSacl, lpdwSaclSize, pOwner, lpdwOwnerSize, pGroup, lpdwPrimaryGroupSize));
        } finally {
            file.delete();
        }
    } finally {
        Kernel32Util.freeLocalMemory(relativeByReference.getValue());
    }
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) PointerByReference(com.sun.jna.ptr.PointerByReference) SECURITY_DESCRIPTOR_RELATIVE(com.sun.jna.platform.win32.WinNT.SECURITY_DESCRIPTOR_RELATIVE) SECURITY_DESCRIPTOR(com.sun.jna.platform.win32.WinNT.SECURITY_DESCRIPTOR) ACL(com.sun.jna.platform.win32.WinNT.ACL) PSID(com.sun.jna.platform.win32.WinNT.PSID) File(java.io.File)

Example 2 with SECURITY_DESCRIPTOR_RELATIVE

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

the class Advapi32UtilTest method testGetFileSecurityDescriptor.

public void testGetFileSecurityDescriptor() throws Exception {
    File file = createTempFile();
    SECURITY_DESCRIPTOR_RELATIVE sdr = Advapi32Util.getFileSecurityDescriptor(file, false);
    assertTrue(Advapi32.INSTANCE.IsValidSecurityDescriptor(sdr.getPointer()));
    file.delete();
}
Also used : SECURITY_DESCRIPTOR_RELATIVE(com.sun.jna.platform.win32.WinNT.SECURITY_DESCRIPTOR_RELATIVE) File(java.io.File)

Example 3 with SECURITY_DESCRIPTOR_RELATIVE

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

the class Advapi32UtilTest method testSetFileSecurityDescriptor.

public void testSetFileSecurityDescriptor() throws Exception {
    File file = createTempFile();
    SECURITY_DESCRIPTOR_RELATIVE sdr = Advapi32Util.getFileSecurityDescriptor(file, false);
    Advapi32Util.setFileSecurityDescriptor(file, sdr, false, true, true, false, true, false);
    sdr = Advapi32Util.getFileSecurityDescriptor(file, false);
    assertTrue(Advapi32.INSTANCE.IsValidSecurityDescriptor(sdr.getPointer()));
    file.delete();
}
Also used : SECURITY_DESCRIPTOR_RELATIVE(com.sun.jna.platform.win32.WinNT.SECURITY_DESCRIPTOR_RELATIVE) File(java.io.File)

Example 4 with SECURITY_DESCRIPTOR_RELATIVE

use of com.sun.jna.platform.win32.WinNT.SECURITY_DESCRIPTOR_RELATIVE 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)

Example 5 with SECURITY_DESCRIPTOR_RELATIVE

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

the class Advapi32Util method getFileSecurityDescriptor.

/**
     * Gets a file's Security Descriptor. Convenience wrapper getSecurityDescriptorForObject.
     *
     * @param file
     *         File object containing a path to a file system object.
     * @param getSACL
     *         Get the SACL. See {@link Advapi32#GetNamedSecurityInfo} for process privilege requirements in getting the SACL.
     * @return The file's Security Descriptor in self relative format.
     */
public static SECURITY_DESCRIPTOR_RELATIVE getFileSecurityDescriptor(File file, boolean getSACL) {
    SECURITY_DESCRIPTOR_RELATIVE sdr = null;
    Memory securityDesc = getSecurityDescriptorForObject(file.getAbsolutePath().replaceAll("/", "\\"), AccCtrl.SE_OBJECT_TYPE.SE_FILE_OBJECT, getSACL);
    sdr = new SECURITY_DESCRIPTOR_RELATIVE(securityDesc);
    return sdr;
}
Also used : Memory(com.sun.jna.Memory) SECURITY_DESCRIPTOR_RELATIVE(com.sun.jna.platform.win32.WinNT.SECURITY_DESCRIPTOR_RELATIVE)

Aggregations

SECURITY_DESCRIPTOR_RELATIVE (com.sun.jna.platform.win32.WinNT.SECURITY_DESCRIPTOR_RELATIVE)6 ACL (com.sun.jna.platform.win32.WinNT.ACL)3 IntByReference (com.sun.jna.ptr.IntByReference)3 File (java.io.File)3 Memory (com.sun.jna.Memory)2 PSID (com.sun.jna.platform.win32.WinNT.PSID)2 SECURITY_DESCRIPTOR (com.sun.jna.platform.win32.WinNT.SECURITY_DESCRIPTOR)2 ACCESS_ACEStructure (com.sun.jna.platform.win32.WinNT.ACCESS_ACEStructure)1 PointerByReference (com.sun.jna.ptr.PointerByReference)1 HashMap (java.util.HashMap)1