Added run as user support to batch commands.

This commit is contained in:
Ylian Saint-Hilaire 2020-09-29 13:20:08 -07:00
parent 3cd39d37c2
commit 7a22f8c702
5 changed files with 1659 additions and 1623 deletions

View File

@ -946,11 +946,23 @@ function createMeshCore(agent) {
case 'runcommands': { case 'runcommands': {
if (mesh.cmdchild != null) { sendConsoleText("Run commands can't execute, already busy."); break; } if (mesh.cmdchild != null) { sendConsoleText("Run commands can't execute, already busy."); break; }
MeshServerLogEx(24, null, "Running commands", data); MeshServerLogEx(24, null, "Running commands", data);
sendConsoleText("Run commands: " + data.cmds); sendConsoleText("Run commands (" + data.runAsUser + "): " + data.cmds);
// data.runAsUser: 0=Agent,1=UserOrAgent,2=UserOnly
var options = {};
if (data.runAsUser > 0) {
try { options.uid = require('user-sessions').consoleUid(); } catch (ex) { }
options.type = require('child_process').SpawnTypes.TERM;
}
if (data.runAsUser == 2) {
if (options.uid == null) break;
if (((require('user-sessions').minUid != null) && (options.uid < require('user-sessions').minUid()))) break; // This command can only run as user.
}
if (process.platform == 'win32') { if (process.platform == 'win32') {
if (data.type == 1) { if (data.type == 1) {
// Windows command shell // Windows command shell
mesh.cmdchild = require('child_process').execFile(process.env['windir'] + '\\system32\\cmd.exe', ['cmd']); mesh.cmdchild = require('child_process').execFile(process.env['windir'] + '\\system32\\cmd.exe', ['cmd'], options);
mesh.cmdchild.descriptorMetadata = 'UserCommandsShell'; mesh.cmdchild.descriptorMetadata = 'UserCommandsShell';
mesh.cmdchild.stdout.on('data', function (c) { sendConsoleText(c.toString()); }); mesh.cmdchild.stdout.on('data', function (c) { sendConsoleText(c.toString()); });
mesh.cmdchild.stderr.on('data', function (c) { sendConsoleText(c.toString()); }); mesh.cmdchild.stderr.on('data', function (c) { sendConsoleText(c.toString()); });
@ -958,7 +970,7 @@ function createMeshCore(agent) {
mesh.cmdchild.on('exit', function () { sendConsoleText("Run commands completed."); delete mesh.cmdchild; }); mesh.cmdchild.on('exit', function () { sendConsoleText("Run commands completed."); delete mesh.cmdchild; });
} else if (data.type == 2) { } else if (data.type == 2) {
// Windows Powershell // Windows Powershell
mesh.cmdchild = require('child_process').execFile(process.env['windir'] + '\\System32\\WindowsPowerShell\\v1.0\\powershell.exe', ['powershell', '-noprofile', '-nologo', '-command', '-']); mesh.cmdchild = require('child_process').execFile(process.env['windir'] + '\\System32\\WindowsPowerShell\\v1.0\\powershell.exe', ['powershell', '-noprofile', '-nologo', '-command', '-'], options);
mesh.cmdchild.descriptorMetadata = 'UserCommandsPowerShell'; mesh.cmdchild.descriptorMetadata = 'UserCommandsPowerShell';
mesh.cmdchild.stdout.on('data', function (c) { sendConsoleText(c.toString()); }); mesh.cmdchild.stdout.on('data', function (c) { sendConsoleText(c.toString()); });
mesh.cmdchild.stderr.on('data', function (c) { sendConsoleText(c.toString()); }); mesh.cmdchild.stderr.on('data', function (c) { sendConsoleText(c.toString()); });
@ -967,7 +979,7 @@ function createMeshCore(agent) {
} }
} else if (data.type == 3) { } else if (data.type == 3) {
// Linux shell // Linux shell
mesh.cmdchild = require('child_process').execFile('/bin/sh', ['sh']); mesh.cmdchild = require('child_process').execFile('/bin/sh', ['sh'], options);
mesh.cmdchild.descriptorMetadata = 'UserCommandsShell'; mesh.cmdchild.descriptorMetadata = 'UserCommandsShell';
mesh.cmdchild.stdout.on('data', function (c) { sendConsoleText(c.toString()); }); mesh.cmdchild.stdout.on('data', function (c) { sendConsoleText(c.toString()); });
mesh.cmdchild.stderr.on('data', function (c) { sendConsoleText(c.toString()); }); mesh.cmdchild.stderr.on('data', function (c) { sendConsoleText(c.toString()); });

View File

@ -557,6 +557,8 @@ if (args['_'].length == 0) {
console.log(" --run \"[command]\" - Shell command to execute on the remote device."); console.log(" --run \"[command]\" - Shell command to execute on the remote device.");
console.log("\r\nOptional arguments:\r\n"); console.log("\r\nOptional arguments:\r\n");
console.log(" --powershell - Run in Windows PowerShell."); console.log(" --powershell - Run in Windows PowerShell.");
console.log(" --runasuser - Attempt to run the command as logged in user.");
console.log(" --runasuseronly - Only run the command as the logged in user.");
break; break;
} }
case 'shell': { case 'shell': {
@ -1031,7 +1033,9 @@ function serverConnect() {
break; break;
} }
case 'runcommand': { case 'runcommand': {
ws.send(JSON.stringify({ action: 'runcommands', nodeids: [args.id], type: ((args.powershell) ? 2 : 0), cmds: args.run, responseid: 'meshctrl' })); var runAsUser = 0;
if (args.runasuser) { runAsUser = 1; } else if (args.runasuseronly) { runAsUser = 2; }
ws.send(JSON.stringify({ action: 'runcommands', nodeids: [args.id], type: ((args.powershell) ? 2 : 0), cmds: args.run, responseid: 'meshctrl', runAsUser: runAsUser }));
break; break;
} }
case 'shell': case 'shell':

View File

@ -3534,6 +3534,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
if (common.validateArray(command.nodeids, 1) == false) break; // Check nodeid's if (common.validateArray(command.nodeids, 1) == false) break; // Check nodeid's
if (typeof command.type != 'number') break; // Check command type if (typeof command.type != 'number') break; // Check command type
if (typeof command.cmds != 'string') break; // Check commands if (typeof command.cmds != 'string') break; // Check commands
if (typeof command.runAsUser != 'number') { command.runAsUser = 0; } // Check runAsUser
for (i in command.nodeids) { for (i in command.nodeids) {
var nodeid = command.nodeids[i], err = null; var nodeid = command.nodeids[i], err = null;
@ -3580,7 +3581,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
} }
if (commandsOk == true) { if (commandsOk == true) {
// Send the commands to the agent // Send the commands to the agent
try { agent.send(JSON.stringify({ action: 'runcommands', type: command.type, cmds: command.cmds })); } catch (ex) { } try { agent.send(JSON.stringify({ action: 'runcommands', type: command.type, cmds: command.cmds, runAsUser: command.runAsUser })); } catch (ex) { }
if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'runcommands', responseid: command.responseid, result: 'OK' })); } catch (ex) { } } if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'runcommands', responseid: command.responseid, result: 'OK' })); } catch (ex) { } }
} else { } else {
if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'runcommands', responseid: command.responseid, result: 'Invalid command type' })); } catch (ex) { } } if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'runcommands', responseid: command.responseid, result: 'Invalid command type' })); } catch (ex) { } }

