OpenVAS Scanner  22.7.9
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_data
 

Macros

#define G_LOG_DOMAIN   "lib misc"
 GLib logging domain. More...
 

Typedefs

typedef struct ipc_hostname ipc_hostname_t
 
typedef struct ipc_user_agent ipc_user_agent_t
 

Functions

enum ipc_data_type ipc_get_data_type_from_data (ipc_data_t *data)
 Get the data type in data. More...
 
gchar * ipc_get_hostname_from_data (ipc_data_t *data)
 Get the hostname from IPC data. More...
 
gchar * ipc_get_hostname_source_from_data (ipc_data_t *data)
 Get the vhost hostname source from IPC data. More...
 
gchar * ipc_get_user_agent_from_data (ipc_data_t *data)
 Get the User-Agent from IPC data. More...
 
struct ipc_dataipc_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. More...
 
static void ipc_hostname_destroy (ipc_hostname_t *data)
 Free ipc_hostname_t data. More...
 
struct ipc_dataipc_data_type_from_user_agent (const char *user_agent, size_t user_agent_len)
 initializes ipc_data for the User-Agent. More...
 
static void ipc_user_agent_destroy (ipc_user_agent_t *data)
 Free a user agent data structure. More...
 
void ipc_data_destroy (ipc_data_t **data)
 destroys ipc_data. More...
 
const char * ipc_data_to_json (struct ipc_data *data)
 transforms ipc_data to a json string More...
 
struct ipc_dataipc_data_from_json (const char *json, size_t len)
 transforms json string to a ipc_data struct More...
 

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 1 of file ipc_openvas.c.

◆ ipc_user_agent_t

Definition at line 1 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 224 of file ipc_openvas.c.

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 }

References IPC_DT_ERROR, IPC_DT_HOSTNAME, IPC_DT_USER_AGENT, ipc_hostname_destroy(), and ipc_user_agent_destroy().

Referenced by add_hostname(), Ensure(), ipc_data_from_json(), 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()

struct ipc_data* 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 319 of file ipc_openvas.c.

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 }

References ipc_hostname::hostname, ipc_hostname::hostname_len, ipc_data_destroy(), IPC_DT_ERROR, IPC_DT_HOSTNAME, IPC_DT_USER_AGENT, ipc_data::ipc_hostname, ipc_hostname_destroy(), 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(), 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 ( struct ipc_data 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 251 of file ipc_openvas.c.

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 }

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

Referenced by add_hostname(), Ensure(), 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()

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.

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

Definition at line 124 of file ipc_openvas.c.

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 }

References free(), ipc_hostname::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_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.

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 173 of file ipc_openvas.c.

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 }

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 1 of file ipc_openvas.c.

59 {
60  if (data != NULL)
61  return data->type;
62  return IPC_DT_ERROR;
63 }

Referenced by ipc_data_to_json(), ipc_get_hostname_from_data(), ipc_get_hostname_source_from_data(), 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 73 of file ipc_openvas.c.

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 }

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

Referenced by 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 89 of file ipc_openvas.c.

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 }

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

Referenced by Ensure(), and 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 105 of file ipc_openvas.c.

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 }

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(), and process_ipc_data().

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

◆ ipc_hostname_destroy()

static 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 153 of file ipc_openvas.c.

154 {
155  if (data == NULL)
156  return;
157  g_free (data->hostname);
158  g_free (data->source);
159  g_free (data);
160 }

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_user_agent_destroy()

static 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 207 of file ipc_openvas.c.

208 {
209  if (data == NULL)
210  return;
211  g_free (data->user_agent);
212  g_free (data);
213 }

References ipc_user_agent::user_agent.

Referenced by ipc_data_destroy().

Here is the caller graph for this function:
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_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_context::type
enum ipc_protocol type
Definition: ipc.h:33
ipc_user_agent::user_agent
char * user_agent
Definition: ipc_openvas.c:31
len
uint8_t len
Definition: nasl_packet_forgery.c:1
IPC_DT_HOSTNAME
@ IPC_DT_HOSTNAME
Definition: ipc_openvas.h:17
ipc_hostname::source
char * source
Definition: ipc_openvas.c:20
ipc_user_agent
Definition: ipc_openvas.c:30
hostname
const char * hostname
Definition: pluginlaunch.c:68
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
Definition: ipc_openvas.c:39
free
void free(void *)
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