diff --git a/include/sudo_json.h b/include/sudo_json.h index 4ac7bc6d9..417d61b7d 100644 --- a/include/sudo_json.h +++ b/include/sudo_json.h @@ -37,7 +37,6 @@ struct json_value { enum json_value_type type; union { const char *string; - char * const * array; long long number; id_t id; bool boolean; diff --git a/lib/iolog/iolog_fileio.c b/lib/iolog/iolog_fileio.c index a1985d4a2..064ebc419 100644 --- a/lib/iolog/iolog_fileio.c +++ b/lib/iolog/iolog_fileio.c @@ -932,6 +932,8 @@ iolog_write_info_file_json(int dfd, const char *parent, struct iolog_info *info) bool ret = false; FILE *fp = NULL; int fd = -1; + size_t i; + char *cp; debug_decl(iolog_write_info_file_json, SUDO_DEBUG_UTIL); if (info->cmd == NULL || info->user == NULL || info->runas_user == NULL) @@ -974,16 +976,28 @@ iolog_write_info_file_json(int dfd, const char *parent, struct iolog_info *info) goto oom; if (info->argv != NULL) { - json_value.type = JSON_ARRAY; - json_value.u.array = info->argv; - if (!sudo_json_add_value(&json, "runargv", &json_value)) + if (!sudo_json_open_array(&json, "runargv")) + goto oom; + for (i = 0; (cp = info->argv[i]) != NULL; i++) { + json_value.type = JSON_STRING; + json_value.u.string = cp; + if (!sudo_json_add_value(&json, NULL, &json_value)) + goto oom; + } + if (!sudo_json_close_array(&json)) goto oom; } if (info->envp != NULL) { - json_value.type = JSON_ARRAY; - json_value.u.array = info->envp; - if (!sudo_json_add_value(&json, "runenv", &json_value)) + if (!sudo_json_open_array(&json, "runenv")) + goto oom; + for (i = 0; (cp = info->envp[i]) != NULL; i++) { + json_value.type = JSON_STRING; + json_value.u.string = cp; + if (!sudo_json_add_value(&json, NULL, &json_value)) + goto oom; + } + if (!sudo_json_close_array(&json)) goto oom; } diff --git a/lib/util/json.c b/lib/util/json.c index 6a12d0a62..1bc46572b 100644 --- a/lib/util/json.c +++ b/lib/util/json.c @@ -305,7 +305,6 @@ sudo_json_add_value_int(struct json_container *json, const char *name, struct json_value *value, bool as_object) { char numbuf[(((sizeof(long long) * 8) + 2) / 3) + 2]; - unsigned int i; debug_decl(sudo_json_add_value, SUDO_DEBUG_UTIL); /* Add comma if we are continuing an object/array. */ @@ -356,41 +355,7 @@ sudo_json_add_value_int(struct json_container *json, const char *name, debug_return_bool(false); break; case JSON_ARRAY: - if (value->u.array[0] == NULL || value->u.array[1] == NULL) { - if (!json_append_buf(json, "[ ")) - debug_return_bool(false); - if (value->u.array[0] != NULL) { - if (!json_append_string(json, value->u.array[0])) - debug_return_bool(false); - if (!json_append_buf(json, " ")) - debug_return_bool(false); - } - if (!json_append_buf(json, "]")) - debug_return_bool(false); - } else { - if (!json_append_buf(json, "[")) - debug_return_bool(false); - if (!json_append_buf(json, json->compact ? " " : "\n")) - debug_return_bool(false); - json->indent_level += json->indent_increment; - for (i = 0; value->u.array[i] != NULL; i++) { - if (!json_append_indent(json, json->indent_level)) - debug_return_bool(false); - if (!json_append_string(json, value->u.array[i])) - debug_return_bool(false); - if (value->u.array[i + 1] != NULL) { - if (!json_append_buf(json, ",")) - debug_return_bool(false); - } - if (!json_append_buf(json, json->compact ? " " : "\n")) - debug_return_bool(false); - } - json->indent_level -= json->indent_increment; - if (!json_append_indent(json, json->indent_level)) - debug_return_bool(false); - if (!json_append_buf(json, "]")) - debug_return_bool(false); - } + sudo_fatalx("internal error: can't print JSON_ARRAY"); break; case JSON_OBJECT: sudo_fatalx("internal error: can't print JSON_OBJECT"); diff --git a/logsrvd/eventlog.c b/logsrvd/eventlog.c index ab48e642a..0f3121d67 100644 --- a/logsrvd/eventlog.c +++ b/logsrvd/eventlog.c @@ -288,7 +288,6 @@ format_json(ClientMessage__TypeCase event_type, struct json_container json = { 0 }; struct json_value json_value; struct timespec ts; - char **strvec; size_t idx; debug_decl(format_json, SUDO_DEBUG_UTIL); @@ -365,20 +364,22 @@ format_json(ClientMessage__TypeCase event_type, if (!sudo_json_add_value(&json, info->key, &json_value)) goto bad; break; - case INFO_MESSAGE__VALUE_STRLISTVAL: - /* Must convert to NULL-terminated string vector. */ - strvec = strlist_copy(info->strlistval); - if (strvec == NULL) { - sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_ERRNO|SUDO_DEBUG_LINENO, - "%s: %s", __func__, "unable to allocate memory"); + case INFO_MESSAGE__VALUE_STRLISTVAL: { + InfoMessage__StringList *strlist = info->strlistval; + size_t n; + + if (!sudo_json_open_array(&json, info->key)) goto bad; + for (n = 0; n < strlist->n_strings; n++) { + json_value.type = JSON_STRING; + json_value.u.string = strlist->strings[n]; + if (!sudo_json_add_value(&json, NULL, &json_value)) + goto bad; } - json_value.type = JSON_ARRAY; - json_value.u.array = strvec; - if (!sudo_json_add_value(&json, info->key, &json_value)) + if (!sudo_json_close_array(&json)) goto bad; - free(strvec); break; + } default: sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO, "unexpected value case %d", info->value_case); diff --git a/logsrvd/iolog_writer.c b/logsrvd/iolog_writer.c index 71fb4be69..86fc40a5e 100644 --- a/logsrvd/iolog_writer.c +++ b/logsrvd/iolog_writer.c @@ -70,7 +70,7 @@ has_strlistval(InfoMessage *info) * The input string list need not be NULL-terminated. * Returns a NULL-terminated string vector. */ -char ** +static char ** strlist_copy(InfoMessage__StringList *strlist) { char **dst, **src = strlist->strings; diff --git a/logsrvd/logsrvd.h b/logsrvd/logsrvd.h index 47e1f457f..0d2550990 100644 --- a/logsrvd/logsrvd.h +++ b/logsrvd/logsrvd.h @@ -188,7 +188,6 @@ int store_suspend(CommandSuspend *msg, struct connection_closure *closure); int store_winsize(ChangeWindowSize *msg, struct connection_closure *closure); void iolog_close_all(struct connection_closure *closure); void iolog_details_free(struct iolog_details *details); -char ** strlist_copy(InfoMessage__StringList *strlist); /* logsrvd_conf.c */ bool logsrvd_conf_read(const char *path); diff --git a/plugins/audit_json/audit_json.c b/plugins/audit_json/audit_json.c index a803a3d02..95b939faf 100644 --- a/plugins/audit_json/audit_json.c +++ b/plugins/audit_json/audit_json.c @@ -258,12 +258,23 @@ add_key_value(struct json_container *json, const char *str) static bool add_array(struct json_container *json, const char *name, char * const * array) { + const char *cp; struct json_value json_value; debug_decl(add_array, SUDO_DEBUG_PLUGIN); - json_value.type = JSON_ARRAY; - json_value.u.array = array; - debug_return_bool(sudo_json_add_value(json, name, &json_value)); + if (!sudo_json_open_array(json, name)) + debug_return_bool(false); + while ((cp = *array) != NULL) { + json_value.type = JSON_STRING; + json_value.u.string = cp; + if (!sudo_json_add_value(json, name, &json_value)) + debug_return_bool(false); + array++; + } + if (!sudo_json_close_array(json)) + debug_return_bool(false); + + debug_return_bool(true); } static bool