2016-07-09 02:26:13 +02:00
|
|
|
<?php
|
|
|
|
|
2019-04-10 10:37:35 +02:00
|
|
|
include_once('includes/status_messages.php');
|
2016-07-09 02:26:13 +02: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();
|
|
|
|
if (isset($_POST['savedhcpdsettings'])) {
|
2019-07-30 17:05:41 +02:00
|
|
|
$errors = '';
|
|
|
|
define('IFNAMSIZ', 16);
|
|
|
|
if (!preg_match('/^[a-zA-Z0-9]+$/', $_POST['interface']) ||
|
|
|
|
strlen($_POST['interface']) >= IFNAMSIZ) {
|
|
|
|
$errors .= _('Invalid interface name.').'<br />'.PHP_EOL;
|
|
|
|
}
|
2016-07-09 02:26:13 +02:00
|
|
|
|
2019-07-30 17:05:41 +02:00
|
|
|
if (!preg_match('/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\z/', $_POST['RangeStart']) &&
|
|
|
|
!empty($_POST['RangeStart'])) { // allow ''/null ?
|
|
|
|
$errors .= _('Invalid DHCP range start.').'<br />'.PHP_EOL;
|
|
|
|
}
|
2018-08-04 14:03:14 +02:00
|
|
|
|
2019-07-30 17:05:41 +02:00
|
|
|
if (!preg_match('/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\z/', $_POST['RangeEnd']) &&
|
|
|
|
!empty($_POST['RangeEnd'])) { // allow ''/null ?
|
|
|
|
$errors .= _('Invalid DHCP range end.').'<br />'.PHP_EOL;
|
|
|
|
}
|
2018-08-04 14:03:14 +02:00
|
|
|
|
2019-07-30 17:05:41 +02: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
|
|
|
|
2019-07-30 17:05:41 +02:00
|
|
|
if (!in_array($_POST['RangeLeaseTimeUnits'], array('m', 'h', 'd', 'infinite'))) {
|
|
|
|
$errors .= _('Unknown DHCP lease time unit.').'<br />'.PHP_EOL;
|
|
|
|
}
|
2018-08-04 14:03:14 +02:00
|
|
|
|
2019-07-30 17:05:41 +02: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'];
|
|
|
|
}
|
2018-09-25 21:32:31 +02:00
|
|
|
|
2019-07-30 17:05:41 +02:00
|
|
|
$config .= $_POST['RangeLeaseTimeUnits'].PHP_EOL;
|
2019-08-01 16:31:11 +02:00
|
|
|
|
2019-07-30 17:05:41 +02: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-04-10 10:37:35 +02:00
|
|
|
}
|
2019-10-04 19:57:58 +02:00
|
|
|
file_put_contents("/tmp/dnsmasqdata", $config);
|
|
|
|
system('sudo cp /tmp/dnsmasqdata '.RASPI_DNSMASQ_CONFIG, $return);
|
2018-08-04 14:03:14 +02:00
|
|
|
} else {
|
2019-07-30 17:05:41 +02:00
|
|
|
$status->addMessage($errors, 'danger');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($return == 0) {
|
|
|
|
$status->addMessage('Dnsmasq configuration updated successfully', 'success');
|
|
|
|
} 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
|
|
|
|
2019-04-10 10:37:35 +02:00
|
|
|
if (isset($_POST['startdhcpd'])) {
|
2019-07-30 17:05:41 +02:00
|
|
|
if ($dnsmasq_state) {
|
|
|
|
$status->addMessage('dnsmasq already running', 'info');
|
|
|
|
} else {
|
|
|
|
exec('sudo /etc/init.d/dnsmasq start', $dnsmasq, $return);
|
|
|
|
if ($return == 0) {
|
|
|
|
$status->addMessage('Successfully started dnsmasq', 'success');
|
|
|
|
$dnsmasq_state = true;
|
2019-04-10 10:37:35 +02:00
|
|
|
} else {
|
2019-07-30 17:05:41 +02:00
|
|
|
$status->addMessage('Failed to start dnsmasq', 'danger');
|
2019-04-10 10:37:35 +02:00
|
|
|
}
|
2016-07-09 02:26:13 +02:00
|
|
|
}
|
2019-04-10 10:37:35 +02:00
|
|
|
} elseif (isset($_POST['stopdhcpd'])) {
|
2019-07-30 17:05:41 +02:00
|
|
|
if ($dnsmasq_state) {
|
|
|
|
exec('sudo /etc/init.d/dnsmasq stop', $dnsmasq, $return);
|
|
|
|
if ($return == 0) {
|
|
|
|
$status->addMessage('Successfully stopped dnsmasq', 'success');
|
|
|
|
$dnsmasq_state = false;
|
2019-04-10 10:37:35 +02:00
|
|
|
} else {
|
2019-07-30 17:05:41 +02:00
|
|
|
$status->addMessage('Failed to stop dnsmasq', 'danger');
|
2019-04-10 10:37:35 +02:00
|
|
|
}
|
2016-07-09 02:26:13 +02:00
|
|
|
} else {
|
2019-07-30 17:05:41 +02:00
|
|
|
$status->addMessage('dnsmasq already stopped', 'info');
|
2016-07-09 02:26:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-02 23:05:27 +02:00
|
|
|
$serviceStatus = $dnsmasq_state ? "running" : "stopped";
|
|
|
|
|
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 ];
|
2016-07-09 02:26:13 +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]) {
|
|
|
|
case 'h':
|
|
|
|
$hselected = ' selected="selected"';
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
$mselected = ' selected="selected"';
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
$dselected = ' selected="selected"';
|
|
|
|
break;
|
|
|
|
}
|
2018-09-25 21:49:54 +02:00
|
|
|
}
|
2016-07-09 02:26:13 +02:00
|
|
|
|
2019-08-19 02:06:44 +02:00
|
|
|
exec("ip -o link show | awk -F': ' '{print $2}'", $interfaces);
|
|
|
|
exec('cat ' . RASPI_DNSMASQ_LEASES, $leases);
|
|
|
|
|
2019-08-19 01:28:01 +02:00
|
|
|
echo renderTemplate("dhcp", compact(
|
|
|
|
"status",
|
|
|
|
"serviceStatus",
|
2019-09-07 17:42:31 +02:00
|
|
|
"RangeStart",
|
|
|
|
"RangeEnd",
|
2019-08-19 01:28:01 +02:00
|
|
|
"arrRangeLeaseTime",
|
2019-09-07 17:42:31 +02:00
|
|
|
"mselected",
|
|
|
|
"hselected",
|
|
|
|
"dselected",
|
|
|
|
"infiniteselected",
|
|
|
|
"dnsmasq_state",
|
|
|
|
"conf",
|
|
|
|
"dhcpHost",
|
|
|
|
"interfaces",
|
|
|
|
"leases"
|
2019-08-19 01:28:01 +02:00
|
|
|
));
|
2019-04-10 10:37:35 +02:00
|
|
|
}
|