I'm trying to use setuid() and setgid() to set the respective id's of a program to drop privileges down from root, but to use them I need to know the uid and gid of the user I want to change to.
Is there a system call to do this? I don't want to hardcode it or parse from /etc/passwd .
Also I'd like to do this programmatically rather than using:
id -u USERNAME
Any help would be greatly appreciated
You can use the following code snippets:
Ref: getpwnam() getgrnam()
Have a look at the getpwnam() and getgrnam() functions.
Look at getpwnam and struct passwd.
You want to use the getpw* family of system calls, generally in pwd.h. It's essentially a C-level interface to the information in /etc/passwd.