Commit 6e8f51d4 authored by Ad Schellevis's avatar Ad Schellevis

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

(config) automatic recovery when config is broken, closes https://github.com/opnsense/core/issues/701
parent a0fc0985
...@@ -65,19 +65,6 @@ function alias_make_table($config) ...@@ -65,19 +65,6 @@ function alias_make_table($config)
function parse_config() function parse_config()
{ {
$cnf = OPNsense\Core\Config::getInstance(); $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) // 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()); $config=$cnf->toArray(listtags());
......
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
namespace OPNsense\Core; namespace OPNsense\Core;
use \Phalcon\DI\FactoryDefault; use \Phalcon\DI\FactoryDefault;
use \Phalcon\Logger\Adapter\Syslog;
/** /**
* Class Config provides access to systems config xml * Class Config provides access to systems config xml
...@@ -255,7 +256,6 @@ class Config extends Singleton ...@@ -255,7 +256,6 @@ class Config extends Singleton
return $this->simplexml; return $this->simplexml;
} }
/** /**
* init new config object, try to load current configuration * init new config object, try to load current configuration
* (executed via Singleton) * (executed via Singleton)
...@@ -267,8 +267,19 @@ class Config extends Singleton ...@@ -267,8 +267,19 @@ class Config extends Singleton
$this->load(); $this->load();
} catch (\Exception $e) { } catch (\Exception $e) {
$this->simplexml = null ; $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 ...@@ -304,6 +315,7 @@ class Config extends Singleton
$this->simplexml = simplexml_load_string($xml); $this->simplexml = simplexml_load_string($xml);
if ($this->simplexml == null) { if ($this->simplexml == null) {
restore_error_handler();
throw new ConfigException("invalid config xml") ; 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