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()
|
|
|
|
{
|
|
|
|
$status = new StatusMessages();
|
2020-02-14 03:38:46 +01:00
|
|
|
if (!RASPI_MONITOR_ENABLED) {
|
|
|
|
if (isset($_POST['savedhcpdsettings'])) {
|
2020-12-04 23:44:27 +01:00
|
|
|
saveDHCPConfig($status);
|
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
|
|
|
}
|
|
|
|
}
|
2020-11-21 14:52:20 +01:00
|
|
|
getWifiInterface();
|
2021-02-22 10:47:25 +01:00
|
|
|
$ap_iface = $_SESSION['ap_interface'];
|
2019-11-01 10:10:24 +01:00
|
|
|
$serviceStatus = $dnsmasq_state ? "up" : "down";
|
2020-12-24 11:20:52 +01:00
|
|
|
exec('cat '. RASPI_DNSMASQ_PREFIX.'raspap.conf', $return);
|
|
|
|
$conf = ParseConfig($return);
|
2021-02-22 10:47:25 +01:00
|
|
|
exec('cat '. RASPI_DNSMASQ_PREFIX.$ap_iface.'.conf', $return);
|
|
|
|
$conf = array_merge(ParseConfig($return));
|
|
|
|
$hosts = (array)$conf['dhcp-host'];
|
2021-09-07 18:04:00 +02:00
|
|
|
$upstreamServers = (array)$conf['server'];
|
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",
|
|
|
|
"dnsmasq_state",
|
2020-11-21 14:52:20 +01:00
|
|
|
"ap_iface",
|
2020-12-24 11:20:52 +01:00
|
|
|
"conf",
|
2021-02-22 10:47:25 +01:00
|
|
|
"hosts",
|
2021-09-07 18:04:00 +02:00
|
|
|
"upstreamServers",
|
2020-02-15 18:57:46 +01:00
|
|
|
"interfaces",
|
2020-11-16 19:21:31 +01:00
|
|
|
"leases"
|
2020-02-15 18:57:46 +01:00
|
|
|
)
|
|
|
|
);
|
2019-04-10 10:37:35 +02:00
|
|
|
}
|
2020-11-17 12:21:04 +01:00
|
|
|
|
2020-12-04 23:44:27 +01:00
|
|
|
/**
|
|
|
|
* Saves a DHCP configuration
|
|
|
|
*
|
|
|
|
* @return object $status
|
|
|
|
*/
|
|
|
|
function saveDHCPConfig($status)
|
2020-11-17 12:21:04 +01:00
|
|
|
{
|
|
|
|
$iface = $_POST['interface'];
|
|
|
|
$return = 1;
|
|
|
|
|
2020-11-27 17:28:37 +01:00
|
|
|
// handle disable dhcp option
|
|
|
|
if (!isset($_POST['dhcp-iface']) && file_exists(RASPI_DNSMASQ_PREFIX.$iface.'.conf')) {
|
2020-12-04 15:40:54 +01:00
|
|
|
// remove dhcp + dnsmasq configs for selected interface
|
|
|
|
$return = removeDHCPConfig($iface,$status);
|
|
|
|
$return = removeDnsmasqConfig($iface,$status);
|
2020-11-27 17:28:37 +01:00
|
|
|
} else {
|
2020-12-04 23:44:27 +01:00
|
|
|
$errors = validateDHCPInput();
|
2020-11-26 18:20:12 +01:00
|
|
|
if (empty($errors)) {
|
2020-12-04 23:44:27 +01:00
|
|
|
$return = updateDHCPConfig($iface,$status);
|
2020-11-17 12:21:04 +01:00
|
|
|
} else {
|
|
|
|
$status->addMessage($errors, 'danger');
|
|
|
|
}
|
2020-11-20 08:35:36 +01:00
|
|
|
if ($return == 1) {
|
2020-11-17 12:21:04 +01:00
|
|
|
$status->addMessage('Dnsmasq configuration failed to be updated.', 'danger');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-11-27 17:28:37 +01:00
|
|
|
if (($_POST['dhcp-iface'] == "1")) {
|
2020-12-04 23:44:27 +01:00
|
|
|
$return = updateDnsmasqConfig($iface,$status);
|
2020-11-27 17:28:37 +01:00
|
|
|
}
|
|
|
|
if ($return == 0) {
|
|
|
|
$status->addMessage('Dnsmasq configuration updated successfully.', 'success');
|
|
|
|
} else {
|
|
|
|
$status->addMessage('Dnsmasq configuration failed to be updated.', 'danger');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2020-11-17 12:21:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-04 23:44:27 +01:00
|
|
|
/**
|
|
|
|
* Validates DHCP user input from the $_POST object
|
|
|
|
*
|
|
|
|
* @return string $errors
|
|
|
|
*/
|
|
|
|
function validateDHCPInput()
|
2020-11-17 12:21:04 +01:00
|
|
|
{
|
|
|
|
define('IFNAMSIZ', 16);
|
|
|
|
$iface = $_POST['interface'];
|
|
|
|
if (!preg_match('/^[a-zA-Z0-9]+$/', $iface)
|
|
|
|
|| strlen($iface) >= IFNAMSIZ
|
|
|
|
) {
|
|
|
|
$errors .= _('Invalid interface name.').'<br />'.PHP_EOL;
|
|
|
|
}
|
2020-11-26 18:20:12 +01:00
|
|
|
if (!filter_var($_POST['StaticIP'], FILTER_VALIDATE_IP) && !empty($_POST['StaticIP'])) {
|
|
|
|
$errors .= _('Invalid static IP address.').'<br />'.PHP_EOL;
|
|
|
|
}
|
2020-11-30 13:09:22 +01:00
|
|
|
if (!filter_var($_POST['SubnetMask'], FILTER_VALIDATE_IP) && !empty($_POST['SubnetMask'])) {
|
|
|
|
$errors .= _('Invalid subnet mask.').'<br />'.PHP_EOL;
|
|
|
|
}
|
|
|
|
if (!filter_var($_POST['DefaultGateway'], FILTER_VALIDATE_IP) && !empty($_POST['DefaultGateway'])) {
|
2020-11-26 18:20:12 +01:00
|
|
|
$errors .= _('Invalid default gateway.').'<br />'.PHP_EOL;
|
2020-11-30 13:09:22 +01:00
|
|
|
var_dump($_POST['DefaultGateway']);
|
|
|
|
die();
|
2020-11-26 18:20:12 +01:00
|
|
|
}
|
2020-11-27 17:28:37 +01:00
|
|
|
if (($_POST['dhcp-iface'] == "1")) {
|
|
|
|
if (!filter_var($_POST['RangeStart'], FILTER_VALIDATE_IP) && !empty($_POST['RangeStart'])) {
|
|
|
|
$errors .= _('Invalid DHCP range start.').'<br />'.PHP_EOL;
|
|
|
|
}
|
|
|
|
if (!filter_var($_POST['RangeEnd'], FILTER_VALIDATE_IP) && !empty($_POST['RangeEnd'])) {
|
|
|
|
$errors .= _('Invalid DHCP range end.').'<br />'.PHP_EOL;
|
|
|
|
}
|
|
|
|
if (!ctype_digit($_POST['RangeLeaseTime']) && $_POST['RangeLeaseTimeUnits'] !== 'infinite') {
|
|
|
|
$errors .= _('Invalid DHCP lease time, not a number.').'<br />'.PHP_EOL;
|
|
|
|
}
|
|
|
|
if (!in_array($_POST['RangeLeaseTimeUnits'], array('m', 'h', 'd', 'infinite'))) {
|
|
|
|
$errors .= _('Unknown DHCP lease time unit.').'<br />'.PHP_EOL;
|
|
|
|
}
|
|
|
|
if ($_POST['Metric'] !== '' && !ctype_digit($_POST['Metric'])) {
|
|
|
|
$errors .= _('Invalid metric value, not a number.').'<br />'.PHP_EOL;
|
|
|
|
}
|
2020-11-26 18:20:12 +01:00
|
|
|
}
|
2020-11-17 12:21:04 +01:00
|
|
|
return $errors;
|
|
|
|
}
|
|
|
|
|
2021-07-27 18:33:00 +02:00
|
|
|
/**
|
|
|
|
* Compares to string IPs
|
|
|
|
*
|
|
|
|
* @param string $ip1
|
|
|
|
* @param string $ip2
|
|
|
|
* @return boolean $result
|
|
|
|
*/
|
|
|
|
function compareIPs($ip1, $ip2)
|
|
|
|
{
|
|
|
|
$ipu1 = sprintf('%u', ip2long($ip1["ip"])) + 0;
|
|
|
|
$ipu2 = sprintf('%u', ip2long($ip2["ip"])) + 0;
|
|
|
|
return $ipu1 > $ipu2;
|
|
|
|
}
|
|
|
|
|
2020-12-04 23:44:27 +01:00
|
|
|
/**
|
|
|
|
* Updates a dnsmasq configuration
|
|
|
|
*
|
|
|
|
* @param string $iface
|
|
|
|
* @param object $status
|
|
|
|
* @return boolean $result
|
|
|
|
*/
|
|
|
|
function updateDnsmasqConfig($iface,$status)
|
2020-11-20 08:35:36 +01:00
|
|
|
{
|
2020-12-04 15:40:54 +01:00
|
|
|
$config = '# RaspAP '.$iface.' configuration'.PHP_EOL;
|
|
|
|
$config .= 'interface='.$iface.PHP_EOL.
|
2020-11-20 08:35:36 +01:00
|
|
|
'dhcp-range='.$_POST['RangeStart'].','.$_POST['RangeEnd'].
|
2021-09-24 18:31:28 +02:00
|
|
|
','.$_POST['SubnetMask'].',';
|
2020-11-20 08:35:36 +01:00
|
|
|
if ($_POST['RangeLeaseTimeUnits'] !== 'infinite') {
|
|
|
|
$config .= $_POST['RangeLeaseTime'];
|
|
|
|
}
|
|
|
|
$config .= $_POST['RangeLeaseTimeUnits'].PHP_EOL;
|
2021-07-27 18:33:00 +02:00
|
|
|
// Static leases
|
|
|
|
$staticLeases = array();
|
2020-11-20 08:35:36 +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]);
|
2021-07-27 18:33:00 +02:00
|
|
|
$comment = trim($_POST["static_leases"]["comment"][$i]);
|
2020-11-20 08:35:36 +01:00
|
|
|
if ($mac != "" && $ip != "") {
|
2021-07-27 18:33:00 +02:00
|
|
|
$staticLeases[] = array('mac' => $mac, 'ip' => $ip, 'comment' => $comment);
|
2020-11-20 08:35:36 +01:00
|
|
|
}
|
|
|
|
}
|
2021-07-27 18:33:00 +02:00
|
|
|
// Sort ascending by IPs
|
|
|
|
usort($staticLeases, "compareIPs");
|
|
|
|
// Update config
|
|
|
|
for ($i = 0; $i < count($staticLeases); $i++) {
|
|
|
|
$mac = $staticLeases[$i]['mac'];
|
|
|
|
$ip = $staticLeases[$i]['ip'];
|
|
|
|
$comment = $staticLeases[$i]['comment'];
|
|
|
|
$config .= "dhcp-host=$mac,$ip # $comment".PHP_EOL;
|
|
|
|
}
|
2020-11-20 08:35:36 +01:00
|
|
|
if ($_POST['no-resolv'] == "1") {
|
|
|
|
$config .= "no-resolv".PHP_EOL;
|
|
|
|
}
|
|
|
|
foreach ($_POST['server'] as $server) {
|
|
|
|
$config .= "server=$server".PHP_EOL;
|
|
|
|
}
|
|
|
|
if ($_POST['DNS1']) {
|
|
|
|
$config .= "dhcp-option=6," . $_POST['DNS1'];
|
|
|
|
if ($_POST['DNS2']) {
|
|
|
|
$config .= ','.$_POST['DNS2'];
|
|
|
|
}
|
|
|
|
$config .= PHP_EOL;
|
|
|
|
}
|
|
|
|
file_put_contents("/tmp/dnsmasqdata", $config);
|
|
|
|
$msg = file_exists(RASPI_DNSMASQ_PREFIX.$iface.'.conf') ? 'updated' : 'added';
|
|
|
|
system('sudo cp /tmp/dnsmasqdata '.RASPI_DNSMASQ_PREFIX.$iface.'.conf', $result);
|
|
|
|
if ($result == 0) {
|
|
|
|
$status->addMessage('Dnsmasq configuration for '.$iface.' '.$msg.'.', 'success');
|
|
|
|
}
|
2020-12-24 11:20:52 +01:00
|
|
|
|
|
|
|
// write default 090_raspap.conf
|
|
|
|
$config = '# RaspAP default config'.PHP_EOL;
|
|
|
|
$config .='log-facility=/tmp/dnsmasq.log'.PHP_EOL;
|
|
|
|
$config .='conf-dir=/etc/dnsmasq.d'.PHP_EOL;
|
|
|
|
// handle log option
|
|
|
|
if ($_POST['log-dhcp'] == "1") {
|
|
|
|
$config .= "log-dhcp".PHP_EOL;
|
|
|
|
}
|
|
|
|
if ($_POST['log-queries'] == "1") {
|
|
|
|
$config .= "log-queries".PHP_EOL;
|
|
|
|
}
|
|
|
|
$config .= PHP_EOL;
|
|
|
|
file_put_contents("/tmp/dnsmasqdata", $config);
|
|
|
|
system('sudo cp /tmp/dnsmasqdata '.RASPI_DNSMASQ_PREFIX.'raspap.conf', $result);
|
|
|
|
|
2020-11-20 08:35:36 +01:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2020-12-04 23:44:27 +01:00
|
|
|
/**
|
|
|
|
* Updates a dhcp configuration
|
|
|
|
*
|
|
|
|
* @param string $iface
|
|
|
|
* @param object $status
|
|
|
|
* @return boolean $result
|
|
|
|
*/
|
|
|
|
function updateDHCPConfig($iface,$status)
|
2020-11-20 08:35:36 +01:00
|
|
|
{
|
2020-11-26 18:20:12 +01:00
|
|
|
$cfg[] = '# RaspAP '.$iface.' configuration';
|
|
|
|
$cfg[] = 'interface '.$iface;
|
2020-11-30 13:09:22 +01:00
|
|
|
if (isset($_POST['StaticIP'])) {
|
2020-12-03 11:29:11 +01:00
|
|
|
$mask = ($_POST['SubnetMask'] !== '' && $_POST['SubnetMask'] !== '0.0.0.0') ? '/'.mask2cidr($_POST['SubnetMask']) : null;
|
|
|
|
$cfg[] = 'static ip_address='.$_POST['StaticIP'].$mask;
|
2020-11-30 13:09:22 +01:00
|
|
|
}
|
2020-11-26 18:20:12 +01:00
|
|
|
if (isset($_POST['DefaultGateway'])) {
|
|
|
|
$cfg[] = 'static routers='.$_POST['DefaultGateway'];
|
|
|
|
}
|
|
|
|
if ($_POST['DNS1'] !== '' || $_POST['DNS2'] !== '') {
|
|
|
|
$cfg[] = 'static domain_name_server='.$_POST['DNS1'].' '.$_POST['DNS2'];
|
|
|
|
}
|
|
|
|
if ($_POST['Metric'] !== '') {
|
|
|
|
$cfg[] = 'metric '.$_POST['Metric'];
|
|
|
|
}
|
|
|
|
if ($_POST['Fallback'] == 1) {
|
2020-11-30 13:09:22 +01:00
|
|
|
$cfg[] = 'profile static_'.$iface;
|
|
|
|
$cfg[] = 'fallback static_'.$iface;
|
2020-11-26 18:20:12 +01:00
|
|
|
}
|
2021-04-03 01:43:42 +02:00
|
|
|
$cfg[] = $_POST['DefaultRoute'] == '1' ? 'gateway' : 'nogateway';
|
2020-11-26 18:20:12 +01:00
|
|
|
$dhcp_cfg = file_get_contents(RASPI_DHCPCD_CONFIG);
|
|
|
|
if (!preg_match('/^interface\s'.$iface.'$/m', $dhcp_cfg)) {
|
2020-11-27 17:28:37 +01:00
|
|
|
$cfg[] = PHP_EOL;
|
|
|
|
$cfg = join(PHP_EOL, $cfg);
|
2020-11-26 18:20:12 +01:00
|
|
|
$dhcp_cfg .= $cfg;
|
|
|
|
$status->addMessage('DHCP configuration for '.$iface.' added.', 'success');
|
2020-11-20 08:35:36 +01:00
|
|
|
} else {
|
2020-11-27 17:28:37 +01:00
|
|
|
$cfg = join(PHP_EOL, $cfg);
|
2020-11-26 18:20:12 +01:00
|
|
|
$dhcp_cfg = preg_replace('/^#\sRaspAP\s'.$iface.'\s.*?(?=\s*^\s*$)/ms', $cfg, $dhcp_cfg, 1);
|
|
|
|
$status->addMessage('DHCP configuration for '.$iface.' updated.', 'success');
|
2020-11-20 08:35:36 +01:00
|
|
|
}
|
2020-11-27 17:28:37 +01:00
|
|
|
file_put_contents("/tmp/dhcpddata", $dhcp_cfg);
|
|
|
|
system('sudo cp /tmp/dhcpddata '.RASPI_DHCPCD_CONFIG, $result);
|
|
|
|
|
2020-11-20 08:35:36 +01:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|