Added config command to MeshCtrl.
Ylian Saint-Hilaire committed
Nov 22, 2019 at 16:17 UTC
04f0e7a321247c94e3efb1104b4a31b9ab91a1c1
3 files changed
+79
-3
agents/meshcore.js
+1
-1
@@ -1103,7 +1103,7 @@ function createMeshCore(agent) {
1103
this.httprequest.process = childProcess.execFile("/bin/bash", ["bash", "-i"], { type: childProcess.SpawnTypes.TERM });
1104
if (process.platform == 'linux') { this.httprequest.process.stdin.write("alias ls='ls --color=auto'\nclear\n"); }
1105
} else {
1106
- this.httprequest.process = childProcess.execFile("/bin/sh", ["sh"], { type: childProcess.SpawnTypes.TERM });
1106
+ this.httprequest.process = childProcess.execFile("/bin/sh", ["sh"], { type: childProcess.SpawnTypes.TERM }); // , uid: require('user-sessions').consoleUid()
1107
if (process.platform == 'linux') { this.httprequest.process.stdin.write("stty erase ^H\nalias ls='ls --color=auto'\nPS1='\\u@\\h:\\w\\$ '\nclear\n"); }
1108
}
1109
this.httprequest.process.tunnel = this;
meshctrl.js
+77
-1
@@ -3,7 +3,7 @@
3
const crypto = require('crypto');
4
var settings = {};
5
const args = require('minimist')(process.argv.slice(2));
6
-const possibleCommands = ['listusers', 'listdevicegroups', 'listdevices', 'listusersofdevicegroup', 'serverinfo', 'userinfo', 'adduser', 'removeuser', 'adddevicegroup', 'removedevicegroup', 'broadcast', 'addusertodevicegroup', 'removeuserfromdevicegroup', 'sendinviteemail', 'generateinvitelink'];
6
+const possibleCommands = ['listusers', 'listdevicegroups', 'listdevices', 'listusersofdevicegroup', 'serverinfo', 'userinfo', 'adduser', 'removeuser', 'adddevicegroup', 'removedevicegroup', 'broadcast', 'addusertodevicegroup', 'removeuserfromdevicegroup', 'sendinviteemail', 'generateinvitelink', 'config'];
7
//console.log(args);
8
9
if (args['_'].length == 0) {
@@ -18,6 +18,7 @@ if (args['_'].length == 0) {
18
console.log(" ListDevices - List devices.");
19
console.log(" ListDeviceGroups - List device groups.");
20
console.log(" ListUsersOfDeviceGroup - List the users in a device group.");
21
+ console.log(" Config - Perform operation on config.json file.");
22
console.log(" AddUser - Create a new user account.");
23
console.log(" RemoveUser - Delete a user account.");
24
console.log(" AddDeviceGroup - Create a new device group.");
@@ -43,6 +44,7 @@ if (args['_'].length == 0) {
44
45
var ok = false;
46
switch (settings.cmd) {
47
+ case 'config': { performConfigOperations(args); return; }
48
case 'serverinfo': { ok = true; break; }
49
case 'userinfo': { ok = true; break; }
50
case 'listusers': { ok = true; break; }
@@ -108,6 +110,21 @@ if (args['_'].length == 0) {
110
console.log("Get help on an action. Type:\r\n\r\n help [action]\r\n\r\nPossible actions are: " + possibleCommands.join(', ') + '.');
111
} else {
112
switch (args['_'][1].toLowerCase()) {
113
+ case 'config': {
114
+ console.log("Perform operations on the config.json file. Example usage:\r\n");
115
+ console.log(" MeshCtrl config --show");
116
+ console.log("\r\nOptional arguments:\r\n");
117
+ console.log(" --show - Display the config.json file.");
118
+ console.log(" --adddomain [domain] - Add a domain.");
119
+ console.log(" --removedomain [domain] - Remove a domain.");
120
+ console.log(" --settodomain [domain] - Set values to the domain.");
121
+ console.log(" --removefromdomain [domain] - Remove values from the domain.");
122
+ console.log("\r\nWith adddomain, removedomain, settodomain and removefromdomain you can add the key and value pair. For example:\r\n");
123
+ console.log(" --adddomain \"MyDomain\" --title \"My Server Name\" --newAccounts false");
124
+ console.log(" --settodomain \"MyDomain\" --title \"My Server Name\"");
125
+ console.log(" --removefromdomain \"MyDomain\" --title");
126
+ break;
127
+ }
128
case 'sendinviteemail': {
129
console.log("Send invitation email with instructions on how to install the mesh agent for a specific device group. Example usage:\r\n");
130
console.log(" MeshCtrl SendInviteEmail --id devicegroupid --email user@sample.com");
@@ -278,6 +295,65 @@ if (args['_'].length == 0) {
295
if (ok) { serverConnect(); }
296
}
297
298
+function performConfigOperations(args) {
299
+ var domainValues = ['title', 'title2', 'titlepicture', 'trustedcert', 'welcomepicture', 'welcometext', 'userquota', 'meshquota', 'newaccounts', 'usernameisemail', 'newaccountemaildomains', 'newaccountspass', 'newaccountsrights', 'geolocation', 'lockagentdownload', 'userconsentflags', 'Usersessionidletimeout', 'auth', 'ldapoptions', 'ldapusername', 'ldapuserbinarykey', 'ldapoptions', 'footer', 'certurl', 'loginKey', 'userallowedip', 'agentallowedip', 'agentnoproxy', 'agentconfig', 'orphanagentuser', 'httpheaders', 'yubikey', 'passwordrequirements', 'limits', 'amtacmactivation', 'redirects', 'sessionrecording'];
300
+ var configChange = false;
301
+ var fs = require('fs');
302
+ var path = require('path');
303
+ var configFile = path.join(__dirname, 'config.json');
304
+ if (fs.existsSync(configFile) == false) { configFile = path.join(__dirname, 'meshcentral-data', 'config.json'); }
305
+ if (fs.existsSync(configFile) == false) { configFile = path.join(__dirname, '..', 'meshcentral-data', 'config.json'); }
306
+ if (fs.existsSync(configFile) == false) { console.log("Unable to find config.json."); return; }
307
+ var config = null;
308
+ try { config = fs.readFileSync(configFile); } catch (ex) { console.log("Error: Unable to read config.json"); return; }
309
+ try { config = JSON.parse(config); } catch (ex) { console.log("Error: Unable to parse config.json"); return; }
310
+ if (args.adddomain != null) {
311
+ if (config.domains == null) { config.domains = {}; }
312
+ if (config.domains[args.adddomain] != null) { console.log("Error: Domain \"" + args.adddomain + "\" already exists"); }
313
+ else {
314
+ configChange = true;
315
+ config.domains[args.adddomain] = {};
316
+ for (var i in args) {
317
+ if (domainValues.indexOf(i.toLowerCase()) >= 0) {
318
+ if (args[i] == 'true') { args[i] = true; } else if (args[i] == 'false') { args[i] = false; } else if (parseInt(args[i]) == args[i]) { args[i] = parseInt(args[i]); }
319
+ config.domains[args.adddomain][i] = args[i];
320
+ configChange = true;
321
+ }
322
+ }
323
+ }
324
+ }
325
+ if (args.removedomain != null) {
326
+ if (config.domains == null) { config.domains = {}; }
327
+ if (config.domains[args.removedomain] == null) { console.log("Error: Domain \"" + args.removedomain + "\" does not exist"); }
328
+ else { delete config.domains[args.removedomain]; configChange = true; }
329
+ }
330
+ if (args.settodomain != null) {
331
+ if (config.domains == null) { config.domains = {}; }
332
+ if (config.domains[args.settodomain] == null) { console.log("Error: Domain \"" + args.settodomain + "\" does not exist"); }
333
+ else {
334
+ for (var i in args) {
335
+ if (domainValues.indexOf(i.toLowerCase()) >= 0) {
336
+ if (args[i] == '') { delete config.domains[args.settodomain][i]; configChange = true; } else {
337
+ if (args[i] == 'true') { args[i] = true; } else if (args[i] == 'false') { args[i] = false; } else if (parseInt(args[i]) == args[i]) { args[i] = parseInt(args[i]); }
338
+ config.domains[args.settodomain][i] = args[i];
339
+ configChange = true;
340
+ }
341
+ }
342
+ }
343
+ }
344
+ }
345
+ if (args.removefromdomain != null) {
346
+ if (config.domains == null) { config.domains = {}; }
347
+ if (config.domains[args.removefromdomain] == null) { console.log("Error: Domain \"" + args.removefromdomain + "\" does not exist"); }
348
+ else { for (var i in args) { if (domainValues.indexOf(i.toLowerCase()) >= 0) { delete config.domains[args.removefromdomain][i]; configChange = true; } } }
349
+ }
350
+ if (args.show == 1) { console.log(JSON.stringify(config, null, 2)); } else { console.log("Done."); }
351
+ if (configChange) {
352
+ try { fs.writeFileSync(configFile, JSON.stringify(config, null, 2)); } catch (ex) { console.log("Error: Unable to read config.json"); return; }
353
+ }
354
+ //console.log(configFile);
355
+}
356
+
357
function onVerifyServer(clientName, certs) { return null; }
358
359
function serverConnect() {
package.json
+1
-1
@@ -1,6 +1,6 @@
1
{
2
"name": "meshcentral",
3
- "version": "0.4.4-r",
3
+ "version": "0.4.4-s",
4
"keywords": [
5
"Remote Management",
6
"Intel AMT",