Skip to content

Commit

Permalink
Merge pull request #728 from timopollmeier/bulk-tags-backend
Browse files Browse the repository at this point in the history
Remove create_tags, allow multiple resources in tags
  • Loading branch information
bjoernricks authored Jun 26, 2018
2 parents 895955f + 7110248 commit ce8c0fd
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 130 deletions.
2 changes: 1 addition & 1 deletion gsad/src/gsad.c
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,7 @@ init_validator ()
"^(agent|alert|asset|config|credential|filter|group|host|nvt|note|os|override|permission|port_list|report|report_format|result|role|scanner|schedule|tag|target|task|user|info|cve|cpe|ovaldef|cert_bund_adv|dfn_cert_adv|vuln|"
"Agent|Alert|Asset|Config|Credential|Filter|Group|Host|Note|NVT|Operating System|Override|Permission|Port List|Report|Report Format|Result|Role|Scanner|Schedule|Tag|Target|Task|User|SecInfo|CVE|CPE|OVAL Definition|CERT-Bund Advisory|DFN-CERT Advisory|Vulnerability)$");
openvas_validator_add (validator, "resource_id", "^[[:alnum:]-_.:\\/~]*$");
openvas_validator_add (validator, "resources_action", "^(|add|set|remove)$");
openvas_validator_add (validator, "optional_resource_type",
"^(agent|alert|asset|config|credential|filter|group|host|note|nvt|os|override|permission|port_list|report|report_format|result|role|scanner|schedule|tag|target|task|user|info|vuln|"
"Agent|Alert|Asset|Config|Credential|Filter|Group|Host|Note|NVT|Operating System|Override|Permission|Port List|Report|Report Format|Result|Role|Scanner|Schedule|Tag|Target|Task|User|SecInfo|Vulnerability)?$");
Expand Down Expand Up @@ -1506,7 +1507,6 @@ exec_gmp_post (http_connection_t *con,
ELSE (create_scanner)
ELSE (create_schedule)
ELSE (create_tag)
ELSE (create_tags)
ELSE (create_target)
ELSE (create_config)
ELSE (create_note)
Expand Down
193 changes: 64 additions & 129 deletions gsad/src/gsad_gmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -9469,104 +9469,7 @@ empty_trashcan_gmp (gvm_connection_t *connection, credentials_t *
}

/**
* @brief Create a tag, get report, envelope the result.
*
* @param[in] connection Connection to manager.
* @param[in] credentials Username and password for authentication.
* @param[in] params Request parameters.
* @param[out] response_data Extra data return for the HTTP response.
*
* @return Enveloped XML object.
*/
char *
create_tag_gmp (gvm_connection_t *connection, credentials_t *credentials,
params_t *params, cmd_response_data_t* response_data)
{
char *ret;
gchar *response;
const char *name, *comment, *value, *resource_type, *resource_id, *active;
entity_t entity;

name = params_value (params, "tag_name");
comment = params_value (params, "comment");
value = params_value (params, "tag_value");
resource_type = params_value (params, "resource_type");
resource_id = params_value (params, "resource_id");
active = params_value (params, "active");

CHECK_PARAM_INVALID (name, "Create Tag")
CHECK_PARAM_INVALID (comment, "Create Tag")
CHECK_PARAM_INVALID (value, "Create Tag")
CHECK_PARAM_INVALID (resource_type, "Create Tag")
CHECK_PARAM_INVALID (resource_id, "Create Tag")
CHECK_PARAM_INVALID (active, "Create Tag")

response = NULL;
entity = NULL;
switch (gmpf (connection, credentials,
&response,
&entity,
response_data,
"<create_tag>"
"<name>%s</name>"
"<comment>%s</comment>"
"<value>%s</value>"
"<resource id=\"%s\">"
"<type>%s</type>"
"</resource>"
"<active>%s</active>"
"</create_tag>",
name,
comment,
value,
resource_id,
resource_type,
active))
{
case 0:
case -1:
break;
case 1:
cmd_response_data_set_status_code (response_data,
MHD_HTTP_INTERNAL_SERVER_ERROR);
return gsad_message (credentials,
"Internal error", __FUNCTION__, __LINE__,
"An internal error occurred while creating a new tag. "
"No new tag was created. "
"Diagnostics: Failure to send command to manager daemon.",
response_data);
case 2:
cmd_response_data_set_status_code (response_data,
MHD_HTTP_INTERNAL_SERVER_ERROR);
return gsad_message (credentials,
"Internal error", __FUNCTION__, __LINE__,
"An internal error occurred while creating a new tag. "
"It is unclear whether the tag has been created or not. "
"Diagnostics: Failure to receive response from manager daemon.",
response_data);
default:
cmd_response_data_set_status_code (response_data,
MHD_HTTP_INTERNAL_SERVER_ERROR);
return gsad_message (credentials,
"Internal error", __FUNCTION__, __LINE__,
"An internal error occurred while creating a new tag. "
"It is unclear whether the tag has been created or not. "
"Diagnostics: Internal Error.",
response_data);
}

if (entity_attribute (entity, "id"))
params_add (params, "tag_id", entity_attribute (entity, "id"));
ret = response_from_entity (connection, credentials, params, entity,
"Create Tag", response_data);

free_entity (entity);
g_free (response);
return ret;
}

/**
* @brief Create multiple tags, envelope the result.
* @brief Create a tag, envelope the result.
*
* @param[in] connection Connection to manager.
* @param[in] credentials Username and password for authentication.
Expand All @@ -9576,8 +9479,8 @@ create_tag_gmp (gvm_connection_t *connection, credentials_t *credentials,
* @return Enveloped XML object.
*/
char *
create_tags_gmp (gvm_connection_t *connection, credentials_t *credentials,
params_t *params, cmd_response_data_t* response_data)
create_tag_gmp (gvm_connection_t *connection, credentials_t *credentials,
params_t *params, cmd_response_data_t* response_data)
{
char *ret;
gchar *response;
Expand Down Expand Up @@ -9605,7 +9508,7 @@ create_tags_gmp (gvm_connection_t *connection, credentials_t *credentials,
command = g_string_new ("");

xml_string_append (command,
"<create_tags>"
"<create_tag>"
"<name>%s</name>"
"<comment>%s</comment>"
"<value>%s</value>"
Expand Down Expand Up @@ -9635,7 +9538,7 @@ create_tags_gmp (gvm_connection_t *connection, credentials_t *credentials,

xml_string_append (command,
"</resources>"
"</create_tags>");
"</create_tag>");

response = NULL;
entity = NULL;
Expand All @@ -9654,7 +9557,7 @@ create_tags_gmp (gvm_connection_t *connection, credentials_t *credentials,
g_string_free (command, TRUE);
return gsad_message (credentials,
"Internal error", __FUNCTION__, __LINE__,
"An internal error occurred while creating new tags. "
"An internal error occurred while creating a new tag. "
"No new tag was created. "
"Diagnostics: Failure to send command to manager daemon.",
response_data);
Expand All @@ -9664,7 +9567,7 @@ create_tags_gmp (gvm_connection_t *connection, credentials_t *credentials,
g_string_free (command, TRUE);
return gsad_message (credentials,
"Internal error", __FUNCTION__, __LINE__,
"An internal error occurred while creating new tags. "
"An internal error occurred while creating a new tag. "
"It is unclear whether the tag has been created or not. "
"Diagnostics: Failure to receive response from manager daemon.",
response_data);
Expand All @@ -9674,15 +9577,15 @@ create_tags_gmp (gvm_connection_t *connection, credentials_t *credentials,
g_string_free (command, TRUE);
return gsad_message (credentials,
"Internal error", __FUNCTION__, __LINE__,
"An internal error occurred while creating new tags. "
"An internal error occurred while creating a new tag. "
"It is unclear whether the tag has been created or not. "
"Diagnostics: Internal Error.",
response_data);
}

g_string_free (command, TRUE);
ret = response_from_entity (connection, credentials, params, entity,
"Create Tags", response_data);
"Create Tag", response_data);

free_entity (entity);
g_free (response);
Expand Down Expand Up @@ -9837,56 +9740,85 @@ save_tag_gmp (gvm_connection_t *connection, credentials_t * credentials,
params_t *params, cmd_response_data_t* response_data)
{
gchar *response;
const char *name, *comment, *value, *resource_type, *resource_id, *active;
const char *name, *comment, *filter, *value, *resource_type, *active;
const char *resources_action;
params_t *resource_ids;
const char *tag_id;
GString *command;
entity_t entity;
char* ret;

tag_id = params_value (params, "tag_id");
name = params_value (params, "tag_name");
comment = params_value (params, "comment");
filter = params_value (params, "filter");
value = params_value (params, "tag_value");
resource_type = params_value (params, "resource_type");
resource_id = params_value (params, "resource_id");
resource_ids = params_values (params, "resource_ids:");
resources_action = params_value (params, "resources_action");
active = params_value (params, "active");

CHECK_PARAM_INVALID (tag_id, "Save Tag")
CHECK_PARAM_INVALID (name, "Save Tag")
CHECK_PARAM_INVALID (comment, "Save Tag")
if (params_given (params, "filter"))
CHECK_PARAM_INVALID (filter, "Save Tag")
CHECK_PARAM_INVALID (value, "Save Tag")
if (params_given (params, "resources_action"))
CHECK_PARAM_INVALID (resources_action, "Save Tag")
CHECK_PARAM_INVALID (resource_type, "Save Tag")
CHECK_PARAM_INVALID (resource_id, "Save Tag")
CHECK_PARAM_INVALID (active, "Save Tag")

command = g_string_new ("");

xml_string_append (command,
"<modify_tag tag_id=\"%s\">"
"<name>%s</name>"
"<comment>%s</comment>"
"<value>%s</value>"
"<resources action=\"%s\" filter=\"%s\">"
"<type>%s</type>",
tag_id,
name,
comment,
value,
resources_action ? resources_action : "",
filter ? filter : "",
resource_type);

if (resource_ids)
{
params_iterator_t iter;
char *name;
param_t *param;

params_iterator_init (&iter, resource_ids);
while (params_iterator_next (&iter, &name, &param))
if (param->value && strcmp (param->value, "0"))
g_string_append_printf (command,
"<resource id=\"%s\"/>",
param->value ? param->value : "");
}

xml_string_append (command,
"</resources>"
"</modify_tag>");

response = NULL;
entity = NULL;
switch (gmpf (connection, credentials,
&response,
&entity,
response_data,
"<modify_tag tag_id=\"%s\">"
"<name>%s</name>"
"<comment>%s</comment>"
"<value>%s</value>"
"<resource id=\"%s\">"
"<type>%s</type>"
"</resource>"
"<active>%s</active>"
"</modify_tag>",
tag_id,
name,
comment,
value,
resource_id,
resource_type,
active))
switch (gmp (connection, credentials,
&response,
&entity,
response_data,
command->str))
{
case 0:
case -1:
break;
case 1:
cmd_response_data_set_status_code (response_data,
MHD_HTTP_INTERNAL_SERVER_ERROR);
g_string_free (command, TRUE);
return gsad_message (credentials,
"Internal error", __FUNCTION__, __LINE__,
"An internal error occurred while saving a tag. "
Expand All @@ -9897,6 +9829,7 @@ save_tag_gmp (gvm_connection_t *connection, credentials_t * credentials,
case 2:
cmd_response_data_set_status_code (response_data,
MHD_HTTP_INTERNAL_SERVER_ERROR);
g_string_free (command, TRUE);
return gsad_message (credentials,
"Internal error", __FUNCTION__, __LINE__,
"An internal error occurred while saving a tag. "
Expand All @@ -9908,6 +9841,7 @@ save_tag_gmp (gvm_connection_t *connection, credentials_t * credentials,
default:
cmd_response_data_set_status_code (response_data,
MHD_HTTP_INTERNAL_SERVER_ERROR);
g_string_free (command, TRUE);
return gsad_message (credentials,
"Internal error", __FUNCTION__, __LINE__,
"An internal error occurred while saving a tag. "
Expand All @@ -9917,6 +9851,7 @@ save_tag_gmp (gvm_connection_t *connection, credentials_t * credentials,
response_data);
}

g_string_free (command, TRUE);
ret = response_from_entity (connection, credentials, params, entity,
"Save Tag", response_data);

Expand Down

0 comments on commit ce8c0fd

Please sign in to comment.