OpenVAS Scanner  7.0.1~git
utils.c File Reference

A bunch of miscellaneous functions, mostly file conversions. More...

#include "../misc/scanneraux.h"
#include <errno.h>
#include <gvm/base/prefs.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/wait.h>
Include dependency graph for utils.c:

Go to the source code of this file.

Macros

#define G_LOG_DOMAIN   "sd main"
 GLib log domain. More...
 

Functions

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. More...
 
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. More...
 
int store_file (struct scan_globals *globals, const char *file, const char *file_hash)
 Stores a file type preference in a hash table. More...
 
int get_max_hosts_number (void)
 
int get_max_checks_number (void)
 
int process_alive (pid_t pid)
 
int data_left (int soc)
 
void wait_for_children1 (void)
 
int is_scanner_only_pref (const char *pref)
 

Variables

int global_max_hosts
 
int global_max_checks
 

Detailed Description

A bunch of miscellaneous functions, mostly file conversions.

Definition in file utils.c.

Macro Definition Documentation

◆ G_LOG_DOMAIN

#define G_LOG_DOMAIN   "sd main"

GLib log domain.

Definition at line 42 of file utils.c.

Function Documentation

◆ data_left()

int data_left ( int  soc)

Definition at line 216 of file utils.c.

217 {
218  int data = 0;
219  ioctl (soc, FIONREAD, &data);
220  return data;
221 }

◆ files_add_size_translation()

static void files_add_size_translation ( struct scan_globals globals,
const char *  file_hash,
const long  filesize 
)
static

Adds a 'content size' entry for a file sent by the client.

Files sent by the client are stored in memory on the server side. Because they may be binary we need to store the size of the uploaded file as well. This function sets up a mapping from the original name sent by the client to the file size.

Parameters
globalsGlobal struct.
file_hashhash to reference the file.
filesizeSize of the file in bytes.

Definition at line 84 of file utils.c.

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 }

References scan_globals::files_size_translation.

Referenced by store_file().

Here is the caller graph for this function:

◆ files_add_translation()

static void files_add_translation ( struct scan_globals globals,
const char *  file_hash,
char *  contents 
)
static

Adds a 'translation' entry for a file sent by the client.

Files sent by the client are stored in memory on the server side. In order to access these files, their original name ('local' to the client) can be 'translated' into the file contents of the in-memory copy of the file on the server side.

Parameters
globalsGlobal struct.
file_hashhash to reference the file.
contentsContents of the file.

Definition at line 57 of file utils.c.

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 }

References scan_globals::files_translation.

Referenced by store_file().

Here is the caller graph for this function:

◆ get_max_checks_number()

int get_max_checks_number ( void  )

Get the max number of plugins to launch against the remote host at the same time

Definition at line 174 of file utils.c.

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 }

References global_max_checks.

Referenced by attack_network(), and pluginlaunch_init().

Here is the caller graph for this function:

◆ get_max_hosts_number()

int get_max_hosts_number ( void  )

Get the max number of hosts to test at the same time.

Definition at line 143 of file utils.c.

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 }

References global_max_hosts.

Referenced by attack_network().

Here is the caller graph for this function:

◆ is_scanner_only_pref()

int is_scanner_only_pref ( const char *  pref)

Definition at line 243 of file utils.c.

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 }

◆ process_alive()

int process_alive ( pid_t  pid)

Determines if a process is alive - as reliably as we can

Definition at line 204 of file utils.c.

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 }

References pid.

Referenced by attack_host(), next_free_process(), and update_running_processes().

Here is the caller graph for this function:

◆ 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.

Parameters
globalsGlobal struct.
fileFile content.
file_hashhash to reference the file.
Returns
0 if successful, -1 in case of errors.

Definition at line 110 of file utils.c.

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 }

References files_add_size_translation(), and files_add_translation().

Referenced by load_scan_preferences().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ wait_for_children1()

void wait_for_children1 ( void  )

Definition at line 224 of file utils.c.

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 }

Variable Documentation

◆ global_max_checks

int global_max_checks

Definition at line 90 of file openvas.c.

Referenced by get_max_checks_number(), and set_globals_from_preferences().

◆ global_max_hosts

int global_max_hosts

Globals that should not be touched (used in utils module).

Definition at line 89 of file openvas.c.

Referenced by get_max_hosts_number(), and set_globals_from_preferences().

scan_globals::files_size_translation
GHashTable * files_size_translation
Definition: scanneraux.h:37
pid
static pid_t pid
Definition: nasl_builtin_nmap.c:499
scan_globals::files_translation
GHashTable * files_translation
Definition: scanneraux.h:36
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
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
global_max_checks
int global_max_checks
Definition: openvas.c:90