From 5b3acfe3cc7216482409b299bbf234b729877dba Mon Sep 17 00:00:00 2001 From: Roger Light Date: Thu, 7 Jan 2021 23:35:32 +0000 Subject: [PATCH] Fix `mosquitto_ctrl dynsec getGroup` not showing roles. Closes #1997. Thanks to Willem Eradus. --- ChangeLog.txt | 1 + apps/mosquitto_ctrl/dynsec.c | 78 ++++++++++++++++-------------------- 2 files changed, 35 insertions(+), 44 deletions(-) diff --git a/ChangeLog.txt b/ChangeLog.txt index 23248974..85deaf28 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -13,6 +13,7 @@ Broker: Apps: - Disallow control characters in mosquitto_passwd usernames. - Fix incorrect description in mosquitto_ctrl man page. Closes #1995. +- Fix `mosquitto_ctrl dynsec getGroup` not showing roles. Closes #1997. 2.0.4 - 2020-12-22 diff --git a/apps/mosquitto_ctrl/dynsec.c b/apps/mosquitto_ctrl/dynsec.c index 35d29e9d..80ac2087 100644 --- a/apps/mosquitto_ctrl/dynsec.c +++ b/apps/mosquitto_ctrl/dynsec.c @@ -124,6 +124,37 @@ static void print_list(cJSON *j_response, const char *arrayname, const char *key } +static void print_roles(cJSON *j_roles, int slen) +{ + bool first; + cJSON *j_elem, *jtmp; + + if(j_roles && cJSON_IsArray(j_roles)){ + first = true; + cJSON_ArrayForEach(j_elem, j_roles){ + jtmp = cJSON_GetObjectItem(j_elem, "rolename"); + if(jtmp && cJSON_IsString(jtmp)){ + if(first){ + first = false; + printf("%-*s %s", slen, "Roles:", jtmp->valuestring); + }else{ + printf("%-*s %s", slen, "", jtmp->valuestring); + } + jtmp = cJSON_GetObjectItem(j_elem, "priority"); + if(jtmp && cJSON_IsNumber(jtmp)){ + printf(" (priority: %d)", (int)jtmp->valuedouble); + }else{ + printf(" (priority: -1)"); + } + printf("\n"); + } + } + }else{ + printf("Roles:\n"); + } +} + + static void print_client(cJSON *j_response) { cJSON *j_data, *j_client, *j_array, *j_elem, *jtmp; @@ -161,29 +192,8 @@ static void print_client(cJSON *j_response) } j_array = cJSON_GetObjectItem(j_client, "roles"); - if(j_array && cJSON_IsArray(j_array)){ - first = true; - cJSON_ArrayForEach(j_elem, j_array){ - jtmp = cJSON_GetObjectItem(j_elem, "rolename"); - if(jtmp && cJSON_IsString(jtmp)){ - if(first){ - first = false; - printf("Roles: %s", jtmp->valuestring); - }else{ - printf(" %s", jtmp->valuestring); - } - jtmp = cJSON_GetObjectItem(j_elem, "priority"); - if(jtmp && cJSON_IsNumber(jtmp)){ - printf(" (priority: %d)", (int)jtmp->valuedouble); - }else{ - printf(" (priority: -1)"); - } - printf("\n"); - } - } - }else{ - printf("Roles:\n"); - } + print_roles(j_array, strlen("Username:")); + j_array = cJSON_GetObjectItem(j_client, "groups"); if(j_array && cJSON_IsArray(j_array)){ first = true; @@ -236,27 +246,7 @@ static void print_group(cJSON *j_response) printf("Groupname: %s\n", jtmp->valuestring); j_array = cJSON_GetObjectItem(j_group, "roles"); - if(j_array && cJSON_IsArray(j_array)){ - first = true; - cJSON_ArrayForEach(j_elem, j_array){ - jtmp = cJSON_GetObjectItem(j_elem, "groupname"); - if(jtmp && cJSON_IsString(jtmp)){ - if(first){ - first = false; - printf("Roles: %s", jtmp->valuestring); - }else{ - printf(" %s", jtmp->valuestring); - } - jtmp = cJSON_GetObjectItem(j_elem, "priority"); - if(jtmp && cJSON_IsNumber(jtmp)){ - printf(" (priority: %d)", (int)jtmp->valuedouble); - }else{ - printf(" (priority: -1)"); - } - printf("\n"); - } - } - } + print_roles(j_array, strlen("Groupname:")); j_array = cJSON_GetObjectItem(j_group, "clients"); if(j_array && cJSON_IsArray(j_array)){