31 #include <gvm/base/hosts.h>
32 #include <gvm/base/networking.h>
33 #include <gvm/base/prefs.h>
34 #include <gvm/util/nvticache.h>
45 #define G_LOG_DOMAIN "lib misc"
67 nvti_t *n = args->
nvti;
68 gchar *old = nvti_dependencies (n);
76 new = g_strdup_printf (
"%s, %s", old, depname);
77 nvti_set_dependencies (n,
new);
81 nvti_set_dependencies (n, depname);
88 snprintf (port_s,
sizeof (port_s),
"Ports/%s/%d", proto, portnum);
100 if (ptype == PORT_PROTOCOL_UDP)
101 return (prefs_get_bool (
"unscanned_closed_udp") ? 0 : 1);
103 return (prefs_get_bool (
"unscanned_closed") ? 0 : 1);
112 char port_s[255], *kbstr;
113 const char *prange = prefs_get (
"port_range");
114 port_protocol_t port_type;
115 array_t *port_ranges;
119 if (!strcmp (proto,
"udp"))
121 port_type = PORT_PROTOCOL_UDP;
122 kbstr =
"Host/udp_scanned";
126 port_type = PORT_PROTOCOL_TCP;
127 kbstr =
"Host/scanned";
131 if (kb_item_get_int (kb, kbstr) <= 0)
134 port_ranges = port_range_ranges (prange);
135 if (!port_in_port_ranges (portnum, port_type, port_ranges))
137 array_free (port_ranges);
140 array_free (port_ranges);
143 snprintf (port_s,
sizeof (port_s),
"Ports/%s/%d", proto, portnum);
144 return kb_item_get_int (kb, port_s) > 0;
173 if (!prefs_get_bool (
"expand_vhosts") || !
hostname || !source)
180 gvm_vhost_t *tmp = vhosts->data;
184 g_warning (
"%s: Value '%s' exists already", __FUNCTION__,
hostname);
187 vhosts = vhosts->next;
190 if (prefs_get (
"exclude_hosts"))
192 char **tmp = excluded = g_strsplit (prefs_get (
"exclude_hosts"),
",", 0);
196 if (!strcmp (g_strstrip (*tmp),
hostname))
198 g_strfreev (excluded);
203 g_strfreev (excluded);
205 vhost = gvm_vhost_new (g_strdup (
hostname), g_strdup (source));
213 GSList *vhosts = args->
vhosts;
216 return addr6_as_str (args->
ip);
233 vhosts = vhosts->next;
241 GSList *results = NULL, *vhosts = args->
vhosts;
244 results = g_slist_prepend (results, addr6_as_str (args->
ip));
248 gvm_vhost_t *vhost = vhosts->data;
250 results = g_slist_prepend (results, g_strdup (vhost->value));
251 vhosts = vhosts->next;
260 return g_strdup (
"IP-address");
264 GSList *vhosts = args->
vhosts;
269 gvm_vhost_t *vhost = vhosts->data;
271 if (!strcmp (vhost->value,
hostname))
272 return g_strdup (vhost->source);
273 vhosts = vhosts->next;
308 const char *proto,
const char *action,
const char *what)
311 char *buffer, *data, port_s[16] =
"general";
312 char ip_str[INET6_ADDRSTRLEN];
322 action_str = g_string_new (
"");
325 action_str = g_string_new (action);
326 g_string_append (action_str,
"\n");
330 snprintf (port_s,
sizeof (port_s),
"%d", port);
336 buffer = g_strdup_printf (
"%s|||%s|||%s/%s|||%s|||%s", what,
hostname ?:
" ",
337 port_s, proto,
oid, action_str->str);
339 data = g_convert (buffer, -1,
"UTF-8",
"ISO_8859-1", NULL, &length, NULL);
341 kb_item_push_str (kb,
"internal/results", data);
344 g_string_free (action_str, TRUE);
349 const char *proto,
const char *action)
366 const char *proto,
const char *action)
383 const char *proto,
const char *action)
400 char *cname, *retval = NULL;
401 void *itername, *itervalue;
402 char prefix[1024], suffix[1024];
404 prefs = preferences_get ();
405 if (!prefs || !nvticache_initialized () || !
oid || !
name)
408 cname = g_strdup (
name);
410 g_hash_table_iter_init (&iter, prefs);
412 snprintf (suffix,
sizeof (suffix),
":%s", cname);
414 while (g_hash_table_iter_next (&iter, &itername, &itervalue))
416 if (g_str_has_prefix (itername,
prefix)
417 && g_str_has_suffix (itername, suffix))
419 retval = g_strdup (itervalue);
426 GSList *nprefs, *tmp;
428 tmp = nprefs = nvticache_get_prefs (
oid);
431 if (!strcmp (cname, nvtpref_name (tmp->data)))
433 retval = g_strdup (nvtpref_default (tmp->data));
438 g_slist_free_full (nprefs, (
void (*) (
void *)) nvtpref_free);
458 long contentsize = 0;
461 GError *error = NULL;
469 if (contentsize <= 0)
473 g_file_open_tmp (
"openvas-file-upload.XXXXXX", &tmpfilename, &error);
476 g_message (
"get_plugin_preference_fname: Could not open temporary"
478 filename, error->message);
479 g_error_free (error);
484 if (!g_file_set_contents (tmpfilename, content, contentsize, &error))
486 g_message (
"get_plugin_preference_fname: could set contents of"
487 " temporary file for %s: %s",
488 filename, error->message);
489 g_error_free (error);
511 const char *identifier)
523 return g_hash_table_lookup (trans, identifier);
542 const char *identifier)
555 filesize_str = g_hash_table_lookup (trans, identifier);
556 if (filesize_str == NULL)
559 return atol (filesize_str);
564 const void *value,
size_t len)
568 if (
name == NULL || value == NULL)
572 kb_item_add_str_unique (kb,
name, value, len);
574 kb_item_add_int_unique (kb,
name, GPOINTER_TO_SIZE (value));
578 g_message (
"set key %s -> %s",
name, (
char *) value);
580 g_message (
"set key %s -> %d",
name, (
int) GPOINTER_TO_SIZE (value));
593 void *value,
size_t len)
597 if (
name == NULL || value == NULL)
601 kb_item_set_str (kb,
name, value, len);
603 kb_item_set_int (kb,
name, GPOINTER_TO_SIZE (value));
607 g_message (
"replace key %s -> %s",
name, (
char *) value);
609 g_message (
"replace key %s -> %d",
name,
610 (
int) GPOINTER_TO_SIZE (value));
641 sig_n (
int signo,
void (*fnc) (
int))
647 sigemptyset (&sa.sa_mask);
648 sigaction (signo, &sa, (
struct sigaction *) 0);
654 sig_n (SIGTERM, fcn);
660 sig_n (SIGCHLD, fcn);
668 if ((
pid = fork ()) == 0)
673 srand48 (getpid () + getppid () + time (NULL));
678 g_warning (
"%s(): fork() failed (%s)", __func__, strerror (errno));
682 waitpid (
pid, NULL, 0);
703 struct kb_item *res = NULL, *res_list;
705 if (type != NULL && *type != KB_TYPE_INT)
711 if (single && *type != KB_TYPE_INT)
712 res = kb_item_get_single (kb,
name, KB_TYPE_UNSPEC);
713 else if (*type == KB_TYPE_INT)
714 res = kb_item_get_single (kb,
name, KB_TYPE_INT);
716 res = kb_item_get_all (kb,
name);
724 if (res->type == KB_TYPE_INT)
728 ret = g_memdup (&res->v_int, sizeof (res->v_int));
736 ret = g_memdup (res->v_str, res->len + 1);
754 if (res->type == KB_TYPE_INT)
758 ret = g_memdup (&res->v_int, sizeof (res->v_int));
766 ret = g_memdup (res->v_str, res->len + 1);
768 kb_item_free (res_list);
775 kb_item_free (res_list);
789 struct kb_item *res, *k;
790 int open21 = 0, open80 = 0;
791 #define MAX_CANDIDATES 16
793 int num_candidates = 0;
795 k = res = kb_item_get_pattern (kb,
"Ports/tcp/*");
805 s = res->name +
sizeof (
"Ports/tcp/") - 1;
813 candidates[num_candidates++] = ret;
823 if (num_candidates != 0)
824 return candidates[lrand48 () % num_candidates];
845 snprintf (s,
sizeof (s),
"Transports/TCP/%d", port);
859 snprintf (s,
sizeof (s),
"Transports/TCP/%d", port);
873 snprintf (s,
sizeof (s),
"SSL/%s", item);