diff --git a/plugins/dynamic-security/acl.c b/plugins/dynamic-security/acl.c index 592142b3..7453bcd1 100644 --- a/plugins/dynamic-security/acl.c +++ b/plugins/dynamic-security/acl.c @@ -23,7 +23,7 @@ Contributors: #include "mosquitto_broker.h" #include "mosquitto_plugin.h" -typedef int (*MOSQ_FUNC_acl_check)(struct mosquitto_evt_acl_check *, struct dynsec__rolelist *); +typedef int (*MOSQ_FUNC_acl_check)(struct dynsec__data *data, struct mosquitto_evt_acl_check *, struct dynsec__rolelist *); /* FIXME - CACHE! */ @@ -33,13 +33,15 @@ typedef int (*MOSQ_FUNC_acl_check)(struct mosquitto_evt_acl_check *, struct dyns * # * ################################################################ */ -static int acl_check_publish_c_recv(struct mosquitto_evt_acl_check *ed, struct dynsec__rolelist *base_rolelist) +static int acl_check_publish_c_recv(struct dynsec__data *data, struct mosquitto_evt_acl_check *ed, struct dynsec__rolelist *base_rolelist) { struct dynsec__rolelist *rolelist, *rolelist_tmp = NULL; struct dynsec__acl *acl, *acl_tmp = NULL; bool result; const char *clientid, *username; + UNUSED(data); + clientid = mosquitto_client_id(ed->client); username = mosquitto_client_username(ed->client); @@ -65,13 +67,15 @@ static int acl_check_publish_c_recv(struct mosquitto_evt_acl_check *ed, struct d * # * ################################################################ */ -static int acl_check_publish_c_send(struct mosquitto_evt_acl_check *ed, struct dynsec__rolelist *base_rolelist) +static int acl_check_publish_c_send(struct dynsec__data *data, struct mosquitto_evt_acl_check *ed, struct dynsec__rolelist *base_rolelist) { struct dynsec__rolelist *rolelist, *rolelist_tmp = NULL; struct dynsec__acl *acl, *acl_tmp = NULL; bool result; const char *clientid, *username; + UNUSED(data); + clientid = mosquitto_client_id(ed->client); username = mosquitto_client_username(ed->client); @@ -97,7 +101,7 @@ static int acl_check_publish_c_send(struct mosquitto_evt_acl_check *ed, struct d * # * ################################################################ */ -static int acl_check_subscribe(struct mosquitto_evt_acl_check *ed, struct dynsec__rolelist *base_rolelist) +static int acl_check_subscribe(struct dynsec__data *data, struct mosquitto_evt_acl_check *ed, struct dynsec__rolelist *base_rolelist) { struct dynsec__rolelist *rolelist, *rolelist_tmp = NULL; struct dynsec__acl *acl, *acl_tmp = NULL; @@ -106,6 +110,8 @@ static int acl_check_subscribe(struct mosquitto_evt_acl_check *ed, struct dynsec const char *clientid, *username; bool has_wildcard; + UNUSED(data); + len = strlen(ed->topic); has_wildcard = (strpbrk(ed->topic, "+#") != NULL); @@ -145,7 +151,7 @@ static int acl_check_subscribe(struct mosquitto_evt_acl_check *ed, struct dynsec * # * ################################################################ */ -static int acl_check_unsubscribe(struct mosquitto_evt_acl_check *ed, struct dynsec__rolelist *base_rolelist) +static int acl_check_unsubscribe(struct dynsec__data *data, struct mosquitto_evt_acl_check *ed, struct dynsec__rolelist *base_rolelist) { struct dynsec__rolelist *rolelist, *rolelist_tmp = NULL; struct dynsec__acl *acl, *acl_tmp = NULL; @@ -153,6 +159,8 @@ static int acl_check_unsubscribe(struct mosquitto_evt_acl_check *ed, struct dyns bool result; const char *clientid, *username; + UNUSED(data); + len = strlen(ed->topic); clientid = mosquitto_client_id(ed->client); @@ -188,7 +196,7 @@ static int acl_check_unsubscribe(struct mosquitto_evt_acl_check *ed, struct dyns * # * ################################################################ */ -static int acl_check(struct mosquitto_evt_acl_check *ed, MOSQ_FUNC_acl_check check, bool acl_default_access) +static int acl_check(struct dynsec__data *data, struct mosquitto_evt_acl_check *ed, MOSQ_FUNC_acl_check check, bool acl_default_access) { struct dynsec__client *client; struct dynsec__grouplist *grouplist, *grouplist_tmp = NULL; @@ -198,24 +206,24 @@ static int acl_check(struct mosquitto_evt_acl_check *ed, MOSQ_FUNC_acl_check che username = mosquitto_client_username(ed->client); if(username){ - client = dynsec_clients__find(username); + client = dynsec_clients__find(data, username); if(client == NULL) return MOSQ_ERR_PLUGIN_DEFER; /* Client roles */ - rc = check(ed, client->rolelist); + rc = check(data, ed, client->rolelist); if(rc != MOSQ_ERR_NOT_FOUND){ return rc; } HASH_ITER(hh, client->grouplist, grouplist, grouplist_tmp){ - rc = check(ed, grouplist->group->rolelist); + rc = check(data, ed, grouplist->group->rolelist); if(rc != MOSQ_ERR_NOT_FOUND){ return rc; } } - }else if(g_dynsec_data.anonymous_group){ + }else if(data->anonymous_group){ /* If we have a group for anonymous users, use that for checking. */ - rc = check(ed, g_dynsec_data.anonymous_group->rolelist); + rc = check(data, ed, data->anonymous_group->rolelist); if(rc != MOSQ_ERR_NOT_FOUND){ return rc; } @@ -244,6 +252,7 @@ static int acl_check(struct mosquitto_evt_acl_check *ed, MOSQ_FUNC_acl_check che int dynsec__acl_check_callback(int event, void *event_data, void *userdata) { struct mosquitto_evt_acl_check *ed = event_data; + struct dynsec__data *data = userdata; UNUSED(event); UNUSED(userdata); @@ -260,16 +269,16 @@ int dynsec__acl_check_callback(int event, void *event_data, void *userdata) switch(ed->access){ case MOSQ_ACL_SUBSCRIBE: - return acl_check(event_data, acl_check_subscribe, g_dynsec_data.default_access.subscribe); + return acl_check(data, event_data, acl_check_subscribe, data->default_access.subscribe); break; case MOSQ_ACL_UNSUBSCRIBE: - return acl_check(event_data, acl_check_unsubscribe, g_dynsec_data.default_access.unsubscribe); + return acl_check(data, event_data, acl_check_unsubscribe, data->default_access.unsubscribe); break; case MOSQ_ACL_WRITE: /* Client to broker */ - return acl_check(event_data, acl_check_publish_c_send, g_dynsec_data.default_access.publish_c_send); + return acl_check(data, event_data, acl_check_publish_c_send, data->default_access.publish_c_send); break; case MOSQ_ACL_READ: - return acl_check(event_data, acl_check_publish_c_recv, g_dynsec_data.default_access.publish_c_recv); + return acl_check(data, event_data, acl_check_publish_c_recv, data->default_access.publish_c_recv); break; default: return MOSQ_ERR_PLUGIN_DEFER; diff --git a/plugins/dynamic-security/auth.c b/plugins/dynamic-security/auth.c index d6d0a3c0..54f23f88 100644 --- a/plugins/dynamic-security/auth.c +++ b/plugins/dynamic-security/auth.c @@ -53,6 +53,7 @@ static int memcmp_const(const void *a, const void *b, size_t len) int dynsec_auth__basic_auth_callback(int event, void *event_data, void *userdata) { struct mosquitto_evt_basic_auth *ed = event_data; + struct dynsec__data *data = userdata; struct dynsec__client *client; unsigned char password_hash[64]; /* For SHA512 */ const char *clientid; @@ -62,7 +63,7 @@ int dynsec_auth__basic_auth_callback(int event, void *event_data, void *userdata if(ed->username == NULL || ed->password == NULL) return MOSQ_ERR_PLUGIN_DEFER; - client = dynsec_clients__find(ed->username); + client = dynsec_clients__find(data, ed->username); if(client){ if(client->disabled){ return MOSQ_ERR_AUTH; diff --git a/plugins/dynamic-security/clients.c b/plugins/dynamic-security/clients.c index 9e47222b..a5493972 100644 --- a/plugins/dynamic-security/clients.c +++ b/plugins/dynamic-security/clients.c @@ -34,7 +34,7 @@ Contributors: * # * ################################################################ */ -static int dynsec__remove_client_from_all_groups(const char *username); +static int dynsec__remove_client_from_all_groups(struct dynsec__data *data, const char *username); static void client__remove_all_roles(struct dynsec__client *client); /* ################################################################ @@ -57,28 +57,28 @@ static int client_cmp(void *a, void *b) return strcmp(client_a->username, client_b->username); } -struct dynsec__client *dynsec_clients__find(const char *username) +struct dynsec__client *dynsec_clients__find(struct dynsec__data *data, const char *username) { struct dynsec__client *client = NULL; if(username){ - HASH_FIND(hh, g_dynsec_data.clients, username, strlen(username), client); + HASH_FIND(hh, data->clients, username, strlen(username), client); } return client; } -static void client__free_item(struct dynsec__client *client) +static void client__free_item(struct dynsec__data *data, struct dynsec__client *client) { struct dynsec__client *client_found; if(client == NULL) return; - client_found = dynsec_clients__find(client->username); + client_found = dynsec_clients__find(data, client->username); if(client_found){ - HASH_DEL(g_dynsec_data.clients, client_found); + HASH_DEL(data->clients, client_found); } dynsec_rolelist__cleanup(&client->rolelist); - dynsec__remove_client_from_all_groups(client->username); + dynsec__remove_client_from_all_groups(data, client->username); mosquitto_free(client->text_name); mosquitto_free(client->text_description); mosquitto_free(client->clientid); @@ -86,12 +86,12 @@ static void client__free_item(struct dynsec__client *client) mosquitto_free(client); } -void dynsec_clients__cleanup(void) +void dynsec_clients__cleanup(struct dynsec__data *data) { struct dynsec__client *client, *client_tmp; - HASH_ITER(hh, g_dynsec_data.clients, client, client_tmp){ - client__free_item(client); + HASH_ITER(hh, data->clients, client, client_tmp){ + client__free_item(data, client); } } @@ -101,7 +101,7 @@ void dynsec_clients__cleanup(void) * # * ################################################################ */ -int dynsec_clients__config_load(cJSON *tree) +int dynsec_clients__config_load(struct dynsec__data *data, cJSON *tree) { cJSON *j_clients, *j_client, *jtmp, *j_roles, *j_role; cJSON *j_salt, *j_password, *j_iterations; @@ -232,29 +232,29 @@ int dynsec_clients__config_load(cJSON *tree) jtmp = cJSON_GetObjectItem(j_role, "rolename"); if(jtmp && cJSON_IsString(jtmp)){ json_get_int(j_role, "priority", &priority, true, -1); - role = dynsec_roles__find(jtmp->valuestring); + role = dynsec_roles__find(data, jtmp->valuestring); dynsec_rolelist__client_add(client, role, priority); } } } } - HASH_ADD_KEYPTR(hh, g_dynsec_data.clients, client->username, strlen(client->username), client); + HASH_ADD_KEYPTR(hh, data->clients, client->username, strlen(client->username), client); } } - HASH_SORT(g_dynsec_data.clients, client_cmp); + HASH_SORT(data->clients, client_cmp); return 0; } -static int dynsec__config_add_clients(cJSON *j_clients) +static int dynsec__config_add_clients(struct dynsec__data *data, cJSON *j_clients) { struct dynsec__client *client, *client_tmp; cJSON *j_client, *j_roles, *jtmp; char *buf; - HASH_ITER(hh, g_dynsec_data.clients, client, client_tmp){ + HASH_ITER(hh, data->clients, client, client_tmp){ j_client = cJSON_CreateObject(); if(j_client == NULL) return 1; cJSON_AddItemToArray(j_clients, j_client); @@ -303,14 +303,14 @@ static int dynsec__config_add_clients(cJSON *j_clients) } -int dynsec_clients__config_save(cJSON *tree) +int dynsec_clients__config_save(struct dynsec__data *data, cJSON *tree) { cJSON *j_clients; if((j_clients = cJSON_AddArrayToObject(tree, "clients")) == NULL){ return 1; } - if(dynsec__config_add_clients(j_clients)){ + if(dynsec__config_add_clients(data, j_clients)){ return 1; } @@ -318,7 +318,7 @@ int dynsec_clients__config_save(cJSON *tree) } -int dynsec_clients__process_create(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) +int dynsec_clients__process_create(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) { char *username, *password, *clientid = NULL; char *text_name, *text_description; @@ -362,7 +362,7 @@ int dynsec_clients__process_create(cJSON *j_responses, struct mosquitto *context return MOSQ_ERR_INVAL; } - client = dynsec_clients__find(username); + client = dynsec_clients__find(data, username); if(client){ dynsec__command_reply(j_responses, context, "createClient", "Client already exists", correlation_data); return MOSQ_ERR_SUCCESS; @@ -376,14 +376,14 @@ int dynsec_clients__process_create(cJSON *j_responses, struct mosquitto *context client->username = mosquitto_strdup(username); if(client->username == NULL){ dynsec__command_reply(j_responses, context, "createClient", "Internal error", correlation_data); - client__free_item(client); + client__free_item(data, client); return MOSQ_ERR_NOMEM; } if(text_name){ client->text_name = mosquitto_strdup(text_name); if(client->text_name == NULL){ dynsec__command_reply(j_responses, context, "createClient", "Internal error", correlation_data); - client__free_item(client); + client__free_item(data, client); return MOSQ_ERR_NOMEM; } } @@ -391,7 +391,7 @@ int dynsec_clients__process_create(cJSON *j_responses, struct mosquitto *context client->text_description = mosquitto_strdup(text_description); if(client->text_description == NULL){ dynsec__command_reply(j_responses, context, "createClient", "Internal error", correlation_data); - client__free_item(client); + client__free_item(data, client); return MOSQ_ERR_NOMEM; } } @@ -399,7 +399,7 @@ int dynsec_clients__process_create(cJSON *j_responses, struct mosquitto *context if(password){ if(dynsec_auth__pw_hash(client, password, client->pw.password_hash, sizeof(client->pw.password_hash), true)){ dynsec__command_reply(j_responses, context, "createClient", "Internal error", correlation_data); - client__free_item(client); + client__free_item(data, client); return MOSQ_ERR_NOMEM; } client->pw.valid = true; @@ -408,16 +408,16 @@ int dynsec_clients__process_create(cJSON *j_responses, struct mosquitto *context client->clientid = mosquitto_strdup(clientid); if(client->clientid == NULL){ dynsec__command_reply(j_responses, context, "createClient", "Internal error", correlation_data); - client__free_item(client); + client__free_item(data, client); return MOSQ_ERR_NOMEM; } } - rc = dynsec_rolelist__load_from_json(command, &client->rolelist); + rc = dynsec_rolelist__load_from_json(data, command, &client->rolelist); if(rc == MOSQ_ERR_SUCCESS || rc == ERR_LIST_NOT_FOUND){ }else if(rc == MOSQ_ERR_NOT_FOUND){ dynsec__command_reply(j_responses, context, "createClient", "Role not found", correlation_data); - client__free_item(client); + client__free_item(data, client); return MOSQ_ERR_INVAL; }else{ if(rc == MOSQ_ERR_INVAL){ @@ -425,12 +425,12 @@ int dynsec_clients__process_create(cJSON *j_responses, struct mosquitto *context }else{ dynsec__command_reply(j_responses, context, "createClient", "Internal error", correlation_data); } - client__free_item(client); + client__free_item(data, client); return MOSQ_ERR_INVAL; } /* Must add user before groups, otherwise adding groups will fail */ - HASH_ADD_KEYPTR_INORDER(hh, g_dynsec_data.clients, client->username, strlen(client->username), client, client_cmp); + HASH_ADD_KEYPTR_INORDER(hh, data->clients, client->username, strlen(client->username), client, client_cmp); j_groups = cJSON_GetObjectItem(command, "groups"); if(j_groups && cJSON_IsArray(j_groups)){ @@ -439,14 +439,14 @@ int dynsec_clients__process_create(cJSON *j_responses, struct mosquitto *context jtmp = cJSON_GetObjectItem(j_group, "groupname"); if(jtmp && cJSON_IsString(jtmp)){ json_get_int(j_group, "priority", &priority, true, -1); - rc = dynsec_groups__add_client(username, jtmp->valuestring, priority, false); + rc = dynsec_groups__add_client(data, username, jtmp->valuestring, priority, false); if(rc == ERR_GROUP_NOT_FOUND){ dynsec__command_reply(j_responses, context, "createClient", "Group not found", correlation_data); - client__free_item(client); + client__free_item(data, client); return MOSQ_ERR_INVAL; }else if(rc != MOSQ_ERR_SUCCESS){ dynsec__command_reply(j_responses, context, "createClient", "Internal error", correlation_data); - client__free_item(client); + client__free_item(data, client); return MOSQ_ERR_INVAL; } } @@ -454,7 +454,7 @@ int dynsec_clients__process_create(cJSON *j_responses, struct mosquitto *context } } - dynsec__config_save(); + dynsec__config_save(data); dynsec__command_reply(j_responses, context, "createClient", NULL, correlation_data); @@ -467,7 +467,7 @@ int dynsec_clients__process_create(cJSON *j_responses, struct mosquitto *context } -int dynsec_clients__process_delete(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) +int dynsec_clients__process_delete(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) { char *username; struct dynsec__client *client; @@ -478,12 +478,12 @@ int dynsec_clients__process_delete(cJSON *j_responses, struct mosquitto *context return MOSQ_ERR_INVAL; } - client = dynsec_clients__find(username); + client = dynsec_clients__find(data, username); if(client){ - dynsec__remove_client_from_all_groups(username); + dynsec__remove_client_from_all_groups(data, username); client__remove_all_roles(client); - client__free_item(client); - dynsec__config_save(); + client__free_item(data, client); + dynsec__config_save(data); dynsec__command_reply(j_responses, context, "deleteClient", NULL, correlation_data); /* Enforce any changes */ @@ -501,7 +501,7 @@ int dynsec_clients__process_delete(cJSON *j_responses, struct mosquitto *context } } -int dynsec_clients__process_disable(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) +int dynsec_clients__process_disable(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) { char *username; struct dynsec__client *client; @@ -516,7 +516,7 @@ int dynsec_clients__process_disable(cJSON *j_responses, struct mosquitto *contex return MOSQ_ERR_INVAL; } - client = dynsec_clients__find(username); + client = dynsec_clients__find(data, username); if(client == NULL){ dynsec__command_reply(j_responses, context, "disableClient", "Client not found", correlation_data); return MOSQ_ERR_SUCCESS; @@ -526,7 +526,7 @@ int dynsec_clients__process_disable(cJSON *j_responses, struct mosquitto *contex mosquitto_kick_client_by_username(username, false); - dynsec__config_save(); + dynsec__config_save(data); dynsec__command_reply(j_responses, context, "disableClient", NULL, correlation_data); admin_clientid = mosquitto_client_id(context); @@ -538,7 +538,7 @@ int dynsec_clients__process_disable(cJSON *j_responses, struct mosquitto *contex } -int dynsec_clients__process_enable(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) +int dynsec_clients__process_enable(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) { char *username; struct dynsec__client *client; @@ -553,7 +553,7 @@ int dynsec_clients__process_enable(cJSON *j_responses, struct mosquitto *context return MOSQ_ERR_INVAL; } - client = dynsec_clients__find(username); + client = dynsec_clients__find(data, username); if(client == NULL){ dynsec__command_reply(j_responses, context, "enableClient", "Client not found", correlation_data); return MOSQ_ERR_SUCCESS; @@ -561,7 +561,7 @@ int dynsec_clients__process_enable(cJSON *j_responses, struct mosquitto *context client->disabled = false; - dynsec__config_save(); + dynsec__config_save(data); dynsec__command_reply(j_responses, context, "enableClient", NULL, correlation_data); admin_clientid = mosquitto_client_id(context); @@ -573,7 +573,7 @@ int dynsec_clients__process_enable(cJSON *j_responses, struct mosquitto *context } -int dynsec_clients__process_set_id(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) +int dynsec_clients__process_set_id(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) { char *username, *clientid, *clientid_heap = NULL; struct dynsec__client *client; @@ -610,7 +610,7 @@ int dynsec_clients__process_set_id(cJSON *j_responses, struct mosquitto *context } } - client = dynsec_clients__find(username); + client = dynsec_clients__find(data, username); if(client == NULL){ mosquitto_free(clientid_heap); dynsec__command_reply(j_responses, context, "setClientId", "Client not found", correlation_data); @@ -620,7 +620,7 @@ int dynsec_clients__process_set_id(cJSON *j_responses, struct mosquitto *context mosquitto_free(client->clientid); client->clientid = clientid_heap; - dynsec__config_save(); + dynsec__config_save(data); dynsec__command_reply(j_responses, context, "setClientId", NULL, correlation_data); /* Enforce any changes */ @@ -648,7 +648,7 @@ static int client__set_password(struct dynsec__client *client, const char *passw } } -int dynsec_clients__process_set_password(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) +int dynsec_clients__process_set_password(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) { char *username, *password; struct dynsec__client *client; @@ -673,14 +673,14 @@ int dynsec_clients__process_set_password(cJSON *j_responses, struct mosquitto *c return MOSQ_ERR_INVAL; } - client = dynsec_clients__find(username); + client = dynsec_clients__find(data, username); if(client == NULL){ dynsec__command_reply(j_responses, context, "setClientPassword", "Client not found", correlation_data); return MOSQ_ERR_SUCCESS; } rc = client__set_password(client, password); if(rc == MOSQ_ERR_SUCCESS){ - dynsec__config_save(); + dynsec__config_save(data); dynsec__command_reply(j_responses, context, "setClientPassword", NULL, correlation_data); /* Enforce any changes */ @@ -715,7 +715,7 @@ static void client__remove_all_roles(struct dynsec__client *client) } } -int dynsec_clients__process_modify(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) +int dynsec_clients__process_modify(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) { char *username; char *clientid; @@ -738,7 +738,7 @@ int dynsec_clients__process_modify(cJSON *j_responses, struct mosquitto *context return MOSQ_ERR_INVAL; } - client = dynsec_clients__find(username); + client = dynsec_clients__find(data, username); if(client == NULL){ dynsec__command_reply(j_responses, context, "modifyClient", "Client not found", correlation_data); return MOSQ_ERR_INVAL; @@ -792,7 +792,7 @@ int dynsec_clients__process_modify(cJSON *j_responses, struct mosquitto *context client->text_description = str; } - rc = dynsec_rolelist__load_from_json(command, &rolelist); + rc = dynsec_rolelist__load_from_json(data, command, &rolelist); if(rc == MOSQ_ERR_SUCCESS){ client__remove_all_roles(client); client__add_new_roles(client, rolelist); @@ -817,20 +817,20 @@ int dynsec_clients__process_modify(cJSON *j_responses, struct mosquitto *context j_groups = cJSON_GetObjectItem(command, "groups"); if(j_groups && cJSON_IsArray(j_groups)){ - dynsec__remove_client_from_all_groups(username); + dynsec__remove_client_from_all_groups(data, username); cJSON_ArrayForEach(j_group, j_groups){ if(cJSON_IsObject(j_group)){ jtmp = cJSON_GetObjectItem(j_group, "groupname"); if(jtmp && cJSON_IsString(jtmp)){ json_get_int(j_group, "priority", &priority, true, -1); - dynsec_groups__add_client(username, jtmp->valuestring, priority, false); + dynsec_groups__add_client(data, username, jtmp->valuestring, priority, false); } } } } - dynsec__config_save(); + dynsec__config_save(data); dynsec__command_reply(j_responses, context, "modifyClient", NULL, correlation_data); /* Enforce any changes */ @@ -844,15 +844,15 @@ int dynsec_clients__process_modify(cJSON *j_responses, struct mosquitto *context } -static int dynsec__remove_client_from_all_groups(const char *username) +static int dynsec__remove_client_from_all_groups(struct dynsec__data *data, const char *username) { struct dynsec__grouplist *grouplist, *grouplist_tmp; struct dynsec__client *client; - client = dynsec_clients__find(username); + client = dynsec_clients__find(data, username); if(client){ HASH_ITER(hh, client->grouplist, grouplist, grouplist_tmp){ - dynsec_groups__remove_client(username, grouplist->group->groupname, false); + dynsec_groups__remove_client(data, username, grouplist->group->groupname, false); } } @@ -904,7 +904,7 @@ static cJSON *add_client_to_json(struct dynsec__client *client, bool verbose) } -int dynsec_clients__process_get(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) +int dynsec_clients__process_get(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) { char *username; struct dynsec__client *client; @@ -920,7 +920,7 @@ int dynsec_clients__process_get(cJSON *j_responses, struct mosquitto *context, c return MOSQ_ERR_INVAL; } - client = dynsec_clients__find(username); + client = dynsec_clients__find(data, username); if(client == NULL){ dynsec__command_reply(j_responses, context, "getClient", "Client not found", correlation_data); return MOSQ_ERR_SUCCESS; @@ -960,7 +960,7 @@ int dynsec_clients__process_get(cJSON *j_responses, struct mosquitto *context, c } -int dynsec_clients__process_list(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) +int dynsec_clients__process_list(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) { bool verbose; struct dynsec__client *client, *client_tmp; @@ -980,7 +980,7 @@ int dynsec_clients__process_list(cJSON *j_responses, struct mosquitto *context, if(cJSON_AddStringToObject(tree, "command", "listClients") == NULL || (j_data = cJSON_AddObjectToObject(tree, "data")) == NULL - || cJSON_AddIntToObject(j_data, "totalCount", (int)HASH_CNT(hh, g_dynsec_data.clients)) == NULL + || cJSON_AddIntToObject(j_data, "totalCount", (int)HASH_CNT(hh, data->clients)) == NULL || (j_clients = cJSON_AddArrayToObject(j_data, "clients")) == NULL || (correlation_data && cJSON_AddStringToObject(tree, "correlationData", correlation_data) == NULL) ){ @@ -991,7 +991,7 @@ int dynsec_clients__process_list(cJSON *j_responses, struct mosquitto *context, } i = 0; - HASH_ITER(hh, g_dynsec_data.clients, client, client_tmp){ + HASH_ITER(hh, data->clients, client, client_tmp){ if(i>=offset){ j_client = add_client_to_json(client, verbose); if(j_client == NULL){ @@ -1021,7 +1021,7 @@ int dynsec_clients__process_list(cJSON *j_responses, struct mosquitto *context, } -int dynsec_clients__process_add_role(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) +int dynsec_clients__process_add_role(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) { char *username, *rolename; struct dynsec__client *client; @@ -1048,13 +1048,13 @@ int dynsec_clients__process_add_role(cJSON *j_responses, struct mosquitto *conte } json_get_int(command, "priority", &priority, true, -1); - client = dynsec_clients__find(username); + client = dynsec_clients__find(data, username); if(client == NULL){ dynsec__command_reply(j_responses, context, "addClientRole", "Client not found", correlation_data); return MOSQ_ERR_SUCCESS; } - role = dynsec_roles__find(rolename); + role = dynsec_roles__find(data, rolename); if(role == NULL){ dynsec__command_reply(j_responses, context, "addClientRole", "Role not found", correlation_data); return MOSQ_ERR_SUCCESS; @@ -1064,7 +1064,7 @@ int dynsec_clients__process_add_role(cJSON *j_responses, struct mosquitto *conte dynsec__command_reply(j_responses, context, "addClientRole", "Internal error", correlation_data); return MOSQ_ERR_UNKNOWN; } - dynsec__config_save(); + dynsec__config_save(data); dynsec__command_reply(j_responses, context, "addClientRole", NULL, correlation_data); /* Enforce any changes */ @@ -1079,7 +1079,7 @@ int dynsec_clients__process_add_role(cJSON *j_responses, struct mosquitto *conte } -int dynsec_clients__process_remove_role(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) +int dynsec_clients__process_remove_role(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) { char *username, *rolename; struct dynsec__client *client; @@ -1105,20 +1105,20 @@ int dynsec_clients__process_remove_role(cJSON *j_responses, struct mosquitto *co } - client = dynsec_clients__find(username); + client = dynsec_clients__find(data, username); if(client == NULL){ dynsec__command_reply(j_responses, context, "removeClientRole", "Client not found", correlation_data); return MOSQ_ERR_SUCCESS; } - role = dynsec_roles__find(rolename); + role = dynsec_roles__find(data, rolename); if(role == NULL){ dynsec__command_reply(j_responses, context, "removeClientRole", "Role not found", correlation_data); return MOSQ_ERR_SUCCESS; } dynsec_rolelist__client_remove(client, role); - dynsec__config_save(); + dynsec__config_save(data); dynsec__command_reply(j_responses, context, "removeClientRole", NULL, correlation_data); /* Enforce any changes */ diff --git a/plugins/dynamic-security/config.c b/plugins/dynamic-security/config.c index f2985a27..c2ab9837 100644 --- a/plugins/dynamic-security/config.c +++ b/plugins/dynamic-security/config.c @@ -33,7 +33,7 @@ Contributors: #include "dynamic_security.h" -static int dynsec__general_config_load(cJSON *tree) +static int dynsec__general_config_load(struct dynsec__data *data, cJSON *tree) { cJSON *j_default_access, *jtmp; @@ -41,36 +41,36 @@ static int dynsec__general_config_load(cJSON *tree) if(j_default_access && cJSON_IsObject(j_default_access)){ jtmp = cJSON_GetObjectItem(j_default_access, ACL_TYPE_PUB_C_SEND); if(jtmp && cJSON_IsBool(jtmp)){ - g_dynsec_data.default_access.publish_c_send = cJSON_IsTrue(jtmp); + data->default_access.publish_c_send = cJSON_IsTrue(jtmp); }else{ - g_dynsec_data.default_access.publish_c_send = false; + data->default_access.publish_c_send = false; } jtmp = cJSON_GetObjectItem(j_default_access, ACL_TYPE_PUB_C_RECV); if(jtmp && cJSON_IsBool(jtmp)){ - g_dynsec_data.default_access.publish_c_recv = cJSON_IsTrue(jtmp); + data->default_access.publish_c_recv = cJSON_IsTrue(jtmp); }else{ - g_dynsec_data.default_access.publish_c_recv = false; + data->default_access.publish_c_recv = false; } jtmp = cJSON_GetObjectItem(j_default_access, ACL_TYPE_SUB_GENERIC); if(jtmp && cJSON_IsBool(jtmp)){ - g_dynsec_data.default_access.subscribe = cJSON_IsTrue(jtmp); + data->default_access.subscribe = cJSON_IsTrue(jtmp); }else{ - g_dynsec_data.default_access.subscribe = false; + data->default_access.subscribe = false; } jtmp = cJSON_GetObjectItem(j_default_access, ACL_TYPE_UNSUB_GENERIC); if(jtmp && cJSON_IsBool(jtmp)){ - g_dynsec_data.default_access.unsubscribe = cJSON_IsTrue(jtmp); + data->default_access.unsubscribe = cJSON_IsTrue(jtmp); }else{ - g_dynsec_data.default_access.unsubscribe = false; + data->default_access.unsubscribe = false; } } return MOSQ_ERR_SUCCESS; } -static int dynsec__general_config_save(cJSON *tree) +static int dynsec__general_config_save(struct dynsec__data *data, cJSON *tree) { cJSON *j_default_access; @@ -80,10 +80,10 @@ static int dynsec__general_config_save(cJSON *tree) } cJSON_AddItemToObject(tree, "defaultACLAccess", j_default_access); - if(cJSON_AddBoolToObject(j_default_access, ACL_TYPE_PUB_C_SEND, g_dynsec_data.default_access.publish_c_send) == NULL - || cJSON_AddBoolToObject(j_default_access, ACL_TYPE_PUB_C_RECV, g_dynsec_data.default_access.publish_c_recv) == NULL - || cJSON_AddBoolToObject(j_default_access, ACL_TYPE_SUB_GENERIC, g_dynsec_data.default_access.subscribe) == NULL - || cJSON_AddBoolToObject(j_default_access, ACL_TYPE_UNSUB_GENERIC, g_dynsec_data.default_access.unsubscribe) == NULL + if(cJSON_AddBoolToObject(j_default_access, ACL_TYPE_PUB_C_SEND, data->default_access.publish_c_send) == NULL + || cJSON_AddBoolToObject(j_default_access, ACL_TYPE_PUB_C_RECV, data->default_access.publish_c_recv) == NULL + || cJSON_AddBoolToObject(j_default_access, ACL_TYPE_SUB_GENERIC, data->default_access.subscribe) == NULL + || cJSON_AddBoolToObject(j_default_access, ACL_TYPE_UNSUB_GENERIC, data->default_access.unsubscribe) == NULL ){ return 1; @@ -92,7 +92,7 @@ static int dynsec__general_config_save(cJSON *tree) return MOSQ_ERR_SUCCESS; } -int dynsec__config_load(void) +int dynsec__config_load(struct dynsec__data *data) { FILE *fptr; long flen_l; @@ -101,14 +101,14 @@ int dynsec__config_load(void) cJSON *tree; /* Load from file */ - fptr = fopen(g_config_file, "rb"); + fptr = fopen(data->config_file, "rb"); if(fptr == NULL){ /* Attempt to initialise a new config file */ - if(dynsec__config_init(g_config_file) == MOSQ_ERR_SUCCESS){ + if(dynsec__config_init(data->config_file) == MOSQ_ERR_SUCCESS){ mosquitto_log_printf(MOSQ_LOG_INFO, "Dynamic security plugin config not found, generating a default config."); - mosquitto_log_printf(MOSQ_LOG_INFO, " Generated passwords are at %s.pw", g_config_file); + mosquitto_log_printf(MOSQ_LOG_INFO, " Generated passwords are at %s.pw", data->config_file); /* If it works, try to open the file again */ - fptr = fopen(g_config_file, "rb"); + fptr = fopen(data->config_file, "rb"); } if(fptr == NULL){ @@ -151,10 +151,10 @@ int dynsec__config_load(void) return 1; } - if(dynsec__general_config_load(tree) - || dynsec_roles__config_load(tree) - || dynsec_clients__config_load(tree) - || dynsec_groups__config_load(tree) + if(dynsec__general_config_load(data, tree) + || dynsec_roles__config_load(data, tree) + || dynsec_clients__config_load(data, tree) + || dynsec_groups__config_load(data, tree) ){ cJSON_Delete(tree); @@ -166,7 +166,7 @@ int dynsec__config_load(void) } -void dynsec__config_save(void) +void dynsec__config_save(struct dynsec__data *data) { cJSON *tree; size_t file_path_len; @@ -178,10 +178,10 @@ void dynsec__config_save(void) tree = cJSON_CreateObject(); if(tree == NULL) return; - if(dynsec__general_config_save(tree) - || dynsec_clients__config_save(tree) - || dynsec_groups__config_save(tree) - || dynsec_roles__config_save(tree)){ + if(dynsec__general_config_save(data, tree) + || dynsec_clients__config_save(data, tree) + || dynsec_groups__config_save(data, tree) + || dynsec_roles__config_save(data, tree)){ cJSON_Delete(tree); return; @@ -198,14 +198,14 @@ void dynsec__config_save(void) json_str_len = strlen(json_str); /* Save to file */ - file_path_len = strlen(g_config_file) + 1; + file_path_len = strlen(data->config_file) + 1; file_path = mosquitto_malloc(file_path_len); if(file_path == NULL){ mosquitto_free(json_str); mosquitto_log_printf(MOSQ_LOG_ERR, "Error saving Dynamic security plugin config: Out of memory.\n"); return; } - snprintf(file_path, file_path_len, "%s.new", g_config_file); + snprintf(file_path, file_path_len, "%s.new", data->config_file); fptr = fopen(file_path, "wt"); if(fptr == NULL){ @@ -219,7 +219,7 @@ void dynsec__config_save(void) fclose(fptr); /* Everything is ok, so move new file over proper file */ - if(rename(file_path, g_config_file) < 0){ + if(rename(file_path, data->config_file) < 0){ mosquitto_log_printf(MOSQ_LOG_ERR, "Error updating dynsec config file: %s", strerror(errno)); } mosquitto_free(file_path); diff --git a/plugins/dynamic-security/control.c b/plugins/dynamic-security/control.c index fd473354..4cc32fcf 100644 --- a/plugins/dynamic-security/control.c +++ b/plugins/dynamic-security/control.c @@ -77,6 +77,7 @@ static void send_response(cJSON *tree) int dynsec_control_callback(int event, void *event_data, void *userdata) { struct mosquitto_evt_control *ed = event_data; + struct dynsec__data *data = userdata; cJSON *tree, *commands; cJSON *j_response_tree, *j_responses; @@ -119,7 +120,7 @@ int dynsec_control_callback(int event, void *event_data, void *userdata) } /* Handle commands */ - dynsec__handle_control(j_responses, ed->client, commands); + dynsec__handle_control(data, j_responses, ed->client, commands); cJSON_Delete(tree); send_response(j_response_tree); @@ -134,7 +135,7 @@ int dynsec_control_callback(int event, void *event_data, void *userdata) * # * ################################################################ */ -int dynsec__handle_control(cJSON *j_responses, struct mosquitto *context, cJSON *commands) +int dynsec__handle_control(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *commands) { int rc = MOSQ_ERR_SUCCESS; cJSON *aiter; @@ -151,73 +152,73 @@ int dynsec__handle_control(cJSON *j_responses, struct mosquitto *context, cJSON /* Plugin */ if(!strcasecmp(command, "setDefaultACLAccess")){ - rc = dynsec__process_set_default_acl_access(j_responses, context, aiter, correlation_data); + rc = dynsec__process_set_default_acl_access(data, j_responses, context, aiter, correlation_data); }else if(!strcasecmp(command, "getDefaultACLAccess")){ - rc = dynsec__process_get_default_acl_access(j_responses, context, aiter, correlation_data); + rc = dynsec__process_get_default_acl_access(data, j_responses, context, aiter, correlation_data); /* Clients */ }else if(!strcasecmp(command, "createClient")){ - rc = dynsec_clients__process_create(j_responses, context, aiter, correlation_data); + rc = dynsec_clients__process_create(data, j_responses, context, aiter, correlation_data); }else if(!strcasecmp(command, "deleteClient")){ - rc = dynsec_clients__process_delete(j_responses, context, aiter, correlation_data); + rc = dynsec_clients__process_delete(data, j_responses, context, aiter, correlation_data); }else if(!strcasecmp(command, "getClient")){ - rc = dynsec_clients__process_get(j_responses, context, aiter, correlation_data); + rc = dynsec_clients__process_get(data, j_responses, context, aiter, correlation_data); }else if(!strcasecmp(command, "listClients")){ - rc = dynsec_clients__process_list(j_responses, context, aiter, correlation_data); + rc = dynsec_clients__process_list(data, j_responses, context, aiter, correlation_data); }else if(!strcasecmp(command, "modifyClient")){ - rc = dynsec_clients__process_modify(j_responses, context, aiter, correlation_data); + rc = dynsec_clients__process_modify(data, j_responses, context, aiter, correlation_data); }else if(!strcasecmp(command, "setClientPassword")){ - rc = dynsec_clients__process_set_password(j_responses, context, aiter, correlation_data); + rc = dynsec_clients__process_set_password(data, j_responses, context, aiter, correlation_data); }else if(!strcasecmp(command, "setClientId")){ - rc = dynsec_clients__process_set_id(j_responses, context, aiter, correlation_data); + rc = dynsec_clients__process_set_id(data, j_responses, context, aiter, correlation_data); }else if(!strcasecmp(command, "addClientRole")){ - rc = dynsec_clients__process_add_role(j_responses, context, aiter, correlation_data); + rc = dynsec_clients__process_add_role(data, j_responses, context, aiter, correlation_data); }else if(!strcasecmp(command, "removeClientRole")){ - rc = dynsec_clients__process_remove_role(j_responses, context, aiter, correlation_data); + rc = dynsec_clients__process_remove_role(data, j_responses, context, aiter, correlation_data); }else if(!strcasecmp(command, "enableClient")){ - rc = dynsec_clients__process_enable(j_responses, context, aiter, correlation_data); + rc = dynsec_clients__process_enable(data, j_responses, context, aiter, correlation_data); }else if(!strcasecmp(command, "disableClient")){ - rc = dynsec_clients__process_disable(j_responses, context, aiter, correlation_data); + rc = dynsec_clients__process_disable(data, j_responses, context, aiter, correlation_data); /* Groups */ }else if(!strcasecmp(command, "addGroupClient")){ - rc = dynsec_groups__process_add_client(j_responses, context, aiter, correlation_data); + rc = dynsec_groups__process_add_client(data, j_responses, context, aiter, correlation_data); }else if(!strcasecmp(command, "createGroup")){ - rc = dynsec_groups__process_create(j_responses, context, aiter, correlation_data); + rc = dynsec_groups__process_create(data, j_responses, context, aiter, correlation_data); }else if(!strcasecmp(command, "deleteGroup")){ - rc = dynsec_groups__process_delete(j_responses, context, aiter, correlation_data); + rc = dynsec_groups__process_delete(data, j_responses, context, aiter, correlation_data); }else if(!strcasecmp(command, "getGroup")){ - rc = dynsec_groups__process_get(j_responses, context, aiter, correlation_data); + rc = dynsec_groups__process_get(data, j_responses, context, aiter, correlation_data); }else if(!strcasecmp(command, "listGroups")){ - rc = dynsec_groups__process_list(j_responses, context, aiter, correlation_data); + rc = dynsec_groups__process_list(data, j_responses, context, aiter, correlation_data); }else if(!strcasecmp(command, "modifyGroup")){ - rc = dynsec_groups__process_modify(j_responses, context, aiter, correlation_data); + rc = dynsec_groups__process_modify(data, j_responses, context, aiter, correlation_data); }else if(!strcasecmp(command, "removeGroupClient")){ - rc = dynsec_groups__process_remove_client(j_responses, context, aiter, correlation_data); + rc = dynsec_groups__process_remove_client(data, j_responses, context, aiter, correlation_data); }else if(!strcasecmp(command, "addGroupRole")){ - rc = dynsec_groups__process_add_role(j_responses, context, aiter, correlation_data); + rc = dynsec_groups__process_add_role(data, j_responses, context, aiter, correlation_data); }else if(!strcasecmp(command, "removeGroupRole")){ - rc = dynsec_groups__process_remove_role(j_responses, context, aiter, correlation_data); + rc = dynsec_groups__process_remove_role(data, j_responses, context, aiter, correlation_data); }else if(!strcasecmp(command, "setAnonymousGroup")){ - rc = dynsec_groups__process_set_anonymous_group(j_responses, context, aiter, correlation_data); + rc = dynsec_groups__process_set_anonymous_group(data, j_responses, context, aiter, correlation_data); }else if(!strcasecmp(command, "getAnonymousGroup")){ - rc = dynsec_groups__process_get_anonymous_group(j_responses, context, aiter, correlation_data); + rc = dynsec_groups__process_get_anonymous_group(data, j_responses, context, aiter, correlation_data); /* Roles */ }else if(!strcasecmp(command, "createRole")){ - rc = dynsec_roles__process_create(j_responses, context, aiter, correlation_data); + rc = dynsec_roles__process_create(data, j_responses, context, aiter, correlation_data); }else if(!strcasecmp(command, "getRole")){ - rc = dynsec_roles__process_get(j_responses, context, aiter, correlation_data); + rc = dynsec_roles__process_get(data, j_responses, context, aiter, correlation_data); }else if(!strcasecmp(command, "listRoles")){ - rc = dynsec_roles__process_list(j_responses, context, aiter, correlation_data); + rc = dynsec_roles__process_list(data, j_responses, context, aiter, correlation_data); }else if(!strcasecmp(command, "modifyRole")){ - rc = dynsec_roles__process_modify(j_responses, context, aiter, correlation_data); + rc = dynsec_roles__process_modify(data, j_responses, context, aiter, correlation_data); }else if(!strcasecmp(command, "deleteRole")){ - rc = dynsec_roles__process_delete(j_responses, context, aiter, correlation_data); + rc = dynsec_roles__process_delete(data, j_responses, context, aiter, correlation_data); }else if(!strcasecmp(command, "addRoleACL")){ - rc = dynsec_roles__process_add_acl(j_responses, context, aiter, correlation_data); + rc = dynsec_roles__process_add_acl(data, j_responses, context, aiter, correlation_data); }else if(!strcasecmp(command, "removeRoleACL")){ - rc = dynsec_roles__process_remove_acl(j_responses, context, aiter, correlation_data); + rc = dynsec_roles__process_remove_acl(data, j_responses, context, aiter, correlation_data); /* Unknown */ }else{ diff --git a/plugins/dynamic-security/default_acl.c b/plugins/dynamic-security/default_acl.c index 8dbc4264..1072e4ed 100644 --- a/plugins/dynamic-security/default_acl.c +++ b/plugins/dynamic-security/default_acl.c @@ -33,7 +33,7 @@ Contributors: #include "dynamic_security.h" -int dynsec__process_set_default_acl_access(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) +int dynsec__process_set_default_acl_access(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) { cJSON *j_actions, *j_action, *j_acltype, *j_allow; bool allow; @@ -57,26 +57,26 @@ int dynsec__process_set_default_acl_access(cJSON *j_responses, struct mosquitto allow = cJSON_IsTrue(j_allow); if(!strcasecmp(j_acltype->valuestring, ACL_TYPE_PUB_C_SEND)){ - g_dynsec_data.default_access.publish_c_send = allow; + data->default_access.publish_c_send = allow; }else if(!strcasecmp(j_acltype->valuestring, ACL_TYPE_PUB_C_RECV)){ - g_dynsec_data.default_access.publish_c_recv = allow; + data->default_access.publish_c_recv = allow; }else if(!strcasecmp(j_acltype->valuestring, ACL_TYPE_SUB_GENERIC)){ - g_dynsec_data.default_access.subscribe = allow; + data->default_access.subscribe = allow; }else if(!strcasecmp(j_acltype->valuestring, ACL_TYPE_UNSUB_GENERIC)){ - g_dynsec_data.default_access.unsubscribe = allow; + data->default_access.unsubscribe = allow; } mosquitto_log_printf(MOSQ_LOG_INFO, "dynsec: %s/%s | setDefaultACLAccess | acltype=%s | allow=%s", admin_clientid, admin_username, j_acltype->valuestring, allow?"true":"false"); } } - dynsec__config_save(); + dynsec__config_save(data); dynsec__command_reply(j_responses, context, "setDefaultACLAccess", NULL, correlation_data); return MOSQ_ERR_SUCCESS; } -int dynsec__process_get_default_acl_access(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) +int dynsec__process_get_default_acl_access(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) { cJSON *tree, *jtmp, *j_data, *j_acls, *j_acl; const char *admin_clientid, *admin_username; @@ -113,7 +113,7 @@ int dynsec__process_get_default_acl_access(cJSON *j_responses, struct mosquitto } cJSON_AddItemToArray(j_acls, j_acl); if(cJSON_AddStringToObject(j_acl, "acltype", ACL_TYPE_PUB_C_SEND) == NULL - || cJSON_AddBoolToObject(j_acl, "allow", g_dynsec_data.default_access.publish_c_send) == NULL + || cJSON_AddBoolToObject(j_acl, "allow", data->default_access.publish_c_send) == NULL ){ goto internal_error; @@ -126,7 +126,7 @@ int dynsec__process_get_default_acl_access(cJSON *j_responses, struct mosquitto } cJSON_AddItemToArray(j_acls, j_acl); if(cJSON_AddStringToObject(j_acl, "acltype", ACL_TYPE_PUB_C_RECV) == NULL - || cJSON_AddBoolToObject(j_acl, "allow", g_dynsec_data.default_access.publish_c_recv) == NULL + || cJSON_AddBoolToObject(j_acl, "allow", data->default_access.publish_c_recv) == NULL ){ goto internal_error; @@ -139,7 +139,7 @@ int dynsec__process_get_default_acl_access(cJSON *j_responses, struct mosquitto } cJSON_AddItemToArray(j_acls, j_acl); if(cJSON_AddStringToObject(j_acl, "acltype", ACL_TYPE_SUB_GENERIC) == NULL - || cJSON_AddBoolToObject(j_acl, "allow", g_dynsec_data.default_access.subscribe) == NULL + || cJSON_AddBoolToObject(j_acl, "allow", data->default_access.subscribe) == NULL ){ goto internal_error; @@ -152,7 +152,7 @@ int dynsec__process_get_default_acl_access(cJSON *j_responses, struct mosquitto } cJSON_AddItemToArray(j_acls, j_acl); if(cJSON_AddStringToObject(j_acl, "acltype", ACL_TYPE_UNSUB_GENERIC) == NULL - || cJSON_AddBoolToObject(j_acl, "allow", g_dynsec_data.default_access.unsubscribe) == NULL + || cJSON_AddBoolToObject(j_acl, "allow", data->default_access.unsubscribe) == NULL ){ goto internal_error; diff --git a/plugins/dynamic-security/dynamic_security.h b/plugins/dynamic-security/dynamic_security.h index 95435b80..3bcb9d92 100644 --- a/plugins/dynamic-security/dynamic_security.h +++ b/plugins/dynamic-security/dynamic_security.h @@ -131,6 +131,7 @@ struct dynsec__acl_default_access{ }; struct dynsec__data{ + char *config_file; struct dynsec__client *clients; struct dynsec__group *groups; struct dynsec__role *roles; @@ -138,9 +139,6 @@ struct dynsec__data{ struct dynsec__acl_default_access default_access; }; -extern struct dynsec__data g_dynsec_data; -extern char *g_config_file; - /* ################################################################ * # * # Plugin Functions @@ -148,9 +146,9 @@ extern char *g_config_file; * ################################################################ */ int dynsec__config_init(const char *filename); -void dynsec__config_save(void); -int dynsec__config_load(void); -int dynsec__handle_control(cJSON *j_responses, struct mosquitto *context, cJSON *commands); +void dynsec__config_save(struct dynsec__data *data); +int dynsec__config_load(struct dynsec__data *data); +int dynsec__handle_control(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *commands); void dynsec__command_reply(cJSON *j_responses, struct mosquitto *context, const char *command, const char *error, const char *correlation_data); int dynsec_control_callback(int event, void *event_data, void *userdata); @@ -162,8 +160,8 @@ int dynsec_control_callback(int event, void *event_data, void *userdata); * ################################################################ */ int dynsec__acl_check_callback(int event, void *event_data, void *userdata); -int dynsec__process_set_default_acl_access(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); -int dynsec__process_get_default_acl_access(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); +int dynsec__process_set_default_acl_access(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); +int dynsec__process_get_default_acl_access(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); /* ################################################################ @@ -182,21 +180,21 @@ int dynsec_auth__basic_auth_callback(int event, void *event_data, void *userdata * # * ################################################################ */ -void dynsec_clients__cleanup(void); -int dynsec_clients__config_load(cJSON *tree); -int dynsec_clients__config_save(cJSON *tree); -int dynsec_clients__process_add_role(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); -int dynsec_clients__process_create(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); -int dynsec_clients__process_delete(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); -int dynsec_clients__process_disable(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); -int dynsec_clients__process_enable(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); -int dynsec_clients__process_get(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); -int dynsec_clients__process_list(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); -int dynsec_clients__process_modify(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); -int dynsec_clients__process_remove_role(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); -int dynsec_clients__process_set_id(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); -int dynsec_clients__process_set_password(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); -struct dynsec__client *dynsec_clients__find(const char *username); +void dynsec_clients__cleanup(struct dynsec__data *data); +int dynsec_clients__config_load(struct dynsec__data *data, cJSON *tree); +int dynsec_clients__config_save(struct dynsec__data *data, cJSON *tree); +int dynsec_clients__process_add_role(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); +int dynsec_clients__process_create(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); +int dynsec_clients__process_delete(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); +int dynsec_clients__process_disable(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); +int dynsec_clients__process_enable(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); +int dynsec_clients__process_get(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); +int dynsec_clients__process_list(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); +int dynsec_clients__process_modify(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); +int dynsec_clients__process_remove_role(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); +int dynsec_clients__process_set_id(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); +int dynsec_clients__process_set_password(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); +struct dynsec__client *dynsec_clients__find(struct dynsec__data *data, const char *username); /* ################################################################ @@ -218,23 +216,23 @@ void dynsec_clientlist__kick_all(struct dynsec__clientlist *base_clientlist); * # * ################################################################ */ -void dynsec_groups__cleanup(void); -int dynsec_groups__config_load(cJSON *tree); -int dynsec_groups__add_client(const char *username, const char *groupname, int priority, bool update_config); -int dynsec_groups__config_save(cJSON *tree); -int dynsec_groups__process_add_client(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); -int dynsec_groups__process_add_role(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); -int dynsec_groups__process_create(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); -int dynsec_groups__process_delete(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); -int dynsec_groups__process_get(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); -int dynsec_groups__process_list(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); -int dynsec_groups__process_modify(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); -int dynsec_groups__process_remove_client(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); -int dynsec_groups__process_remove_role(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); -int dynsec_groups__process_get_anonymous_group(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); -int dynsec_groups__process_set_anonymous_group(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); -int dynsec_groups__remove_client(const char *username, const char *groupname, bool update_config); -struct dynsec__group *dynsec_groups__find(const char *groupname); +void dynsec_groups__cleanup(struct dynsec__data *data); +int dynsec_groups__config_load(struct dynsec__data *data, cJSON *tree); +int dynsec_groups__add_client(struct dynsec__data *data, const char *username, const char *groupname, int priority, bool update_config); +int dynsec_groups__config_save(struct dynsec__data *data, cJSON *tree); +int dynsec_groups__process_add_client(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); +int dynsec_groups__process_add_role(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); +int dynsec_groups__process_create(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); +int dynsec_groups__process_delete(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); +int dynsec_groups__process_get(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); +int dynsec_groups__process_list(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); +int dynsec_groups__process_modify(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); +int dynsec_groups__process_remove_client(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); +int dynsec_groups__process_remove_role(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); +int dynsec_groups__process_get_anonymous_group(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); +int dynsec_groups__process_set_anonymous_group(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); +int dynsec_groups__remove_client(struct dynsec__data *data, const char *username, const char *groupname, bool update_config); +struct dynsec__group *dynsec_groups__find(struct dynsec__data *data, const char *groupname); /* ################################################################ @@ -255,17 +253,17 @@ void dynsec_grouplist__remove(struct dynsec__grouplist **base_grouplist, struct * # * ################################################################ */ -void dynsec_roles__cleanup(void); -int dynsec_roles__config_load(cJSON *tree); -int dynsec_roles__config_save(cJSON *tree); -int dynsec_roles__process_add_acl(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); -int dynsec_roles__process_create(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); -int dynsec_roles__process_delete(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); -int dynsec_roles__process_get(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); -int dynsec_roles__process_list(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); -int dynsec_roles__process_modify(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); -int dynsec_roles__process_remove_acl(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); -struct dynsec__role *dynsec_roles__find(const char *rolename); +void dynsec_roles__cleanup(struct dynsec__data *data); +int dynsec_roles__config_load(struct dynsec__data *data, cJSON *tree); +int dynsec_roles__config_save(struct dynsec__data *data, cJSON *tree); +int dynsec_roles__process_add_acl(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); +int dynsec_roles__process_create(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); +int dynsec_roles__process_delete(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); +int dynsec_roles__process_get(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); +int dynsec_roles__process_list(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); +int dynsec_roles__process_modify(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); +int dynsec_roles__process_remove_acl(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data); +struct dynsec__role *dynsec_roles__find(struct dynsec__data *data, const char *rolename); /* ################################################################ @@ -278,7 +276,7 @@ int dynsec_rolelist__client_add(struct dynsec__client *client, struct dynsec__ro int dynsec_rolelist__client_remove(struct dynsec__client *client, struct dynsec__role *role); int dynsec_rolelist__group_add(struct dynsec__group *group, struct dynsec__role *role, int priority); void dynsec_rolelist__group_remove(struct dynsec__group *group, struct dynsec__role *role); -int dynsec_rolelist__load_from_json(cJSON *command, struct dynsec__rolelist **rolelist); +int dynsec_rolelist__load_from_json(struct dynsec__data *data, cJSON *command, struct dynsec__rolelist **rolelist); void dynsec_rolelist__cleanup(struct dynsec__rolelist **base_rolelist); cJSON *dynsec_rolelist__all_to_json(struct dynsec__rolelist *base_rolelist); diff --git a/plugins/dynamic-security/groups.c b/plugins/dynamic-security/groups.c index dd45615d..ab06e5cb 100644 --- a/plugins/dynamic-security/groups.c +++ b/plugins/dynamic-security/groups.c @@ -57,9 +57,9 @@ static cJSON *add_group_to_json(struct dynsec__group *group); * # * ################################################################ */ -static void group__kick_all(struct dynsec__group *group) +static void group__kick_all(struct dynsec__data *data, struct dynsec__group *group) { - if(group == g_dynsec_data.anonymous_group){ + if(group == data->anonymous_group){ mosquitto_kick_client_by_username(NULL, false); } dynsec_clientlist__kick_all(group->clientlist); @@ -75,25 +75,25 @@ static int group_cmp(void *a, void *b) } -struct dynsec__group *dynsec_groups__find(const char *groupname) +struct dynsec__group *dynsec_groups__find(struct dynsec__data *data, const char *groupname) { struct dynsec__group *group = NULL; if(groupname){ - HASH_FIND(hh, g_dynsec_data.groups, groupname, strlen(groupname), group); + HASH_FIND(hh, data->groups, groupname, strlen(groupname), group); } return group; } -static void group__free_item(struct dynsec__group *group) +static void group__free_item(struct dynsec__data *data, struct dynsec__group *group) { struct dynsec__group *found_group = NULL; if(group == NULL) return; - found_group = dynsec_groups__find(group->groupname); + found_group = dynsec_groups__find(data, group->groupname); if(found_group){ - HASH_DEL(g_dynsec_data.groups, found_group); + HASH_DEL(data->groups, found_group); } dynsec__remove_all_clients_from_group(group); mosquitto_free(group->text_name); @@ -103,7 +103,7 @@ static void group__free_item(struct dynsec__group *group) mosquitto_free(group); } -int dynsec_groups__process_add_role(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) +int dynsec_groups__process_add_role(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) { char *groupname, *rolename; struct dynsec__group *group; @@ -131,13 +131,13 @@ int dynsec_groups__process_add_role(cJSON *j_responses, struct mosquitto *contex } json_get_int(command, "priority", &priority, true, -1); - group = dynsec_groups__find(groupname); + group = dynsec_groups__find(data, groupname); if(group == NULL){ dynsec__command_reply(j_responses, context, "addGroupRole", "Group not found", correlation_data); return MOSQ_ERR_SUCCESS; } - role = dynsec_roles__find(rolename); + role = dynsec_roles__find(data, rolename); if(role == NULL){ dynsec__command_reply(j_responses, context, "addGroupRole", "Role not found", correlation_data); return MOSQ_ERR_SUCCESS; @@ -160,22 +160,22 @@ int dynsec_groups__process_add_role(cJSON *j_responses, struct mosquitto *contex mosquitto_log_printf(MOSQ_LOG_INFO, "dynsec: %s/%s | addGroupRole | groupname=%s | rolename=%s | priority=%d", admin_clientid, admin_username, groupname, rolename, priority); - dynsec__config_save(); + dynsec__config_save(data); dynsec__command_reply(j_responses, context, "addGroupRole", NULL, correlation_data); /* Enforce any changes */ - group__kick_all(group); + group__kick_all(data, group); return MOSQ_ERR_SUCCESS; } -void dynsec_groups__cleanup(void) +void dynsec_groups__cleanup(struct dynsec__data *data) { struct dynsec__group *group, *group_tmp = NULL; - HASH_ITER(hh, g_dynsec_data.groups, group, group_tmp){ - group__free_item(group); + HASH_ITER(hh, data->groups, group, group_tmp){ + group__free_item(data, group); } } @@ -186,7 +186,7 @@ void dynsec_groups__cleanup(void) * # * ################################################################ */ -int dynsec_groups__config_load(cJSON *tree) +int dynsec_groups__config_load(struct dynsec__data *data, cJSON *tree) { cJSON *j_groups, *j_group; cJSON *j_clientlist, *j_client, *j_username; @@ -257,7 +257,7 @@ int dynsec_groups__config_load(cJSON *tree) j_rolename = cJSON_GetObjectItem(j_role, "rolename"); if(j_rolename && cJSON_IsString(j_rolename)){ json_get_int(j_role, "priority", &priority, true, -1); - role = dynsec_roles__find(j_rolename->valuestring); + role = dynsec_roles__find(data, j_rolename->valuestring); dynsec_rolelist__group_add(group, role, priority); } } @@ -265,7 +265,7 @@ int dynsec_groups__config_load(cJSON *tree) } /* This must go before clients are loaded, otherwise the group won't be found */ - HASH_ADD_KEYPTR(hh, g_dynsec_data.groups, group->groupname, strlen(group->groupname), group); + HASH_ADD_KEYPTR(hh, data->groups, group->groupname, strlen(group->groupname), group); /* Clients */ j_clientlist = cJSON_GetObjectItem(j_group, "clients"); @@ -275,18 +275,18 @@ int dynsec_groups__config_load(cJSON *tree) j_username = cJSON_GetObjectItem(j_client, "username"); if(j_username && cJSON_IsString(j_username)){ json_get_int(j_client, "priority", &priority, true, -1); - dynsec_groups__add_client(j_username->valuestring, group->groupname, priority, false); + dynsec_groups__add_client(data, j_username->valuestring, group->groupname, priority, false); } } } } } } - HASH_SORT(g_dynsec_data.groups, group_cmp); + HASH_SORT(data->groups, group_cmp); j_group = cJSON_GetObjectItem(tree, "anonymousGroup"); if(j_group && cJSON_IsString(j_group)){ - g_dynsec_data.anonymous_group = dynsec_groups__find(j_group->valuestring); + data->anonymous_group = dynsec_groups__find(data, j_group->valuestring); } return 0; @@ -300,12 +300,12 @@ int dynsec_groups__config_load(cJSON *tree) * ################################################################ */ -static int dynsec__config_add_groups(cJSON *j_groups) +static int dynsec__config_add_groups(struct dynsec__data *data, cJSON *j_groups) { struct dynsec__group *group, *group_tmp = NULL; cJSON *j_group, *j_clients, *j_roles; - HASH_ITER(hh, g_dynsec_data.groups, group, group_tmp){ + HASH_ITER(hh, data->groups, group, group_tmp){ j_group = cJSON_CreateObject(); if(j_group == NULL) return 1; cJSON_AddItemToArray(j_groups, j_group); @@ -335,7 +335,7 @@ static int dynsec__config_add_groups(cJSON *j_groups) } -int dynsec_groups__config_save(cJSON *tree) +int dynsec_groups__config_save(struct dynsec__data *data, cJSON *tree) { cJSON *j_groups; @@ -344,12 +344,12 @@ int dynsec_groups__config_save(cJSON *tree) return 1; } cJSON_AddItemToObject(tree, "groups", j_groups); - if(dynsec__config_add_groups(j_groups)){ + if(dynsec__config_add_groups(data, j_groups)){ return 1; } - if(g_dynsec_data.anonymous_group - && cJSON_AddStringToObject(tree, "anonymousGroup", g_dynsec_data.anonymous_group->groupname) == NULL){ + if(data->anonymous_group + && cJSON_AddStringToObject(tree, "anonymousGroup", data->anonymous_group->groupname) == NULL){ return 1; } @@ -358,7 +358,7 @@ int dynsec_groups__config_save(cJSON *tree) } -int dynsec_groups__process_create(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) +int dynsec_groups__process_create(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) { char *groupname, *text_name, *text_description; struct dynsec__group *group = NULL; @@ -384,7 +384,7 @@ int dynsec_groups__process_create(cJSON *j_responses, struct mosquitto *context, return MOSQ_ERR_INVAL; } - group = dynsec_groups__find(groupname); + group = dynsec_groups__find(data, groupname); if(group){ dynsec__command_reply(j_responses, context, "createGroup", "Group already exists", correlation_data); return MOSQ_ERR_SUCCESS; @@ -398,14 +398,14 @@ int dynsec_groups__process_create(cJSON *j_responses, struct mosquitto *context, group->groupname = strdup(groupname); if(group->groupname == NULL){ dynsec__command_reply(j_responses, context, "createGroup", "Internal error", correlation_data); - group__free_item(group); + group__free_item(data, group); return MOSQ_ERR_NOMEM; } if(text_name){ group->text_name = strdup(text_name); if(group->text_name == NULL){ dynsec__command_reply(j_responses, context, "createGroup", "Internal error", correlation_data); - group__free_item(group); + group__free_item(data, group); return MOSQ_ERR_NOMEM; } } @@ -413,37 +413,37 @@ int dynsec_groups__process_create(cJSON *j_responses, struct mosquitto *context, group->text_description = strdup(text_description); if(group->text_description == NULL){ dynsec__command_reply(j_responses, context, "createGroup", "Internal error", correlation_data); - group__free_item(group); + group__free_item(data, group); return MOSQ_ERR_NOMEM; } } - rc = dynsec_rolelist__load_from_json(command, &group->rolelist); + rc = dynsec_rolelist__load_from_json(data, command, &group->rolelist); if(rc == MOSQ_ERR_SUCCESS || rc == ERR_LIST_NOT_FOUND){ }else if(rc == MOSQ_ERR_NOT_FOUND){ dynsec__command_reply(j_responses, context, "createGroup", "Role not found", correlation_data); - group__free_item(group); + group__free_item(data, group); return MOSQ_ERR_INVAL; }else{ dynsec__command_reply(j_responses, context, "createGroup", "Internal error", correlation_data); - group__free_item(group); + group__free_item(data, group); return MOSQ_ERR_INVAL; } - HASH_ADD_KEYPTR_INORDER(hh, g_dynsec_data.groups, group->groupname, strlen(group->groupname), group, group_cmp); + HASH_ADD_KEYPTR_INORDER(hh, data->groups, group->groupname, strlen(group->groupname), group, group_cmp); admin_clientid = mosquitto_client_id(context); admin_username = mosquitto_client_username(context); mosquitto_log_printf(MOSQ_LOG_INFO, "dynsec: %s/%s | createGroup | groupname=%s", admin_clientid, admin_username, groupname); - dynsec__config_save(); + dynsec__config_save(data); dynsec__command_reply(j_responses, context, "createGroup", NULL, correlation_data); return MOSQ_ERR_SUCCESS; } -int dynsec_groups__process_delete(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) +int dynsec_groups__process_delete(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) { char *groupname; struct dynsec__group *group; @@ -458,14 +458,14 @@ int dynsec_groups__process_delete(cJSON *j_responses, struct mosquitto *context, return MOSQ_ERR_INVAL; } - group = dynsec_groups__find(groupname); + group = dynsec_groups__find(data, groupname); if(group){ /* Enforce any changes */ - group__kick_all(group); + group__kick_all(data, group); dynsec__remove_all_roles_from_group(group); - group__free_item(group); - dynsec__config_save(); + group__free_item(data, group); + dynsec__config_save(data); dynsec__command_reply(j_responses, context, "deleteGroup", NULL, correlation_data); admin_clientid = mosquitto_client_id(context); @@ -481,19 +481,19 @@ int dynsec_groups__process_delete(cJSON *j_responses, struct mosquitto *context, } -int dynsec_groups__add_client(const char *username, const char *groupname, int priority, bool update_config) +int dynsec_groups__add_client(struct dynsec__data *data, const char *username, const char *groupname, int priority, bool update_config) { struct dynsec__client *client; struct dynsec__clientlist *clientlist; struct dynsec__group *group; int rc; - client = dynsec_clients__find(username); + client = dynsec_clients__find(data, username); if(client == NULL){ return ERR_USER_NOT_FOUND; } - group = dynsec_groups__find(groupname); + group = dynsec_groups__find(data, groupname); if(group == NULL){ return ERR_GROUP_NOT_FOUND; } @@ -515,14 +515,14 @@ int dynsec_groups__add_client(const char *username, const char *groupname, int p } if(update_config){ - dynsec__config_save(); + dynsec__config_save(data); } return MOSQ_ERR_SUCCESS; } -int dynsec_groups__process_add_client(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) +int dynsec_groups__process_add_client(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) { char *username, *groupname; int rc; @@ -549,7 +549,7 @@ int dynsec_groups__process_add_client(cJSON *j_responses, struct mosquitto *cont json_get_int(command, "priority", &priority, true, -1); - rc = dynsec_groups__add_client(username, groupname, priority, true); + rc = dynsec_groups__add_client(data, username, groupname, priority, true); if(rc == MOSQ_ERR_SUCCESS){ admin_clientid = mosquitto_client_id(context); admin_username = mosquitto_client_username(context); @@ -600,17 +600,17 @@ static int dynsec__remove_all_roles_from_group(struct dynsec__group *group) return MOSQ_ERR_SUCCESS; } -int dynsec_groups__remove_client(const char *username, const char *groupname, bool update_config) +int dynsec_groups__remove_client(struct dynsec__data *data, const char *username, const char *groupname, bool update_config) { struct dynsec__client *client; struct dynsec__group *group; - client = dynsec_clients__find(username); + client = dynsec_clients__find(data, username); if(client == NULL){ return ERR_USER_NOT_FOUND; } - group = dynsec_groups__find(groupname); + group = dynsec_groups__find(data, groupname); if(group == NULL){ return ERR_GROUP_NOT_FOUND; } @@ -619,12 +619,12 @@ int dynsec_groups__remove_client(const char *username, const char *groupname, bo dynsec_grouplist__remove(&client->grouplist, group); if(update_config){ - dynsec__config_save(); + dynsec__config_save(data); } return MOSQ_ERR_SUCCESS; } -int dynsec_groups__process_remove_client(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) +int dynsec_groups__process_remove_client(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) { char *username, *groupname; int rc; @@ -648,7 +648,7 @@ int dynsec_groups__process_remove_client(cJSON *j_responses, struct mosquitto *c return MOSQ_ERR_INVAL; } - rc = dynsec_groups__remove_client(username, groupname, true); + rc = dynsec_groups__remove_client(data, username, groupname, true); if(rc == MOSQ_ERR_SUCCESS){ admin_clientid = mosquitto_client_id(context); admin_username = mosquitto_client_username(context); @@ -718,7 +718,7 @@ static cJSON *add_group_to_json(struct dynsec__group *group) } -int dynsec_groups__process_list(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) +int dynsec_groups__process_list(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) { bool verbose; cJSON *tree, *j_groups, *j_group, *j_data; @@ -738,7 +738,7 @@ int dynsec_groups__process_list(cJSON *j_responses, struct mosquitto *context, c if(cJSON_AddStringToObject(tree, "command", "listGroups") == NULL || (j_data = cJSON_AddObjectToObject(tree, "data")) == NULL - || cJSON_AddIntToObject(j_data, "totalCount", (int)HASH_CNT(hh, g_dynsec_data.groups)) == NULL + || cJSON_AddIntToObject(j_data, "totalCount", (int)HASH_CNT(hh, data->groups)) == NULL || (j_groups = cJSON_AddArrayToObject(j_data, "groups")) == NULL || (correlation_data && cJSON_AddStringToObject(tree, "correlationData", correlation_data) == NULL) ){ @@ -749,7 +749,7 @@ int dynsec_groups__process_list(cJSON *j_responses, struct mosquitto *context, c } i = 0; - HASH_ITER(hh, g_dynsec_data.groups, group, group_tmp){ + HASH_ITER(hh, data->groups, group, group_tmp){ if(i>=offset){ if(verbose){ j_group = add_group_to_json(group); @@ -792,7 +792,7 @@ int dynsec_groups__process_list(cJSON *j_responses, struct mosquitto *context, c } -int dynsec_groups__process_get(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) +int dynsec_groups__process_get(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) { char *groupname; cJSON *tree, *j_group, *j_data; @@ -824,7 +824,7 @@ int dynsec_groups__process_get(cJSON *j_responses, struct mosquitto *context, cJ return MOSQ_ERR_NOMEM; } - group = dynsec_groups__find(groupname); + group = dynsec_groups__find(data, groupname); if(group){ j_group = add_group_to_json(group); if(j_group == NULL){ @@ -850,7 +850,7 @@ int dynsec_groups__process_get(cJSON *j_responses, struct mosquitto *context, cJ } -int dynsec_groups__process_remove_role(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) +int dynsec_groups__process_remove_role(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) { char *groupname, *rolename; struct dynsec__group *group; @@ -875,24 +875,24 @@ int dynsec_groups__process_remove_role(cJSON *j_responses, struct mosquitto *con return MOSQ_ERR_INVAL; } - group = dynsec_groups__find(groupname); + group = dynsec_groups__find(data, groupname); if(group == NULL){ dynsec__command_reply(j_responses, context, "removeGroupRole", "Group not found", correlation_data); return MOSQ_ERR_SUCCESS; } - role = dynsec_roles__find(rolename); + role = dynsec_roles__find(data, rolename); if(role == NULL){ dynsec__command_reply(j_responses, context, "removeGroupRole", "Role not found", correlation_data); return MOSQ_ERR_SUCCESS; } dynsec_rolelist__group_remove(group, role); - dynsec__config_save(); + dynsec__config_save(data); dynsec__command_reply(j_responses, context, "removeGroupRole", NULL, correlation_data); /* Enforce any changes */ - group__kick_all(group); + group__kick_all(data, group); admin_clientid = mosquitto_client_id(context); admin_username = mosquitto_client_username(context); @@ -903,7 +903,7 @@ int dynsec_groups__process_remove_role(cJSON *j_responses, struct mosquitto *con } -int dynsec_groups__process_modify(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) +int dynsec_groups__process_modify(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) { char *groupname; char *text_name, *text_description; @@ -924,7 +924,7 @@ int dynsec_groups__process_modify(cJSON *j_responses, struct mosquitto *context, return MOSQ_ERR_INVAL; } - group = dynsec_groups__find(groupname); + group = dynsec_groups__find(data, groupname); if(group == NULL){ dynsec__command_reply(j_responses, context, "modifyGroup", "Group not found", correlation_data); return MOSQ_ERR_INVAL; @@ -950,7 +950,7 @@ int dynsec_groups__process_modify(cJSON *j_responses, struct mosquitto *context, group->text_description = str; } - rc = dynsec_rolelist__load_from_json(command, &rolelist); + rc = dynsec_rolelist__load_from_json(data, command, &rolelist); if(rc == MOSQ_ERR_SUCCESS){ dynsec_rolelist__cleanup(&group->rolelist); group->rolelist = rolelist; @@ -959,7 +959,7 @@ int dynsec_groups__process_modify(cJSON *j_responses, struct mosquitto *context, }else if(rc == MOSQ_ERR_NOT_FOUND){ dynsec__command_reply(j_responses, context, "modifyGroup", "Role not found", correlation_data); dynsec_rolelist__cleanup(&rolelist); - group__kick_all(group); + group__kick_all(data, group); return MOSQ_ERR_INVAL; }else{ if(rc == MOSQ_ERR_INVAL){ @@ -968,7 +968,7 @@ int dynsec_groups__process_modify(cJSON *j_responses, struct mosquitto *context, dynsec__command_reply(j_responses, context, "modifyGroup", "Internal error", correlation_data); } dynsec_rolelist__cleanup(&rolelist); - group__kick_all(group); + group__kick_all(data, group); return MOSQ_ERR_INVAL; } @@ -981,18 +981,18 @@ int dynsec_groups__process_modify(cJSON *j_responses, struct mosquitto *context, jtmp = cJSON_GetObjectItem(j_client, "username"); if(jtmp && cJSON_IsString(jtmp)){ json_get_int(j_client, "priority", &priority, true, -1); - dynsec_groups__add_client(jtmp->valuestring, groupname, priority, false); + dynsec_groups__add_client(data, jtmp->valuestring, groupname, priority, false); } } } } - dynsec__config_save(); + dynsec__config_save(data); dynsec__command_reply(j_responses, context, "modifyGroup", NULL, correlation_data); /* Enforce any changes */ - group__kick_all(group); + group__kick_all(data, group); admin_clientid = mosquitto_client_id(context); admin_username = mosquitto_client_username(context); @@ -1003,7 +1003,7 @@ int dynsec_groups__process_modify(cJSON *j_responses, struct mosquitto *context, } -int dynsec_groups__process_set_anonymous_group(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) +int dynsec_groups__process_set_anonymous_group(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) { char *groupname; struct dynsec__group *group = NULL; @@ -1018,15 +1018,15 @@ int dynsec_groups__process_set_anonymous_group(cJSON *j_responses, struct mosqui return MOSQ_ERR_INVAL; } - group = dynsec_groups__find(groupname); + group = dynsec_groups__find(data, groupname); if(group == NULL){ dynsec__command_reply(j_responses, context, "setAnonymousGroup", "Group not found", correlation_data); return MOSQ_ERR_SUCCESS; } - g_dynsec_data.anonymous_group = group; + data->anonymous_group = group; - dynsec__config_save(); + dynsec__config_save(data); dynsec__command_reply(j_responses, context, "setAnonymousGroup", NULL, correlation_data); /* Enforce any changes */ @@ -1040,7 +1040,7 @@ int dynsec_groups__process_set_anonymous_group(cJSON *j_responses, struct mosqui return MOSQ_ERR_SUCCESS; } -int dynsec_groups__process_get_anonymous_group(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) +int dynsec_groups__process_get_anonymous_group(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) { cJSON *tree, *j_data, *j_group; const char *groupname; @@ -1054,8 +1054,8 @@ int dynsec_groups__process_get_anonymous_group(cJSON *j_responses, struct mosqui return MOSQ_ERR_NOMEM; } - if(g_dynsec_data.anonymous_group){ - groupname = g_dynsec_data.anonymous_group->groupname; + if(data->anonymous_group){ + groupname = data->anonymous_group->groupname; }else{ groupname = ""; } diff --git a/plugins/dynamic-security/plugin.c b/plugins/dynamic-security/plugin.c index db85a884..70e504ea 100644 --- a/plugins/dynamic-security/plugin.c +++ b/plugins/dynamic-security/plugin.c @@ -33,9 +33,8 @@ Contributors: MOSQUITTO_PLUGIN_DECLARE_VERSION(5); -struct dynsec__data g_dynsec_data; +static struct dynsec__data dynsec_data; static mosquitto_plugin_id_t *plg_id = NULL; -char *g_config_file = NULL; int mosquitto_plugin_init(mosquitto_plugin_id_t *identifier, void **user_data, struct mosquitto_opt *options, int option_count) { @@ -43,18 +42,18 @@ int mosquitto_plugin_init(mosquitto_plugin_id_t *identifier, void **user_data, s UNUSED(user_data); - memset(&g_dynsec_data, 0, sizeof(struct dynsec__data)); + memset(&dynsec_data, 0, sizeof(struct dynsec__data)); for(i=0; ivaluestring); + role = dynsec_roles__find(data, j_rolename->valuestring); if(role){ dynsec_rolelist__add(rolelist, role, priority); }else{ diff --git a/plugins/dynamic-security/roles.c b/plugins/dynamic-security/roles.c index 7de8fd5d..e19d3d45 100644 --- a/plugins/dynamic-security/roles.c +++ b/plugins/dynamic-security/roles.c @@ -64,10 +64,10 @@ static void role__free_all_acls(struct dynsec__acl **acl) } } -static void role__free_item(struct dynsec__role *role, bool remove_from_hash) +static void role__free_item(struct dynsec__data *data, struct dynsec__role *role, bool remove_from_hash) { if(remove_from_hash){ - HASH_DEL(g_dynsec_data.roles, role); + HASH_DEL(data->roles, role); } dynsec_clientlist__cleanup(&role->clientlist); dynsec_grouplist__cleanup(&role->grouplist); @@ -83,35 +83,35 @@ static void role__free_item(struct dynsec__role *role, bool remove_from_hash) mosquitto_free(role); } -struct dynsec__role *dynsec_roles__find(const char *rolename) +struct dynsec__role *dynsec_roles__find(struct dynsec__data *data, const char *rolename) { struct dynsec__role *role = NULL; if(rolename){ - HASH_FIND(hh, g_dynsec_data.roles, rolename, strlen(rolename), role); + HASH_FIND(hh, data->roles, rolename, strlen(rolename), role); } return role; } -void dynsec_roles__cleanup(void) +void dynsec_roles__cleanup(struct dynsec__data *data) { struct dynsec__role *role, *role_tmp = NULL; - HASH_ITER(hh, g_dynsec_data.roles, role, role_tmp){ - role__free_item(role, true); + HASH_ITER(hh, data->roles, role, role_tmp){ + role__free_item(data, role, true); } } -static void role__kick_all(struct dynsec__role *role) +static void role__kick_all(struct dynsec__data *data, struct dynsec__role *role) { struct dynsec__grouplist *grouplist, *grouplist_tmp = NULL; dynsec_clientlist__kick_all(role->clientlist); HASH_ITER(hh, role->grouplist, grouplist, grouplist_tmp){ - if(grouplist->group == g_dynsec_data.anonymous_group){ + if(grouplist->group == data->anonymous_group){ mosquitto_kick_client_by_username(NULL, false); } dynsec_clientlist__kick_all(grouplist->group->clientlist); @@ -173,7 +173,7 @@ static int add_acls_to_json(cJSON *j_role, struct dynsec__role *role) return 0; } -int dynsec_roles__config_save(cJSON *tree) +int dynsec_roles__config_save(struct dynsec__data *data, cJSON *tree) { cJSON *j_roles, *j_role; struct dynsec__role *role, *role_tmp = NULL; @@ -182,7 +182,7 @@ int dynsec_roles__config_save(cJSON *tree) return 1; } - HASH_ITER(hh, g_dynsec_data.roles, role, role_tmp){ + HASH_ITER(hh, data->roles, role, role_tmp){ j_role = add_role_to_json(role, true); if(j_role == NULL){ return 1; @@ -240,7 +240,7 @@ static int dynsec_roles__acl_load(cJSON *j_acls, const char *key, struct dynsec_ } -int dynsec_roles__config_load(cJSON *tree) +int dynsec_roles__config_load(struct dynsec__data *data, cJSON *tree) { cJSON *j_roles, *j_role, *jtmp, *j_acls; struct dynsec__role *role; @@ -321,16 +321,16 @@ int dynsec_roles__config_load(cJSON *tree) } } - HASH_ADD_KEYPTR(hh, g_dynsec_data.roles, role->rolename, strlen(role->rolename), role); + HASH_ADD_KEYPTR(hh, data->roles, role->rolename, strlen(role->rolename), role); } } - HASH_SORT(g_dynsec_data.roles, role_cmp); + HASH_SORT(data->roles, role_cmp); return 0; } -int dynsec_roles__process_create(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) +int dynsec_roles__process_create(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) { char *rolename; char *text_name, *text_description; @@ -364,7 +364,7 @@ int dynsec_roles__process_create(cJSON *j_responses, struct mosquitto *context, return MOSQ_ERR_INVAL; } - role = dynsec_roles__find(rolename); + role = dynsec_roles__find(data, rolename); if(role){ dynsec__command_reply(j_responses, context, "createRole", "Role already exists", correlation_data); return MOSQ_ERR_SUCCESS; @@ -417,9 +417,9 @@ int dynsec_roles__process_create(cJSON *j_responses, struct mosquitto *context, } - HASH_ADD_KEYPTR_INORDER(hh, g_dynsec_data.roles, role->rolename, strlen(role->rolename), role, role_cmp); + HASH_ADD_KEYPTR_INORDER(hh, data->roles, role->rolename, strlen(role->rolename), role, role_cmp); - dynsec__config_save(); + dynsec__config_save(data); dynsec__command_reply(j_responses, context, "createRole", NULL, correlation_data); @@ -431,7 +431,7 @@ int dynsec_roles__process_create(cJSON *j_responses, struct mosquitto *context, return MOSQ_ERR_SUCCESS; error: if(role){ - role__free_item(role, false); + role__free_item(data, role, false); } return rc; } @@ -448,12 +448,12 @@ static void role__remove_all_clients(struct dynsec__role *role) } } -static void role__remove_all_groups(struct dynsec__role *role) +static void role__remove_all_groups(struct dynsec__data *data, struct dynsec__role *role) { struct dynsec__grouplist *grouplist, *grouplist_tmp = NULL; HASH_ITER(hh, role->grouplist, grouplist, grouplist_tmp){ - if(grouplist->group == g_dynsec_data.anonymous_group){ + if(grouplist->group == data->anonymous_group){ mosquitto_kick_client_by_username(NULL, false); } dynsec_clientlist__kick_all(grouplist->group->clientlist); @@ -462,7 +462,7 @@ static void role__remove_all_groups(struct dynsec__role *role) } } -int dynsec_roles__process_delete(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) +int dynsec_roles__process_delete(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) { char *rolename; struct dynsec__role *role; @@ -477,12 +477,12 @@ int dynsec_roles__process_delete(cJSON *j_responses, struct mosquitto *context, return MOSQ_ERR_INVAL; } - role = dynsec_roles__find(rolename); + role = dynsec_roles__find(data, rolename); if(role){ role__remove_all_clients(role); - role__remove_all_groups(role); - role__free_item(role, true); - dynsec__config_save(); + role__remove_all_groups(data, role); + role__free_item(data, role, true); + dynsec__config_save(data); dynsec__command_reply(j_responses, context, "deleteRole", NULL, correlation_data); admin_clientid = mosquitto_client_id(context); @@ -530,7 +530,7 @@ static cJSON *add_role_to_json(struct dynsec__role *role, bool verbose) return j_role; } -int dynsec_roles__process_list(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) +int dynsec_roles__process_list(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) { bool verbose; struct dynsec__role *role, *role_tmp = NULL; @@ -550,7 +550,7 @@ int dynsec_roles__process_list(cJSON *j_responses, struct mosquitto *context, cJ if(cJSON_AddStringToObject(tree, "command", "listRoles") == NULL || (j_data = cJSON_AddObjectToObject(tree, "data")) == NULL - || cJSON_AddIntToObject(j_data, "totalCount", (int)HASH_CNT(hh, g_dynsec_data.roles)) == NULL + || cJSON_AddIntToObject(j_data, "totalCount", (int)HASH_CNT(hh, data->roles)) == NULL || (j_roles = cJSON_AddArrayToObject(j_data, "roles")) == NULL || (correlation_data && cJSON_AddStringToObject(tree, "correlationData", correlation_data) == NULL) ){ @@ -561,7 +561,7 @@ int dynsec_roles__process_list(cJSON *j_responses, struct mosquitto *context, cJ } i = 0; - HASH_ITER(hh, g_dynsec_data.roles, role, role_tmp){ + HASH_ITER(hh, data->roles, role, role_tmp){ if(i>=offset){ j_role = add_role_to_json(role, verbose); if(j_role == NULL){ @@ -592,7 +592,7 @@ int dynsec_roles__process_list(cJSON *j_responses, struct mosquitto *context, cJ } -int dynsec_roles__process_add_acl(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) +int dynsec_roles__process_add_acl(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) { char *rolename; char *topic; @@ -611,7 +611,7 @@ int dynsec_roles__process_add_acl(cJSON *j_responses, struct mosquitto *context, return MOSQ_ERR_INVAL; } - role = dynsec_roles__find(rolename); + role = dynsec_roles__find(data, rolename); if(role == NULL){ dynsec__command_reply(j_responses, context, "addRoleACL", "Role not found", correlation_data); return MOSQ_ERR_SUCCESS; @@ -679,10 +679,10 @@ int dynsec_roles__process_add_acl(cJSON *j_responses, struct mosquitto *context, json_get_bool(command, "allow", &acl->allow, true, false); HASH_ADD_KEYPTR_INORDER(hh, *acllist, acl->topic, strlen(acl->topic), acl, insert_acl_cmp); - dynsec__config_save(); + dynsec__config_save(data); dynsec__command_reply(j_responses, context, "addRoleACL", NULL, correlation_data); - role__kick_all(role); + role__kick_all(data, role); admin_clientid = mosquitto_client_id(context); admin_username = mosquitto_client_username(context); @@ -693,7 +693,7 @@ int dynsec_roles__process_add_acl(cJSON *j_responses, struct mosquitto *context, } -int dynsec_roles__process_remove_acl(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) +int dynsec_roles__process_remove_acl(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) { char *rolename; struct dynsec__role *role; @@ -712,7 +712,7 @@ int dynsec_roles__process_remove_acl(cJSON *j_responses, struct mosquitto *conte return MOSQ_ERR_INVAL; } - role = dynsec_roles__find(rolename); + role = dynsec_roles__find(data, rolename); if(role == NULL){ dynsec__command_reply(j_responses, context, "removeRoleACL", "Role not found", correlation_data); return MOSQ_ERR_SUCCESS; @@ -757,10 +757,10 @@ int dynsec_roles__process_remove_acl(cJSON *j_responses, struct mosquitto *conte HASH_FIND(hh, *acllist, topic, strlen(topic), acl); if(acl){ role__free_acl(acllist, acl); - dynsec__config_save(); + dynsec__config_save(data); dynsec__command_reply(j_responses, context, "removeRoleACL", NULL, correlation_data); - role__kick_all(role); + role__kick_all(data, role); admin_clientid = mosquitto_client_id(context); admin_username = mosquitto_client_username(context); @@ -775,7 +775,7 @@ int dynsec_roles__process_remove_acl(cJSON *j_responses, struct mosquitto *conte } -int dynsec_roles__process_get(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) +int dynsec_roles__process_get(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) { char *rolename; struct dynsec__role *role; @@ -790,7 +790,7 @@ int dynsec_roles__process_get(cJSON *j_responses, struct mosquitto *context, cJS return MOSQ_ERR_INVAL; } - role = dynsec_roles__find(rolename); + role = dynsec_roles__find(data, rolename); if(role == NULL){ dynsec__command_reply(j_responses, context, "getRole", "Role not found", correlation_data); return MOSQ_ERR_SUCCESS; @@ -825,7 +825,7 @@ int dynsec_roles__process_get(cJSON *j_responses, struct mosquitto *context, cJS } -int dynsec_roles__process_modify(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) +int dynsec_roles__process_modify(struct dynsec__data *data, cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data) { char *rolename; char *text_name, *text_description; @@ -848,7 +848,7 @@ int dynsec_roles__process_modify(cJSON *j_responses, struct mosquitto *context, return MOSQ_ERR_INVAL; } - role = dynsec_roles__find(rolename); + role = dynsec_roles__find(data, rolename); if(role == NULL){ dynsec__command_reply(j_responses, context, "modifyRole", "Role does not exist", correlation_data); return MOSQ_ERR_INVAL; @@ -920,9 +920,9 @@ int dynsec_roles__process_modify(cJSON *j_responses, struct mosquitto *context, } if(do_kick){ - role__kick_all(role); + role__kick_all(data, role); } - dynsec__config_save(); + dynsec__config_save(data); dynsec__command_reply(j_responses, context, "modifyRole", NULL, correlation_data);