timestee
timestee

Reputation: 1096

Programmatically change the uid and gid of a running external process using c

How to programatically change the uid and gid of a running external process using c?

Upvotes: 1

Views: 814

Answers (1)

SirDarius
SirDarius

Reputation: 42879

A small example that includes the possibility to change uid et gid using existing user and group names:

#include <sys/types.h>
#include <unistd.h>
#include <pwd.h>
#include <grp.h>

// .. snip

// find user and group
struct passwd * pwd = getpwnam("new_user");
struct group * grp = getgrnam("new_group");

// not included : error checking

uid_t uid = pwd->pw_uid;
gid_t gid = grp->gr_gid;

setgid(gid);
setuid(uid);

edit: This only works for the current process

Upvotes: 3

Related Questions