Resolve KASM-2194 "Feature/ bulk user mgmt"
This commit is contained in:
@@ -16,7 +16,12 @@
|
||||
* USA.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "jsonescape.h"
|
||||
#include "cJSON.h"
|
||||
|
||||
void JSON_escape(const char *in, char *out) {
|
||||
for (; *in; in++) {
|
||||
@@ -79,3 +84,97 @@ void JSON_unescape(const char *in, char *out) {
|
||||
|
||||
*out = '\0';
|
||||
}
|
||||
|
||||
struct kasmpasswd_t *parseJsonUsers(const char *data) {
|
||||
|
||||
cJSON *json = cJSON_Parse(data);
|
||||
if (!json)
|
||||
return NULL;
|
||||
|
||||
if (!(json->type & cJSON_Array))
|
||||
return NULL;
|
||||
|
||||
struct kasmpasswd_t *set = calloc(sizeof(struct kasmpasswd_t), 1);
|
||||
set->num = cJSON_GetArraySize(json);
|
||||
set->entries = calloc(sizeof(struct kasmpasswd_entry_t), set->num);
|
||||
|
||||
cJSON *cur;
|
||||
unsigned s, len;
|
||||
for (cur = json->child, s = 0; cur; cur = cur->next, s++) {
|
||||
if (!(cur->type & cJSON_Object))
|
||||
goto fail;
|
||||
|
||||
cJSON *e;
|
||||
struct kasmpasswd_entry_t * const entry = &set->entries[s];
|
||||
|
||||
entry->user[0] = '\0';
|
||||
entry->password[0] = '\0';
|
||||
entry->write = entry->owner = 0;
|
||||
|
||||
for (e = cur->child; e; e = e->next) {
|
||||
#define field(x) if (!strcmp(x, e->string))
|
||||
|
||||
field("user") {
|
||||
if (!(e->type & cJSON_String))
|
||||
goto fail;
|
||||
len = strlen(e->valuestring);
|
||||
|
||||
//printf("Val '%.*s'\n", len, start);
|
||||
|
||||
if (len >= USERNAME_LEN)
|
||||
goto fail;
|
||||
|
||||
memcpy(entry->user, e->valuestring, len);
|
||||
entry->user[len] = '\0';
|
||||
} else field("password") {
|
||||
if (!(e->type & cJSON_String))
|
||||
goto fail;
|
||||
len = strlen(e->valuestring);
|
||||
|
||||
//printf("Val '%.*s'\n", len, start);
|
||||
|
||||
if (len >= PASSWORD_LEN)
|
||||
goto fail;
|
||||
|
||||
memcpy(entry->password, e->valuestring, len);
|
||||
entry->password[len] = '\0';
|
||||
} else field("write") {
|
||||
if (!(e->type & (cJSON_False | cJSON_True)))
|
||||
goto fail;
|
||||
|
||||
if (e->type & cJSON_True)
|
||||
entry->write = 1;
|
||||
} else field("owner") {
|
||||
if (!(e->type & (cJSON_False | cJSON_True)))
|
||||
goto fail;
|
||||
|
||||
if (e->type & cJSON_True)
|
||||
entry->owner = 1;
|
||||
/* } else field("read") {
|
||||
start = end + 3;
|
||||
if (!(e->type & (cJSON_False | cJSON_True)))
|
||||
goto fail;
|
||||
|
||||
if (e->type & cJSON_True)
|
||||
entry->read = 1;
|
||||
*/
|
||||
} else {
|
||||
//printf("Unknown field '%.*s'\n", len, start);
|
||||
goto fail;
|
||||
}
|
||||
|
||||
#undef field
|
||||
}
|
||||
}
|
||||
|
||||
cJSON_Delete(json);
|
||||
|
||||
return set;
|
||||
fail:
|
||||
free(set->entries);
|
||||
free(set);
|
||||
|
||||
cJSON_Delete(json);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user