Commit 148f0d07 authored by Franco Fichtner's avatar Franco Fichtner

cron: merge low-level rework from master

(cherry picked from commit 731a9566)
(cherry picked from commit d25e1259)
(cherry picked from commit 5e97c86d)
(cherry picked from commit 8ff7c87f)
(cherry picked from commit 65882381)
(cherry picked from commit 8c9be5ca)
(cherry picked from commit 4958ea7b)
(cherry picked from commit 98485c39)
(cherry picked from commit 5a10f56a)
(cherry picked from commit 5d80d462)
(cherry picked from commit fd3ac67c)
(cherry picked from commit 9ce4fc1d)
(cherry picked from commit af4ce2d0)
(cherry picked from commit ed8c5a4f)
(cherry picked from commit 9f12e7f7)
(cherry picked from commit 8be72857)
(cherry picked from commit 07799412)
(cherry picked from commit c09b9299)
parent 8af3361f
......@@ -310,62 +310,6 @@
</destination>
</rule>
</filter>
<cron>
<item>
<minute>1,31</minute>
<hour>0-5</hour>
<mday>*</mday>
<month>*</month>
<wday>*</wday>
<who>root</who>
<command>adjkerntz -a</command>
</item>
<item>
<minute>1</minute>
<hour>3</hour>
<mday>1</mday>
<month>*</month>
<wday>*</wday>
<who>root</who>
<command>/usr/local/etc/rc.update_bogons</command>
</item>
<item>
<minute>*/60</minute>
<hour>*</hour>
<mday>*</mday>
<month>*</month>
<wday>*</wday>
<who>root</who>
<command>/usr/local/sbin/expiretable -v -t 3600 sshlockout</command>
</item>
<item>
<minute>1</minute>
<hour>1</hour>
<mday>*</mday>
<month>*</month>
<wday>*</wday>
<who>root</who>
<command>/usr/local/etc/rc.dyndns.update</command>
</item>
<item>
<minute>*/60</minute>
<hour>*</hour>
<mday>*</mday>
<month>*</month>
<wday>*</wday>
<who>root</who>
<command>/usr/local/sbin/expiretable -v -t 3600 virusprot</command>
</item>
<item>
<minute>30</minute>
<hour>12</hour>
<mday>*</mday>
<month>*</month>
<wday>*</wday>
<who>root</who>
<command>/usr/local/etc/rc.update_urltables</command>
</item>
</cron>
<rrd>
<enable/>
</rrd>
......
......@@ -116,21 +116,6 @@ function convert_config()
return;
}
/* special case upgrades */
/* fix every minute crontab bogons entry */
if (is_array($config['cron'])) {
$cron_item_count = count($config['cron']['item']);
for($x=0; $x<$cron_item_count; $x++) {
if(stristr($config['cron']['item'][$x]['command'], 'rc.update_bogons')) {
if($config['cron']['item'][$x]['hour'] == "*" ) {
$config['cron']['item'][$x]['hour'] = "3";
write_config(gettext("Updated bogon update frequency to 3am"));
log_error(gettext("Updated bogon update frequency to 3am"));
}
}
}
}
// Save off config version
$prev_version = $config['version'];
/* Loop and run upgrade_VER_to_VER() until we're at current version */
......
......@@ -2345,6 +2345,88 @@ function configure_cron()
{
global $config;
$obsoletes = array(
'/usr/local/etc/rc.backup_dhcpleases',
'/usr/local/etc/rc.backup_netflow',
'/usr/local/etc/rc.backup_rrd',
'/usr/local/etc/rc.dyndns.update',
'/usr/local/etc/rc.update_bogons',
'/usr/local/etc/rc.update_urltables',
'/usr/local/opnsense/scripts/remote_backup.php',
'/usr/local/sbin/expiretable -v -t 3600 sshlockout',
'/usr/local/sbin/expiretable -v -t 3600 virusprot',
'adjkerntz -a',
/* old config migration for extra care: */
'checkreload.sh',
'newsyslog',
);
/*
* Strip old cron jobs that we no longer save explicitly.
* This can be safely removed on our final road to 17.1.
*/
if (isset($config['cron']['item'])) {
$flush = false;
$index = 0;
/* PHP retains the index, can iterate all and unset */
foreach ($config['cron']['item'] as $item) {
foreach ($obsoletes as $obsolete) {
if ($item['command'] === $obsolete) {
unset($config['cron']['item'][$index]);
$flush = true;
}
}
$index++;
}
if (!count($config['cron']['item'])) {
unset($config['cron']);
$flush = true;
}
if ($flush) {
write_config(gettext('Removed obsolete cron jobs'));
}
}
$autocron = array();
$autocron[] = generate_cron_job('/usr/local/sbin/expiretable -v -t 3600 sshlockout', '*/60');
$autocron[] = generate_cron_job('/usr/local/sbin/expiretable -v -t 3600 virusprot', '*/60');
$autocron[] = generate_cron_job('/usr/local/etc/rc.dyndns.update', '1', '1');
$autocron[] = generate_cron_job('/usr/local/etc/rc.update_urltables', '30', '12');
if (!empty($config['system']['rrdbackup'])) {
$autocron[] = generate_cron_job('/usr/local/etc/rc.backup_rrd', $minute = '0', '*/' . $config['system']['rrdbackup']);
}
if (!empty($config['system']['dhcpbackup'])) {
$autocron[] = generate_cron_job('/usr/local/etc/rc.backup_dhcpleases', $minute = '0', '*/' . $config['system']['dhcpbackup']);
}
if (!empty($config['system']['netflowbackup'])) {
$autocron[] = generate_cron_job('/usr/local/etc/rc.backup_netflow', $minute = '0', '*/' . $config['system']['netflowbackup']);
}
if (!empty($config['system']['remotebackup']['GDriveEnabled'])) {
$autocron[] = generate_cron_job('/usr/local/opnsense/scripts/remote_backup.php', 0, 1);
}
/* bogons fetch always set in default config.xml */
switch ($config['system']['bogons']['interval']) {
case 'daily':
$autocron[] = generate_cron_job('/usr/local/etc/rc.update_bogons', '1', '3', '*', '*', '*');
break;
case 'weekly':
$autocron[] = generate_cron_job('/usr/local/etc/rc.update_bogons', '1', '3', '*', '*', '0');
break;
case 'monthly':
default:
$autocron[] = generate_cron_job('/usr/local/etc/rc.update_bogons', '1', '3', '1', '*', '*');
break;
}
$anchor = '# custom crontab entries';
/* preserve existing crontab entries */
......@@ -2361,83 +2443,51 @@ function configure_cron()
/* please maintain the newline at the end of file */
$crontab_contents = implode("\n", $crontab_contents) . "\n";
if (isset($config['cron']['item'])) {
$crontab_contents .= "#\n";
$crontab_contents .= $anchor . "\n";
$crontab_contents .= '# auto-generated at ' . date('F j, Y, g:i a') . "\n";
$crontab_contents .= "#\n";
$crontab_contents .= "#\n";
$crontab_contents .= $anchor . "\n";
$crontab_contents .= '# auto-generated at ' . date('F j, Y, g:i a') . "\n";
$crontab_contents .= "#\n";
foreach ($config['cron']['item'] as $item) {
$crontab_contents .= "\n{$item['minute']}\t";
$crontab_contents .= "{$item['hour']}\t";
$crontab_contents .= "{$item['mday']}\t";
$crontab_contents .= "{$item['month']}\t";
$crontab_contents .= "{$item['wday']}\t";
$crontab_contents .= "{$item['who']}\t";
$crontab_contents .= "{$item['command']}";
}
if (isset($config['cron']['item'])) {
$autocron = array_merge($config['cron']['item'], $autocron);
}
$crontab_contents .= "\n#\n";
$crontab_contents .= "# If possible do not add items to this file manually.\n";
$crontab_contents .= "# If you do so, this file must be terminated with a blank line (e.g. new line)\n";
$crontab_contents .= "#\n\n";
foreach ($autocron as $item) {
$crontab_contents .= "{$item['minute']}\t";
$crontab_contents .= "{$item['hour']}\t";
$crontab_contents .= "{$item['mday']}\t";
$crontab_contents .= "{$item['month']}\t";
$crontab_contents .= "{$item['wday']}\t";
$crontab_contents .= "{$item['who']}\t";
$crontab_contents .= "{$item['command']}\n";
}
$crontab_contents .= "#\n";
$crontab_contents .= "# If possible do not add items to this file manually.\n";
$crontab_contents .= "# If you do so, this file must be terminated with a blank line (e.g. new line)\n";
$crontab_contents .= "#\n\n";
file_put_contents('/etc/crontab', $crontab_contents);
unset($crontab_contents);
killbypid('/var/run/cron.pid', 'HUP');
}
function install_cron_job($command, $active=false, $minute="0", $hour="*", $monthday="*", $month="*", $weekday="*", $who="root") {
global $config;
$is_installed = false;
function generate_cron_job($command, $minute = '0', $hour = '*', $monthday = '*', $month = '*', $weekday = '*', $who = 'root')
{
$cron_item = array();
if (!is_array($config['cron'])) {
$config['cron'] = array();
}
if (!is_array($config['cron']['item'])) {
$config['cron']['item'] = array();
}
$cron_item['minute'] = $minute;
$cron_item['hour'] = $hour;
$cron_item['mday'] = $monthday;
$cron_item['month'] = $month;
$cron_item['wday'] = $weekday;
$cron_item['who'] = $who;
$cron_item['command'] = $command;
$x=0;
foreach($config['cron']['item'] as $item) {
if (strstr($item['command'], $command)) {
$is_installed = true;
break;
}
$x++;
}
if ($active) {
$cron_item = array();
$cron_item['minute'] = $minute;
$cron_item['hour'] = $hour;
$cron_item['mday'] = $monthday;
$cron_item['month'] = $month;
$cron_item['wday'] = $weekday;
$cron_item['who'] = $who;
$cron_item['command'] = $command;
if (!$is_installed) {
$config['cron']['item'][] = $cron_item;
write_config(sprintf(gettext("Installed cron job for %s"), $command));
} else {
$config['cron']['item'][$x] = $cron_item;
write_config(sprintf(gettext("Updated cron job for %s"), $command));
}
} else {
if ($is_installed == true) {
unset($config['cron']['item'][$x]);
write_config(sprintf(gettext("Removed cron job for %s"), $command));
}
}
configure_cron();
return $cron_item;
}
/**
* check if apinger service is enabled
*/
function is_apinger_enabled()
{
global $config;
......
......@@ -501,76 +501,6 @@ function upgrade_024_to_025() {
unset($config['system']['use_rrd_gateway']);
}
function upgrade_025_to_026()
{
global $config;
$cron_item = array();
$cron_item['minute'] = "0";
$cron_item['hour'] = "*";
$cron_item['mday'] = "*";
$cron_item['month'] = "*";
$cron_item['wday'] = "*";
$cron_item['who'] = "root";
$cron_item['command'] = "newsyslog";
$config['cron']['item'][] = $cron_item;
$cron_item = array();
$cron_item['minute'] = "1,31";
$cron_item['hour'] = "0-5";
$cron_item['mday'] = "*";
$cron_item['month'] = "*";
$cron_item['wday'] = "*";
$cron_item['who'] = "root";
$cron_item['command'] = "adjkerntz -a";
$config['cron']['item'][] = $cron_item;
$cron_item = array();
$cron_item['minute'] = "1";
$cron_item['hour'] = "*";
$cron_item['mday'] = "1";
$cron_item['month'] = "*";
$cron_item['wday'] = "*";
$cron_item['who'] = "root";
$cron_item['command'] = "/usr/local/etc/rc.update_bogons";
$config['cron']['item'][] = $cron_item;
$cron_item = array();
$cron_item['minute'] = "*/60";
$cron_item['hour'] = "*";
$cron_item['mday'] = "*";
$cron_item['month'] = "*";
$cron_item['wday'] = "*";
$cron_item['who'] = "root";
$cron_item['command'] = "/usr/local/sbin/expiretable -v -t 3600 sshlockout";
$config['cron']['item'][] = $cron_item;
$cron_item = array();
$cron_item['minute'] = "1";
$cron_item['hour'] = "1";
$cron_item['mday'] = "*";
$cron_item['month'] = "*";
$cron_item['wday'] = "*";
$cron_item['who'] = "root";
$cron_item['command'] = "/usr/local/etc/rc.dyndns.update";
$config['cron']['item'][] = $cron_item;
$cron_item = array();
$cron_item['minute'] = "*/60";
$cron_item['hour'] = "*";
$cron_item['mday'] = "*";
$cron_item['month'] = "*";
$cron_item['wday'] = "*";
$cron_item['who'] = "root";
$cron_item['command'] = "/usr/local/sbin/expiretable -v -t 3600 virusprot";
$config['cron']['item'][] = $cron_item;
}
function upgrade_028_to_029() {
global $config;
$rule_item = array();
......@@ -584,7 +514,6 @@ function upgrade_028_to_029() {
$a_filter[] = $rule_item;
}
function upgrade_029_to_030() {
global $config;
/* enable the rrd config setting by default */
......@@ -2504,19 +2433,6 @@ function upgrade_069_to_070() {
}
}
function upgrade_070_to_071() {
global $config;
if (is_array($config['cron']['item'])) {
foreach($config['cron']['item'] as $idx => $cronitem) {
if(stristr($cronitem['command'], "checkreload.sh")) {
unset($config['cron']['item'][$idx]);
break;
}
}
}
}
function rename_field(& $section, $oldname, $newname) {
if (is_array($section)) {
foreach($section as & $item) {
......@@ -2570,19 +2486,6 @@ function upgrade_074_to_075() {
rename_field($config['crl'], 'name', 'descr');
}
function upgrade_075_to_076() {
global $config;
$cron_item = array();
$cron_item['minute'] = "30";
$cron_item['hour'] = "12";
$cron_item['mday'] = "*";
$cron_item['month'] = "*";
$cron_item['wday'] = "*";
$cron_item['who'] = "root";
$cron_item['command'] = "/usr/local/etc/rc.update_urltables";
$config['cron']['item'][] = $cron_item;
}
function upgrade_076_to_077() {
global $config;
foreach($config['filter']['rule'] as & $rule) {
......@@ -3119,11 +3022,6 @@ function upgrade_098_to_099() {
}
}
function upgrade_099_to_100()
{
install_cron_job("newsyslog", false);
}
function upgrade_100_to_101()
{
global $config;
......
......@@ -40,20 +40,6 @@ require_once("rrd.inc");
require_once("system.inc");
require_once("pfsense-utils.inc");
/**
* check if cron exists
*/
function cron_job_exists($command) {
global $config;
foreach($config['cron']['item'] as $item) {
if(strstr($item['command'], $command)) {
return true;
}
}
return false;
}
function rrd_data_xml() {
$rrddbpath = '/var/db/rrd';
......@@ -422,7 +408,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'GET') {
if (is_numeric($_POST['GDriveBackupCount'])) {
$config['system']['remotebackup']['GDriveBackupCount'] = $_POST['GDriveBackupCount'];
} else {
$config['system']['remotebackup']['GDriveBackupCount'] = 30;
$config['system']['remotebackup']['GDriveBackupCount'] = 60;
}
if ( $_POST['GDrivePasswordConfirm'] != $_POST['GDrivePassword'] ) {
......@@ -430,34 +416,37 @@ if ($_SERVER['REQUEST_METHOD'] === 'GET') {
$input_errors[] = gettext("The supplied 'Password' and 'Confirm' field values must match.");
}
if (is_uploaded_file($_FILES['GDriveP12file']['tmp_name'])) {
$data = file_get_contents($_FILES['GDriveP12file']['tmp_name']);
$config['system']['remotebackup']['GDriveP12key'] = base64_encode($data);
} elseif ($config['system']['remotebackup']['GDriveEnabled'] != "on") {
unset($config['system']['remotebackup']['GDriveP12key']);
}
if (count($input_errors) == 0) {
if (is_uploaded_file($_FILES['GDriveP12file']['tmp_name'])) {
$data = file_get_contents($_FILES['GDriveP12file']['tmp_name']);
$config['system']['remotebackup']['GDriveP12key'] = base64_encode($data);
} elseif ($config['system']['remotebackup']['GDriveEnabled'] != "on") {
unset($config['system']['remotebackup']['GDriveP12key']);
}
$savemsg = gettext("Google Drive backup settings have been saved.");
write_config();
// test / perform backup
try {
$filesInBackup = backup_to_google_drive() ;
$cron_job = "/usr/local/opnsense/scripts/remote_backup.php";
if (!cron_job_exists($cron_job)) {
// initial cron job install
install_cron_job($cron_job,true,0,1);
write_config();
configure_cron();
try {
$filesInBackup = backup_to_google_drive();
} catch (Exception $e) {
$filesInBackup = array();
}
} catch (Exception $e) {
$filesInBackup = array() ;
}
if (count($filesInBackup) == 0) {
$input_errors[] = gettext("Google Drive communication failure");
} else {
$input_messages = gettext("Backup succesfull, current filelist:");
foreach ($filesInBackup as $filename => $file) {
$input_messages = $input_messages . "<br>" . $filename ;
if (empty($config['system']['remotebackup']['GDriveEnabled'])) {
/* unused */
} elseif (count($filesInBackup) == 0) {
$input_errors[] = gettext("Google Drive communication failure");
} else {
$input_messages = gettext("Backup successful, current file list:") . "<br>";
foreach ($filesInBackup as $filename => $file) {
$input_messages = $input_messages . "<br>" . $filename;
}
}
}
}
}
......
......@@ -214,17 +214,6 @@ if ($_SERVER['REQUEST_METHOD'] === 'GET') {
}
if ($pconfig['bogonsinterval'] != $config['system']['bogons']['interval']) {
switch ($pconfig['bogonsinterval']) {
case 'daily':
install_cron_job("/usr/local/etc/rc.update_bogons", true, "1", "3", "*", "*", "*");
break;
case 'weekly':
install_cron_job("/usr/local/etc/rc.update_bogons", true, "1", "3", "*", "*", "0");
break;
case 'monthly':
default:
install_cron_job("/usr/local/etc/rc.update_bogons", true, "1", "3", "1", "*", "*");
}
$config['system']['bogons']['interval'] = $pconfig['bogonsinterval'];
}
......@@ -237,6 +226,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'GET') {
$savemsg = get_std_save_message();
configure_cron();
filter_configure();
}
}
......
......@@ -216,17 +216,14 @@ if ($_SERVER['REQUEST_METHOD'] === 'GET') {
}
if (!empty($pconfig['rrdbackup'])) {
$config['system']['rrdbackup'] = $_POST['rrdbackup'];
install_cron_job("/usr/local/etc/rc.backup_rrd", ($config['system']['rrdbackup'] > 0), $minute = "0", "*/{$config['system']['rrdbackup']}");
$config['system']['rrdbackup'] = $pconfig['rrdbackup'];
} elseif (isset($config['system']['rrdbackup'])) {
install_cron_job("/usr/local/etc/rc.backup_rrd", false, $minute = "0", "*/{$config['system']['rrdbackup']}");
unset($config['system']['rrdbackup']);
}
if (!empty($pconfig['dhcpbackup'])) {
$config['system']['dhcpbackup'] = $pconfig['dhcpbackup'];
install_cron_job("/usr/local/etc/rc.backup_dhcpleases", ($config['system']['dhcpbackup'] > 0), $minute = "0", "*/{$config['system']['dhcpbackup']}");
} elseif (isset($config['system']['dhcpbackup'])) {
install_cron_job("/usr/local/etc/rc.backup_dhcpleases", false, $minute = "0", "*/{$config['system']['dhcpbackup']}");
unset($config['system']['dhcpbackup']);
}
......@@ -234,6 +231,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'GET') {
$savemsg = get_std_save_message();
system_resolvconf_generate(true);
configure_cron();
filter_configure();
activate_powerd();
load_crypto_module();
......@@ -523,22 +521,20 @@ include("head.inc");
</tr>
<tr>
<td><a id="help_for_rrdbackup" href="#" class="showhelp"><i class="fa fa-info-circle"></i></a> <?=gettext("Periodic RRD Backup");?></td>
<td>
<select name="rrdbackup" class="selectpicker" data-style="btn-default" id="rrdbackup">
<option value='0' <?=!$pconfig['rrdbackup'] == 0 ? "selected='selected'" : "";?>>
<?=gettext("Disable"); ?>
</option>
<td>
<select name="rrdbackup" class="selectpicker" data-style="btn-default" id="rrdbackup">
<option value='0' <?=!$pconfig['rrdbackup'] == 0 ? 'selected="selected"' : ''; ?>><?=gettext("Disabled"); ?></option>
<?php
for ($x=1; $x<=24; $x++):?>
<option value='<?= $x ?>' <?= $pconfig['rrdbackup'] == $x ? "selected='selected'" : "";?>>
<?= $x ?> <?=gettext("hour"); ?><?=($x>1) ? "s" : "";?>
</option>
for ($x = 1; $x <= 24; $x++): ?>
<option value="<?= $x ?>" <?= $pconfig['rrdbackup'] == $x ? 'selected="selected"' : ''; ?>>
<?= $x == 1 ? gettext('1 hour') : sprintf(gettext('%s hours'), $x) ?>
</option>
<?php
endfor; ?>
</select>
<br />
<div class="hidden" for="help_for_rrdbackup">
<?=gettext("This will periodically backup the RRD data so it can be restored automatically on the next boot. Keep in mind that the more frequent the backup, the more writes will happen to your media.");?>
<?=gettext("This will periodically backup the RRD data so it can be restored automatically on the next boot.");?>
</div>
</td>
</tr>
......@@ -546,17 +542,17 @@ include("head.inc");
<td><a id="help_for_dhcpbackup" href="#" class="showhelp"><i class="fa fa-info-circle"></i></a> <?=gettext("Periodic DHCP Leases Backup");?></td>
<td>
<select name="dhcpbackup" class="selectpicker" data-style="btn-default" id="dhcpbackup">
<option value='0' <?= $pconfig['dhcpbackup'] == 0 ? "selected='selected'" : ""; ?>><?=gettext("Disable"); ?></option>
<option value='0' <?= $pconfig['dhcpbackup'] == 0 ? "selected='selected'" : ''; ?>><?=gettext('Disabled'); ?></option>
<?php
for ($x=1; $x<=24; $x++):?>
<option value='<?= $x ?>' <?= $pconfig['dhcpbackup'] == $x ? "selected='selected'" : "";?>>
<?= $x ?> <?=gettext("hour"); ?><?=($x>1) ? "s" : "";?>
for ($x = 1; $x <= 24; $x++): ?>
<option value="<?= $x ?>" <?= $pconfig['dhcpbackup'] == $x ? 'selected="selected"' : '';?>>
<?= $x == 1 ? gettext('1 hour') : sprintf(gettext('%s hours'), $x) ?>
</option>
<?php
endfor; ?>
</select>
<div class="hidden" for="help_for_dhcpbackup">
<?=gettext("This will periodically backup the DHCP leases data so it can be restored automatically on the next boot. Keep in mind that the more frequent the backup, the more writes will happen to your media.");?>
<?=gettext("This will periodically backup the DHCP leases data so it can be restored automatically on the next boot.");?>
</div>
</td>
</tr>
......@@ -570,7 +566,7 @@ include("head.inc");
<strong><?=gettext("Use memory file system for /tmp and /var"); ?></strong>
<div class="hidden" for="help_for_use_mfs_tmpvar">
<?=gettext("Set this if you wish to use /tmp and /var as RAM disks (memory file system disks) on a full install " .
"rather than use the hard disk. Setting this will cause the data in /tmp and /var to be lost at reboot, including log data. RRD and DHCP Leases will be retained."); ?>
"rather than use the hard disk. Setting this will cause the data in /tmp and /var to be lost on reboot, including log data."); ?>
</div>
</td>
</tr>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment