Greenbone Vulnerability Manager 22.5.2
Data Structures | Macros | Functions | Variables
manage_sql_nvts.c File Reference

GVM management layer: NVTs. More...

#include <assert.h>
#include <errno.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <gvm/base/cvss.h>
#include "manage_sql_nvts.h"
#include "manage_preferences.h"
#include "manage_sql.h"
#include "manage_sql_configs.h"
#include "sql.h"
#include "utils.h"

Data Structures

struct  batch_t
 SQL batch. More...
 

Macros

#define _GNU_SOURCE
 Enable extra GNU functions.
 
#define G_LOG_DOMAIN   "md manage"
 GLib log domain.
 

Functions

void create_tables_nvt (const gchar *)
 Create NVT related tables.
 
const gchar * get_osp_vt_update_socket ()
 Get the current file socket for OSP NVT update.
 
void set_osp_vt_update_socket (const char *new_socket)
 Set the file socket for OSP NVT update.
 
int check_osp_vt_update_socket ()
 Check the files socket used for OSP NVT update.
 
void set_vt_ref_insert_size (int new_size)
 Set the VT ref insert size.
 
void set_vt_sev_insert_size (int new_size)
 Set the VT severity insert size.
 
void check_db_nvts ()
 Ensures the sanity of nvts cache in DB.
 
char * manage_nvt_name (nvt_t nvt)
 Get the name of an NVT.
 
char * nvt_name (const char *oid)
 Get the name of an NVT given its OID.
 
char * nvts_feed_version ()
 Return feed version of the plugins in the plugin cache.
 
time_t nvts_feed_version_epoch ()
 Return feed version of the plugins as seconds since epoch.
 
void set_nvts_feed_version (const char *feed_version)
 Set the feed version of the plugins in the plugin cache.
 
gboolean find_nvt (const char *oid, nvt_t *nvt)
 Find an NVT given an identifier.
 
batch_tbatch_start (int max)
 Create an SQL batch.
 
int batch_check (batch_t *b)
 Check an SQL batch.
 
void batch_end (batch_t *b)
 End and free an SQL batch.
 
static void insert_vt_refs (const nvti_t *nvti, int rebuild, batch_t *batch)
 Insert vt_refs for an NVT.
 
static double insert_vt_severities (const nvti_t *nvti, int rebuild, batch_t *batch)
 Insert vt_severities for an NVT.
 
static void insert_nvt (const nvti_t *nvti, int rebuild, batch_t *vt_refs_batch, batch_t *vt_sevs_batch)
 Insert an NVT.
 
int init_nvt_info_iterator (iterator_t *iterator, get_data_t *get, const char *name)
 Initialise an NVT iterator.
 
static gchar * nvt_iterator_columns ()
 Get NVT iterator SELECT columns.
 
static gchar * nvt_iterator_columns_nvts ()
 Get NVT iterator SELECT columns.
 
int nvt_info_count (const get_data_t *get)
 Count number of nvt.
 
int nvt_info_count_after (const get_data_t *get, time_t count_time, gboolean get_modified)
 Count number of nvts created or modified after a given time.
 
static gchar * select_config_nvts (const config_t config, const char *family, int ascending, const char *sort_field)
 Return SQL for selecting NVT's of a config from one family.
 
void init_nvt_iterator (iterator_t *iterator, nvt_t nvt, config_t config, const char *family, const char *category, int ascending, const char *sort_field)
 Initialise an NVT iterator.
 
void init_cve_nvt_iterator (iterator_t *iterator, const char *cve, int ascending, const char *sort_field)
 Initialise an NVT iterator, for NVTs of a certain CVE.
 
int nvt_iterator_category (iterator_t *iterator)
 Get the category from an NVT iterator.
 
char * nvt_default_timeout (const char *oid)
 Get the default timeout of an NVT.
 
char * nvt_family (const char *oid)
 Get the family of an NVT.
 
int family_nvt_count (const char *family)
 Get the number of NVTs in one or all families.
 
int family_count ()
 Get the number of families.
 
static void insert_nvt_preference (gpointer nvt_preference, gpointer rebuild)
 Insert a NVT preferences.
 
static void insert_nvt_preferences_list (GList *nvt_preferences_list, int rebuild)
 Inserts NVT preferences in DB from a list of nvt_preference_t structures.
 
static void set_nvts_check_time (int count_new, int count_modified)
 Set the NVT update check time in the meta table.
 
static int update_preferences_from_vt (element_t vt, const gchar *oid, GList **preferences)
 Update NVT from VT XML.
 
static nvti_t * nvti_from_vt (element_t vt)
 Create NVTI structure from VT XML.
 
static int update_nvts_from_vts (element_t *get_vts_response, const gchar *scanner_feed_version, int rebuild)
 Update NVTs from VTs XML.
 
static void check_old_preference_names (const gchar *table)
 Check that preference names are in the new format.
 
static void check_preference_names (int trash, time_t modification_time)
 Update config preferences where the name has changed in the NVTs.
 
void init_nvt_severity_iterator (iterator_t *iterator, const char *oid)
 Initialise an NVT severity iterator.
 
double nvt_severity_iterator_score (iterator_t *iterator)
 Gets the score from an NVT severity iterator.
 
static int update_nvt_cache_osp (const gchar *update_socket, gchar *db_feed_version, gchar *scanner_feed_version, int rebuild)
 Update VTs via OSP.
 
static char * osp_scanner_feed_version (const gchar *update_socket)
 Get the VTs feed version from an OSP scanner.
 
static int nvts_feed_version_status_internal (const gchar *update_socket, gchar **db_feed_version_out, gchar **scanner_feed_version_out)
 Check VTs feed version status via OSP, optionally get versions.
 
int nvts_feed_version_status ()
 Check VTs feed version status.
 
int manage_update_nvt_cache_osp (const gchar *update_socket)
 Update VTs via OSP.
 
void manage_sync_nvts (int(*fork_update_nvt_cache)())
 Sync NVTs if newer NVTs are available.
 
int update_or_rebuild_nvts (int update)
 Update or rebuild NVT db.
 
int manage_rebuild (GSList *log_config, const db_conn_info_t *database)
 Rebuild NVT db.
 
int manage_dump_vt_verification (GSList *log_config, const db_conn_info_t *database)
 Dump the string used to calculate the VTs verification hash to stdout.
 
int cleanup_nvt_sequences ()
 Cleans up NVT related id sequences likely to run out.
 

Variables

static int vt_ref_insert_size = VT_REF_INSERT_SIZE_DEFAULT
 Max number of rows inserted per statement.
 
static int vt_sev_insert_size = VT_SEV_INSERT_SIZE_DEFAULT
 Max number of rows inserted per statement.
 
static gchar * osp_vt_update_socket = NULL
 File socket for OSP NVT update.
 

Detailed Description

GVM management layer: NVTs.

The NVT parts of the GVM management layer.

Function Documentation

◆ batch_check()

int batch_check ( batch_t b)

Check an SQL batch.

@para[in] b Batch.

Returns
1 init b->str, 0 continue as normal.

◆ batch_end()

void batch_end ( batch_t b)

End and free an SQL batch.

@para[in] b Batch.

◆ batch_start()

batch_t * batch_start ( int  max)

Create an SQL batch.

Parameters
[in]maxMax number of iterations.
Returns
Freshly allocated batch.

◆ check_old_preference_names()

static void check_old_preference_names ( const gchar *  table)
static

Check that preference names are in the new format.

Parameters
[in]tableTable name.

◆ check_osp_vt_update_socket()

int check_osp_vt_update_socket ( )

Check the files socket used for OSP NVT update.

Returns
0 success, 1 no socket found.

◆ check_preference_names()

static void check_preference_names ( int  trash,
time_t  modification_time 
)
static

Update config preferences where the name has changed in the NVTs.

Parameters
[in]trashWhether to update the trash table.
[in]modification_timeTime NVTs considered must be modified after.

◆ cleanup_nvt_sequences()

int cleanup_nvt_sequences ( )

Cleans up NVT related id sequences likely to run out.

Returns
0 success, -1 error.

◆ family_count()

int family_count ( )

Get the number of families.

Returns
Total number of families.

◆ family_nvt_count()

int family_nvt_count ( const char *  family)

