OpenVAS Scanner  22.7.9
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
10 
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 
26 typedef struct ipc_hostname ipc_hostname_t;
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 
35 typedef struct ipc_user_agent ipc_user_agent_t;
36 
37 // ipc_data is used to send / retrieve a given data of the union member
38 struct ipc_data
39 {
40  enum ipc_data_type type;
41  union
42  {
45  };
46 };
47 
48 // Functions to access the structures
49 
57 enum ipc_data_type
59 {
60  if (data != NULL)
61  return data->type;
62  return IPC_DT_ERROR;
63 }
64 
72 gchar *
74 {
75  if (data == NULL || (ipc_get_data_type_from_data (data) != IPC_DT_HOSTNAME))
76  return NULL;
77 
78  return data->ipc_hostname->hostname;
79 }
80 
88 gchar *
90 {
91  if (data == NULL || (ipc_get_data_type_from_data (data) != IPC_DT_HOSTNAME))
92  return NULL;
93 
94  return data->ipc_hostname->source;
95 }
96 
104 gchar *
106 {
107  if (data == NULL || (ipc_get_data_type_from_data (data) != IPC_DT_USER_AGENT))
108  return NULL;
109 
110  return data->ipc_user_agent->user_agent;
111 }
112 
113 // Hostname
114 
123 struct ipc_data *
124 ipc_data_type_from_hostname (const char *source, size_t source_len,
125  const char *hostname, size_t hostname_len)
126 {
127  struct ipc_data *data = NULL;
128  ipc_hostname_t *hnd = NULL;
129  if (source == NULL || hostname == NULL)
130  return NULL;
131  if ((data = calloc (1, sizeof (*data))) == NULL)
132  return NULL;
133  data->type = IPC_DT_HOSTNAME;
134  if ((hnd = calloc (1, sizeof (*hnd))) == NULL)
135  goto failure_exit;
136  hnd->hostname = g_strdup (hostname);
137  hnd->source = g_strdup (source);
138  hnd->hostname_len = hostname_len;
139  hnd->source_len = source_len;
140  data->ipc_hostname = hnd;
141  return data;
142 failure_exit:
143  free (data);
144  return NULL;
145 }
146 
152 static void
154 {
155  if (data == NULL)
156  return;
157  g_free (data->hostname);
158  g_free (data->source);
159  g_free (data);
160 }
161 
162 // User-Agent
163 
172 struct ipc_data *
173 ipc_data_type_from_user_agent (const char *user_agent, size_t user_agent_len)
174 {
175  struct ipc_data *data = NULL;
176  ipc_user_agent_t *uad = NULL;
177  gchar *ua_str = NULL;
178 
179  if (user_agent == NULL)
180  return NULL;
181 
182  if ((data = calloc (1, sizeof (*data))) == NULL)
183  return NULL;
184  data->type = IPC_DT_USER_AGENT;
185 
186  if ((uad = calloc (1, sizeof (*uad))) == NULL)
187  goto failure_exit;
188 
189  ua_str = g_strdup (user_agent);
190  uad->user_agent = ua_str;
191  uad->user_agent_len = user_agent_len;
192 
193  data->ipc_user_agent = uad;
194  return data;
195 
196 failure_exit:
197  free (data);
198  return NULL;
199 }
200 
206 static void
208 {
209  if (data == NULL)
210  return;
211  g_free (data->user_agent);
212  g_free (data);
213 }
214 
215 // General IPC data functios
216 
223 void
225 {
226  if (*data == NULL)
227  return;
228  switch ((*data)->type)
229  {
230  case IPC_DT_HOSTNAME:
231  ipc_hostname_destroy ((*data)->ipc_hostname);
232  break;
233  case IPC_DT_USER_AGENT:
234  ipc_user_agent_destroy ((*data)->ipc_user_agent);
235  break;
236  case IPC_DT_ERROR:
237  break;
238  }
239  g_free (*data);
240  *data = NULL;
241 }
242 
250 const char *
252 {
253  JsonBuilder *builder;
254  JsonGenerator *gen;
255  JsonNode *root;
256  gchar *json_str;
257  ipc_hostname_t *hn = NULL;
258  ipc_user_agent_t *ua = NULL;
260 
261  if (data == NULL)
262  return NULL;
263 
265  return NULL;
266 
267  builder = json_builder_new ();
268 
269  json_builder_begin_object (builder);
270 
271  json_builder_set_member_name (builder, "type");
272  builder = json_builder_add_int_value (builder, type);
273  switch (type)
274  {
275  case IPC_DT_HOSTNAME:
276  hn = data->ipc_hostname;
277  json_builder_set_member_name (builder, "source");
278  builder = json_builder_add_string_value (builder, hn->source);
279  json_builder_set_member_name (builder, "hostname");
280  builder = json_builder_add_string_value (builder, hn->hostname);
281  break;
282 
283  case IPC_DT_USER_AGENT:
284  ua = data->ipc_user_agent;
285  json_builder_set_member_name (builder, "user-agent");
286  builder = json_builder_add_string_value (builder, ua->user_agent);
287  break;
288 
289  default:
290  g_warning ("%s: Unknown data type %d.", __func__, type);
291  }
292 
293  json_builder_end_object (builder);
294 
295  gen = json_generator_new ();
296  root = json_builder_get_root (builder);
297  json_generator_set_root (gen, root);
298  json_str = json_generator_to_data (gen, NULL);
299 
300  json_node_free (root);
301  g_object_unref (gen);
302  g_object_unref (builder);
303 
304  if (json_str == NULL)
305  g_warning ("%s: Error while creating JSON.", __func__);
306 
307  return json_str;
308 }
309 
318 struct ipc_data *
319 ipc_data_from_json (const char *json, size_t len)
320 {
321  JsonParser *parser = NULL;
322  JsonReader *reader = NULL;
323 
324  GError *err = NULL;
325  ipc_data_t *ret = NULL;
326  ipc_user_agent_t *ua;
327  ipc_hostname_t *hn;
329 
330  if ((ret = calloc (1, sizeof (*ret))) == NULL)
331  goto cleanup;
332 
333  /* Initialize the type with error.
334  * Usefull for cleanup, in case of parser error. */
335  ret->type = type;
336 
337  parser = json_parser_new ();
338  if (!json_parser_load_from_data (parser, json, len, &err))
339  {
340  goto cleanup;
341  }
342 
343  reader = json_reader_new (json_parser_get_root (parser));
344 
345  if (!json_reader_read_member (reader, "type"))
346  {
347  goto cleanup;
348  }
349 
350  type = json_reader_get_int_value (reader);
351  ret->type = type;
352  json_reader_end_member (reader);
353 
354  switch (type)
355  {
356  case IPC_DT_ERROR:
357  goto cleanup;
358  case IPC_DT_HOSTNAME:
359  if ((hn = calloc (1, sizeof (*hn))) == NULL)
360  goto cleanup;
361  if (!json_reader_read_member (reader, "hostname"))
362  {
363  g_free (hn);
364  goto cleanup;
365  }
366  hn->hostname = g_strdup (json_reader_get_string_value (reader));
367  hn->hostname_len = strlen (hn->hostname);
368  json_reader_end_member (reader);
369  if (!json_reader_read_member (reader, "source"))
370  {
372  goto cleanup;
373  }
374  hn->source = g_strdup (json_reader_get_string_value (reader));
375  hn->source_len = strlen (hn->source);
376  json_reader_end_member (reader);
377  ret->ipc_hostname = hn;
378  break;
379 
380  case IPC_DT_USER_AGENT:
381 
382  if ((ua = calloc (1, sizeof (*ua))) == NULL)
383  goto cleanup;
384  if (!json_reader_read_member (reader, "user-agent"))
385  {
386  g_free (ua);
387  goto cleanup;
388  }
389  ua->user_agent = g_strdup (json_reader_get_string_value (reader));
390  ua->user_agent_len = strlen (ua->user_agent);
391  json_reader_end_member (reader);
392  ret->ipc_user_agent = ua;
393  break;
394  }
395 
396 cleanup:
397  if (reader)
398  g_object_unref (reader);
399  g_object_unref (parser);
400 
401  if (err != NULL)
402  {
403  g_warning ("%s: Unable to parse json (%s). Reason: %s", __func__, json,
404  err->message);
405 
406  if (ret != NULL)
407  ipc_data_destroy (&ret);
408  }
409 
410  return ret;
411 }
ipc_user_agent::user_agent_len
size_t user_agent_len
Definition: ipc_openvas.c:32
ipc_data::ipc_hostname
ipc_hostname_t * ipc_hostname
Definition: ipc_openvas.c:44
ipc_hostname
Definition: ipc_openvas.c:19
ipc_data_destroy
void ipc_data_destroy(ipc_data_t **data)
destroys ipc_data.
Definition: ipc_openvas.c:224
ipc_data::type
enum ipc_data_type type
Definition: ipc_openvas.c:40
IPC_DT_USER_AGENT
@ IPC_DT_USER_AGENT
Definition: ipc_openvas.h:18
IPC_DT_ERROR
@ IPC_DT_ERROR
Definition: ipc_openvas.h:16
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.
Definition: ipc_openvas.c:58
ipc_user_agent_destroy
static void ipc_user_agent_destroy(ipc_user_agent_t *data)
Free a user agent data structure.
Definition: ipc_openvas.c:207
ipc_data_type
ipc_data_type
Definition: ipc_openvas.h:15
ipc_get_hostname_from_data
gchar * ipc_get_hostname_from_data(ipc_data_t *data)
Get the hostname from IPC data.
Definition: ipc_openvas.c:73
ipc_data::ipc_user_agent
ipc_user_agent_t * ipc_user_agent
Definition: ipc_openvas.c:43
ipc_hostname::hostname_len
size_t hostname_len
Definition: ipc_openvas.c:23
ipc_data_type_from_user_agent
struct ipc_data * 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:173
ipc_user_agent::user_agent
char * user_agent
Definition: ipc_openvas.c:31
len
uint8_t len
Definition: nasl_packet_forgery.c:1
ipc_data_to_json
const char * ipc_data_to_json(struct ipc_data *data)
transforms ipc_data to a json string
Definition: ipc_openvas.c:251
IPC_DT_HOSTNAME
@ IPC_DT_HOSTNAME
Definition: ipc_openvas.h:17
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.
Definition: ipc_openvas.c:89
ipc_hostname::source
char * source
Definition: ipc_openvas.c:20
ipc_get_user_agent_from_data
gchar * ipc_get_user_agent_from_data(ipc_data_t *data)
Get the User-Agent from IPC data.
Definition: ipc_openvas.c:105
ipc_user_agent
Definition: ipc_openvas.c:30
hostname
const char * hostname
Definition: pluginlaunch.c:68
ipc_openvas.h
ipc_hostname::source_len
size_t source_len
Definition: ipc_openvas.c:22
ipc_hostname::hostname
char * hostname
Definition: ipc_openvas.c:21
ipc_data_from_json
struct ipc_data * ipc_data_from_json(const char *json, size_t len)
transforms json string to a ipc_data struct
Definition: ipc_openvas.c:319
ipc_data
Definition: ipc_openvas.c:39
free
void free(void *)
ipc_data_type_from_hostname
struct ipc_data * 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:124
user_agent
static gchar * user_agent
user-agent, or NULL.
Definition: user_agent.c:29
ipc_hostname_destroy
static void ipc_hostname_destroy(ipc_hostname_t *data)
Free ipc_hostname_t data.
Definition: ipc_openvas.c:153