sidorares / dbus-native

D-bus protocol client and server for node.js written in native javascript
Other
258 stars 93 forks source link

I got a handshake error #158

Open mikeyqin opened 7 years ago

mikeyqin commented 7 years ago

I have a problem when I use dbus-native and need your help to trouble shoot. Thanks in advance!

I am running on Ubuntu 16.04 LTS. D-Bus Message Bus Daemon 1.10.6. I configured dbus to listen to 5555, and export DBUS_SYSTEM_BUS_ADDRESS=tcp:host=127.0.0.1,port=5555,family=ipv4

When I try to connected via this code: var dbus = require('dbus-native'); var systembus = dbus.systemBus();

I got events.js:146 throw err; ^

Error: Uncaught, unspecified "error" event. (undefined) at EventEmitter.emit (events.js:144:17) at /home/haba1/node/node_modules/dbus-native/index.js:107:19 at /home/haba1/node/node_modules/dbus-native/lib/handshake.js:109:22 at /home/haba1/node/node_modules/dbus-native/lib/handshake.js:29:14 at FSReqWrap.oncomplete (fs.js:82:15)

Here is my dbus system config <!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-Bus Bus Configuration 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">

EXTERNAL ANONYMOUS tcp:host=*,bind=*,port=5555,family=ipv4 unix:path=/var/run/dbus/system_bus_socket

