OpenVAS Libraries  9.0.3
nasl_host.c
Go to the documentation of this file.
1 /* Nessus Attack Scripting Language
2  *
3  * Copyright (C) 2002 - 2004 Tenable Network Security
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2,
7  * 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 Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  *
18  */
19 
30 #include <arpa/inet.h> /* for inet_aton */
31 #include <netdb.h> /* for gethostbyaddr */
32 #include <netinet/in.h> /* for in_addr */
33 #include <string.h> /* for strlen */
34 #include <unistd.h> /* for gethostname */
35 
36 #include "../misc/network.h"
37 #include "../base/openvas_networking.h"
38 #include "../misc/plugutils.h" /* for plug_get_host_fqdn */
39 #include "../misc/pcap_openvas.h" /* for v6_is_local_ip */
40 
41 #include "nasl_tree.h"
42 #include "nasl_global_ctxt.h"
43 #include "nasl_func.h"
44 #include "nasl_var.h"
45 #include "nasl_lex_ctxt.h"
46 #include "nasl_debug.h"
47 
48 #include "nasl_host.h"
49 
50 tree_cell *
52 {
53  struct arglist *script_infos = lexic->script_infos;
54  char *hostname = plug_get_host_fqdn (script_infos);
55  tree_cell *retc;
56 
57  if (hostname == NULL)
58  return NULL;
59 
60  retc = alloc_tree_cell (0, NULL);
61  retc->type = CONST_STR;
62  retc->size = strlen (hostname);
63  retc->x.str_val = hostname;
64  return retc;
65 }
66 
67 
68 tree_cell *
70 {
71  struct arglist *script_infos = lexic->script_infos;
72  struct in6_addr *ip = plug_get_host_ip (script_infos);
73  tree_cell *retc;
74 
75  if (ip == NULL) /* WTF ? */
76  {
77  return FAKE_CELL;
78  }
79 
80  retc = alloc_tree_cell (0, NULL);
81  retc->type = CONST_STR;
82  retc->x.str_val = addr6_as_str (ip);
83  retc->size = strlen (retc->x.str_val);
84 
85  return retc;
86 }
87 
88 
89 tree_cell *
91 {
92  struct arglist *script_infos = lexic->script_infos;
93  unsigned int port = plug_get_host_open_port (script_infos);
94  tree_cell *retc;
95 
96  retc = alloc_tree_cell (0, NULL);
97  retc->type = CONST_INT;
98  retc->x.i_val = port;
99 
100  return retc;
101 }
102 
103 
104 tree_cell *
106 {
107  int open;
108  struct arglist *script_infos = lexic->script_infos;
109  tree_cell *retc;
110  int port;
111 
112  port = get_int_var_by_num (lexic, 0, -1);
113  if (port < 0)
114  return FAKE_CELL;
115 
116  retc = alloc_tree_cell (0, NULL);
117  retc->type = CONST_INT;
118  open = host_get_port_state (script_infos, port);
119  retc->x.i_val = open;
120  return retc;
121 }
122 
123 tree_cell *
125 {
126  int open;
127  struct arglist *script_infos = lexic->script_infos;
128  tree_cell *retc;
129  int port;
130 
131  port = get_int_var_by_num (lexic, 0, -1);
132  if (port < 0)
133  return FAKE_CELL;
134 
135  retc = alloc_tree_cell (0, NULL);
136  retc->type = CONST_INT;
137  open = host_get_port_state_udp (script_infos, port);
138  retc->x.i_val = open;
139  return retc;
140 }
141 
142 
143 tree_cell *
145 {
146  struct arglist *script_infos = lexic->script_infos;
147  struct in6_addr *dst = plug_get_host_ip (script_infos);
148  tree_cell *retc;
149 
150  retc = alloc_tree_cell (0, NULL);
151  retc->type = CONST_INT;
152  retc->x.i_val = v6_islocalhost (dst);
153  return retc;
154 }
155 
156 
157 tree_cell *
159 {
160  struct arglist *script_infos = lexic->script_infos;
161  struct in6_addr *ip = plug_get_host_ip (script_infos);
162  tree_cell *retc;
163 
164  retc = alloc_tree_cell (0, NULL);
165  retc->type = CONST_INT;
166  retc->x.i_val = v6_is_local_ip (ip);
167  return retc;
168 }
169 
170 
171 tree_cell *
173 {
174  struct arglist *script_infos = lexic->script_infos;
175  tree_cell *retc;
176  char hostname[255];
177  struct in6_addr *ia = plug_get_host_ip (script_infos);
178  struct in6_addr in6addr;
179  struct in6_addr src6;
180 
181  retc = alloc_tree_cell (0, NULL);
182  retc->type = CONST_DATA;
183 
185  {
186  struct in6_addr addr;
187 
188  /* Use source_iface's IP address when available. */
189  if (IN6_IS_ADDR_V4MAPPED (ia))
191  else
192  openvas_source_addr6 (&addr);
193  retc->x.str_val = addr6_as_str (&addr);
194  retc->size = strlen (retc->x.str_val);
195  return retc;
196  }
197  else
198  {
199  /* Manually find the source IP that will be used. */
200  int err = 1;
201  if (v6_islocalhost (ia))
202  memcpy (&src6, ia, sizeof (struct in6_addr));
203  else
204  err = v6_getsourceip (&src6, ia);
205 
206  if (err && !IN6_ARE_ADDR_EQUAL (&src6, &in6addr_any))
207  {
208  retc->x.str_val = addr6_as_str (&src6);
209  retc->size = strlen (retc->x.str_val);
210 
211  return retc;
212  }
213 
214  hostname[sizeof (hostname) - 1] = '\0';
215  gethostname (hostname, sizeof (hostname) - 1);
216  if (openvas_resolve_as_addr6 (hostname, &in6addr))
217  {
218  retc->x.str_val = addr6_as_str (&in6addr);
219  retc->size = strlen (retc->x.str_val);
220  }
221  }
222  return retc;
223 }
224 
225 
226 tree_cell *
228 {
229  char *hostname;
230  tree_cell *retc;
231 
232  retc = alloc_tree_cell (0, NULL);
233  retc->type = CONST_DATA;
234 
235  hostname = g_malloc0 (256);
236  gethostname (hostname, 255);
237 
238  retc->x.str_val = hostname;
239  retc->size = strlen (hostname);
240  return retc;
241 }
242 
243 
276 tree_cell *
278 {
279  struct arglist *script_infos = lexic->script_infos;
280  tree_cell *retc;
281  int port = get_int_var_by_num (lexic, 0, -1);
282 
283  if (port >= 0)
284  {
285  int trp = plug_get_port_transport (script_infos, port);
286 
287  retc = alloc_tree_cell (0, NULL);
288  if (get_int_local_var_by_name (lexic, "asstring", 0))
289  {
290  const char *s = get_encaps_name (trp);
291  retc->type = CONST_STR;
292  retc->x.str_val = g_strdup (s);
293  retc->size = strlen (s);
294  }
295  else
296  {
297  retc->type = CONST_INT;
298  retc->x.i_val = trp;
299  }
300  return retc;
301  }
302  return NULL;
303 }
304 
305 
306 tree_cell *
308 {
309  tree_cell *retc;
310  struct hostent *h;
311  char *hn[2], **names[2];
312  struct in_addr ia, *a[2];
313  int i, j, n[2], names_nb[2], flag;
314  int cmp_hostname = get_int_local_var_by_name (lexic, "cmp_hostname", 0);
315 
316  for (i = 0; i < 2; i++)
317  {
318  hn[i] = get_str_var_by_num (lexic, i);
319  if (hn[i] == NULL)
320  {
321  nasl_perror (lexic, "same_host needs two parameters!\n");
322  return NULL;
323  }
324  if (strlen (hn[i]) >= 256)
325  {
326  nasl_perror (lexic, "same_host(): Too long hostname !\n");
327  return NULL;
328  }
329  }
330  for (i = 0; i < 2; i++)
331  {
332  if (!inet_aton (hn[i], &ia)) /* Not an IP address */
333  {
334  h = gethostbyname (hn[i]);
335  if (h == NULL)
336  {
337  nasl_perror (lexic, "same_host: %s does not resolve\n", hn[i]);
338  n[i] = 0;
339  if (cmp_hostname)
340  {
341  names_nb[i] = 1;
342  names[i] = g_malloc0 (sizeof (char *));
343  names[i][0] = g_strdup (hn[i]);
344  }
345  }
346  else
347  {
348  for (names_nb[i] = 0; h->h_aliases[names_nb[i]] != NULL;
349  names_nb[i]++)
350  ;
351  names_nb[i]++;
352  names[i] = g_malloc0 (sizeof (char *) * names_nb[i]);
353  names[i][0] = g_strdup (h->h_name);
354  for (j = 1; j < names_nb[i]; j++)
355  names[i][j] = g_strdup (h->h_aliases[j - 1]);
356 
357  /* Here, we should check that h_addrtype == AF_INET */
358  for (n[i] = 0; ((struct in_addr **) h->h_addr_list)[n[i]] != NULL;
359  n[i]++)
360  ;
361  a[i] = g_malloc0 (h->h_length * n[i]);
362  for (j = 0; j < n[i]; j++)
363  a[i][j] = *((struct in_addr **) h->h_addr_list)[j];
364  }
365  }
366  else
367  {
368  if (cmp_hostname)
369  h = gethostbyaddr ((const char *) &ia, sizeof (ia), AF_INET);
370  else
371  h = NULL;
372  if (h == NULL)
373  {
374  a[i] = g_malloc0 (sizeof (struct in_addr));
375  memcpy (a[i], &ia, sizeof (struct in_addr));
376  n[i] = 1;
377  }
378  else
379  {
380  for (names_nb[i] = 0; h->h_aliases[names_nb[i]] != NULL;
381  names_nb[i]++)
382  ;
383  names_nb[i]++;
384  names[i] = g_malloc0 (sizeof (char *) * names_nb[i]);
385  names[i][0] = g_strdup (h->h_name);
386  for (j = 1; j < names_nb[i]; j++)
387  names[i][j] = g_strdup (h->h_aliases[j - 1]);
388 
389  /* Here, we should check that h_addrtype == AF_INET */
390  for (n[i] = 0; ((struct in_addr **) h->h_addr_list)[n[i]] != NULL;
391  n[i]++)
392  ;
393  a[i] = g_malloc0 (h->h_length * n[i]);
394  for (j = 0; j < n[i]; j++)
395  a[i][j] = *((struct in_addr **) h->h_addr_list)[j];
396  }
397  }
398  }
399  flag = 0;
400  for (i = 0; i < n[0] && !flag; i++)
401  for (j = 0; j < n[1] && !flag; j++)
402  if (a[0][i].s_addr == a[1][j].s_addr)
403  {
404  flag = 1;
405  }
406 
407  if (cmp_hostname)
408  for (i = 0; i < names_nb[0] && !flag; i++)
409  for (j = 0; j < names_nb[1] && !flag; j++)
410  if (strcmp (names[0][i], names[1][j]) == 0)
411  {
412  flag = 1;
413  }
414 
415  retc = alloc_typed_cell (CONST_INT);
416  retc->x.i_val = flag;
417 
418  for (i = 0; i < 2; i++)
419  g_free (a[i]);
420  if (cmp_hostname)
421  {
422  for (i = 0; i < 2; i++)
423  {
424  for (j = 0; j < names_nb[i]; j++)
425  g_free (names[i][j]);
426  g_free (names[i]);
427  }
428  }
429  return retc;
430 }
431 
432 tree_cell *
434 {
435  tree_cell *retc;
436  struct arglist *script_infos = lexic->script_infos;
437  struct in6_addr *addr;
438 
439  addr = plug_get_host_ip (script_infos);
440  retc = alloc_tree_cell (0, NULL);
441  retc->type = CONST_INT;
442 
443  if (addr == NULL)
444  {
445  nasl_perror (lexic, "address is NULL!\n");
446  return NULL;
447  }
448  if (IN6_IS_ADDR_V4MAPPED (addr) == 1)
449  retc->x.i_val = 0;
450  else
451  retc->x.i_val = 1;
452 
453  return retc;
454 }
#define FAKE_CELL
Definition: nasl_tree.h:120
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:378
#define err(x)
void openvas_source_addr6(void *addr6)
Gives the source IPv6 address.
const char * get_encaps_name(openvas_encaps_t code)
Definition: network.c:1729
short type
Definition: nasl_tree.h:107
int host_get_port_state(struct arglist *plugdata, int portnum)
Definition: plugutils.c:177
char * str_val
Definition: nasl_tree.h:113
tree_cell * get_port_state(lex_ctxt *lexic)
Definition: nasl_host.c:105
int v6_getsourceip(struct in6_addr *src, struct in6_addr *dst)
Definition: pcap.c:692
tree_cell * nasl_target_is_ipv6(lex_ctxt *lexic)
Definition: nasl_host.c:433
tree_cell * nasl_islocalnet(lex_ctxt *lexic)
Definition: nasl_host.c:158
char * plug_get_host_fqdn(struct arglist *desc)
Definition: plugutils.c:200
long int get_int_local_var_by_name(lex_ctxt *, const char *, int)
Definition: nasl_var.c:1240
union TC::@7 x
tree_cell * alloc_typed_cell(int typ)
Definition: nasl_tree.c:53
tree_cell * get_hostname(lex_ctxt *lexic)
Definition: nasl_host.c:51
tree_cell * nasl_this_host(lex_ctxt *lexic)
Definition: nasl_host.c:172
tree_cell * nasl_islocalhost(lex_ctxt *lexic)
Definition: nasl_host.c:144
char * addr6_as_str(const struct in6_addr *addr6)
Definition: nasl_tree.h:105
struct in6_addr * plug_get_host_ip(struct arglist *desc)
Definition: plugutils.c:216
int v6_is_local_ip(struct in6_addr *addr)
Definition: pcap.c:106
long int get_int_var_by_num(lex_ctxt *, int, int)
Definition: nasl_var.c:1226
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition: nasl_debug.c:94
char * get_str_var_by_num(lex_ctxt *, int)
Definition: nasl_var.c:1248
tree_cell * get_host_ip(lex_ctxt *lexic)
Definition: nasl_host.c:69
tree_cell * nasl_same_host(lex_ctxt *lexic)
Definition: nasl_host.c:307
long int i_val
Definition: nasl_tree.h:114
tree_cell * nasl_this_host_name(lex_ctxt *lexic)
Definition: nasl_host.c:227
tree_cell * get_host_open_port(lex_ctxt *lexic)
Definition: nasl_host.c:90
tree_cell * alloc_tree_cell(int lnb, char *s)
Definition: nasl_tree.c:37
int host_get_port_state_udp(struct arglist *plugdata, int portnum)
Definition: plugutils.c:183
int openvas_resolve_as_addr6(const char *name, struct in6_addr *ip6)
Resolves a hostname to an IPv4-mapped IPv6 or IPv6 address.
void openvas_source_addr_as_addr6(struct in6_addr *addr6)
Gives the source IPv4 mapped as an IPv6 address. eg. 192.168.20.10 would map to ::ffff:192....
unsigned int plug_get_host_open_port(struct arglist *desc)
Definition: plugutils.c:917
int openvas_source_iface_is_set(void)
struct arglist * script_infos
Definition: nasl_lex_ctxt.h:39
int plug_get_port_transport(struct arglist *args, int port)
Definition: plugutils.c:990
tree_cell * get_udp_port_state(lex_ctxt *lexic)
Definition: nasl_host.c:124
tree_cell * get_port_transport(lex_ctxt *lexic)
Return the encapsulation mode of a port.
Definition: nasl_host.c:277
int size
Definition: nasl_tree.h:110