Commit 7c407af8 authored by Ad Schellevis's avatar Ad Schellevis Committed by Franco Fichtner

(config) automatic recovery when config is broken, closes...

(config) automatic recovery when config is broken, closes https://github.com/opnsense/core/issues/701

(cherry picked from commit 6e8f51d4)
parent 9793b651
......@@ -65,19 +65,6 @@ function alias_make_table($config)
function parse_config()
{
$cnf = OPNsense\Core\Config::getInstance();
if (!$cnf->isValid()) {
// there was an issue with loading the config, try to restore the last backup
$backups = $cnf->getBackups();
if (count($backups) > 0) {
// load last backup
syslog(LOG_ERR, gettext('No valid config.xml found, attempting last known config restore.'));
$cnf->restoreBackup($backups[0]);
} else {
// we don't have backups, try to load the default
syslog(LOG_ERR, gettext('No valid config.xml found, attempting to restore factory config.'));
$cnf->restoreBackup('/usr/local/etc/config.xml');
}
}
// return config data as array, use old "listags" construction to mark certain elements as array (even if they're not recurring)
$config=$cnf->toArray(listtags());
......
......@@ -29,6 +29,7 @@
namespace OPNsense\Core;
use \Phalcon\DI\FactoryDefault;
use \Phalcon\Logger\Adapter\Syslog;
/**
* Class Config provides access to systems config xml
......@@ -255,7 +256,6 @@ class Config extends Singleton
return $this->simplexml;
}
/**
* init new config object, try to load current configuration
* (executed via Singleton)
......@@ -267,8 +267,19 @@ class Config extends Singleton
$this->load();
} catch (\Exception $e) {
$this->simplexml = null ;
// there was an issue with loading the config, try to restore the last backup
$backups = $this->getBackups();
$logger = new Syslog("config", array('option' => LOG_PID, 'facility' => LOG_LOCAL4));
if (count($backups) > 0) {
// load last backup
$logger->error(gettext('No valid config.xml found, attempting last known config restore.'));
$this->restoreBackup($backups[0]);
} else {
// in case there are no backups, restore defaults.
$logger->error(gettext('No valid config.xml found, attempting to restore factory config.'));
$this->restoreBackup('/usr/local/etc/config.xml');
}
}
}
/**
......@@ -304,6 +315,7 @@ class Config extends Singleton
$this->simplexml = simplexml_load_string($xml);
if ($this->simplexml == null) {
restore_error_handler();
throw new ConfigException("invalid config xml") ;
}
......
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