55 #include <openvas/misc/openvas_proctitle.h> 56 #include <openvas/misc/openvas_logging.h> 57 #include <openvas/base/pidfile.h> 58 #include <openvas/base/nvticache.h> 59 #include <openvas/base/kb.h> 60 #include <openvas/base/gpgme_util.h> 61 #include <openvas/misc/prefs.h> 62 #include <openvas/misc/vendorversion.h> 75 #if GNUTLS_VERSION_NUMBER < 0x030300 76 #include <openvas/misc/network.h> 79 #ifdef SVN_REV_AVAILABLE 80 #include "svnrevision.h" 89 static int global_iana_socket = -1;
91 static volatile int loading_stop_signal = 0;
92 static volatile int reload_signal = 0;
93 static volatile int termination_signal = 0;
105 {
"plugins_folder", OPENVAS_NVT_DIR},
106 {
"cache_folder", OPENVAS_CACHE_DIR},
107 {
"include_folders", OPENVAS_NVT_DIR},
109 {
"max_checks",
"10"},
111 {
"logfile", OPENVASSD_MESSAGES},
112 {
"log_whole_attack",
"no"},
113 {
"log_plugins_name_at_load",
"no"},
114 {
"dumpfile", OPENVASSD_DEBUGMSG},
115 {
"cgi_path",
"/cgi-bin:/scripts"},
116 {
"optimize_test",
"yes"},
117 {
"checks_read_timeout",
"5"},
118 {
"network_scan",
"no"},
119 {
"non_simult_ports",
"139, 445"},
120 {
"plugins_timeout", G_STRINGIFY (NVT_TIMEOUT)},
121 {
"scanner_plugins_timeout", G_STRINGIFY (SCANNER_NVT_TIMEOUT)},
122 {
"safe_checks",
"yes"},
123 {
"auto_enable_dependencies",
"yes"},
124 {
"use_mac_addr",
"no"},
125 {
"nasl_no_signature_check",
"yes"},
126 {
"drop_privileges",
"no"},
127 {
"unscanned_closed",
"yes"},
128 {
"unscanned_closed_udp",
"yes"},
132 {
"report_host_details",
"yes"},
133 {
"kb_location", KB_PATH_DEFAULT},
134 {
"timeout_retry",
"3"},
135 {
"time_between_request",
"0"},
142 start_daemon_mode (
void)
148 close (global_iana_socket);
153 log_write (
"Warning: Cannot set process group leader (%s)",
157 if ((fd = open (
"/dev/tty", O_RDWR)) >= 0)
161 if ((fd = open (
"/dev/null", O_RDONLY)) < 0)
163 log_write (
"Cannot open /dev/null (%s) -- aborting", strerror (errno));
171 if ((s = prefs_get (
"dumpfile")) == 0)
172 s = OPENVASSD_DEBUGMSG;
174 if (strcmp (s,
"-") == 0)
180 if ((fd = open (s, O_WRONLY | O_CREAT | O_APPEND, 0600)) < 0)
182 log_write (
"Cannot create a new dumpfile %s (%s)-- aborting", s,
196 end_daemon_mode (
void)
203 set_globals_from_preferences (
void)
207 if ((str = prefs_get (
"max_hosts")) != NULL)
214 if ((str = prefs_get (
"max_checks")) != NULL)
223 handle_reload_signal (
int sig)
229 handle_termination_signal (
int sig)
231 termination_signal = sig;
240 loading_client_handle (
int soc)
246 setsockopt (soc, SOL_SOCKET, SO_KEEPALIVE, &opt,
sizeof (opt));
256 handle_loading_stop_signal (
int sig)
258 loading_stop_signal = sig;
264 pidfile_remove (
"openvassd");
274 loading_handler_start ()
276 pid_t child_pid, parent_pid;
279 parent_pid = getpid ();
284 proctitle_set (
"openvassd (Loading Handler)");
293 unsigned int lg_address;
294 struct sockaddr_un address;
297 struct timeval timeout;
301 if (loading_stop_signal || kill (parent_pid, 0) < 0)
303 lg_address =
sizeof (
struct sockaddr_un);
305 if (listen (global_iana_socket, 5) < 0)
309 FD_SET(global_iana_socket, &
set);
312 timeout.tv_usec = 500000;
314 rv = select(global_iana_socket + 1, &
set, NULL, NULL, &timeout);
320 soc = accept (global_iana_socket, (
struct sockaddr *) (&address),
325 child_pid1 = fork ();
328 loading_client_handle (soc);
332 waitpid (child_pid1, &ret, WNOHANG);
353 init_signal_handlers ()
366 const char *config_file;
377 handler_pid = loading_handler_start ();
381 config_file = prefs_get (
"config_file");
382 for (i = 0; openvassd_defaults[i].
option != NULL; i++)
383 prefs_set (openvassd_defaults[i].option, openvassd_defaults[i].value);
384 prefs_config (config_file);
388 set_globals_from_preferences ();
391 log_write (
"Finished reloading the scanner.");
399 handle_client (
struct arglist *globals)
402 int soc = arg_get_value_int (globals,
"global_socket");
405 start_daemon_mode ();
420 scanner_thread (
struct arglist *globals)
425 soc = arg_get_value_int (globals,
"global_socket");
429 if (prefs_get_bool (
"be_nice"))
432 if (nice(10) == -1 && errno != 0)
434 log_write (
"Unable to renice process: %d", errno);
439 close (global_iana_socket);
442 goto shutdown_and_exit;
444 setsockopt (soc, SOL_SOCKET, SO_KEEPALIVE, &opt,
sizeof (opt));
446 arg_add_value (globals,
"parent_socket", ARG_INT, GSIZE_TO_POINTER (soc));
447 arg_set_value (globals,
"global_socket", GSIZE_TO_POINTER (soc));
451 handle_client (globals);
468 if (termination_signal)
470 log_write (
"Received the %s signal", strsignal (termination_signal));
486 proctitle_set (
"openvassd: Reloading");
488 proctitle_set (
"openvassd: Waiting for incoming connections");
498 stop_all_scans (
void)
502 const gchar *piddir = NULL;
503 gchar *pidstatfn = NULL;
504 gchar **contents_split = NULL;
505 gchar *contents = NULL;
506 GError *error = NULL;
507 gchar *parentID = NULL;
508 gchar *processID = NULL;
510 proc = g_dir_open (
"/proc", 0, &error);
513 log_write (
"Unable to open directory: %s\n", error->message);
514 g_error_free (error);
517 while ((piddir = g_dir_read_name (proc)) != NULL)
520 for (i = 0; i < (int)strlen (piddir); i++)
521 if (!g_ascii_isdigit (piddir[i]))
529 pidstatfn = g_strconcat (
"/proc/", piddir,
"/stat", NULL);
530 if (g_file_get_contents (pidstatfn, &contents, NULL, NULL))
532 contents_split = g_strsplit (contents,
" ", 6);
533 parentID = g_strdup (contents_split[3]);
534 processID = g_strdup (contents_split[0]);
540 g_strfreev (contents_split);
541 contents_split = NULL;
543 if (atoi(parentID) == (
int)getpid())
545 log_write (
"Stopping running scan with PID: %s", processID);
546 kill (atoi(processID), SIGUSR2);
572 int waitredis = 5, waitkb = 5, ret = 0;
575 while (waitredis != 0)
577 ret = kb_new (&kb_access_aux, prefs_get (
"kb_location"));
580 log_write (
"Redis connection lost. Trying to reconnect.");
587 kb_delete (kb_access_aux);
594 log_write (
"Critical Redis connection error.");
600 kb_access_aux = kb_find (prefs_get (
"kb_location"),
"nvticache");
603 log_write (
"Redis kb not found. Trying again in 2 seconds.");
610 kb_lnk_reset (kb_access_aux);
611 g_free (kb_access_aux);
616 if (waitredis != 5 || waitkb == 0)
618 log_write (
"Redis connection error. Stopping all the running scans.");
627 #ifdef OPENVASSD_SVN_REVISION 628 log_write (
"openvassd %s (SVN revision %i) started",
630 OPENVASSD_SVN_REVISION);
632 log_write (
"openvassd %s started", OPENVASSD_VERSION);
634 proctitle_set (
"openvassd: Waiting for incoming connections");
638 unsigned int lg_address;
639 struct sockaddr_un address;
640 struct arglist *globals;
642 struct timeval timeout;
645 check_termination ();
649 lg_address =
sizeof (
struct sockaddr_un);
657 if ((opts = fcntl (global_iana_socket, F_GETFL, 0)) < 0)
659 log_write (
"fcntl: %s", strerror (errno));
662 if (fcntl (global_iana_socket, F_SETFL, opts | O_NONBLOCK) < 0)
664 log_write (
"fcntl: %s", strerror (errno));
668 if (listen (global_iana_socket, 5) < 0)
672 FD_SET(global_iana_socket, &
set);
675 timeout.tv_usec = 500000;
677 rv = select(global_iana_socket + 1, &
set, NULL, NULL, &timeout);
683 soc = accept (global_iana_socket, (
struct sockaddr *) (&address),
689 if (fcntl (global_iana_socket, F_SETFL, opts) < 0)
690 log_write (
"fcntl: %s", strerror (errno));
692 globals = g_malloc0 (
sizeof (
struct arglist));
693 arg_add_value (globals,
"global_socket", ARG_INT, GSIZE_TO_POINTER (soc));
698 log_write (
"Could not fork - client won't be served");
717 init_unix_network (
int *sock,
const char *owner,
const char *group,
720 struct sockaddr_un addr;
725 unix_socket = socket (AF_UNIX, SOCK_STREAM, 0);
726 if (unix_socket == -1)
728 log_write (
"%s: Couldn't create UNIX socket", __FUNCTION__);
731 addr.sun_family = AF_UNIX;
733 if (!stat (addr.sun_path, &ustat))
736 unlink (addr.sun_path);
738 if (bind (unix_socket, (
struct sockaddr *) &addr,
sizeof (
struct sockaddr_un))
741 log_write (
"%s: Error on bind(%s): %s", __FUNCTION__,
748 struct passwd *pwd = getpwnam (owner);
751 log_write (
"%s: User %s not found.", __FUNCTION__, owner);
756 log_write (
"%s: chown: %s", __FUNCTION__, strerror (errno));
763 struct group *grp = getgrnam (group);
766 log_write (
"%s: Group %s not found.", __FUNCTION__, group);
771 log_write (
"%s: chown: %s", __FUNCTION__, strerror (errno));
778 omode = strtol (mode, 0, 8);
779 if (omode <= 0 || omode > 4095)
781 log_write (
"%s: Erroneous liste-mode value", __FUNCTION__);
786 log_write (
"%s: chmod: %s", __FUNCTION__, strerror (errno));
790 if (listen (unix_socket, 128) == -1)
792 log_write (
"%s: Error on listen(): %s", __FUNCTION__, strerror (errno));
806 init_openvassd (
int dont_fork,
const char *config_file)
810 for (i = 0; openvassd_defaults[i].
option != NULL; i++)
811 prefs_set (openvassd_defaults[i].option, openvassd_defaults[i].value);
812 prefs_config (config_file);
815 if (dont_fork == FALSE)
818 set_globals_from_preferences ();
827 int i = open (
"/dev/null", O_RDONLY, 0640);
828 if (dup2 (i, STDIN_FILENO) != STDIN_FILENO)
829 log_write (
"Could not redirect stdin to /dev/null: %s\n", strerror (errno));
830 if (dup2 (i, STDOUT_FILENO) != STDOUT_FILENO)
831 log_write (
"Could not redirect stdout to /dev/null: %s\n",
833 if (dup2 (i, STDERR_FILENO) != STDERR_FILENO)
834 log_write (
"Could not redirect stderr to /dev/null: %s\n",
848 rc = kb_new (&kb, prefs_get (
"kb_location"));
852 rc = kb_flush (kb,
"nvticache");
859 if (gcry_control (GCRYCTL_ANY_INITIALIZATION_P))
861 gcry_check_version (NULL);
862 gcry_control (GCRYCTL_SUSPEND_SECMEM_WARN);
863 gcry_control (GCRYCTL_INIT_SECMEM, 16384, 0);
864 gcry_control (GCRYCTL_RESUME_SECMEM_WARN);
865 gcry_control (GCRYCTL_INITIALIZATION_FINISHED);
879 proctitle_init (argc, argv);
882 static gboolean display_version = FALSE;
883 static gboolean dont_fork = FALSE;
884 static gchar *config_file = NULL;
885 static gchar *vendor_version_string = NULL;
886 static gchar *listen_owner = NULL;
887 static gchar *listen_group = NULL;
888 static gchar *listen_mode = NULL;
889 static gchar *gnupg_dir = NULL;
890 static gboolean print_specs = FALSE;
891 static gboolean print_sysconfdir = FALSE;
892 static gboolean only_cache = FALSE;
893 GError *error = NULL;
894 GOptionContext *option_context;
895 static GOptionEntry entries[] = {
896 {
"version",
'V', 0, G_OPTION_ARG_NONE, &display_version,
897 "Display version information", NULL},
898 {
"foreground",
'f', 0, G_OPTION_ARG_NONE, &dont_fork,
899 "Do not run in daemon mode but stay in foreground", NULL},
900 {
"config-file",
'c', 0, G_OPTION_ARG_FILENAME, &config_file,
901 "Configuration file",
"<filename>"},
902 {
"vendor-version",
'\0', 0, G_OPTION_ARG_STRING, &vendor_version_string,
903 "Use <string> as vendor version.",
"<string>"},
904 {
"cfg-specs",
's', 0, G_OPTION_ARG_NONE, &print_specs,
905 "Print configuration settings", NULL},
906 {
"sysconfdir",
'y', 0, G_OPTION_ARG_NONE, &print_sysconfdir,
907 "Print system configuration directory (set at compile time)", NULL},
908 {
"only-cache",
'C', 0, G_OPTION_ARG_NONE, &only_cache,
909 "Exit once the NVT cache has been initialized or updated", NULL},
911 "Path of unix socket to listen on",
"<filename>"},
912 {
"listen-owner",
'\0', 0, G_OPTION_ARG_STRING, &listen_owner,
913 "Owner of the unix socket",
"<string>"},
914 {
"listen-group",
'\0', 0, G_OPTION_ARG_STRING, &listen_group,
915 "Group of the unix socket",
"<string>"},
916 {
"listen-mode",
'\0', 0, G_OPTION_ARG_STRING, &listen_mode,
917 "File mode of the unix socket",
"<string>"},
918 {
"gnupg-home",
'c', 0, G_OPTION_ARG_STRING, &gnupg_dir,
919 "Gnupg home directory",
"<directory>"},
920 {NULL, 0, 0, 0, NULL, NULL, NULL}
924 g_option_context_new (
"- Scanner of the Open Vulnerability Assessment System");
925 g_option_context_add_main_entries (option_context, entries, NULL);
926 if (!g_option_context_parse (option_context, &argc, &argv, &error))
928 g_print (
"%s\n\n", error->message);
931 g_option_context_free (option_context);
933 if (print_sysconfdir)
935 g_print (
"%s\n", SYSCONFDIR);
940 if (setenv (
"TZ",
"utc 0", 1) == -1)
942 g_print (
"%s\n\n", strerror (errno));
948 unix_socket_path = g_build_filename (OPENVAS_RUN_DIR,
"openvassd.sock", NULL);
952 printf (
"OpenVAS Scanner %s\n", OPENVASSD_VERSION);
953 #ifdef OPENVASSD_SVN_REVISION 954 printf (
"SVN revision %i\n", OPENVASSD_SVN_REVISION);
957 (
"Most new code since 2005: (C) 2016 Greenbone Networks GmbH\n");
959 (
"Nessus origin: (C) 2004 Renaud Deraison <deraison@nessus.org>\n");
960 printf (
"License GPLv2: GNU GPL version 2\n");
962 (
"This is free software: you are free to change and redistribute it.\n" 963 "There is NO WARRANTY, to the extent permitted by law.\n\n");
968 set_gpghome (gnupg_dir);
970 if (vendor_version_string)
971 vendor_version_set (vendor_version_string);
974 config_file = OPENVASSD_CONF;
977 if (init_openvassd (dont_fork, config_file))
984 if (init_openvassd (dont_fork, config_file))
988 if (init_unix_network (&global_iana_socket, listen_owner, listen_group,
1001 #if GNUTLS_VERSION_NUMBER < 0x030300 1002 if (openvas_SSL_init () < 0)
1003 log_write (
"Could not initialize openvas SSL!");
1007 if (dont_fork == FALSE)
1009 pidfile_create (
"openvassd");
1013 handler_pid = loading_handler_start ();
1014 if (handler_pid < 0)
1020 init_signal_handlers ();
void log_init(const char *filename)
Initialization of the log file.
void(*)(int) openvas_signal(int signum, void(*handler)(int))
void log_write(const char *str,...)
Write into the logfile / syslog.
int comm_loading(int soc)
Informs the client that the scanner is still loading.
int ntp_timestamp_scan_ends(int soc)
void attack_network(struct arglist *globals, kb_t *network_kb)
Attack a whole network.
void check_kb_status()
Check if Redis Server is up and if the KB exists. If KB does not exist,force a reload and stop all th...
void loading_handler_stop(pid_t handler_pid)
void log_vwrite(const char *str, va_list arg_ptr)
Write into the logfile / syslog using a va_list.
void sighand_chld(pid_t pid)
int main(int argc, char *argv[])
openvassd.
void destroy_loading_shm(void)
void make_em_die(int sig)
int comm_init(int soc)
Initializes the communication between the scanner (us) and the client.
void(* process_func_t)(void *)
void init_loading_shm(void)
int comm_wait_order(struct arglist *globals)
This function waits for the attack order of the client. Meanwhile, it processes all the messages the ...
int ntp_timestamp_scan_starts(int soc)
int terminate_process(pid_t pid)
void comm_terminate(int soc)
This function must be called at the end of a session.
pid_t create_process(process_func_t function, void *argument)
Create a new process (fork).
void wait_for_children1(void)