OpenVAS Scanner  7.0.1~git
utils.c
Go to the documentation of this file.
1 /* Portions Copyright (C) 2009-2019 Greenbone Networks GmbH
2  * Portions Copyright (C) 2006 Software in the Public Interest, Inc.
3  * Based on work Copyright (C) 1998 - 2006 Tenable Network Security, Inc.
4  *
5  * SPDX-License-Identifier: GPL-2.0-only
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20 
26 #include "../misc/scanneraux.h" /* for struct scan_globals */
27 
28 #include <errno.h> /* for errno() */
29 #include <gvm/base/prefs.h> /* for prefs_get() */
30 #include <stdlib.h> /* for atoi() */
31 #include <string.h> /* for strcmp() */
32 #include <sys/ioctl.h> /* for ioctl() */
33 #include <sys/wait.h> /* for waitpid() */
34 
35 extern int global_max_hosts;
36 extern int global_max_checks;
37 
38 #undef G_LOG_DOMAIN
39 
42 #define G_LOG_DOMAIN "sd main"
43 
56 static void
57 files_add_translation (struct scan_globals *globals, const char *file_hash,
58  char *contents)
59 {
60  GHashTable *trans = globals->files_translation;
61  // Register the mapping table if none there yet
62  if (trans == NULL)
63  {
64  trans = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
65  globals->files_translation = trans;
66  }
67 
68  g_hash_table_insert (trans, g_strdup (file_hash), contents);
69 }
70 
83 static void
84 files_add_size_translation (struct scan_globals *globals, const char *file_hash,
85  const long filesize)
86 {
87  GHashTable *trans = globals->files_size_translation;
88  gchar *filesize_str = g_strdup_printf ("%ld", filesize);
89 
90  // Register the mapping table if none there yet
91  if (trans == NULL)
92  {
93  trans = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
94  globals->files_size_translation = trans;
95  }
96 
97  g_hash_table_insert (trans, g_strdup (file_hash), g_strdup (filesize_str));
98 }
99 
109 int
110 store_file (struct scan_globals *globals, const char *file,
111  const char *file_hash)
112 {
113  char *origname;
114  gchar *contents = NULL;
115 
116  size_t bytes = 0;
117 
118  if (!file_hash && *file_hash == '\0')
119  return -1;
120 
121  origname = g_strdup (file_hash);
122 
123  contents = (gchar *) g_base64_decode (file, &bytes);
124 
125  if (contents == NULL)
126  {
127  g_debug ("store_file: Failed to allocate memory for uploaded file.");
128  g_free (origname);
129  return -1;
130  }
131 
132  files_add_translation (globals, origname, contents);
133  files_add_size_translation (globals, origname, bytes);
134 
135  g_free (origname);
136  return 0;
137 }
138 
142 int
144 {
145  int max_hosts;
146  if (prefs_get ("max_hosts"))
147  {
148  max_hosts = atoi (prefs_get ("max_hosts"));
149  if (max_hosts <= 0)
150  {
151  g_debug ("Error ! max_hosts = %d -- check %s", max_hosts,
152  (char *) prefs_get ("config_file"));
153  max_hosts = global_max_hosts;
154  }
155  else if (max_hosts > global_max_hosts)
156  {
157  g_debug ("Client tried to raise the maximum hosts number - %d."
158  " Using %d. Change 'max_hosts' in openvas.conf if you"
159  " believe this is incorrect",
160  max_hosts, global_max_hosts);
161  max_hosts = global_max_hosts;
162  }
163  }
164  else
165  max_hosts = global_max_hosts;
166  return max_hosts;
167 }
168 
173 int
175 {
176  int max_checks;
177  if (prefs_get ("max_checks"))
178  {
179  max_checks = atoi (prefs_get ("max_checks"));
180  if (max_checks <= 0)
181  {
182  g_debug ("Error ! max_hosts = %d -- check %s", max_checks,
183  (char *) prefs_get ("config_file"));
184  max_checks = global_max_checks;
185  }
186  else if (max_checks > global_max_checks)
187  {
188  g_debug ("Client tried to raise the maximum checks number - %d."
189  " Using %d. Change 'max_checks' in openvas.conf if you"
190  " believe this is incorrect",
191  max_checks, global_max_checks);
192  max_checks = global_max_checks;
193  }
194  }
195  else
196  max_checks = global_max_checks;
197  return max_checks;
198 }
199 
203 int
205 {
206  int i, ret;
207  if (pid == 0)
208  return 0;
209 
210  for (i = 0, ret = 1; (i < 10) && (ret > 0); i++)
211  ret = waitpid (pid, NULL, WNOHANG);
212 
213  return kill (pid, 0) == 0;
214 }
215 
216 int data_left (soc) int soc;
217 {
218  int data = 0;
219  ioctl (soc, FIONREAD, &data);
220  return data;
221 }
222 
223 void
225 {
226  int e, n = 0;
227  do
228  {
229  errno = 0;
230  e = waitpid (-1, NULL, WNOHANG);
231  n++;
232  }
233  while ((e > 0 || errno == EINTR) && n < 20);
234 }
235 
236 /*
237  * @brief Checks if a provided preference is scanner-only and can't be
238  * read/written by the client.
239  *
240  * @return 1 if pref is scanner-only, 0 otherwise.
241  */
242 int
243 is_scanner_only_pref (const char *pref)
244 {
245  if (pref == NULL)
246  return 0;
247  if (!strcmp (pref, "logfile") || !strcmp (pref, "config_file")
248  || !strcmp (pref, "plugins_folder")
249  || !strcmp (
250  pref,
251  "kb_location") // old name of db_address, ignore from old conf's
252  || !strcmp (pref, "db_address") || !strcmp (pref, "negot_timeout")
253  || !strcmp (pref, "force_pubkey_auth")
254  || !strcmp (pref, "log_whole_attack")
255  || !strcmp (pref, "log_plugins_name_at_load")
256  || !strcmp (pref, "nasl_no_signature_check")
257  || !strcmp (pref, "vendor_version")
258  /* Preferences starting with sys_ are scanner-side only. */
259  || !strncmp (pref, "sys_", 4))
260  return 1;
261  return 0;
262 }
scan_globals::files_size_translation
GHashTable * files_size_translation
Definition: scanneraux.h:37
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:110
pid
static pid_t pid
Definition: nasl_builtin_nmap.c:499
data_left
int data_left(int soc)
Definition: utils.c:216
scan_globals::files_translation
GHashTable * files_translation
Definition: scanneraux.h:36
wait_for_children1
void wait_for_children1(void)
Definition: utils.c:224
process_alive
int process_alive(pid_t pid)
Definition: utils.c:204
scan_globals
Definition: scanneraux.h:32
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:57
global_max_hosts
int global_max_hosts
Definition: openvas.c:89
get_max_checks_number
int get_max_checks_number(void)
Definition: utils.c:174
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:84
is_scanner_only_pref
int is_scanner_only_pref(const char *pref)
Definition: utils.c:243
get_max_hosts_number
int get_max_hosts_number(void)
Definition: utils.c:143
global_max_checks
int global_max_checks
Definition: openvas.c:90