Completed MeshCentralv1 migration code.

Ylian Saint-Hilaire committed Nov 7, 2017 at 17:05 UTC 801567f564da845531a14c2a5d8b6c9cbc155b64
5 files changed +60 -7
agents/MeshService.exe
Binary files a/agents/MeshService.exe and b/agents/MeshService.exe differ
agents/meshcore.js
+5 -1
@@ -668,7 +668,9 @@ function createMeshCore(agent) {
668 break;
669 }
670 case 'info': { // Return information about the agent and agent core module
671 - response = 'Current Core: ' + obj.meshCoreInfo + '.\r\nAgent Time: ' + Date() + '.\r\nUser Rights: 0x' + rights.toString(16) + '.\r\nPlatform Info: ' + process.platform + '.\r\nCapabilities: ' + obj.meshCoreCapabilities + '.\r\nNative Pipes: ' + obj.useNativePipes + '.\r\nServer URL: ' + mesh.ServerUrl + '.';
671 + response = 'Current Core: ' + obj.meshCoreInfo + '.\r\nAgent Time: ' + Date() + '.\r\nUser Rights: 0x' + rights.toString(16) + '.\r\nPlatform Info: ' + process.platform + '.\r\nCapabilities: ' + obj.meshCoreCapabilities + '.\r\nNative Pipes: ' + obj.useNativePipes + '.\r\nServer URL: ' + mesh.ServerUrl + '.\r\n';
672 + var oldNodeId = db.Get('OldNodeId');
673 + if (oldNodeId != null) { response += 'OldNodeID: ' + oldNodeId + '.\r\n'; }
674 break;
675 }
676 case 'selfinfo': { // Return self information block
@@ -934,6 +936,8 @@ function createMeshCore(agent) {
936 lastSelfInfo = null;
937 } else {
938 // Server connected, send mesh core information
939 + var oldNodeId = db.Get('OldNodeId');
940 + if (oldNodeId != null) { mesh.SendCommand({ action: 'mc1migration', oldnodeid: oldNodeId }); }
941 sendPeriodicServerUpdate(true);
942 if (selfInfoUpdateTimer == null) { selfInfoUpdateTimer = setInterval(sendPeriodicServerUpdate, 60000); } // Should be a long time, like 20 minutes. For now, 1 minute.
943 }
meshagent.js
+44 -4
@@ -155,18 +155,27 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
155 if (obj.parent.webCertificateHash != msg.substring(2, 50)) { console.log('Agent connected with bad web certificate hash, holding connection (' + obj.remoteaddr + ').'); return; }
156
157 // Use our server private key to sign the ServerHash + AgentNonce + ServerNonce
158 - var privateKey = obj.forge.pki.privateKeyFromPem(obj.parent.certificates.agent.key);
158 + var privateKey, certasn1;
159 + if (obj.useSwarmCert == true) {
160 + // Use older SwarmServer certificate of MC1
161 + certasn1 = obj.parent.swarmCertificateAsn1;
162 + privateKey = obj.forge.pki.privateKeyFromPem(obj.parent.certificates.swarmserver.key);
163 + } else {
164 + // Use new MC2 certificate
165 + certasn1 = obj.parent.agentCertificateAsn1;
166 + privateKey = obj.forge.pki.privateKeyFromPem(obj.parent.certificates.agent.key);
167 + }
168 var md = obj.forge.md.sha384.create();
169 md.update(msg.substring(2), 'binary');
170 md.update(obj.nonce, 'binary');
171 obj.agentnonce = msg.substring(50);
172
173 // Send back our certificate + signature
165 - obj.send(obj.common.ShortToStr(2) + obj.common.ShortToStr(parent.agentCertificateAsn1.length) + parent.agentCertificateAsn1 + privateKey.sign(md)); // Command 2, certificate + signature
174 + obj.send(obj.common.ShortToStr(2) + obj.common.ShortToStr(certasn1.length) + certasn1 + privateKey.sign(md)); // Command 2, certificate + signature
175
176 // Check the agent signature if we can
177 if (obj.unauthsign != null) {
169 - if (processAgentSignature(obj.unauthsign) == false) { console.log('Agent connected with bad signature, holding connection (' + obj.remoteaddr + ').'); return; } else { completeAgentConnection(); }
178 + if (processAgentSignature(obj.unauthsign) == false) { console.log('Agent connected with bad signature, holding connection (' + obj.remoteaddr + ').'); return; } else { completeAgentConnection(); }
179 }
180 }
181 else if (cmd == 2) {
@@ -197,12 +206,20 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
206 obj.agentInfo.agentVersion = obj.common.ReadInt(msg, 10);
207 obj.agentInfo.platformType = obj.common.ReadInt(msg, 14);
208 if (obj.agentInfo.platformType > 6 || obj.agentInfo.platformType < 1) { obj.agentInfo.platformType = 1; }
200 - obj.meshid = new Buffer(msg.substring(18, 66), 'binary').toString('base64').replace(/\+/g, '@').replace(/\//g, '$');;
209 + if (msg.substring(50, 66) == '\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0') {
210 + obj.meshid = new Buffer(msg.substring(18, 50), 'binary').toString('hex'); // Older HEX MeshID
211 + } else {
212 + obj.meshid = new Buffer(msg.substring(18, 66), 'binary').toString('base64').replace(/\+/g, '@').replace(/\//g, '$'); // New Base64 MeshID
213 + }
214 + //console.log('MeshID', obj.meshid);
215 obj.agentInfo.capabilities = obj.common.ReadInt(msg, 66);
216 var computerNameLen = obj.common.ReadShort(msg, 70);
217 obj.agentInfo.computerName = msg.substring(72, 72 + computerNameLen);
218 obj.dbMeshKey = 'mesh/' + obj.domain.id + '/' + obj.meshid;
219 completeAgentConnection();
220 + } else if (cmd == 5) {
221 + // ServerID. Agent is telling us what serverid it expects. Useful if we have many server certificates.
222 + if ((msg.substring(2, 34) == obj.parent.swarmCertificateHash256) || (msg.substring(2, 50) == obj.parent.swarmCertificateHash384)) { obj.useSwarmCert = true; }
223 }
224 }
225 });
@@ -463,6 +480,28 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
480 }
481 break;
482 }
483 + case 'mc1migration':
484 + {
485 + if (command.oldnodeid.length != 64) break;
486 + var oldNodeKey = 'node//' + command.oldnodeid.toLowerCase();
487 + obj.db.Get(oldNodeKey, function (err, nodes) {
488 + if (nodes.length != 1) return;
489 + var node = nodes[0];
490 + if (node.meshid == obj.dbMeshKey) {
491 + // Update the device name & host
492 + ChangeAgentCoreInfo({ name: node.name });
493 +
494 + // Delete this node including network interface information and events
495 + obj.db.Remove(node._id);
496 + obj.db.Remove('if' + node._id);
497 +
498 + // Event node deletion
499 + var change = 'Migrated device ' + node.name;
500 + obj.parent.parent.DispatchEvent(['*', node.meshid], obj, { etype: 'node', action: 'removenode', nodeid: node._id, msg: change, domain: node.domain })
501 + }
502 + });
503 + break;
504 + }
505 }
506 }
507 }
@@ -486,6 +525,7 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
525 var changes = [], change = 0;
526
527 // Check if anything changes
528 + if (command.name && (command.name != device.name)) { change = 1; device.name = command.name; changes.push('name'); }
529 if (device.agent.core != command.value) { if ((command.value == null) && (device.agent.core != null)) { delete device.agent.core; } else { device.agent.core = command.value; } change = 1; changes.push('agent core'); }
530 if ((device.agent.caps & 0xFFFFFFE7) != (command.caps & 0xFFFFFFE7)) { device.agent.caps = ((device.agent.caps & 24) + (command.caps & 0xFFFFFFE7)); change = 1; changes.push('agent capabilities'); } // Allow Javascript on the agent to change all capabilities except console and javascript support
531 if (command.intelamt) {
swarmserver.js
+6 -2
@@ -184,15 +184,19 @@ module.exports.CreateSwarmServer = function (parent, db, args, certificates) {
184 Debug(3, 'Swarm:NODEPUSH:' + JSON.stringify(nodeblock));
185
186 // Figure out what is the next agent version we need.
187 - var nextAgentVersion = 200; // TODO
187 + var nextAgentVersion = 0;
188 + if (nodeblock.agentversion < 200) { nextAgentVersion = 200; } // If less then 200, move to transitional MC1 agent.
189 + if (nodeblock.agentversion == 200) { nextAgentVersion = 201; } // If at 200, move to first MC2 agent.
190
191 // See if we need to start the agent update
190 - if ((obj.migrationAgents[nodeblock.agenttype] != null) && (obj.migrationAgents[nodeblock.agenttype][nextAgentVersion] != null)) {
192 + if ((nextAgentVersion > 0) && (obj.migrationAgents[nodeblock.agenttype] != null) && (obj.migrationAgents[nodeblock.agenttype][nextAgentVersion] != null)) {
193 // Start the update
194 socket.tag.update = obj.migrationAgents[nodeblock.agenttype][nextAgentVersion];
195 socket.tag.updatePtr = 0;
196 console.log('Performing legacy agent update from ' + nodeblock.agentversion + '.' + nodeblock.agenttype + ' to ' + socket.tag.update.ver + '.' + socket.tag.update.arch + ' on ' + nodeblock.agentname + '.');
197 obj.SendCommand(socket, LegacyMeshProtocol.GETSTATE, common.IntToStr(5) + common.IntToStr(0)); // agent.SendQuery(5, 0); // Start the agent download
198 + } else {
199 + console.log('No legacy agent update for ' + nodeblock.agentversion + '.' + nodeblock.agenttype + ' on ' + nodeblock.agentname + '.');
200 }
201 }
202 break;
webserver.js
+5
@@ -90,6 +90,11 @@ module.exports.CreateWebServer = function (parent, db, args, secret, certificate
90 obj.agentCertificateHashHex = parent.certificateOperations.forge.pki.getPublicKeyFingerprint(parent.certificateOperations.forge.pki.certificateFromPem(obj.certificates.agent.cert).publicKey, { md: parent.certificateOperations.forge.md.sha384.create(), encoding: 'hex' });
91 obj.agentCertificateHashBase64 = new Buffer(parent.certificateOperations.forge.pki.getPublicKeyFingerprint(parent.certificateOperations.forge.pki.certificateFromPem(obj.certificates.agent.cert).publicKey, { md: parent.certificateOperations.forge.md.sha384.create(), encoding: 'binary' }), 'binary').toString('base64').replace(/\+/g, '@').replace(/\//g, '$');
92 obj.agentCertificateAsn1 = parent.certificateOperations.forge.asn1.toDer(parent.certificateOperations.forge.pki.certificateToAsn1(parent.certificateOperations.forge.pki.certificateFromPem(parent.certificates.agent.cert))).getBytes();
93 + if (parent.certificates.swarmserver != null) {
94 + obj.swarmCertificateAsn1 = parent.certificateOperations.forge.asn1.toDer(parent.certificateOperations.forge.pki.certificateToAsn1(parent.certificateOperations.forge.pki.certificateFromPem(parent.certificates.swarmserver.cert))).getBytes();
95 + obj.swarmCertificateHash384 = parent.certificateOperations.forge.pki.getPublicKeyFingerprint(parent.certificateOperations.forge.pki.certificateFromPem(obj.certificates.swarmserver.cert).publicKey, { md: parent.certificateOperations.forge.md.sha384.create(), encoding: 'binary' });
96 + obj.swarmCertificateHash256 = parent.certificateOperations.forge.pki.getPublicKeyFingerprint(parent.certificateOperations.forge.pki.certificateFromPem(obj.certificates.swarmserver.cert).publicKey, { md: parent.certificateOperations.forge.md.sha256.create(), encoding: 'binary' });
97 + }
98
99 // Main lists
100 obj.wsagents = {};