OpenVAS Scanner 23.32.3
utils.c File Reference

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

#include "utils.h"
#include "../misc/plugutils.h"
#include "../misc/scanneraux.h"
#include "base/networking.h"
#include <errno.h>
#include <fcntl.h>
#include <gvm/base/prefs.h>
#include <gvm/boreas/cli.h>
#include <stdio.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.

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.
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.
int store_file (struct scan_globals *globals, const char *file, const char *file_hash)
 Stores a file type preference in a hash table.
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)
void write_script_stats (const char *buf, const char *path, int mode)
 Writes scripts stats into a file.
void write_host_stats (kb_t kb, const char *scan_id, const char *ip)
 Reads the script stats from the kb and generate a string in json format to be stored in the disk.

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 36 of file utils.c.

Function Documentation

◆ data_left()

int data_left ( int soc)

Definition at line 211 of file utils.c.

212{
213 int data = 0;
214 ioctl (soc, FIONREAD, &data);
215 return data;
216}

◆ files_add_size_translation()

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 78 of file utils.c.

80{
81 GHashTable *trans = globals->files_size_translation;
82 gchar *filesize_str = g_strdup_printf ("%ld", filesize);
83
84 // Register the mapping table if none there yet
85 if (trans == NULL)
86 {
87 trans = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
88 globals->files_size_translation = trans;
89 }
90
91 g_hash_table_insert (trans, g_strdup (file_hash), g_strdup (filesize_str));
92}
GHashTable * files_size_translation
Definition scanneraux.h:21

References scan_globals::files_size_translation.

Referenced by store_file().

Here is the caller graph for this function:

◆ files_add_translation()

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 51 of file utils.c.

53{
54 GHashTable *trans = globals->files_translation;
55 // Register the mapping table if none there yet
56 if (trans == NULL)
57 {
58 trans = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
59 globals->files_translation = trans;
60 }
61
62 g_hash_table_insert (trans, g_strdup (file_hash), contents);
63}
GHashTable * files_translation
Definition scanneraux.h:20

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 168 of file utils.c.

169{
170 int max_checks;
171 if (prefs_get ("max_checks"))
172 {
173 max_checks = atoi (prefs_get ("max_checks"));
174 if (max_checks <= 0)
175 {
176 g_debug ("Error ! max_hosts = %d -- check %s", max_checks,
177 (char *) prefs_get ("config_file"));
178 max_checks = global_max_checks;
179 }
180 else if (max_checks > global_max_checks)
181 {
182 g_debug ("Client tried to raise the maximum checks number - %d."
183 " Using %d. Change 'max_checks' in openvas.conf if you"
184 " believe this is incorrect",
185 max_checks, global_max_checks);
186 max_checks = global_max_checks;
187 }
188 }
189 else
190 max_checks = global_max_checks;
191 return (max_checks);
192}
int global_max_checks
Definition openvas.c:85

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 137 of file utils.c.

138{
139 int max_hosts;
140 if (prefs_get ("max_hosts"))
141 {
142 max_hosts = atoi (prefs_get ("max_hosts"));
143 if (max_hosts <= 0)
144 {
145 g_debug ("Error ! max_hosts = %d -- check %s", max_hosts,
146 (char *) prefs_get ("config_file"));
147 max_hosts = global_max_hosts;
148 }
149 else if (max_hosts > global_max_hosts)
150 {
151 g_debug ("Client tried to raise the maximum hosts number - %d."
152 " Using %d. Change 'max_hosts' in openvas.conf if you"
153 " believe this is incorrect",
154 max_hosts, global_max_hosts);
155 max_hosts = global_max_hosts;
156 }
157 }
158 else
159 max_hosts = global_max_hosts;
160 return (max_hosts);
161}
int global_max_hosts
Definition openvas.c:84

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 238 of file utils.c.

239{
240 if (pref == NULL)
241 return 0;
242 if (!strcmp (pref, "config_file") || !strcmp (pref, "plugins_folder")
243 || !strcmp (
244 pref,
245 "kb_location") // old name of db_address, ignore from old conf's
246 || !strcmp (pref, "db_address") || !strcmp (pref, "negot_timeout")
247 || !strcmp (pref, "force_pubkey_auth")
248 || !strcmp (pref, "log_whole_attack")
249 || !strcmp (pref, "log_plugins_name_at_load")
250 || !strcmp (pref, "nasl_no_signature_check")
251 || !strcmp (pref, "vendor_version") || !strcmp (pref, "drop_privileges")
252 || !strcmp (pref, "nasl_drop_privileges_user")
253 || !strcmp (pref, "debug_tls") || !strcmp (pref, "min_free_mem")
254 || !strcmp (pref, "max_sysload")
255 /* Preferences starting with sys_ are scanner-side only. */
256 || !strncmp (pref, "sys_", 4))
257 return 1;
258 return 0;
259}

Referenced by overwrite_openvas_prefs_with_prefs_from_client().

Here is the caller graph for this function:

◆ process_alive()

