From 14d1c32f9de8fb8769747cbe25559f64741c673e Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Thu, 24 Feb 2022 15:52:06 +0000 Subject: [PATCH] Update changelog. Style, whitespace and, linker fixes. --- ChangeLog.txt | 4 +++ apps/mosquitto_ctrl/dynsec.c | 32 +++++++++---------- plugins/common/plugin_common.c | 3 +- plugins/dynamic-security/clients.c | 44 +++++++++++++------------- plugins/dynamic-security/config_init.c | 2 +- src/linker-macosx.syms | 1 + src/linker.syms | 4 +-- 7 files changed, 47 insertions(+), 43 deletions(-) diff --git a/ChangeLog.txt b/ChangeLog.txt index ff6b2305..79e504ba 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -92,6 +92,10 @@ Plugins / plugin interface: - Plugins no longer need to define mosquitto_plugin_cleanup() if they do not need to do any of their own cleanup. Callbacks will be unregistered automatically. +- The dynamic security plugin now reports client connections in getClient and + listClients. +- The dynamic security plugin now generates an initial configuration if none + is present, including a set of default roles. Client library: - Add MOSQ_OPT_DISABLE_SOCKETPAIR to allow the disabling of the socketpair diff --git a/apps/mosquitto_ctrl/dynsec.c b/apps/mosquitto_ctrl/dynsec.c index 6ba23a00..5c6854a7 100644 --- a/apps/mosquitto_ctrl/dynsec.c +++ b/apps/mosquitto_ctrl/dynsec.c @@ -130,41 +130,41 @@ static void print_list(cJSON *j_response, const char *arrayname, const char *key } } -static void print_json_value(cJSON* value, const char* null_value) +static void print_json_value(cJSON *value, const char *null_value) { - if (value){ - if (cJSON_IsString(value)){ + if(value){ + if(cJSON_IsString(value)){ printf("%s", value->valuestring); }else{ char buffer[MAX_STRING_LEN]; cJSON_PrintPreallocated(value, buffer, sizeof(buffer), 0); printf("%s", buffer); } - } else if (null_value){ + }else if(null_value){ printf("%s",null_value); } } -static void print_json_array(cJSON *j_list, int slen, const char* label, const char* element_name, const char* optional_element_name, const char* optional_element_null_value) +static void print_json_array(cJSON *j_list, int slen, const char *label, const char *element_name, const char *optional_element_name, const char *optional_element_null_value) { cJSON *j_elem; if(j_list && cJSON_IsArray(j_list)){ cJSON_ArrayForEach(j_elem, j_list){ - if (cJSON_IsObject(j_elem)) { - cJSON* jtmp = cJSON_GetObjectItem(j_elem, element_name); + if(cJSON_IsObject(j_elem)){ + cJSON *jtmp = cJSON_GetObjectItem(j_elem, element_name); if(!jtmp || !cJSON_IsString(jtmp)){ continue; } printf("%-*s %s", (int)slen, label, jtmp->valuestring); - if (optional_element_name) { + if(optional_element_name){ printf(" (%s: ", optional_element_name); print_json_value(cJSON_GetObjectItem(j_elem,optional_element_name),optional_element_null_value); printf(")"); - } - } else if (cJSON_IsString(j_elem)) { + } + }else if(cJSON_IsString(j_elem)){ printf("%-*s %s", (int)slen, label, j_elem->valuestring); - } + } label = ""; printf("\n"); } @@ -176,9 +176,9 @@ static void print_json_array(cJSON *j_list, int slen, const char* label, const c static void print_client(cJSON *j_response) { - cJSON *j_data, *j_client, *jtmp; + cJSON *j_data, *j_client, *jtmp; const int label_width = strlen( "Connections:"); - + j_data = cJSON_GetObjectItem(j_response, "data"); if(j_data == NULL || !cJSON_IsObject(j_data)){ fprintf(stderr, "Error: Invalid response from server.\n"); @@ -210,7 +210,7 @@ static void print_client(cJSON *j_response) printf("%-*s %s\n", label_width, "Disabled:", cJSON_IsTrue(jtmp)?"true":"false"); } - print_json_array(cJSON_GetObjectItem(j_client, "roles"), label_width, "Roles:", "rolename", "priority", "-1"); + print_json_array(cJSON_GetObjectItem(j_client, "roles"), label_width, "Roles:", "rolename", "priority", "-1"); print_json_array(cJSON_GetObjectItem(j_client, "groups"), label_width, "Groups:", "groupname", "priority", "-1"); print_json_array(cJSON_GetObjectItem(j_client, "connections"), label_width, "Connections:", "address", NULL, NULL); } @@ -240,8 +240,8 @@ static void print_group(cJSON *j_response) } printf("Groupname: %s\n", jtmp->valuestring); - print_json_array(cJSON_GetObjectItem(j_group, "roles"), label_width, "Roles:", "rolename", "priority", "-1"); - print_json_array(cJSON_GetObjectItem(j_group, "clients"), label_width, "Clients:", "username", NULL, NULL); + print_json_array(cJSON_GetObjectItem(j_group, "roles"), label_width, "Roles:", "rolename", "priority", "-1"); + print_json_array(cJSON_GetObjectItem(j_group, "clients"), label_width, "Clients:", "username", NULL, NULL); } diff --git a/plugins/common/plugin_common.c b/plugins/common/plugin_common.c index 210c30ab..ed71a6cf 100644 --- a/plugins/common/plugin_common.c +++ b/plugins/common/plugin_common.c @@ -24,7 +24,7 @@ void plugin__command_reply(struct plugin_cmd *cmd, const char *error) cJSON_AddItemToArray(cmd->j_responses, j_response); } -void plugin_send_response(cJSON *tree, const char* topic) +void plugin_send_response(cJSON *tree, const char *topic) { char *payload; size_t payload_len; @@ -40,4 +40,3 @@ void plugin_send_response(cJSON *tree, const char* topic) } mosquitto_broker_publish(NULL, topic, (int)payload_len, payload, 0, 0, NULL); } - diff --git a/plugins/dynamic-security/clients.c b/plugins/dynamic-security/clients.c index b047ed55..53636100 100644 --- a/plugins/dynamic-security/clients.c +++ b/plugins/dynamic-security/clients.c @@ -28,6 +28,11 @@ Contributors: #include "dynamic_security.h" +struct connection_array_context{ + const char *username; + cJSON *j_connections; +}; + /* ################################################################ * # * # Function declarations @@ -859,44 +864,39 @@ static int dynsec__remove_client_from_all_groups(struct dynsec__data *data, cons return MOSQ_ERR_SUCCESS; } -struct connection_array_context { - const char* username; - cJSON *j_connections; -}; -static int dynsec__add_client_address(const struct mosquitto* client, void* context_ptr) +static int dynsec__add_client_address(const struct mosquitto *client, void *context_ptr) { - struct connection_array_context* functor_context = (struct connection_array_context*)context_ptr; + struct connection_array_context *functor_context = (struct connection_array_context*)context_ptr; - if (!strcmp(functor_context->username,mosquitto_client_username(client))) { + if(!strcmp(functor_context->username,mosquitto_client_username(client))){ cJSON *j_connection = cJSON_CreateObject(); - const char* address; - if (!j_connection){ + const char *address; + if(!j_connection){ return MOSQ_ERR_NOMEM; } - if ((address=mosquitto_client_address(client)) && !cJSON_AddStringToObject(j_connection,"address",address)) { + if((address=mosquitto_client_address(client)) && !cJSON_AddStringToObject(j_connection,"address",address)){ cJSON_Delete(j_connection); return MOSQ_ERR_NOMEM; - } + } cJSON_AddItemToArray(functor_context->j_connections,j_connection); } return MOSQ_ERR_SUCCESS; } - -static cJSON* dynsec_connections__all_to_json(const char* username, const char* clientid) + +static cJSON *dynsec_connections__all_to_json(const char *username, const char *clientid) { struct connection_array_context functor_context = { username, cJSON_CreateArray()}; - // functor_context.j_connections = cJSON_CreateArray(); - //functor_context.username = username; - if (clientid) { - const struct mosquitto* client = mosquitto_client(clientid); - if (client && dynsec__add_client_address(client, &functor_context) != MOSQ_ERR_SUCCESS) { + + if(clientid){ + const struct mosquitto *client = mosquitto_client(clientid); + if(client && dynsec__add_client_address(client, &functor_context) != MOSQ_ERR_SUCCESS){ cJSON_Delete(functor_context.j_connections); return NULL; } - } else { - if (mosquitto_apply_on_all_clients(&dynsec__add_client_address, &functor_context) != MOSQ_ERR_SUCCESS) { + }else{ + if(mosquitto_apply_on_all_clients(&dynsec__add_client_address, &functor_context) != MOSQ_ERR_SUCCESS){ cJSON_Delete(functor_context.j_connections); return NULL; } @@ -911,7 +911,7 @@ static cJSON *add_client_to_json(struct dynsec__client *client, bool verbose) if(verbose){ cJSON *j_groups, *j_roles, *j_connections; - + j_client = cJSON_CreateObject(); if(j_client == NULL){ return NULL; @@ -943,7 +943,7 @@ static cJSON *add_client_to_json(struct dynsec__client *client, bool verbose) cJSON_AddItemToObject(j_client, "groups", j_groups); j_connections = dynsec_connections__all_to_json(client->username, client->clientid); - if (j_connections == NULL){ + if(j_connections == NULL){ cJSON_Delete(j_client); return NULL; } diff --git a/plugins/dynamic-security/config_init.c b/plugins/dynamic-security/config_init.c index 824b8eb1..264464d4 100644 --- a/plugins/dynamic-security/config_init.c +++ b/plugins/dynamic-security/config_init.c @@ -162,6 +162,7 @@ static int client_add_admin(FILE *pwfile, cJSON *j_clients) if(client_role_add(j_roles, "super-admin") || client_role_add(j_roles, "topic-observe")){ + free(password); return MOSQ_ERR_NOMEM; } @@ -206,7 +207,6 @@ static int client_add_user(FILE *pwfile, cJSON *j_clients) free(salt); if(client_role_add(j_roles, "client")){ - free(password); return MOSQ_ERR_NOMEM; } diff --git a/src/linker-macosx.syms b/src/linker-macosx.syms index 11975eb1..ca316d48 100644 --- a/src/linker-macosx.syms +++ b/src/linker-macosx.syms @@ -1,3 +1,4 @@ +_mosquitto_apply_on_all_clients _mosquitto_broker_node_id_set _mosquitto_broker_publish _mosquitto_broker_publish_copy diff --git a/src/linker.syms b/src/linker.syms index 2e62c9cf..6652e230 100644 --- a/src/linker.syms +++ b/src/linker.syms @@ -1,4 +1,5 @@ { + mosquitto_apply_on_all_clients; mosquitto_broker_node_id_set; mosquitto_broker_publish; mosquitto_broker_publish_copy; @@ -20,14 +21,13 @@ mosquitto_free; mosquitto_kick_client_by_clientid; mosquitto_kick_client_by_username; - mosquitto_apply_on_all_clients; mosquitto_log_printf; mosquitto_malloc; mosquitto_persist_client_add; + mosquitto_persist_client_delete; mosquitto_persist_client_msg_add; mosquitto_persist_client_msg_delete; mosquitto_persist_client_msg_update; - mosquitto_persist_client_delete; mosquitto_persist_client_update; mosquitto_persist_msg_add; mosquitto_persist_msg_delete;