From 1548bd353c5439666e9c75cfca2faee938d33dde Mon Sep 17 00:00:00 2001 From: Phil Davis Date: Wed, 13 Jan 2016 23:34:27 +0545 Subject: Fix #5767 config validation errors when first enabling DNS Resolver Generate a complete set of test conf files in /var/unbound/test subdir when doing the validation of the tentative config that is being saved. This avoids any dependency on any of the "real" files already existing in /var/unbound and also avoids the chance of overwriting a live file that is currently in use by the running unbound. /var/unbound/test is removed once the validation has been done. The real unbound conf files are created in /var/unbound like they always were after the user presses apply. --- src/etc/inc/unbound.inc | 109 +++++++++++++++++++++++++++--------------------- 1 file changed, 62 insertions(+), 47 deletions(-) (limited to 'src/etc') diff --git a/src/etc/inc/unbound.inc b/src/etc/inc/unbound.inc index ccae458..2ab6e3a 100644 --- a/src/etc/inc/unbound.inc +++ b/src/etc/inc/unbound.inc @@ -33,7 +33,7 @@ require_once("functions.inc"); require_once("filter.inc"); require_once("shaper.inc"); -function create_unbound_chroot_path() { +function create_unbound_chroot_path($cfgsubdir = "") { global $config, $g; // Configure chroot @@ -43,6 +43,14 @@ function create_unbound_chroot_path() { chgrp($g['unbound_chroot_path'], "unbound"); } + if ($cfgsubdir != "") { + $cfgdir = $g['unbound_chroot_path'] . $cfgsubdir; + if (!is_dir($cfgdir)) { + mkdir($cfgdir); + chown($cfgdir, "unbound"); + chgrp($cfgdir, "unbound"); + } + } } /* Optimize Unbound for environment */ @@ -102,38 +110,41 @@ function unbound_optimization() { function test_unbound_config($unboundcfg, &$output) { global $g; - $cfgfile = "{$g['unbound_chroot_path']}/unbound.test.conf"; - $unboundcfgtxt = unbound_generate_config_text($unboundcfg); - file_put_contents($cfgfile, $unboundcfgtxt); + $cfgsubdir = "/test"; + unbound_generate_config($unboundcfg, $cfgsubdir); + unbound_remote_control_setup($cfgsubdir); + do_as_unbound_user("unbound-anchor", $cfgsubdir); + + $cfgdir = "{$g['unbound_chroot_path']}{$cfgsubdir}"; $rv = 0; - exec("/usr/local/sbin/unbound-checkconf {$cfgfile} 2>&1", $output, $rv); - unlink_if_exists($cfgfile); + exec("/usr/local/sbin/unbound-checkconf {$cfgdir}/unbound.conf 2>&1", $output, $rv); + rmdir_recursive($cfgdir); return $rv; } -function unbound_generate_config() { +function unbound_generate_config($unboundcfg = NULL, $cfgsubdir = "") { global $g; - $unboundcfgtxt = unbound_generate_config_text(); + $unboundcfgtxt = unbound_generate_config_text($unboundcfg, $cfgsubdir); // Configure static Host entries - unbound_add_host_entries(); + unbound_add_host_entries($cfgsubdir); // Configure Domain Overrides - unbound_add_domain_overrides(); + unbound_add_domain_overrides("", $cfgsubdir); // Configure Unbound access-lists - unbound_acls_config(); + unbound_acls_config($cfgsubdir); - create_unbound_chroot_path(); - file_put_contents("{$g['unbound_chroot_path']}/unbound.conf", $unboundcfgtxt); + create_unbound_chroot_path($cfgsubdir); + file_put_contents("{$g['unbound_chroot_path']}{$cfgsubdir}/unbound.conf", $unboundcfgtxt); } -function unbound_generate_config_text($unboundcfg=NULL) { +function unbound_generate_config_text($unboundcfg = NULL, $cfgsubdir = "") { global $config, $g; if (is_null($unboundcfg)) { @@ -146,7 +157,7 @@ function unbound_generate_config_text($unboundcfg=NULL) { // Setup DNSSEC support if (isset($unboundcfg['dnssec'])) { $module_config = "validator iterator"; - $anchor_file = "auto-trust-anchor-file: {$g['unbound_chroot_path']}/root.key"; + $anchor_file = "auto-trust-anchor-file: {$g['unbound_chroot_path']}{$cfgsubdir}/root.key"; } else { $module_config = "iterator"; } @@ -358,16 +369,16 @@ use-caps-for-id: {$use_caps} {$private_domains} # Access lists -include: {$g['unbound_chroot_path']}/access_lists.conf +include: {$g['unbound_chroot_path']}{$cfgsubdir}/access_lists.conf # Static host entries -include: {$g['unbound_chroot_path']}/host_entries.conf +include: {$g['unbound_chroot_path']}{$cfgsubdir}/host_entries.conf # dhcp lease entries -include: {$g['unbound_chroot_path']}/dhcpleases_entries.conf +include: {$g['unbound_chroot_path']}{$cfgsubdir}/dhcpleases_entries.conf # Domain overrides -include: {$g['unbound_chroot_path']}/domainoverrides.conf +include: {$g['unbound_chroot_path']}{$cfgsubdir}/domainoverrides.conf {$forward_conf} {$custom_options} @@ -375,34 +386,34 @@ include: {$g['unbound_chroot_path']}/domainoverrides.conf ### # Remote Control Config ### -include: {$g['unbound_chroot_path']}/remotecontrol.conf +include: {$g['unbound_chroot_path']}{$cfgsubdir}/remotecontrol.conf EOD; return $unboundconf; } -function unbound_remote_control_setup() { +function unbound_remote_control_setup($cfgsubdir = "") { global $g; - if (!file_exists("{$g['unbound_chroot_path']}/remotecontrol.conf") || !file_exists("{$g['unbound_chroot_path']}/unbound_control.key")) { + if (!file_exists("{$g['unbound_chroot_path']}{$cfgsubdir}/remotecontrol.conf") || !file_exists("{$g['unbound_chroot_path']}{$cfgsubdir}/unbound_control.key")) { $remotcfg = <<