|
|
|
@ -28,6 +28,8 @@ Contributors:
|
|
|
|
|
#include "password_mosq.h"
|
|
|
|
|
#include "get_password.h"
|
|
|
|
|
|
|
|
|
|
#define MAX_STRING_LEN 4096
|
|
|
|
|
|
|
|
|
|
void dynsec__print_usage(void)
|
|
|
|
|
{
|
|
|
|
|
printf("\nDynamic Security module\n");
|
|
|
|
@ -128,43 +130,55 @@ static void print_list(cJSON *j_response, const char *arrayname, const char *key
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void print_roles(cJSON *j_roles, size_t slen)
|
|
|
|
|
static void print_json_value(cJSON* value, const char* null_value)
|
|
|
|
|
{
|
|
|
|
|
bool first;
|
|
|
|
|
cJSON *j_elem, *jtmp;
|
|
|
|
|
if (value){
|
|
|
|
|
if (cJSON_IsString(value)){
|
|
|
|
|
printf("%s", value->valuestring);
|
|
|
|
|
}else{
|
|
|
|
|
char buffer[MAX_STRING_LEN];
|
|
|
|
|
cJSON_PrintPreallocated(value, buffer, sizeof(buffer), 0);
|
|
|
|
|
printf("%s", buffer);
|
|
|
|
|
}
|
|
|
|
|
} else if (null_value){
|
|
|
|
|
printf("%s",null_value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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", (int)slen, "Roles:", jtmp->valuestring);
|
|
|
|
|
}else{
|
|
|
|
|
printf("%-*s %s", (int)slen, "", jtmp->valuestring);
|
|
|
|
|
}
|
|
|
|
|
jtmp = cJSON_GetObjectItem(j_elem, "priority");
|
|
|
|
|
if(jtmp && cJSON_IsNumber(jtmp)){
|
|
|
|
|
printf(" (priority: %d)", (int)jtmp->valuedouble);
|
|
|
|
|
}else{
|
|
|
|
|
printf(" (priority: -1)");
|
|
|
|
|
static void print_json_array(cJSON *j_list, int slen, const char* label, const char* element_name, const char* optional_element_name, const char* optional_element_null_value)
|
|
|
|
|
{
|
|
|
|
|
cJSON *j_elem;
|
|
|
|
|
|
|
|
|
|
if(j_list && cJSON_IsArray(j_list)){
|
|
|
|
|
cJSON_ArrayForEach(j_elem, j_list){
|
|
|
|
|
if (cJSON_IsObject(j_elem)) {
|
|
|
|
|
cJSON* jtmp = cJSON_GetObjectItem(j_elem, element_name);
|
|
|
|
|
if(!jtmp || !cJSON_IsString(jtmp)){
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
printf("\n");
|
|
|
|
|
}
|
|
|
|
|
printf("%-*s %s", (int)slen, label, jtmp->valuestring);
|
|
|
|
|
if (optional_element_name) {
|
|
|
|
|
printf(" (%s: ", optional_element_name);
|
|
|
|
|
print_json_value(cJSON_GetObjectItem(j_elem,optional_element_name),optional_element_null_value);
|
|
|
|
|
printf(")");
|
|
|
|
|
}
|
|
|
|
|
} else if (cJSON_IsString(j_elem)) {
|
|
|
|
|
printf("%-*s %s", (int)slen, label, j_elem->valuestring);
|
|
|
|
|
}
|
|
|
|
|
label = "";
|
|
|
|
|
printf("\n");
|
|
|
|
|
}
|
|
|
|
|
}else{
|
|
|
|
|
printf("Roles:\n");
|
|
|
|
|
printf("%s\n", label);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void print_client(cJSON *j_response)
|
|
|
|
|
{
|
|
|
|
|
cJSON *j_data, *j_client, *j_array, *j_elem, *jtmp;
|
|
|
|
|
bool first;
|
|
|
|
|
|
|
|
|
|
cJSON *j_data, *j_client, *jtmp;
|
|
|
|
|
const int label_width = strlen( "Connections:");
|
|
|
|
|
|
|
|
|
|
j_data = cJSON_GetObjectItem(j_response, "data");
|
|
|
|
|
if(j_data == NULL || !cJSON_IsObject(j_data)){
|
|
|
|
|
fprintf(stderr, "Error: Invalid response from server.\n");
|
|
|
|
@ -182,54 +196,30 @@ static void print_client(cJSON *j_response)
|
|
|
|
|
fprintf(stderr, "Error: Invalid response from server.\n");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
printf("Username: %s\n", jtmp->valuestring);
|
|
|
|
|
printf("%-*s %s\n", label_width, "Username:", jtmp->valuestring);
|
|
|
|
|
|
|
|
|
|
jtmp = cJSON_GetObjectItem(j_client, "clientid");
|
|
|
|
|
if(jtmp && cJSON_IsString(jtmp)){
|
|
|
|
|
printf("Clientid: %s\n", jtmp->valuestring);
|
|
|
|
|
printf("%-*s %s\n", label_width, "Clientid:", jtmp->valuestring);
|
|
|
|
|
}else{
|
|
|
|
|
printf("Clientid:\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
jtmp = cJSON_GetObjectItem(j_client, "disabled");
|
|
|
|
|
if(jtmp && cJSON_IsBool(jtmp)){
|
|
|
|
|
printf("Disabled: %s\n", cJSON_IsTrue(jtmp)?"true":"false");
|
|
|
|
|
printf("%-*s %s\n", label_width, "Disabled:", cJSON_IsTrue(jtmp)?"true":"false");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
j_array = cJSON_GetObjectItem(j_client, "roles");
|
|
|
|
|
print_roles(j_array, strlen("Username:"));
|
|
|
|
|
|
|
|
|
|
j_array = cJSON_GetObjectItem(j_client, "groups");
|
|
|
|
|
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){
|
|
|
|
|
printf("Groups: %s", jtmp->valuestring);
|
|
|
|
|
first = false;
|
|
|
|
|
}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("Groups:\n");
|
|
|
|
|
}
|
|
|
|
|
print_json_array(cJSON_GetObjectItem(j_client, "roles"), label_width, "Roles:", "rolename", "priority", "-1");
|
|
|
|
|
print_json_array(cJSON_GetObjectItem(j_client, "groups"), label_width, "Groups:", "groupname", "priority", "-1");
|
|
|
|
|
print_json_array(cJSON_GetObjectItem(j_client, "connections"), label_width, "Connections:", "address", NULL, NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void print_group(cJSON *j_response)
|
|
|
|
|
{
|
|
|
|
|
cJSON *j_data, *j_group, *j_array, *j_elem, *jtmp;
|
|
|
|
|
bool first;
|
|
|
|
|
cJSON *j_data, *j_group, *jtmp;
|
|
|
|
|
int label_width = strlen("Groupname:");
|
|
|
|
|
|
|
|
|
|
j_data = cJSON_GetObjectItem(j_response, "data");
|
|
|
|
|
if(j_data == NULL || !cJSON_IsObject(j_data)){
|
|
|
|
@ -250,24 +240,8 @@ static void print_group(cJSON *j_response)
|
|
|
|
|
}
|
|
|
|
|
printf("Groupname: %s\n", jtmp->valuestring);
|
|
|
|
|
|
|
|
|
|
j_array = cJSON_GetObjectItem(j_group, "roles");
|
|
|
|
|
print_roles(j_array, strlen("Groupname:"));
|
|
|
|
|
|
|
|
|
|
j_array = cJSON_GetObjectItem(j_group, "clients");
|
|
|
|
|
if(j_array && cJSON_IsArray(j_array)){
|
|
|
|
|
first = true;
|
|
|
|
|
cJSON_ArrayForEach(j_elem, j_array){
|
|
|
|
|
jtmp = cJSON_GetObjectItem(j_elem, "username");
|
|
|
|
|
if(jtmp && cJSON_IsString(jtmp)){
|
|
|
|
|
if(first){
|
|
|
|
|
first = false;
|
|
|
|
|
printf("Clients: %s\n", jtmp->valuestring);
|
|
|
|
|
}else{
|
|
|
|
|
printf(" %s\n", jtmp->valuestring);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
print_json_array(cJSON_GetObjectItem(j_group, "roles"), label_width, "Roles:", "rolename", "priority", "-1");
|
|
|
|
|
print_json_array(cJSON_GetObjectItem(j_group, "clients"), label_width, "Clients:", "username", NULL, NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|