From 873525726ebe79523fced2dae56d4544b9e799b9 Mon Sep 17 00:00:00 2001 From: fermin831 Date: Fri, 5 Apr 2019 12:32:43 +0200 Subject: [PATCH] Avoid infinite loops when the day of the week in cron is out of limits Former-commit-id: 6c0a87a29818219364fb9c08c44b48cb79dd74a8 --- pandora_agents/unix/pandora_agent | 7 ++++++- pandora_console/include/functions_cron.php | 5 +++++ pandora_server/lib/PandoraFMS/Tools.pm | 5 +++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/pandora_agents/unix/pandora_agent b/pandora_agents/unix/pandora_agent index 1a047590d8..e6e2132f6a 100755 --- a/pandora_agents/unix/pandora_agent +++ b/pandora_agents/unix/pandora_agent @@ -2102,6 +2102,12 @@ sub cron_next_execution { # Get day of the week and month from cron config my ($wday) = (split (/\s/, $cron))[4]; + # Check the wday values to avoid infinite loop + my ($wday_down, $wday_up) = cron_get_interval($wday); + if ($wday_down ne "*" && ($wday_down > 6 || (defined($wday_up) && $wday_up > 6))) { + log_message('setup', "Invalid cron configuration $cron. Day of the week is out of limits."); + $wday = "*"; + } # Get current time and day of the week my $cur_time = time(); @@ -2110,7 +2116,6 @@ sub cron_next_execution { my $nex_time = cron_next_execution_date ($cron, $cur_time, $interval); # Check the day - print localtime($nex_time)->strftime('%c') . " - OUTSIDE\n"; while (!cron_check_interval($wday, (localtime ($nex_time))[6])) { # If it does not acomplish the day of the week, go to the next day. $nex_time += 86400; diff --git a/pandora_console/include/functions_cron.php b/pandora_console/include/functions_cron.php index a5e1b2dcf7..6a79096175 100644 --- a/pandora_console/include/functions_cron.php +++ b/pandora_console/include/functions_cron.php @@ -83,6 +83,11 @@ function cron_next_execution($cron, $module_interval, $module_id) $cur_time = ($last_execution !== false) ? $last_execution : time(); $nex_time = cron_next_execution_date($cron, $cur_time, $module_interval); $nex_wday = (int) date('w', $nex_time); + // Check the wday values to avoid infinite loop. + $wday_int = cron_get_interval($wday); + if ($wday_int['down'] !== '*' && ($wday_int['down'] > 6 || ($wday_int['up'] !== false && $wday_int['up'] > 6))) { + $wday = '*'; + } // Check day of the way. while (!cron_check_interval($nex_wday, $wday)) { diff --git a/pandora_server/lib/PandoraFMS/Tools.pm b/pandora_server/lib/PandoraFMS/Tools.pm index 90fe9dc6c1..a03ebb83d5 100755 --- a/pandora_server/lib/PandoraFMS/Tools.pm +++ b/pandora_server/lib/PandoraFMS/Tools.pm @@ -1360,6 +1360,11 @@ sub cron_next_execution { # Get day of the week and month from cron config my ($wday) = (split (/\s/, $cron))[4]; + # Check the wday values to avoid infinite loop + my ($wday_down, $wday_up) = cron_get_interval($wday); + if ($wday_down ne "*" && ($wday_down > 6 || (defined($wday_up) && $wday_up > 6))) { + $wday = "*"; + } # Get current time and day of the week my $cur_time = time();