2015-07-20 00:09:47 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
2021-05-12 17:30:29 -07:00
|
|
|
const assert = require('assert'),
|
2019-12-04 10:29:06 -08:00
|
|
|
BoxError = require('./boxerror.js'),
|
2015-07-20 00:09:47 -07:00
|
|
|
child_process = require('child_process'),
|
2016-08-30 21:33:56 -07:00
|
|
|
debug = require('debug')('box:shell'),
|
2022-04-15 19:01:35 -05:00
|
|
|
once = require('./once.js'),
|
2015-07-20 00:09:47 -07:00
|
|
|
util = require('util');
|
|
|
|
|
|
2021-05-12 17:30:29 -07:00
|
|
|
exports = module.exports = {
|
|
|
|
|
sudo,
|
|
|
|
|
|
|
|
|
|
promises: {
|
|
|
|
|
exec: util.promisify(exec),
|
2024-02-21 18:37:10 +01:00
|
|
|
execArgs: util.promisify(execArgs),
|
2021-05-12 17:30:29 -07:00
|
|
|
sudo: util.promisify(sudo)
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2020-07-31 16:35:06 -07:00
|
|
|
const SUDO = '/usr/bin/sudo';
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2024-02-21 17:16:33 +01:00
|
|
|
// default encoding utf8, no shell, separate args
|
2024-02-21 18:37:10 +01:00
|
|
|
function execArgs(tag, file, args, options, callback) {
|
2024-02-21 13:09:59 +01:00
|
|
|
assert.strictEqual(typeof tag, 'string');
|
|
|
|
|
assert.strictEqual(typeof file, 'string');
|
|
|
|
|
assert(Array.isArray(args));
|
|
|
|
|
assert.strictEqual(typeof options, 'object');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
debug(`${tag} exec: ${file}`);
|
|
|
|
|
|
2024-02-21 17:16:33 +01:00
|
|
|
const execOptions = Object.assign({ encoding: 'utf8', shell: false }, options);
|
|
|
|
|
|
2024-02-21 13:09:59 +01:00
|
|
|
// https://github.com/nodejs/node/issues/25231
|
2024-02-21 17:16:33 +01:00
|
|
|
const cp = child_process.execFile(file, args, execOptions, function (error, stdout, stderr) {
|
2024-02-21 13:09:59 +01:00
|
|
|
let e = null;
|
|
|
|
|
if (error) {
|
|
|
|
|
e = new BoxError(BoxError.SHELL_ERROR, `${tag} errored with code ${error.code} message ${error.message}`);
|
|
|
|
|
e.stdout = stdout; // when promisified, this is the way to get stdout
|
|
|
|
|
e.stderr = stderr; // when promisified, this is the way to get stderr
|
|
|
|
|
|
|
|
|
|
debug(`${tag}: ${file} with args ${args.join(' ')} errored`, error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
callback(e, stdout);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (options.input) {
|
|
|
|
|
cp.stdin.write(options.input);
|
|
|
|
|
cp.stdin.end();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-21 18:37:10 +01:00
|
|
|
// default encoding utf8, shell, handles input, full command
|
|
|
|
|
function exec(tag, cmd, options, callback) {
|
|
|
|
|
assert.strictEqual(typeof tag, 'string');
|
|
|
|
|
assert.strictEqual(typeof cmd, 'string');
|
|
|
|
|
assert.strictEqual(typeof options, 'object');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
const [file, ...args] = cmd.split(' ');
|
|
|
|
|
execArgs(tag, file, args, options, callback);
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-21 12:33:04 +01:00
|
|
|
// use this when you are afraid of how arguments will split up
|
2018-11-17 19:26:19 -08:00
|
|
|
function spawn(tag, file, args, options, callback) {
|
2015-07-20 00:09:47 -07:00
|
|
|
assert.strictEqual(typeof tag, 'string');
|
|
|
|
|
assert.strictEqual(typeof file, 'string');
|
2021-05-02 11:26:08 -07:00
|
|
|
assert(Array.isArray(args));
|
2017-08-09 00:30:39 -07:00
|
|
|
assert.strictEqual(typeof options, 'object');
|
2017-09-09 19:48:05 -07:00
|
|
|
assert.strictEqual(typeof callback, 'function');
|
2015-07-20 00:09:47 -07:00
|
|
|
|
|
|
|
|
callback = once(callback); // exit may or may not be called after an 'error'
|
|
|
|
|
|
2018-11-26 15:21:48 -08:00
|
|
|
if (options.ipc) options.stdio = ['pipe', 'pipe', 'pipe', 'ipc'];
|
|
|
|
|
|
2021-06-18 14:33:50 -07:00
|
|
|
debug(tag + ' spawn: %s %s', file, args.join(' ').replace(/\n/g, '\\n'));
|
2020-07-31 16:35:06 -07:00
|
|
|
const cp = child_process.spawn(file, args, options);
|
2021-08-18 15:54:53 -07:00
|
|
|
let stdoutResult = '';
|
2020-07-31 16:35:06 -07:00
|
|
|
|
2024-02-21 13:35:56 +01:00
|
|
|
cp.stdout.on('data', function (data) {
|
|
|
|
|
debug(tag + ' (stdout): %s', data.toString('utf8'));
|
|
|
|
|
stdoutResult += data.toString('utf8');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
cp.stderr.on('data', function (data) {
|
|
|
|
|
debug(tag + ' (stderr): %s', data.toString('utf8'));
|
|
|
|
|
});
|
2015-07-20 00:09:47 -07:00
|
|
|
|
|
|
|
|
cp.on('exit', function (code, signal) {
|
|
|
|
|
if (code || signal) debug(tag + ' code: %s, signal: %s', code, signal);
|
2021-08-18 15:54:53 -07:00
|
|
|
if (code === 0) return callback(null, stdoutResult);
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2024-02-20 23:09:49 +01:00
|
|
|
let e = new BoxError(BoxError.SHELL_ERROR, `${tag} exited with code ${code} signal ${signal}`);
|
2017-04-21 14:07:10 -07:00
|
|
|
e.code = code;
|
|
|
|
|
e.signal = signal;
|
|
|
|
|
callback(e);
|
2015-07-20 00:09:47 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
cp.on('error', function (error) {
|
|
|
|
|
debug(tag + ' code: %s, signal: %s', error.code, error.signal);
|
2024-02-20 23:09:49 +01:00
|
|
|
let e = new BoxError(BoxError.SHELL_ERROR, `${tag} errored with code ${error.code} message ${error.message}`);
|
2019-12-05 09:54:29 -08:00
|
|
|
callback(e);
|
2015-07-20 00:09:47 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return cp;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-09 00:30:39 -07:00
|
|
|
function sudo(tag, args, options, callback) {
|
2015-07-20 00:09:47 -07:00
|
|
|
assert.strictEqual(typeof tag, 'string');
|
2021-05-02 11:26:08 -07:00
|
|
|
assert(Array.isArray(args));
|
2017-08-09 00:30:39 -07:00
|
|
|
assert.strictEqual(typeof options, 'object');
|
2018-11-25 14:57:17 -08:00
|
|
|
assert.strictEqual(typeof callback, 'function');
|
2017-08-09 00:30:39 -07:00
|
|
|
|
2018-11-26 15:21:48 -08:00
|
|
|
let sudoArgs = [ '-S' ]; // -S makes sudo read stdin for password
|
|
|
|
|
if (options.preserveEnv) sudoArgs.push('-E'); // -E preserves environment
|
|
|
|
|
if (options.ipc) sudoArgs.push('--close-from=4'); // keep the ipc open. requires closefrom_override in sudoers file
|
|
|
|
|
|
2021-05-12 17:30:29 -07:00
|
|
|
const cp = spawn(tag, SUDO, sudoArgs.concat(args), options, callback);
|
2015-07-20 00:09:47 -07:00
|
|
|
cp.stdin.end();
|
2022-04-28 21:29:11 -07:00
|
|
|
if (options.onMessage) cp.on('message', options.onMessage);
|
2017-04-21 14:07:10 -07:00
|
|
|
return cp;
|
2015-07-20 00:09:47 -07:00
|
|
|
}
|