From 3752550f4c061ee89ce8994a3c48f4b5f1405a76 Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Wed, 20 Dec 2023 08:26:06 +0100 Subject: [PATCH] Change: make authenticated connection to MQTT broker backward comaptible --- util/mqtt.c | 45 ++++++++++++++++++++++++++++++++++++++++----- util/mqtt.h | 14 +++++++++++--- 2 files changed, 51 insertions(+), 8 deletions(-) diff --git a/util/mqtt.c b/util/mqtt.c index 2a4878e6..a0ffafc8 100644 --- a/util/mqtt.c +++ b/util/mqtt.c @@ -357,6 +357,18 @@ mqtt_connect (mqtt_t *mqtt, const char *server_uri, const char *username, return 0; } +/** + * @brief Init MQTT communication + * + * @param server_uri Server URI + * + * @return 0 on success, <0 on error. + */ +int +mqtt_init (const char *server_uri) +{ + return mqtt_init_auth (server_uri, NULL, NULL); +} /** * @brief Init MQTT communication * @@ -367,7 +379,8 @@ mqtt_connect (mqtt_t *mqtt, const char *server_uri, const char *username, * @return 0 on success, <0 on error. */ int -mqtt_init (const char *server_uri, const char *username, const char *password) +mqtt_init_auth (const char *server_uri, const char *username, + const char *password) { mqtt_t *mqtt = NULL; const char *g_server_uri; @@ -433,7 +446,7 @@ mqtt_reinit () } username = mqtt_get_global_username (); password = mqtt_get_global_password (); - mqtt_init (server_uri, username, password); + mqtt_init_auth (server_uri, username, password); } /** @@ -517,6 +530,27 @@ mqtt_publish (const char *topic, const char *msg) * meant for error messages and the likes emitted by openvas. * * @param server_uri_in Server URI + * @param topic Topic to publish to + * @param msg Message to publish + * + * @return 0 on success, <0 on failure. + */ +int +mqtt_publish_single_message (const char *server_uri_in, const char *topic, + const char *msg) +{ + return mqtt_publish_single_message_auth (server_uri_in, NULL, NULL, topic, + msg); +} +/** + * @brief Send a single message with credentials + * + * This functions creates a mqtt handle, connects, sends the message, closes + * the connection and destroys the handler. + * This function should not be chosen for repeated and frequent messaging. Its + * meant for error messages and the likes emitted by openvas. + * + * @param server_uri_in Server URI * @param username_in Username * @param password_in Password * @param topic Topic to publish to @@ -525,9 +559,10 @@ mqtt_publish (const char *topic, const char *msg) * @return 0 on success, <0 on failure. */ int -mqtt_publish_single_message (const char *server_uri_in, const char *username_in, - const char *passwd_in, const char *topic, - const char *msg) +mqtt_publish_single_message_auth (const char *server_uri_in, + const char *username_in, + const char *passwd_in, const char *topic, + const char *msg) { const char *server_uri; const char *username = NULL; diff --git a/util/mqtt.h b/util/mqtt.h index 4e4424e6..a4c248e1 100644 --- a/util/mqtt.h +++ b/util/mqtt.h @@ -14,8 +14,13 @@ #include #include +#define AUTH_MQTT 1 + +int +mqtt_init (const char *); + int -mqtt_init (const char *, const char *, const char *); +mqtt_init_auth (const char *, const char *, const char *); gboolean mqtt_is_initialized (void); @@ -27,8 +32,11 @@ int mqtt_publish (const char *, const char *); int -mqtt_publish_single_message (const char *, const char *, const char *, - const char *, const char *); +mqtt_publish_single_message_auth (const char *, const char *, const char *, + const char *, const char *); + +int +mqtt_publish_single_message (const char *, const char *, const char *); int mqtt_subscribe (const char *);