OpenVAS Libraries  9.0.3
prefs.c File Reference

Implementation of API to handle globally stored preferences. More...

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <glib.h>
#include "../base/settings.h"
#include "arglists.h"
Include dependency graph for prefs.c:

Go to the source code of this file.

Functions

struct arglistpreferences_get (void)
 Get the pointer to the global preferences structure. Eventually this function should not be used anywhere. More...
 
const gchar * prefs_get (const gchar *key)
 Get a string preference value via a key. More...
 
int prefs_get_bool (const gchar *key)
 Get a boolean expression of a preference value via a key. More...
 
void prefs_set (const gchar *key, const gchar *value)
 Set a string preference value via a key. More...
 
void prefs_config (const char *config)
 Apply the configs from given file as preferences. More...
 
void prefs_dump (void)
 Dump the preferences to stdout. More...
 
int prefs_nvt_timeout (const char *oid)
 Returns the timeout defined by the client or 0 if none was set. More...
 

Detailed Description

Implementation of API to handle globally stored preferences.

A gloabl store of preferences to scanner and NVTs is handled by this module.

The module is currently using arglist, but eventually once the all of the Scanner uses the preferences via this module, it cann be replaced by a better technology and then be moved to base. Possibly a consolidation with the settings iterator make sense.

Definition in file prefs.c.

Function Documentation

◆ preferences_get()

struct arglist* preferences_get ( void  )

Get the pointer to the global preferences structure. Eventually this function should not be used anywhere.

Definition at line 68 of file prefs.c.

69 {
70  if (!global_prefs)
71  prefs_init ();
72 
73  return global_prefs;
74 }

Referenced by get_plugin_preference().

Here is the caller graph for this function:

◆ prefs_config()

void prefs_config ( const char *  config)

Apply the configs from given file as preferences.

Parameters
configFilename of the configuration file.

Definition at line 155 of file prefs.c.

156 {
157  settings_iterator_t settings;
158 
159  if (!global_prefs)
160  prefs_init ();
161 
162  if (!init_settings_iterator_from_file (&settings, config, "Misc"))
163  {
164  while (settings_iterator_next (&settings))
165  prefs_set (settings_iterator_name (&settings),
166  settings_iterator_value (&settings));
167 
168  cleanup_settings_iterator (&settings);
169  }
170 
171  prefs_set ("config_file", config);
172 }
const gchar * settings_iterator_name(settings_iterator_t *iterator)
Get the name from a settings iterator.
Definition: settings.c:184
gboolean settings_iterator_next(settings_iterator_t *iterator)
Increment an iterator.
Definition: settings.c:168
void cleanup_settings_iterator(settings_iterator_t *iterator)
Cleanup a settings iterator.
Definition: settings.c:154
void prefs_set(const gchar *key, const gchar *value)
Set a string preference value via a key.
Definition: prefs.c:133
const gchar * settings_iterator_value(settings_iterator_t *iterator)
Get the value from a settings iterator.
Definition: settings.c:197
int init_settings_iterator_from_file(settings_iterator_t *iterator, const gchar *filename, const gchar *group)
Initialise a settings iterator from a file.
Definition: settings.c:115

◆ prefs_dump()

void prefs_dump ( void  )

Dump the preferences to stdout.

Definition at line 178 of file prefs.c.

179 {
180  struct arglist * prefs = global_prefs;
181 
182  while (prefs && prefs->next)
183  {
184  printf ("%s = %s\n", prefs->name, (char *) prefs->value);
185  prefs = prefs->next;
186  }
187 }
void * value
Definition: arglists.h:32
struct arglist * next
Definition: arglists.h:33
char * name
Definition: arglists.h:31

◆ prefs_get()

const gchar* prefs_get ( const gchar *  key)

Get a string preference value via a key.

Parameters
keyThe identifier for the preference.
Returns
A pointer to a string with the value for the preference. NULL in case for the key no preference was found or the preference is not of type string.

