Merge pull request #1714 from RaspAP/fix/dhcp-roundup

Roundup of fixes for DHCP handling
This commit is contained in:
Bill Zimmerman 2024-12-17 13:15:55 -08:00 committed by GitHub
commit 80be4d34af
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 36 additions and 32 deletions

View File

@ -18,14 +18,14 @@ if (isset($interface)) {
} else { } else {
$arrRange = explode(",", $conf['dhcp-range'][0]); $arrRange = explode(",", $conf['dhcp-range'][0]);
} }
$dhcpdata['RangeStart'] = $arrRange[0]; $dhcpdata['RangeStart'] = $arrRange[0] ?? null;
$dhcpdata['RangeEnd'] = $arrRange[1]; $dhcpdata['RangeEnd'] = $arrRange[1] ?? null;
$dhcpdata['RangeMask'] = $arrRange[2]; $dhcpdata['RangeMask'] = $arrRange[2] ?? null;
$dhcpdata['leaseTime'] = $arrRange[3]; $dhcpdata['leaseTime'] = $arrRange[3] ?? null;
$dhcpHost = $conf["dhcp-host"]; $dhcpHost = $conf["dhcp-host"] ?? null;
$dhcpHost = empty($dhcpHost) ? [] : $dhcpHost; $dhcpHost = empty($dhcpHost) ? [] : $dhcpHost;
$dhcpdata['dhcpHost'] = is_array($dhcpHost) ? $dhcpHost : [ $dhcpHost ]; $dhcpdata['dhcpHost'] = is_array($dhcpHost) ? $dhcpHost : [ $dhcpHost ];
$upstreamServers = is_array($conf['server']) ? $conf['server'] : [ $conf['server'] ]; $upstreamServers = is_array($conf['server'] ?? null) ? $conf['server'] : [ $conf['server'] ?? '' ];
$dhcpdata['upstreamServersEnabled'] = empty($conf['server']) ? false: true; $dhcpdata['upstreamServersEnabled'] = empty($conf['server']) ? false: true;
$dhcpdata['upstreamServers'] = array_filter($upstreamServers); $dhcpdata['upstreamServers'] = array_filter($upstreamServers);
preg_match('/([0-9]*)([a-z])/i', $dhcpdata['leaseTime'], $arrRangeLeaseTime); preg_match('/([0-9]*)([a-z])/i', $dhcpdata['leaseTime'], $arrRangeLeaseTime);
@ -35,10 +35,10 @@ if (isset($interface)) {
$arrDns = explode(",", $conf['dhcp-option']); $arrDns = explode(",", $conf['dhcp-option']);
if ($arrDns[0] == '6') { if ($arrDns[0] == '6') {
if (count($arrDns) > 1) { if (count($arrDns) > 1) {
$dhcpdata['DNS1'] = $arrDns[1]; $dhcpdata['DNS1'] = $arrDns[1] ?? null;
} }
if (count($arrDns) > 2) { if (count($arrDns) > 2) {
$dhcpdata['DNS2'] = $arrDns[2]; $dhcpdata['DNS2'] = $arrDns[2] ?? null;
} }
} }
} }
@ -53,13 +53,15 @@ if (isset($interface)) {
preg_match('/fallback\sstatic_'.$interface.'/', $matched[0], $fallback); preg_match('/fallback\sstatic_'.$interface.'/', $matched[0], $fallback);
preg_match('/(?:no)?gateway/', $matched[0], $gateway); preg_match('/(?:no)?gateway/', $matched[0], $gateway);
preg_match('/nohook\swpa_supplicant/', $matched[0], $nohook_wpa_supplicant); preg_match('/nohook\swpa_supplicant/', $matched[0], $nohook_wpa_supplicant);
$dhcpdata['Metric'] = $metric[1]; $dhcpdata['Metric'] = $metric[1] ?? null;
$dhcpdata['StaticIP'] = strpos($static_ip[1],'/') ? substr($static_ip[1], 0, strpos($static_ip[1],'/')) : $static_ip[1]; $dhcpdata['StaticIP'] = isset($static_ip[1]) && strpos($static_ip[1], '/') !== false
$dhcpdata['SubnetMask'] = cidr2mask($static_ip[1]); ? substr($static_ip[1], 0, strpos($static_ip[1], '/'))
$dhcpdata['StaticRouters'] = $static_routers[1]; : ($static_ip[1] ?? '');
$dhcpdata['StaticDNS'] = $static_dns[1]; $dhcpdata['SubnetMask'] = cidr2mask($static_ip[1] ?? '');
$dhcpdata['StaticRouters'] = $static_routers[1] ?? null;
$dhcpdata['StaticDNS'] = $static_dns[1] ?? null;
$dhcpdata['FallbackEnabled'] = empty($fallback) ? false: true; $dhcpdata['FallbackEnabled'] = empty($fallback) ? false: true;
$dhcpdata['DefaultRoute'] = $gateway[0] == "gateway"; $dhcpdata['DefaultRoute'] = $gateway[0] == "gateway";
$dhcpdata['NoHookWPASupplicant'] = $nohook_wpa_supplicant[0] == "nohook wpa_supplicant"; $dhcpdata['NoHookWPASupplicant'] = ($nohook_wpa_supplicant[0] ?? '') == "nohook wpa_supplicant";
echo json_encode($dhcpdata); echo json_encode($dhcpdata);
} }

