Skip to content

Commit

Permalink
dynsec: Rolelist as own file for consistency.
Browse files Browse the repository at this point in the history
  • Loading branch information
ralight committed Nov 18, 2020
1 parent fe436cd commit 4f9fe4d
Show file tree
Hide file tree
Showing 7 changed files with 261 additions and 214 deletions.
1 change: 1 addition & 0 deletions plugins/dynamic-security/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ add_library(mosquitto_dynamic_security SHARED
json_help.h
plugin.c
roles.c
rolelist.c
sub_matches_sub.c)

set_target_properties(mosquitto_dynamic_security PROPERTIES
Expand Down
4 changes: 4 additions & 0 deletions plugins/dynamic-security/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ OBJS= \
json_help.o \
plugin.o \
roles.o \
rolelist.o \
sub_matches_sub.o

all : binary
Expand Down Expand Up @@ -50,6 +51,9 @@ plugin.o : plugin.c dynamic_security.h
roles.o : roles.c dynamic_security.h
${CROSS_COMPILE}${CC} $(LOCAL_CPPFLAGS) $(PLUGIN_CPPFLAGS) $(PLUGIN_CFLAGS) -c $< -o $@

rolelist.o : rolelist.c dynamic_security.h
${CROSS_COMPILE}${CC} $(LOCAL_CPPFLAGS) $(PLUGIN_CPPFLAGS) $(PLUGIN_CFLAGS) -c $< -o $@

sub_matches_sub.o : sub_matches_sub.c dynamic_security.h
${CROSS_COMPILE}${CC} $(LOCAL_CPPFLAGS) $(PLUGIN_CPPFLAGS) $(PLUGIN_CFLAGS) -c $< -o $@

Expand Down
26 changes: 13 additions & 13 deletions plugins/dynamic-security/clients.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ static void client__free_item(struct dynsec__client *client)
if(client == NULL) return;

HASH_DEL(local_clients, client);
dynsec_rolelists__free_all(&client->rolelist);
dynsec_rolelist__cleanup(&client->rolelist);
dynsec__remove_client_from_all_groups(client->username);
mosquitto_free(client->text_name);
mosquitto_free(client->text_description);
Expand Down Expand Up @@ -237,7 +237,7 @@ int dynsec_clients__config_load(cJSON *tree)
if(jtmp && cJSON_IsString(jtmp)){
json_get_int(j_role, "priority", &priority, true, -1);
role = dynsec_roles__find(jtmp->valuestring);
dynsec_rolelists__client_add_role(client, role, priority);
dynsec_rolelist__client_add(client, role, priority);
}
}
}
Expand Down Expand Up @@ -273,7 +273,7 @@ static int dynsec__config_add_clients(cJSON *j_clients)
return 1;
}

j_roles = dynsec_rolelists__all_to_json(client->rolelist);
j_roles = dynsec_rolelist__all_to_json(client->rolelist);
if(j_roles == NULL){
return 1;
}
Expand Down Expand Up @@ -419,7 +419,7 @@ int dynsec_clients__process_create(cJSON *j_responses, struct mosquitto *context
}
}

rc = dynsec_rolelists__load_from_json(command, &client->rolelist);
rc = dynsec_rolelist__load_from_json(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);
Expand Down Expand Up @@ -644,7 +644,7 @@ static void client__add_new_roles(struct dynsec__client *client, struct dynsec__
struct dynsec__rolelist *rolelist, *rolelist_tmp;

HASH_ITER(hh, base_rolelist, rolelist, rolelist_tmp){
dynsec_rolelists__client_add_role(client, rolelist->role, rolelist->priority);
dynsec_rolelist__client_add(client, rolelist->role, rolelist->priority);
}
}

Expand All @@ -653,7 +653,7 @@ static void client__remove_all_roles(struct dynsec__client *client)
struct dynsec__rolelist *rolelist, *rolelist_tmp;

HASH_ITER(hh, client->rolelist, rolelist, rolelist_tmp){
dynsec_rolelists__client_remove_role(client, rolelist->role);
dynsec_rolelist__client_remove(client, rolelist->role);
}
}

