Added access control checks to IP-KVM remote desktop.

Ylian Saint-Hilaire committed Dec 6, 2021 at 22:05 UTC 94c731bf449d5865b24dd2f6c5622388a9be891d
1 file changed +54 -9
meshipkvm.js
+54 -9
@@ -11,7 +11,31 @@ function CreateIPKVMManager(parent) {
11 obj.parent = parent;
12 obj.managedGroups = {} // meshid --> Manager
13 obj.managedPorts = {} // nodeid --> PortInfo
14 -
14 +
15 + // Mesh Rights
16 + const MESHRIGHT_EDITMESH = 0x00000001; // 1
17 + const MESHRIGHT_MANAGEUSERS = 0x00000002; // 2
18 + const MESHRIGHT_MANAGECOMPUTERS = 0x00000004; // 4
19 + const MESHRIGHT_REMOTECONTROL = 0x00000008; // 8
20 + const MESHRIGHT_AGENTCONSOLE = 0x00000010; // 16
21 + const MESHRIGHT_SERVERFILES = 0x00000020; // 32
22 + const MESHRIGHT_WAKEDEVICE = 0x00000040; // 64
23 + const MESHRIGHT_SETNOTES = 0x00000080; // 128
24 + const MESHRIGHT_REMOTEVIEWONLY = 0x00000100; // 256
25 + const MESHRIGHT_NOTERMINAL = 0x00000200; // 512
26 + const MESHRIGHT_NOFILES = 0x00000400; // 1024
27 + const MESHRIGHT_NOAMT = 0x00000800; // 2048
28 + const MESHRIGHT_DESKLIMITEDINPUT = 0x00001000; // 4096
29 + const MESHRIGHT_LIMITEVENTS = 0x00002000; // 8192
30 + const MESHRIGHT_CHATNOTIFY = 0x00004000; // 16384
31 + const MESHRIGHT_UNINSTALL = 0x00008000; // 32768
32 + const MESHRIGHT_NODESKTOP = 0x00010000; // 65536
33 + const MESHRIGHT_REMOTECOMMAND = 0x00020000; // 131072
34 + const MESHRIGHT_RESETOFF = 0x00040000; // 262144
35 + const MESHRIGHT_GUESTSHARING = 0x00080000; // 524288
36 + const MESHRIGHT_DEVICEDETAILS = 0x00100000; // ?1048576?
37 + const MESHRIGHT_ADMIN = 0xFFFFFFFF;
38 +
39 // Subscribe for mesh creation events
40 parent.AddEventDispatch(['server-createmesh', 'server-deletemesh'], obj);
41 obj.HandleEvent = function (source, event, ids, id) {
@@ -182,16 +206,36 @@ function CreateIPKVMManager(parent) {
206 }
207
208 // Handle a IP-KVM HTTP get request
185 - obj.handleIpKvmGet = function(domain, req, res, next) {
209 + obj.handleIpKvmGet = function (domain, req, res, next) {
210 + // Parse the URL and get information about this KVM port
211 const reqinfo = parseIpKvmUrl(domain, req.url);
212 if (reqinfo == null) { next(); return; }
213 +
214 + // Check node rights
215 + if ((req.session == null) || (req.session.userid == null)) { next(); return; }
216 + const user = parent.webserver.users[req.session.userid];
217 + if (user == null) { next(); return; }
218 + const rights = parent.webserver.GetNodeRights(user, reqinfo.kvmmanager.meshid, reqinfo.nodeid);
219 + if ((rights & MESHRIGHT_REMOTECONTROL) == 0) { next(); return; }
220 +
221 + // Process the request
222 reqinfo.kvmmanager.handleIpKvmGet(domain, reqinfo, req, res, next);
223 }
224
225 // Handle a IP-KVM HTTP websocket request
226 obj.handleIpKvmWebSocket = function (domain, ws, req) {
227 + // Parse the URL and get information about this KVM port
228 const reqinfo = parseIpKvmUrl(domain, req.url);
229 if (reqinfo == null) { try { ws.close(); } catch (ex) { } return; }
230 +
231 + // Check node rights
232 + if ((req.session == null) || (req.session.userid == null)) { try { ws.close(); } catch (ex) { } return; }
233 + const user = parent.webserver.users[req.session.userid];
234 + if (user == null) { try { ws.close(); } catch (ex) { } return; }
235 + const rights = parent.webserver.GetNodeRights(user, reqinfo.kvmmanager.meshid, reqinfo.nodeid);
236 + if ((rights & MESHRIGHT_REMOTECONTROL) == 0) { try { ws.close(); } catch (ex) { } return; }
237 +
238 + // Process the request
239 reqinfo.kvmmanager.handleIpKvmWebSocket(domain, reqinfo, ws, req);
240 }
241
@@ -308,6 +352,7 @@ function CreateRaritanKX3Manager(parent, hostname, port, username, password) {
352 }
353 }
354 obj.fetch('/sidebar.asp', null, null, function (server, tag, data) {
355 + data = data.toString();
356 var dataBlock = getSubString(data, "updateKVMLinkHintOnContainer();", "devices.resetDevicesNew(1);");
357 if (dataBlock == null) { setState(0); return; }
358 const parsed = parseJsScript(dataBlock);
@@ -329,6 +374,7 @@ function CreateRaritanKX3Manager(parent, hostname, port, username, password) {
374
375 obj.update = function () {
376 obj.fetch('/webs_cron.asp?_portsstatushash=' + obj.portHash + '&_devicesstatushash=' + obj.deviceHash, null, null, function (server, tag, data) {
377 + data = data.toString();
378 const parsed = parseJsScript(data);
379 if (parsed['updatePortStatus']) {
380 obj.portCount = parseInt(parsed['updatePortStatus'][0][0]) - 2;
@@ -433,7 +479,7 @@ function CreateRaritanKX3Manager(parent, hostname, port, username, password) {
479 obj.fetch = function(url, postdata, tag, func) {
480 if (obj.state == 0) return;
481
436 - var data = '';
482 + var data = [];
483 const options = {
484 hostname: hostname,
485 port: port,
@@ -450,11 +496,11 @@ function CreateRaritanKX3Manager(parent, hostname, port, username, password) {
496 if (obj.state == 0) return;
497 if (res.statusCode != 200) { setState(0); return; }
498 if (res.headers['set-cookie'] != null) { for (var i in res.headers['set-cookie']) { if (res.headers['set-cookie'][i].startsWith('pp_session_id=')) { obj.authCookie = res.headers['set-cookie'][i].substring(14).split(';')[0]; } } }
453 - res.on('data', function (d) { data += d; });
499 + res.on('data', function (d) { data.push(d); });
500 res.on('end', function () {
501 // This line is used for debugging only, used to swap a file.
456 - //if (url.endsWith('js_kvm_client.1604062083669.min.js')) { data = parent.parent.fs.readFileSync('c:\\tmp\\js_kvm_client.1604062083669.min.js').toString(); }
457 - func(obj, tag, data, res);
502 + //if (url.endsWith('js_kvm_client.1604062083669.min.js')) { data = [ parent.parent.fs.readFileSync('c:\\tmp\\js_kvm_client.1604062083669.min.js') ] ; }
503 + func(obj, tag, Buffer.concat(data), res);
504 });
505 });
506 req.on('error', function (error) { console.log(error); setState(0); })
@@ -469,8 +515,8 @@ function CreateRaritanKX3Manager(parent, hostname, port, username, password) {
515 obj.fetch(reqinfo.relurl, null, [res, reqinfo], function (server, args, data, rres) {
516 const resx = args[0], xreqinfo = args[1];
517 if (rres.headers['content-type']) { resx.set('content-type', rres.headers['content-type']); }
472 -
518 if (xreqinfo.relurl.startsWith('/js/js_kvm_client.')) {
519 + data = data.toString();
520 // Since our cookies can't be read from the html page for security, we embed the cookie right into the page.
521 data = data.replace('module$js$helper$Extensions.Utils.getCookieValue("pp_session_id")', '"' + obj.authCookie + '"');
522 // Add the connection information directly into the file.
@@ -478,7 +524,6 @@ function CreateRaritanKX3Manager(parent, hostname, port, username, password) {
524 // Replace the WebSocket code in one of the files to make it work with our server.
525 data = data.replace('b=new WebSocket(e+"//"+c+"/"+g);', 'b=new WebSocket(e+"//"+c+"/ipkvm.ashx/' + xreqinfo.nid + '/"+g);');
526 }
481 -
527 resx.end(data);
528 });
529 }
@@ -497,7 +542,7 @@ function CreateRaritanKX3Manager(parent, hostname, port, username, password) {
542 try {
543 const options = {
544 rejectUnauthorized: false,
500 - servername: 'Raritan',
545 + servername: 'raritan', // We set this to remove the IP address warning from NodeJS.
546 headers: { Cookie: 'pp_session_id=' + obj.authCookie + '; view_length=32' }
547 };
548 parent.parent.debug('relay', 'IPKVM: Relay connecting to: wss://' + hostname + ':' + port + '/rfb');