Skip to content

Commit

Permalink
improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
jjnicola committed Oct 9, 2024
1 parent a035b05 commit 71207be
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 46 deletions.
75 changes: 39 additions & 36 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 @@ -238,6 +237,7 @@ openvasd_response_free (openvasd_resp_t resp)
void
init_stringstream (stringstream *s)

Check warning on line 238 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L238

Added line #L238 was not covered by tests
{
g_free (s->ptr);
s->len = 0;
s->ptr = g_malloc0 (s->len + 1);

Check warning on line 242 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L240-L242

Added lines #L240 - L242 were not covered by tests
if (s->ptr == NULL)
Expand Down Expand Up @@ -516,29 +516,34 @@ openvasd_get_version (openvasd_connector_t *conn)
g_free (resp.ptr);
return response;

Check warning on line 517 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L516-L517

Added lines #L516 - L517 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 523 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L523

Added line #L523 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 526 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L525-L526

Added lines #L525 - L526 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 530 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L530

Added line #L530 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 532 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L532

Added line #L532 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 535 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L535

Added line #L535 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 543 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L540-L543

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

Check warning on line 545 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L545

Added line #L545 was not covered by tests
}
}

/**
Expand All @@ -552,15 +557,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 560 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L560

Added line #L560 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 568 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L564-L568

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

Check warning on line 570 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L570

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

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

(*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 585 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L583-L585

Added lines #L583 - L585 were not covered by tests

response->code = RESP_CODE_OK;
return response;

Check warning on line 588 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L587-L588

Added lines #L587 - L588 were not covered by tests
Expand All @@ -596,19 +600,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 603 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L603

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

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

Check warning on line 606 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L606

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

Check warning on line 609 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L609

Added line #L609 was not covered by tests
}

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

Check warning on line 612 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L612

Added line #L612 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 615 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L615

Added line #L615 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 +727,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 731 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L728-L731

Added lines #L728 - L731 were not covered by tests
}

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

Check warning on line 734 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L734

Added line #L734 was not covered by tests
Expand All @@ -741,10 +746,10 @@ 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 750 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L745-L750

Added lines #L745 - L750 were not covered by tests
}
g_free (resp.ptr);

init_stringstream (&resp);

Check warning on line 753 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L753

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

Check warning on line 776 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L774-L776

Added lines #L774 - L776 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 782 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L779-L782

Added lines #L779 - L782 were not covered by tests
Expand Down Expand Up @@ -1000,12 +1003,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 +1704,7 @@ openvasd_parsed_scans_preferences (openvasd_connector_t *conn, GSList **params)

cJSON_ArrayForEach (param_obj, parser)

Check warning on line 1705 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1705

Added line #L1705 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 1710 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1707-L1710

Added lines #L1707 - L1710 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 71207be

Please sign in to comment.