mirror of
https://github.com/Ylianst/MeshAgent
synced 2025-12-24 12:13:16 +00:00
1. Fixed bug with X enumeration for multi-user
2. Fixed Linux KVM to correctly differentiate session id and monitor id
This commit is contained in:
@@ -527,11 +527,73 @@ function monitorinfo()
|
||||
if (ret == null)
|
||||
{
|
||||
// This Linux Distro does not spawn an XServer instance in the user session, that specifies the XAUTHORITY.
|
||||
if (process.platform == 'linux' && require('user-sessions').hasLoginCtl)
|
||||
{
|
||||
child = require('child_process').execFile('/bin/sh', ['sh']);
|
||||
child.stdout.str = ''; child.stdout.on('data', function (chunk) { this.str += chunk.toString(); });
|
||||
child.stderr.str = ''; child.stderr.on('data', function (chunk) { this.str += chunk.toString(); });
|
||||
|
||||
child.stdin.write("loginctl list-sessions | tr '\\n' '`' | awk '{");
|
||||
child.stdin.write('printf "[";');
|
||||
child.stdin.write('del="";');
|
||||
child.stdin.write('n=split($0, lines, "`");');
|
||||
child.stdin.write('for(i=1;i<n;++i)');
|
||||
child.stdin.write('{');
|
||||
child.stdin.write(' split(lines[i], tok, " ");');
|
||||
child.stdin.write(' if((tok[2]+0)==' + consoleuid + ')');
|
||||
child.stdin.write(' {');
|
||||
child.stdin.write(' if(tok[4]=="") { continue; }');
|
||||
child.stdin.write(' printf "%s{\\"Username\\": \\"%s\\", \\"SessionId\\": \\"%s\\", \\"State\\": \\"Online\\", \\"uid\\": \\"%s\\"}", del, tok[3], tok[1], tok[2];');
|
||||
child.stdin.write(' del=",";');
|
||||
child.stdin.write(' }');
|
||||
child.stdin.write('}');
|
||||
child.stdin.write('printf "]";');
|
||||
child.stdin.write("}'\nexit\n");
|
||||
child.waitExit();
|
||||
|
||||
var info1 = JSON.parse(child.stdout.str);
|
||||
var sids = [];
|
||||
var i;
|
||||
for (i = 0; i < info1.length; ++i) { sids.push(info1[i].SessionId); }
|
||||
child = require('child_process').execFile('/bin/sh', ['sh']);
|
||||
child.stdout.str = ''; child.stdout.on('data', function (chunk) { this.str += chunk.toString(); });
|
||||
child.stderr.str = ''; child.stderr.on('data', function (chunk) { this.str += chunk.toString(); });
|
||||
child.stdin.write("loginctl show-session -p State -p Display " + sids.join(' ') + " | tr '\\n' '`' | awk '{");
|
||||
child.stdin.write(' len=split($0,tok,"``");');
|
||||
child.stdin.write(' for(n=1;n<=len;++n)');
|
||||
child.stdin.write(' {');
|
||||
child.stdin.write(' len2=split(tok[n],val,"`");');
|
||||
child.stdin.write(' display="";');
|
||||
child.stdin.write(' active="";');
|
||||
child.stdin.write(' for(i=1;i<=len2;++i)');
|
||||
child.stdin.write(' {');
|
||||
child.stdin.write(' if(val[i] ~ /^Display=/)');
|
||||
child.stdin.write(' {');
|
||||
child.stdin.write(' gsub(/^Display=/,"",val[i]);');
|
||||
child.stdin.write(' display=val[i];');
|
||||
child.stdin.write(' }');
|
||||
child.stdin.write(' if(val[i] ~ /^State=/)');
|
||||
child.stdin.write(' {');
|
||||
child.stdin.write(' gsub(/^State=/,"",val[i]);');
|
||||
child.stdin.write(' active=val[i];');
|
||||
child.stdin.write(' }');
|
||||
child.stdin.write(' }');
|
||||
child.stdin.write(' if(active=="active") { print display; break; }');
|
||||
child.stdin.write(' }');
|
||||
child.stdin.write("}'\nexit\n");
|
||||
child.waitExit();
|
||||
|
||||
ret = { tty: '?', xauthority: (require('user-sessions').getHomeFolder(consoleuid) + '/.Xauthority').split('//').join('/'), display: child.stdout.str.trim(), exportEnv: exportEnv };
|
||||
return (xinfo_xdm(ret, consoleuid));
|
||||
}
|
||||
|
||||
|
||||
|
||||
// So we're going to brute force it, by enumerating all processes owned by this user, and inspect the environment variables
|
||||
var child = require('child_process').execFile('/bin/sh', ['sh']);
|
||||
child.stdout.str = '';
|
||||
child.stdout.on('data', function (chunk) { this.str += chunk.toString(); });
|
||||
child.stdin.write("ps " + (process.platform=='freebsd'?"-ax ":"") + "-e -o pid -o user | grep " + uname + " | awk '{ print $1 }'\nexit\n");
|
||||
child.stdin.write("ps " + (process.platform=='freebsd'?"-ax ":"") + "-e -o pid -o user | grep \" " + uname + "$\" | awk '{ print $1 }'\nexit\n");
|
||||
child.waitExit();
|
||||
|
||||
var lines = child.stdout.str.split('\n');
|
||||
|
||||
@@ -419,6 +419,18 @@ function UserSessions()
|
||||
}
|
||||
else if(process.platform == 'linux' || process.platform == 'freebsd')
|
||||
{
|
||||
Object.defineProperty(this, 'hasLoginCtl',
|
||||
{
|
||||
get: function ()
|
||||
{
|
||||
var child = require('child_process').execFile('/bin/sh', ['sh']);
|
||||
child.stdout.str = '';
|
||||
child.stdout.on('data', function (chunk) { this.str += chunk.toString(); });
|
||||
child.stdin.write("whereis loginctl | awk '{ print $2 }'\nexit\n");
|
||||
child.waitExit();
|
||||
return (child.stdout.str.trim()!="");
|
||||
}
|
||||
});
|
||||
Object.defineProperty(this, "gdmUid", {
|
||||
get: function ()
|
||||
{
|
||||
@@ -438,6 +450,13 @@ function UserSessions()
|
||||
child.waitExit();
|
||||
if (child.stdout.str.trim() != '' && (ret = parseInt(child.stdout.str.trim())) < min) { return (parseInt(child.stdout.str.trim())); }
|
||||
|
||||
child = require('child_process').execFile('/bin/sh', ['sh']);
|
||||
child.stdout.str = ''; child.stdout.on('data', function (c) { this.str += c.toString(); });
|
||||
child.stderr.str = ''; child.stderr.on('data', function (c) { this.str += c.toString(); });
|
||||
child.stdin.write('getent passwd | grep "Light Display Manager" | ' + "tr '\\n' '`' | awk -F: '{ print $3 }'\nexit\n");
|
||||
child.waitExit();
|
||||
if (child.stdout.str.trim() != '' && (ret = parseInt(child.stdout.str.trim())) < min) { return (parseInt(child.stdout.str.trim())); }
|
||||
|
||||
return (0);
|
||||
}
|
||||
});
|
||||
@@ -456,26 +475,77 @@ function UserSessions()
|
||||
|
||||
this.Current = function Current(cb)
|
||||
{
|
||||
var ret = null;
|
||||
var child = require('child_process').execFile('/bin/sh', ['sh']);
|
||||
child.stdout.str = ''; child.stdout.on('data', function (chunk) { this.str += chunk.toString(); });
|
||||
child.stderr.str = ''; child.stderr.on('data', function (chunk) { this.str += chunk.toString(); });
|
||||
child.stdin.write("who | tr '\\n' '`' | awk -F'`' '" + '{ printf "{"; for(a=1;a<NF;++a) { n=split($a, tok, " "); printf "%s\\"%s\\": \\"%s\\"", (a>1?",":""), tok[2], tok[1]; } printf "}"; }\'\nexit\n');
|
||||
child.waitExit();
|
||||
|
||||
var ret = {};
|
||||
|
||||
try
|
||||
if (process.platform == 'freebsd' || !this.hasLoginCtl)
|
||||
{
|
||||
ret = JSON.parse(child.stdout.str.trim());
|
||||
for (var key in ret)
|
||||
child.stdin.write("who | tr '\\n' '`' | awk -F'`' '" + '{ printf "{"; for(a=1;a<NF;++a) { n=split($a, tok, " "); printf "%s\\"%s\\": \\"%s\\"", (a>1?",":""), tok[2], tok[1]; } printf "}"; }\'\nexit\n');
|
||||
child.waitExit();
|
||||
}
|
||||
else
|
||||
{
|
||||
var min = this.minUid();
|
||||
|
||||
child.stdin.write("loginctl list-sessions | tr '\\n' '`' | awk '{");
|
||||
child.stdin.write('printf "[";');
|
||||
child.stdin.write('del="";');
|
||||
child.stdin.write('n=split($0, lines, "`");');
|
||||
child.stdin.write('for(i=1;i<n;++i)');
|
||||
child.stdin.write('{');
|
||||
child.stdin.write(' split(lines[i], tok, " ");');
|
||||
child.stdin.write(' if((tok[2]+0)>=' + min + ')');
|
||||
child.stdin.write(' {');
|
||||
child.stdin.write(' if(tok[4]=="") { continue; }');
|
||||
child.stdin.write(' printf "%s{\\"Username\\": \\"%s\\", \\"SessionId\\": \\"%s\\", \\"State\\": \\"Online\\", \\"uid\\": \\"%s\\"}", del, tok[3], tok[1], tok[2];');
|
||||
child.stdin.write(' del=",";');
|
||||
child.stdin.write(' }');
|
||||
child.stdin.write('}');
|
||||
child.stdin.write('printf "]";');
|
||||
child.stdin.write("}'\nexit\n");
|
||||
child.waitExit();
|
||||
|
||||
var info1 = JSON.parse(child.stdout.str);
|
||||
var sids = [];
|
||||
var i;
|
||||
for (i = 0; i < info1.length; ++i) { sids.push(info1[i].SessionId); }
|
||||
|
||||
child = require('child_process').execFile('/bin/sh', ['sh']);
|
||||
child.stdout.str = ''; child.stdout.on('data', function (chunk) { this.str += chunk.toString(); });
|
||||
child.stderr.str = ''; child.stderr.on('data', function (chunk) { this.str += chunk.toString(); });
|
||||
child.stdin.write("loginctl show-session -p State " + sids.join(' ') + " | grep State= | tr '\\n' '`' | awk -F'`' '{");
|
||||
child.stdin.write(' for(n=1;n<NF;++n)');
|
||||
child.stdin.write(' {');
|
||||
child.stdin.write(' if($n=="State=active") { print n; break; }');
|
||||
child.stdin.write(' }');
|
||||
child.stdin.write(' if(n==NF) { print 0; }');
|
||||
child.stdin.write("}'\nexit\n");
|
||||
child.waitExit();
|
||||
|
||||
i = parseInt(child.stdout.str.trim());
|
||||
if (i > 0)
|
||||
{
|
||||
info1[i - 1].State = 'Active';
|
||||
}
|
||||
ret = info1;
|
||||
}
|
||||
|
||||
if (ret == null)
|
||||
{
|
||||
try
|
||||
{
|
||||
ret = JSON.parse(child.stdout.str.trim());
|
||||
for (var key in ret)
|
||||
{
|
||||
ret[key] = { Username: ret[key], SessionId: key, State: 'Active', uid: this.getUid(ret[key]) };
|
||||
}
|
||||
}
|
||||
catch (e)
|
||||
{
|
||||
ret[key] = { Username: ret[key], SessionId: key, State: 'Active', uid: this.getUid(ret[key]) };
|
||||
}
|
||||
}
|
||||
catch(e)
|
||||
{
|
||||
}
|
||||
|
||||
Object.defineProperty(ret, 'Active', { value: showActiveOnly(ret) });
|
||||
|
||||
if (cb)
|
||||
@@ -566,31 +636,79 @@ function UserSessions()
|
||||
var child = require('child_process').execFile('/bin/sh', ['sh']);
|
||||
child.stdout.str = ''; child.stdout.on('data', function (chunk) { this.str += chunk.toString(); });
|
||||
child.stderr.str = ''; child.stderr.on('data', function (chunk) { this.str += chunk.toString(); });
|
||||
child.stdin.write("who | tr '\\n' '`' | awk -F'`' '{");
|
||||
child.stdin.write(" for(i=1;i<NF;++i) ");
|
||||
child.stdin.write(" { ");
|
||||
child.stdin.write(' split($i,tok," "); x=split(tok[2],itm,"pts"); ');
|
||||
if (process.platform != 'freebsd')
|
||||
{
|
||||
child.stdin.write(' if(x==1) ');
|
||||
}
|
||||
child.stdin.write(' { ');
|
||||
child.stdin.write(' print tok[1]; ');
|
||||
child.stdin.write(' break; ');
|
||||
child.stdin.write(' }');
|
||||
child.stdin.write(' }');
|
||||
child.stdin.write("}'\nexit\n");
|
||||
child.waitExit();
|
||||
|
||||
if (child.stderr.str != '') { return (0); }
|
||||
if (child.stdout.str.trim() != '')
|
||||
if (process.platform == 'freebsd' || !this.hasLoginCtl)
|
||||
{
|
||||
try
|
||||
child.stdin.write("who | tr '\\n' '`' | awk -F'`' '{");
|
||||
child.stdin.write(" for(i=1;i<NF;++i) ");
|
||||
child.stdin.write(" { ");
|
||||
child.stdin.write(' split($i,tok," "); x=split(tok[2],itm,"pts"); ');
|
||||
if (process.platform != 'freebsd')
|
||||
{
|
||||
return (this.getUid(child.stdout.str.trim()));
|
||||
child.stdin.write(' if(x==1) ');
|
||||
}
|
||||
catch (e)
|
||||
child.stdin.write(' { ');
|
||||
child.stdin.write(' print tok[1]; ');
|
||||
child.stdin.write(' break; ');
|
||||
child.stdin.write(' }');
|
||||
child.stdin.write(' }');
|
||||
child.stdin.write("}'\nexit\n");
|
||||
child.waitExit();
|
||||
|
||||
if (child.stderr.str != '') { return (0); }
|
||||
if (child.stdout.str.trim() != '')
|
||||
{
|
||||
try
|
||||
{
|
||||
return (this.getUid(child.stdout.str.trim()));
|
||||
}
|
||||
catch (e)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var min = this.minUid();
|
||||
|
||||
child.stdin.write("loginctl list-sessions | tr '\\n' '`' | awk '{");
|
||||
child.stdin.write('printf "[";');
|
||||
child.stdin.write('del="";');
|
||||
child.stdin.write('n=split($0, lines, "`");');
|
||||
child.stdin.write('for(i=1;i<n;++i)');
|
||||
child.stdin.write('{');
|
||||
child.stdin.write(' split(lines[i], tok, " ");');
|
||||
child.stdin.write(' if((tok[2]+0)>=' + min + ')');
|
||||
child.stdin.write(' {');
|
||||
child.stdin.write(' if(tok[4]=="") { continue; }');
|
||||
child.stdin.write(' printf "%s{\\"uid\\": \\"%s\\", \\"sid\\": \\"%s\\"}", del, tok[2], tok[1];');
|
||||
child.stdin.write(' del=",";');
|
||||
child.stdin.write(' }');
|
||||
child.stdin.write('}');
|
||||
child.stdin.write('printf "]";');
|
||||
child.stdin.write("}'\nexit\n");
|
||||
child.waitExit();
|
||||
var info1 = JSON.parse(child.stdout.str);
|
||||
var sids = [];
|
||||
var i;
|
||||
for (i = 0; i < info1.length; ++i) { sids.push(info1[i].sid); }
|
||||
|
||||
child = require('child_process').execFile('/bin/sh', ['sh']);
|
||||
child.stdout.str = ''; child.stdout.on('data', function (chunk) { this.str += chunk.toString(); });
|
||||
child.stderr.str = ''; child.stderr.on('data', function (chunk) { this.str += chunk.toString(); });
|
||||
child.stdin.write("loginctl show-session -p State " + sids.join(' ') + " | grep State= | tr '\\n' '`' | awk -F'`' '{");
|
||||
child.stdin.write(' for(n=1;n<NF;++n)');
|
||||
child.stdin.write(' {');
|
||||
child.stdin.write(' if($n=="State=active") { print n; break; }');
|
||||
child.stdin.write(' }');
|
||||
child.stdin.write(' if(n==NF) { print 0; }');
|
||||
child.stdin.write("}'\nexit\n");
|
||||
child.waitExit();
|
||||
|
||||
i = parseInt(child.stdout.str.trim());
|
||||
if (i > 0)
|
||||
{
|
||||
return (parseInt(info1[i - 1].uid));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user