Greenbone Vulnerability Management Libraries  10.0.0
prefs.c
Go to the documentation of this file.
1 /* Copyright (C) 2014-2019 Greenbone Networks GmbH
2  *
3  * SPDX-License-Identifier: GPL-2.0-or-later
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
28 #include "settings.h" /* for init_settings_iterator_from_file */
29 
30 #include <glib.h> /* for gchar */
31 #include <stdio.h> /* for printf() */
32 #include <stdlib.h> /* for atoi() */
33 #include <string.h> /* for strlen() */
34 
35 static GHashTable *global_prefs = NULL;
36 
37 void
38 prefs_set (const gchar *, const gchar *);
39 
45 static void
46 prefs_init (void)
47 {
48  if (global_prefs)
49  g_hash_table_destroy (global_prefs);
50 
51  global_prefs =
52  g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
53  prefs_set ("cgi_path", "/cgi-bin:/scripts");
54  prefs_set ("checks_read_timeout", "5");
55  prefs_set ("unscanned_closed", "yes");
56  prefs_set ("unscanned_closed_udp", "yes");
57  prefs_set ("timeout_retry", "3");
58  prefs_set ("expand_vhosts", "yes");
59  prefs_set ("test_empty_vhost", "no");
60  prefs_set ("open_sock_max_attempts", "5");
61  prefs_set ("time_between_request", "0");
62  prefs_set ("nasl_no_signature_check", "yes");
63 }
64 
71 GHashTable *
73 {
74  if (!global_prefs)
75  prefs_init ();
76 
77  return global_prefs;
78 }
79 
89 const gchar *
90 prefs_get (const gchar *key)
91 {
92  if (!global_prefs)
93  prefs_init ();
94 
95  return g_hash_table_lookup (global_prefs, key);
96 }
97 
109 int
110 prefs_get_bool (const gchar *key)
111 {
112  gchar *str;
113 
114  if (!global_prefs)
115  prefs_init ();
116 
117  str = g_hash_table_lookup (global_prefs, key);
118  if (str && !strcmp (str, "yes"))
119  return 1;
120 
121  return 0;
122 }
123 
132 void
133 prefs_set (const gchar *key, const gchar *value)
134 {
135  if (!global_prefs)
136  prefs_init ();
137 
138  g_hash_table_insert (global_prefs, g_strdup (key), g_strdup (value));
139 }
140 
146 void
147 prefs_config (const char *config)
148 {
149  settings_iterator_t settings;
150  char buffer[2048];
151 
152  if (!global_prefs)
153  prefs_init ();
154 
155  strncpy (buffer, config, sizeof (buffer));
156  if (!init_settings_iterator_from_file (&settings, buffer, "Misc"))
157  {
158  while (settings_iterator_next (&settings))
159  prefs_set (settings_iterator_name (&settings),
160  settings_iterator_value (&settings));
161 
162  cleanup_settings_iterator (&settings);
163  }
164 
165  prefs_set ("config_file", buffer);
166 }
167 
171 void
173 {
174  void *name, *value;
175  GHashTableIter iter;
176 
177  if (global_prefs)
178  {
179  g_hash_table_iter_init (&iter, global_prefs);
180  while (g_hash_table_iter_next (&iter, &name, &value))
181  {
182  printf ("%s = %s\n", (char *) name, (char *) value);
183  }
184  }
185 }
186 
195 int
196 prefs_nvt_timeout (const char *oid)
197 {
198  char *pref_name = g_strdup_printf ("timeout.%s", oid);
199  const char *val = prefs_get (pref_name);
200  int timeout = (val ? atoi (val) : 0);
201 
202  g_free (pref_name);
203 
204  return timeout;
205 }
const gchar * settings_iterator_name(settings_iterator_t *iterator)
Get the name from a settings iterator.
Definition: settings.c:174
gboolean settings_iterator_next(settings_iterator_t *iterator)
Increment an iterator.
Definition: settings.c:158
GHashTable * preferences_get(void)
Get the pointer to the global preferences structure. Eventually this function should not be used anyw...
Definition: prefs.c:72
static void prefs_init(void)
Initializes the preferences structure. If it was already initialized, remove old settings and start f...
Definition: prefs.c:46
Struct holding options to iterate over a GKeyFile.
Definition: settings.h:49
const gchar * prefs_get(const gchar *key)
Get a string preference value via a key.
Definition: prefs.c:90
static GHashTable * global_prefs
Definition: prefs.c:35
void cleanup_settings_iterator(settings_iterator_t *iterator)
Cleanup a settings iterator.
Definition: settings.c:144
void prefs_set(const gchar *, const gchar *)
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:187
void prefs_config(const char *config)
Apply the configs from given file as preferences.
Definition: prefs.c:147
Protos and data structures for configuration file management.
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:106
int prefs_nvt_timeout(const char *oid)
Returns the timeout defined by the client or 0 if none was set.
Definition: prefs.c:196
int prefs_get_bool(const gchar *key)
Get a boolean expression of a preference value via a key.
Definition: prefs.c:110
void prefs_dump(void)
Dump the preferences to stdout.
Definition: prefs.c:172