From a8ff148c6a5d96dee5873065340ba644b3238730 Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Fri, 16 Aug 2019 18:34:59 +0200 Subject: [PATCH 1/5] Implement report return when starting OSP tasks This gets tasks of scanner type GMP working again. --- src/manage.c | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/src/manage.c b/src/manage.c index 4fe2d8914..aebad2666 100644 --- a/src/manage.c +++ b/src/manage.c @@ -4074,20 +4074,24 @@ launch_osp_openvas_task (task_t task, target_t target, const char *scan_id, * * @param[in] task The task. * @param[in] target The target. + * @param[out] report The report. * * @return Parent returns with 0 if success, -1 if failure. Child process * doesn't return and simply exits. */ static int -fork_osp_scan_handler (task_t task, target_t target) +fork_osp_scan_handler (task_t task, target_t target, char **report_id) { - char *report_id, title[128], *error = NULL; + char *id, title[128], *error = NULL; int rc; assert (task); assert (target); - if (create_current_report (task, &report_id, TASK_STATUS_REQUESTED)) + if (report_id == NULL) + report_id = &id; + + if (create_current_report (task, report_id, TASK_STATUS_REQUESTED)) { g_debug (" %s: failed to create report", __FUNCTION__); return -1; @@ -4101,6 +4105,7 @@ fork_osp_scan_handler (task_t task, target_t target) break; case -1: /* Parent, failed to fork. */ + global_current_report = 0; g_warning ("%s: Failed to fork: %s", __FUNCTION__, strerror (errno)); @@ -4113,6 +4118,7 @@ fork_osp_scan_handler (task_t task, target_t target) return -9; default: /* Parent, successfully forked. */ + global_current_report = 0; return 0; } @@ -4125,18 +4131,18 @@ fork_osp_scan_handler (task_t task, target_t target) if (scanner_type (task_scanner (task)) == SCANNER_TYPE_OPENVAS) { - rc = launch_osp_openvas_task (task, target, report_id, &error); + rc = launch_osp_openvas_task (task, target, *report_id, &error); } else { - rc = launch_osp_task (task, target, report_id, &error); + rc = launch_osp_task (task, target, *report_id, &error); } if (rc) { result_t result; - g_warning ("OSP start_scan %s: %s", report_id, error); + g_warning ("OSP start_scan %s: %s", *report_id, error); result = make_osp_result (task, "", "", "", threat_message_type ("Error"), error, "", "", QOD_DEFAULT); @@ -4147,18 +4153,18 @@ fork_osp_scan_handler (task_t task, target_t target) set_scan_end_time_epoch (global_current_report, time (NULL)); g_free (error); - g_free (report_id); + g_free (*report_id); exit (-1); } set_task_run_status (task, TASK_STATUS_RUNNING); set_report_scan_run_status (global_current_report, TASK_STATUS_RUNNING); - snprintf (title, sizeof (title), "gvmd: OSP: Handling scan %s", report_id); + snprintf (title, sizeof (title), "gvmd: OSP: Handling scan %s", *report_id); proctitle_set (title); - rc = handle_osp_scan (task, global_current_report, report_id); - g_free (report_id); + rc = handle_osp_scan (task, global_current_report, *report_id); + g_free (*report_id); if (rc == 0) { set_task_run_status (task, TASK_STATUS_DONE); @@ -4181,11 +4187,12 @@ fork_osp_scan_handler (task_t task, target_t target) * @brief Start a task on an OSP or OpenVAS via OSP scanner. * * @param[in] task The task. + * @param[out] report_id The report ID. * * @return 0 success, 99 permission denied, -1 error. */ static int -run_osp_task (task_t task) +run_osp_task (task_t task, char **report_id) { target_t target; @@ -4206,7 +4213,7 @@ run_osp_task (task_t task) return 99; } - if (fork_osp_scan_handler (task, target)) + if (fork_osp_scan_handler (task, target, report_id)) { g_warning ("Couldn't fork OSP scan handler"); return -1; @@ -5002,7 +5009,7 @@ run_task (const char *task_id, char **report_id, int from) if (scanner_type (scanner) == SCANNER_TYPE_OPENVAS || scanner_type (scanner) == SCANNER_TYPE_OSP) - return run_osp_task (task); + return run_osp_task (task, report_id); return -1; // Unknown scanner type } From 481b3bde9aa4d21e257b2fcc92b6831ce6e866dc Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Fri, 16 Aug 2019 18:37:11 +0200 Subject: [PATCH 2/5] Update changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fe2064048..0e1c2e807 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,7 +20,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ### Changed - Check if NVT preferences exist before inserting. [#406](https://github.com/greenbone/gvmd/pull/406) - Raise minimum version for SQL functions. [#420](https://github.com/greenbone/gvmd/pull/420) -- Run OpenVAS scans via OSP instead of OTP. [#422](https://github.com/greenbone/gvmd/pull/422) [#584](https://github.com/greenbone/gvmd/pull/584) [#623](https://github.com/greenbone/gvmd/pull/623) [#636](https://github.com/greenbone/gvmd/pull/636) +- Run OpenVAS scans via OSP instead of OTP. [#422](https://github.com/greenbone/gvmd/pull/422) [#584](https://github.com/greenbone/gvmd/pull/584) [#623](https://github.com/greenbone/gvmd/pull/623) [#636](https://github.com/greenbone/gvmd/pull/636) [#704](https://github.com/greenbone/gvmd/pull/704) - Request nvti_cache update only at very end of NVT update. [#426](https://github.com/greenbone/gvmd/pull/426) - Consolidate NVT references into unified "refs" element. [#427](https://github.com/greenbone/gvmd/pull/427) - Update gvm-libs version requirements to v11.0. [#480](https://github.com/greenbone/gvmd/pull/480) From 0c7b26acc2269fcaf8a43791ac37da3585f83100 Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Fri, 16 Aug 2019 20:20:16 +0200 Subject: [PATCH 3/5] Correct doc arg --- src/manage.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/manage.c b/src/manage.c index aebad2666..c9968f25f 100644 --- a/src/manage.c +++ b/src/manage.c @@ -4074,7 +4074,7 @@ launch_osp_openvas_task (task_t task, target_t target, const char *scan_id, * * @param[in] task The task. * @param[in] target The target. - * @param[out] report The report. + * @param[out] report_id UUID of the report. * * @return Parent returns with 0 if success, -1 if failure. Child process * doesn't return and simply exits. From bd7d132a3eeb77c94993aac8bb7cba92c71d46fb Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Tue, 20 Aug 2019 12:33:56 +0200 Subject: [PATCH 4/5] Use more consistent style for return param --- src/manage.c | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/src/manage.c b/src/manage.c index c9968f25f..38fb6475e 100644 --- a/src/manage.c +++ b/src/manage.c @@ -4072,26 +4072,26 @@ launch_osp_openvas_task (task_t task, target_t target, const char *scan_id, /** * @brief Fork a child to handle an OSP scan's fetching and inserting. * - * @param[in] task The task. - * @param[in] target The target. - * @param[out] report_id UUID of the report. + * @param[in] task The task. + * @param[in] target The target. + * @param[out] report_id_return UUID of the report. * * @return Parent returns with 0 if success, -1 if failure. Child process * doesn't return and simply exits. */ static int -fork_osp_scan_handler (task_t task, target_t target, char **report_id) +fork_osp_scan_handler (task_t task, target_t target, char **report_id_return) { - char *id, title[128], *error = NULL; + char *report_id, title[128], *error = NULL; int rc; assert (task); assert (target); - if (report_id == NULL) - report_id = &id; + if (report_id_return) + *report_id_return = NULL; - if (create_current_report (task, report_id, TASK_STATUS_REQUESTED)) + if (create_current_report (task, &report_id, TASK_STATUS_REQUESTED)) { g_debug (" %s: failed to create report", __FUNCTION__); return -1; @@ -4115,10 +4115,13 @@ fork_osp_scan_handler (task_t task, target_t target, char **report_id) set_report_scan_run_status (global_current_report, TASK_STATUS_INTERRUPTED); global_current_report = (report_t) 0; + g_free (report_id); return -9; default: /* Parent, successfully forked. */ global_current_report = 0; + if (report_id_return) + *report_id_return = report_id; return 0; } @@ -4131,18 +4134,18 @@ fork_osp_scan_handler (task_t task, target_t target, char **report_id) if (scanner_type (task_scanner (task)) == SCANNER_TYPE_OPENVAS) { - rc = launch_osp_openvas_task (task, target, *report_id, &error); + rc = launch_osp_openvas_task (task, target, report_id, &error); } else { - rc = launch_osp_task (task, target, *report_id, &error); + rc = launch_osp_task (task, target, report_id, &error); } if (rc) { result_t result; - g_warning ("OSP start_scan %s: %s", *report_id, error); + g_warning ("OSP start_scan %s: %s", report_id, error); result = make_osp_result (task, "", "", "", threat_message_type ("Error"), error, "", "", QOD_DEFAULT); @@ -4153,18 +4156,18 @@ fork_osp_scan_handler (task_t task, target_t target, char **report_id) set_scan_end_time_epoch (global_current_report, time (NULL)); g_free (error); - g_free (*report_id); + g_free (report_id); exit (-1); } set_task_run_status (task, TASK_STATUS_RUNNING); set_report_scan_run_status (global_current_report, TASK_STATUS_RUNNING); - snprintf (title, sizeof (title), "gvmd: OSP: Handling scan %s", *report_id); + snprintf (title, sizeof (title), "gvmd: OSP: Handling scan %s", report_id); proctitle_set (title); - rc = handle_osp_scan (task, global_current_report, *report_id); - g_free (*report_id); + rc = handle_osp_scan (task, global_current_report, report_id); + g_free (report_id); if (rc == 0) { set_task_run_status (task, TASK_STATUS_DONE); From a6ad618d6fe74e056462126f8fe9eb8a29c18f65 Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Tue, 20 Aug 2019 12:40:47 +0200 Subject: [PATCH 5/5] Free report_id if no return is requested --- src/manage.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/manage.c b/src/manage.c index 38fb6475e..9fc6ce006 100644 --- a/src/manage.c +++ b/src/manage.c @@ -4122,6 +4122,8 @@ fork_osp_scan_handler (task_t task, target_t target, char **report_id_return) global_current_report = 0; if (report_id_return) *report_id_return = report_id; + else + g_free (report_id); return 0; }