20#include <gvm/base/hosts.h>
21#include <gvm/base/networking.h>
22#include <gvm/base/prefs.h>
23#include <gvm/util/mqtt.h>
24#include <gvm/util/nvticache.h>
35#define G_LOG_DOMAIN "lib misc"
57 nvti_t *n = args->
nvti;
58 gchar *old = nvti_dependencies (n);
66 new = g_strdup_printf (
"%s, %s", old, depname);
67 nvti_set_dependencies (n,
new);
71 nvti_set_dependencies (n, depname);
78 snprintf (port_s,
sizeof (port_s),
"Ports/%s/%d", proto, portnum);
90 if (ptype == PORT_PROTOCOL_UDP)
91 return (prefs_get_bool (
"unscanned_closed_udp") ? 0 : 1);
93 return (prefs_get_bool (
"unscanned_closed") ? 0 : 1);
102 char port_s[255], *kbstr;
103 const char *prange = prefs_get (
"port_range");
104 port_protocol_t port_type;
105 array_t *port_ranges;
109 if (!strcmp (proto,
"udp"))
111 port_type = PORT_PROTOCOL_UDP;
112 kbstr =
"Host/udp_scanned";
116 port_type = PORT_PROTOCOL_TCP;
117 kbstr =
"Host/scanned";
121 if (kb_item_get_int (kb, kbstr) <= 0)
124 port_ranges = port_range_ranges (prange);
125 if (!port_in_port_ranges (portnum, port_type, port_ranges))
127 array_free (port_ranges);
130 array_free (port_ranges);
133 snprintf (port_s,
sizeof (port_s),
"Ports/%s/%d", proto, portnum);
134 return kb_item_get_int (kb, port_s) > 0;
166 GSList *vhosts = NULL;
168 struct kb_item *current_vhosts = NULL;
174 gvm_vhost_t *tmp = vhosts->data;
178 g_warning (
"%s: Value '%s' exists already", __func__,
hostname);
181 vhosts = vhosts->next;
187 current_vhosts = kb_item_get_all (
host_kb,
"internal/vhosts");
191 while (current_vhosts)
193 if (!strcmp (current_vhosts->v_str,
hostname))
195 g_warning (
"%s: Value '%s' exists already", __func__,
hostname);
196 kb_item_free (current_vhosts);
200 current_vhosts = current_vhosts->next;
203 kb_item_free (current_vhosts);
214 if (!prefs_get_bool (
"expand_vhosts") || !
hostname || !source)
221 if (prefs_get (
"exclude_hosts"))
223 char **tmp = excluded = g_strsplit (prefs_get (
"exclude_hosts"),
",", 0);
227 if (!strcmp (g_strstrip (*tmp),
hostname))
229 g_strfreev (excluded);
234 g_strfreev (excluded);
236 vhost = gvm_vhost_new (g_strdup (
hostname), g_strdup (source));
244 GSList *vhosts = args->
vhosts;
247 return addr6_as_str (args->
ip);
264 vhosts = vhosts->next;
272 GSList *results = NULL, *vhosts = args->
vhosts;
275 results = g_slist_prepend (results, addr6_as_str (args->
ip));
279 gvm_vhost_t *vhost = vhosts->data;
281 results = g_slist_prepend (results, g_strdup (vhost->value));
282 vhosts = vhosts->next;
291 return g_strdup (
"IP-address");
295 GSList *vhosts = args->
vhosts;
300 gvm_vhost_t *vhost = vhosts->data;
302 if (!strcmp (vhost->value,
hostname))
303 return g_strdup (vhost->source);
304 vhosts = vhosts->next;
345 type_str =
"HOST_START";
348 type_str =
"HOST_END";
354 type_str =
"HOST_DETAIL";
360 type_str =
"DEADHOST";
389 const char *original_scan_id;
390 char *current_scan_id;
393 if (original_scan_id == NULL)
395 current_scan_id = kb_item_get_str (
main_kb, (
"internal/scanid"));
396 if (current_scan_id == NULL)
399 if (!g_strcmp0 (original_scan_id, current_scan_id))
401 g_free (current_scan_id);
405 g_warning (
"KB inconsitency. %s writing into %s KB", original_scan_id,
407 g_free (current_scan_id);
428 char *current_scan_id;
434 current_scan_id = kb_item_get_str (kb, (
"internal/scanid"));
436 "%s: scan_id (%s) does not match global scan_id (%s); abort to "
437 "prevent data corruption",
439 g_free (current_scan_id);
448 "%s: No internal/scanid found; abort to prevent data corruption.",
482 return result == 0 ? kb_item_push_str (kb,
name, value) : -1;
504 const char *value,
size_t len)
507 return result == 0 ? kb_item_set_str (kb,
name, value,
len) : -1;
529 const char *value,
size_t len,
533 return result == 0 ? kb_item_add_str_unique (kb,
name, value,
len, pos) : -1;
557 return result == 0 ? kb_item_set_int (kb,
name, value) : -1;
581 return result == 0 ? kb_item_add_int (kb,
name, value) : -1;
605 return result == 0 ? kb_item_add_int_unique (kb,
name, value) : -1;
621 const char *proto,
const char *action,
msg_t msg_type,
625 char *buffer, *data, port_s[16] =
"general";
626 char ip_str[INET6_ADDRSTRLEN];
636 action_str = g_string_new (
"");
639 action_str = g_string_new (action);
640 g_string_append (action_str,
"\n");
644 snprintf (port_s,
sizeof (port_s),
"%d", port);
650 buffer = g_strdup_printf (
"%s|||%s|||%s|||%s/%s|||%s|||%s|||%s",
653 action_str->str, uri ? uri :
"");
655 data = g_convert (buffer, -1,
"UTF-8",
"ISO_8859-1", NULL, &
length, &err);
658 g_warning (
"%s: Error converting to UTF-8: %s\nOriginal string: %s",
659 __func__, err->message, buffer);
661 g_string_free (action_str, TRUE);
669 g_string_free (action_str, TRUE);
674 const char *proto,
const char *action,
const char *uri)
681 const char *action,
const char *uri)
691 const char *proto,
const char *action,
const char *uri)
711 const char *action,
const char *uri)
718 const char *proto,
const char *action,
const char *uri)
725 const char *action,
const char *uri)
747 char *cname = NULL, *retval = NULL;
748 void *itername, *itervalue;
749 char prefix[1024], suffix[1024];
751 prefs = preferences_get ();
752 if (!prefs || !nvticache_initialized () || !
oid || (!
name && pref_id < 0))
755 g_hash_table_iter_init (&iter, prefs);
760 while (g_hash_table_iter_next (&iter, &itername, &itervalue))
762 if (g_str_has_prefix (itername,
prefix))
764 retval = g_strdup (itervalue);
771 cname = g_strdup (
name);
774 snprintf (suffix,
sizeof (suffix),
":%s", cname);
776 while (g_hash_table_iter_next (&iter, &itername, &itervalue))
778 if (g_str_has_prefix (itername,
prefix)
779 && g_str_has_suffix (itername, suffix))
781 retval = g_strdup (itervalue);
790 GSList *nprefs, *tmp;
792 tmp = nprefs = nvticache_get_prefs (
oid);
795 if ((cname && !strcmp (cname, nvtpref_name (tmp->data)))
796 || (pref_id >= 0 && pref_id == nvtpref_id (tmp->data)))
798 if (!strcmp (nvtpref_type (tmp->data),
"radio"))
801 g_strsplit (nvtpref_default (tmp->data),
";", -1);
803 retval = g_strdup (opts[0]);
807 retval = g_strdup (nvtpref_default (tmp->data));
813 g_slist_free_full (nprefs, (
void (*) (
void *)) nvtpref_free);
834 long contentsize = 0;
837 GError *error = NULL;
845 if (contentsize <= 0)
849 g_file_open_tmp (
"openvas-file-upload.XXXXXX", &tmpfilename, &error);
852 g_message (
"get_plugin_preference_fname: Could not open temporary"
854 filename, error->message);
855 g_error_free (error);
860 if (!g_file_set_contents (tmpfilename, content, contentsize, &error))
862 g_message (
"get_plugin_preference_fname: could set contents of"
863 " temporary file for %s: %s",
864 filename, error->message);
865 g_error_free (error);
887 const char *identifier)
899 return g_hash_table_lookup (trans, identifier);
918 const char *identifier)
931 filesize_str = g_hash_table_lookup (trans, identifier);
932 if (filesize_str == NULL)
935 return atol (filesize_str);
940 const void *value,
size_t len)
945 if (
name == NULL || value == NULL)
949 kb_item_add_str_unique (kb,
name, value,
len, pos);
951 kb_item_add_int_unique (kb,
name, GPOINTER_TO_SIZE (value));
955 g_message (
"set key %s -> %s",
name, (
char *) value);
957 g_message (
"set key %s -> %d",
name, (
int) GPOINTER_TO_SIZE (value));
980 const void *value,
int expire,
size_t len)
985 if (
name == NULL || value == NULL || expire == -1)
989 kb_add_str_unique_volatile (kb,
name, value, expire,
len, pos);
991 kb_add_int_unique_volatile (kb,
name, GPOINTER_TO_SIZE (value),
992 GPOINTER_TO_SIZE (expire));
996 g_message (
"set volatile key %s -> %s",
name, (
char *) value);
998 g_message (
"set volatile key %s -> %d",
name,
999 (
int) GPOINTER_TO_SIZE (value));
1014 const void *value,
int expire)
1021 void *value,
size_t len)
1025 if (
name == NULL || value == NULL)
1029 kb_item_set_str (kb,
name, value,
len);
1031 kb_item_set_int (kb,
name, GPOINTER_TO_SIZE (value));
1035 g_message (
"replace key %s -> %s",
name, (
char *) value);
1037 g_message (
"replace key %s -> %d",
name,
1038 (
int) GPOINTER_TO_SIZE (value));
1072 struct sigaction sa;
1074 sa.sa_handler = fnc;
1076 sigemptyset (&sa.sa_mask);
1077 sigaction (signo, &sa, (
struct sigaction *) 0);
1094 if ((
pid = fork ()) == 0)
1096 sig_n (SIGTERM, _exit);
1101 srand48 (getpid () + getppid () + time (NULL));
1106 g_warning (
"%s(): fork() failed (%s)", __func__, strerror (errno));
1112 waitpid (
pid, NULL, 0);
1132 kb_t kb = args->
key;
1133 struct kb_item *res = NULL, *res_list;
1135 if (type != NULL && *type != KB_TYPE_INT)
1141 if (single && type != NULL && *type != KB_TYPE_INT)
1142 res = kb_item_get_single (kb,
name, KB_TYPE_UNSPEC);
1143 else if (type != NULL && *type == KB_TYPE_INT)
1144 res = kb_item_get_single (kb,
name, KB_TYPE_INT);
1146 res = kb_item_get_all (kb,
name);
1154 if (res->type == KB_TYPE_INT)
1157 *type = KB_TYPE_INT;
1158 ret = g_memdup2 (&res->v_int, sizeof (res->v_int));
1163 *type = KB_TYPE_STR;
1167 ret = g_malloc0 (res->len + 1);
1168 memcpy (ret, res->v_str, res->len + 1);
1186 if (res->type == KB_TYPE_INT)
1189 *type = KB_TYPE_INT;
1190 ret = g_memdup2 (&res->v_int, sizeof (res->v_int));
1195 *type = KB_TYPE_STR;
1199 ret = g_malloc0 (res->len + 1);
1200 memcpy (ret, res->v_str, res->len + 1);
1202 kb_item_free (res_list);
1205 else if (pret == -1)
1209 kb_item_free (res_list);
1223 struct kb_item *res, *k;
1224 int open21 = 0, open80 = 0;
1225#define MAX_CANDIDATES 16
1227 int num_candidates = 0;
1229 k = res = kb_item_get_pattern (kb,
"Ports/tcp/*");
1239 s = res->name +
sizeof (
"Ports/tcp/") - 1;
1247 candidates[num_candidates++] = ret;
1257 if (num_candidates != 0)
1258 return candidates[lrand48 () % num_candidates];
1279 snprintf (s,
sizeof (s),
"Transports/TCP/%d", port);
1293 snprintf (s,
sizeof (s),
"Transports/TCP/%d", port);
1307 snprintf (s,
sizeof (s),
"SSL/%s", item);
kb_t get_main_kb(void)
gets the main_kb. @description returns the previously set main_kb; when asserts are enabled it will a...
Header file to cache main_kb.
static void prefix(int n, int i)
Header file for module network.
void plug_replace_key_len(struct script_infos *args, char *name, int type, void *value, size_t len)
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....
static int check_duplicated_vhost(struct script_infos *args, const char *hostname)
Check for duplicated vhosts before inserting a new one.
void post_alarm(const char *oid, struct script_infos *desc, int port, const char *action, const char *uri)
void plug_set_key_volatile(struct script_infos *args, char *name, int type, const void *value, int expire)
Set volatile key with expire.
int host_get_port_state_udp(struct script_infos *plugdata, int portnum)
static int host_get_port_state_proto(struct script_infos *args, int portnum, char *proto)
int host_get_port_state(struct script_infos *plugdata, int portnum)
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...
void scanner_add_port(struct script_infos *args, int port, char *proto)
void proto_post_alarm(const char *oid, struct script_infos *desc, int port, const char *proto, const char *action, const char *uri)
void plug_set_ssl_cert(struct script_infos *args, char *cert)
void plug_set_ssl_CA_file(struct script_infos *args, char *key)
void plug_set_key_len(struct script_infos *args, char *name, int type, const void *value, size_t len)
void plug_set_dep(struct script_infos *args, const char *depname)
static int check_kb_inconsistency_log(void)
calls check_kb_inconsistency and logs as debug when local scan_id is missing.
int plug_get_port_transport(struct script_infos *args, int port)
unsigned int plug_get_host_open_port(struct script_infos *desc)
void proto_post_error(const char *oid, struct script_infos *desc, int port, const char *proto, const char *action, const char *uri)
int kb_get_port_state_proto(kb_t kb, int portnum, char *proto)
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...
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.
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.
const char * plug_current_vhost(void)
static const char * msg_type_to_str(msg_t type)
Return string representation of the given msg_t.
void plug_set_port_transport(struct script_infos *args, int port, int tr)
static void host_add_port_proto(struct script_infos *args, int portnum, char *proto)
GSList * plug_get_host_fqdn_list(struct script_infos *args)
void plug_replace_key(struct script_infos *args, char *name, int type, void *value)
char * plug_get_host_source(struct script_infos *args, const char *hostname)
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....
char * plug_get_host_fqdn(struct script_infos *args)
static void plug_set_ssl_item(struct script_infos *args, char *item, char *itemfname)
char * get_plugin_preference(const char *oid, const char *name, int pref_id)
Get the a plugins preference.
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....
static int unscanned_ports_as_closed(port_protocol_t ptype)
Report state of preferences "unscanned_closed".
static void sig_n(int signo, void(*fnc)(int))
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...
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 ...).
void post_error(const char *oid, struct script_infos *desc, int port, const char *action, const char *uri)
char * plug_get_host_ip_str(struct script_infos *desc)
void plug_set_ssl_key(struct script_infos *args, char *key)
void plug_set_key(struct script_infos *args, char *name, int type, const void *value)
int plug_add_host_fqdn(struct script_infos *args, const char *hostname, const char *source)
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.
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".
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...
struct in6_addr * plug_get_host_ip(struct script_infos *args)
gvm_vhost_t * current_vhost
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....
void plug_set_ssl_pem_password(struct script_infos *args, char *key)
static void plug_get_key_sigchld(int s)
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.
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".
void post_log(const char *oid, struct script_infos *desc, int port, const char *action)
Post a log message about a tcp port.
kb_t plug_get_kb(struct script_infos *args)
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".
Header file for module plugutils.
const char * get_scan_id()
GHashTable * files_size_translation
GHashTable * files_translation
struct scan_globals * globals
Support macros for special platforms.