Greenbone Vulnerability Manager  8.0.1~git
Macros | Enumerations | Functions | Variables
otp.c File Reference

The Greenbone Vulnerability Manager OTP library. More...

#include "otp.h"
#include "manage.h"
#include "scanner.h"
#include "types.h"
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <gvm/base/strings.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

Macros

#define G_LOG_DOMAIN   "md otp"
 GLib log domain. More...
 

Enumerations

enum  scanner_state_t {
  SCANNER_BYE, SCANNER_DONE, SCANNER_ERRMSG_DESCRIPTION, SCANNER_ERRMSG_HOST,
  SCANNER_ERRMSG_HOSTNAME, SCANNER_ERRMSG_NUMBER, SCANNER_ERRMSG_OID, SCANNER_ALARM_DESCRIPTION,
  SCANNER_ALARM_HOST, SCANNER_ALARM_HOSTNAME, SCANNER_ALARM_NUMBER, SCANNER_ALARM_OID,
  SCANNER_LOG_DESCRIPTION, SCANNER_LOG_HOST, SCANNER_LOG_HOSTNAME, SCANNER_LOG_NUMBER,
  SCANNER_LOG_OID, SCANNER_NVT_INFO, SCANNER_PLUGIN_LIST_BUGTRAQ_ID, SCANNER_PLUGIN_LIST_CATEGORY,
  SCANNER_PLUGIN_LIST_CVE_ID, SCANNER_PLUGIN_LIST_FAMILY, SCANNER_PLUGIN_LIST_NAME, SCANNER_PLUGIN_LIST_OID,
  SCANNER_PLUGIN_LIST_TAGS, SCANNER_PLUGIN_LIST_XREFS, SCANNER_PREFERENCE_NAME, SCANNER_PREFERENCE_VALUE,
  SCANNER_SERVER, SCANNER_STATUS, SCANNER_STATUS_HOST, SCANNER_STATUS_PROGRESS,
  SCANNER_TIME, SCANNER_TIME_HOST_START_HOST, SCANNER_TIME_HOST_START_TIME, SCANNER_TIME_HOST_END_HOST,
  SCANNER_TIME_HOST_END_TIME, SCANNER_TIME_SCAN_START, SCANNER_TIME_SCAN_END, SCANNER_TOP
}
 Possible states of the scanner.
 

Functions

static void blank_control_chars (char *string)
 Replace any control characters in string with spaces. More...
 
static message_tmake_message (const char *host)
 Make a message. More...
 
static void free_message (message_t *message)
 Free a message for g_ptr_array_foreach. More...
 
static void set_message_hostname (message_t *message, char *hostname)
 Set the hostname of a message. More...
 
static void set_message_port_number (message_t *message, int number)
 Set the port number of a message. More...
 
static void set_message_port_protocol (message_t *message, const char *protocol)
 Set the port protocol of a message. More...
 
static void set_message_port_string (message_t *message, char *string)
 Set the original string of a port of a message. More...
 
static void set_message_description (message_t *message, char *description)
 Set the description of a message. More...
 
static void set_message_oid (message_t *message, char *oid)
 Set the OID of a message. More...
 
static void write_message (task_t task, message_t *message, char *type)
 Write a message for g_ptr_array_foreach. More...
 
static void append_error_message (task_t task, message_t *message)
 Append a error message to a report. More...
 
static void append_alarm_message (task_t task, message_t *message)
 Append a hole message to a report. More...
 
static void append_log_message (task_t task, message_t *message)
 Append a log message to a report. More...
 
void init_otp_data ()
 Initialise OTP library data. More...
 
static void set_scanner_state (scanner_state_t state)
 Set the scanner state, scanner_state. More...
 
void set_scanner_init_state (scanner_init_state_t state)
 Set the scanner initialisation state, scanner_init_state. More...
 
void reset_scanner_states ()
 Set the scanner initialisation state, scanner_init_state. More...
 
static int sync_buffer ()
 "Synchronise" the from_scanner buffer. More...
 
static int parse_scanner_done (char **messages)
 Parse the final SERVER field of an OTP message. More...
 
static int parse_scanner_bad_login (char **messages)
 Check for a bad login response from the scanner. More...
 
static int parse_scanner_preference_value (char **messages)
 Parse preference value. More...
 
static int parse_scanner_plugin_list_tags (char **messages)
 Parse the tags of a plugin list. More...
 
static int parse_scanner_server (char **messages)
 Parse the field following "SERVER <|>". More...
 
static int scanner_is_loading (char *messages)
 Check if current message is the scanner loading message. More...
 
