diff --git a/openvasd/openvasd.c b/openvasd/openvasd.c index a8ed0e48..7298e013 100644 --- a/openvasd/openvasd.c +++ b/openvasd/openvasd.c @@ -12,6 +12,7 @@ #include "../base/array.h" #include "../base/networking.h" +#include "../util/json.h" #include #include @@ -1095,7 +1096,6 @@ openvasd_parsed_results (openvasd_connector_t conn, unsigned long first, const gchar *err = NULL; openvasd_resp_t resp = NULL; openvasd_result_t result = NULL; - unsigned long id = 0; gchar *type = NULL; gchar *ip_address = NULL; gchar *hostname = NULL; @@ -1133,10 +1133,6 @@ openvasd_parsed_results (openvasd_connector_t conn, unsigned long first, // error goto res_cleanup; - if ((item = cJSON_GetObjectItem (result_obj, "id")) != NULL - && cJSON_IsNumber (item)) - id = item->valuedouble; - if ((item = cJSON_GetObjectItem (result_obj, "type")) != NULL && cJSON_IsString (item)) type = g_strdup (item->valuestring); @@ -1193,7 +1189,8 @@ openvasd_parsed_results (openvasd_connector_t conn, unsigned long first, detail_source_description = g_strdup (detail_obj->valuestring); } - result = openvasd_result_new (id, type, ip_address, hostname, oid, port, + result = openvasd_result_new (gvm_json_obj_double (result_obj, "id"), + type, ip_address, hostname, oid, port, protocol, message, detail_name, detail_value, detail_source_type, detail_source_name, detail_source_description); @@ -1410,7 +1407,6 @@ openvasd_parsed_scan_status (openvasd_connector_t conn) cJSON *status = NULL; openvasd_resp_t resp = NULL; gchar *status_val = NULL; - time_t start_time = 0, end_time = 0; int progress = -1; openvasd_status_t status_code = OPENVASD_SCAN_STATUS_ERROR; openvasd_scan_status_t status_info; @@ -1433,14 +1429,6 @@ openvasd_parsed_scan_status (openvasd_connector_t conn) goto status_cleanup; status_val = g_strdup (status->valuestring); - if ((status = cJSON_GetObjectItem (parser, "start_time")) != NULL - && !cJSON_IsNumber (status)) - start_time = status->valuedouble; - - if ((status = cJSON_GetObjectItem (parser, "end_time")) != NULL - && !cJSON_IsNumber (status)) - end_time = status->valuedouble; - progress = openvasd_get_scan_progress_ext (NULL, resp); status_cleanup: @@ -1451,8 +1439,8 @@ openvasd_parsed_scan_status (openvasd_connector_t conn) g_free (status_val); status_info->status = status_code; - status_info->end_time = end_time; - status_info->start_time = start_time; + status_info->end_time = gvm_json_obj_double (parser, "end_time"); + status_info->start_time = gvm_json_obj_double (parser, "start_time"); status_info->progress = progress; return status_info; diff --git a/openvasd/vtparser.c b/openvasd/vtparser.c index 2007598e..ce51499c 100644 --- a/openvasd/vtparser.c +++ b/openvasd/vtparser.c @@ -83,13 +83,9 @@ add_tags_to_nvt (nvti_t *nvt, cJSON *tag_obj) && cJSON_IsString (item)) nvti_set_affected (nvt, item->valuestring); - if ((item = cJSON_GetObjectItem (tag_obj, "creation_date")) != NULL - && cJSON_IsNumber (item)) - nvti_set_creation_time (nvt, item->valuedouble); + nvti_set_creation_time (nvt, gvm_json_obj_double (tag_obj, "creation_date")); - if ((item = cJSON_GetObjectItem (tag_obj, "last_modification")) != NULL - && cJSON_IsNumber (item)) - nvti_set_modification_time (nvt, item->valuedouble); + nvti_set_modification_time (nvt, gvm_json_obj_double (tag_obj, "last_modification")); if ((item = cJSON_GetObjectItem (tag_obj, "insight")) != NULL && cJSON_IsString (item)) @@ -150,7 +146,6 @@ add_tags_to_nvt (nvti_t *nvt, cJSON *tag_obj) gchar *severity_origin = NULL, *severity_type = NULL; gchar *cvss_base; - time_t severity_date = 0; double cvss_base_dbl; if (g_strrstr (severity_vector, "CVSS:3")) @@ -160,17 +155,14 @@ add_tags_to_nvt (nvti_t *nvt, cJSON *tag_obj) cvss_base_dbl = get_cvss_score_from_base_metrics (severity_vector); - if ((item = cJSON_GetObjectItem (tag_obj, "severity_date")) != NULL - && cJSON_IsNumber (item)) - severity_date = item->valuedouble; - if ((item = cJSON_GetObjectItem (tag_obj, "severity_origin")) != NULL && cJSON_IsString (item)) severity_origin = item->valuestring; - nvti_add_vtseverity ( - nvt, vtseverity_new (severity_type, severity_origin, severity_date, - cvss_base_dbl, severity_vector)); + nvti_add_vtseverity (nvt, + vtseverity_new (severity_type, severity_origin, + gvm_json_obj_double (tag_obj, "severity_date"), + cvss_base_dbl, severity_vector)); nvti_add_tag (nvt, "cvss_base_vector", severity_vector); diff --git a/util/json.c b/util/json.c index d33a001b..c865b2b9 100644 --- a/util/json.c +++ b/util/json.c @@ -62,3 +62,23 @@ gvm_json_string_escape (const char *string, gboolean single_quote) } return g_string_free (escaped, FALSE); } + +/** + * @brief Get a double field from a JSON object. + * + * @param[in] obj Object + * @param[in] key Field name. + * + * @return A double. + */ +double +gvm_json_obj_double (cJSON *obj, const gchar *key) +{ + cJSON *item; + + item = cJSON_GetObjectItem (obj, key); + if (item && cJSON_IsNumber (item)) + return item->valuedouble; + + return 0; +} diff --git a/util/json.h b/util/json.h index e72e1fce..277be4db 100644 --- a/util/json.h +++ b/util/json.h @@ -15,4 +15,7 @@ gchar * gvm_json_string_escape (const char *, gboolean); +double +gvm_json_obj_double (cJSON *, const gchar *); + #endif /* _GVM_JSON_H */