Definition at line 86 of file prefs.c.

87 {
88  if (!global_prefs)
89  prefs_init ();
90 
91  if (arg_get_type (global_prefs, key) != ARG_STRING)
92  return NULL;
93 
94  return arg_get_value (global_prefs, key);
95 }
#define ARG_STRING
Definition: arglists.h:38
int arg_get_type(struct arglist *args, const char *name)
Definition: arglists.c:268
void * arg_get_value(struct arglist *args, const char *name)
Definition: arglists.c:252

Referenced by cgibin(), exec_nasl_script(), kb_get_port_state_proto(), nasl_get_preference(), nasl_scanner_get_port(), open_sock_tcp(), plug_get_host_fqdn(), plugin_run_openvas_tcp_scanner(), plugin_run_synscan(), prefs_nvt_timeout(), and proto_post_wrapped().

Here is the caller graph for this function:

◆ prefs_get_bool()

int prefs_get_bool ( const gchar *  key)

Get a boolean expression of a preference value via a key.

Parameters
keyThe identifier for the preference.
Returns
1 if the value is considered to represent "true" and 0 if the value is considered to represent "false". If the preference is of type string, value "yes" is true, anything else is false. Any other type or non-existing key is false.

Definition at line 109 of file prefs.c.

110 {
111  if (!global_prefs)
112  prefs_init ();
113 
114  if (arg_get_type (global_prefs, key) == ARG_STRING)
115  {
116  gchar *str = arg_get_value (global_prefs, key);
117  if (str && !strcmp (str, "yes"))
118  return 1;
119  }
120 
121  return 0;
122 }
#define ARG_STRING
Definition: arglists.h:38
int arg_get_type(struct arglist *args, const char *name)
Definition: arglists.c:268
void * arg_get_value(struct arglist *args, const char *name)
Definition: arglists.c:252

Referenced by plugin_run_openvas_tcp_scanner(), and safe_checks().

Here is the caller graph for this function:

◆ prefs_nvt_timeout()

int prefs_nvt_timeout ( const char *  oid)

Returns the timeout defined by the client or 0 if none was set.

Parameters
oidOID of NVT to ask timeout value of.
Returns
0 if no timeout for the NVT oid was found, timeout in seconds otherwise.

Definition at line 198 of file prefs.c.

199 {
200  char *pref_name = g_strdup_printf ("timeout.%s", oid);
201  const char * val = prefs_get (pref_name);
202  int timeout = (val ? atoi (val) : 0);
203 
204  g_free (pref_name);
205 
206  return timeout;
207 }
const char * val
Definition: nasl_init.c:525
const char * oid
const gchar * prefs_get(const gchar *key)
Get a string preference value via a key.
Definition: prefs.c:86

References oid, prefs_get(), and val.

Here is the call graph for this function:

◆ prefs_set()

void prefs_set ( const gchar *  key,
const gchar *  value 
)

Set a string preference value via a key.

Parameters
keyThe identifier for the preference. A copy of this will be created if necessary.
valueThe value to set. A copy of this will be created.

Definition at line 133 of file prefs.c.

134 {
135  if (!global_prefs)
136  prefs_init ();
137 
138  if (arg_get_value (global_prefs, key))
139  {
140  gchar *old = arg_get_value (global_prefs, key);
141  arg_set_value (global_prefs, key, g_strdup (value));
142  g_free (old);
143  return;
144  }
145 
146  arg_add_value (global_prefs, key, ARG_STRING, g_strdup (value));
147 }
int arg_set_value(struct arglist *arglst, const char *name, void *value)
Definition: arglists.c:225
void * value
Definition: arglists.h:32
void arg_add_value(struct arglist *arglst, const char *name, int type, void *value)
Definition: arglists.c:170
#define ARG_STRING
Definition: arglists.h:38
void * arg_get_value(struct arglist *args, const char *name)
Definition: arglists.c:252

Referenced by init(), and main().

Here is the caller graph for this function: