Added --removesubdomain command, #3440

Ylian Saint-Hilaire committed Jan 11, 2022 at 17:30 UTC 01dbc70098c8b530453d47938526bb799dc3d745
5 files changed +51 -7
db.js
+27 -3
@@ -225,13 +225,37 @@ module.exports.CreateDB = function (parent, func) {
225 }
226 }
227
228 + // Remove all reference to a domain from the database
229 + obj.removeDomain = function (domainName, func) {
230 + var pendingCalls;
231 + // Remove all events, power events and SMBIOS data from the main collection. They are all in seperate collections now.
232 + if ((obj.databaseType == 4) || (obj.databaseType == 5) || (obj.databaseType == 6)) {
233 + // MariaDB, MySQL or PostgreSQL
234 + pendingCalls = 2;
235 + sqlDbQuery('DELETE FROM main WHERE domain = $1', [domainName], function () { if (--pendingCalls == 0) { func(); } });
236 + sqlDbQuery('DELETE FROM events WHERE domain = $1', [domainName], function () { if (--pendingCalls == 0) { func(); } });
237 + } else if (obj.databaseType == 3) {
238 + // MongoDB
239 + pendingCalls = 3;
240 + obj.file.deleteMany({ domain: domainName }, { multi: true }, function () { if (--pendingCalls == 0) { func(); } });
241 + obj.eventsfile.deleteMany({ domain: domainName }, { multi: true }, function () { if (--pendingCalls == 0) { func(); } });
242 + obj.powerfile.deleteMany({ domain: domainName }, { multi: true }, function () { if (--pendingCalls == 0) { func(); } });
243 + } else {
244 + // NeDB or MongoJS
245 + pendingCalls = 3;
246 + obj.file.remove({ domain: domainName }, { multi: true }, function () { if (--pendingCalls == 0) { func(); } });
247 + obj.eventsfile.remove({ domain: domainName }, { multi: true }, function () { if (--pendingCalls == 0) { func(); } });
248 + obj.powerfile.remove({ domain: domainName }, { multi: true }, function () { if (--pendingCalls == 0) { func(); } });
249 + }
250 + }
251 +
252 obj.cleanup = function (func) {
253 // TODO: Remove all mesh links to invalid users
254 // TODO: Remove all meshes that dont have any links
255
256 // Remove all events, power events and SMBIOS data from the main collection. They are all in seperate collections now.
257 if ((obj.databaseType == 4) || (obj.databaseType == 5) || (obj.databaseType == 6)) {
234 - // MariaDB or MySQL
258 + // MariaDB, MySQL or PostgreSQL
259 obj.RemoveAllOfType('event', function () { });
260 obj.RemoveAllOfType('power', function () { });
261 obj.RemoveAllOfType('smbios', function () { });
@@ -569,8 +593,8 @@ module.exports.CreateDB = function (parent, func) {
593 parent.debug('db', 'Checking tables...');
594 sqlDbBatchExec([
595 'CREATE TABLE IF NOT EXISTS main (id VARCHAR(256) NOT NULL, type CHAR(32), domain CHAR(64), extra CHAR(255), extraex CHAR(255), doc JSON, PRIMARY KEY(id), CHECK (json_valid(doc)))',
572 - 'CREATE TABLE IF NOT EXISTS events(id INT NOT NULL AUTO_INCREMENT, time DATETIME, domain CHAR(64), action CHAR(255), nodeid CHAR(255), userid CHAR(255), doc JSON, PRIMARY KEY(id), CHECK(json_valid(doc)))',
573 - 'CREATE TABLE IF NOT EXISTS eventids(fkid INT NOT NULL, target CHAR(255), CONSTRAINT fk_eventid FOREIGN KEY (fkid) REFERENCES events (id) ON DELETE CASCADE ON UPDATE RESTRICT)',
596 + 'CREATE TABLE IF NOT EXISTS events (id INT NOT NULL AUTO_INCREMENT, time DATETIME, domain CHAR(64), action CHAR(255), nodeid CHAR(255), userid CHAR(255), doc JSON, PRIMARY KEY(id), CHECK(json_valid(doc)))',
597 + 'CREATE TABLE IF NOT EXISTS eventids (fkid INT NOT NULL, target CHAR(255), CONSTRAINT fk_eventid FOREIGN KEY (fkid) REFERENCES events (id) ON DELETE CASCADE ON UPDATE RESTRICT)',
598 'CREATE TABLE IF NOT EXISTS serverstats (time DATETIME, expire DATETIME, doc JSON, PRIMARY KEY(time), CHECK (json_valid(doc)))',
599 'CREATE TABLE IF NOT EXISTS power (id INT NOT NULL AUTO_INCREMENT, time DATETIME, nodeid CHAR(255), doc JSON, PRIMARY KEY(id), CHECK (json_valid(doc)))',
600 'CREATE TABLE IF NOT EXISTS smbios (id CHAR(255), time DATETIME, expire DATETIME, doc JSON, PRIMARY KEY(id), CHECK (json_valid(doc)))',
meshagent.js
+1
@@ -1287,6 +1287,7 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
1287 delete command.action;
1288 command.updateTime = Date.now();
1289 command._id = 'if' + obj.dbNodeKey;
1290 + command.domain = domain.id;
1291 command.type = 'ifinfo';
1292 db.Set(command);
1293
meshcentral.js
+7 -2
@@ -137,7 +137,7 @@ function CreateMeshCentralServer(config, args) {
137 try { require('./pass').hash('test', function () { }, 0); } catch (e) { console.log('Old version of node, must upgrade.'); return; } // TODO: Not sure if this test works or not.
138
139 // Check for invalid arguments
140 - var validArguments = ['_', 'user', 'port', 'aliasport', 'mpsport', 'mpsaliasport', 'redirport', 'rediraliasport', 'cert', 'mpscert', 'deletedomain', 'deletedefaultdomain', 'showall', 'showusers', 'showitem', 'listuserids', 'showusergroups', 'shownodes', 'showallmeshes', 'showmeshes', 'showevents', 'showsmbios', 'showpower', 'clearpower', 'showiplocations', 'help', 'exactports', 'xinstall', 'xuninstall', 'install', 'uninstall', 'start', 'stop', 'restart', 'debug', 'filespath', 'datapath', 'noagentupdate', 'launch', 'noserverbackup', 'mongodb', 'mongodbcol', 'wanonly', 'lanonly', 'nousers', 'mpspass', 'ciralocalfqdn', 'dbexport', 'dbexportmin', 'dbimport', 'dbmerge', 'dbfix', 'dbencryptkey', 'selfupdate', 'tlsoffload', 'userallowedip', 'userblockedip', 'swarmallowedip', 'agentallowedip', 'agentblockedip', 'fastcert', 'swarmport', 'logintoken', 'logintokenkey', 'logintokengen', 'mailtokengen', 'admin', 'unadmin', 'sessionkey', 'sessiontime', 'minify', 'minifycore', 'dblistconfigfiles', 'dbshowconfigfile', 'dbpushconfigfiles', 'dbpullconfigfiles', 'dbdeleteconfigfiles', 'vaultpushconfigfiles', 'vaultpullconfigfiles', 'vaultdeleteconfigfiles', 'configkey', 'loadconfigfromdb', 'npmpath', 'serverid', 'recordencryptionrecode', 'vault', 'token', 'unsealkey', 'name', 'log', 'dbstats', 'translate', 'createaccount', 'resetaccount', 'pass', 'adminaccount', 'domain', 'email', 'configfile', 'maintenancemode', 'nedbtodb', 'removetestagents', 'agentupdatetest', 'hashpassword', 'hashpass', 'indexmcrec', 'mpsdebug'];
140 + var validArguments = ['_', 'user', 'port', 'aliasport', 'mpsport', 'mpsaliasport', 'redirport', 'rediraliasport', 'cert', 'mpscert', 'deletedomain', 'deletedefaultdomain', 'showall', 'showusers', 'showitem', 'listuserids', 'showusergroups', 'shownodes', 'showallmeshes', 'showmeshes', 'showevents', 'showsmbios', 'showpower', 'clearpower', 'showiplocations', 'help', 'exactports', 'xinstall', 'xuninstall', 'install', 'uninstall', 'start', 'stop', 'restart', 'debug', 'filespath', 'datapath', 'noagentupdate', 'launch', 'noserverbackup', 'mongodb', 'mongodbcol', 'wanonly', 'lanonly', 'nousers', 'mpspass', 'ciralocalfqdn', 'dbexport', 'dbexportmin', 'dbimport', 'dbmerge', 'dbfix', 'dbencryptkey', 'selfupdate', 'tlsoffload', 'userallowedip', 'userblockedip', 'swarmallowedip', 'agentallowedip', 'agentblockedip', 'fastcert', 'swarmport', 'logintoken', 'logintokenkey', 'logintokengen', 'mailtokengen', 'admin', 'unadmin', 'sessionkey', 'sessiontime', 'minify', 'minifycore', 'dblistconfigfiles', 'dbshowconfigfile', 'dbpushconfigfiles', 'dbpullconfigfiles', 'dbdeleteconfigfiles', 'vaultpushconfigfiles', 'vaultpullconfigfiles', 'vaultdeleteconfigfiles', 'configkey', 'loadconfigfromdb', 'npmpath', 'serverid', 'recordencryptionrecode', 'vault', 'token', 'unsealkey', 'name', 'log', 'dbstats', 'translate', 'createaccount', 'resetaccount', 'pass', 'removesubdomain', 'adminaccount', 'domain', 'email', 'configfile', 'maintenancemode', 'nedbtodb', 'removetestagents', 'agentupdatetest', 'hashpassword', 'hashpass', 'indexmcrec', 'mpsdebug'];
141 for (var arg in obj.args) { obj.args[arg.toLocaleLowerCase()] = obj.args[arg]; if (validArguments.indexOf(arg.toLocaleLowerCase()) == -1) { console.log('Invalid argument "' + arg + '", use --help.'); return; } }
142 if (obj.args.mongodb == true) { console.log('Must specify: --mongodb [connectionstring] \r\nSee https://docs.mongodb.com/manual/reference/connection-string/ for MongoDB connection string.'); return; }
143 for (i in obj.config.settings) { obj.args[i] = obj.config.settings[i]; } // Place all settings into arguments, arguments have already been placed into settings so arguments take precedence.
@@ -843,7 +843,7 @@ function CreateMeshCentralServer(config, args) {
843 return;
844 }
845 if (obj.args.adminaccount) { // Set a user account to server administrator
846 - if ((typeof obj.args.adminaccount != 'string') || (obj.args.adminaccount.indexOf(' ') >= 0)) { console.log("Invalid userid, usage: --adminaccount [username] --domain (domain)."); process.exit(); return; }
846 + if ((typeof obj.args.adminaccount != 'string') || (obj.args.adminaccount.indexOf(' ') >= 0)) { console.log("Invalid userid, usage: --adminaccount [username] --domain (domain)"); process.exit(); return; }
847 var userid = 'user/' + (obj.args.domain ? obj.args.domain : '') + '/' + obj.args.adminaccount.toLowerCase();
848 if (obj.args.adminaccount.startsWith('user/')) { userid = obj.args.adminaccount; }
849 if (userid.split('/').length != 3) { console.log("Invalid userid."); process.exit(); return; }
@@ -855,6 +855,11 @@ function CreateMeshCentralServer(config, args) {
855 });
856 return;
857 }
858 + if (obj.args.removesubdomain) { // Remove all references to a sub domain from the database
859 + if ((typeof obj.args.removesubdomain != 'string') || (obj.args.removesubdomain.indexOf(' ') >= 0)) { console.log("Invalid sub domain, usage: --removesubdomain [domain]"); process.exit(); return; }
860 + obj.db.removeDomain(obj.args.removesubdomain, function () { console.log("Done."); process.exit(); return; });
861 + return;
862 + }
863 if (obj.args.removetestagents) { // Remove all test agents from the database
864 db.GetAllType('node', function (err, docs) {
865 if ((err != null) || (docs.length == 0)) {
meshuser.js
+1
@@ -3516,6 +3516,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
3516 if (common.validateString(command.state, 1, 30000) == false) break; // Check state size, no more than 30k
3517 command.state = parent.filterUserWebState(command.state); // Filter the state to remove anything bad
3518 if ((command.state == null) || (typeof command.state !== 'string')) break; // If state did not validate correctly, quit here.
3519 + command.domain = domain.id;
3520 db.Set({ _id: 'ws' + user._id, state: command.state });
3521 parent.parent.DispatchEvent([user._id], obj, { action: 'userWebState', nolog: 1, domain: domain.id, state: command.state });
3522 break;
package.json
+15 -2
@@ -36,6 +36,8 @@
36 "sample-config-advanced.json"
37 ],
38 "dependencies": {
39 + "@yetzt/nedb": "^1.8.0",
40 + "archiver": "^4.0.2",
41 "body-parser": "^1.19.0",
42 "cbor": "~5.2.0",
43 "compression": "^1.7.4",
@@ -43,13 +45,24 @@
45 "express": "^4.17.0",
46 "express-handlebars": "^3.1.0",
47 "express-ws": "^4.0.0",
48 + "image-size": "^1.0.1",
49 "ipcheck": "^0.1.0",
50 + "loadavg-windows": "^1.1.1",
51 "minimist": "^1.2.5",
52 + "mongodb": "^4.1.0",
53 "multiparty": "^4.2.1",
49 - "@yetzt/nedb": "^1.8.0",
54 "node-forge": "^1.0.0",
55 + "node-rdpjs-2": "^0.3.5",
56 + "node-windows": "^0.1.4",
57 + "otplib": "^10.2.3",
58 + "pg": "^8.7.1",
59 + "pgtools": "^0.3.2",
60 + "saslprep": "^1.0.3",
61 + "ssh2": "^1.5.0",
62 + "web-push": "^3.4.5",
63 "ws": "^5.2.3",
52 - "yauzl": "^2.10.0"
64 + "yauzl": "^2.10.0",
65 + "yubikeyotp": "^0.2.0"
66 },
67 "engines": {
68 "node": ">=10.0.0"