dynsec: Refactor some cJSON adding code to reduce redundancy
This commit is contained in:
parent
d8775b7d9c
commit
ba8e888e41
@ -311,12 +311,9 @@ int dynsec_clients__config_save(cJSON *tree)
|
||||
{
|
||||
cJSON *j_clients;
|
||||
|
||||
j_clients = cJSON_CreateArray();
|
||||
if(j_clients == NULL){
|
||||
if((j_clients = cJSON_AddArrayToObject(tree, "clients")) == NULL){
|
||||
return 1;
|
||||
}
|
||||
|
||||
cJSON_AddItemToObject(tree, "clients", j_clients);
|
||||
if(dynsec__config_add_clients(j_clients)){
|
||||
return 1;
|
||||
}
|
||||
@ -852,7 +849,7 @@ int dynsec_clients__process_get(cJSON *j_responses, struct mosquitto *context, c
|
||||
{
|
||||
char *username;
|
||||
struct dynsec__client *client;
|
||||
cJSON *tree, *j_client, *jtmp, *j_data;
|
||||
cJSON *tree, *j_client, *j_data;
|
||||
|
||||
if(json_get_string(command, "username", &username, false) != MOSQ_ERR_SUCCESS){
|
||||
dynsec__command_reply(j_responses, context, "getClient", "Invalid/missing username", correlation_data);
|
||||
@ -875,21 +872,15 @@ int dynsec_clients__process_get(cJSON *j_responses, struct mosquitto *context, c
|
||||
return MOSQ_ERR_NOMEM;
|
||||
}
|
||||
|
||||
jtmp = cJSON_CreateString("getClient");
|
||||
if(jtmp == NULL){
|
||||
cJSON_Delete(tree);
|
||||
dynsec__command_reply(j_responses, context, "getClient", "Internal error", correlation_data);
|
||||
return MOSQ_ERR_NOMEM;
|
||||
}
|
||||
cJSON_AddItemToObject(tree, "command", jtmp);
|
||||
if(cJSON_AddStringToObject(tree, "command", "getClient") == NULL
|
||||
|| (j_data = cJSON_AddObjectToObject(tree, "data")) == NULL
|
||||
|| (correlation_data && cJSON_AddStringToObject(tree, "correlationData", correlation_data) == NULL)
|
||||
){
|
||||
|
||||
j_data = cJSON_CreateObject();
|
||||
if(j_data == NULL){
|
||||
cJSON_Delete(tree);
|
||||
dynsec__command_reply(j_responses, context, "getClient", "Internal error", correlation_data);
|
||||
return MOSQ_ERR_NOMEM;
|
||||
}
|
||||
cJSON_AddItemToObject(tree, "data", j_data);
|
||||
|
||||
j_client = add_client_to_json(client, true);
|
||||
if(j_client == NULL){
|
||||
@ -898,16 +889,6 @@ int dynsec_clients__process_get(cJSON *j_responses, struct mosquitto *context, c
|
||||
return MOSQ_ERR_NOMEM;
|
||||
}
|
||||
cJSON_AddItemToObject(j_data, "client", j_client);
|
||||
|
||||
if(correlation_data){
|
||||
jtmp = cJSON_AddStringToObject(tree, "correlationData", correlation_data);
|
||||
if(jtmp == NULL){
|
||||
cJSON_Delete(tree);
|
||||
dynsec__command_reply(j_responses, context, "getClient", "Internal error", correlation_data);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
cJSON_AddItemToArray(j_responses, tree);
|
||||
|
||||
return MOSQ_ERR_SUCCESS;
|
||||
@ -918,7 +899,7 @@ int dynsec_clients__process_list(cJSON *j_responses, struct mosquitto *context,
|
||||
{
|
||||
bool verbose;
|
||||
struct dynsec__client *client, *client_tmp;
|
||||
cJSON *tree, *j_clients, *j_client, *jtmp, *j_data;
|
||||
cJSON *tree, *j_clients, *j_client, *j_data;
|
||||
int i, count, offset;
|
||||
|
||||
json_get_bool(command, "verbose", &verbose, true, false);
|
||||
@ -931,31 +912,17 @@ int dynsec_clients__process_list(cJSON *j_responses, struct mosquitto *context,
|
||||
return MOSQ_ERR_NOMEM;
|
||||
}
|
||||
|
||||
jtmp = cJSON_CreateString("listClients");
|
||||
if(jtmp == NULL){
|
||||
if(cJSON_AddStringToObject(tree, "command", "listClients") == NULL
|
||||
|| (j_data = cJSON_AddObjectToObject(tree, "data")) == NULL
|
||||
|| cJSON_AddIntToObject(j_data, "totalCount", (int)HASH_CNT(hh, local_clients)) == NULL
|
||||
|| (j_clients = cJSON_AddArrayToObject(j_data, "clients")) == NULL
|
||||
|| (correlation_data && cJSON_AddStringToObject(tree, "correlationData", correlation_data) == NULL)
|
||||
){
|
||||
|
||||
cJSON_Delete(tree);
|
||||
dynsec__command_reply(j_responses, context, "listClients", "Internal error", correlation_data);
|
||||
return MOSQ_ERR_NOMEM;
|
||||
}
|
||||
cJSON_AddItemToObject(tree, "command", jtmp);
|
||||
|
||||
j_data = cJSON_CreateObject();
|
||||
if(j_data == NULL){
|
||||
cJSON_Delete(tree);
|
||||
dynsec__command_reply(j_responses, context, "listClients", "Internal error", correlation_data);
|
||||
return MOSQ_ERR_NOMEM;
|
||||
}
|
||||
cJSON_AddItemToObject(tree, "data", j_data);
|
||||
|
||||
cJSON_AddIntToObject(j_data, "totalCount", (int)HASH_CNT(hh, local_clients));
|
||||
|
||||
j_clients = cJSON_CreateArray();
|
||||
if(j_clients == NULL){
|
||||
cJSON_Delete(tree);
|
||||
dynsec__command_reply(j_responses, context, "listClients", "Internal error", correlation_data);
|
||||
return MOSQ_ERR_NOMEM;
|
||||
}
|
||||
cJSON_AddItemToObject(j_data, "clients", j_clients);
|
||||
|
||||
i = 0;
|
||||
HASH_ITER(hh, local_clients, client, client_tmp){
|
||||
@ -977,15 +944,6 @@ int dynsec_clients__process_list(cJSON *j_responses, struct mosquitto *context,
|
||||
}
|
||||
i++;
|
||||
}
|
||||
if(correlation_data){
|
||||
jtmp = cJSON_AddStringToObject(tree, "correlationData", correlation_data);
|
||||
if(jtmp == NULL){
|
||||
cJSON_Delete(tree);
|
||||
dynsec__command_reply(j_responses, context, "listClients", "Internal error", correlation_data);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
cJSON_AddItemToArray(j_responses, tree);
|
||||
|
||||
return MOSQ_ERR_SUCCESS;
|
||||
|
@ -668,7 +668,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)
|
||||
{
|
||||
bool verbose;
|
||||
cJSON *tree, *j_groups, *j_group, *jtmp, *j_data;
|
||||
cJSON *tree, *j_groups, *j_group, *j_data;
|
||||
struct dynsec__group *group, *group_tmp;
|
||||
int i, count, offset;
|
||||
|
||||
@ -682,31 +682,17 @@ int dynsec_groups__process_list(cJSON *j_responses, struct mosquitto *context, c
|
||||
return MOSQ_ERR_NOMEM;
|
||||
}
|
||||
|
||||
jtmp = cJSON_CreateString("listGroups");
|
||||
if(jtmp == NULL){
|
||||
if(cJSON_AddStringToObject(tree, "command", "listGroups") == NULL
|
||||
|| (j_data = cJSON_AddObjectToObject(tree, "data")) == NULL
|
||||
|| cJSON_AddIntToObject(j_data, "totalCount", (int)HASH_CNT(hh, local_groups)) == NULL
|
||||
|| (j_groups = cJSON_AddArrayToObject(j_data, "groups")) == NULL
|
||||
|| (correlation_data && cJSON_AddStringToObject(tree, "correlationData", correlation_data) == NULL)
|
||||
){
|
||||
|
||||
cJSON_Delete(tree);
|
||||
dynsec__command_reply(j_responses, context, "listGroups", "Internal error", correlation_data);
|
||||
return MOSQ_ERR_NOMEM;
|
||||
}
|
||||
cJSON_AddItemToObject(tree, "command", jtmp);
|
||||
|
||||
j_data = cJSON_CreateObject();
|
||||
if(j_data == NULL){
|
||||
cJSON_Delete(tree);
|
||||
dynsec__command_reply(j_responses, context, "listGroups", "Internal error", correlation_data);
|
||||
return MOSQ_ERR_NOMEM;
|
||||
}
|
||||
cJSON_AddItemToObject(tree, "data", j_data);
|
||||
|
||||
cJSON_AddIntToObject(j_data, "totalCount", (int)HASH_CNT(hh, local_groups));
|
||||
|
||||
j_groups = cJSON_CreateArray();
|
||||
if(j_groups == NULL){
|
||||
cJSON_Delete(tree);
|
||||
dynsec__command_reply(j_responses, context, "listGroups", "Internal error", correlation_data);
|
||||
return MOSQ_ERR_NOMEM;
|
||||
}
|
||||
cJSON_AddItemToObject(j_data, "groups", j_groups);
|
||||
|
||||
i = 0;
|
||||
HASH_ITER(hh, local_groups, group, group_tmp){
|
||||
@ -740,14 +726,6 @@ int dynsec_groups__process_list(cJSON *j_responses, struct mosquitto *context, c
|
||||
}
|
||||
i++;
|
||||
}
|
||||
if(correlation_data){
|
||||
jtmp = cJSON_AddStringToObject(tree, "correlationData", correlation_data);
|
||||
if(jtmp == NULL){
|
||||
cJSON_Delete(tree);
|
||||
dynsec__command_reply(j_responses, context, "listGroups", "Internal error", correlation_data);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
cJSON_AddItemToArray(j_responses, tree);
|
||||
|
||||
@ -758,7 +736,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)
|
||||
{
|
||||
char *groupname;
|
||||
cJSON *tree, *j_group, *jtmp, *j_data;
|
||||
cJSON *tree, *j_group, *j_data;
|
||||
struct dynsec__group *group;
|
||||
|
||||
if(json_get_string(command, "groupname", &groupname, false) != MOSQ_ERR_SUCCESS){
|
||||
@ -776,21 +754,15 @@ int dynsec_groups__process_get(cJSON *j_responses, struct mosquitto *context, cJ
|
||||
return MOSQ_ERR_NOMEM;
|
||||
}
|
||||
|
||||
jtmp = cJSON_CreateString("getGroup");
|
||||
if(jtmp == NULL){
|
||||
cJSON_Delete(tree);
|
||||
dynsec__command_reply(j_responses, context, "getGroup", "Internal error", correlation_data);
|
||||
return MOSQ_ERR_NOMEM;
|
||||
}
|
||||
cJSON_AddItemToObject(tree, "command", jtmp);
|
||||
if(cJSON_AddStringToObject(tree, "command", "getGroup") == NULL
|
||||
|| (j_data = cJSON_AddObjectToObject(tree, "data")) == NULL
|
||||
|| (correlation_data && cJSON_AddStringToObject(tree, "correlationData", correlation_data) == NULL)
|
||||
){
|
||||
|
||||
j_data = cJSON_CreateObject();
|
||||
if(j_data == NULL){
|
||||
cJSON_Delete(tree);
|
||||
dynsec__command_reply(j_responses, context, "getGroup", "Internal error", correlation_data);
|
||||
return MOSQ_ERR_NOMEM;
|
||||
}
|
||||
cJSON_AddItemToObject(tree, "data", j_data);
|
||||
|
||||
group = dynsec_groups__find(groupname);
|
||||
if(group){
|
||||
@ -802,14 +774,6 @@ int dynsec_groups__process_get(cJSON *j_responses, struct mosquitto *context, cJ
|
||||
}
|
||||
cJSON_AddItemToObject(j_data, "group", j_group);
|
||||
}
|
||||
if(correlation_data){
|
||||
jtmp = cJSON_AddStringToObject(tree, "correlationData", correlation_data);
|
||||
if(jtmp == NULL){
|
||||
cJSON_Delete(tree);
|
||||
dynsec__command_reply(j_responses, context, "getGroup", "Internal error", correlation_data);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
cJSON_AddItemToArray(j_responses, tree);
|
||||
|
||||
@ -985,7 +949,8 @@ int dynsec_groups__process_set_anonymous_group(cJSON *j_responses, struct mosqui
|
||||
|
||||
int dynsec_groups__process_get_anonymous_group(cJSON *j_responses, struct mosquitto *context, cJSON *command, char *correlation_data)
|
||||
{
|
||||
cJSON *tree, *jtmp, *j_data, *j_group;
|
||||
cJSON *tree, *j_data, *j_group;
|
||||
const char *groupname;
|
||||
|
||||
tree = cJSON_CreateObject();
|
||||
if(tree == NULL){
|
||||
@ -993,54 +958,22 @@ int dynsec_groups__process_get_anonymous_group(cJSON *j_responses, struct mosqui
|
||||
return MOSQ_ERR_NOMEM;
|
||||
}
|
||||
|
||||
jtmp = cJSON_CreateString("getAnonymousGroup");
|
||||
if(jtmp == NULL){
|
||||
cJSON_Delete(tree);
|
||||
dynsec__command_reply(j_responses, context, "getAnonymousGroup", "Internal error", correlation_data);
|
||||
return MOSQ_ERR_NOMEM;
|
||||
}
|
||||
cJSON_AddItemToObject(tree, "command", jtmp);
|
||||
|
||||
j_data = cJSON_CreateObject();
|
||||
if(j_data == NULL){
|
||||
cJSON_Delete(tree);
|
||||
dynsec__command_reply(j_responses, context, "getAnonymousGroup", "Internal error", correlation_data);
|
||||
return MOSQ_ERR_NOMEM;
|
||||
}
|
||||
cJSON_AddItemToObject(tree, "data", j_data);
|
||||
|
||||
j_group = cJSON_CreateObject();
|
||||
if(j_group == NULL){
|
||||
cJSON_Delete(tree);
|
||||
dynsec__command_reply(j_responses, context, "getAnonymousGroup", "Internal error", correlation_data);
|
||||
return MOSQ_ERR_NOMEM;
|
||||
}
|
||||
cJSON_AddItemToObject(j_data, "group", j_group);
|
||||
|
||||
if(dynsec_anonymous_group){
|
||||
if(cJSON_AddStringToObject(j_group, "groupname", dynsec_anonymous_group->groupname) == NULL
|
||||
){
|
||||
|
||||
cJSON_Delete(tree);
|
||||
dynsec__command_reply(j_responses, context, "getAnonymousGroup", "Internal error", correlation_data);
|
||||
return MOSQ_ERR_NOMEM;
|
||||
}
|
||||
groupname = dynsec_anonymous_group->groupname;
|
||||
}else{
|
||||
if(cJSON_AddStringToObject(j_group, "groupname", "") == NULL
|
||||
){
|
||||
|
||||
cJSON_Delete(tree);
|
||||
dynsec__command_reply(j_responses, context, "getAnonymousGroup", "Internal error", correlation_data);
|
||||
return MOSQ_ERR_NOMEM;
|
||||
}
|
||||
groupname = "";
|
||||
}
|
||||
if(correlation_data){
|
||||
jtmp = cJSON_AddStringToObject(tree, "correlationData", correlation_data);
|
||||
if(jtmp == NULL){
|
||||
cJSON_Delete(tree);
|
||||
dynsec__command_reply(j_responses, context, "getGroup", "Internal error", correlation_data);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if(cJSON_AddStringToObject(tree, "command", "getAnonymousGroup") == NULL
|
||||
|| (j_data = cJSON_AddObjectToObject(tree, "data")) == NULL
|
||||
|| (j_group = cJSON_AddObjectToObject(j_data, "group")) == NULL
|
||||
|| cJSON_AddStringToObject(j_group, "groupname", groupname) == NULL
|
||||
|| (correlation_data && cJSON_AddStringToObject(tree, "correlationData", correlation_data) == NULL)
|
||||
){
|
||||
|
||||
cJSON_Delete(tree);
|
||||
dynsec__command_reply(j_responses, context, "getAnonymousGroup", "Internal error", correlation_data);
|
||||
return MOSQ_ERR_NOMEM;
|
||||
}
|
||||
|
||||
cJSON_AddItemToArray(j_responses, tree);
|
||||
|
@ -185,11 +185,9 @@ int dynsec_roles__config_save(cJSON *tree)
|
||||
cJSON *j_roles, *j_role;
|
||||
struct dynsec__role *role, *role_tmp;
|
||||
|
||||
j_roles = cJSON_CreateArray();
|
||||
if(j_roles == NULL){
|
||||
if((j_roles = cJSON_AddArrayToObject(tree, "roles")) == NULL){
|
||||
return 1;
|
||||
}
|
||||
cJSON_AddItemToObject(tree, "roles", j_roles);
|
||||
|
||||
HASH_ITER(hh, local_roles, role, role_tmp){
|
||||
j_role = add_role_to_json(role, true);
|
||||
@ -519,7 +517,7 @@ int dynsec_roles__process_list(cJSON *j_responses, struct mosquitto *context, cJ
|
||||
{
|
||||
bool verbose;
|
||||
struct dynsec__role *role, *role_tmp;
|
||||
cJSON *tree, *j_roles, *j_role, *jtmp, *j_data;
|
||||
cJSON *tree, *j_roles, *j_role, *j_data;
|
||||
int i, count, offset;
|
||||
|
||||
json_get_bool(command, "verbose", &verbose, true, false);
|
||||
@ -532,31 +530,17 @@ int dynsec_roles__process_list(cJSON *j_responses, struct mosquitto *context, cJ
|
||||
return MOSQ_ERR_NOMEM;
|
||||
}
|
||||
|
||||
jtmp = cJSON_CreateString("listRoles");
|
||||
if(jtmp == NULL){
|
||||
if(cJSON_AddStringToObject(tree, "command", "listRoles") == NULL
|
||||
|| (j_data = cJSON_AddObjectToObject(tree, "data")) == NULL
|
||||
|| cJSON_AddIntToObject(j_data, "totalCount", (int)HASH_CNT(hh, local_roles)) == NULL
|
||||
|| (j_roles = cJSON_AddArrayToObject(j_data, "roles")) == NULL
|
||||
|| (correlation_data && cJSON_AddStringToObject(tree, "correlationData", correlation_data) == NULL)
|
||||
){
|
||||
|
||||
cJSON_Delete(tree);
|
||||
dynsec__command_reply(j_responses, context, "listRoles", "Internal error", correlation_data);
|
||||
return MOSQ_ERR_NOMEM;
|
||||
}
|
||||
cJSON_AddItemToObject(tree, "command", jtmp);
|
||||
|
||||
j_data = cJSON_CreateObject();
|
||||
if(j_data == NULL){
|
||||
cJSON_Delete(tree);
|
||||
dynsec__command_reply(j_responses, context, "listRoles", "Internal error", correlation_data);
|
||||
return MOSQ_ERR_NOMEM;
|
||||
}
|
||||
cJSON_AddItemToObject(tree, "data", j_data);
|
||||
|
||||
cJSON_AddIntToObject(j_data, "totalCount", (int)HASH_CNT(hh, local_roles));
|
||||
|
||||
j_roles = cJSON_CreateArray();
|
||||
if(j_roles == NULL){
|
||||
cJSON_Delete(tree);
|
||||
dynsec__command_reply(j_responses, context, "listRoles", "Internal error", correlation_data);
|
||||
return MOSQ_ERR_NOMEM;
|
||||
}
|
||||
cJSON_AddItemToObject(j_data, "roles", j_roles);
|
||||
|
||||
i = 0;
|
||||
HASH_ITER(hh, local_roles, role, role_tmp){
|
||||
@ -578,14 +562,6 @@ int dynsec_roles__process_list(cJSON *j_responses, struct mosquitto *context, cJ
|
||||
}
|
||||
i++;
|
||||
}
|
||||
if(correlation_data){
|
||||
jtmp = cJSON_AddStringToObject(tree, "correlationData", correlation_data);
|
||||
if(jtmp == NULL){
|
||||
cJSON_Delete(tree);
|
||||
dynsec__command_reply(j_responses, context, "listRoles", "Internal error", correlation_data);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
cJSON_AddItemToArray(j_responses, tree);
|
||||
|
||||
@ -765,7 +741,7 @@ int dynsec_roles__process_get(cJSON *j_responses, struct mosquitto *context, cJS
|
||||
{
|
||||
char *rolename;
|
||||
struct dynsec__role *role;
|
||||
cJSON *tree, *j_role, *jtmp, *j_data;
|
||||
cJSON *tree, *j_role, *j_data;
|
||||
|
||||
if(json_get_string(command, "rolename", &rolename, false) != MOSQ_ERR_SUCCESS){
|
||||
dynsec__command_reply(j_responses, context, "getRole", "Invalid/missing rolename", correlation_data);
|
||||
@ -788,21 +764,15 @@ int dynsec_roles__process_get(cJSON *j_responses, struct mosquitto *context, cJS
|
||||
return MOSQ_ERR_NOMEM;
|
||||
}
|
||||
|
||||
jtmp = cJSON_CreateString("getRole");
|
||||
if(jtmp == NULL){
|
||||
cJSON_Delete(tree);
|
||||
dynsec__command_reply(j_responses, context, "getRole", "Internal error", correlation_data);
|
||||
return MOSQ_ERR_NOMEM;
|
||||
}
|
||||
cJSON_AddItemToObject(tree, "command", jtmp);
|
||||
if(cJSON_AddStringToObject(tree, "command", "getRole") == NULL
|
||||
|| (j_data = cJSON_AddObjectToObject(tree, "data")) == NULL
|
||||
|| (correlation_data && cJSON_AddStringToObject(tree, "correlationData", correlation_data) == NULL)
|
||||
){
|
||||
|
||||
j_data = cJSON_CreateObject();
|
||||
if(j_data == NULL){
|
||||
cJSON_Delete(tree);
|
||||
dynsec__command_reply(j_responses, context, "getRole", "Internal error", correlation_data);
|
||||
return MOSQ_ERR_NOMEM;
|
||||
}
|
||||
cJSON_AddItemToObject(tree, "data", j_data);
|
||||
|
||||
j_role = add_role_to_json(role, true);
|
||||
if(j_role == NULL){
|
||||
@ -811,16 +781,6 @@ int dynsec_roles__process_get(cJSON *j_responses, struct mosquitto *context, cJS
|
||||
return MOSQ_ERR_NOMEM;
|
||||
}
|
||||
cJSON_AddItemToObject(j_data, "role", j_role);
|
||||
|
||||
if(correlation_data){
|
||||
jtmp = cJSON_AddStringToObject(tree, "correlationData", correlation_data);
|
||||
if(jtmp == NULL){
|
||||
cJSON_Delete(tree);
|
||||
dynsec__command_reply(j_responses, context, "getRole", "Internal error", correlation_data);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
cJSON_AddItemToArray(j_responses, tree);
|
||||
|
||||
return MOSQ_ERR_SUCCESS;
|
||||
|
Loading…
Reference in New Issue
Block a user