OpenVAS Scanner 23.32.3
plugutils.c File Reference

Plugin-specific stuff. More...

#include "plugutils.h"
#include "kb_cache.h"
#include "network.h"
#include "scan_id.h"
#include "support.h"
#include <errno.h>
#include <gvm/base/hosts.h>
#include <gvm/base/networking.h>
#include <gvm/base/prefs.h>
#include <gvm/util/mqtt.h>
#include <gvm/util/nvticache.h>
#include <magic.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
Include dependency graph for plugutils.c:

Go to the source code of this file.

Macros

#define G_LOG_DOMAIN   "lib misc"
 GLib logging domain.
#define MAX_CANDIDATES   16

Functions

void init_kb_usage (void)
static int add_kb_usage (struct script_infos *args, size_t size)
const char * plug_current_vhost (void)
static int plug_fork_child (kb_t kb)
 Spawns a new child process. Setups everything that is needed for a new process. Child must be handled by caller.
void plug_set_dep (struct script_infos *args, const char *depname)
static void host_add_port_proto (struct script_infos *args, int portnum, char *proto)
static int unscanned_ports_as_closed (port_protocol_t ptype)
 Report state of preferences "unscanned_closed".
int kb_get_port_state_proto (kb_t kb, int portnum, char *proto)
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)
int host_get_port_state_udp (struct script_infos *plugdata, int portnum)
static int check_duplicated_vhost (struct script_infos *args, const char *hostname)
 Check for duplicated vhosts before inserting a new one.
int plug_add_host_fqdn (struct script_infos *args, const char *hostname, const char *source)
char * plug_get_host_fqdn (struct script_infos *args)
GSList * plug_get_host_fqdn_list (struct script_infos *args)
char * plug_get_host_source (struct script_infos *args, const char *hostname)
struct in6_addr * plug_get_host_ip (struct script_infos *args)
char * plug_get_host_ip_str (struct script_infos *desc)
static const char * msg_type_to_str (msg_t type)
 Return string representation of the given msg_t.
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 id in get_scan_id, set at the beginning of the scan, with the one found in the main kb. Therefore it is mandatory that the global main_kb variable to be set. It helps to detect that the kb was not taken by another task/scan, and that the current plugins does not stores results in a wrong kb.
static int check_kb_inconsistency_log (void)
 calls check_kb_inconsistency and logs as debug when local scan_id is missing.
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. @description Compares the scan id in get_scan_id, set at the beginning of the scan, with the one found in the main kb. Therefore it is mandatory that the global main_kb variable to be set. It helps to detect that the kb was not taken by another task/scan, and that the current plugins does not stores results in a wrong kb.
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. @description Compares the scan id in get_scan_id, set at the beginning of the scan, with the one found in the main kb. Therefore it is mandatory that the global main_kb variable to be set. It helps to detect that the kb was not taken by another task/scan, and that the current plugins does not stores results in a wrong kb.
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_unique. @description Compares the scan id in get_scan_id, set at the beginning of the scan, with the one found in the main kb. Therefore it is mandatory that the global main_kb variable to be set. It helps to detect that the kb was not taken by another task/scan, and that the current plugins does not stores results in a wrong kb.
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. @description Compares the scan id in get_scan_id, set at the beginning of the scan, with the one found in the main kb. Therefore it is mandatory that the global main_kb variable to be set. It helps to detect that the kb was not taken by another task/scan, and that the current plugins does not stores results in a wrong kb.
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. @description Compares the scan id in get_scan_id, add at the beginning of the scan, with the one found in the main kb. Therefore it is mandatory that the global main_kb variable to be set. It helps to detect that the kb was not taken by another task/scan, and that the current plugins does not stores results in a wrong kb.
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_unique. @description Compares the scan id in get_scan_id, add at the beginning of the scan, with the one found in the main kb. Therefore it is mandatory that the global main_kb variable to be set. It helps to detect that the kb was not taken by another task/scan, and that the current plugins does not stores results in a wrong kb.
static int is_utf8_encoded (const char *filename)
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 proto_post_alarm (const char *oid, struct script_infos *desc, int port, const char *proto, const char *action, const char *uri)
void post_alarm (const char *oid, struct script_infos *desc, int port, const char *action, const char *uri)
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 post_log (const char *oid, struct script_infos *desc, int port, const char *action)
 Post a log message about a tcp port.
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.
void proto_post_error (const char *oid, struct script_infos *desc, int port, const char *proto, const char *action, const char *uri)
void post_error (const char *oid, struct script_infos *desc, int port, const char *action, const char *uri)
char * get_plugin_preference (const char *oid, const char *name, int pref_id)
 Get the a plugins preference.
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".
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".
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".
void plug_set_key_len (struct script_infos *args, char *name, int type, const void *value, size_t len)
void plug_set_key (struct script_infos *args, char *name, int type, const void *value)
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.
void plug_set_key_volatile (struct script_infos *args, char *name, int type, const void *value, int expire)
 Set volatile key with expire.
void plug_replace_key_len (struct script_infos *args, char *name, int type, void *value, size_t len)
void plug_replace_key (struct script_infos *args, char *name, int type, void *value)
void scanner_add_port (struct script_infos *args, int port, char *proto)
kb_t plug_get_kb (struct script_infos *args)
static void plug_get_key_sigchld (int s)
static void sig_n (int signo, void(*fnc)(int))
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.
unsigned int plug_get_host_open_port (struct script_infos *desc)
void plug_set_port_transport (struct script_infos *args, int port, int tr)
int plug_get_port_transport (struct script_infos *args, int port)
static void plug_set_ssl_item (struct script_infos *args, char *item, char *itemfname)
void plug_set_ssl_cert (struct script_infos *args, char *cert)
void plug_set_ssl_key (struct script_infos *args, char *key)
void plug_set_ssl_pem_password (struct script_infos *args, char *key)
void plug_set_ssl_CA_file (struct script_infos *args, char *key)

Variables

int global_nasl_debug = 0
static size_t kb_usage = 0
static size_t max_kb_usage
gvm_vhost_t * current_vhost = NULL

Detailed Description

Plugin-specific stuff.

Definition in file plugutils.c.

Macro Definition Documentation

◆ G_LOG_DOMAIN

#define G_LOG_DOMAIN   "lib misc"

GLib logging domain.

Definition at line 36 of file plugutils.c.

◆ MAX_CANDIDATES

#define MAX_CANDIDATES   16

Referenced by plug_get_host_open_port().

Function Documentation

◆ add_kb_usage()

int add_kb_usage ( struct script_infos * args,
size_t size )
static

Definition at line 69 of file plugutils.c.

70{
71 if (max_kb_usage == 0)
72 return 0;
74 return -1;
75 kb_usage += size;
77 {
78 g_warning ("KB usage exceeded %lu MB. Unable to store any further KB "
79 "Items for script %s",
80 max_kb_usage / 1024 / 1024, args->name);
81 return -1;
82 }
83 return 0;
84}
static size_t max_kb_usage
Definition plugutils.c:44
static size_t kb_usage
Definition plugutils.c:42
char * name
Definition scanneraux.h:35

References kb_usage, max_kb_usage, and script_infos::name.

Referenced by plug_replace_key_len(), and plug_set_key_len().

Here is the caller graph for this function:

◆ check_duplicated_vhost()

int check_duplicated_vhost ( struct script_infos * args,
const char * hostname )
static

Check for duplicated vhosts before inserting a new one.

Parameters
argsscript info structure
hostnamehostname to check
Returns
0 if the vhosts was still not added. -1 if the vhosts already exists.

Definition at line 213 of file plugutils.c.

