Fixed IPv6 issue with session binding and added device group name to CIRA script.

Ylian Saint-Hilaire committed Sep 20, 2019 at 10:41 UTC 9de07f0962d7c294eaccefeb3ca342822f1ea895
3 files changed +22 -15
package.json
+1 -1
@@ -1,6 +1,6 @@
1 {
2 "name": "meshcentral",
3 - "version": "0.4.0-y",
3 + "version": "0.4.0-z",
4 "keywords": [
5 "Remote Management",
6 "Intel AMT",
views/default.handlebars
+2 -1
@@ -2879,7 +2879,8 @@
2879
2880 // Setup CIRA using a MeshCommander script (Pretty Simple)
2881 x += "<div id=dlgAddCira0>To add a new Intel&reg; AMT device to device group \"" + EscapeHtml(mesh.name) + "\" with CIRA, download the following script files and use <a href='http://meshcommander.com' rel='noreferrer noopener' target='_blank'>MeshCommander</a> to run the script to configure computers.<br /><br />";
2882 - x += addHtmlValue('Setup CIRA', '<a href="mescript.ashx?type=1&meshid=' + meshidx.substring(0, 16) + '" download>cira_setup.mescript</a>');
2882 + //x += addHtmlValue('Setup CIRA', '<a href="mescript.ashx?type=1&meshid=' + meshidx.substring(0, 16) + '" download>cira_setup.mescript</a>');
2883 + x += addHtmlValue('Setup CIRA', '<a href="mescript.ashx?type=1&meshid=' + meshid + '" download>cira_setup.mescript</a>');
2884 x += addHtmlValue('Cleanup CIRA', '<a href="mescript.ashx?type=2" download>cira_clean.mescript</a>');
2885 x += "</div>";
2886
webserver.js
+19 -13
@@ -737,7 +737,7 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
737 req.session.userid = userid;
738 req.session.domainid = domain.id;
739 req.session.currentNode = '';
740 - req.session.ip = cleanRemoteAddr(req.ip); // Bind this session to the IP address of the request
740 + if (typeof req.ip == 'string') { req.session.ip = cleanRemoteAddr(req.ip); } // Bind this session to the IP address of the request
741 if (req.body.viewmode) { req.session.viewmode = req.body.viewmode; }
742 if (req.body.host) {
743 // TODO: This is a terrible search!!! FIX THIS.
@@ -854,7 +854,7 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
854 obj.users[user._id] = user;
855 req.session.userid = user._id;
856 req.session.domainid = domain.id;
857 - req.session.ip = cleanRemoteAddr(req.ip); // Bind this session to the IP address of the request
857 + if (typeof req.ip == 'string') { req.session.ip = cleanRemoteAddr(req.ip); } // Bind this session to the IP address of the request
858 // Create a user, generate a salt and hash the password
859 require('./pass').hash(req.body.password1, function (err, salt, hash, tag) {
860 if (err) throw err;
@@ -939,7 +939,7 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
939 parent.debug('web', 'handleResetPasswordRequest: success');
940 req.session.userid = userid;
941 req.session.domainid = domain.id;
942 - req.session.ip = cleanRemoteAddr(req.ip); // Bind this session to the IP address of the request
942 + if (typeof req.ip == 'string') { req.session.ip = cleanRemoteAddr(req.ip); } // Bind this session to the IP address of the request
943 completeLoginRequest(req, res, domain, obj.users[userid], userid, req.session.tokenusername, req.session.tokenpassword, direct);
944 }, 0);
945 }
@@ -1347,7 +1347,7 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
1347 req.session.userid = userid;
1348 req.session.domainid = domain.id;
1349 req.session.currentNode = '';
1350 - req.session.ip = cleanRemoteAddr(req.ip); // Bind this session to the IP address of the request
1350 + if (typeof req.ip == 'string') { req.session.ip = cleanRemoteAddr(req.ip); } // Bind this session to the IP address of the request
1351 handleRootRequestEx(req, res, domain, direct);
1352 });
1353 } else {
@@ -1373,7 +1373,7 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
1373 req.session.userid = 'user/' + domain.id + '/~';
1374 req.session.domainid = domain.id;
1375 req.session.currentNode = '';
1376 - req.session.ip = cleanRemoteAddr(req.ip); // Bind this session to the IP address of the request
1376 + if (typeof req.ip == 'string') { req.session.ip = cleanRemoteAddr(req.ip); } // Bind this session to the IP address of the request
1377 if (obj.users[req.session.userid] == null) {
1378 // Create the dummy user ~ with impossible password
1379 parent.debug('web', 'handleRootRequestEx: created dummy user in nouser mode.');
@@ -1387,7 +1387,7 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
1387 req.session.userid = 'user/' + domain.id + '/' + obj.args.user.toLowerCase();
1388 req.session.domainid = domain.id;
1389 req.session.currentNode = '';
1390 - req.session.ip = cleanRemoteAddr(req.ip); // Bind this session to the IP address of the request
1390 + if (typeof req.ip == 'string') { req.session.ip = cleanRemoteAddr(req.ip); } // Bind this session to the IP address of the request
1391 } else if (req.query.login && (obj.parent.loginCookieEncryptionKey != null)) {
1392 var loginCookie = obj.parent.decodeCookie(req.query.login, obj.parent.loginCookieEncryptionKey, 60); // 60 minute timeout
1393 //if ((loginCookie != null) && (loginCookie.ip != null) && (loginCookie.ip != cleanRemoteAddr(req.ip))) { loginCookie = null; } // If the cookie if binded to an IP address, check here.
@@ -1398,7 +1398,7 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
1398 req.session.userid = loginCookie.u;
1399 req.session.domainid = domain.id;
1400 req.session.currentNode = '';
1401 - req.session.ip = cleanRemoteAddr(req.ip); // Bind this session to the IP address of the request
1401 + if (typeof req.ip == 'string') { req.session.ip = cleanRemoteAddr(req.ip); } // Bind this session to the IP address of the request
1402 } else {
1403 parent.debug('web', 'handleRootRequestEx: cookie auth failed.');
1404 }
@@ -1415,7 +1415,7 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
1415 req.session.usersGroups = req.connection.userGroups;
1416 req.session.domainid = domain.id;
1417 req.session.currentNode = '';
1418 - req.session.ip = cleanRemoteAddr(req.ip); // Bind this session to the IP address of the request
1418 + if (typeof req.ip == 'string') { req.session.ip = cleanRemoteAddr(req.ip); } // Bind this session to the IP address of the request
1419
1420 // Check if this user exists, create it if not.
1421 user = obj.users[req.session.userid];
@@ -1737,7 +1737,8 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
1737 }
1738
1739 // Return the CIRA configuration script
1740 - obj.getCiraConfigurationScript = function(meshid, func) {
1740 + obj.getCiraConfigurationScript = function (meshid, func) {
1741 + var meshidx = meshid.split('/')[2].replace(/\@/g, 'X').replace(/\$/g, 'X').substring(0, 16);
1742 var serverNameSplit = obj.certificates.AmtMpsName.split('.');
1743
1744 // Figure out the MPS port, use the alias if set
@@ -1754,7 +1755,7 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
1755 scriptFile.scriptBlocks[3].vars.IP.value = obj.certificates.AmtMpsName; // Set the server IPv4 address name
1756 scriptFile.scriptBlocks[3].vars.ServerName.value = obj.certificates.AmtMpsName; // Set the server certificate name
1757 scriptFile.scriptBlocks[3].vars.Port.value = mpsport; // Set the server MPS port
1757 - scriptFile.scriptBlocks[3].vars.username.value = meshid; // Set the username
1758 + scriptFile.scriptBlocks[3].vars.username.value = meshidx; // Set the username
1759 scriptFile.scriptBlocks[3].vars.password.value = obj.args.mpspass ? obj.args.mpspass : 'A@xew9rt'; // Set the password
1760 scriptFile.scriptBlocks[4].vars.AccessInfo1.value = obj.certificates.AmtMpsName + ':' + mpsport; // Set the primary server name:port to set periodic timer
1761 //scriptFile.scriptBlocks[4].vars.AccessInfo2.value = obj.certificates.AmtMpsName + ':' + mpsport; // Set the secondary server name:port to set periodic timer
@@ -1779,7 +1780,7 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
1780 scriptFile.scriptBlocks[2].vars.CertBin.value = getRootCertBase64(); // Set the root certificate
1781 scriptFile.scriptBlocks[3].vars.FQDN.value = obj.certificates.AmtMpsName; // Set the server DNS name
1782 scriptFile.scriptBlocks[3].vars.Port.value = mpsport; // Set the server MPS port
1782 - scriptFile.scriptBlocks[3].vars.username.value = meshid; // Set the username
1783 + scriptFile.scriptBlocks[3].vars.username.value = meshidx; // Set the username
1784 scriptFile.scriptBlocks[3].vars.password.value = obj.args.mpspass ? obj.args.mpspass : 'A@xew9rt'; // Set the password
1785 scriptFile.scriptBlocks[4].vars.AccessInfo1.value = obj.certificates.AmtMpsName + ':' + mpsport; // Set the primary server name:port to set periodic timer
1786 //scriptFile.scriptBlocks[4].vars.AccessInfo2.value = obj.certificates.AmtMpsName + ':' + mpsport; // Set the secondary server name:port to set periodic timer
@@ -1803,7 +1804,12 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
1804 if (req.query.type == 1) {
1805 obj.getCiraConfigurationScript(req.query.meshid, function (script) {
1806 if (script == null) { res.sendStatus(404); } else {
1806 - res.set({ 'Cache-Control': 'no-cache, no-store, must-revalidate', 'Pragma': 'no-cache', 'Expires': '0', 'Content-Type': 'application/octet-stream', 'Content-Disposition': 'attachment; filename="cira_setup.mescript"' });
1807 + try {
1808 + var cirafilename = obj.meshes[req.query.meshid].name.split('\\').join('').split('/').join('').split(':').join('').split('*').join('').split('?').join('').split('"').join('').split('<').join('').split('>').join('').split('|').join('').split(' ').join('').split('\'').join('');
1809 + res.set({ 'Cache-Control': 'no-cache, no-store, must-revalidate', 'Pragma': 'no-cache', 'Expires': '0', 'Content-Type': 'application/octet-stream', 'Content-Disposition': 'attachment; filename="cira_setup_' + cirafilename + '.mescript"' });
1810 + } catch (ex) {
1811 + res.set({ 'Cache-Control': 'no-cache, no-store, must-revalidate', 'Pragma': 'no-cache', 'Expires': '0', 'Content-Type': 'application/octet-stream', 'Content-Disposition': 'attachment; filename="cira_setup.mescript"' });
1812 + }
1813 res.send(script);
1814 }
1815 });
@@ -3710,7 +3716,7 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
3716 function getRandomPassword() { return Buffer.from(obj.crypto.randomBytes(9), 'binary').toString('base64').split('/').join('@'); }
3717
3718 // Clean a IPv6 address that encodes a IPv4 address
3713 - function cleanRemoteAddr(addr) { if (addr.startsWith('::ffff:')) { return addr.substring(7); } else { return addr; } }
3719 + function cleanRemoteAddr(addr) { if (typeof addr != 'string') { return null; } if (addr.indexOf('::ffff:') == 0) { return addr.substring(7); } else { return addr; } }
3720
3721 // Record a new entry in a recording log
3722 function recordingEntry(fd, type, flags, data, func, tag) {