OpenVAS Scanner  22.7.9
utils.c
Go to the documentation of this file.
1 /* SPDX-FileCopyrightText: 2023 Greenbone AG
2  * SPDX-FileCopyrightText: 2006 Software in the Public Interest, Inc.
3  * SPDX-FileCopyrightText: 1998-2006 Tenable Network Security, Inc.
4  *
5  * SPDX-License-Identifier: GPL-2.0-only
6  */
7 
13 #include "utils.h"
14 
15 #include "../misc/plugutils.h" /* for kb_item_set_int_with_main_kb_check */
16 #include "../misc/scanneraux.h" /* for struct scan_globals */
17 
18 #include <errno.h> /* for errno() */
19 #include <gvm/base/prefs.h> /* for prefs_get() */
20 #include <gvm/boreas/cli.h> /* for is_host_alive() */
21 #include <stdlib.h> /* for atoi() */
22 #include <string.h> /* for strcmp() */
23 #include <sys/ioctl.h> /* for ioctl() */
24 #include <sys/wait.h> /* for waitpid() */
25 
26 extern int global_max_hosts;
27 extern int global_max_checks;
28 
29 #undef G_LOG_DOMAIN
30 
33 #define G_LOG_DOMAIN "sd main"
34 
47 static void
48 files_add_translation (struct scan_globals *globals, const char *file_hash,
49  char *contents)
50 {
51  GHashTable *trans = globals->files_translation;
52  // Register the mapping table if none there yet
53  if (trans == NULL)
54  {
55  trans = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
56  globals->files_translation = trans;
57  }
58 
59  g_hash_table_insert (trans, g_strdup (file_hash), contents);
60 }
61 
74 static void
75 files_add_size_translation (struct scan_globals *globals, const char *file_hash,
76  const long filesize)
77 {
78  GHashTable *trans = globals->files_size_translation;
79  gchar *filesize_str = g_strdup_printf ("%ld", filesize);
80 
81  // Register the mapping table if none there yet
82  if (trans == NULL)
83  {
84  trans = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
85  globals->files_size_translation = trans;
86  }
87 
88  g_hash_table_insert (trans, g_strdup (file_hash), g_strdup (filesize_str));
89 }
90 
100 int
101 store_file (struct scan_globals *globals, const char *file,
102  const char *file_hash)
103 {
104  char *origname;
105  gchar *contents = NULL;
106 
107  size_t bytes = 0;
108 
109  if (!file_hash || *file_hash == '\0')
110  return -1;
111 
112  origname = g_strdup (file_hash);
113 
114  contents = (gchar *) g_base64_decode (file, &bytes);
115 
116  if (contents == NULL)
117  {
118  g_debug ("store_file: Failed to allocate memory for uploaded file.");
119  g_free (origname);
120  return -1;
121  }
122 
123  files_add_translation (globals, origname, contents);
124  files_add_size_translation (globals, origname, bytes);
125 
126  g_free (origname);
127  return 0;
128 }
129 
133 int
135 {
136  int max_hosts;
137  if (prefs_get ("max_hosts"))
138  {
139  max_hosts = atoi (prefs_get ("max_hosts"));
140  if (max_hosts <= 0)
141  {
142  g_debug ("Error ! max_hosts = %d -- check %s", max_hosts,
143  (char *) prefs_get ("config_file"));
144  max_hosts = global_max_hosts;
145  }
146  else if (max_hosts > global_max_hosts)
147  {
148  g_debug ("Client tried to raise the maximum hosts number - %d."
149  " Using %d. Change 'max_hosts' in openvas.conf if you"
150  " believe this is incorrect",
151  max_hosts, global_max_hosts);
152  max_hosts = global_max_hosts;
153  }
154  }
155  else
156  max_hosts = global_max_hosts;
157  return (max_hosts);
158 }
159 
164 int
166 {
167  int max_checks;
168  if (prefs_get ("max_checks"))
169  {
170  max_checks = atoi (prefs_get ("max_checks"));
171  if (max_checks <= 0)
172  {
173  g_debug ("Error ! max_hosts = %d -- check %s", max_checks,
174  (char *) prefs_get ("config_file"));
175  max_checks = global_max_checks;
176  }
177  else if (max_checks > global_max_checks)
178  {
179  g_debug ("Client tried to raise the maximum checks number - %d."
180  " Using %d. Change 'max_checks' in openvas.conf if you"
181  " believe this is incorrect",
182  max_checks, global_max_checks);
183  max_checks = global_max_checks;
184  }
185  }
186  else
187  max_checks = global_max_checks;
188  return (max_checks);
189 }
190 
194 int
196 {
197  int i, ret;
198  if (pid == 0)
199  return 0;
200 
201  for (i = 0, ret = 1; (i < 10) && (ret > 0); i++)
202  ret = waitpid (pid, NULL, WNOHANG);
203 
204  return kill (pid, 0) == 0;
205 }
206 
207 int
208 data_left (int soc)
209 {
210  int data = 0;
211  ioctl (soc, FIONREAD, &data);
212  return data;
213 }
214 
215 void
217 {
218  int e, n = 0;
219  do
220  {
221  errno = 0;
222  e = waitpid (-1, NULL, WNOHANG);
223  n++;
224  }
225  while ((e > 0 || errno == EINTR) && n < 20);
226 }
227 
228 /*
229  * @brief Checks if a provided preference is scanner-only and can't be
230  * read/written by the client.
231  *
232  * @return 1 if pref is scanner-only, 0 otherwise.
233  */
234 int
235 is_scanner_only_pref (const char *pref)
236 {
237  if (pref == NULL)
238  return 0;
239  if (!strcmp (pref, "config_file") || !strcmp (pref, "plugins_folder")
240  || !strcmp (
241  pref,
242  "kb_location") // old name of db_address, ignore from old conf's
243  || !strcmp (pref, "db_address") || !strcmp (pref, "negot_timeout")
244  || !strcmp (pref, "force_pubkey_auth")
245  || !strcmp (pref, "log_whole_attack")
246  || !strcmp (pref, "log_plugins_name_at_load")
247  || !strcmp (pref, "nasl_no_signature_check")
248  || !strcmp (pref, "vendor_version") || !strcmp (pref, "drop_privileges")
249  || !strcmp (pref, "nasl_drop_privileges_user")
250  || !strcmp (pref, "debug_tls") || !strcmp (pref, "min_free_mem")
251  || !strcmp (pref, "max_sysload")
252  /* Preferences starting with sys_ are scanner-side only. */
253  || !strncmp (pref, "sys_", 4))
254  return 1;
255  return 0;
256 }
scan_globals::files_size_translation
GHashTable * files_size_translation
Definition: scanneraux.h:21
store_file
int store_file(struct scan_globals *globals, const char *file, const char *file_hash)
Stores a file type preference in a hash table.
Definition: utils.c:101
data_left
int data_left(int soc)
Definition: utils.c:208
scan_globals::files_translation
GHashTable * files_translation
Definition: scanneraux.h:20
utils.h
utils.c headerfile.
pid
static pid_t pid
Definition: nasl_cmd_exec.c:39
wait_for_children1
void wait_for_children1(void)
Definition: utils.c:216
process_alive
int process_alive(pid_t pid)
Definition: utils.c:195
scan_globals
Definition: scanneraux.h:19
files_add_translation
static void files_add_translation(struct scan_globals *globals, const char *file_hash, char *contents)
Adds a 'translation' entry for a file sent by the client.
Definition: utils.c:48
global_max_hosts
int global_max_hosts
Definition: openvas.c:83
get_max_checks_number
int get_max_checks_number(void)
Definition: utils.c:165
files_add_size_translation
static void files_add_size_translation(struct scan_globals *globals, const char *file_hash, const long filesize)
Adds a 'content size' entry for a file sent by the client.
Definition: utils.c:75
is_scanner_only_pref
int is_scanner_only_pref(const char *pref)
Definition: utils.c:235
get_max_hosts_number
int get_max_hosts_number(void)
Definition: utils.c:134
global_max_checks
int global_max_checks
Definition: openvas.c:84