Commit 949542de authored by Ad Schellevis's avatar Ad Schellevis

(config backup/restore) fix for https://github.com/opnsense/core/issues/202

parent dee7f51e
...@@ -32,6 +32,11 @@ require_once("config.lib.inc"); ...@@ -32,6 +32,11 @@ require_once("config.lib.inc");
require_once("guiconfig.inc"); require_once("guiconfig.inc");
require_once("script/load_phalcon.php"); require_once("script/load_phalcon.php");
// list backups
$cnf = OPNsense\Core\Config::getInstance();
$confvers = $cnf->getBackups(true);
if (isset($_POST['backupcount'])) { if (isset($_POST['backupcount'])) {
if (is_numeric($_POST['backupcount']) && ($_POST['backupcount'] >= 0)) { if (is_numeric($_POST['backupcount']) && ($_POST['backupcount'] >= 0)) {
$config['system']['backupcount'] = $_POST['backupcount']; $config['system']['backupcount'] = $_POST['backupcount'];
...@@ -42,29 +47,42 @@ if (isset($_POST['backupcount'])) { ...@@ -42,29 +47,42 @@ if (isset($_POST['backupcount'])) {
} }
write_config("Changed backup revision count to {$changedescr}"); write_config("Changed backup revision count to {$changedescr}");
} elseif ($_POST) { } elseif ($_POST) {
if (!isset($_POST['confirm']) || ($_POST['confirm'] != gettext("Confirm")) || (!isset($_POST['newver']) && !isset($_POST['rmver']))) { if (!isset($_POST['confirm']) || ($_POST['confirm'] != "Confirm") || (!isset($_POST['newver']) && !isset($_POST['rmver']))) {
header("Location: diag_confbak.php"); header("Location: diag_confbak.php");
return; return;
} }
$confvers = unserialize(file_get_contents('/conf/backup/backup.cache'));
if($_POST['newver'] != "") { if($_POST['newver'] != "") {
if(config_restore('/conf/backup/config-' . $_POST['newver'] . '.xml') == 0) // search item by revision
$savemsg = sprintf(gettext('Successfully reverted to timestamp %1$s with description "%2$s".'), date(gettext("n/j/y H:i:s"), $_POST['newver']), $confvers[$_POST['newver']]['description']); foreach ($confvers as $filename => $revision) {
else if (isset($revision['time']) && $revision['time'] == $_POST['newver']) {
if(config_restore($filename)== 0) {
$savemsg = sprintf(gettext('Successfully reverted to timestamp %1$s with description "%2$s".'), date(gettext("n/j/y H:i:s"), $_POST['newver']), $revision['description']);
} else {
$savemsg = gettext("Unable to revert to the selected configuration."); $savemsg = gettext("Unable to revert to the selected configuration.");
} }
break;
}
}
}
if($_POST['rmver'] != "") { if($_POST['rmver'] != "") {
@unlink('/conf/backup/config-' . $_POST['rmver'] . '.xml'); // search item by revision
$savemsg = sprintf(gettext('Deleted backup with timestamp %1$s and description "%2$s".'), date(gettext("n/j/y H:i:s"), $_POST['rmver']),$confvers[$_POST['rmver']]['description']); foreach ($confvers as $filename => $revision) {
if (isset($revision['time']) && $revision['time'] == $_POST['rmver']) {
@unlink($filename);
$savemsg = sprintf(gettext('Deleted backup with timestamp %1$s and description "%2$s".'), date(gettext("n/j/y H:i:s"), $_POST['rmver']),$revision['description']);
break;
}
}
} }
} }
if($_GET['getcfg'] != "") { if($_GET['getcfg'] != "") {
$file = '/conf/backup/config-' . $_GET['getcfg'] . '.xml'; foreach ($confvers as $filename => $revision) {
if ($revision['time'] == $_GET['getcfg']) {
$exp_name = urlencode("config-{$config['system']['hostname']}.{$config['system']['domain']}-{$_GET['getcfg']}.xml"); $exp_name = urlencode("config-{$config['system']['hostname']}.{$config['system']['domain']}-{$_GET['getcfg']}.xml");
$exp_data = file_get_contents($file); $exp_data = file_get_contents($filename);
$exp_size = strlen($exp_data); $exp_size = strlen($exp_data);
header("Content-Type: application/octet-stream"); header("Content-Type: application/octet-stream");
...@@ -72,28 +90,37 @@ if($_GET['getcfg'] != "") { ...@@ -72,28 +90,37 @@ if($_GET['getcfg'] != "") {
header("Content-Length: $exp_size"); header("Content-Length: $exp_size");
echo $exp_data; echo $exp_data;
exit; exit;
}
}
} }
if (($_GET['diff'] == 'Diff') && isset($_GET['oldtime']) && isset($_GET['newtime']) if (($_GET['diff'] == 'Diff') && isset($_GET['oldtime']) && isset($_GET['newtime'])
&& is_numeric($_GET['oldtime']) && (is_numeric($_GET['newtime']) || ($_GET['newtime'] == 'current'))) { && is_numeric($_GET['oldtime']) && (is_numeric($_GET['newtime']) || ($_GET['newtime'] == 'current'))) {
$oldfile = "";
$newfile = "" ;
// search filenames to compare
foreach ($confvers as $filename => $revision) {
if ($revision['time'] == $_GET['oldtime']) {
$oldfile = $filename;
} elseif ($revision['time'] == $_GET['oldtime']) {
$newfile = $filename;
}
}
$diff = ""; $diff = "";
$oldfile = '/conf/backup/config-' . $_GET['oldtime'] . '.xml';
$oldtime = $_GET['oldtime']; $oldtime = $_GET['oldtime'];
if ($_GET['newtime'] == 'current') { if ($_GET['newtime'] == 'current') {
$newfile = '/conf/config.xml'; $newfile = '/conf/config.xml';
$newtime = $config['revision']['time']; $newtime = $config['revision']['time'];
} else { } else {
$newfile = '/conf/backup/config-' . $_GET['newtime'] . '.xml';
$newtime = $_GET['newtime']; $newtime = $_GET['newtime'];
} }
if (file_exists($oldfile) && file_exists($newfile)) { if (file_exists($oldfile) && file_exists($newfile)) {
exec("/usr/bin/diff -u " . escapeshellarg($oldfile) . " " . escapeshellarg($newfile), $diff); exec("/usr/bin/diff -u " . escapeshellarg($oldfile) . " " . escapeshellarg($newfile), $diff);
} }
} }
// list backups
$cnf = OPNsense\Core\Config::getInstance();
$confvers = $cnf->getBackups(true);
$pgtitle = array(gettext("Diagnostics"),gettext("Configuration History")); $pgtitle = array(gettext("Diagnostics"),gettext("Configuration History"));
include("head.inc"); include("head.inc");
...@@ -169,7 +196,7 @@ include("head.inc"); ...@@ -169,7 +196,7 @@ include("head.inc");
<?PHP if ($_GET["newver"] || $_GET["rmver"]): ?> <?PHP if ($_GET["newver"] || $_GET["rmver"]): ?>
<form action="<?=$_SERVER['REQUEST_URI'];?>" method="post"> <form action="<?=explode("?", $_SERVER['REQUEST_URI'])[0];?>" method="post">
<section> <section>
<div class="content-box"> <div class="content-box">
......
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