You can now run agent console commands as a group action (#4343)
Ylian Saint-Hilaire committed
Jul 30, 2022 at 12:05 UTC
338d5b206129e4f7c3b664e52b46e684c795d937
2 files changed
+63
-41
meshuser.js
+53
-32
@@ -2809,44 +2809,65 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
2809
return;
2810
}
2811
2812
- // Check we have the rights to run commands on this device
2813
- if ((rights & MESHRIGHT_REMOTECOMMAND) == 0) {
2814
- if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'runcommands', responseid: command.responseid, result: 'Access denied' })); } catch (ex) { } }
2815
- return;
2816
- }
2812
+ if (command.type == 4) {
2813
+ // This is an agent console command
2814
2818
- // Get the agent and run the commands
2819
- var agent = parent.wsagents[node._id];
2820
- if ((agent != null) && (agent.authenticated == 2) && (agent.agentInfo != null)) {
2821
- // Check if this agent is correct for this command type
2822
- // command.type 1 = Windows Command, 2 = Windows PowerShell, 3 = Linux/BSD/macOS
2823
- var commandsOk = false;
2824
- if ((agent.agentInfo.agentId > 0) && (agent.agentInfo.agentId < 5)) {
2825
- // Windows Agent
2826
- if ((command.type == 1) || (command.type == 2)) { commandsOk = true; }
2827
- else if (command.type === 0) { command.type = 1; commandsOk = true; } // Set the default type of this agent
2828
- } else {
2829
- // Non-Windows Agent
2830
- if (command.type == 3) { commandsOk = true; }
2831
- else if (command.type === 0) { command.type = 3; commandsOk = true; } // Set the default type of this agent
2815
+ // Check we have the rights to run commands on this device, MESHRIGHT_REMOTECONTROL & MESHRIGHT_AGENTCONSOLE are needed
2816
+ if ((rights & 24) != 24) {
2817
+ if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'runcommands', responseid: command.responseid, result: 'Access denied' })); } catch (ex) { } }
2818
+ return;
2819
}
2833
- if (commandsOk == true) {
2834
- // Send the commands to the agent
2835
- try { agent.send(JSON.stringify({ action: 'runcommands', type: command.type, cmds: command.cmds, runAsUser: command.runAsUser })); } catch (ex) { }
2836
- if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'runcommands', responseid: command.responseid, result: 'OK' })); } catch (ex) { } }
2820
2838
- // Send out an event that these commands where run on this device
2839
- var targets = parent.CreateNodeDispatchTargets(node.meshid, node._id, ['server-users', user._id]);
2840
- var msgid = 24; // "Running commands"
2841
- if (command.type == 1) { msgid = 99; } // "Running commands as user"
2842
- if (command.type == 2) { msgid = 100; } // "Running commands as user if possible"
2843
- var event = { etype: 'node', userid: user._id, username: user.name, nodeid: node._id, action: 'runcommands', msg: 'Running commands', msgid: msgid, cmds: command.cmds, cmdType: command.type, runAsUser: command.runAsUser, domain: domain.id };
2844
- parent.parent.DispatchEvent(targets, obj, event);
2821
+ // Send the commands to the agent
2822
+ var agent = parent.wsagents[node._id];
2823
+ if ((agent != null) && (agent.authenticated == 2) && (agent.agentInfo != null)) {
2824
+ try { agent.send(JSON.stringify({ action: 'msg', type: 'console', value: command.cmds, rights: rights, sessionid: ws.sessionId })); } catch (ex) { }
2825
+ if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'runcommands', responseid: command.responseid, result: 'OK' })); } catch (ex) { } }
2826
} else {
2846
- if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'runcommands', responseid: command.responseid, result: 'Invalid command type' })); } catch (ex) { } }
2827
+ if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'runcommands', responseid: command.responseid, result: 'Agent not connected' })); } catch (ex) { } }
2828
}
2829
} else {
2849
- if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'runcommands', responseid: command.responseid, result: 'Agent not connected' })); } catch (ex) { } }
2830
+ // This is a standard (bash/shell/powershell) command.
2831
+
2832
+ // Check we have the rights to run commands on this device
2833
+ if ((rights & MESHRIGHT_REMOTECOMMAND) == 0) {
2834
+ if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'runcommands', responseid: command.responseid, result: 'Access denied' })); } catch (ex) { } }
2835
+ return;
2836
+ }
2837
+
2838
+ // Get the agent and run the commands
2839
+ var agent = parent.wsagents[node._id];
2840
+ if ((agent != null) && (agent.authenticated == 2) && (agent.agentInfo != null)) {
2841
+ // Check if this agent is correct for this command type
2842
+ // command.type 1 = Windows Command, 2 = Windows PowerShell, 3 = Linux/BSD/macOS
2843
+ var commandsOk = false;
2844
+ if ((agent.agentInfo.agentId > 0) && (agent.agentInfo.agentId < 5)) {
2845
+ // Windows Agent
2846
+ if ((command.type == 1) || (command.type == 2)) { commandsOk = true; }
2847
+ else if (command.type === 0) { command.type = 1; commandsOk = true; } // Set the default type of this agent
2848
+ } else {
2849
+ // Non-Windows Agent
2850
+ if (command.type == 3) { commandsOk = true; }
2851
+ else if (command.type === 0) { command.type = 3; commandsOk = true; } // Set the default type of this agent
2852
+ }
2853
+ if (commandsOk == true) {
2854
+ // Send the commands to the agent
2855
+ try { agent.send(JSON.stringify({ action: 'runcommands', type: command.type, cmds: command.cmds, runAsUser: command.runAsUser })); } catch (ex) { }
2856
+ if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'runcommands', responseid: command.responseid, result: 'OK' })); } catch (ex) { } }
2857
+
2858
+ // Send out an event that these commands where run on this device
2859
+ var targets = parent.CreateNodeDispatchTargets(node.meshid, node._id, ['server-users', user._id]);
2860
+ var msgid = 24; // "Running commands"
2861
+ if (command.type == 1) { msgid = 99; } // "Running commands as user"
2862
+ if (command.type == 2) { msgid = 100; } // "Running commands as user if possible"
2863
+ var event = { etype: 'node', userid: user._id, username: user.name, nodeid: node._id, action: 'runcommands', msg: 'Running commands', msgid: msgid, cmds: command.cmds, cmdType: command.type, runAsUser: command.runAsUser, domain: domain.id };
2864
+ parent.parent.DispatchEvent(targets, obj, event);
2865
+ } else {
2866
+ if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'runcommands', responseid: command.responseid, result: 'Invalid command type' })); } catch (ex) { } }
2867
+ }
2868
+ } else {
2869
+ if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'runcommands', responseid: command.responseid, result: 'Agent not connected' })); } catch (ex) { } }
2870
+ }
2871
}
2872
});
2873
}
views/default.handlebars
+10
-9
@@ -5534,24 +5534,24 @@
5534
p2downloadDeviceInfo();
5535
} else if (op == 106) {
5536
// Run commands
5537
- var wintype = false, linuxtype = false, chkNodeIds = getCheckedDevices();
5537
+ var wintype = false, linuxtype = false, agenttype = false, chkNodeIds = getCheckedDevices();
5538
for (var i in chkNodeIds) {
5539
var n = getNodeFromId(chkNodeIds[i]);
5540
- if (n.agent) { if ((n.agent.id > 0) && (n.agent.id < 5)) { wintype = true; } else { linuxtype = true; } }
5540
+ if (n.agent) { if ((GetNodeRights(n) & 24) == 24) { agenttype = true; } if ((n.agent.id > 0) && (n.agent.id < 5)) { wintype = true; } else { linuxtype = true; } }
5541
}
5542
- if ((wintype == true) || (linuxtype == true)) {
5542
+ if ((wintype == true) || (linuxtype == true) || (agenttype == true)) {
5543
var x = "Run commands on selected devices." + '<br />';
5544
- if (wintype == true) {
5545
- x += '<select id=d2cmdtype style=width:100%;margin-bottom:4px;margin-top:4px>';
5546
- x += '<option value=1>' + "Windows Command Prompt" + '</option><option value=2>' + "Windows PowerShell" + '</option>';
5547
- if (linuxtype == true) { x += '<option value=3>' + "Linux/BSD/macOS Command Shell" + '</option>'; }
5548
- x += '</select>';
5549
- }
5544
+ x += '<select id=d2cmdtype onclick=d2runCommandValidate() style=width:100%;margin-bottom:4px;margin-top:4px>';
5545
+ if (wintype == true) { x += '<option value=1>' + "Windows Command Prompt" + '</option><option value=2>' + "Windows PowerShell" + '</option>'; }
5546
+ if (linuxtype == true) { x += '<option value=3>' + "Linux/BSD/macOS Command Shell" + '</option>'; }
5547
+ if (agenttype == true) { x += '<option value=4>' + "Agent console command" + '</option>'; } // MESHRIGHT_REMOTECONTROL & MESHRIGHT_AGENTCONSOLE are needed
5548
+ x += '</select>';
5549
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>';
5550
x += '<textarea id=d2runcmd style=background-color:#fcf3cf;width:100%;height:200px;resize:none;overflow-y:scroll></textarea>';
5551
setDialogMode(2, "Run Commands", 3, d2groupActionFunctionRunCommands, x);
5552
Q('d2runcmd').focus();
5553
//QE('idx_dlgOkButton', true);
5554
+ d2runCommandValidate();
5555
}
5556
} else if (op == 107) {
5557
// Edit tags
@@ -5608,6 +5608,7 @@
5608
}
5609
}
5610
5611
+ function d2runCommandValidate() { QV('d2cmduser', Q('d2cmdtype').value < 4); }
5612
function d2batchUploadValidate() { QE('idx_dlgOkButton', (Q('d2uploadinput').files.length != 0) && ((Q('d2winuploadpath') == null) || (Q('d2winuploadpath').value != '')) && ((Q('d2linuxuploadpath') == null) || (Q('d2linuxuploadpath').value != ''))); }
5613
function d2batchUploadValidateOk() { Q('d2batchUploadSubmit').click(); }
5614
function d2groupActionFunctionAgentUpdateExec() { meshserver.send({ action: 'updateAgents', nodeids: getCheckedDevices() }); }