Skip to content

Commit

Permalink
Add: function to request /scans/preferences and to get a list of pref…
Browse files Browse the repository at this point in the history
…erences
  • Loading branch information
jjnicola committed Jul 30, 2024
1 parent cd81282 commit 4b77d34
Show file tree
Hide file tree
Showing 2 changed files with 178 additions and 41 deletions.
196 changes: 163 additions & 33 deletions openvasd/openvasd.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ struct openvasd_connector
*/
struct openvasd_param
{
char *id; /**< Parameter id. */
char *value; /**< Parameter name. */
char *id; /**< Parameter id. */
char *name; /**< Parameter name. */
char *defval; /**< Default value. */
char *description; /**< Description. */
};

/**
Expand Down Expand Up @@ -747,14 +749,14 @@ openvasd_parsed_results (openvasd_connector_t *conn, unsigned long first,
if (!json_parser_load_from_data (parser, resp->body, strlen (resp->body),
&err))
{
goto cleanup;
goto res_cleanup;

Check warning on line 752 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L752

Added line #L752 was not covered by tests
}
reader = json_reader_new (json_parser_get_root (parser));

Check warning on line 754 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L754

Added line #L754 was not covered by tests

if (!json_reader_is_array (reader))
{
// No results. No information.
goto cleanup;
goto res_cleanup;

Check warning on line 759 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L759

Added line #L759 was not covered by tests
}

results_count = json_reader_count_elements (reader);

Check warning on line 762 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L762

Added line #L762 was not covered by tests
Expand All @@ -764,7 +766,7 @@ openvasd_parsed_results (openvasd_connector_t *conn, unsigned long first,
if (!json_reader_is_object (reader))
{
// error
goto cleanup;
goto res_cleanup;

Check warning on line 769 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L769

Added line #L769 was not covered by tests
}

json_reader_read_member (reader, "id");
Expand Down Expand Up @@ -839,7 +841,7 @@ openvasd_parsed_results (openvasd_connector_t *conn, unsigned long first,
ret = resp->code;

Check warning on line 841 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L840-L841

Added lines #L840 - L841 were not covered by tests
}

cleanup:
res_cleanup:
openvasd_response_free (resp);

Check warning on line 845 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L844-L845

Added lines #L844 - L845 were not covered by tests
if (reader)
g_object_unref (reader);
Expand All @@ -848,6 +850,7 @@ openvasd_parsed_results (openvasd_connector_t *conn, unsigned long first,
{
g_warning ("%s: Unable to parse scan results. Reason: %s", __func__,

Check warning on line 851 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L851

Added line #L851 was not covered by tests
err->message);
g_error_free (err);

Check warning on line 853 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L853

Added line #L853 was not covered by tests
}

return ret;

Check warning on line 856 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L856

Added line #L856 was not covered by tests
Expand Down Expand Up @@ -997,6 +1000,7 @@ openvasd_get_scan_progress_ext (openvasd_connector_t *conn,
{
g_warning ("%s: Unable to parse scan status. Reason: %s", __func__,

Check warning on line 1001 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1001

Added line #L1001 was not covered by tests
err->message);
g_error_free (err);

Check warning on line 1003 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1003

Added line #L1003 was not covered by tests
}
return progress;

Check warning on line 1005 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1005

Added line #L1005 was not covered by tests
}
Expand Down Expand Up @@ -1040,7 +1044,7 @@ openvasd_parsed_scan_status (openvasd_connector_t *conn)
parser = json_parser_new ();

Check warning on line 1044 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1044

Added line #L1044 was not covered by tests
if (!json_parser_load_from_data (parser, resp->body, strlen (resp->body),
&err))
goto cleanup;
goto status_cleanup;

Check warning on line 1047 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1047

Added line #L1047 was not covered by tests

reader = json_reader_new (json_parser_get_root (parser));

Check warning on line 1049 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1049

Added line #L1049 was not covered by tests

Expand All @@ -1060,7 +1064,7 @@ openvasd_parsed_scan_status (openvasd_connector_t *conn)

progress = openvasd_get_scan_progress_ext (NULL, resp);

Check warning on line 1065 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1065

Added line #L1065 was not covered by tests

cleanup:
status_cleanup:
openvasd_response_free (resp);

Check warning on line 1068 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1067-L1068

Added lines #L1067 - L1068 were not covered by tests
if (reader)
g_object_unref (reader);
Expand All @@ -1069,6 +1073,7 @@ openvasd_parsed_scan_status (openvasd_connector_t *conn)
{
g_warning ("%s: Unable to parse scan status. Reason: %s", __func__,

Check warning on line 1074 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1074

Added line #L1074 was not covered by tests
err->message);
g_error_free (err);

Check warning on line 1076 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1076

Added line #L1076 was not covered by tests
}

if (g_strcmp0 (status, "stored") == 0)
Expand Down Expand Up @@ -1142,6 +1147,156 @@ openvasd_get_health_started (openvasd_connector_t *conn)
return openvasd_send_request (conn, GET, "/health/started", NULL, NULL);

Check warning on line 1147 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1147

Added line #L1147 was not covered by tests
}

openvasd_resp_t
openvasd_get_scan_preferences (openvasd_connector_t *conn)

Check warning on line 1151 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1151

Added line #L1151 was not covered by tests
{
return openvasd_send_request (conn, GET, "/scans/preferences", NULL, NULL);

Check warning on line 1153 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1153

Added line #L1153 was not covered by tests
}

/**
* @brief Create a new Openvasd parameter.
*
* @return New Openvasd parameter.
*/
static openvasd_param_t *
openvasd_param_new (char *id, char *name, char *defval, char *description)

