OpenVAS Scanner 23.40.3
nasl_host.c
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Greenbone AG
2 * SPDX-FileCopyrightText: 2002-2004 Tenable Network Security
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 */
6
16
17#include "nasl_host.h"
18
19#include "../misc/ipc_openvas.h"
20#include "../misc/network.h"
21#include "../misc/pcap_openvas.h" /* for v6_is_local_ip */
22#include "../misc/plugutils.h" /* for plug_get_host_fqdn */
23#include "base/hosts.h"
24#include "nasl_debug.h"
25#include "nasl_func.h"
26#include "nasl_global_ctxt.h"
27#include "nasl_lex_ctxt.h"
28#include "nasl_tree.h"
29#include "nasl_var.h"
30
31#include <arpa/inet.h> /* for inet_aton */
32#include <gvm/base/networking.h>
33#include <gvm/util/kb.h>
34#include <limits.h>
35#include <net/if.h>
36#include <net/if_arp.h>
37#include <netdb.h> /* for gethostbyaddr */
38#include <netinet/in.h> /* for in_addr */
39#include <string.h> /* for strlen */
40#include <sys/ioctl.h>
41#include <unistd.h> /* for gethostname */
42
43#undef G_LOG_DOMAIN
47#define G_LOG_DOMAIN "sd nasl"
50{
51 struct script_infos *script_infos = lexic->script_infos;
52 tree_cell *retc;
53 int i = 0;
54 nasl_array *arr;
55 GSList *tmp, *hostnames;
56
57 hostnames = tmp = plug_get_host_fqdn_list (script_infos);
58 if (!hostnames)
59 return NULL;
60
62 retc->x.ref_val = arr = g_malloc0 (sizeof (nasl_array));
63 while (tmp)
64 {
66
68 v.v.v_str.s_siz = strlen (tmp->data);
69 v.v.v_str.s_val = tmp->data;
70 add_var_to_list (arr, i++, &v);
71 tmp = tmp->next;
72 }
73
74 g_slist_free_full (hostnames, g_free);
75 return retc;
76}
77
80{
81 struct script_infos *script_infos = lexic->script_infos;
83 tree_cell *retc;
84
85 if (hostname == NULL)
86 return NULL;
87
89 retc->size = strlen (hostname);
90 retc->x.str_val = hostname;
91 return retc;
92}
93
96{
97 struct script_infos *script_infos = lexic->script_infos;
98 char *source;
99 tree_cell *retc;
100
102 get_str_var_by_name (lexic, "hostname"));
103 if (!source)
104 return NULL;
105
107 retc->size = strlen (source);
108 retc->x.str_val = source;
109 return retc;
110}
111
112tree_cell *
114{
115 struct ipc_data *hn = NULL;
116 char *lower;
117 const char *json = NULL;
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", __func__);
124 return NULL;
125 }
126 if (!source || !*source)
127 source = "NASL";
128 /* Add to current process' vhosts list. */
129 lower = g_ascii_strdown (value, -1);
130 hn = ipc_data_type_from_hostname (source, strlen (source), lower,
131 strlen (lower));
132 json = ipc_data_to_json (hn);
133 ipc_data_destroy (&hn);
134 if (plug_add_host_fqdn (lexic->script_infos, lower, source))
135 goto end_add_hostname;
136
137 // send it to host process to extend vhosts list there
138 if (ipc_send (lexic->script_infos->ipc_context, IPC_MAIN, json, strlen (json))
139 < 0)
140 g_warning ("Unable to send %s to host process", lower);
141
142end_add_hostname:
143 g_free (lower);
144 g_free ((void *) json);
145 return NULL;
146}
147
152tree_cell *
154{
155 GSList *list = NULL;
156 char *value = get_str_var_by_name (lexic, "hostname");
157 tree_cell *retc;
158 nasl_array *arr;
159 int i = 0;
160
161 if (!value)
162 {
163 nasl_perror (lexic, "%s: Empty hostname\n", __func__);
164 return NULL;
165 }
166
167 list = gvm_resolve_list (value);
168
170 retc->x.ref_val = arr = g_malloc0 (sizeof (nasl_array));
171 while (list)
172 {
173 anon_nasl_var var_aux;
174
175 var_aux.var_type = VAR2_DATA;
176 var_aux.v.v_str.s_siz = strlen (addr6_as_str (list->data));
177 var_aux.v.v_str.s_val = (unsigned char *) addr6_as_str (list->data);
178 add_var_to_list (arr, i++, &var_aux);
179 list = list->next;
180 }
181 g_slist_free_full (list, g_free);
182 return retc;
183}
184
185tree_cell *
187{
188 struct in6_addr in6addr;
189 char *value = get_str_var_by_name (lexic, "hostname");
190
191 if (!value)
192 {
193 nasl_perror (lexic, "%s: Empty hostname\n", __func__);
194 return NULL;
195 }
196
197 if (!gvm_resolve_as_addr6 (value, &in6addr))
198 {
200 retc->x.str_val = addr6_as_str (&in6addr);
201 retc->size = strlen (retc->x.str_val);
202 return retc;
203 }
204 return NULL;
205}
206
207tree_cell *
209{
210 struct script_infos *script_infos = lexic->script_infos;
211 struct in6_addr *ip = plug_get_host_ip (script_infos);
212 tree_cell *retc;
213
214 if (ip == NULL) /* WTF ? */
215 {
216 return FAKE_CELL;
217 }
218
220 retc->x.str_val = addr6_as_str (ip);
221 retc->size = strlen (retc->x.str_val);
222
223 return retc;
224}
225
226tree_cell *
228{
229 struct script_infos *script_infos = lexic->script_infos;
230 unsigned int port = plug_get_host_open_port (script_infos);
231 tree_cell *retc;
232
234 retc->x.i_val = port;
235
236 return retc;
237}
238
243
244tree_cell *
246{
247 char *t = get_str_var_by_num (lexic, 0);
248 gvm_host_t *target = NULL;
249 tree_cell *retc;
250
251 if (t == NULL)
252 {
254 }
255 else
256 {
257 // we need to duplicate t because plug_get_host_ip_from_str allocates
258 // memory and to have the same behavior for both we simply duplicate the
259 // memory here
260 t = g_strdup (t);
261 }
262 if (t == NULL)
263 {
264 nasl_perror (lexic, "Empty target\n");
265 goto fail;
266 }
267 target = gvm_host_from_str (t);
268 if (target == NULL)
269 {
270 nasl_perror (lexic, "%s: Invalid target\n", t);
271 g_free (t);
272 goto fail;
273 }
274 g_free (t);
275
276 t = gvm_host_reverse_lookup (target);
277 if (t == NULL)
278 {
279 goto fail;
280 }
281
283 retc->x.str_val = t;
284 retc->size = strlen (t);
285
286 return retc;
287fail:
288 return FAKE_CELL;
289}
290
291tree_cell *
293{
294 int open;
295 struct script_infos *script_infos = lexic->script_infos;
296 tree_cell *retc;
297 long int port;
298
299 port = get_int_var_by_num (lexic, 0, -1);
300
301 if (port < 0 || port > USHRT_MAX)
302 return FAKE_CELL;
303
305 open = host_get_port_state (script_infos, port);
306 retc->x.i_val = open;
307 return retc;
308}
309
310tree_cell *
312{
313 int open;
314 struct script_infos *script_infos = lexic->script_infos;
315 tree_cell *retc;
316 long int port;
317
318 port = get_int_var_by_num (lexic, 0, -1);
319 if (port < 0 || port > USHRT_MAX)
320 return FAKE_CELL;
321
324 retc->x.i_val = open;
325 return retc;
326}
327
328tree_cell *
330{
331 struct script_infos *script_infos = lexic->script_infos;
332 struct in6_addr *dst = plug_get_host_ip (script_infos);
333 tree_cell *retc;
334
336 retc->x.i_val = v6_islocalhost (dst);
337 return retc;
338}
339
340tree_cell *
342{
343 struct script_infos *script_infos = lexic->script_infos;
344 struct in6_addr *ip = plug_get_host_ip (script_infos);
345 tree_cell *retc;
346
348 retc->x.i_val = v6_is_local_ip (ip);
349 return retc;
350}
351
352tree_cell *
354{
355 struct script_infos *script_infos = lexic->script_infos;
356 tree_cell *retc;
357 char hostname[255];
358 struct in6_addr *ia = plug_get_host_ip (script_infos);
359 struct in6_addr in6addr;
360 struct in6_addr src6;
361
363
364 if (gvm_source_iface_is_set ())
365 {
366 struct in6_addr addr;
367
368 /* Use source_iface's IP address when available. */
369 if (IN6_IS_ADDR_V4MAPPED (ia))
370 gvm_source_addr_as_addr6 (&addr);
371 else
372 gvm_source_addr6 (&addr);
373 retc->x.str_val = addr6_as_str (&addr);
374 retc->size = strlen (retc->x.str_val);
375 return retc;
376 }
377 else
378 {
379 /* Manually find the source IP that will be used. */
380 int err = 1;
381 if (v6_islocalhost (ia))
382 memcpy (&src6, ia, sizeof (struct in6_addr));
383 else
384 err = v6_getsourceip (&src6, ia);
385
386 if (err && !IN6_ARE_ADDR_EQUAL (&src6, &in6addr_any))
387 {
388 retc->x.str_val = addr6_as_str (&src6);
389 retc->size = strlen (retc->x.str_val);
390
391 return retc;
392 }
393
394 hostname[sizeof (hostname) - 1] = '\0';
395 gethostname (hostname, sizeof (hostname) - 1);
396 if (gvm_resolve_as_addr6 (hostname, &in6addr))
397 {
398 retc->x.str_val = addr6_as_str (&in6addr);
399 retc->size = strlen (retc->x.str_val);
400 }
401 }
402 return retc;
403}
404
405tree_cell *
407{
408 char *hostname;
409 tree_cell *retc;
410
411 (void) lexic;
413
414 hostname = g_malloc0 (256);
415 gethostname (hostname, 255);
416
417 retc->x.str_val = hostname;
418 retc->size = strlen (hostname);
419 return retc;
420}
421
455tree_cell *
457{
458 struct script_infos *script_infos = lexic->script_infos;
459 tree_cell *retc;
460 long int port = get_int_var_by_num (lexic, 0, -1);
461
462 if (port >= 0 && port <= USHRT_MAX)
463 {
464 int trp = plug_get_port_transport (script_infos, port);
465
467 if (get_int_var_by_name (lexic, "asstring", 0))
468 {
469 const char *s = get_encaps_name (trp);
470 retc->x.str_val = g_strdup (s);
471 retc->size = strlen (s);
472 }
473 else
474 {
475 retc->type = CONST_INT;
476 retc->x.i_val = trp;
477 }
478 return retc;
479 }
480 return NULL;
481}
482
483tree_cell *
485{
486 tree_cell *retc;
487 struct hostent *h;
488 char *hn[2], **names[2];
489 struct in_addr ia, *a[2];
490 int i, j, n[2], names_nb[2], flag;
491 int cmp_hostname = get_int_var_by_name (lexic, "cmp_hostname", 0);
492
493 memset (names_nb, '\0', sizeof (names_nb));
494 memset (names, '\0', sizeof (names));
495 memset (a, '\0', sizeof (a));
496 for (i = 0; i < 2; i++)
497 {
498 hn[i] = get_str_var_by_num (lexic, i);
499 if (hn[i] == NULL)
500 {
501 nasl_perror (lexic, "same_host needs two parameters!\n");
502 return NULL;
503 }
504 if (strlen (hn[i]) >= 256)
505 {
506 nasl_perror (lexic, "same_host(): Too long hostname !\n");
507 return NULL;
508 }
509 }
510 for (i = 0; i < 2; i++)
511 {
512 if (!inet_aton (hn[i], &ia)) /* Not an IP address */
513 {
514 h = gethostbyname (hn[i]);
515 if (h == NULL)
516 {
517 nasl_perror (lexic, "same_host: %s does not resolve\n", hn[i]);
518 n[i] = 0;
519 if (cmp_hostname)
520 {
521 names_nb[i] = 1;
522 names[i] = g_malloc0 (sizeof (char *));
523 names[i][0] = g_strdup (hn[i]);
524 }
525 }
526 else
527 {
528 for (names_nb[i] = 0; h->h_aliases[names_nb[i]] != NULL;
529 names_nb[i]++)
530 ;
531 names_nb[i]++;
532 names[i] = g_malloc0 (sizeof (char *) * names_nb[i]);
533 names[i][0] = g_strdup (h->h_name);
534 for (j = 1; j < names_nb[i]; j++)
535 names[i][j] = g_strdup (h->h_aliases[j - 1]);
536
537 /* Here, we should check that h_addrtype == AF_INET */
538 for (n[i] = 0; ((struct in_addr **) h->h_addr_list)[n[i]] != NULL;
539 n[i]++)
540 ;
541 a[i] = g_malloc0 ((gsize) h->h_length * n[i]);
542 for (j = 0; j < n[i]; j++)
543 a[i][j] = *((struct in_addr **) h->h_addr_list)[j];
544 }
545 }
546 else
547 {
548 if (cmp_hostname)
549 h = gethostbyaddr ((const char *) &ia, sizeof (ia), AF_INET);
550 else
551 h = NULL;
552 if (h == NULL)
553 {
554 a[i] = g_malloc0 (sizeof (struct in_addr));
555 memcpy (a[i], &ia, sizeof (struct in_addr));
556 n[i] = 1;
557 }
558 else
559 {
560 for (names_nb[i] = 0; h->h_aliases[names_nb[i]] != NULL;
561 names_nb[i]++)
562 ;
563 names_nb[i]++;
564 names[i] = g_malloc0 (sizeof (char *) * names_nb[i]);
565 names[i][0] = g_strdup (h->h_name);
566 for (j = 1; j < names_nb[i]; j++)
567 names[i][j] = g_strdup (h->h_aliases[j - 1]);
568
569 /* Here, we should check that h_addrtype == AF_INET */
570 for (n[i] = 0; ((struct in_addr **) h->h_addr_list)[n[i]] != NULL;
571 n[i]++)
572 ;
573 a[i] = g_malloc0 ((gsize) h->h_length * n[i]);
574 for (j = 0; j < n[i]; j++)
575 a[i][j] = *((struct in_addr **) h->h_addr_list)[j];
576 }
577 }
578 }
579 flag = 0;
580 for (i = 0; i < n[0] && !flag; i++)
581 for (j = 0; j < n[1] && !flag; j++)
582 if (a[0][i].s_addr == a[1][j].s_addr)
583 {
584 flag = 1;
585 }
586
587 if (cmp_hostname)
588 for (i = 0; i < names_nb[0] && !flag; i++)
589 for (j = 0; j < names_nb[1] && !flag; j++)
590 if (strcmp (names[0][i], names[1][j]) == 0)
591 {
592 flag = 1;
593 }
594
596 retc->x.i_val = flag;
597
598 for (i = 0; i < 2; i++)
599 g_free (a[i]);
600
601 for (i = 0; i < 2; i++)
602 {
603 for (j = 0; j < names_nb[i]; j++)
604 g_free (names[i][j]);
605 g_free (names[i]);
606 }
607
608 return retc;
609}
610
611tree_cell *
613{
614 tree_cell *retc;
615 struct script_infos *script_infos = lexic->script_infos;
616 struct in6_addr *addr;
617
620
621 if (addr == NULL)
622 {
623 nasl_perror (lexic, "address is NULL!\n");
624 return NULL;
625 }
626 if (IN6_IS_ADDR_V4MAPPED (addr) == 1)
627 retc->x.i_val = 0;
628 else
629 retc->x.i_val = 1;
630
631 return retc;
632}
int ipc_send(struct ipc_context *context, enum ipc_relation to, const char *msg, size_t len)
sends given msg to the target based on the given context
Definition ipc.c:46
@ IPC_MAIN
Definition ipc.h:18
void ipc_data_destroy(ipc_data_t **data)
destroys ipc_data.
const char * ipc_data_to_json(ipc_data_t *data)
transforms ipc_data to a json string
ipc_data_t * ipc_data_type_from_hostname(const char *source, size_t source_len, const char *hostname, size_t hostname_len)
initializes ipc_data for a hostname data.
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition nasl_debug.c:105
tree_cell * nasl_same_host(lex_ctxt *lexic)
Definition nasl_host.c:484
tree_cell * nasl_this_host_name(lex_ctxt *lexic)
Definition nasl_host.c:406
tree_cell * get_hostname_source(lex_ctxt *lexic)
Definition nasl_host.c:95
tree_cell * get_port_state(lex_ctxt *lexic)
Definition nasl_host.c:292
tree_cell * get_hostname(lex_ctxt *lexic)
Definition nasl_host.c:79
tree_cell * nasl_islocalhost(lex_ctxt *lexic)
Definition nasl_host.c:329
tree_cell * nasl_islocalnet(lex_ctxt *lexic)
Definition nasl_host.c:341
tree_cell * nasl_this_host(lex_ctxt *lexic)
Definition nasl_host.c:353
tree_cell * get_host_ip(lex_ctxt *lexic)
Definition nasl_host.c:208
tree_cell * get_port_transport(lex_ctxt *lexic)
Return the encapsulation mode of a port.
Definition nasl_host.c:456
tree_cell * resolve_hostname(lex_ctxt *lexic)
Definition nasl_host.c:186
tree_cell * get_host_open_port(lex_ctxt *lexic)
Definition nasl_host.c:227
tree_cell * get_udp_port_state(lex_ctxt *lexic)
Definition nasl_host.c:311
tree_cell * nasl_target_is_ipv6(lex_ctxt *lexic)
Definition nasl_host.c:612
tree_cell * add_hostname(lex_ctxt *lexic)
Definition nasl_host.c:113
tree_cell * host_reverse_lookup(lex_ctxt *lexic)
implements ip_reverse_lookup
Definition nasl_host.c:245
tree_cell * get_hostnames(lex_ctxt *lexic)
Definition nasl_host.c:49
tree_cell * resolve_hostname_to_multiple_ips(lex_ctxt *lexic)
Resolve a hostname and return all ip addresses as nasl array.
Definition nasl_host.c:153
struct struct_lex_ctxt lex_ctxt
char * get_str_var_by_name(lex_ctxt *, const char *)
Definition nasl_var.c:1118
char * get_str_var_by_num(lex_ctxt *, int)
Definition nasl_var.c:1108
long int get_int_var_by_num(lex_ctxt *, int, int)
Definition nasl_var.c:1094
long int get_int_var_by_name(lex_ctxt *, const char *, int)
Definition nasl_var.c:1101
tree_cell * alloc_typed_cell(int typ)
Definition nasl_tree.c:25
@ CONST_DATA
Definition nasl_tree.h:82
@ DYN_ARRAY
Definition nasl_tree.h:90
@ CONST_STR
Definition nasl_tree.h:80
@ CONST_INT
Definition nasl_tree.h:79
struct TC tree_cell
#define FAKE_CELL
Definition nasl_tree.h:110
int add_var_to_list(nasl_array *a, int i, const anon_nasl_var *v)
Definition nasl_var.c:1245
struct st_a_nasl_var anon_nasl_var
struct st_nasl_array nasl_array
@ VAR2_DATA
Definition nasl_var.h:18
const char * get_encaps_name(openvas_encaps_t code)
Definition network.c:1733
Header file for module network.
int v6_is_local_ip(struct in6_addr *addr)
Definition pcap.c:115
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:234
int v6_getsourceip(struct in6_addr *src, struct in6_addr *dst)
Definition pcap.c:487
Header file for module pcap.
const char * hostname
int host_get_port_state_udp(struct script_infos *plugdata, int portnum)
Definition plugutils.c:199
int host_get_port_state(struct script_infos *plugdata, int portnum)
Definition plugutils.c:193
int plug_get_port_transport(struct script_infos *args, int port)
Definition plugutils.c:1396
unsigned int plug_get_host_open_port(struct script_infos *desc)
Definition plugutils.c:1328
GSList * plug_get_host_fqdn_list(struct script_infos *args)
Definition plugutils.c:325
char * plug_get_host_source(struct script_infos *args, const char *hostname)
Definition plugutils.c:343
char * plug_get_host_fqdn(struct script_infos *args)
Definition plugutils.c:291
char * plug_get_host_ip_str(struct script_infos *desc)
Definition plugutils.c:377
int plug_add_host_fqdn(struct script_infos *args, const char *hostname, const char *source)
Definition plugutils.c:257
struct in6_addr * plug_get_host_ip(struct script_infos *args)
Definition plugutils.c:371
Header file for module plugutils.
long int i_val
Definition nasl_tree.h:104
long int size
Definition nasl_tree.h:99
union TC::@332262321161220155002104006201360276211317150140 x
char * str_val
Definition nasl_tree.h:103
void * ref_val
Definition nasl_tree.h:105
short type
Definition nasl_tree.h:95
struct list * next
struct ipc_context * ipc_context
Definition scanneraux.h:31
nasl_string_t v_str
Definition nasl_var.h:47
union st_a_nasl_var::@154137074032032170165360023270032033276061363156 v
unsigned char * s_val
Definition nasl_var.h:26
long int s_siz
Definition nasl_var.h:27
struct script_infos * script_infos