View File

@ -52,8 +52,8 @@ function DisplayDHCPConfig()
$conf = ParseConfig($return); $conf = ParseConfig($return);
exec('cat '. RASPI_DNSMASQ_PREFIX.$ap_iface.'.conf', $return); exec('cat '. RASPI_DNSMASQ_PREFIX.$ap_iface.'.conf', $return);
$conf = array_merge(ParseConfig($return)); $conf = array_merge(ParseConfig($return));
$hosts = (array)$conf['dhcp-host']; $hosts = (array)($conf['dhcp-host'] ?? []);
$upstreamServers = (array)$conf['server']; $upstreamServers = (array)($conf['server'] ?? []);
exec("ip -o link show | awk -F': ' '{print $2}'", $interfaces); exec("ip -o link show | awk -F': ' '{print $2}'", $interfaces);
exec('cat ' . RASPI_DNSMASQ_LEASES, $leases); exec('cat ' . RASPI_DNSMASQ_LEASES, $leases);
@ -280,10 +280,10 @@ function updateDnsmasqConfig($iface,$status)
$config .='log-facility='.RASPI_DHCPCD_LOG.PHP_EOL; $config .='log-facility='.RASPI_DHCPCD_LOG.PHP_EOL;
$config .='conf-dir=/etc/dnsmasq.d'.PHP_EOL; $config .='conf-dir=/etc/dnsmasq.d'.PHP_EOL;
// handle log option // handle log option
if ($_POST['log-dhcp'] == "1") { if (($_POST['log-dhcp'] ?? '') == "1") {
$config .= "log-dhcp".PHP_EOL; $config .= "log-dhcp".PHP_EOL;
} }
if ($_POST['log-queries'] == "1") { if (($_POST['log-queries'] ?? '') == "1") {
$config .= "log-queries".PHP_EOL; $config .= "log-queries".PHP_EOL;
} }
$config .= PHP_EOL; $config .= PHP_EOL;
@ -317,12 +317,12 @@ function updateDHCPConfig($iface,$status)
if ($_POST['Metric'] !== '') { if ($_POST['Metric'] !== '') {
$cfg[] = 'metric '.$_POST['Metric']; $cfg[] = 'metric '.$_POST['Metric'];
} }
if ($_POST['Fallback'] == 1) { if (($_POST['Fallback'] ?? 0) == 1) {
$cfg[] = 'profile static_'.$iface; $cfg[] = 'profile static_'.$iface;
$cfg[] = 'fallback static_'.$iface; $cfg[] = 'fallback static_'.$iface;
} }
$cfg[] = $_POST['DefaultRoute'] == '1' ? 'gateway' : 'nogateway'; $cfg[] = ($_POST['DefaultRoute'] ?? '') == '1' ? 'gateway' : 'nogateway';
if (( substr($iface, 0, 2) === "wl") && $_POST['NoHookWPASupplicant'] == '1') { if (substr($iface, 0, 2) === "wl" && ($_POST['NoHookWPASupplicant'] ?? '') == '1') {
$cfg[] = 'nohook wpa_supplicant'; $cfg[] = 'nohook wpa_supplicant';
} }
$dhcp_cfg = file_get_contents(RASPI_DHCPCD_CONFIG); $dhcp_cfg = file_get_contents(RASPI_DHCPCD_CONFIG);

