OpenVAS Scanner 23.40.3
hosts.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
12
13#include "hosts.h" /* for hosts_new() */
14
15#include "../misc/network.h" /* for internal_recv */
16#include "../misc/plugutils.h"
17#include "utils.h" /* for data_left() */
18
19#include <errno.h> /* for errno() */
20#include <glib.h> /* for g_free() */
21#include <gvm/base/networking.h> /* for gvm_resolve_list */
22#include <stdio.h> /* for snprintf() */
23#include <string.h> /* for strlen() */
24#include <sys/wait.h> /* for waitpid() */
25#include <unistd.h> /* for close() */
26
27#undef G_LOG_DOMAIN
31#define G_LOG_DOMAIN "sd main"
32
36struct host
37{
38 char *name;
39 char *ip;
40 pid_t pid;
41 kb_t host_kb;
43 struct host *next;
44 struct host *prev;
45};
46
48
49static struct host *hosts = NULL;
50static int g_max_hosts = 15;
51
52/*-------------------------------------------------------------------*/
53extern int global_scan_stop;
54
63void
64host_set_time (kb_t kb, char *ip, char *type)
65{
66 char *timestr;
67 char log_msg[1024];
68 time_t t;
69 int len;
70
71 t = time (NULL);
72 char ts[26];
73 char *ts_ptr = ts;
74 ctime_r (&t, ts_ptr);
75 timestr = g_strdup (ts_ptr);
76 len = strlen (timestr);
77 if (timestr[len - 1] == '\n')
78 timestr[len - 1] = '\0';
79
80 snprintf (log_msg, sizeof (log_msg), "%s|||%s||||||||| |||%s", type, ip,
81 timestr);
82 g_free (timestr);
83
84 kb_item_push_str_with_main_kb_check (kb, "internal/results", log_msg);
85}
86
87static void
88host_rm (struct host *h)
89{
90 if (h->pid != 0)
91 waitpid (h->pid, NULL, WNOHANG);
92
93 if (h->next != NULL)
94 h->next->prev = h->prev;
95
96 if (h->prev != NULL)
97 h->prev->next = h->next;
98
99 if (h->host_kb)
100 {
101 kb_delete (h->host_kb);
102 h->host_kb = NULL;
103 kb_lnk_reset (h->results_kb);
104 }
105
106 g_free (h->name);
107 g_free (h);
108}
109
110/*-----------------------------------------------------------------*/
111
115static int
117{
118 struct host *h = hosts;
119 int num;
120
121 for (num = 0; h != NULL; num++, h = h->next)
122 ;
123
124 return num;
125}
126
130static struct host *
132{
133 struct host *h = hosts;
134 while (h != NULL)
135 {
136 if (strcmp (h->name, name) == 0)
137 return h;
138 h = h->next;
139 }
140 return NULL;
141}
142
143int
144hosts_init (int max_hosts)
145{
146 g_max_hosts = max_hosts;
147 return 0;
148}
149
150int
151hosts_new (char *name, kb_t kb, kb_t main_kb)
152{
153 struct host *h;
154
155 while (hosts_num () >= g_max_hosts)
156 {
157 if (hosts_read () < 0)
158 return -1;
159 }
161 return 0;
162
163 h = g_malloc0 (sizeof (struct host));
164 h->name = g_strdup (name);
165 h->pid = 0;
166 h->host_kb = kb;
167 h->results_kb = main_kb;
168 if (hosts != NULL)
169 hosts->prev = h;
170 h->next = hosts;
171 h->prev = NULL;
172 hosts = h;
173 return 0;
174}
175
176int
177hosts_set_pid (char *name, pid_t pid)
178{
179 struct host *h = hosts_get (name);
180 if (h == NULL)
181 {
182 g_debug ("host_set_pid() failed!\n");
183 return -1;
184 }
185
186 h->pid = pid;
187 return 0;
188}
189
190/*-----------------------------------------------------------------*/
191static int
193{
194 if (h == NULL)
195 return -1;
196
197 g_message ("Stopping host %s scan (pid: %d)", h->name, h->pid);
198 kill (h->pid, SIGUSR1);
199 return 0;
200}
201
202void
204{
205 struct host *host = hosts;
206
208 while (host)
209 {
211 host = host->next;
212 }
213}
214
215/*-----------------------------------------------------------------*/
216
217static void
219{
220 struct host *h = hosts;
221 int ret = 1;
222
223 while (ret > 0)
224 {
225 ret = waitpid (-1, NULL, WNOHANG);
226 if (ret < 0)
227 g_debug ("waitpid() failed. %s)", strerror (errno));
228 }
229
230 if (h == NULL)
231 return;
232
233 while (h)
234 {
235 if (h->pid != 0 && kill (h->pid, 0) < 0) /* Process is dead */
236 {
237 if (!h->prev)
238 hosts = hosts->next;
239 host_rm (h);
240 h = hosts;
241 if (!h)
242 break;
243 }
244 h = h->next;
245 }
246}
247
252int
254{
255 if (hosts == NULL)
256 return -1;
257
259 usleep (500000);
260
261 return 0;
262}
263
270int
271host_is_currently_scanned (gvm_host_t *host_to_check)
272{
273 struct host *h = hosts;
274
275 GSList *list, *tmp;
276 char *vhost = NULL;
277
278 hosts_read ();
279
280 if (h == NULL)
281 return 0;
282
283 vhost = gvm_host_reverse_lookup (host_to_check);
284 if (!vhost)
285 return 0;
286
287 list = tmp = gvm_resolve_list (vhost);
288 g_free (vhost);
289 while (tmp)
290 {
291 h = hosts;
292 char buffer[INET6_ADDRSTRLEN];
293 addr6_to_str (tmp->data, buffer);
294
295 while (h != NULL)
296 {
297 if (!strcasecmp (h->name, buffer))
298 {
299 g_slist_free_full (list, g_free);
300 return 1;
301 }
302 h = h->next;
303 }
304 tmp = tmp->next;
305 }
306
307 g_slist_free_full (list, g_free);
308 return 0;
309}
int global_scan_stop
Definition attack.c:261
static void hosts_read_data(void)
Definition hosts.c:218
static struct host * hosts
Definition hosts.c:49
static void host_rm(struct host *h)
Definition hosts.c:88
static int hosts_stop_host(struct host *h)
Definition hosts.c:192
static struct host * hosts_get(char *name)
Retrieves a host specified by its name from the global host list.
Definition hosts.c:131
static int g_max_hosts
Definition hosts.c:50
void host_set_time(kb_t kb, char *ip, char *type)
Add star_scan and end_scan results to the main kb.
Definition hosts.c:64
static int hosts_num(void)
Returns the number of entries in the global hosts list.
Definition hosts.c:116
int host_is_currently_scanned(gvm_host_t *host_to_check)
Returns 1 if the host is being scanned. 0 otherwise.
Definition hosts.c:271
void hosts_stop_all(void)
Definition hosts.c:203
int hosts_set_pid(char *name, pid_t pid)
Definition hosts.c:177
int hosts_read(void)
Returns -1 if client asked to stop all tests or connection was lost or error. 0 otherwise.
Definition hosts.c:253
int hosts_init(int max_hosts)
Definition hosts.c:144
int hosts_new(char *name, kb_t kb, kb_t main_kb)
Definition hosts.c:151
hosts.c header.
kb_t main_kb
Definition kb_cache.c:15
static pid_t pid
const char * name
Definition nasl_init.c:440
uint8_t len
Header file for module network.
int kb_item_push_str_with_main_kb_check(kb_t kb, const char *name, const char *value)
Check if the current kb corresponds to the original scanid, if it matches it kb_item_push_str....
Definition plugutils.c:533
Header file for module plugutils.
Host information, implemented as doubly linked list.
Definition hosts.c:37
struct host * prev
Definition hosts.c:44
kb_t host_kb
Definition hosts.c:41
pid_t pid
Definition hosts.c:40
char * ip
Definition hosts.c:39
kb_t results_kb
Definition hosts.c:42
struct host * next
Definition hosts.c:43
char * name
Definition hosts.c:38
utils.c headerfile.