File: prefs.c

package info (click to toggle)
gvm-libs 22.35.4-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,980 kB
  • sloc: ansic: 39,095; makefile: 26
file content (197 lines) | stat: -rw-r--r-- 4,874 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/* SPDX-FileCopyrightText: 2014-2023 Greenbone AG
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

/**
 * @file
 * @brief Implementation of API to handle globally stored preferences.
 *
 * A global store of preferences to scanner and NVTs is handled by this
 * module.
 */

#include "prefs.h"

#include "settings.h" /* for init_settings_iterator_from_file */

#include <glib.h>   /* for gchar */
#include <stdio.h>  /* for printf() */
#include <stdlib.h> /* for atoi() */
#include <string.h> /* for strlen() */

#undef G_LOG_DOMAIN
/**
 * @brief GLib log domain.
 */
#define G_LOG_DOMAIN "libgvm base"

static GHashTable *global_prefs = NULL;

void
prefs_set (const gchar *, const gchar *);

/**
 * @brief Initializes the preferences structure. If it was
 *        already initialized, remove old settings and start
 *        from scratch.
 */
static void
prefs_init (void)
{
  if (global_prefs)
    g_hash_table_destroy (global_prefs);

  global_prefs =
    g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
  prefs_set ("cgi_path", "/cgi-bin:/scripts");
  prefs_set ("checks_read_timeout", "5");
  prefs_set ("unscanned_closed", "yes");
  prefs_set ("unscanned_closed_udp", "yes");
  prefs_set ("timeout_retry", "3");
  prefs_set ("expand_vhosts", "yes");
  prefs_set ("test_empty_vhost", "no");
  prefs_set ("open_sock_max_attempts", "5");
  prefs_set ("time_between_request", "0");
  prefs_set ("nasl_no_signature_check", "yes");
  prefs_set ("max_hosts", "30");
  prefs_set ("max_checks", "10");
  prefs_set ("log_whole_attack", "no");
  prefs_set ("log_plugins_name_at_load", "no");
  prefs_set ("optimize_test", "yes");
  prefs_set ("non_simult_ports", "139, 445, 3389, Services/irc");
  prefs_set ("safe_checks", "yes");
  prefs_set ("auto_enable_dependencies", "yes");
  prefs_set ("drop_privileges", "no");
  prefs_set ("report_host_details", "yes");
  prefs_set ("vendor_version", "\0");
  prefs_set ("test_alive_hosts_only", "yes");
  prefs_set ("test_alive_wait_timeout", "3");
  prefs_set ("debug_tls", "0");
  prefs_set ("allow_simultaneous_ips", "yes");
}

/**
 * @brief Get the pointer to the global preferences structure.
 *        Eventually this function should not be used anywhere.
 *
 * @return Pointer to the global preferences structure.
 */
GHashTable *
preferences_get (void)
{
  if (!global_prefs)
    prefs_init ();

  return global_prefs;
}

/**
 * @brief Get a string preference value via a key.
 *
 * @param key    The identifier for the preference.
 *
 * @return 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.
 */
const gchar *
prefs_get (const gchar *key)
{
  if (!global_prefs)
    prefs_init ();

  return g_hash_table_lookup (global_prefs, key);
}

/**
 * @brief Get a boolean expression of a preference value via a key.
 *
 * @param key    The identifier for the preference.
 *
 * @return 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.
 */
int
prefs_get_bool (const gchar *key)
{
  gchar *str;

  if (!global_prefs)
    prefs_init ();

  str = g_hash_table_lookup (global_prefs, key);
  if (str && !strcmp (str, "yes"))
    return 1;

  return 0;
}

/**
 * @brief Set a string preference value via a key.
 *
 * @param key    The identifier for the preference. A copy of this will
 *               be created if necessary.
 *
 * @param value  The value to set. A copy of this will be created.
 */
void
prefs_set (const gchar *key, const gchar *value)
{
  if (!global_prefs)
    prefs_init ();

  g_hash_table_insert (global_prefs, g_strdup (key), g_strdup (value));
}

/**
 * @brief Apply the configs from given file as preferences.
 *
 * @param config    Filename of the configuration file.
 */
void
prefs_config (const char *config)
{
  settings_iterator_t settings;

  if (!global_prefs)
    prefs_init ();

  if (!init_settings_iterator_from_file (&settings, config, "Misc"))
    {
      while (settings_iterator_next (&settings))
        {
          gchar *value;

          value = settings_iterator_value (&settings);
          prefs_set (settings_iterator_name (&settings), value);
          g_free (value);
        }

      cleanup_settings_iterator (&settings);
    }

  prefs_set ("config_file", config);
}

/**
 * @brief Dump the preferences to stdout
 */
void
prefs_dump (void)
{
  void *name, *value;
  GHashTableIter iter;

  if (global_prefs)
    {
      g_hash_table_iter_init (&iter, global_prefs);
      while (g_hash_table_iter_next (&iter, &name, &value))
        {
          printf ("%s = %s\n", (char *) name, (char *) value);
        }
    }
}