Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: add ability to get/set effective uid/gid #1536

Merged
merged 1 commit into from
Apr 29, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions doc/api/process.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,19 @@ This is the numerical group id, not the group name.
}


## process.getegid()

Note: this function is only available on POSIX platforms (i.e. not Windows,
Android)

Gets the effective group identity of the process. (See getegid(2).)
This is the numerical group id, not the group name.

if (process.getegid) {
console.log('Current gid: ' + process.getegid());
}


## process.setgid(id)

Note: this function is only available on POSIX platforms (i.e. not Windows,
Expand All @@ -476,6 +489,27 @@ blocks while resolving it to a numerical ID.
}


## process.setegid(id)

Note: this function is only available on POSIX platforms (i.e. not Windows,
Android)

Sets the effective group identity of the process. (See setegid(2).)
This accepts either a numerical ID or a groupname string. If a groupname
is specified, this method blocks while resolving it to a numerical ID.

if (process.getegid && process.setegid) {
console.log('Current gid: ' + process.getegid());
try {
process.setegid(501);
console.log('New gid: ' + process.getegid());
}
catch (err) {
console.log('Failed to set gid: ' + err);
}
}


## process.getuid()

Note: this function is only available on POSIX platforms (i.e. not Windows,
Expand All @@ -489,6 +523,19 @@ This is the numerical userid, not the username.
}


## process.geteuid()

Note: this function is only available on POSIX platforms (i.e. not Windows,
Android)

Gets the effective user identity of the process. (See geteuid(2).)
This is the numerical userid, not the username.

if (process.geteuid) {
console.log('Current uid: ' + process.geteuid());
}


## process.setuid(id)

Note: this function is only available on POSIX platforms (i.e. not Windows,
Expand All @@ -510,6 +557,27 @@ blocks while resolving it to a numerical ID.
}


## process.seteuid(id)

Note: this function is only available on POSIX platforms (i.e. not Windows,
Android)

Sets the effective user identity of the process. (See seteuid(2).)
This accepts either a numerical ID or a username string. If a username
is specified, this method blocks while resolving it to a numerical ID.

if (process.geteuid && process.seteuid) {
console.log('Current uid: ' + process.geteuid());
try {
process.seteuid(501);
console.log('New uid: ' + process.geteuid());
}
catch (err) {
console.log('Failed to set uid: ' + err);
}
}


## process.getgroups()

Note: this function is only available on POSIX platforms (i.e. not Windows,
Expand Down
54 changes: 54 additions & 0 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1750,6 +1750,18 @@ static void GetGid(const FunctionCallbackInfo<Value>& args) {
}


static void GetEUid(const FunctionCallbackInfo<Value>& args) {
// uid_t is an uint32_t on all supported platforms.
args.GetReturnValue().Set(static_cast<uint32_t>(geteuid()));
}


static void GetEGid(const FunctionCallbackInfo<Value>& args) {
// gid_t is an uint32_t on all supported platforms.
args.GetReturnValue().Set(static_cast<uint32_t>(getegid()));
}


static void SetGid(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

Expand All @@ -1769,6 +1781,25 @@ static void SetGid(const FunctionCallbackInfo<Value>& args) {
}


static void SetEGid(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

if (!args[0]->IsUint32() && !args[0]->IsString()) {
return env->ThrowTypeError("setegid argument must be a number or string");
}

gid_t gid = gid_by_name(env->isolate(), args[0]);

if (gid == gid_not_found) {
return env->ThrowError("setegid group id does not exist");
}

if (setegid(gid)) {
return env->ThrowErrnoException(errno, "setegid");
}
}


static void SetUid(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

Expand All @@ -1788,6 +1819,25 @@ static void SetUid(const FunctionCallbackInfo<Value>& args) {
}


static void SetEUid(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

if (!args[0]->IsUint32() && !args[0]->IsString()) {
return env->ThrowTypeError("seteuid argument must be a number or string");
}

uid_t uid = uid_by_name(env->isolate(), args[0]);

if (uid == uid_not_found) {
return env->ThrowError("seteuid user id does not exist");
}

if (seteuid(uid)) {
return env->ThrowErrnoException(errno, "seteuid");
}
}


static void GetGroups(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

Expand Down Expand Up @@ -2821,10 +2871,14 @@ void SetupProcessObject(Environment* env,

#if defined(__POSIX__) && !defined(__ANDROID__)
env->SetMethod(process, "getuid", GetUid);
env->SetMethod(process, "geteuid", GetEUid);
env->SetMethod(process, "setuid", SetUid);
env->SetMethod(process, "seteuid", SetEUid);

env->SetMethod(process, "setgid", SetGid);
env->SetMethod(process, "setegid", SetEGid);
env->SetMethod(process, "getgid", GetGid);
env->SetMethod(process, "getegid", GetEGid);

env->SetMethod(process, "getgroups", GetGroups);
env->SetMethod(process, "setgroups", SetGroups);
Expand Down