Commit a670d05c authored by Franco Fichtner's avatar Franco Fichtner

dnsmasq: extract dnsmasq_hosts_generate()

parent ee09677b
......@@ -188,3 +188,85 @@ function dnsmasq_configure_do($verbose = false)
echo "done.\n";
}
}
function dnsmasq_hosts_generate()
{
global $config;
if (!dnsmasq_enabled()) {
return '';
}
$dnsmasqcfg = $config['dnsmasq'];
$lhosts = '';
$dhosts = '';
if (!isset($dnsmasqcfg['hosts']) || !is_array($dnsmasqcfg['hosts'])) {
$dnsmasqcfg['hosts'] = array();
}
foreach ($dnsmasqcfg['hosts'] as $host) {
if ($host['host']) {
$lhosts .= "{$host['ip']} {$host['host']}.{$host['domain']} {$host['host']}\n";
} else {
$lhosts .= "{$host['ip']} {$host['domain']}\n";
}
if (!isset($host['aliases']) || !is_array($host['aliases']) || !is_array($host['aliases']['item'])) {
continue;
}
foreach ($host['aliases']['item'] as $alias) {
if ($alias['host']) {
$lhosts .= "{$host['ip']} {$alias['host']}.{$alias['domain']} {$alias['host']}\n";
} else {
$lhosts .= "{$host['ip']} {$alias['domain']}\n";
}
}
}
if (isset($dnsmasqcfg['regdhcpstatic']) && is_array($config['dhcpd'])) {
foreach ($config['dhcpd'] as $dhcpif => $dhcpifconf) {
if (isset($dhcpifconf['staticmap']) && isset($dhcpifconf['enable'])) {
foreach ($dhcpifconf['staticmap'] as $host) {
if (!$host['ipaddr'] || !$host['hostname']) {
continue;
}
$domain = $config['system']['domain'];
if ($host['domain']) {
$domain = $host['domain'];
} elseif ($dhcpifconf['domain']) {
$domain = $dhcpifconf['domain'];
}
$dhosts .= "{$host['ipaddr']} {$host['hostname']}.{$domain} {$host['hostname']}\n";
}
}
}
}
if (isset($dnsmasqcfg['regdhcpstatic']) && is_array($config['dhcpdv6'])) {
foreach ($config['dhcpdv6'] as $dhcpif => $dhcpifconf) {
if (isset($dhcpifconf['staticmap']) && isset($dhcpifconf['enable'])) {
foreach ($dhcpifconf['staticmap'] as $host) {
if (!$host['ipaddrv6'] || !$host['hostname']) {
continue;
}
$domain = $config['system']['domain'];
if ($host['domain']) {
$domain = $host['domain'];
} elseif ($dhcpifconf['domain']) {
$domain = $dhcpifconf['domain'];
}
$dhosts .= "{$host['ipaddrv6']} {$host['hostname']}.{$domain} {$host['hostname']}\n";
}
}
}
}
if (isset($dnsmasqcfg['dhcpfirst'])) {
return $dhosts . $lhosts;
}
return $lhosts . $dhosts;
}
......@@ -363,11 +363,8 @@ function system_hosts_generate()
global $config;
$syscfg = $config['system'];
$dnsmasqcfg = $config['dnsmasq'];
$hosts = "127.0.0.1 localhost localhost.{$syscfg['domain']}\n";
$lhosts = "";
$dhosts = "";
if (isset($config['interfaces']['lan'])) {
$cfgip = get_interface_ip("lan");
......@@ -387,89 +384,17 @@ function system_hosts_generate()
}
}
if (isset($dnsmasqcfg['enable'])) {
if (!isset($dnsmasqcfg['hosts']) || !is_array($dnsmasqcfg['hosts'])) {
$dnsmasqcfg['hosts'] = array();
}
foreach ($dnsmasqcfg['hosts'] as $host) {
if ($host['host']) {
$lhosts .= "{$host['ip']} {$host['host']}.{$host['domain']} {$host['host']}\n";
} else {
$lhosts .= "{$host['ip']} {$host['domain']}\n";
}
if (!isset($host['aliases']) || !is_array($host['aliases']) || !is_array($host['aliases']['item'])) {
continue;
}
foreach ($host['aliases']['item'] as $alias) {
if ($alias['host']) {
$lhosts .= "{$host['ip']} {$alias['host']}.{$alias['domain']} {$alias['host']}\n";
} else {
$lhosts .= "{$host['ip']} {$alias['domain']}\n";
}
}
}
if (isset($dnsmasqcfg['regdhcpstatic']) && is_array($config['dhcpd'])) {
foreach ($config['dhcpd'] as $dhcpif => $dhcpifconf) {
if (isset($dhcpifconf['staticmap']) && isset($dhcpifconf['enable'])) {
foreach ($dhcpifconf['staticmap'] as $host) {
if (!$host['ipaddr'] || !$host['hostname']) {
continue;
}
$domain = $syscfg['domain'];
if ($host['domain']) {
$domain = $host['domain'];
} elseif ($dhcpifconf['domain']) {
$domain = $dhcpifconf['domain'];
}
$dhosts .= "{$host['ipaddr']} {$host['hostname']}.{$domain} {$host['hostname']}\n";
}
}
}
}
if (isset($dnsmasqcfg['regdhcpstatic']) && is_array($config['dhcpdv6'])) {
foreach ($config['dhcpdv6'] as $dhcpif => $dhcpifconf) {
if (isset($dhcpifconf['staticmap']) && isset($dhcpifconf['enable'])) {
foreach ($dhcpifconf['staticmap'] as $host) {
if (!$host['ipaddrv6'] || !$host['hostname']) {
continue;
}
$domain = $config['system']['domain'];
if ($host['domain']) {
$domain = $host['domain'];
} elseif ($dhcpifconf['domain']) {
$domain = $dhcpifconf['domain'];
}
$dhosts .= "{$host['ipaddrv6']} {$host['hostname']}.{$domain} {$host['hostname']}\n";
}
}
}
}
if (isset($dnsmasqcfg['dhcpfirst'])) {
$hosts .= $dhosts . $lhosts;
} else {
$hosts .= $lhosts . $dhosts;
}
}
/* XXX currenty taints the hosts file, unbound doesn't */
hosts .= dnsmasq_hosts_generate();
unbound_hosts_generate();
/*
* Do not remove this because dhcpleases monitors with kqueue
* it needs to be * killed before writing to hosts files.
* it needs to be killed before writing to hosts files.
*/
killbypid('/var/run/dhcpleases.pid', 'TERM', true);
$fd = fopen('/etc/hosts', 'w');
if ($fd) {
fwrite($fd, $hosts);
fclose($fd);
}
unbound_hosts_generate();
file_put_contents('/etc/hosts', $hosts);
}
function system_hostname_configure()
......
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