Greenbone Vulnerability Management Libraries  22.8.0
prefs.c
Go to the documentation of this file.
1 /* SPDX-FileCopyrightText: 2014-2023 Greenbone AG
2  *
3  * SPDX-License-Identifier: GPL-2.0-or-later
4  */
5 
14 #include "prefs.h"
15 
16 #include "settings.h" /* for init_settings_iterator_from_file */
17 
18 #include <glib.h> /* for gchar */
19 #include <stdio.h> /* for printf() */
20 #include <stdlib.h> /* for atoi() */
21 #include <string.h> /* for strlen() */
22 
23 #undef G_LOG_DOMAIN
24 
27 #define G_LOG_DOMAIN "libgvm base"
28 
29 static GHashTable *global_prefs = NULL;
30 
31 void
32 prefs_set (const gchar *, const gchar *);
33 
39 static void
40 prefs_init (void)
41 {
42  if (global_prefs)
43  g_hash_table_destroy (global_prefs);
44 
45  global_prefs =
46  g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
47  prefs_set ("cgi_path", "/cgi-bin:/scripts");
48  prefs_set ("checks_read_timeout", "5");
49  prefs_set ("unscanned_closed", "yes");
50  prefs_set ("unscanned_closed_udp", "yes");
51  prefs_set ("timeout_retry", "3");
52  prefs_set ("expand_vhosts", "yes");
53  prefs_set ("test_empty_vhost", "no");
54  prefs_set ("open_sock_max_attempts", "5");
55  prefs_set ("time_between_request", "0");
56  prefs_set ("nasl_no_signature_check", "yes");
57  prefs_set ("max_hosts", "30");
58  prefs_set ("max_checks", "10");
59  prefs_set ("log_whole_attack", "no");
60  prefs_set ("log_plugins_name_at_load", "no");
61  prefs_set ("optimize_test", "yes");
62  prefs_set ("non_simult_ports", "139, 445, 3389, Services/irc");
63  prefs_set ("safe_checks", "yes");
64  prefs_set ("auto_enable_dependencies", "yes");
65  prefs_set ("drop_privileges", "no");
66  prefs_set ("report_host_details", "yes");
67  prefs_set ("vendor_version", "\0");
68  prefs_set ("test_alive_hosts_only", "yes");
69  prefs_set ("test_alive_wait_timeout", "3");
70  prefs_set ("debug_tls", "0");
71  prefs_set ("allow_simultaneous_ips", "yes");
72 }
73 
80 GHashTable *
82 {
83  if (!global_prefs)
84  prefs_init ();
85 
86  return global_prefs;
87 }
88 
98 const gchar *
99 prefs_get (const gchar *key)
100 {
101  if (!global_prefs)
102  prefs_init ();
103 
104  return g_hash_table_lookup (global_prefs, key);
105 }
106 
118 int
119 prefs_get_bool (const gchar *key)
120 {
121  gchar *str;
122 
123  if (!global_prefs)
124  prefs_init ();
125 
126  str = g_hash_table_lookup (global_prefs, key);
127  if (str && !strcmp (str, "yes"))
128  return 1;
129 
130  return 0;
131 }
132 
141 void
142 prefs_set (const gchar *key, const gchar *value)
143 {
144  if (!global_prefs)
145  prefs_init ();
146 
147  g_hash_table_insert (global_prefs, g_strdup (key), g_strdup (value));
148 }
149 
155 void
156 prefs_config (const char *config)
157 {
158  settings_iterator_t settings;
159 
160  if (!global_prefs)
161  prefs_init ();
162 
163  if (!init_settings_iterator_from_file (&settings, config, "Misc"))
164  {
165  while (settings_iterator_next (&settings))
166  prefs_set (settings_iterator_name (&settings),
167  settings_iterator_value (&settings));
168 
169  cleanup_settings_iterator (&settings);
170  }
171 
172  prefs_set ("config_file", config);
173 }
174 
178 void
180 {
181  void *name, *value;
182  GHashTableIter iter;
183 
184  if (global_prefs)
185  {
186  g_hash_table_iter_init (&iter, global_prefs);
187  while (g_hash_table_iter_next (&iter, &name, &value))
188  {
189  printf ("%s = %s\n", (char *) name, (char *) value);
190  }
191  }
192 }
prefs_init
static void prefs_init(void)
Initializes the preferences structure. If it was already initialized, remove old settings and start f...
Definition: prefs.c:40
prefs_config
void prefs_config(const char *config)
Apply the configs from given file as preferences.
Definition: prefs.c:156
settings.h
Protos and data structures for configuration file management.
nvti::name
gchar * name
The name.
Definition: nvti.c:396
settings_iterator_t
Struct holding options to iterate over a GKeyFile.
Definition: settings.h:36
init_settings_iterator_from_file
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:98
prefs.h
Protos and data structures for NVT Information data sets.
prefs_set
void prefs_set(const gchar *, const gchar *)
Set a string preference value via a key.
Definition: prefs.c:142
settings_iterator_value
const gchar * settings_iterator_value(settings_iterator_t *iterator)
Get the value from a settings iterator.
Definition: settings.c:179
preferences_get
GHashTable * preferences_get(void)
Get the pointer to the global preferences structure. Eventually this function should not be used anyw...
Definition: prefs.c:81
cleanup_settings_iterator
void cleanup_settings_iterator(settings_iterator_t *iterator)
Cleanup a settings iterator.
Definition: settings.c:136
prefs_dump
void prefs_dump(void)
Dump the preferences to stdout.
Definition: prefs.c:179
prefs_get
const gchar * prefs_get(const gchar *key)
Get a string preference value via a key.
Definition: prefs.c:99
global_prefs
static GHashTable * global_prefs
Definition: prefs.c:29
settings_iterator_name
const gchar * settings_iterator_name(settings_iterator_t *iterator)
Get the name from a settings iterator.
Definition: settings.c:166
prefs_get_bool
int prefs_get_bool(const gchar *key)
Get a boolean expression of a preference value via a key.
Definition: prefs.c:119
settings_iterator_next
gboolean settings_iterator_next(settings_iterator_t *iterator)
Increment an iterator.
Definition: settings.c:150