214{
215 GSList *vhosts = NULL;
216 kb_t host_kb = NULL;
217 struct kb_item *current_vhosts = NULL;
218
219 /* Check for duplicate vhost value in args. */
220 vhosts = args->vhosts;
221 while (vhosts)
222 {
223 gvm_vhost_t *tmp = vhosts->data;
224
225 if (!strcmp (tmp->value, hostname))
226 {
227 g_warning ("%s: Value '%s' exists already", __func__, hostname);
228 return -1;
229 }
230 vhosts = vhosts->next;
231 }
232
233 /* Check for duplicate vhost value already added by other forked child of the
234 * same plugin. */
235 host_kb = args->key;
236 current_vhosts = kb_item_get_all (host_kb, "internal/vhosts");
237 if (!current_vhosts)
238 return 0;
239
240 while (current_vhosts)
241 {
242 if (!strcmp (current_vhosts->v_str, hostname))
243 {
244 g_warning ("%s: Value '%s' exists already", __func__, hostname);
245 kb_item_free (current_vhosts);
246
247 return -1;
248 }
249 current_vhosts = current_vhosts->next;
250 }
251
252 kb_item_free (current_vhosts);
253 return 0;
254}
static kb_t host_kb
Definition attack.c:290
const char * hostname
GSList * vhosts
Definition scanneraux.h:38

References host_kb, hostname, script_infos::key, and script_infos::vhosts.

Referenced by plug_add_host_fqdn().

Here is the caller graph for this function:

◆ check_kb_inconsistency()

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 id in get_scan_id, set at the beginning of the scan, with the one found in the main kb. Therefore it is mandatory that the global main_kb variable to be set. It helps to detect that the kb was not taken by another task/scan, and that the current plugins does not stores results in a wrong kb.

Parameters
main_kbCurrent main kb.
Returns
0 on success, -1 on missing global scan_id, -2 on missing current_scan_id, -3 when inconsistent.

Definition at line 442 of file plugutils.c.

443{
444 const char *original_scan_id;
445 char *current_scan_id;
446
447 original_scan_id = get_scan_id ();
448 if (original_scan_id == NULL)
449 return -1;
450 current_scan_id = kb_item_get_str (main_kb, ("internal/scanid"));
451 if (current_scan_id == NULL)
452 return -2;
453
454 if (!g_strcmp0 (original_scan_id, current_scan_id))
455 {
456 g_free (current_scan_id);
457 return 0;
458 }
459
460 g_warning ("KB inconsitency. %s writing into %s KB", original_scan_id,
461 current_scan_id);
462 g_free (current_scan_id);
463 return -3;
464}
kb_t main_kb
Definition kb_cache.c:15
const char * get_scan_id()
Definition scan_id.c:22

References get_scan_id(), and main_kb.

Referenced by attack_host(), check_kb_inconsistency_log(), and set_scan_status().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_kb_inconsistency_log()

int check_kb_inconsistency_log ( void )
static

calls check_kb_inconsistency and logs as debug when local scan_id is missing.

@description Compares the scan id in get_scan_id, set at the beginning of the scan, with the one found in the main kb. Therefore it is mandatory that the global main_kb variable to be set. It helps to detect that the kb was not taken by another task/scan, and that the current plugins does not stores results in a wrong kb.

Returns
0 on success, -1 on inconsistency.

Definition at line 481 of file plugutils.c.

482{
483 char *current_scan_id;
484 kb_t kb = get_main_kb ();
485 int result = check_kb_inconsistency (kb);
486 switch (result)
487 {
488 case -3:
489 current_scan_id = kb_item_get_str (kb, ("internal/scanid"));
490 g_warning (
491 "%s: scan_id (%s) does not match global scan_id (%s); abort to "
492 "prevent data corruption",
493 __func__, current_scan_id, get_scan_id ());
494 g_free (current_scan_id);
495 _exit (1);
496 break;
497 case -1:
498 // a call without global scan id can happen in e.g. nasl-lint or
499 // openvas-nasl calls
500 break;
501 case -2:
502 g_warning (
503 "%s: No internal/scanid found; abort to prevent data corruption.",
504 __func__);
505 _exit (1);
506 break;
507 default:
508 {
509 // nothing
510 }
511 }
512 return 0;
513}
kb_t get_main_kb(void)
gets the main_kb. @description returns the previously set main_kb; when asserts are enabled it will a...
Definition kb_cache.c:41
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...
Definition plugutils.c:442

References check_kb_inconsistency(), get_main_kb(), and get_scan_id().

Referenced by kb_item_add_int_unique_with_main_kb_check(), kb_item_add_int_with_main_kb_check(), kb_item_add_str_unique_with_main_kb_check(), kb_item_push_str_with_main_kb_check(), kb_item_set_int_with_main_kb_check(), and kb_item_set_str_with_main_kb_check().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_plugin_preference()

char * get_plugin_preference ( const char * oid,
const char * name,
int pref_id )

Get the a plugins preference.

Search in the preferences set by the client. If it is not present, search in redis cache for the default.

Parameters
[in]oidScript OID to get the preference from
[in]nameName of the preference to get
[in]pref_idId of the preferences to get
Returns
script preference on success, Null otherwise.

Definition at line 832 of file plugutils.c.

833{
834 GHashTable *prefs;
835 GHashTableIter iter;
836 char *cname = NULL, *retval = NULL;
837 void *itername, *itervalue;
838 char prefix[1024], suffix[1024];
839
840 prefs = preferences_get ();
841 if (!prefs || !nvticache_initialized () || !oid || (!name && pref_id < 0))
842 return NULL;
843
844 g_hash_table_iter_init (&iter, prefs);
845
846 if (pref_id >= 0)
847 {
848 snprintf (prefix, sizeof (prefix), "%s:%d:", oid, pref_id);
849 while (g_hash_table_iter_next (&iter, &itername, &itervalue))
850 {
851 if (g_str_has_prefix (itername, prefix))
852 {
853 retval = g_strdup (itervalue);
854 break;
855 }
856 }
857 }
858 else
859 {
860 cname = g_strdup (name);
861 g_strchomp (cname);
862 snprintf (prefix, sizeof (prefix), "%s:", oid);
863 snprintf (suffix, sizeof (suffix), ":%s", cname);
864 /* NVT preferences received in OID:PrefID:PrefType:PrefName form */
865 while (g_hash_table_iter_next (&iter, &itername, &itervalue))
866 {
867 if (g_str_has_prefix (itername, prefix)
868 && g_str_has_suffix (itername, suffix))
869 {
870 retval = g_strdup (itervalue);
871 break;
872 }
873 }
874 }
875
876 /* If no value set by the user, get the default one. */
877 if (!retval)
878 {
879 GSList *nprefs, *tmp;
880
881 tmp = nprefs = nvticache_get_prefs (oid);
882 while (tmp)
883 {
884 if ((cname && !strcmp (cname, nvtpref_name (tmp->data)))
885 || (pref_id >= 0 && pref_id == nvtpref_id (tmp->data)))
886 {
887 if (!strcmp (nvtpref_type (tmp->data), "radio"))
888 {
889 char **opts =
890 g_strsplit (nvtpref_default (tmp->data), ";", -1);
891
892 retval = g_strdup (opts[0]);
893 g_strfreev (opts);
894 }
895 else
896 retval = g_strdup (nvtpref_default (tmp->data));
897
898 break;
899 }
900 tmp = tmp->next;
901 }
902 g_slist_free_full (nprefs, (void (*) (void *)) nvtpref_free);
903 }
904 if (cname)
905 g_free (cname);
906 return retval;
907}
const char * oid
const char * name
Definition nasl_init.c:439
static void prefix(int n, int i)
Definition nasl_tree.c:219

References name, oid, and prefix().

Referenced by plugin_do_run(), plugin_run_find_service(), plugin_timeout(), script_get_preference(), script_get_preference_file_content(), script_get_preference_file_location(), and user_agent_create().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_plugin_preference_file_content()

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".

