Greenbone Vulnerability Manager  22.4.0~dev1
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...
 
int migrate_221_to_222 ()
 Migrate the database from version 221 to version 222. More...
 
int migrate_222_to_223 ()
 Migrate the database from version 222 to version 223. More...
 
int migrate_223_to_224 ()
 Migrate the database from version 223 to version 224. More...
 
int migrate_224_to_225 ()
 Migrate the database from version 224 to version 225. More...
 
int migrate_225_to_226 ()
 Migrate the database from version 225 to version 226. More...
 
int migrate_226_to_227 ()
 Migrate the database from version 226 to version 227. More...
 
static int migrate_227_to_228_delete (const char *table)
 Delete results for migrate_227_to_228. More...
 
int migrate_227_to_228 ()
 Migrate the database from version 227 to version 228. More...
 
int migrate_228_to_229 ()
 Migrate the database from version 228 to version 229. More...
 
int migrate_229_to_230 ()
 Migrate the database from version 229 to version 230. More...
 
static void convert_schedules_231 (gboolean trash)
 Add timezones to schedule iCalendar strings. More...
 
int migrate_230_to_231 ()
 Migrate the database from version 230 to version 231. More...
 
int migrate_231_to_232 ()
 Migrate the database from version 231 to version 232. More...
 
int migrate_232_to_233_set_predefined (const gchar *type, const gchar *table)
 Set predefined. More...
 
int migrate_232_to_233 ()
 Migrate the database from version 232 to version 233. More...
 
int migrate_233_to_234 ()
 Migrate the database from version 233 to version 234. More...
 
int migrate_234_to_235 ()
 Migrate the database from version 234 to version 235. More...
 
int migrate_235_to_236 ()
 Migrate the database from version 235 to version 236. More...
 
int migrate_236_to_237 ()
 Migrate the database from version 236 to version 237. More...
 
int migrate_237_to_238 ()
 Migrate the database from version 237 to version 238. More...
 
int migrate_238_to_239 ()
 Migrate the database from version 238 to version 239. More...
 
int migrate_239_to_240 ()
 Migrate the database from version 239 to version 240. More...
 
int migrate_240_to_241 ()
 Migrate the database from version 240 to version 241. More...
 
int migrate_241_to_242 ()
 Migrate the database from version 241 to version 242. More...
 
int migrate_242_to_243 ()
 Migrate the database from version 242 to version 243. More...
 
int migrate_243_to_244 ()
 Migrate the database from version 243 to version 244. More...
 
int migrate_244_to_245 ()
 Migrate the database from version 244 to version 245. More...
 
int migrate_245_to_246 ()
 Migrate the database from version 245 to version 246. More...
 
int migrate_246_to_247 ()
 Migrate the database from version 246 to version 247. More...
 
int migrate_247_to_248 ()
 Migrate the database from version 247 to version 248. More...
 
int migrate_248_to_249 ()
 Migrate the database from version 248 to version 249. More...
 
int migrate_249_to_250 ()
 Migrate the database from version 249 to version 250. More...
 
gboolean manage_migrate_needs_timezone (GSList *log_config, const db_conn_info_t *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 db_conn_info_t *database)
 Migrate database to version supported by this manager. More...
 

Variables

static migrator_t database_migrators []
 Array of database version migrators.
 

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.

◆ convert_schedules_231()

static void convert_schedules_231 ( gboolean  trash)
static

Add timezones to schedule iCalendar strings.

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 db_conn_info_t 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 db_conn_info_t 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_221_to_222()

int migrate_221_to_222 ( )

Migrate the database from version 221 to version 222.

Returns
0 success, -1 error.

◆ migrate_222_to_223()

int migrate_222_to_223 ( )

Migrate the database from version 222 to version 223.

Returns
0 success, -1 error.

◆ migrate_223_to_224()

int migrate_223_to_224 ( )

Migrate the database from version 223 to version 224.

Returns
0 success, -1 error.

◆ migrate_224_to_225()

int migrate_224_to_225 ( )

Migrate the database from version 224 to version 225.

Returns
0 success, -1 error.

◆ migrate_225_to_226()

int migrate_225_to_226 ( )

