OpenVAS Scanner  7.0.0~git
nasl_host.c
Go to the documentation of this file.
1 /* Based on work Copyright (C) 2002 - 2004 Tenable Network Security
2  *
3  * SPDX-License-Identifier: GPL-2.0-only
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * version 2 as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 
29 #include "nasl_host.h"
30 
31 #include "../misc/network.h"
32 #include "../misc/pcap_openvas.h" /* for v6_is_local_ip */
33 #include "../misc/plugutils.h" /* for plug_get_host_fqdn */
34 #include "nasl_debug.h"
35 #include "nasl_func.h"
36 #include "nasl_global_ctxt.h"
37 #include "nasl_lex_ctxt.h"
38 #include "nasl_tree.h"
39 #include "nasl_var.h"
40 
41 #include <arpa/inet.h> /* for inet_aton */
42 #include <gvm/base/networking.h>
43 #include <gvm/util/kb.h>
44 #include <netdb.h> /* for gethostbyaddr */
45 #include <netinet/in.h> /* for in_addr */
46 #include <string.h> /* for strlen */
47 #include <unistd.h> /* for gethostname */
48 
49 tree_cell *
51 {
52  struct script_infos *script_infos = lexic->script_infos;
53  tree_cell *retc;
54  int i = 0;
55  nasl_array *arr;
56  GSList *tmp, *hostnames;
57 
58  hostnames = tmp = plug_get_host_fqdn_list (script_infos);
59  if (!hostnames)
60  return NULL;
61 
62  retc = alloc_typed_cell (DYN_ARRAY);
63  retc->x.ref_val = arr = g_malloc0 (sizeof (nasl_array));
64  while (tmp)
65  {
66  anon_nasl_var v;
67 
68  v.var_type = VAR2_DATA;
69  v.v.v_str.s_siz = strlen (tmp->data);
70  v.v.v_str.s_val = tmp->data;
71  add_var_to_list (arr, i++, &v);
72  tmp = tmp->next;
73  }
74 
75  g_slist_free_full (hostnames, g_free);
76  return retc;
77 }
78 
79 tree_cell *
81 {
82  struct script_infos *script_infos = lexic->script_infos;
84  tree_cell *retc;
85 
86  if (hostname == NULL)
87  return NULL;
88 
89  retc = alloc_typed_cell (CONST_STR);
90  retc->size = strlen (hostname);
91  retc->x.str_val = hostname;
92  return retc;
93 }
94 
95 tree_cell *
97 {
98  struct script_infos *script_infos = lexic->script_infos;
99  char *source;
100  tree_cell *retc;
101 
103  get_str_var_by_name (lexic, "hostname"));
104  if (!source)
105  return NULL;
106 
107  retc = alloc_typed_cell (CONST_STR);
108  retc->size = strlen (source);
109  retc->x.str_val = source;
110  return retc;
111 }
112 
113 tree_cell *
115 {
116  pid_t host_pid;
117  char buffer[4096], *lower;
118  char *value = get_str_var_by_name (lexic, "hostname");
119  char *source = get_str_var_by_name (lexic, "source");
120 
121  if (!value)
122  {
123  nasl_perror (lexic, "%s: Empty hostname\n", __FUNCTION__);
124  return NULL;
125  }
126  if (!source || !*source)
127  source = "NASL";
128 
129  /* Add to current process' vhosts list. */
130  lower = g_ascii_strdown (value, -1);
131  if (plug_add_host_fqdn (lexic->script_infos, lower, source))
132  goto end_add_hostname;
133 
134  /* Push to KB. Signal host process to fetch it. */
135  kb_item_push_str (lexic->script_infos->key, "internal/vhosts", lower);
136  snprintf (buffer, sizeof (buffer), "internal/source/%s", lower);
137  kb_item_push_str (lexic->script_infos->key, buffer, source);
138  host_pid = kb_item_get_int (lexic->script_infos->key, "internal/hostpid");
139  if (host_pid > 0)
140  kill (host_pid, SIGUSR2);
141 
142 end_add_hostname:
143  g_free (lower);
144  return NULL;
145 }
146 
147 tree_cell *
149 {
150  struct in6_addr in6addr;
151  char *value = get_str_var_by_name (lexic, "hostname");
152 
153  if (!value)
154  {
155  nasl_perror (lexic, "%s: Empty hostname\n", __FUNCTION__);
156  return NULL;
157  }
158 
159  if (!gvm_resolve_as_addr6 (value, &in6addr))
160  {
162  retc->x.str_val = addr6_as_str (&in6addr);
163  retc->size = strlen (retc->x.str_val);
164  return retc;
165  }
166  return NULL;
167 }
168 
169 tree_cell *
171 {
172  struct script_infos *script_infos = lexic->script_infos;
173  struct in6_addr *ip = plug_get_host_ip (script_infos);
174  tree_cell *retc;
175 
176  if (ip == NULL) /* WTF ? */
177  {
178  return FAKE_CELL;
179  }
180 
181  retc = alloc_typed_cell (CONST_STR);
182  retc->x.str_val = addr6_as_str (ip);
183  retc->size = strlen (retc->x.str_val);
184 
185  return retc;
186 }
187 
188 tree_cell *
190 {
191  struct script_infos *script_infos = lexic->script_infos;
192  unsigned int port = plug_get_host_open_port (script_infos);
193  tree_cell *retc;
194 
195  retc = alloc_typed_cell (CONST_INT);
196  retc->x.i_val = port;
197 
198  return retc;
199 }
200 
201 tree_cell *
203 {
204  int open;
205  struct script_infos *script_infos = lexic->script_infos;
206  tree_cell *retc;
207  int port;
208 
209  port = get_int_var_by_num (lexic, 0, -1);
210  if (port < 0)
211  return FAKE_CELL;
212 
213  retc = alloc_typed_cell (CONST_INT);
214  open = host_get_port_state (script_infos, port);
215  retc->x.i_val = open;
216  return retc;
217 }
218 
219 tree_cell *
221 {
222  int open;
223  struct script_infos *script_infos = lexic->script_infos;
224  tree_cell *retc;
225  int port;
226 
227  port = get_int_var_by_num (lexic, 0, -1);
228  if (port < 0)
229  return FAKE_CELL;
230 
231  retc = alloc_typed_cell (CONST_INT);
232  open = host_get_port_state_udp (script_infos, port);
233  retc->x.i_val = open;
234  return retc;
235 }
236 
237 tree_cell *
239 {
240  struct script_infos *script_infos = lexic->script_infos;
241  struct in6_addr *dst = plug_get_host_ip (script_infos);
242  tree_cell *retc;
243 
244  retc = alloc_typed_cell (CONST_INT);
245  retc->x.i_val = v6_islocalhost (dst);
246  return retc;
247 }
248 
249 tree_cell *
251 {
252  struct script_infos *script_infos = lexic->script_infos;
253  struct in6_addr *ip = plug_get_host_ip (script_infos);
254  tree_cell *retc;
255 
256  retc = alloc_typed_cell (CONST_INT);
257  retc->x.i_val = v6_is_local_ip (ip);
258  return retc;
259 }
260 
261 tree_cell *
263 {
264  struct script_infos *script_infos = lexic->script_infos;
265  tree_cell *retc;
266  char hostname[255];
267  struct in6_addr *ia = plug_get_host_ip (script_infos);
268  struct in6_addr in6addr;
269  struct in6_addr src6;
270 
271  retc = alloc_typed_cell (CONST_DATA);
272 
273  if (gvm_source_iface_is_set ())
274  {
275  struct in6_addr addr;
276 
277  /* Use source_iface's IP address when available. */
278  if (IN6_IS_ADDR_V4MAPPED (ia))
279  gvm_source_addr_as_addr6 (&addr);
280  else
281  gvm_source_addr6 (&addr);
282  retc->x.str_val = addr6_as_str (&addr);
283  retc->size = strlen (retc->x.str_val);
284  return retc;
285  }
286  else
287  {
288  /* Manually find the source IP that will be used. */
289  int err = 1;
290  if (v6_islocalhost (ia))
291  memcpy (&src6, ia, sizeof (struct in6_addr));
292  else
293  err = v6_getsourceip (&src6, ia);
294 
295  if (err && !IN6_ARE_ADDR_EQUAL (&src6, &in6addr_any))
296  {
297  retc->x.str_val = addr6_as_str (&src6);
298  retc->size = strlen (retc->x.str_val);
299 
300  return retc;
301  }
302 
303  hostname[sizeof (hostname) - 1] = '\0';
304  gethostname (hostname, sizeof (hostname) - 1);
305  if (gvm_resolve_as_addr6 (hostname, &in6addr))
306  {
307  retc->x.str_val = addr6_as_str (&in6addr);
308  retc->size = strlen (retc->x.str_val);
309  }
310  }
311  return retc;
312 }
313 
314 tree_cell *
316 {
317  char *hostname;
318  tree_cell *retc;
319 
320  (void) lexic;
321  retc = alloc_typed_cell (CONST_DATA);
322 
323  hostname = g_malloc0 (256);
324  gethostname (hostname, 255);
325 
326  retc->x.str_val = hostname;
327  retc->size = strlen (hostname);
328  return retc;
329 }
330 
363 tree_cell *
365 {
366  struct script_infos *script_infos = lexic->script_infos;
367  tree_cell *retc;
368  int port = get_int_var_by_num (lexic, 0, -1);
369 
370  if (port >= 0)
371  {
372  int trp = plug_get_port_transport (script_infos, port);
373 
374  retc = alloc_typed_cell (CONST_STR);
375  if (get_int_var_by_name (lexic, "asstring", 0))
376  {
377  const char *s = get_encaps_name (trp);
378  retc->x.str_val = g_strdup (s);
379  retc->size = strlen (s);
380  }
381  else
382  {
383  retc->type = CONST_INT;
384  retc->x.i_val = trp;
385  }
386  return retc;
387  }
388  return NULL;
389 }
390 
391 tree_cell *
393 {
394  tree_cell *retc;
395  struct hostent *h;
396  char *hn[2], **names[2];
397  struct in_addr ia, *a[2];
398  int i, j, n[2], names_nb[2], flag;
399  int cmp_hostname = get_int_var_by_name (lexic, "cmp_hostname", 0);
400 
401  memset (names_nb, '\0', sizeof (names_nb));
402  memset (names, '\0', sizeof (names));
403  memset (a, '\0', sizeof (a));
404  for (i = 0; i < 2; i++)
405  {
406  hn[i] = get_str_var_by_num (lexic, i);
407  if (hn[i] == NULL)
408  {
409  nasl_perror (lexic, "same_host needs two parameters!\n");
410  return NULL;
411  }
412  if (strlen (hn[i]) >= 256)
413  {
414  nasl_perror (lexic, "same_host(): Too long hostname !\n");
415  return NULL;
416  }
417  }
418  for (i = 0; i < 2; i++)
419  {
420  if (!inet_aton (hn[i], &ia)) /* Not an IP address */
421  {
422  h = gethostbyname (hn[i]);
423  if (h == NULL)
424  {
425  nasl_perror (lexic, "same_host: %s does not resolve\n", hn[i]);
426  n[i] = 0;
427  if (cmp_hostname)
428  {
429  names_nb[i] = 1;
430  names[i] = g_malloc0 (sizeof (char *));
431  names[i][0] = g_strdup (hn[i]);
432  }
433  }
434  else
435  {
436  for (names_nb[i] = 0; h->h_aliases[names_nb[i]] != NULL;
437  names_nb[i]++)
438  ;
439  names_nb[i]++;
440  names[i] = g_malloc0 (sizeof (char *) * names_nb[i]);
441  names[i][0] = g_strdup (h->h_name);
442  for (j = 1; j < names_nb[i]; j++)
443  names[i][j] = g_strdup (h->h_aliases[j - 1]);
444 
445  /* Here, we should check that h_addrtype == AF_INET */
446  for (n[i] = 0; ((struct in_addr **) h->h_addr_list)[n[i]] != NULL;
447  n[i]++)
448  ;
449  a[i] = g_malloc0 (h->h_length * n[i]);
450  for (j = 0; j < n[i]; j++)
451  a[i][j] = *((struct in_addr **) h->h_addr_list)[j];
452  }
453  }
454  else
455  {
456  if (cmp_hostname)
457  h = gethostbyaddr ((const char *) &ia, sizeof (ia), AF_INET);
458  else
459  h = NULL;
460  if (h == NULL)
461  {
462  a[i] = g_malloc0 (sizeof (struct in_addr));
463  memcpy (a[i], &ia, sizeof (struct in_addr));
464  n[i] = 1;
465  }
466  else
467  {
468  for (names_nb[i] = 0; h->h_aliases[names_nb[i]] != NULL;
469  names_nb[i]++)
470  ;
471  names_nb[i]++;
472  names[i] = g_malloc0 (sizeof (char *) * names_nb[i]);
473  names[i][0] = g_strdup (h->h_name);
474  for (j = 1; j < names_nb[i]; j++)
475  names[i][j] = g_strdup (h->h_aliases[j - 1]);
476 
477  /* Here, we should check that h_addrtype == AF_INET */
478  for (n[i] = 0; ((struct in_addr **) h->h_addr_list)[n[i]] != NULL;
479  n[i]++)
480  ;
481  a[i] = g_malloc0 (h->h_length * n[i]);
482  for (j = 0; j < n[i]; j++)
483  a[i][j] = *((struct in_addr **) h->h_addr_list)[j];
484  }
485  }
486  }
487  flag = 0;
488  for (i = 0; i < n[0] && !flag; i++)
489  for (j = 0; j < n[1] && !flag; j++)
490  if (a[0][i].s_addr == a[1][j].s_addr)
491  {
492  flag = 1;
493  }
494 
495  if (cmp_hostname)
496  for (i = 0; i < names_nb[0] && !flag; i++)
497  for (j = 0; j < names_nb[1] && !flag; j++)
498  if (strcmp (names[0][i], names[1][j]) == 0)
499  {
500  flag = 1;
501  }
502 
503  retc = alloc_typed_cell (CONST_INT);
504  retc->x.i_val = flag;
505 
506  for (i = 0; i < 2; i++)
507  g_free (a[i]);
508  if (cmp_hostname)
509  {
510  for (i = 0; i < 2; i++)
511  {
512  for (j = 0; j < names_nb[i]; j++)
513  g_free (names[i][j]);
514  g_free (names[i]);
515  }
516  }
517  return retc;
518 }
519 
520 tree_cell *
522 {
523  tree_cell *retc;
524  struct script_infos *script_infos = lexic->script_infos;
525  struct in6_addr *addr;
526 
528  retc = alloc_typed_cell (CONST_INT);
529 
530  if (addr == NULL)
531  {
532  nasl_perror (lexic, "address is NULL!\n");
533  return NULL;
534  }
535  if (IN6_IS_ADDR_V4MAPPED (addr) == 1)
536  retc->x.i_val = 0;
537  else
538  retc->x.i_val = 1;
539 
540  return retc;
541 }
st_a_nasl_var
Definition: nasl_var.h:50
script_infos
Definition: scanneraux.h:43
plug_get_host_source
char * plug_get_host_source(struct script_infos *args, const char *hostname)
Definition: plugutils.c:257
resolve_hostname
tree_cell * resolve_hostname(lex_ctxt *lexic)
Definition: nasl_host.c:148
get_hostname
tree_cell * get_hostname(lex_ctxt *lexic)
Definition: nasl_host.c:80
host_get_port_state
int host_get_port_state(struct script_infos *plugdata, int portnum)
Definition: plugutils.c:154
plug_get_port_transport
int plug_get_port_transport(struct script_infos *args, int port)
Definition: plugutils.c:854
CONST_DATA
@ CONST_DATA
Definition: nasl_tree.h:93
plug_get_host_ip
struct in6_addr * plug_get_host_ip(struct script_infos *args)
Definition: plugutils.c:285
TC::str_val
char * str_val
Definition: nasl_tree.h:112
script_infos::key
kb_t key
Definition: scanneraux.h:46
plug_get_host_fqdn
char * plug_get_host_fqdn(struct script_infos *args)
Definition: plugutils.c:211
CONST_STR
@ CONST_STR
Definition: nasl_tree.h:91
nasl_target_is_ipv6
tree_cell * nasl_target_is_ipv6(lex_ctxt *lexic)
Definition: nasl_host.c:521
DYN_ARRAY
@ DYN_ARRAY
Definition: nasl_tree.h:101
add_hostname
tree_cell * add_hostname(lex_ctxt *lexic)
Definition: nasl_host.c:114
get_encaps_name
const char * get_encaps_name(openvas_encaps_t code)
Definition: network.c:1546
FAKE_CELL
#define FAKE_CELL
Definition: nasl_tree.h:119
TC::x
union TC::@2 x
nasl_this_host_name
tree_cell * nasl_this_host_name(lex_ctxt *lexic)
Definition: nasl_host.c:315
get_str_var_by_name
char * get_str_var_by_name(lex_ctxt *, const char *)
Definition: nasl_var.c:1127
st_a_nasl_var::v_str
nasl_string_t v_str
Definition: nasl_var.h:58
st_nasl_string::s_siz
int s_siz
Definition: nasl_var.h:38
get_hostnames
tree_cell * get_hostnames(lex_ctxt *lexic)
Definition: nasl_host.c:50
v6_is_local_ip
int v6_is_local_ip(struct in6_addr *addr)
Definition: pcap.c:117
get_host_ip
tree_cell * get_host_ip(lex_ctxt *lexic)
Definition: nasl_host.c:170
st_nasl_array
Definition: nasl_var.h:43
plug_add_host_fqdn
int plug_add_host_fqdn(struct script_infos *args, const char *hostname, const char *source)
Definition: plugutils.c:166
plug_get_host_open_port
unsigned int plug_get_host_open_port(struct script_infos *desc)
Definition: plugutils.c:786
nasl_debug.h
get_hostname_source
tree_cell * get_hostname_source(lex_ctxt *lexic)
Definition: nasl_host.c:96
get_port_transport
tree_cell * get_port_transport(lex_ctxt *lexic)
Return the encapsulation mode of a port.
Definition: nasl_host.c:364
nasl_perror
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition: nasl_debug.c:120
v6_getsourceip
int v6_getsourceip(struct in6_addr *src, struct in6_addr *dst)
Definition: pcap.c:478
TC::size
int size
Definition: nasl_tree.h:109
host_get_port_state_udp
int host_get_port_state_udp(struct script_infos *plugdata, int portnum)
Definition: plugutils.c:160
get_udp_port_state
tree_cell * get_udp_port_state(lex_ctxt *lexic)
Definition: nasl_host.c:220
nasl_host.h
nasl_lex_ctxt.h
nasl_this_host
tree_cell * nasl_this_host(lex_ctxt *lexic)
Definition: nasl_host.c:262
get_int_var_by_name
long int get_int_var_by_name(lex_ctxt *, const char *, int)
Definition: nasl_var.c:1113
VAR2_DATA
@ VAR2_DATA
Definition: nasl_var.h:29
nasl_func.h
TC::ref_val
void * ref_val
Definition: nasl_tree.h:114
get_int_var_by_num
long int get_int_var_by_num(lex_ctxt *, int, int)
Definition: nasl_var.c:1106
get_str_var_by_num
char * get_str_var_by_num(lex_ctxt *, int)
Definition: nasl_var.c:1120
st_a_nasl_var::var_type
int var_type
Definition: nasl_var.h:52
struct_lex_ctxt::script_infos
struct script_infos * script_infos
Definition: nasl_lex_ctxt.h:41
TC
Definition: nasl_tree.h:104
struct_lex_ctxt
Definition: nasl_lex_ctxt.h:33
TC::type
short type
Definition: nasl_tree.h:106
nasl_islocalnet
tree_cell * nasl_islocalnet(lex_ctxt *lexic)
Definition: nasl_host.c:250
nasl_var.h
nasl_same_host
tree_cell * nasl_same_host(lex_ctxt *lexic)
Definition: nasl_host.c:392
get_host_open_port
tree_cell * get_host_open_port(lex_ctxt *lexic)
Definition: nasl_host.c:189
nasl_global_ctxt.h
CONST_INT
@ CONST_INT
Definition: nasl_tree.h:90
hostname
const char * hostname
Definition: pluginlaunch.c:76
v6_islocalhost
int v6_islocalhost(struct in6_addr *addr)
Tests whether a packet sent to IP is LIKELY to route through the kernel localhost interface.
Definition: pcap.c:231
add_var_to_list
int add_var_to_list(nasl_array *a, int i, const anon_nasl_var *v)
Definition: nasl_var.c:1254
st_a_nasl_var::v
union st_a_nasl_var::@4 v
nasl_islocalhost
tree_cell * nasl_islocalhost(lex_ctxt *lexic)
Definition: nasl_host.c:238
get_port_state
tree_cell * get_port_state(lex_ctxt *lexic)
Definition: nasl_host.c:202
alloc_typed_cell
tree_cell * alloc_typed_cell(int typ)
Definition: nasl_tree.c:40
plug_get_host_fqdn_list
GSList * plug_get_host_fqdn_list(struct script_infos *args)
Definition: plugutils.c:239
nasl_tree.h
st_nasl_string::s_val
unsigned char * s_val
Definition: nasl_var.h:37
TC::i_val
long int i_val
Definition: nasl_tree.h:113