Skip to content

Commit

Permalink
Merge pull request #574 from janowagner/drop_table_nvt_cves
Browse files Browse the repository at this point in the history
Drop table nvt_cves
  • Loading branch information
mattmundell authored Jun 5, 2019
2 parents 4e6ca40 + b6fdd16 commit ee8210d
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 84 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ include (CPack)

## Variables

set (GVMD_DATABASE_VERSION 208)
set (GVMD_DATABASE_VERSION 209)

set (GVMD_SCAP_DATABASE_VERSION 15)

Expand Down
34 changes: 34 additions & 0 deletions src/manage_migrators.c
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand Down Expand Up @@ -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}};

Expand Down
7 changes: 0 additions & 7 deletions src/manage_pg.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,"
Expand Down Expand Up @@ -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');");
Expand Down
58 changes: 0 additions & 58 deletions src/manage_sql_nvts.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,6 @@
*/
#define G_LOG_DOMAIN "md manage"


/* Static headers. */

static void
refresh_nvt_cves ();


/* Helper functions. */

Expand Down Expand Up @@ -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 ();
}

/**
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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;");
}
Expand Down
16 changes: 9 additions & 7 deletions src/manage_sql_secinfo.c
Original file line number Diff line number Diff line change
@@ -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
*
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
16 changes: 9 additions & 7 deletions src/manage_sql_secinfo.h
Original file line number Diff line number Diff line change
@@ -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
*
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 0 additions & 4 deletions src/manage_sqlite3.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,"
Expand Down

0 comments on commit ee8210d

Please sign in to comment.