Programmatically getting UID and GID from username in Unix?

Evan picture Evan · Jun 17, 2009 · Viewed 24.1k times · Source

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

Answer

jpalecek picture jpalecek · Jun 17, 2009

Have a look at the getpwnam() and getgrnam() functions.