Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change: make authenticated connection to MQTT broker backward comaptible #804

Merged
merged 1 commit into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 40 additions & 5 deletions util/mqtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand Down
14 changes: 11 additions & 3 deletions util/mqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@
#include <MQTTClient.h>
#include <glib.h>

#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);
Expand All @@ -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 *);
Expand Down