diff --git a/util/mqtt.c b/util/mqtt.c index f59924db..2a4878e6 100644 --- a/util/mqtt.c +++ b/util/mqtt.c @@ -44,6 +44,8 @@ typedef struct } mqtt_t; static const char *global_server_uri = NULL; +static const char *global_username = NULL; +static const char *global_password = NULL; static mqtt_t *global_mqtt_client = NULL; static gboolean mqtt_initialized = FALSE; @@ -91,6 +93,46 @@ mqtt_get_global_server_uri () return global_server_uri; } +/** + * @brief Set the global mqtt username. + + * @param username to set. + */ +static void +mqtt_set_global_username (const char *username) +{ + global_username = username; +} + +/** + * @brief Get global username. + */ +static const char * +mqtt_get_global_username () +{ + return global_username; +} + +/** + * @brief Set the global mqtt password. + + * @param password to set. + */ +static void +mqtt_set_global_password (const char *password) +{ + global_password = password; +} + +/** + * @brief Get global password. + */ +static const char * +mqtt_get_global_password () +{ + return global_password; +} + /** * @brief * @@ -264,12 +306,16 @@ mqtt_set_client (mqtt_t *mqtt, MQTTClient client) /** * @brief Make new client and connect to mqtt broker. * - * @param mqtt Initialized mqtt_t + * @param mqtt Initialized mqtt_t + * @param server_uri Server URI + * @param username Username + * @param password Password * * @return 0 on success, <0 on error. */ static int -mqtt_connect (mqtt_t *mqtt, const char *server_uri) +mqtt_connect (mqtt_t *mqtt, const char *server_uri, const char *username, + const char *password) { int rc; MQTTClient client; @@ -288,6 +334,12 @@ mqtt_connect (mqtt_t *mqtt, const char *server_uri) conn_opts.cleanstart = 1; conn_opts.MQTTVersion = MQTTVERSION_5; + if (username != NULL && password != NULL) + { + conn_opts.username = username; + conn_opts.password = password; + } + resp = MQTTClient_connect5 (client, &conn_opts, &connect_properties, NULL); rc = resp.reasonCode; MQTTProperties_free (&connect_properties); @@ -309,17 +361,22 @@ mqtt_connect (mqtt_t *mqtt, const char *server_uri) * @brief Init MQTT communication * * @param server_uri Server URI + * @param username Username + * @param password Password * * @return 0 on success, <0 on error. */ int -mqtt_init (const char *server_uri) +mqtt_init (const char *server_uri, const char *username, const char *password) { - g_debug ("%s: start", __func__); mqtt_t *mqtt = NULL; + const char *g_server_uri; + const char *g_username; + const char *g_password; + + g_debug ("%s: start", __func__); mqtt = g_malloc0 (sizeof (mqtt_t)); - const char *g_server_uri; // Set random uuid as client id if (mqtt_set_client_id (mqtt) == NULL) { @@ -332,7 +389,16 @@ mqtt_init (const char *server_uri) g_server_uri = mqtt_get_global_server_uri (); if (g_server_uri == NULL) mqtt_set_global_server_uri (server_uri); - if (mqtt_connect (mqtt, server_uri)) + + g_username = mqtt_get_global_username (); + if (g_username == NULL) + mqtt_set_global_username (username); + + g_password = mqtt_get_global_password (); + if (g_password == NULL) + mqtt_set_global_password (password); + + if (mqtt_connect (mqtt, server_uri, username, password)) { g_warning ("%s: Unable to connect to MQTT broker.", __func__); g_free (mqtt); @@ -355,6 +421,8 @@ static void mqtt_reinit () { const char *server_uri; + const char *username; + const char *password; server_uri = mqtt_get_global_server_uri (); if (server_uri == NULL) @@ -363,7 +431,9 @@ mqtt_reinit () "else the server URI is not set. ", __func__); } - mqtt_init (server_uri); + username = mqtt_get_global_username (); + password = mqtt_get_global_password (); + mqtt_init (server_uri, username, password); } /** @@ -447,16 +517,21 @@ 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 username_in Username + * @param password_in Password * @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, +mqtt_publish_single_message (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; + const char *password = NULL; mqtt_t *mqtt = NULL; int ret = 0; @@ -477,6 +552,17 @@ mqtt_publish_single_message (const char *server_uri_in, const char *topic, server_uri = server_uri_in; } + if (username_in == NULL || passwd_in == NULL) + { + username = mqtt_get_global_username (); + password = mqtt_get_global_password (); + } + else + { + username = username_in; + password = passwd_in; + } + mqtt = g_malloc0 (sizeof (mqtt_t)); // Set random uuid as client id if (mqtt_set_client_id (mqtt) == NULL) @@ -485,7 +571,8 @@ mqtt_publish_single_message (const char *server_uri_in, const char *topic, g_free (mqtt); return -2; } - mqtt_connect (mqtt, server_uri); + + mqtt_connect (mqtt, server_uri, username, password); mqtt_client_publish (mqtt, topic, msg); mqtt_disconnect (mqtt); diff --git a/util/mqtt.h b/util/mqtt.h index 76dcb41d..4e4424e6 100644 --- a/util/mqtt.h +++ b/util/mqtt.h @@ -15,7 +15,7 @@ #include int -mqtt_init (const char *); +mqtt_init (const char *, const char *, const char *); gboolean mqtt_is_initialized (void); @@ -27,7 +27,8 @@ int mqtt_publish (const char *, const char *); int -mqtt_publish_single_message (const char *, const char *, const char *); +mqtt_publish_single_message (const char *, const char *, const char *, + const char *, const char *); int mqtt_subscribe (const char *);