From e90c1fd2395e79dea0e2667cf1a5064d1a0fc6cc Mon Sep 17 00:00:00 2001 From: Ian Scott Date: Thu, 28 Jan 2016 14:50:24 -0800 Subject: [PATCH 1/3] Find cookie with same logic as libXau --- lib/auth.js | 29 +++++++++++++++-------------- lib/handshake.js | 4 +++- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/lib/auth.js b/lib/auth.js index aa044f8..b75a0c7 100644 --- a/lib/auth.js +++ b/lib/auth.js @@ -5,6 +5,19 @@ var Buffer = require('buffer').Buffer; // add 'unpack' method for buffer require('./unpackbuffer').addUnpack(Buffer); +var typeToName = { + 256: 'Local', + 65535: 'Wild', + 254: 'Netname', + 253: 'Krb5Principal', + 252: 'LocalHost', + 0: 'Internet', + 1: 'DECnet', + 2: 'Chaos', + 5: 'ServerInterpreted', + 6: 'InternetV6' +}; + function parseXauth( buf ) { var offset = 0; @@ -14,20 +27,7 @@ function parseXauth( buf ) while (offset < buf.length) { var cookie = {}; - var typeToName = { - 256: 'Local', - 65535: 'Wild', - 254: 'Netname', - 253: 'Krb5Principal', - 252: 'LocalHost', - 0: 'Internet', - 1: 'DECnet', - 2: 'Chaos', - 5: 'ServerInterpreted', - 6: 'InternetV6' - }; cookie.type = buf.readUInt16LE(offset); - console.log('Cookie type: '); if (!typeToName[cookie.type]) { console.warn('Unknown address type'); } @@ -76,7 +76,8 @@ module.exports = function( display, host, cb ) for (var cookieNum in auth) { var cookie = auth[cookieNum]; - if (cookie.display === display && cookie.address === host) + if ((typeToName[cookie.family] === 'Wild' || cookie.address === host) && + (cookie.display.length === 0 || cookie.display === display)) return cb( null, cookie ); } cb(new Error('No auth cookie matching display=' + display + ' and host=' + host)); diff --git a/lib/handshake.js b/lib/handshake.js index 7dda277..45f08b0 100644 --- a/lib/handshake.js +++ b/lib/handshake.js @@ -192,7 +192,9 @@ function getByteOrder() { function writeClientHello(stream, displayNum, authHost) { getAuthString( displayNum, authHost, function( err, cookie ) { - debugger; + if (err) { + throw err; + } var byte_order = getByteOrder(); var protocol_major = 11; // TODO: config? env? var protocol_minor = 0; From 582bd1a1b20d9220079bd880abcfde4ae113d24c Mon Sep 17 00:00:00 2001 From: Ian Scott Date: Thu, 28 Jan 2016 14:51:16 -0800 Subject: [PATCH 2/3] Use os-homedir for older node.js versions --- lib/auth.js | 6 +++--- package.json | 3 +++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/auth.js b/lib/auth.js index b75a0c7..95b948f 100644 --- a/lib/auth.js +++ b/lib/auth.js @@ -49,17 +49,17 @@ function parseXauth( buf ) return auth; } -var os = require('os'); +var homedir = require('os-homedir'); var path = require('path'); function readXauthority(cb) { - var filename = process.env.XAUTHORITY || path.join(os.homedir(), '.Xauthority'); + var filename = process.env.XAUTHORITY || path.join(homedir(), '.Xauthority'); fs.readFile(filename, function(err, data) { if (!err) return cb(null, data); if(err.code == 'ENOENT') { // Xming/windows uses %HOME%/Xauthority ( .Xauthority with no dot ) - try with this name - filename = process.env.XAUTHORITY || path.join(os.homedir(), 'Xauthority'); + filename = process.env.XAUTHORITY || path.join(homedir(), 'Xauthority'); return fs.readFile(filename, cb); } else { cb(err); diff --git a/package.json b/package.json index c1312aa..ec03ae3 100644 --- a/package.json +++ b/package.json @@ -49,5 +49,8 @@ "scripts": { "test": "node test-runner.js", "prepublish": "npm prune" + }, + "dependencies": { + "os-homedir": "^1.0.1" } } From 86f977f9bd207783d5130a4f7afb9e82af151cea Mon Sep 17 00:00:00 2001 From: Ian Scott Date: Thu, 28 Jan 2016 16:35:04 -0800 Subject: [PATCH 3/3] Handle nonexistent Xauthority file --- lib/auth.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/auth.js b/lib/auth.js index 95b948f..264a3f2 100644 --- a/lib/auth.js +++ b/lib/auth.js @@ -60,7 +60,13 @@ function readXauthority(cb) { if(err.code == 'ENOENT') { // Xming/windows uses %HOME%/Xauthority ( .Xauthority with no dot ) - try with this name filename = process.env.XAUTHORITY || path.join(homedir(), 'Xauthority'); - return fs.readFile(filename, cb); + fs.readFile(filename, function (err, data) { + if (err.code == 'ENOENT') { + cb(null, null); + } else { + cb(err); + } + }); } else { cb(err); } @@ -72,6 +78,13 @@ module.exports = function( display, host, cb ) readXauthority(function(err, data) { if(err) return cb(err); + if (!data) { + return cb(null, { + authName: '', + authData: '' + }); + } + var auth = parseXauth(data); for (var cookieNum in auth) {