Skip to content

Commit

Permalink
Merge pull request #2145 from greenbone/GEA-419_gvmd_keeps_parsing_co…
Browse files Browse the repository at this point in the history
…rrupt_cpe_dictionary

Fix: Handle value of last_update and SCAP schema when feed update fails.
  • Loading branch information
a-h-abdelsalam authored Mar 7, 2024
2 parents 56eaac9 + 55bda1c commit ddd1430
Showing 1 changed file with 94 additions and 32 deletions.
126 changes: 94 additions & 32 deletions src/manage_sql_secinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -3389,38 +3389,15 @@ update_scap_placeholders ()
" WHERE cpe=cpes.id))"
" WHERE cpes.title IS NULL;");
}

/**
* @brief Finish scap update.
*
* @return 0 success, -1 error.
* @brief Update CERT data that depends on SCAP.
*/
static int
update_scap_end ()
static void
update_cert_data ()
{
int cert_db_version;

g_debug ("%s: update timestamp", __func__);

update_scap_timestamp ();

/* Replace the real scap schema with the new one. */

if (sql_int ("SELECT EXISTS (SELECT schema_name FROM"
" information_schema.schemata"
" WHERE schema_name = 'scap');"))
{
sql ("ALTER SCHEMA scap RENAME TO scap3;");
sql ("ALTER SCHEMA scap2 RENAME TO scap;");
sql ("DROP SCHEMA scap3 CASCADE;");
/* View 'vulns' contains references into the SCAP schema, so it is
* removed by the CASCADE. */
create_view_vulns ();
}
else
sql ("ALTER SCHEMA scap2 RENAME TO scap;");

/* Update CERT data that depends on SCAP. */
cert_db_version = manage_cert_db_version();

if (cert_db_version == -1)
Expand Down Expand Up @@ -3452,6 +3429,37 @@ update_scap_end ()
update_cvss_dfn_cert (1, last_cert_update, last_scap_update);
update_cvss_cert_bund (1, last_cert_update, last_scap_update);
}
}

/**
* @brief Finish scap update.
*/
static void
update_scap_end ()
{
g_debug ("%s: update timestamp", __func__);

update_scap_timestamp ();

/* Replace the real scap schema with the new one. */

if (sql_int ("SELECT EXISTS (SELECT schema_name FROM"
" information_schema.schemata"
" WHERE schema_name = 'scap');"))
{
sql ("ALTER SCHEMA scap RENAME TO scap3;");
sql ("ALTER SCHEMA scap2 RENAME TO scap;");
sql ("DROP SCHEMA scap3 CASCADE;");
/* View 'vulns' contains references into the SCAP schema, so it is
* removed by the CASCADE. */
create_view_vulns ();
}
else
sql ("ALTER SCHEMA scap2 RENAME TO scap;");

/* Update CERT data that depends on SCAP. */

update_cert_data ();

/* Analyze. */

Expand All @@ -3461,8 +3469,60 @@ update_scap_end ()

g_info ("%s: Updating SCAP info succeeded", __func__);
setproctitle ("Syncing SCAP: done");
}

return 0;
/**
* @brief Abort scap update.
*/
static void
abort_scap_update ()
{
g_debug ("%s: update timestamp", __func__);

if (sql_int ("SELECT EXISTS (SELECT schema_name FROM"
" information_schema.schemata"
" WHERE schema_name = 'scap');"))
{
update_scap_timestamp ();
sql ("UPDATE scap.meta SET value = "
" (SELECT value from scap2.meta WHERE name = 'last_update')"
" WHERE name = 'last_update';");
sql ("DROP SCHEMA scap2 CASCADE;");
/* View 'vulns' contains references into the SCAP schema, so it is
* removed by the CASCADE. */
create_view_vulns ();
/* Update CERT data that depends on SCAP. */
update_cert_data ();
}
else
{
/* reset scap2 schema */
if (manage_db_init ("scap"))
{
g_warning ("%s: could not reset scap2 schema, db init failed", __func__);
}
else if (manage_db_init_indexes ("scap"))
{
g_warning ("%s: could not reset scap2 schema, init indexes failed", __func__);
}
else if (manage_db_add_constraints ("scap"))
{
g_warning ("%s: could not reset scap2 schema, add constrains failed", __func__);
}

if (sql_int ("SELECT EXISTS (SELECT schema_name FROM"
" information_schema.schemata"
" WHERE schema_name = 'scap2');"))
{
update_scap_timestamp ();
sql ("ALTER SCHEMA scap2 RENAME TO scap;");
/* Update CERT data that depends on SCAP. */
update_cert_data ();
}
}

g_info ("%s: Updating SCAP data aborted", __func__);
setproctitle ("Syncing SCAP: aborted");
}

/**
Expand Down Expand Up @@ -3524,7 +3584,8 @@ try_load_csv ()
return -1;
}

return update_scap_end ();
update_scap_end ();
return 0;
}
return 1;
}
Expand Down Expand Up @@ -3623,7 +3684,7 @@ update_scap (gboolean reset_scap_db)

if (update_scap_cpes () == -1)
{
update_scap_timestamp ();
abort_scap_update ();
return -1;
}

Expand All @@ -3632,7 +3693,7 @@ update_scap (gboolean reset_scap_db)

if (update_scap_cves () == -1)
{
update_scap_timestamp ();
abort_scap_update ();
return -1;
}

Expand All @@ -3651,7 +3712,8 @@ update_scap (gboolean reset_scap_db)

update_scap_placeholders ();

return update_scap_end ();
update_scap_end ();
return 0;
}

/**
Expand Down

0 comments on commit ddd1430

Please sign in to comment.