As files sent to the scanner (e.g. as plugin preference) are stored in a hash table with an identifier supplied by the client as the key, the contents have to be looked up here.

Parameters
identifierIdentifier that was supplied by the client when the file was uploaded.
Returns
Contents of the file identified by identifier, NULL if not found or setup broken.

Definition at line 975 of file plugutils.c.

977{
978 struct scan_globals *globals = desc->globals;
979 GHashTable *trans;
980
981 if (!globals)
982 return NULL;
983
984 trans = globals->files_translation;
985 if (!trans)
986 return NULL;
987
988 return g_hash_table_lookup (trans, identifier);
989}
GHashTable * files_translation
Definition scanneraux.h:20
struct scan_globals * globals
Definition scanneraux.h:30

References scan_globals::files_translation, and script_infos::globals.

Referenced by get_plugin_preference_fname(), and script_get_preference_file_content().

Here is the caller graph for this function:

◆ get_plugin_preference_file_size()

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".

Files sent to the scanner (e.g. as plugin preference) are stored in a hash table with an identifier supplied by the client as the key. The size of the file is stored in a separate hash table with the same identifier as key, which can be looked up here.

Parameters
identifierIdentifier that was supplied by the client when the file was uploaded.
Returns
Size of the file identified by identifier, -1 if not found or setup broken.

Definition at line 1006 of file plugutils.c.

1008{
1009 struct scan_globals *globals = desc->globals;
1010 GHashTable *trans;
1011 gchar *filesize_str;
1012
1013 if (!globals)
1014 return -1;
1015
1016 trans = globals->files_size_translation;
1017 if (!trans)
1018 return -1;
1019
1020 filesize_str = g_hash_table_lookup (trans, identifier);
1021 if (filesize_str == NULL)
1022 return -1;
1023
1024 return atol (filesize_str);
1025}
GHashTable * files_size_translation
Definition scanneraux.h:21

References scan_globals::files_size_translation, and script_infos::globals.

Referenced by get_plugin_preference_fname(), and script_get_preference_file_content().

Here is the caller graph for this function:

◆ get_plugin_preference_fname()

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".

As files sent to the server (e.g. as plugin preference) are stored at pseudo-random locations with different names, the "real" file name has to be looked up in a hashtable.

Returns
Filename on disc for filename, NULL if not found or setup broken.

Definition at line 920 of file plugutils.c.

921{
922 const char *content;
923 long contentsize = 0;
924 gint tmpfile;
925 gchar *tmpfilename;
926 GError *error = NULL;
927
928 content = get_plugin_preference_file_content (desc, filename);
929 if (content == NULL)
930 {
931 return NULL;
932 }
933 contentsize = get_plugin_preference_file_size (desc, filename);
934 if (contentsize <= 0)
935 return NULL;
936
937 tmpfile =
938 g_file_open_tmp ("openvas-file-upload.XXXXXX", &tmpfilename, &error);
939 if (tmpfile == -1)
940 {
941 g_message ("get_plugin_preference_fname: Could not open temporary"
942 " file for %s: %s",
943 filename, error->message);
944 g_error_free (error);
945 return NULL;
946 }
947 close (tmpfile);
948
949 if (!g_file_set_contents (tmpfilename, content, contentsize, &error))
950 {
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);
955 return NULL;
956 }
957
958 return tmpfilename;
959}
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".
Definition plugutils.c:975
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".
Definition plugutils.c:1006

References get_plugin_preference_file_content(), and get_plugin_preference_file_size().

Referenced by plugin_run_find_service(), and script_get_preference_file_location().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ host_add_port_proto()

void host_add_port_proto ( struct script_infos * args,
int portnum,
char * proto )
static

Definition at line 124 of file plugutils.c.

125{
126 char port_s[255];
127 snprintf (port_s, sizeof (port_s), "Ports/%s/%d", proto, portnum);
128 plug_set_key (args, port_s, ARG_INT, (void *) 1);
129}
void plug_set_key(struct script_infos *args, char *name, int type, const void *value)
Definition plugutils.c:1055
#define ARG_INT
Definition plugutils.h:20

References ARG_INT, and plug_set_key().

Referenced by scanner_add_port().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ host_get_port_state()

int host_get_port_state ( struct script_infos * plugdata,
int portnum )

Definition at line 193 of file plugutils.c.

194{
195 return (host_get_port_state_proto (plugdata, portnum, "tcp"));
196}
static int host_get_port_state_proto(struct script_infos *args, int portnum, char *proto)
Definition plugutils.c:187

References host_get_port_state_proto().

Referenced by get_port_state(), and open_sock_tcp().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ host_get_port_state_proto()

int host_get_port_state_proto ( struct script_infos * args,
int portnum,
char * proto )
static

Definition at line 187 of file plugutils.c.

188{
189 return kb_get_port_state_proto (args->key, portnum, proto);
190}
int kb_get_port_state_proto(kb_t kb, int portnum, char *proto)
Definition plugutils.c:149

References kb_get_port_state_proto(), and script_infos::key.

Referenced by host_get_port_state(), and host_get_port_state_udp().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ host_get_port_state_udp()

int host_get_port_state_udp ( struct script_infos * plugdata,
int portnum )

Definition at line 199 of file plugutils.c.

200{
201 return (host_get_port_state_proto (plugdata, portnum, "udp"));
202}

References host_get_port_state_proto().

Referenced by get_udp_port_state().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ init_kb_usage()

void init_kb_usage ( void )

Definition at line 47 of file plugutils.c.

48{
49 const char *usage_char;
50 int usage_int;
51 usage_char = prefs_get ("max_mem_kb");
52 if (usage_char)
53 {
54 usage_int = atoi (usage_char);
55 if (usage_int < 0)
56 {
57 max_kb_usage = 0;
58 }
59 else
60 {
61 max_kb_usage = (size_t) usage_int * 1024 * 1024;
62 }
63 }
64 else
65 max_kb_usage = 0;
66}

References max_kb_usage.

Referenced by pluginlaunch_init().

Here is the caller graph for this function:

◆ is_utf8_encoded()

int is_utf8_encoded ( const char * filename)
static

Definition at line 664 of file plugutils.c.

665{
666 magic_t magic_cookie = magic_open (MAGIC_MIME_ENCODING);
667 if (!magic_cookie)
668 {
669 g_warning ("%s: It is not possible initialize magic db", __func__);
670 return -1;
671 }
672 if (magic_load (magic_cookie, NULL) != 0)
673 {
674 g_warning ("%s: It was not possible to load the default magic db",
675 __func__);
676 return -1;
677 }
678 const char *file_encoding = magic_file (magic_cookie, filename);
679 if (!file_encoding)
680 {
681 g_warning ("%s: It was not possible to identify the file encoding for %s",
682 __func__, filename);
683 return -1;
684 }
685
686 if (g_strstr_len (file_encoding, strlen (file_encoding), "utf-8"))
687 return 1;
688
689 return 0;
690}

Referenced by proto_post_wrapped().

Here is the caller graph for this function:

◆ kb_get_port_state_proto()

int kb_get_port_state_proto ( kb_t kb,
int portnum,
char * proto )
Parameters
protoProtocol (udp/tcp). If NULL, "tcp" will be used.

Definition at line 149 of file plugutils.c.

