OpenVAS Scanner 23.40.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 int ret = 0;
668 if (!magic_cookie)
669 {
670 g_warning ("%s: It is not possible initialize magic db", __func__);
671 return -1;
672 }
673 if (magic_load (magic_cookie, NULL) != 0)
674 {
675 g_warning ("%s: It was not possible to load the default magic db",
676 __func__);
677 ret = -1;
678 goto magic_ret;
679 }
680 const char *file_encoding = magic_file (magic_cookie, filename);
681 if (!file_encoding)
682 {
683 g_warning ("%s: It was not possible to identify the file encoding for %s",
684 __func__, filename);
685 ret = -1;
686 goto magic_ret;
687 }
688
689 if (g_strstr_len (file_encoding, strlen (file_encoding), "utf-8"))
690 ret = 1;
691
692 magic_ret:
693 magic_close (magic_cookie);
694 return ret;
695}
696
708static void
709proto_post_wrapped (const char *oid, struct script_infos *desc, int port,
710 const char *proto, const char *action, msg_t msg_type,
711 const char *uri)
712{
713 const char *hostname = "";
714 char *buffer, *data, port_s[16] = "general";
715 char ip_str[INET6_ADDRSTRLEN];
716 GError *err = NULL;
717 GString *action_str;
718 gsize length;
719
720 /* Should not happen, just to avoid trouble stop here if no NVTI found */
721 if (!oid)
722 return;
723
724 if (action == NULL)
725 action_str = g_string_new ("");
726 else
727 {
728 action_str = g_string_new (action);
729 g_string_append (action_str, "\n");
730 }
731
732 if (port > 0)
733 snprintf (port_s, sizeof (port_s), "%d", port);
734 if (current_vhost)
735 hostname = current_vhost->value;
736 else if (desc->vhosts)
737 hostname = ((gvm_vhost_t *) desc->vhosts->data)->value;
738 addr6_to_str (plug_get_host_ip (desc), ip_str);
739 buffer = g_strdup_printf ("%s|||%s|||%s|||%s/%s|||%s|||%s|||%s",
740 msg_type_to_str (msg_type), ip_str,
741 hostname ? hostname : " ", port_s, proto, oid,
742 action_str->str, uri ? uri : "");
743
744 /* Convert to UTF-8 before sending to Manager only if necessary. */
745 if (is_utf8_encoded (desc->name) > 0)
746 data = g_strdup (buffer);
747 else
748 data = g_convert (buffer, -1, "UTF-8", "ISO_8859-1", NULL, &length, &err);
749
750 if (!data)
751 {
752 g_warning ("%s: Error converting to UTF-8: %s\nOriginal string: %s",
753 __func__, err ? err->message: "", buffer);
754 g_free (buffer);
755 g_string_free (action_str, TRUE);
756 return;
757 }
758
759 kb_item_push_str_with_main_kb_check (get_main_kb (), "internal/results",
760 data);
761 g_free (data);
762 g_free (buffer);
763 g_string_free (action_str, TRUE);
764}
765
766void
767proto_post_alarm (const char *oid, struct script_infos *desc, int port,
768 const char *proto, const char *action, const char *uri)
769{
770 proto_post_wrapped (oid, desc, port, proto, action, ALARM, uri);
771}
772
773void
774post_alarm (const char *oid, struct script_infos *desc, int port,
775 const char *action, const char *uri)
776{
777 proto_post_alarm (oid, desc, port, "tcp", action, uri);
778}
779
783void
784proto_post_log (const char *oid, struct script_infos *desc, int port,
785 const char *proto, const char *action, const char *uri)
786{
787 proto_post_wrapped (oid, desc, port, proto, action, LOG, uri);
788}
789
793void
794post_log (const char *oid, struct script_infos *desc, int port,
795 const char *action)
796{
797 proto_post_log (oid, desc, port, "tcp", action, NULL);
798}
799
803void
804post_log_with_uri (const char *oid, struct script_infos *desc, int port,
805 const char *action, const char *uri)
806{
807 proto_post_log (oid, desc, port, "tcp", action, uri);
808}
809
810void
811proto_post_error (const char *oid, struct script_infos *desc, int port,
812 const char *proto, const char *action, const char *uri)
813{
814 proto_post_wrapped (oid, desc, port, proto, action, ERRMSG, uri);
815}
816
817void
818post_error (const char *oid, struct script_infos *desc, int port,
819 const char *action, const char *uri)
820{
821 proto_post_error (oid, desc, port, "tcp", action, uri);
822}
823
836char *
837get_plugin_preference (const char *oid, const char *name, int pref_id)
838{
839 GHashTable *prefs;
840 GHashTableIter iter;
841 char *cname = NULL, *retval = NULL;
842 void *itername, *itervalue;
843 char prefix[1024], suffix[1024];
844
845 prefs = preferences_get ();
846 if (!prefs || !nvticache_initialized () || !oid || (!name && pref_id < 0))
847 return NULL;
848
849 g_hash_table_iter_init (&iter, prefs);
850
851 if (pref_id >= 0)
852 {
853 snprintf (prefix, sizeof (prefix), "%s:%d:", oid, pref_id);
854 while (g_hash_table_iter_next (&iter, &itername, &itervalue))
855 {
856 if (g_str_has_prefix (itername, prefix))
857 {
858 retval = g_strdup (itervalue);
859 break;
860 }
861 }
862 }
863 else
864 {
865 cname = g_strdup (name);
866 g_strchomp (cname);
867 snprintf (prefix, sizeof (prefix), "%s:", oid);
868 snprintf (suffix, sizeof (suffix), ":%s", cname);
869 /* NVT preferences received in OID:PrefID:PrefType:PrefName form */
870 while (g_hash_table_iter_next (&iter, &itername, &itervalue))
871 {
872 if (g_str_has_prefix (itername, prefix)
873 && g_str_has_suffix (itername, suffix))
874 {
875 retval = g_strdup (itervalue);
876 break;
877 }
878 }
879 }
880
881 /* If no value set by the user, get the default one. */
882 if (!retval)
883 {
884 GSList *nprefs, *tmp;
885
886 tmp = nprefs = nvticache_get_prefs (oid);
887 while (tmp)
888 {
889 if ((cname && !strcmp (cname, nvtpref_name (tmp->data)))
890 || (pref_id >= 0 && pref_id == nvtpref_id (tmp->data)))
891 {
892 if (!strcmp (nvtpref_type (tmp->data), "radio"))
893 {
894 char **opts =
895 g_strsplit (nvtpref_default (tmp->data), ";", -1);
896
897 retval = g_strdup (opts[0]);
898 g_strfreev (opts);
899 }
900 else
901 retval = g_strdup (nvtpref_default (tmp->data));
902
903 break;
904 }
905 tmp = tmp->next;
906 }
907 g_slist_free_full (nprefs, (void (*) (void *)) nvtpref_free);
908 }
909 if (cname)
910 g_free (cname);
911 return retval;
912}
913
924const char *
925get_plugin_preference_fname (struct script_infos *desc, const char *filename)
926{
927 const char *content;
928 long contentsize = 0;
929 gint tmpfile;
930 gchar *tmpfilename;
931 GError *error = NULL;
932
933 content = get_plugin_preference_file_content (desc, filename);
934 if (content == NULL)
935 {
936 return NULL;
937 }
938 contentsize = get_plugin_preference_file_size (desc, filename);
939 if (contentsize <= 0)
940 return NULL;
941
942 tmpfile =
943 g_file_open_tmp ("openvas-file-upload.XXXXXX", &tmpfilename, &error);
944 if (tmpfile == -1)
945 {
946 g_message ("get_plugin_preference_fname: Could not open temporary"
947 " file for %s: %s",
948 filename, error->message);
949 g_error_free (error);
950 return NULL;
951 }
952 close (tmpfile);
953
954 if (!g_file_set_contents (tmpfilename, content, contentsize, &error))
955 {
956 g_message ("get_plugin_preference_fname: could set contents of"
957 " temporary file for %s: %s",
958 filename, error->message);
959 g_error_free (error);
960 return NULL;
961 }
962
963 return tmpfilename;
964}
965
979char *
981 const char *identifier)
982{
983 struct scan_globals *globals = desc->globals;
984 GHashTable *trans;
985
986 if (!globals)
987 return NULL;
988
989 trans = globals->files_translation;
990 if (!trans)
991 return NULL;
992
993 return g_hash_table_lookup (trans, identifier);
994}
995
1010long
1012 const char *identifier)
1013{
1014 struct scan_globals *globals = desc->globals;
1015 GHashTable *trans;
1016 gchar *filesize_str;
1017
1018 if (!globals)
1019 return -1;
1020
1021 trans = globals->files_size_translation;
1022 if (!trans)
1023 return -1;
1024
1025 filesize_str = g_hash_table_lookup (trans, identifier);
1026 if (filesize_str == NULL)
1027 return -1;
1028
1029 return atol (filesize_str);
1030}
1031
1032void
1033plug_set_key_len (struct script_infos *args, char *name, int type,
1034 const void *value, size_t len)
1035{
1036 kb_t kb = plug_get_kb (args);
1037 int pos = 0; // Append the item on the right position of the list
1038
1039 if (name == NULL || value == NULL)
1040 return;
1041
1042 if (type == ARG_STRING)
1043 {
1044 if (add_kb_usage (args, len) == -1)
1045 return;
1046 kb_item_add_str_unique (kb, name, value, len, pos);
1047 }
1048 else if (type == ARG_INT)
1049 kb_item_add_int_unique (kb, name, GPOINTER_TO_SIZE (value));
1050 if (global_nasl_debug == 1)
1051 {
1052 if (type == ARG_STRING)
1053 g_message ("set key %s -> %s", name, (char *) value);
1054 else if (type == ARG_INT)
1055 g_message ("set key %s -> %d", name, (int) GPOINTER_TO_SIZE (value));
1056 }
1057}
1058
1059void
1060plug_set_key (struct script_infos *args, char *name, int type,
1061 const void *value)
1062{
1063 plug_set_key_len (args, name, type, value, 0);
1064}
1065
1076void
1077plug_set_key_len_volatile (struct script_infos *args, char *name, int type,
1078 const void *value, int expire, size_t len)
1079{
1080 kb_t kb = plug_get_kb (args);
1081 int pos = 0; // Append the item on the right position of the list
1082
1083 if (name == NULL || value == NULL || expire == -1)
1084 return;
1085
1086 if (type == ARG_STRING)
1087 kb_add_str_unique_volatile (kb, name, value, expire, len, pos);
1088 else if (type == ARG_INT)
1089 kb_add_int_unique_volatile (kb, name, GPOINTER_TO_SIZE (value),
1090 GPOINTER_TO_SIZE (expire));
1091 if (global_nasl_debug == 1)
1092 {
1093 if (type == ARG_STRING)
1094 g_message ("set volatile key %s -> %s", name, (char *) value);
1095 else if (type == ARG_INT)
1096 g_message ("set volatile key %s -> %d", name,
1097 (int) GPOINTER_TO_SIZE (value));
1098 }
1099}
1100
1110void
1111plug_set_key_volatile (struct script_infos *args, char *name, int type,
1112 const void *value, int expire)
1113{
1114 plug_set_key_len_volatile (args, name, type, value, expire, 0);
1115}
1116
1117void
1118plug_replace_key_len (struct script_infos *args, char *name, int type,
1119 void *value, size_t len)
1120{
1121 kb_t kb = plug_get_kb (args);
1122
1123 if (name == NULL || value == NULL)
1124 return;
1125
1126 if (type == ARG_STRING)
1127 {
1128 if (add_kb_usage (args, len) == -1)
1129 return;
1130 kb_item_set_str (kb, name, value, len);
1131 }
1132 else if (type == ARG_INT)
1133 kb_item_set_int (kb, name, GPOINTER_TO_SIZE (value));
1134 if (global_nasl_debug == 1)
1135 {
1136 if (type == ARG_STRING)
1137 g_message ("replace key %s -> %s", name, (char *) value);
1138 else if (type == ARG_INT)
1139 g_message ("replace key %s -> %d", name,
1140 (int) GPOINTER_TO_SIZE (value));
1141 }
1142}
1143
1144void
1145plug_replace_key (struct script_infos *args, char *name, int type, void *value)
1146{
1147 plug_replace_key_len (args, name, type, value, 0);
1148}
1149
1150void
1151scanner_add_port (struct script_infos *args, int port, char *proto)
1152{
1153 host_add_port_proto (args, port, proto);
1154}
1155
1156kb_t
1158{
1159 return args->key;
1160}
1161
1162static void
1164{
1165 int status;
1166 (void) s;
1167
1168 wait (&status);
1169}
1170
1171static void
1172sig_n (int signo, void (*fnc) (int))
1173{
1174 struct sigaction sa;
1175
1176 sa.sa_handler = fnc;
1177 sa.sa_flags = 0;
1178 sigemptyset (&sa.sa_mask);
1179 sigaction (signo, &sa, (struct sigaction *) 0);
1180}
1181
1190static int
1192{
1193 pid_t pid;
1194
1195 // TODO change forking to official channels
1196 if ((pid = fork ()) == 0)
1197 {
1198 sig_n (SIGTERM, _exit);
1199 mqtt_reset ();
1200 kb_lnk_reset (kb);
1201 kb_lnk_reset (get_main_kb ());
1202 nvticache_reset ();
1203 srand48 (getpid () + getppid () + time (NULL));
1204 return 0;
1205 }
1206 else if (pid < 0)
1207 {
1208 g_warning ("%s(): fork() failed (%s)", __func__, strerror (errno));
1209 return -1;
1210 }
1211 else
1212 // the parent waits for the spawned process to finish to prevent DDOS on a
1213 // host when multiple vhosts got spawned
1214 waitpid (pid, NULL, 0);
1215 return 1;
1216}
1217
1230void *
1231plug_get_key (struct script_infos *args, char *name, int *type, size_t *len,
1232 int single)
1233{
1234 kb_t kb = args->key;
1235 struct kb_item *res = NULL, *res_list;
1236
1237 if (type != NULL && *type != KB_TYPE_INT)
1238 *type = -1;
1239
1240 if (kb == NULL)
1241 return NULL;
1242
1243 if (single && type != NULL && *type != KB_TYPE_INT)
1244 res = kb_item_get_single (kb, name, KB_TYPE_UNSPEC);
1245 else if (type != NULL && *type == KB_TYPE_INT)
1246 res = kb_item_get_single (kb, name, KB_TYPE_INT);
1247 else
1248 res = kb_item_get_all (kb, name);
1249
1250 if (res == NULL)
1251 return NULL;
1252
1253 if (!res->next) /* No fork - good */
1254 {
1255 void *ret;
1256 if (res->type == KB_TYPE_INT)
1257 {
1258 if (type != NULL)
1259 *type = KB_TYPE_INT;
1260 ret = g_memdup2 (&res->v_int, sizeof (res->v_int));
1261 }
1262 else
1263 {
1264 if (type != NULL)
1265 *type = KB_TYPE_STR;
1266 if (len)
1267 *len = res->len;
1268
1269 ret = g_malloc0 (res->len + 1);
1270 memcpy (ret, res->v_str, res->len + 1);
1271 }
1272 kb_item_free (res);
1273 return ret;
1274 }
1275
1276 /* More than one value - we will fork() then */
1277 sig_n (SIGCHLD, plug_get_key_sigchld);
1278 res_list = res;
1279 while (res)
1280 {
1281 int pret = plug_fork_child (kb);
1282
1283 if (pret == 0)
1284 {
1285 /* Forked child. */
1286 void *ret;
1287
1288 if (res->type == KB_TYPE_INT)
1289 {
1290 if (type != NULL)
1291 *type = KB_TYPE_INT;
1292 ret = g_memdup2 (&res->v_int, sizeof (res->v_int));
1293 }
1294 else
1295 {
1296 if (type != NULL)
1297 *type = KB_TYPE_STR;
1298 if (len)
1299 *len = res->len;
1300
1301 ret = g_malloc0 (res->len + 1);
1302 memcpy (ret, res->v_str, res->len + 1);
1303 }
1304 kb_item_free (res_list);
1305 return ret;
1306 }
1307 else if (pret == -1)
1308 return NULL;
1309 res = res->next;
1310 }
1311 kb_item_free (res_list);
1312
1313 // Allow to return to the main process if parent process is openvas-nasl.
1314 // So, the main process can do e.g. a kb clean up
1315 if (args->standalone)
1316 return NULL;
1317
1318 _exit (0);
1319}
1320
1327unsigned int
1329{
1330 kb_t kb = plug_get_kb (desc);
1331 struct kb_item *res, *k;
1332 int open21 = 0, open80 = 0;
1333#define MAX_CANDIDATES 16
1334 u_short candidates[MAX_CANDIDATES];
1335 int num_candidates = 0;
1336
1337 k = res = kb_item_get_pattern (kb, "Ports/tcp/*");
1338 if (res == NULL)
1339 return 0;
1340 else
1341 {
1342 int ret;
1343 char *s;
1344
1345 for (;;)
1346 {
1347 s = res->name + sizeof ("Ports/tcp/") - 1;
1348 ret = atoi (s);
1349 if (ret == 21)
1350 open21 = 1;
1351 else if (ret == 80)
1352 open80 = 1;
1353 else
1354 {
1355 candidates[num_candidates++] = ret;
1356 if (num_candidates >= MAX_CANDIDATES)
1357 break;
1358 }
1359 res = res->next;
1360 if (res == NULL)
1361 break;
1362 }
1363
1364 kb_item_free (k);
1365 if (num_candidates != 0)
1366 return candidates[lrand48 () % num_candidates];
1367 else if (open21)
1368 return 21;
1369 else if (open80)
1370 return 80;
1371 }
1372
1373 /* Not reachable */
1374 return 0;
1375}
1376
1381
1382void
1383plug_set_port_transport (struct script_infos *args, int port, int tr)
1384{
1385 char s[256];
1386
1387 snprintf (s, sizeof (s), "Transports/TCP/%d", port);
1388 plug_set_key (args, s, ARG_INT, GSIZE_TO_POINTER (tr));
1389}
1390
1391/* Return the transport encapsulation mode (OPENVAS_ENCAPS_*) for the
1392 given PORT. If no such encapsulation mode has been stored in the
1393 knowledge base (or its value is < 0), OPENVAS_ENCAPS_IP is
1394 currently returned. */
1395int
1397{
1398 char s[256];
1399 int trp;
1400
1401 snprintf (s, sizeof (s), "Transports/TCP/%d", port);
1402 trp = kb_item_get_int (plug_get_kb (args), s);
1403 if (trp >= 0)
1404 return trp;
1405 else
1406 return OPENVAS_ENCAPS_IP; /* Change this to 0 for ultra smart SSL
1407 negotiation, at the expense of possibly
1408 breaking stuff */
1409}
1410
1411static void
1412plug_set_ssl_item (struct script_infos *args, char *item, char *itemfname)
1413{
1414 char s[256];
1415 snprintf (s, sizeof (s), "SSL/%s", item);
1416 plug_set_key (args, s, ARG_STRING, itemfname);
1417}
1418
1419void
1420plug_set_ssl_cert (struct script_infos *args, char *cert)
1421{
1422 plug_set_ssl_item (args, "cert", cert);
1423}
1424
1425void
1426plug_set_ssl_key (struct script_infos *args, char *key)
1427{
1428 plug_set_ssl_item (args, "key", key);
1429}
1430
1431void
1433{
1434 plug_set_ssl_item (args, "password", key);
1435}
1436
1441void
1442plug_set_ssl_CA_file (struct script_infos *args, char *key)
1443{
1444 plug_set_ssl_item (args, "CA", key);
1445}
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:440
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:1118
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:774
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:1111
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:1191
void scanner_add_port(struct script_infos *args, int port, char *proto)
Definition plugutils.c:1151
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:767
int global_nasl_debug
Definition plugutils.c:39
void plug_set_ssl_cert(struct script_infos *args, char *cert)
Definition plugutils.c:1420
void plug_set_ssl_CA_file(struct script_infos *args, char *key)
Definition plugutils.c:1442
void plug_set_key_len(struct script_infos *args, char *name, int type, const void *value, size_t len)
Definition plugutils.c:1033
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:1396
unsigned int plug_get_host_open_port(struct script_infos *desc)
Definition plugutils.c:1328
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:811
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:784
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:1077
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:1383
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:1145
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:1412
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:837
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:1172
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:709
void post_error(const char *oid, struct script_infos *desc, int port, const char *action, const char *uri)
Definition plugutils.c:818
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:1426
void plug_set_key(struct script_infos *args, char *name, int type, const void *value)
Definition plugutils.c:1060
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:1231
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:925
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:1432
static void plug_get_key_sigchld(int s)
Definition plugutils.c:1163
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:804
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:980
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:794
kb_t plug_get_kb(struct script_infos *args)
Definition plugutils.c:1157
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:1011
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.