Commit 6bbf6cf2 authored by Franco Fichtner's avatar Franco Fichtner

inc/config: tmp_path removal, style, review & stale code

parent ae871654
<?php <?php
/* /*
Copyright (C) 2015 Franco Fichtner <franco@opnsense.org>
Ported from config.inc by Erik Kristensen Ported from config.inc by Erik Kristensen
Copyright (C) 2004-2010 Scott Ullrich Copyright (C) 2004-2010 Scott Ullrich
Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>. Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
...@@ -86,7 +87,7 @@ function encrypted_configxml() ...@@ -86,7 +87,7 @@ function encrypted_configxml()
******/ ******/
function parse_config($parse = false) function parse_config($parse = false)
{ {
global $g, $config_parsed, $config_extra; global $g, $config_parsed;
$config_xml = '/conf/config.xml'; $config_xml = '/conf/config.xml';
$lockkey = lock('config'); $lockkey = lock('config');
...@@ -108,9 +109,9 @@ function parse_config($parse = false) ...@@ -108,9 +109,9 @@ function parse_config($parse = false)
// Check for encrypted config.xml // Check for encrypted config.xml
encrypted_configxml(); encrypted_configxml();
if(!$parse) { if (!$parse) {
if (file_exists($g['tmp_path'] . '/config.cache')) { if (file_exists('/tmp/config.cache')) {
$config = unserialize(file_get_contents($g['tmp_path'] . '/config.cache')); $config = unserialize(file_get_contents('/tmp/config.cache'));
if (is_null($config)) if (is_null($config))
$parse = true; $parse = true;
} else } else
...@@ -150,36 +151,22 @@ function parse_config($parse = false) ...@@ -150,36 +151,22 @@ function parse_config($parse = false)
return $config; return $config;
} }
/****f* config/generate_config_cache function generate_config_cache($config)
* NAME {
* generate_config_cache - Write serialized configuration to cache. $configcache = fopen('/tmp/config.cache', 'w');
* INPUTS
* $config - array containing current firewall configuration
* RESULT
* boolean - true on completion
******/
function generate_config_cache($config) {
global $g, $config_extra;
$configcache = fopen($g['tmp_path'] . '/config.cache', "w");
fwrite($configcache, serialize($config)); fwrite($configcache, serialize($config));
fclose($configcache); fclose($configcache);
unset($configcache); unset($configcache);
/* Used for config.extra.xml */
if(file_exists($g['tmp_path'] . '/config.extra.cache') && $config_extra) {
$configcacheextra = fopen($g['tmp_path'] . '/config.extra.cache', "w");
fwrite($configcacheextra, serialize($config_extra));
fclose($configcacheextra);
unset($configcacheextra);
}
} }
function discover_last_backup() { function discover_last_backup()
{
$backups = glob('/conf/backup/*.xml'); $backups = glob('/conf/backup/*.xml');
$last_backup = ""; $last_backup = '';
$last_mtime = 0; $last_mtime = 0;
foreach($backups as $backup) { foreach($backups as $backup) {
if(filemtime($backup) > $last_mtime) { if (filemtime($backup) > $last_mtime) {
$last_mtime = filemtime($backup); $last_mtime = filemtime($backup);
$last_backup = $backup; $last_backup = $backup;
} }
...@@ -188,13 +175,14 @@ function discover_last_backup() { ...@@ -188,13 +175,14 @@ function discover_last_backup() {
return basename($last_backup); return basename($last_backup);
} }
function restore_backup($file) { function restore_backup($file)
{
global $g; global $g;
if (file_exists($file)) { if (file_exists($file)) {
conf_mount_rw(); conf_mount_rw();
unlink_if_exists("{$g['tmp_path']}/config.cache"); @unlink('/tmp/config.cache');
copy("$file","/conf/config.xml"); copy($file, '/conf/config.xml');
disable_security_checks(); disable_security_checks();
log_error(sprintf(gettext('%1$s is restoring the configuration %2$s'), $g['product_name'], $file)); log_error(sprintf(gettext('%1$s is restoring the configuration %2$s'), $g['product_name'], $file));
file_notice("config.xml", sprintf(gettext('%1$s is restoring the configuration %2$s'), $g['product_name'], $file), "pfSenseConfigurator", ""); file_notice("config.xml", sprintf(gettext('%1$s is restoring the configuration %2$s'), $g['product_name'], $file), "pfSenseConfigurator", "");
...@@ -578,7 +566,7 @@ function reset_factory_defaults($lock = false) ...@@ -578,7 +566,7 @@ function reset_factory_defaults($lock = false)
} }
} }
closedir($dh); closedir($dh);
unlink_if_exists($g['tmp_path'] . "/config.cache"); unlink_if_exists('/tmp/config.cache');
/* copy default configuration */ /* copy default configuration */
copy('/usr/local/etc/config.xml', '/conf/config.xml'); copy('/usr/local/etc/config.xml', '/conf/config.xml');
...@@ -606,7 +594,7 @@ function config_restore($conffile) { ...@@ -606,7 +594,7 @@ function config_restore($conffile) {
$lockkey = lock('config', LOCK_EX); $lockkey = lock('config', LOCK_EX);
unlink_if_exists("{$g['tmp_path']}/config.cache"); unlink_if_exists('/tmp/config.cache');
copy($conffile, '/conf/config.xml'); copy($conffile, '/conf/config.xml');
disable_security_checks(); disable_security_checks();
...@@ -622,30 +610,28 @@ function config_restore($conffile) { ...@@ -622,30 +610,28 @@ function config_restore($conffile) {
return 0; return 0;
} }
function config_install($conffile) { function config_install($conffile)
global $config, $g; {
if (!file_exists($conffile)) {
if (!file_exists($conffile))
return 1; return 1;
}
if (!config_validate("{$conffile}")) if (!config_validate($conffile)) {
return 1; return 1;
}
if(file_exists("/var/run/booting")) if (file_exists('/var/run/booting')) {
echo gettext("Installing configuration...") . "\n"; echo gettext("Installing configuration...") . "\n";
else } else {
log_error(gettext("Installing configuration ....")); log_error(gettext("Installing configuration ...."));
}
conf_mount_rw(); conf_mount_rw();
$lockkey = lock('config', LOCK_EX); $lockkey = lock('config', LOCK_EX);
copy($conffile, '/conf/config.xml'); copy($conffile, '/conf/config.xml');
disable_security_checks(); disable_security_checks();
@unlink('/tmp/config.cache');
/* unlink cache file if it exists */
if(file_exists("{$g['tmp_path']}/config.cache"))
unlink("{$g['tmp_path']}/config.cache");
unlock($lockkey); unlock($lockkey);
conf_mount_ro(); conf_mount_ro();
...@@ -661,21 +647,21 @@ function config_install($conffile) { ...@@ -661,21 +647,21 @@ function config_install($conffile) {
* Intended for use when restoring a configuration or directly * Intended for use when restoring a configuration or directly
* modifying config.xml without an unconditional reboot. * modifying config.xml without an unconditional reboot.
*/ */
function disable_security_checks() { function disable_security_checks()
global $g; {
touch("{$g['tmp_path']}/disable_security_checks"); touch('/tmp/disable_security_checks');
} }
/* Restores security checks. Should be called after all succeed. */ /* Restores security checks. Should be called after all succeed. */
function restore_security_checks() { function restore_security_checks()
global $g; {
unlink_if_exists("{$g['tmp_path']}/disable_security_checks"); @unlink('/tmp/disable_security_checks');
} }
/* Returns status of security check temporary disable. */ /* Returns status of security check temporary disable. */
function security_checks_disabled() { function security_checks_disabled()
global $g; {
return file_exists("{$g['tmp_path']}/disable_security_checks"); return file_exists('/tmp/disable_security_checks');
} }
function config_validate($conffile) { function config_validate($conffile) {
......
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