I am writing some software (in C++, for Linux/Mac OSX) which runs as a non-privileged user but needs root privileges at some point (to create a new virtual device).
Running this program as root is not a option (mainly for security issues) and I need to know the identity (uid) of the "real" user.
Is there a way to mimic the "sudo" command behavior (ask for user password) to temporarily gain root privileges and perform the particular task ? If so, which functions would I use ?
Thank you very much for your help !
On OS X, you can use the
AuthorizationExecuteWithPrivileges
function. The page on Authorization Services Tasks has some elaborate discussion of this (and related) functions.Here's a bit of C++ code to execute a program with administrator privileges: