Greenbone Vulnerability Manager  8.0.0~git
Macros | Functions | Variables
scanner.c File Reference

GVM management layer: Scanner connection handling. More...

#include "scanner.h"
#include "comm.h"
#include "gmpd.h"
#include "otp.h"
#include "utils.h"
#include <assert.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <gvm/util/serverutils.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/un.h>
#include <unistd.h>

Macros

#define G_LOG_DOMAIN   "md main"
 GLib log domain.
 

Functions

static int write_string_to_server (char *const string)
 Write as much as possible from a string to the server. More...
 
static int write_to_server_buffer ()
 Write as much as possible from the internal buffer to the server. More...
 
int openvas_scanner_read ()
 Read as much from the server as the from_scanner buffer will. More...
 
int openvas_scanner_full ()
 Check whether the buffer for data from Scanner is full. More...
 
int openvas_scanner_realloc ()
 Reallocates the from_scanner buffer to a higher size. More...
 
int openvas_scanner_write (int nvt_cache_mode)
 Write as much as possible from the to_scanner buffer to the scanner. More...
 
static int openvas_scanner_wait ()
 Wait for the scanner socket to be writable. More...
 
static int load_cas (gnutls_certificate_credentials_t *scanner_credentials)
 Load certificates from the CA directory. More...
 
int openvas_scanner_close ()
 Finish the connection to the Scanner and free internal buffers. More...
 
void openvas_scanner_fork ()
 Reset Scanner variables after a fork. More...
 
static int openvas_scanner_connect_unix ()
 Create a new connection to the scanner and set it as current scanner. More...
 
int openvas_scanner_connect ()
 Create a new connection to the scanner and set it as current scanner. More...
 
void openvas_scanner_free ()
 Free the scanner allocated data. Doesn't close socket and terminate the session.
 
int openvas_scanner_fd_isset (fd_set *fd)
 Check if connected to Scanner is set in an fd_set. More...
 
void openvas_scanner_fd_set (fd_set *fd)
 Add connected to Scanner's socket to an fd_set. More...
 
int openvas_scanner_peek ()
 Check if there is any data to receive from connected Scanner socket. More...
 
int openvas_scanner_get_nfds (int socket)
 Get the nfds value to use for a select() call. More...
 
int openvas_scanner_session_peek ()
 Check if there is any data to receive from connected Scanner session. More...
 
int openvas_scanner_connected ()
 Whether we have started a connection to the Scanner using openvas_scanner_connect(). More...
 
int openvas_scanner_init (int cache_mode)
 Initializes the already setup connection with the Scanner. More...
 
int openvas_scanner_set_address (const char *addr, int port)
 Set the scanner's address and port. Will try to resolve addr if it is a hostname. More...
 
int openvas_scanner_set_unix (const char *path)
 Set the scanner's unix socket path. More...
 
void openvas_scanner_set_certs (const char *ca_pub, const char *key_pub, const char *key_priv)
 Set the scanner's CA Certificate, and public/private key pair. More...
 
int openvas_scanner_is_loading ()
 Checks whether the connected to OpenVAS Scanner is still loading plugins. To be called right after openvas_scanner_init(). More...
 

Variables

static gnutls_session_t openvas_scanner_session = NULL
 Current OpenVAS Scanner session.
 
static gnutls_certificate_credentials_t openvas_scanner_credentials = NULL
 Current OpenVAS Scanner credentials.
 
static int openvas_scanner_socket = -1
 Current OpenVAS Scanner socket.
 
static struct sockaddr_in openvas_scanner_address
 Current OpenVAS Scanner address.
 
static char * openvas_scanner_ca_pub = NULL
 Current OpenVAS Scanner CA Cert.
 
static char * openvas_scanner_key_pub = NULL
 Current OpenVAS Scanner public key.
 
static char * openvas_scanner_key_priv = NULL
 Current OpenVAS Scanner private key.
 
static char * openvas_scanner_unix_path = NULL
 Current OpenVAS Scanner UNIX path.
 
char * from_scanner = NULL
 Buffer of input from the scanner. More...
 
buffer_size_t from_scanner_start = 0
 The start of the data in the from_scanner buffer.
 
buffer_size_t from_scanner_end = 0
 The end of the data in the from_scanner buffer.
 
static buffer_size_t from_scanner_size = 1048576
 The current size of the from_scanner buffer.
 
static buffer_size_t from_scanner_max_size = 1073741824
 The max size of the from_scanner buffer.
 

Detailed Description

GVM management layer: Scanner connection handling.

This file provides facilities for working with scanner connections.

Function Documentation

◆ load_cas()

static int load_cas ( gnutls_certificate_credentials_t *  scanner_credentials)
static

Load certificates from the CA directory.

Parameters
[in]scanner_credentialsScanner credentials.
Returns
0 success, -1 error.

◆ openvas_scanner_close()

int openvas_scanner_close ( )

Finish the connection to the Scanner and free internal buffers.

Returns
-1 if error, 0 if success.

◆ openvas_scanner_connect()

int openvas_scanner_connect ( )

Create a new connection to the scanner and set it as current scanner.