Check warning on line 1162 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1162

Added line #L1162 was not covered by tests
{
openvasd_param_t *param = g_malloc0 (sizeof (openvasd_param_t));

Check warning on line 1164 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1164

Added line #L1164 was not covered by tests

param->id = id;
param->defval = defval;
param->description = description;
param->name = name;
return param;

Check warning on line 1170 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1166-L1170

Added lines #L1166 - L1170 were not covered by tests
}

/**
* @brief Free an Openvasd parameter.
*
* @param[in] param Openvasd parameter to destroy.
*/
void
openvasd_param_free (openvasd_param_t *param)

Check warning on line 1179 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1179

Added line #L1179 was not covered by tests
{
if (!param)
return;
g_free (param->id);
g_free (param->name);
g_free (param->defval);
g_free (param->description);

Check warning on line 1186 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1182-L1186

Added lines #L1182 - L1186 were not covered by tests
}

int
openvasd_parsed_scans_preferences (openvasd_connector_t *conn, GSList **params)

Check warning on line 1190 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1190

Added line #L1190 was not covered by tests
{
openvasd_resp_t resp = NULL;
GError *err = NULL;

Check warning on line 1193 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1192-L1193

Added lines #L1192 - L1193 were not covered by tests
JsonParser *parser;
JsonReader *reader = NULL;

Check warning on line 1195 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1195

Added line #L1195 was not covered by tests

resp = openvasd_send_request (conn, GET, "/scans/preferences", NULL, NULL);

Check warning on line 1197 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1197

Added line #L1197 was not covered by tests

if (resp->code != 200)
return -1;

Check warning on line 1200 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1200

Added line #L1200 was not covered by tests

parser = json_parser_new ();

Check warning on line 1202 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1202

Added line #L1202 was not covered by tests
if (!json_parser_load_from_data (parser, resp->body, strlen (resp->body),
&err))
goto prefs_cleanup;
reader = json_reader_new (json_parser_get_root (parser));

Check warning on line 1206 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1205-L1206

Added lines #L1205 - L1206 were not covered by tests

if (!json_reader_is_array (reader))
{
// No results. No information.
goto prefs_cleanup;

Check warning on line 1211 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1211

Added line #L1211 was not covered by tests
}

int results_count = json_reader_count_elements (reader);

Check warning on line 1214 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1214

Added line #L1214 was not covered by tests
for (int i = 0; i < results_count; i++)
{
const char *id, *name, *desc;
char *defval = NULL;
openvasd_param_t *param = NULL;

Check warning on line 1219 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1218-L1219

Added lines #L1218 - L1219 were not covered by tests
GType t;
JsonNode *node = NULL;

Check warning on line 1221 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1221

Added line #L1221 was not covered by tests
gboolean bool;
int val;
char buf[6];

json_reader_read_element (reader, i);

Check warning on line 1226 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1226

Added line #L1226 was not covered by tests
if (!json_reader_is_object (reader))
{
// error
goto prefs_cleanup;

Check warning on line 1230 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1230

Added line #L1230 was not covered by tests
}

json_reader_read_member (reader, "id");
id = json_reader_get_string_value (reader);
json_reader_end_member (reader);

Check warning on line 1235 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1233-L1235

Added lines #L1233 - L1235 were not covered by tests

json_reader_read_member (reader, "name");
name = json_reader_get_string_value (reader);
json_reader_end_member (reader);

Check warning on line 1239 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1237-L1239

Added lines #L1237 - L1239 were not covered by tests

json_reader_read_member (reader, "description");
desc = json_reader_get_string_value (reader);
json_reader_end_member (reader);

Check warning on line 1243 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1241-L1243

Added lines #L1241 - L1243 were not covered by tests

json_reader_read_member (reader, "default");
node = json_reader_get_value (reader);
t = json_node_get_value_type (node);
val = json_reader_get_int_value (reader);

Check warning on line 1248 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1245-L1248

Added lines #L1245 - L1248 were not covered by tests

switch (t)

Check warning on line 1250 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1250

Added line #L1250 was not covered by tests
{
case G_TYPE_INT:

Check warning on line 1252 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1252

Added line #L1252 was not covered by tests
case G_TYPE_INT64:
val = json_reader_get_int_value (reader);
g_snprintf (buf, sizeof (buf), "%d", val);
defval = g_strdup (buf);
break;
case G_TYPE_STRING:
defval = g_strdup (json_reader_get_string_value (reader));
break;
case G_TYPE_BOOLEAN:
bool = json_reader_get_boolean_value (reader);

Check warning on line 1262 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1254-L1262

Added lines #L1254 - L1262 were not covered by tests
if (bool)
defval = g_strdup ("yes");

Check warning on line 1264 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1264

Added line #L1264 was not covered by tests
else
defval = g_strdup ("no");
break;
default:
g_warning ("%s: Unable to parse scan preferences.", __func__);
g_free (defval);
json_reader_end_member (reader);
json_reader_end_element (reader);
continue;

Check warning on line 1273 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1266-L1273

Added lines #L1266 - L1273 were not covered by tests
}
json_reader_end_member (reader);
json_reader_end_element (reader);

Check warning on line 1276 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1275-L1276

Added lines #L1275 - L1276 were not covered by tests

param = openvasd_param_new (g_strdup (id), g_strdup (name),
g_strdup (defval), g_strdup (desc));
g_free (defval);
*params = g_slist_append (*params, param);

Check warning on line 1281 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1278-L1281

Added lines #L1278 - L1281 were not covered by tests
}

prefs_cleanup:
openvasd_response_free (resp);

Check warning on line 1285 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1284-L1285

Added lines #L1284 - L1285 were not covered by tests
if (reader)
g_object_unref (reader);
g_object_unref (parser);

Check warning on line 1288 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1287-L1288

Added lines #L1287 - L1288 were not covered by tests
if (err != NULL)
{
g_warning ("%s: Unable to parse scan preferences. Reason: %s", __func__,

Check warning on line 1291 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1291

Added line #L1291 was not covered by tests
err->message);
g_error_free (err);
return -1;

Check warning on line 1294 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1293-L1294

Added lines #L1293 - L1294 were not covered by tests
}

return 0;

Check warning on line 1297 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1297

Added line #L1297 was not covered by tests
}

