Skip to content

Commit

Permalink
dns: small cleanups related to #1806
Browse files Browse the repository at this point in the history
  • Loading branch information
fichtner committed Sep 16, 2017
1 parent b5b92c1 commit c945095
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 55 deletions.
34 changes: 14 additions & 20 deletions src/etc/inc/plugins.inc.d/unbound.inc
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ EOF;
$bindints = "";
if (!empty($config['unbound']['active_interface'])) {
$active_interfaces = explode(",", $config['unbound']['active_interface']);
foreach($active_interfaces as $ubif) {
foreach ($active_interfaces as $ubif) {
$intip = get_interface_ip($ubif);
if (!empty($intip)) {
$bindints .= "interface: $intip\n";
Expand All @@ -192,7 +192,7 @@ EOF;
if (!empty($config['unbound']['outgoing_interface'])) {
$outgoingints = "# Outgoing interfaces to be used\n";
$outgoing_interfaces = explode(",", $config['unbound']['outgoing_interface']);
foreach($outgoing_interfaces as $outif) {
foreach ($outgoing_interfaces as $outif) {
$outip = get_interface_ip($outif);
if (!empty($outip)) {
$outgoingints .= "outgoing-interface: $outip\n";
Expand Down Expand Up @@ -261,18 +261,12 @@ EOF;
if (isset($config['unbound']['forwarding'])) {
$dnsservers = array();
if (isset($config['system']['dnsallowoverride'])) {
$ns = array_unique(get_nameservers());
foreach($ns as $nameserver) {
if ($nameserver) {
$dnsservers[] = $nameserver;
}
foreach (get_nameservers() as $nameserver) {
$dnsservers[] = $nameserver;
}
} else {
$ns = array_unique(get_dns_servers());
foreach($ns as $nameserver) {
if ($nameserver) {
$dnsservers[] = $nameserver;
}
foreach (get_dns_servers() as $nameserver) {
$dnsservers[] = $nameserver;
}
}

Expand All @@ -283,7 +277,7 @@ forward-zone:
name: "."
EOD;
foreach($dnsservers as $dnsserver) {
foreach ($dnsservers as $dnsserver) {
$forward_conf .= "\tforward-addr: $dnsserver\n";
}
}
Expand Down Expand Up @@ -486,7 +480,7 @@ function unbound_add_domain_overrides($pvt = false)

$sorted_domains = msort($domains, "domain");
$result = array();
foreach($sorted_domains as $domain) {
foreach ($sorted_domains as $domain) {
$domain_key = current($domain);
if (!isset($result[$domain_key])) {
$result[$domain_key] = array();
Expand All @@ -496,7 +490,7 @@ function unbound_add_domain_overrides($pvt = false)

// Domain overrides that have multiple entries need multiple stub-addr: added
$domain_entries = "";
foreach($result as $domain=>$ips) {
foreach ($result as $domain => $ips) {
if ($pvt == true) {
$domain_entries .= "private-domain: \"$domain\"\n";
$domain_entries .= "domain-insecure: \"$domain\"\n";
Expand All @@ -506,7 +500,7 @@ function unbound_add_domain_overrides($pvt = false)
} else {
$domain_entries .= "stub-zone:\n";
$domain_entries .= "\tname: \"$domain\"\n";
foreach($ips as $ip) {
foreach ($ips as $ip) {
$domain_entries .= "\tstub-addr: $ip\n";
}
$domain_entries .= "\tstub-prime: no\n";
Expand Down Expand Up @@ -575,7 +569,7 @@ function unbound_add_host_entries()
if (isset($config['unbound']['hosts'])) {
$added_item = array();

foreach($config['unbound']['hosts'] as $host) {
foreach ($config['unbound']['hosts'] as $host) {
if ($host['host'] != "") {
$host['host'] = $host['host'].".";
}
Expand Down Expand Up @@ -701,7 +695,7 @@ function unbound_acls_config()
}

$bindints = "";
foreach($active_interfaces as $ubif => $ifdesc) {
foreach ($active_interfaces as $ubif => $ifdesc) {
$ifip = get_interface_ip($ubif);
if (!empty($ifip)) {
$subnet_bits = get_interface_subnet($ubif);
Expand All @@ -722,9 +716,9 @@ function unbound_acls_config()

// Configure the custom ACLs
if (isset($config['unbound']['acls'])) {
foreach($config['unbound']['acls'] as $unbound_acl) {
foreach ($config['unbound']['acls'] as $unbound_acl) {
$aclcfg .= "#{$unbound_acl['aclname']}\n";
foreach($unbound_acl['row'] as $network) {
foreach ($unbound_acl['row'] as $network) {
if ($unbound_acl['aclaction'] == "allow snoop") {
$unbound_acl['aclaction'] = "allow_snoop";
}
Expand Down
58 changes: 23 additions & 35 deletions src/etc/inc/system.inc
Original file line number Diff line number Diff line change
Expand Up @@ -161,37 +161,28 @@ function system_resolvconf_generate($verbose = false)
flush();
}

// Do not create blank domain lines, it breaks tools like dig.
if($syscfg['domain']) {
if (!empty($syscfg['domain'])) {
$resolvconf = "domain {$syscfg['domain']}\n";
}

if (((isset($config['dnsmasq']['enable']) && (empty($config['dnsmasq']['interface']) || in_array("lo0", explode(",", $config['dnsmasq']['interface']))))
|| (isset($config['unbound']['enable'])) && (empty($config['unbound']['active_interface']) || in_array("lo0", explode(",", $config['unbound']['active_interface']))))
&& !isset($config['system']['dnslocalhost'])) {
$resolvconf .= "nameserver 127.0.0.1\n";
}
$resolvconf .= "nameserver 127.0.0.1\n";
}

if (isset($syscfg['dnsallowoverride'])) {
/* get dynamically assigned DNS servers (if any) */
$ns = array_unique(get_searchdomains());
foreach($ns as $searchserver) {
if($searchserver) {
$resolvconf .= "search {$searchserver}\n";
}
foreach (get_searchdomains() as $searchserver) {
$resolvconf .= "search {$searchserver}\n";
}
$ns = array_unique(get_nameservers());
foreach($ns as $nameserver) {
if($nameserver) {
$resolvconf .= "nameserver $nameserver\n";
}
foreach (get_nameservers() as $nameserver) {
$resolvconf .= "nameserver $nameserver\n";
}
}
if (isset($syscfg['dnsserver']) && is_array($syscfg['dnsserver'])) {

if (isset($syscfg['dnsserver'][0])) {
foreach ($syscfg['dnsserver'] as $ns) {
if ($ns) {
$resolvconf .= "nameserver $ns\n";
}
$resolvconf .= "nameserver $ns\n";
}
}

Expand Down Expand Up @@ -283,38 +274,36 @@ function get_zoneinfo()

function get_searchdomains()
{
global $config;

$master_list = array();

// Read in dhclient nameservers
$search_list = glob("/var/etc/searchdomain_*");
$search_list = glob('/var/etc/searchdomain_*');

if (is_array($search_list)) {
foreach($search_list as $fdns) {
foreach ($search_list as $fdns) {
$contents = file($fdns, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
if (!is_array($contents)) {
continue;
}
foreach ($contents as $dns) {
if(is_hostname($dns)) {
if (!empty($dns) && is_hostname($dns)) {
$master_list[] = $dns;
}
}
}
}

return $master_list;
return array_unique($master_list);
}

function get_dns_servers()
{
$dns_servers = array();

$dns_s = file("/etc/resolv.conf", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$dns_s = file('/etc/resolv.conf', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

foreach($dns_s as $dns) {
$matches = "";
if (preg_match("/nameserver (.*)/", $dns, $matches)) {
$matches = '';
if (preg_match('/nameserver ([^ ]+)/', $dns, $matches)) {
$dns_servers[] = $matches[1];
}
}
Expand All @@ -324,26 +313,25 @@ function get_dns_servers()

function get_nameservers()
{
global $config;
$master_list = array();

// Read in dhclient nameservers
$dns_lists = glob("/var/etc/nameserver_*");
$dns_lists = glob('/var/etc/nameserver_*');

if (is_array($dns_lists)) {
foreach($dns_lists as $fdns) {
foreach ($dns_lists as $fdns) {
$contents = file($fdns, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
if (!is_array($contents)) {
continue;
}
foreach ($contents as $dns) {
if(is_ipaddr($dns)) {
if (!empty($dns) && is_ipaddr($dns)) {
$master_list[] = $dns;
}
}
}
}

return $master_list;
return array_unique($master_list);
}

function system_hosts_generate($verbose = false)
Expand Down

0 comments on commit c945095

Please sign in to comment.