dokan-dev / dokan-java

Dokan Java Wrapper
GNU Lesser General Public License v3.0
51 stars 28 forks source link

Howto set WinBase.SECURITY_ATTRIBUTES #20

Closed fiurthorn closed 5 years ago

fiurthorn commented 5 years ago
@Override public int zwCreateFile(WString rawPath, WinBase.SECURITY_ATTRIBUTES securityContext, int rawDesiredAccess, int rawFileAttributes, int rawShareAccess, int rawCreateDisposition, int rawCreateOptions, DokanFileInfo dokanFileInfo);

I've found that I need to use SelfRelativeSecurityDescriptor. But how?

fiurthorn commented 5 years ago

the following does not work:

        var netUser = Netapi32Util.getUserInfo("xxx");
        log.info(netUser.name, netUser.sidString);

        // var sec = new com.sun.jna.platform.win32.WinNT.SECURITY_DESCRIPTOR(securityContext.lpSecurityDescriptor);
        // var user = Advapi32Util.getAccountByName("xxx");
        // log.info(user.name, user.sidString);

        SecurityIdentifier owner = SecurityIdentifier.fromString(netUser.sidString);
        SecurityIdentifier group = null;

        AccessControlEntry entry = new AccessAllowedACE(
                new EnumIntegerSet(AccessControlEntryFlag.NO_PROPAGATE_INHERIT_ACE), owner, new EnumIntegerSet(AccessMask.GENERIC_READ)
        );

        AccessControlList dacl = AccessControlList.createDaclRevision2(List.of(entry));
        SelfRelativeSecurityDescriptor srsd = SelfRelativeSecurityDescriptor.createSD(
                new EnumIntegerSet(SecurityDescriptorControlFlag.DP),
                owner, group, null, dacl
        );

        securityContext.bInheritHandle = false;
        securityContext.lpSecurityDescriptor = new Memory(srsd.sizeOfByteArray());
        securityContext.lpSecurityDescriptor.write(0, srsd.toByteArray(), 0, srsd.sizeOfByteArray());
        securityContext.dwLength = new WinDef.DWORD(srsd.sizeOfByteArray());
infeo commented 5 years ago

First of all you don't create a security descriptor in a zwCreateFile() call. The security descriptor either already exists or is set to null.

In fact dokan-java does not provide a way to set the security attributes of a file or anything since you implement your own filesystem. What it does provide is a tool to create a self-relative security descriptor to answer a GetFileSecurity() call. For your purpose i would suggest to take a look at the WinNT.SECURITY_DESCIRPTOR class of the jna project.

fiurthorn commented 5 years ago

Enabling the following features never gives me a SecurityDescriptior that can be deserialized. The error of uninitialized memory always comes.

The tab Securitiy can be seen in the file properties, but empty.

PERSISTENT_ACLS(WinNT.FILE_PERSISTENT_ACLS)
SUPPORTS_EXTENDED_ATTRIBUTES(WinNT.FILE_SUPPORTS_EXTENDED_ATTRIBUTES)
fiurthorn commented 5 years ago

I simply want to know who access the file, or set who can access the file.

infeo commented 5 years ago

dokan-java is the wrong library for this purpose. It does not give you access to native windows functions and/or features. It only provides a way to implement your own filesystem in Java for Windows and provide a mount/access point for it. The filesystem could be anything, not only some additional layer between you and the files on disk.

For your purpose you should use the JNA library.

I'm closing this issue since it is not about dokan-java and rather about accessing/setting a native windows structure.