Skip to content

Commit

Permalink
Improvement: send the lsc data when it is gathered. (greenbone#1203)
Browse files Browse the repository at this point in the history
* Add: Add new ipc data type IPC_DT_LSC

* Add: Add function to handle LSC data type

* Change: Update existing general IPC function to handle data with the new type LSC

* Add: Add functions to access the internal ipc_data_t information related to the table driven LSC

* Add: Add NASL function update_table_driven_lsc_data()

This function sends the necessary data for starting a table driven LSC to the parent process

* Change: Modify run_table_driven_lsc(). Also, moved to `table_driven_lsc.c`

It receives now the the package list and os release, and don't have to look into redis

* Change: Make the change backward compatible with older feed versions.
If LSC check is not started via IPC, it will run at the end of the scan as before.

---------

Co-authored-by: Arno Stiefvater <[email protected]>
  • Loading branch information
jjnicola and ArnoStiefvater authored Dec 14, 2023
1 parent 85e8f9a commit 474b4bf
Show file tree
Hide file tree
Showing 9 changed files with 436 additions and 212 deletions.
108 changes: 102 additions & 6 deletions misc/ipc_openvas.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ struct ipc_user_agent

typedef struct ipc_user_agent ipc_user_agent_t;

// ipc_lsc is used to send / retrieve the table driven LSC data.
struct ipc_lsc
{
gboolean data_ready; // flag indicating that lsc data is in the kb
};

typedef struct ipc_lsc ipc_lsc_t;

// ipc_data is used to send / retrieve a given data of the union member
struct ipc_data
{
Expand All @@ -42,6 +50,7 @@ struct ipc_data
{
ipc_user_agent_t *ipc_user_agent;
ipc_hostname_t *ipc_hostname;
ipc_lsc_t *ipc_lsc;
};
};

Expand Down Expand Up @@ -110,6 +119,22 @@ ipc_get_user_agent_from_data (ipc_data_t *data)
return data->ipc_user_agent->user_agent;
}

/**
* @brief Get the package list from LSC IPC data
*
* @param data Data structure of IPC_DT_LSC type.
*
* @Return True if the data is ready for running with LSC, False otherwise.
*/
gboolean
ipc_get_lsc_data_ready_flag (ipc_data_t *data)
{
if (data == NULL || (ipc_get_data_type_from_data (data) != IPC_DT_LSC))
return FALSE;

return data->ipc_lsc->data_ready;
}

// Hostname

