From 0087431bf42ac29715dbeec8fceaddd783f47e71 Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Thu, 14 Jan 2021 15:36:29 +0000 Subject: [PATCH] Add MOSQ_EVT_CONNECT event. This allows plugins to know when a client has successfully authenticated to the broker. Add connection-state example plugin to demonstrate MOSQ_EVT_CONNECT. --- ChangeLog.txt | 5 + include/mosquitto_broker.h | 34 +++++ plugins/Makefile | 1 + plugins/README.md | 6 + plugins/connection-state/CMakeLists.txt | 11 ++ plugins/connection-state/Makefile | 27 ++++ .../mosquitto_connection_state.c | 117 ++++++++++++++++++ src/handle_connect.c | 4 + src/mosquitto_broker_internal.h | 2 + src/plugin.c | 28 ++++- 10 files changed, 234 insertions(+), 1 deletion(-) create mode 100644 plugins/connection-state/CMakeLists.txt create mode 100644 plugins/connection-state/Makefile create mode 100644 plugins/connection-state/mosquitto_connection_state.c diff --git a/ChangeLog.txt b/ChangeLog.txt index bf67d2fe..5cd2adc8 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -1,6 +1,11 @@ 2.1.0 - 2021-xx-xx ================== +Broker: +- Add MOSQ_EVT_CONNECT, to allow plugins to know when a client has + successfully authenticated to the broker. +- Add connection-state example plugin to demonstrate MOSQ_EVT_CONNECT. + Client library: - Add MOSQ_OPT_DISABLE_SOCKETPAIR to allow the disabling of the socketpair feature that allows the network thread to be woken from select() by another diff --git a/include/mosquitto_broker.h b/include/mosquitto_broker.h index 9768975f..f8c473e6 100644 --- a/include/mosquitto_broker.h +++ b/include/mosquitto_broker.h @@ -65,6 +65,7 @@ enum mosquitto_plugin_event { MOSQ_EVT_PSK_KEY = 8, MOSQ_EVT_TICK = 9, MOSQ_EVT_DISCONNECT = 10, + MOSQ_EVT_CONNECT = 11, }; /* Data for the MOSQ_EVT_RELOAD event */ @@ -162,6 +163,13 @@ struct mosquitto_evt_tick { void *future2[4]; }; +/* Data for the MOSQ_EVT_CONNECT event */ +struct mosquitto_evt_connect { + void *future; + struct mosquitto *client; + void *future2[4]; +}; + /* Data for the MOSQ_EVT_DISCONNECT event */ struct mosquitto_evt_disconnect { void *future; @@ -185,15 +193,40 @@ typedef struct mosquitto_plugin_id_t mosquitto_plugin_id_t; * identifier - the plugin identifier, as provided by . * event - the event to register a callback for. Can be one of: * * MOSQ_EVT_RELOAD + * Called when the broker is sent a signal indicating it should + * reload its configuration. * * MOSQ_EVT_ACL_CHECK + * Called when a publish/subscribe/unsubscribe command is received + * and the broker wants to check when the client is allowed to carry + * out this command. * * MOSQ_EVT_BASIC_AUTH + * Called when a client connects to the broker, to allow the + * username/password/clientid to be authenticated. * * MOSQ_EVT_EXT_AUTH_START + * Called when an MQTT v5 client connects, if it is using extended + * authentication. * * MOSQ_EVT_EXT_AUTH_CONTINUE + * Called when an MQTT v5 client connects, if it is using extended + * authentication. * * MOSQ_EVT_CONTROL + * Called on receipt of a $CONTROL message that the plugin has + * registered for. * * MOSQ_EVT_MESSAGE + * Called for each PUBLISH message after it has been received and + * authorised, but before it is sent to subscribing clients. The + * contents of the message can be modified. * * MOSQ_EVT_PSK_KEY + * Called when a client connects with TLS-PSK and the broker needs + * the PSK information. * * MOSQ_EVT_TICK + * Called periodically in the event loop. At the moment this + * occurs at a regular frequency, but this should not be relied + * upon. * * MOSQ_EVT_DISCONNECT + * Called when a client disconnects from the broker. + * * MOSQ_EVT_CONNECT + * Called when a client has successfully connected to the broker, + * i.e. has been authenticated. * cb_func - the callback function * event_data - event specific data * @@ -229,6 +262,7 @@ mosq_EXPORT int mosquitto_callback_register( * * MOSQ_EVT_PSK_KEY * * MOSQ_EVT_TICK * * MOSQ_EVT_DISCONNECT + * * MOSQ_EVT_CONNECT * cb_func - the callback function * event_data - event specific data * diff --git a/plugins/Makefile b/plugins/Makefile index f039b2c4..a84288f1 100644 --- a/plugins/Makefile +++ b/plugins/Makefile @@ -1,5 +1,6 @@ DIRS= \ auth-by-ip \ + connection-state \ dynamic-security \ message-timestamp \ payload-modification diff --git a/plugins/README.md b/plugins/README.md index e407d40e..5636737f 100644 --- a/plugins/README.md +++ b/plugins/README.md @@ -7,6 +7,12 @@ This is a fully functioning plugin that implements authentication and access control, with configuration via a $CONTROL topic. See the readme in dynamic-security for more information. +## Connection state +This is an **example** plugin to demonstrate the use of the connect and +disconnect events. It publishes messages to +$SYS/broker/connection/client//state for every client that connects +to the broker, to indicate the connection state of that client. + ## Message timestamp This is an **example** plugin to demonstrate how it is possible to attach MQTT v5 properties to messages after they have been received, and before they are sent on to subscribers. diff --git a/plugins/connection-state/CMakeLists.txt b/plugins/connection-state/CMakeLists.txt new file mode 100644 index 00000000..32b7fb7f --- /dev/null +++ b/plugins/connection-state/CMakeLists.txt @@ -0,0 +1,11 @@ +include_directories(${mosquitto_SOURCE_DIR} ${mosquitto_SOURCE_DIR}/include + ${STDBOOL_H_PATH} ${STDINT_H_PATH}) + +add_library(mosquitto_connection_state SHARED mosquitto_connection_state.c) +set_target_properties(mosquitto_connection_state PROPERTIES + POSITION_INDEPENDENT_CODE 1 +) +set_target_properties(mosquitto_connection_state PROPERTIES PREFIX "") + +# Don't install, these are example plugins only. +#install(TARGETS mosquitto_connection_state RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}") diff --git a/plugins/connection-state/Makefile b/plugins/connection-state/Makefile new file mode 100644 index 00000000..8397e9f1 --- /dev/null +++ b/plugins/connection-state/Makefile @@ -0,0 +1,27 @@ +include ../../config.mk + +.PHONY : all binary check clean reallyclean test install uninstall + +PLUGIN_NAME=mosquitto_connection_state + +all : binary + +binary : ${PLUGIN_NAME}.so + +${PLUGIN_NAME}.so : ${PLUGIN_NAME}.c + $(CROSS_COMPILE)$(CC) $(PLUGIN_CPPFLAGS) $(PLUGIN_CFLAGS) $(PLUGIN_LDFLAGS) -fPIC -shared $< -o $@ + +reallyclean : clean +clean: + -rm -f *.o ${PLUGIN_NAME}.so *.gcda *.gcno + +check: test +test: + +install: ${PLUGIN_NAME}.so + # Don't install, these are examples only. + #$(INSTALL) -d "${DESTDIR}$(libdir)" + #$(INSTALL) ${STRIP_OPTS} ${PLUGIN_NAME}.so "${DESTDIR}${libdir}/${PLUGIN_NAME}.so" + +uninstall : + -rm -f "${DESTDIR}${libdir}/${PLUGIN_NAME}.so" diff --git a/plugins/connection-state/mosquitto_connection_state.c b/plugins/connection-state/mosquitto_connection_state.c new file mode 100644 index 00000000..7b2809b5 --- /dev/null +++ b/plugins/connection-state/mosquitto_connection_state.c @@ -0,0 +1,117 @@ +/* +Copyright (c) 2021 Roger Light + +All rights reserved. This program and the accompanying materials +are made available under the terms of the Eclipse Public License 2.0 +and Eclipse Distribution License v1.0 which accompany this distribution. + +The Eclipse Public License is available at + https://www.eclipse.org/legal/epl-2.0/ +and the Eclipse Distribution License is available at + http://www.eclipse.org/org/documents/edl-v10.php. + +SPDX-License-Identifier: EPL-2.0 OR EDL-1.0 + +Contributors: + Roger Light - initial implementation and documentation. +*/ + +/* + * This is an example plugin showing how you could publish online/offline + * state for all clients. + * + * Compile with: + * gcc -I -fPIC -shared mosquitto_connection_state.c -o mosquitto_connection_state.so + * + * Use in config with: + * + * plugin /path/to/mosquitto_connection_state.so + * + * Note that this only works on Mosquitto 2.0 or later. + */ + + +#include +#include +#include + +#include "mosquitto_broker.h" +#include "mosquitto_plugin.h" +#include "mosquitto.h" +#include "mqtt_protocol.h" + +static mosquitto_plugin_id_t *mosq_pid = NULL; + +static int connect_callback(int event, void *event_data, void *userdata) +{ + struct mosquitto_evt_connect *ed = event_data; + const char *client_id; + char topic[1024]; + int len; + + client_id = mosquitto_client_id(ed->client); + len = snprintf(topic, sizeof(topic), "$SYS/broker/connection/client/%s/state", client_id); + if(len < sizeof(topic)){ + mosquitto_broker_publish_copy(NULL, topic, 1, "1", 0, true, NULL); + }else{ + /* client id too large */ + } + + return MOSQ_ERR_SUCCESS; +} + +static int disconnect_callback(int event, void *event_data, void *userdata) +{ + struct mosquitto_evt_disconnect *ed = event_data; + const char *client_id; + char topic[1024]; + int len; + mosquitto_property *proplist = NULL; + int rc; + + client_id = mosquitto_client_id(ed->client); + len = snprintf(topic, sizeof(topic), "$SYS/broker/connection/client/%s/state", client_id); + if(len < sizeof(topic)){ + /* Expire our "disconnected" message after a day. */ + mosquitto_property_add_int32(&proplist, MQTT_PROP_MESSAGE_EXPIRY_INTERVAL, 86400); + rc = mosquitto_broker_publish_copy(NULL, topic, 1, "0", 0, true, proplist); + if(rc){ + mosquitto_property_free_all(&proplist); + } + }else{ + /* client id too large */ + } + + return MOSQ_ERR_SUCCESS; +} + + +int mosquitto_plugin_version(int supported_version_count, const int *supported_versions) +{ + int i; + + for(i=0; iper_listener_settings){ + if(context->listener == NULL){ + return; + } + opts = &context->listener->security_options; + }else{ + opts = &db.config->security_options; + } + memset(&event_data, 0, sizeof(event_data)); + + event_data.client = context; + DL_FOREACH(opts->plugin_callbacks.connect, cb_base){ + cb_base->cb(MOSQ_EVT_CONNECT, &event_data, cb_base->userdata); + } +} + + void plugin__handle_disconnect(struct mosquitto *context, int reason) { struct mosquitto_evt_disconnect event_data; @@ -116,8 +139,8 @@ void plugin__handle_disconnect(struct mosquitto *context, int reason) opts = &context->listener->security_options; }else{ opts = &db.config->security_options; - memset(&event_data, 0, sizeof(event_data)); } + memset(&event_data, 0, sizeof(event_data)); event_data.client = context; event_data.reason = reason; @@ -244,6 +267,9 @@ int mosquitto_callback_register( case MOSQ_EVT_DISCONNECT: cb_base = &security_options->plugin_callbacks.disconnect; break; + case MOSQ_EVT_CONNECT: + cb_base = &security_options->plugin_callbacks.connect; + break; default: return MOSQ_ERR_NOT_SUPPORTED; break;