diff --git a/CMakeLists.txt b/CMakeLists.txt index 1aca5286b..fc6351742 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -117,7 +117,7 @@ include (CPack) ## Variables -set (GVMD_DATABASE_VERSION 208) +set (GVMD_DATABASE_VERSION 209) set (GVMD_SCAP_DATABASE_VERSION 15) diff --git a/src/manage_migrators.c b/src/manage_migrators.c index 5cbfafb47..e86f38161 100644 --- a/src/manage_migrators.c +++ b/src/manage_migrators.c @@ -1704,6 +1704,39 @@ migrate_207_to_208 () return 0; } +/** + * @brief Migrate the database from version 208 to version 209. + * + * @return 0 success, -1 error. + */ +int +migrate_208_to_209 () +{ + sql_begin_immediate (); + + /* Ensure that the database is currently version 208. */ + + if (manage_db_version () != 208) + { + sql_rollback (); + return -1; + } + + /* Update the database. */ + + /* Drop the now-unused table "nvt_cves". */ + + sql ("DROP TABLE IF EXISTS nvt_cves;"); + + /* Set the database version to 209. */ + + set_db_version (209); + + sql_commit (); + + return 0; +} + #undef UPDATE_DASHBOARD_SETTINGS /** @@ -1739,6 +1772,7 @@ static migrator_t database_migrators[] = { {206, migrate_205_to_206}, {207, migrate_206_to_207}, {208, migrate_207_to_208}, + {209, migrate_208_to_209}, /* End marker. */ {-1, NULL}}; diff --git a/src/manage_pg.c b/src/manage_pg.c index bd634072f..b8c646c4f 100644 --- a/src/manage_pg.c +++ b/src/manage_pg.c @@ -2873,12 +2873,6 @@ create_tables () " qod integer," " qod_type text);"); - sql ("CREATE TABLE IF NOT EXISTS nvt_cves" - " (id SERIAL PRIMARY KEY," - " nvt integer REFERENCES nvts (id) ON DELETE RESTRICT," - " oid text," - " cve_name text);"); - sql ("CREATE TABLE IF NOT EXISTS notes" " (id SERIAL PRIMARY KEY," " uuid text UNIQUE NOT NULL," @@ -3206,7 +3200,6 @@ create_tables () sql ("SELECT create_index ('host_oss_by_host'," " 'host_oss', 'host');"); - sql ("SELECT create_index ('nvt_cves_by_oid', 'nvt_cves', 'oid');"); sql ("SELECT create_index ('nvt_selectors_by_family_or_nvt'," " 'nvt_selectors'," " 'type, family_or_nvt');"); diff --git a/src/manage_sql_nvts.c b/src/manage_sql_nvts.c index 4e1d59768..6c782d6b1 100644 --- a/src/manage_sql_nvts.c +++ b/src/manage_sql_nvts.c @@ -47,12 +47,6 @@ */ #define G_LOG_DOMAIN "md manage" - -/* Static headers. */ - -static void -refresh_nvt_cves (); - /* Helper functions. */ @@ -86,10 +80,6 @@ check_db_nvts () sql ("INSERT INTO %s.meta (name, value)" " VALUES ('update_nvti_cache', 0);", sql_schema ()); - - /* Ensure the NVT CVE table is filled. */ - if (sql_int ("SELECT count (*) FROM nvt_cves;") == 0) - refresh_nvt_cves (); } /** @@ -991,53 +981,6 @@ check_for_updated_nvts () event (EVENT_UPDATED_SECINFO, "nvt", 0, 0); } -/** - * @brief Refresh nvt_cves table. - * - * Caller must organise transaction. - */ -static void -refresh_nvt_cves () -{ - iterator_t nvts; - - sql ("DELETE FROM nvt_cves;"); - - init_iterator (&nvts, "SELECT id, oid, cve FROM nvts;"); - while (next (&nvts)) - { - gchar **split, **point; - - split = g_strsplit_set (iterator_string (&nvts, 2), " ,", 0); - - point = split; - while (*point) - { - g_strstrip (*point); - if (strlen (*point)) - { - gchar *quoted_cve, *quoted_oid; - - quoted_cve = sql_insert (*point); - quoted_oid = sql_insert (iterator_string (&nvts, 1)); - sql ("INSERT INTO nvt_cves (nvt, oid, cve_name)" - " VALUES (%llu, %s, %s);", - iterator_int64 (&nvts, 0), - quoted_oid, - quoted_cve); - g_free (quoted_cve); - g_free (quoted_oid); - } - point++; - } - g_strfreev (split); - } - cleanup_iterator (&nvts); - - if (sql_is_sqlite3 ()) - sql ("REINDEX nvt_cves_by_oid;"); -} - /** * @brief Set the NVT update check time in the meta table. */ @@ -1437,7 +1380,6 @@ update_nvts_from_vts (entity_t *get_vts_response, if (sql_is_sqlite3 ()) { - sql ("DELETE FROM nvt_cves;"); sql ("DELETE FROM nvts;"); sql ("DELETE FROM nvt_preferences;"); } diff --git a/src/manage_sql_secinfo.c b/src/manage_sql_secinfo.c index 080d2bc4b..58889b229 100644 --- a/src/manage_sql_secinfo.c +++ b/src/manage_sql_secinfo.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2009-2018 Greenbone Networks GmbH +/* Copyright (C) 2009-2019 Greenbone Networks GmbH * * SPDX-License-Identifier: GPL-2.0-or-later * @@ -918,9 +918,10 @@ init_nvt_cert_bund_adv_iterator (iterator_t *iterator, const char *oid, "SELECT %s" " FROM cert_bund_advs" " WHERE id IN (SELECT adv_id FROM cert_bund_cves" - " WHERE cve_name IN (SELECT cve_name" - " FROM nvt_cves" - " WHERE oid = '%s'))" + " WHERE cve_name IN (SELECT ref_id" + " FROM vt_refs" + " WHERE vt_oid = '%s'" + " AND type = 'cve'))" " ORDER BY %s %s;", columns, oid, @@ -1098,9 +1099,10 @@ init_nvt_dfn_cert_adv_iterator (iterator_t *iterator, const char *oid, "SELECT %s" " FROM dfn_cert_advs" " WHERE id IN (SELECT adv_id FROM dfn_cert_cves" - " WHERE cve_name IN (SELECT cve_name" - " FROM nvt_cves" - " WHERE oid = '%s'))" + " WHERE cve_name IN (SELECT ref_id" + " FROM vt_refs" + " WHERE vt_oid = '%s'" + " AND type = 'cve'))" " ORDER BY %s %s;", columns, oid, diff --git a/src/manage_sql_secinfo.h b/src/manage_sql_secinfo.h index 90dabe16e..7f2661002 100644 --- a/src/manage_sql_secinfo.h +++ b/src/manage_sql_secinfo.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2010-2018 Greenbone Networks GmbH +/* Copyright (C) 2010-2019 Greenbone Networks GmbH * * SPDX-License-Identifier: GPL-2.0-or-later * @@ -30,18 +30,20 @@ */ #define SECINFO_SQL_RESULT_HAS_CERT_BUNDS \ "(SELECT EXISTS (SELECT * FROM cert_bund_cves" \ - " WHERE cve_name IN (SELECT cve_name" \ - " FROM nvt_cves" \ - " WHERE oid = results.nvt)))" + " WHERE cve_name IN (SELECT ref_id" \ + " FROM vt_refs" \ + " WHERE vt_oid = results.nvt" \ + " AND type = 'cve')))" /** * @brief SQL to check if a result has CERT Bunds. */ #define SECINFO_SQL_RESULT_HAS_DFN_CERTS \ "(SELECT EXISTS (SELECT * FROM dfn_cert_cves" \ - " WHERE cve_name IN (SELECT cve_name" \ - " FROM nvt_cves" \ - " WHERE oid = results.nvt)))" + " WHERE cve_name IN (SELECT ref_id" \ + " FROM vt_refs" \ + " WHERE vt_oid = results.nvt" \ + " AND type = 'cve')))" /** * @brief Filter columns for CVE iterator. diff --git a/src/manage_sqlite3.c b/src/manage_sqlite3.c index 2529de193..f1d82b950 100644 --- a/src/manage_sqlite3.c +++ b/src/manage_sqlite3.c @@ -3674,8 +3674,6 @@ create_tables () " ON nvts (name);"); sql ("CREATE INDEX IF NOT EXISTS nvts_by_family" " ON nvts (family);"); - sql ("CREATE TABLE IF NOT EXISTS nvt_cves" - " (nvt, oid, cve_name)"); sql ("CREATE INDEX IF NOT EXISTS nvts_by_creation_time" " ON nvts (creation_time);"); sql ("CREATE INDEX IF NOT EXISTS nvts_by_modification_time" @@ -3684,8 +3682,6 @@ create_tables () " ON nvts (cvss_base);"); sql ("CREATE INDEX IF NOT EXISTS nvts_by_solution_type" " ON nvts (solution_type);"); - sql ("CREATE INDEX IF NOT EXISTS nvt_cves_by_oid" - " ON nvt_cves (oid);"); sql ("CREATE TABLE IF NOT EXISTS overrides" " (id INTEGER PRIMARY KEY, uuid UNIQUE, owner INTEGER, nvt, result_nvt," " creation_time, modification_time, text, hosts, port, severity,"