Skip to content

Commit

Permalink
Change: New error message for locked reports table
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
timopollmeier committed Nov 10, 2023
1 parent 3309a1f commit 3518ff0
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 9 deletions.
84 changes: 83 additions & 1 deletion src/gmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(
"<delete_report_response"
" status=\"%s\""
" status_text=\"%s\"/>",
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");
Expand Down Expand Up @@ -18658,6 +18721,16 @@ gmp_xml_handle_end_element (/* unused */ GMarkupParseContext* context,
return;
}
break;
case 4:
SENDF_TO_CLIENT_OR_FAIL(
"<delete_task_response"
" status=\"%s\""
" status_text=\"%s\"/>",
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",
Expand Down Expand Up @@ -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(
"<create_task_response"
" status=\"%s\""
" status_text=\"%s\"/>",
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",
Expand Down
19 changes: 11 additions & 8 deletions src/manage_sql.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -31420,7 +31423,7 @@ request_delete_task_uuid (const char *task_id, int ultimate)
if (lock_ret == 0)
{
sql_rollback ();
return -1;
return 4;
}
}

Expand Down

0 comments on commit 3518ff0

Please sign in to comment.