Skip to content

Commit

Permalink
Merge pull request #2150 from greenbone/fix-modify-config-preference
Browse files Browse the repository at this point in the history
Fix: Fix modify config preference, set pref_... fields in cleanup-config-prefs
  • Loading branch information
a-h-abdelsalam authored Mar 11, 2024
2 parents 3e37dd7 + 097a880 commit 54a0645
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 13 deletions.
27 changes: 20 additions & 7 deletions src/manage_sql.c
Original file line number Diff line number Diff line change
Expand Up @@ -58340,23 +58340,36 @@ manage_optimize (GSList *log_config, const db_conn_info_t *database,
}
else if (strcasecmp (name, "cleanup-config-prefs") == 0)
{
int removed, fixed_values;
sql ("DELETE FROM config_preferences WHERE id NOT IN"
" (SELECT min(id) FROM config_preferences"
" GROUP BY config, type, name, value);");
removed = sql_changes();

sql ("UPDATE config_preferences"
" SET value = (SELECT value FROM nvt_preferences"
" WHERE name='scanner_plugins_timeout')"
" WHERE name = 'scanner_plugins_timeout'"
" AND value = 'SCANNER_NVT_TIMEOUT';");
fixed_values = sql_changes();

success_text = g_strdup_printf ("Optimized: cleanup-config-prefs."
" Duplicate config preferences removed:"
" %d. Corrected preference values: %d",
removed, fixed_values);
sql ("UPDATE config_preferences"
" SET pref_nvt = NULL,"
" pref_id = NULL,"
" pref_type = NULL,"
" pref_name = NULL"
" WHERE type = 'SERVER_PREFS' AND pref_nvt IS NOT NULL;");

sql ("UPDATE config_preferences"
" SET pref_nvt = substring (name, '^([^:]*)'),"
" pref_id = CAST(substring (name, '^[^:]*:([0-9]+)') AS integer),"
" pref_type = substring (name, '^[^:]*:[0-9]+:([^:]*):'),"
" pref_name = substring (name, '^[^:]*:[0-9]+:[^:]*:(.*)')"
" WHERE type = 'PLUGINS_PREFS'"
" AND (pref_nvt = '(null)' OR pref_nvt IS NULL"
" OR pref_type = '(null)' OR pref_type IS NULL"
" OR pref_name = '(null)' OR pref_name IS NULL)"
" AND name ~ '^[^:]*:[0-9]+:[^:]*:.*'"
" AND type = 'PLUGINS_PREFS';");

success_text = g_strdup_printf ("Optimized: cleanup-config-prefs.");
}
else if (strcasecmp (name, "cleanup-feed-permissions") == 0)
{
Expand Down
32 changes: 26 additions & 6 deletions src/manage_sql_configs.c
Original file line number Diff line number Diff line change
Expand Up @@ -3627,6 +3627,13 @@ modify_config_preference (config_t config, const char* nvt,
g_free (quoted_name);
quoted_name = sql_quote (splits[3]);
}
else
{
quoted_pref_nvt = sql_quote (splits[0]);
pref_id = atoi (splits[1]);
quoted_pref_type = sql_quote (splits[2]);
quoted_pref_name = sql_quote (splits[3]);
}
}
g_strfreev (splits);

Expand All @@ -3639,12 +3646,25 @@ modify_config_preference (config_t config, const char* nvt,
config,
nvt ? "= 'PLUGINS_PREFS'" : "= 'SERVER_PREFS'",
quoted_name);
sql ("INSERT INTO config_preferences"
" (config, type, name, value, pref_nvt, pref_id, pref_type, pref_name)"
" VALUES (%llu, %s, '%s', '%s', '%s', %i, '%s', '%s');",
config, nvt ? "'PLUGINS_PREFS'" : "'SERVER_PREFS'", quoted_name,
quoted_value, quoted_pref_nvt, pref_id, quoted_pref_type,
quoted_pref_name);
if (nvt)
{
sql ("INSERT INTO config_preferences"
" (config, type, name, value,"
" pref_nvt, pref_id, pref_type, pref_name)"
" VALUES (%llu, 'PLUGINS_PREFS', '%s', '%s',"
" '%s', %i, '%s', '%s');",
config, quoted_name, quoted_value,
quoted_pref_nvt, pref_id, quoted_pref_type, quoted_pref_name);
}
else
{
sql ("INSERT INTO config_preferences"
" (config, type, name, value,"
" pref_nvt, pref_id, pref_type, pref_name)"
" VALUES (%llu, 'SERVER_PREFS', '%s', '%s',"
" NULL, NULL, NULL, NULL);",
config, quoted_name, quoted_value);
}

g_free (quoted_value);
g_free (quoted_name);
Expand Down

0 comments on commit 54a0645

Please sign in to comment.