2016-07-09 02:26:13 +02:00
|
|
|
<?php
|
|
|
|
|
2020-02-15 18:57:46 +01:00
|
|
|
require_once 'includes/status_messages.php';
|
2020-02-14 03:38:46 +01:00
|
|
|
require_once 'config.php';
|
2016-07-09 02:26:13 +02:00
|
|
|
|
|
|
|
/**
|
2020-02-15 18:57:46 +01:00
|
|
|
* Manage DHCP configuration
|
|
|
|
*/
|
2019-04-10 10:37:35 +02:00
|
|
|
function DisplayDHCPConfig()
|
|
|
|
{
|
2016-07-09 02:26:13 +02:00
|
|
|
|
2019-04-10 10:37:35 +02:00
|
|
|
$status = new StatusMessages();
|
2020-02-14 03:38:46 +01:00
|
|
|
if (!RASPI_MONITOR_ENABLED) {
|
|
|
|
if (isset($_POST['savedhcpdsettings'])) {
|
|
|
|
$errors = '';
|
|
|
|
define('IFNAMSIZ', 16);
|
2020-02-15 18:57:46 +01:00
|
|
|
if (!preg_match('/^[a-zA-Z0-9]+$/', $_POST['interface'])
|
|
|
|
|| strlen($_POST['interface']) >= IFNAMSIZ
|
|
|
|
) {
|
2020-02-14 03:38:46 +01:00
|
|
|
$errors .= _('Invalid interface name.').'<br />'.PHP_EOL;
|
|
|
|
}
|
2018-08-04 14:03:14 +02:00
|
|
|
|
2020-02-15 18:57:46 +01:00
|
|
|
if (!preg_match('/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\z/', $_POST['RangeStart'])
|
|
|
|
&& !empty($_POST['RangeStart'])
|
|
|
|
) { // allow ''/null ?
|
2020-02-14 03:38:46 +01:00
|
|
|
$errors .= _('Invalid DHCP range start.').'<br />'.PHP_EOL;
|
|
|
|
}
|
2018-08-04 14:03:14 +02:00
|
|
|
|
2020-02-15 18:57:46 +01:00
|
|
|
if (!preg_match('/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\z/', $_POST['RangeEnd'])
|
|
|
|
&& !empty($_POST['RangeEnd'])
|
|
|
|
) { // allow ''/null ?
|
2020-02-14 03:38:46 +01:00
|
|
|
$errors .= _('Invalid DHCP range end.').'<br />'.PHP_EOL;
|
|
|
|
}
|
2018-08-04 14:03:14 +02:00
|
|
|
|
2020-02-14 03:38:46 +01:00
|
|
|
if (!ctype_digit($_POST['RangeLeaseTime']) && $_POST['RangeLeaseTimeUnits'] !== 'infinite') {
|
|
|
|
$errors .= _('Invalid DHCP lease time, not a number.').'<br />'.PHP_EOL;
|
|
|
|
}
|
2018-08-04 14:03:14 +02:00
|
|
|
|
2020-02-14 03:38:46 +01:00
|
|
|
if (!in_array($_POST['RangeLeaseTimeUnits'], array('m', 'h', 'd', 'infinite'))) {
|
|
|
|
$errors .= _('Unknown DHCP lease time unit.').'<br />'.PHP_EOL;
|
2019-07-30 17:05:41 +02:00
|
|
|
}
|
2018-09-25 21:32:31 +02:00
|
|
|
|
2020-02-14 03:38:46 +01:00
|
|
|
$return = 1;
|
|
|
|
if (empty($errors)) {
|
|
|
|
$config = 'interface='.$_POST['interface'].PHP_EOL.
|
|
|
|
'dhcp-range='.$_POST['RangeStart'].','.$_POST['RangeEnd'].
|
|
|
|
',255.255.255.0,';
|
|
|
|
if ($_POST['RangeLeaseTimeUnits'] !== 'infinite') {
|
|
|
|
$config .= $_POST['RangeLeaseTime'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$config .= $_POST['RangeLeaseTimeUnits'].PHP_EOL;
|
2019-08-01 16:31:11 +02:00
|
|
|
|
2020-02-14 03:38:46 +01:00
|
|
|
for ($i=0; $i < count($_POST["static_leases"]["mac"]); $i++) {
|
|
|
|
$mac = trim($_POST["static_leases"]["mac"][$i]);
|
|
|
|
$ip = trim($_POST["static_leases"]["ip"][$i]);
|
|
|
|
if ($mac != "" && $ip != "") {
|
|
|
|
$config .= "dhcp-host=$mac,$ip".PHP_EOL;
|
|
|
|
}
|
2019-08-01 16:31:11 +02:00
|
|
|
}
|
2019-10-15 00:33:41 +02:00
|
|
|
|
2020-02-29 18:33:51 +01:00
|
|
|
if ($_POST['no-resolv'] == "1") {
|
|
|
|
$config .= "no-resolv".PHP_EOL;
|
|
|
|
}
|
2020-02-28 23:37:52 +01:00
|
|
|
foreach ($_POST['server'] as $server) {
|
|
|
|
$config .= "server=$server".PHP_EOL;
|
|
|
|
}
|
2020-03-05 11:35:40 +01:00
|
|
|
if ($_POST['log-dhcp'] == "1") {
|
|
|
|
$config .= "log-dhcp".PHP_EOL;
|
|
|
|
}
|
|
|
|
if ($_POST['log-queries'] == "1") {
|
|
|
|
$config .= "log-queries".PHP_EOL;
|
|
|
|
}
|
2020-02-15 18:57:46 +01:00
|
|
|
if ($_POST['DNS1']) {
|
2020-02-14 03:38:46 +01:00
|
|
|
$config .= "dhcp-option=6," . $_POST['DNS1'];
|
2020-02-15 18:57:46 +01:00
|
|
|
if ($_POST['DNS2']) {
|
2020-02-14 03:38:46 +01:00
|
|
|
$config .= ','.$_POST['DNS2'];
|
|
|
|
}
|
|
|
|
$config .= PHP_EOL;
|
2019-10-15 00:33:41 +02:00
|
|
|
}
|
|
|
|
|
2020-03-05 11:35:40 +01:00
|
|
|
$config .= "log-facility=/tmp/dnsmasq.log".PHP_EOL;
|
2020-02-26 02:48:29 +01:00
|
|
|
$config .= "conf-dir=/etc/dnsmasq.d".PHP_EOL;
|
2020-02-14 03:38:46 +01:00
|
|
|
file_put_contents("/tmp/dnsmasqdata", $config);
|
2020-11-09 18:48:32 +01:00
|
|
|
|
|
|
|
// handle DHCP for eth0 option
|
|
|
|
if ($_POST['dhcp-eth0'] == "1" && $_POST['interface'] == "eth0") {
|
2020-11-11 13:40:22 +01:00
|
|
|
if (!file_exists(RASPI_CONFIG_NETWORKING.'/eth0.ini') || filesize(RASPI_CONFIG_NETWORKING.'/eth0.ini') ==0 ) {
|
|
|
|
$status->addMessage('Static IP address for eth0 not found.', 'danger');
|
|
|
|
$status->addMessage('Configure this interface in Networking > eth0.', 'danger');
|
|
|
|
$return = 1;
|
|
|
|
} else {
|
|
|
|
$dhcp_cfg = file_get_contents(RASPI_DHCPCD_CONFIG);
|
|
|
|
if (!preg_match('/^interface\seth0$/m', $dhcp_cfg)) {
|
|
|
|
// set dhcp values from ini
|
|
|
|
$eth0_cfg = parse_ini_file(RASPI_CONFIG_NETWORKING.'/eth0.ini', false, INI_SCANNER_RAW);
|
|
|
|
$ip_address = $eth0_cfg['ip_address'];
|
|
|
|
$domain_name_server = ($eth0_cfg['domain_name_server'] =='') ? '1.1.1.1 8.8.8.8' : $eth0_cfg['domain_name_server'];
|
2020-11-09 18:48:32 +01:00
|
|
|
|
2020-11-11 13:40:22 +01:00
|
|
|
// append eth0 config to dhcpcd.conf
|
|
|
|
$cfg = $dhcp_conf;
|
|
|
|
$cfg[] = '# RaspAP '.$_POST['interface'].' configuration';
|
|
|
|
$cfg[] = 'interface '.$_POST['interface'];
|
|
|
|
$cfg[] = 'static ip_address='.$ip_address;
|
|
|
|
$cfg[] = 'static domain_name_server='.$domain_name_server;
|
|
|
|
$cfg[] = PHP_EOL;
|
|
|
|
$cfg = join(PHP_EOL, $cfg);
|
|
|
|
$dhcp_cfg .= $cfg;
|
|
|
|
file_put_contents("/tmp/dhcpddata", $dhcp_cfg);
|
|
|
|
system('sudo cp /tmp/dhcpddata '.RASPI_DHCPCD_CONFIG, $return);
|
|
|
|
$status->addMessage('DHCP configuration for eth0 added.', 'success');
|
|
|
|
system('sudo cp /tmp/dnsmasqdata '.RASPI_DNSMASQ_ETH0, $return);
|
|
|
|
$status->addMessage('Dnsmasq configuration for eth0 added.', 'success');
|
|
|
|
} else {
|
|
|
|
$status->addMessage('DHCP for '.$_POST['interface'].' already enabled.', 'danger');
|
|
|
|
}
|
2020-11-09 18:48:32 +01:00
|
|
|
}
|
|
|
|
} elseif (!isset($_POST['dhcp-eth0']) && file_exists(RASPI_DNSMASQ_ETH0)) {
|
2020-11-11 13:40:22 +01:00
|
|
|
// remove dhcpcd eth0 conf
|
|
|
|
$dhcp_cfg = file_get_contents(RASPI_DHCPCD_CONFIG);
|
|
|
|
$dhcp_cfg = preg_replace('/^#\sRaspAP\seth0.*(\n.*){3,}/m', '', $dhcp_cfg);
|
|
|
|
file_put_contents("/tmp/dhcpddata", $dhcp_cfg);
|
|
|
|
system('sudo cp /tmp/dhcpddata '.RASPI_DHCPCD_CONFIG, $return);
|
|
|
|
$status->addMessage('DHCP configuration for eth0 removed.', 'success');
|
|
|
|
// remove dnsmasq eth0 conf
|
2020-11-09 18:48:32 +01:00
|
|
|
system('sudo rm '.RASPI_DNSMASQ_ETH0, $return);
|
|
|
|
$status->addMessage('Dnsmasq configuration for eth0 removed.', 'success');
|
|
|
|
} else {
|
|
|
|
system('sudo cp /tmp/dnsmasqdata '.RASPI_DNSMASQ_CONFIG, $return);
|
|
|
|
}
|
|
|
|
|
2020-02-14 03:38:46 +01:00
|
|
|
} else {
|
|
|
|
$status->addMessage($errors, 'danger');
|
|
|
|
}
|
2019-07-30 17:05:41 +02:00
|
|
|
|
2020-02-14 03:38:46 +01:00
|
|
|
if ($return == 0) {
|
2020-11-09 18:48:32 +01:00
|
|
|
$status->addMessage('Dnsmasq configuration updated successfully.', 'success');
|
2020-02-14 03:38:46 +01:00
|
|
|
} else {
|
|
|
|
$status->addMessage('Dnsmasq configuration failed to be updated.', 'danger');
|
|
|
|
}
|
2018-08-04 14:03:14 +02:00
|
|
|
}
|
2016-07-09 02:26:13 +02:00
|
|
|
}
|
|
|
|
|
2019-04-10 10:37:35 +02:00
|
|
|
exec('pidof dnsmasq | wc -l', $dnsmasq);
|
|
|
|
$dnsmasq_state = ($dnsmasq[0] > 0);
|
2016-07-09 02:26:13 +02:00
|
|
|
|
2020-02-14 03:38:46 +01:00
|
|
|
if (!RASPI_MONITOR_ENABLED) {
|
|
|
|
if (isset($_POST['startdhcpd'])) {
|
|
|
|
if ($dnsmasq_state) {
|
|
|
|
$status->addMessage('dnsmasq already running', 'info');
|
2019-04-10 10:37:35 +02:00
|
|
|
} else {
|
2020-02-14 03:38:46 +01:00
|
|
|
exec('sudo /bin/systemctl start dnsmasq.service', $dnsmasq, $return);
|
|
|
|
if ($return == 0) {
|
|
|
|
$status->addMessage('Successfully started dnsmasq', 'success');
|
|
|
|
$dnsmasq_state = true;
|
|
|
|
} else {
|
|
|
|
$status->addMessage('Failed to start dnsmasq', 'danger');
|
|
|
|
}
|
2019-04-10 10:37:35 +02:00
|
|
|
}
|
2020-02-14 03:38:46 +01:00
|
|
|
} elseif (isset($_POST['stopdhcpd'])) {
|
|
|
|
if ($dnsmasq_state) {
|
|
|
|
exec('sudo /bin/systemctl stop dnsmasq.service', $dnsmasq, $return);
|
|
|
|
if ($return == 0) {
|
|
|
|
$status->addMessage('Successfully stopped dnsmasq', 'success');
|
|
|
|
$dnsmasq_state = false;
|
|
|
|
} else {
|
|
|
|
$status->addMessage('Failed to stop dnsmasq', 'danger');
|
|
|
|
}
|
2019-04-10 10:37:35 +02:00
|
|
|
} else {
|
2020-02-14 03:38:46 +01:00
|
|
|
$status->addMessage('dnsmasq already stopped', 'info');
|
2019-04-10 10:37:35 +02:00
|
|
|
}
|
2016-07-09 02:26:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-01 10:10:24 +01:00
|
|
|
$serviceStatus = $dnsmasq_state ? "up" : "down";
|
2019-08-02 23:05:27 +02:00
|
|
|
|
2019-04-10 10:37:35 +02:00
|
|
|
exec('cat '. RASPI_DNSMASQ_CONFIG, $return);
|
|
|
|
$conf = ParseConfig($return);
|
|
|
|
$arrRange = explode(",", $conf['dhcp-range']);
|
|
|
|
$RangeStart = $arrRange[0];
|
|
|
|
$RangeEnd = $arrRange[1];
|
|
|
|
$RangeMask = $arrRange[2];
|
|
|
|
$leaseTime = $arrRange[3];
|
2019-08-01 16:31:11 +02:00
|
|
|
$dhcpHost = $conf["dhcp-host"];
|
|
|
|
$dhcpHost = empty($dhcpHost) ? [] : $dhcpHost;
|
|
|
|
$dhcpHost = is_array($dhcpHost) ? $dhcpHost : [ $dhcpHost ];
|
2020-02-28 23:37:52 +01:00
|
|
|
$upstreamServers = is_array($conf['server']) ? $conf['server'] : [ $conf['server'] ];
|
2020-03-04 18:23:51 +01:00
|
|
|
$upstreamServers = array_filter($upstreamServers);
|
2016-07-09 02:26:13 +02:00
|
|
|
|
2019-10-15 00:33:41 +02:00
|
|
|
$DNS1 = '';
|
|
|
|
$DNS2 = '';
|
2020-02-15 18:57:46 +01:00
|
|
|
if (isset($conf['dhcp-option'])) {
|
2019-10-15 00:33:41 +02:00
|
|
|
$arrDns = explode(",", $conf['dhcp-option']);
|
2020-02-15 18:57:46 +01:00
|
|
|
if ($arrDns[0] == '6') {
|
|
|
|
if (count($arrDns) > 1) {
|
2019-10-15 00:33:41 +02:00
|
|
|
$DNS1 = $arrDns[1];
|
|
|
|
}
|
2020-02-15 18:57:46 +01:00
|
|
|
if (count($arrDns) > 2) {
|
2019-10-15 00:33:41 +02:00
|
|
|
$DNS2 = $arrDns[2];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-20 22:33:09 +02:00
|
|
|
|
2019-04-10 10:37:35 +02:00
|
|
|
$hselected = '';
|
|
|
|
$mselected = '';
|
|
|
|
$dselected = '';
|
|
|
|
$infiniteselected = '';
|
|
|
|
preg_match('/([0-9]*)([a-z])/i', $leaseTime, $arrRangeLeaseTime);
|
|
|
|
if ($leaseTime === 'infinite') {
|
|
|
|
$infiniteselected = ' selected="selected"';
|
|
|
|
} else {
|
|
|
|
switch ($arrRangeLeaseTime[2]) {
|
2020-02-15 18:57:46 +01:00
|
|
|
case 'h':
|
|
|
|
$hselected = ' selected="selected"';
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
$mselected = ' selected="selected"';
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
$dselected = ' selected="selected"';
|
|
|
|
break;
|
2019-04-10 10:37:35 +02:00
|
|
|
}
|
2018-09-25 21:49:54 +02:00
|
|
|
}
|
2020-11-09 18:48:32 +01:00
|
|
|
if (file_exists(RASPI_DNSMASQ_ETH0)) {
|
|
|
|
$dhcp_eth0 = 1;
|
|
|
|
}
|
2019-08-19 02:06:44 +02:00
|
|
|
exec("ip -o link show | awk -F': ' '{print $2}'", $interfaces);
|
|
|
|
exec('cat ' . RASPI_DNSMASQ_LEASES, $leases);
|
|
|
|
|
2020-02-15 18:57:46 +01:00
|
|
|
echo renderTemplate(
|
|
|
|
"dhcp", compact(
|
|
|
|
"status",
|
|
|
|
"serviceStatus",
|
|
|
|
"RangeStart",
|
|
|
|
"RangeEnd",
|
|
|
|
"DNS1",
|
|
|
|
"DNS2",
|
2020-02-28 23:37:52 +01:00
|
|
|
"upstreamServers",
|
2020-02-15 18:57:46 +01:00
|
|
|
"arrRangeLeaseTime",
|
|
|
|
"mselected",
|
|
|
|
"hselected",
|
|
|
|
"dselected",
|
|
|
|
"infiniteselected",
|
|
|
|
"dnsmasq_state",
|
|
|
|
"conf",
|
|
|
|
"dhcpHost",
|
|
|
|
"interfaces",
|
2020-11-09 18:48:32 +01:00
|
|
|
"leases",
|
|
|
|
"dhcp_eth0"
|
2020-02-15 18:57:46 +01:00
|
|
|
)
|
|
|
|
);
|
2019-04-10 10:37:35 +02:00
|
|
|
}
|