OpenVAS Scanner 23.40.3
ipc_openvas.c File Reference
#include "ipc_openvas.h"
#include <json-glib/json-glib.h>
Include dependency graph for ipc_openvas.c:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  ipc_hostname
struct  ipc_user_agent
struct  ipc_lsc
struct  ipc_data

Macros

#define G_LOG_DOMAIN   "lib misc"
 GLib logging domain.

Typedefs

typedef struct ipc_hostname ipc_hostname_t
typedef struct ipc_user_agent ipc_user_agent_t
typedef struct ipc_lsc ipc_lsc_t

Functions

enum ipc_data_type ipc_get_data_type_from_data (ipc_data_t *data)
 Get the data type in data.
gchar * ipc_get_hostname_from_data (ipc_data_t *data)
 Get the hostname from IPC data.
gchar * ipc_get_hostname_source_from_data (ipc_data_t *data)
 Get the vhost hostname source from IPC data.
gchar * ipc_get_user_agent_from_data (ipc_data_t *data)
 Get the User-Agent from IPC data.
gboolean ipc_get_lsc_data_ready_flag (ipc_data_t *data)
 Get the package list from LSC IPC data.
ipc_data_tipc_data_type_from_hostname (const char *source, size_t source_len, const char *hostname, size_t hostname_len)
 initializes ipc_data for a hostname data.
static void ipc_hostname_destroy (ipc_hostname_t *data)
 Free ipc_hostname_t data.
ipc_data_tipc_data_type_from_user_agent (const char *user_agent, size_t user_agent_len)
 initializes ipc_data for the User-Agent.
static void ipc_user_agent_destroy (ipc_user_agent_t *data)
 Free a user agent data structure.
ipc_data_tipc_data_type_from_lsc (gboolean data_ready)
 initializes ipc_data for the table driven LSC.
static void ipc_lsc_destroy (ipc_lsc_t *data)
 Free a LSC data structure.
void ipc_data_destroy (ipc_data_t **data)
 destroys ipc_data.
const char * ipc_data_to_json (ipc_data_t *data)
 transforms ipc_data to a json string
ipc_data_tipc_data_from_json (const char *json, size_t len)
 transforms json string to a ipc_data struct

Macro Definition Documentation

◆ G_LOG_DOMAIN

#define G_LOG_DOMAIN   "lib misc"

GLib logging domain.

Definition at line 13 of file ipc_openvas.c.

Typedef Documentation

◆ ipc_hostname_t

typedef struct ipc_hostname ipc_hostname_t

Definition at line 26 of file ipc_openvas.c.

◆ ipc_lsc_t

typedef struct ipc_lsc ipc_lsc_t

Definition at line 43 of file ipc_openvas.c.

◆ ipc_user_agent_t

Definition at line 35 of file ipc_openvas.c.

Function Documentation

◆ ipc_data_destroy()

void ipc_data_destroy ( ipc_data_t ** data)

destroys ipc_data.

Parameters
datathe ipc_data to be destroyed.

Definition at line 294 of file ipc_openvas.c.

295{
296 if (*data == NULL)
297 return;
298 switch ((*data)->type)
299 {
300 case IPC_DT_HOSTNAME:
301 ipc_hostname_destroy ((*data)->ipc_hostname);
302 break;
304 ipc_user_agent_destroy ((*data)->ipc_user_agent);
305 break;
306 case IPC_DT_LSC:
307 ipc_lsc_destroy ((*data)->ipc_lsc);
308 break;
309 case IPC_DT_ERROR:
310 case IPC_DT_NO_DATA:
311 break;
312 }
313 g_free (*data);
314 *data = NULL;
315}
static void ipc_user_agent_destroy(ipc_user_agent_t *data)
Free a user agent data structure.
static void ipc_lsc_destroy(ipc_lsc_t *data)
Free a LSC data structure.
static void ipc_hostname_destroy(ipc_hostname_t *data)
Free ipc_hostname_t data.
@ IPC_DT_NO_DATA
Definition ipc_openvas.h:17
@ IPC_DT_HOSTNAME
Definition ipc_openvas.h:18
@ IPC_DT_USER_AGENT
Definition ipc_openvas.h:19
@ IPC_DT_ERROR
Definition ipc_openvas.h:16
@ IPC_DT_LSC
Definition ipc_openvas.h:20

References IPC_DT_ERROR, IPC_DT_HOSTNAME, IPC_DT_LSC, IPC_DT_NO_DATA, IPC_DT_USER_AGENT, ipc_hostname_destroy(), ipc_lsc_destroy(), and ipc_user_agent_destroy().