Get the number of NVTs in one or all families.

Parameters
[in]familyFamily name. NULL for all families.
Returns
Number of NVTs in family, or total number of nvts.

◆ find_nvt()

gboolean find_nvt ( const char *  oid,
nvt_t *  nvt 
)

Find an NVT given an identifier.

Parameters
[in]oidAn NVT identifier.
[out]nvtNVT return, 0 if successfully failed to find task.
Returns
FALSE on success (including if failed to find NVT), TRUE on error.

◆ get_osp_vt_update_socket()

const gchar * get_osp_vt_update_socket ( )

Get the current file socket for OSP NVT update.

Returns
The path of the file socket for OSP NVT update.

◆ init_cve_nvt_iterator()

void init_cve_nvt_iterator ( iterator_t iterator,
const char *  cve,
int  ascending,
const char *  sort_field 
)

Initialise an NVT iterator, for NVTs of a certain CVE.

Parameters
[in]iteratorIterator.
[in]cveCVE name.
[in]ascendingWhether to sort ascending or descending.
[in]sort_fieldField to sort on, or NULL for "id".

◆ init_nvt_info_iterator()

int init_nvt_info_iterator ( iterator_t iterator,
get_data_t get,
const char *  name 
)

Initialise an NVT iterator.

Parameters
[in]iteratorIterator.
[in]getGET data.
[in]nameName of the info
Returns
0 success, 1 failed to find NVT, 2 failed to find filter, -1 error.

◆ init_nvt_iterator()

void init_nvt_iterator ( iterator_t iterator,
nvt_t  nvt,
config_t  config,
const char *  family,
const char *  category,
int  ascending,
const char *  sort_field 
)

Initialise an NVT iterator.

Parameters
[in]iteratorIterator.
[in]nvtNVT to iterate over, all if 0.
[in]configConfig to limit selection to. NULL for all NVTs. Overridden by
  • nvt.
[in]familyFamily to limit selection to. NULL for all NVTs. Overridden by
  • config.
[in]categoryCategory to limit selection to. NULL for all.
[in]ascendingWhether to sort ascending or descending.
[in]sort_fieldField to sort on, or NULL for "id".

◆ init_nvt_severity_iterator()

void init_nvt_severity_iterator ( iterator_t iterator,
const char *  oid 
)

Initialise an NVT severity iterator.

Parameters
[in]iteratorIterator.
[in]oidOID of NVT.

◆ insert_nvt()

static void insert_nvt ( const nvti_t *  nvti,
int  rebuild,
batch_t vt_refs_batch,
batch_t vt_sevs_batch 
)
static

Insert an NVT.

Always called within a transaction.

Parameters
[in]nvtiNVT Information.
[in]rebuildTrue if rebuilding.
[in]vt_refs_batchBatch for vt_refs.
[in]vt_sevs_batchBatch for vt_severities.

◆ insert_nvt_preference()

static void insert_nvt_preference ( gpointer  nvt_preference,
gpointer  rebuild 
)
static

Insert a NVT preferences.

Parameters
[in]nvt_preferencePreference.
[in]rebuildWhether a rebuild is happening.

◆ insert_nvt_preferences_list()

static void insert_nvt_preferences_list ( GList *  nvt_preferences_list,
int  rebuild 
)
static

Inserts NVT preferences in DB from a list of nvt_preference_t structures.

Parameters
[in]nvt_preferences_listList of nvts to be inserted.
[in]rebuildWhether a rebuild is happening.

◆ insert_vt_refs()

static void insert_vt_refs ( const nvti_t *  nvti,
int  rebuild,
batch_t batch 
)
static

Insert vt_refs for an NVT.

Parameters
[in]nvtiNVT Information.
[in]rebuildTrue if rebuilding.
[in]batchBatch for inserts.

◆ insert_vt_severities()

static double insert_vt_severities ( const nvti_t *  nvti,
int  rebuild,
batch_t batch 
)
static

Insert vt_severities for an NVT.

Parameters
[in]nvtiNVT Information.
[in]rebuildTrue if rebuilding.
[in]batchBatch for inserts.
Returns
Highest severity.

◆ manage_dump_vt_verification()