Migrate the database from version 225 to version 226.

Returns
0 success, -1 error.

◆ migrate_226_to_227()

int migrate_226_to_227 ( )

Migrate the database from version 226 to version 227.

Returns
0 success, -1 error.

◆ migrate_227_to_228()

int migrate_227_to_228 ( )

Migrate the database from version 227 to version 228.

Returns
0 success, -1 error.

◆ migrate_227_to_228_delete()

static int migrate_227_to_228_delete ( const char *  table)
static

Delete results for migrate_227_to_228.

Parameters
[in]tableName of table.
Returns
Count of deleted rows.

◆ migrate_228_to_229()

int migrate_228_to_229 ( )

Migrate the database from version 228 to version 229.

Returns
0 success, -1 error.

◆ migrate_229_to_230()

int migrate_229_to_230 ( )

Migrate the database from version 229 to version 230.

Returns
0 success, -1 error.

◆ migrate_230_to_231()

int migrate_230_to_231 ( )

Migrate the database from version 230 to version 231.

Returns
0 success, -1 error.

◆ migrate_231_to_232()

int migrate_231_to_232 ( )

Migrate the database from version 231 to version 232.

Returns
0 success, -1 error.

◆ migrate_232_to_233()

int migrate_232_to_233 ( )

Migrate the database from version 232 to version 233.

Returns
0 success, -1 error.

◆ migrate_232_to_233_set_predefined()

int migrate_232_to_233_set_predefined ( const gchar *  type,
const gchar *  table 
)

Set predefined.

Parameters
[in]typeType to update.
[in]tableTable to update.
Returns
0 success, -1 error.

◆ migrate_233_to_234()

int migrate_233_to_234 ( )

Migrate the database from version 233 to version 234.

Returns
0 success, -1 error.

◆ migrate_234_to_235()

int migrate_234_to_235 ( )

Migrate the database from version 234 to version 235.

Returns
0 success, -1 error.

◆ migrate_235_to_236()

int migrate_235_to_236 ( )

Migrate the database from version 235 to version 236.

Returns
0 success, -1 error.

◆ migrate_236_to_237()

int migrate_236_to_237 ( )

Migrate the database from version 236 to version 237.

Returns
0 success, -1 error.

◆ migrate_237_to_238()

int migrate_237_to_238 ( )

Migrate the database from version 237 to version 238.

Returns
0 success, -1 error.

◆ migrate_238_to_239()

int migrate_238_to_239 ( )

Migrate the database from version 238 to version 239.

Returns
0 success, -1 error.

◆ migrate_239_to_240()

int migrate_239_to_240 ( )

Migrate the database from version 239 to version 240.

Returns
0 success, -1 error.

◆ migrate_240_to_241()

int migrate_240_to_241 ( )

Migrate the database from version 240 to version 241.

Returns
0 success, -1 error.

◆ migrate_241_to_242()

int migrate_241_to_242 ( )

Migrate the database from version 241 to version 242.

Returns
0 success, -1 error.

◆ migrate_242_to_243()

int migrate_242_to_243 ( )

Migrate the database from version 242 to version 243.

Returns
0 success, -1 error.

◆ migrate_243_to_244()

int migrate_243_to_244 ( )

Migrate the database from version 243 to version 244.

Returns
0 success, -1 error.

◆ migrate_244_to_245()

int migrate_244_to_245 ( )

Migrate the database from version 244 to version 245.

Returns
0 success, -1 error.

◆ migrate_245_to_246()

int migrate_245_to_246 ( )

Migrate the database from version 245 to version 246.

Returns
0 success, -1 error.

◆ migrate_246_to_247()

int migrate_246_to_247 ( )

Migrate the database from version 246 to version 247.

Returns
0 success, -1 error.

◆ migrate_247_to_248()

int migrate_247_to_248 ( )

Migrate the database from version 247 to version 248.

Returns
0 success, -1 error.

◆ migrate_248_to_249()

int migrate_248_to_249 ( )

Migrate the database from version 248 to version 249.

Returns
0 success, -1 error.

◆ migrate_249_to_250()

int migrate_249_to_250 ( )

Migrate the database from version 249 to version 250.

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