OpenVAS Scanner 22.7.9
utils.h File Reference

utils.c headerfile. More...

#include "../misc/scanneraux.h"
#include <sys/types.h>
Include dependency graph for utils.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

int get_max_hosts_number (void)
 
int get_max_checks_number (void)
 
int process_alive (pid_t)
 
int data_left (int)
 
void wait_for_children1 (void)
 
int is_scanner_only_pref (const char *)
 
int store_file (struct scan_globals *globals, const char *file, const char *file_hash)
 Stores a file type preference in a hash table.
 
int check_host_still_alive (kb_t, const char *)
 Check if the hosts is still alive and set it as dead if not.
 

Detailed Description

utils.c headerfile.

Definition in file utils.h.

Function Documentation

◆ check_host_still_alive()

int check_host_still_alive ( kb_t  kb,
const char *  hostname 
)

Check if the hosts is still alive and set it as dead if not.

Parameters
kbHost kb where the host is set as dead.
Returns
1 if considered alive, 0 if it is dead. -1 on error or option disabled.

Definition at line 33 of file heartbeat.c.

34{
35 int is_alive = 0;
36 boreas_error_t alive_err;
37
38 /* Heartbeat will work only with boreas enabled. We check if we
39 have all what we need before running a heartbeat check. */
40 if (prefs_get_bool ("test_alive_hosts_only"))
41 {
42 const gchar *alive_test_str = prefs_get ("ALIVE_TEST");
43
44 /* Don't perform a hearbeat check if the host is always considered
45 alive or the alive test is not valid. */
46 if (!(alive_test_str
47 && atoi (alive_test_str) >= ALIVE_TEST_TCP_ACK_SERVICE
48 && atoi (alive_test_str) < 32 // max value for alive test combi.
49 && !((atoi (alive_test_str)) & ALIVE_TEST_CONSIDER_ALIVE)))
50 return -1;
51 }
52 else
53 {
54 g_warning ("%s: Trying to perform an alive test, but Boreas is not "
55 "enabled. Heartbeat check for %s will not be performed",
56 __func__, hostname);
57 return -1;
58 }
59
60 alive_err = is_host_alive (hostname, &is_alive);
61 if (alive_err)
62 {
63 g_warning ("%s: Heartbeat check failed for %s with error %d.", __func__,
64 hostname, alive_err);
65 return -1;
66 }
67
68 if (is_alive == 0)
69 {
70 g_message ("%s: Heartbeat check was not successful. The host %s has"
71 " been set as dead.",
72 __func__, hostname);
73 kb_item_set_int_with_main_kb_check (kb, "Host/dead", 1);
74 return 0;
75 }
76
77 return 1;
78}
const char * hostname
Definition: pluginlaunch.c:68
int kb_item_set_int_with_main_kb_check(kb_t kb, const char *name, int value)
Check if the current kb corresponds to the original scanid, if it matches it call kb_item_set_int....
Definition: plugutils.c:554

References hostname, and kb_item_set_int_with_main_kb_check().

Referenced by nasl_end_denial(), and update_running_processes().

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

◆ data_left()

int data_left ( int  soc)

Definition at line 208 of file utils.c.

209{
210 int data = 0;
211 ioctl (soc, FIONREAD, &data);
212 return data;
213}

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

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}
int global_max_checks
Definition: openvas.c:84

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

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}
int global_max_hosts
Definition: openvas.c:83

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

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}

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

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}
static pid_t pid
Definition: nasl_cmd_exec.c:39

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

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}
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
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

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

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}