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 gmp_start_task_ext_c #327

Merged
merged 2 commits into from
Apr 30, 2020
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Add support for test_alive_hosts_only feature of openvas. [#320](https://github.com/greenbone/gvm-libs/pull/320)
- Add function to set and get the NVT QoD. [#321](https://github.com/greenbone/gvm-libs/pull/321)
- Add unit tests for networking.c port list functions. [#325](https://github.com/greenbone/gvm-libs/pull/325)
- Add gmp_start_task_ext_c. [#327](https://github.com/greenbone/gvm-libs/pull/327)

### Fixed
- Fix is_cidr_block(). [#322](https://github.com/greenbone/gvm-libs/pull/322)
Expand Down
115 changes: 86 additions & 29 deletions gmp/gmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,78 @@ gmp_check_response (gnutls_session_t *session, entity_t *entity)
return ret;
}

/**
* @brief Read response and convert status of response to a return value.
*
* @param[in] connection Connection.
* @param[in] convert_99 If true, return will be 99 if response was 400 with
* status_text "Permission Denied".
*
* @return 0 on success, 99 if convert_99 and permission denied, -1 or GMP
* response code on error.
*/
static int
check_response_c (gvm_connection_t *connection, int convert_99)
{
int ret;
const char *status;
entity_t entity;

/* Read the response. */

entity = NULL;
if (read_entity_c (connection, &entity))
return -1;

/* Check the response. */

status = entity_attribute (entity, "status");
if (status == NULL)
{
free_entity (entity);
return -1;
}
if (strlen (status) == 0)
{
free_entity (entity);
return -1;
}
if (status[0] == '2')
{
free_entity (entity);
return 0;
}
ret = (int) strtol (status, NULL, 10);
if (errno == ERANGE)
{
free_entity (entity);
return -1;
}

if (convert_99
&& entity_attribute (entity, "status_text")
&& strcasecmp (entity_attribute (entity, "status_text"),
"Permission denied")
== 0)
ret = 99;

free_entity (entity);
return ret;
}

/**
* @brief Read response and convert status of response to a return value.
*
* @param[in] connection Connection.
*
* @return 0 on success, -1 or GMP response code on error.
*/
int
gmp_check_response_c (gvm_connection_t *connection)
{
return check_response_c (connection, 0);
}

/**
* @brief "Ping" the manager.
*
Expand Down Expand Up @@ -791,48 +863,33 @@ gmp_start_task_report_c (gvm_connection_t *connection, const char *task_id,
}

/**
* @brief Read response and convert status of response to a return value.
* @brief Start a task and read the manager response.
*
* @param[in] connection Connection.
* @param[in] opts Options to apply.
*
* @return 0 on success, -1 or GMP response code on error.
* @return 0 on success, 99 permission denied, -1 or GMP response code on error.
*/
int
gmp_check_response_c (gvm_connection_t *connection)
gmp_start_task_ext_c (gvm_connection_t *connection, gmp_start_task_opts_t opts)
{
int ret;
const char *status;
entity_t entity;

/* Read the response. */
/* Check args. */

entity = NULL;
if (read_entity_c (connection, &entity))
if (opts.task_id == NULL)
return -1;

/* Check the response. */
/* Send request. */

status = entity_attribute (entity, "status");
if (status == NULL)
{
free_entity (entity);
return -1;
}
if (strlen (status) == 0)
{
free_entity (entity);
return -1;
}
if (status[0] == '2')
{
free_entity (entity);
return 0;
}
ret = (int) strtol (status, NULL, 10);
free_entity (entity);
if (errno == ERANGE)
ret = gvm_connection_sendf (connection, "<start_task task_id=\"%s\"/>",
opts.task_id);
if (ret)
return -1;
return ret;

/* Read response. */

return check_response_c (connection, 1);
}

/**
Expand Down
13 changes: 13 additions & 0 deletions gmp/gmp.h
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,19 @@ gmp_start_task_report (gnutls_session_t *, const char *, char **);
int
gmp_start_task_report_c (gvm_connection_t *, const char *, char **);

/**
* @brief Struct holding options for gmp start_task command.
*/
typedef struct
{
const char *task_id; ///< ID of task.
} gmp_start_task_opts_t;

static const gmp_start_task_opts_t gmp_start_task_opts_defaults = { NULL };

int
gmp_start_task_ext_c (gvm_connection_t *, gmp_start_task_opts_t);

int
gmp_stop_task (gnutls_session_t *, const char *);

Expand Down