:: Re: [DNG] How do I get the user's h…
Kezdőlap
Delete this message
Reply to this message
Szerző: Rainer Weikusat
Dátum:  
Címzett: dng
Tárgy: Re: [DNG] How do I get the user's home directory path?
Edward Bartolo <edbarx@???> writes:
> I used:
>
> char* home = getenv("HOME");
>
> to get the user's home directory but got "/root" instead. The reason
> is backend has a setuid = 0. However, I need to get the unprivileged
> user home directory path. Does anyone know of a solution?


A setuid program will have its so-called 'effective user-id' set to the
owner of the file but the so-called 'real user-id' will remain the
user-id of the invoking user.

--------
#include <pwd.h>
#include <stdio.h>
#include <unistd.h>

int main(void)
{
    struct passwd *pwd;


    /* home directory of 'real user' */
    pwd = getpwuid(getuid());
    puts(pwd->pw_dir);


    /* home directory of 'effective user', ie, root */
    pwd = getpwuid(geteuid());
    puts(pwd->pw_dir);


    return 0;
}
---------


When compiling this, making the resulting file root-owned and setting
the setuid bit, executing the program will print the 'real user' home
directory, followed by the root home directory.