Referenced by add_hostname(), Ensure(), Ensure(), Ensure(), ipc_data_from_json(), nasl_update_table_driven_lsc_data(), process_ipc_data(), and send_user_agent_via_ipc().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ipc_data_from_json()

ipc_data_t * ipc_data_from_json ( const char * json,
size_t len )

transforms json string to a ipc_data struct

Parameters
jsonthe json representation to be transformed.
lenthe length of the json representation
Returns
a heap allocated ipc_data or NULL on failure.

Definition at line 400 of file ipc_openvas.c.

401{
402 JsonParser *parser = NULL;
403 JsonReader *reader = NULL;
404
405 GError *err = NULL;
406 ipc_data_t *ret = NULL;
408 ipc_hostname_t *hn;
409 ipc_lsc_t *lsc;
410
411 enum ipc_data_type type = IPC_DT_ERROR;
412
413 if ((ret = calloc (1, sizeof (*ret))) == NULL)
414 goto cleanup;
415
416 /* Initialize the type with error.
417 * Usefull for cleanup, in case of parser error. */
418 ret->type = type;
419
420 parser = json_parser_new ();
421 if (!json_parser_load_from_data (parser, json, len, &err))
422 {
423 goto cleanup;
424 }
425
426 reader = json_reader_new (json_parser_get_root (parser));
427
428 if (!json_reader_read_member (reader, "type"))
429 {
430 goto cleanup;
431 }
432
433 type = json_reader_get_int_value (reader);
434 ret->type = type;
435 json_reader_end_member (reader);
436
437 switch (type)
438 {
439 case IPC_DT_ERROR:
440 case IPC_DT_NO_DATA:
441 goto cleanup;
442 case IPC_DT_HOSTNAME:
443 if ((hn = calloc (1, sizeof (*hn))) == NULL)
444 goto cleanup;
445 if (!json_reader_read_member (reader, "hostname"))
446 {
447 g_free (hn);
448 goto cleanup;
449 }
450 hn->hostname = g_strdup (json_reader_get_string_value (reader));
451 hn->hostname_len = strlen (hn->hostname);
452 json_reader_end_member (reader);
453 if (!json_reader_read_member (reader, "source"))
454 {
456 goto cleanup;
457 }
458 hn->source = g_strdup (json_reader_get_string_value (reader));
459 hn->source_len = strlen (hn->source);
460 json_reader_end_member (reader);
461 ret->ipc_hostname = hn;
462 break;
463
465
466 if ((ua = calloc (1, sizeof (*ua))) == NULL)
467 goto cleanup;
468 if (!json_reader_read_member (reader, "user-agent"))
469 {
470 g_free (ua);
471 goto cleanup;
472 }
473 ua->user_agent = g_strdup (json_reader_get_string_value (reader));
474 ua->user_agent_len = strlen (ua->user_agent);
475 json_reader_end_member (reader);
476 ret->ipc_user_agent = ua;
477 break;
478
479 case IPC_DT_LSC:
480 if ((lsc = calloc (1, sizeof (*lsc))) == NULL)
481 goto cleanup;
482 if (!json_reader_read_member (reader, "data_ready"))
483 {
484 goto cleanup;
485 }
486 lsc->data_ready = json_reader_get_boolean_value (reader);
487 json_reader_end_member (reader);
488 ret->ipc_lsc = lsc;
489 break;
490 }
491
492cleanup:
493 if (reader)
494 g_object_unref (reader);
495 g_object_unref (parser);
496
497 if (err != NULL)
498 {
499 g_warning ("%s: Unable to parse json (%s). Reason: %s", __func__, json,
500 err->message);
501
502 if (ret != NULL)
503 ipc_data_destroy (&ret);
504 }
505
506 return ret;
507}
struct ipc_hostname ipc_hostname_t
Definition ipc_openvas.c:26
void ipc_data_destroy(ipc_data_t **data)
destroys ipc_data.
struct ipc_lsc ipc_lsc_t
Definition ipc_openvas.c:43
struct ipc_user_agent ipc_user_agent_t
Definition ipc_openvas.c:35
struct ipc_data ipc_data_t
Definition ipc_openvas.h:23
ipc_data_type
Definition ipc_openvas.h:15
uint8_t len
ipc_hostname_t * ipc_hostname
Definition ipc_openvas.c:52
enum ipc_data_type type
Definition ipc_openvas.c:48
ipc_user_agent_t * ipc_user_agent
Definition ipc_openvas.c:51
ipc_lsc_t * ipc_lsc
Definition ipc_openvas.c:53
size_t hostname_len
Definition ipc_openvas.c:23
char * hostname
Definition ipc_openvas.c:21
char * source
Definition ipc_openvas.c:20
size_t source_len
Definition ipc_openvas.c:22
gboolean data_ready
Definition ipc_openvas.c:40
char * user_agent
Definition ipc_openvas.c:31
size_t user_agent_len
Definition ipc_openvas.c:32