View File

@ -54,7 +54,7 @@ function cidr2mask($cidr)
{ {
$ipParts = explode('/', $cidr); $ipParts = explode('/', $cidr);
$ip = $ipParts[0]; $ip = $ipParts[0];
$prefixLength = $ipParts[1]; $prefixLength = $ipParts[1] ?? null;
$ipLong = ip2long($ip); $ipLong = ip2long($ip);
$netmaskLong = bindec(str_pad(str_repeat('1', $prefixLength), 32, '0')); $netmaskLong = bindec(str_pad(str_repeat('1', $prefixLength), 32, '0'));
@ -338,9 +338,9 @@ function CSRFValidate()
{ {
if(isset($_POST['csrf_token'])) { if(isset($_POST['csrf_token'])) {
$post_token = $_POST['csrf_token']; $post_token = $_POST['csrf_token'];
$header_token = $_SERVER['HTTP_X_CSRF_TOKEN']; $header_token = $_SERVER['HTTP_X_CSRF_TOKEN'] ?? null;
if (empty($post_token) && empty($header_token)) { if (empty($post_token) && is_null($header_token)) {
return false; return false;
} }
$request_token = $post_token; $request_token = $post_token;
@ -458,6 +458,9 @@ function ParseConfig($arrConfig, $wg = false)
if (strpos($line, "=") !== false) { if (strpos($line, "=") !== false) {
list($option, $value) = array_map("trim", explode("=", $line, 2)); list($option, $value) = array_map("trim", explode("=", $line, 2));
} else {
$option = $line;
$value = "";
} }
if (empty($config[$option])) { if (empty($config[$option])) {
$config[$option] = $value ?: true; $config[$option] = $value ?: true;

View File

@ -7,7 +7,7 @@
<div class="input-group"> <div class="input-group">
<input type="hidden" name="no-resolv" value="0"> <input type="hidden" name="no-resolv" value="0">
<div class="form-check form-switch"> <div class="form-check form-switch">
<input class="form-check-input" id="no-resolv" type="checkbox" name="no-resolv" value="1" <?php echo $conf['no-resolv'] ? ' checked="checked"' : "" ?> aria-describedby="no-resolv-description"> <input class="form-check-input" id="no-resolv" type="checkbox" name="no-resolv" value="1" <?php echo ($conf['no-resolv'] ?? false) ? ' checked="checked"' : '' ?> aria-describedby="no-resolv-description">
<label class="form-check-label" for="no-resolv"><?php echo _("Only ever query DNS servers configured below") ?></label> <label class="form-check-label" for="no-resolv"><?php echo _("Only ever query DNS servers configured below") ?></label>
</div> </div>
<p id="no-resolv-description"> <p id="no-resolv-description">
@ -19,9 +19,8 @@
<div class="js-dhcp-upstream-servers"> <div class="js-dhcp-upstream-servers">
<?php foreach ($upstreamServers as $server): ?> <?php foreach ($upstreamServers as $server): ?>
<div class="mb-3 input-group input-group-sm js-dhcp-upstream-server"> <div class="mb-3 input-group input-group-sm js-dhcp-upstream-server">
<input type="text" class="form-control" name="server[]" value="<?php echo $server ?>"> <input type="text" class="form-control" name="server[]" value="<?php echo $server; ?>">
<div class="input-group-text js-remove-dhcp-upstream-server"><i class="fas fa-minus"></i></div> <div class="input-group-text js-remove-dhcp-upstream-server"><i class="fas fa-minus"></i></div>
</div>
</div> </div>
<?php endforeach ?> <?php endforeach ?>
</div> </div>

View File

@ -3,7 +3,7 @@
<div class="row"> <div class="row">
<div class="mb-3 col-md-6"> <div class="mb-3 col-md-6">
<label for="code">Interface</label> <label for="code">Interface</label>
<?php SelectorOptions('interface', $interfaces, $ap_iface, 'cbxdhcpiface', 'loadInterfaceDHCPSelect', $DHCPDisabled); ?> <?php SelectorOptions('interface', $interfaces, $ap_iface, 'cbxdhcpiface', 'loadInterfaceDHCPSelect'); ?>
</div> </div>
</div> </div>

View File

@ -4,11 +4,11 @@
<p><?php echo _("Enable these options to log <code>dhcpcd</code> and <code>dnsmasq</code> activity.") ?></p> <p><?php echo _("Enable these options to log <code>dhcpcd</code> and <code>dnsmasq</code> activity.") ?></p>
<div class="form-check form-switch"> <div class="form-check form-switch">
<input class="form-check-input" id="log-dhcp" type="checkbox" name="log-dhcp" value="1" <?php echo $conf['log-dhcp'] ? ' checked="checked"' : "" ?> aria-describedby="log-dhcp-requests"> <input class="form-check-input" id="log-dhcp" type="checkbox" name="log-dhcp" value="1" <?php echo !empty($conf['log-dhcp']) ? ' checked="checked"' : "" ?> aria-describedby="log-dhcp-requests">
<label class="form-check-label" for="log-dhcp"><?php echo _("Log DHCP requests") ?></label> <label class="form-check-label" for="log-dhcp"><?php echo _("Log DHCP requests") ?></label>
</div> </div>
<div class="form-check form-switch"> <div class="form-check form-switch">
<input class="form-check-input" id="log-queries" type="checkbox" name="log-queries" value="1" <?php echo $conf['log-queries'] ? ' checked="checked"' : "" ?> aria-describedby="log-dhcp-queries"> <input class="form-check-input" id="log-queries" type="checkbox" name="log-queries" value="1" <?php echo !empty($conf['log-queries']) ? ' checked="checked"' : "" ?> aria-describedby="log-dhcp-queries">
<label class="form-check-label align-middle" for="log-queries"><?php echo _("Log DNS queries") ?></label> <label class="form-check-label align-middle" for="log-queries"><?php echo _("Log DNS queries") ?></label>
<input type="button" class="btn btn-outline btn-warning btn-sm align-top ms-4" id="js-cleardnsmasq-log" value="<?php echo _("Clear log"); ?>" /> <input type="button" class="btn btn-outline btn-warning btn-sm align-top ms-4" id="js-cleardnsmasq-log" value="<?php echo _("Clear log"); ?>" />
</div> </div>
@ -16,7 +16,7 @@
<div class="row"> <div class="row">
<div class="mb-3 col-md-8 mt-2"> <div class="mb-3 col-md-8 mt-2">
<?php <?php
if ($conf['log-dhcp'] == 1 || $conf['log-queries'] == 1) { if (($conf['log-dhcp'] ?? 0) == 1 || ($conf['log-queries'] ?? 0) == 1) {
echo '<textarea class="logoutput text-secondary" id="dnsmasq-log">'.htmlspecialchars($logdata, ENT_QUOTES).'</textarea>'; echo '<textarea class="logoutput text-secondary" id="dnsmasq-log">'.htmlspecialchars($logdata, ENT_QUOTES).'</textarea>';
} else { } else {
echo '<textarea class="logoutput my-3"></textarea>'; echo '<textarea class="logoutput my-3"></textarea>';

View File

@ -47,7 +47,7 @@
<div class="input-group"> <div class="input-group">
<input type="hidden" name="dhcp-ignore" value="0"> <input type="hidden" name="dhcp-ignore" value="0">
<div class="form-check form-switch"> <div class="form-check form-switch">
<input class="form-check-input" id="dhcp-ignore" type="checkbox" name="dhcp-ignore" value="1" <?php echo $conf['dhcp-ignore'] ? ' checked="checked"' : "" ?> aria-describedby="dhcp-ignore-description"> <input class="form-check-input" id="dhcp-ignore" type="checkbox" name="dhcp-ignore" value="1" <?php echo !empty($conf['dhcp-ignore']) ? ' checked="checked"' : "" ?> aria-describedby="dhcp-ignore-description">
<label class="form-check-label" for="dhcp-ignore"><?php echo _("Limit network access to static clients") ?></label> <label class="form-check-label" for="dhcp-ignore"><?php echo _("Limit network access to static clients") ?></label>
</div> </div>
<p id="dhcp-ignore-description"> <p id="dhcp-ignore-description">