2015-06-08 15:24:56 +02:00
|
|
|
var Node = {
|
|
|
|
child: require('child_process'),
|
2015-11-17 14:40:53 +01:00
|
|
|
crypto: require('crypto'),
|
2015-06-08 15:24:56 +02:00
|
|
|
fs: require('fs'),
|
|
|
|
os: require('os'),
|
|
|
|
path: require('path'),
|
2015-11-17 14:40:53 +01:00
|
|
|
process: process,
|
|
|
|
util: require('util')
|
2015-06-08 15:24:56 +02:00
|
|
|
};
|
|
|
|
|
2016-04-15 13:11:23 +02:00
|
|
|
function Attempt(instance, end) {
|
2016-07-15 17:13:03 +02:00
|
|
|
var platform = Node.process.platform;
|
2018-09-11 15:20:05 +02:00
|
|
|
if (platform === 'darwin') return Mac(instance, end);
|
|
|
|
if (platform === 'linux') return Linux(instance, end);
|
2016-07-15 17:13:03 +02:00
|
|
|
if (platform === 'win32') return Windows(instance, end);
|
2018-09-11 15:20:05 +02:00
|
|
|
end(new Error('Platform not yet supported.'));
|
2015-11-17 14:40:53 +01:00
|
|
|
}
|
|
|
|
|
2016-04-15 13:11:23 +02:00
|
|
|
function EscapeDoubleQuotes(string) {
|
|
|
|
if (typeof string !== 'string') throw new Error('Expected a string.');
|
2015-11-17 14:40:53 +01:00
|
|
|
return string.replace(/"/g, '\\"');
|
|
|
|
}
|
|
|
|
|
2016-04-15 13:11:23 +02:00
|
|
|
function Exec() {
|
2015-11-17 14:40:53 +01:00
|
|
|
if (arguments.length < 1 || arguments.length > 3) {
|
|
|
|
throw new Error('Wrong number of arguments.');
|
|
|
|
}
|
|
|
|
var command = arguments[0];
|
|
|
|
var options = {};
|
|
|
|
var end = function() {};
|
|
|
|
if (typeof command !== 'string') {
|
|
|
|
throw new Error('Command should be a string.');
|
|
|
|
}
|
|
|
|
if (arguments.length === 2) {
|
|
|
|
if (Node.util.isObject(arguments[1])) {
|
|
|
|
options = arguments[1];
|
|
|
|
} else if (Node.util.isFunction(arguments[1])) {
|
|
|
|
end = arguments[1];
|
|
|
|
} else {
|
|
|
|
throw new Error('Expected options or callback.');
|
|
|
|
}
|
|
|
|
} else if (arguments.length === 3) {
|
|
|
|
if (Node.util.isObject(arguments[1])) {
|
|
|
|
options = arguments[1];
|
|
|
|
} else {
|
|
|
|
throw new Error('Expected options to be an object.');
|
|
|
|
}
|
|
|
|
if (Node.util.isFunction(arguments[2])) {
|
|
|
|
end = arguments[2];
|
|
|
|
} else {
|
|
|
|
throw new Error('Expected callback to be a function.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (/^sudo/i.test(command)) {
|
2016-04-15 13:11:23 +02:00
|
|
|
return end(new Error('Command should not be prefixed with "sudo".'));
|
2015-11-17 14:40:53 +01:00
|
|
|
}
|
|
|
|
if (typeof options.name === 'undefined') {
|
2016-04-15 13:11:23 +02:00
|
|
|
var title = Node.process.title;
|
|
|
|
if (ValidName(title)) {
|
|
|
|
options.name = title;
|
2015-11-17 14:40:53 +01:00
|
|
|
} else {
|
2016-04-15 13:11:23 +02:00
|
|
|
return end(new Error('process.title cannot be used as a valid name.'));
|
2015-11-17 14:40:53 +01:00
|
|
|
}
|
2016-04-15 13:11:23 +02:00
|
|
|
} else if (!ValidName(options.name)) {
|
|
|
|
var error = '';
|
|
|
|
error += 'options.name must be alphanumeric only ';
|
|
|
|
error += '(spaces are allowed) and <= 70 characters.';
|
|
|
|
return end(new Error(error));
|
2015-11-17 14:40:53 +01:00
|
|
|
}
|
|
|
|
if (typeof options.icns !== 'undefined') {
|
|
|
|
if (typeof options.icns !== 'string') {
|
|
|
|
return end(new Error('options.icns must be a string if provided.'));
|
|
|
|
} else if (options.icns.trim().length === 0) {
|
2016-04-15 13:11:23 +02:00
|
|
|
return end(new Error('options.icns must not be empty if provided.'));
|
2015-11-26 11:11:57 +01:00
|
|
|
}
|
|
|
|
}
|
2016-04-15 13:11:23 +02:00
|
|
|
var platform = Node.process.platform;
|
2016-07-15 17:13:03 +02:00
|
|
|
if (platform !== 'darwin' && platform !== 'linux' && platform !== 'win32') {
|
2015-11-17 14:40:53 +01:00
|
|
|
return end(new Error('Platform not yet supported.'));
|
|
|
|
}
|
2016-04-15 13:11:23 +02:00
|
|
|
var instance = {
|
|
|
|
command: command,
|
|
|
|
options: options,
|
|
|
|
uuid: undefined,
|
|
|
|
path: undefined
|
|
|
|
};
|
|
|
|
Attempt(instance, end);
|
2015-11-17 14:40:53 +01:00
|
|
|
}
|
|
|
|
|
2016-04-15 13:11:23 +02:00
|
|
|
function Linux(instance, end) {
|
|
|
|
LinuxBinary(instance,
|
2015-11-17 14:40:53 +01:00
|
|
|
function(error, binary) {
|
|
|
|
if (error) return end(error);
|
2016-04-15 13:11:23 +02:00
|
|
|
var command = [];
|
|
|
|
command.push('"' + EscapeDoubleQuotes(binary) + '"');
|
2016-06-04 13:17:14 +02:00
|
|
|
if (/kdesudo/i.test(binary)) {
|
2016-06-16 19:06:11 +02:00
|
|
|
var reason = 'needs administrative privileges. Please enter your password.';
|
|
|
|
command.push('--comment', '"' + instance.options.name + ' ' + reason + '"');
|
2016-05-20 17:06:09 +02:00
|
|
|
command.push('--');
|
2016-04-15 13:11:23 +02:00
|
|
|
} else if (/pkexec/i.test(binary)) {
|
|
|
|
command.push('--disable-internal-agent');
|
|
|
|
}
|
|
|
|
command.push(instance.command);
|
|
|
|
command = command.join(' ');
|
2018-01-10 14:26:24 +01:00
|
|
|
Node.child.exec(command, { maxBuffer: MAX_BUFFER },
|
2016-04-15 13:11:23 +02:00
|
|
|
function(error, stdout, stderr) {
|
2016-08-01 04:52:06 +02:00
|
|
|
if (error) {
|
|
|
|
if (/No authentication agent found/.test(stderr)) {
|
|
|
|
error = new Error(NO_POLKIT_AGENT);
|
|
|
|
} else if (/Request dismissed|Command failed/i.test(error)) {
|
|
|
|
error = new Error(PERMISSION_DENIED);
|
|
|
|
}
|
2016-04-15 13:11:23 +02:00
|
|
|
}
|
|
|
|
end(error, stdout, stderr);
|
|
|
|
}
|
|
|
|
);
|
2015-11-17 14:40:53 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-04-15 13:11:23 +02:00
|
|
|
function LinuxBinary(instance, end) {
|
2015-11-17 14:40:53 +01:00
|
|
|
var index = 0;
|
2016-06-04 13:17:14 +02:00
|
|
|
// We used to prefer gksudo over pkexec since it enabled a better prompt.
|
|
|
|
// However, gksudo cannot run multiple commands concurrently.
|
2016-06-30 18:19:21 +02:00
|
|
|
var paths = ['/usr/bin/kdesudo', '/usr/bin/pkexec'];
|
2015-11-17 14:40:53 +01:00
|
|
|
function test() {
|
2018-12-12 09:27:28 +01:00
|
|
|
if (index === paths.length) {
|
|
|
|
return end(new Error('Unable to find pkexec or kdesudo.'));
|
|
|
|
}
|
2015-11-17 14:40:53 +01:00
|
|
|
var path = paths[index++];
|
|
|
|
Node.fs.stat(path,
|
|
|
|
function(error) {
|
|
|
|
if (error) {
|
2016-04-15 13:11:23 +02:00
|
|
|
if (error.code === 'ENOTDIR') return test();
|
|
|
|
if (error.code === 'ENOENT') return test();
|
|
|
|
end(error);
|
2015-11-17 14:40:53 +01:00
|
|
|
} else {
|
|
|
|
end(undefined, path);
|
|
|
|
}
|
2015-06-08 15:24:56 +02:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2015-11-17 14:40:53 +01:00
|
|
|
test();
|
|
|
|
}
|
|
|
|
|
2016-04-15 13:11:23 +02:00
|
|
|
function Mac(instance, callback) {
|
|
|
|
var temp = Node.os.tmpdir();
|
|
|
|
if (!temp) return callback(new Error('os.tmpdir() not defined.'));
|
|
|
|
var user = Node.process.env.USER; // Applet shell scripts require $USER.
|
|
|
|
if (!user) return callback(new Error('env[\'USER\'] not defined.'));
|
|
|
|
UUID(instance,
|
|
|
|
function(error, uuid) {
|
|
|
|
if (error) return callback(error);
|
|
|
|
instance.uuid = uuid;
|
|
|
|
instance.path = Node.path.join(
|
|
|
|
temp,
|
|
|
|
instance.uuid,
|
|
|
|
instance.options.name + '.app'
|
|
|
|
);
|
|
|
|
function end(error, stdout, stderr) {
|
|
|
|
Remove(Node.path.dirname(instance.path),
|
|
|
|
function(errorRemove) {
|
|
|
|
if (error) return callback(error);
|
|
|
|
if (errorRemove) return callback(errorRemove);
|
2016-07-15 17:13:03 +02:00
|
|
|
callback(undefined, stdout, stderr);
|
2016-04-15 13:11:23 +02:00
|
|
|
}
|
|
|
|
);
|
2015-11-17 14:40:53 +01:00
|
|
|
}
|
2016-04-15 13:11:23 +02:00
|
|
|
MacApplet(instance,
|
|
|
|
function(error) {
|
|
|
|
if (error) return end(error);
|
|
|
|
MacIcon(instance,
|
|
|
|
function(error) {
|
|
|
|
if (error) return end(error);
|
|
|
|
MacPropertyList(instance,
|
|
|
|
function(error) {
|
|
|
|
if (error) return end(error);
|
|
|
|
MacCommand(instance,
|
|
|
|
function(error) {
|
|
|
|
if (error) return end(error);
|
|
|
|
MacOpen(instance,
|
|
|
|
function(error) {
|
|
|
|
if (error) return end(error);
|
|
|
|
MacResult(instance, end);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
2015-11-17 14:40:53 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-04-15 13:11:23 +02:00
|
|
|
function MacApplet(instance, end) {
|
|
|
|
var parent = Node.path.dirname(instance.path);
|
|
|
|
Node.fs.mkdir(parent,
|
2016-02-09 08:30:04 +01:00
|
|
|
function(error) {
|
|
|
|
if (error) return end(error);
|
2016-04-15 13:11:23 +02:00
|
|
|
var zip = Node.path.join(parent, 'sudo-prompt-applet.zip');
|
|
|
|
Node.fs.writeFile(zip, APPLET, 'base64',
|
|
|
|
function(error) {
|
|
|
|
if (error) return end(error);
|
|
|
|
var command = [];
|
|
|
|
command.push('/usr/bin/unzip');
|
|
|
|
command.push('-o'); // Overwrite any existing applet.
|
|
|
|
command.push('"' + EscapeDoubleQuotes(zip) + '"');
|
|
|
|
command.push('-d "' + EscapeDoubleQuotes(instance.path) + '"');
|
|
|
|
command = command.join(' ');
|
|
|
|
Node.child.exec(command, end);
|
|
|
|
}
|
|
|
|
);
|
2016-02-09 08:30:04 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-04-15 13:11:23 +02:00
|
|
|
function MacCommand(instance, end) {
|
|
|
|
var path = Node.path.join(
|
|
|
|
instance.path,
|
|
|
|
'Contents',
|
|
|
|
'MacOS',
|
|
|
|
'sudo-prompt-command'
|
|
|
|
);
|
2016-04-16 12:29:20 +02:00
|
|
|
var script = [];
|
|
|
|
// Preserve current working directory.
|
|
|
|
// We do this for commands that rely on relative paths.
|
|
|
|
// This runs in a subshell and will not change the cwd of sudo-prompt-script.
|
|
|
|
var cwd = Node.process.cwd();
|
|
|
|
script.push('cd "' + EscapeDoubleQuotes(cwd) + '"');
|
|
|
|
script.push(instance.command);
|
|
|
|
script = script.join('\n');
|
|
|
|
Node.fs.writeFile(path, script, 'utf-8', end);
|
2015-11-17 14:40:53 +01:00
|
|
|
}
|
|
|
|
|
2016-04-15 13:11:23 +02:00
|
|
|
function MacIcon(instance, end) {
|
|
|
|
if (!instance.options.icns) return end();
|
|
|
|
Node.fs.readFile(instance.options.icns,
|
|
|
|
function(error, buffer) {
|
2015-11-17 14:40:53 +01:00
|
|
|
if (error) return end(error);
|
2016-04-15 13:11:23 +02:00
|
|
|
var icns = Node.path.join(
|
|
|
|
instance.path,
|
|
|
|
'Contents',
|
|
|
|
'Resources',
|
|
|
|
'applet.icns'
|
2015-11-17 14:40:53 +01:00
|
|
|
);
|
2016-04-15 13:11:23 +02:00
|
|
|
Node.fs.writeFile(icns, buffer, end);
|
2015-11-17 14:40:53 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2015-06-08 15:24:56 +02:00
|
|
|
|
2016-04-15 13:11:23 +02:00
|
|
|
function MacOpen(instance, end) {
|
|
|
|
// We must run the binary directly so that the cwd will apply.
|
|
|
|
var binary = Node.path.join(instance.path, 'Contents', 'MacOS', 'applet');
|
|
|
|
// We must set the cwd so that the AppleScript can find the shell scripts.
|
|
|
|
var options = { cwd: Node.path.dirname(binary) };
|
2016-05-18 12:51:54 +02:00
|
|
|
// We use the relative path rather than the absolute path. The instance.path
|
|
|
|
// may contain spaces which the cwd can handle, but which exec() cannot.
|
|
|
|
Node.child.exec('./' + Node.path.basename(binary), options, end);
|
2016-04-15 13:11:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function MacPropertyList(instance, end) {
|
2015-11-17 14:40:53 +01:00
|
|
|
// Value must be in single quotes (not double quotes) according to man entry.
|
|
|
|
// e.g. defaults write com.companyname.appname "Default Color" '(255, 0, 0)'
|
|
|
|
// The defaults command will be changed in an upcoming major release to only
|
|
|
|
// operate on preferences domains. General plist manipulation utilities will
|
|
|
|
// be folded into a different command-line program.
|
2016-04-15 13:11:23 +02:00
|
|
|
var plist = Node.path.join(instance.path, 'Contents', 'Info.plist');
|
|
|
|
var path = EscapeDoubleQuotes(plist);
|
|
|
|
var key = EscapeDoubleQuotes('CFBundleName');
|
|
|
|
var value = instance.options.name + ' Password Prompt';
|
2015-11-17 14:40:53 +01:00
|
|
|
if (/'/.test(value)) {
|
|
|
|
return end(new Error('Value should not contain single quotes.'));
|
|
|
|
}
|
2016-04-15 13:11:23 +02:00
|
|
|
var command = [];
|
|
|
|
command.push('/usr/bin/defaults');
|
|
|
|
command.push('write');
|
|
|
|
command.push('"' + path + '"');
|
|
|
|
command.push('"' + key + '"');
|
|
|
|
command.push("'" + value + "'"); // We must use single quotes for value.
|
|
|
|
command = command.join(' ');
|
2015-11-17 14:40:53 +01:00
|
|
|
Node.child.exec(command, end);
|
|
|
|
}
|
2015-06-08 15:24:56 +02:00
|
|
|
|
2016-04-15 13:11:23 +02:00
|
|
|
function MacResult(instance, end) {
|
|
|
|
var cwd = Node.path.join(instance.path, 'Contents', 'MacOS');
|
|
|
|
Node.fs.readFile(Node.path.join(cwd, 'code'), 'utf-8',
|
|
|
|
function(error, code) {
|
|
|
|
if (error) {
|
|
|
|
if (error.code === 'ENOENT') return end(new Error(PERMISSION_DENIED));
|
|
|
|
end(error);
|
|
|
|
} else {
|
|
|
|
Node.fs.readFile(Node.path.join(cwd, 'stdout'), 'utf-8',
|
|
|
|
function(error, stdout) {
|
|
|
|
if (error) return end(error);
|
|
|
|
Node.fs.readFile(Node.path.join(cwd, 'stderr'), 'utf-8',
|
|
|
|
function(error, stderr) {
|
|
|
|
if (error) return end(error);
|
|
|
|
code = parseInt(code.trim(), 10); // Includes trailing newline.
|
|
|
|
if (code === 0) {
|
|
|
|
end(undefined, stdout, stderr);
|
|
|
|
} else {
|
2017-03-15 15:50:30 +01:00
|
|
|
error = new Error(
|
|
|
|
'Command failed: ' + instance.command + '\n' + stderr
|
|
|
|
);
|
|
|
|
error.code = code;
|
2016-04-15 13:11:23 +02:00
|
|
|
end(error, stdout, stderr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
2015-11-17 14:40:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-04-15 13:11:23 +02:00
|
|
|
function Remove(path, end) {
|
2016-07-15 17:13:03 +02:00
|
|
|
if (typeof path !== 'string' || !path.trim()) {
|
|
|
|
return end(new Error('Argument path not defined.'));
|
|
|
|
}
|
2016-04-15 13:11:23 +02:00
|
|
|
var command = [];
|
2016-07-15 17:13:03 +02:00
|
|
|
if (Node.process.platform === 'win32') {
|
|
|
|
if (/"/.test(path)) {
|
|
|
|
return end(new Error('Argument path cannot contain double-quotes.'));
|
|
|
|
}
|
|
|
|
command.push('rmdir /s /q "' + path + '"');
|
|
|
|
} else {
|
|
|
|
command.push('/bin/rm');
|
|
|
|
command.push('-rf');
|
|
|
|
command.push('"' + EscapeDoubleQuotes(Node.path.normalize(path)) + '"');
|
|
|
|
}
|
2016-04-15 13:11:23 +02:00
|
|
|
command = command.join(' ');
|
2015-11-17 14:40:53 +01:00
|
|
|
Node.child.exec(command, end);
|
|
|
|
}
|
|
|
|
|
2016-04-15 13:11:23 +02:00
|
|
|
function UUID(instance, end) {
|
|
|
|
Node.crypto.randomBytes(256,
|
|
|
|
function(error, random) {
|
|
|
|
if (error) random = Date.now() + '' + Math.random();
|
2015-11-17 14:40:53 +01:00
|
|
|
var hash = Node.crypto.createHash('SHA256');
|
2016-04-15 13:11:23 +02:00
|
|
|
hash.update('sudo-prompt-3');
|
|
|
|
hash.update(instance.options.name);
|
|
|
|
hash.update(instance.command);
|
|
|
|
hash.update(random);
|
2016-07-15 17:13:03 +02:00
|
|
|
var uuid = hash.digest('hex').slice(-32);
|
|
|
|
if (!uuid || typeof uuid !== 'string' || uuid.length !== 32) {
|
|
|
|
// This is critical to ensure we don't remove the wrong temp directory.
|
|
|
|
return end(new Error('Expected a valid UUID.'));
|
|
|
|
}
|
|
|
|
end(undefined, uuid);
|
2015-11-17 14:40:53 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-04-15 13:11:23 +02:00
|
|
|
function ValidName(string) {
|
|
|
|
// We use 70 characters as a limit to side-step any issues with Unicode
|
|
|
|
// normalization form causing a 255 character string to exceed the fs limit.
|
|
|
|
if (!/^[a-z0-9 ]+$/i.test(string)) return false;
|
|
|
|
if (string.trim().length === 0) return false;
|
|
|
|
if (string.length > 70) return false;
|
|
|
|
return true;
|
2015-11-17 14:40:53 +01:00
|
|
|
}
|
|
|
|
|
2016-07-15 17:13:03 +02:00
|
|
|
function Windows(instance, callback) {
|
|
|
|
var temp = Node.os.tmpdir();
|
|
|
|
if (!temp) return callback(new Error('os.tmpdir() not defined.'));
|
|
|
|
UUID(instance,
|
|
|
|
function(error, uuid) {
|
|
|
|
if (error) return callback(error);
|
|
|
|
instance.uuid = uuid;
|
|
|
|
instance.path = Node.path.join(temp, instance.uuid);
|
|
|
|
if (/"/.test(instance.path)) {
|
|
|
|
// We expect double quotes to be reserved on Windows.
|
|
|
|
// Even so, we test for this and abort if they are present.
|
|
|
|
return callback(
|
|
|
|
new Error('instance.path cannot contain double-quotes.')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
instance.pathElevate = Node.path.join(instance.path, 'elevate.vbs');
|
|
|
|
instance.pathExecute = Node.path.join(instance.path, 'execute.bat');
|
|
|
|
instance.pathCommand = Node.path.join(instance.path, 'command.bat');
|
|
|
|
instance.pathStdout = Node.path.join(instance.path, 'stdout');
|
|
|
|
instance.pathStderr = Node.path.join(instance.path, 'stderr');
|
|
|
|
instance.pathStatus = Node.path.join(instance.path, 'status');
|
|
|
|
Node.fs.mkdir(instance.path,
|
|
|
|
function(error) {
|
|
|
|
if (error) return callback(error);
|
|
|
|
function end(error, stdout, stderr) {
|
|
|
|
Remove(instance.path,
|
|
|
|
function(errorRemove) {
|
|
|
|
if (error) return callback(error);
|
|
|
|
if (errorRemove) return callback(errorRemove);
|
|
|
|
callback(undefined, stdout, stderr);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
WindowsWriteElevateScript(instance,
|
|
|
|
function(error) {
|
|
|
|
if (error) return end(error);
|
|
|
|
WindowsWriteExecuteScript(instance,
|
|
|
|
function(error) {
|
|
|
|
if (error) return end(error);
|
|
|
|
WindowsWriteCommandScript(instance,
|
|
|
|
function(error) {
|
|
|
|
if (error) return end(error);
|
|
|
|
WindowsElevate(instance,
|
|
|
|
function(error) {
|
|
|
|
if (error) return end(error);
|
|
|
|
WindowsWaitForStatus(instance,
|
|
|
|
function(error) {
|
|
|
|
if (error) return end(error);
|
|
|
|
WindowsResult(instance, end);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function WindowsElevate(instance, end) {
|
|
|
|
// We used to use this for executing elevate.vbs:
|
|
|
|
// var command = 'cscript.exe //NoLogo "' + instance.pathElevate + '"';
|
|
|
|
var command = [];
|
|
|
|
command.push('powershell.exe');
|
2017-06-20 10:03:07 +02:00
|
|
|
command.push('Start-Process');
|
|
|
|
command.push('-FilePath');
|
|
|
|
// Escape characters for cmd using double quotes:
|
|
|
|
// Escape characters for PowerShell using single quotes:
|
|
|
|
// Escape single quotes for PowerShell using backtick:
|
|
|
|
// See: https://ss64.com/ps/syntax-esc.html
|
|
|
|
command.push('"\'' + instance.pathExecute.replace(/'/g, "`'") + '\'"');
|
2016-07-15 17:13:03 +02:00
|
|
|
command.push('-WindowStyle hidden');
|
|
|
|
command.push('-Verb runAs');
|
|
|
|
command = command.join(' ');
|
|
|
|
var child = Node.child.exec(command,
|
|
|
|
function(error, stdout, stderr) {
|
|
|
|
if (error) {
|
|
|
|
if (/canceled by the user/i.test(error)) {
|
|
|
|
end(PERMISSION_DENIED);
|
|
|
|
} else {
|
|
|
|
end(error);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
end();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
child.stdin.end(); // Otherwise PowerShell waits indefinitely on Windows 7.
|
|
|
|
}
|
|
|
|
|
|
|
|
function WindowsResult(instance, end) {
|
|
|
|
Node.fs.readFile(instance.pathStatus, 'utf-8',
|
|
|
|
function(error, code) {
|
|
|
|
if (error) return end(error);
|
|
|
|
Node.fs.readFile(instance.pathStdout, 'utf-8',
|
|
|
|
function(error, stdout) {
|
|
|
|
if (error) return end(error);
|
|
|
|
Node.fs.readFile(instance.pathStderr, 'utf-8',
|
|
|
|
function(error, stderr) {
|
|
|
|
if (error) return end(error);
|
|
|
|
code = parseInt(code.trim(), 10);
|
|
|
|
if (code === 0) {
|
|
|
|
end(undefined, stdout, stderr);
|
|
|
|
} else {
|
2017-03-15 15:50:30 +01:00
|
|
|
error = new Error(
|
|
|
|
'Command failed: ' + instance.command + '\r\n' + stderr
|
|
|
|
);
|
|
|
|
error.code = code;
|
2016-07-15 17:13:03 +02:00
|
|
|
end(error, stdout, stderr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function WindowsWaitForStatus(instance, end) {
|
|
|
|
// VBScript cannot wait for the elevated process to finish so we have to poll.
|
|
|
|
// VBScript cannot return error code if user does not grant permission.
|
|
|
|
// PowerShell can be used to elevate and wait on Windows 10.
|
|
|
|
// PowerShell can be used to elevate on Windows 7 but it cannot wait.
|
|
|
|
// powershell.exe Start-Process cmd.exe -Verb runAs -Wait
|
|
|
|
Node.fs.stat(instance.pathStatus,
|
|
|
|
function(error, stats) {
|
|
|
|
if ((error && error.code === 'ENOENT') || stats.size < 2) {
|
|
|
|
// Retry if file does not exist or is not finished writing.
|
|
|
|
// We expect a file size of 2. That should cover at least "0\r".
|
|
|
|
// We use a 1 second timeout to keep a light footprint for long-lived
|
|
|
|
// sudo-prompt processes.
|
|
|
|
setTimeout(
|
|
|
|
function() {
|
2017-06-20 10:04:09 +02:00
|
|
|
// If administrator has no password and user clicks Yes, then
|
|
|
|
// PowerShell returns no error and execute (and command) never runs.
|
|
|
|
// We check that command output has been redirected to stdout file:
|
|
|
|
Node.fs.stat(instance.pathStdout,
|
|
|
|
function(error) {
|
|
|
|
if (error) return end(PERMISSION_DENIED);
|
|
|
|
WindowsWaitForStatus(instance, end);
|
|
|
|
}
|
|
|
|
);
|
2016-07-15 17:13:03 +02:00
|
|
|
},
|
|
|
|
1000
|
|
|
|
);
|
|
|
|
} else if (error) {
|
|
|
|
end(error);
|
|
|
|
} else {
|
|
|
|
end();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function WindowsWriteCommandScript(instance, end) {
|
|
|
|
var cwd = Node.process.cwd();
|
|
|
|
if (/"/.test(cwd)) {
|
|
|
|
// We expect double quotes to be reserved on Windows.
|
|
|
|
// Even so, we test for this and abort if they are present.
|
|
|
|
return end(new Error('process.cwd() cannot contain double-quotes.'));
|
|
|
|
}
|
|
|
|
var script = [];
|
|
|
|
script.push('@echo off');
|
2017-11-02 11:43:00 +01:00
|
|
|
// Set code page to UTF-8:
|
|
|
|
script.push('chcp 65001>nul');
|
2018-03-22 07:48:36 +01:00
|
|
|
// We pass /d as an option in case the cwd is on another drive (issue 70):
|
|
|
|
script.push('cd /d "' + cwd + '"');
|
2016-07-15 17:13:03 +02:00
|
|
|
script.push(instance.command);
|
|
|
|
script = script.join('\r\n');
|
|
|
|
Node.fs.writeFile(instance.pathCommand, script, 'utf-8', end);
|
|
|
|
}
|
|
|
|
|
|
|
|
function WindowsWriteElevateScript(instance, end) {
|
|
|
|
end();
|
|
|
|
// We do not use VBScript to elevate since it does not return an error if
|
|
|
|
// the user does not grant permission. This is here for reference.
|
|
|
|
// var script = [];
|
|
|
|
// script.push('Set objShell = CreateObject("Shell.Application")');
|
|
|
|
// script.push(
|
|
|
|
// 'objShell.ShellExecute "' + instance.pathExecute + '", "", "", "runas", 0'
|
|
|
|
// );
|
|
|
|
// script = script.join('\r\n');
|
|
|
|
// Node.fs.writeFile(instance.pathElevate, script, 'utf-8', end);
|
|
|
|
}
|
|
|
|
|
|
|
|
function WindowsWriteExecuteScript(instance, end) {
|
|
|
|
var script = [];
|
|
|
|
script.push('@echo off');
|
|
|
|
script.push(
|
|
|
|
'call "' + instance.pathCommand + '"' +
|
|
|
|
' > "' + instance.pathStdout + '" 2> "' + instance.pathStderr + '"'
|
|
|
|
);
|
|
|
|
script.push('(echo %ERRORLEVEL%) > "' + instance.pathStatus + '"');
|
|
|
|
script = script.join('\r\n');
|
|
|
|
Node.fs.writeFile(instance.pathExecute, script, 'utf-8', end);
|
|
|
|
}
|
|
|
|
|
2016-04-15 13:11:23 +02:00
|
|
|
module.exports.exec = Exec;
|
2015-11-17 14:40:53 +01:00
|
|
|
|
2016-04-15 13:11:23 +02:00
|
|
|
// We used to expect that applet.app would be included with this module.
|
|
|
|
// This could not be copied when sudo-prompt was packaged within an asar file.
|
2016-02-09 08:30:04 +01:00
|
|
|
// We now store applet.app as a zip file in base64 within index.js instead.
|
2016-04-15 13:11:23 +02:00
|
|
|
// To recreate: "zip -r ../applet.zip Contents" (with applet.app as CWD).
|
2016-02-09 08:30:04 +01:00
|
|
|
// The zip file must not include applet.app as the root directory so that we
|
|
|
|
// can extract it directly to the target app directory.
|
2016-04-15 13:11:23 +02:00
|
|
|
var APPLET = 'UEsDBAoAAAAAAO1YcEcAAAAAAAAAAAAAAAAJABwAQ29udGVudHMvVVQJAAPNnElWLZEQV3V4CwABBPUBAAAEFAAAAFBLAwQUAAAACACgeXBHlHaGqKEBAAC+AwAAEwAcAENvbnRlbnRzL0luZm8ucGxpc3RVVAkAA1zWSVYtkRBXdXgLAAEE9QEAAAQUAAAAfZNRb5swFIWfl1/BeA9OpSmqJkqVBCJFop1VyKQ9Ta59S6wa27NNCfv1M0naJWTsEXO+c8+9vo7v97UI3sBYruRdeBPNwgAkVYzL6i7cluvpbXifTOLP6bdV+QNngRbcugBvl/lmFYRThBZaC0AoLdMA55uiDLwHQtljGIQ75/RXhNq2jUiviqiqe6FF2CgNxnW5N5t6IGKOhb7M0f0ijj9lnLpk8il+hS5ZrZeNZAIWQqj2ge+B5YoSwX8T5xEbo17ktc40gIZQCm8glK5BuieovP5Dbp3xHSeZrHyCXYxO3wM+2wNtHHkWMAQP/bkxbkOVXPMxKuK0Dz6CMh+Wv3AwQ9gPM7INU1NtVK3Ha8sXlfoB+m6J6b4fRzv0mkezMf6R1Fe5MbG2VYYF+L+lMaGvpIKy01cOC4zzMazYKeNOQYuDYkjfjMcteCWJa8w/Zi2ugubFA5e8buqisw7qU81ltzB0xx3QC5/TFh7J/e385/zL+7+/wWbR/LwIOl/dvHiCXw03YFfEPJ9dwsWu5sV2kwnod3QoeLeL0eGdJJM/UEsDBAoAAAAAAHSBjkgAAAAAAAAAAAAAAAAPABwAQ29udGVudHMvTWFjT1MvVVQJAAMb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Wr93C/u7wbVKWyoDhVM/8XZAOPOXvcm+IyXxGcizeaU
|
|
|
|
|
|
|
|
var PERMISSION_DENIED = 'User did not grant permission.';
|
2016-08-01 04:52:06 +02:00
|
|
|
var NO_POLKIT_AGENT = 'No polkit authentication agent found.';
|
2018-01-10 14:26:24 +01:00
|
|
|
|
|
|
|
// See issue 66:
|
|
|
|
var MAX_BUFFER = 134217728;
|