Returns
0 on success, -1 on error.

◆ openvas_scanner_connect_unix()

static int openvas_scanner_connect_unix ( )
static

Create a new connection to the scanner and set it as current scanner.

Use a UNIX socket for the connection.

Returns
0 on success, -1 on error.

◆ openvas_scanner_connected()

int openvas_scanner_connected ( )

Whether we have started a connection to the Scanner using openvas_scanner_connect().

Returns
1 if connected, 0 otherwise.

◆ openvas_scanner_fd_isset()

int openvas_scanner_fd_isset ( fd_set *  fd)

Check if connected to Scanner is set in an fd_set.

Parameters
[in]fdFile descriptor set.
Returns
1 if scanner socket in fd_set, 0 if not connected or or not set.

◆ openvas_scanner_fd_set()

void openvas_scanner_fd_set ( fd_set *  fd)

Add connected to Scanner's socket to an fd_set.

Parameters
[in]fdFile Descriptor set.

◆ openvas_scanner_fork()

void openvas_scanner_fork ( )

Reset Scanner variables after a fork.

This other side of the fork will do the actual cleanup.

◆ openvas_scanner_full()

int openvas_scanner_full ( )

Check whether the buffer for data from Scanner is full.

Returns
1 if full, 0 otherwise.

◆ openvas_scanner_get_nfds()

int openvas_scanner_get_nfds ( int  socket)

Get the nfds value to use for a select() call.

Parameters
[in]socketSocket to compare to.
Returns
socket + 1 if socket value is higher then scanner's or not connected to a scanner, scanner socket + 1 otherwise.

◆ openvas_scanner_init()

int openvas_scanner_init ( int  cache_mode)

Initializes the already setup connection with the Scanner.

Parameters
[in]cache_modeNVT Cache mode if true, which means sending NVT_INFO command to scanner in initial negotiation.
Returns
0 success, -1 error.

◆ openvas_scanner_is_loading()

int openvas_scanner_is_loading ( )

Checks whether the connected to OpenVAS Scanner is still loading plugins. To be called right after openvas_scanner_init().

Returns
1 if loading, 0 if not loading or error.

◆ openvas_scanner_peek()

int openvas_scanner_peek ( )

Check if there is any data to receive from connected Scanner socket.

Returns
1 if there is data in socket buffer, 0 if no data or not connected to a scanner.

◆ openvas_scanner_read()

int openvas_scanner_read ( )

Read as much from the server as the from_scanner buffer will.

hold.

Returns
0 on reading everything available, -1 on error, -2 if from_scanner buffer is full or -3 on reaching end of file.
Todo:
Rehandshake.

◆ openvas_scanner_realloc()

int openvas_scanner_realloc ( )

Reallocates the from_scanner buffer to a higher size.

Returns
1 if max size reached, 0 otherwise.

◆ openvas_scanner_session_peek()

int openvas_scanner_session_peek ( )

Check if there is any data to receive from connected Scanner session.

Returns
1 if there is data in session buffer, 0 if no data or not connected to a scanner.

◆ openvas_scanner_set_address()

int openvas_scanner_set_address ( const char *  addr,
int  port 
)

Set the scanner's address and port. Will try to resolve addr if it is a hostname.

Parameters
[in]addrScanner address string.
[in]portScanner port.
Returns
0 success, -1 error.

◆ openvas_scanner_set_certs()

void openvas_scanner_set_certs ( const char *  ca_pub,
const char *  key_pub,
const char *  key_priv 
)

Set the scanner's CA Certificate, and public/private key pair.

Parameters
[in]ca_pubCA Certificate.
[in]key_pubScanner Certificate.
[in]key_privScanner private key.

◆ openvas_scanner_set_unix()

int openvas_scanner_set_unix ( const char *  path)

Set the scanner's unix socket path.

Parameters
[in]pathPath to scanner unix socket.
Returns
0 success, -1 error.

◆ openvas_scanner_wait()

static int openvas_scanner_wait ( )
static

Wait for the scanner socket to be writable.

Returns
0 on success, -1 on error.

◆ openvas_scanner_write()

int openvas_scanner_write ( int  nvt_cache_mode)

Write as much as possible from the to_scanner buffer to the scanner.

Parameters
[in]nvt_cache_modeNVT cache mode.
Returns
0 wrote everything, -1 error, -2 wrote as much as scanner accepted, -3 did an initialisation step.

◆ write_string_to_server()

static int write_string_to_server ( char *const  string)
static

Write as much as possible from a string to the server.

Parameters
[in]stringThe string.
Returns
0 wrote everything, -1 error, or the number of bytes written when the server accepted fewer bytes than given in string.
Todo:
Rehandshake.

◆ write_to_server_buffer()

static int write_to_server_buffer ( )
static

Write as much as possible from the internal buffer to the server.

Returns
0 wrote everything, -1 error, -2 wrote as much as server accepted, -3 interrupted.
Todo:
Rehandshake.

Variable Documentation

◆ from_scanner

char* from_scanner = NULL

Buffer of input from the scanner.

Todo:
As with the GMP version, these should most likely be passed to and from the client in a data structure like an otp_parser_t.