52 #include <sys/select.h> 53 #include <sys/socket.h> 55 #include <sys/types.h> 59 #include <openvas/misc/openvas_server.h> 61 #if FROM_BUFFER_SIZE > SSIZE_MAX 62 #error FROM_BUFFER_SIZE too big for "read" 69 #define G_LOG_DOMAIN "md main" 94 static int ompd_nvt_cache_mode = 0;
118 int max_ips_per_target,
int max_email_attachment_size,
119 int max_email_include_size,
int max_email_message_size,
121 int (*fork_connection) (openvas_connection_t *, gchar*),
124 return init_omp (log_config, nvt_cache_mode, database, max_ips_per_target,
125 max_email_attachment_size, max_email_include_size,
126 max_email_message_size,
127 progress, fork_connection, skip_db_check);
154 read_from_client_unix (
int client_socket)
159 count = read (client_socket,
170 g_warning (
"%s: failed to read from client: %s\n",
171 __FUNCTION__, strerror (errno));
209 count = gnutls_record_recv (*client_session,
214 if (count == GNUTLS_E_AGAIN)
217 if (count == GNUTLS_E_INTERRUPTED)
220 if (count == GNUTLS_E_REHANDSHAKE)
223 g_debug (
" should rehandshake\n");
226 if (gnutls_error_is_fatal ((
int) count) == 0
227 && (count == GNUTLS_E_WARNING_ALERT_RECEIVED
228 || count == GNUTLS_E_FATAL_ALERT_RECEIVED))
230 int alert = gnutls_alert_get (*client_session);
231 const char*
alert_name = gnutls_alert_get_name (alert);
232 g_warning (
"%s: TLS Alert %d: %s\n",
233 __FUNCTION__, alert, alert_name);
235 g_warning (
"%s: failed to read from client: %s\n",
236 __FUNCTION__, gnutls_strerror ((
int) count));
269 read_from_client (openvas_connection_t *client_connection)
271 if (client_connection->tls)
272 return read_from_client_tls (&client_connection->session);
273 return read_from_client_unix (client_connection->socket);
285 write_to_client_tls (gnutls_session_t* client_session)
290 count = gnutls_record_send (*client_session,
295 if (count == GNUTLS_E_AGAIN)
298 if (count == GNUTLS_E_INTERRUPTED)
301 if (count == GNUTLS_E_REHANDSHAKE)
304 g_warning (
"%s: failed to write to client: %s\n",
306 gnutls_strerror ((
int) count));
309 logf (
"=> client %.*s\n",
313 g_debug (
"=> client %u bytes\n", (
unsigned int) count);
315 g_debug (
"=> client done\n");
330 write_to_client_unix (
int client_socket)
335 count = write (client_socket,
346 g_warning (
"%s: failed to write to client: %s\n",
351 logf (
"=> client %.*s\n",
355 g_debug (
"=> client %u bytes\n", (
unsigned int) count);
357 g_debug (
"=> client done\n");
372 write_to_client (openvas_connection_t *client_connection)
374 if (client_connection->tls)
375 return write_to_client_tls (&client_connection->session);
376 return write_to_client_unix (client_connection->socket);
402 switch (write_to_client (write_to_client_data))
407 g_debug (
" %s full (%i < %zu); client write failed\n",
420 if (length > strlen (msg))
424 g_debug (
"-> client: %.*s\n", (
int) length, msg);
434 g_debug (
"-> client: %s\n", msg);
447 session_clean (openvas_connection_t *client_connection)
449 if (client_connection->session)
451 gnutls_deinit (client_connection->session);
452 client_connection->session = NULL;
454 if (client_connection->credentials)
456 gnutls_certificate_free_credentials (client_connection->credentials);
457 client_connection->credentials = NULL;
493 serve_omp (openvas_connection_t *client_connection,
const gchar *database,
494 gchar **disable,
void (*
progress) ())
496 int nfds, scan_handler = 0, rc = 0;
499 short client_input_stalled;
502 short client_active = client_connection->socket > 0;
504 if (client_connection->socket < 0)
505 ompd_nvt_cache_mode = client_connection->socket;
507 if (ompd_nvt_cache_mode)
508 g_info (
" Updating NVT cache.\n");
510 g_debug (
" Serving OMP.\n");
516 (
void*) client_connection,
521 if (current_scanner_preference) free (current_scanner_preference);
523 maybe_free_scanner_preferences ();
545 client_input_stalled = 0;
583 fd_set readfds, writefds;
586 if (termination_signal)
588 g_debug (
"%s: Received %s signal.",
613 FD_SET (client_connection->socket, &readfds);
616 FD_SET (client_connection->socket, &writefds);
649 if (client_connection->socket > 0
650 && client_connection->tls
651 && FD_ISSET (client_connection->socket, &readfds)
652 && gnutls_record_check_pending (client_connection->session))
657 FD_SET (client_connection->socket, &readfds);
683 struct timeval timeout;
687 ret = select (nfds, &readfds, &writefds, NULL, &timeout);
689 if ((ret < 0 && errno == EINTR) || ret == 0)
696 if (!scan_handler && !ompd_nvt_cache_mode)
701 g_warning (
"%s: child select failed: %s\n", __FUNCTION__,
708 if (client_connection->socket > 0
709 && FD_ISSET (client_connection->socket, &readfds))
713 switch (read_from_client (client_connection))
724 g_debug (
" EOF reading from client.\n");
725 if (client_connection->socket > 0
726 && FD_ISSET (client_connection->socket, &writefds))
729 write_to_client (client_connection);
740 logf (
"<= client %.*s\n",
746 g_debug (
"<= client Input may contain password, suppressed.\n");
748 g_debug (
"<= client \"%.*s\"\n",
756 client_input_stalled = 0;
764 client_input_stalled = 0;
775 session_clean (client_connection);
777 client_input_stalled = 0;
786 session_clean (client_connection);
793 session_clean (client_connection);
796 else if (ret == -1 || ret == -4)
801 write_to_client (client_connection);
808 g_debug (
" client input stalled 1\n");
809 client_input_stalled = 1;
815 g_debug (
" client input stalled 2\n");
816 client_input_stalled = 2;
823 client_input_stalled = 0;
847 if (client_active == 0)
883 if (client_connection->socket > 0
884 && FD_ISSET (client_connection->socket, &writefds))
888 switch (write_to_client (client_connection))
902 if (client_input_stalled)
910 client_input_stalled = 0;
928 session_clean (client_connection);
938 session_clean (client_connection);
945 session_clean (client_connection);
953 write_to_client (client_connection);
960 g_debug (
" client input still stalled (1)\n");
961 client_input_stalled = 1;
966 g_debug (
" client input still stalled (2)\n");
967 client_input_stalled = 2;
973 client_input_stalled = 0;
990 if (client_active == 0)
998 if (client_active == 0)
1006 if (ompd_nvt_cache_mode)
1018 g_debug (
" scanner input stalled\n");
1022 assert (ret == 0 || ret == 5);
1036 openvas_connection_free (client_connection);
int init_omp(GSList *log_config, int nvt_cache_mode, const gchar *database, int max_ips_per_target, int max_email_attachment_size, int max_email_include_size, int max_email_message_size, void(*progress)(), int(*fork_connection)(openvas_connection_t *, gchar *), int skip_db_check)
Initialise OMP library.
Protos for communication between openvas-manager and openvas-server.
void init_omp_process(int update_nvt_cache, const gchar *database, int(*write_to_client)(const char *, void *), void *write_to_client_data, gchar **disable)
Initialise OMP library data for a process.
buffer_size_t from_client_start
The start of the data in the from_client buffer.
#define TO_CLIENT_BUFFER_SIZE
The size of the to_client data buffer, in bytes.
int openvas_scanner_read()
Read as much from the server as the from_scanner buffer will.
GSList * log_config
Logging parameters, as passed to setup_log_handlers.
void openvas_scanner_free()
Free the scanner allocated data. Doesn't close socket and terminate the session.
int process_omp_change()
Deal with any changes caused by other processes.
scanner_init_state_t scanner_init_state
The initialisation state of the scanner.
#define logf(format, args...)
Dummy macro, enabled with LOG.
int openvas_scanner_peek()
Check if there is any data to receive from connected Scanner socket.
buffer_size_t from_client_end
The end of the data in the from_client buffer.
gboolean ompd_send_to_client(const char *msg, void *write_to_client_data)
Send a response message to the client.
buffer_size_t from_buffer_size
Size of from_client data buffer, in bytes.
gnutls_session_t client_session
The client session.
buffer_size_t to_client_start
The start of the data in the to_client buffer.
unsigned int to_server_buffer_space()
Get the number of characters free in the server output buffer.
void openvas_scanner_fd_set(fd_set *fd)
Add connected to Scanner's socket to an fd_set.
int openvas_scanner_connected()
Whether we have started a connection to the Scanner using openvas_scanner_connect().
int process_omp_client_input()
Process any XML available in from_client.
int openvas_scanner_full()
Check whether the buffer for data from Scanner is full.
void(* progress)()
Function to mark progress.
volatile int termination_signal
Flag for signal handlers.
int serve_omp(openvas_connection_t *client_connection, const gchar *database, gchar **disable, void(*progress)())
Serve the OpenVAS Management Protocol (OMP).
void openvas_scanner_fork()
Reset Scanner variables after a fork.
int openvas_scanner_write(int nvt_cache_mode)
Write as much as possible from the to_scanner buffer to the scanner.
char * alert_name(alert_t alert)
Return the name of an alert.
void set_scanner_init_state(scanner_init_state_t state)
Set the scanner initialisation state, scanner_init_state.
void init_ompd_process(const gchar *database, gchar **disable)
Initialise a process forked within the OMP daemon.
#define FROM_BUFFER_SIZE
Size of from_client and from_scanner data buffers, in bytes.
int openvas_scanner_connect()
Create a new connection to the scanner and set it as current scanner.
int get_termination_signal()
Gets the last termination signal or 0.
int init_ompd(GSList *log_config, int nvt_cache_mode, const gchar *database, int max_ips_per_target, int max_email_attachment_size, int max_email_include_size, int max_email_message_size, void(*progress)(), int(*fork_connection)(openvas_connection_t *, gchar *), int skip_db_check)
Initialise the OMP library for the OMP daemon.
int openvas_scanner_get_nfds(int socket)
Get the nfds value to use for a select() call.
char to_client[TO_CLIENT_BUFFER_SIZE]
Buffer of output to the client.
A printf like macro for logging communication.
char from_client[FROM_BUFFER_SIZE]
Buffer of input from the client.
int openvas_scanner_init(int cache_mode)
Initializes the already setup connection with the Scanner.
int openvas_scanner_session_peek()
Check if there is any data to receive from connected Scanner session.
credentials_t current_credentials
Current credentials during any OMP command.
#define g_info(...)
Defines g_info for glib versions older than 2.40.
void free_tasks()
Dummy function.
buffer_size_t to_client_end
The end of the data in the to_client buffer.
int manage_scanner_set_default()
Set the default scanner as the scanner to connect to.
int process_otp_scanner_input(void(*progress)())
Process any lines available in from_scanner.
unsigned int buffer_size_t
int openvas_scanner_fd_isset(fd_set *fd)
Check if connected to Scanner is set in an fd_set.
int openvas_scanner_close()
Finish the connection to the Scanner and free internal buffers.