Greenbone Vulnerability Manager  9.0.1
Data Structures | Macros | Functions | Variables
manage_migrators.c File Reference

The Greenbone Vulnerability Manager DB Migrators file. More...

#include <assert.h>
#include <errno.h>
#include <glib/gstdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <time.h>
#include "manage_migrators_219_to_220_names.h"
#include "manage_sql.h"
#include "sql.h"
#include "utils.h"
#include <ctype.h>
#include <dirent.h>
#include <gvm/base/logging.h>
#include <gvm/util/fileutils.h>
#include <gvm/util/uuidutils.h>

Data Structures

struct  migrator_t
 A migrator. More...
 

Macros

#define _XOPEN_SOURCE
 
#define G_LOG_DOMAIN   "md main"
 GLib log domain.
 
#define MIGRATE_MIN_OLD_VERSION   205
 The oldest version for which migration is supported.
 

Functions

void manage_create_result_indexes ()
 Create result indexes.
 
static void move (const gchar *table, const gchar *old, const gchar *new)
 Rename a column. More...
 
int migrate_204_to_205 ()
 Migrate the database from version 204 to version 205. More...
 
static void replace_preference_names_205_to_206 (const char *table_name)
 Converts old NVT preferences to the new format. More...
 
int migrate_205_to_206 ()
 Migrate the database from version 205 to version 206. More...
 
int migrate_206_to_207 ()
 Migrate the database from version 206 to version 207. More...
 
int migrate_207_to_208 ()
 Migrate the database from version 207 to version 208. More...
 
int migrate_208_to_209 ()
 Migrate the database from version 208 to version 209. More...
 
int migrate_209_to_210 ()
 Migrate the database from version 209 to version 210. More...
 
int migrate_210_to_211 ()
 Migrate the database from version 210 to version 211. More...
 
int migrate_211_to_212 ()
 Migrate the database from version 211 to version 212. More...
 
resource_t tls_certificate_get_location_213 (const char *host_ip, const char *port)
 Gets or creates a tls_certificate_location in the version 213 format. More...
 
resource_t tls_certificate_get_origin_213 (const char *origin_type, const char *origin_id, const char *origin_data)
 Gets or creates a tls_certificate_origin in the version 213 format. More...
 
int migrate_212_to_213 ()
 Migrate the database from version 212 to version 213. More...
 
static tls_certificate_t make_tls_certificate_214 (user_t owner, const char *certificate_b64, const char *subject_dn, const char *issuer_dn, time_t activation_time, time_t expiration_time, const char *md5_fingerprint, const char *sha256_fingerprint, const char *serial, gnutls_x509_crt_fmt_t certificate_format)
 Create a TLS certificate in the version 214 format. More...
 
static void create_temp_tables_213_to_214 ()
 Create temporary, indexed tables for TLS certificate host details. More...
 
int migrate_213_to_214 ()
 Migrate the database from version 213 to version 214. More...
 
int migrate_214_to_215 ()
 Migrate the database from version 214 to version 215. More...
 
int migrate_215_to_216 ()
 Migrate the database from version 215 to version 216. More...
 
int migrate_216_to_217 ()
 Migrate the database from version 216 to version 217. More...
 
int migrate_217_to_218 ()
 Migrate the database from version 217 to version 218. More...
 
int migrate_218_to_219 ()
 Migrate the database from version 218 to version 219. More...
 
static const gchar * migrate_219_to_220_new_name (const char *old_name)
 Get new name of a preference. More...
 
static void replace_preference_names_219_to_220 (const char *table_name)
 Converts old NVT preferences to the new format. More...
 
int migrate_219_to_220 ()
 Migrate the database from version 219 to version 220. More...
 
static void convert_schedules_221 (gboolean trash)
 Convert iCalendar strings of schedules to new format for version 221. More...
 
int migrate_220_to_221 ()
 Migrate the database from version 220 to version 221. More...
 
gboolean manage_migrate_needs_timezone (GSList *log_config, const gchar *database)
 Check whether the migration needs the real timezone. More...
 
static int migrate_is_available (int old_version, int new_version)
 Check whether a migration is available. More...
 
int manage_migrate (GSList *log_config, const gchar *database)
 Migrate database to version supported by this manager. More...
 

