From be8cf754fbdaecb637e037560b28efd63d1d6b05 Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Fri, 16 Dec 2022 16:37:35 +0000 Subject: [PATCH] Move plugin to examples dir, plus some tweaks. --- plugins/CMakeLists.txt | 1 - plugins/auth-by-env/CMakeLists.txt | 11 ------- plugins/examples/CMakeLists.txt | 1 + plugins/examples/Makefile | 1 + plugins/examples/auth-by-env/CMakeLists.txt | 20 ++++++++++++ .../auth-by-env/mosquitto_auth_by_env.c | 32 +++++++------------ 6 files changed, 34 insertions(+), 32 deletions(-) delete mode 100644 plugins/auth-by-env/CMakeLists.txt create mode 100644 plugins/examples/auth-by-env/CMakeLists.txt rename plugins/{ => examples}/auth-by-env/mosquitto_auth_by_env.c (76%) diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt index ad2b3220..2ecbadb2 100644 --- a/plugins/CMakeLists.txt +++ b/plugins/CMakeLists.txt @@ -1,4 +1,3 @@ -add_subdirectory(auth-by-env) add_subdirectory(dynamic-security) add_subdirectory(persist-sqlite) add_subdirectory(examples) diff --git a/plugins/auth-by-env/CMakeLists.txt b/plugins/auth-by-env/CMakeLists.txt deleted file mode 100644 index d8214e7d..00000000 --- a/plugins/auth-by-env/CMakeLists.txt +++ /dev/null @@ -1,11 +0,0 @@ -include_directories(${mosquitto_SOURCE_DIR} ${mosquitto_SOURCE_DIR}/include - ${OPENSSL_INCLUDE_DIR} ${STDBOOL_H_PATH} ${STDINT_H_PATH}) - -add_library(mosquitto_auth_by_env MODULE mosquitto_auth_by_env.c) -set_target_properties(mosquitto_auth_by_env PROPERTIES - POSITION_INDEPENDENT_CODE 1 -) -set_target_properties(mosquitto_auth_by_env PROPERTIES PREFIX "") - -# Don't install, these are example plugins only. XXX -#install(TARGETS mosquitto_auth_by_env RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}") diff --git a/plugins/examples/CMakeLists.txt b/plugins/examples/CMakeLists.txt index 54af8942..7ad697b6 100644 --- a/plugins/examples/CMakeLists.txt +++ b/plugins/examples/CMakeLists.txt @@ -3,6 +3,7 @@ if(NOT WIN32) add_subdirectory(client-lifetime-stats) add_subdirectory(message-timestamp) endif() +add_subdirectory(auth-by-env) add_subdirectory(auth-by-ip) add_subdirectory(client-properties) add_subdirectory(connection-state) diff --git a/plugins/examples/Makefile b/plugins/examples/Makefile index 8882b7e7..25031227 100644 --- a/plugins/examples/Makefile +++ b/plugins/examples/Makefile @@ -1,5 +1,6 @@ DIRS= \ add-properties \ + auth-by-env \ auth-by-ip \ client-lifetime-stats \ client-properties \ diff --git a/plugins/examples/auth-by-env/CMakeLists.txt b/plugins/examples/auth-by-env/CMakeLists.txt new file mode 100644 index 00000000..cf21d7ee --- /dev/null +++ b/plugins/examples/auth-by-env/CMakeLists.txt @@ -0,0 +1,20 @@ +set (PLUGIN_NAME mosquitto_auth_by_env) + +add_library(${PLUGIN_NAME} MODULE + ${PLUGIN_NAME}.c +) + +target_include_directories(${PLUGIN_NAME} PRIVATE + "${mosquitto_SOURCE_DIR}" + "${mosquitto_SOURCE_DIR}/include" +) + +set_target_properties(${PLUGIN_NAME} PROPERTIES + PREFIX "" + POSITION_INDEPENDENT_CODE 1 +) + +target_link_libraries(${PLUGIN_NAME} PRIVATE mosquitto) + +# Don't install, these are example plugins only. +#install(TARGETS ${PLUGIN_NAME} RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}") diff --git a/plugins/auth-by-env/mosquitto_auth_by_env.c b/plugins/examples/auth-by-env/mosquitto_auth_by_env.c similarity index 76% rename from plugins/auth-by-env/mosquitto_auth_by_env.c rename to plugins/examples/auth-by-env/mosquitto_auth_by_env.c index c81cefaf..6b7be78b 100644 --- a/plugins/auth-by-env/mosquitto_auth_by_env.c +++ b/plugins/examples/auth-by-env/mosquitto_auth_by_env.c @@ -36,8 +36,10 @@ Contributors: #define ENV_MOSQUITTO_PASSWORD "MOSQUITTO_PASSWORD" +MOSQUITTO_PLUGIN_DECLARE_VERSION(5); + static mosquitto_plugin_id_t *mosq_pid = NULL; -static char *environment_password; +static char *environment_password = NULL; static int basic_auth_callback(int event, void *event_data, void *userdata) { @@ -58,18 +60,6 @@ static int basic_auth_callback(int event, void *event_data, void *userdata) } } -int mosquitto_plugin_version(int supported_version_count, const int *supported_versions) -{ - int i; - - for(i=0; i 0){ - environment_password = strdup(env_var_content); - return mosquitto_callback_register(mosq_pid, MOSQ_EVT_BASIC_AUTH, basic_auth_callback, NULL, NULL); + if(env_var_content && strlen(env_var_content) > 0){ + environment_password = mosquitto_strdup(env_var_content); + if(!environment_password){ + mosquitto_log_printf(MOSQ_LOG_ERR, "Out of memory."); + return MOSQ_ERR_NOMEM; } + return mosquitto_callback_register(mosq_pid, MOSQ_EVT_BASIC_AUTH, basic_auth_callback, NULL, NULL); } - log__printf(NULL, MOSQ_LOG_INFO, "Auth-by-env plugin called, but "ENV_MOSQUITTO_PASSWORD" env var is empty\n"); - return 0; + mosquitto_log_printf(MOSQ_LOG_ERR, "auth-by-env plugin called, but " ENV_MOSQUITTO_PASSWORD " environment variable is empty"); + return MOSQ_ERR_INVAL; } int mosquitto_plugin_cleanup(void *user_data, struct mosquitto_opt *opts, int opt_count) @@ -98,7 +90,7 @@ int mosquitto_plugin_cleanup(void *user_data, struct mosquitto_opt *opts, int op UNUSED(opts); UNUSED(opt_count); - free(environment_password); + mosquitto_free(environment_password); return mosquitto_callback_unregister(mosq_pid, MOSQ_EVT_BASIC_AUTH, basic_auth_callback, NULL); }