The problem is: the same program can be run with and without sudo. With sudo it should store configuration in system directory, without sudo it should store configuration in user's directory. Also, when run with sudo it should write both configurations.
The problem is when I create a file or directory in my program when it's run with sudo - it will create file and directories as root. Those files would not be writeable by the user then.
My program also deletes a file that makes resetting the configuration a second time impossible. So on the first run it needs to create both files, user and system.
I know one way of doing that: create all files and directories as root, then change the owner of user directories and files to UID, since libc getuid() should still return my original user id. But this approach requires to add a specialized package for Linux system call. As I'm doing another package, I'm looking a way to reduce dependencies, so maybe is there a build-in way to just switch context to non-sudo from root?
Another way to do that is to use shell, but I don't want to use shell.
I know there is a built-in principal context switching for Windows.
Is there something similar for Linux or multi-platform, or the only way is to use libc calls?
BTW, one or 2 simple calls is no problem, I just don't want to include all stuff like stat, chown, chmod and friends. But if there's no other way I have it all in my Woof.LinuxAdmin package. I just don't want to add a dependency in a package that is not intended strictly for system administration. My goal is just to use the new DPAPI, and this (at least on LINUX) requires configuring key storage somehow. So this IS a little administrative task.
UPDATE:
Oops, it's a rabbit hole ;) I just tested, that libc getuid() and geteuid() return the same value with sudo. It's getting interesting. So of course I can still get user's id by stating the main executable owner. I know, it's not the equivalent to the current user, but it's most common scenario that files in user's home directory are owned by the user. But that leaves me with my Woof.LinuxAdmin package, since it contains all needed system calls and tools to effortlessly use Linux file system permission features.
Since there's probably no easy way around, so when I make a package that should both work on Linux and Windows, I'll probably go for adding the dependency on that Linux module.
I'll just make another "lite" version for Windows only, without the dependency.