mirror of
https://github.com/Ylianst/MeshCentral.git
synced 2024-11-26 07:57:56 +03:00
Added run as user support to batch commands.
This commit is contained in:
parent
e97e3db8d3
commit
5de23d6341
@ -946,11 +946,23 @@ function createMeshCore(agent) {
|
||||
case 'runcommands': {
|
||||
if (mesh.cmdchild != null) { sendConsoleText("Run commands can't execute, already busy."); break; }
|
||||
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 (data.type == 1) {
|
||||
// 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.stdout.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; });
|
||||
} else if (data.type == 2) {
|
||||
// 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.stdout.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) {
|
||||
// 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.stdout.on('data', function (c) { sendConsoleText(c.toString()); });
|
||||
mesh.cmdchild.stderr.on('data', function (c) { sendConsoleText(c.toString()); });
|
||||
|
@ -557,6 +557,8 @@ if (args['_'].length == 0) {
|
||||
console.log(" --run \"[command]\" - Shell command to execute on the remote device.");
|
||||
console.log("\r\nOptional arguments:\r\n");
|
||||
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;
|
||||
}
|
||||
case 'shell': {
|
||||
@ -1031,7 +1033,9 @@ function serverConnect() {
|
||||
break;
|
||||
}
|
||||
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;
|
||||
}
|
||||
case 'shell':
|
||||
|
@ -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 (typeof command.type != 'number') break; // Check command type
|
||||
if (typeof command.cmds != 'string') break; // Check commands
|
||||
if (typeof command.runAsUser != 'number') { command.runAsUser = 0; } // Check runAsUser
|
||||
|
||||
for (i in command.nodeids) {
|
||||
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) {
|
||||
// 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) { } }
|
||||
} else {
|
||||
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
@ -4452,13 +4452,14 @@
|
||||
if (n.agent) { if ((n.agent.id > 0) && (n.agent.id < 5)) { wintype = true; } else { 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) {
|
||||
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>';
|
||||
if (linuxtype == true) { x += '<option value=3>' + "Linux/BSD/macOS Command Shell" + '</option>'; }
|
||||
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>';
|
||||
setDialogMode(2, "Run Commands", 3, d2groupActionFunctionRunCommands, x);
|
||||
Q('d2runcmd').focus();
|
||||
@ -4549,7 +4550,7 @@
|
||||
function d2groupActionFunctionRunCommands() {
|
||||
var type = 3;
|
||||
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) {
|
||||
|
Loading…
Reference in New Issue
Block a user