Skip to content

Commit

Permalink
improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
jjnicola committed Oct 10, 2024
1 parent a035b05 commit d8a3725
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 47 deletions.
88 changes: 51 additions & 37 deletions openvasd/openvasd.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include <curl/curl.h>
#include <curl/easy.h>
#include <curl/multi.h>
#include <gnutls/gnutls.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
Expand Down Expand Up @@ -231,6 +230,7 @@ openvasd_response_free (openvasd_resp_t resp)
resp = NULL;

Check warning on line 230 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L227-L230

Added lines #L227 - L230 were not covered by tests
}


/** @brief Initialize the string struct to hold the response
*
* @param s[in/out] The string struct to be initialized
Expand All @@ -248,6 +248,17 @@ init_stringstream (stringstream *s)
s->ptr[0] = '\0';

Check warning on line 248 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L248

Added line #L248 was not covered by tests
}

/** @brief Reinitialize the string struct to hold the response
*
* @param s[in/out] The string struct to be initialized
*/
static void
reset_stringstream (stringstream *s)

Check warning on line 256 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L256

Added line #L256 was not covered by tests
{
g_free (s->ptr);
init_stringstream(s);

Check warning on line 259 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L258-L259

Added lines #L258 - L259 were not covered by tests
}

/** @brief Call back function to stored the response.
*
* @description The function signature is the necessary to work with
Expand Down Expand Up @@ -516,29 +527,34 @@ openvasd_get_version (openvasd_connector_t *conn)
g_free (resp.ptr);
return response;

Check warning on line 528 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L527-L528

Added lines #L527 - L528 were not covered by tests
}

struct curl_handlers
{
CURLM *mhnd;
CURL *hnd;
};

/**
* @brief Wrapps a CURLM * handler
*/
static curl_handler_t *
curlm_handler_new (void)
curlm_t
openvasd_curlm_handler_new (void)

Check warning on line 534 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L534

Added line #L534 was not covered by tests
{
curl_handler_t *handlers = g_malloc0 (sizeof (curl_handler_t));
return handlers;
CURLM *h = NULL;
return h;

Check warning on line 537 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L536-L537

Added lines #L536 - L537 were not covered by tests
}

void
openvasd_curl_handler_close (curl_handler_t *h)
openvasd_curl_handler_close (curlm_t *h)

Check warning on line 541 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L541

Added line #L541 was not covered by tests
{
curl_multi_remove_handle (h->mhnd, h->hnd);
curl_easy_cleanup (h->hnd);
curl_multi_cleanup (h->mhnd);
int queued = 0;

Check warning on line 543 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L543

Added line #L543 was not covered by tests

/* when an easy handle has completed, remove it */
CURLMsg *msg = curl_multi_info_read (h, &queued);

Check warning on line 546 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L546

Added line #L546 was not covered by tests
if (msg)
{
if (msg->msg == CURLMSG_DONE)
{
curl_multi_remove_handle (h, msg->easy_handle);
curl_easy_cleanup (msg->easy_handle);
curl_multi_cleanup (h);
return;

Check warning on line 554 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L551-L554

Added lines #L551 - L554 were not covered by tests
}
g_warning ("%s: Not possible to clean up the curl handler", __func__);

Check warning on line 556 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L556

Added line #L556 was not covered by tests
}
}