References ipc_lsc::data_ready, ipc_hostname::hostname, ipc_hostname::hostname_len, ipc_data_destroy(), IPC_DT_ERROR, IPC_DT_HOSTNAME, IPC_DT_LSC, IPC_DT_NO_DATA, IPC_DT_USER_AGENT, ipc_data::ipc_hostname, ipc_hostname_destroy(), ipc_data::ipc_lsc, ipc_data::ipc_user_agent, len, ipc_hostname::source, ipc_hostname::source_len, ipc_data::type, ipc_user_agent::user_agent, and ipc_user_agent::user_agent_len.

Referenced by Ensure(), Ensure(), Ensure(), Ensure(), and process_ipc_data().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ipc_data_to_json()

const char * ipc_data_to_json ( ipc_data_t * data)

transforms ipc_data to a json string

Parameters
datathe ipc_data to be transformed.
Returns
a heap allocated achar array containing the json or NULL on failure.

Definition at line 325 of file ipc_openvas.c.

326{
327 JsonBuilder *builder;
328 JsonGenerator *gen;
329 JsonNode *root;
330 gchar *json_str;
331 ipc_hostname_t *hn = NULL;
332 ipc_user_agent_t *ua = NULL;
333 ipc_lsc_t *lsc = NULL;
334 enum ipc_data_type type = IPC_DT_ERROR;
335
336 if (data == NULL)
337 return NULL;
338
339 if ((type = ipc_get_data_type_from_data (data)) == IPC_DT_ERROR)
340 return NULL;
341
342 builder = json_builder_new ();
343
344 json_builder_begin_object (builder);
345
346 json_builder_set_member_name (builder, "type");
347 builder = json_builder_add_int_value (builder, type);
348 switch (type)
349 {
350 case IPC_DT_HOSTNAME:
351 hn = data->ipc_hostname;
352 json_builder_set_member_name (builder, "source");
353 builder = json_builder_add_string_value (builder, hn->source);
354 json_builder_set_member_name (builder, "hostname");
355 builder = json_builder_add_string_value (builder, hn->hostname);
356 break;
357
359 ua = data->ipc_user_agent;
360 json_builder_set_member_name (builder, "user-agent");
361 builder = json_builder_add_string_value (builder, ua->user_agent);
362 break;
363
364 case IPC_DT_LSC:
365 lsc = data->ipc_lsc;
366 json_builder_set_member_name (builder, "data_ready");
367 builder = json_builder_add_boolean_value (builder, lsc->data_ready);
368 break;
369
370 default:
371 g_warning ("%s: Unknown data type %d.", __func__, type);
372 }
373
374 json_builder_end_object (builder);
375
376 gen = json_generator_new ();
377 root = json_builder_get_root (builder);
378 json_generator_set_root (gen, root);
379 json_str = json_generator_to_data (gen, NULL);
380
381 json_node_free (root);
382 g_object_unref (gen);
383 g_object_unref (builder);
384
385 if (json_str == NULL)
386 g_warning ("%s: Error while creating JSON.", __func__);
387
388 return json_str;
389}
enum ipc_data_type ipc_get_data_type_from_data(ipc_data_t *data)
Get the data type in data.
Definition ipc_openvas.c:67

References ipc_lsc::data_ready, ipc_hostname::hostname, IPC_DT_ERROR, IPC_DT_HOSTNAME, IPC_DT_LSC, IPC_DT_USER_AGENT, ipc_get_data_type_from_data(), ipc_data::ipc_hostname, ipc_data::ipc_lsc, ipc_data::ipc_user_agent, ipc_hostname::source, and ipc_user_agent::user_agent.

Referenced by add_hostname(), Ensure(), Ensure(), nasl_update_table_driven_lsc_data(), and send_user_agent_via_ipc().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ipc_data_type_from_hostname()

ipc_data_t * ipc_data_type_from_hostname ( const char * source,
size_t source_len,
const char * hostname,
size_t hostname_len )

initializes ipc_data for a hostname data.

