简体   繁体   中英

Restoring inheritance to a Windows directory via Java using JNA

I have a problem and I HOPE someone out there can give me advice! Basically in my program I will be taking a windows folder, changing ownership, setting Access Control rules etc, for this its pretty vanilla Java using:

Files.getFileAttributeView(target, AclFileAttributeView.class);

The problem arises when I want to forget about a directory, and to do this I want to re-enable the inheritance to the parent directory and remove all my ACL rules. I can remove the rules and set the owner back again using Java however it looks like I have to go native to set the inheritance flag...

I THINK I can get a pointer to the security descriptor like this:

    Advapi32 advapi32 = Advapi32.INSTANCE;
    PointerByReference ppsidOwner = new PointerByReference();
    PointerByReference ppsidGroup = new PointerByReference();
    PointerByReference ppDacl = new PointerByReference();
    PointerByReference ppSacl = new PointerByReference();
    PointerByReference ppSecurityDescriptor = new PointerByReference();
    int reqSecurityInfo = Advapi32.OWNER_SECURITY_INFORMATION |
            Advapi32.DACL_SECURITY_INFORMATION |
            Advapi32.SACL_SECURITY_INFORMATION |
            Advapi32.GROUP_SECURITY_INFORMATION;
    int ret = advapi32.GetNamedSecurityInfo("c:\\\\testpaths", Advapi32.SE_FILE_OBJECT, 
            reqSecurityInfo, ppsidOwner, ppsidGroup, ppDacl, ppSacl, ppSecurityDescriptor);
    if(ret != 0){
        throw new Win32Exception(ret);
    }

Where Advapi32.java contains: Advapi32 INSTANCE = (Advapi32) Native.loadLibrary("Advapi32", Advapi32.class, W32APIOptions.UNICODE_OPTIONS);

But I really am floundering a bit here, am I on the right track or am I being an idiot? It is unfamiliar ground for me, please help!

I decided the easiest way to do this was to implement the code in C# and import the simple DLL to java and call that, as an example:

    [RGiesecke.DllExport.DllExport]
    static void ReEnableInheretance(String dirPath)
    {
        DirectorySecurity dirSecurity = new DirectorySecurity();
        dirSecurity.SetAccessRuleProtection(false, false);
        Directory.SetAccessControl(dirPath, dirSecurity); 
    }

And in Java:

public interface MyNewLib extends Library {
    public void ReEnableInheretance(String dirPath);
}

MyNewLib myNewLib = (MyNewLib )Native.loadLibrary("MyNewLib ",MyNewLib.class);
myNewLib .ReEnableInheretance(path.toString());

Puts a dependency onto .net however that is not a problem for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM