victronenergy / venus

Victron Energy Unix/Linux OS
https://github.com/victronenergy/venus/wiki
577 stars 72 forks source link

Fix MQTT connections being refused when using empty username #1257

Closed mpvader closed 5 months ago

mpvader commented 5 months ago

NOTE: I wrote this issue mainly to have the details available when someone asks again in the future - problem is solved already; per Venus OS v3.31

The story:

Per Venus OS ... we switched from Mosquitto to FlashMQ. And that resulted, for some MQTT clients, in a regression:

There is a difference between how Mosquitto and FlashMQ treat anonymous logins on MQTT. Mosquitto treated the specifications a bit loosely, and accepted an empty username, instead of only no username. FlashMQ is more strict, and if during login the client says that there is a username, it also expects one. And if there is none, it refuses to connect.

On some clients and implementations that works fine; but - as clear now - there are also implementations on which that gives a problem. What you see then is that the software you're using is unable to connect. If you'd look in the logs on the GX device, you'd see this:

[ERROR] Unspecified or non-MQTT protocol error: Username flagged as present, but it's 0 bytes.. Removing client. To work around this, configure a username and password in the MQTT software you are using. Then FlashMQ will accept the connection, even though there is no username or password required.

Note that that workaround does not require you to login to the GX command line and make changes there.

Per Venus OS v3.31, that was fixed, by updating flashmq + enabling the new zero_byte_username_is_anonymous option.

Which clients were affected is not totally clear, but names that were mentioned on Community are the openEVSE charger and edomi/knx. The often used MQTT Explorer package is not affected.

Here is how to test / reproduce it, all using the MQTT v5 protocol. With v3.11 protocol its the same.:

## No user name against Venus OS v3.30 - connects OK

matthijs@mva-ubuntu2204:~$ mosquitto_sub -d -t '#' -h 192.168.178.221 -V 5
(...)
{"value":"c0847dc9a93d"}

## Empty user name against Venus OS v3.30 - connection FAILS:

$ mosquitto_sub -d -t '#' -h 192.168.178.221 -u '' -P '' -V 5
Client (null) sending CONNECT
Client (null) received CONNACK (129)
Connection error: Malformed Packet
Client (null) sending DISCONNECT
^C

## No user name against Venus OS v3.31 - still connects OK

$ mosquitto_sub -d -t '#' -h 192.168.178.221 -V 5
(...)
{"value":"c0847dc9a93d"}
^C

## Empty user name againast Venus OS v3.31 - connects OK - PROBLEM SOLVED:

$ mosquitto_sub -d -t '#' -h 192.168.178.221 -u '' -P '' -V 5
(...)
{"value":"c0847dc9a93d"}
^C