MQTT connection refused
Environment
- ejabberd version: 21.04
- Erlang version: Erlang (SMP,ASYNC_THREADS,HIPE) (BEAM) emulator version 11.2.1
- OS: arch Linux
- Installed from: distro package
Error
Connection error: Connection Refused: not authorised.
Bug description
MQTT setup according to Marc Foss tutorial https://www.process-one.net/blog/starting-with-mqtt-protocol-and-ejabberd-mqtt-broker/
Created the user mqtt, but the internal select uses a number which of course does not exist in users table in database.
2021-05-08 13:55:12.980715+02:00 [debug] <0.839.0>@mod_mqtt_session:handle_info/2:240 Got MQTT packet:
#connect{proto_level = 4,will = undefined,clean_start = true,keep_alive = 60,
client_id = <<>>,username = <<"[email protected]">>,
password = <<"mqtt12345">>,will_properties = #{},properties = #{}}
2021-05-08 13:55:12.982617+02:00 [debug] <0.374.0>@ejabberd_sql:sql_query_internal/1:658 SQL:
"select password, serverkey, salt, iterationcount from users where username='10347175647217333926' and 0=0"
2021-05-08 13:55:12.984472+02:00 [warning] <0.839.0>@mod_mqtt_session:log_disconnection/2:1361
Rejected MQTT connection from 0.0.0.0: Authentication failed: Not authorized
similar here client_id = <<>>, but mnesia db and tls:
2021-09-27 02:22:50.835688+02:00 [info] <0.538.0>@ejabberd_listener:accept/7:273 (<0.754.0>) Accepted connection [::ffff:xxx.xx.xx.xx]:13301 -> [::ffff:10.0.0.yy]:8883
2021-09-27 02:22:50.846762+02:00 [notice] <0.726.0> (tls|<0.726.0>) Received XML on stream = <<" ">>
2021-09-27 02:22:50.920901+02:00 [debug] <0.754.0>@mod_mqtt_session:handle_info/2:240 Got MQTT packet:
#connect{proto_level = 4,will = undefined,clean_start = true,keep_alive = 60,
client_id = <<>>,username = <<"[email protected]">>,
password = <<"xxxxxxxxxxxxxxxxxxx">>,will_properties = #{},
properties = #{}}
2021-09-27 02:22:50.921552+02:00 [warning] <0.754.0>@mod_mqtt_session:log_disconnection/2:1362 Rejected MQTT connection from ::ffff:zz.zz.zz.zz: Authentication failed: Not authorized
2021-09-27 02:22:50.921652+02:00 [debug] <0.754.0>@mod_mqtt_session:do_send/2:871 Send MQTT packet:
#connack{session_present = false,code = 'not-authorized',properties = #{}}
Environment: ejabberd 21.01-2 (Debian repo) Debian 11
any movement on this issue? I'm having the same issue, I've been tying a lot of different configurations all with the same result. must be some configuration I am missing that maybe isn't documented?
(ejabberd@localhost)1> 22:21:31.517 [debug] Got MQTT packet:
#connect{proto_level = 4,will = undefined,clean_start = true,keep_alive = 60,
client_id = <<>>,username = <<"broker@******.***">>,
password = <<"testpassword">>,will_properties = #{},properties = #{}}
(ejabberd@localhost)1> 22:21:31.519 [warning] Rejected MQTT connection from 1*2.25*.*1*.56: Authentication failed: Not authorized
(ejabberd@localhost)1> 22:21:31.519 [debug] Send MQTT packet:
#connack{session_present = false,code = 'not-authorized',properties = #{}}
Ejabberd: 21.12 ubuntu 20.04 not using tls
Gist of your .yml?
thanks for your time. here is the current version. I have tried a lot of different things but so far most of my syntax changes result in the same authentication problem. the username and password I am using do work in other cases.
https://gist.github.com/cedarknoll/f78de81dab2e7a382283c71480eace03
EDIT: I also wanted to add that I tried setting up MQTT without "access control" following the "Adding MQTT service in ejabberd.yml" part of this guide: https://docs.ejabberd.im/admin/guide/mqtt/ I got the same error.
Hi, I'm learning MQTT now, I followed that tutorial and found some problems and their solutions:
No auth details? Set anonymous
❯ mosquitto_sub -h localhost -t "test/channel/1" -d
Client (null) sending CONNECT
Client (null) received CONNACK (5)
Connection error: Connection Refused: not authorised.
Client (null) sending DISCONNECT
Something important that the tutorial does not mention is that, if you don't want to specify authentication credentials, you must replace
auth_method: internal
with:
auth_method: anonymous
anonymous_protocol: login_anon
And now:
❯ mosquitto_sub -h localhost -t "test/channel/1" -d
Client (null) sending CONNECT
Client (null) received CONNACK (0)
Client (null) sending SUBSCRIBE (Mid: 1, Topic: test/channel/1, QoS: 0, Options: 0x00)
Client (null) received SUBACK
Subscribed (mid: 1): 0
Empty ClientId triggers a bug in ejabberd
man mosquitto_sub
-i, --id The id to use for this client. If not given, a client id will be generated depending on the MQTT version being used. For v3.1.1/v3.1, the client generates a client id in the format mosq-XXXXXXXXXXXXXXXXXX, where the X are replaced with random alphanumeric characters. For v5.0, the client sends a zero length client id, and the server will generate a client id for the client.
However, there's a bug in ejabberd: when it generates a clientid, it forgets all the other authentication credentials, including username and password. This is fixed in https://github.com/processone/ejabberd/commit/03a11c63bd9450a857b5c2d60c0c7337280918e3
A workaround, if you can't update ejabberd right now: provide a client id, so you don't suffer the bug in ejabberd:
❯ mosquitto_sub -h example.com -t "test/channel/1" -u [email protected] -P asd -d --id 123456789
I've improved the section about MQTT in the ejabberd documentation. From now on, please use that one as reference: https://docs.ejabberd.im/admin/guide/mqtt/
Thanks @badlop for sharing your findings!
Where do you set auth_method: anonymous?
I did check the link from your last post.
There are still the buggy examples with the full username like mosquitto_sub -u user1@localhost ... and i'm missing the hints about the empty ClientId problem. (Or did I find an old version again?)
There are still the buggy examples with the full username like mosquitto_sub -u user1@localhost ... and i'm missing the hints about the empty ClientId problem. (Or did I find an old version again?)
Initially I thought -u is only for the username. But with more testing, -u accepts username@server, and in fact that is preferable. So I edited my comment, and included that in the docs site.
Where do you set auth_method: anonymous?
anonymous is only if you are not interested in accounts at all: https://docs.ejabberd.im/admin/configuration/authentication/
Thanks for clarification!