I have a program that runs as the root user (getuid()
returns 0).
I need to execute a command line tool as the user that's currently logged in (which I can determine by getting the HOME env var). (See below for an elaboration)
How do I do that, ideally by using NSTask
?
I know how to execute the tool using NSTask as the current (root) user, so all I need is to know what I need to change about that.
Alternatively, is there a way to run a section of my program's code under a different user's ID?
Elaboration
In particular, said program is launched by AuthorizationExecuteWithPrivileges
. Interestingly, this gives the program a slightly different environment as when launching the program with sudo:
With sudo, the actual and effective user (from getuid()
and geteuid()
) are both "root".
However, with AuthorizationExecuteWithPrivileges
, the effective user remains the user that called that function. This has some weird effects, e.g. then the program uses NSUserDefaults, as they end up in different domains, my testing shows.