Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure all NVT preference names are in the new format #744

Merged
merged 17 commits into from
Sep 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Added
- Added TLS certificates as a new resource type [#585](https://github.com/greenbone/gvmd/pull/585) [#663](https://github.com/greenbone/gvmd/pull/663) [#673](https://github.com/greenbone/gvmd/pull/673) [#695](https://github.com/greenbone/gvmd/pull/695) [#703](https://github.com/greenbone/gvmd/pull/703) [#728](https://github.com/greenbone/gvmd/pull/728) [#732](https://github.com/greenbone/gvmd/pull/732) [#750](https://github.com/greenbone/gvmd/pull/750) [#752](https://github.com/greenbone/gvmd/pull/752)
- Update NVTs via OSP [#392](https://github.com/greenbone/gvmd/pull/392) [#609](https://github.com/greenbone/gvmd/pull/609) [#626](https://github.com/greenbone/gvmd/pull/626)
- Handle addition of ID to NVT preferences. [#413](https://github.com/greenbone/gvmd/pull/413)
- Handle addition of ID to NVT preferences. [#413](https://github.com/greenbone/gvmd/pull/413) [#744](https://github.com/greenbone/gvmd/pull/744)
- Add setting 'OMP Slave Check Period' [#491](https://github.com/greenbone/gvmd/pull/491)
- Document switching between releases when using Postgres. [#563](https://github.com/greenbone/gvmd/pull/563)
- Cgreen based unit tests for gvmd has been added. [#579](https://github.com/greenbone/gvmd/pull/579)
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ include (CPack)

## Variables

set (GVMD_DATABASE_VERSION 219)
set (GVMD_DATABASE_VERSION 220)

set (GVMD_SCAP_DATABASE_VERSION 15)

Expand Down
121 changes: 121 additions & 0 deletions src/manage_migrators.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
#ifdef __FreeBSD__
#include <sys/wait.h>
#endif
#include "manage_migrators_219_to_220_names.h"
#include "manage_sql.h"
#include "sql.h"
#include "utils.h"
Expand Down Expand Up @@ -1361,6 +1362,125 @@ migrate_218_to_219 ()
return 0;
}

/**
* @brief Get new name of a preference.
*
* @param[in] old_name Old name of preference.
*
* @return Static string containing new name for preference if found, else NULL.
*/
static const gchar *
migrate_219_to_220_new_name (const char *old_name)
{
int index;

for (index = 0; migrate_219_to_220_names[index][0]; index++)
if (strcmp (migrate_219_to_220_names[index][0], old_name) == 0)
return migrate_219_to_220_names[index][1];
return NULL;
}

/**
* @brief Converts old NVT preferences to the new format.
*
* @param[in] table_name The name of the table to update.
*/
static void
replace_preference_names_219_to_220 (const char *table_name)
{
iterator_t preferences;

/* 1.3.6.1.4.1.25623.1.0.14259:checkbox:Log nmap output
* =>
* 1.3.6.1.4.1.25623.1.0.14259:21:checkbox:Log nmap output */

init_iterator (&preferences,
"SELECT id, name"
" FROM \"%s\""
" WHERE name LIKE '%%:%%:%%'"
" AND name !~ '.*:[0-9]+:.*:.*';",
table_name);

while (next (&preferences))
{
resource_t preference;
const char *old_name;
const gchar *new_name;

preference = iterator_int64 (&preferences, 0);
old_name = iterator_string (&preferences, 1);
new_name = migrate_219_to_220_new_name (old_name);
if (new_name)
{
gchar *quoted_new_name;

quoted_new_name = sql_quote (new_name);
sql ("UPDATE \"%s\" SET name = '%s' WHERE id = %llu;",
table_name,
quoted_new_name,
preference);
g_free (quoted_new_name);
}
else
g_warning ("%s: No new name for '%s'", __FUNCTION__, old_name);
}
cleanup_iterator (&preferences);
}

/**
* @brief Migrate the database from version 219 to version 220.
*
* @return 0 success, -1 error.
*/
int
migrate_219_to_220 ()
{
sql_begin_immediate ();

/* Ensure that the database is currently version 219. */

if (manage_db_version () != 219)
{
sql_rollback ();
return -1;
}

/* Update the database. */

/* OSP uses lowercase for timeout NVT preferences where OTP used a capital,
* so update those first:
*
* 1.3.6.1.4.1.25623.1.0.107305:entry:Timeout
* =>
* 1.3.6.1.4.1.25623.1.0.107305:entry:timeout */

sql ("UPDATE nvt_preferences"
" SET name = split_part (name, ':', 1) || ':entry:timeout'"
" WHERE name = split_part (name, ':', 1) || ':entry:Timeout';");

/* Then update config and NVT preferences from the 3 part format to the
* newer 4 part format:
*
* 1.3.6.1.4.1.25623.1.0.14259:checkbox:Log nmap output
* =>
* 1.3.6.1.4.1.25623.1.0.14259:21:checkbox:Log nmap output
*
* Any preferences that aren't in our hardcoded list will be updated after
* the first NVT sync. */

replace_preference_names_219_to_220 ("nvt_preferences");
replace_preference_names_219_to_220 ("config_preferences");
replace_preference_names_219_to_220 ("config_preferences_trash");

/* Set the database version to 220. */

set_db_version (220);

sql_commit ();

return 0;
}

#undef UPDATE_DASHBOARD_SETTINGS

/**
Expand All @@ -1386,6 +1506,7 @@ static migrator_t database_migrators[] = {
{217, migrate_216_to_217},
{218, migrate_217_to_218},
{219, migrate_218_to_219},
{220, migrate_219_to_220},
/* End marker. */
{-1, NULL}};

Expand Down
Loading