Expand Down Expand Up @@ -726,21 +726,21 @@ int dynsec_clients__process_modify(cJSON *j_responses, struct mosquitto *context
client->text_description = str;
}

rc = dynsec_rolelists__load_from_json(command, &rolelist);
rc = dynsec_rolelist__load_from_json(command, &rolelist);
if(rc == MOSQ_ERR_SUCCESS){
client__remove_all_roles(client);
client__add_new_roles(client, rolelist);
dynsec_rolelists__free_all(&rolelist);
dynsec_rolelist__cleanup(&rolelist);
}else if(rc == MOSQ_ERR_NOT_FOUND){
dynsec__command_reply(j_responses, context, "modifyClient", "Role not found", correlation_data);
dynsec_rolelists__free_all(&rolelist);
dynsec_rolelist__cleanup(&rolelist);
mosquitto_kick_client_by_username(username, false);
return MOSQ_ERR_INVAL;
}else if(rc == ERR_LIST_NOT_FOUND){
/* There was no list in the JSON, so no modification */
}else{
dynsec__command_reply(j_responses, context, "modifyClient", "Internal error", correlation_data);
dynsec_rolelists__free_all(&rolelist);
dynsec_rolelist__cleanup(&rolelist);
mosquitto_kick_client_by_username(username, false);
return MOSQ_ERR_INVAL;
}
Expand Down Expand Up @@ -807,7 +807,7 @@ static cJSON *add_client_to_json(struct dynsec__client *client, bool verbose)
return NULL;
}

j_roles = dynsec_rolelists__all_to_json(client->rolelist);
j_roles = dynsec_rolelist__all_to_json(client->rolelist);
if(j_roles == NULL){
cJSON_Delete(j_client);
return NULL;
Expand Down Expand Up @@ -1014,7 +1014,7 @@ int dynsec_clients__process_add_role(cJSON *j_responses, struct mosquitto *conte
return MOSQ_ERR_SUCCESS;
}

dynsec_rolelists__client_add_role(client, role, priority);
dynsec_rolelist__client_add(client, role, priority);
dynsec__config_save();
dynsec__command_reply(j_responses, context, "addClientRole", NULL, correlation_data);

Expand Down Expand Up @@ -1062,7 +1062,7 @@ int dynsec_clients__process_remove_role(cJSON *j_responses, struct mosquitto *co
return MOSQ_ERR_SUCCESS;
}

dynsec_rolelists__client_remove_role(client, role);
dynsec_rolelist__client_remove(client, role);
dynsec__config_save();
dynsec__command_reply(j_responses, context, "removeClientRole", NULL, correlation_data);

Expand Down
21 changes: 14 additions & 7 deletions plugins/dynamic-security/dynamic_security.h
Original file line number Diff line number Diff line change
Expand Up @@ -253,12 +253,19 @@ int dynsec_roles__process_modify(cJSON *j_responses, struct mosquitto *context,
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);

int dynsec_rolelists__client_add_role(struct dynsec__client *client, struct dynsec__role *role, int priority);
int dynsec_rolelists__client_remove_role(struct dynsec__client *client, struct dynsec__role *role);
int dynsec_rolelists__group_add_role(struct dynsec__group *group, struct dynsec__role *role, int priority);
void dynsec_rolelists__group_remove_role(struct dynsec__group *group, struct dynsec__role *role);
int dynsec_rolelists__load_from_json(cJSON *command, struct dynsec__rolelist **rolelist);
void dynsec_rolelists__free_all(struct dynsec__rolelist **base_rolelist);
cJSON *dynsec_rolelists__all_to_json(struct dynsec__rolelist *base_rolelist);

/* ################################################################
* #
* # Role List Functions
* #
* ################################################################ */

int dynsec_rolelist__client_add(struct dynsec__client *client, struct dynsec__role *role, int priority);
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);
void dynsec_rolelist__cleanup(struct dynsec__rolelist **base_rolelist);
cJSON *dynsec_rolelist__all_to_json(struct dynsec__rolelist *base_rolelist);

