diff --git a/src/gmp.c b/src/gmp.c index 96df085f9..3fdc1bc24 100644 --- a/src/gmp.c +++ b/src/gmp.c @@ -2028,10 +2028,61 @@ get_data_reset (get_data_t *data) free (data->filter_replacement); free (data->subtype); free (data->type); + if (data->extra_params) + g_hash_table_destroy (data->extra_params); memset (data, 0, sizeof (get_data_t)); } +/** + * @brief Retrieves a type-specific extra parameter from a get_data_t. + * + * @param[in] data The get data to add the parameter to. + * @param[in] name Name of the parameter to add. + * + * @return Value of the parameter or NULL if not set. + */ +const char * +get_data_get_extra (const get_data_t *data, const char *name) +{ + if (data->extra_params == NULL) + return NULL; + else + return g_hash_table_lookup (data->extra_params, name); +} + +/** + * @brief Sets a type-specific extra parameter in a get_data_t. + * + * The names and values will be duplicated. + * + * @param[in] data The get data to add the parameter to. + * @param[in] name Name of the parameter to add. + * @param[in] value Value of the parameter to add. + */ +void +get_data_set_extra (get_data_t *data, const char *name, const char *value) +{ + if (name == NULL) + return; + + if (data->extra_params == NULL) + { + if (value == NULL) + return; + + data->extra_params + = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); + } + + if (value) + g_hash_table_insert (data->extra_params, + g_strdup (name), + g_strdup (value)); + else + g_hash_table_remove (data->extra_params, name); +} + /** * @brief Command data for the get_agents command. */ @@ -5959,6 +6010,15 @@ gmp_xml_handle_start_element (/* unused */ GMarkupParseContext* context, "ignore_pagination", &attribute) == 0) get_aggregates_data->get.ignore_pagination = 1; + // Extra selection attribute for configs and tasks + if (find_attribute (attribute_names, attribute_values, + "usage_type", &attribute)) + { + get_data_set_extra (&get_aggregates_data->get, + "usage_type", + attribute); + } + set_client_state (CLIENT_GET_AGGREGATES); } else if (strcasecmp ("GET_CONFIGS", element_name) == 0) @@ -5988,6 +6048,14 @@ gmp_xml_handle_start_element (/* unused */ GMarkupParseContext* context, else get_configs_data->preferences = 0; + if (find_attribute (attribute_names, attribute_values, + "usage_type", &attribute)) + { + get_data_set_extra (&get_configs_data->get, + "usage_type", + attribute); + } + set_client_state (CLIENT_GET_CONFIGS); } else if (strcasecmp ("GET_ALERTS", element_name) == 0) @@ -6454,6 +6522,15 @@ gmp_xml_handle_start_element (/* unused */ GMarkupParseContext* context, get_tasks_data->schedules_only = strcmp (attribute, "0"); else get_tasks_data->schedules_only = 0; + + if (find_attribute (attribute_names, attribute_values, + "usage_type", &attribute)) + { + get_data_set_extra (&get_tasks_data->get, + "usage_type", + attribute); + } + set_client_state (CLIENT_GET_TASKS); } ELSE_GET_START (tickets, TICKETS) diff --git a/src/manage.h b/src/manage.h index bfe3c6555..7bec76b06 100644 --- a/src/manage.h +++ b/src/manage.h @@ -383,11 +383,18 @@ typedef struct int ignore_max_rows_per_page; ///< Whether to ignore the Max Rows Per Page setting. int ignore_pagination; ///< Whether to ignore the pagination (first and max). int minimal; ///< Whether to respond with minimal information. + GHashTable *extra_params; ///< Hashtable of type-specific extra parameters. } get_data_t; void get_data_reset (get_data_t*); +const char * +get_data_get_extra (const get_data_t *, const char *); + +void +get_data_set_extra (get_data_t *, const char *, const char *); + resource_t get_iterator_resource (iterator_t*); diff --git a/src/manage_sql.c b/src/manage_sql.c index 8aa6cf393..f2f0db256 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -14695,6 +14695,35 @@ alert_task_iterator_readable (iterator_t* iterator) /* Task functions. */ +/** + * @brief Generate an extra WHERE clause for selecting tasks + * + * @param[in] trash Whether to get tasks from the trashcan. + * @param[in] usage_type The usage type to limit the selection to. + * + * @return Newly allocated where clause string. + */ +static gchar * +tasks_extra_where (int trash, const char *usage_type) +{ + gchar *extra_where = NULL; + if (usage_type && strcmp (usage_type, "")) + { + gchar *quoted_usage_type; + quoted_usage_type = sql_quote (usage_type); + extra_where = g_strdup_printf (" AND hidden = %d" + " AND usage_type = '%s'", + trash ? 2 : 0, + quoted_usage_type); + g_free (quoted_usage_type); + } + else + extra_where = g_strdup_printf (" AND hidden = %d", + trash ? 2 : 0); + + return extra_where; +} + /** * @brief Append value to field of task. * @@ -15061,7 +15090,8 @@ init_task_iterator (iterator_t* iterator, const get_data_t *get) static column_t where_columns_min[] = TASK_ITERATOR_WHERE_COLUMNS_MIN; char *filter; int overrides, min_qod; - gchar *extra_tables; + const char *usage_type; + gchar *extra_tables, *extra_where; int ret; if (get->filt_id && strcmp (get->filt_id, FILT_ID_NONE)) @@ -15079,6 +15109,8 @@ init_task_iterator (iterator_t* iterator, const get_data_t *get) free (filter); extra_tables = task_iterator_opts_table (overrides, min_qod, 0); + usage_type = get_data_get_extra (get, "usage_type"); + extra_where = tasks_extra_where (get->trash, usage_type); ret = init_get_iterator2 (iterator, "task", @@ -15092,14 +15124,13 @@ init_task_iterator (iterator_t* iterator, const get_data_t *get) filter_columns, 0, extra_tables, - get->trash - ? " AND hidden = 2" - : " AND hidden = 0", + extra_where, current_credentials.uuid ? TRUE : FALSE, FALSE, NULL); g_free (extra_tables); + g_free (extra_where); return ret; } @@ -18855,8 +18886,8 @@ resource_count (const char *type, const get_data_t *get) static const char *filter_columns[] = { "owner", NULL }; static column_t select_columns[] = {{ "owner", NULL }, { NULL, NULL }}; get_data_t count_get; - - const char *extra_where; + gchar *extra_where; + int rc; memset (&count_get, '\0', sizeof (count_get)); count_get.trash = get->trash; @@ -18865,41 +18896,49 @@ resource_count (const char *type, const get_data_t *get) else count_get.filter = "rows=-1 first=1 permission=any"; - if (strcmp (type, "task") == 0) + if (strcasecmp (type, "config") == 0) + { + const gchar *usage_type = get_data_get_extra (get, "usage_type"); + extra_where = configs_extra_where (usage_type); + } + else if (strcmp (type, "task") == 0) { - extra_where = get->trash - ? " AND hidden = 2" - : " AND hidden = 0"; + const gchar *usage_type = get_data_get_extra (get, "usage_type"); + extra_where = tasks_extra_where (get->trash, usage_type); } else if (strcmp (type, "report") == 0) { - extra_where = " AND (SELECT hidden FROM tasks" - " WHERE tasks.id = task)" - " = 0"; + extra_where = g_strdup (" AND (SELECT hidden FROM tasks" + " WHERE tasks.id = task)" + " = 0"); } else if (strcmp (type, "result") == 0) { - extra_where = " AND (severity != " G_STRINGIFY (SEVERITY_ERROR) ")"; + extra_where + = g_strdup (" AND (severity != " G_STRINGIFY (SEVERITY_ERROR) ")"); } else if (strcmp (type, "vuln") == 0) { - extra_where = " AND (vuln_results (vulns.uuid," - " cast (null AS integer)," - " cast (null AS integer)," - " cast (null AS text))" - " > 0)"; + extra_where = g_strdup (" AND (vuln_results (vulns.uuid," + " cast (null AS integer)," + " cast (null AS integer)," + " cast (null AS text))" + " > 0)"); } else extra_where = NULL; - return count (get->subtype ? get->subtype : type, - &count_get, - type_owned (type) ? select_columns : NULL, - type_owned (type) ? select_columns : NULL, - type_owned (type) ? filter_columns : NULL, - 0, NULL, - extra_where, - type_owned (type)); + rc = count (get->subtype ? get->subtype : type, + &count_get, + type_owned (type) ? select_columns : NULL, + type_owned (type) ? select_columns : NULL, + type_owned (type) ? filter_columns : NULL, + 0, NULL, + extra_where, + type_owned (type)); + + g_free (extra_where); + return rc; } /** @@ -18917,7 +18956,8 @@ task_count (const get_data_t *get) static column_t where_columns[] = TASK_ITERATOR_WHERE_COLUMNS; char *filter; int overrides, min_qod; - gchar *extra_tables; + const char *usage_type; + gchar *extra_tables, *extra_where; int ret; if (get->filt_id && strcmp (get->filt_id, FILT_ID_NONE)) @@ -18935,6 +18975,8 @@ task_count (const get_data_t *get) free (filter); extra_tables = task_iterator_opts_table (overrides, min_qod, 0); + usage_type = get_data_get_extra (get, "usage_type"); + extra_where = tasks_extra_where (get->trash, usage_type); ret = count2 ("task", get, columns, @@ -18943,12 +18985,11 @@ task_count (const get_data_t *get) where_columns, extra_columns, 0, extra_tables, - get->trash - ? " AND hidden = 2" - : " AND hidden = 0", + extra_where, TRUE); g_free (extra_tables); + g_free (extra_where); return ret; } @@ -63408,15 +63449,24 @@ type_table (const char *type, int trash) * @param[in] type Resource type to get columns of. * @param[in] trash Whether to get the trash table. * @param[in] filter The filter term. + * @param[in] extra_params Optional extra parameters. * * @return The newly allocated WHERE clause additions. */ static gchar* -type_extra_where (const char *type, int trash, const char *filter) +type_extra_where (const char *type, int trash, const char *filter, + GHashTable *extra_params) { gchar *extra_where; - if (strcasecmp (type, "TASK") == 0) + if (strcasecmp (type, "CONFIG") == 0 && extra_params) + { + gchar *usage_type = g_hash_table_lookup (extra_params, "usage_type"); + extra_where = configs_extra_where (usage_type); + if (extra_where == NULL) + extra_where = g_strdup (""); + } + else if (strcasecmp (type, "TASK") == 0) { if (trash) extra_where = g_strdup (" AND hidden = 2"); @@ -63536,7 +63586,8 @@ type_build_select (const char *type, const char *columns_str, extra_where = g_strdup (given_extra_where); else extra_where = type_extra_where (type, get->trash, - filter ? filter : get->filter); + filter ? filter : get->filter, + get->extra_params); if (get->ignore_pagination) pagination_clauses = NULL; diff --git a/src/manage_sql_configs.c b/src/manage_sql_configs.c index 80e0b29fd..0557665eb 100644 --- a/src/manage_sql_configs.c +++ b/src/manage_sql_configs.c @@ -2327,6 +2327,28 @@ insert_osp_parameter (osp_param_t *param, config_t config) return ret; } +/** + * @brief Generate an extra WHERE clause for selecting configs + * + * @param[in] usage_type The usage type to limit the selection to. + * + * @return Newly allocated where clause string. + */ +gchar * +configs_extra_where (const char *usage_type) +{ + gchar *extra_where = NULL; + if (usage_type && strcmp (usage_type, "")) + { + gchar *quoted_usage_type; + quoted_usage_type = sql_quote (usage_type); + extra_where = g_strdup_printf (" AND usage_type = '%s'", + quoted_usage_type); + g_free (quoted_usage_type); + } + return extra_where; +} + /** * @brief Create a config from an OSP scanner. * @@ -3008,11 +3030,18 @@ sync_config (const char *config_id) int config_count (const get_data_t *get) { + int rc; static const char *filter_columns[] = CONFIG_ITERATOR_FILTER_COLUMNS; static column_t columns[] = CONFIG_ITERATOR_COLUMNS; static column_t trash_columns[] = CONFIG_ITERATOR_TRASH_COLUMNS; - return count ("config", get, columns, trash_columns, filter_columns, - 0, 0, 0, TRUE); + const char *usage_type = get_data_get_extra (get, "usage_type"); + gchar *extra_where = configs_extra_where (usage_type); + + rc = count ("config", get, columns, trash_columns, filter_columns, + 0, 0, extra_where, TRUE); + + g_free (extra_where); + return rc; } /** @@ -3074,20 +3103,25 @@ init_user_config_iterator (iterator_t* iterator, config_t config, int trash, int init_config_iterator (iterator_t* iterator, const get_data_t *get) { + int rc; static const char *filter_columns[] = CONFIG_ITERATOR_FILTER_COLUMNS; static column_t columns[] = CONFIG_ITERATOR_COLUMNS; static column_t trash_columns[] = CONFIG_ITERATOR_TRASH_COLUMNS; - - return init_get_iterator (iterator, - "config", - get, - columns, - trash_columns, - filter_columns, - 0, - NULL, - NULL, - TRUE); + const char *usage_type = get_data_get_extra (get, "usage_type"); + gchar *extra_where = configs_extra_where (usage_type); + + rc = init_get_iterator (iterator, + "config", + get, + columns, + trash_columns, + filter_columns, + 0, + NULL, + extra_where, + TRUE); + g_free (extra_where); + return rc; } /** diff --git a/src/manage_sql_configs.h b/src/manage_sql_configs.h index 3fca6f5d3..32ef08b9e 100644 --- a/src/manage_sql_configs.h +++ b/src/manage_sql_configs.h @@ -72,4 +72,7 @@ { NULL, NULL, KEYWORD_TYPE_UNKNOWN } \ } +gchar * +configs_extra_where (const char *); + #endif /* not _GVMD_MANAGE_SQL_CONFIGS_H */ diff --git a/src/schema_formats/XML/GMP.xml.in b/src/schema_formats/XML/GMP.xml.in index e0f367a97..ec828b6f9 100644 --- a/src/schema_formats/XML/GMP.xml.in +++ b/src/schema_formats/XML/GMP.xml.in @@ -8123,6 +8123,17 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. Whether to get tasks using this config boolean + + usage_type + Optional usage type to limit the configs to. Affects total count unlike filter + + + policy + scan + + + + @@ -8850,6 +8861,18 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + + usage_type + Optional usage type to limit configs and tasks to + + + audit + policy + scan + + + + sort data_column text_column @@ -20600,6 +20623,17 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. Whether to only include id, name and schedule details boolean + + usage_type + Optional usage type to limit the tasks to. Affects total count unlike filter + + + audit + policy + + + +