Search in sources :

Example 6 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 setSecurityDescriptorForObject.

/**
     * Set a self relative security descriptor for the given object type.
     *
     * @param absoluteObjectPath
     *         A pointer to a null-terminated string that specifies the name of the object
     *         from which to retrieve security information. For descriptions of the string
     *         formats for the different object types, see {@link AccCtrl.SE_OBJECT_TYPE}.
     * @param objectType
     *         Object type referred to by the path. See  {@link AccCtrl.SE_OBJECT_TYPE} for valid definitions.
     * @param securityDescriptor
     *         A security descriptor to set.
     * @param setOwner
     *         Set the owner. The owner is extracted from securityDescriptor and must be valid,
     *         otherwise IllegalArgumentException is throw.
     *         See {@link Advapi32#SetNamedSecurityInfo} for process privilege requirements in getting the OWNER.
     * @param setGroup
     *         Set the group. The group is extracted from securityDescriptor and must be valid,
     *         otherwise IllegalArgumentException is throw.
     * @param setDACL
     *         Set the DACL. The DACL is extracted from securityDescriptor and must be valid,
     *         otherwise IllegalArgumentException is throw.
     * @param setSACL
     *         Set the SACL. The SACL is extracted from securityDescriptor and must be valid,
     *         otherwise IllegalArgumentException is throw.
     *          See {@link Advapi32#SetNamedSecurityInfo} for process privilege requirements in getting the SACL.
     * @param setDACLProtectedStatus
     *         Set DACL protected status as contained within securityDescriptor.control.
     * @param setSACLProtectedStatus
     *         Set SACL protected status as contained within securityDescriptor.control.
     */
public static void setSecurityDescriptorForObject(final String absoluteObjectPath, int objectType, SECURITY_DESCRIPTOR_RELATIVE securityDescriptor, boolean setOwner, boolean setGroup, boolean setDACL, boolean setSACL, boolean setDACLProtectedStatus, boolean setSACLProtectedStatus) {
    final PSID psidOwner = securityDescriptor.getOwner();
    final PSID psidGroup = securityDescriptor.getGroup();
    final ACL dacl = securityDescriptor.getDiscretionaryACL();
    final ACL sacl = securityDescriptor.getSystemACL();
    int infoType = 0;
    // Parameter validation and infoType flag setting.
    if (setOwner) {
        if (psidOwner == null)
            throw new IllegalArgumentException("SECURITY_DESCRIPTOR_RELATIVE does not contain owner");
        if (!Advapi32.INSTANCE.IsValidSid(psidOwner))
            throw new IllegalArgumentException("Owner PSID is invalid");
        infoType |= OWNER_SECURITY_INFORMATION;
    }
    if (setGroup) {
        if (psidGroup == null)
            throw new IllegalArgumentException("SECURITY_DESCRIPTOR_RELATIVE does not contain group");
        if (!Advapi32.INSTANCE.IsValidSid(psidGroup))
            throw new IllegalArgumentException("Group PSID is invalid");
        infoType |= GROUP_SECURITY_INFORMATION;
    }
    if (setDACL) {
        if (dacl == null)
            throw new IllegalArgumentException("SECURITY_DESCRIPTOR_RELATIVE does not contain DACL");
        if (!Advapi32.INSTANCE.IsValidAcl(dacl.getPointer()))
            throw new IllegalArgumentException("DACL is invalid");
        infoType |= DACL_SECURITY_INFORMATION;
    }
    if (setSACL) {
        if (sacl == null)
            throw new IllegalArgumentException("SECURITY_DESCRIPTOR_RELATIVE does not contain SACL");
        if (!Advapi32.INSTANCE.IsValidAcl(sacl.getPointer()))
            throw new IllegalArgumentException("SACL is invalid");
        infoType |= SACL_SECURITY_INFORMATION;
    }
    /*
    	 * Control bits SE_DACL_PROTECTED/SE_SACL_PROTECTED indicate the *ACL is protected. The *ACL_SECURITY_INFORMATION flags
    	 * are meta flags for SetNamedSecurityInfo and are not stored in the SD.  If either *ACLProtectedStatus is set,
    	 * get the current status from the securityDescriptor and apply as such, otherwise the ACL remains at its default.
    	*/
    if (setDACLProtectedStatus) {
        if ((securityDescriptor.Control & SE_DACL_PROTECTED) != 0) {
            infoType |= PROTECTED_DACL_SECURITY_INFORMATION;
        } else if ((securityDescriptor.Control & SE_DACL_PROTECTED) == 0) {
            infoType |= UNPROTECTED_DACL_SECURITY_INFORMATION;
        }
    }
    if (setSACLProtectedStatus) {
        if ((securityDescriptor.Control & SE_SACL_PROTECTED) != 0) {
            infoType |= PROTECTED_SACL_SECURITY_INFORMATION;
        } else if ((securityDescriptor.Control & SE_SACL_PROTECTED) == 0) {
            infoType |= UNPROTECTED_SACL_SECURITY_INFORMATION;
        }
    }
    int lastError = Advapi32.INSTANCE.SetNamedSecurityInfo(absoluteObjectPath, objectType, infoType, setOwner ? psidOwner.getPointer() : null, setGroup ? psidGroup.getPointer() : null, setDACL ? dacl.getPointer() : null, setSACL ? sacl.getPointer() : null);
    if (lastError != 0) {
        throw new Win32Exception(lastError);
    }
}
Also used : ACL(com.sun.jna.platform.win32.WinNT.ACL) PSID(com.sun.jna.platform.win32.WinNT.PSID)

Example 7 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 testMakeSelfRelativeSD.

public void testMakeSelfRelativeSD() {
    SECURITY_DESCRIPTOR absolute = new SECURITY_DESCRIPTOR(64 * 1024);
    assertTrue(Advapi32.INSTANCE.InitializeSecurityDescriptor(absolute, WinNT.SECURITY_DESCRIPTOR_REVISION));
    SECURITY_DESCRIPTOR_RELATIVE relative = new SECURITY_DESCRIPTOR_RELATIVE(64 * 1024);
    IntByReference lpdwBufferLength = new IntByReference(64 * 1024);
    assertTrue(Advapi32.INSTANCE.MakeSelfRelativeSD(absolute, relative, lpdwBufferLength));
    assertEquals(WinNT.SECURITY_DESCRIPTOR_REVISION, relative.Revision);
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) SECURITY_DESCRIPTOR_RELATIVE(com.sun.jna.platform.win32.WinNT.SECURITY_DESCRIPTOR_RELATIVE) SECURITY_DESCRIPTOR(com.sun.jna.platform.win32.WinNT.SECURITY_DESCRIPTOR)

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