int manage_dump_vt_verification ( GSList *  log_config,
const db_conn_info_t database 
)

Dump the string used to calculate the VTs verification hash to stdout.

Parameters
[in]log_configLog configuration.
[in]databaseLocation of manage database.
Returns
0 success, -1 error, -2 database is wrong version, -3 database needs to be initialised from server, -5 sync active.

◆ manage_nvt_name()

char * manage_nvt_name ( nvt_t  nvt)

Get the name of an NVT.

Parameters
[in]nvtNVT.
Returns
Freshly allocated name of NVT if possible, else NULL.

◆ manage_rebuild()

int manage_rebuild ( GSList *  log_config,
const db_conn_info_t database 
)

Rebuild NVT db.

Parameters
[in]log_configLog configuration.
[in]databaseLocation of manage database.
Returns
0 success, 1 VT integrity check failed, -1 error, -2 database is wrong version, -3 database needs to be initialised from server, -5 sync active.

◆ manage_sync_nvts()

void manage_sync_nvts ( int(*)()  fork_update_nvt_cache)

Sync NVTs if newer NVTs are available.

Parameters
[in]fork_update_nvt_cacheFunction to do the update.

◆ manage_update_nvt_cache_osp()

int manage_update_nvt_cache_osp ( const gchar *  update_socket)

Update VTs via OSP.

Expect to be called in the child after a fork.

Parameters
[in]update_socketSocket to use to contact ospd-openvas scanner.
Returns
0 success, -1 error, 1 VT integrity check failed.

◆ nvt_default_timeout()

char * nvt_default_timeout ( const char *  oid)

Get the default timeout of an NVT.

Parameters
[in]oidThe OID of the NVT to get the timeout of.
Returns
Newly allocated string of the timeout in seconds or NULL.

◆ nvt_family()

char * nvt_family ( const char *  oid)

Get the family of an NVT.

Parameters
[in]oidThe OID of the NVT.
Returns
Newly allocated string of the family, or NULL.

◆ nvt_info_count()

int nvt_info_count ( const get_data_t get)

Count number of nvt.

Parameters
[in]getGET params.
Returns
Total number of cpes in filtered set.

◆ nvt_info_count_after()

int nvt_info_count_after ( const get_data_t get,
time_t  count_time,
gboolean  get_modified 
)

Count number of nvts created or modified after a given time.

Parameters
[in]getGET params.
[in]count_timeTime NVTs must be created or modified after.
[in]get_modifiedWhether to get the modification time.
Returns
Total number of nvts in filtered set.

◆ nvt_iterator_category()

int nvt_iterator_category ( iterator_t iterator)

Get the category from an NVT iterator.

Parameters
[in]iteratorIterator.
Returns
Category.

◆ nvt_iterator_columns()

static gchar * nvt_iterator_columns ( )
static

Get NVT iterator SELECT columns.

Returns
SELECT columns

◆ nvt_iterator_columns_nvts()

static gchar * nvt_iterator_columns_nvts ( )
static

Get NVT iterator SELECT columns.

Returns
SELECT columns

◆ nvt_name()

char * nvt_name ( const char *  oid)

Get the name of an NVT given its OID.

Parameters
[in]oidOID of NVT.
Returns
Name of NVT if possible, else NULL.

◆ nvt_severity_iterator_score()

double nvt_severity_iterator_score ( iterator_t iterator)

Gets the score from an NVT severity iterator.

Parameters
[in]iteratorIterator.
Returns
The score of the severity.

◆ nvti_from_vt()

static nvti_t * nvti_from_vt ( element_t  vt)
static

Create NVTI structure from VT XML.

