From e963f315eb8afff727703b65b38c38e99f756404 Mon Sep 17 00:00:00 2001 From: Franco Fichtner Date: Wed, 30 Jun 2021 11:46:25 +0200 Subject: [PATCH] dhcp: last round of changes; closes #4642 o Move the IPv6 recompress to dhcpd_staticmap() o Add DHCPv4 leases page as consumer of dhcpd_staticmap() o Emit the MAC address in IPv4 case in dhcpd_staticmap() o Let dhcpd_staticmap() emit valid entries with an IP address o Check for required hostname in Dnsmasq and Unbound integration --- src/etc/inc/plugins.inc.d/dhcpd.inc | 15 ++++++-- src/etc/inc/plugins.inc.d/dnsmasq.inc | 4 +++ src/etc/inc/plugins.inc.d/unbound.inc | 4 +++ src/www/status_dhcp_leases.php | 49 ++++++++++++++------------- src/www/status_dhcpv6_leases.php | 4 +-- 5 files changed, 47 insertions(+), 29 deletions(-) diff --git a/src/etc/inc/plugins.inc.d/dhcpd.inc b/src/etc/inc/plugins.inc.d/dhcpd.inc index 15ba97192..1c62c14d7 100644 --- a/src/etc/inc/plugins.inc.d/dhcpd.inc +++ b/src/etc/inc/plugins.inc.d/dhcpd.inc @@ -1905,14 +1905,21 @@ function dhcpd_staticmap($domain_fallback = 'not.found', $ifconfig_details = nul interfaces_primary_address6($dhcpif, null, $ifconfig_details) : [null]; foreach ($dhcpifconf['staticmap'] as $host) { - if (empty($host[$ipaddr]) || empty($host['hostname'])) { + if (empty($host[$ipaddr])) { + /* we only return proper entries with an IP address */ continue; } if (!empty($ipaddrv6)) { + /* expand IPv6 suffix address */ $host['ipaddrv6'] = make_ipv6_64_address($ipaddrv6, $host['ipaddrv6']); } + if (!empty($host['ipaddrv6'])) { + /* avoid sloppy input by recompressing the address correctly */ + $host['ipaddrv6'] = Net_IPv6::compress(Net_IPv6::uncompress($host['ipaddrv6'])); + } + // XXX: dhcpdv6 domain entries have been superseded by domainsearchlist, // for backward compatibilty support both here. if (!empty($ipaddrv6) && !empty($host['domainsearchlist'])) { @@ -1938,8 +1945,10 @@ function dhcpd_staticmap($domain_fallback = 'not.found', $ifconfig_details = nul $ipaddr => $host[$ipaddr], ]; - if (isset($host['duid'])) { - $entry['duid'] = $host['duid']; + foreach (['mac', 'duid'] as $property) { + if (isset($host[$property])) { + $entry[$property] = $host[$property]; + } } $staticmap[] = $entry; diff --git a/src/etc/inc/plugins.inc.d/dnsmasq.inc b/src/etc/inc/plugins.inc.d/dnsmasq.inc index 69c90ee08..241d69b1b 100644 --- a/src/etc/inc/plugins.inc.d/dnsmasq.inc +++ b/src/etc/inc/plugins.inc.d/dnsmasq.inc @@ -233,6 +233,10 @@ function _dnsmasq_add_host_entries() } foreach (dhcpd_staticmap($domain, legacy_interfaces_details()) as $host) { + if (empty($host['hostname'])) { + /* cannot register without a hostname */ + continue; + } if (isset($host['ipaddr'])) { $dhosts .= "{$host['ipaddr']}\t{$host['hostname']}.{$host['domain']} {$host['hostname']}\n"; } else { diff --git a/src/etc/inc/plugins.inc.d/unbound.inc b/src/etc/inc/plugins.inc.d/unbound.inc index f108dedd5..0d97ad7e4 100644 --- a/src/etc/inc/plugins.inc.d/unbound.inc +++ b/src/etc/inc/plugins.inc.d/unbound.inc @@ -655,6 +655,10 @@ function unbound_add_host_entries($ifconfig_details = null) require_once 'plugins.inc.d/dhcpd.inc'; /* XXX */ foreach (dhcpd_staticmap($config['system']['domain'], $ifconfig_details) as $host) { + if (empty($host['hostname'])) { + /* cannot register without a hostname */ + continue; + } if (isset($host['ipaddr'])) { $unbound_entries .= "local-data-ptr: \"{$host['ipaddr']} {$host['hostname']}.{$host['domain']}\"\n"; $unbound_entries .= "local-data: \"{$host['hostname']}.{$host['domain']} IN A {$host['ipaddr']}\"\n"; diff --git a/src/www/status_dhcp_leases.php b/src/www/status_dhcp_leases.php index e00e36469..cc644da85 100644 --- a/src/www/status_dhcp_leases.php +++ b/src/www/status_dhcp_leases.php @@ -1,7 +1,7 @@ * Copyright (C) 2003-2004 Manuel Kasper * All rights reserved. @@ -201,7 +201,6 @@ if ($_SERVER['REQUEST_METHOD'] === 'GET') { /* remove the old array */ unset($lease_content); - /* remove duplicate items by mac address */ if (count($leases) > 0) { $leases = remove_duplicate($leases,"ip"); } @@ -217,30 +216,32 @@ if ($_SERVER['REQUEST_METHOD'] === 'GET') { $macs[$this_lease['mac']] = $i; } } - foreach ($interfaces as $ifname => $ifarr) { - if (isset($config['dhcpd'][$ifname]['staticmap'])) { - foreach($config['dhcpd'][$ifname]['staticmap'] as $static) { - $slease = array(); - $slease['ip'] = $static['ipaddr']; - $slease['type'] = "static"; - $slease['mac'] = $static['mac']; - $slease['start'] = ''; - $slease['end'] = ''; - $slease['hostname'] = $static['hostname']; - $slease['descr'] = $static['descr']; - $slease['act'] = "static"; - $slease['online'] = in_array(strtolower($slease['mac']), $arpdata_mac) ? 'online' : 'offline'; - if (isset($macs[$slease['mac']])) { - // update lease with static data - foreach ($slease as $key => $value) { - if (!empty($value)) { - $leases[$macs[$slease['mac']]][$key] = $slease[$key]; - } - } - } else { - $leases[] = $slease; + + foreach (dhcpd_staticmap() as $static) { + if (!isset($static['ipaddr'])) { + continue; + } + + $slease = array(); + $slease['ip'] = $static['ipaddr']; + $slease['type'] = 'static'; + $slease['mac'] = $static['mac']; + $slease['start'] = ''; + $slease['end'] = ''; + $slease['hostname'] = $static['hostname']; + $slease['descr'] = $static['descr']; + $slease['act'] = 'static'; + $slease['online'] = in_array(strtolower($slease['mac']), $arpdata_mac) ? 'online' : 'offline'; + + if (isset($macs[$slease['mac']])) { + /* update lease with static data */ + foreach ($slease as $key => $value) { + if (!empty($value)) { + $leases[$macs[$slease['mac']]][$key] = $slease[$key]; } } + } else { + $leases[] = $slease; } } diff --git a/src/www/status_dhcpv6_leases.php b/src/www/status_dhcpv6_leases.php index c7e13baaf..6dfda511b 100644 --- a/src/www/status_dhcpv6_leases.php +++ b/src/www/status_dhcpv6_leases.php @@ -1,7 +1,7 @@ * Copyright (C) 2011 Seth Mos * Copyright (C) 2003-2004 Manuel Kasper @@ -301,7 +301,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'GET') { } $slease = []; - $slease['ip'] = Net_IPv6::compress($static['ipaddrv6']); + $slease['ip'] = $static['ipaddrv6']; $slease['if'] = $static['interface']; $slease['type'] = 'static'; $slease['duid'] = $static['duid'];