Peter Smith
Peter Smith

Reputation: 889

How to find UID of user for setuid in C program?

Programs like apache2 and mysql install on various machines throughout the world. How do they ensure they know the uid or gid of the restricted rights user they wish to run as? I want my program to run as a service with a user who has least privileges through init.d. I assume services started in init.d are initially run as root?

Upvotes: 1

Views: 1941

Answers (1)

cnicutar
cnicutar

Reputation: 182744

Usually there's a configuration file which specifies the user the service is to use. Then the service can use getpwnam(3) and retrieve the UID.

struct passwd *getpwnam(const char *name);

struct passwd {
    char *pw_name;      /* Login name (username) */
    char *pw_passwd;    /* Encrypted password */
    uid_t pw_uid;       /* User ID */      <-----

Upvotes: 2

Related Questions