Parameters
[in]vtOSP GET_VTS VT element.
Returns
The NVTI object on success (needs to be free'd), NULL on error.

◆ nvts_feed_version()

char * nvts_feed_version ( )

Return feed version of the plugins in the plugin cache.

Returns
Feed version of plugins if the plugins are cached, else NULL.

◆ nvts_feed_version_epoch()

time_t nvts_feed_version_epoch ( )

Return feed version of the plugins as seconds since epoch.

Returns
Feed version in seconds since epoch of plugins.

◆ nvts_feed_version_status()

int nvts_feed_version_status ( )

Check VTs feed version status.

Returns
0 VTs feed current, 1 VT update needed, -1 error.

◆ nvts_feed_version_status_internal()

static int nvts_feed_version_status_internal ( const gchar *  update_socket,
gchar **  db_feed_version_out,
gchar **  scanner_feed_version_out 
)
static

Check VTs feed version status via OSP, optionally get versions.

Parameters
[in]update_socketSocket to use to contact ospd-openvas scanner.
[out]db_feed_version_outOutput of database feed version.
[out]scanner_feed_version_outOutput of scanner feed version.
Returns
0 VTs feed current, -1 error, 1 VT update needed.

◆ osp_scanner_feed_version()

static char * osp_scanner_feed_version ( const gchar *  update_socket)
static

Get the VTs feed version from an OSP scanner.

Parameters
[in]update_socketSocket to use to contact ospd-openvas scanner.
Returns
The feed version or NULL on error.

◆ select_config_nvts()

static gchar * select_config_nvts ( const config_t  config,
const char *  family,
int  ascending,
const char *  sort_field 
)
static

Return SQL for selecting NVT's of a config from one family.

Parameters
[in]configConfig.
[in]familyFamily to limit selection to.
[in]ascendingWhether to sort ascending or descending.
[in]sort_fieldField to sort on, or NULL for "nvts.id".
Returns
Freshly allocated SELECT statement on success, or NULL on error.

◆ set_nvts_check_time()

static void set_nvts_check_time ( int  count_new,
int  count_modified 
)
static

Set the NVT update check time in the meta table.

Parameters
[in]count_newNumber of new VTs with current update.
[in]count_modifiedNumber of modified VTs with current update.

◆ set_nvts_feed_version()

void set_nvts_feed_version ( const char *  feed_version)

Set the feed version of the plugins in the plugin cache.

Parameters
[in]feed_versionNew feed version.

Also queue an update to the nvti cache.

◆ set_osp_vt_update_socket()

void set_osp_vt_update_socket ( const char *  new_socket)

Set the file socket for OSP NVT update.

Parameters
new_socketThe new path of the file socket for OSP NVT update.

◆ set_vt_ref_insert_size()

void set_vt_ref_insert_size ( int  new_size)

Set the VT ref insert size.

Parameters
new_sizeNew size.

◆ set_vt_sev_insert_size()

void set_vt_sev_insert_size ( int  new_size)

Set the VT severity insert size.

Parameters
new_sizeNew size.

◆ update_nvt_cache_osp()

static int update_nvt_cache_osp ( const gchar *  update_socket,
gchar *  db_feed_version,
gchar *  scanner_feed_version,
int  rebuild 
)
static

Update VTs via OSP.

Parameters
[in]update_socketSocket to use to contact scanner.
[in]db_feed_versionFeed version from meta table.
[in]scanner_feed_versionFeed version from scanner.
[in]rebuildWhether to rebuild the NVT tables from scratch.
Returns
0 success, 1 VT integrity check failed, -1 error.

◆ update_nvts_from_vts()

static int update_nvts_from_vts ( element_t *  get_vts_response,
const gchar *  scanner_feed_version,
int  rebuild 
)
static

Update NVTs from VTs XML.

Parameters
[in]get_vts_responseOSP GET_VTS response.
[in]scanner_feed_versionVersion of feed from scanner.
[in]rebuildWhether we're rebuilding the tables.
Returns
0 success, 1 VT integrity check failed, -1 error

◆ update_or_rebuild_nvts()

int update_or_rebuild_nvts ( int  update)

Update or rebuild NVT db.

Caller must get the lock.

Parameters
[in]update0 rebuild, else update.
Returns
0 success, -1 error, -1 no osp update socket, -2 could not connect to osp update socket -3 failed to get scanner version

◆ update_preferences_from_vt()

static int update_preferences_from_vt ( element_t  vt,
const gchar *  oid,
GList **  preferences 
)
static

Update NVT from VT XML.

Parameters
[in]vtOSP GET_VTS VT element.
[in]oidOID of NVT.
[in]preferencesAll NVT preferences.
Returns
0 success, -1 error.