@@ -20,6 +20,7 @@ SPDX-License-Identifier: EPL-2.0 OR EDL-1.0
2020 * This is an example plugin showing how to carry out delayed authentication.
2121 * The "authentication" in this example makes no checks whatsoever, but delays
2222 * the response by 5 seconds, and randomly chooses whether it should succeed.
23+ * The example supports basic and extended authentication.
2324 *
2425 * Compile with:
2526 * gcc -I<path to mosquitto-repo/include> -fPIC -shared mosquitto_delayed_auth.c -o mosquitto_delayed_auth.so
@@ -33,6 +34,7 @@ SPDX-License-Identifier: EPL-2.0 OR EDL-1.0
3334
3435
3536#include <limits.h>
37+ #include <mosquitto/broker.h>
3638#include <stdbool.h>
3739#include <stdio.h>
3840#include <string.h>
@@ -54,6 +56,7 @@ struct client_list {
5456 UT_hash_handle hh ;
5557 char * id ;
5658 time_t request_time ;
59+ bool basic_auth ;
5760};
5861
5962static mosquitto_plugin_id_t * mosq_pid = NULL ;
@@ -71,16 +74,9 @@ static bool authentication_check(struct client_list *client, time_t now)
7174}
7275
7376
74- static int basic_auth_callback (int event , void * event_data , void * userdata )
77+ static int start_auth (int event , const char * id )
7578{
76- struct mosquitto_evt_basic_auth * ed = event_data ;
7779 static struct client_list * client ;
78- const char * id ;
79-
80- UNUSED (event );
81- UNUSED (userdata );
82-
83- id = mosquitto_client_id (ed -> client );
8480
8581 HASH_FIND (hh , clients , id , strlen (id ), client );
8682 if (client ){
@@ -97,12 +93,47 @@ static int basic_auth_callback(int event, void *event_data, void *userdata)
9793 return MOSQ_ERR_NOMEM ;
9894 }
9995 client -> request_time = time (NULL );
100- HASH_ADD_KEYPTR (hh , clients , client -> id , strlen (client -> id ), client );
96+ if (event == MOSQ_EVT_BASIC_AUTH ){
97+ mosquitto_log_printf (MOSQ_LOG_DEBUG , "Starting basic auth for %s at %ld" , client -> id , time (NULL ));
98+ client -> basic_auth = true;
99+ }else {
100+ mosquitto_log_printf (MOSQ_LOG_DEBUG , "Starting extended auth for %s at %ld" , client -> id , time (NULL ));
101+ client -> basic_auth = false;
102+ }
101103
102- mosquitto_log_printf ( MOSQ_LOG_DEBUG , "Starting auth for %s at %ld" , client -> id , time ( NULL ) );
104+ HASH_ADD_KEYPTR ( hh , clients , client -> id , strlen ( client -> id ), client );
103105 }
104106
105107 return MOSQ_ERR_AUTH_DELAYED ;
108+
109+ }
110+
111+
112+ static int basic_auth_callback (int event , void * event_data , void * userdata )
113+ {
114+ struct mosquitto_evt_basic_auth * ed = event_data ;
115+ const char * id ;
116+
117+ UNUSED (event );
118+ UNUSED (userdata );
119+
120+ id = mosquitto_client_id (ed -> client );
121+
122+ return start_auth (event , id );
123+ }
124+
125+
126+ static int extended_auth_callback (int event , void * event_data , void * userdata )
127+ {
128+ struct mosquitto_evt_extended_auth * ed = event_data ;
129+ const char * id ;
130+
131+ UNUSED (event );
132+ UNUSED (userdata );
133+
134+ id = mosquitto_client_id (ed -> client );
135+
136+ return start_auth (event , id );
106137}
107138
108139
@@ -128,9 +159,17 @@ static int tick_callback(int event, void *event_data, void *userdata)
128159 r = random () % 1000 ;
129160#endif
130161 if (r > 740 ){
131- mosquitto_complete_basic_auth (client -> id , MOSQ_ERR_AUTH );
162+ if (client -> basic_auth ){
163+ mosquitto_complete_basic_auth (client -> id , MOSQ_ERR_AUTH );
164+ }else {
165+ mosquitto_complete_extended_auth (client -> id , MOSQ_ERR_AUTH , NULL , 0 );
166+ }
132167 }else {
133- mosquitto_complete_basic_auth (client -> id , MOSQ_ERR_SUCCESS );
168+ if (client -> basic_auth ){
169+ mosquitto_complete_basic_auth (client -> id , MOSQ_ERR_SUCCESS );
170+ }else {
171+ mosquitto_complete_extended_auth (client -> id , MOSQ_ERR_SUCCESS , NULL , 0 );
172+ }
134173 }
135174 mosquitto_log_printf (MOSQ_LOG_DEBUG , "Completing auth for %s at %ld" , client -> id , now );
136175 HASH_DELETE (hh , clients , client );
@@ -162,6 +201,10 @@ int mosquitto_plugin_init(mosquitto_plugin_id_t *identifier, void **user_data, s
162201 if (rc ){
163202 return rc ;
164203 }
204+ rc = mosquitto_callback_register (mosq_pid , MOSQ_EVT_EXT_AUTH_START , extended_auth_callback , NULL , NULL );
205+ if (rc ){
206+ return rc ;
207+ }
165208 rc = mosquitto_callback_register (mosq_pid , MOSQ_EVT_TICK , tick_callback , NULL , NULL );
166209 return rc ;
167210}
0 commit comments