150{
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;
155
156 if (!proto)
157 proto = "tcp";
158 if (!strcmp (proto, "udp"))
159 {
160 port_type = PORT_PROTOCOL_UDP;
161 kbstr = "Host/udp_scanned";
162 }
163 else
164 {
165 port_type = PORT_PROTOCOL_TCP;
166 kbstr = "Host/scanned";
167 }
168
169 /* Check that we actually scanned the port */
170 if (kb_item_get_int (kb, kbstr) <= 0)
171 return unscanned_ports_as_closed (port_type);
172
173 port_ranges = port_range_ranges (prange);
174 if (!port_in_port_ranges (portnum, port_type, port_ranges))
175 {
176 array_free (port_ranges);
177 return unscanned_ports_as_closed (port_type);
178 }
179 array_free (port_ranges);
180
181 /* Ok, we scanned it. What is its state ? */
182 snprintf (port_s, sizeof (port_s), "Ports/%s/%d", proto, portnum);
183 return kb_item_get_int (kb, port_s) > 0;
184}
static int unscanned_ports_as_closed(port_protocol_t ptype)
Report state of preferences "unscanned_closed".
Definition plugutils.c:137

References unscanned_ports_as_closed().

Referenced by get_closed_ports(), and host_get_port_state_proto().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ kb_item_add_int_unique_with_main_kb_check()

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_unique. @description Compares the scan id in get_scan_id, add at the beginning of the scan, with the one found in the main kb. Therefore it is mandatory that the global main_kb variable to be set. It helps to detect that the kb was not taken by another task/scan, and that the current plugins does not stores results in a wrong kb.

Parameters
kbKb where to store the item into.
namekey for the given value.
valueto store under key within kb.
Returns
0 on success, -1 on inconsistency.

Definition at line 657 of file plugutils.c.

658{
659 int result = check_kb_inconsistency_log ();
660 return result == 0 ? kb_item_add_int_unique (kb, name, value) : -1;
661}
static int check_kb_inconsistency_log(void)
calls check_kb_inconsistency and logs as debug when local scan_id is missing.
Definition plugutils.c:481

References check_kb_inconsistency_log(), and name.

Referenced by set_kb_readable().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ kb_item_add_int_with_main_kb_check()

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. @description Compares the scan id in get_scan_id, add at the beginning of the scan, with the one found in the main kb. Therefore it is mandatory that the global main_kb variable to be set. It helps to detect that the kb was not taken by another task/scan, and that the current plugins does not stores results in a wrong kb.

Parameters
kbKb where to store the item into.
namekey for the given value.
valueto store under key within kb.
Returns
0 on success, -1 on inconsistency.

Definition at line 633 of file plugutils.c.

634{
635 int result = check_kb_inconsistency_log ();
636 return result == 0 ? kb_item_add_int (kb, name, value) : -1;
637}

References check_kb_inconsistency_log(), and name.

Here is the call graph for this function:

◆ kb_item_add_str_unique_with_main_kb_check()

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_unique. @description Compares the scan id in get_scan_id, set at the beginning of the scan, with the one found in the main kb. Therefore it is mandatory that the global main_kb variable to be set. It helps to detect that the kb was not taken by another task/scan, and that the current plugins does not stores results in a wrong kb.

Parameters
kbKb where to store the item into.
namekey for the given value.
valueto store under key within kb.
Returns
0 on success, -1 on inconsistency.

Definition at line 583 of file plugutils.c.

586{
587 int result = check_kb_inconsistency_log ();
588 return result == 0 ? kb_item_add_str_unique (kb, name, value, len, pos) : -1;
589}
uint8_t len

References check_kb_inconsistency_log(), len, and name.

Here is the call graph for this function:

◆ kb_item_push_str_with_main_kb_check()

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. @description Compares the scan id in get_scan_id, set at the beginning of the scan, with the one found in the main kb. Therefore it is mandatory that the global main_kb variable to be set. It helps to detect that the kb was not taken by another task/scan, and that the current plugins does not stores results in a wrong kb.

Parameters
kbKb where to store the item into.
namekey for the given value.
valueto store under key within kb.
Returns
0 on success, -1 on inconsistency.

Definition at line 533 of file plugutils.c.

535{
536 int result = check_kb_inconsistency_log ();
537 return result == 0 ? kb_item_push_str (kb, name, value) : -1;
538}

References check_kb_inconsistency_log(), and name.

Referenced by attack_host(), call_lsc(), call_rs_notus(), comm_send_status(), comm_send_status_host_dead(), host_set_time(), message_to_client(), open_sock_tcp(), proto_post_wrapped(), security_notus(), and update_running_processes().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ kb_item_set_int_with_main_kb_check()

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. @description Compares the scan id in get_scan_id, set at the beginning of the scan, with the one found in the main kb. Therefore it is mandatory that the global main_kb variable to be set. It helps to detect that the kb was not taken by another task/scan, and that the current plugins does not stores results in a wrong kb.

Parameters
kbKb where to store the item into.
namekey for the given value.
valueto store under key within kb.
Returns
0 on success, -1 on inconsistency.

Definition at line 609 of file plugutils.c.

610{
611 int result = check_kb_inconsistency_log ();
612 return result == 0 ? kb_item_set_int (kb, name, value) : -1;
613}

References check_kb_inconsistency_log(), and name.

Referenced by check_host_still_alive(), open_sock_tcp(), and overwrite_openvas_prefs_with_prefs_from_client().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ kb_item_set_str_with_main_kb_check()

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. @description Compares the scan id in get_scan_id, set at the beginning of the scan, with the one found in the main kb. Therefore it is mandatory that the global main_kb variable to be set. It helps to detect that the kb was not taken by another task/scan, and that the current plugins does not stores results in a wrong kb.

Parameters
kbKb where to store the item into.
namekey for the given value.
valueto store under key within kb.
Returns
0 on success, -1 on inconsistency.

Definition at line 558 of file plugutils.c.

560{
561 int result = check_kb_inconsistency_log ();
562 return result == 0 ? kb_item_set_str (kb, name, value, len) : -1;
563}

References check_kb_inconsistency_log(), len, and name.

Referenced by attack_start(), overwrite_openvas_prefs_with_prefs_from_client(), and set_scan_status().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ msg_type_to_str()

const char * msg_type_to_str ( msg_t type)
static

Return string representation of the given msg_t.

Parameters
msgmsg_t to transform
Returns
string representation of the given msg_t if successful, else NULL.

Definition at line 390 of file plugutils.c.

391{
392 gchar *type_str;
393
394 switch (type)
395 {
396 case ERRMSG:
397 type_str = "ERRMSG";
398 break;
399 case HOST_START:
400 type_str = "HOST_START";
401 break;
402 case HOST_END:
403 type_str = "HOST_END";
404 break;
405 case LOG:
406 type_str = "LOG";
407 break;
408 case HOST_DETAIL:
409 type_str = "HOST_DETAIL";
410 break;
411 case ALARM:
412 type_str = "ALARM";
413 break;
414 case DEADHOST:
415 type_str = "DEADHOST";
416 break;
417 default:
418 return NULL;
419 break;
420 }
421
422 return type_str;
423}
@ HOST_DETAIL
Definition plugutils.h:84
@ HOST_END
Definition plugutils.h:82
@ ALARM
Definition plugutils.h:85
@ DEADHOST
Definition plugutils.h:86
@ LOG
Definition plugutils.h:83
@ ERRMSG
Definition plugutils.h:80
@ HOST_START
Definition plugutils.h:81

References ALARM, DEADHOST, ERRMSG, HOST_DETAIL, HOST_END, HOST_START, and LOG.

Referenced by proto_post_wrapped().

Here is the caller graph for this function:

◆ plug_add_host_fqdn()

int plug_add_host_fqdn ( struct script_infos * args,
const char * hostname,
const char * source )

Definition at line 257 of file plugutils.c.

