Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scan start end time #874

Merged
merged 2 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 47 additions & 33 deletions openvasd/openvasd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1184,7 +1184,8 @@
&& cJSON_IsString (source_obj))
detail_source_name = g_strdup (source_obj->valuestring);

if ((source_obj = cJSON_GetObjectItem (detail_obj, "description")) != NULL
if ((source_obj = cJSON_GetObjectItem (detail_obj, "description"))
!= NULL
&& cJSON_IsString (source_obj))
detail_source_description = g_strdup (source_obj->valuestring);
}
Expand All @@ -1199,7 +1200,7 @@
ret = 200;
}

res_cleanup:
res_cleanup:
if (err != NULL)
{
g_warning ("%s: Unable to parse scan results. Reason: %s", __func__, err);
Expand Down Expand Up @@ -1410,6 +1411,48 @@
return status_code;
}

static int
parse_status (const gchar *body, openvasd_scan_status_t status_info)
{
cJSON *parser = NULL;
cJSON *status = NULL;
gchar *status_val = NULL;
time_t start_time = 0, end_time = 0;
openvasd_status_t status_code = OPENVASD_SCAN_STATUS_ERROR;

if (!status_info)
return -1;

Check warning on line 1424 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1424

Added line #L1424 was not covered by tests

if ((parser = cJSON_Parse (body)) == NULL)
return -1;

Check warning on line 1427 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1427

Added line #L1427 was not covered by tests

if ((status = cJSON_GetObjectItem (parser, "status")) == NULL
|| !cJSON_IsString (status))
{
cJSON_Delete (parser);
return -1;

Check warning on line 1433 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1432-L1433

Added lines #L1432 - L1433 were not covered by tests
}

status_val = g_strdup (status->valuestring);
status_code = get_status_code_from_openvas (status_val);
g_free (status_val);

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;
cJSON_Delete (parser);

status_info->status = status_code;
status_info->end_time = end_time;
status_info->start_time = start_time;

return 0;
}

/** @brief Return a struct with the general scan status
*
* @param conn Openvasd connector data
Expand All @@ -1420,53 +1463,24 @@
openvasd_scan_status_t
openvasd_parsed_scan_status (openvasd_connector_t conn)
{
cJSON *parser = NULL;
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;
openvasd_scan_status_t status_info = NULL;

Check warning on line 1469 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1469

Added line #L1469 was not covered by tests

resp = openvasd_get_scan_status (conn);

status_info = g_malloc0 (sizeof (struct openvasd_scan_status));
if (resp->code != 200)
if (resp->code != 200 || !parse_status (resp->body, status_info))
{
status_info->status = status_code;
status_info->response_code = resp->code;
openvasd_response_cleanup (resp);
return status_info;
}
if ((parser = cJSON_Parse (resp->body)) == NULL)
goto status_cleanup;

if ((status = cJSON_GetObjectItem (parser, "status")) == NULL
|| !cJSON_IsString (status))
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:
openvasd_response_cleanup (resp);
cJSON_Delete (parser);

status_code = get_status_code_from_openvas (status_val);
g_free (status_val);

status_info->status = status_code;
status_info->end_time = end_time;
status_info->start_time = start_time;
status_info->progress = progress;

return status_info;
Expand Down
54 changes: 49 additions & 5 deletions openvasd/openvasd_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@

#include "openvasd.c"

#include <cgreen/assertions.h>
#include <cgreen/cgreen.h>
#include <cgreen/constraint_syntax_helpers.h>
#include <cgreen/internal/c_assertions.h>
#include <cgreen/mocks.h>

Describe (openvasd);
Expand All @@ -25,16 +28,19 @@ Ensure (openvasd, parse_results_handles_details)
GSList *results;
openvasd_result_t result;


results = NULL;

str = "[ {"
str =
"[ {"
" \"id\": 16,"
" \"type\": \"host_detail\","
" \"ip_address\": \"192.168.0.101\","
" \"hostname\": \"g\","
" \"oid\": \"1.3.6.1.4.1.25623.1.0.103997\","
" \"message\": \"<host><detail><name>MAC</name><value>94:E6:F7:67:4B:C0</value><source><type>nvt</type><name>1.3.6.1.4.1.25623.1.0.103585</name><description>Nmap MAC Scan</description></source></detail></host>\","
" \"message\": "
"\"<host><detail><name>MAC</name><value>94:E6:F7:67:4B:C0</"
"value><source><type>nvt</type><name>1.3.6.1.4.1.25623.1.0.103585</"
"name><description>Nmap MAC Scan</description></source></detail></host>\","
" \"detail\": {"
" \"name\": \"MAC\","
" \"value\": \"00:1A:2B:3C:4D:5E\","
Expand All @@ -54,13 +60,50 @@ Ensure (openvasd, parse_results_handles_details)
assert_that (result->detail_name, is_equal_to_string ("MAC"));
assert_that (result->detail_value, is_equal_to_string ("00:1A:2B:3C:4D:5E"));
assert_that (result->detail_source_type, is_equal_to_string ("nvt"));
assert_that (result->detail_source_name, is_equal_to_string ("1.3.6.1.4.1.25623.1.0.103585"));
assert_that (result->detail_source_description, is_equal_to_string ("Nmap MAC Scan"));
assert_that (result->detail_source_name,
is_equal_to_string ("1.3.6.1.4.1.25623.1.0.103585"));
assert_that (result->detail_source_description,
is_equal_to_string ("Nmap MAC Scan"));

if (g_slist_length (results))
g_slist_free_full (results, (GDestroyNotify) openvasd_result_free);
}

/* parse_status */

Ensure (openvasd, parse_status_start_end_time)
{
const gchar *str;
openvasd_scan_status_t openvasd_scan_status = NULL;

openvasd_scan_status = g_malloc0 (sizeof (struct openvasd_scan_status));
str = "{"
" \"start_time\":1737642308,"
" \"end_time\":1737642389,"
" \"status\":\"succeeded\","
" \"host_info\":{"
" \"all\":1,"
" \"excluded\":0,"
" \"dead\":0,"
" \"alive\":1,"
" \"queued\":0,"
" \"finished\":1,"
" \"scanning\":{},"
" \"remaining_vts_per_host\":{}"
" }"
"}";

parse_status (str, openvasd_scan_status);

assert_that (openvasd_scan_status->status, is_equal_to (4));
assert_that_double (openvasd_scan_status->start_time,
is_equal_to_double (1737642308));
assert_that_double (openvasd_scan_status->end_time,
is_equal_to_double (1737642389));

g_free (openvasd_scan_status);
}

/* Test suite. */
int
main (int argc, char **argv)
Expand All @@ -70,6 +113,7 @@ main (int argc, char **argv)
suite = create_test_suite ();

add_test_with_context (suite, openvasd, parse_results_handles_details);
add_test_with_context (suite, openvasd, parse_status_start_end_time);

if (argc > 1)
return run_single_test (suite, argv[1], create_text_reporter ());
Expand Down
Loading