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

Adding support for supplementary groups for Linux #685

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const ptyProcess = pty.spawn(shell, [], {
env: process.env
});

ptyProcess.onData((data) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you change this? on was deprecated some time ago and I don't think it's in the .d.ts anymore

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I made that change based on ITerminal from src/interfaces.ts. Reverted it back

ptyProcess.on('data', (data) => {
process.stdout.write(data);
});

Expand Down
1 change: 1 addition & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ interface IBasePtyForkOptions {
export interface IPtyForkOptions extends IBasePtyForkOptions {
uid?: number;
gid?: number;
suppGids?: number[];
}

export interface IWindowsPtyForkOptions extends IBasePtyForkOptions {
Expand Down
2 changes: 1 addition & 1 deletion src/native.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ interface IWinptyNative {
}

interface IUnixNative {
fork(file: string, args: string[], parsedEnv: string[], cwd: string, cols: number, rows: number, uid: number, gid: number, useUtf8: boolean, helperPath: string, onExitCallback: (code: number, signal: number) => void): IUnixProcess;
fork(file: string, args: string[], parsedEnv: string[], cwd: string, cols: number, rows: number, uid: number, gid: number, useUtf8: boolean, helperPath: string, onExitCallback: (code: number, signal: number) => void, suppGids: number[]): IUnixProcess;
open(cols: number, rows: number): IUnixOpenProcess;
process(fd: number, pty?: string): string;
resize(fd: number, cols: number, rows: number): void;
Expand Down
28 changes: 25 additions & 3 deletions src/unix/pty.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@
#include <termios.h>
#endif

/* for setgroups */
#if defined(__linux__)
#include <grp.h>
#endif

/* NSIG - macro for highest signal + 1, should be defined */
#ifndef NSIG
#define NSIG 32
Expand Down Expand Up @@ -258,7 +263,7 @@ Napi::Value PtyFork(const Napi::CallbackInfo& info) {
Napi::Env napiEnv(info.Env());
Napi::HandleScope scope(napiEnv);

if (info.Length() != 11 ||
if (info.Length() != 12 ||
!info[0].IsString() ||
!info[1].IsArray() ||
!info[2].IsArray() ||
Expand All @@ -269,8 +274,9 @@ Napi::Value PtyFork(const Napi::CallbackInfo& info) {
!info[7].IsNumber() ||
!info[8].IsBoolean() ||
!info[9].IsString() ||
!info[10].IsFunction()) {
throw Napi::Error::New(napiEnv, "Usage: pty.fork(file, args, env, cwd, cols, rows, uid, gid, utf8, helperPath, onexit)");
!info[10].IsFunction() ||
!info[11].IsArray()) {
throw Napi::Error::New(napiEnv, "Usage: pty.fork(file, args, env, cwd, cols, rows, uid, gid, utf8, helperPath, onexit, suppGids)");
}

// file
Expand Down Expand Up @@ -419,6 +425,22 @@ Napi::Value PtyFork(const Napi::CallbackInfo& info) {
}

if (uid != -1 && gid != -1) {
// supplementary group IDs
Napi::Array suppGids_ = info[11].As<Napi::Array>();
if (suppGids_.Length() > 0) {
int gidsLength = suppGids_.Length() + 1;
std::unique_ptr<gid_t[]> gidsPtr(new gid_t[gidsLength]);
for (int i = 0; i < gidsLength - 1; ++i) {
gidsPtr[i] = suppGids_.Get(i).As<Napi::Number>().Uint32Value();
}
gidsPtr[gidsLength - 1] = gid;

if(setgroups(gidsLength, gidsPtr.get()) == -1) {
perror("setgroups(2) failed.");
_exit(1);
}
}

if (setgid(gid) == -1) {
perror("setgid(2) failed.");
_exit(1);
Expand Down
4 changes: 3 additions & 1 deletion src/unixTerminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,10 @@ export class UnixTerminal extends Terminal {
this.emit('exit', code, signal);
};

const suppGids = opt.suppGids || [];

// fork
const term = pty.fork(file, args, parsedEnv, cwd, this._cols, this._rows, uid, gid, (encoding === 'utf8'), helperPath, onexit);
const term = pty.fork(file, args, parsedEnv, cwd, this._cols, this._rows, uid, gid, (encoding === 'utf8'), helperPath, onexit, suppGids);

this._socket = new tty.ReadStream(term.fd);
if (encoding !== null) {
Expand Down
1 change: 1 addition & 0 deletions typings/node-pty.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ declare module 'node-pty' {
*/
uid?: number;
gid?: number;
suppGids?: number[];
}

export interface IWindowsPtyForkOptions extends IBasePtyForkOptions {
Expand Down