diff --git a/meshctrl.js b/meshctrl.js index fb2cf9f2..8f1c1402 100644 --- a/meshctrl.js +++ b/meshctrl.js @@ -113,13 +113,13 @@ if (args['_'].length == 0) { break; } case 'sendinviteemail': { - if (args.id == null) { console.log("Device group identifier id missing, use --id [groupid]"); } + if ((args.id == null) && (args.group == null)) { console.log("Device group identifier missing, use --id [groupid] or --group [groupname]"); } else if (args.email == null) { console.log("Device email is missing, use --email [email]"); } - else { ok = true; } + else { ok = true; } break; } case 'generateinvitelink': { - if (args.id == null) { console.log("Device group identifier id missing, use --id [groupid]"); } + if ((args.id == null) && (args.group == null)) { console.log("Device group identifier missing, use --id [groupid] or --group [groupname]"); } else if (args.hours == null) { console.log("Invitation validity period missing, use --hours [hours]"); } else { ok = true; } break; @@ -135,18 +135,24 @@ if (args['_'].length == 0) { } case 'sendinviteemail': { console.log("Send invitation email with instructions on how to install the mesh agent for a specific device group. Example usage:\r\n"); - console.log(" MeshCtrl SendInviteEmail --id devicegroupid --email user@sample.com"); + console.log(" MeshCtrl SendInviteEmail --id devicegroupid --message \"msg\" --email user@sample.com"); + console.log(" MeshCtrl SendInviteEmail --group \"My Computers\" --name \"Jack\" --email user@sample.com"); console.log("\r\nRequired arguments:\r\n"); - console.log(" --id [groupid] - Device group identifier."); + console.log(" --id [groupid] - Device group identifier (or --group)."); + console.log(" --group [groupname] - Device group name (or --id)."); console.log(" --email [email] - Email address."); + console.log("\r\nOptional arguments:\r\n"); + console.log(" --name (name) - Name of recipient to be included in the email."); + console.log(" --message (msg) - Message to be included in the email."); break; } case 'generateinvitelink': { console.log("Generate a agent invitation URL for a given group. Example usage:\r\n"); console.log(" MeshCtrl GenerateInviteLink --id devicegroupid --hours 24"); - console.log(" MeshCtrl GenerateInviteLink --id devicegroupid --hours 0"); + console.log(" MeshCtrl GenerateInviteLink --group \"My Computers\" --hours 0"); console.log("\r\nRequired arguments:\r\n"); - console.log(" --id [groupid] - Device group identifier."); + console.log(" --id [groupid] - Device group identifier (or --group)."); + console.log(" --group [groupname] - Device group name (or --id)."); console.log(" --hours [hours] - Validity period in hours or 0 for infinit."); break; } @@ -624,12 +630,16 @@ function serverConnect() { break; } case 'sendinviteemail': { - var op = { action: 'inviteAgent', meshid: args.id, email: args.email, name: '', os: '0', responseid: 'meshctrl' } + var op = { action: 'inviteAgent', email: args.email, name: '', os: '0', responseid: 'meshctrl' } + if (args.id) { op.meshid = args.id; } else if (args.group) { op.meshname = args.group; } + if (args.name) { op.name = args.name; } + if (args.message) { op.msg = args.message; } ws.send(JSON.stringify(op)); break; } case 'generateinvitelink': { - var op = { action: 'createInviteLink', meshid: args.id, expire: args.hours, flags: 0, responseid: 'meshctrl' } + var op = { action: 'createInviteLink', expire: args.hours, flags: 0, responseid: 'meshctrl' } + if (args.id) { op.meshid = args.id; } else if (args.group) { op.meshname = args.group; } ws.send(JSON.stringify(op)); break; } diff --git a/meshuser.js b/meshuser.js index a13e9592..7c43f061 100644 --- a/meshuser.js +++ b/meshuser.js @@ -3126,6 +3126,14 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use } case 'inviteAgent': { + // Resolve the device group name if needed + if ((typeof command.meshname == 'string') && (command.meshid == null)) { + for (var i in parent.meshes) { + var m = parent.meshes[i]; + if ((m.mtype == 2) && (m.name == command.meshname) && parent.IsMeshViewable(user, m)) { command.meshid = m._id; break; } + } + } + var err = null, mesh = null; try { if ((parent.parent.mailserver == null) || (args.lanonly == true)) { err = 'Unsupported feature'; } // This operation requires the email server @@ -3573,13 +3581,26 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use break; } case 'createInviteLink': { + // Resolve the device group name if needed + if ((typeof command.meshname == 'string') && (command.meshid == null)) { + for (var i in parent.meshes) { + var m = parent.meshes[i]; + if ((m.mtype == 2) && (m.name == command.meshname) && parent.IsMeshViewable(user, m)) { command.meshid = m._id; break; } + } + } + var err = null; if (common.validateString(command.meshid, 8, 128) == false) { err = 'Invalid group id'; } // Check the meshid else if (common.validateInt(command.expire, 0, 99999) == false) { err = 'Invalid expire time'; } // Check the expire time in hours - else if (common.validateInt(command.flags, 0, 256) == false) { err = 'Invalid flags'; }; // Check the flags - if (command.meshid.split('/').length == 1) { command.meshid = 'mesh/' + domain.id + '/' + command.meshid; } - var smesh = command.meshid.split('/'); - if ((smesh.length != 3) || (smesh[0] != 'mesh') || (smesh[1] != domain.id)) { err = 'Invalid group id'; } + else if (common.validateInt(command.flags, 0, 256) == false) { err = 'Invalid flags'; } // Check the flags + else if (common.validateString(command.meshid, 1, 1024) == false) { err = 'Invalid group identifier'; } // Check meshid + else { + if (command.meshid.split('/').length == 1) { command.meshid = 'mesh/' + domain.id + '/' + command.meshid; } + var smesh = command.meshid.split('/'); + if ((smesh.length != 3) || (smesh[0] != 'mesh') || (smesh[1] != domain.id)) { err = 'Invalid group id'; } + mesh = parent.meshes[command.meshid]; + if ((mesh == null) || (parent.IsMeshViewable(user, mesh) == false)) { err = 'Invalid group id'; } + } var serverName = parent.getWebServerName(domain); // Handle any errors @@ -3588,8 +3609,6 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use break; } - mesh = parent.meshes[command.meshid]; - if (mesh == null) { if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'createInviteLink', responseid: command.responseid, result: 'Unable to find this device group id' })); } catch (ex) { } } break; } const inviteCookie = parent.parent.encodeCookie({ a: 4, mid: command.meshid, f: command.flags, expire: command.expire * 60 }, parent.parent.invitationLinkEncryptionKey); if (inviteCookie == null) { if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'createInviteLink', responseid: command.responseid, result: 'Unable to generate invitation cookie' })); } catch (ex) { } } break; } diff --git a/package.json b/package.json index 88d8c968..847f7e7b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "meshcentral", - "version": "0.5.1-i", + "version": "0.5.1-j", "keywords": [ "Remote Management", "Intel AMT",