Parameters
sourcethe source of the hostname
hostnamethe name of the host
Returns
a heap initialized ipc_data or NULL on failure.

Definition at line 149 of file ipc_openvas.c.

151{
152 ipc_data_t *data = NULL;
153 ipc_hostname_t *hnd = NULL;
154 if (source == NULL || hostname == NULL)
155 return NULL;
156 if ((data = calloc (1, sizeof (*data))) == NULL)
157 return NULL;
158 data->type = IPC_DT_HOSTNAME;
159 if ((hnd = calloc (1, sizeof (*hnd))) == NULL)
160 goto failure_exit;
161 hnd->hostname = g_strdup (hostname);
162 hnd->source = g_strdup (source);
163 hnd->hostname_len = hostname_len;
164 hnd->source_len = source_len;
165 data->ipc_hostname = hnd;
166 return data;
167failure_exit:
168 free (data);
169 return NULL;
170}
void free(void *)
const char * hostname

References free(), hostname, ipc_hostname::hostname, ipc_hostname::hostname_len, IPC_DT_HOSTNAME, ipc_data::ipc_hostname, ipc_hostname::source, ipc_hostname::source_len, and ipc_data::type.

Referenced by add_hostname(), and Ensure().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ipc_data_type_from_lsc()

ipc_data_t * ipc_data_type_from_lsc ( gboolean data_ready)

initializes ipc_data for the table driven LSC.

Parameters
os_releaseThe OS release
Returns
a heap initialized ipc_data or NULL on failure.

Definition at line 250 of file ipc_openvas.c.

251{
252 ipc_data_t *data = NULL;
253 ipc_lsc_t *lscd = NULL;
254
255 if (data_ready != FALSE && data_ready != TRUE)
256 return NULL;
257
258 if ((data = calloc (1, sizeof (*data))) == NULL)
259 return NULL;
260 data->type = IPC_DT_LSC;
261
262 if ((lscd = calloc (1, sizeof (*lscd))) == NULL)
263 goto failure_exit;
264
265 lscd->data_ready = data_ready;
266 data->ipc_lsc = lscd;
267 return data;
268
269failure_exit:
270 free (data);
271 return NULL;
272}

References ipc_lsc::data_ready, free(), IPC_DT_LSC, ipc_data::ipc_lsc, and ipc_data::type.

Referenced by nasl_update_table_driven_lsc_data().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ipc_data_type_from_user_agent()

ipc_data_t * ipc_data_type_from_user_agent ( const char * user_agent,
size_t user_agent_len )

initializes ipc_data for the User-Agent.

Parameters
user_agentThe User-Agent
user_agent_lenLength of the user agent string.
Returns
a heap initialized ipc_data or NULL on failure.

Definition at line 198 of file ipc_openvas.c.

199{
200 ipc_data_t *data = NULL;
201 ipc_user_agent_t *uad = NULL;
202 gchar *ua_str = NULL;
203
204 if (user_agent == NULL)
205 return NULL;
206
207 if ((data = calloc (1, sizeof (*data))) == NULL)
208 return NULL;
209 data->type = IPC_DT_USER_AGENT;
210
211 if ((uad = calloc (1, sizeof (*uad))) == NULL)
212 goto failure_exit;
213
214 ua_str = g_strdup (user_agent);
215 uad->user_agent = ua_str;
216 uad->user_agent_len = user_agent_len;
217
218 data->ipc_user_agent = uad;
219 return data;
220
221failure_exit:
222 free (data);
223 return NULL;
224}
static gchar * user_agent
user-agent, or NULL.
Definition user_agent.c:29

References free(), IPC_DT_USER_AGENT, ipc_data::ipc_user_agent, ipc_data::type, ipc_user_agent::user_agent, user_agent, and ipc_user_agent::user_agent_len.

Referenced by Ensure(), and send_user_agent_via_ipc().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ipc_get_data_type_from_data()

enum ipc_data_type ipc_get_data_type_from_data ( ipc_data_t * data)

Get the data type in data.

Parameters
dataStructure containing the data and data type

@Return The corresponding ipc_data_type, IPC_DT_ERROR on error.

Definition at line 67 of file ipc_openvas.c.

68{
69 if (data != NULL)
70 return data->type;
71 return IPC_DT_ERROR;
72}

References IPC_DT_ERROR, and ipc_data::type.

Referenced by Ensure(), ipc_data_to_json(), ipc_get_hostname_from_data(), ipc_get_hostname_source_from_data(), ipc_get_lsc_data_ready_flag(), ipc_get_user_agent_from_data(), and process_ipc_data().

