From d1bd0b549ac51f6bc3098188b1bb9fb1b5258836 Mon Sep 17 00:00:00 2001 From: Jonathan Naylor Date: Wed, 26 Mar 2025 15:11:42 +0000 Subject: [PATCH] Add MQTT authentication. --- Conf.cpp | 24 ++++++++++++++++++++++++ Conf.h | 8 +++++++- DAPNETGateway.cpp | 8 ++------ DAPNETGateway.ini | 3 +++ MQTTConnection.cpp | 8 +++++++- MQTTConnection.h | 5 ++++- Version.h | 2 +- 7 files changed, 48 insertions(+), 10 deletions(-) diff --git a/Conf.cpp b/Conf.cpp index a164587..d087d49 100644 --- a/Conf.cpp +++ b/Conf.cpp @@ -51,6 +51,9 @@ m_mqttAddress("127.0.0.1"), m_mqttPort(1883U), m_mqttKeepalive(60U), m_mqttName("dapnet-gateway"), +m_mqttAuthEnabled(false), +m_mqttUsername(), +m_mqttPassword(), m_dapnetAddress(), m_dapnetPort(0U), m_dapnetAuthKey(), @@ -167,6 +170,12 @@ bool CConf::read() m_mqttKeepalive = (unsigned int)::atoi(value); else if (::strcmp(key, "Name") == 0) m_mqttName = value; + else if (::strcmp(key, "Auth") == 0) + m_mqttAuthEnabled = ::atoi(value) == 1; + else if (::strcmp(key, "Username") == 0) + m_mqttUsername = value; + else if (::strcmp(key, "Password") == 0) + m_mqttPassword = value; } else if (section == SECTION::DAPNET) { if (::strcmp(key, "Address") == 0) m_dapnetAddress = value; @@ -267,6 +276,21 @@ std::string CConf::getMQTTName() const return m_mqttName; } +bool CConf::getMQTTAuthEnabled() const +{ + return m_mqttAuthEnabled; +} + +std::string CConf::getMQTTUsername() const +{ + return m_mqttUsername; +} + +std::string CConf::getMQTTPassword() const +{ + return m_mqttPassword; +} + std::string CConf::getDAPNETAddress() const { return m_dapnetAddress; diff --git a/Conf.h b/Conf.h index c2cc26c..1f8ec6b 100644 --- a/Conf.h +++ b/Conf.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018,2023 by Jonathan Naylor G4KLX + * Copyright (C) 2018,2023,2025 by Jonathan Naylor G4KLX * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -51,6 +51,9 @@ public: unsigned short getMQTTPort() const; unsigned int getMQTTKeepalive() const; std::string getMQTTName() const; + bool getMQTTAuthEnabled() const; + std::string getMQTTUsername() const; + std::string getMQTTPassword() const; // The DAPNET section std::string getDAPNETAddress() const; @@ -80,6 +83,9 @@ private: unsigned short m_mqttPort; unsigned int m_mqttKeepalive; std::string m_mqttName; + bool m_mqttAuthEnabled; + std::string m_mqttUsername; + std::string m_mqttPassword; std::string m_dapnetAddress; unsigned short m_dapnetPort; diff --git a/DAPNETGateway.cpp b/DAPNETGateway.cpp index b0b5dea..6118d84 100644 --- a/DAPNETGateway.cpp +++ b/DAPNETGateway.cpp @@ -1,9 +1,5 @@ /* -<<<<<<< HEAD -* Copyright (C) 2018,2020,2023,2024 by Jonathan Naylor G4KLX -======= -* Copyright (C) 2018,2020,2024,2025 by Jonathan Naylor G4KLX ->>>>>>> master +* Copyright (C) 2018,2020,2023,2024,2025 by Jonathan Naylor G4KLX * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -258,7 +254,7 @@ int CDAPNETGateway::run() ::LogInitialise(m_conf.getLogDisplayLevel(), m_conf.getLogMQTTLevel()); std::vector> subscriptions; - m_mqtt = new CMQTTConnection(m_conf.getMQTTAddress(), m_conf.getMQTTPort(), m_conf.getMQTTName(), subscriptions, m_conf.getMQTTKeepalive()); + m_mqtt = new CMQTTConnection(m_conf.getMQTTAddress(), m_conf.getMQTTPort(), m_conf.getMQTTName(), m_conf.getMQTTAuthEnabled(), m_conf.getMQTTUsername(), m_conf.getMQTTPassword(), subscriptions, m_conf.getMQTTKeepalive()); ret = m_mqtt->open(); if (!ret) { delete m_mqtt; diff --git a/DAPNETGateway.ini b/DAPNETGateway.ini index cd3e282..6fccbbb 100644 --- a/DAPNETGateway.ini +++ b/DAPNETGateway.ini @@ -19,6 +19,9 @@ MQTTLevel=1 Address=127.0.0.1 Port=1883 Keepalive=60 +Auth=0 +Username=mmdvm +Password=mmdvm Name=dapnet-gateway [DAPNET] diff --git a/MQTTConnection.cpp b/MQTTConnection.cpp index 333fbbe..465b5c2 100644 --- a/MQTTConnection.cpp +++ b/MQTTConnection.cpp @@ -23,10 +23,13 @@ #include #include -CMQTTConnection::CMQTTConnection(const std::string& host, unsigned short port, const std::string& name, const std::vector>& subs, unsigned int keepalive, MQTT_QOS qos) : +CMQTTConnection::CMQTTConnection(const std::string& host, unsigned short port, const std::string& name, const bool authEnabled, const std::string& username, const std::string& password, const std::vector>& subs, unsigned int keepalive, MQTT_QOS qos) : m_host(host), m_port(port), m_name(name), +m_authEnabled(authEnabled), +m_username(username), +m_password(password), m_subs(subs), m_keepalive(keepalive), m_qos(qos), @@ -59,6 +62,9 @@ bool CMQTTConnection::open() return false; } + if (m_authEnabled) + ::mosquitto_username_pw_set(m_mosq, m_username.c_str(), m_password.c_str()); + ::mosquitto_connect_callback_set(m_mosq, onConnect); ::mosquitto_subscribe_callback_set(m_mosq, onSubscribe); ::mosquitto_message_callback_set(m_mosq, onMessage); diff --git a/MQTTConnection.h b/MQTTConnection.h index 3d9ccfb..2c695fe 100644 --- a/MQTTConnection.h +++ b/MQTTConnection.h @@ -32,7 +32,7 @@ enum class MQTT_QOS : int { class CMQTTConnection { public: - CMQTTConnection(const std::string& host, unsigned short port, const std::string& name, const std::vector>& subs, unsigned int keepalive, MQTT_QOS qos = MQTT_QOS::EXACTLY_ONCE); + CMQTTConnection(const std::string& host, unsigned short port, const std::string& name, const bool authEnabled, const std::string& username, const std::string& password, const std::vector>& subs, unsigned int keepalive, MQTT_QOS qos = MQTT_QOS::EXACTLY_ONCE); ~CMQTTConnection(); bool open(); @@ -47,6 +47,9 @@ private: std::string m_host; unsigned short m_port; std::string m_name; + bool m_authEnabled; + std::string m_username; + std::string m_password; std::vector> m_subs; unsigned int m_keepalive; MQTT_QOS m_qos; diff --git a/Version.h b/Version.h index 3752c6e..d055aad 100644 --- a/Version.h +++ b/Version.h @@ -19,6 +19,6 @@ #if !defined(VERSION_H) #define VERSION_H -const char* VERSION = "20250318"; +const char* VERSION = "20250326"; #endif