diff --git a/src/manage_sql_secinfo.c b/src/manage_sql_secinfo.c index 13d54605d..0a2d90cc7 100644 --- a/src/manage_sql_secinfo.c +++ b/src/manage_sql_secinfo.c @@ -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) @@ -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. */ @@ -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"); } /** @@ -3524,7 +3584,8 @@ try_load_csv () return -1; } - return update_scap_end (); + update_scap_end (); + return 0; } return 1; } @@ -3623,7 +3684,7 @@ update_scap (gboolean reset_scap_db) if (update_scap_cpes () == -1) { - update_scap_timestamp (); + abort_scap_update (); return -1; } @@ -3632,7 +3693,7 @@ update_scap (gboolean reset_scap_db) if (update_scap_cves () == -1) { - update_scap_timestamp (); + abort_scap_update (); return -1; } @@ -3651,7 +3712,8 @@ update_scap (gboolean reset_scap_db) update_scap_placeholders (); - return update_scap_end (); + update_scap_end (); + return 0; } /**