Variables

static migrator_t database_migrators []
 Array of database version migrators. More...
 

Detailed Description

The Greenbone Vulnerability Manager DB Migrators file.

This file defines the functions used by the manager to migrate the DB to the newest version.

Macro Definition Documentation

◆ _XOPEN_SOURCE

#define _XOPEN_SOURCE

Procedure for writing a migrator

Every change that affects the database schema or the format of the data in the database must have a migrator so that someone using an older version of the database can update to the newer version.

Simply adding a new table to the database is, however, OK. At startup, the manager will automatically add a table if it is missing from the database.

  • Ensure that the ChangeLog notes the changes to the database and the increase of GVMD_DATABASE_VERSION, with an entry like
    * CMakeLists.txt (GVMD_DATABASE_VERSION): Increase to 6, for...
    
    * src/manage_sql.c (create_tables): Add new table...
    
  • Add the migrator function in the style of the others. In particular, the function must check the version, do the modification and then set the new version, all inside an exclusive transaction. Use the generic iterator (init_iterator, iterator_string, iterator_int64...) because the specialised iterators (like init_target_iterator) can change behaviour across Manager SVN versions. Use copies of any other "manage" interfaces, for example update_all_config_caches, as these may also change in later versions of the Manager.
  • Remember to ensure that tables exist in the migrator before the migrator modifies them. If a migrator modifies a table then the table must either have existed in database version 0 (listed below), or some earlier migrator must have added the table, or the migrator must add the table (using the original schema of the table).
  • Add the migrator to the database_migrators array.
  • Test that everything still works for a database that has been migrated from the previous version.
  • Test that everything still works for a database that has been migrated from version 0.
  • Commit with a ChangeLog heading like
    Add database migration from version 5 to 6.
    

SQL that created database version 0:

CREATE TABLE IF NOT EXISTS config_preferences
  (config INTEGER, type, name, value);

CREATE TABLE IF NOT EXISTS configs
  (name UNIQUE, nvt_selector, comment, family_count INTEGER,
   nvt_count INTEGER, families_growing INTEGER, nvts_growing INTEGER);

CREATE TABLE IF NOT EXISTS meta
  (name UNIQUE, value);

CREATE TABLE IF NOT EXISTS nvt_selectors
  (name, exclude INTEGER, type INTEGER, family_or_nvt);

CREATE TABLE IF NOT EXISTS nvts
  (oid, version, name, summary, description, copyright, cve, bid, xref,
   tag, sign_key_ids, category, family);

CREATE TABLE IF NOT EXISTS report_hosts
  (report INTEGER, host, start_time, end_time, attack_state,
   current_port, max_port);

CREATE TABLE IF NOT EXISTS report_results
  (report INTEGER, result INTEGER);

CREATE TABLE IF NOT EXISTS reports
  (uuid, hidden INTEGER, task INTEGER, date INTEGER, start_time,
   end_time, nbefile, comment);

CREATE TABLE IF NOT EXISTS results
  (task INTEGER, subnet, host, port, nvt, type, description);

CREATE TABLE IF NOT EXISTS targets
  (name, hosts, comment);

CREATE TABLE IF NOT EXISTS tasks
  (uuid, name, hidden INTEGER, time, comment, description, owner,
   run_status, start_time, end_time, config, target);

CREATE TABLE IF NOT EXISTS users
  (name UNIQUE, password);

Function Documentation

◆ convert_schedules_221()

static void convert_schedules_221 ( gboolean  trash)
static

Convert iCalendar strings of schedules to new format for version 221.

Parameters
[in]trashWhether to convert the trash table.

◆ create_temp_tables_213_to_214()

static void create_temp_tables_213_to_214 ( )
static

Create temporary, indexed tables for TLS certificate host details.

This makes it quicker to access host details that are slow to get repeatedly for every TLS certificate, presumably because of the "LIKE" conditions for the name or value field.

◆ make_tls_certificate_214()

static tls_certificate_t make_tls_certificate_214 ( user_t  owner,
const char *  certificate_b64,
const char *  subject_dn,
const char *  issuer_dn,
time_t  activation_time,
time_t  expiration_time,
const char *  md5_fingerprint,
const char *  sha256_fingerprint,
const char *  serial,
gnutls_x509_crt_fmt_t  certificate_format 
)
static

