I'm trying to create a file (if it doesn't exist) on Windows - so far, so good. But I'd like that file to be readable by everyone else. And that last bit is where I'm hitting a snag. I'd like to create a file in a public location (e.g. %PUBLIC%/shared_info.txt) a location that other users can at least see; not all that different from log files. Which seems like it should be basic multi-user system functionality?
Looking at OpenOptionsExt and the MS docs it links, I can set the permissions for other processes (presumably under the same user) while I hold the handle. But I can't find anything about managing user permissions. There's an old crate windows-permissions but the docs seem to imply it's only for reading permissions similar to e.g. faccess.
Is there any way to manage user permissions on Windows short of hacking something together myself over the windows crate?
edit: Turns out the example %PUBLIC% causes inheritance of the file being created - if it's initially created there. The MS docs on ACLs only say it's possible to inherit. So I figured I'd just run icacls as a subprocess to make it happen. And then ss64 had some great news
By default, an object will inherit permissions from its parent object, either at the time of creation or when it is copied or moved. The only exception to this rule occurs when you move an object to a different folder on the same volume.
That said, I would still like to mangle permissions if possible, so consider the question still open
Is it what you need? It seems someone else had the same issue and proposed an ACP, which was accepted (I'm not sure it includes those specific user permissions, though):
Alas, those attributes are the ones controlling what other processes can do. They'd leave it accessible only to the creating user and processes run by elevated users like admins and system. Windows' alternative to user/group/world permissions seem to be per-user, per-group and predefined Everyone (localized name) permissions set with ACLs. And I can't find anything about manipulating those from Rust...
Sorry for the misleading links, then. I've always found Windows' way of dealing with file access permissions quite obscure. I know there's a windows crate, which is maintained by someone at Microsoft, and there are some bits related to DACL (if that's even the correct entry point to managing file access), but it's big and not too well documented.
In my experience it's extremely rare for any Windows program to actually modify the ACLs on the files it creates, and so this might just not be something anyone has made a nice library for.
The vast majority of files/directories on Windows systems have no ACL specified at all and just inherit the ACL of their parent, so permissions are often configured by human administrators somewhere near the "top level" and just get inherited by all the directories and files beneath, without any program ever needing to touch them.
If you want to set up a location that's readable to all the users on the system then most often that would be done manually by creating and setting a specific ACL on that directory and then just configuring whatever program is creating the data to output to that location.
As I understand it, to work like icacls /grant:r, you would call ConvertStringSecurityDescriptorToSecurityDescriptorW to get a SECURITY_DESCRIPTOR, put that into a SECURITY_ATTRIBUTES struct, and call SetSecurityInfo to apply it to an open file handle or SetNamedSecurityInfo with a filename instead. You should also be able to use it with CreateFileW to do what you canβt with OpenOptions.
These Windows APIs are available in the windows crate with the Win32_Security_Authorization feature.
Yeah I think you're right. That's also the part of the initial question that was still unanswered (is there an ergonomic approach or do I roll my own) so I get to hack around I guess.
Exactly what I wanted to avoid for reproducible deployment reasons
Thanks a bunch for finding that. My browser was not handling search in those docs well at all!
And thanks for the other info as well, that is indeed more useful than looking into ACL documentation. Time to start another project