259{
260 gvm_vhost_t *vhost;
261 char **excluded;
262
263 if (!prefs_get_bool ("expand_vhosts") || !hostname || !source)
264 return -1;
265
267 return -1;
268
269 /* Check for excluded vhost value. */
270 if (prefs_get ("exclude_hosts"))
271 {
272 char **tmp = excluded = g_strsplit (prefs_get ("exclude_hosts"), ",", 0);
273
274 while (*tmp)
275 {
276 if (!strcmp (g_strstrip (*tmp), hostname))
277 {
278 g_strfreev (excluded);
279 return -1;
280 }
281 tmp++;
282 }
283 g_strfreev (excluded);
284 }
285 vhost = gvm_vhost_new (g_strdup (hostname), g_strdup (source));
286 args->vhosts = g_slist_prepend (args->vhosts, vhost);
287 return 0;
288}
static int check_duplicated_vhost(struct script_infos *args, const char *hostname)
Check for duplicated vhosts before inserting a new one.
Definition plugutils.c:213

References check_duplicated_vhost(), hostname, and script_infos::vhosts.

Referenced by add_hostname().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ plug_current_vhost()

const char * plug_current_vhost ( void )

Definition at line 93 of file plugutils.c.

94{
95 if (current_vhost != NULL)
96 return current_vhost->value;
97
98 return NULL;
99}
gvm_vhost_t * current_vhost
Definition plugutils.c:89

References current_vhost.

Referenced by nasl_end_denial(), and open_sock_tcp().

Here is the caller graph for this function:

◆ plug_fork_child()

int plug_fork_child ( kb_t kb)
static

Spawns a new child process. Setups everything that is needed for a new process. Child must be handled by caller.

Parameters
kbfor redis connection
Returns
int 0 for the child process, 1 for the parent process and -1 on failure

Definition at line 1186 of file plugutils.c.

1187{
1188 pid_t pid;
1189
1190 // TODO change forking to official channels
1191 if ((pid = fork ()) == 0)
1192 {
1193 sig_n (SIGTERM, _exit);
1194 mqtt_reset ();
1195 kb_lnk_reset (kb);
1196 kb_lnk_reset (get_main_kb ());
1197 nvticache_reset ();
1198 srand48 (getpid () + getppid () + time (NULL));
1199 return 0;
1200 }
1201 else if (pid < 0)
1202 {
1203 g_warning ("%s(): fork() failed (%s)", __func__, strerror (errno));
1204 return -1;
1205 }
1206 else
1207 // the parent waits for the spawned process to finish to prevent DDOS on a
1208 // host when multiple vhosts got spawned
1209 waitpid (pid, NULL, 0);
1210 return 1;
1211}
static pid_t pid
static void sig_n(int signo, void(*fnc)(int))
Definition plugutils.c:1167

References get_main_kb(), pid, and sig_n().

Referenced by plug_get_host_fqdn(), and plug_get_key().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ plug_get_host_fqdn()

char * plug_get_host_fqdn ( struct script_infos * args)

Definition at line 291 of file plugutils.c.

292{
293 GSList *vhosts = args->vhosts;
294
295 if (!args->vhosts)
296 return addr6_as_str (args->ip);
297
298 /* Workaround for rapid growth of forked processes ie. http_get() calls
299 * within foreach() loops. */
300 if (current_vhost)
301 return g_strdup (current_vhost->value);
302 while (vhosts)
303 {
304 int ret = plug_fork_child (args->key);
305
306 if (ret == 0)
307 {
308 current_vhost = vhosts->data;
309 return g_strdup (current_vhost->value);
310 }
311 else if (ret == -1)
312 return NULL;
313 vhosts = vhosts->next;
314 }
315
316 // Allow to return to the main process if parent process is openvas-nasl.
317 // So, the main process can do e.g. a kb clean up
318 if (args->standalone)
319 return NULL;
320
321 _exit (0);
322}
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...
Definition plugutils.c:1186
struct in6_addr * ip
Definition scanneraux.h:37

References current_vhost, script_infos::ip, script_infos::key, plug_fork_child(), script_infos::standalone, and script_infos::vhosts.

Referenced by _http2_req(), _http_req(), get_hostname(), open_stream_connection_ext(), plug_get_host_source(), plugin_do_run(), retry_stream_connection(), and socket_negotiate_ssl().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ plug_get_host_fqdn_list()

GSList * plug_get_host_fqdn_list ( struct script_infos * args)

Definition at line 325 of file plugutils.c.

326{
327 GSList *results = NULL, *vhosts = args->vhosts;
328
329 if (!args->vhosts)
330 results = g_slist_prepend (results, addr6_as_str (args->ip));
331
332 while (vhosts)
333 {
334 gvm_vhost_t *vhost = vhosts->data;
335
336 results = g_slist_prepend (results, g_strdup (vhost->value));
337 vhosts = vhosts->next;
338 }
339 return results;
340}

References script_infos::ip, and script_infos::vhosts.

Referenced by get_hostnames().

Here is the caller graph for this function:

◆ plug_get_host_ip()

◆ plug_get_host_ip_str()

char * plug_get_host_ip_str ( struct script_infos * desc)

Definition at line 377 of file plugutils.c.

378{
379 return addr6_as_str (plug_get_host_ip (desc));
380}
struct in6_addr * plug_get_host_ip(struct script_infos *args)
Definition plugutils.c:371

References plug_get_host_ip().

Referenced by host_reverse_lookup(), nasl_snmpv1v2c_get(), nasl_snmpv3_get_action(), open_sock_tcp(), and socket_negotiate_ssl().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ plug_get_host_open_port()

unsigned int plug_get_host_open_port ( struct script_infos * desc)

Don't always return the first open port, otherwise we might get bitten by OSes doing active SYN flood countermeasures. Also, avoid returning 80 and 21 as open ports, as many transparent proxies are acting for these...

Definition at line 1323 of file plugutils.c.

1324{
1325 kb_t kb = plug_get_kb (desc);
1326 struct kb_item *res, *k;
1327 int open21 = 0, open80 = 0;
1328#define MAX_CANDIDATES 16
1329 u_short candidates[MAX_CANDIDATES];
1330 int num_candidates = 0;
1331
1332 k = res = kb_item_get_pattern (kb, "Ports/tcp/*");
1333 if (res == NULL)
1334 return 0;
1335 else
1336 {
1337 int ret;
1338 char *s;
1339
1340 for (;;)
1341 {
1342 s = res->name + sizeof ("Ports/tcp/") - 1;
1343 ret = atoi (s);
1344 if (ret == 21)
1345 open21 = 1;
1346 else if (ret == 80)
1347 open80 = 1;
1348 else
1349 {
1350 candidates[num_candidates++] = ret;
1351 if (num_candidates >= MAX_CANDIDATES)
1352 break;
1353 }
1354 res = res->next;
1355 if (res == NULL)
1356 break;
1357 }
1358
1359 kb_item_free (k);
1360 if (num_candidates != 0)
1361 return candidates[lrand48 () % num_candidates];
1362 else if (open21)
1363 return 21;
1364 else if (open80)
1365 return 80;
1366 }
1367
1368 /* Not reachable */
1369 return 0;
1370}
#define MAX_CANDIDATES
kb_t plug_get_kb(struct script_infos *args)
Definition plugutils.c:1152

References MAX_CANDIDATES, and plug_get_kb().

Referenced by get_host_open_port(), nasl_start_denial(), nasl_tcp_ping(), and nasl_tcp_v6_ping().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ plug_get_host_source()

char * plug_get_host_source ( struct script_infos * args,
const char * hostname )

Definition at line 343 of file plugutils.c.

