This repository has been archived by the owner on Oct 29, 2024. It is now read-only.
forked from dbusjs/node-dbus-next
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 64deceb
Showing
12 changed files
with
577 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
lib-cov | ||
*.seed | ||
*.log | ||
*.csv | ||
*.dat | ||
*.out | ||
*.pid | ||
*.gz | ||
|
||
pids | ||
logs | ||
results | ||
|
||
node_modules | ||
npm-debug.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
language: node_js | ||
node_js: | ||
- 0.6 | ||
- 0.8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
This software is released under the MIT license: | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the "Software"), to deal in | ||
the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
the Software, and to permit persons to whom the Software is furnished to do so, | ||
subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
TODO: describe examples here |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
// dbus.freedesktop.org/doc/dbus-specification.html | ||
|
||
var EventEmitter = require('events').EventEmitter; | ||
var net = require('net'); | ||
var binary = require('binary'); | ||
var Parser = require('./lib/parser'); | ||
//var constants = require('./lib/constants'); | ||
|
||
function createStream(opts) { | ||
if (opts.stream) | ||
return stream; | ||
var host = opts.host; | ||
var port = opts.port; | ||
var stream; | ||
if (port) | ||
return net.createConnection(port, host); | ||
var busAddress = opts.busAddress || process.env.DBUS_SESSION_BUS_ADDRESS; | ||
if (!busAddress) throw new Error('unknown bus address'); | ||
var familyParams = busAddress.split(':'); | ||
var family = familyParams[0]; | ||
var params = {} | ||
familyParams[1].split(',').map(function(p) { | ||
var keyVal = p.split('='); | ||
params[keyVal[0]] = keyVal[1] | ||
}); | ||
switch(family.toLowerCase()) { | ||
case 'tcp': | ||
host = params.host || 'localhost'; | ||
port = params.port; | ||
return net.createConnection(port, host); | ||
|
||
case 'unix': | ||
if (params.socket) | ||
return net.createConnection(params.socket); | ||
if (params.abstract) { | ||
return net.createConnection('\u0000' + params.abstract); | ||
} | ||
default: | ||
throw new Error('unknown address type'); | ||
} | ||
|
||
} | ||
|
||
module.exports = function (opts) { | ||
var self = new EventEmitter; | ||
if (!opts) opts = {}; | ||
var stream = self.stream = createStream(opts); | ||
stream.setNoDelay(); | ||
|
||
stream.on('error', function (err) { | ||
self.emit('error', err); | ||
}); | ||
|
||
stream.on('end', function () { | ||
self.emit('end'); | ||
self.write = function (buf) { | ||
console.warn("Didn't write bytes to closed stream"); | ||
}; | ||
}); | ||
|
||
var parser = new Parser(self, opts); | ||
|
||
self.write = function (buf) { | ||
if (Buffer.isBuffer(buf)) { | ||
stream.write(buf); | ||
} | ||
else { | ||
stream.write(buf, 'binary'); | ||
} | ||
return self; | ||
}; | ||
|
||
self.end = function () { | ||
stream.end(); | ||
return self; | ||
}; | ||
|
||
var littleEndian = 108; // 'l' | ||
//PATH = 1 | ||
//INTERFACE = 2 | ||
//MEMBER = 3 | ||
//ERROR_NAME = 4 | ||
//REPLY_SERIAL = 5 | ||
//DESTINATION = 6 | ||
//SENDER = 7 | ||
//SIGNATURE = 8 | ||
|
||
var headerType = { | ||
path: 1, | ||
interface: 2, | ||
member: 3, | ||
errorName: 4, | ||
repySerial: 5, | ||
destination: 6, | ||
sender: 7, | ||
signature: 8 | ||
}; | ||
|
||
function dstring(s) { | ||
var padded = ((s.length+1 + 3) >> 2) << 2 | ||
var buf = Buffer(padded + 4); | ||
buf.fill(0); | ||
buf.writeUInt32LE(s.length, 0); | ||
buf.write(s, 4); | ||
return buf; | ||
} | ||
|
||
// TODO: put dbus serialising here | ||
self.message = function (msg) { | ||
function send() { | ||
var messageType = 1; | ||
var flags = 0; | ||
var protocolVersion = 1; | ||
var bodyLength = 0; | ||
var serial = 1; | ||
var path = '/org/freedesktop/DBus'; | ||
var iface = 'org.freedesktop.DBus'; | ||
var member = 'Hello' | ||
var destination = 'org.freedesktop.DBus'; | ||
debugger; | ||
binary.put() | ||
.word8(littleEndian) | ||
.word8(messageType) | ||
.word8(flags) | ||
.word8(protocolVersion) | ||
.word32le(bodyLength) | ||
.word32le(serial) | ||
.word32le(0x6e) // ?? header length | ||
.word8(headerType.path) // type: path | ||
.word8(1) // ??? (length of 'o' string) | ||
.put(Buffer('o')) // object path | ||
.word8(0) | ||
.put(dstring(path)) | ||
.word8(headerType.destination) // type: iface | ||
.word8(1) | ||
.put(Buffer('s')) // utf8 string | ||
.word8(0) | ||
.put(dstring(destination)) | ||
.word8(headerType.interface) | ||
.word8(1) | ||
.put(Buffer('s')) // utf8 string | ||
.word8(0) | ||
.put(dstring(iface)) | ||
.word8(headerType.member) // type: iface | ||
.word8(1) | ||
.put(Buffer('s')) // utf8 string | ||
.word8(0) | ||
.put(dstring(member)) | ||
.write(self) | ||
; | ||
} | ||
if (self.state == 'connected') | ||
send() | ||
else { | ||
self.once('connect', send); | ||
} | ||
return self; | ||
}; | ||
return self; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
var put = require('put'); | ||
var binary = require('binary'); | ||
var crypto = require('crypto'); | ||
var fs = require('fs'); | ||
var byline = require('byline'); | ||
|
||
//var constants = require('./constants'); | ||
//var clientMsgTypes = constants.clientMsgTypes; | ||
//var encodings = constants.encodings; | ||
|
||
|
||
function sha1(input) | ||
{ | ||
var shasum = crypto.createHash('sha1'); | ||
shasum.update(input); | ||
return shasum.digest('hex'); | ||
} | ||
|
||
function getUserHome() { | ||
return process.env[(process.platform.match(/$win/)) ? 'USERPROFILE' : 'HOME']; | ||
} | ||
|
||
function getCookie(context, id, cb) { | ||
|
||
var dirname = getUserHome() + '/.dbus-keyrings'; | ||
var filename = dirname + '/' + context; | ||
// check it's not writable by others and readable by user | ||
fs.stat(dirname, function(err, stat) { | ||
if (err) | ||
cb(err); | ||
//if (stat.mode & 066) | ||
if (stat.mode & 022) | ||
return cb(new Error('User keyrings directory is writeable by other users. Aborting authentication')); | ||
if (stat.uid != process.getuid()) | ||
return cb(new Error('Keyrings directory is not owned by the current user. Aborting authentication!')); | ||
// TODO: some strange problems reading lines with binary && scan('\r\n'). Using byline until resolved | ||
var stream = byline(fs.createReadStream(filename)); | ||
var found = false; | ||
stream.on('data', function(line) { | ||
var data = line.split(' '); | ||
if (id == data[0]) | ||
{ | ||
found = true; | ||
stream.end(); | ||
return cb(null, data[2]); | ||
} | ||
}); | ||
stream.on('end', function() { | ||
if (!found) | ||
return cb(new Error('cookie not found')); | ||
}); | ||
}); | ||
} | ||
|
||
function hexlify(input) { | ||
return Buffer(input.toString(), 'ascii').toString('hex'); | ||
//return input.toString().split('').map(function(n) { return n.charCodeAt(0).toString(16) } ).join(''); | ||
} | ||
|
||
var exports = module.exports = function auth(dbus, opts) { | ||
// filter used to make a copy so we don't accidently change opts data | ||
authMethods = (opts.authMethods && opts.authMethods.filter(function() { return true})) || ['EXTERNAL', 'DBUS_COOKIE_SHA1', 'ANONYMOUS']; | ||
tryAuth.call(this, authMethods, dbus, opts); | ||
} | ||
|
||
function tryAuth(methods, dbus, opts) { | ||
var authMethod = methods.shift(); | ||
console.log('TRYING ' + authMethod); | ||
|
||
var id = hexlify(process.getuid()); | ||
var binarystream = this; | ||
|
||
function beginOrNextAuth() { | ||
binarystream | ||
.scan('reply', Buffer('\r\n')) | ||
.tap(function(vars) { | ||
var ok = vars.reply.toString('ascii').match(/^([A-Za-z]+) (.*)/); | ||
console.log(ok); | ||
console.log(vars.reply.toString()); | ||
if (ok && ok[1] === 'OK') { | ||
put().put(Buffer('BEGIN\r\n')).write(dbus.stream); | ||
dbus.state = 'connected'; | ||
dbus.emit('connect', ok[2]) // ok[2] = guid. Do we need it? | ||
} else { | ||
// TODO: parse error! | ||
if (!methods.empty) | ||
tryAuth(methods, dbus, opts); | ||
else | ||
dbus.emit('error', vars.reply); | ||
} | ||
}); | ||
} | ||
|
||
switch(authMethod) { | ||
case 'EXTERNAL': | ||
put().put(Buffer('\0AUTH ' + authMethod + ' ' + id + '\r\n')).write(dbus.stream); | ||
beginOrNextAuth(); | ||
break; | ||
case 'DBUS_COOKIE_SHA1': | ||
put().put(Buffer('\0AUTH ' + authMethod + ' ' + id + '\r\n')).write(dbus.stream); | ||
debugger; | ||
binarystream | ||
.scan('reply', Buffer('\r\n')) | ||
.tap(function(vars) { | ||
var data = Buffer(vars.reply.toString().split(' ')[1], 'hex').toString('binary').split(' '); | ||
var cookieContext = data[0]; | ||
var cookieId = data[1]; | ||
var serverChallenge = data[2]; | ||
// any random 16 bytesshould work, sha1(rnd) to make it simplier | ||
var clientChallenge = sha1(Date.now().toString() + (Math.random()*100).toString()); | ||
getCookie(cookieContext, cookieId, function(err, cookie) { | ||
if (err) throw err; | ||
var response = sha1([serverChallenge, clientChallenge, cookie].join(':')); | ||
var reply = hexlify(clientChallenge + response); | ||
dbus.write('DATA ' + reply + '\r\n'); | ||
beginOrNextAuth(); | ||
}); | ||
}); | ||
break; | ||
case 'ANONYMOUS': | ||
put().put(Buffer('\0AUTH ANONYMOUS \r\n')).write(dbus.stream); | ||
beginOrNextAuth(); | ||
break; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
var put = require('put'); | ||
var binary = require('binary'); | ||
var hexy = require('./hexy').hexy; | ||
|
||
//var constants = require('./constants'); | ||
//var clientMsgTypes = constants.clientMsgTypes; | ||
//var encodings = constants.encodings; | ||
|
||
|
||
var exports = module.exports = { | ||
unmarshall: function (stream, signature) { | ||
var binarystream = this; | ||
binarystream | ||
.word8('endianness') // TODO: use endianness! | ||
.word8('messageType') | ||
.word8('flags') | ||
.word8('protocolVersion') | ||
.word32le('bodyLength') | ||
.word32le('serial'); | ||
|
||
int32buf(binarystream, function(header) { | ||
var headerStream = binary(header); | ||
function readHeaderField() | ||
{ | ||
if (headerStream.eof()) { | ||
console.log('done header'); | ||
return; | ||
} | ||
headerStream.word8('headerType'); // the rest should be generic 'read variant' | ||
int8str(headerStream, 'ascii', function(sig) { | ||
//switch(s) { | ||
//case 's': | ||
//} | ||
console.log('SIGNATURE:', sig); | ||
int32str(headerStream, 'utf8', function(param) { | ||
console.log('PARAM: ', param); | ||
readHeaderField(); | ||
}); | ||
}); | ||
}; | ||
readHeaderField(); | ||
}); | ||
}; |
Oops, something went wrong.