Skip to content

Commit

Permalink
Merge pull request #2328 from greenbone/fix-cleanup-sequences
Browse files Browse the repository at this point in the history
Fix: Avoid DB check inserts in cleanup-sequences
  • Loading branch information
a-h-abdelsalam authored Nov 29, 2024
2 parents 16ef4c4 + 6c4aa12 commit 2d79e64
Show file tree
Hide file tree
Showing 12 changed files with 112 additions and 54 deletions.
9 changes: 6 additions & 3 deletions src/manage.c
Original file line number Diff line number Diff line change
Expand Up @@ -978,7 +978,8 @@ int
manage_create_encryption_key (GSList *log_config,
const db_conn_info_t *database)
{
int ret = manage_option_setup (log_config, database);
int ret = manage_option_setup (log_config, database,
0 /* avoid_db_check_inserts */);
if (ret)
{
printf ("Error setting up log config or database connection.");
Expand Down Expand Up @@ -1042,7 +1043,8 @@ manage_set_encryption_key (GSList *log_config,
const db_conn_info_t *database,
const char *uid)
{
int ret = manage_option_setup (log_config, database);
int ret = manage_option_setup (log_config, database,
0 /* avoid_db_check_inserts */);
if (ret)
{
printf ("Error setting up log config or database connection.\n");
Expand Down Expand Up @@ -5709,7 +5711,8 @@ manage_rebuild_gvmd_data_from_feed (const char *types,
return -1;
}

ret = manage_option_setup (log_config, database);
ret = manage_option_setup (log_config, database,
0 /* avoid_db_check_inserts */);
if (ret)
{
if (error_msg)
Expand Down
2 changes: 1 addition & 1 deletion src/manage.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ init_manage (GSList*, const db_conn_info_t *, int, int, int, int,
manage_connection_forker_t, int);

int
init_manage_helper (GSList *, const db_conn_info_t *, int);
init_manage_helper (GSList *, const db_conn_info_t *, int, int);

void
init_manage_process (const db_conn_info_t*);
Expand Down
113 changes: 75 additions & 38 deletions src/manage_sql.c
Original file line number Diff line number Diff line change
Expand Up @@ -930,13 +930,15 @@ cert_check_time ()
*
* @param[in] log_config Log configuration.
* @param[in] database Database.
* @param[in] avoid_db_check_inserts Whether to avoid inserts in DB check.
*
* @return 0 success, -1 error, -2 database is too old,
* -3 database needs to be initialised from server,
* -5 database is too new.
*/
int
manage_option_setup (GSList *log_config, const db_conn_info_t *database)
manage_option_setup (GSList *log_config, const db_conn_info_t *database,
int avoid_db_check_inserts)
{
int ret;

Expand All @@ -947,7 +949,8 @@ manage_option_setup (GSList *log_config, const db_conn_info_t *database)
}

ret = init_manage_helper (log_config, database,
MANAGE_ABSOLUTE_MAX_IPS_PER_TARGET);
MANAGE_ABSOLUTE_MAX_IPS_PER_TARGET,
avoid_db_check_inserts);
assert (ret != -4);
switch (ret)
{
Expand Down Expand Up @@ -6159,10 +6162,9 @@ manage_cert_db_version ()
void
set_db_version (int version)
{
sql ("DELETE FROM %s.meta WHERE name = 'database_version';",
sql_schema ());
sql ("INSERT INTO %s.meta (name, value)"
" VALUES ('database_version', '%i');",
" VALUES ('database_version', '%i')"
" ON CONFLICT (name) DO UPDATE SET value = EXCLUDED.value;",
sql_schema (),
version);
}
Expand Down Expand Up @@ -6408,7 +6410,8 @@ manage_encrypt_all_credentials (GSList *log_config,

g_info (" (Re-)encrypting all credentials.");

ret = manage_option_setup (log_config, database);
ret = manage_option_setup (log_config, database,
0 /* avoid_db_check_inserts */);
if (ret)
return ret;

Expand Down Expand Up @@ -6449,7 +6452,8 @@ manage_decrypt_all_credentials (GSList *log_config,

g_info (" Decrypting all credentials.");

ret = manage_option_setup (log_config, database);
ret = manage_option_setup (log_config, database,
0 /* avoid_db_check_inserts */);
if (ret)
return ret;

Expand Down Expand Up @@ -6712,7 +6716,8 @@ manage_check_alerts (GSList *log_config, const db_conn_info_t *database)

g_info (" Checking alerts.");

ret = manage_option_setup (log_config, database);
ret = manage_option_setup (log_config, database,
0 /* avoid_db_check_inserts */);
if (ret)
return ret;

Expand Down Expand Up @@ -16680,11 +16685,11 @@ manage_migrate_relay_sensors ()
* Only called by init_manage_internal, and ultimately only by the main process.
*
* @param[in] check_encryption_key Whether to check encryption key.
*
* @param[in] avoid_db_check_inserts Whether to avoid inserts in DB check.
* @return 0 success, -1 error.
*/
static int
check_db (int check_encryption_key)
check_db (int check_encryption_key, int avoid_db_check_inserts)
{
/* The file locks managed at startup ensure that this is the only Manager
* process accessing the db. Nothing else should be accessing the db, access
Expand All @@ -16695,19 +16700,25 @@ check_db (int check_encryption_key)
create_tables ();
check_db_sequences ();
set_db_version (GVMD_DATABASE_VERSION);
check_db_roles ();
check_db_nvt_selectors ();
if (avoid_db_check_inserts == 0)
{
check_db_roles ();
check_db_nvt_selectors ();
}
check_db_nvts ();
check_db_port_lists ();
check_db_port_lists (avoid_db_check_inserts);
clean_auth_cache ();
if (check_db_scanners ())
if (avoid_db_check_inserts == 0 && check_db_scanners ())
goto fail;
if (check_db_report_formats ())
if (check_db_report_formats (avoid_db_check_inserts))
goto fail;
if (check_db_report_formats_trash ())
goto fail;
check_db_permissions ();
check_db_settings ();
if (avoid_db_check_inserts == 0)
{
check_db_permissions ();
check_db_settings ();
}
cleanup_schedule_times ();
if (check_encryption_key && check_db_encryption_key ())
goto fail;
Expand Down Expand Up @@ -16874,6 +16885,7 @@ cleanup_tables ()
* with GMP when an alert occurs.
* @param[in] skip_db_check Skip DB check.
* @param[in] check_encryption_key Check encryption key if doing DB check.
* @param[in] avoid_db_check_inserts Whether to avoid inserts in DB check.
*
* @return 0 success, -1 error, -2 database is too old,
* -4 max_ips_per_target out of range, -5 database is too new.
Expand All @@ -16888,7 +16900,8 @@ init_manage_internal (GSList *log_config,
int stop_tasks,
manage_connection_forker_t fork_connection,
int skip_db_check,
int check_encryption_key)
int check_encryption_key,
int avoid_db_check_inserts)
{
int ret;

Expand Down Expand Up @@ -16974,16 +16987,18 @@ init_manage_internal (GSList *log_config,
* 2 a helper processes (--create-user, --get-users, etc) when the
* main process is not running. */

ret = check_db (check_encryption_key);
ret = check_db (check_encryption_key, avoid_db_check_inserts);
if (ret)
return ret;

cleanup_tables ();

/* Set max_hosts in db, so database server side can access it. */

sql ("DELETE FROM meta WHERE name = 'max_hosts';");
sql ("INSERT INTO meta (name, value) VALUES ('max_hosts', %i);", max_hosts);
sql ("INSERT INTO meta (name, value)"
" VALUES ('max_hosts', %i)"
" ON CONFLICT (name) DO UPDATE SET value = EXCLUDED.value;",
max_hosts);
}

if (stop_tasks)
Expand All @@ -16997,7 +17012,7 @@ init_manage_internal (GSList *log_config,

if (skip_db_check == 0)
/* Requires NVT cache. */
check_db_configs ();
check_db_configs (avoid_db_check_inserts);

sql_close ();
gvmd_db_conn_info.name = database->name ? g_strdup (database->name) : NULL;
Expand Down Expand Up @@ -17051,7 +17066,8 @@ init_manage (GSList *log_config, const db_conn_info_t *database,
1, /* Stop active tasks. */
fork_connection,
skip_db_check,
1); /* Check encryption key if checking db. */
1, /* Check encryption key if checking db. */
0 /* Do not avoid inserts if checking db. */);
}

/**
Expand All @@ -17063,15 +17079,16 @@ init_manage (GSList *log_config, const db_conn_info_t *database,
*
* @param[in] log_config Log configuration.
* @param[in] database Location of database.
* @param[in] max_ips_per_target Max number of IPs per target.
* @param[in] max_ips_per_target Max number of IPs per target.
* @param[in] avoid_db_check_inserts Whether to avoid inserts in DB check.
*
* @return 0 success, -1 error, -2 database is too old, -3 database needs
* to be initialised from server, -4 max_ips_per_target out of range,
* -5 database is too new.
*/
int
init_manage_helper (GSList *log_config, const db_conn_info_t *database,
int max_ips_per_target)
int max_ips_per_target, int avoid_db_check_inserts)
{
return init_manage_internal (log_config,
database,
Expand All @@ -17088,7 +17105,8 @@ init_manage_helper (GSList *log_config, const db_conn_info_t *database,
lockfile_locked ("gvm-serving")
? 1 /* Skip DB check. */
: 0, /* Do DB check. */
0); /* Dummy. */
0, /* Dummy. */
avoid_db_check_inserts);
}

/**
Expand Down Expand Up @@ -40304,7 +40322,8 @@ manage_create_scanner (GSList *log_config, const db_conn_info_t *database,

g_info (" Creating scanner.");

ret = manage_option_setup (log_config, database);
ret = manage_option_setup (log_config, database,
0 /* avoid_db_check_inserts */);
if (ret)
return ret;

Expand Down Expand Up @@ -40493,7 +40512,8 @@ manage_delete_scanner (GSList *log_config, const db_conn_info_t *database,
return 3;
}

ret = manage_option_setup (log_config, database);
ret = manage_option_setup (log_config, database,
0 /* avoid_db_check_inserts */);
if (ret)
return ret;

Expand Down Expand Up @@ -40561,7 +40581,8 @@ manage_modify_scanner (GSList *log_config, const db_conn_info_t *database,

g_info (" Modifying scanner.");

ret = manage_option_setup (log_config, database);
ret = manage_option_setup (log_config, database,
0 /* avoid_db_check_inserts */);
if (ret)
return ret;

Expand Down Expand Up @@ -40780,7 +40801,8 @@ manage_verify_scanner (GSList *log_config, const db_conn_info_t *database,

g_info (" Verifying scanner.");

ret = manage_option_setup (log_config, database);
ret = manage_option_setup (log_config, database,
0 /* avoid_db_check_inserts */);
if (ret)
return ret;

Expand Down Expand Up @@ -42265,7 +42287,8 @@ manage_get_scanners (GSList *log_config, const db_conn_info_t *database)

g_info (" Getting scanners.");

ret = manage_option_setup (log_config, database);
ret = manage_option_setup (log_config, database,
0 /* avoid_db_check_inserts */);
if (ret)
return ret;

Expand Down Expand Up @@ -46204,7 +46227,8 @@ manage_get_roles (GSList *log_config, const db_conn_info_t *database,

g_info (" Getting roles.");

ret = manage_option_setup (log_config, database);
ret = manage_option_setup (log_config, database,
0 /* avoid_db_check_inserts */);
if (ret)
return ret;

Expand Down Expand Up @@ -52849,7 +52873,8 @@ manage_modify_setting (GSList *log_config, const db_conn_info_t *database,
return 3;
}

ret = manage_option_setup (log_config, database);
ret = manage_option_setup (log_config, database,
0 /* avoid_db_check_inserts */);
if (ret)
return ret;

Expand Down Expand Up @@ -53003,7 +53028,8 @@ manage_create_user (GSList *log_config, const db_conn_info_t *database,

g_info (" Creating user.");

ret = manage_option_setup (log_config, database);
ret = manage_option_setup (log_config, database,
0 /* avoid_db_check_inserts */);
if (ret)
return ret;

Expand Down Expand Up @@ -53093,7 +53119,8 @@ manage_delete_user (GSList *log_config, const db_conn_info_t *database,

g_info (" Deleting user.");

ret = manage_option_setup (log_config, database);
ret = manage_option_setup (log_config, database,
0 /* avoid_db_check_inserts */);
if (ret)
return ret;

Expand Down Expand Up @@ -53158,7 +53185,8 @@ manage_get_users (GSList *log_config, const db_conn_info_t *database,

g_info (" Getting users.");

ret = manage_option_setup (log_config, database);
ret = manage_option_setup (log_config, database,
0 /* avoid_db_check_inserts */);
if (ret)
return ret;

Expand Down Expand Up @@ -53262,7 +53290,8 @@ manage_set_password (GSList *log_config, const db_conn_info_t *database,
return -1;
}

ret = manage_option_setup (log_config, database);
ret = manage_option_setup (log_config, database,
0 /* avoid_db_check_inserts */);
if (ret)
return ret;

Expand Down Expand Up @@ -58273,7 +58302,15 @@ manage_optimize (GSList *log_config, const db_conn_info_t *database,
return 1;
}

ret = manage_option_setup (log_config, database);
int avoid_db_check_inserts = 0;
/* The optimize=cleanup-sequences option may be used if a sequence has
* already reached its maximum value, so avoid any inserts that may cause
* a sequence maximum error. *
*/
if (strcasecmp (name, "cleanup-sequences") == 0)
avoid_db_check_inserts = 1;

ret = manage_option_setup (log_config, database, avoid_db_check_inserts);
if (ret)
return ret;

Expand Down
2 changes: 1 addition & 1 deletion src/manage_sql.h
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ void
check_alerts ();

int
manage_option_setup (GSList *, const db_conn_info_t *);
manage_option_setup (GSList *, const db_conn_info_t *, int);

void
manage_option_cleanup ();
Expand Down
7 changes: 6 additions & 1 deletion src/manage_sql_configs.c
Original file line number Diff line number Diff line change
Expand Up @@ -4532,12 +4532,17 @@ update_config (config_t config, const gchar *name,

/**
* @brief Check configs, for startup.
*
* @param[in] avoid_db_check_inserts Whether to avoid inserts.
*/
void
check_db_configs ()
check_db_configs (int avoid_db_check_inserts)
{
migrate_predefined_configs ();

if (avoid_db_check_inserts)
return;

if (sync_configs_with_feed (FALSE) <= -1)
g_warning ("%s: Failed to sync configs with feed", __func__);

Expand Down
Loading

0 comments on commit 2d79e64

Please sign in to comment.