Create a TLS certificate in the version 214 format.

Parameters
[in]ownerOwner of the new tls_certificate.
[in]certificate_b64The Base64 encoded certificate.
[in]subject_dnThe subject DN of the certificate.
[in]issuer_dnThe issuer DN of the certificate.
[in]activation_timeTime before which the certificate is invalid.
[in]expiration_timeTime after which the certificate is expired.
[in]md5_fingerprintMD5 fingerprint of the certificate.
[in]sha256_fingerprintSHA-256 fingerprint of the certificate.
[in]serialSerial of the certificate.
[in]certificate_formatCertificate format (DER or PEM).
Returns
The new TLS certificate.

◆ manage_migrate()

int manage_migrate ( GSList *  log_config,
const gchar *  database 
)

Migrate database to version supported by this manager.

Parameters
[in]log_configLog configuration.
[in]databaseLocation of manage database.
Returns
0 success, 1 already on supported version, 2 too hard, 11 cannot migrate SCAP DB, 12 cannot migrate CERT DB, -1 error, -11 error running SCAP migration, -12 error running CERT migration.

◆ manage_migrate_needs_timezone()

gboolean manage_migrate_needs_timezone ( GSList *  log_config,
const gchar *  database 
)

Check whether the migration needs the real timezone.

Parameters
[in]log_configLog configuration.
[in]databaseLocation of manage database.
Returns
TRUE if yes, else FALSE.

◆ migrate_204_to_205()

int migrate_204_to_205 ( )

Migrate the database from version 204 to version 205.

Returns
0 success, -1 error.

◆ migrate_205_to_206()

int migrate_205_to_206 ( )

Migrate the database from version 205 to version 206.

Returns
0 success, -1 error.

◆ migrate_206_to_207()

int migrate_206_to_207 ( )

Migrate the database from version 206 to version 207.

Returns
0 success, -1 error.

◆ migrate_207_to_208()

int migrate_207_to_208 ( )

Migrate the database from version 207 to version 208.

Returns
0 success, -1 error.

◆ migrate_208_to_209()

int migrate_208_to_209 ( )

Migrate the database from version 208 to version 209.

Returns
0 success, -1 error.

◆ migrate_209_to_210()

int migrate_209_to_210 ( )

Migrate the database from version 209 to version 210.

Returns
0 success, -1 error.

◆ migrate_210_to_211()

int migrate_210_to_211 ( )

Migrate the database from version 210 to version 211.

Returns
0 success, -1 error.

◆ migrate_211_to_212()

int migrate_211_to_212 ( )

Migrate the database from version 211 to version 212.

Returns
0 success, -1 error.

◆ migrate_212_to_213()

int migrate_212_to_213 ( )

Migrate the database from version 212 to version 213.

Returns
0 success, -1 error.

◆ migrate_213_to_214()

int migrate_213_to_214 ( )

Migrate the database from version 213 to version 214.

Returns
0 success, -1 error.

◆ migrate_214_to_215()

int migrate_214_to_215 ( )

Migrate the database from version 214 to version 215.

Returns
0 success, -1 error.

◆ migrate_215_to_216()

int migrate_215_to_216 ( )

Migrate the database from version 215 to version 216.

Returns
0 success, -1 error.

◆ migrate_216_to_217()

int migrate_216_to_217 ( )

Migrate the database from version 216 to version 217.

Returns
0 success, -1 error.

◆ migrate_217_to_218()

int migrate_217_to_218 ( )

Migrate the database from version 217 to version 218.

Returns
0 success, -1 error.

◆ migrate_218_to_219()

int migrate_218_to_219 ( )

Migrate the database from version 218 to version 219.

Returns
0 success, -1 error.

◆ migrate_219_to_220()

int migrate_219_to_220 ( )

Migrate the database from version 219 to version 220.

Returns
0 success, -1 error.

◆ migrate_219_to_220_new_name()

static const gchar* migrate_219_to_220_new_name ( const char *  old_name)
static

Get new name of a preference.

Parameters
[in]old_nameOld name of preference.
Returns
Static string containing new name for preference if found, else NULL.

◆ migrate_220_to_221()

