get_nameservers(): also return manual dns entries to simplify code elsewhere

This commit is contained in:
Stephan de Wit 2022-03-25 17:20:36 +01:00 committed by Franco Fichtner
parent 57097e20ac
commit 8d3584be16
4 changed files with 61 additions and 60 deletions

View File

@ -259,19 +259,7 @@ EOF;
$forward_local = '';
$resolv_conf_root = '';
if (isset($config['unbound']['forwarding'])) {
$dnsservers = array();
if (isset($config['system']['dnsallowoverride'])) {
foreach (get_nameservers() as $nameserver) {
$dnsservers[] = $nameserver;
}
}
if (!empty($config['system']['dnsserver'][0])) {
foreach ($config['system']['dnsserver'] as $nameserver) {
$dnsservers[] = $nameserver;
}
}
$dnsservers = get_nameservers();
if (!empty($dnsservers)) {
$forward_conf .= <<<EOD

View File

@ -201,15 +201,14 @@ function system_resolvconf_generate($verbose = false)
if (isset($syscfg['dnsallowoverride'])) {
$search = array_merge($search, get_searchdomains());
foreach (get_nameservers(null, true) as $nameserver) {
$resolvconf .= "nameserver {$nameserver['host']}\n";
$routes[] = $nameserver;
}
}
if (isset($syscfg['dnsserver'][0])) {
foreach ($syscfg['dnsserver'] as $ns) {
$resolvconf .= "nameserver $ns\n";
foreach (get_nameservers(null, true) as $dnsserver) {
$host = !empty($dnsserver['host']) ? $dnsserver['host'] : $dnsserver;
$resolvconf .= "nameserver {$host}\n";
if (!empty($dnsserver['gateway'])) {
$routes[] = $dnsserver;
}
}
@ -235,16 +234,6 @@ function system_resolvconf_generate($verbose = false)
rename($tempfile, '/etc/resolv.conf');
for ($dnscounter = 1; $dnscounter < 9; $dnscounter++) {
$dnsgw = "dns{$dnscounter}gw";
if (!empty($syscfg[$dnsgw])) {
$gwname = $syscfg[$dnsgw];
if ($gwname != 'none') {
$routes[] = ['host' => $syscfg['dnsserver'][$dnscounter - 1], 'gateway' => $gateways->getAddress($gwname)];
}
}
}
/* XXX check for overlapping host routes */
/* setup static routes for DNS servers as configured */
@ -340,8 +329,32 @@ function get_nameservers($interface = null, $with_gateway = false)
$master_list = array();
$dns_lists = glob('/tmp/*_nameserver*');
$dns_static = [];
$exclude_interfaces = [];
$gateways = new \OPNsense\Routing\Gateways(legacy_interfaces_details());
$syscfg = config_read_array('system');
for ($dnscounter = 1; $dnscounter < 9; $dnscounter++) {
$dnsgw = "dns{$dnscounter}gw";
if (!empty($syscfg[$dnsgw])) {
$gwname = $syscfg[$dnsgw];
$entry = $syscfg['dnsserver'][$dnscounter - 1];
if ($gwname != 'none') {
$dns_static[] = [
'host' => $entry,
'gw_addr' => $gateways->getAddress($gwname),
'if' => $gateways->getInterface($gwname)
];
} else if (!empty($entry)) {
$dns_static[] = $entry;
}
}
}
$dns_static_new = $dns_static;
if (!empty($interface)) {
/* only acquire servers provided for this interface */
$realif = get_real_interface($interface);
@ -350,8 +363,18 @@ function get_nameservers($interface = null, $with_gateway = false)
"/tmp/{$realif}_nameserver",
"/tmp/{$realifv6}_nameserverv6",
];
/* this interface can also be assigned multiple manual dns servers */
$dns_static_new = [];
foreach ($dns_static as $static) {
if (isset($static['if']) && $static['if'] == $realif) {
$dns_static_new[] = $static;
}
}
}
$dns_static = $dns_static_new;
if (isset($config['system']['dnsallowoverride_exclude'])) {
foreach (explode(',', $config['system']['dnsallowoverride_exclude']) as $intf) {
if (isset($config['interfaces'][$intf])) {
@ -362,6 +385,10 @@ function get_nameservers($interface = null, $with_gateway = false)
}
foreach ($dns_lists as $fdns) {
if (!isset($syscfg['dnsallowoverride'])) {
continue;
}
$intf = explode('_', basename($fdns))[0];
if (in_array($intf, $exclude_interfaces)) {
continue;
@ -381,7 +408,7 @@ function get_nameservers($interface = null, $with_gateway = false)
foreach ($contents as $dns) {
if (!empty($dns) && is_ipaddr($dns)) {
if ($with_gateway) {
$master_list[] = ['host' => $dns, 'gateway' => $gw];
$master_list[] = ['type' => 'dynamic', 'host' => $dns, 'gateway' => $gw];
} else {
$master_list[] = $dns;
}
@ -389,6 +416,18 @@ function get_nameservers($interface = null, $with_gateway = false)
}
}
foreach ($dns_static as $static) {
if (isset($static['if']) && in_array($static['if'], $exclude_interfaces)) {
continue;
}
if ($with_gateway && isset($static['gw_addr'])) {
$master_list[] = ['type' => 'static', 'host' => $static['host'], 'gateway' => $static['gw_addr']];
} else {
$master_list[] = isset($static['host']) ? $static['host'] : $static;
}
}
return array_unique($master_list, SORT_REGULAR);
}

View File

@ -78,18 +78,7 @@ class SettingsController extends ApiMutableModelControllerBase
$nameservers = json_decode(trim($backend->configdRun("system list nameservers")));
if ($nameservers !== null) {
$result = array();
$config = Config::getInstance()->object();
if (isset($config->system->dnsallowoverride)) {
foreach ($nameservers->dynamic as $dynamic) {
$result[] = $dynamic;
}
}
foreach ($nameservers->static as $static) {
$result[] = $static;
}
return $result;
return $nameservers;
}
}
return array("message" => "Unable to run configd action");

View File

@ -30,23 +30,8 @@
require_once 'config.inc';
require_once 'system.inc';
require_once 'util.inc';
require_once 'interfaces.inc';
use OPNsense\Core\Config;
$config = Config::getInstance()->object();
$result = array();
/* get dynamic nameservers */
foreach (get_nameservers() as $nameserver) {
$result["dynamic"][] = $nameserver;
}
/* get manually entered nameservers */
foreach ($config->system->children() as $key => $node) {
if ($key == "dnsserver") {
$result["static"][] = (string)$node;
}
}
$result = get_nameservers();
echo json_encode($result) . PHP_EOL;