OpenVAS Scanner  7.0.1~git
hosts.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 "hosts.h" /* for hosts_new() */
27 
28 #include "../misc/network.h" /* for internal_recv */
29 #include "utils.h" /* for data_left() */
30 
31 #include <errno.h> /* for errno() */
32 #include <glib.h> /* for g_free() */
33 #include <stdio.h> /* for snprintf() */
34 #include <string.h> /* for strlen() */
35 #include <sys/wait.h> /* for waitpid() */
36 #include <unistd.h> /* for close() */
37 
38 #undef G_LOG_DOMAIN
39 
42 #define G_LOG_DOMAIN "sd main"
43 
47 struct host
48 {
49  char *name;
50  char *ip;
51  pid_t pid;
52  kb_t host_kb;
53  struct host *next;
54  struct host *prev;
55 };
59 static struct host *hosts = NULL;
60 static int g_max_hosts = 15;
61 
62 /*-------------------------------------------------------------------*/
63 extern int global_scan_stop;
64 
65 static void
66 host_set_time (kb_t kb, char *key)
67 {
68  char timestr[1024];
69  char *tmp;
70  time_t t;
71  int len;
72 
73  t = time (NULL);
74  tmp = ctime (&t);
75  timestr[sizeof (timestr) - 1] = '\0';
76  strncpy (timestr, tmp, sizeof (timestr) - 1);
77  len = strlen (timestr);
78  if (timestr[len - 1] == '\n')
79  timestr[len - 1] = '\0';
80 
81  kb_item_push_str (kb, key, timestr);
82 }
83 
84 static void
85 host_rm (struct host *h)
86 {
87  if (h->pid != 0)
88  waitpid (h->pid, NULL, WNOHANG);
89 
90  if (!global_scan_stop)
91  {
92  char key[1024];
93  char *scan_id = kb_item_get_str (h->host_kb, "internal/scan_id");
94  snprintf (key, sizeof (key), "internal/%s", scan_id);
95  kb_item_set_str (h->host_kb, key, "finished", 0);
96 
97  host_set_time (h->host_kb, "internal/end_time");
98  kb_lnk_reset (h->host_kb);
99  g_free (scan_id);
100  }
101 
102  if (h->next != NULL)
103  h->next->prev = h->prev;
104 
105  if (h->prev != NULL)
106  h->prev->next = h->next;
107 
108  if (global_scan_stop == 1 && h->host_kb)
109  {
110  kb_delete (h->host_kb);
111  h->host_kb = NULL;
112  }
113 
114  g_free (h->name);
115  g_free (h->ip);
116  g_free (h);
117 }
118 
119 /*-----------------------------------------------------------------*/
120 
124 static int
125 hosts_num (void)
126 {
127  struct host *h = hosts;
128  int num;
129 
130  for (num = 0; h != NULL; num++, h = h->next)
131  ;
132 
133  return num;
134 }
135 
139 static struct host *
141 {
142  struct host *h = hosts;
143  while (h != NULL)
144  {
145  if (strcmp (h->name, name) == 0)
146  return h;
147  h = h->next;
148  }
149  return NULL;
150 }
151 
152 int
153 hosts_init (int max_hosts)
154 {
155  g_max_hosts = max_hosts;
156  return 0;
157 }
158 
159 int
160 hosts_new (char *name, kb_t kb)
161 {
162  struct host *h;
163 
164  while (hosts_num () >= g_max_hosts)
165  {
166  if (hosts_read () < 0)
167  return -1;
168  }
169  if (global_scan_stop)
170  return 0;
171 
172  h = g_malloc0 (sizeof (struct host));
173  h->name = g_strdup (name);
174  h->pid = 0;
175  h->host_kb = kb;
176  if (hosts != NULL)
177  hosts->prev = h;
178  h->next = hosts;
179  h->prev = NULL;
180  hosts = h;
181  return 0;
182 }
183 
184 int
185 hosts_set_pid (char *name, pid_t pid)
186 {
187  struct host *h = hosts_get (name);
188  if (h == NULL)
189  {
190  g_debug ("host_set_pid() failed!\n");
191  return -1;
192  }
193 
194  h->pid = pid;
195  return 0;
196 }
197 
198 /*-----------------------------------------------------------------*/
199 static int
201 {
202  if (h == NULL)
203  return -1;
204 
205  g_message ("Stopping host %s scan", h->name);
206  kill (h->pid, SIGUSR1);
207  return 0;
208 }
209 
210 void
212 {
213  struct host *host = hosts;
214 
215  global_scan_stop = 1;
216  while (host)
217  {
219  host = host->next;
220  }
221 }
222 
223 /*-----------------------------------------------------------------*/
224 
225 static void
227 {
228  struct host *h = hosts;
229  int ret = 1;
230 
231  while (ret > 0)
232  {
233  ret = waitpid (-1, NULL, WNOHANG);
234  if (ret < 0)
235  g_debug ("waitpid() failed. %s)", strerror (errno));
236  }
237 
238  if (h == NULL)
239  return;
240 
241  while (h)
242  {
243  if (!h->ip)
244  {
245  /* Scan started. */
246  h->ip = kb_item_get_str (h->host_kb, "internal/ip");
247  if (h->ip)
248  host_set_time (h->host_kb, "internal/start_time");
249  }
250  if (h->ip)
251  {
252  if (kill (h->pid, 0) < 0) /* Process is dead */
253  {
254  if (!h->prev)
255  hosts = hosts->next;
256  host_rm (h);
257  h = hosts;
258  if (!h)
259  break;
260  }
261  }
262  h = h->next;
263  }
264 }
265 
270 int
272 {
273  if (hosts == NULL)
274  return -1;
275 
276  hosts_read_data ();
277  usleep (500000);
278 
279  return 0;
280 }
void hosts_stop_all(void)
Definition: hosts.c:211
int hosts_new(char *name, kb_t kb)
Definition: hosts.c:160
static int hosts_stop_host(struct host *h)
Definition: hosts.c:200
static struct host * hosts_get(char *name)
Retrieves a host specified by its name from the global host list.
Definition: hosts.c:140
static struct host * hosts
Definition: hosts.c:59
char * ip
Definition: hosts.c:50
static pid_t pid
utils.c headerfile.
static void hosts_read_data(void)
Definition: hosts.c:226
static int g_max_hosts
Definition: hosts.c:60
int hosts_read(void)
Returns -1 if client asked to stop all tests or connection was lost or error. 0 otherwise.
Definition: hosts.c:271
Host information, implemented as doubly linked list.
Definition: hosts.c:47
struct host * prev
Definition: hosts.c:54
const char * name
Definition: nasl_init.c:377
static int hosts_num(void)
Returns the number of entries in the global hosts list.
Definition: hosts.c:125
int hosts_init(int max_hosts)
Definition: hosts.c:153
int global_scan_stop
Definition: attack.c:224
int hosts_set_pid(char *name, pid_t pid)
Definition: hosts.c:185
char * name
Definition: hosts.c:49
static void host_rm(struct host *h)
Definition: hosts.c:85
kb_t host_kb
Definition: hosts.c:52
static void host_set_time(kb_t kb, char *key)
Definition: hosts.c:66
pid_t pid
Definition: hosts.c:51
hosts.c header.
struct host * next
Definition: hosts.c:53