344{
345 if (!args->vhosts)
346 return g_strdup ("IP-address");
347
348 if (hostname)
349 {
350 GSList *vhosts = args->vhosts;
351
352 /* Search for source of specified hostname/vhost. */
353 while (vhosts)
354 {
355 gvm_vhost_t *vhost = vhosts->data;
356
357 if (!strcmp (vhost->value, hostname))
358 return g_strdup (vhost->source);
359 vhosts = vhosts->next;
360 }
361 return NULL;
362 }
363 /* Call plug_get_host_fqdn() to set current_vhost (and fork, in case of
364 * multiple vhosts.) */
365 if (!current_vhost)
366 g_free (plug_get_host_fqdn (args));
367 return g_strdup (current_vhost->source);
368}
char * plug_get_host_fqdn(struct script_infos *args)
Definition plugutils.c:291

References current_vhost, hostname, plug_get_host_fqdn(), and script_infos::vhosts.

Referenced by get_hostname_source().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ plug_get_kb()

◆ plug_get_key()

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.

Parameters
[in]argsThe script infos where to get the kb from.
[in]nameKey name to search in the kb.
[in/out]type If 1 is given, the answer is forced to be KB_TYPE_INT type. Otherwise it returns the fetched type.
[in]lenDesired string length to be returned.
[in]singleIn case of a list, fetch only the last element
Returns
Null if no result, or a void pointer to the result in success.

Definition at line 1226 of file plugutils.c.

1228{
1229 kb_t kb = args->key;
1230 struct kb_item *res = NULL, *res_list;
1231
1232 if (type != NULL && *type != KB_TYPE_INT)
1233 *type = -1;
1234
1235 if (kb == NULL)
1236 return NULL;
1237
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);
1242 else
1243 res = kb_item_get_all (kb, name);
1244
1245 if (res == NULL)
1246 return NULL;
1247
1248 if (!res->next) /* No fork - good */
1249 {
1250 void *ret;
1251 if (res->type == KB_TYPE_INT)
1252 {
1253 if (type != NULL)
1254 *type = KB_TYPE_INT;
1255 ret = g_memdup2 (&res->v_int, sizeof (res->v_int));
1256 }
1257 else
1258 {
1259 if (type != NULL)
1260 *type = KB_TYPE_STR;
1261 if (len)
1262 *len = res->len;
1263
1264 ret = g_malloc0 (res->len + 1);
1265 memcpy (ret, res->v_str, res->len + 1);
1266 }
1267 kb_item_free (res);
1268 return ret;
1269 }
1270
1271 /* More than one value - we will fork() then */
1272 sig_n (SIGCHLD, plug_get_key_sigchld);
1273 res_list = res;
1274 while (res)
1275 {
1276 int pret = plug_fork_child (kb);
1277
1278 if (pret == 0)
1279 {
1280 /* Forked child. */
1281 void *ret;
1282
1283 if (res->type == KB_TYPE_INT)
1284 {
1285 if (type != NULL)
1286 *type = KB_TYPE_INT;
1287 ret = g_memdup2 (&res->v_int, sizeof (res->v_int));
1288 }
1289 else
1290 {
1291 if (type != NULL)
1292 *type = KB_TYPE_STR;
1293 if (len)
1294 *len = res->len;
1295
1296 ret = g_malloc0 (res->len + 1);
1297 memcpy (ret, res->v_str, res->len + 1);
1298 }
1299 kb_item_free (res_list);
1300 return ret;
1301 }
1302 else if (pret == -1)
1303 return NULL;
1304 res = res->next;
1305 }
1306 kb_item_free (res_list);
1307
1308 // Allow to return to the main process if parent process is openvas-nasl.
1309 // So, the main process can do e.g. a kb clean up
1310 if (args->standalone)
1311 return NULL;
1312
1313 _exit (0);
1314}
static void plug_get_key_sigchld(int s)
Definition plugutils.c:1158

References script_infos::key, len, name, plug_fork_child(), plug_get_key_sigchld(), sig_n(), and script_infos::standalone.

Referenced by banner_grab(), get_kb_item(), get_ssh_port(), nasl_open_sock_kdc(), and plugin_do_run().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ plug_get_key_sigchld()

void plug_get_key_sigchld ( int s)
static

Definition at line 1158 of file plugutils.c.

1159{
1160 int status;
1161 (void) s;
1162
1163 wait (&status);
1164}

Referenced by plug_get_key().

Here is the caller graph for this function:

◆ plug_get_port_transport()

int plug_get_port_transport ( struct script_infos * args,
int port )

Definition at line 1391 of file plugutils.c.

1392{
1393 char s[256];
1394 int trp;
1395
1396 snprintf (s, sizeof (s), "Transports/TCP/%d", port);
1397 trp = kb_item_get_int (plug_get_kb (args), s);
1398 if (trp >= 0)
1399 return trp;
1400 else
1401 return OPENVAS_ENCAPS_IP; /* Change this to 0 for ultra smart SSL
1402 negotiation, at the expense of possibly
1403 breaking stuff */
1404}
@ OPENVAS_ENCAPS_IP
Definition network.h:31

References OPENVAS_ENCAPS_IP, and plug_get_kb().

Referenced by get_port_transport(), and open_stream_auto_encaps_ext().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ plug_replace_key()

void plug_replace_key ( struct script_infos * args,
char * name,
int type,
void * value )

◆ plug_replace_key_len()

void plug_replace_key_len ( struct script_infos * args,
char * name,
int type,
void * value,
size_t len )

Definition at line 1113 of file plugutils.c.

1115{
1116 kb_t kb = plug_get_kb (args);
1117
1118 if (name == NULL || value == NULL)
1119 return;
1120
1121 if (type == ARG_STRING)
1122 {
1123 if (add_kb_usage (args, len) == -1)
1124 return;
1125 kb_item_set_str (kb, name, value, len);
1126 }
1127 else if (type == ARG_INT)
1128 kb_item_set_int (kb, name, GPOINTER_TO_SIZE (value));
1129 if (global_nasl_debug == 1)
1130 {
1131 if (type == ARG_STRING)
1132 g_message ("replace key %s -> %s", name, (char *) value);
1133 else if (type == ARG_INT)
1134 g_message ("replace key %s -> %d", name,
1135 (int) GPOINTER_TO_SIZE (value));
1136 }
1137}
int global_nasl_debug
Definition plugutils.c:39
static int add_kb_usage(struct script_infos *args, size_t size)
Definition plugutils.c:69
#define ARG_STRING
Definition plugutils.h:19

References add_kb_usage(), ARG_INT, ARG_STRING, global_nasl_debug, len, name, and plug_get_kb().

Referenced by plug_replace_key(), and replace_kb_item().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ plug_set_dep()

void plug_set_dep ( struct script_infos * args,
const char * depname )

Definition at line 104 of file plugutils.c.

105{
106 nvti_t *n = args->nvti;
107 gchar *old = nvti_dependencies (n);
108 gchar *new;
109
110 if (!depname)
111 return;
112
113 if (old)
114 {
115 new = g_strdup_printf ("%s, %s", old, depname);
116 nvti_set_dependencies (n, new);
117 g_free (new);
118 }
119 else
120 nvti_set_dependencies (n, depname);
121}
nvti_t * nvti
Definition scanneraux.h:33

References script_infos::nvti.

Referenced by script_dependencies().

Here is the caller graph for this function:

◆ plug_set_key()

void plug_set_key ( struct script_infos * args,
char * name,
int type,
const void * value )

Definition at line 1055 of file plugutils.c.

1057{
1058 plug_set_key_len (args, name, type, value, 0);
1059}
void plug_set_key_len(struct script_infos *args, char *name, int type, const void *value, size_t len)
Definition plugutils.c:1028

References name, and plug_set_key_len().

Referenced by banner_grab(), host_add_port_proto(), mark_unknown_svc(), mark_wrapped_svc(), nasl_update_table_driven_lsc_data(), plug_set_port_transport(), plug_set_ssl_item(), plugin_do_run(), plugin_run_openvas_tcp_scanner(), plugin_run_synscan(), register_service(), scan(), set_kb_item(), and simple_register_host_detail().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ plug_set_key_len()

