c - set euid with posix_spawn -


i have following code snippet.

char *const parmlist[] = {"sh", "-c", "whoami", null}; if(geteuid() == 0) {    seteuid(atoi(getenv("sudo_uid"))); } posix_spawn(&pid, "/bin/sh", null, null, parmlist, environ); 

from understanding default behavior of posix_spawn is:

if posix_spawn_resetids flag not set, child process shall inherit parent process' effective user id.

however, when run program sudo, still root output posix_spawn. how have posix_spawn run original user? there better way this?

i ended accomplishing creating function forks execs

pid_t runcmd(char *cmd) {     if(!cmd) return -1;      pid_t ans = fork();     if(ans == 0) {         if(geteuid() == 0) {             int uid = atoi(getenv("sudo_uid"));             setreuid(uid, uid);         }         if(verbose_flag) println("uid %d; euid %d", getuid(), geteuid());         char *const parmlist[] = {"sh", "-c", cmd, null};         execv("/bin/sh", parmlist);     }     return ans; } 

whoami returns original user


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -