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'])) {
|
|
|
|
if (CSRFValidate()) {
|
|
|
|
$errors = '';
|
|
|
|
define('IFNAMSIZ', 16);
|
|
|
|
if (!preg_match('/^[a-zA-Z0-9]+$/', $_POST['interface']) ||
|
2018-08-04 14:03:14 +02:00
|
|
|
strlen($_POST['interface']) >= IFNAMSIZ) {
|
2019-04-10 10:37:35 +02:00
|
|
|
$errors .= _('Invalid interface name.').'<br />'.PHP_EOL;
|
|
|
|
}
|
2016-07-09 02:26:13 +02:00
|
|
|
|
2019-04-10 10:37:35 +02:00
|
|
|
if (!preg_match('/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\z/', $_POST['RangeStart']) &&
|
2018-08-04 14:03:14 +02:00
|
|
|
!empty($_POST['RangeStart'])) { // allow ''/null ?
|
2019-04-10 10:37:35 +02:00
|
|
|
$errors .= _('Invalid DHCP range start.').'<br />'.PHP_EOL;
|
|
|
|
}
|
2018-08-04 14:03:14 +02:00
|
|
|
|
2019-04-10 10:37:35 +02:00
|
|
|
if (!preg_match('/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\z/', $_POST['RangeEnd']) &&
|
2018-08-04 14:03:14 +02:00
|
|
|
!empty($_POST['RangeEnd'])) { // allow ''/null ?
|
2019-04-10 10:37:35 +02:00
|
|
|
$errors .= _('Invalid DHCP range end.').'<br />'.PHP_EOL;
|
|
|
|
}
|
2018-08-04 14:03:14 +02:00
|
|
|
|
2019-04-10 10:37:35 +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-04-10 10:37:35 +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-04-10 10:37:35 +02:00
|
|
|
$return = 1;
|
|
|
|
if (empty($errors)) {
|
|
|
|
$config = 'interface='.$_POST['interface'].PHP_EOL.
|
2018-08-04 14:03:14 +02:00
|
|
|
'dhcp-range='.$_POST['RangeStart'].','.$_POST['RangeEnd'].
|
2018-09-25 21:32:31 +02:00
|
|
|
',255.255.255.0,';
|
2019-04-10 10:37:35 +02:00
|
|
|
if ($_POST['RangeLeaseTimeUnits'] !== 'infinite') {
|
|
|
|
$config .= $_POST['RangeLeaseTime'];
|
|
|
|
}
|
2018-09-25 21:32:31 +02:00
|
|
|
|
2019-08-01 16:31:11 +02:00
|
|
|
$config .= $_POST['RangeLeaseTimeUnits'].PHP_EOL;
|
|
|
|
|
|
|
|
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-07-30 20:51:48 +02:00
|
|
|
file_put_contents("/tmp/dhcpddata", $config);
|
2019-04-10 10:37:35 +02:00
|
|
|
system('sudo cp /tmp/dhcpddata '.RASPI_DNSMASQ_CONFIG, $return);
|
|
|
|
} else {
|
|
|
|
$status->addMessage($errors, 'danger');
|
|
|
|
}
|
2018-08-04 14:03:14 +02:00
|
|
|
|
2019-04-10 10:37:35 +02:00
|
|
|
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
|
|
|
} else {
|
2019-04-10 10:37:35 +02:00
|
|
|
error_log('CSRF violation');
|
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'])) {
|
|
|
|
if (CSRFValidate()) {
|
|
|
|
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;
|
|
|
|
} else {
|
|
|
|
$status->addMessage('Failed to start dnsmasq', 'danger');
|
|
|
|
}
|
|
|
|
}
|
2016-07-09 04:46:21 +02:00
|
|
|
} else {
|
2019-04-10 10:37:35 +02:00
|
|
|
error_log('CSRF violation');
|
2016-07-09 02:26:13 +02:00
|
|
|
}
|
2019-04-10 10:37:35 +02:00
|
|
|
} elseif (isset($_POST['stopdhcpd'])) {
|
|
|
|
if (CSRFValidate()) {
|
|
|
|
if ($dnsmasq_state) {
|
|
|
|
exec('sudo /etc/init.d/dnsmasq stop', $dnsmasq, $return);
|
|
|
|
if ($return == 0) {
|
|
|
|
$status->addMessage('Successfully stopped dnsmasq', 'success');
|
|
|
|
$dnsmasq_state = false;
|
|
|
|
} else {
|
|
|
|
$status->addMessage('Failed to stop dnsmasq', 'danger');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$status->addMessage('dnsmasq already stopped', 'info');
|
|
|
|
}
|
2016-07-09 02:26:13 +02:00
|
|
|
} else {
|
2019-04-10 10:37:35 +02:00
|
|
|
error_log('CSRF violation');
|
2016-07-09 02:26:13 +02:00
|
|
|
}
|
|
|
|
} else {
|
2019-04-10 10:37:35 +02:00
|
|
|
if ($dnsmasq_state) {
|
|
|
|
$status->addMessage('Dnsmasq is running', 'success');
|
|
|
|
} else {
|
|
|
|
$status->addMessage('Dnsmasq is not running', 'warning');
|
|
|
|
}
|
2016-07-09 02:26:13 +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 ];
|
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
|
|
|
|
2018-09-25 21:49:54 +02:00
|
|
|
?>
|
2016-07-09 02:26:13 +02:00
|
|
|
<div class="row">
|
|
|
|
<div class="col-lg-12">
|
|
|
|
<div class="panel panel-primary">
|
2017-10-23 21:51:34 +02:00
|
|
|
<div class="panel-heading"><i class="fa fa-exchange fa-fw"></i> <?php echo _("Configure DHCP"); ?></div>
|
2016-07-09 02:26:13 +02:00
|
|
|
<!-- /.panel-heading -->
|
|
|
|
<div class="panel-body">
|
|
|
|
<p><?php $status->showMessages(); ?></p>
|
2019-08-01 16:31:11 +02:00
|
|
|
<form method="POST" action="?page=dhcpd_conf">
|
|
|
|
<?php CSRFToken() ?>
|
2016-07-09 02:26:13 +02:00
|
|
|
<!-- Nav tabs -->
|
|
|
|
<ul class="nav nav-tabs">
|
2017-10-23 21:51:34 +02:00
|
|
|
<li class="active"><a href="#server-settings" data-toggle="tab"><?php echo _("Server settings"); ?></a>
|
2016-07-09 02:26:13 +02:00
|
|
|
</li>
|
2019-08-01 16:31:11 +02:00
|
|
|
<li><a href="#static-leases" data-toggle="tab"><?php echo _("Static Leases") ?></a></li>
|
2017-10-23 21:51:34 +02:00
|
|
|
<li><a href="#client-list" data-toggle="tab"><?php echo _("Client list"); ?></a>
|
2016-07-09 02:26:13 +02:00
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
<!-- Tab panes -->
|
|
|
|
<div class="tab-content">
|
|
|
|
<div class="tab-pane fade in active" id="server-settings">
|
|
|
|
<h4>DHCP server settings</h4>
|
|
|
|
<div class="row">
|
|
|
|
<div class="form-group col-md-4">
|
|
|
|
<label for="code">Interface</label>
|
|
|
|
<select class="form-control" name="interface">
|
2019-04-10 10:37:35 +02:00
|
|
|
<?php
|
2016-07-09 02:26:13 +02:00
|
|
|
exec("ip -o link show | awk -F': ' '{print $2}'", $interfaces);
|
|
|
|
|
2019-04-10 10:37:35 +02:00
|
|
|
foreach ($interfaces as $inet) {
|
|
|
|
$select = '';
|
|
|
|
if ($inet === $conf['interface']) {
|
|
|
|
$select = ' selected="selected"';
|
|
|
|
}
|
2018-08-04 01:58:34 +02:00
|
|
|
|
2019-04-10 10:37:35 +02:00
|
|
|
echo ' <option value="'.htmlspecialchars($inet, ENT_QUOTES).'"'.
|
|
|
|
$select.'>'.htmlspecialchars($inet, ENT_QUOTES).'</option>' , PHP_EOL;
|
|
|
|
}
|
2018-08-04 01:58:34 +02:00
|
|
|
?>
|
2016-07-09 02:26:13 +02:00
|
|
|
</select>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="row">
|
|
|
|
<div class="form-group col-md-4">
|
2017-10-23 21:51:34 +02:00
|
|
|
<label for="code"><?php echo _("Starting IP Address"); ?></label>
|
2018-08-04 01:58:34 +02:00
|
|
|
<input type="text" class="form-control"name="RangeStart" value="<?php echo htmlspecialchars($RangeStart, ENT_QUOTES); ?>" />
|
2016-07-09 02:26:13 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div class="row">
|
|
|
|
<div class="form-group col-md-4">
|
2017-10-23 21:51:34 +02:00
|
|
|
<label for="code"><?php echo _("Ending IP Address"); ?></label>
|
2018-08-04 01:58:34 +02:00
|
|
|
<input type="text" class="form-control" name="RangeEnd" value="<?php echo htmlspecialchars($RangeEnd, ENT_QUOTES); ?>" />
|
2016-07-09 02:26:13 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div class="row">
|
|
|
|
<div class="form-group col-xs-2 col-sm-2">
|
2017-10-23 21:51:34 +02:00
|
|
|
<label for="code"><?php echo _("Lease Time"); ?></label>
|
2018-08-04 01:58:34 +02:00
|
|
|
<input type="text" class="form-control" name="RangeLeaseTime" value="<?php echo htmlspecialchars($arrRangeLeaseTime[1], ENT_QUOTES); ?>" />
|
2016-07-09 02:26:13 +02:00
|
|
|
</div>
|
|
|
|
<div class="col-xs-2 col-sm-2">
|
2017-10-23 21:51:34 +02:00
|
|
|
<label for="code"><?php echo _("Interval"); ?></label>
|
2018-08-04 01:58:34 +02:00
|
|
|
<select name="RangeLeaseTimeUnits" class="form-control" >
|
2018-09-25 21:49:54 +02:00
|
|
|
<option value="m"<?php echo $mselected; ?>><?php echo _("Minute(s)"); ?></option>
|
|
|
|
<option value="h"<?php echo $hselected; ?>><?php echo _("Hour(s)"); ?></option>
|
|
|
|
<option value="d"<?php echo $dselected; ?>><?php echo _("Day(s)"); ?></option>
|
|
|
|
<option value="infinite"<?php echo $infiniteselected; ?>><?php echo _("Infinite"); ?></option>
|
2018-08-04 01:58:34 +02:00
|
|
|
</select>
|
2016-07-09 02:26:13 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
2017-10-23 21:51:34 +02:00
|
|
|
<input type="submit" class="btn btn-outline btn-primary" value="<?php echo _("Save settings"); ?>" name="savedhcpdsettings" />
|
2016-07-09 02:26:13 +02:00
|
|
|
<?php
|
|
|
|
|
2019-04-10 10:37:35 +02:00
|
|
|
if ($dnsmasq_state) {
|
|
|
|
echo '<input type="submit" class="btn btn-warning" value="' . _("Stop dnsmasq") . '" name="stopdhcpd" />';
|
2016-07-09 02:26:13 +02:00
|
|
|
} else {
|
2019-04-10 10:37:35 +02:00
|
|
|
echo'<input type="submit" class="btn btn-success" value="' . _("Start dnsmasq") . '" name="startdhcpd" />';
|
2016-07-09 02:26:13 +02:00
|
|
|
}
|
2019-04-10 10:37:35 +02:00
|
|
|
?>
|
2016-07-09 02:26:13 +02:00
|
|
|
</div><!-- /.tab-pane -->
|
|
|
|
|
|
|
|
<div class="tab-pane fade in" id="client-list">
|
|
|
|
<h4>Client list</h4>
|
|
|
|
<div class="col-lg-12">
|
|
|
|
<div class="panel panel-default">
|
2017-10-23 21:51:34 +02:00
|
|
|
<div class="panel-heading"><?php echo _("Active DHCP leases"); ?></div>
|
2016-07-09 02:26:13 +02:00
|
|
|
<!-- /.panel-heading -->
|
|
|
|
<div class="panel-body">
|
|
|
|
<div class="table-responsive">
|
|
|
|
<table class="table table-hover">
|
|
|
|
<thead>
|
|
|
|
<tr>
|
2017-10-23 21:51:34 +02:00
|
|
|
<th><?php echo _("Expire time"); ?></th>
|
|
|
|
<th><?php echo _("MAC Address"); ?></th>
|
|
|
|
<th><?php echo _("IP Address"); ?></th>
|
|
|
|
<th><?php echo _("Host name"); ?></th>
|
|
|
|
<th><?php echo _("Client ID"); ?></th>
|
2016-07-09 02:26:13 +02:00
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
2018-08-04 01:58:34 +02:00
|
|
|
<?php
|
2019-04-10 10:37:35 +02:00
|
|
|
exec('cat ' . RASPI_DNSMASQ_LEASES, $leases);
|
|
|
|
foreach ($leases as $lease) {
|
2019-03-17 13:33:24 +01:00
|
|
|
echo ' <tr>'.PHP_EOL;
|
2018-08-04 01:58:34 +02:00
|
|
|
$lease_items = explode(' ', $lease);
|
2019-04-10 10:37:35 +02:00
|
|
|
foreach ($lease_items as $lease_item) {
|
2019-03-17 13:33:24 +01:00
|
|
|
echo ' <td>'.htmlspecialchars($lease_item, ENT_QUOTES).'</td>'.PHP_EOL;
|
2018-08-04 01:58:34 +02:00
|
|
|
}
|
2019-03-17 13:33:24 +01:00
|
|
|
echo ' </tr>'.PHP_EOL;
|
2018-08-04 01:58:34 +02:00
|
|
|
};
|
|
|
|
?>
|
2016-07-09 02:26:13 +02:00
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
</div><!-- /.table-responsive -->
|
|
|
|
</div><!-- /.panel-body -->
|
|
|
|
</div><!-- /.panel -->
|
|
|
|
</div><!-- /.col-lg-6 -->
|
|
|
|
</div><!-- /.tab-pane -->
|
2019-08-01 16:31:11 +02:00
|
|
|
|
|
|
|
<div class="tab-pane fade in" id="static-leases">
|
|
|
|
<div class="dhcp-static-leases js-dhcp-static-lease-container">
|
|
|
|
<?php foreach ($dhcpHost as $host): ?>
|
|
|
|
<?php list($mac, $ip) = array_map("trim", explode(",", $host)); ?>
|
|
|
|
<div class="row dhcp-static-lease-row js-dhcp-static-lease-row">
|
|
|
|
<div class="col-md-5 col-xs-5">
|
|
|
|
<input type="text" name="static_leases[mac][]" value="<?php echo htmlspecialchars($mac, ENT_QUOTES) ?>" placeholder="<?php echo _("MAC address") ?>" class="form-control">
|
|
|
|
</div>
|
|
|
|
<div class="col-md-5 col-xs-4">
|
|
|
|
<input type="text" name="static_leases[ip][]" value="<?php echo htmlspecialchars($ip, ENT_QUOTES) ?>" placeholder="<?php echo _("IP address") ?>" class="form-control">
|
|
|
|
</div>
|
|
|
|
<div class="col-md-2 col-xs-3">
|
|
|
|
<button type="button" class="btn btn-danger js-remove-dhcp-static-lease"><?php echo _("Remove") ?></button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<?php endforeach ?>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<h5><?php echo _("Add static DHCP lease") ?></h5>
|
|
|
|
<div class="row dhcp-static-lease-row js-new-dhcp-static-lease">
|
|
|
|
<div class="col-md-5 col-xs-5">
|
|
|
|
<input type="text" name="mac" value="" placeholder="<?php echo _("MAC address") ?>" class="form-control" autofocus="autofocus">
|
|
|
|
</div>
|
|
|
|
<div class="col-md-5 col-xs-4">
|
|
|
|
<input type="text" name="ip" value="" placeholder="<?php echo _("IP address") ?>" class="form-control">
|
|
|
|
</div>
|
|
|
|
<div class="col-md-2 col-xs-3">
|
|
|
|
<button type="button" class="btn btn-success js-add-dhcp-static-lease"><?php echo _("Add") ?></button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<template id="js-dhcp-static-lease-row">
|
|
|
|
<div class="row dhcp-static-lease-row js-dhcp-static-lease-row">
|
|
|
|
<div class="col-md-5 col-xs-5">
|
|
|
|
<input type="text" name="static_leases[mac][]" value="{{ mac }}" placeholder="<?php echo _("MAC address") ?>" class="form-control">
|
|
|
|
</div>
|
|
|
|
<div class="col-md-5 col-xs-4">
|
|
|
|
<input type="text" name="static_leases[ip][]" value="{{ ip }}" placeholder="<?php echo _("IP address") ?>" class="form-control">
|
|
|
|
</div>
|
|
|
|
<div class="col-md-2 col-xs-3">
|
|
|
|
<button type="button" class="btn btn-warning js-remove-dhcp-static-lease"><?php echo _("Remove") ?></button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<input type="submit" class="btn btn-outline btn-primary" value="<?php echo _("Save settings"); ?>" name="savedhcpdsettings" />
|
|
|
|
<?php
|
|
|
|
|
|
|
|
if ($dnsmasq_state) {
|
|
|
|
echo '<input type="submit" class="btn btn-warning" value="' . _("Stop dnsmasq") . '" name="stopdhcpd" />';
|
|
|
|
} else {
|
|
|
|
echo'<input type="submit" class="btn btn-success" value="' . _("Start dnsmasq") . '" name="startdhcpd" />';
|
|
|
|
}
|
|
|
|
?>
|
|
|
|
</div>
|
|
|
|
|
2016-07-09 02:26:13 +02:00
|
|
|
</div><!-- /.tab-content -->
|
2019-08-01 16:31:11 +02:00
|
|
|
</form>
|
2016-07-09 02:26:13 +02:00
|
|
|
</div><!-- ./ Panel body -->
|
2018-08-04 01:58:34 +02:00
|
|
|
<div class="panel-footer"> <?php echo _("Information provided by Dnsmasq"); ?></div>
|
2016-07-09 02:26:13 +02:00
|
|
|
</div><!-- /.panel-primary -->
|
|
|
|
</div><!-- /.col-lg-12 -->
|
|
|
|
</div><!-- /.row -->
|
|
|
|
<?php
|
|
|
|
}
|
|
|
|
|