From 5cec6e3620df4170f5f81740d97e33dc75a91f03 Mon Sep 17 00:00:00 2001 From: Timo Pollmeier Date: Fri, 26 Apr 2019 17:55:11 +0200 Subject: [PATCH 1/2] Always return long task schedule XML if available If the schedule of a task is available, GET_TASKS will always return the long schedule XML, not just if only the schedules are requested. --- src/gmp.c | 307 +++++++++++++++++++++++++----------------------------- 1 file changed, 143 insertions(+), 164 deletions(-) diff --git a/src/gmp.c b/src/gmp.c index 76c6ba3bf..e9aeee70a 100644 --- a/src/gmp.c +++ b/src/gmp.c @@ -18557,6 +18557,137 @@ handle_get_targets (gmp_parser_t *gmp_parser, GError **error) set_client_state (CLIENT_AUTHENTIC); } +/** + * @brief Gets task schedule data of a task as XML. + * + * @param[in] task The task to get schedule data for. + * + * @return Newly allocated XML string. + */ +static gchar* +get_task_schedule_xml (task_t task) +{ + schedule_t schedule; + time_t next_time; + int schedule_in_trash, schedule_available; + char *task_schedule_uuid, *task_schedule_name; + GString *xml; + + xml = g_string_new (""); + + schedule_available = 1; + schedule = task_schedule (task); + if (schedule) + { + schedule_in_trash = task_schedule_in_trash (task); + if (schedule_in_trash) + { + task_schedule_uuid = schedule_uuid (schedule); + task_schedule_name = schedule_name (schedule); + schedule_available = trash_schedule_readable (schedule); + } + else + { + schedule_t found; + task_schedule_uuid = schedule_uuid (schedule); + task_schedule_name = schedule_name (schedule); + if (find_schedule_with_permission (task_schedule_uuid, + &found, + "get_schedules")) + g_error ("%s: GET_TASKS: error finding" + " task schedule, aborting", + __FUNCTION__); + schedule_available = (found > 0); + } + } + else + { + task_schedule_uuid = (char*) g_strdup (""); + task_schedule_name = (char*) g_strdup (""); + schedule_in_trash = 0; + } + + if (schedule_available && schedule) + { + time_t first_time, info_next_time; + int period, period_months, duration; + gchar *icalendar, *zone; + + icalendar = zone = NULL; + + if (schedule_info (schedule, schedule_in_trash, + &first_time, &info_next_time, &period, + &period_months, &duration, + &icalendar, &zone) == 0) + { + gchar *first_time_str, *next_time_str; + + // Copy ISO time strings to avoid one overwriting the other + first_time_str = g_strdup (first_time + ? iso_time (&first_time) + : ""); + next_time_str = g_strdup (info_next_time + ? iso_time (&info_next_time) + : "over"); + + xml_string_append (xml, + "" + "%s" + "%d" + "%s" + "%s" + "%s" + "%d" + "" + "%d" + "" + "%d" + "%s" + "" + "" + "%d" + "", + task_schedule_uuid, + task_schedule_name, + schedule_in_trash, + first_time_str, + next_time_str, + icalendar ? icalendar : "", + period, + period_months, + duration, + zone ? zone : "", + task_schedule_periods (task)); + + g_free (first_time_str); + g_free (next_time_str); + } + + g_free (icalendar); + g_free (zone); + } + else + { + next_time = task_schedule_next_time (task); + + xml_string_append (xml, + "" + "%s" + "%s" + "%d" + "", + task_schedule_uuid, + task_schedule_name, + next_time + ? iso_time (&next_time) + : "over", + schedule_in_trash); + } + + return g_string_free (xml, FALSE); +} + + /** * @brief Handle end of GET_TASKS element. * @@ -18654,22 +18785,19 @@ handle_get_tasks (gmp_parser_t *gmp_parser, GError **error) gchar *config_name_escaped; char *task_target_uuid, *task_target_name; gchar *task_target_name_escaped; - char *task_schedule_uuid, *task_schedule_name; - gchar *task_schedule_name_escaped; + gchar *task_schedule_xml; char *task_scanner_uuid, *task_scanner_name; gchar *task_scanner_name_escaped; gchar *first_report, *last_report; gchar *second_last_report_id, *second_last_report; gchar *current_report; report_t running_report; - schedule_t schedule; - time_t next_time; char *owner, *observers; - int target_in_trash, schedule_in_trash, scanner_in_trash; + int target_in_trash, scanner_in_trash; int debugs, holes = 0, infos = 0, logs, warnings = 0; int holes_2 = 0, infos_2 = 0, warnings_2 = 0; int false_positives, task_scanner_type; - int schedule_available, target_available, config_available; + int target_available, config_available; int scanner_available; double severity = 0, severity_2 = 0; gchar *response; @@ -18691,6 +18819,8 @@ handle_get_tasks (gmp_parser_t *gmp_parser, GError **error) index = get_iterator_resource (&tasks); target = task_target (index); + task_schedule_xml = get_task_schedule_xml (index); + if (get_tasks_data->schedules_only) { SENDF_TO_CLIENT_OR_FAIL ("" @@ -18698,112 +18828,8 @@ handle_get_tasks (gmp_parser_t *gmp_parser, GError **error) get_iterator_uuid (&tasks), get_iterator_name (&tasks)); - schedule_available = 1; - schedule = task_schedule (index); - if (schedule) - { - schedule_in_trash = task_schedule_in_trash (index); - if (schedule_in_trash) - { - task_schedule_uuid = schedule_uuid (schedule); - task_schedule_name = schedule_name (schedule); - schedule_available = trash_schedule_readable (schedule); - } - else - { - schedule_t found; - task_schedule_uuid = schedule_uuid (schedule); - task_schedule_name = schedule_name (schedule); - if (find_schedule_with_permission (task_schedule_uuid, - &found, - "get_schedules")) - g_error ("%s: GET_TASKS: error finding" - " task schedule, aborting", - __FUNCTION__); - schedule_available = (found > 0); - } - } - else - { - task_schedule_uuid = (char*) g_strdup (""); - task_schedule_name = (char*) g_strdup (""); - schedule_in_trash = 0; - } - - if (schedule_available && schedule) - { - time_t first_time, info_next_time; - int period, period_months, duration; - gchar *icalendar, *zone; - - icalendar = zone = NULL; - - if (schedule_info (schedule, schedule_in_trash, - &first_time, &info_next_time, &period, - &period_months, &duration, - &icalendar, &zone) == 0) - { - gchar *first_time_str, *next_time_str; - - // Copy ISO time strings to avoid one overwriting the other - first_time_str = g_strdup (first_time - ? iso_time (&first_time) - : ""); - next_time_str = g_strdup (info_next_time - ? iso_time (&info_next_time) - : "over"); - - SENDF_TO_CLIENT_OR_FAIL ("" - "%s" - "%d" - "%s" - "%s" - "%s" - "%d" - "" - "%d" - "" - "%d" - "%s" - "" - "" - "%d" - "", - task_schedule_uuid, - task_schedule_name, - schedule_in_trash, - first_time_str, - next_time_str, - icalendar ? icalendar : "", - period, - period_months, - duration, - zone ? zone : "", - task_schedule_periods (index)); - - g_free (first_time_str); - g_free (next_time_str); - } - - g_free (icalendar); - g_free (zone); - } - else - { - next_time = task_schedule_next_time (index); - - SENDF_TO_CLIENT_OR_FAIL ("" - "%s" - "%s" - "%d" - "", - task_schedule_uuid, - task_schedule_name, - next_time - ? iso_time (&next_time) - : "over", - schedule_in_trash); - } + SEND_TO_CLIENT_OR_FAIL (task_schedule_xml); + g_free (task_schedule_xml); SENDF_TO_CLIENT_OR_FAIL (""); @@ -19162,37 +19188,6 @@ handle_get_tasks (gmp_parser_t *gmp_parser, GError **error) __FUNCTION__); config_available = (found > 0); } - schedule_available = 1; - schedule = task_schedule (index); - if (schedule) - { - schedule_in_trash = task_schedule_in_trash (index); - if (schedule_in_trash) - { - task_schedule_uuid = schedule_uuid (schedule); - task_schedule_name = schedule_name (schedule); - schedule_available = trash_schedule_readable (schedule); - } - else - { - schedule_t found; - task_schedule_uuid = schedule_uuid (schedule); - task_schedule_name = schedule_name (schedule); - if (find_schedule_with_permission (task_schedule_uuid, - &found, - "get_schedules")) - g_error ("%s: GET_TASKS: error finding" - " task schedule, aborting", - __FUNCTION__); - schedule_available = (found > 0); - } - } - else - { - task_schedule_uuid = (char*) g_strdup (""); - task_schedule_name = (char*) g_strdup (""); - schedule_in_trash = 0; - } scanner_available = 1; scanner = task_iterator_scanner (&tasks); if (scanner) @@ -19224,7 +19219,7 @@ handle_get_tasks (gmp_parser_t *gmp_parser, GError **error) task_scanner_type = 0; scanner_in_trash = 0; } - next_time = task_schedule_next_time (index); + config_name_escaped = config_name ? g_markup_escape_text (config_name, -1) @@ -19237,10 +19232,7 @@ handle_get_tasks (gmp_parser_t *gmp_parser, GError **error) = task_scanner_name ? g_markup_escape_text (task_scanner_name, -1) : NULL; - task_schedule_name_escaped - = task_schedule_name - ? g_markup_escape_text (task_schedule_name, -1) - : NULL; + response = g_strdup_printf ("%i" "" @@ -19267,13 +19259,7 @@ handle_get_tasks (gmp_parser_t *gmp_parser, GError **error) "%u%u" "" "%s" - "" - "%s" - "%s" - "%i" - "%s" - "" - "%i" + "%s" // Schedule XML "%s%s%s%s", get_tasks_data->get.trash ? 0 @@ -19302,12 +19288,7 @@ handle_get_tasks (gmp_parser_t *gmp_parser, GError **error) : task_iterator_trend_counts (&tasks, holes, warnings, infos, severity, holes_2, warnings_2, infos_2, severity_2), - task_schedule_uuid, - task_schedule_name_escaped, - (next_time == 0 ? "over" : iso_time (&next_time)), - schedule_in_trash, - schedule_available ? "" : "", - task_schedule_periods (index), + task_schedule_xml, current_report, first_report, last_report, @@ -19323,9 +19304,7 @@ handle_get_tasks (gmp_parser_t *gmp_parser, GError **error) g_free (first_report); g_free (last_report); g_free (second_last_report); - g_free (task_schedule_uuid); - g_free (task_schedule_name); - g_free (task_schedule_name_escaped); + g_free (task_schedule_xml); g_free (task_scanner_uuid); g_free (task_scanner_name); g_free (task_scanner_name_escaped); From b93aa8bf0b2ed6e6505df1c1acfe41a37a961bae Mon Sep 17 00:00:00 2001 From: Timo Pollmeier Date: Fri, 26 Apr 2019 18:02:19 +0200 Subject: [PATCH 2/2] Get correct name, UUID of task schedules in trash GET_TASKS previously queried the wrong table for the name and UUID of the schedule if it was in the trashcan. --- src/gmp.c | 4 ++-- src/manage.h | 4 ++++ src/manage_sql.c | 28 ++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/gmp.c b/src/gmp.c index e9aeee70a..67fbba6cf 100644 --- a/src/gmp.c +++ b/src/gmp.c @@ -18582,8 +18582,8 @@ get_task_schedule_xml (task_t task) schedule_in_trash = task_schedule_in_trash (task); if (schedule_in_trash) { - task_schedule_uuid = schedule_uuid (schedule); - task_schedule_name = schedule_name (schedule); + task_schedule_uuid = trash_schedule_uuid (schedule); + task_schedule_name = trash_schedule_name (schedule); schedule_available = trash_schedule_readable (schedule); } else diff --git a/src/manage.h b/src/manage.h index dbd086188..ef75470e9 100644 --- a/src/manage.h +++ b/src/manage.h @@ -2986,8 +2986,12 @@ manage_schedule (manage_connection_forker_t, gboolean, sigset_t *); char *schedule_uuid (schedule_t); +char *trash_schedule_uuid (schedule_t); + char *schedule_name (schedule_t); +char *trash_schedule_name (schedule_t); + int schedule_duration (schedule_t); int schedule_period (schedule_t); diff --git a/src/manage_sql.c b/src/manage_sql.c index 40b1ef067..f7d200da1 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -49887,6 +49887,20 @@ schedule_uuid (schedule_t schedule) schedule); } +/** + * @brief Return the UUID of a trash schedule. + * + * @param[in] schedule Schedule. + * + * @return Newly allocated UUID. + */ +char * +trash_schedule_uuid (schedule_t schedule) +{ + return sql_string ("SELECT uuid FROM schedules_trash WHERE id = %llu;", + schedule); +} + /** * @brief Return the name of a schedule. * @@ -49901,6 +49915,20 @@ schedule_name (schedule_t schedule) schedule); } +/** + * @brief Return the name of a trash schedule. + * + * @param[in] schedule Schedule. + * + * @return Newly allocated name. + */ +char * +trash_schedule_name (schedule_t schedule) +{ + return sql_string ("SELECT name FROM schedules_trash WHERE id = %llu;", + schedule); +} + /** * @brief Return the period of a schedule. *