/**
Expand All @@ -120,11 +145,11 @@ ipc_get_user_agent_from_data (ipc_data_t *data)
*
* @return a heap initialized ipc_data or NULL on failure.
*/
struct ipc_data *
ipc_data_t *
ipc_data_type_from_hostname (const char *source, size_t source_len,
const char *hostname, size_t hostname_len)
{
struct ipc_data *data = NULL;
ipc_data_t *data = NULL;
ipc_hostname_t *hnd = NULL;
if (source == NULL || hostname == NULL)
return NULL;
Expand Down Expand Up @@ -169,10 +194,10 @@ ipc_hostname_destroy (ipc_hostname_t *data)
*
* @return a heap initialized ipc_data or NULL on failure.
*/
struct ipc_data *
ipc_data_t *
ipc_data_type_from_user_agent (const char *user_agent, size_t user_agent_len)
{
struct ipc_data *data = NULL;
ipc_data_t *data = NULL;
ipc_user_agent_t *uad = NULL;
gchar *ua_str = NULL;

Expand Down Expand Up @@ -212,6 +237,51 @@ ipc_user_agent_destroy (ipc_user_agent_t *data)
g_free (data);
}

// Table driven LSC

/**
* @brief initializes ipc_data for the table driven LSC.
*
* @param os_release The OS release
*
* @return a heap initialized ipc_data or NULL on failure.
*/
ipc_data_t *
ipc_data_type_from_lsc (gboolean data_ready)
{
ipc_data_t *data = NULL;
ipc_lsc_t *lscd = NULL;

if (data_ready != FALSE && data_ready != TRUE)
return NULL;

if ((data = calloc (1, sizeof (*data))) == NULL)
return NULL;
data->type = IPC_DT_LSC;

if ((lscd = calloc (1, sizeof (*lscd))) == NULL)
goto failure_exit;

lscd->data_ready = data_ready;
data->ipc_lsc = lscd;
return data;

failure_exit:
free (data);
return NULL;
}

/**
* @brief Free a LSC data structure
*
* @param data The lsc data structure to be free()'ed
*/
static void
ipc_lsc_destroy (ipc_lsc_t *data)
{
g_free (data);
}

// General IPC data functios

/**
Expand All @@ -233,7 +303,11 @@ ipc_data_destroy (ipc_data_t **data)
case IPC_DT_USER_AGENT:
ipc_user_agent_destroy ((*data)->ipc_user_agent);
break;
case IPC_DT_LSC:
ipc_lsc_destroy ((*data)->ipc_lsc);
break;
case IPC_DT_ERROR:
case IPC_DT_NO_DATA:
break;
}
g_free (*data);
Expand All @@ -248,14 +322,15 @@ ipc_data_destroy (ipc_data_t **data)
* @return a heap allocated achar array containing the json or NULL on failure.
*/
const char *
ipc_data_to_json (struct ipc_data *data)
ipc_data_to_json (ipc_data_t *data)
{
JsonBuilder *builder;
JsonGenerator *gen;
JsonNode *root;
gchar *json_str;
ipc_hostname_t *hn = NULL;
ipc_user_agent_t *ua = NULL;
ipc_lsc_t *lsc = NULL;
enum ipc_data_type type = IPC_DT_ERROR;

if (data == NULL)
Expand Down Expand Up @@ -286,6 +361,12 @@ ipc_data_to_json (struct ipc_data *data)
builder = json_builder_add_string_value (builder, ua->user_agent);
break;

case IPC_DT_LSC:
lsc = data->ipc_lsc;
json_builder_set_member_name (builder, "data_ready");
builder = json_builder_add_boolean_value (builder, lsc->data_ready);
break;

default:
g_warning ("%s: Unknown data type %d.", __func__, type);
}
Expand Down Expand Up @@ -315,7 +396,7 @@ ipc_data_to_json (struct ipc_data *data)
*
* @return a heap allocated ipc_data or NULL on failure.
*/
struct ipc_data *
ipc_data_t *
ipc_data_from_json (const char *json, size_t len)
{
JsonParser *parser = NULL;
Expand All @@ -325,6 +406,8 @@ ipc_data_from_json (const char *json, size_t len)
ipc_data_t *ret = NULL;
ipc_user_agent_t *ua;
ipc_hostname_t *hn;
ipc_lsc_t *lsc;

enum ipc_data_type type = IPC_DT_ERROR;

if ((ret = calloc (1, sizeof (*ret))) == NULL)
Expand Down Expand Up @@ -354,6 +437,7 @@ ipc_data_from_json (const char *json, size_t len)
switch (type)
{
case IPC_DT_ERROR:
case IPC_DT_NO_DATA:
goto cleanup;
case IPC_DT_HOSTNAME:
if ((hn = calloc (1, sizeof (*hn))) == NULL)
Expand Down Expand Up @@ -391,6 +475,18 @@ ipc_data_from_json (const char *json, size_t len)
json_reader_end_member (reader);
ret->ipc_user_agent = ua;
break;

case IPC_DT_LSC:
if ((lsc = calloc (1, sizeof (*lsc))) == NULL)
goto cleanup;
if (!json_reader_read_member (reader, "data_ready"))
{
goto cleanup;
}
lsc->data_ready = json_reader_get_boolean_value (reader);
json_reader_end_member (reader);
ret->ipc_lsc = lsc;
break;
}

cleanup:
Expand Down
15 changes: 13 additions & 2 deletions misc/ipc_openvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
enum ipc_data_type
{
IPC_DT_ERROR = -1,
IPC_DT_HOSTNAME = 0,
IPC_DT_USER_AGENT,
IPC_DT_NO_DATA = 0,
IPC_DT_HOSTNAME = 1,
IPC_DT_USER_AGENT = 2,
IPC_DT_LSC = 4,
};

typedef struct ipc_data ipc_data_t;
Expand All @@ -33,6 +35,12 @@ ipc_get_hostname_source_from_data (ipc_data_t *data);
gchar *
ipc_get_user_agent_from_data (ipc_data_t *data);

gboolean
ipc_get_lsc_data_ready_flag (ipc_data_t *data);

gchar *
ipc_get_lsc_os_release_from_data (ipc_data_t *data);

// prototypes for handling of ipc_data_t and json
ipc_data_t *
ipc_data_type_from_hostname (const char *source, size_t source_len,
Expand All @@ -41,6 +49,9 @@ ipc_data_type_from_hostname (const char *source, size_t source_len,
ipc_data_t *
ipc_data_type_from_user_agent (const char *user_agent, size_t user_agent_len);

ipc_data_t *
ipc_data_type_from_lsc (gboolean data_ready);

void
ipc_data_destroy (ipc_data_t **data);

Expand Down
30 changes: 15 additions & 15 deletions misc/ipc_openvas_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,22 @@ Ensure (ipc_openvas, ipc_data_from_json_parse_error)

// malformed json string
json_fake = g_strdup (
"{\"type\":1,\"user-agent\":\"Mozilla/5.0 [en] (X11, U; Greenbone OS "
"22.04.4)\"}{\"type\":1,\"user-agent\":\"Mozilla/5.0 [en] (X11, U; "
"Greenbone OS 22.04.4)\"}{\"type\":1,\"user-agent\":\"Mozilla/5.0 [en] "
"(X11, U; Greenbone OS 22.04.4)\"}{\"type\":1,\"user-agent\":\"Mozilla/5.0 "
"{\"type\":2,\"user-agent\":\"Mozilla/5.0 [en] (X11, U; Greenbone OS "
"22.04.4)\"}{\"type\":2,\"user-agent\":\"Mozilla/5.0 [en] (X11, U; "
"Greenbone OS 22.04.4)\"}{\"type\":2,\"user-agent\":\"Mozilla/5.0 [en] "
"(X11, U; Greenbone OS 22.04.4)\"}{\"type\":2,\"user-agent\":\"Mozilla/5.0 "
"[en] (X11, U; Greenbone OS "
"22.04.4)\"}{\"type\":1,\"user-agent\":\"Mozilla/5.0 [en] (X11, U; "
"Greenbone OS 22.04.4)\"}{\"type\":1,\"user-agent\":\"Mozilla/5.0 [en] "
"(X11, U; Greenbone OS 22.04.4)\"}{\"type\":1,\"user-agent\":\"Mozilla/5.0 "
"22.04.4)\"}{\"type\":2,\"user-agent\":\"Mozilla/5.0 [en] (X11, U; "
"Greenbone OS 22.04.4)\"}{\"type\":2,\"user-agent\":\"Mozilla/5.0 [en] "
"(X11, U; Greenbone OS 22.04.4)\"}{\"type\":2,\"user-agent\":\"Mozilla/5.0 "
"[en] (X11, U; Greenbone OS "
"22.04.4)\"}{\"type\":1,\"user-agent\":\"Mozilla/5.0 [en] (X11, U; "
"Greenbone OS 22.04.4)\"}{\"type\":1,\"user-agent\":\"Mozilla/5.0 [en] "
"(X11, U; Greenbone OS 22.04.4)\"}{\"type\":1,\"user-agent\":\"Mozilla/5.0 "
"22.04.4)\"}{\"type\":2,\"user-agent\":\"Mozilla/5.0 [en] (X11, U; "
"Greenbone OS 22.04.4)\"}{\"type\":2,\"user-agent\":\"Mozilla/5.0 [en] "
"(X11, U; Greenbone OS 22.04.4)\"}{\"type\":2,\"user-agent\":\"Mozilla/5.0 "
"[en] (X11, U; Greenbone OS "
"22.04.4)\"}{\"type\":1,\"user-agent\":\"Mozilla/5.0 [en] (X11, U; "
"Greenbone OS 22.04.4)\"}{\"type\":1,\"user-agent\":\"Mozilla/5.0 [en] "
"(X11, U; Greenbone OS 22.04.4)\"}{\"type\":1,\"user-agent\":\"Mozilla/5.0 "
"22.04.4)\"}{\"type\":2,\"user-agent\":\"Mozilla/5.0 [en] (X11, U; "
"Greenbone OS 22.04.4)\"}{\"type\":2,\"user-agent\":\"Mozilla/5.0 [en] "
"(X11, U; Greenbone OS 22.04.4)\"}{\"type\":2,\"user-agent\":\"Mozilla/5.0 "
"[en] (X11, U; Greenbone OS 22.04.4)\"}{\"type\":");

// Read received data
Expand All @@ -107,8 +107,8 @@ Ensure (ipc_openvas, ipc_data_from_json_parse_many_objects)

// malformed json string
json_fake =
g_strdup ("{\"type\":0,\"source\":\"TLS "
"certificate\",\"hostname\":\"localhost\"}{\"type\":1,\"user-"
g_strdup ("{\"type\":1,\"source\":\"TLS "
"certificate\",\"hostname\":\"localhost\"}{\"type\":2,\"user-"
"agent\":\"Mozilla/5.0 [en] (X11, U; Greenbone OS "
"22.04.4)\"}");

Expand Down
Loading

0 comments on commit 474b4bf

Please sign in to comment.