OpenVAS Scanner 23.32.3
plugutils.c
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Greenbone AG
2 * SPDX-FileCopyrightText: 1998-2003 Renaud Deraison
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
11
12#include "plugutils.h"
13
14#include "kb_cache.h" // for get_main_kb
15#include "network.h" // for OPENVAS_ENCAPS_IP
16#include "scan_id.h"
17#include "support.h" // for g_memdup2 workaround
18
19#include <errno.h> // for errno
20#include <gvm/base/hosts.h> // for g_vhost_t
21#include <gvm/base/networking.h> // for port_protocol_t
22#include <gvm/base/prefs.h> // for prefs_get_bool
23#include <gvm/util/mqtt.h> // for mqtt_reset
24#include <gvm/util/nvticache.h> // for nvticache_initialized
25#include <magic.h>
26#include <stdio.h> // for snprintf
27#include <stdlib.h> // for exit
28#include <string.h> // for strcmp
29#include <sys/wait.h> // for wait
30#include <unistd.h> // for fork
31
32#undef G_LOG_DOMAIN
36#define G_LOG_DOMAIN "lib misc"
37
38/* Used to allow debugging for openvas-nasl */
40
41/* Track amount of KB memory used */
42static size_t kb_usage = 0;
43
44static size_t max_kb_usage;
45
46void
48{
49 const char *usage_char;
50 int usage_int;
51 usage_char = prefs_get ("max_mem_kb");
52 if (usage_char)
53 {
54 usage_int = atoi (usage_char);
55 if (usage_int < 0)
56 {
57 max_kb_usage = 0;
58 }
59 else
60 {
61 max_kb_usage = (size_t) usage_int * 1024 * 1024;
62 }
63 }
64 else
65 max_kb_usage = 0;
66}
67
68static int
69add_kb_usage (struct script_infos *args, size_t size)
70{
71 if (max_kb_usage == 0)
72 return 0;
74 return -1;
75 kb_usage += size;
77 {
78 g_warning ("KB usage exceeded %lu MB. Unable to store any further KB "
79 "Items for script %s",
80 max_kb_usage / 1024 / 1024, args->name);
81 return -1;
82 }
83 return 0;
84}
85
86/* In case of multiple vhosts fork, this holds the value of the current vhost
87 * we're scanning.
88 */
89gvm_vhost_t *current_vhost = NULL;
90
91/* @brief: Return the currently scanned vhost. */
92const char *
94{
95 if (current_vhost != NULL)
96 return current_vhost->value;
97
98 return NULL;
99}
100
101static int plug_fork_child (kb_t);
102
103void
104plug_set_dep (struct script_infos *args, const char *depname)
105{
106 nvti_t *n = args->nvti;
107 gchar *old = nvti_dependencies (n);
108 gchar *new;
109
110 if (!depname)
111 return;
112
113 if (old)
114 {
115 new = g_strdup_printf ("%s, %s", old, depname);
116 nvti_set_dependencies (n, new);
117 g_free (new);
118 }
119 else
120 nvti_set_dependencies (n, depname);
121}
122
123static void
124host_add_port_proto (struct script_infos *args, int portnum, char *proto)
125{
126 char port_s[255];
127 snprintf (port_s, sizeof (port_s), "Ports/%s/%d", proto, portnum);
128 plug_set_key (args, port_s, ARG_INT, (void *) 1);
129}
130
136static int
137unscanned_ports_as_closed (port_protocol_t ptype)
138{
139 if (ptype == PORT_PROTOCOL_UDP)
140 return (prefs_get_bool ("unscanned_closed_udp") ? 0 : 1);
141
142 return (prefs_get_bool ("unscanned_closed") ? 0 : 1);
143}
144
148int
149kb_get_port_state_proto (kb_t kb, int portnum, char *proto)
150{
151 char port_s[255], *kbstr;
152 const char *prange = prefs_get ("port_range");
153 port_protocol_t port_type;
154 array_t *port_ranges;
155
156 if (!proto)
157 proto = "tcp";
158 if (!strcmp (proto, "udp"))
159 {
160 port_type = PORT_PROTOCOL_UDP;
161 kbstr = "Host/udp_scanned";
162 }
163 else
164 {
165 port_type = PORT_PROTOCOL_TCP;
166 kbstr = "Host/scanned";
167 }
168
169 /* Check that we actually scanned the port */
170 if (kb_item_get_int (kb, kbstr) <= 0)
171 return unscanned_ports_as_closed (port_type);
172
173 port_ranges = port_range_ranges (prange);
174 if (!port_in_port_ranges (portnum, port_type, port_ranges))
175 {
176 array_free (port_ranges);
177 return unscanned_ports_as_closed (port_type);
178 }
179 array_free (port_ranges);
180
181 /* Ok, we scanned it. What is its state ? */
182 snprintf (port_s, sizeof (port_s), "Ports/%s/%d", proto, portnum);
183 return kb_item_get_int (kb, port_s) > 0;
184}
185
186static int
187host_get_port_state_proto (struct script_infos *args, int portnum, char *proto)
188{
189 return kb_get_port_state_proto (args->key, portnum, proto);
190}
191
192int
193host_get_port_state (struct script_infos *plugdata, int portnum)
194{
195 return (host_get_port_state_proto (plugdata, portnum, "tcp"));
196}
197
198int
199host_get_port_state_udp (struct script_infos *plugdata, int portnum)
200{
201 return (host_get_port_state_proto (plugdata, portnum, "udp"));
202}
203
212static int
214{
215 GSList *vhosts = NULL;
216 kb_t host_kb = NULL;
217 struct kb_item *current_vhosts = NULL;
218
219 /* Check for duplicate vhost value in args. */
220 vhosts = args->vhosts;
221 while (vhosts)
222 {
223 gvm_vhost_t *tmp = vhosts->data;
224
225 if (!strcmp (tmp->value, hostname))
226 {
227 g_warning ("%s: Value '%s' exists already", __func__, hostname);
228 return -1;
229 }
230 vhosts = vhosts->next;
231 }
232
233 /* Check for duplicate vhost value already added by other forked child of the
234 * same plugin. */
235 host_kb = args->key;
236 current_vhosts = kb_item_get_all (host_kb, "internal/vhosts");
237 if (!current_vhosts)
238 return 0;
239
240 while (current_vhosts)
241 {
242 if (!strcmp (current_vhosts->v_str, hostname))
243 {
244 g_warning ("%s: Value '%s' exists already", __func__, hostname);
245 kb_item_free (current_vhosts);
246
247 return -1;
248 }
249 current_vhosts = current_vhosts->next;
250 }
251
252 kb_item_free (current_vhosts);
253 return 0;
254}
255
256int
257plug_add_host_fqdn (struct script_infos *args, const char *hostname,
258 const char *source)
259{
260 gvm_vhost_t *vhost;
261 char **excluded;
262
263 if (!prefs_get_bool ("expand_vhosts") || !hostname || !source)
264 return -1;
265
267 return -1;
268
269 /* Check for excluded vhost value. */
270 if (prefs_get ("exclude_hosts"))
271 {
272 char **tmp = excluded = g_strsplit (prefs_get ("exclude_hosts"), ",", 0);
273
274 while (*tmp)
275 {
276 if (!strcmp (g_strstrip (*tmp), hostname))
277 {
278 g_strfreev (excluded);
279 return -1;
280 }
281 tmp++;
282 }
283 g_strfreev (excluded);
284 }
285 vhost = gvm_vhost_new (g_strdup (hostname), g_strdup (source));
286 args->vhosts = g_slist_prepend (args->vhosts, vhost);
287 return 0;
288}
289
290char *
292{
293 GSList *vhosts = args->vhosts;
294
295 if (!args->vhosts)
296 return addr6_as_str (args->ip);
297
298 /* Workaround for rapid growth of forked processes ie. http_get() calls
299 * within foreach() loops. */
300 if (current_vhost)
301 return g_strdup (current_vhost->value);
302 while (vhosts)
303 {
304 int ret = plug_fork_child (args->key);
305
306 if (ret == 0)
307 {
308 current_vhost = vhosts->data;
309 return g_strdup (current_vhost->value);
310 }
311 else if (ret == -1)
312 return NULL;
313 vhosts = vhosts->next;
314 }
315
316 // Allow to return to the main process if parent process is openvas-nasl.
317 // So, the main process can do e.g. a kb clean up
318 if (args->standalone)
319 return NULL;
320
321 _exit (0);
322}
323
324GSList *
326{
327 GSList *results = NULL, *vhosts = args->vhosts;
328
329 if (!args->vhosts)
330 results = g_slist_prepend (results, addr6_as_str (args->ip));
331
332 while (vhosts)
333 {
334 gvm_vhost_t *vhost = vhosts->data;
335
336 results = g_slist_prepend (results, g_strdup (vhost->value));
337 vhosts = vhosts->next;
338 }
339 return results;
340}
341
342char *
343plug_get_host_source (struct script_infos *args, const char *hostname)
344{
345 if (!args->vhosts)
346 return g_strdup ("IP-address");
347
348 if (hostname)
349 {
350 GSList *vhosts = args->vhosts;
351
352 /* Search for source of specified hostname/vhost. */
353 while (vhosts)
354 {
355 gvm_vhost_t *vhost = vhosts->data;
356
357 if (!strcmp (vhost->value, hostname))
358 return g_strdup (vhost->source);
359 vhosts = vhosts->next;
360 }
361 return NULL;
362 }
363 /* Call plug_get_host_fqdn() to set current_vhost (and fork, in case of
364 * multiple vhosts.) */
365 if (!current_vhost)
366 g_free (plug_get_host_fqdn (args));
367 return g_strdup (current_vhost->source);
368}
369
370struct in6_addr *
372{
373 return args->ip;
374}
375
376char *
378{
379 return addr6_as_str (plug_get_host_ip (desc));
380}
381
389static const char *
391{
392 gchar *type_str;
393
394 switch (type)
395 {
396 case ERRMSG:
397 type_str = "ERRMSG";
398 break;
399 case HOST_START:
400 type_str = "HOST_START";
401 break;
402 case HOST_END:
403 type_str = "HOST_END";
404 break;
405 case LOG:
406 type_str = "LOG";
407 break;
408 case HOST_DETAIL:
409 type_str = "HOST_DETAIL";
410 break;
411 case ALARM:
412 type_str = "ALARM";
413 break;
414 case DEADHOST:
415 type_str = "DEADHOST";
416 break;
417 default:
418 return NULL;
419 break;
420 }
421
422 return type_str;
423}
424
441int
443{
444 const char *original_scan_id;
445 char *current_scan_id;
446
447 original_scan_id = get_scan_id ();
448 if (original_scan_id == NULL)
449 return -1;
450 current_scan_id = kb_item_get_str (main_kb, ("internal/scanid"));
451 if (current_scan_id == NULL)
452 return -2;
453
454 if (!g_strcmp0 (original_scan_id, current_scan_id))
455 {
456 g_free (current_scan_id);
457 return 0;
458 }
459
460 g_warning ("KB inconsitency. %s writing into %s KB", original_scan_id,
461 current_scan_id);
462 g_free (current_scan_id);
463 return -3;
464}
465
480static int
482{
483 char *current_scan_id;
484 kb_t kb = get_main_kb ();
485 int result = check_kb_inconsistency (kb);
486 switch (result)
487 {
488 case -3:
489 current_scan_id = kb_item_get_str (kb, ("internal/scanid"));
490 g_warning (
491 "%s: scan_id (%s) does not match global scan_id (%s); abort to "
492 "prevent data corruption",
493 __func__, current_scan_id, get_scan_id ());
494 g_free (current_scan_id);
495 _exit (1);
496 break;
497 case -1:
498 // a call without global scan id can happen in e.g. nasl-lint or
499 // openvas-nasl calls
500 break;
501 case -2:
502 g_warning (
503 "%s: No internal/scanid found; abort to prevent data corruption.",
504 __func__);
505 _exit (1);
506 break;
507 default:
508 {
509 // nothing
510 }
511 }
512 return 0;
513}
514
532int
534 const char *value)
535{
536 int result = check_kb_inconsistency_log ();
537 return result == 0 ? kb_item_push_str (kb, name, value) : -1;
538}
539
557int
559 const char *value, size_t len)
560{
561 int result = check_kb_inconsistency_log ();
562 return result == 0 ? kb_item_set_str (kb, name, value, len) : -1;
563}
564
582int
584 const char *value, size_t len,
585 int pos)
586{
587 int result = check_kb_inconsistency_log ();
588 return result == 0 ? kb_item_add_str_unique (kb, name, value, len, pos) : -1;
589}
590
608int
609kb_item_set_int_with_main_kb_check (kb_t kb, const char *name, int value)
610{
611 int result = check_kb_inconsistency_log ();
612 return result == 0 ? kb_item_set_int (kb, name, value) : -1;
613}
614
632int
633kb_item_add_int_with_main_kb_check (kb_t kb, const char *name, int value)
634{
635 int result = check_kb_inconsistency_log ();
636 return result == 0 ? kb_item_add_int (kb, name, value) : -1;
637}
638
656int
657kb_item_add_int_unique_with_main_kb_check (kb_t kb, const char *name, int value)
658{
659 int result = check_kb_inconsistency_log ();
660 return result == 0 ? kb_item_add_int_unique (kb, name, value) : -1;
661}
662
663static int
664is_utf8_encoded (const char *filename)
665{
666 magic_t magic_cookie = magic_open (MAGIC_MIME_ENCODING);
667 if (!magic_cookie)
668 {
669 g_warning ("%s: It is not possible initialize magic db", __func__);
670 return -1;
671 }
672 if (magic_load (magic_cookie, NULL) != 0)
673 {
674 g_warning ("%s: It was not possible to load the default magic db",
675 __func__);
676 return -1;
677 }
678 const char *file_encoding = magic_file (magic_cookie, filename);
679 if (!file_encoding)
680 {
681 g_warning ("%s: It was not possible to identify the file encoding for %s",
682 __func__, filename);
683 return -1;
684 }
685
686 if (g_strstr_len (file_encoding, strlen (file_encoding), "utf-8"))
687 return 1;
688
689 return 0;
690}
691
703static void
704proto_post_wrapped (const char *oid, struct script_infos *desc, int port,
705 const char *proto, const char *action, msg_t msg_type,
706 const char *uri)
707{
708 const char *hostname = "";
709 char *buffer, *data, port_s[16] = "general";
710 char ip_str[INET6_ADDRSTRLEN];
711 GError *err = NULL;
712 GString *action_str;
713 gsize length;
714
715 /* Should not happen, just to avoid trouble stop here if no NVTI found */
716 if (!oid)
717 return;
718
719 if (action == NULL)
720 action_str = g_string_new ("");
721 else
722 {
723 action_str = g_string_new (action);
724 g_string_append (action_str, "\n");
725 }
726
727 if (port > 0)
728 snprintf (port_s, sizeof (port_s), "%d", port);
729 if (current_vhost)
730 hostname = current_vhost->value;
731 else if (desc->vhosts)
732 hostname = ((gvm_vhost_t *) desc->vhosts->data)->value;
733 addr6_to_str (plug_get_host_ip (desc), ip_str);
734 buffer = g_strdup_printf ("%s|||%s|||%s|||%s/%s|||%s|||%s|||%s",
735 msg_type_to_str (msg_type), ip_str,
736 hostname ? hostname : " ", port_s, proto, oid,
737 action_str->str, uri ? uri : "");
738
739 /* Convert to UTF-8 before sending to Manager only if necessary. */
740 if (is_utf8_encoded (desc->name) > 0)
741 data = g_strdup (buffer);
742 else
743 data = g_convert (buffer, -1, "UTF-8", "ISO_8859-1", NULL, &length, &err);
744
745 if (!data)
746 {
747 g_warning ("%s: Error converting to UTF-8: %s\nOriginal string: %s",
748 __func__, err->message, buffer);
749 g_free (buffer);
750 g_string_free (action_str, TRUE);
751 return;
752 }
753
754 kb_item_push_str_with_main_kb_check (get_main_kb (), "internal/results",
755 data);
756 g_free (data);
757 g_free (buffer);
758 g_string_free (action_str, TRUE);
759}
760
761void
762proto_post_alarm (const char *oid, struct script_infos *desc, int port,
763 const char *proto, const char *action, const char *uri)
764{
765 proto_post_wrapped (oid, desc, port, proto, action, ALARM, uri);
766}
767
768void
769post_alarm (const char *oid, struct script_infos *desc, int port,
770 const char *action, const char *uri)
771{
772 proto_post_alarm (oid, desc, port, "tcp", action, uri);
773}
774
778void
779proto_post_log (const char *oid, struct script_infos *desc, int port,
780 const char *proto, const char *action, const char *uri)
781{
782 proto_post_wrapped (oid, desc, port, proto, action, LOG, uri);
783}
784
788void
789post_log (const char *oid, struct script_infos *desc, int port,
790 const char *action)
791{
792 proto_post_log (oid, desc, port, "tcp", action, NULL);
793}
794
798void
799post_log_with_uri (const char *oid, struct script_infos *desc, int port,
800 const char *action, const char *uri)
801{
802 proto_post_log (oid, desc, port, "tcp", action, uri);
803}
804
805void
806proto_post_error (const char *oid, struct script_infos *desc, int port,
807 const char *proto, const char *action, const char *uri)
808{
809 proto_post_wrapped (oid, desc, port, proto, action, ERRMSG, uri);
810}
811
812void
813post_error (const char *oid, struct script_infos *desc, int port,
814 const char *action, const char *uri)
815{
816 proto_post_error (oid, desc, port, "tcp", action, uri);
817}
818
831char *
832get_plugin_preference (const char *oid, const char *name, int pref_id)
833{
834 GHashTable *prefs;
835 GHashTableIter iter;
836 char *cname = NULL, *retval = NULL;
837 void *itername, *itervalue;
838 char prefix[1024], suffix[1024];
839
840 prefs = preferences_get ();
841 if (!prefs || !nvticache_initialized () || !oid || (!name && pref_id < 0))
842 return NULL;
843
844 g_hash_table_iter_init (&iter, prefs);
845
846 if (pref_id >= 0)
847 {
848 snprintf (prefix, sizeof (prefix), "%s:%d:", oid, pref_id);
849 while (g_hash_table_iter_next (&iter, &itername, &itervalue))
850 {
851 if (g_str_has_prefix (itername, prefix))
852 {
853 retval = g_strdup (itervalue);
854 break;
855 }
856 }
857 }
858 else
859 {
860 cname = g_strdup (name);
861 g_strchomp (cname);
862 snprintf (prefix, sizeof (prefix), "%s:", oid);
863 snprintf (suffix, sizeof (suffix), ":%s", cname);
864 /* NVT preferences received in OID:PrefID:PrefType:PrefName form */
865 while (g_hash_table_iter_next (&iter, &itername, &itervalue))
866 {
867 if (g_str_has_prefix (itername, prefix)
868 && g_str_has_suffix (itername, suffix))
869 {
870 retval = g_strdup (itervalue);
871 break;
872 }
873 }
874 }
875
876 /* If no value set by the user, get the default one. */
877 if (!retval)
878 {
879 GSList *nprefs, *tmp;
880
881 tmp = nprefs = nvticache_get_prefs (oid);
882 while (tmp)
883 {
884 if ((cname && !strcmp (cname, nvtpref_name (tmp->data)))
885 || (pref_id >= 0 && pref_id == nvtpref_id (tmp->data)))
886 {
887 if (!strcmp (nvtpref_type (tmp->data), "radio"))
888 {
889 char **opts =
890 g_strsplit (nvtpref_default (tmp->data), ";", -1);
891
892 retval = g_strdup (opts[0]);
893 g_strfreev (opts);
894 }
895 else
896 retval = g_strdup (nvtpref_default (tmp->data));
897
898 break;
899 }
900 tmp = tmp->next;
901 }
902 g_slist_free_full (nprefs, (void (*) (void *)) nvtpref_free);
903 }
904 if (cname)
905 g_free (cname);
906 return retval;
907}
908
919const char *
920get_plugin_preference_fname (struct script_infos *desc, const char *filename)
921{
922 const char *content;
923 long contentsize = 0;
924 gint tmpfile;
925 gchar *tmpfilename;
926 GError *error = NULL;
927
928 content = get_plugin_preference_file_content (desc, filename);
929 if (content == NULL)
930 {
931 return NULL;
932 }
933 contentsize = get_plugin_preference_file_size (desc, filename);
934 if (contentsize <= 0)
935 return NULL;
936
937 tmpfile =
938 g_file_open_tmp ("openvas-file-upload.XXXXXX", &tmpfilename, &error);
939 if (tmpfile == -1)
940 {
941 g_message ("get_plugin_preference_fname: Could not open temporary"
942 " file for %s: %s",
943 filename, error->message);
944 g_error_free (error);
945 return NULL;
946 }
947 close (tmpfile);
948
949 if (!g_file_set_contents (tmpfilename, content, contentsize, &error))
950 {
951 g_message ("get_plugin_preference_fname: could set contents of"
952 " temporary file for %s: %s",
953 filename, error->message);
954 g_error_free (error);
955 return NULL;
956 }
957
958 return tmpfilename;
959}
960
974char *
976 const char *identifier)
977{
978 struct scan_globals *globals = desc->globals;
979 GHashTable *trans;
980
981 if (!globals)
982 return NULL;
983
984 trans = globals->files_translation;
985 if (!trans)
986 return NULL;
987
988 return g_hash_table_lookup (trans, identifier);
989}
990
1005long
1007 const char *identifier)
1008{
1009 struct scan_globals *globals = desc->globals;
1010 GHashTable *trans;
1011 gchar *filesize_str;
1012
1013 if (!globals)
1014 return -1;
1015
1016 trans = globals->files_size_translation;
1017 if (!trans)
1018 return -1;
1019
1020 filesize_str = g_hash_table_lookup (trans, identifier);
1021 if (filesize_str == NULL)
1022 return -1;
1023
1024 return atol (filesize_str);
1025}
1026
1027void
1028plug_set_key_len (struct script_infos *args, char *name, int type,
1029 const void *value, size_t len)
1030{
1031 kb_t kb = plug_get_kb (args);
1032 int pos = 0; // Append the item on the right position of the list
1033
1034 if (name == NULL || value == NULL)
1035 return;
1036
1037 if (type == ARG_STRING)
1038 {
1039 if (add_kb_usage (args, len) == -1)
1040 return;
1041 kb_item_add_str_unique (kb, name, value, len, pos);
1042 }
1043 else if (type == ARG_INT)
1044 kb_item_add_int_unique (kb, name, GPOINTER_TO_SIZE (value));
1045 if (global_nasl_debug == 1)
1046 {
1047 if (type == ARG_STRING)
1048 g_message ("set key %s -> %s", name, (char *) value);
1049 else if (type == ARG_INT)
1050 g_message ("set key %s -> %d", name, (int) GPOINTER_TO_SIZE (value));
1051 }
1052}
1053
1054void
1055plug_set_key (struct script_infos *args, char *name, int type,
1056 const void *value)
1057{
1058 plug_set_key_len (args, name, type, value, 0);
1059}
1060
1071void
1072plug_set_key_len_volatile (struct script_infos *args, char *name, int type,
1073 const void *value, int expire, size_t len)
1074{
1075 kb_t kb = plug_get_kb (args);
1076 int pos = 0; // Append the item on the right position of the list
1077
1078 if (name == NULL || value == NULL || expire == -1)
1079 return;
1080
1081 if (type == ARG_STRING)
1082 kb_add_str_unique_volatile (kb, name, value, expire, len, pos);
1083 else if (type == ARG_INT)
1084 kb_add_int_unique_volatile (kb, name, GPOINTER_TO_SIZE (value),
1085 GPOINTER_TO_SIZE (expire));
1086 if (global_nasl_debug == 1)
1087 {
1088 if (type == ARG_STRING)
1089 g_message ("set volatile key %s -> %s", name, (char *) value);
1090 else if (type == ARG_INT)
1091 g_message ("set volatile key %s -> %d", name,
1092 (int) GPOINTER_TO_SIZE (value));
1093 }
1094}
1095
1105void
1106plug_set_key_volatile (struct script_infos *args, char *name, int type,
1107 const void *value, int expire)
1108{
1109 plug_set_key_len_volatile (args, name, type, value, expire, 0);
1110}
1111
1112void
1113plug_replace_key_len (struct script_infos *args, char *name, int type,
1114 void *value, size_t len)
1115{
1116 kb_t kb = plug_get_kb (args);
1117
1118 if (name == NULL || value == NULL)
1119 return;
1120
1121 if (type == ARG_STRING)
1122 {
1123 if (add_kb_usage (args, len) == -1)
1124 return;
1125 kb_item_set_str (kb, name, value, len);
1126 }
1127 else if (type == ARG_INT)
1128 kb_item_set_int (kb, name, GPOINTER_TO_SIZE (value));
1129 if (global_nasl_debug == 1)
1130 {
1131 if (type == ARG_STRING)
1132 g_message ("replace key %s -> %s", name, (char *) value);
1133 else if (type == ARG_INT)
1134 g_message ("replace key %s -> %d", name,
1135 (int) GPOINTER_TO_SIZE (value));
1136 }
1137}
1138
1139void
1140plug_replace_key (struct script_infos *args, char *name, int type, void *value)
1141{
1142 plug_replace_key_len (args, name, type, value, 0);
1143}
1144
1145void
1146scanner_add_port (struct script_infos *args, int port, char *proto)
1147{
1148 host_add_port_proto (args, port, proto);
1149}
1150
1151kb_t
1153{
1154 return args->key;
1155}
1156
1157static void
1159{
1160 int status;
1161 (void) s;
1162
1163 wait (&status);
1164}
1165
1166static void
1167sig_n (int signo, void (*fnc) (int))
1168{
1169 struct sigaction sa;
1170
1171 sa.sa_handler = fnc;
1172 sa.sa_flags = 0;
1173 sigemptyset (&sa.sa_mask);
1174 sigaction (signo, &sa, (struct sigaction *) 0);
1175}
1176
1185static int
1187{
1188 pid_t pid;
1189
1190 // TODO change forking to official channels
1191 if ((pid = fork ()) == 0)
1192 {
1193 sig_n (SIGTERM, _exit);
1194 mqtt_reset ();
1195 kb_lnk_reset (kb);
1196 kb_lnk_reset (get_main_kb ());
1197 nvticache_reset ();
1198 srand48 (getpid () + getppid () + time (NULL));
1199 return 0;
1200 }
1201 else if (pid < 0)
1202 {
1203 g_warning ("%s(): fork() failed (%s)", __func__, strerror (errno));
1204 return -1;
1205 }
1206 else
1207 // the parent waits for the spawned process to finish to prevent DDOS on a
1208 // host when multiple vhosts got spawned
1209 waitpid (pid, NULL, 0);
1210 return 1;
1211}
1212
1225void *
1226plug_get_key (struct script_infos *args, char *name, int *type, size_t *len,
1227 int single)
1228{
1229 kb_t kb = args->key;
1230 struct kb_item *res = NULL, *res_list;
1231
1232 if (type != NULL && *type != KB_TYPE_INT)
1233 *type = -1;
1234
1235 if (kb == NULL)
1236 return NULL;
1237
1238 if (single && type != NULL && *type != KB_TYPE_INT)
1239 res = kb_item_get_single (kb, name, KB_TYPE_UNSPEC);
1240 else if (type != NULL && *type == KB_TYPE_INT)
1241 res = kb_item_get_single (kb, name, KB_TYPE_INT);
1242 else
1243 res = kb_item_get_all (kb, name);
1244
1245 if (res == NULL)
1246 return NULL;
1247
1248 if (!res->next) /* No fork - good */
1249 {
1250 void *ret;
1251 if (res->type == KB_TYPE_INT)
1252 {
1253 if (type != NULL)
1254 *type = KB_TYPE_INT;
1255 ret = g_memdup2 (&res->v_int, sizeof (res->v_int));
1256 }
1257 else
1258 {
1259 if (type != NULL)
1260 *type = KB_TYPE_STR;
1261 if (len)
1262 *len = res->len;
1263
1264 ret = g_malloc0 (res->len + 1);
1265 memcpy (ret, res->v_str, res->len + 1);
1266 }
1267 kb_item_free (res);
1268 return ret;
1269 }
1270
1271 /* More than one value - we will fork() then */
1272 sig_n (SIGCHLD, plug_get_key_sigchld);
1273 res_list = res;
1274 while (res)
1275 {
1276 int pret = plug_fork_child (kb);
1277
1278 if (pret == 0)
1279 {
1280 /* Forked child. */
1281 void *ret;
1282
1283 if (res->type == KB_TYPE_INT)
1284 {
1285 if (type != NULL)
1286 *type = KB_TYPE_INT;
1287 ret = g_memdup2 (&res->v_int, sizeof (res->v_int));
1288 }
1289 else
1290 {
1291 if (type != NULL)
1292 *type = KB_TYPE_STR;
1293 if (len)
1294 *len = res->len;
1295
1296 ret = g_malloc0 (res->len + 1);
1297 memcpy (ret, res->v_str, res->len + 1);
1298 }
1299 kb_item_free (res_list);
1300 return ret;
1301 }
1302 else if (pret == -1)
1303 return NULL;
1304 res = res->next;
1305 }
1306 kb_item_free (res_list);
1307
1308 // Allow to return to the main process if parent process is openvas-nasl.
1309 // So, the main process can do e.g. a kb clean up
1310 if (args->standalone)
1311 return NULL;
1312
1313 _exit (0);
1314}
1315
1322unsigned int
1324{
1325 kb_t kb = plug_get_kb (desc);
1326 struct kb_item *res, *k;
1327 int open21 = 0, open80 = 0;
1328#define MAX_CANDIDATES 16
1329 u_short candidates[MAX_CANDIDATES];
1330 int num_candidates = 0;
1331
1332 k = res = kb_item_get_pattern (kb, "Ports/tcp/*");
1333 if (res == NULL)
1334 return 0;
1335 else
1336 {
1337 int ret;
1338 char *s;
1339
1340 for (;;)
1341 {
1342 s = res->name + sizeof ("Ports/tcp/") - 1;
1343 ret = atoi (s);
1344 if (ret == 21)
1345 open21 = 1;
1346 else if (ret == 80)
1347 open80 = 1;
1348 else
1349 {
1350 candidates[num_candidates++] = ret;
1351 if (num_candidates >= MAX_CANDIDATES)
1352 break;
1353 }
1354 res = res->next;
1355 if (res == NULL)
1356 break;
1357 }
1358
1359 kb_item_free (k);
1360 if (num_candidates != 0)
1361 return candidates[lrand48 () % num_candidates];
1362 else if (open21)
1363 return 21;
1364 else if (open80)
1365 return 80;
1366 }
1367
1368 /* Not reachable */
1369 return 0;
1370}
1371
1376
1377void
1378plug_set_port_transport (struct script_infos *args, int port, int tr)
1379{
1380 char s[256];
1381
1382 snprintf (s, sizeof (s), "Transports/TCP/%d", port);
1383 plug_set_key (args, s, ARG_INT, GSIZE_TO_POINTER (tr));
1384}
1385
1386/* Return the transport encapsulation mode (OPENVAS_ENCAPS_*) for the
1387 given PORT. If no such encapsulation mode has been stored in the
1388 knowledge base (or its value is < 0), OPENVAS_ENCAPS_IP is
1389 currently returned. */
1390int
1392{
1393 char s[256];
1394 int trp;
1395
1396 snprintf (s, sizeof (s), "Transports/TCP/%d", port);
1397 trp = kb_item_get_int (plug_get_kb (args), s);
1398 if (trp >= 0)
1399 return trp;
1400 else
1401 return OPENVAS_ENCAPS_IP; /* Change this to 0 for ultra smart SSL
1402 negotiation, at the expense of possibly
1403 breaking stuff */
1404}
1405
1406static void
1407plug_set_ssl_item (struct script_infos *args, char *item, char *itemfname)
1408{
1409 char s[256];
1410 snprintf (s, sizeof (s), "SSL/%s", item);
1411 plug_set_key (args, s, ARG_STRING, itemfname);
1412}
1413
1414void
1415plug_set_ssl_cert (struct script_infos *args, char *cert)
1416{
1417 plug_set_ssl_item (args, "cert", cert);
1418}
1419
1420void
1421plug_set_ssl_key (struct script_infos *args, char *key)
1422{
1423 plug_set_ssl_item (args, "key", key);
1424}
1425
1426void
1428{
1429 plug_set_ssl_item (args, "password", key);
1430}
1431
1436void
1437plug_set_ssl_CA_file (struct script_infos *args, char *key)
1438{
1439 plug_set_ssl_item (args, "CA", key);
1440}
static kb_t host_kb
Definition attack.c:290
kb_t get_main_kb(void)
gets the main_kb. @description returns the previously set main_kb; when asserts are enabled it will a...
Definition kb_cache.c:41
kb_t main_kb
Definition kb_cache.c:15
Header file to cache main_kb.
const char * oid
static pid_t pid
const char * name
Definition nasl_init.c:439
uint8_t len
u_short length
static void prefix(int n, int i)
Definition nasl_tree.c:219
Header file for module network.
@ OPENVAS_ENCAPS_IP
Definition network.h:31
const char * hostname
void plug_replace_key_len(struct script_infos *args, char *name, int type, void *value, size_t len)
Definition plugutils.c:1113
int kb_item_set_str_with_main_kb_check(kb_t kb, const char *name, const char *value, size_t len)
Check if the current kb corresponds to the original scanid, if it matches it call kb_item_set_str....
Definition plugutils.c:558
static size_t max_kb_usage
Definition plugutils.c:44
static int check_duplicated_vhost(struct script_infos *args, const char *hostname)
Check for duplicated vhosts before inserting a new one.
Definition plugutils.c:213
void post_alarm(const char *oid, struct script_infos *desc, int port, const char *action, const char *uri)
Definition plugutils.c:769
void plug_set_key_volatile(struct script_infos *args, char *name, int type, const void *value, int expire)
Set volatile key with expire.
Definition plugutils.c:1106
int host_get_port_state_udp(struct script_infos *plugdata, int portnum)
Definition plugutils.c:199
void init_kb_usage(void)
Definition plugutils.c:47
static int host_get_port_state_proto(struct script_infos *args, int portnum, char *proto)
Definition plugutils.c:187
int host_get_port_state(struct script_infos *plugdata, int portnum)
Definition plugutils.c:193
static int plug_fork_child(kb_t)
Spawns a new child process. Setups everything that is needed for a new process. Child must be handled...
Definition plugutils.c:1186
void scanner_add_port(struct script_infos *args, int port, char *proto)
Definition plugutils.c:1146
void proto_post_alarm(const char *oid, struct script_infos *desc, int port, const char *proto, const char *action, const char *uri)
Definition plugutils.c:762
int global_nasl_debug
Definition plugutils.c:39
void plug_set_ssl_cert(struct script_infos *args, char *cert)
Definition plugutils.c:1415
void plug_set_ssl_CA_file(struct script_infos *args, char *key)
Definition plugutils.c:1437
void plug_set_key_len(struct script_infos *args, char *name, int type, const void *value, size_t len)
Definition plugutils.c:1028
void plug_set_dep(struct script_infos *args, const char *depname)
Definition plugutils.c:104
static int check_kb_inconsistency_log(void)
calls check_kb_inconsistency and logs as debug when local scan_id is missing.
Definition plugutils.c:481
#define MAX_CANDIDATES
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
void proto_post_error(const char *oid, struct script_infos *desc, int port, const char *proto, const char *action, const char *uri)
Definition plugutils.c:806
int kb_get_port_state_proto(kb_t kb, int portnum, char *proto)
Definition plugutils.c:149
int kb_item_add_str_unique_with_main_kb_check(kb_t kb, const char *name, const char *value, size_t len, int pos)
Check if the current kb corresponds to the original scanid, if it matches it call kb_item_add_str_uni...
Definition plugutils.c:583
static size_t kb_usage
Definition plugutils.c:42
void proto_post_log(const char *oid, struct script_infos *desc, int port, const char *proto, const char *action, const char *uri)
Post a log message.
Definition plugutils.c:779
void plug_set_key_len_volatile(struct script_infos *args, char *name, int type, const void *value, int expire, size_t len)
Set volatile key with expire.
Definition plugutils.c:1072
const char * plug_current_vhost(void)
Definition plugutils.c:93
static const char * msg_type_to_str(msg_t type)
Return string representation of the given msg_t.
Definition plugutils.c:390
void plug_set_port_transport(struct script_infos *args, int port, int tr)
Definition plugutils.c:1378
static void host_add_port_proto(struct script_infos *args, int portnum, char *proto)
Definition plugutils.c:124
GSList * plug_get_host_fqdn_list(struct script_infos *args)
Definition plugutils.c:325
void plug_replace_key(struct script_infos *args, char *name, int type, void *value)
Definition plugutils.c:1140
char * plug_get_host_source(struct script_infos *args, const char *hostname)
Definition plugutils.c:343
int kb_item_add_int_with_main_kb_check(kb_t kb, const char *name, int value)
Check if the current kb corresponds to the original scanid, if it matches it call kb_item_add_int....
Definition plugutils.c:633
char * plug_get_host_fqdn(struct script_infos *args)
Definition plugutils.c:291
static void plug_set_ssl_item(struct script_infos *args, char *item, char *itemfname)
Definition plugutils.c:1407
static int is_utf8_encoded(const char *filename)
Definition plugutils.c:664
char * get_plugin_preference(const char *oid, const char *name, int pref_id)
Get the a plugins preference.
Definition plugutils.c:832
int kb_item_push_str_with_main_kb_check(kb_t kb, const char *name, const char *value)
Check if the current kb corresponds to the original scanid, if it matches it kb_item_push_str....
Definition plugutils.c:533
static int unscanned_ports_as_closed(port_protocol_t ptype)
Report state of preferences "unscanned_closed".
Definition plugutils.c:137
static void sig_n(int signo, void(*fnc)(int))
Definition plugutils.c:1167
int kb_item_add_int_unique_with_main_kb_check(kb_t kb, const char *name, int value)
Check if the current kb corresponds to the original scanid, if it matches it call kb_item_add_int_uni...
Definition plugutils.c:657
static void proto_post_wrapped(const char *oid, struct script_infos *desc, int port, const char *proto, const char *action, msg_t msg_type, const char *uri)
Post a security message (e.g. LOG, NOTE, WARNING ...).
Definition plugutils.c:704
void post_error(const char *oid, struct script_infos *desc, int port, const char *action, const char *uri)
Definition plugutils.c:813
char * plug_get_host_ip_str(struct script_infos *desc)
Definition plugutils.c:377
void plug_set_ssl_key(struct script_infos *args, char *key)
Definition plugutils.c:1421
void plug_set_key(struct script_infos *args, char *name, int type, const void *value)
Definition plugutils.c:1055
int plug_add_host_fqdn(struct script_infos *args, const char *hostname, const char *source)
Definition plugutils.c:257
void * plug_get_key(struct script_infos *args, char *name, int *type, size_t *len, int single)
Get values from a kb under the given key name.
Definition plugutils.c:1226
static int add_kb_usage(struct script_infos *args, size_t size)
Definition plugutils.c:69
const char * get_plugin_preference_fname(struct script_infos *desc, const char *filename)
Get the file name of a plugins preference that is of type "file".
Definition plugutils.c:920
int check_kb_inconsistency(kb_t main_kb)
Check if the current main kb corresponds to the original scan main kb. @description Compares the scan...
Definition plugutils.c:442
struct in6_addr * plug_get_host_ip(struct script_infos *args)
Definition plugutils.c:371
gvm_vhost_t * current_vhost
Definition plugutils.c:89
int kb_item_set_int_with_main_kb_check(kb_t kb, const char *name, int value)
Check if the current kb corresponds to the original scanid, if it matches it call kb_item_set_int....
Definition plugutils.c:609
void plug_set_ssl_pem_password(struct script_infos *args, char *key)
Definition plugutils.c:1427
static void plug_get_key_sigchld(int s)
Definition plugutils.c:1158
void post_log_with_uri(const char *oid, struct script_infos *desc, int port, const char *action, const char *uri)
Post a log message about a tcp port with a uri.
Definition plugutils.c:799
char * get_plugin_preference_file_content(struct script_infos *desc, const char *identifier)
Get the file contents of a plugins preference that is of type "file".
Definition plugutils.c:975
void post_log(const char *oid, struct script_infos *desc, int port, const char *action)
Post a log message about a tcp port.
Definition plugutils.c:789
kb_t plug_get_kb(struct script_infos *args)
Definition plugutils.c:1152
long get_plugin_preference_file_size(struct script_infos *desc, const char *identifier)
Get the file size of a plugins preference that is of type "file".
Definition plugutils.c:1006
Header file for module plugutils.
#define ARG_STRING
Definition plugutils.h:19
#define ARG_INT
Definition plugutils.h:20
msg_t
Definition plugutils.h:79
@ HOST_DETAIL
Definition plugutils.h:84
@ HOST_END
Definition plugutils.h:82
@ ALARM
Definition plugutils.h:85
@ DEADHOST
Definition plugutils.h:86
@ LOG
Definition plugutils.h:83
@ ERRMSG
Definition plugutils.h:80
@ HOST_START
Definition plugutils.h:81
const char * get_scan_id()
Definition scan_id.c:22
GHashTable * files_size_translation
Definition scanneraux.h:21
GHashTable * files_translation
Definition scanneraux.h:20
nvti_t * nvti
Definition scanneraux.h:33
struct scan_globals * globals
Definition scanneraux.h:30
GSList * vhosts
Definition scanneraux.h:38
struct in6_addr * ip
Definition scanneraux.h:37
char * name
Definition scanneraux.h:35
Support macros for special platforms.