Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task schedule data fix (8.0) #500

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
307 changes: 143 additions & 164 deletions src/gmp.c
Original file line number Diff line number Diff line change
@@ -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 = trash_schedule_uuid (schedule);
task_schedule_name = trash_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,
"<schedule id=\"%s\">"
"<name>%s</name>"
"<trash>%d</trash>"
"<first_time>%s</first_time>"
"<next_time>%s</next_time>"
"<icalendar>%s</icalendar>"
"<period>%d</period>"
"<period_months>"
"%d"
"</period_months>"
"<duration>%d</duration>"
"<timezone>%s</timezone>"
"</schedule>"
"<schedule_periods>"
"%d"
"</schedule_periods>",
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,
"<schedule id=\"%s\">"
"<name>%s</name>"
"<next_time>%s</next_time>"
"<trash>%d</trash>"
"</schedule>",
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,119 +18819,17 @@ 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 ("<task id=\"%s\">"
"<name>%s</name>",
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 ("<schedule id=\"%s\">"
"<name>%s</name>"
"<trash>%d</trash>"
"<first_time>%s</first_time>"
"<next_time>%s</next_time>"
"<icalendar>%s</icalendar>"
"<period>%d</period>"
"<period_months>"
"%d"
"</period_months>"
"<duration>%d</duration>"
"<timezone>%s</timezone>"
"</schedule>"
"<schedule_periods>"
"%d"
"</schedule_periods>",
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 ("<schedule id=\"%s\">"
"<name>%s</name>"
"<next_time>%s</next_time>"
"<trash>%d</trash>"
"</schedule>",
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 ("</task>");

@@ -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
("<alterable>%i</alterable>"
"<config id=\"%s\">"
@@ -19267,13 +19259,7 @@ handle_get_tasks (gmp_parser_t *gmp_parser, GError **error)
"%u<finished>%u</finished>"
"</report_count>"
"<trend>%s</trend>"
"<schedule id=\"%s\">"
"<name>%s</name>"
"<next_time>%s</next_time>"
"<trash>%i</trash>"
"%s"
"</schedule>"
"<schedule_periods>%i</schedule_periods>"
"%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 ? "" : "<permissions/>",
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);
4 changes: 4 additions & 0 deletions src/manage.h
Original file line number Diff line number Diff line change
@@ -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);
Loading