OpenVAS Scanner 22.7.9
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
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 "nasl_debug.h"
24#include "nasl_func.h"
25#include "nasl_global_ctxt.h"
26#include "nasl_lex_ctxt.h"
27#include "nasl_tree.h"
28#include "nasl_var.h"
29
30#include <arpa/inet.h> /* for inet_aton */
31#include <gvm/base/networking.h>
32#include <gvm/util/kb.h>
33#include <net/if.h>
34#include <net/if_arp.h>
35#include <netdb.h> /* for gethostbyaddr */
36#include <netinet/in.h> /* for in_addr */
37#include <string.h> /* for strlen */
38#include <sys/ioctl.h>
39#include <unistd.h> /* for gethostname */
40
41#undef G_LOG_DOMAIN
45#define G_LOG_DOMAIN "sd nasl"
48{
49 struct script_infos *script_infos = lexic->script_infos;
50 tree_cell *retc;
51 int i = 0;
52 nasl_array *arr;
53 GSList *tmp, *hostnames;
54
55 hostnames = tmp = plug_get_host_fqdn_list (script_infos);
56 if (!hostnames)
57 return NULL;
58
60 retc->x.ref_val = arr = g_malloc0 (sizeof (nasl_array));
61 while (tmp)
62 {
64
66 v.v.v_str.s_siz = strlen (tmp->data);
67 v.v.v_str.s_val = tmp->data;
68 add_var_to_list (arr, i++, &v);
69 tmp = tmp->next;
70 }
71
72 g_slist_free_full (hostnames, g_free);
73 return retc;
74}
75
78{
79 struct script_infos *script_infos = lexic->script_infos;
81 tree_cell *retc;
82
83 if (hostname == NULL)
84 return NULL;
85
87 retc->size = strlen (hostname);
88 retc->x.str_val = hostname;
89 return retc;
90}
91
94{
95 struct script_infos *script_infos = lexic->script_infos;
96 char *source;
97 tree_cell *retc;
98
100 get_str_var_by_name (lexic, "hostname"));
101 if (!source)
102 return NULL;
103
105 retc->size = strlen (source);
106 retc->x.str_val = source;
107 return retc;
108}
109
110tree_cell *
112{
113 struct ipc_data *hn = NULL;
114 char *lower;
115 const char *json = NULL;
116 char *value = get_str_var_by_name (lexic, "hostname");
117 char *source = get_str_var_by_name (lexic, "source");
118
119 if (!value)
120 {
121 nasl_perror (lexic, "%s: Empty hostname\n", __func__);
122 return NULL;
123 }
124 if (!source || !*source)
125 source = "NASL";
126 /* Add to current process' vhosts list. */
127 lower = g_ascii_strdown (value, -1);
128 hn = ipc_data_type_from_hostname (source, strlen (source), lower,
129 strlen (lower));
130 json = ipc_data_to_json (hn);
131 ipc_data_destroy (&hn);
132 if (plug_add_host_fqdn (lexic->script_infos, lower, source))
133 goto end_add_hostname;
134
135 // send it to host process to extend vhosts list there
136 if (ipc_send (lexic->script_infos->ipc_context, IPC_MAIN, json, strlen (json))
137 < 0)
138 g_warning ("Unable to send %s to host process", lower);
139
140end_add_hostname:
141 g_free (lower);
142 g_free ((void *) json);
143 return NULL;
144}
145
150tree_cell *
152{
153 GSList *list = NULL;
154 char *value = get_str_var_by_name (lexic, "hostname");
155 tree_cell *retc;
156 nasl_array *arr;
157 int i = 0;
158
159 if (!value)
160 {
161 nasl_perror (lexic, "%s: Empty hostname\n", __func__);
162 return NULL;
163 }
164
165 list = gvm_resolve_list (value);
166
168 retc->x.ref_val = arr = g_malloc0 (sizeof (nasl_array));
169 while (list)
170 {
171 anon_nasl_var var_aux;
172
173 var_aux.var_type = VAR2_DATA;
174 var_aux.v.v_str.s_siz = strlen (addr6_as_str (list->data));
175 var_aux.v.v_str.s_val = (unsigned char *) addr6_as_str (list->data);
176 add_var_to_list (arr, i++, &var_aux);
177 list = list->next;
178 }
179 g_slist_free_full (list, g_free);
180 return retc;
181}
182
183tree_cell *
185{
186 struct in6_addr in6addr;
187 char *value = get_str_var_by_name (lexic, "hostname");
188
189 if (!value)
190 {
191 nasl_perror (lexic, "%s: Empty hostname\n", __func__);
192 return NULL;
193 }
194
195 if (!gvm_resolve_as_addr6 (value, &in6addr))
196 {
198 retc->x.str_val = addr6_as_str (&in6addr);
199 retc->size = strlen (retc->x.str_val);
200 return retc;
201 }
202 return NULL;
203}
204
205tree_cell *
207{
208 struct script_infos *script_infos = lexic->script_infos;
209 struct in6_addr *ip = plug_get_host_ip (script_infos);
210 tree_cell *retc;
211
212 if (ip == NULL) /* WTF ? */
213 {
214 return FAKE_CELL;
215 }
216
218 retc->x.str_val = addr6_as_str (ip);
219 retc->size = strlen (retc->x.str_val);
220
221 return retc;
222}
223
224tree_cell *
226{
227 struct script_infos *script_infos = lexic->script_infos;
228 unsigned int port = plug_get_host_open_port (script_infos);
229 tree_cell *retc;
230
232 retc->x.i_val = port;
233
234 return retc;
235}
236
237tree_cell *
239{
240 int open;
241 struct script_infos *script_infos = lexic->script_infos;
242 tree_cell *retc;
243 int port;
244
245 port = get_int_var_by_num (lexic, 0, -1);
246 if (port < 0)
247 return FAKE_CELL;
248
250 open = host_get_port_state (script_infos, port);
251 retc->x.i_val = open;
252 return retc;
253}
254
255tree_cell *
257{
258 int open;
259 struct script_infos *script_infos = lexic->script_infos;
260 tree_cell *retc;
261 int port;
262
263 port = get_int_var_by_num (lexic, 0, -1);
264 if (port < 0)
265 return FAKE_CELL;
266
269 retc->x.i_val = open;
270 return retc;
271}
272
273tree_cell *
275{
276 struct script_infos *script_infos = lexic->script_infos;
277 struct in6_addr *dst = plug_get_host_ip (script_infos);
278 tree_cell *retc;
279
281 retc->x.i_val = v6_islocalhost (dst);
282 return retc;
283}
284
285tree_cell *
287{
288 struct script_infos *script_infos = lexic->script_infos;
289 struct in6_addr *ip = plug_get_host_ip (script_infos);
290 tree_cell *retc;
291
293 retc->x.i_val = v6_is_local_ip (ip);
294 return retc;
295}
296
297tree_cell *
299{
300 struct script_infos *script_infos = lexic->script_infos;
301 tree_cell *retc;
302 char hostname[255];
303 struct in6_addr *ia = plug_get_host_ip (script_infos);
304 struct in6_addr in6addr;
305 struct in6_addr src6;
306
308
309 if (gvm_source_iface_is_set ())
310 {
311 struct in6_addr addr;
312
313 /* Use source_iface's IP address when available. */
314 if (IN6_IS_ADDR_V4MAPPED (ia))
315 gvm_source_addr_as_addr6 (&addr);
316 else
317 gvm_source_addr6 (&addr);
318 retc->x.str_val = addr6_as_str (&addr);
319 retc->size = strlen (retc->x.str_val);
320 return retc;
321 }
322 else
323 {
324 /* Manually find the source IP that will be used. */
325 int err = 1;
326 if (v6_islocalhost (ia))
327 memcpy (&src6, ia, sizeof (struct in6_addr));
328 else
329 err = v6_getsourceip (&src6, ia);
330
331 if (err && !IN6_ARE_ADDR_EQUAL (&src6, &in6addr_any))
332 {
333 retc->x.str_val = addr6_as_str (&src6);
334 retc->size = strlen (retc->x.str_val);
335
336 return retc;
337 }
338
339 hostname[sizeof (hostname) - 1] = '\0';
340 gethostname (hostname, sizeof (hostname) - 1);
341 if (gvm_resolve_as_addr6 (hostname, &in6addr))
342 {
343 retc->x.str_val = addr6_as_str (&in6addr);
344 retc->size = strlen (retc->x.str_val);
345 }
346 }
347 return retc;
348}
349
350tree_cell *
352{
353 char *hostname;
354 tree_cell *retc;
355
356 (void) lexic;
358
359 hostname = g_malloc0 (256);
360 gethostname (hostname, 255);
361
362 retc->x.str_val = hostname;
363 retc->size = strlen (hostname);
364 return retc;
365}
366
400tree_cell *
402{
403 struct script_infos *script_infos = lexic->script_infos;
404 tree_cell *retc;
405 int port = get_int_var_by_num (lexic, 0, -1);
406
407 if (port >= 0)
408 {
409 int trp = plug_get_port_transport (script_infos, port);
410
412 if (get_int_var_by_name (lexic, "asstring", 0))
413 {
414 const char *s = get_encaps_name (trp);
415 retc->x.str_val = g_strdup (s);
416 retc->size = strlen (s);
417 }
418 else
419 {
420 retc->type = CONST_INT;
421 retc->x.i_val = trp;
422 }
423 return retc;
424 }
425 return NULL;
426}
427
428tree_cell *
430{
431 tree_cell *retc;
432 struct hostent *h;
433 char *hn[2], **names[2];
434 struct in_addr ia, *a[2];
435 int i, j, n[2], names_nb[2], flag;
436 int cmp_hostname = get_int_var_by_name (lexic, "cmp_hostname", 0);
437
438 memset (names_nb, '\0', sizeof (names_nb));
439 memset (names, '\0', sizeof (names));
440 memset (a, '\0', sizeof (a));
441 for (i = 0; i < 2; i++)
442 {
443 hn[i] = get_str_var_by_num (lexic, i);
444 if (hn[i] == NULL)
445 {
446 nasl_perror (lexic, "same_host needs two parameters!\n");
447 return NULL;
448 }
449 if (strlen (hn[i]) >= 256)
450 {
451 nasl_perror (lexic, "same_host(): Too long hostname !\n");
452 return NULL;
453 }
454 }
455 for (i = 0; i < 2; i++)
456 {
457 if (!inet_aton (hn[i], &ia)) /* Not an IP address */
458 {
459 h = gethostbyname (hn[i]);
460 if (h == NULL)
461 {
462 nasl_perror (lexic, "same_host: %s does not resolve\n", hn[i]);
463 n[i] = 0;
464 if (cmp_hostname)
465 {
466 names_nb[i] = 1;
467 names[i] = g_malloc0 (sizeof (char *));
468 names[i][0] = g_strdup (hn[i]);
469 }
470 }
471 else
472 {
473 for (names_nb[i] = 0; h->h_aliases[names_nb[i]] != NULL;
474 names_nb[i]++)
475 ;
476 names_nb[i]++;
477 names[i] = g_malloc0 (sizeof (char *) * names_nb[i]);
478 names[i][0] = g_strdup (h->h_name);
479 for (j = 1; j < names_nb[i]; j++)
480 names[i][j] = g_strdup (h->h_aliases[j - 1]);
481
482 /* Here, we should check that h_addrtype == AF_INET */
483 for (n[i] = 0; ((struct in_addr **) h->h_addr_list)[n[i]] != NULL;
484 n[i]++)
485 ;
486 a[i] = g_malloc0 ((gsize) h->h_length * n[i]);
487 for (j = 0; j < n[i]; j++)
488 a[i][j] = *((struct in_addr **) h->h_addr_list)[j];
489 }
490 }
491 else
492 {
493 if (cmp_hostname)
494 h = gethostbyaddr ((const char *) &ia, sizeof (ia), AF_INET);
495 else
496 h = NULL;
497 if (h == NULL)
498 {
499 a[i] = g_malloc0 (sizeof (struct in_addr));
500 memcpy (a[i], &ia, sizeof (struct in_addr));
501 n[i] = 1;
502 }
503 else
504 {
505 for (names_nb[i] = 0; h->h_aliases[names_nb[i]] != NULL;
506 names_nb[i]++)
507 ;
508 names_nb[i]++;
509 names[i] = g_malloc0 (sizeof (char *) * names_nb[i]);
510 names[i][0] = g_strdup (h->h_name);
511 for (j = 1; j < names_nb[i]; j++)
512 names[i][j] = g_strdup (h->h_aliases[j - 1]);
513
514 /* Here, we should check that h_addrtype == AF_INET */
515 for (n[i] = 0; ((struct in_addr **) h->h_addr_list)[n[i]] != NULL;
516 n[i]++)
517 ;
518 a[i] = g_malloc0 ((gsize) h->h_length * n[i]);
519 for (j = 0; j < n[i]; j++)
520 a[i][j] = *((struct in_addr **) h->h_addr_list)[j];
521 }
522 }
523 }
524 flag = 0;
525 for (i = 0; i < n[0] && !flag; i++)
526 for (j = 0; j < n[1] && !flag; j++)
527 if (a[0][i].s_addr == a[1][j].s_addr)
528 {
529 flag = 1;
530 }
531
532 if (cmp_hostname)
533 for (i = 0; i < names_nb[0] && !flag; i++)
534 for (j = 0; j < names_nb[1] && !flag; j++)
535 if (strcmp (names[0][i], names[1][j]) == 0)
536 {
537 flag = 1;
538 }
539
541 retc->x.i_val = flag;
542
543 for (i = 0; i < 2; i++)
544 g_free (a[i]);
545
546 for (i = 0; i < 2; i++)
547 {
548 for (j = 0; j < names_nb[i]; j++)
549 g_free (names[i][j]);
550 g_free (names[i]);
551 }
552
553 return retc;
554}
555
556tree_cell *
558{
559 tree_cell *retc;
560 struct script_infos *script_infos = lexic->script_infos;
561 struct in6_addr *addr;
562
565
566 if (addr == NULL)
567 {
568 nasl_perror (lexic, "address is NULL!\n");
569 return NULL;
570 }
571 if (IN6_IS_ADDR_V4MAPPED (addr) == 1)
572 retc->x.i_val = 0;
573 else
574 retc->x.i_val = 1;
575
576 return retc;
577}
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
struct ipc_data * 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.
Definition: ipc_openvas.c:124
void ipc_data_destroy(ipc_data_t **data)
destroys ipc_data.
Definition: ipc_openvas.c:224
const char * ipc_data_to_json(struct ipc_data *data)
transforms ipc_data to a json string
Definition: ipc_openvas.c:251
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition: nasl_debug.c:111
tree_cell * nasl_same_host(lex_ctxt *lexic)
Definition: nasl_host.c:429
tree_cell * nasl_this_host_name(lex_ctxt *lexic)
Definition: nasl_host.c:351
tree_cell * get_hostname_source(lex_ctxt *lexic)
Definition: nasl_host.c:93
tree_cell * get_port_state(lex_ctxt *lexic)
Definition: nasl_host.c:238
tree_cell * get_hostname(lex_ctxt *lexic)
Definition: nasl_host.c:77
tree_cell * nasl_islocalhost(lex_ctxt *lexic)
Definition: nasl_host.c:274
tree_cell * nasl_islocalnet(lex_ctxt *lexic)
Definition: nasl_host.c:286
tree_cell * nasl_this_host(lex_ctxt *lexic)
Definition: nasl_host.c:298
tree_cell * get_host_ip(lex_ctxt *lexic)
Definition: nasl_host.c:206
tree_cell * get_port_transport(lex_ctxt *lexic)
Return the encapsulation mode of a port.
Definition: nasl_host.c:401
tree_cell * resolve_hostname(lex_ctxt *lexic)
Definition: nasl_host.c:184
tree_cell * get_host_open_port(lex_ctxt *lexic)
Definition: nasl_host.c:225
tree_cell * get_udp_port_state(lex_ctxt *lexic)
Definition: nasl_host.c:256
tree_cell * nasl_target_is_ipv6(lex_ctxt *lexic)
Definition: nasl_host.c:557
tree_cell * add_hostname(lex_ctxt *lexic)
Definition: nasl_host.c:111
tree_cell * get_hostnames(lex_ctxt *lexic)
Definition: nasl_host.c:47
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:151
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:1111
long int get_int_var_by_num(lex_ctxt *, int, int)
Definition: nasl_var.c:1097
long int get_int_var_by_name(lex_ctxt *, const char *, int)
Definition: nasl_var.c:1104
tree_cell * alloc_typed_cell(int typ)
Definition: nasl_tree.c:28
@ 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
#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
@ VAR2_DATA
Definition: nasl_var.h:18
const char * get_encaps_name(openvas_encaps_t code)
Definition: network.c:1733
int v6_is_local_ip(struct in6_addr *addr)
Definition: pcap.c:108
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:224
int v6_getsourceip(struct in6_addr *src, struct in6_addr *dst)
Definition: pcap.c:477
const char * hostname
Definition: pluginlaunch.c:68
int host_get_port_state_udp(struct script_infos *plugdata, int portnum)
Definition: plugutils.c:150
int host_get_port_state(struct script_infos *plugdata, int portnum)
Definition: plugutils.c:144
int plug_get_port_transport(struct script_infos *args, int port)
Definition: plugutils.c:1288
unsigned int plug_get_host_open_port(struct script_infos *desc)
Definition: plugutils.c:1220
GSList * plug_get_host_fqdn_list(struct script_infos *args)
Definition: plugutils.c:270
char * plug_get_host_source(struct script_infos *args, const char *hostname)
Definition: plugutils.c:288
char * plug_get_host_fqdn(struct script_infos *args)
Definition: plugutils.c:242
int plug_add_host_fqdn(struct script_infos *args, const char *hostname, const char *source)
Definition: plugutils.c:208
struct in6_addr * plug_get_host_ip(struct script_infos *args)
Definition: plugutils.c:316
Definition: nasl_tree.h:94
union TC::@5 x
int size
Definition: nasl_tree.h:99
long int i_val
Definition: nasl_tree.h:104
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
int var_type
Definition: nasl_var.h:41
union st_a_nasl_var::@7 v
unsigned char * s_val
Definition: nasl_var.h:26
struct script_infos * script_infos
Definition: nasl_lex_ctxt.h:30