// Scan config builder
static void
add_port_to_scan_json (gpointer range, gpointer builder)

Check warning on line 1302 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1302

Added line #L1302 was not covered by tests
Expand Down Expand Up @@ -1403,31 +1558,6 @@ openvasd_build_scan_config_json (openvasd_target_t *target,
return json_str;

Check warning on line 1558 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1558

Added line #L1558 was not covered by tests
}

/**
* @brief Create a new Openvasd parameter.
*
* @return New Openvasd parameter.
*/
openvasd_param_t *
openvasd_param_new (void)
{
return g_malloc0 (sizeof (openvasd_param_t));
}

/**
* @brief Free an Openvasd parameter.
*
* @param[in] param Openvasd parameter to destroy.
*/
void
openvasd_param_free (openvasd_param_t *param)
{
if (!param)
return;
g_free (param->id);
g_free (param->value);
}

/**
* @brief Allocate and initialize a new Openvasd credential.
*
Expand Down
23 changes: 15 additions & 8 deletions openvasd/openvasd.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,26 @@ openvasd_get_scan_progress (openvasd_connector_t *);

openvasd_resp_t
openvasd_get_health_alive (openvasd_connector_t *);

openvasd_resp_t
openvasd_get_health_ready (openvasd_connector_t *);

openvasd_resp_t
openvasd_get_health_started (openvasd_connector_t *);

/* Scanner preferences */

typedef struct openvasd_param openvasd_param_t;

openvasd_resp_t
openvasd_get_scan_preferences (openvasd_connector_t *);

int
openvasd_parsed_scans_preferences (openvasd_connector_t *, GSList **);

void
openvasd_param_free (openvasd_param_t *);

/* Target builder */

typedef struct openvasd_target openvasd_target_t;
Expand All @@ -193,8 +208,6 @@ typedef struct openvasd_vt_single openvasd_vt_single_t;

typedef struct openvasd_credential openvasd_credential_t;

typedef struct openvasd_param openvasd_param_t;

openvasd_target_t *
openvasd_target_new (const char *, const char *, const char *, const char *,
int, int);
Expand All @@ -209,12 +222,6 @@ openvasd_target_add_alive_test_methods (openvasd_target_t *, gboolean, gboolean,
void
openvasd_target_free (openvasd_target_t *);

openvasd_param_t *
openvasd_param_new (void);

void
openvasd_param_free (openvasd_param_t *);

openvasd_credential_t *
openvasd_credential_new (const char *, const char *, const char *);

Expand Down

0 comments on commit 4b77d34

Please sign in to comment.