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