From 6e8f51d434313a3188c8b5cc8a7fe9579ae233dd Mon Sep 17 00:00:00 2001 From: Ad Schellevis Date: Tue, 26 Jan 2016 17:09:54 +0100 Subject: [PATCH] (config) automatic recovery when config is broken, closes https://github.com/opnsense/core/issues/701 --- src/etc/inc/config.lib.inc | 13 ------------- .../mvc/app/library/OPNsense/Core/Config.php | 16 ++++++++++++++-- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/etc/inc/config.lib.inc b/src/etc/inc/config.lib.inc index 7677f3008..178b7d1d9 100644 --- a/src/etc/inc/config.lib.inc +++ b/src/etc/inc/config.lib.inc @@ -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()); diff --git a/src/opnsense/mvc/app/library/OPNsense/Core/Config.php b/src/opnsense/mvc/app/library/OPNsense/Core/Config.php index cdda0dda2..690d21899 100644 --- a/src/opnsense/mvc/app/library/OPNsense/Core/Config.php +++ b/src/opnsense/mvc/app/library/OPNsense/Core/Config.php @@ -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") ; }