Skip to content

Commit

Permalink
Add: Feed data objects can now be deprecated
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
timopollmeier committed Nov 6, 2023
1 parent a5b0e74 commit 9bde214
Show file tree
Hide file tree
Showing 20 changed files with 451 additions and 38 deletions.
18 changes: 18 additions & 0 deletions src/gmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 ("<deprecated>1</deprecated>");
}

/** @todo This should really be an nvt_selector_t. */
selector = config_iterator_nvt_selector (&configs);
config = get_iterator_resource (&configs);
Expand Down Expand Up @@ -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 ("<deprecated>1</deprecated>");
}

SENDF_TO_CLIENT_OR_FAIL ("<port_count>"
"<all>%i</all>"
"<tcp>%i</tcp>"
Expand Down Expand Up @@ -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 ("<deprecated>1</deprecated>");
}

if (get_report_formats_data->alerts)
{
iterator_t alerts;
Expand Down
14 changes: 12 additions & 2 deletions src/gmp_configs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/gmp_configs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand Down
14 changes: 12 additions & 2 deletions src/gmp_port_lists.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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. */

Expand Down
3 changes: 2 additions & 1 deletion src/gmp_port_lists.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
8 changes: 6 additions & 2 deletions src/gmp_report_formats.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,16 @@ 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,
const char **report_format_id, char **name,
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;
Expand All @@ -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 ();
Expand Down Expand Up @@ -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,
&params, &params_options);
&params, &params_options, NULL);

/* Check data, then create report format. */

Expand Down
2 changes: 1 addition & 1 deletion src/gmp_report_formats.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
6 changes: 6 additions & 0 deletions src/manage.h
Original file line number Diff line number Diff line change
Expand Up @@ -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. */

Expand Down
39 changes: 34 additions & 5 deletions src/manage_configs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -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. */

Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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)
{
Expand All @@ -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;
}

Expand Down
7 changes: 7 additions & 0 deletions src/manage_pg.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,"
Expand Down
40 changes: 35 additions & 5 deletions src/manage_port_lists.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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. */

Expand Down Expand Up @@ -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);
Expand All @@ -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. */

Expand Down Expand Up @@ -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)
{
Expand All @@ -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;
}

Expand Down
Loading

0 comments on commit 9bde214

Please sign in to comment.