void plug_set_key_len ( struct script_infos * args,
char * name,
int type,
const void * value,
size_t len )

Definition at line 1028 of file plugutils.c.

1030{
1031 kb_t kb = plug_get_kb (args);
1032 int pos = 0; // Append the item on the right position of the list
1033
1034 if (name == NULL || value == NULL)
1035 return;
1036
1037 if (type == ARG_STRING)
1038 {
1039 if (add_kb_usage (args, len) == -1)
1040 return;
1041 kb_item_add_str_unique (kb, name, value, len, pos);
1042 }
1043 else if (type == ARG_INT)
1044 kb_item_add_int_unique (kb, name, GPOINTER_TO_SIZE (value));
1045 if (global_nasl_debug == 1)
1046 {
1047 if (type == ARG_STRING)
1048 g_message ("set key %s -> %s", name, (char *) value);
1049 else if (type == ARG_INT)
1050 g_message ("set key %s -> %d", name, (int) GPOINTER_TO_SIZE (value));
1051 }
1052}

References add_kb_usage(), ARG_INT, ARG_STRING, global_nasl_debug, len, name, and plug_get_kb().

Referenced by plug_set_key(), and set_kb_item().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ plug_set_key_len_volatile()

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.

Parameters
argsScript infos.
nameKey name.
typeKey type.
valueKey value.
expireKey expire in seconds.
lenLen of value.

Definition at line 1072 of file plugutils.c.

1074{
1075 kb_t kb = plug_get_kb (args);
1076 int pos = 0; // Append the item on the right position of the list
1077
1078 if (name == NULL || value == NULL || expire == -1)
1079 return;
1080
1081 if (type == ARG_STRING)
1082 kb_add_str_unique_volatile (kb, name, value, expire, len, pos);
1083 else if (type == ARG_INT)
1084 kb_add_int_unique_volatile (kb, name, GPOINTER_TO_SIZE (value),
1085 GPOINTER_TO_SIZE (expire));
1086 if (global_nasl_debug == 1)
1087 {
1088 if (type == ARG_STRING)
1089 g_message ("set volatile key %s -> %s", name, (char *) value);
1090 else if (type == ARG_INT)
1091 g_message ("set volatile key %s -> %d", name,
1092 (int) GPOINTER_TO_SIZE (value));
1093 }
1094}

References ARG_INT, ARG_STRING, global_nasl_debug, len, name, and plug_get_kb().

Referenced by plug_set_key_volatile(), and set_kb_item_volatile().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ plug_set_key_volatile()

void plug_set_key_volatile ( struct script_infos * args,
char * name,
int type,
const void * value,
int expire )

Set volatile key with expire.

Parameters
argsScript infos.
nameKey name.
typeKey type.
valueKey value.
expireKey expire in seconds.

Definition at line 1106 of file plugutils.c.

1108{
1109 plug_set_key_len_volatile (args, name, type, value, expire, 0);
1110}
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.
Definition plugutils.c:1072

References name, and plug_set_key_len_volatile().

Referenced by set_kb_item_volatile().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ plug_set_port_transport()

void plug_set_port_transport ( struct script_infos * args,
int port,
int tr )
Todo
Those brain damaged functions should probably be in another file They are use to remember who speaks SSL or not

Definition at line 1378 of file plugutils.c.

1379{
1380 char s[256];
1381
1382 snprintf (s, sizeof (s), "Transports/TCP/%d", port);
1383 plug_set_key (args, s, ARG_INT, GSIZE_TO_POINTER (tr));
1384}

References ARG_INT, and plug_set_key().

Referenced by open_stream_auto_encaps_ext(), and plugin_do_run().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ plug_set_ssl_CA_file()

void plug_set_ssl_CA_file ( struct script_infos * args,
char * key )
Todo
Also, all plug_set_ssl*-functions set values that are only accessed in network.c:open_stream_connection under specific conditions. Check whether these conditions can actually occur. Document the functions on the way.

Definition at line 1437 of file plugutils.c.

1438{
1439 plug_set_ssl_item (args, "CA", key);
1440}
static void plug_set_ssl_item(struct script_infos *args, char *item, char *itemfname)
Definition plugutils.c:1407

References plug_set_ssl_item().

Referenced by plugin_run_find_service().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ plug_set_ssl_cert()

void plug_set_ssl_cert ( struct script_infos * args,
char * cert )

Definition at line 1415 of file plugutils.c.

1416{
1417 plug_set_ssl_item (args, "cert", cert);
1418}

References plug_set_ssl_item().

Referenced by plugin_run_find_service().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ plug_set_ssl_item()

void plug_set_ssl_item ( struct script_infos * args,
char * item,
char * itemfname )
static

Definition at line 1407 of file plugutils.c.

1408{
1409 char s[256];
1410 snprintf (s, sizeof (s), "SSL/%s", item);
1411 plug_set_key (args, s, ARG_STRING, itemfname);
1412}

References ARG_STRING, and plug_set_key().

Referenced by plug_set_ssl_CA_file(), plug_set_ssl_cert(), plug_set_ssl_key(), and plug_set_ssl_pem_password().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ plug_set_ssl_key()

void plug_set_ssl_key ( struct script_infos * args,
char * key )

Definition at line 1421 of file plugutils.c.

1422{
1423 plug_set_ssl_item (args, "key", key);
1424}

References plug_set_ssl_item().

Referenced by plugin_run_find_service().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ plug_set_ssl_pem_password()

void plug_set_ssl_pem_password ( struct script_infos * args,
char * key )

Definition at line 1427 of file plugutils.c.

1428{
1429 plug_set_ssl_item (args, "password", key);
1430}

References plug_set_ssl_item().

Referenced by plugin_run_find_service().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ post_alarm()

void post_alarm ( const char * oid,
struct script_infos * desc,
int port,
const char * action,
const char * uri )

Definition at line 769 of file plugutils.c.

771{
772 proto_post_alarm (oid, desc, port, "tcp", action, uri);
773}
void proto_post_alarm(const char *oid, struct script_infos *desc, int port, const char *proto, const char *action, const char *uri)
Definition plugutils.c:762

References oid, and proto_post_alarm().

Referenced by mark_fssniffer(), mark_netbus_server(), mark_sub7_server(), mark_wild_shell(), and security_message().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ post_error()

void post_error ( const char * oid,
struct script_infos * desc,
int port,
const char * action,
const char * uri )

Definition at line 813 of file plugutils.c.

815{
816 proto_post_error (oid, desc, port, "tcp", action, uri);
817}
void proto_post_error(const char *oid, struct script_infos *desc, int port, const char *proto, const char *action, const char *uri)
Definition plugutils.c:806

References oid, and proto_post_error().

Referenced by error_message2().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ post_log()

void post_log ( const char * oid,
struct script_infos * desc,
int port,
const char * action )

Post a log message about a tcp port.

Definition at line 789 of file plugutils.c.

791{
792 proto_post_log (oid, desc, port, "tcp", action, NULL);
793}
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.
Definition plugutils.c:779

References oid, and proto_post_log().

