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)
412 char *cname = NULL, *retval = NULL;
413 void *itername, *itervalue;
414 char prefix[1024], suffix[1024];
416 prefs = preferences_get ();
417 if (!prefs || !nvticache_initialized () || !
oid || (!
name && pref_id < 1))
420 g_hash_table_iter_init (&iter, prefs);
425 while (g_hash_table_iter_next (&iter, &itername, &itervalue))
427 if (g_str_has_prefix (itername,
prefix))
429 retval = g_strdup (itervalue);
436 cname = g_strdup (
name);
439 snprintf (suffix,
sizeof (suffix),
":%s", cname);
441 while (g_hash_table_iter_next (&iter, &itername, &itervalue))
443 if (g_str_has_prefix (itername,
prefix)
444 && g_str_has_suffix (itername, suffix))
446 retval = g_strdup (itervalue);
455 GSList *nprefs, *tmp;
457 tmp = nprefs = nvticache_get_prefs (
oid);
460 if ((cname && !strcmp (cname, nvtpref_name (tmp->data)))
461 || (pref_id >= 0 && pref_id == nvtpref_id (tmp->data)))
463 retval = g_strdup (nvtpref_default (tmp->data));
468 g_slist_free_full (nprefs, (
void (*) (
void *)) nvtpref_free);
489 long contentsize = 0;
492 GError *error = NULL;
500 if (contentsize <= 0)
504 g_file_open_tmp (
"openvas-file-upload.XXXXXX", &tmpfilename, &error);
507 g_message (
"get_plugin_preference_fname: Could not open temporary" 509 filename, error->message);
510 g_error_free (error);
515 if (!g_file_set_contents (tmpfilename, content, contentsize, &error))
517 g_message (
"get_plugin_preference_fname: could set contents of" 518 " temporary file for %s: %s",
519 filename, error->message);
520 g_error_free (error);
542 const char *identifier)
554 return g_hash_table_lookup (trans, identifier);
573 const char *identifier)
586 filesize_str = g_hash_table_lookup (trans, identifier);
587 if (filesize_str == NULL)
590 return atol (filesize_str);
595 const void *value,
size_t len)
599 if (
name == NULL || value == NULL)
603 kb_item_add_str_unique (kb,
name, value, len);
605 kb_item_add_int_unique (kb,
name, GPOINTER_TO_SIZE (value));
609 g_message (
"set key %s -> %s",
name, (
char *) value);
611 g_message (
"set key %s -> %d",
name, (
int) GPOINTER_TO_SIZE (value));
624 void *value,
size_t len)
628 if (
name == NULL || value == NULL)
632 kb_item_set_str (kb,
name, value, len);
634 kb_item_set_int (kb,
name, GPOINTER_TO_SIZE (value));
638 g_message (
"replace key %s -> %s",
name, (
char *) value);
640 g_message (
"replace key %s -> %d",
name,
641 (
int) GPOINTER_TO_SIZE (value));
672 sig_n (
int signo,
void (*fnc) (
int))
678 sigemptyset (&sa.sa_mask);
679 sigaction (signo, &sa, (
struct sigaction *) 0);
685 sig_n (SIGTERM, fcn);
691 sig_n (SIGCHLD, fcn);
699 if ((
pid = fork ()) == 0)
704 srand48 (getpid () + getppid () + time (NULL));
709 g_warning (
"%s(): fork() failed (%s)", __func__, strerror (errno));
713 waitpid (
pid, NULL, 0);
734 struct kb_item *res = NULL, *res_list;
736 if (type != NULL && *type != KB_TYPE_INT)
742 if (single && *type != KB_TYPE_INT)
743 res = kb_item_get_single (kb,
name, KB_TYPE_UNSPEC);
744 else if (*type == KB_TYPE_INT)
745 res = kb_item_get_single (kb,
name, KB_TYPE_INT);
747 res = kb_item_get_all (kb,
name);
755 if (res->type == KB_TYPE_INT)
759 ret = g_memdup (&res->v_int, sizeof (res->v_int));
767 ret = g_memdup (res->v_str, res->len + 1);
785 if (res->type == KB_TYPE_INT)
789 ret = g_memdup (&res->v_int, sizeof (res->v_int));
797 ret = g_memdup (res->v_str, res->len + 1);
799 kb_item_free (res_list);
806 kb_item_free (res_list);
820 struct kb_item *res, *k;
821 int open21 = 0, open80 = 0;
822 #define MAX_CANDIDATES 16 824 int num_candidates = 0;
826 k = res = kb_item_get_pattern (kb,
"Ports/tcp/*");
836 s = res->name +
sizeof (
"Ports/tcp/") - 1;
844 candidates[num_candidates++] = ret;
854 if (num_candidates != 0)
855 return candidates[lrand48 () % num_candidates];
876 snprintf (s,
sizeof (s),
"Transports/TCP/%d", port);
890 snprintf (s,
sizeof (s),
"Transports/TCP/%d", port);
904 snprintf (s,
sizeof (s),
"SSL/%s", item);
char * plug_get_host_fqdn(struct script_infos *args)
Header file for module plugutils.
int plug_add_host_fqdn(struct script_infos *args, const char *hostname, const char *source)
struct scan_globals * globals
int host_get_port_state_proto(struct script_infos *args, int portnum, char *proto)
GHashTable * files_translation
void scanner_add_port(struct script_infos *args, int port, char *proto)
static void sig_term(void(*fcn)())
void plug_set_ssl_CA_file(struct script_infos *args, char *key)
void plug_replace_key(struct script_infos *args, char *name, int type, void *value)
GHashTable * files_size_translation
static void sig_n(int signo, void(*fnc)(int))
kb_t plug_get_kb(struct script_infos *args)
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".
void plug_set_ssl_key(struct script_infos *args, char *key)
const char * plug_current_vhost(void)
GSList * plug_get_host_fqdn_list(struct script_infos *args)
static void sig_chld(void(*fcn)())
char * plug_get_host_source(struct script_infos *args, const char *hostname)
gvm_vhost_t * current_vhost
void post_alarm(const char *oid, struct script_infos *desc, int port, const char *action)
void plug_replace_key_len(struct script_infos *args, char *name, int type, void *value, size_t len)
int host_get_port_state_udp(struct script_infos *plugdata, int portnum)
void post_log(const char *oid, struct script_infos *desc, int port, const char *action)
Post a log message about a tcp port.
void host_add_port_proto(struct script_infos *args, int portnum, char *proto)
unsigned int plug_get_host_open_port(struct script_infos *desc)
int plug_get_port_transport(struct script_infos *args, int port)
static int unscanned_ports_as_closed(port_protocol_t ptype)
Report state of preferences "unscanned_closed".
int host_get_port_state(struct script_infos *plugdata, int portnum)
static void plug_get_key_sigchld()
void plug_set_key_len(struct script_infos *args, char *name, int type, const void *value, size_t len)
void proto_post_error(const char *oid, struct script_infos *desc, int port, const char *proto, const char *action)
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".
struct in6_addr * plug_get_host_ip(struct script_infos *args)
void plug_set_key(struct script_infos *args, char *name, int type, const void *value)
static void plug_set_ssl_item(struct script_infos *args, char *item, char *itemfname)
int kb_get_port_state_proto(kb_t kb, int portnum, char *proto)
Header file for module network.
void proto_post_alarm(const char *oid, struct script_infos *desc, int port, const char *proto, const char *action)
void plug_set_port_transport(struct script_infos *args, int port, int tr)
void proto_post_wrapped(const char *oid, struct script_infos *desc, int port, const char *proto, const char *action, const char *what)
Post a security message (e.g. LOG, NOTE, WARNING ...).
void post_error(const char *oid, struct script_infos *desc, int port, const char *action)
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.
char * get_plugin_preference(const char *oid, const char *name, int pref_id)
Get the a plugins preference.
static int plug_fork_child(kb_t)
char * plug_get_host_ip_str(struct script_infos *desc)
void plug_set_dep(struct script_infos *args, const char *depname)
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".
static void prefix(int n, int i)
void proto_post_log(const char *oid, struct script_infos *desc, int port, const char *proto, const char *action)
Post a log message.
void plug_set_ssl_cert(struct script_infos *args, char *cert)
void plug_set_ssl_pem_password(struct script_infos *args, char *key)