From 3518ff03787572fb695e4fdc4ba5c3af40b7d9b4 Mon Sep 17 00:00:00 2001 From: Timo Pollmeier Date: Fri, 10 Nov 2023 14:21:08 +0100 Subject: [PATCH] Change: New error message for locked reports table If a command fails because the lock on the reports table could not be acquired before the timeout, the GMP response will mention this instead of returning a generic internal error. --- src/gmp.c | 84 +++++++++++++++++++++++++++++++++++++++++++++++- src/manage_sql.c | 19 ++++++----- 2 files changed, 94 insertions(+), 9 deletions(-) diff --git a/src/gmp.c b/src/gmp.c index b1a6d32b84..731d520c54 100644 --- a/src/gmp.c +++ b/src/gmp.c @@ -18618,7 +18618,70 @@ gmp_xml_handle_end_element (/* unused */ GMarkupParseContext* context, CASE_DELETE (PERMISSION, permission, "Permission"); CASE_DELETE (PORT_LIST, port_list, "Port list"); CASE_DELETE (PORT_RANGE, port_range, "Port range"); - CASE_DELETE (REPORT, report, "Report"); + + case CLIENT_DELETE_REPORT: + if (delete_report_data->report_id) + { + switch (delete_report (delete_report_data->report_id, + delete_report_data->ultimate)) + { + case 0: /* Deleted. */ + SEND_TO_CLIENT_OR_FAIL (XML_OK ("delete_report")); + log_event ("report", "Report", + delete_report_data->report_id, + "deleted"); + break; + case 2: /* Failed to find report. */ + if (send_find_error_to_client + ("delete_report", "report", + delete_report_data->report_id, + gmp_parser)) + { + error_send_to_client (error); + return; + } + log_event_fail ("report", "Report", + delete_report_data->report_id, + "deleted"); + break; + case 3: + SENDF_TO_CLIENT_OR_FAIL( + "", + STATUS_ERROR_BUSY, + "Reports database is busy. Please try again later."); + log_event_fail ("report", "Report", + delete_report_data->report_id, + "deleted"); + break; + case 99: + SEND_TO_CLIENT_OR_FAIL + (XML_ERROR_SYNTAX ("delete_report", + "Permission denied")); + log_event_fail ("report", "Report", + delete_report_data->report_id, + "deleted"); + break; + case -1: + SEND_TO_CLIENT_OR_FAIL + (XML_INTERNAL_ERROR ("delete_report")); + log_event_fail ("report", "Report", + delete_report_data->report_id, + "deleted"); + break; + default: /* Programming error. */ + assert (0); + } + } + else + SEND_TO_CLIENT_OR_FAIL + (XML_ERROR_SYNTAX ("delete_report", + "A report_id attribute is required")); + delete_report_data_reset (delete_report_data); + set_client_state (CLIENT_AUTHENTIC); + break; + CASE_DELETE (REPORT_FORMAT, report_format, "Report format"); CASE_DELETE (ROLE, role, "Role"); CASE_DELETE (SCANNER, scanner, "Scanner"); @@ -18658,6 +18721,16 @@ gmp_xml_handle_end_element (/* unused */ GMarkupParseContext* context, return; } break; + case 4: + SENDF_TO_CLIENT_OR_FAIL( + "", + STATUS_ERROR_BUSY, + "Reports database is busy. Please try again later."); + log_event_fail ("task", "Task", delete_task_data->task_id, + "deleted"); + break; case 99: SEND_TO_CLIENT_OR_FAIL (XML_ERROR_SYNTAX ("delete_task", @@ -21928,6 +22001,15 @@ gmp_xml_handle_end_element (/* unused */ GMarkupParseContext* context, } log_event_fail ("task", "Task", NULL, "created"); break; + case 3: + SENDF_TO_CLIENT_OR_FAIL( + "", + STATUS_ERROR_BUSY, + "Reports database is busy. Please try again later."); + log_event_fail ("task", "Task", NULL, "created"); + break; case 99: SEND_TO_CLIENT_OR_FAIL (XML_ERROR_SYNTAX ("create_task", diff --git a/src/manage_sql.c b/src/manage_sql.c index 2c9a2056ac..9cf3b28100 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -25480,7 +25480,8 @@ delete_report_internal (report_t report) * @param[in] report_id UUID of report. * @param[in] dummy Dummy arg to match other delete functions. * - * @return 0 success, 2 failed to find report, 99 permission denied, -1 error. + * @return 0 success, 2 failed to find report, 3 reports table is locked, + * 99 permission denied, -1 error. */ int delete_report (const char *report_id, int dummy) @@ -25506,7 +25507,7 @@ delete_report (const char *report_id, int dummy) if (lock_ret == 0) { sql_rollback (); - return -1; + return 3; } if (acl_user_may ("delete_report") == 0) @@ -31214,7 +31215,8 @@ copy_task (const char* name, const char* comment, const char *task_id, * @param[in] task The task. * @param[in] ultimate Whether to remove entirely, or to trashcan. * - * @return 0 on success, 1 if task is hidden, -1 on error. + * @return 0 on success, 1 if task is hidden, 2 if reports table is locked, + * -1 on error. */ static int delete_task_lock (task_t task, int ultimate) @@ -31241,13 +31243,13 @@ delete_task_lock (task_t task, int ultimate) if (lock_ret == 0) { sql_rollback (); - return -1; + return 2; } if (sql_int ("SELECT hidden FROM tasks WHERE id = %llu;", task)) { sql_rollback (); - return -1; + return 1; } ret = delete_task (task, ultimate); @@ -31268,6 +31270,7 @@ delete_task_lock (task_t task, int ultimate) * @param[in] task_pointer A pointer to the task. * * @return 0 if deleted, 1 if delete requested, 2 if task is hidden, + * 3 if reports table is locked, * -1 if error, -5 if scanner is down. */ int @@ -31320,8 +31323,8 @@ request_delete_task (task_t* task_pointer) * @param[in] ultimate Whether to remove entirely, or to trashcan. * * @return 0 deleted, 1 delete requested, 2 task is hidden, 3 failed to find - * task, 99 permission denied, -1 error, -5 scanner is down, -7 no CA - * cert. + * task, 4 reports table locked, 99 permission denied, + * -1 error, -5 scanner is down, -7 no CA cert. */ int request_delete_task_uuid (const char *task_id, int ultimate) @@ -31420,7 +31423,7 @@ request_delete_task_uuid (const char *task_id, int ultimate) if (lock_ret == 0) { sql_rollback (); - return -1; + return 4; } }