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);
669 g_warning (
"%s: It is not possible initialize magic db", __func__);
672 if (magic_load (magic_cookie, NULL) != 0)
674 g_warning (
"%s: It was not possible to load the default magic db",
678 const char *file_encoding = magic_file (magic_cookie, filename);
681 g_warning (
"%s: It was not possible to identify the file encoding for %s",
686 if (g_strstr_len (file_encoding, strlen (file_encoding),
"utf-8"))
705 const char *proto,
const char *action,
msg_t msg_type,
709 char *buffer, *data, port_s[16] =
"general";
710 char ip_str[INET6_ADDRSTRLEN];
720 action_str = g_string_new (
"");
723 action_str = g_string_new (action);
724 g_string_append (action_str,
"\n");
728 snprintf (port_s,
sizeof (port_s),
"%d", port);
734 buffer = g_strdup_printf (
"%s|||%s|||%s|||%s/%s|||%s|||%s|||%s",
737 action_str->str, uri ? uri :
"");
741 data = g_strdup (buffer);
743 data = g_convert (buffer, -1,
"UTF-8",
"ISO_8859-1", NULL, &
length, &err);
747 g_warning (
"%s: Error converting to UTF-8: %s\nOriginal string: %s",
748 __func__, err->message, buffer);
750 g_string_free (action_str, TRUE);
758 g_string_free (action_str, TRUE);
763 const char *proto,
const char *action,
const char *uri)
770 const char *action,
const char *uri)
780 const char *proto,
const char *action,
const char *uri)
800 const char *action,
const char *uri)
807 const char *proto,
const char *action,
const char *uri)
814 const char *action,
const char *uri)
836 char *cname = NULL, *retval = NULL;
837 void *itername, *itervalue;
838 char prefix[1024], suffix[1024];
840 prefs = preferences_get ();
841 if (!prefs || !nvticache_initialized () || !
oid || (!
name && pref_id < 0))
844 g_hash_table_iter_init (&iter, prefs);
849 while (g_hash_table_iter_next (&iter, &itername, &itervalue))
851 if (g_str_has_prefix (itername,
prefix))
853 retval = g_strdup (itervalue);
860 cname = g_strdup (
name);
863 snprintf (suffix,
sizeof (suffix),
":%s", cname);
865 while (g_hash_table_iter_next (&iter, &itername, &itervalue))
867 if (g_str_has_prefix (itername,
prefix)
868 && g_str_has_suffix (itername, suffix))
870 retval = g_strdup (itervalue);
879 GSList *nprefs, *tmp;
881 tmp = nprefs = nvticache_get_prefs (
oid);
884 if ((cname && !strcmp (cname, nvtpref_name (tmp->data)))
885 || (pref_id >= 0 && pref_id == nvtpref_id (tmp->data)))
887 if (!strcmp (nvtpref_type (tmp->data),
"radio"))
890 g_strsplit (nvtpref_default (tmp->data),
";", -1);
892 retval = g_strdup (opts[0]);
896 retval = g_strdup (nvtpref_default (tmp->data));
902 g_slist_free_full (nprefs, (
void (*) (
void *)) nvtpref_free);
923 long contentsize = 0;
926 GError *error = NULL;
934 if (contentsize <= 0)
938 g_file_open_tmp (
"openvas-file-upload.XXXXXX", &tmpfilename, &error);
941 g_message (
"get_plugin_preference_fname: Could not open temporary"
943 filename, error->message);
944 g_error_free (error);
949 if (!g_file_set_contents (tmpfilename, content, contentsize, &error))
951 g_message (
"get_plugin_preference_fname: could set contents of"
952 " temporary file for %s: %s",
953 filename, error->message);
954 g_error_free (error);
976 const char *identifier)
988 return g_hash_table_lookup (trans, identifier);
1007 const char *identifier)
1011 gchar *filesize_str;
1020 filesize_str = g_hash_table_lookup (trans, identifier);
1021 if (filesize_str == NULL)
1024 return atol (filesize_str);
1029 const void *value,
size_t len)
1034 if (
name == NULL || value == NULL)
1041 kb_item_add_str_unique (kb,
name, value,
len, pos);
1044 kb_item_add_int_unique (kb,
name, GPOINTER_TO_SIZE (value));
1048 g_message (
"set key %s -> %s",
name, (
char *) value);
1050 g_message (
"set key %s -> %d",
name, (
int) GPOINTER_TO_SIZE (value));
1073 const void *value,
int expire,
size_t len)
1078 if (
name == NULL || value == NULL || expire == -1)
1082 kb_add_str_unique_volatile (kb,
name, value, expire,
len, pos);
1084 kb_add_int_unique_volatile (kb,
name, GPOINTER_TO_SIZE (value),
1085 GPOINTER_TO_SIZE (expire));
1089 g_message (
"set volatile key %s -> %s",
name, (
char *) value);
1091 g_message (
"set volatile key %s -> %d",
name,
1092 (
int) GPOINTER_TO_SIZE (value));
1107 const void *value,
int expire)
1114 void *value,
size_t len)
1118 if (
name == NULL || value == NULL)
1125 kb_item_set_str (kb,
name, value,
len);
1128 kb_item_set_int (kb,
name, GPOINTER_TO_SIZE (value));
1132 g_message (
"replace key %s -> %s",
name, (
char *) value);
1134 g_message (
"replace key %s -> %d",
name,
1135 (
int) GPOINTER_TO_SIZE (value));
1169 struct sigaction sa;
1171 sa.sa_handler = fnc;
1173 sigemptyset (&sa.sa_mask);
1174 sigaction (signo, &sa, (
struct sigaction *) 0);
1191 if ((
pid = fork ()) == 0)
1193 sig_n (SIGTERM, _exit);
1198 srand48 (getpid () + getppid () + time (NULL));
1203 g_warning (
"%s(): fork() failed (%s)", __func__, strerror (errno));
1209 waitpid (
pid, NULL, 0);
1229 kb_t kb = args->
key;
1230 struct kb_item *res = NULL, *res_list;
1232 if (type != NULL && *type != KB_TYPE_INT)
1238 if (single && type != NULL && *type != KB_TYPE_INT)
1239 res = kb_item_get_single (kb,
name, KB_TYPE_UNSPEC);
1240 else if (type != NULL && *type == KB_TYPE_INT)
1241 res = kb_item_get_single (kb,
name, KB_TYPE_INT);
1243 res = kb_item_get_all (kb,
name);
1251 if (res->type == KB_TYPE_INT)
1254 *type = KB_TYPE_INT;
1255 ret = g_memdup2 (&res->v_int, sizeof (res->v_int));
1260 *type = KB_TYPE_STR;
1264 ret = g_malloc0 (res->len + 1);
1265 memcpy (ret, res->v_str, res->len + 1);
1283 if (res->type == KB_TYPE_INT)
1286 *type = KB_TYPE_INT;
1287 ret = g_memdup2 (&res->v_int, sizeof (res->v_int));
1292 *type = KB_TYPE_STR;
1296 ret = g_malloc0 (res->len + 1);
1297 memcpy (ret, res->v_str, res->len + 1);
1299 kb_item_free (res_list);
1302 else if (pret == -1)
1306 kb_item_free (res_list);
1326 struct kb_item *res, *k;
1327 int open21 = 0, open80 = 0;
1328#define MAX_CANDIDATES 16
1330 int num_candidates = 0;
1332 k = res = kb_item_get_pattern (kb,
"Ports/tcp/*");
1342 s = res->name +
sizeof (
"Ports/tcp/") - 1;
1350 candidates[num_candidates++] = ret;
1360 if (num_candidates != 0)
1361 return candidates[lrand48 () % num_candidates];
1382 snprintf (s,
sizeof (s),
"Transports/TCP/%d", port);
1396 snprintf (s,
sizeof (s),
"Transports/TCP/%d", port);
1410 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.