/**
Expand All @@ -552,15 +568,14 @@ openvasd_curl_handler_close (curl_handler_t *h)
* @return The response. Null on error.
*/
openvasd_resp_t
openvasd_get_vts_stream_init (openvasd_connector_t *conn, curl_handler_t **h,
openvasd_get_vts_stream_init (openvasd_connector_t *conn, curlm_t *mhnd,

Check warning on line 571 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L571

Added line #L571 was not covered by tests
stringstream *resp)
{
GString *path;
openvasd_resp_t response = NULL;
char *err = NULL;
CURL *hnd = NULL;

*h = curlm_handler_new ();
CURLM *h = NULL;
response = g_malloc0 (sizeof (struct openvasd_response));

Check warning on line 579 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L575-L579

Added lines #L575 - L579 were not covered by tests
if (response == NULL)
return NULL;

Check warning on line 581 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L581

Added line #L581 was not covered by tests
Expand All @@ -576,9 +591,9 @@ openvasd_get_vts_stream_init (openvasd_connector_t *conn, curl_handler_t **h,
}
g_string_free (path, TRUE);

Check warning on line 592 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L592

Added line #L592 was not covered by tests

(*h)->mhnd = curl_multi_init ();
curl_multi_add_handle ((*h)->mhnd, hnd);
(*h)->hnd = hnd;
h = curl_multi_init ();
curl_multi_add_handle (h, hnd);
*mhnd = h;

Check warning on line 596 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L594-L596

Added lines #L594 - L596 were not covered by tests

response->code = RESP_CODE_OK;
return response;

Check warning on line 599 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L598-L599

Added lines #L598 - L599 were not covered by tests
Expand All @@ -596,19 +611,19 @@ openvasd_get_vts_stream_init (openvasd_connector_t *conn, curl_handler_t **h,
* transmision finished. -1 on error
*/
int
openvasd_get_vts_stream (curl_handler_t *h)
openvasd_get_vts_stream (curlm_t mhnd)

Check warning on line 614 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L614

Added line #L614 was not covered by tests
{
static int running = 0;

if (!(h->mhnd))
CURLM *h = mhnd;

Check warning on line 617 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L617

Added line #L617 was not covered by tests
if (!(h))
{
return -1;

Check warning on line 620 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L620

Added line #L620 was not covered by tests
}

CURLMcode mc = curl_multi_perform (h->mhnd, &running);
CURLMcode mc = curl_multi_perform (h, &running);

Check warning on line 623 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L623

Added line #L623 was not covered by tests
if (!mc && running)
/* wait for activity, timeout or "nothing" */
mc = curl_multi_poll (h->mhnd, NULL, 0, 5000, NULL);
mc = curl_multi_poll (h, NULL, 0, 5000, NULL);

Check warning on line 626 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L626

Added line #L626 was not covered by tests
if (mc != CURLM_OK)
{
g_warning ("%s: error on curl_multi_poll(): %d\n", __func__, mc);
Expand Down Expand Up @@ -723,7 +738,8 @@ openvasd_start_scan (openvasd_connector_t *conn, char *data)
}
response->code = RESP_CODE_ERR;
g_free (resp.ptr);
goto cleanup_start_scan;
cJSON_Delete (parser);
return response;

Check warning on line 742 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L739-L742

Added lines #L739 - L742 were not covered by tests
}

(*conn)->scan_id = g_strdup (cJSON_GetStringValue (parser));

Check warning on line 745 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L745

Added line #L745 was not covered by tests
Expand All @@ -741,11 +757,11 @@ openvasd_start_scan (openvasd_connector_t *conn, char *data)
response->body = g_strdup ("{\"error\": \"Missing scan ID\"}");
g_string_free (path, TRUE);
g_warning ("%s: Missing scan ID", __func__);
g_free (resp.ptr);
goto cleanup_start_scan;
cJSON_Delete (parser);
return response;

Check warning on line 761 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L756-L761

Added lines #L756 - L761 were not covered by tests
}
g_free (resp.ptr);
init_stringstream (&resp);

reset_stringstream (&resp);

Check warning on line 764 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L764

Added line #L764 was not covered by tests
if ((hnd = handler (conn, POST, path->str, "{\"action\": \"start\"}", &resp,
&err))
== NULL)
Expand All @@ -771,9 +787,7 @@ openvasd_start_scan (openvasd_connector_t *conn, char *data)
return response;

Check warning on line 787 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L785-L787

Added lines #L785 - L787 were not covered by tests
}

cleanup_start_scan:
cJSON_Delete (parser);

response->body = g_strdup (resp.ptr);
g_free (resp.ptr);
return response;

Check warning on line 793 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L790-L793

Added lines #L790 - L793 were not covered by tests
Expand Down Expand Up @@ -1000,12 +1014,12 @@ openvasd_parsed_results (openvasd_connector_t *conn, unsigned long first,
const char *err = NULL;
openvasd_resp_t resp = NULL;
openvasd_result_t result = NULL;
unsigned long id;
unsigned long id = 0;
gchar *type = NULL;
gchar *ip_address = NULL;
gchar *hostname = NULL;
gchar *oid = NULL;
int port;
int port = 0;
gchar *protocol = NULL;
gchar *message = NULL;
gchar *detail_name = NULL;
Expand Down Expand Up @@ -1701,7 +1715,7 @@ openvasd_parsed_scans_preferences (openvasd_connector_t *conn, GSList **params)

cJSON_ArrayForEach (param_obj, parser)

Check warning on line 1716 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1716

Added line #L1716 was not covered by tests
{
const char *id, *name, *desc;
const char *id = NULL, *name = NULL, *desc = NULL;
char *defval = NULL, *param_type = NULL;
openvasd_param_t *param = NULL;
int val, mandatory = 0;

Check warning on line 1721 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1718-L1721

Added lines #L1718 - L1721 were not covered by tests
Expand Down
16 changes: 9 additions & 7 deletions openvasd/openvasd.h
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,7 @@ openvasd_build_scan_config_json (openvasd_target_t *, GHashTable *, GSList *);

/* Curl multiperform wrapper */

typedef struct curl_handlers curl_handler_t;

void
openvasd_curl_handler_close (curl_handler_t *);
typedef void *curlm_t;

/** @brief Define a string struct for storing the response.
*/
Expand All @@ -288,11 +285,16 @@ typedef struct string
void
init_stringstream (stringstream *s);

curlm_t
openvasd_curlm_handler_new (void);

void
openvasd_curl_handler_close (curlm_t *);

openvasd_resp_t
openvasd_get_vts_stream_init (openvasd_connector_t *, curl_handler_t **,
openvasd_get_vts_stream_init (openvasd_connector_t *, curlm_t *,
stringstream *);

int
openvasd_get_vts_stream (curl_handler_t *);
int openvasd_get_vts_stream (curlm_t);

#endif
6 changes: 4 additions & 2 deletions openvasd/vtparser.c
Original file line number Diff line number Diff line change
Expand Up @@ -323,17 +323,19 @@ openvasd_parse_vt (gvm_json_pull_parser_t *parser, gvm_json_pull_event_t *event)
g_debug ("%s: Start parsing feed", __func__);

Check warning on line 323 in openvasd/vtparser.c

View check run for this annotation

Codecov / codecov/patch

openvasd/vtparser.c#L322-L323

Added lines #L322 - L323 were not covered by tests
}
else if (!g_strcmp0 (path, "$")
&& event->type == GVM_JSON_PULL_EVENT_ARRAY_END)
&& (event->type == GVM_JSON_PULL_EVENT_ARRAY_END
|| event->type == GVM_JSON_PULL_EVENT_EOF))

Check warning on line 327 in openvasd/vtparser.c

View check run for this annotation

Codecov / codecov/patch

openvasd/vtparser.c#L325-L327

Added lines #L325 - L327 were not covered by tests
{
g_debug ("%s: Finish parsing feed", __func__);
g_free (path);
return NULL;

Check warning on line 331 in openvasd/vtparser.c

View check run for this annotation

Codecov / codecov/patch

openvasd/vtparser.c#L329-L331

Added lines #L329 - L331 were not covered by tests
}
g_free (path);

Check warning on line 333 in openvasd/vtparser.c

View check run for this annotation

Codecov / codecov/patch

openvasd/vtparser.c#L333

Added line #L333 was not covered by tests

// It is an NVT object
if (event->type != GVM_JSON_PULL_EVENT_OBJECT_START)
{
g_message ("%s: Error reading VT object", __func__);
g_warning ("%s: Error reading VT object", __func__);
return NULL;

Check warning on line 339 in openvasd/vtparser.c

View check run for this annotation

Codecov / codecov/patch

openvasd/vtparser.c#L338-L339

Added lines #L338 - L339 were not covered by tests
}

Expand Down
2 changes: 1 addition & 1 deletion util/jsonpull.c
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ gvm_json_pull_parser_next (gvm_json_pull_parser_t *parser,
return;
}
}

event->path = parser->path;

// Delayed addition to path after a container start element
Expand Down

0 comments on commit d8a3725

Please sign in to comment.