Referenced by mark_acap_server(), mark_adsgone(), mark_auth_server(), mark_avotus_mm_server(), mark_BitTorrent_server(), mark_chargen_server(), mark_citrix_server(), mark_cvslockserver(), mark_cvspserver(), mark_cvsupserver(), mark_dameware_server(), mark_dictd_server(), mark_direct_connect_hub(), mark_echo_server(), mark_eggdrop_server(), mark_ens_server(), mark_exchg_routing_server(), mark_finger_server(), mark_ftp_server(), mark_fw1(), mark_giop_server(), mark_gnocatan_server(), mark_gnome14_server(), mark_gnuserv(), mark_gopher_server(), mark_http_proxy(), mark_http_server(), mark_imap_server(), mark_interscan_viruswall(), mark_ircxpro_admin_server(), mark_iss_realsecure(), mark_jabber_server(), mark_linuxconf(), mark_LISa_server(), mark_listserv_server(), mark_locked_adsubtract_server(), mark_lpd_server(), mark_lyskom_server(), mark_mldonkey(), mark_mon_server(), mark_mongodb(), mark_msdtc_server(), mark_mysql(), mark_nagiosd_server(), mark_nntp_server(), mark_ofa_express_server(), mark_pblocald_server(), mark_pbmaster_server(), mark_ph_server(), mark_pnsclient(), mark_pop3pw_server(), mark_pop_server(), mark_postgresql(), mark_ppp_daemon(), mark_psybnc(), mark_quicktime_streaming_server(), mark_remote_nc_server(), mark_rmserver(), mark_rsync(), mark_shoutcast_server(), mark_smppd_server(), mark_smtp_server(), mark_smux_server(), mark_snpp_server(), mark_socks_proxy(), mark_spamd_server(), mark_sphinxql(), mark_ssh_server(), mark_stonegate_auth_server(), mark_tcpmux_server(), mark_teamspeak2_server(), mark_telnet_server(), mark_time_server(), mark_unknown_svc(), mark_upsmon_server(), mark_uucp_server(), mark_veritas_backup(), mark_vmware_auth(), mark_vtun_server(), mark_websm_server(), mark_whois_plus2_server(), mark_wrapped_svc(), mark_zebra_server(), and plugin_do_run().

Here is the call graph for this function:

◆ post_log_with_uri()

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.

Definition at line 799 of file plugutils.c.

801{
802 proto_post_log (oid, desc, port, "tcp", action, uri);
803}

References oid, and proto_post_log().

Referenced by log_message().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ proto_post_alarm()

void proto_post_alarm ( const char * oid,
struct script_infos * desc,
int port,
const char * proto,
const char * action,
const char * uri )

Definition at line 762 of file plugutils.c.

764{
765 proto_post_wrapped (oid, desc, port, proto, action, ALARM, uri);
766}
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 ...).
Definition plugutils.c:704

References ALARM, oid, and proto_post_wrapped().

Referenced by post_alarm(), and security_message().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ proto_post_error()

void proto_post_error ( const char * oid,
struct script_infos * desc,
int port,
const char * proto,
const char * action,
const char * uri )

Definition at line 806 of file plugutils.c.

808{
809 proto_post_wrapped (oid, desc, port, proto, action, ERRMSG, uri);
810}

References ERRMSG, oid, and proto_post_wrapped().

Referenced by error_message2(), and post_error().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ proto_post_log()

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.

Definition at line 779 of file plugutils.c.

781{
782 proto_post_wrapped (oid, desc, port, proto, action, LOG, uri);
783}

References LOG, oid, and proto_post_wrapped().

Referenced by log_message(), post_log(), and post_log_with_uri().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ proto_post_wrapped()

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 )
static

Post a security message (e.g. LOG, NOTE, WARNING ...).

Parameters
oidThe oid of the NVT
descThe script infos where to get settings.
portPort number related to the issue.
protoProtocol related to the issue (tcp or udp).
actionThe actual result text
msg_typeThe message type.
uriLocation like file path or webservice URL.

Definition at line 704 of file plugutils.c.

707{
708 const char *hostname = "";
709 char *buffer, *data, port_s[16] = "general";
710 char ip_str[INET6_ADDRSTRLEN];
711 GError *err = NULL;
712 GString *action_str;
713 gsize length;
714
715 /* Should not happen, just to avoid trouble stop here if no NVTI found */
716 if (!oid)
717 return;
718
719 if (action == NULL)
720 action_str = g_string_new ("");
721 else
722 {
723 action_str = g_string_new (action);
724 g_string_append (action_str, "\n");
725 }
726
727 if (port > 0)
728 snprintf (port_s, sizeof (port_s), "%d", port);
729 if (current_vhost)
730 hostname = current_vhost->value;
731 else if (desc->vhosts)
732 hostname = ((gvm_vhost_t *) desc->vhosts->data)->value;
733 addr6_to_str (plug_get_host_ip (desc), ip_str);
734 buffer = g_strdup_printf ("%s|||%s|||%s|||%s/%s|||%s|||%s|||%s",
735 msg_type_to_str (msg_type), ip_str,
736 hostname ? hostname : " ", port_s, proto, oid,
737 action_str->str, uri ? uri : "");
738
739 /* Convert to UTF-8 before sending to Manager only if necessary. */
740 if (is_utf8_encoded (desc->name) > 0)
741 data = g_strdup (buffer);
742 else
743 data = g_convert (buffer, -1, "UTF-8", "ISO_8859-1", NULL, &length, &err);
744
745 if (!data)
746 {
747 g_warning ("%s: Error converting to UTF-8: %s\nOriginal string: %s",
748 __func__, err->message, buffer);
749 g_free (buffer);
750 g_string_free (action_str, TRUE);
751 return;
752 }
753
754 kb_item_push_str_with_main_kb_check (get_main_kb (), "internal/results",
755 data);
756 g_free (data);
757 g_free (buffer);
758 g_string_free (action_str, TRUE);
759}
u_short length
static const char * msg_type_to_str(msg_t type)
Return string representation of the given msg_t.
Definition plugutils.c:390
static int is_utf8_encoded(const char *filename)
Definition plugutils.c:664
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....
Definition plugutils.c:533

References current_vhost, get_main_kb(), hostname, is_utf8_encoded(), kb_item_push_str_with_main_kb_check(), length, msg_type_to_str(), script_infos::name, oid, plug_get_host_ip(), and script_infos::vhosts.

Referenced by proto_post_alarm(), proto_post_error(), and proto_post_log().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ scanner_add_port()

void scanner_add_port ( struct script_infos * args,
int port,
char * proto )

Definition at line 1146 of file plugutils.c.

1147{
1148 host_add_port_proto (args, port, proto);
1149}
static void host_add_port_proto(struct script_infos *args, int portnum, char *proto)
Definition plugutils.c:124

References host_add_port_proto().

Referenced by banner_grab(), nasl_scanner_add_port(), sendpacket(), and v6_sendpacket().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sig_n()

void sig_n ( int signo,
void(* fnc )(int) )
static

Definition at line 1167 of file plugutils.c.

1168{
1169 struct sigaction sa;
1170
1171 sa.sa_handler = fnc;
1172 sa.sa_flags = 0;
1173 sigemptyset (&sa.sa_mask);
1174 sigaction (signo, &sa, (struct sigaction *) 0);
1175}

Referenced by plug_fork_child(), and plug_get_key().

Here is the caller graph for this function:

◆ unscanned_ports_as_closed()

int unscanned_ports_as_closed ( port_protocol_t ptype)
static

Report state of preferences "unscanned_closed".

Returns
0 if pref is "yes", 1 otherwise.

Definition at line 137 of file plugutils.c.

138{
139 if (ptype == PORT_PROTOCOL_UDP)
140 return (prefs_get_bool ("unscanned_closed_udp") ? 0 : 1);
141
142 return (prefs_get_bool ("unscanned_closed") ? 0 : 1);
143}

Referenced by kb_get_port_state_proto().

Here is the caller graph for this function:

Variable Documentation

◆ current_vhost

gvm_vhost_t* current_vhost = NULL

◆ global_nasl_debug

int global_nasl_debug = 0

◆ kb_usage

size_t kb_usage = 0
static

Definition at line 42 of file plugutils.c.

Referenced by add_kb_usage().

◆ max_kb_usage

size_t max_kb_usage
static

Definition at line 44 of file plugutils.c.

Referenced by add_kb_usage(), and init_kb_usage().