File diff suppressed because it is too large Load Diff

View File

@ -4452,13 +4452,14 @@
if (n.agent) { if ((n.agent.id > 0) && (n.agent.id < 5)) { wintype = true; } else { linuxtype = true; } } if (n.agent) { if ((n.agent.id > 0) && (n.agent.id < 5)) { wintype = true; } else { linuxtype = true; } }
} }
if ((wintype == true) || (linuxtype == true)) { if ((wintype == true) || (linuxtype == true)) {
var x = "Run commands on selected devices." + '<br /><br />'; var x = "Run commands on selected devices." + '<br />';
if (wintype == true) { if (wintype == true) {
x += '<select id=d2cmdtype style=width:100%>'; x += '<select id=d2cmdtype style=width:100%;margin-bottom:4px;margin-top:4px>';
x += '<option value=1>' + "Windows Command Prompt" + '</option><option value=2>' + "Windows PowerShell" + '</option>'; x += '<option value=1>' + "Windows Command Prompt" + '</option><option value=2>' + "Windows PowerShell" + '</option>';
if (linuxtype == true) { x += '<option value=3>' + "Linux/BSD/macOS Command Shell" + '</option>'; } if (linuxtype == true) { x += '<option value=3>' + "Linux/BSD/macOS Command Shell" + '</option>'; }
x += '</select>'; x += '</select>';
} }
x += '<select id=d2cmduser style=width:100%;margin-bottom:4px><option value=0>' + "Run as agent" + '</option><option value=1>' + "Run as user, agent if no user" + '</option><option value=2>' + "Must run as user" + '</option></select>';
x += '<textarea id=d2runcmd style=background-color:#fcf3cf;width:100%;height:200px;resize:none;overflow-y:scroll></textarea>'; x += '<textarea id=d2runcmd style=background-color:#fcf3cf;width:100%;height:200px;resize:none;overflow-y:scroll></textarea>';
setDialogMode(2, "Run Commands", 3, d2groupActionFunctionRunCommands, x); setDialogMode(2, "Run Commands", 3, d2groupActionFunctionRunCommands, x);
Q('d2runcmd').focus(); Q('d2runcmd').focus();
@ -4549,7 +4550,7 @@
function d2groupActionFunctionRunCommands() { function d2groupActionFunctionRunCommands() {
var type = 3; var type = 3;
try { type = parseInt(Q('d2cmdtype').value); } catch (ex) { } try { type = parseInt(Q('d2cmdtype').value); } catch (ex) { }
meshserver.send({ action: 'runcommands', nodeids: getCheckedDevices(), type: type, cmds: Q('d2runcmd').value }); uncheckAllDevices(); meshserver.send({ action: 'runcommands', nodeids: getCheckedDevices(), type: type, cmds: Q('d2runcmd').value, runAsUser: parseInt(Q('d2cmduser').value) }); uncheckAllDevices();
} }
function onSortSelectChange(skipsave) { function onSortSelectChange(skipsave) {