OpenVAS Scanner 23.23.1
ipc_openvas.c
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Greenbone AG
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later
4 */
5
6#include "ipc_openvas.h"
7
8#include <json-glib/json-glib.h>
9#undef G_LOG_DOMAIN
13#define G_LOG_DOMAIN "lib misc"
14
15// Data types definitions
16
17// ipc_hostname is used to send / retrieve new hostnames.
19{
20 char *source; // source value
21 char *hostname; // hostname value
22 size_t source_len; // length of source
23 size_t hostname_len; // length of hostname
24};
25
27
28// ipc_user_agent is used to send / retrieve the User-Agent.
30{
31 char *user_agent; // user_agent value
32 size_t user_agent_len; // length of user_agent
33};
34
36
37// ipc_lsc is used to send / retrieve the table driven LSC data.
38struct ipc_lsc
39{
40 gboolean data_ready; // flag indicating that lsc data is in the kb
41};
42
43typedef struct ipc_lsc ipc_lsc_t;
44
45// ipc_data is used to send / retrieve a given data of the union member
56
57// Functions to access the structures
58
68{
69 if (data != NULL)
70 return data->type;
71 return IPC_DT_ERROR;
72}
73
81gchar *
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}
89
97gchar *
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}
105
113gchar *
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}
121
129gboolean
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}
137
138// Hostname
139
149ipc_data_type_from_hostname (const char *source, size_t source_len,
150 const char *hostname, size_t hostname_len)
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}
171
177static void
179{
180 if (data == NULL)
181 return;
182 g_free (data->hostname);
183 g_free (data->source);
184 g_free (data);
185}
186
187// User-Agent
188
198ipc_data_type_from_user_agent (const char *user_agent, size_t user_agent_len)
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}
225
231static void
233{
234 if (data == NULL)
235 return;
236 g_free (data->user_agent);
237 g_free (data);
238}
239
240// Table driven LSC
241
250ipc_data_type_from_lsc (gboolean data_ready)
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}
273
279static void
281{
282 g_free (data);
283}
284
285// General IPC data functios
286
293void
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}
316
324const char *
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}
390
400ipc_data_from_json (const char *json, size_t len)
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}
ipc_data_t * ipc_data_from_json(const char *json, size_t len)
transforms json string to a ipc_data struct
Definition ipc_openvas.c:400
gchar * ipc_get_hostname_from_data(ipc_data_t *data)
Get the hostname from IPC data.
Definition ipc_openvas.c:82
struct ipc_hostname ipc_hostname_t
Definition ipc_openvas.c:26
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
static void ipc_user_agent_destroy(ipc_user_agent_t *data)
Free a user agent data structure.
Definition ipc_openvas.c:232
void ipc_data_destroy(ipc_data_t **data)
destroys ipc_data.
Definition ipc_openvas.c:294
ipc_data_t * ipc_data_type_from_lsc(gboolean data_ready)
initializes ipc_data for the table driven LSC.
Definition ipc_openvas.c:250
const char * ipc_data_to_json(ipc_data_t *data)
transforms ipc_data to a json string
Definition ipc_openvas.c:325
gchar * ipc_get_user_agent_from_data(ipc_data_t *data)
Get the User-Agent from IPC data.
Definition ipc_openvas.c:114
static void ipc_lsc_destroy(ipc_lsc_t *data)
Free a LSC data structure.
Definition ipc_openvas.c:280
gboolean ipc_get_lsc_data_ready_flag(ipc_data_t *data)
Get the package list from LSC IPC data.
Definition ipc_openvas.c:130
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.
Definition ipc_openvas.c:198
gchar * ipc_get_hostname_source_from_data(ipc_data_t *data)
Get the vhost hostname source from IPC data.
Definition ipc_openvas.c:98
struct ipc_lsc ipc_lsc_t
Definition ipc_openvas.c:43
static void ipc_hostname_destroy(ipc_hostname_t *data)
Free ipc_hostname_t data.
Definition ipc_openvas.c:178
struct ipc_user_agent ipc_user_agent_t
Definition ipc_openvas.c:35
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.
Definition ipc_openvas.c:149
struct ipc_data ipc_data_t
Definition ipc_openvas.h:23
ipc_data_type
Definition ipc_openvas.h:15
@ 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
void free(void *)
uint8_t len
Definition nasl_packet_forgery.c:1
const char * hostname
Definition pluginlaunch.c:68
Definition ipc_openvas.c:47
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
Definition ipc_openvas.c:19
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
Definition ipc_openvas.c:39
gboolean data_ready
Definition ipc_openvas.c:40
Definition ipc_openvas.c:30
char * user_agent
Definition ipc_openvas.c:31
size_t user_agent_len
Definition ipc_openvas.c:32
static gchar * user_agent
user-agent, or NULL.
Definition user_agent.c:29