From 9f98c7e5bca0a3678f5c402a611f49b2b1bc67e6 Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Thu, 22 Aug 2019 15:44:45 +0200 Subject: [PATCH 1/7] Add function to get an osp scan status. --- osp/osp.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ osp/osp.h | 3 +++ 2 files changed, 53 insertions(+) diff --git a/osp/osp.c b/osp/osp.c index 9f4f1d294..3521288af 100644 --- a/osp/osp.c +++ b/osp/osp.c @@ -444,6 +444,56 @@ osp_delete_scan (osp_connection_t *connection, const char *scan_id) return ret; } + +/** + * @brief Get a scan status from an OSP server + * + * @param[in] connection Connection to an OSP server. + * @param[in] scan_id ID of scan to get. + * @param[out] status Pointer to status. + * @param[out] error Pointer to error, if any. + * + * @return 0 on success, -1 if error. + */ +int +osp_get_scan_status (osp_connection_t *connection, const char *scan_id, + char **status, char **error) +{ + entity_t entity, child; + int rc; + + assert (connection); + assert (scan_id); + rc = osp_send_command (connection, &entity, + "", + scan_id); + + if (rc) + { + if (error) + *error = g_strdup ("Couldn't send get_scans command to scanner"); + return -1; + } + + child = entity_child (entity, "scan"); + if (!child) + { + const char *text = entity_attribute (entity, "status_text"); + + assert (text); + if (error) + *error = g_strdup (text); + free_entity (entity); + return -1; + } + if (status) + *status = g_strdup (entity_attribute (child, "status")); + free_entity (entity); + return 0; +} + /** * @brief Get a scan from an OSP server, optionally removing the results. * diff --git a/osp/osp.h b/osp/osp.h index f71c6f785..3e2817d3a 100644 --- a/osp/osp.h +++ b/osp/osp.h @@ -111,6 +111,9 @@ osp_get_scan_pop (osp_connection_t *, int, char **); +int +osp_get_scan_status (osp_connection_t *, const char *, char **, char **); + int osp_delete_scan (osp_connection_t *, const char *); From 025d75d29c23b8f1d7640eb91e17e8ad9e336f56 Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Fri, 23 Aug 2019 09:35:48 +0200 Subject: [PATCH 2/7] Add enum type for osp scan status --- osp/osp.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/osp/osp.h b/osp/osp.h index 3e2817d3a..558f93999 100644 --- a/osp/osp.h +++ b/osp/osp.h @@ -55,6 +55,18 @@ typedef enum OSP_PARAM_TYPE_CRD_UP, /**< Credential user/pass type. */ } osp_param_type_t; +/** + * @brief OSP scan status. + */ +typedef enum +{ + OSP_SCAN_STATUS_ERROR = -1, + OSP_SCAN_STATUS_INIT, + OSP_SCAN_STATUS_RUNNING, + OSP_SCAN_STATUS_STOPPED, + OSP_SCAN_STATUS_FINISHED, +} osp_scan_status_t; + typedef struct osp_param osp_param_t; /* OSP Connection handling */ From 1c9ba93b93f55d39a70b19ab5f9bf1b04451c0de Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Fri, 23 Aug 2019 09:39:41 +0200 Subject: [PATCH 3/7] Use the osp_scan_status_t in osp_get_scan_status(). --- osp/osp.c | 26 +++++++++++++++++--------- osp/osp.h | 4 ++-- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/osp/osp.c b/osp/osp.c index 3521288af..8106ec0f0 100644 --- a/osp/osp.c +++ b/osp/osp.c @@ -450,17 +450,17 @@ osp_delete_scan (osp_connection_t *connection, const char *scan_id) * * @param[in] connection Connection to an OSP server. * @param[in] scan_id ID of scan to get. - * @param[out] status Pointer to status. * @param[out] error Pointer to error, if any. * - * @return 0 on success, -1 if error. + * @return Osp scan status */ -int +osp_scan_status_t osp_get_scan_status (osp_connection_t *connection, const char *scan_id, - char **status, char **error) + char **error) { entity_t entity, child; int rc; + osp_scan_status_t status = OSP_SCAN_STATUS_ERROR; assert (connection); assert (scan_id); @@ -474,7 +474,7 @@ osp_get_scan_status (osp_connection_t *connection, const char *scan_id, { if (error) *error = g_strdup ("Couldn't send get_scans command to scanner"); - return -1; + return status; } child = entity_child (entity, "scan"); @@ -486,12 +486,20 @@ osp_get_scan_status (osp_connection_t *connection, const char *scan_id, if (error) *error = g_strdup (text); free_entity (entity); - return -1; + return status; } - if (status) - *status = g_strdup (entity_attribute (child, "status")); + + if (!strcmp (entity_attribute (child, "status"), "init")) + status = OSP_SCAN_STATUS_INIT; + else if (!strcmp (entity_attribute (child, "status"), "running")) + status = OSP_SCAN_STATUS_RUNNING; + else if (!strcmp (entity_attribute (child, "status"), "stopped")) + status = OSP_SCAN_STATUS_STOPPED; + else if (!strcmp (entity_attribute (child, "status"), "finished")) + status = OSP_SCAN_STATUS_FINISHED; + free_entity (entity); - return 0; + return status; } /** diff --git a/osp/osp.h b/osp/osp.h index 558f93999..617390eb9 100644 --- a/osp/osp.h +++ b/osp/osp.h @@ -123,8 +123,8 @@ osp_get_scan_pop (osp_connection_t *, int, char **); -int -osp_get_scan_status (osp_connection_t *, const char *, char **, char **); +osp_scan_status_t +osp_get_scan_status (osp_connection_t *, const char *, char **); int osp_delete_scan (osp_connection_t *, const char *); From 933a2768444be53a628a183cf8832a4ec18ce91c Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Fri, 23 Aug 2019 09:42:55 +0200 Subject: [PATCH 4/7] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 178123cde..0e549e79d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ### Added - Allow to configure the path to the redis socket via CMake [#256](https://github.com/greenbone/gvm-libs/pull/256) - A new data model for unified handling of cross references in the NVT meta data as been added. All previous API elements to handle cve, bid, xref werehas been removed. [#225](https://github.com/greenbone/gvm-libs/pull/225) [#232](https://github.com/greenbone/gvm-libs/pull/232). - +- A function to get an osp scan status and a enum type for the different status [#259](https://github.com/greenbone/gvm-libs/pull/259) ### Changed - Change the default path to the redis socket to /run/redis/redis.sock [#256](https://github.com/greenbone/gvm-libs/pull/256) - Handle EAI_AGAIN in gvm_host_reverse_lookup() IPv6 case and function refactor. [#229](https://github.com/greenbone/gvm-libs/pull/229) From 6bc6dd4545e9476bf8e42d3ae4d6157893713772 Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Fri, 23 Aug 2019 10:48:56 +0200 Subject: [PATCH 5/7] Improve docstrings for osp_scan_status_t. --- osp/osp.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/osp/osp.h b/osp/osp.h index 617390eb9..d67f9a47e 100644 --- a/osp/osp.h +++ b/osp/osp.h @@ -60,11 +60,11 @@ typedef enum */ typedef enum { - OSP_SCAN_STATUS_ERROR = -1, - OSP_SCAN_STATUS_INIT, - OSP_SCAN_STATUS_RUNNING, - OSP_SCAN_STATUS_STOPPED, - OSP_SCAN_STATUS_FINISHED, + OSP_SCAN_STATUS_ERROR = -1, /**< Erros status. */ + OSP_SCAN_STATUS_INIT, /**< Init status. */ + OSP_SCAN_STATUS_RUNNING, /**< Running status. */ + OSP_SCAN_STATUS_STOPPED, /**< Stopped status. */ + OSP_SCAN_STATUS_FINISHED, /**< Finished status. */ } osp_scan_status_t; typedef struct osp_param osp_param_t; From 6e9e432fde15b26aa444dd37759b62f100462916 Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Mon, 26 Aug 2019 12:27:52 +0200 Subject: [PATCH 6/7] Fix typo --- osp/osp.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/osp/osp.h b/osp/osp.h index d67f9a47e..750d1fefc 100644 --- a/osp/osp.h +++ b/osp/osp.h @@ -60,7 +60,7 @@ typedef enum */ typedef enum { - OSP_SCAN_STATUS_ERROR = -1, /**< Erros status. */ + OSP_SCAN_STATUS_ERROR = -1, /**< Error status. */ OSP_SCAN_STATUS_INIT, /**< Init status. */ OSP_SCAN_STATUS_RUNNING, /**< Running status. */ OSP_SCAN_STATUS_STOPPED, /**< Stopped status. */ From d2884d62af26b06b89167d16c9ce3ffe64a5c47f Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Mon, 26 Aug 2019 13:09:50 +0200 Subject: [PATCH 7/7] Use _opts_t style interface. --- osp/osp.c | 8 ++++---- osp/osp.h | 8 +++++++- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/osp/osp.c b/osp/osp.c index 8106ec0f0..e6d765a71 100644 --- a/osp/osp.c +++ b/osp/osp.c @@ -455,20 +455,20 @@ osp_delete_scan (osp_connection_t *connection, const char *scan_id) * @return Osp scan status */ osp_scan_status_t -osp_get_scan_status (osp_connection_t *connection, const char *scan_id, - char **error) +osp_get_scan_status_ext (osp_connection_t *connection, + osp_get_scan_status_opts_t opts, char **error) { entity_t entity, child; int rc; osp_scan_status_t status = OSP_SCAN_STATUS_ERROR; assert (connection); - assert (scan_id); + assert (opts.scan_id); rc = osp_send_command (connection, &entity, "", - scan_id); + opts.scan_id); if (rc) { diff --git a/osp/osp.h b/osp/osp.h index 750d1fefc..f50469309 100644 --- a/osp/osp.h +++ b/osp/osp.h @@ -123,8 +123,14 @@ osp_get_scan_pop (osp_connection_t *, int, char **); +typedef struct { + const char *scan_id; ///< UUID of the scan which get the status from. +} osp_get_scan_status_opts_t; + osp_scan_status_t -osp_get_scan_status (osp_connection_t *, const char *, char **); +osp_get_scan_status_ext (osp_connection_t *, + osp_get_scan_status_opts_t, + char **); int osp_delete_scan (osp_connection_t *, const char *);