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>
36#define G_LOG_DOMAIN "lib misc"
49 const char *usage_char;
51 usage_char = prefs_get (
"max_mem_kb");
54 usage_int = atoi (usage_char);
78 g_warning (
"KB usage exceeded %lu MB. Unable to store any further KB "
79 "Items for script %s",
106 nvti_t *n = args->
nvti;
107 gchar *old = nvti_dependencies (n);
115 new = g_strdup_printf (
"%s, %s", old, depname);
116 nvti_set_dependencies (n,
new);
120 nvti_set_dependencies (n, depname);
127 snprintf (port_s,
sizeof (port_s),
"Ports/%s/%d", proto, portnum);
139 if (ptype == PORT_PROTOCOL_UDP)
140 return (prefs_get_bool (
"unscanned_closed_udp") ? 0 : 1);
142 return (prefs_get_bool (
"unscanned_closed") ? 0 : 1);
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;
158 if (!strcmp (proto,
"udp"))
160 port_type = PORT_PROTOCOL_UDP;
161 kbstr =
"Host/udp_scanned";
165 port_type = PORT_PROTOCOL_TCP;
166 kbstr =
"Host/scanned";
170 if (kb_item_get_int (kb, kbstr) <= 0)
173 port_ranges = port_range_ranges (prange);
174 if (!port_in_port_ranges (portnum, port_type, port_ranges))
176 array_free (port_ranges);
179 array_free (port_ranges);
182 snprintf (port_s,
sizeof (port_s),
"Ports/%s/%d", proto, portnum);
183 return kb_item_get_int (kb, port_s) > 0;
215 GSList *vhosts = NULL;
217 struct kb_item *current_vhosts = NULL;
223 gvm_vhost_t *tmp = vhosts->data;
227 g_warning (
"%s: Value '%s' exists already", __func__,
hostname);
230 vhosts = vhosts->next;
236 current_vhosts = kb_item_get_all (
host_kb,
"internal/vhosts");
240 while (current_vhosts)
242 if (!strcmp (current_vhosts->v_str,
hostname))
244 g_warning (
"%s: Value '%s' exists already", __func__,
hostname);
245 kb_item_free (current_vhosts);
249 current_vhosts = current_vhosts->next;
252 kb_item_free (current_vhosts);
263 if (!prefs_get_bool (
"expand_vhosts") || !
hostname || !source)
270 if (prefs_get (
"exclude_hosts"))
272 char **tmp = excluded = g_strsplit (prefs_get (
"exclude_hosts"),
",", 0);
276 if (!strcmp (g_strstrip (*tmp),
hostname))
278 g_strfreev (excluded);
283 g_strfreev (excluded);
285 vhost = gvm_vhost_new (g_strdup (
hostname), g_strdup (source));
293 GSList *vhosts = args->
vhosts;
296 return addr6_as_str (args->
ip);
313 vhosts = vhosts->next;
327 GSList *results = NULL, *vhosts = args->
vhosts;
330 results = g_slist_prepend (results, addr6_as_str (args->
ip));
334 gvm_vhost_t *vhost = vhosts->data;
336 results = g_slist_prepend (results, g_strdup (vhost->value));
337 vhosts = vhosts->next;
346 return g_strdup (
"IP-address");
350 GSList *vhosts = args->
vhosts;
355 gvm_vhost_t *vhost = vhosts->data;
357 if (!strcmp (vhost->value,
hostname))
358 return g_strdup (vhost->source);
359 vhosts = vhosts->next;
400 type_str =
"HOST_START";
403 type_str =
"HOST_END";
409 type_str =
"HOST_DETAIL";
415 type_str =
"DEADHOST";
444 const char *original_scan_id;
445 char *current_scan_id;
448 if (original_scan_id == NULL)
450 current_scan_id = kb_item_get_str (
main_kb, (
"internal/scanid"));
451 if (current_scan_id == NULL)
454 if (!g_strcmp0 (original_scan_id, current_scan_id))
456 g_free (current_scan_id);
460 g_warning (
"KB inconsitency. %s writing into %s KB", original_scan_id,
462 g_free (current_scan_id);
483 char *current_scan_id;
489 current_scan_id = kb_item_get_str (kb, (
"internal/scanid"));
491 "%s: scan_id (%s) does not match global scan_id (%s); abort to "
492 "prevent data corruption",
494 g_free (current_scan_id);
503 "%s: No internal/scanid found; abort to prevent data corruption.",
537 return result == 0 ? kb_item_push_str (kb,
name, value) : -1;
559 const char *value,
size_t len)
562 return result == 0 ? kb_item_set_str (kb,
name, value,
len) : -1;
584 const char *value,
size_t len,
588 return result == 0 ? kb_item_add_str_unique (kb,
name, value,
len, pos) : -1;
612 return result == 0 ? kb_item_set_int (kb,
name, value) : -1;
636 return result == 0 ? kb_item_add_int (kb,
name, value) : -1;
660 return result == 0 ? kb_item_add_int_unique (kb,
name, value) : -1;
666 magic_t magic_cookie = magic_open (MAGIC_MIME_ENCODING);
670 g_warning (
"%s: It is not possible initialize magic db", __func__);
673 if (magic_load (magic_cookie, NULL) != 0)
675 g_warning (
"%s: It was not possible to load the default magic db",
680 const char *file_encoding = magic_file (magic_cookie, filename);
683 g_warning (
"%s: It was not possible to identify the file encoding for %s",
689 if (g_strstr_len (file_encoding, strlen (file_encoding),
"utf-8"))
693 magic_close (magic_cookie);
710 const char *proto,
const char *action,
msg_t msg_type,
714 char *buffer, *data, port_s[16] =
"general";
715 char ip_str[INET6_ADDRSTRLEN];
725 action_str = g_string_new (
"");
728 action_str = g_string_new (action);
729 g_string_append (action_str,
"\n");
733 snprintf (port_s,
sizeof (port_s),
"%d", port);
739 buffer = g_strdup_printf (
"%s|||%s|||%s|||%s/%s|||%s|||%s|||%s",
742 action_str->str, uri ? uri :
"");
746 data = g_strdup (buffer);
748 data = g_convert (buffer, -1,
"UTF-8",
"ISO_8859-1", NULL, &
length, &err);
752 g_warning (
"%s: Error converting to UTF-8: %s\nOriginal string: %s",
753 __func__, err ? err->message:
"", buffer);
755 g_string_free (action_str, TRUE);
763 g_string_free (action_str, TRUE);
768 const char *proto,
const char *action,
const char *uri)
775 const char *action,
const char *uri)
785 const char *proto,
const char *action,
const char *uri)
805 const char *action,
const char *uri)
812 const char *proto,
const char *action,
const char *uri)
819 const char *action,
const char *uri)
841 char *cname = NULL, *retval = NULL;
842 void *itername, *itervalue;
843 char prefix[1024], suffix[1024];
845 prefs = preferences_get ();
846 if (!prefs || !nvticache_initialized () || !
oid || (!
name && pref_id < 0))
849 g_hash_table_iter_init (&iter, prefs);
854 while (g_hash_table_iter_next (&iter, &itername, &itervalue))
856 if (g_str_has_prefix (itername,
prefix))
858 retval = g_strdup (itervalue);
865 cname = g_strdup (
name);
868 snprintf (suffix,
sizeof (suffix),
":%s", cname);
870 while (g_hash_table_iter_next (&iter, &itername, &itervalue))
872 if (g_str_has_prefix (itername,
prefix)
873 && g_str_has_suffix (itername, suffix))
875 retval = g_strdup (itervalue);
884 GSList *nprefs, *tmp;
886 tmp = nprefs = nvticache_get_prefs (
oid);
889 if ((cname && !strcmp (cname, nvtpref_name (tmp->data)))
890 || (pref_id >= 0 && pref_id == nvtpref_id (tmp->data)))
892 if (!strcmp (nvtpref_type (tmp->data),
"radio"))
895 g_strsplit (nvtpref_default (tmp->data),
";", -1);
897 retval = g_strdup (opts[0]);
901 retval = g_strdup (nvtpref_default (tmp->data));
907 g_slist_free_full (nprefs, (
void (*) (
void *)) nvtpref_free);
928 long contentsize = 0;
931 GError *error = NULL;
939 if (contentsize <= 0)
943 g_file_open_tmp (
"openvas-file-upload.XXXXXX", &tmpfilename, &error);
946 g_message (
"get_plugin_preference_fname: Could not open temporary"
948 filename, error->message);
949 g_error_free (error);
954 if (!g_file_set_contents (tmpfilename, content, contentsize, &error))
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);
981 const char *identifier)
993 return g_hash_table_lookup (trans, identifier);
1012 const char *identifier)
1016 gchar *filesize_str;
1025 filesize_str = g_hash_table_lookup (trans, identifier);
1026 if (filesize_str == NULL)
1029 return atol (filesize_str);
1034 const void *value,
size_t len)
1039 if (
name == NULL || value == NULL)
1046 kb_item_add_str_unique (kb,
name, value,
len, pos);
1049 kb_item_add_int_unique (kb,
name, GPOINTER_TO_SIZE (value));
1053 g_message (
"set key %s -> %s",
name, (
char *) value);
1055 g_message (
"set key %s -> %d",
name, (
int) GPOINTER_TO_SIZE (value));
1078 const void *value,
int expire,
size_t len)
1083 if (
name == NULL || value == NULL || expire == -1)
1087 kb_add_str_unique_volatile (kb,
name, value, expire,
len, pos);
1089 kb_add_int_unique_volatile (kb,
name, GPOINTER_TO_SIZE (value),
1090 GPOINTER_TO_SIZE (expire));
1094 g_message (
"set volatile key %s -> %s",
name, (
char *) value);
1096 g_message (
"set volatile key %s -> %d",
name,
1097 (
int) GPOINTER_TO_SIZE (value));
1112 const void *value,
int expire)
1119 void *value,
size_t len)
1123 if (
name == NULL || value == NULL)
1130 kb_item_set_str (kb,
name, value,
len);
1133 kb_item_set_int (kb,
name, GPOINTER_TO_SIZE (value));
1137 g_message (
"replace key %s -> %s",
name, (
char *) value);
1139 g_message (
"replace key %s -> %d",
name,
1140 (
int) GPOINTER_TO_SIZE (value));
1174 struct sigaction sa;
1176 sa.sa_handler = fnc;
1178 sigemptyset (&sa.sa_mask);
1179 sigaction (signo, &sa, (
struct sigaction *) 0);
1196 if ((
pid = fork ()) == 0)
1198 sig_n (SIGTERM, _exit);
1203 srand48 (getpid () + getppid () + time (NULL));
1208 g_warning (
"%s(): fork() failed (%s)", __func__, strerror (errno));
1214 waitpid (
pid, NULL, 0);
1234 kb_t kb = args->
key;
1235 struct kb_item *res = NULL, *res_list;
1237 if (type != NULL && *type != KB_TYPE_INT)
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);
1248 res = kb_item_get_all (kb,
name);
1256 if (res->type == KB_TYPE_INT)
1259 *type = KB_TYPE_INT;
1260 ret = g_memdup2 (&res->v_int, sizeof (res->v_int));
1265 *type = KB_TYPE_STR;
1269 ret = g_malloc0 (res->len + 1);
1270 memcpy (ret, res->v_str, res->len + 1);
1288 if (res->type == KB_TYPE_INT)
1291 *type = KB_TYPE_INT;
1292 ret = g_memdup2 (&res->v_int, sizeof (res->v_int));
1297 *type = KB_TYPE_STR;
1301 ret = g_malloc0 (res->len + 1);
1302 memcpy (ret, res->v_str, res->len + 1);
1304 kb_item_free (res_list);
1307 else if (pret == -1)
1311 kb_item_free (res_list);
1331 struct kb_item *res, *k;
1332 int open21 = 0, open80 = 0;
1333#define MAX_CANDIDATES 16
1335 int num_candidates = 0;
1337 k = res = kb_item_get_pattern (kb,
"Ports/tcp/*");
1347 s = res->name +
sizeof (
"Ports/tcp/") - 1;
1355 candidates[num_candidates++] = ret;
1365 if (num_candidates != 0)
1366 return candidates[lrand48 () % num_candidates];
1387 snprintf (s,
sizeof (s),
"Transports/TCP/%d", port);
1401 snprintf (s,
sizeof (s),
"Transports/TCP/%d", port);
1415 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 size_t max_kb_usage
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)
static int is_utf8_encoded(const char *filename)
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.
static int add_kb_usage(struct script_infos *args, size_t size)
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.