Skip to content

Commit 3c92ca2

Browse files
committed
src: add ability to get/set effective uid/gid
Adds the following to process: - `process.geteuid()` - `process.seteuid(id)` - `process.getegid()` - `process.setegid(id)` PR-URL: #1536 Reviewed-By: Ben Noordhuis <[email protected]>
1 parent f9b226c commit 3c92ca2

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed

doc/api/process.markdown

+68
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,19 @@ This is the numerical group id, not the group name.
455455
}
456456

457457

458+
## process.getegid()
459+
460+
Note: this function is only available on POSIX platforms (i.e. not Windows,
461+
Android)
462+
463+
Gets the effective group identity of the process. (See getegid(2).)
464+
This is the numerical group id, not the group name.
465+
466+
if (process.getegid) {
467+
console.log('Current gid: ' + process.getegid());
468+
}
469+
470+
458471
## process.setgid(id)
459472

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

478491

492+
## process.setegid(id)
493+
494+
Note: this function is only available on POSIX platforms (i.e. not Windows,
495+
Android)
496+
497+
Sets the effective group identity of the process. (See setegid(2).)
498+
This accepts either a numerical ID or a groupname string. If a groupname
499+
is specified, this method blocks while resolving it to a numerical ID.
500+
501+
if (process.getegid && process.setegid) {
502+
console.log('Current gid: ' + process.getegid());
503+
try {
504+
process.setegid(501);
505+
console.log('New gid: ' + process.getegid());
506+
}
507+
catch (err) {
508+
console.log('Failed to set gid: ' + err);
509+
}
510+
}
511+
512+
479513
## process.getuid()
480514

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

491525

526+
## process.geteuid()
527+
528+
Note: this function is only available on POSIX platforms (i.e. not Windows,
529+
Android)
530+
531+
Gets the effective user identity of the process. (See geteuid(2).)
532+
This is the numerical userid, not the username.
533+
534+
if (process.geteuid) {
535+
console.log('Current uid: ' + process.geteuid());
536+
}
537+
538+
492539
## process.setuid(id)
493540

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

512559

560+
## process.seteuid(id)
561+
562+
Note: this function is only available on POSIX platforms (i.e. not Windows,
563+
Android)
564+
565+
Sets the effective user identity of the process. (See seteuid(2).)
566+
This accepts either a numerical ID or a username string. If a username
567+
is specified, this method blocks while resolving it to a numerical ID.
568+
569+
if (process.geteuid && process.seteuid) {
570+
console.log('Current uid: ' + process.geteuid());
571+
try {
572+
process.seteuid(501);
573+
console.log('New uid: ' + process.geteuid());
574+
}
575+
catch (err) {
576+
console.log('Failed to set uid: ' + err);
577+
}
578+
}
579+
580+
513581
## process.getgroups()
514582

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

src/node.cc

+54
Original file line numberDiff line numberDiff line change
@@ -1750,6 +1750,18 @@ static void GetGid(const FunctionCallbackInfo<Value>& args) {
17501750
}
17511751

17521752

1753+
static void GetEUid(const FunctionCallbackInfo<Value>& args) {
1754+
// uid_t is an uint32_t on all supported platforms.
1755+
args.GetReturnValue().Set(static_cast<uint32_t>(geteuid()));
1756+
}
1757+
1758+
1759+
static void GetEGid(const FunctionCallbackInfo<Value>& args) {
1760+
// gid_t is an uint32_t on all supported platforms.
1761+
args.GetReturnValue().Set(static_cast<uint32_t>(getegid()));
1762+
}
1763+
1764+
17531765
static void SetGid(const FunctionCallbackInfo<Value>& args) {
17541766
Environment* env = Environment::GetCurrent(args);
17551767

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

17711783

1784+
static void SetEGid(const FunctionCallbackInfo<Value>& args) {
1785+
Environment* env = Environment::GetCurrent(args);
1786+
1787+
if (!args[0]->IsUint32() && !args[0]->IsString()) {
1788+
return env->ThrowTypeError("setegid argument must be a number or string");
1789+
}
1790+
1791+
gid_t gid = gid_by_name(env->isolate(), args[0]);
1792+
1793+
if (gid == gid_not_found) {
1794+
return env->ThrowError("setegid group id does not exist");
1795+
}
1796+
1797+
if (setegid(gid)) {
1798+
return env->ThrowErrnoException(errno, "setegid");
1799+
}
1800+
}
1801+
1802+
17721803
static void SetUid(const FunctionCallbackInfo<Value>& args) {
17731804
Environment* env = Environment::GetCurrent(args);
17741805

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

17901821

1822+
static void SetEUid(const FunctionCallbackInfo<Value>& args) {
1823+
Environment* env = Environment::GetCurrent(args);
1824+
1825+
if (!args[0]->IsUint32() && !args[0]->IsString()) {
1826+
return env->ThrowTypeError("seteuid argument must be a number or string");
1827+
}
1828+
1829+
uid_t uid = uid_by_name(env->isolate(), args[0]);
1830+
1831+
if (uid == uid_not_found) {
1832+
return env->ThrowError("seteuid user id does not exist");
1833+
}
1834+
1835+
if (seteuid(uid)) {
1836+
return env->ThrowErrnoException(errno, "seteuid");
1837+
}
1838+
}
1839+
1840+
17911841
static void GetGroups(const FunctionCallbackInfo<Value>& args) {
17921842
Environment* env = Environment::GetCurrent(args);
17931843

@@ -2821,10 +2871,14 @@ void SetupProcessObject(Environment* env,
28212871

28222872
#if defined(__POSIX__) && !defined(__ANDROID__)
28232873
env->SetMethod(process, "getuid", GetUid);
2874+
env->SetMethod(process, "geteuid", GetEUid);
28242875
env->SetMethod(process, "setuid", SetUid);
2876+
env->SetMethod(process, "seteuid", SetEUid);
28252877

28262878
env->SetMethod(process, "setgid", SetGid);
2879+
env->SetMethod(process, "setegid", SetEGid);
28272880
env->SetMethod(process, "getgid", GetGid);
2881+
env->SetMethod(process, "getegid", GetEGid);
28282882

28292883
env->SetMethod(process, "getgroups", GetGroups);
28302884
env->SetMethod(process, "setgroups", SetGroups);

0 commit comments

Comments
 (0)