From 5ecc8f95fe186cfc31467920866fb2ee4d2e634e Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Wed, 17 Mar 2021 15:31:17 +0100 Subject: [PATCH 01/11] Add auto retry on scanner connection lost during a runnning task --- src/manage.c | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/manage.c b/src/manage.c index c7131d2d5..d3217c510 100644 --- a/src/manage.c +++ b/src/manage.c @@ -3562,6 +3562,7 @@ handle_osp_scan (task_t task, report_t report, const char *scan_id) int rc, port; scanner_t scanner; gboolean started, queued_status_updated; + int retry; scanner = task_scanner (task); host = scanner_host (scanner); @@ -3572,7 +3573,8 @@ handle_osp_scan (task_t task, report_t report, const char *scan_id) started = FALSE; queued_status_updated = FALSE; - while (1) + retry = 3; + while (1 && retry >= 0) { int run_status, progress; osp_scan_status_t osp_scan_status; @@ -3585,10 +3587,20 @@ handle_osp_scan (task_t task, report_t report, const char *scan_id) break; } + /* Get only the progress, without results and details. */ progress = get_osp_scan_report (scan_id, host, port, ca_pub, key_pub, key_priv, 0, 0, NULL); + if (progress < 0 || progress > 100) { + if (retry > 0) + { + retry--; + g_warning ("Connection lost with the scanner at %s. " + "Trying again in 1 second.", host); + gvm_sleep (1); + continue; + } result_t result = make_osp_result (task, "", "", "", threat_message_type ("Error"), @@ -3608,6 +3620,15 @@ handle_osp_scan (task_t task, report_t report, const char *scan_id) key_priv, 1, 1, &report_xml); if (progress < 0 || progress > 100) { + if (retry > 0) + { + retry--; + g_warning ("Connection lost with the scanner at %s. " + "Trying again in 1 second.", host); + gvm_sleep (1); + continue; + } + g_free (report_xml); result_t result = make_osp_result (task, "", "", "", @@ -3653,6 +3674,15 @@ handle_osp_scan (task_t task, report_t report, const char *scan_id) else if (progress >= 0 && progress < 100 && osp_scan_status == OSP_SCAN_STATUS_STOPPED) { + if (retry > 0) + { + retry--; + g_warning ("Connection lost with the scanner at %s. " + "Trying again in 1 second.", host); + gvm_sleep (1); + continue; + } + result_t result = make_osp_result (task, "", "", "", threat_message_type ("Error"), @@ -3683,6 +3713,7 @@ handle_osp_scan (task_t task, report_t report, const char *scan_id) } } + retry = 3; gvm_sleep (5); } From 3428320cfe2dcba201ff0f776b7f09e3a84bf356 Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Wed, 17 Mar 2021 15:34:35 +0100 Subject: [PATCH 02/11] Add gvmd option 'scanner-connection-retry' --- src/gvmd.c | 6 ++++++ src/manage.h | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/src/gvmd.c b/src/gvmd.c index bbfcb1d19..f8273e5b6 100644 --- a/src/gvmd.c +++ b/src/gvmd.c @@ -1723,6 +1723,7 @@ gvmd (int argc, char** argv) static gchar *scanner_credential = NULL; static gchar *scanner_key_pub = NULL; static gchar *scanner_key_priv = NULL; + static int scanner_connection_retry = SCANNER_CONNECTION_RETRY_DEFAULT; static int schedule_timeout = SCHEDULE_TIMEOUT_DEFAULT; static int secinfo_commit_size = SECINFO_COMMIT_SIZE_DEFAULT; static int slave_commit_size = SLAVE_COMMIT_SIZE_DEFAULT; @@ -1958,6 +1959,11 @@ gvmd (int argc, char** argv) &scanner_ca_pub, "Scanner CA Certificate path for --[create|modify]-scanner.", "" }, + { "scanner-connection-retry", '\0', 0, G_OPTION_ARG_INT, + &scanner_connection_retry, + "During a running task, number of auto retry on lost connection," + " default: "G_STRINGIFY (SCANNER_CONNECTION_RETRY), + "" }, { "scanner-credential", '\0', 0, G_OPTION_ARG_STRING, &scanner_credential, "Scanner credential for --create-scanner and --modify-scanner." diff --git a/src/manage.h b/src/manage.h index 340da3c90..5bb959037 100644 --- a/src/manage.h +++ b/src/manage.h @@ -2432,6 +2432,11 @@ manage_system_report (const char *, const char *, const char *, const char *, */ #define SLAVE_COMMIT_SIZE_DEFAULT 0 +/** + * @brief Default for max auto retry on connection to scanner lost. + */ +#define SCANNER_CONNECTION_RETRY_DEFAULT 3 + int manage_create_scanner (GSList *, const db_conn_info_t *, const char *, const char *, const char *, const char *, const char *, From b6a11076d0fadc2669e45014f9c6350512bcb3cd Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Wed, 17 Mar 2021 15:36:09 +0100 Subject: [PATCH 03/11] Add functions to get/set scanner_connection_retry --- src/manage.c | 28 ++++++++++++++++++++++++++++ src/manage.h | 6 ++++++ 2 files changed, 34 insertions(+) diff --git a/src/manage.c b/src/manage.c index d3217c510..adee037ce 100644 --- a/src/manage.c +++ b/src/manage.c @@ -185,6 +185,11 @@ static int relay_migrate_sensors = 0; */ static int schedule_timeout = SCHEDULE_TIMEOUT_DEFAULT; +/** + * @brief Default for max auto retry on connection to scanner lost. + */ +static int scanner_connection_retry = SCANNER_CONNECTION_RETRY_DEFAULT; + /* Certificate and key management. */ @@ -4679,6 +4684,29 @@ run_osp_task (task_t task, int from, char **report_id) return 0; } +/** + * @brief Get the number of retry on a scanner connection lost. + * + * @return The number of retry on a scanner connection lost. + */ +int +get_scanner_connection_retry () +{ + return scanner_connection_retry; +} + +/** + * @brief Set the number of retry on a scanner connection lost. + * + * @param new_retry The number of retry on a scanner connection lost. + */ +void +set_scanner_connection_retry (int new_retry) +{ + if (new_retry > 1) + scanner_connection_retry = new_retry; +} + /* CVE tasks. */ diff --git a/src/manage.h b/src/manage.h index 5bb959037..253dd615d 100644 --- a/src/manage.h +++ b/src/manage.h @@ -2604,6 +2604,12 @@ osp_connect_with_data (const char *, osp_connection_t * osp_scanner_connect (scanner_t); +int +get_scanner_connection_retry (); + +void +set_scanner_connection_retry (int); + int verify_scanner (const char *, char **); From 9c5893eb162cef373d3c619d3971b37ceabe1821 Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Wed, 17 Mar 2021 15:38:53 +0100 Subject: [PATCH 04/11] Set scanner_connection_retry at start --- src/gvmd.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/gvmd.c b/src/gvmd.c index f8273e5b6..a0ccae473 100644 --- a/src/gvmd.c +++ b/src/gvmd.c @@ -2088,6 +2088,9 @@ gvmd (int argc, char** argv) set_schedule_timeout (schedule_timeout); + /* Set the connection auto retry */ + set_scanner_connection_retry (scanner_connection_retry); + /* Set slave commit size */ set_slave_commit_size (slave_commit_size); From 0c96d264b6b0f54278d48fc187c09af8011f3abd Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Wed, 17 Mar 2021 15:54:18 +0100 Subject: [PATCH 05/11] Use scanner_connection_retry for running task --- src/manage.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/manage.c b/src/manage.c index adee037ce..726912e20 100644 --- a/src/manage.c +++ b/src/manage.c @@ -3567,7 +3567,7 @@ handle_osp_scan (task_t task, report_t report, const char *scan_id) int rc, port; scanner_t scanner; gboolean started, queued_status_updated; - int retry; + int retry, connection_retry; scanner = task_scanner (task); host = scanner_host (scanner); @@ -3577,8 +3577,9 @@ handle_osp_scan (task_t task, report_t report, const char *scan_id) key_priv = scanner_key_priv (scanner); started = FALSE; queued_status_updated = FALSE; + connection_retry = get_scanner_connection_retry (); - retry = 3; + retry = connection_retry; while (1 && retry >= 0) { int run_status, progress; @@ -3718,7 +3719,7 @@ handle_osp_scan (task_t task, report_t report, const char *scan_id) } } - retry = 3; + retry = connection_retry; gvm_sleep (5); } From 09c51a027dd123cea41ba51872637d883113b7eb Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Wed, 17 Mar 2021 16:25:18 +0100 Subject: [PATCH 06/11] Allow to disable the auto retry --- src/manage.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/manage.c b/src/manage.c index 726912e20..1cbdb0794 100644 --- a/src/manage.c +++ b/src/manage.c @@ -4704,7 +4704,7 @@ get_scanner_connection_retry () void set_scanner_connection_retry (int new_retry) { - if (new_retry > 1) + if (new_retry >= 0) scanner_connection_retry = new_retry; } From 5fb7f4d382dec4e427ba1d49a8c8595ecc98d40a Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Wed, 17 Mar 2021 16:37:24 +0100 Subject: [PATCH 07/11] Update man page --- doc/gvmd.8.xml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/doc/gvmd.8.xml b/doc/gvmd.8.xml index 4c8721fba..8631a6c50 100644 --- a/doc/gvmd.8.xml +++ b/doc/gvmd.8.xml @@ -382,6 +382,13 @@ along with this program. If not, see . as used in GMP.

+