OpenVAS Scanner 23.32.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 <net/if.h>
35#include <net/if_arp.h>
36#include <netdb.h> /* for gethostbyaddr */
37#include <netinet/in.h> /* for in_addr */
38#include <string.h> /* for strlen */
39#include <sys/ioctl.h>
40#include <unistd.h> /* for gethostname */
41
42#undef G_LOG_DOMAIN
46#define G_LOG_DOMAIN "sd nasl"
49{
50 struct script_infos *script_infos = lexic->script_infos;
51 tree_cell *retc;
52 int i = 0;
53 nasl_array *arr;
54 GSList *tmp, *hostnames;
55
56 hostnames = tmp = plug_get_host_fqdn_list (script_infos);
57 if (!hostnames)
58 return NULL;
59
61 retc->x.ref_val = arr = g_malloc0 (sizeof (nasl_array));
62 while (tmp)
63 {
65
67 v.v.v_str.s_siz = strlen (tmp->data);
68 v.v.v_str.s_val = tmp->data;
69 add_var_to_list (arr, i++, &v);
70 tmp = tmp->next;
71 }
72
73 g_slist_free_full (hostnames, g_free);
74 return retc;
75}
76
79{
80 struct script_infos *script_infos = lexic->script_infos;
82 tree_cell *retc;
83
84 if (hostname == NULL)
85 return NULL;
86
88 retc->size = strlen (hostname);
89 retc->x.str_val = hostname;
90 return retc;
91}
92
95{
96 struct script_infos *script_infos = lexic->script_infos;
97 char *source;
98 tree_cell *retc;
99
101 get_str_var_by_name (lexic, "hostname"));
102 if (!source)
103 return NULL;
104
106 retc->size = strlen (source);
107 retc->x.str_val = source;
108 return retc;
109}
110
111tree_cell *
113{
114 struct ipc_data *hn = NULL;
115 char *lower;
116 const char *json = NULL;
117 char *value = get_str_var_by_name (lexic, "hostname");
118 char *source = get_str_var_by_name (lexic, "source");
119
120 if (!value)
121 {
122 nasl_perror (lexic, "%s: Empty hostname\n", __func__);
123 return NULL;
124 }
125 if (!source || !*source)
126 source = "NASL";
127 /* Add to current process' vhosts list. */
128 lower = g_ascii_strdown (value, -1);
129 hn = ipc_data_type_from_hostname (source, strlen (source), lower,
130 strlen (lower));
131 json = ipc_data_to_json (hn);
132 ipc_data_destroy (&hn);
133 if (plug_add_host_fqdn (lexic->script_infos, lower, source))
134 goto end_add_hostname;
135
136 // send it to host process to extend vhosts list there
137 if (ipc_send (lexic->script_infos->ipc_context, IPC_MAIN, json, strlen (json))
138 < 0)
139 g_warning ("Unable to send %s to host process", lower);
140
141end_add_hostname:
142 g_free (lower);
143 g_free ((void *) json);
144 return NULL;
145}
146
151tree_cell *
153{
154 GSList *list = NULL;
155 char *value = get_str_var_by_name (lexic, "hostname");
156 tree_cell *retc;
157 nasl_array *arr;
158 int i = 0;
159
160 if (!value)
161 {
162 nasl_perror (lexic, "%s: Empty hostname\n", __func__);
163 return NULL;
164 }
165
166 list = gvm_resolve_list (value);
167
169 retc->x.ref_val = arr = g_malloc0 (sizeof (nasl_array));
170 while (list)
171 {
172 anon_nasl_var var_aux;
173
174 var_aux.var_type = VAR2_DATA;
175 var_aux.v.v_str.s_siz = strlen (addr6_as_str (list->data));
176 var_aux.v.v_str.s_val = (unsigned char *) addr6_as_str (list->data);
177 add_var_to_list (arr, i++, &var_aux);
178 list = list->next;
179 }
180 g_slist_free_full (list, g_free);
181 return retc;
182}
183
184tree_cell *
186{
187 struct in6_addr in6addr;
188 char *value = get_str_var_by_name (lexic, "hostname");
189
190 if (!value)
191 {
192 nasl_perror (lexic, "%s: Empty hostname\n", __func__);
193 return NULL;
194 }
195
196 if (!gvm_resolve_as_addr6 (value, &in6addr))
197 {
199 retc->x.str_val = addr6_as_str (&in6addr);
200 retc->size = strlen (retc->x.str_val);
201 return retc;
202 }
203 return NULL;
204}
205
206tree_cell *
208{
209 struct script_infos *script_infos = lexic->script_infos;
210 struct in6_addr *ip = plug_get_host_ip (script_infos);
211 tree_cell *retc;
212
213 if (ip == NULL) /* WTF ? */
214 {
215 return FAKE_CELL;
216 }
217
219 retc->x.str_val = addr6_as_str (ip);
220 retc->size = strlen (retc->x.str_val);
221
222 return retc;
223}
224
225tree_cell *
227{
228 struct script_infos *script_infos = lexic->script_infos;
229 unsigned int port = plug_get_host_open_port (script_infos);
230 tree_cell *retc;
231
233 retc->x.i_val = port;
234
235 return retc;
236}
237
242
243tree_cell *
245{
246 char *t = get_str_var_by_num (lexic, 0);
247 gvm_host_t *target = NULL;
248 tree_cell *retc;
249
250 if (t == NULL)
251 {
253 }
254 else
255 {
256 // we need to duplicate t because plug_get_host_ip_from_str allocates
257 // memory and to have the same behavior for both we simply duplicate the
258 // memory here
259 t = g_strdup (t);
260 }
261 if (t == NULL)
262 {
263 nasl_perror (lexic, "Empty target\n");
264 goto fail;
265 }
266 target = gvm_host_from_str (t);
267 if (target == NULL)
268 {
269 nasl_perror (lexic, "%s: Invalid target\n", t);
270 g_free (t);
271 goto fail;
272 }
273 g_free (t);
274
275 t = gvm_host_reverse_lookup (target);
276 if (t == NULL)
277 {
278 goto fail;
279 }
280
282 retc->x.str_val = t;
283 retc->size = strlen (t);
284
285 return retc;
286fail:
287 return FAKE_CELL;
288}
289
290tree_cell *
292{
293 int open;
294 struct script_infos *script_infos = lexic->script_infos;
295 tree_cell *retc;
296 int port;
297
298 port = get_int_var_by_num (lexic, 0, -1);
299 if (port < 0)
300 return FAKE_CELL;
301
303 open = host_get_port_state (script_infos, port);
304 retc->x.i_val = open;
305 return retc;
306}
307
308tree_cell *
310{
311 int open;
312 struct script_infos *script_infos = lexic->script_infos;
313 tree_cell *retc;
314 int port;
315
316 port = get_int_var_by_num (lexic, 0, -1);
317 if (port < 0)
318 return FAKE_CELL;
319
322 retc->x.i_val = open;
323 return retc;
324}
325
326tree_cell *
328{
329 struct script_infos *script_infos = lexic->script_infos;
330 struct in6_addr *dst = plug_get_host_ip (script_infos);
331 tree_cell *retc;
332
334 retc->x.i_val = v6_islocalhost (dst);
335 return retc;
336}
337
338tree_cell *
340{
341 struct script_infos *script_infos = lexic->script_infos;
342 struct in6_addr *ip = plug_get_host_ip (script_infos);
343 tree_cell *retc;
344
346 retc->x.i_val = v6_is_local_ip (ip);
347 return retc;
348}
349
350tree_cell *
352{
353 struct script_infos *script_infos = lexic->script_infos;
354 tree_cell *retc;
355 char hostname[255];
356 struct in6_addr *ia = plug_get_host_ip (script_infos);
357 struct in6_addr in6addr;
358 struct in6_addr src6;
359
361
362 if (gvm_source_iface_is_set ())
363 {
364 struct in6_addr addr;
365
366 /* Use source_iface's IP address when available. */
367 if (IN6_IS_ADDR_V4MAPPED (ia))
368 gvm_source_addr_as_addr6 (&addr);
369 else
370 gvm_source_addr6 (&addr);
371 retc->x.str_val = addr6_as_str (&addr);
372 retc->size = strlen (retc->x.str_val);
373 return retc;
374 }
375 else
376 {
377 /* Manually find the source IP that will be used. */
378 int err = 1;
379 if (v6_islocalhost (ia))
380 memcpy (&src6, ia, sizeof (struct in6_addr));
381 else
382 err = v6_getsourceip (&src6, ia);
383
384 if (err && !IN6_ARE_ADDR_EQUAL (&src6, &in6addr_any))
385 {
386 retc->x.str_val = addr6_as_str (&src6);
387 retc->size = strlen (retc->x.str_val);
388
389 return retc;
390 }
391
392 hostname[sizeof (hostname) - 1] = '\0';
393 gethostname (hostname, sizeof (hostname) - 1);
394 if (gvm_resolve_as_addr6 (hostname, &in6addr))
395 {
396 retc->x.str_val = addr6_as_str (&in6addr);
397 retc->size = strlen (retc->x.str_val);
398 }
399 }
400 return retc;
401}
402
403tree_cell *
405{
406 char *hostname;
407 tree_cell *retc;
408
409 (void) lexic;
411
412 hostname = g_malloc0 (256);
413 gethostname (hostname, 255);
414
415 retc->x.str_val = hostname;
416 retc->size = strlen (hostname);
417 return retc;
418}
419
453tree_cell *
455{
456 struct script_infos *script_infos = lexic->script_infos;
457 tree_cell *retc;
458 int port = get_int_var_by_num (lexic, 0, -1);
459
460 if (port >= 0)
461 {
462 int trp = plug_get_port_transport (script_infos, port);
463
465 if (get_int_var_by_name (lexic, "asstring", 0))
466 {
467 const char *s = get_encaps_name (trp);
468 retc->x.str_val = g_strdup (s);
469 retc->size = strlen (s);
470 }
471 else
472 {
473 retc->type = CONST_INT;
474 retc->x.i_val = trp;
475 }
476 return retc;
477 }
478 return NULL;
479}
480
481tree_cell *
483{
484 tree_cell *retc;
485 struct hostent *h;
486 char *hn[2], **names[2];
487 struct in_addr ia, *a[2];
488 int i, j, n[2], names_nb[2], flag;
489 int cmp_hostname = get_int_var_by_name (lexic, "cmp_hostname", 0);
490
491 memset (names_nb, '\0', sizeof (names_nb));
492 memset (names, '\0', sizeof (names));
493 memset (a, '\0', sizeof (a));
494 for (i = 0; i < 2; i++)
495 {
496 hn[i] = get_str_var_by_num (lexic, i);
497 if (hn[i] == NULL)
498 {
499 nasl_perror (lexic, "same_host needs two parameters!\n");
500 return NULL;
501 }
502 if (strlen (hn[i]) >= 256)
503 {
504 nasl_perror (lexic, "same_host(): Too long hostname !\n");
505 return NULL;
506 }
507 }
508 for (i = 0; i < 2; i++)
509 {
510 if (!inet_aton (hn[i], &ia)) /* Not an IP address */
511 {
512 h = gethostbyname (hn[i]);
513 if (h == NULL)
514 {
515 nasl_perror (lexic, "same_host: %s does not resolve\n", hn[i]);
516 n[i] = 0;
517 if (cmp_hostname)
518 {
519 names_nb[i] = 1;
520 names[i] = g_malloc0 (sizeof (char *));
521 names[i][0] = g_strdup (hn[i]);
522 }
523 }
524 else
525 {
526 for (names_nb[i] = 0; h->h_aliases[names_nb[i]] != NULL;
527 names_nb[i]++)
528 ;
529 names_nb[i]++;
530 names[i] = g_malloc0 (sizeof (char *) * names_nb[i]);
531 names[i][0] = g_strdup (h->h_name);
532 for (j = 1; j < names_nb[i]; j++)
533 names[i][j] = g_strdup (h->h_aliases[j - 1]);
534
535 /* Here, we should check that h_addrtype == AF_INET */
536 for (n[i] = 0; ((struct in_addr **) h->h_addr_list)[n[i]] != NULL;
537 n[i]++)
538 ;
539 a[i] = g_malloc0 ((gsize) h->h_length * n[i]);
540 for (j = 0; j < n[i]; j++)
541 a[i][j] = *((struct in_addr **) h->h_addr_list)[j];
542 }
543 }
544 else
545 {
546 if (cmp_hostname)
547 h = gethostbyaddr ((const char *) &ia, sizeof (ia), AF_INET);
548 else
549 h = NULL;
550 if (h == NULL)
551 {
552 a[i] = g_malloc0 (sizeof (struct in_addr));
553 memcpy (a[i], &ia, sizeof (struct in_addr));
554 n[i] = 1;
555 }
556 else
557 {
558 for (names_nb[i] = 0; h->h_aliases[names_nb[i]] != NULL;
559 names_nb[i]++)
560 ;
561 names_nb[i]++;
562 names[i] = g_malloc0 (sizeof (char *) * names_nb[i]);
563 names[i][0] = g_strdup (h->h_name);
564 for (j = 1; j < names_nb[i]; j++)
565 names[i][j] = g_strdup (h->h_aliases[j - 1]);
566
567 /* Here, we should check that h_addrtype == AF_INET */
568 for (n[i] = 0; ((struct in_addr **) h->h_addr_list)[n[i]] != NULL;
569 n[i]++)
570 ;
571 a[i] = g_malloc0 ((gsize) h->h_length * n[i]);
572 for (j = 0; j < n[i]; j++)
573 a[i][j] = *((struct in_addr **) h->h_addr_list)[j];
574 }
575 }
576 }
577 flag = 0;
578 for (i = 0; i < n[0] && !flag; i++)
579 for (j = 0; j < n[1] && !flag; j++)
580 if (a[0][i].s_addr == a[1][j].s_addr)
581 {
582 flag = 1;
583 }
584
585 if (cmp_hostname)
586 for (i = 0; i < names_nb[0] && !flag; i++)
587 for (j = 0; j < names_nb[1] && !flag; j++)
588 if (strcmp (names[0][i], names[1][j]) == 0)
589 {
590 flag = 1;
591 }
592
594 retc->x.i_val = flag;
595
596 for (i = 0; i < 2; i++)
597 g_free (a[i]);
598
599 for (i = 0; i < 2; i++)
600 {
601 for (j = 0; j < names_nb[i]; j++)
602 g_free (names[i][j]);
603 g_free (names[i]);
604 }
605
606 return retc;
607}
608
609tree_cell *
611{
612 tree_cell *retc;
613 struct script_infos *script_infos = lexic->script_infos;
614 struct in6_addr *addr;
615
618
619 if (addr == NULL)
620 {
621 nasl_perror (lexic, "address is NULL!\n");
622 return NULL;
623 }
624 if (IN6_IS_ADDR_V4MAPPED (addr) == 1)
625 retc->x.i_val = 0;
626 else
627 retc->x.i_val = 1;
628
629 return retc;
630}
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:482
tree_cell * nasl_this_host_name(lex_ctxt *lexic)
Definition nasl_host.c:404
tree_cell * get_hostname_source(lex_ctxt *lexic)
Definition nasl_host.c:94
tree_cell * get_port_state(lex_ctxt *lexic)
Definition nasl_host.c:291
tree_cell * get_hostname(lex_ctxt *lexic)
Definition nasl_host.c:78
tree_cell * nasl_islocalhost(lex_ctxt *lexic)
Definition nasl_host.c:327
tree_cell * nasl_islocalnet(lex_ctxt *lexic)
Definition nasl_host.c:339
tree_cell * nasl_this_host(lex_ctxt *lexic)
Definition nasl_host.c:351
tree_cell * get_host_ip(lex_ctxt *lexic)
Definition nasl_host.c:207
tree_cell * get_port_transport(lex_ctxt *lexic)
Return the encapsulation mode of a port.
Definition nasl_host.c:454
tree_cell * resolve_hostname(lex_ctxt *lexic)
Definition nasl_host.c:185
tree_cell * get_host_open_port(lex_ctxt *lexic)
Definition nasl_host.c:226
tree_cell * get_udp_port_state(lex_ctxt *lexic)
Definition nasl_host.c:309
tree_cell * nasl_target_is_ipv6(lex_ctxt *lexic)
Definition nasl_host.c:610
tree_cell * add_hostname(lex_ctxt *lexic)
Definition nasl_host.c:112
tree_cell * host_reverse_lookup(lex_ctxt *lexic)
implements ip_reverse_lookup
Definition nasl_host.c:244
tree_cell * get_hostnames(lex_ctxt *lexic)
Definition nasl_host.c:48
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:152
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:1391
unsigned int plug_get_host_open_port(struct script_infos *desc)
Definition plugutils.c:1323
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.
int size
Definition nasl_tree.h:99
long int i_val
Definition nasl_tree.h:104
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
struct script_infos * script_infos