Fixes for MeshCore and MariaDB.

Ylian Saint-Hilaire committed Nov 3, 2020 at 02:58 UTC 49c41cb02c8dd7a6c64c4bfcd0c6b43d530b4fae
4 files changed +20 -18
agents/meshcore.js
+9 -4
@@ -2579,7 +2579,7 @@ function createMeshCore(agent) {
2579 var response = null;
2580 switch (cmd) {
2581 case 'help': { // Displays available commands
2582 - var fin = '', f = '', availcommands = 'coreinfo, coredump,service,fdsnapshot,fdcount,startupoptions,alert,agentsize,versions,help,info,osinfo,args,print,type,dbkeys,dbget,dbset,dbcompact,eval,parseuri,httpget,nwslist,plugin,wsconnect,wssend,wsclose,notify,ls,ps,kill,netinfo,location,power,wakeonlan,setdebug,smbios,rawsmbios,toast,lock,users,openurl,getscript,getclip,setclip,log,av,cpuinfo,sysinfo,apf,scanwifi,wallpaper,agentmsg';
2582 + var fin = '', f = '', availcommands = 'coreinfo,coredump,service,fdsnapshot,fdcount,startupoptions,alert,agentsize,versions,help,info,osinfo,args,print,type,dbkeys,dbget,dbset,dbcompact,eval,parseuri,httpget,nwslist,plugin,wsconnect,wssend,wsclose,notify,ls,ps,kill,netinfo,location,power,wakeonlan,setdebug,smbios,rawsmbios,toast,lock,users,openurl,getscript,getclip,setclip,log,av,cpuinfo,sysinfo,apf,scanwifi,wallpaper,agentmsg';
2583 if (process.platform == 'win32') { availcommands += ',safemode,wpfhwacceleration,uac'; }
2584 if (amt != null) { availcommands += ',amt,amtconfig,amtevents'; }
2585 if (process.platform != 'freebsd') { availcommands += ',vm';}
@@ -3647,7 +3647,7 @@ function createMeshCore(agent) {
3647
3648 // Update the server on with basic info, logged in users and more advanced stuff, like Intel ME and Network Settings
3649 meInfoStr = null;
3650 - sendPeriodicServerUpdate();
3650 + sendPeriodicServerUpdate(null, true);
3651 if (selfInfoUpdateTimer == null) { selfInfoUpdateTimer = setInterval(sendPeriodicServerUpdate, 1200000); } // 20 minutes
3652
3653 // Send any state messages
@@ -3708,7 +3708,10 @@ function createMeshCore(agent) {
3708 if (force) {
3709 meshCoreObj = sortObjRec(meshCoreObj);
3710 var x = JSON.stringify(meshCoreObj);
3711 - if (x != LastPeriodicServerUpdate) { LastPeriodicServerUpdate = x; mesh.SendCommand(meshCoreObj); }
3711 + if (x != LastPeriodicServerUpdate) {
3712 + LastPeriodicServerUpdate = x;
3713 + mesh.SendCommand(meshCoreObj);
3714 + }
3715 }
3716 }
3717
@@ -3720,7 +3723,9 @@ function createMeshCore(agent) {
3723 PeriodicServerUpdateNagleTimer = null;
3724 meshCoreObj = sortObjRec(meshCoreObj);
3725 var x = JSON.stringify(meshCoreObj);
3723 - if (x != LastPeriodicServerUpdate) { LastPeriodicServerUpdate = x; mesh.SendCommand(meshCoreObj); }
3726 + if (x != LastPeriodicServerUpdate) {
3727 + try { LastPeriodicServerUpdate = x; mesh.SendCommand(meshCoreObj); } catch (ex) { }
3728 + }
3729 }
3730
3731 function sortObjRec(o) { if ((typeof o != 'object') || (Array.isArray(o))) return o; for (var i in o) { if (typeof o[i] == 'object') { o[i] = sortObjRec(o[i]); } } return sortObj(o); }
db.js
+7 -10
@@ -261,6 +261,7 @@ module.exports.CreateDB = function (parent, func) {
261 function performTypedRecordDecrypt(data) {
262 if ((data == null) || (obj.dbRecordsDecryptKey == null) || (typeof data != 'object')) return data;
263 for (var i in data) {
264 + if (data[i] == null) continue;
265 if (data[i].type == 'user') {
266 data[i] = performPartialRecordDecrypt(data[i]);
267 } else if ((data[i].type == 'node') && (data[i].intelamt != null)) {
@@ -762,7 +763,7 @@ module.exports.CreateDB = function (parent, func) {
763 .then(function (rows) {
764 conn.release();
765 const docs = [];
765 - for (var i in rows) { if (rows[i].doc) { docs.push(performTypedRecordDecrypt(JSON.parse(rows[i].doc))); } }
766 + for (var i in rows) { if (rows[i].doc) { docs.push(performTypedRecordDecrypt((typeof rows[i].doc == 'object')? rows[i].doc : JSON.parse(rows[i].doc))); } }
767 if (func) try { func(null, docs); } catch (ex) { console.log('SQLERR1', ex); }
768 })
769 .catch(function (err) { conn.release(); if (func) try { func(err); } catch (ex) { console.log('SQLERR2', ex); } });
@@ -827,16 +828,12 @@ module.exports.CreateDB = function (parent, func) {
828 obj.Set = function (value, func) {
829 var extra = null, extraex = null;
830 value = common.escapeLinksFieldNameEx(value);
830 - if (value.meshid) { extra = value.meshid; } else if (value.email) { extra = 'email/' + value.email; }
831 + if (value.meshid) { extra = value.meshid; } else if (value.email) { extra = 'email/' + value.email; } else if (value.nodeid) { extra = value.nodeid; }
832 if ((value.type == 'node') && (value.intelamt != null) && (value.intelamt.uuid != null)) { extraex = 'uuid/' + value.intelamt.uuid; }
833 + if (value._id == null) { value._id = require('crypto').randomBytes(16).toString('hex'); }
834 sqlDbQuery('REPLACE INTO meshcentral.main VALUE (?, ?, ?, ?, ?, ?)', [value._id, (value.type ? value.type : null), ((value.domain != null) ? value.domain : null), extra, extraex, JSON.stringify(performTypedRecordEncrypt(value))], func);
835 }
834 - obj.Get = function (_id, func) {
835 - sqlDbQuery('SELECT doc FROM meshcentral.main WHERE id = ?', [_id], function (err, docs) {
836 - if ((docs != null) && (docs.length > 0) && (docs[0].links != null)) { docs[0] = common.unEscapeLinksFieldName(docs[0]); }
837 - func(err, docs);
838 - });
839 - }
836 + obj.Get = function (_id, func) { sqlDbQuery('SELECT doc FROM meshcentral.main WHERE id = ?', [_id], function (err, docs) { if ((docs != null) && (docs.length > 0) && (docs[0].links != null)) { docs[0] = common.unEscapeLinksFieldName(docs[0]); } func(err, docs); }); }
837 obj.GetAll = function (func) { sqlDbQuery('SELECT domain, doc FROM meshcentral.main', null, func); }
838 obj.GetHash = function (id, func) { sqlDbQuery('SELECT doc FROM meshcentral.main WHERE id = ?', [id], func); }
839 obj.GetAllTypeNoTypeField = function (type, domain, func) { sqlDbQuery('SELECT doc FROM meshcentral.main WHERE type = ? AND domain = ?', [type, domain], function (err, docs) { if (err == null) { for (var i in docs) { delete docs[i].type } } func(err, docs); }); };
@@ -853,9 +850,9 @@ module.exports.CreateDB = function (parent, func) {
850 };
851 obj.GetAllTypeNodeFiltered = function (nodes, domain, type, id, func) {
852 if (id && (id != '')) {
856 - sqlDbQuery('SELECT doc FROM meshcentral.main WHERE id = ? AND type = ? AND domain = ? AND nodeid IN (?)', [id, type, domain, nodes], function (err, docs) { if (err == null) { for (var i in docs) { delete docs[i].type } } func(err, docs); });
853 + sqlDbQuery('SELECT doc FROM meshcentral.main WHERE id = ? AND type = ? AND domain = ? AND extra IN (?)', [id, type, domain, nodes], function (err, docs) { if (err == null) { for (var i in docs) { delete docs[i].type } } func(err, docs); });
854 } else {
858 - sqlDbQuery('SELECT doc FROM meshcentral.main WHERE type = ? AND domain = ? AND nodeid IN (?)', [type, domain, nodes], function (err, docs) { if (err == null) { for (var i in docs) { delete docs[i].type } } func(err, docs); });
855 + sqlDbQuery('SELECT doc FROM meshcentral.main WHERE type = ? AND domain = ? AND extra IN (?)', [type, domain, nodes], function (err, docs) { if (err == null) { for (var i in docs) { delete docs[i].type } } func(err, docs); });
856 }
857 };
858 obj.GetAllType = function (type, func) { sqlDbQuery('SELECT doc FROM meshcentral.main WHERE type = ?', [type], func); }
meshcentral.js
+3 -3
@@ -1156,7 +1156,7 @@ function CreateMeshCentralServer(config, args) {
1156
1157 // Read or setup database configuration values
1158 obj.db.Get('dbconfig', function (err, dbconfig) {
1159 - if (dbconfig.length == 1) { obj.dbconfig = dbconfig[0]; } else { obj.dbconfig = { _id: 'dbconfig', version: 1 }; }
1159 + if ((dbconfig != null) && (dbconfig.length == 1)) { obj.dbconfig = dbconfig[0]; } else { obj.dbconfig = { _id: 'dbconfig', version: 1 }; }
1160 if (obj.dbconfig.amtWsEventSecret == null) { obj.crypto.randomBytes(32, function (err, buf) { obj.dbconfig.amtWsEventSecret = buf.toString('hex'); obj.db.Set(obj.dbconfig); }); }
1161
1162 // This is used by the user to create a username/password for a Intel AMT WSMAN event subscription
@@ -1384,7 +1384,7 @@ function CreateMeshCentralServer(config, args) {
1384 // Login cookie encryption key not set, use one from the database
1385 if (obj.loginCookieEncryptionKey == null) {
1386 obj.db.Get('LoginCookieEncryptionKey', function (err, docs) {
1387 - if ((docs.length > 0) && (docs[0].key != null) && (obj.args.logintokengen == null) && (docs[0].key.length >= 160)) {
1387 + if ((docs != null) && (docs.length > 0) && (docs[0].key != null) && (obj.args.logintokengen == null) && (docs[0].key.length >= 160)) {
1388 obj.loginCookieEncryptionKey = Buffer.from(docs[0].key, 'hex');
1389 } else {
1390 obj.loginCookieEncryptionKey = obj.generateCookieKey(); obj.db.Set({ _id: 'LoginCookieEncryptionKey', key: obj.loginCookieEncryptionKey.toString('hex'), time: Date.now() });
@@ -1394,7 +1394,7 @@ function CreateMeshCentralServer(config, args) {
1394
1395 // Load the invitation link encryption key from the database
1396 obj.db.Get('InvitationLinkEncryptionKey', function (err, docs) {
1397 - if ((docs.length > 0) && (docs[0].key != null) && (docs[0].key.length >= 160)) {
1397 + if ((docs != null) && (docs.length > 0) && (docs[0].key != null) && (docs[0].key.length >= 160)) {
1398 obj.invitationLinkEncryptionKey = Buffer.from(docs[0].key, 'hex');
1399 } else {
1400 obj.invitationLinkEncryptionKey = obj.generateCookieKey(); obj.db.Set({ _id: 'InvitationLinkEncryptionKey', key: obj.invitationLinkEncryptionKey.toString('hex'), time: Date.now() });
webserver.js
+1 -1
@@ -1167,7 +1167,7 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
1167 } else {
1168 // Check if this email was already verified
1169 obj.db.GetUserWithVerifiedEmail(domain.id, req.body.email, function (err, docs) {
1170 - if (docs.length > 0) {
1170 + if ((docs != null) && (docs.length > 0)) {
1171 parent.debug('web', 'handleCreateAccountRequest: Existing account with this email address');
1172 req.session.loginmode = '2';
1173 req.session.messageid = 102; // Existing account with this email address.