static void parse_scanner_loading (char *messages)
 Parse SCANNER_LOADING response. More...
 
int process_otp_scanner_input ()
 Process any lines available in from_scanner. More...
 

Variables

buffer_size_t from_buffer_size
 Size of from_client data buffer, in bytes. More...
 
static message_tcurrent_message = NULL
 Current message during OTP SERVER message commands.
 
static gchar * current_host = NULL
 Current host during OTP SERVER message commands.
 
static char * plugins_feed_version = NULL
 The version of the NVT feed.
 
static char * current_scanner_preference = NULL
 The current scanner preference, during reading of scanner preferences.
 
static nvti_t * current_plugin = NULL
 The current plugin, during reading of scanner plugin list.
 
static GList * scanner_plugins_list = NULL
 The full plugins list, during reading of scanner plugin list.
 
static GList * scanner_preferences_list = NULL
 The full preferences list, during reading of scanner plugin list.
 
static scanner_state_t scanner_state = SCANNER_TOP
 The state of the scanner.
 
scanner_init_state_t scanner_init_state = SCANNER_INIT_TOP
 The initialisation state of the scanner. More...
 
int scanner_init_offset = 0
 Offset into initialisation string being sent to scanner. More...
 
int scanner_current_loading = 0
 Scanner current number of loaded plugins, when still loading.
 
int scanner_total_loading = 0
 Scanner total number of plugins to be loaded, when still loading.
 
char * from_scanner
 Buffer of input from the scanner. More...
 
buffer_size_t from_scanner_start
 The start of the data in the from_scanner buffer.
 
buffer_size_t from_scanner_end
 The end of the data in the from_scanner buffer.
 

Detailed Description

The Greenbone Vulnerability Manager OTP library.

This file defines an OpenVAS Transfer Protocol (OTP) library, for implementing managers such as the Greenbone Vulnerability Manager daemon.

The library provides a single function, process_otp_scanner_input. This function parses a given string of OTP text and adjusts local task records according to the OTP messages in the string.

Macro Definition Documentation

◆ G_LOG_DOMAIN

#define G_LOG_DOMAIN   "md otp"

GLib log domain.

Todo:
Ensure that the globals used to store information across the XML parser callbacks (for example, current_scanner_preferences) are freed in the failure cases.

Function Documentation

◆ append_alarm_message()

static void append_alarm_message ( task_t  task,
message_t message 
)
static

Append a hole message to a report.

Parameters
[in]taskTask.
[in]messageMessage.

◆ append_error_message()

static void append_error_message ( task_t  task,
message_t message 
)
static

Append a error message to a report.

Parameters
[in]taskTask.
[in]messageMessage.

◆ append_log_message()

static void append_log_message ( task_t  task,
message_t message 
)
static

Append a log message to a report.

Parameters
[in]taskTask.
[in]messageMessage.

◆ blank_control_chars()

static void blank_control_chars ( char *  string)
static

Replace any control characters in string with spaces.

Parameters
[in,out]stringString to replace in.

◆ free_message()

static void free_message ( message_t message)
static

Free a message for g_ptr_array_foreach.

Parameters
[in]messagePointer to the message.

◆ init_otp_data()

void init_otp_data ( )

Initialise OTP library data.

This must run once, before the first call to process_otp_scanner_input.

◆ make_message()

static message_t* make_message ( const char *  host)
static

Make a message.

Parameters
[in]hostHost name.
Returns
A pointer to the new message.

◆ parse_scanner_bad_login()

static int parse_scanner_bad_login ( char **  messages)
static

Check for a bad login response from the scanner.

Parameters
messagesA pointer into the OTP input buffer.
Returns
0 if there is a bad login response, else 1.
Todo:
Are there 19 characters available?

◆ parse_scanner_done()

static int parse_scanner_done ( char **  messages)
static

Parse the final SERVER field of an OTP message.

Parameters
messagesA pointer into the OTP input buffer.
Returns
0 success, -1 fail, -2 too few characters (need more input).

◆ parse_scanner_loading()

static void parse_scanner_loading ( char *  messages)
static

Parse SCANNER_LOADING response.

Updates scanner_current_loading and scanner_total_loading values.

Parameters
[in]messagesMessages.

◆ parse_scanner_plugin_list_tags()

static int parse_scanner_plugin_list_tags ( char **  messages)
static

Parse the tags of a plugin list.

Parameters
messagesA pointer into the OTP input buffer.
Returns
0 success, -2 too few characters (need more input).

◆ parse_scanner_preference_value()