int migrate_220_to_221 ( )

Migrate the database from version 220 to version 221.

Returns
0 success, -1 error.

◆ migrate_is_available()

static int migrate_is_available ( int  old_version,
int  new_version 
)
static

Check whether a migration is available.

Parameters
[in]old_versionVersion to migrate from.
[in]new_versionVersion to migrate to.
Returns
1 yes, 0 no, -1 error.

◆ move()

static void move ( const gchar *  table,
const gchar *  old,
const gchar *  new 
)
static

Rename a column.

Todo:
May be better ensure a ROLLBACK when functions like "sql" fail.

Currently the SQL functions abort on failure. This a general problem, not just for migrators, so perhaps the SQL interface should keep track of the transaction, and rollback before aborting.

Parameters
[in]tableTable
[in]oldOld column.
[in]newNew column.

◆ replace_preference_names_205_to_206()

static void replace_preference_names_205_to_206 ( const char *  table_name)
static

Converts old NVT preferences to the new format.

Parameters
[in]table_nameThe name of the table to update.

◆ replace_preference_names_219_to_220()

static void replace_preference_names_219_to_220 ( const char *  table_name)
static

Converts old NVT preferences to the new format.

Parameters
[in]table_nameThe name of the table to update.

◆ tls_certificate_get_location_213()

resource_t tls_certificate_get_location_213 ( const char *  host_ip,
const char *  port 
)

Gets or creates a tls_certificate_location in the version 213 format.

If a location with matching host_ip and port exists its id is returned, otherwise a new one is created and its id is returned.

Parameters
[in]host_ipIP address of the location
[in]portPort number of the location
Returns
Row id of the tls_certificate_location

◆ tls_certificate_get_origin_213()

resource_t tls_certificate_get_origin_213 ( const char *  origin_type,
const char *  origin_id,
const char *  origin_data 
)

Gets or creates a tls_certificate_origin in the version 213 format.

If an origin with matching type, id and data exists its id is returned, otherwise a new one is created and its id is returned.

Parameters
[in]origin_typeOrigin type, e.g. "GMP" or "Report"
[in]origin_idOrigin resource id, e.g. a report UUID.
[in]origin_dataOrigin extra data, e.g. OID of generating NVT.
Returns
Row id of the tls_certificate_origin

Variable Documentation

◆ database_migrators

migrator_t database_migrators[]
static
Initial value:
= {
{-1, NULL}}
int migrate_215_to_216()
Migrate the database from version 215 to version 216.
Definition: manage_migrators.c:1284
int migrate_212_to_213()
Migrate the database from version 212 to version 213.
Definition: manage_migrators.c:669
int migrate_216_to_217()
Migrate the database from version 216 to version 217.
Definition: manage_migrators.c:1316
int migrate_220_to_221()
Migrate the database from version 220 to version 221.
Definition: manage_migrators.c:1606
int migrate_206_to_207()
Migrate the database from version 206 to version 207.
Definition: manage_migrators.c:338
int migrate_210_to_211()
Migrate the database from version 210 to version 211.
Definition: manage_migrators.c:489
int migrate_209_to_210()
Migrate the database from version 209 to version 210.
Definition: manage_migrators.c:455
int migrate_217_to_218()
Migrate the database from version 217 to version 218.
Definition: manage_migrators.c:1356
int migrate_218_to_219()
Migrate the database from version 218 to version 219.
Definition: manage_migrators.c:1396
int migrate_208_to_209()
Migrate the database from version 208 to version 209.
Definition: manage_migrators.c:422
int migrate_213_to_214()
Migrate the database from version 213 to version 214.
Definition: manage_migrators.c:969
int migrate_214_to_215()
Migrate the database from version 214 to version 215.
Definition: manage_migrators.c:1252
int migrate_219_to_220()
Migrate the database from version 219 to version 220.
Definition: manage_migrators.c:1498
int migrate_205_to_206()
Migrate the database from version 205 to version 206.
Definition: manage_migrators.c:300
int migrate_207_to_208()
Migrate the database from version 207 to version 208.
Definition: manage_migrators.c:385
int migrate_211_to_212()
Migrate the database from version 211 to version 212.
Definition: manage_migrators.c:525

Array of database version migrators.