dbus-send works fine: $dbus-send --system --print-reply --type=method_call --dest=org.freedesktop.NetworkManager /org/freedesktop/NetworkManager org.freedesktop.NetworkManager.GetAllDevices method return time=1505175986.555811 sender=:1.12 -> destination=:1.106 serial=1406 reply_serial=2 array [ object path "/org/freedesktop/NetworkManager/Devices/0" object path "/org/freedesktop/NetworkManager/Devices/1" object path "/org/freedesktop/NetworkManager/Devices/2" object path "/org/freedesktop/NetworkManager/Devices/3"

sidorares commented 7 years ago

thanks for report @mikeyqin !

Can you help me debugging this? Tha error comes from https://github.com/sidorares/node-dbus/blob/a6ef757feb485d9f9a6367e2dc594fd5a456d1a2/lib/handshake.js#L27

I'm not exactly sure if auth method is correct ( https://dbus.freedesktop.org/doc/dbus-specification.html#auth-mechanisms-sha ) and why error passed is printed as "undefined"

Can you log filename, dirname, and error fs.stat returns? Plus maybe content of filename and attributes of thet file on your system

mikeyqin commented 7 years ago

When that code is executing, fs.stat(dirname, function(err, stat) I don't have dirname existing on my system, where dirname='/home/(mylogin)/.dbus-keyrings'.

I am wondering, if the handshake is necessary if SSL is not being used -- i don't know where to config dbus to use SSL.

mpvader commented 6 years ago

hi, I get that exact same error. I'm also trying to connect to a dbus remotely, using anonous authentication:

  const bus = dbus.createClient({
    busAddress: 'tcp:host=192.168.178.57,port=78'
  })

from the dbus-conf file on the other host (those three lines in the middle is what I added, and yes only do this in a trusted network.. :smile: ):

  <listen>unix:path=/var/run/dbus/system_bus_socket</listen>

  <listen>tcp:host=0.0.0.0,port=78</listen>
  <auth>ANONYMOUS</auth>
  <allow_anonymous/>

  <policy context="default">

The file its trying to read is /home/matthijs/.dbus-keyrings/org_freedesktop_general and it simply doesn't exist.

The code trying to do the stat are these lines:

  var dirname = path.join(getUserHome(), '.dbus-keyrings');
  // > There is a default context, "org_freedesktop_general" that's used by servers that do not specify otherwise.
  if (context.length === 0) context = 'org_freedesktop_general';

  var filename = path.join(dirname, context);
  // check it's not writable by others and readable by user
  fs.stat(dirname, function(err, stat) {
    if (err) return cb(err);

Isn't the fix simply to first check that the file exists? I'm not too familiar with fs.stat, but more than happy to make a small patch that fixes this.

Ofcourse in my case I an also work around it by specifying the auth method like this:

  const bus = dbus.createClient({
    busAddress: 'tcp:host=192.168.178.57,port=78',
    authMethods: ['ANONYMOUS']
  })
mpvader commented 6 years ago

ps. the error is this:

{ Error: ENOENT: no such file or directory, stat '/home/matthijs/.dbus-keyrings'
    at Error (native)
  errno: -2,
  code: 'ENOENT',
  syscall: 'stat',
  path: '/home/matthijs/.dbus-keyrings' }
sidorares commented 6 years ago

looks lifor some reason it tries DBUS_COOKIE_SHA1 auth

can you add some logging and check 1) if array of auth methods passed correctly at https://github.com/sidorares/dbus-native/blob/e115cc8e8a27670152216a7a7ffc51d643d4abef/lib/handshake.js#L60 2) log each line returned from deamon at https://github.com/sidorares/dbus-native/blob/e115cc8e8a27670152216a7a7ffc51d643d4abef/lib/handshake.js#L80

mpvader commented 6 years ago

looks lifor some reason it tries DBUS_COOKIE_SHA1 auth

Yes, but isnt that by design? Specifying an auth option in the opts indeed gets rid of the error.

See https://github.com/sidorares/dbus-native/blob/master/lib/constants.js#L53

It specifies the default order of trying the auth methods.

mpvader commented 6 years ago

(I’ve slighty updated above comment, and can do that logging at line 80 tomorrow)

sidorares commented 6 years ago

Sorry, I misread your comment. I thought you getting this error when you pass authMethods: ['ANONYMOUS'] setting

I guess we need to add error handling to DBUS_COOKIE_SHA1 and when it fails continue all other available methods. Should be easy to do - do you want to volunteer @mpvader ?

mpvader commented 6 years ago

Yes happy to do so. Give me a few days though; I’ll be back here!

alager commented 6 years ago

Has this been fixed? I just cloned the repo and ran the p2p example serv.js and cli.js and am getting this same error: Error: ENOENT: no such file or directory, stat '/home/aaronl/.dbus-keyrings'

UPDATE: I get an error no matter the authMethod used.

I get this crash output if I specify DBUS_COOKIE_SHA1' or 'ANONYMOUS' cli.js crash output:

node cli.js 
events.js:183
      throw er; // Unhandled 'error' event
      ^

Error: No authentication methods left to try
    at tryAuth (/home/aaronl/dbus-native/lib/handshake.js:71:15)
    at /home/aaronl/dbus-native/lib/handshake.js:87:11
    at Socket.readable (/home/aaronl/dbus-native/lib/readline.js:12:11)
    at emitNone (events.js:106:13)
    at Socket.emit (events.js:208:7)
    at emitReadable_ (_stream_readable.js:513:10)
    at emitReadable (_stream_readable.js:507:7)
    at addChunk (_stream_readable.js:274:7)
    at readableAddChunk (_stream_readable.js:250:11)
    at Socket.Readable.push (_stream_readable.js:208:10)

serv.js output

node serv.js 
[ 'hello string: ',
  '\u0000AUTH ANONYMOUS \r',
  <Buffer 00 41 55 54 48 20 41 4e 4f 4e 59 4d 4f 55 53 20 0d> ]
[ 'hello string: ',
  '\u0000AUTH EXTERNAL 31303030\r',
  <Buffer 00 41 55 54 48 20 45 58 54 45 52 4e 41 4c 20 33 31 33 30 33 30 33 30 0d> ]
[ 'hello string: ',
  '\u0000AUTH DBUS_COOKIE_SHA1 31303030\r',
  <Buffer 00 41 55 54 48 20 44 42 55 53 5f 43 4f 4f 4b 49 45 5f 53 48 41 31 20 33 31 33 30 33 30 33 30 0d> ]
[ 'hello string: ',
  '\u0000AUTH EXTERNAL 31303030\r',
  <Buffer 00 41 55 54 48 20 45 58 54 45 52 4e 41 4c 20 33 31 33 30 33 30 33 30 0d> ]
mpvader commented 6 years ago

No; it wasn’t @alager; I’ve been; and still am; completely side tracked since above exchange. Sorry