@@ -375,6 +375,43 @@ static void SetGroups(const FunctionCallbackInfo<Value>& args) {
375
375
args.GetReturnValue ().Set (0 );
376
376
}
377
377
378
+ static void GetRESUid (const FunctionCallbackInfo<Value>& args) {
379
+ Environment* env = Environment::GetCurrent (args);
380
+ CHECK (env->has_run_bootstrapping_code ());
381
+ uid_t ruid, euid, suid;
382
+ getresuid (&ruid, &euid, &suid);
383
+ MaybeLocal<Value> array = ToV8Value (env->context (), std::vector<uid_t >{ruid, euid, suid});
384
+ args.GetReturnValue ().Set (array.ToLocalChecked ());
385
+ }
386
+
387
+ static void SetRESUid (const FunctionCallbackInfo<Value>& args) {
388
+ Environment* env = Environment::GetCurrent (args);
389
+ CHECK (env->owns_process_state ());
390
+
391
+ CHECK_EQ (args.Length (), 3 );
392
+ for (int i = 0 ; i < 3 ; i++) {
393
+ CHECK (args[i]->IsUint32 () || args[i]->IsString ());
394
+ }
395
+
396
+ uid_t ruid = uid_by_name (env->isolate (), args[0 ]);
397
+ uid_t euid = uid_by_name (env->isolate (), args[1 ]);
398
+ uid_t suid = uid_by_name (env->isolate (), args[2 ]);
399
+
400
+ if (ruid == uid_not_found || euid == uid_not_found ||
401
+ suid == uid_not_found) {
402
+ // Tells JS to throw ERR_INVALID_CREDENTIAL
403
+ int flag = 0b1000 ;
404
+ if (ruid == uid_not_found) flag |= 0b0001 ;
405
+ if (euid == uid_not_found) flag |= 0b0010 ;
406
+ if (suid == uid_not_found) flag |= 0b0100 ;
407
+ args.GetReturnValue ().Set (flag);
408
+ } else if (setresuid (ruid, euid, suid)) {
409
+ env->ThrowErrnoException (errno, " setresuid" );
410
+ } else {
411
+ args.GetReturnValue ().Set (0 );
412
+ }
413
+ }
414
+
378
415
static void InitGroups (const FunctionCallbackInfo<Value>& args) {
379
416
Environment* env = Environment::GetCurrent (args);
380
417
@@ -429,6 +466,9 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
429
466
registry->Register (GetEGid);
430
467
registry->Register (GetGroups);
431
468
469
+ registry->Register (SetRESUid);
470
+ registry->Register (GetRESUid);
471
+
432
472
registry->Register (InitGroups);
433
473
registry->Register (SetEGid);
434
474
registry->Register (SetEUid);
@@ -454,6 +494,7 @@ static void Initialize(Local<Object> target,
454
494
env->SetMethodNoSideEffect (target, " getgid" , GetGid);
455
495
env->SetMethodNoSideEffect (target, " getegid" , GetEGid);
456
496
env->SetMethodNoSideEffect (target, " getgroups" , GetGroups);
497
+ env->SetMethodNoSideEffect (target, " getresuid" , GetRESUid);
457
498
458
499
if (env->owns_process_state ()) {
459
500
env->SetMethod (target, " initgroups" , InitGroups);
@@ -462,6 +503,7 @@ static void Initialize(Local<Object> target,
462
503
env->SetMethod (target, " setgid" , SetGid);
463
504
env->SetMethod (target, " setuid" , SetUid);
464
505
env->SetMethod (target, " setgroups" , SetGroups);
506
+ env->SetMethod (target, " setresuid" , SetRESUid);
465
507
}
466
508
#endif // NODE_IMPLEMENTS_POSIX_CREDENTIALS
467
509
}
0 commit comments