From 7e6e5172080dddb5c0d48e808ea6b13369650924 Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Wed, 10 Nov 2021 12:13:19 +0000 Subject: [PATCH] Move extended auth to own file. --- src/CMakeLists.txt | 2 +- src/Makefile | 4 + src/plugin_extended_auth.c | 167 +++++++++++++++++++++++++++++++++++++ src/security.c | 139 ------------------------------ 4 files changed, 172 insertions(+), 140 deletions(-) create mode 100644 src/plugin_extended_auth.c diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 9be042a8..2772aeed 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -44,7 +44,7 @@ set (MOSQ_SRCS persist_write_v5.c persist_write.c persist.h plugin.c plugin_v4.c plugin_v3.c plugin_v2.c - plugin_connect.c plugin_disconnect.c plugin_message.c plugin_public.c plugin_tick.c + plugin_connect.c plugin_disconnect.c plugin_extended_auth.c plugin_message.c plugin_public.c plugin_tick.c property_broker.c ../lib/property_mosq.c ../lib/property_mosq.h read_handle.c diff --git a/src/Makefile b/src/Makefile index 45817d42..a666fac8 100644 --- a/src/Makefile +++ b/src/Makefile @@ -61,6 +61,7 @@ OBJS= mosquitto.o \ plugin_v4.o \ plugin_connect.o \ plugin_disconnect.o \ + plugin_extended_auth.o \ plugin_message.o \ plugin_public.o \ plugin_tick.o \ @@ -276,6 +277,9 @@ plugin_connect.o : plugin_connect.c ../include/mosquitto_plugin.h mosquitto_brok plugin_disconnect.o : plugin_disconnect.c ../include/mosquitto_plugin.h mosquitto_broker_internal.h ${CROSS_COMPILE}${CC} $(BROKER_CPPFLAGS) $(BROKER_CFLAGS) -c $< -o $@ +plugin_extended_auth.o : plugin_extended_auth.c ../include/mosquitto_plugin.h mosquitto_broker_internal.h + ${CROSS_COMPILE}${CC} $(BROKER_CPPFLAGS) $(BROKER_CFLAGS) -c $< -o $@ + plugin_message.o : plugin_message.c ../include/mosquitto_plugin.h mosquitto_broker_internal.h ${CROSS_COMPILE}${CC} $(BROKER_CPPFLAGS) $(BROKER_CFLAGS) -c $< -o $@ diff --git a/src/plugin_extended_auth.c b/src/plugin_extended_auth.c new file mode 100644 index 00000000..4de7f4bc --- /dev/null +++ b/src/plugin_extended_auth.c @@ -0,0 +1,167 @@ +/* +Copyright (c) 2011-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 BSD-3-Clause + +Contributors: + Roger Light - initial implementation and documentation. +*/ + +#include "config.h" + +#include +#include + +#include "mosquitto_broker.h" +#include "mosquitto_broker_internal.h" +#include "mosquitto_plugin.h" +#include "memory_mosq.h" +#include "lib_load.h" +#include "utlist.h" + +static int plugin__ext_auth_start(struct mosquitto__security_options *opts, struct mosquitto *context, bool reauth, const void *data_in, uint16_t data_in_len, void **data_out, uint16_t *data_out_len) +{ + struct mosquitto_evt_extended_auth event_data; + struct mosquitto__callback *cb_base; + int rc; + int rc_final = MOSQ_ERR_PLUGIN_DEFER; + + UNUSED(reauth); + + DL_FOREACH(opts->plugin_callbacks.ext_auth_start, cb_base){ + memset(&event_data, 0, sizeof(event_data)); + event_data.client = context; + event_data.auth_method = context->auth_method; + event_data.data_in = data_in; + event_data.data_out = NULL; + event_data.data_in_len = data_in_len; + event_data.data_out_len = 0; + rc = cb_base->cb(MOSQ_EVT_EXT_AUTH_START, &event_data, cb_base->userdata); + if(rc == MOSQ_ERR_PLUGIN_IGNORE){ + /* Do nothing */ + }else if(rc == MOSQ_ERR_PLUGIN_DEFER){ + rc_final = MOSQ_ERR_PLUGIN_DEFER; + }else{ + *data_out = event_data.data_out; + *data_out_len = event_data.data_out_len; + return rc; + } + } + return rc_final; +} + + +int mosquitto_security_auth_start(struct mosquitto *context, bool reauth, const void *data_in, uint16_t data_in_len, void **data_out, uint16_t *data_out_len) +{ + int rc; + + if(!context || !context->listener || !context->auth_method) return MOSQ_ERR_INVAL; + if(!data_out || !data_out_len) return MOSQ_ERR_INVAL; + + /* Global plugins */ + if(db.config->security_options.plugin_callbacks.ext_auth_start){ + rc = plugin__ext_auth_start(&db.config->security_options, context, + reauth, data_in, data_in_len, data_out, data_out_len); + + if(rc == MOSQ_ERR_PLUGIN_IGNORE || rc == MOSQ_ERR_PLUGIN_DEFER){ + /* Do nothing */ + }else{ + return rc; + } + } + + /* Per listener plugins */ + if(db.config->per_listener_settings){ + if(context->listener == NULL){ + return MOSQ_ERR_AUTH; + } + if(context->listener->security_options.plugin_callbacks.ext_auth_start){ + rc = plugin__ext_auth_start(&context->listener->security_options, context, + reauth, data_in, data_in_len, data_out, data_out_len); + + if(rc == MOSQ_ERR_PLUGIN_IGNORE || rc == MOSQ_ERR_PLUGIN_DEFER){ + /* Do nothing */ + }else{ + return rc; + } + } + } + + return MOSQ_ERR_NOT_SUPPORTED; +} + + +static int plugin__ext_auth_continue(struct mosquitto__security_options *opts, struct mosquitto *context, const void *data_in, uint16_t data_in_len, void **data_out, uint16_t *data_out_len) +{ + int rc; + struct mosquitto_evt_extended_auth event_data; + struct mosquitto__callback *cb_base; + + DL_FOREACH(opts->plugin_callbacks.ext_auth_continue, cb_base){ + memset(&event_data, 0, sizeof(event_data)); + event_data.client = context; + event_data.data_in = data_in; + event_data.data_out = NULL; + event_data.data_in_len = data_in_len; + event_data.data_out_len = 0; + rc = cb_base->cb(MOSQ_EVT_EXT_AUTH_CONTINUE, &event_data, cb_base->userdata); + if(rc == MOSQ_ERR_PLUGIN_IGNORE || rc == MOSQ_ERR_PLUGIN_DEFER){ + /* Do nothing */ + }else{ + *data_out = event_data.data_out; + *data_out_len = event_data.data_out_len; + return rc; + } + } + + return MOSQ_ERR_PLUGIN_DEFER; +} + + +int mosquitto_security_auth_continue(struct mosquitto *context, const void *data_in, uint16_t data_in_len, void **data_out, uint16_t *data_out_len) +{ + int rc; + + if(!context || !context->listener || !context->auth_method) return MOSQ_ERR_INVAL; + if(!data_out || !data_out_len) return MOSQ_ERR_INVAL; + + /* Global plugins */ + if(db.config->security_options.plugin_callbacks.ext_auth_continue){ + rc = plugin__ext_auth_continue(&db.config->security_options, context, + data_in, data_in_len, data_out, data_out_len); + + if(rc == MOSQ_ERR_PLUGIN_IGNORE || rc == MOSQ_ERR_PLUGIN_DEFER){ + /* Do nothing */ + }else{ + return rc; + } + } + + /* Per listener plugins */ + if(db.config->per_listener_settings){ + if(context->listener == NULL){ + return MOSQ_ERR_AUTH; + } + if(context->listener->security_options.plugin_callbacks.ext_auth_continue){ + rc = plugin__ext_auth_continue(&context->listener->security_options, context, + data_in, data_in_len, data_out, data_out_len); + + if(rc == MOSQ_ERR_PLUGIN_IGNORE || rc == MOSQ_ERR_PLUGIN_DEFER){ + /* Do nothing */ + }else{ + return rc; + } + } + } + + return MOSQ_ERR_NOT_SUPPORTED; +} diff --git a/src/security.c b/src/security.c index 79324ae2..296efd1a 100644 --- a/src/security.c +++ b/src/security.c @@ -695,142 +695,3 @@ int mosquitto_psk_key_get(struct mosquitto *context, const char *hint, const cha } return rc_final; } - - -static int plugin__ext_auth_start(struct mosquitto__security_options *opts, struct mosquitto *context, bool reauth, const void *data_in, uint16_t data_in_len, void **data_out, uint16_t *data_out_len) -{ - struct mosquitto_evt_extended_auth event_data; - struct mosquitto__callback *cb_base; - int rc; - int rc_final = MOSQ_ERR_PLUGIN_DEFER; - - UNUSED(reauth); - - DL_FOREACH(opts->plugin_callbacks.ext_auth_start, cb_base){ - memset(&event_data, 0, sizeof(event_data)); - event_data.client = context; - event_data.auth_method = context->auth_method; - event_data.data_in = data_in; - event_data.data_out = NULL; - event_data.data_in_len = data_in_len; - event_data.data_out_len = 0; - rc = cb_base->cb(MOSQ_EVT_EXT_AUTH_START, &event_data, cb_base->userdata); - if(rc == MOSQ_ERR_PLUGIN_IGNORE){ - /* Do nothing */ - }else if(rc == MOSQ_ERR_PLUGIN_DEFER){ - rc_final = MOSQ_ERR_PLUGIN_DEFER; - }else{ - *data_out = event_data.data_out; - *data_out_len = event_data.data_out_len; - return rc; - } - } - return rc_final; -} - - -int mosquitto_security_auth_start(struct mosquitto *context, bool reauth, const void *data_in, uint16_t data_in_len, void **data_out, uint16_t *data_out_len) -{ - int rc; - - if(!context || !context->listener || !context->auth_method) return MOSQ_ERR_INVAL; - if(!data_out || !data_out_len) return MOSQ_ERR_INVAL; - - /* Global plugins */ - if(db.config->security_options.plugin_callbacks.ext_auth_start){ - rc = plugin__ext_auth_start(&db.config->security_options, context, - reauth, data_in, data_in_len, data_out, data_out_len); - - if(rc == MOSQ_ERR_PLUGIN_IGNORE || rc == MOSQ_ERR_PLUGIN_DEFER){ - /* Do nothing */ - }else{ - return rc; - } - } - - /* Per listener plugins */ - if(db.config->per_listener_settings){ - if(context->listener == NULL){ - return MOSQ_ERR_AUTH; - } - if(context->listener->security_options.plugin_callbacks.ext_auth_start){ - rc = plugin__ext_auth_start(&context->listener->security_options, context, - reauth, data_in, data_in_len, data_out, data_out_len); - - if(rc == MOSQ_ERR_PLUGIN_IGNORE || rc == MOSQ_ERR_PLUGIN_DEFER){ - /* Do nothing */ - }else{ - return rc; - } - } - } - - return MOSQ_ERR_NOT_SUPPORTED; -} - - -static int plugin__ext_auth_continue(struct mosquitto__security_options *opts, struct mosquitto *context, const void *data_in, uint16_t data_in_len, void **data_out, uint16_t *data_out_len) -{ - int rc; - struct mosquitto_evt_extended_auth event_data; - struct mosquitto__callback *cb_base; - - DL_FOREACH(opts->plugin_callbacks.ext_auth_continue, cb_base){ - memset(&event_data, 0, sizeof(event_data)); - event_data.client = context; - event_data.data_in = data_in; - event_data.data_out = NULL; - event_data.data_in_len = data_in_len; - event_data.data_out_len = 0; - rc = cb_base->cb(MOSQ_EVT_EXT_AUTH_CONTINUE, &event_data, cb_base->userdata); - if(rc == MOSQ_ERR_PLUGIN_IGNORE || rc == MOSQ_ERR_PLUGIN_DEFER){ - /* Do nothing */ - }else{ - *data_out = event_data.data_out; - *data_out_len = event_data.data_out_len; - return rc; - } - } - - return MOSQ_ERR_PLUGIN_DEFER; -} - - -int mosquitto_security_auth_continue(struct mosquitto *context, const void *data_in, uint16_t data_in_len, void **data_out, uint16_t *data_out_len) -{ - int rc; - - if(!context || !context->listener || !context->auth_method) return MOSQ_ERR_INVAL; - if(!data_out || !data_out_len) return MOSQ_ERR_INVAL; - - /* Global plugins */ - if(db.config->security_options.plugin_callbacks.ext_auth_continue){ - rc = plugin__ext_auth_continue(&db.config->security_options, context, - data_in, data_in_len, data_out, data_out_len); - - if(rc == MOSQ_ERR_PLUGIN_IGNORE || rc == MOSQ_ERR_PLUGIN_DEFER){ - /* Do nothing */ - }else{ - return rc; - } - } - - /* Per listener plugins */ - if(db.config->per_listener_settings){ - if(context->listener == NULL){ - return MOSQ_ERR_AUTH; - } - if(context->listener->security_options.plugin_callbacks.ext_auth_continue){ - rc = plugin__ext_auth_continue(&context->listener->security_options, context, - data_in, data_in_len, data_out, data_out_len); - - if(rc == MOSQ_ERR_PLUGIN_IGNORE || rc == MOSQ_ERR_PLUGIN_DEFER){ - /* Do nothing */ - }else{ - return rc; - } - } - } - - return MOSQ_ERR_NOT_SUPPORTED; -}