Fixed server account and login times.

Ylian Saint-Hilaire committed Jan 24, 2019 at 17:01 UTC 75ee8dfd7830dc89e70e6d7a007d477b6a720e03
8 files changed +57 -24
.gitignore
+1
@@ -8,6 +8,7 @@
8 meshcentral.db
9 meshcentral.db.json
10 mesherrors.txt
11 +bob.json
12
13 ## Ignore Visual Studio temporary files, build results, and
14 ## files generated by popular Visual Studio add-ons.
MeshCentralServer.njsproj
+1
@@ -94,6 +94,7 @@
94 <Compile Include="agents\modules_meshcore_min\win-message-pump.min.js" />
95 <Compile Include="agents\modules_meshcore_min\win-registry.min.js" />
96 <Compile Include="agents\modules_meshcore_min\win-terminal.min.js" />
97 + <Compile Include="agents\recoverycore.js" />
98 <Compile Include="agents\testsuite.js" />
99 <Compile Include="agents\tinycore.js" />
100 <Compile Include="amtevents.js" />
db.js
+36 -7
@@ -99,7 +99,7 @@ module.exports.CreateDB = function (parent) {
99 });
100 };
101
102 - obj.cleanup = function () {
102 + obj.cleanup = function (func) {
103 // TODO: Remove all mesh links to invalid users
104 // TODO: Remove all meshes that dont have any links
105
@@ -108,14 +108,43 @@ module.exports.CreateDB = function (parent) {
108 var meshlist = [];
109 if (err == null && docs.length > 0) { for (var i in docs) { meshlist.push(docs[i]._id); } }
110 obj.file.remove({ meshid: { $exists: true, $nin: meshlist } }, { multi: true });
111 - });
111
113 - // Clear up all users
114 - /*
115 - obj.GetAllType('user', function (err, docs) {
116 - for (var i in docs) { if (docs[i].subscriptions != null) { console.log('Clean user: ' + docs[i].name); obj.SetUser(docs[i]); } } // Remove "subscriptions" that should not be there.
112 + // Fix all of the creating & login to ticks by seconds, not milliseconds.
113 + obj.GetAllType('user', function (err, docs) {
114 + if (err == null && docs.length > 0) {
115 + for (var i in docs) {
116 + var fixed = false;
117 +
118 + // Fix account creation
119 + if (docs[i].creation) {
120 + if (docs[i].creation > 1300000000000) { docs[i].creation = Math.floor(docs[i].creation / 1000); fixed = true; }
121 + if ((docs[i].creation % 1) != 0) { docs[i].creation = Math.floor(docs[i].creation); fixed = true; }
122 + }
123 +
124 + // Fix last account login
125 + if (docs[i].login) {
126 + if (docs[i].login > 1300000000000) { docs[i].login = Math.floor(docs[i].login / 1000); fixed = true; }
127 + if ((docs[i].login % 1) != 0) { docs[i].login = Math.floor(docs[i].login); fixed = true; }
128 + }
129 +
130 + // Fix last password change
131 + if (docs[i].passchange) {
132 + if (docs[i].passchange > 1300000000000) { docs[i].passchange = Math.floor(docs[i].passchange / 1000); fixed = true; }
133 + if ((docs[i].passchange % 1) != 0) { docs[i].passchange = Math.floor(docs[i].passchange); fixed = true; }
134 + }
135 +
136 + // Fix subscriptions
137 + if (docs[i].subscriptions != null) { delete docs[i].subscriptions; fixed = true; }
138 +
139 + // Save the user if needed
140 + if (fixed) { obj.Set(docs[i]); }
141 +
142 + // We are done
143 + if (func) { func(); }
144 + }
145 + }
146 + });
147 });
118 - */
148 };
149
150 obj.Set = function (data, func) { obj.file.update({ _id: data._id }, data, { upsert: true }, func); };
meshcentral.js
+6 -5
@@ -100,7 +100,8 @@ function CreateMeshCentralServer(config, args) {
100 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.
101
102 if ((obj.args.help == true) || (obj.args['?'] == true)) {
103 - console.log('MeshCentral2 Beta 2, a web-based remote computer management web portal.\r\n');
103 + console.log('MeshCentral v' + obj.currentVer + ', a open source remote computer management web portal.');
104 + console.log('Details at: https://www.meshcommander.com/meshcentral2\r\n');
105 if (obj.platform == 'win32') {
106 console.log('Run as a Windows Service');
107 console.log(' --install/uninstall Install Meshcentral as a background service.');
@@ -361,16 +362,16 @@ function CreateMeshCentralServer(config, args) {
362 // Read or setup database configuration values
363 obj.db.Get('dbconfig', function (err, dbconfig) {
364 if (dbconfig.length == 1) { obj.dbconfig = dbconfig[0]; } else { obj.dbconfig = { _id: 'dbconfig', version: 1 }; }
364 - if (obj.dbconfig.amtWsEventSecret == null) { require('crypto').randomBytes(32, function (err, buf) { obj.dbconfig.amtWsEventSecret = buf.toString('hex'); obj.db.Set(obj.dbconfig); }); }
365 + if (obj.dbconfig.amtWsEventSecret == null) { obj.crypto.randomBytes(32, function (err, buf) { obj.dbconfig.amtWsEventSecret = buf.toString('hex'); obj.db.Set(obj.dbconfig); }); }
366
367 // This is used by the user to create a username/password for a Intel AMT WSMAN event subscription
368 if (obj.args.getwspass) {
369 if (obj.args.getwspass.length == 64) {
369 - require('crypto').randomBytes(6, function (err, buf) {
370 + obj.crypto.randomBytes(6, function (err, buf) {
371 while (obj.dbconfig.amtWsEventSecret == null) { process.nextTick(); }
372 var username = buf.toString('hex');
373 var nodeid = obj.args.getwspass;
373 - var pass = require('crypto').createHash('sha384').update(username.toLowerCase() + ":" + nodeid + ":" + obj.dbconfig.amtWsEventSecret).digest("base64").substring(0, 12).split("/").join("x").split("\\").join("x");
374 + var pass = obj.crypto.createHash('sha384').update(username.toLowerCase() + ":" + nodeid + ":" + obj.dbconfig.amtWsEventSecret).digest("base64").substring(0, 12).split("/").join("x").split("\\").join("x");
375 console.log('--- Intel(r) AMT WSMAN eventing credentials ---');
376 console.log('Username: ' + username);
377 console.log('Password: ' + pass);
@@ -489,7 +490,7 @@ function CreateMeshCentralServer(config, args) {
490 obj.updateMeshAgentInstallScripts();
491
492 // Setup and start the web server
492 - require('crypto').randomBytes(48, function (err, buf) {
493 + obj.crypto.randomBytes(48, function (err, buf) {
494 // Setup Mesh Multi-Server if needed
495 obj.multiServer = require('./multiserver.js').CreateMultiServer(obj, obj.args);
496 if (obj.multiServer != null) {
meshuser.js
+1 -1
@@ -587,7 +587,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
587 var newusername = command.username, newuserid = 'user/' + domain.id + '/' + command.username.toLowerCase();
588 if (newusername == '~') break; // This is a reserved user name
589 if (!obj.parent.users[newuserid]) {
590 - var newuser = { type: 'user', _id: newuserid, name: newusername, creation: Date.now(), domain: domain.id };
590 + var newuser = { type: 'user', _id: newuserid, name: newusername, creation: Math.floor(Date.now() / 1000), domain: domain.id };
591 if (command.email != null) { newuser.email = command.email; } // Email
592 obj.parent.users[newuserid] = newuser;
593 // Create a user, generate a salt and hash the password
package.json
+1 -1
@@ -1,6 +1,6 @@
1 {
2 "name": "meshcentral",
3 - "version": "0.2.6-w",
3 + "version": "0.2.6-x",
4 "keywords": [
5 "Remote Management",
6 "Intel AMT",
views/default.handlebars
+6 -5
@@ -1568,7 +1568,7 @@
1568 }
1569 case 'login': {
1570 // Update the last login time
1571 - if (users != null && users['user/' + domain + '/' + message.event.username.toLowerCase()]) { users['user/' + domain + '/' + message.event.username.toLowerCase()].login = message.event.time; }
1571 + if (users != null && users['user/' + domain + '/' + message.event.username.toLowerCase()]) { users['user/' + domain + '/' + message.event.username.toLowerCase()].login = Math.floor(message.event.time / 1000); }
1572 break;
1573 }
1574 case 'scanamtdevice': {
@@ -5247,7 +5247,8 @@
5247 count++;
5248
5249 // Mesh rights
5250 - var meshrights = meshes[i].links['user/' + domain + '/' + userinfo.name.toLowerCase()].rights;
5250 + var meshrights = 0;
5251 + if (meshes[i].links['user/' + domain + '/' + userinfo.name.toLowerCase()]) { meshrights = meshes[i].links['user/' + domain + '/' + userinfo.name.toLowerCase()].rights; }
5252 var rights = 'Partial Rights';
5253 if (meshrights == 0xFFFFFFFF) rights = 'Full Administrator'; else if (meshrights == 0) rights = 'No Rights';
5254
@@ -5882,7 +5883,7 @@
5883 }
5884 if (sessions == 1) { msg += '1 active session'; } else { msg += sessions + ' active sessions'; }
5885 } else {
5885 - if (user.login) { msg += '<span title="Last login: ' + new Date(user.login).toLocaleString() + '">' + new Date(user.login).toLocaleDateString() + '</span>'; }
5886 + if (user.login) { msg += '<span title="Last login: ' + new Date(user.login * 1000).toLocaleString() + '">' + new Date(user.login * 1000).toLocaleDateString() + '</span>'; }
5887 }
5888 if (msg != '') msg += ', ';
5889 if (self) { msg += "<a onclick=showUserAdminDialog(event,\"" + encodeURIComponent(user._id) + "\")>"; }
@@ -6058,8 +6059,8 @@
6059 x += addDeviceAttribute('Email', everify + "<a style=cursor:pointer onclick=p30showUserEmailChangeDialog(event,\"" + userid + "\")>" + email + '</a> <a style=cursor:pointer onclick=doemail(event,\"' + user.email + '\")><img src="images/link1.png" /></a>');
6060 x += addDeviceAttribute('Server Rights', "<a style=cursor:pointer onclick=showUserAdminDialog(event,\"" + userid + "\")>" + msg + "</a>");
6061 if (user.quota) x += addDeviceAttribute('Server Quota', EscapeHtml(parseInt(user.quota) / 1024) + ' k');
6061 - x += addDeviceAttribute('Creation', new Date(user.creation).toLocaleString());
6062 - if (user.login) x += addDeviceAttribute('Last Login', new Date(user.login).toLocaleString());
6062 + x += addDeviceAttribute('Creation', new Date(user.creation * 1000).toLocaleString());
6063 + if (user.login) x += addDeviceAttribute('Last Login', new Date(user.login * 1000).toLocaleString());
6064
6065 x += '</table></div><br />';
6066
webserver.js
+5 -5
@@ -353,7 +353,7 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
353 }
354
355 // Save login time
356 - user.login = Date.now();
356 + user.login = Math.floor(Date.now() / 1000);
357 obj.db.SetUser(user);
358
359 // Regenerate session when signing in to prevent fixation
@@ -434,7 +434,7 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
434 } else {
435 var hint = req.body.apasswordhint;
436 if (hint.length > 250) hint = hint.substring(0, 250);
437 - var user = { type: 'user', _id: 'user/' + domain.id + '/' + req.body.username.toLowerCase(), name: req.body.username, email: req.body.email, creation: Date.now(), login: Date.now(), domain: domain.id, passhint: hint };
437 + var user = { type: 'user', _id: 'user/' + domain.id + '/' + req.body.username.toLowerCase(), name: req.body.username, email: req.body.email, creation: Math.floor(Date.now() / 1000), login: Math.floor(Date.now() / 1000), domain: domain.id, passhint: hint };
438 var usercount = 0;
439 for (var i in obj.users) { if (obj.users[i].domain == domain.id) { usercount++; } }
440 if (usercount == 0) { user.siteadmin = 0xFFFFFFFF; if (domain.newaccounts === 2) { domain.newaccounts = 0; } } // If this is the first user, give the account site admin.
@@ -563,7 +563,7 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
563 userinfo = obj.users[user._id];
564 userinfo.salt = salt;
565 userinfo.hash = hash;
566 - userinfo.passchange = Date.now();
566 + userinfo.passchange = Math.floor(Date.now() / 1000);
567 userinfo.passhint = null;
568 delete userinfo.otpsecret; // Currently a email password reset will turn off 2-step login.
569 obj.db.SetUser(userinfo);
@@ -658,7 +658,7 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
658 var user = obj.users[req.session.userid];
659 user.salt = salt;
660 user.hash = hash;
661 - user.passchange = Date.now();
661 + user.passchange = Math.floor(Date.now() / 1000);
662 user.passhint = req.body.apasswordhint;
663 obj.db.SetUser(user);
664 req.session.viewmode = 2;
@@ -743,7 +743,7 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
743 user = obj.users[req.session.userid];
744 if ((user == null) || (user.sid != req.session.usersid)) {
745 // Create the domain user
746 - var usercount = 0, user2 = { type: 'user', _id: req.session.userid, name: req.connection.user, domain: domain.id, sid: req.session.usersid, creation: Date.now() };
746 + var usercount = 0, user2 = { type: 'user', _id: req.session.userid, name: req.connection.user, domain: domain.id, sid: req.session.usersid, creation: Math.floor(Date.now() / 1000), login: Math.floor(Date.now() / 1000) };
747 for (var i in obj.users) { if (obj.users[i].domain == domain.id) { usercount++; } }
748 if (usercount == 0) { user2.siteadmin = 0xFFFFFFFF; } // If this is the first user, give the account site admin.
749 obj.users[req.session.userid] = user2;