Here is the caller graph for this function:

◆ ipc_get_hostname_from_data()

gchar * ipc_get_hostname_from_data ( ipc_data_t * data)

Get the hostname from IPC data.

Parameters
dataData structure of IPC_DT_HOSNAME type.

@Return a string containing the hostname, NULL on error.

Definition at line 82 of file ipc_openvas.c.

83{
84 if (data == NULL || (ipc_get_data_type_from_data (data) != IPC_DT_HOSTNAME))
85 return NULL;
86
87 return data->ipc_hostname->hostname;
88}

References ipc_hostname::hostname, IPC_DT_HOSTNAME, ipc_get_data_type_from_data(), and ipc_data::ipc_hostname.

Referenced by Ensure(), Ensure(), Ensure(), and process_ipc_data().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ipc_get_hostname_source_from_data()

gchar * ipc_get_hostname_source_from_data ( ipc_data_t * data)

Get the vhost hostname source from IPC data.

Parameters
dataData structure of IPC_DT_HOSNAME type.

@Return a string containing the vhost hostname source, NULL on error.

Definition at line 98 of file ipc_openvas.c.

99{
100 if (data == NULL || (ipc_get_data_type_from_data (data) != IPC_DT_HOSTNAME))
101 return NULL;
102
103 return data->ipc_hostname->source;
104}

References IPC_DT_HOSTNAME, ipc_get_data_type_from_data(), ipc_data::ipc_hostname, and ipc_hostname::source.

Referenced by Ensure(), Ensure(), Ensure(), and process_ipc_data().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ipc_get_lsc_data_ready_flag()

gboolean ipc_get_lsc_data_ready_flag ( ipc_data_t * data)

Get the package list from LSC IPC data.

Parameters
dataData structure of IPC_DT_LSC type.

@Return True if the data is ready for running with LSC, False otherwise.

Definition at line 130 of file ipc_openvas.c.

131{
132 if (data == NULL || (ipc_get_data_type_from_data (data) != IPC_DT_LSC))
133 return FALSE;
134
135 return data->ipc_lsc->data_ready;
136}

References ipc_lsc::data_ready, IPC_DT_LSC, ipc_get_data_type_from_data(), and ipc_data::ipc_lsc.

Referenced by process_ipc_data().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ipc_get_user_agent_from_data()

gchar * ipc_get_user_agent_from_data ( ipc_data_t * data)

Get the User-Agent from IPC data.

Parameters
dataData structure of IPC_DT_USER_AGENT type.

@Return a string containing the User-Agent, NULL on error.

Definition at line 114 of file ipc_openvas.c.

115{
116 if (data == NULL || (ipc_get_data_type_from_data (data) != IPC_DT_USER_AGENT))
117 return NULL;
118
119 return data->ipc_user_agent->user_agent;
120}

References IPC_DT_USER_AGENT, ipc_get_data_type_from_data(), ipc_data::ipc_user_agent, and ipc_user_agent::user_agent.

Referenced by Ensure(), Ensure(), and process_ipc_data().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ipc_hostname_destroy()

void ipc_hostname_destroy ( ipc_hostname_t * data)
static

Free ipc_hostname_t data.

Parameters
dataThe hostname data structure to be free()'ed

Definition at line 178 of file ipc_openvas.c.

179{
180 if (data == NULL)
181 return;
182 g_free (data->hostname);
183 g_free (data->source);
184 g_free (data);
185}

References ipc_hostname::hostname, and ipc_hostname::source.

Referenced by ipc_data_destroy(), and ipc_data_from_json().

Here is the caller graph for this function:

◆ ipc_lsc_destroy()

void ipc_lsc_destroy ( ipc_lsc_t * data)
static

Free a LSC data structure.

Parameters
dataThe lsc data structure to be free()'ed

Definition at line 280 of file ipc_openvas.c.

281{
282 g_free (data);
283}

Referenced by ipc_data_destroy().

Here is the caller graph for this function:

◆ ipc_user_agent_destroy()

void ipc_user_agent_destroy ( ipc_user_agent_t * data)
static

Free a user agent data structure.

Parameters
dataThe user agent data structure to be free()'ed

Definition at line 232 of file ipc_openvas.c.

233{
234 if (data == NULL)
235 return;
236 g_free (data->user_agent);
237 g_free (data);
238}

References ipc_user_agent::user_agent.

Referenced by ipc_data_destroy().

Here is the caller graph for this function: