Linux user shell improvements.

Ylian Saint-Hilaire committed Nov 25, 2019 at 16:11 UTC 92df2b5ea7478f0ac77b36cbfc3559749001a886
3 files changed +42 -30
agents/meshcore.js
+18 -12
@@ -1088,8 +1088,7 @@ function createMeshCore(agent) {
1088 } else {
1089 this.httprequest._term = require('win-terminal').Start(80, 25); // TODO: Start as logged in used when protocol is 7
1090 }
1091 - }
1092 - catch (e) {
1091 + } catch (e) {
1092 MeshServerLog('Failed to start remote terminal session, ' + e.toString() + ' (' + this.httprequest.remoteaddr + ')', this.httprequest);
1093 this.write(JSON.stringify({ ctrlChannel: '102938', type: 'console', msg: e.toString() }));
1094 this.end();
@@ -1097,17 +1096,24 @@ function createMeshCore(agent) {
1096 }
1097 this.httprequest._term.pipe(this, { dataTypeSkip: 1 });
1098 this.pipe(this.httprequest._term, { dataTypeSkip: 1, end: false });
1100 - this.prependListener('end', function () { this.httprequest._term.end(function () { console.log('Terminal was closed'); }); });
1099 + this.prependListener('end', function () { this.httprequest._term.end(function () { console.log("Terminal was closed"); }); });
1100 } else {
1102 - if (fs.existsSync("/usr/bin/python") && fs.existsSync("/bin/bash")) {
1103 - this.httprequest.process = childProcess.execFile("/usr/bin/python", ["python", "-c", "import pty; pty.spawn([\"/bin/bash\"])"], { uid: (this.httprequest.protocol == 7) ? require('user-sessions').consoleUid() : null }); // Start as active user
1104 - if (process.platform == 'linux') { this.httprequest.process.stdin.write("export TERM='xterm'\nalias ls='ls --color=auto'\nclear\n"); }
1105 - } else if (fs.existsSync("/bin/bash")) {
1106 - this.httprequest.process = childProcess.execFile("/bin/bash", ["bash", "-i"], { type: childProcess.SpawnTypes.TERM, uid: (this.httprequest.protocol == 7) ? require('user-sessions').consoleUid() : null }); // Start as active user
1107 - if (process.platform == 'linux') { this.httprequest.process.stdin.write("alias ls='ls --color=auto'\nclear\n"); }
1108 - } else {
1109 - this.httprequest.process = childProcess.execFile("/bin/sh", ["sh"], { type: childProcess.SpawnTypes.TERM, uid: (this.httprequest.protocol == 7)?require('user-sessions').consoleUid():null }); // Start as active user
1110 - if (process.platform == 'linux') { this.httprequest.process.stdin.write("stty erase ^H\nalias ls='ls --color=auto'\nPS1='\\u@\\h:\\w\\$ '\nclear\n"); }
1101 + try {
1102 + if (fs.existsSync('/usr/bin/python') && fs.existsSync('/bin/bash')) {
1103 + this.httprequest.process = childProcess.execFile('/usr/bin/python', ['python', '-c', "import pty; pty.spawn([\"/bin/bash\"])"], { uid: (this.httprequest.protocol == 7) ? require('user-sessions').consoleUid() : null }); // Start as active user
1104 + if (process.platform == 'linux') { this.httprequest.process.stdin.write("export TERM='xterm'\nalias ls='ls --color=auto'\nclear\n"); }
1105 + } else if (fs.existsSync('/bin/bash')) {
1106 + this.httprequest.process = childProcess.execFile('/bin/bash', ['bash', '-i'], { type: childProcess.SpawnTypes.TERM, uid: (this.httprequest.protocol == 7) ? require('user-sessions').consoleUid() : null }); // Start as active user
1107 + if (process.platform == 'linux') { this.httprequest.process.stdin.write("alias ls='ls --color=auto'\nclear\n"); }
1108 + } else {
1109 + this.httprequest.process = childProcess.execFile('/bin/sh', ['sh'], { type: childProcess.SpawnTypes.TERM, uid: (this.httprequest.protocol == 7)?require('user-sessions').consoleUid():null }); // Start as active user
1110 + if (process.platform == 'linux') { this.httprequest.process.stdin.write("stty erase ^H\nalias ls='ls --color=auto'\nPS1='\\u@\\h:\\w\\$ '\nclear\n"); }
1111 + }
1112 + } catch (e) {
1113 + MeshServerLog("Failed to start remote terminal session, " + e.toString() + ' (' + this.httprequest.remoteaddr + ')', this.httprequest);
1114 + this.write(JSON.stringify({ ctrlChannel: '102938', type: 'console', msg: e.toString() }));
1115 + this.end();
1116 + return;
1117 }
1118 this.httprequest.process.tunnel = this;
1119 this.httprequest.process.on('exit', function (ecode, sig) { this.tunnel.end(); });
agents/meshcore.min.js
+23 -17
@@ -1046,11 +1046,11 @@ function createMeshCore(agent) {
1046 if ((data == 'c') || (data == 'cr')) { this.httprequest.state = 1; /*sendConsoleText("Tunnel #" + this.httprequest.index + " now active", this.httprequest.sessionid);*/ }
1047 } else {
1048 // Handle tunnel data
1049 - if (this.httprequest.protocol == 0) { // 1 = Terminal, 2 = Desktop, 5 = Files, 6 = PowerShell
1049 + if (this.httprequest.protocol == 0) { // 1 = Terminal (admin), 2 = Desktop, 5 = Files, 6 = PowerShell (admin), 7 = Terminal (user), 8 = PowerShell (user)
1050 // Take a look at the protocol
1051 this.httprequest.protocol = parseInt(data);
1052 if (typeof this.httprequest.protocol != 'number') { this.httprequest.protocol = 0; }
1053 - if ((this.httprequest.protocol == 1) || (this.httprequest.protocol == 6)) {
1053 + if ((this.httprequest.protocol == 1) || (this.httprequest.protocol == 6) || (this.httprequest.protocol == 7) || (this.httprequest.protocol == 8)) {
1054 // Check user access rights for terminal
1055 if (((this.httprequest.rights & MESHRIGHT_REMOTECONTROL) == 0) || ((this.httprequest.rights != 0xFFFFFFFF) && ((this.httprequest.rights & MESHRIGHT_NOTERMINAL) != 0))) {
1056 // Disengage this tunnel, user does not have the rights to do this!!
@@ -1083,13 +1083,12 @@ function createMeshCore(agent) {
1083 // Remote terminal using native pipes
1084 if (process.platform == "win32") {
1085 try {
1086 - if ((this.httprequest.protocol == 6) && (require('win-terminal').PowerShellCapable() == true)) {
1087 - this.httprequest._term = require('win-terminal').StartPowerShell(80, 25);
1086 + if (((this.httprequest.protocol == 6) || (this.httprequest.protocol == 8)) && (require('win-terminal').PowerShellCapable() == true)) {
1087 + this.httprequest._term = require('win-terminal').StartPowerShell(80, 25); // TODO: Start as logged in used when protocol is 8
1088 } else {
1089 - this.httprequest._term = require('win-terminal').Start(80, 25);
1089 + this.httprequest._term = require('win-terminal').Start(80, 25); // TODO: Start as logged in used when protocol is 7
1090 }
1091 - }
1092 - catch (e) {
1091 + } catch (e) {
1092 MeshServerLog('Failed to start remote terminal session, ' + e.toString() + ' (' + this.httprequest.remoteaddr + ')', this.httprequest);
1093 this.write(JSON.stringify({ ctrlChannel: '102938', type: 'console', msg: e.toString() }));
1094 this.end();
@@ -1097,17 +1096,24 @@ function createMeshCore(agent) {
1096 }
1097 this.httprequest._term.pipe(this, { dataTypeSkip: 1 });
1098 this.pipe(this.httprequest._term, { dataTypeSkip: 1, end: false });
1100 - this.prependListener('end', function () { this.httprequest._term.end(function () { console.log('Terminal was closed'); }); });
1099 + this.prependListener('end', function () { this.httprequest._term.end(function () { console.log("Terminal was closed"); }); });
1100 } else {
1102 - if (fs.existsSync("/usr/bin/python") && fs.existsSync("/bin/bash")) {
1103 - this.httprequest.process = childProcess.execFile("/usr/bin/python", [ "python", "-c", "import pty; pty.spawn([\"/bin/bash\"])" ]);
1104 - if (process.platform == 'linux') { this.httprequest.process.stdin.write("export TERM='xterm'\nalias ls='ls --color=auto'\nclear\n"); }
1105 - } else if (fs.existsSync("/bin/bash")) {
1106 - this.httprequest.process = childProcess.execFile("/bin/bash", ["bash", "-i"], { type: childProcess.SpawnTypes.TERM });
1107 - if (process.platform == 'linux') { this.httprequest.process.stdin.write("alias ls='ls --color=auto'\nclear\n"); }
1108 - } else {
1109 - this.httprequest.process = childProcess.execFile("/bin/sh", ["sh"], { type: childProcess.SpawnTypes.TERM }); // , uid: require('user-sessions').consoleUid()
1110 - if (process.platform == 'linux') { this.httprequest.process.stdin.write("stty erase ^H\nalias ls='ls --color=auto'\nPS1='\\u@\\h:\\w\\$ '\nclear\n"); }
1101 + try {
1102 + if (fs.existsSync('/usr/bin/python') && fs.existsSync('/bin/bash')) {
1103 + this.httprequest.process = childProcess.execFile('/usr/bin/python', ['python', '-c', "import pty; pty.spawn([\"/bin/bash\"])"], { uid: (this.httprequest.protocol == 7) ? require('user-sessions').consoleUid() : null }); // Start as active user
1104 + if (process.platform == 'linux') { this.httprequest.process.stdin.write("export TERM='xterm'\nalias ls='ls --color=auto'\nclear\n"); }
1105 + } else if (fs.existsSync('/bin/bash')) {
1106 + this.httprequest.process = childProcess.execFile('/bin/bash', ['bash', '-i'], { type: childProcess.SpawnTypes.TERM, uid: (this.httprequest.protocol == 7) ? require('user-sessions').consoleUid() : null }); // Start as active user
1107 + if (process.platform == 'linux') { this.httprequest.process.stdin.write("alias ls='ls --color=auto'\nclear\n"); }
1108 + } else {
1109 + this.httprequest.process = childProcess.execFile('/bin/sh', ['sh'], { type: childProcess.SpawnTypes.TERM, uid: (this.httprequest.protocol == 7)?require('user-sessions').consoleUid():null }); // Start as active user
1110 + if (process.platform == 'linux') { this.httprequest.process.stdin.write("stty erase ^H\nalias ls='ls --color=auto'\nPS1='\\u@\\h:\\w\\$ '\nclear\n"); }
1111 + }
1112 + } catch (e) {
1113 + MeshServerLog("Failed to start remote terminal session, " + e.toString() + ' (' + this.httprequest.remoteaddr + ')', this.httprequest);
1114 + this.write(JSON.stringify({ ctrlChannel: '102938', type: 'console', msg: e.toString() }));
1115 + this.end();
1116 + return;
1117 }
1118 this.httprequest.process.tunnel = this;
1119 this.httprequest.process.on('exit', function (ecode, sig) { this.tunnel.end(); });
package.json
+1 -1
@@ -1,6 +1,6 @@
1 {
2 "name": "meshcentral",
3 - "version": "0.4.4-u",
3 + "version": "0.4.4-v",
4 "keywords": [
5 "Remote Management",
6 "Intel AMT",