From 9bde214e6a344484f2996da6a88862a166f666a7 Mon Sep 17 00:00:00 2001 From: Timo Pollmeier Date: Tue, 31 Oct 2023 09:43:08 +0100 Subject: [PATCH 1/2] Add: Feed data objects can now be deprecated The data objects in the feed (configs, port lists, report formats) can now be deprecated, which prevents them from being imported unless they already exist in the database. Existing data objects are will be marked with a new XML element to indicate they are deprecated. This will be used to deprecate feed data objects that are no longer maintained. --- src/gmp.c | 18 +++++++++ src/gmp_configs.c | 14 ++++++- src/gmp_configs.h | 2 +- src/gmp_port_lists.c | 14 ++++++- src/gmp_port_lists.h | 3 +- src/gmp_report_formats.c | 8 +++- src/gmp_report_formats.h | 2 +- src/manage.h | 6 +++ src/manage_configs.c | 39 +++++++++++++++--- src/manage_pg.c | 7 ++++ src/manage_port_lists.c | 40 +++++++++++++++--- src/manage_report_formats.c | 37 +++++++++++++++-- src/manage_sql.c | 57 ++++++++++++++++++++++++++ src/manage_sql_configs.c | 67 +++++++++++++++++++++++++++++-- src/manage_sql_configs.h | 5 ++- src/manage_sql_port_lists.c | 66 +++++++++++++++++++++++++++++- src/manage_sql_port_lists.h | 6 ++- src/manage_sql_report_formats.c | 65 ++++++++++++++++++++++++++++-- src/manage_sql_report_formats.h | 5 ++- src/schema_formats/XML/GMP.xml.in | 28 ++++++++++--- 20 files changed, 451 insertions(+), 38 deletions(-) diff --git a/src/gmp.c b/src/gmp.c index 8f548a4d5..b1a6d32b8 100644 --- a/src/gmp.c +++ b/src/gmp.c @@ -11773,6 +11773,12 @@ handle_get_configs (gmp_parser_t *gmp_parser, GError **error) } SEND_GET_COMMON (config, &get_configs_data->get, &configs); + if (resource_id_deprecated ("config", + get_iterator_uuid (&configs))) + { + SENDF_TO_CLIENT_OR_FAIL ("1"); + } + /** @todo This should really be an nvt_selector_t. */ selector = config_iterator_nvt_selector (&configs); config = get_iterator_resource (&configs); @@ -14046,6 +14052,12 @@ handle_get_port_lists (gmp_parser_t *gmp_parser, GError **error) SEND_GET_COMMON (port_list, &get_port_lists_data->get, &port_lists); + if (resource_id_deprecated ("port_list", + get_iterator_uuid (&port_lists))) + { + SENDF_TO_CLIENT_OR_FAIL ("1"); + } + SENDF_TO_CLIENT_OR_FAIL ("" "%i" "%i" @@ -14926,6 +14938,12 @@ handle_get_report_formats (gmp_parser_t *gmp_parser, GError **error) : report_format_predefined (get_iterator_resource (&report_formats))); + if (resource_id_deprecated ("report_format", + get_iterator_uuid (&report_formats))) + { + SENDF_TO_CLIENT_OR_FAIL ("1"); + } + if (get_report_formats_data->alerts) { iterator_t alerts; diff --git a/src/gmp_configs.c b/src/gmp_configs.c index 3daa9eada..f7d550dd4 100644 --- a/src/gmp_configs.c +++ b/src/gmp_configs.c @@ -182,6 +182,7 @@ attr_or_null (entity_t entity, const gchar *name) * @param[out] all_selector True if ALL_SELECTOR was present. * @param[out] import_nvt_selectors Address for selectors. * @param[out] import_preferences Address for preferences. + * @param[out] deprecated Address for deprecation status. * * @return 0 success, 1 preference did no exist, -1 preference without ID. */ @@ -190,7 +191,8 @@ parse_config_entity (entity_t config, const char **config_id, char **name, char **comment, char **usage_type, int *all_selector, array_t **import_nvt_selectors, - array_t **import_preferences) + array_t **import_preferences, + char **deprecated) { entity_t entity, preferences, nvt_selectors; @@ -217,6 +219,14 @@ parse_config_entity (entity_t config, const char **config_id, char **name, *usage_type = NULL; } + if (deprecated) + { + *deprecated = NULL; + entity = entity_child (config, "deprecated"); + if (entity) + *deprecated = entity_text (entity); + } + /* Collect NVT selectors. */ *import_nvt_selectors = NULL; @@ -416,7 +426,7 @@ create_config_run (gmp_parser_t *gmp_parser, GError **error) if (parse_config_entity (config, NULL, &import_name, &comment, NULL, &all_selector, &import_nvt_selectors, - &import_preferences)) + &import_preferences, NULL)) { SEND_TO_CLIENT_OR_FAIL (XML_ERROR_SYNTAX ("create_config", diff --git a/src/gmp_configs.h b/src/gmp_configs.h index e126ec3ba..210621e15 100644 --- a/src/gmp_configs.h +++ b/src/gmp_configs.h @@ -41,7 +41,7 @@ create_config_element_text (const gchar *, gsize); int parse_config_entity (entity_t, const char **, char **, char **, - char **, int *, array_t **, array_t **); + char **, int *, array_t **, array_t **, char **); /* modify_config */ diff --git a/src/gmp_port_lists.c b/src/gmp_port_lists.c index d5fd98f03..a89b2cd30 100644 --- a/src/gmp_port_lists.c +++ b/src/gmp_port_lists.c @@ -109,10 +109,12 @@ create_port_list_element_start (gmp_parser_t *gmp_parser, const gchar *name, * @param[out] name Address for name. * @param[out] comment Address for comment. * @param[out] ranges Address for port ranges. + * @param[out] deprecated Address for deprecation status. */ void parse_port_list_entity (entity_t port_list, const char **port_list_id, - char **name, char **comment, array_t **ranges) + char **name, char **comment, array_t **ranges, + char **deprecated) { entity_t entity, port_ranges; @@ -129,6 +131,14 @@ parse_port_list_entity (entity_t port_list, const char **port_list_id, if (entity) *comment = entity_text (entity); + if (deprecated) + { + *deprecated = NULL; + entity = entity_child (port_list, "deprecated"); + if (entity) + *deprecated = entity_text (entity); + } + /* Collect port ranges. */ *ranges = NULL; @@ -207,7 +217,7 @@ create_port_list_run (gmp_parser_t *gmp_parser, GError **error) /* Get the port_list data from the XML. */ parse_port_list_entity (port_list, &port_list_id, &import_name, - &comment, &ranges); + &comment, &ranges, NULL); /* Check data, then create port list. */ diff --git a/src/gmp_port_lists.h b/src/gmp_port_lists.h index 1de86925c..67cd2ab6f 100644 --- a/src/gmp_port_lists.h +++ b/src/gmp_port_lists.h @@ -38,6 +38,7 @@ void create_port_list_element_text (const gchar *, gsize); void -parse_port_list_entity (entity_t, const char **, char **, char **, array_t **); +parse_port_list_entity (entity_t, const char **, char **, char **, array_t **, + char **); #endif /* not _GVMD_GMP_PORT_LISTS_H */ diff --git a/src/gmp_report_formats.c b/src/gmp_report_formats.c index 0302f5db7..37ccdce1d 100644 --- a/src/gmp_report_formats.c +++ b/src/gmp_report_formats.c @@ -156,6 +156,7 @@ params_options_free (array_t *params_options) * @param[out] files Address for files. * @param[out] params Address for params. * @param[out] params_options Address for param options. + * @param[out] deprecated Address for deprecation status. */ void parse_report_format_entity (entity_t report_format, @@ -163,7 +164,8 @@ parse_report_format_entity (entity_t report_format, char **content_type, char **extension, char **summary, char **description, char **signature, array_t **files, - array_t **params, array_t **params_options) + array_t **params, array_t **params_options, + char **deprecated) { entity_t file, param_entity; entities_t children; @@ -177,6 +179,8 @@ parse_report_format_entity (entity_t report_format, *summary = child_or_null (report_format, "summary"); *description = child_or_null (report_format, "description"); *signature = child_or_null (report_format, "signature"); + if (deprecated) + *deprecated = child_or_null (report_format, "deprecated"); *files = make_array (); *params = make_array (); @@ -367,7 +371,7 @@ create_report_format_run (gmp_parser_t *gmp_parser, GError **error) parse_report_format_entity (report_format, &report_format_id, &import_name, &content_type, &extension, &summary, &description, &signature, &files, - ¶ms, ¶ms_options); + ¶ms, ¶ms_options, NULL); /* Check data, then create report format. */ diff --git a/src/gmp_report_formats.h b/src/gmp_report_formats.h index ee462d3ed..8f732904a 100644 --- a/src/gmp_report_formats.h +++ b/src/gmp_report_formats.h @@ -43,6 +43,6 @@ params_options_free (array_t *); void parse_report_format_entity (entity_t, const char **, char **, char **, char **, char **, char **, char **, - array_t **, array_t **, array_t **); + array_t **, array_t **, array_t **, char **); #endif /* not _GVMD_GMP_REPORT_FORMATS_H */ diff --git a/src/manage.h b/src/manage.h index 0e5399067..a4936ea7c 100644 --- a/src/manage.h +++ b/src/manage.h @@ -407,6 +407,12 @@ type_is_scap (const char*); int delete_resource (const char *, const char *, int); +int +resource_id_deprecated (const char *, const char *); + +void +set_resource_id_deprecated (const char *, const char *, gboolean); + /* Events and Alerts. */ diff --git a/src/manage_configs.c b/src/manage_configs.c index 9dd0fa214..02cae9e8a 100644 --- a/src/manage_configs.c +++ b/src/manage_configs.c @@ -141,7 +141,7 @@ update_config_from_file (config_t config, const gchar *path) { entity_t entity; array_t *nvt_selectors, *preferences; - char *comment, *name, *usage_type; + char *comment, *name, *usage_type, *deprecated; const char *config_id; int all_selector; @@ -156,7 +156,7 @@ update_config_from_file (config_t config, const gchar *path) switch (parse_config_entity (entity, &config_id, &name, &comment, &usage_type, &all_selector, &nvt_selectors, - &preferences)) + &preferences, &deprecated)) { case 0: break; @@ -174,7 +174,7 @@ update_config_from_file (config_t config, const gchar *path) /* Update the config. */ update_config (config, name, comment, usage_type, all_selector, - nvt_selectors, preferences); + nvt_selectors, preferences, deprecated); /* Cleanup. */ @@ -197,7 +197,7 @@ create_config_from_file (const gchar *path) { entity_t config; array_t *nvt_selectors, *preferences; - char *created_name, *comment, *name, *usage_type; + char *created_name, *comment, *name, *usage_type, *deprecated; const char *config_id; config_t new_config; int all_selector; @@ -213,7 +213,7 @@ create_config_from_file (const gchar *path) switch (parse_config_entity (config, &config_id, &name, &comment, &usage_type, &all_selector, &nvt_selectors, - &preferences)) + &preferences, &deprecated)) { case 0: break; @@ -228,6 +228,16 @@ create_config_from_file (const gchar *path) return -1; } + /* Handle deprecation status */ + + if (deprecated && atoi (deprecated)) + { + g_debug ("Skipping import of deprecated config %s.", + config_id); + set_resource_id_deprecated ("config", config_id, TRUE); + return 0; + } + /* Create the config. */ switch (create_config_no_acl (config_id, @@ -329,6 +339,24 @@ should_sync_config_from_path (const char *path, gboolean rebuild, uuid = g_strdup_printf ("%s-%s-%s-%s-%s", split[1], split[2], split[3], split[4], split[5]); g_strfreev (split); + + if (resource_id_deprecated ("config", uuid)) + { + find_config_no_acl (uuid, config); + + if (rebuild) + return 1; + + full_path = g_build_filename (feed_dir_configs (), path, NULL); + if (deprecated_config_id_updated_in_feed (uuid, full_path)) + { + g_free (full_path); + return 1; + } + g_free (full_path); + return 0; + } + if (find_config_no_acl (uuid, config) == 0 && *config) { @@ -343,6 +371,7 @@ should_sync_config_from_path (const char *path, gboolean rebuild, if (config_updated_in_feed (*config, full_path)) { + g_free (full_path); return 1; } diff --git a/src/manage_pg.c b/src/manage_pg.c index b39368f7f..df9741fed 100644 --- a/src/manage_pg.c +++ b/src/manage_pg.c @@ -1993,6 +1993,13 @@ create_tables () " type TEXT," " value TEXT);"); + sql ("CREATE TABLE IF NOT EXISTS deprecated_feed_data" + " (id SERIAL PRIMARY KEY," + " type TEXT," + " uuid TEXT," + " modification_time INTEGER," + " UNIQUE (type, uuid));"); + sql ("CREATE TABLE IF NOT EXISTS filters" " (id SERIAL PRIMARY KEY," " uuid text UNIQUE NOT NULL," diff --git a/src/manage_port_lists.c b/src/manage_port_lists.c index 82590b949..843238929 100644 --- a/src/manage_port_lists.c +++ b/src/manage_port_lists.c @@ -115,7 +115,7 @@ create_port_list_from_file (const gchar *path) { entity_t port_list; array_t *ranges; - char *comment, *name; + char *comment, *name, *deprecated; const char *port_list_id; port_list_t new_port_list; @@ -129,7 +129,17 @@ create_port_list_from_file (const gchar *path) /* Parse the data out of the entity. */ parse_port_list_entity (port_list, &port_list_id, &name, &comment, - &ranges); + &ranges, &deprecated); + + /* Handle deprecation status */ + + if (deprecated && atoi (deprecated)) + { + g_debug ("Skipping import of deprecated port list %s.", + port_list_id); + set_resource_id_deprecated ("port_list", port_list_id, TRUE); + return 0; + } /* Create the port_list. */ @@ -207,7 +217,7 @@ update_port_list_from_file (port_list_t port_list, const gchar *path) { entity_t entity; array_t *ranges; - char *comment, *name; + char *comment, *name, *deprecated; const char *port_list_id; g_debug ("%s: updating %s", __func__, path); @@ -219,11 +229,12 @@ update_port_list_from_file (port_list_t port_list, const gchar *path) /* Parse the data out of the entity. */ - parse_port_list_entity (entity, &port_list_id, &name, &comment, &ranges); + parse_port_list_entity (entity, &port_list_id, &name, &comment, &ranges, + &deprecated); /* Update the port list. */ - update_port_list (port_list, name, comment, ranges); + update_port_list (port_list, name, comment, ranges, deprecated); /* Cleanup. */ @@ -265,6 +276,24 @@ should_sync_port_list_from_path (const char *path, gboolean rebuild, uuid = g_strdup_printf ("%s-%s-%s-%s-%s", split[1], split[2], split[3], split[4], split[5]); g_strfreev (split); + + if (resource_id_deprecated ("port_list", uuid)) + { + find_port_list_no_acl (uuid, port_list); + + if (rebuild) + return 1; + + full_path = g_build_filename (feed_dir_port_lists (), path, NULL); + if (deprecated_port_list_id_updated_in_feed (uuid, full_path)) + { + g_free (full_path); + return 1; + } + g_free (full_path); + return 0; + } + if (find_port_list_no_acl (uuid, port_list) == 0 && *port_list) { @@ -279,6 +308,7 @@ should_sync_port_list_from_path (const char *path, gboolean rebuild, if (port_list_updated_in_feed (*port_list, full_path)) { + g_free (full_path); return 1; } diff --git a/src/manage_report_formats.c b/src/manage_report_formats.c index fad78ce4b..4da844980 100644 --- a/src/manage_report_formats.c +++ b/src/manage_report_formats.c @@ -391,6 +391,7 @@ update_report_format_from_file (report_format_t report_format, entity_t entity; array_t *files, *params, *params_options; char *name, *content_type, *extension, *summary, *description, *signature; + char *deprecated; const char *report_format_id; g_debug ("%s: updating %s", __func__, path); @@ -405,13 +406,13 @@ update_report_format_from_file (report_format_t report_format, parse_report_format_entity (entity, &report_format_id, &name, &content_type, &extension, &summary, &description, &signature, &files, ¶ms, - ¶ms_options); + ¶ms_options, &deprecated); /* Update the report format. */ update_report_format (report_format, report_format_id, name, content_type, extension, summary, description, signature, files, - params, params_options); + params, params_options, deprecated); /* Cleanup. */ @@ -482,6 +483,7 @@ create_report_format_from_file (const gchar *path) entity_t report_format; array_t *files, *params, *params_options; char *name, *content_type, *extension, *summary, *description, *signature; + char *deprecated; const char *report_format_id; report_format_t new_report_format; @@ -497,7 +499,17 @@ create_report_format_from_file (const gchar *path) parse_report_format_entity (report_format, &report_format_id, &name, &content_type, &extension, &summary, &description, &signature, &files, ¶ms, - ¶ms_options); + ¶ms_options, &deprecated); + + /* Handle deprecation status */ + + if (deprecated && atoi (deprecated)) + { + g_debug ("Skipping import of deprecated report format %s.", + report_format_id); + set_resource_id_deprecated ("report_format", report_format_id, TRUE); + return 0; + } /* Create the report format. */ @@ -625,6 +637,24 @@ should_sync_report_format_from_path (const char *path, uuid = g_strdup_printf ("%s-%s-%s-%s-%s", split[1], split[2], split[3], split[4], split[5]); g_strfreev (split); + + if (resource_id_deprecated ("report_format", uuid)) + { + find_report_format_no_acl (uuid, report_format); + + if (rebuild) + return 1; + + full_path = g_build_filename (feed_dir_report_formats (), path, NULL); + if (deprecated_report_format_id_updated_in_feed (uuid, full_path)) + { + g_free (full_path); + return 1; + } + g_free (full_path); + return 0; + } + if (find_report_format_no_acl (uuid, report_format) == 0 && *report_format) { @@ -639,6 +669,7 @@ should_sync_report_format_from_path (const char *path, if (report_format_updated_in_feed (*report_format, full_path)) { + g_free (full_path); return 1; } diff --git a/src/manage_sql.c b/src/manage_sql.c index a3cda0615..9f6ff27d1 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -4748,6 +4748,63 @@ manage_trash_resource_name (const char *type, const char *uuid, char **name) return resource_name (type, uuid, LOCATION_TRASH, name); } +/** + * @brief Check if a resource has been marked as deprecated. + * + * @param[in] type Resource type. + * @param[in] resource_id UUID of the resource. + * + * @return 1 if deprecated, else 0. + */ +int +resource_id_deprecated (const char *type, const char *resource_id) +{ + int ret; + gchar *quoted_type = sql_quote (type); + gchar *quoted_uuid = sql_quote (resource_id); + + ret = sql_int ("SELECT count(*) FROM deprecated_feed_data" + " WHERE type = '%s' AND uuid = '%s';", + quoted_type, quoted_uuid); + + g_free (quoted_type); + g_free (quoted_uuid); + + return ret != 0; +} + +/** + * @brief Mark whether resource as deprecated. + * + * @param[in] type Resource type. + * @param[in] resource_id UUID of the resource. + * @param[in] deprecated Whether the resource is deprecated. + */ +void +set_resource_id_deprecated (const char *type, const char *resource_id, + gboolean deprecated) +{ + gchar *quoted_type = sql_quote (type); + gchar *quoted_uuid = sql_quote (resource_id); + + if (deprecated) + { + sql ("INSERT INTO deprecated_feed_data (type, uuid, modification_time)" + " VALUES ('%s', '%s', m_now ())" + " ON CONFLICT (uuid, type)" + " DO UPDATE SET modification_time = m_now ()", + quoted_type, quoted_uuid); + } + else + { + sql ("DELETE FROM deprecated_feed_data" + " WHERE type = '%s' AND uuid = '%s'", + quoted_type, quoted_uuid); + } + g_free (quoted_type); + g_free (quoted_uuid); +} + /** * @brief Get the UUID of a resource. * diff --git a/src/manage_sql_configs.c b/src/manage_sql_configs.c index 4c87780f9..4fe5b0814 100644 --- a/src/manage_sql_configs.c +++ b/src/manage_sql_configs.c @@ -4322,8 +4322,8 @@ migrate_predefined_configs () /** * @brief Check if a config has been updated in the feed. * - * @param[in] path Full path to config XML in feed. * @param[in] config Config. + * @param[in] path Full path to config XML in feed. * * @return 1 if updated in feed, else 0. */ @@ -4351,6 +4351,42 @@ config_updated_in_feed (config_t config, const gchar *path) return 1; } +/** + * @brief Check if a deprecated config has been updated in the feed. + * + * @param[in] config_id Config UUID. + * @param[in] path Full path to Config XML in feed. + * + * @return 1 if updated in feed, else 0. + */ +int +deprecated_config_id_updated_in_feed (const char *config_id, + const gchar *path) +{ + gchar *quoted_uuid; + GStatBuf state; + int last_update; + + quoted_uuid = sql_quote (config_id); + last_update = sql_int ("SELECT modification_time FROM deprecated_feed_data" + " WHERE type = 'config' AND uuid = '%s';", + quoted_uuid); + g_free (quoted_uuid); + + if (g_stat (path, &state)) + { + g_warning ("%s: Failed to stat feed config file: %s", + __func__, + strerror (errno)); + return 0; + } + + if (state.st_mtime <= last_update) + return 0; + + return 1; +} + /** * @brief Update a config from an XML file. * @@ -4361,15 +4397,17 @@ config_updated_in_feed (config_t config, const gchar *path) * @param[in] all_selector Whether to use "all" selector instead of selectors. * @param[in] selectors New NVT selectors. * @param[in] preferences New preferences. + * @param[in] deprecated Deprecation status. */ void update_config (config_t config, const gchar *name, const gchar *comment, const gchar *usage_type, int all_selector, const array_t* selectors /* nvt_selector_t. */, - const array_t* preferences /* preference_t. */) + const array_t* preferences /* preference_t. */, + const gchar *deprecated) { - gchar *quoted_name, *quoted_comment, *actual_usage_type; + gchar *quoted_name, *quoted_comment, *actual_usage_type, *config_id; sql_begin_immediate (); @@ -4451,6 +4489,29 @@ update_config (config_t config, const gchar *name, " AND type = 'SERVER_PREFS');", config); + /* Handle deprecation status */ + + config_id = resource_uuid ("config", config); + if (deprecated && atoi (deprecated)) + { + if (resource_id_deprecated ("config", config_id) == 0) + { + g_info ("Config %s is now deprecated.", + config_id); + } + set_resource_id_deprecated ("config", config_id, TRUE); + } + else + { + if (resource_id_deprecated ("config", config_id)) + { + set_resource_id_deprecated ("config", config_id, FALSE); + g_info ("Deprecation of config %s has been revoked.", + config_id); + } + } + g_free (config_id); + sql_commit (); } diff --git a/src/manage_sql_configs.h b/src/manage_sql_configs.h index ba4b79804..888d4e4fb 100644 --- a/src/manage_sql_configs.h +++ b/src/manage_sql_configs.h @@ -89,9 +89,12 @@ migrate_predefined_configs (); int config_updated_in_feed (config_t, const gchar *); +int +deprecated_config_id_updated_in_feed (const char *, const gchar *); + void update_config (config_t, const gchar *, const gchar *, const gchar *, - int, const array_t*, const array_t*); + int, const array_t*, const array_t*, const gchar *); void check_db_configs (); diff --git a/src/manage_sql_port_lists.c b/src/manage_sql_port_lists.c index a3db4a041..ce0771219 100644 --- a/src/manage_sql_port_lists.c +++ b/src/manage_sql_port_lists.c @@ -2516,8 +2516,8 @@ migrate_predefined_port_lists () /** * @brief Check if a port list has been updated in the feed. * - * @param[in] path Full path to port list XML in feed. * @param[in] port_list Port List. + * @param[in] path Full path to port list XML in feed. * * @return 1 if updated in feed, else 0. */ @@ -2545,6 +2545,42 @@ port_list_updated_in_feed (port_list_t port_list, const gchar *path) return 1; } +/** + * @brief Check if a deprecated port list has been updated in the feed. + * + * @param[in] port_list_id Port list UUID. + * @param[in] path Full path to port list XML in feed. + * + * @return 1 if updated in feed, else 0. + */ +int +deprecated_port_list_id_updated_in_feed (const char *port_list_id, + const gchar *path) +{ + gchar *quoted_uuid; + GStatBuf state; + int last_update; + + quoted_uuid = sql_quote (port_list_id); + last_update = sql_int ("SELECT modification_time FROM deprecated_feed_data" + " WHERE type = 'port_list' AND uuid = '%s';", + quoted_uuid); + g_free (quoted_uuid); + + if (g_stat (path, &state)) + { + g_warning ("%s: Failed to stat feed port_list file: %s", + __func__, + strerror (errno)); + return 0; + } + + if (state.st_mtime <= last_update) + return 0; + + return 1; +} + /** * @brief Update a port list from an XML file. * @@ -2552,15 +2588,18 @@ port_list_updated_in_feed (port_list_t port_list, const gchar *path) * @param[in] name New name. * @param[in] comment New comment. * @param[in] ranges New port ranges. + * @param[in] deprecated Deprecation status. */ void update_port_list (port_list_t port_list, const gchar *name, const gchar *comment, - array_t *ranges /* range_t */) + array_t *ranges /* range_t */, + const char *deprecated) { gchar *quoted_name, *quoted_comment; int index; range_t *range; + char *port_list_id; sql_begin_immediate (); @@ -2585,7 +2624,30 @@ update_port_list (port_list_t port_list, const gchar *name, while ((range = (range_t*) g_ptr_array_index (ranges, index++))) insert_port_range (port_list, range->type, range->start, range->end); + /* Handle deprecation status */ + + port_list_id = resource_uuid ("port_list", port_list); + if (deprecated && atoi (deprecated)) + { + if (resource_id_deprecated ("port_list", port_list_id) == 0) + { + g_info ("Port list %s is now deprecated.", + port_list_id); + } + set_resource_id_deprecated ("port_list", port_list_id, TRUE); + } + else + { + if (resource_id_deprecated ("port_list", port_list_id)) + { + set_resource_id_deprecated ("port_list", port_list_id, FALSE); + g_info ("Deprecation of port list %s has been revoked.", + port_list_id); + } + } + sql_commit (); + free (port_list_id); } /** diff --git a/src/manage_sql_port_lists.h b/src/manage_sql_port_lists.h index dce863cb3..9d20c199c 100644 --- a/src/manage_sql_port_lists.h +++ b/src/manage_sql_port_lists.h @@ -66,8 +66,12 @@ migrate_predefined_port_lists (); int port_list_updated_in_feed (port_list_t, const gchar *); +int +deprecated_port_list_id_updated_in_feed (const char *, const gchar *); + void -update_port_list (port_list_t, const gchar *, const gchar *, array_t *); +update_port_list (port_list_t, const gchar *, const gchar *, array_t *, + const gchar *); void check_db_port_lists (); diff --git a/src/manage_sql_report_formats.c b/src/manage_sql_report_formats.c index 155cc5079..89b23c4ca 100644 --- a/src/manage_sql_report_formats.c +++ b/src/manage_sql_report_formats.c @@ -4129,13 +4129,15 @@ delete_report_format_dirs_user (const gchar *user_id, iterator_t *rows) * @param[in] files New files. * @param[in] params New params. * @param[in] params_options Options for new params. + * @param[in] deprecated New deprecation status. */ void -update_report_format (report_format_t report_format, const gchar *report_id, const gchar *name, +update_report_format (report_format_t report_format, const gchar *report_id, + const gchar *name, const gchar *content_type, const gchar *extension, const gchar *summary, const gchar *description, const gchar *signature, array_t *files, array_t *params, - array_t *params_options) + array_t *params_options, const char *deprecated) { int ret; gchar *quoted_name, *quoted_content_type, *quoted_extension, *quoted_summary; @@ -4205,14 +4207,35 @@ update_report_format (report_format_t report_format, const gchar *report_id, con save_report_format_files (report_id, files, NULL); + /* Handle deprecation status */ + + if (deprecated && atoi (deprecated)) + { + if (resource_id_deprecated ("report_format", report_id) == 0) + { + g_info ("Report format %s is now deprecated.", + report_id); + } + set_resource_id_deprecated ("report_format", report_id, TRUE); + } + else + { + if (resource_id_deprecated ("report_format", report_id)) + { + set_resource_id_deprecated ("report_format", report_id, FALSE); + g_info ("Deprecation of report format %s has been revoked.", + report_id); + } + } + sql_commit (); } /** * @brief Check if a report format has been updated in the feed. * - * @param[in] path Full path to report format XML in feed. * @param[in] report_format Report Format. + * @param[in] path Full path to report format XML in feed. * * @return 1 if updated in feed, else 0. */ @@ -4240,6 +4263,42 @@ report_format_updated_in_feed (report_format_t report_format, const gchar *path) return 1; } +/** + * @brief Check if a deprecated report format has been updated in the feed. + * + * @param[in] report_format_id Report Format UUID. + * @param[in] path Full path to report format XML in feed. + * + * @return 1 if updated in feed, else 0. + */ +int +deprecated_report_format_id_updated_in_feed (const char *report_format_id, + const gchar *path) +{ + gchar *quoted_uuid; + GStatBuf state; + int last_update; + + quoted_uuid = sql_quote (report_format_id); + last_update = sql_int ("SELECT modification_time FROM deprecated_feed_data" + " WHERE type = 'report_format' AND uuid = '%s';", + quoted_uuid); + g_free (quoted_uuid); + + if (g_stat (path, &state)) + { + g_warning ("%s: Failed to stat feed report_format file: %s", + __func__, + strerror (errno)); + return 0; + } + + if (state.st_mtime <= last_update) + return 0; + + return 1; +} + /** * @brief Migrate old ownerless report formats to the Feed Owner. * diff --git a/src/manage_sql_report_formats.h b/src/manage_sql_report_formats.h index bd55a09c1..632c22089 100644 --- a/src/manage_sql_report_formats.h +++ b/src/manage_sql_report_formats.h @@ -71,11 +71,14 @@ void update_report_format (report_format_t, const gchar *, const gchar *, const gchar *, const gchar *, const gchar *, const gchar *, const gchar *, array_t *, array_t *, - array_t *); + array_t *, const gchar *); int report_format_updated_in_feed (report_format_t, const gchar *); +int +deprecated_report_format_id_updated_in_feed (const char*, const gchar *); + int migrate_predefined_report_formats (); diff --git a/src/schema_formats/XML/GMP.xml.in b/src/schema_formats/XML/GMP.xml.in index d36ebdd6a..fa40654bd 100644 --- a/src/schema_formats/XML/GMP.xml.in +++ b/src/schema_formats/XML/GMP.xml.in @@ -7459,6 +7459,7 @@ END:VCALENDAR preferences nvt_selectors predefined + deprecated owner @@ -7838,6 +7839,11 @@ END:VCALENDAR Whether the config was predefined by the feed boolean + + deprecated + Whether the config is deprecated + boolean + filters @@ -14910,6 +14916,7 @@ END:VCALENDAR port_ranges targets predefined + deprecated owner @@ -15114,6 +15121,11 @@ END:VCALENDAR Whether the port list was predefined by the feed boolean + + deprecated + Whether the port list is deprecated + boolean + filters @@ -16522,6 +16534,7 @@ END:VCALENDAR trust active predefined + deprecated param @@ -16778,11 +16791,11 @@ END:VCALENDAR 1 time - - yes - no - unknown - + + yes + no + unknown + time @@ -16800,6 +16813,11 @@ END:VCALENDAR Whether the report format was predefined by the feed boolean + + deprecated + Whether the report format is deprecated + boolean + filters From 49461394c336d910a1361a99bb4bc1128d4161d3 Mon Sep 17 00:00:00 2001 From: Timo Pollmeier Date: Mon, 6 Nov 2023 15:56:15 +0100 Subject: [PATCH 2/2] Free uuid in should_sync_... functions, fix typo --- src/manage_configs.c | 12 ++++++++++-- src/manage_port_lists.c | 11 +++++++++-- src/manage_report_formats.c | 11 +++++++++-- src/manage_sql.c | 2 +- 4 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/manage_configs.c b/src/manage_configs.c index 02cae9e8a..e88f7b023 100644 --- a/src/manage_configs.c +++ b/src/manage_configs.c @@ -345,14 +345,19 @@ should_sync_config_from_path (const char *path, gboolean rebuild, find_config_no_acl (uuid, config); if (rebuild) - return 1; + { + g_free (uuid); + return 1; + } full_path = g_build_filename (feed_dir_configs (), path, NULL); if (deprecated_config_id_updated_in_feed (uuid, full_path)) { + g_free (uuid); g_free (full_path); return 1; } + g_free (uuid); g_free (full_path); return 0; } @@ -361,7 +366,10 @@ should_sync_config_from_path (const char *path, gboolean rebuild, && *config) { if (rebuild) - return 1; + { + g_free (uuid); + return 1; + } full_path = g_build_filename (feed_dir_configs (), path, NULL); diff --git a/src/manage_port_lists.c b/src/manage_port_lists.c index 843238929..39f2d1775 100644 --- a/src/manage_port_lists.c +++ b/src/manage_port_lists.c @@ -282,14 +282,18 @@ should_sync_port_list_from_path (const char *path, gboolean rebuild, find_port_list_no_acl (uuid, port_list); if (rebuild) - return 1; + { + return 1; + } full_path = g_build_filename (feed_dir_port_lists (), path, NULL); if (deprecated_port_list_id_updated_in_feed (uuid, full_path)) { + g_free (uuid); g_free (full_path); return 1; } + g_free (uuid); g_free (full_path); return 0; } @@ -298,7 +302,10 @@ should_sync_port_list_from_path (const char *path, gboolean rebuild, && *port_list) { if (rebuild) - return 1; + { + g_free (uuid); + return 1; + } full_path = g_build_filename (feed_dir_port_lists (), path, NULL); diff --git a/src/manage_report_formats.c b/src/manage_report_formats.c index 4da844980..340321189 100644 --- a/src/manage_report_formats.c +++ b/src/manage_report_formats.c @@ -643,7 +643,10 @@ should_sync_report_format_from_path (const char *path, find_report_format_no_acl (uuid, report_format); if (rebuild) - return 1; + { + g_free (uuid); + return 1; + } full_path = g_build_filename (feed_dir_report_formats (), path, NULL); if (deprecated_report_format_id_updated_in_feed (uuid, full_path)) @@ -651,6 +654,7 @@ should_sync_report_format_from_path (const char *path, g_free (full_path); return 1; } + g_free (uuid); g_free (full_path); return 0; } @@ -659,7 +663,10 @@ should_sync_report_format_from_path (const char *path, && *report_format) { if (rebuild) - return 1; + { + g_free (uuid); + return 1; + } full_path = g_build_filename (feed_dir_report_formats (), path, NULL); diff --git a/src/manage_sql.c b/src/manage_sql.c index 9f6ff27d1..c57d597b6 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -4774,7 +4774,7 @@ resource_id_deprecated (const char *type, const char *resource_id) } /** - * @brief Mark whether resource as deprecated. + * @brief Mark whether resource is deprecated. * * @param[in] type Resource type. * @param[in] resource_id UUID of the resource.