int process_alive ( pid_t pid)

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

Definition at line 198 of file utils.c.

199{
200 int i, ret;
201 if (pid == 0)
202 return 0;
203
204 for (i = 0, ret = 1; (i < 10) && (ret > 0); i++)
205 ret = waitpid (pid, NULL, WNOHANG);
206
207 return kill (pid, 0) == 0;
208}
static pid_t pid

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 104 of file utils.c.

106{
107 char *origname;
108 gchar *contents = NULL;
109
110 size_t bytes = 0;
111
112 if (!file_hash || *file_hash == '\0')
113 return -1;
114
115 origname = g_strdup (file_hash);
116
117 contents = (gchar *) g_base64_decode (file, &bytes);
118
119 if (contents == NULL)
120 {
121 g_debug ("store_file: Failed to allocate memory for uploaded file.");
122 g_free (origname);
123 return -1;
124 }
125
126 files_add_translation (globals, origname, contents);
127 files_add_size_translation (globals, origname, bytes);
128
129 g_free (origname);
130 return 0;
131}
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:78
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:51

References files_add_size_translation(), and files_add_translation().

Referenced by overwrite_openvas_prefs_with_prefs_from_client().

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 219 of file utils.c.

220{
221 int e, n = 0;
222 do
223 {
224 errno = 0;
225 e = waitpid (-1, NULL, WNOHANG);
226 n++;
227 }
228 while ((e > 0 || errno == EINTR) && n < 20);
229}

◆ write_host_stats()

void write_host_stats ( kb_t kb,
const char * scan_id,
const char * ip )

Reads the script stats from the kb and generate a string in json format to be stored in the disk.

Parameters
kbthe host knowledge base to get the information from.
scan_idScan ID for the file name.
iptarget IP address.

Definition at line 308 of file utils.c.

309{
310 GString *data = g_string_new ("");
311 struct kb_item *stats = NULL, *stats_tmp = NULL;
312 int firstvt = 1;
313 char *path = NULL;
314
315 if (!prefs_get ("report_scripts"))
316 return;
317
318 stats = kb_item_get_pattern (kb, "general/script_stats*");
319 stats_tmp = stats;
320
321 g_string_append_printf (data, "\"%s\": [", ip);
322 while (stats_tmp)
323 {
324 char **spl = g_strsplit (stats_tmp->v_str, "/", 0);
325 char *buf = NULL;
326
327 if (!firstvt)
328 g_string_append_c (data, ',');
329
330 buf = g_strdup_printf ("{\"%s\": {\"start\": %s, \"stop\": %s}}", spl[0],
331 spl[1], spl[2]);
332
333 g_string_append (data, buf);
334 g_strfreev (spl);
335 g_free (buf);
336
337 stats_tmp = stats_tmp->next;
338 if (firstvt)
339 firstvt = 0;
340 }
341 g_string_append (data, "],");
342
343 path =
344 g_strdup_printf ("%s/%s-stats.json", prefs_get ("report_scripts"), scan_id);
345
346 kb_item_free (stats);
347 write_script_stats (data->str, path, 0);
348 g_free (path);
349 g_string_free (data, TRUE);
350}
const char * scan_id
Definition scan_id.c:10
void write_script_stats(const char *buf, const char *path, int mode)
Writes scripts stats into a file.
Definition utils.c:271

References scan_id, and write_script_stats().

Referenced by attack_host().

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

◆ write_script_stats()

void write_script_stats ( const char * buf,
const char * path,
int mode )

Writes scripts stats into a file.

Parameters
bufString to write.
pathPath to the file to write into.
mode2 to create the file, 0 to append text to the file, 1 to finish the json list removing the trailing comma before appending the last text in the buffer.

Definition at line 271 of file utils.c.

272{
273 FILE *pd = NULL;
274
275 if (mode < 0 || mode > 2)
276 {
277 g_warning ("%s: invalid mode %d", __func__, mode);
278 return;
279 }
280 pd = fopen (path, mode == 0 ? "a" : mode == 1 ? "r+" : "w");
281 if (pd == NULL)
282 {
283 g_warning ("%s: Error opening FILE '%s' for script stats: %d - %s",
284 __func__, path, errno, strerror (errno));
285 return;
286 }
287
288 if (mode == 1)
289 {
290 int ch;
291 while ((ch = fgetc (pd)) != EOF)
292 ;
293 fseek (pd, -1, SEEK_CUR);
294 }
295 fprintf (pd, "%s", buf);
296 fflush (pd);
297 fclose (pd);
298}

Referenced by attack_network(), and write_host_stats().

Here is the caller graph for this function:

Variable Documentation

◆ global_max_checks

int global_max_checks
extern

Definition at line 85 of file openvas.c.

Referenced by get_max_checks_number(), and set_globals_from_preferences().

◆ global_max_hosts

int global_max_hosts
extern

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

Definition at line 84 of file openvas.c.

Referenced by get_max_hosts_number(), and set_globals_from_preferences().