From 1134dd2f7c583e57fb0a3577f55ab52465246334 Mon Sep 17 00:00:00 2001 From: Ylian Saint-Hilaire Date: Sat, 4 Jan 2020 13:46:55 -0800 Subject: [PATCH] First version with user groups enabled. --- meshagent.js | 36 ++++++++++++++++++------------------ meshcentral.js | 6 +++--- webserver.js | 13 +++++++++++++ 3 files changed, 34 insertions(+), 21 deletions(-) diff --git a/meshagent.js b/meshagent.js index 9ed8b456..5f9d5626 100644 --- a/meshagent.js +++ b/meshagent.js @@ -72,7 +72,7 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) { db.removeAllPowerEventsForNode(obj.dbNodeKey); // Remove all power events for this node // Event node deletion - parent.parent.DispatchEvent(['*', obj.dbMeshKey], obj, { etype: 'node', action: 'removenode', nodeid: obj.dbNodeKey, domain: domain.id, nolog: 1 }); + parent.parent.DispatchEvent(parent.CreateMeshDispatchTargets(obj.dbMeshKey), obj, { etype: 'node', action: 'removenode', nodeid: obj.dbNodeKey, domain: domain.id, nolog: 1 }); // Disconnect all connections if needed const state = parent.parent.GetConnectivityState(obj.dbNodeKey); @@ -541,12 +541,12 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) { if (adminUser.links == null) adminUser.links = {}; adminUser.links[obj.dbMeshKey] = { rights: 0xFFFFFFFF }; db.SetUser(adminUser); - parent.parent.DispatchEvent(['*', obj.dbMeshKey, adminUser._id], obj, { etype: 'mesh', username: adminUser.name, meshid: obj.dbMeshKey, name: meshname, mtype: 2, desc: '', action: 'createmesh', links: links, msg: 'Mesh created: ' + obj.meshid, domain: domain.id }); + parent.parent.DispatchEvent(parent.CreateMeshDispatchTargets(obj.dbMeshKey, [adminUser._id]), obj, { etype: 'mesh', username: adminUser.name, meshid: obj.dbMeshKey, name: meshname, mtype: 2, desc: '', action: 'createmesh', links: links, msg: 'Mesh created: ' + obj.meshid, domain: domain.id }); } } else { if ((mesh != null) && (mesh.deleted != null) && (mesh.links)) { // Must un-delete this mesh - var ids = ['*', mesh._id]; + var ids = parent.CreateMeshDispatchTargets(mesh._id); // See if users still exists, if so, add links to the mesh for (var userid in mesh.links) { @@ -626,13 +626,13 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) { mesh = { type: 'mesh', _id: obj.dbMeshKey, name: obj.meshid, mtype: 2, desc: '', domain: domain.id, links: links }; db.Set(common.escapeLinksFieldName(mesh)); parent.meshes[obj.meshid] = mesh; - parent.parent.AddEventDispatch([obj.meshid], ws); + parent.parent.AddEventDispatch(parent.CreateMeshDispatchTargets(obj.meshid), ws); if (adminUser.links == null) user.links = {}; adminUser.links[obj.meshid] = { rights: 0xFFFFFFFF }; //adminUser.subscriptions = parent.subscribe(adminUser._id, ws); db.SetUser(user); - parent.parent.DispatchEvent(['*', meshid, user._id], obj, { etype: 'mesh', username: user.name, meshid: obj.meshid, name: obj.meshid, mtype: 2, desc: '', action: 'createmesh', links: links, msg: 'Mesh created: ' + obj.meshid, domain: domain.id }); + parent.parent.DispatchEvent(parent.CreateMeshDispatchTargets(meshid, [user._id]), obj, { etype: 'mesh', username: user.name, meshid: obj.meshid, name: obj.meshid, mtype: 2, desc: '', action: 'createmesh', links: links, msg: 'Mesh created: ' + obj.meshid, domain: domain.id }); } } @@ -727,7 +727,7 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) { var event = { etype: 'node', action: 'changenode', nodeid: obj.dbNodeKey, domain: domain.id, node: parent.CloneSafeNode(device) }; if (log == 0) { event.nolog = 1; } else { event.msg = 'Changed device ' + device.name + ' from group ' + mesh.name + ': ' + changes.join(', '); } if (db.changeStream) { event.noact = 1; } // If DB change stream is active, don't use this event to change the node. Another event will come. - parent.parent.DispatchEvent(['*', device.meshid], obj, event); + parent.parent.DispatchEvent(parent.CreateMeshDispatchTargets(device.meshid), obj, event); } } @@ -766,9 +766,9 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) { // Event the new node if (obj.agentInfo.capabilities & 0x20) { // This is a temporary agent, don't log. - parent.parent.DispatchEvent(['*', obj.dbMeshKey], obj, { etype: 'node', action: 'addnode', node: device, domain: domain.id, nolog: 1 }); + parent.parent.DispatchEvent(parent.CreateMeshDispatchTargets(obj.dbMeshKey), obj, { etype: 'node', action: 'addnode', node: device, domain: domain.id, nolog: 1 }); } else { - parent.parent.DispatchEvent(['*', obj.dbMeshKey], obj, { etype: 'node', action: 'addnode', node: device, msg: ('Added device ' + obj.agentInfo.computerName + ' to mesh ' + mesh.name), domain: domain.id }); + parent.parent.DispatchEvent(parent.CreateMeshDispatchTargets(obj.dbMeshKey), obj, { etype: 'node', action: 'addnode', node: device, msg: ('Added device ' + obj.agentInfo.computerName + ' to mesh ' + mesh.name), domain: domain.id }); } completeAgentConnection3(device, mesh); @@ -1099,7 +1099,7 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) { } catch (ex) { } // Event the node interface information change (This is a lot of traffic, probably don't need this). - //parent.parent.DispatchEvent(['*', obj.meshid], obj, { action: 'smBiosChange', nodeid: obj.dbNodeKey, domain: domain.id, smbios: command.value, nolog: 1 }); + //parent.parent.DispatchEvent(parent.CreateMeshDispatchTargets(obj.meshid), obj, { action: 'smBiosChange', nodeid: obj.dbNodeKey, domain: domain.id, smbios: command.value, nolog: 1 }); break; } @@ -1113,7 +1113,7 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) { db.Set(command); // Event the node interface information change - parent.parent.DispatchEvent(['*', obj.meshid], obj, { action: 'ifchange', nodeid: obj.dbNodeKey, domain: domain.id, nolog: 1 }); + parent.parent.DispatchEvent(parent.CreateMeshDispatchTargets(obj.meshid), obj, { action: 'ifchange', nodeid: obj.dbNodeKey, domain: domain.id, nolog: 1 }); break; } @@ -1152,7 +1152,7 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) { // Event node deletion const change = 'Migrated device ' + node.name; - parent.parent.DispatchEvent(['*', node.meshid], obj, { etype: 'node', action: 'removenode', nodeid: node._id, msg: change, domain: node.domain }); + parent.parent.DispatchEvent(parent.CreateMeshDispatchTargets(node.meshid), obj, { etype: 'node', action: 'removenode', nodeid: node._id, msg: change, domain: node.domain }); } }); break; @@ -1168,7 +1168,7 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) { // Log a value in the event log if ((typeof command.msg == 'string') && (command.msg.length < 4096)) { var event = { etype: 'node', action: 'agentlog', nodeid: obj.dbNodeKey, domain: domain.id, msg: command.msg }; - var targets = ['*', obj.dbMeshKey]; + var targets = parent.CreateMeshDispatchTargets(obj.dbMeshKey); if (typeof command.userid == 'string') { var loguser = parent.users[command.userid]; if (loguser) { event.userid = command.userid; event.username = loguser.name; targets.push(command.userid); } @@ -1212,7 +1212,7 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) { // Log this activation event var event = { etype: 'node', action: 'amtactivate', nodeid: obj.dbNodeKey, domain: domain.id, msg: 'Device requested Intel AMT ACM activation, FQDN: ' + command.fqdn, ip: obj.remoteaddrport }; if (db.changeStream) { event.noact = 1; } // If DB change stream is active, don't use this event to change the node. Another event will come. - parent.parent.DispatchEvent(['*', obj.dbMeshKey], obj, event); + parent.parent.DispatchEvent(parent.CreateMeshDispatchTargets(obj.dbMeshKey), obj, event); // Update the device Intel AMT information ChangeAgentCoreInfo({ "intelamt": { user: 'admin', pass: amtpassword, uuid: command.uuid, realm: command.realm } }); @@ -1262,7 +1262,7 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) { if (((obj.agentInfo.capabilities & 0x40) != 0) && (typeof command.value.value == 'string') && (command.value.value.length < 256)) { // If this is a diagnostic agent, log the event in the log of the main agent var event = { etype: 'node', action: 'diagnostic', nodeid: obj.realNodeKey, domain: domain.id, msg: command.value.value }; - parent.parent.DispatchEvent(['*', obj.dbMeshKey], obj, event); + parent.parent.DispatchEvent(parent.CreateMeshDispatchTargets(obj.dbMeshKey), obj, event); } break; } @@ -1280,7 +1280,7 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) { // Event the new sysinfo hash, this will notify everyone that the sysinfo document was changed var event = { etype: 'node', action: 'sysinfohash', nodeid: obj.dbNodeKey, domain: domain.id, hash: command.data.hash, nolog: 1 }; - parent.parent.DispatchEvent(['*', obj.dbMeshKey], obj, event); + parent.parent.DispatchEvent(parent.CreateMeshDispatchTargets(obj.dbMeshKey), obj, event); } break; } @@ -1379,7 +1379,7 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) { if (changes.length > 0) { event.msg = 'Changed device ' + device.name + ' from group ' + mesh.name + ': ' + changes.join(', '); } if ((log == 0) || ((obj.agentInfo) && (obj.agentInfo.capabilities) && (obj.agentInfo.capabilities & 0x20)) || (changes.length == 0)) { event.nolog = 1; } // If this is a temporary device, don't log changes if (db.changeStream) { event.noact = 1; } // If DB change stream is active, don't use this event to change the node. Another event will come. - parent.parent.DispatchEvent(['*', device.meshid], obj, event); + parent.parent.DispatchEvent(parent.CreateMeshDispatchTargets(device.meshid), obj, event); } } }); @@ -1420,7 +1420,7 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) { var event = { etype: 'node', action: 'changenode', nodeid: obj.dbNodeKey, domain: domain.id, node: parent.CloneSafeNode(device), msg: 'Changed device ' + device.name + ' from group ' + mesh.name + ': ' + changes.join(', ') }; if (obj.agentInfo.capabilities & 0x20) { event.nolog = 1; } // If this is a temporary device, don't log changes if (db.changeStream) { event.noact = 1; } // If DB change stream is active, don't use this event to change the node. Another event will come. - parent.parent.DispatchEvent(['*', device.meshid], obj, event); + parent.parent.DispatchEvent(parent.CreateMeshDispatchTargets(device.meshid), obj, event); } } }); @@ -1449,7 +1449,7 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) { // Event the node change var event = { etype: 'node', action: 'changenode', nodeid: obj.dbNodeKey, domain: domain.id, node: parent.CloneSafeNode(device), nolog: 1 }; if (db.changeStream) { event.noact = 1; } // If DB change stream is active, don't use this event to change the node. Another event will come. - parent.parent.DispatchEvent(['*', device.meshid], obj, event); + parent.parent.DispatchEvent(parent.CreateMeshDispatchTargets(device.meshid), obj, event); } } }); diff --git a/meshcentral.js b/meshcentral.js index 7fa44d77..726b4906 100644 --- a/meshcentral.js +++ b/meshcentral.js @@ -1338,7 +1338,7 @@ function CreateMeshCentralServer(config, args) { // Event any changes on this server only if ((newConnectivity != oldPowerState) || (newPowerState != oldPowerState)) { - obj.DispatchEvent(['*', meshid], obj, { action: 'nodeconnect', meshid: meshid, nodeid: nodeid, conn: newConnectivity, pwr: newPowerState, nolog: 1, nopeers: 1 }); + obj.DispatchEvent(obj.webserver.CreateMeshDispatchTargets(meshid), obj, { action: 'nodeconnect', meshid: meshid, nodeid: nodeid, conn: newConnectivity, pwr: newPowerState, nolog: 1, nopeers: 1 }); } } }; @@ -1386,7 +1386,7 @@ function CreateMeshCentralServer(config, args) { } // Event the node connection change - if (eventConnectChange == 1) { obj.DispatchEvent(['*', meshid], obj, { action: 'nodeconnect', meshid: meshid, nodeid: nodeid, conn: state.connectivity, pwr: state.powerState, ct: connectTime, nolog: 1, nopeers: 1 }); } + if (eventConnectChange == 1) { obj.DispatchEvent(obj.webserver.CreateMeshDispatchTargets(meshid), obj, { action: 'nodeconnect', meshid: meshid, nodeid: nodeid, conn: state.connectivity, pwr: state.powerState, ct: connectTime, nolog: 1, nopeers: 1 }); } } else { // Multi server mode @@ -1459,7 +1459,7 @@ function CreateMeshCentralServer(config, args) { } // Event the node connection change - if (eventConnectChange == 1) { obj.DispatchEvent(['*', meshid], obj, { action: 'nodeconnect', meshid: meshid, nodeid: nodeid, conn: state.connectivity, pwr: state.powerState, nolog: 1, nopeers: 1 }); } + if (eventConnectChange == 1) { obj.DispatchEvent(obj.webserver.CreateMeshDispatchTargets(meshid), obj, { action: 'nodeconnect', meshid: meshid, nodeid: nodeid, conn: state.connectivity, pwr: state.powerState, nolog: 1, nopeers: 1 }); } } else { // Multi server mode diff --git a/webserver.js b/webserver.js index 6a4a4e5e..b453ced6 100644 --- a/webserver.js +++ b/webserver.js @@ -4079,6 +4079,19 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) { return false; } + // Returns a list of displatch targets for a given mesh + // We have to target the meshid and all user groups for this mesh, plus any added targets + obj.CreateMeshDispatchTargets = function (mesh, addedTargets) { + var targets = (addedTargets != null) ? addedTargets : []; + if (targets.indexOf('*') == -1) { targets.push('*'); } + if (typeof mesh == 'string') { mesh = obj.meshes[mesh]; } + if (mesh != null) { + targets.push(mesh._id); + for (var i in mesh.links) { if (i.startsWith('ugrp/')) { targets.push(i); } } + } + return targets; + } + // Clone a safe version of a user object, remove everything that is secret. obj.CloneSafeUser = function (user) { if (typeof user != 'object') { return user; }