#endif
22 changes: 11 additions & 11 deletions plugins/dynamic-security/groups.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ static void group__free_item(struct dynsec__group *group)
mosquitto_free(group->text_name);
mosquitto_free(group->text_description);
mosquitto_free(group->groupname);
dynsec_rolelists__free_all(&group->rolelist);
dynsec_rolelist__cleanup(&group->rolelist);
mosquitto_free(group);
}

Expand Down Expand Up @@ -130,7 +130,7 @@ int dynsec_groups__process_add_role(cJSON *j_responses, struct mosquitto *contex
return MOSQ_ERR_SUCCESS;
}

dynsec_rolelists__group_add_role(group, role, priority);
dynsec_rolelist__group_add(group, role, priority);
dynsec__config_save();
dynsec__command_reply(j_responses, context, "addGroupRole", NULL, correlation_data);
return MOSQ_ERR_SUCCESS;
Expand Down Expand Up @@ -230,7 +230,7 @@ int dynsec_groups__config_load(cJSON *tree)
if(j_rolename && cJSON_IsString(j_rolename)){
json_get_int(j_role, "priority", &priority, true, -1);
role = dynsec_roles__find(j_rolename->valuestring);
dynsec_rolelists__group_add_role(group, role, priority);
dynsec_rolelist__group_add(group, role, priority);
}
}
}
Expand Down Expand Up @@ -290,7 +290,7 @@ static int dynsec__config_add_groups(cJSON *j_groups)
return 1;
}

j_roles = dynsec_rolelists__all_to_json(group->rolelist);
j_roles = dynsec_rolelist__all_to_json(group->rolelist);
if(j_roles == NULL){
return 1;
}
Expand Down Expand Up @@ -389,7 +389,7 @@ int dynsec_groups__process_create(cJSON *j_responses, struct mosquitto *context,
}
}

rc = dynsec_rolelists__load_from_json(command, &group->rolelist);
rc = dynsec_rolelist__load_from_json(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);
Expand Down Expand Up @@ -644,7 +644,7 @@ static cJSON *add_group_to_json(struct dynsec__group *group)
cJSON_AddItemToObject(j_client, "username", jtmp);
}

j_rolelist = dynsec_rolelists__all_to_json(group->rolelist);
j_rolelist = dynsec_rolelist__all_to_json(group->rolelist);
if(j_rolelist == NULL){
cJSON_Delete(j_group);
return NULL;
Expand Down Expand Up @@ -845,7 +845,7 @@ int dynsec_groups__process_remove_role(cJSON *j_responses, struct mosquitto *con
return MOSQ_ERR_SUCCESS;
}

dynsec_rolelists__group_remove_role(group, role);
dynsec_rolelist__group_remove(group, role);
dynsec__config_save();
dynsec__command_reply(j_responses, context, "removeGroupRole", NULL, correlation_data);

Expand Down Expand Up @@ -904,19 +904,19 @@ int dynsec_groups__process_modify(cJSON *j_responses, struct mosquitto *context,
group->text_description = str;
}

rc = dynsec_rolelists__load_from_json(command, &rolelist);
rc = dynsec_rolelist__load_from_json(command, &rolelist);
if(rc == MOSQ_ERR_SUCCESS){
dynsec_rolelists__free_all(&group->rolelist);
dynsec_rolelist__cleanup(&group->rolelist);
group->rolelist = rolelist;
}else if(rc == MOSQ_ERR_NOT_FOUND){
dynsec__command_reply(j_responses, context, "modifyGroup", "Role not found", correlation_data);
dynsec_rolelists__free_all(&rolelist);
dynsec_rolelist__cleanup(&rolelist);
return MOSQ_ERR_INVAL;
}else if(rc == ERR_LIST_NOT_FOUND){
/* There was no list in the JSON, so no modification */
}else{
dynsec__command_reply(j_responses, context, "modifyGroup", "Internal error", correlation_data);
dynsec_rolelists__free_all(&rolelist);
dynsec_rolelist__cleanup(&rolelist);
return MOSQ_ERR_INVAL;
}

Expand Down
Loading

0 comments on commit 4f9fe4d

Please sign in to comment.