static int parse_scanner_preference_value ( char **  messages)
static

Parse preference value.

Parameters
messagesA pointer into the OTP input buffer.
Returns
0 success, -2 too few characters (need more input).

◆ parse_scanner_server()

static int parse_scanner_server ( char **  messages)
static

Parse the field following "SERVER <|>".

Parameters
messagesA pointer into the OTP input buffer.
Returns
0 found a newline delimited field, -1 error, -2 need more input, -3 found a <|> before next newline (that is, a <|> delimited field follows), -4 failed to find a newline (may be a <|>)
Todo:
Is there ever whitespace before the newline?
Todo:
Are there 20 characters available?
Todo:
Are there 12 characters available?

◆ process_otp_scanner_input()

int process_otp_scanner_input ( )

Process any lines available in from_scanner.

Update scanner information according to the input from the scanner.

This function simply records input from the scanner. Output to the scanner or client is almost always done via process_gmp_client_input in reaction to client requests, the only exception being stop requests initiated in other processes.

Returns
0 success, 1 received scanner BYE, 2 bad login, 3 scanner loading, -1 error.
Todo:
Replace "\n" with newline in description.
Todo:
Field could be "general".
Todo:
Replace "\n" with newline in description.
Todo:
Field could be "general".
Todo:
Replace "\n" with newline in description.
Todo:
Field could be "general".
Todo:
Exit more formally with Scanner?
Todo:
Consider reading all fields up to <|> SERVER?

◆ reset_scanner_states()

void reset_scanner_states ( )

Set the scanner initialisation state, scanner_init_state.

Todo:
Exported for scanner.c.

◆ scanner_is_loading()

static int scanner_is_loading ( char *  messages)
static

Check if current message is the scanner loading message.

Parameters
[in]messagesMessage.
Returns
1 if scanner loading message, else 0.

◆ set_message_description()

static void set_message_description ( message_t message,
char *  description 
)
static

Set the description of a message.

Parameters
[in]messagePointer to the message. Used directly, freed by free_message.
[in]descriptionDescription.

◆ set_message_hostname()

static void set_message_hostname ( message_t message,
char *  hostname 
)
static

Set the hostname of a message.

Parameters
[in]messagePointer to the message. Used directly, freed by free_message.
[in]hostnameHostname.

◆ set_message_oid()

static void set_message_oid ( message_t message,
char *  oid 
)
static

Set the OID of a message.

Parameters
[in]messagePointer to the message. Used directly, freed by free_message.
[in]oidOID.

◆ set_message_port_number()

static void set_message_port_number ( message_t message,
int  number 
)
static

Set the port number of a message.

Parameters
[in]messagePointer to the message. Used directly, freed by free_message.
[in]numberPort number.

◆ set_message_port_protocol()

static void set_message_port_protocol ( message_t message,
const char *  protocol 
)
static

Set the port protocol of a message.

Parameters
[in]messagePointer to the message. Used directly, freed by free_message.
[in]protocolName of protocol on port.

◆ set_message_port_string()

static void set_message_port_string ( message_t message,
char *  string 
)
static

Set the original string of a port of a message.

Parameters
[in]messagePointer to the message.
[in]stringPort string.

◆ set_scanner_init_state()

void set_scanner_init_state ( scanner_init_state_t  state)

Set the scanner initialisation state, scanner_init_state.

Parameters
[in]stateNew init state.

◆ set_scanner_state()

static void set_scanner_state ( scanner_state_t  state)
static

Set the scanner state, scanner_state.

Parameters
[in]stateNew state.

◆ sync_buffer()

static int sync_buffer ( )
static

"Synchronise" the from_scanner buffer.

Move any OTP in the from_scanner buffer to the front of the buffer.

Returns
0 success, -1 from_scanner is full.

◆ write_message()

static void write_message ( task_t  task,
message_t message,
char *  type 
)
static

Write a message for g_ptr_array_foreach.

Parameters
[in]taskThe task with which to associate the message.
[in]messageThe message.
[in]typeThe message type (for example "Security Warning").

Variable Documentation

◆ from_buffer_size

buffer_size_t from_buffer_size

Size of from_client data buffer, in bytes.

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

◆ from_scanner

char* from_scanner

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.

◆ scanner_init_offset

int scanner_init_offset = 0

Offset into initialisation string being sent to scanner.

Todo:
Exported for gmpd.c.

◆ scanner_init_state

scanner_init_state_t scanner_init_state = SCANNER_INIT_TOP

The initialisation state of the scanner.

Todo:
Exported for gmpd.c.