From abe6c8ba1b253498bcd145b83b27fd44ff94ca9b Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Mon, 12 Jun 2023 19:53:12 +0200 Subject: [PATCH 1/4] Add detailed nvt_preferences columns, with migrator --- CMakeLists.txt | 2 +- src/manage_migrators.c | 50 +++++++++++++++++++++++++++++++++++++++++- src/manage_pg.c | 4 ++++ 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8bf433fae..f8afcfd5a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -98,7 +98,7 @@ include (CPack) ## Variables -set (GVMD_DATABASE_VERSION 254) +set (GVMD_DATABASE_VERSION 255) set (GVMD_SCAP_DATABASE_VERSION 20) diff --git a/src/manage_migrators.c b/src/manage_migrators.c index 4e63ba829..91d1c464a 100644 --- a/src/manage_migrators.c +++ b/src/manage_migrators.c @@ -3058,7 +3058,6 @@ migrate_252_to_253 () return 0; } - /** * @brief Alter and update for migrate_253_to_254. * @@ -3124,6 +3123,54 @@ migrate_253_to_254 () return 0; } +/** + * @brief Migrate the database from version 254 to version 255. + * + * @return 0 success, -1 error. + */ +int +migrate_254_to_255 () +{ + sql_begin_immediate (); + + /* Ensure that the database is currently version 254. */ + + if (manage_db_version () != 254) + { + sql_rollback (); + return -1; + } + + /* Update the database. */ + + sql ("ALTER TABLE nvt_preferences ADD COLUMN pref_nvt text;"); + sql ("UPDATE nvt_preferences" + " SET pref_nvt = substring (name, '^([^:]*)')" + " WHERE name LIKE '%:%';"); + + sql ("ALTER TABLE nvt_preferences ADD COLUMN pref_id integer;"); + sql ("UPDATE nvt_preferences" + " SET pref_id = CAST (substring (name, '^[^:]*:([^:]*)') AS integer)" + " WHERE name LIKE '%:%';"); + + sql ("ALTER table nvt_preferences ADD COLUMN pref_type text;"); + sql ("UPDATE nvt_preferences" + " SET pref_type = substring (name, '^[^:]*:[^:]*:([^:]*):')" + " WHERE name LIKE '%:%';"); + + sql ("ALTER table nvt_preferences ADD COLUMN pref_name text;"); + sql ("UPDATE nvt_preferences" + " SET pref_name = substring (name, '^[^:]*:[^:]*:[^:]*:(.*)')" + " WHERE name LIKE '%:%';"); + + /* Set the database version to 255. */ + + set_db_version (255); + + sql_commit (); + + return 0; +} #undef UPDATE_DASHBOARD_SETTINGS @@ -3185,6 +3232,7 @@ static migrator_t database_migrators[] = { {252, migrate_251_to_252}, {253, migrate_252_to_253}, {254, migrate_253_to_254}, + {255, migrate_254_to_255}, /* End marker. */ {-1, NULL}}; diff --git a/src/manage_pg.c b/src/manage_pg.c index 5a3777f2b..695b81063 100644 --- a/src/manage_pg.c +++ b/src/manage_pg.c @@ -1774,6 +1774,10 @@ create_tables_nvt (const gchar *suffix) sql ("CREATE TABLE IF NOT EXISTS nvt_preferences%s" " (id SERIAL PRIMARY KEY," " name text UNIQUE NOT NULL," + " pref_nvt text," + " pref_id integer," + " pref_type text," + " pref_name text," " value text);", suffix); From aca0050e7e51cc3bc39fd80a161c8ffcf1a41690 Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Mon, 12 Jun 2023 20:08:36 +0200 Subject: [PATCH 2/4] Use new columns in nvt_preference_count --- src/manage_sql_configs.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/manage_sql_configs.c b/src/manage_sql_configs.c index 865de17db..3d25e100f 100644 --- a/src/manage_sql_configs.c +++ b/src/manage_sql_configs.c @@ -1940,10 +1940,8 @@ nvt_preference_count (const char *oid) { gchar *quoted_oid = sql_quote (oid); int ret = sql_int ("SELECT COUNT(*) FROM nvt_preferences" - " WHERE name != '%s:0:entry:Timeout'" - " AND name %s '%s:%%';", - quoted_oid, - sql_ilike_op (), + " WHERE NOT (pref_type = 'entry' AND pref_name = 'Timeout')" + " AND pref_nvt = '%s';", quoted_oid); g_free (quoted_oid); return ret; From 8a971c48c97150e5f4e88194c3f3fb37a8cc2d38 Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Tue, 20 Jun 2023 19:23:22 +0200 Subject: [PATCH 3/4] Set new columns when adding NVT preference --- src/manage.h | 3 ++- src/manage_preferences.c | 1 + src/manage_preferences.h | 3 ++- src/manage_sql_configs.c | 34 ++++++++++++++++++++++++---------- src/manage_sql_nvts.c | 10 ++++++++-- 5 files changed, 37 insertions(+), 14 deletions(-) diff --git a/src/manage.h b/src/manage.h index 0d42c026c..a1a8faa1c 100644 --- a/src/manage.h +++ b/src/manage.h @@ -1924,7 +1924,8 @@ nvt_selector_iterator_type (iterator_t*); /* NVT preferences. */ void -manage_nvt_preference_add (const char*, const char*, int); +manage_nvt_preference_add (const char*, const char*, const char*, const char*, + const char*, const char*, int); void manage_nvt_preferences_enable (); diff --git a/src/manage_preferences.c b/src/manage_preferences.c index 165accda0..197a622ee 100644 --- a/src/manage_preferences.c +++ b/src/manage_preferences.c @@ -80,6 +80,7 @@ preference_free (preference_t *preference) { free (preference->id); free (preference->name); + free (preference->pref_name); free (preference->type); free (preference->value); free (preference->nvt_name); diff --git a/src/manage_preferences.h b/src/manage_preferences.h index 19c628039..14637c8db 100644 --- a/src/manage_preferences.h +++ b/src/manage_preferences.h @@ -31,7 +31,8 @@ */ typedef struct { - char *name; ///< Name of preference. + char *name; ///< Full name of preference, including OID etc. + char *pref_name; ///< Name of preference. char *id; ///< ID of preference. char *type; ///< Type of preference (radio, password, ...). char *value; ///< Value of preference. diff --git a/src/manage_sql_configs.c b/src/manage_sql_configs.c index 3d25e100f..f79d6b1ea 100644 --- a/src/manage_sql_configs.c +++ b/src/manage_sql_configs.c @@ -1699,13 +1699,22 @@ check_config_families () * @param[in] rebuild Whether a rebuild is happening. */ void -manage_nvt_preference_add (const char* name, const char* value, int rebuild) +manage_nvt_preference_add (const char *name, const char *value, + const char *oid, const char *id, + const char *type, const char *pref_name, + int rebuild) { - gchar* quoted_name = sql_quote (name); - gchar* quoted_value = sql_quote (value); - if (strcmp (name, "port_range")) { + gchar *quoted_name, *quoted_value, *quoted_oid, *quoted_type; + gchar *quoted_pref_name; + + quoted_name = sql_quote (name); + quoted_value = sql_quote (value); + quoted_oid = sql_quote (oid); + quoted_type = sql_quote (type); + quoted_pref_name = sql_quote (pref_name); + if (rebuild == 0) { if (sql_int ("SELECT EXISTS" " (SELECT * FROM nvt_preferences" @@ -1714,14 +1723,19 @@ manage_nvt_preference_add (const char* name, const char* value, int rebuild) sql ("DELETE FROM nvt_preferences WHERE name = '%s';", quoted_name); } - sql ("INSERT into nvt_preferences%s (name, value)" - " VALUES ('%s', '%s');", + sql ("INSERT into nvt_preferences%s" + " (name, value, pref_nvt, pref_id, pref_type, pref_name)" + " VALUES ('%s', '%s', '%s', %i, '%s', '%s');", rebuild ? "_rebuild" : "", - quoted_name, quoted_value); - } + quoted_name, quoted_value, quoted_oid, atoi (id), quoted_type, + quoted_pref_name); - g_free (quoted_name); - g_free (quoted_value); + g_free (quoted_name); + g_free (quoted_value); + g_free (quoted_oid); + g_free (quoted_type); + g_free (quoted_pref_name); + } } /** diff --git a/src/manage_sql_nvts.c b/src/manage_sql_nvts.c index 0a74b4554..dcdf1f157 100644 --- a/src/manage_sql_nvts.c +++ b/src/manage_sql_nvts.c @@ -1255,7 +1255,10 @@ insert_nvt_preference (gpointer nvt_preference, gpointer rebuild) return; preference = (preference_t*) nvt_preference; - manage_nvt_preference_add (preference->name, preference->value, GPOINTER_TO_INT (rebuild)); + manage_nvt_preference_add (preference->name, preference->value, + preference->nvt_oid, preference->id, + preference->type, preference->pref_name, + GPOINTER_TO_INT (rebuild)); } /** @@ -1368,7 +1371,6 @@ update_preferences_from_vt (element_t vt, const gchar *oid, GList **preferences) id, type, text); - g_free (text); blank_control_chars (full_name); preference = g_malloc0 (sizeof (preference_t)); @@ -1377,6 +1379,10 @@ update_preferences_from_vt (element_t vt, const gchar *oid, GList **preferences) preference->value = element_text (def); else preference->value = g_strdup (""); + preference->nvt_oid = g_strdup (oid); + preference->id = g_strdup (id); + preference->type = g_strdup (type); + preference->pref_name = text; *preferences = g_list_prepend (*preferences, preference); } From b93bf514ce3f30b413c4b552d12cf3ef890ae18d Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Tue, 20 Jun 2023 19:24:32 +0200 Subject: [PATCH 4/4] Free preferences with correct function --- src/manage_sql_nvts.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/manage_sql_nvts.c b/src/manage_sql_nvts.c index dcdf1f157..91b0b12fa 100644 --- a/src/manage_sql_nvts.c +++ b/src/manage_sql_nvts.c @@ -1374,6 +1374,7 @@ update_preferences_from_vt (element_t vt, const gchar *oid, GList **preferences) blank_control_chars (full_name); preference = g_malloc0 (sizeof (preference_t)); + preference->free_strings = 1; preference->name = full_name; if (def) preference->value = element_text (def); @@ -1769,7 +1770,7 @@ update_nvts_from_vts (element_t *get_vts_response, rebuild ? "_rebuild" : "", nvti_oid (nvti)); insert_nvt_preferences_list (preferences, rebuild); - g_list_free_full (preferences, g_free); + g_list_free_full (preferences, (GDestroyNotify) preference_free); nvti_free (nvti); vt = element_next (vt);