Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add usage_type selection to get_configs and get_tasks #625

Merged
merged 4 commits into from
Jul 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions src/gmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions src/manage.h
Original file line number Diff line number Diff line change
Expand Up @@ -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*);

Expand Down
119 changes: 85 additions & 34 deletions src/manage_sql.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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))
Expand All @@ -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",
Expand All @@ -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;
}

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

/**
Expand All @@ -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))
Expand All @@ -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,
Expand All @@ -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;
}

Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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;
Expand Down
Loading