36#include <bsd/unistd.h>
41#include <gnutls/gnutls.h>
43#include <gvm/base/logging.h>
44#include <gvm/base/nvti.h>
45#include <gvm/base/prefs.h>
46#include <gvm/base/version.h>
47#include <gvm/util/kb.h>
48#include <gvm/util/mqtt.h>
49#include <gvm/util/nvticache.h>
50#include <gvm/util/uuidutils.h>
61#ifdef GIT_REV_AVAILABLE
62#include "gitrevision.h"
65#if GNUTLS_VERSION_NUMBER < 0x030300
73#define G_LOG_DOMAIN "sd main"
75#define PROCTITLE_WAITING "openvas: Waiting for incoming connections"
76#define PROCTITLE_LOADING "openvas: Loading Handler"
77#define PROCTITLE_RELOADING "openvas: Reloading"
78#define PROCTITLE_SERVING "openvas: Serving %s"
110 {
"plugins_folder", OPENVAS_NVT_DIR},
111 {
"include_folders", OPENVAS_NVT_DIR},
112 {
"plugins_timeout", G_STRINGIFY (NVT_TIMEOUT)},
113 {
"scanner_plugins_timeout", G_STRINGIFY (SCANNER_NVT_TIMEOUT)},
114 {
"db_address", KB_PATH_DEFAULT},
130 g_message (
"(%d) %s", level, text);
138 if ((str = prefs_get (
"max_hosts")) != NULL)
145 if ((str = prefs_get (
"max_checks")) != NULL)
152 if ((str = prefs_get (
"max_sysload")) != NULL)
159 if ((str = prefs_get (
"min_free_mem")) != NULL)
204 struct kb_item *res = NULL;
206 g_debug (
"Start loading scan preferences.");
210 snprintf (key,
sizeof (key),
"internal/%s/scanprefs", globals->
scan_id);
212 kb = kb_find (prefs_get (
"db_address"), key);
219 res = kb_item_get_all (kb, key);
225 gchar **pref = g_strsplit (res->v_str,
"|||", 2);
228 gchar **pref_name = g_strsplit (pref[0],
":", 3);
229 if (pref_name[1] && pref_name[2] && !strncmp (pref_name[2],
"file", 4)
230 && strcmp (pref[1],
""))
232 char *file_uuid = gvm_uuid_make ();
234 prefs_set (pref[0], file_uuid);
235 ret =
store_file (globals, pref[1], file_uuid);
237 g_debug (
"Load preference: Failed to upload file "
238 "for nvt %s preference.",
244 g_warning (
"%s is a scanner only preference. It can not be written "
245 "by the client and will be ignored.",
248 prefs_set (pref[0], pref[1] ? pref[1] :
"");
249 g_strfreev (pref_name);
255 kb_del_items (kb, key);
256 snprintf (key,
sizeof (key),
"internal/%s", globals->
scan_id);
261 g_debug (
"End loading scan preferences.");
275 static gchar *log_config_file_name = NULL;
278 log_config_file_name =
279 g_build_filename (OPENVAS_SYSCONF_DIR,
"openvas_log.conf", NULL);
280 if (g_file_test (log_config_file_name, G_FILE_TEST_EXISTS))
281 log_config = load_log_configuration (log_config_file_name);
285 g_warning (
"%s: Can not open or create log file or directory. "
286 "Please check permissions of log files listed in %s.",
287 __func__, log_config_file_name);
288 g_free (log_config_file_name);
291 g_free (log_config_file_name);
299 if (gcry_control (GCRYCTL_ANY_INITIALIZATION_P))
301 gcry_check_version (NULL);
302 gcry_control (GCRYCTL_SUSPEND_SECMEM_WARN);
303 gcry_control (GCRYCTL_INIT_SECMEM, 16384, 0);
304 gcry_control (GCRYCTL_RESUME_SECMEM_WARN);
305 gcry_control (GCRYCTL_INITIALIZATION_FINISHED);
314#if GNUTLS_VERSION_NUMBER < 0x030300
316 g_message (
"Could not initialize openvas SSL!");
319 if (prefs_get (
"debug_tls") != NULL && atoi (prefs_get (
"debug_tls")) > 0)
321 g_warning (
"TLS debug is enabled and should only be used with care, "
322 "since it may reveal sensitive information in the scanner "
323 "logs and might make openvas fill your disk rather quickly.");
325 gnutls_global_set_log_level (atoi (prefs_get (
"debug_tls")));
335#ifdef OPENVAS_GIT_REVISION
336 g_message (
"openvas %s (GIT revision %s) started", OPENVAS_VERSION,
337 OPENVAS_GIT_REVISION);
339 g_message (
"openvas %s started", OPENVAS_VERSION);
361 snprintf (key,
sizeof (key),
"internal/%s",
get_scan_id ());
362 kb = kb_find (prefs_get (
"db_address"), key);
366 pid = kb_item_get_int (kb,
"internal/ovas_pid");
377 killpg (
pid, SIGUSR1);
393 snprintf (key,
sizeof (key),
"internal/%s/scanprefs",
get_scan_id ());
394 kb = kb_find (prefs_get (
"db_address"), key);
395 kb_item_push_str (kb,
"internal/results", msg);
396 snprintf (key,
sizeof (key),
"internal/%s",
get_scan_id ());
397 kb_item_set_str (kb, key,
"finished", 0);
412 const char *mqtt_server_uri;
413 const char *openvasd_server_uri;
416 prefs_config (config_file);
419 if (prefs_get (
"vendor_version") != NULL)
426 g_message (
"Failed to initialize nvti cache.");
428 "ERRMSG||| ||| ||| ||| |||NVTI cache initialization failed");
435 openvasd_server_uri = prefs_get (
"openvasd_server");
436 if (openvasd_server_uri)
438 g_message (
"%s: LSC via openvasd", __func__);
439 prefs_set (
"openvasd_lsc_enabled",
"yes");
443 mqtt_server_uri = prefs_get (
"mqtt_server_uri");
447 const char *mqtt_user = prefs_get (
"mqtt_user");
448 const char *mqtt_pass = prefs_get (
"mqtt_pass");
449 if ((mqtt_init_auth (mqtt_server_uri, mqtt_user, mqtt_pass)) != 0)
451 if ((mqtt_init (mqtt_server_uri)) != 0)
454 g_message (
"%s: INIT MQTT: FAIL", __func__);
456 "ERRMSG||| ||| ||| ||| |||MQTT initialization failed");
460 g_message (
"%s: INIT MQTT: SUCCESS", __func__);
461 prefs_set (
"mqtt_enabled",
"yes");
466 g_message (
"%s: Neither openvasd_server nor mqtt_server_uri given, "
480 g_warning (
"No preferences found for the scan %s", globals->
scan_id);
497 setproctitle_init (argc, argv, env);
500 static gboolean display_version = FALSE;
501 static gchar *config_file = NULL;
503 static gchar *stop_scan_id = NULL;
504 static gboolean print_specs = FALSE;
505 static gboolean print_sysconfdir = FALSE;
506 static gboolean update_vt_info = FALSE;
507 GError *error = NULL;
508 GOptionContext *option_context;
509 static GOptionEntry entries[] = {
510 {
"version",
'V', 0, G_OPTION_ARG_NONE, &display_version,
511 "Display version information", NULL},
512 {
"config-file",
'c', 0, G_OPTION_ARG_FILENAME, &config_file,
513 "Configuration file",
"<filename>"},
514 {
"cfg-specs",
's', 0, G_OPTION_ARG_NONE, &print_specs,
515 "Print configuration settings", NULL},
516 {
"sysconfdir",
'y', 0, G_OPTION_ARG_NONE, &print_sysconfdir,
517 "Print system configuration directory (set at compile time)", NULL},
518 {
"update-vt-info",
'u', 0, G_OPTION_ARG_NONE, &update_vt_info,
519 "Updates VT info into redis store from VT files", NULL},
520 {
"scan-start",
'\0', 0, G_OPTION_ARG_STRING, &
scan_id,
521 "ID of scan to start. ID and related data must be stored into redis "
524 {
"scan-stop",
'\0', 0, G_OPTION_ARG_STRING, &stop_scan_id,
525 "ID of scan to stop",
"<string>"},
527 {NULL, 0, 0, 0, NULL, NULL, NULL}};
530 g_option_context_new (
"- Open Vulnerability Assessment Scanner");
531 g_option_context_add_main_entries (option_context, entries, NULL);
532 if (!g_option_context_parse (option_context, &argc, &argv, &error))
534 g_print (
"%s\n\n", error->message);
537 g_option_context_free (option_context);
540 if (print_sysconfdir)
542 g_print (
"%s\n", SYSCONFDIR);
549 printf (
"OpenVAS %s\n", OPENVAS_VERSION);
550#ifdef OPENVAS_GIT_REVISION
551 printf (
"GIT revision %s\n", OPENVAS_GIT_REVISION);
553 printf (
"gvm-libs %s\n", gvm_libs_version ());
554 printf (
"Most new code since 2005: (C) 2024 Greenbone AG\n");
556 "Nessus origin: (C) 2004 Renaud Deraison <deraison@nessus.org>\n");
557 printf (
"License GPLv2: GNU GPL version 2\n");
559 "This is free software: you are free to change and redistribute it.\n"
560 "There is NO WARRANTY, to the extent permitted by law.\n\n");
565 if (setenv (
"TZ",
"utc 0", 1) == -1)
567 g_print (
"%s\n\n", strerror (errno));
572#ifdef LOG_REFERENCES_AVAILABLE
576 set_log_reference (stop_scan_id);
583 g_message (
"Sentry is enabled. This can log sensitive information.");
588 config_file = OPENVAS_CONF;
593 prefs_config (config_file);
598 return err ? EXIT_FAILURE : EXIT_SUCCESS;
605 prefs_config (config_file);
608 g_message (
"Failed to initialize nvti cache. Not possible to "
619#ifdef LOG_REFERENCES_AVAILABLE
620 free_log_reference ();
622 return err ? EXIT_FAILURE : EXIT_SUCCESS;
628 int attack_error = 0;
643#ifdef LOG_REFERENCES_AVAILABLE
644 free_log_reference ();
649 g_warning (
"Scan ending with FAILURE status");
658 prefs_config (config_file);
int attack_network(struct scan_globals *globals)
Attack a whole network. return 0 on successes, -1 if there was a critical error.
int init_sentry(void)
Init sentry.
debug_utils.c headerfile.
void set_main_kb(kb_t kb)
sets the shared database between ospd and openvas as a main_kb for further usage. @description this s...
Header file to cache main_kb.
int openvas_SSL_init()
Initializes SSL support.
Header file for module network.
static volatile int termination_signal
int openvas(int argc, char *argv[], char *env[])
openvas.
static void my_gnutls_log_func(int level, const char *text)
static int attack_network_init(struct scan_globals *globals, const gchar *config_file)
Set up data needed for attack_network().
static void openvas_print_start_msg()
Print start message.
static void set_globals_from_preferences(void)
GSList * log_config
Logging parameters, as passed to setup_log_handlers.
static int overwrite_openvas_prefs_with_prefs_from_client(struct scan_globals *globals)
Read the scan preferences from redis.
static void set_default_openvas_prefs()
Set the prefs from the openvas_defaults array.
static void send_message_to_client_and_finish_scan(const char *msg)
Send a failure message and set the scan as finished.
static openvas_option openvas_defaults[]
Default values for scanner options. Must be NULL terminated.
static void handle_termination_signal(int sig)
static void init_signal_handlers(void)
Initializes main scanner process' signal handlers.
static int init_logging()
Init logging.
static void gcrypt_init(void)
static int stop_single_task_scan(void)
Search in redis the process ID of a running scan and sends it the kill signal SIGUSR1,...
static void check_tls()
Check TLS.
int plugins_init(void)
main function for loading all the plugins
int plugins_cache_init(void)
Main function for nvticache initialization without loading the plugins.
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....
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....
Header file for module plugutils.
void procs_terminate_childs(void)
This function terminates all processes spawned with create_process. Calls terminate_child for each pr...
int set_scan_id(const char *new_scan_id)
const char * get_scan_id()
void destroy_scan_globals(struct scan_globals *globals)
void(*)(int) openvas_signal(int signum, void(*handler)(int))
void sighand_chld(int sig)
headerfile for sighand.c.
int is_scanner_only_pref(const char *pref)
int store_file(struct scan_globals *globals, const char *file, const char *file_hash)
Stores a file type preference in a hash table.
void vendor_version_set(const gchar *version)
Set vendor version.
Header file: vendor version functions prototypes.