| 1 |
<?php |
| 2 |
|
| 3 |
use LAM\PDF\PDFTable; |
| 4 |
use LAM\PDF\PDFTableCell; |
| 5 |
use LAM\PDF\PDFTableRow; |
| 6 |
use LAM\TYPES\ConfiguredType; |
| 7 |
|
| 8 |
/* |
| 9 |
|
| 10 |
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/) |
| 11 |
Copyright (C) 2008 Thomas Manninger |
| 12 |
2008 - 2026 Roland Gruber |
| 13 |
|
| 14 |
This program is free software; you can redistribute it and/or modify |
| 15 |
it under the terms of the GNU General Public License as published by |
| 16 |
the Free Software Foundation; either version 2 of the License, or |
| 17 |
(at your option) any later version. |
| 18 |
|
| 19 |
This program is distributed in the hope that it will be useful, |
| 20 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 |
GNU General Public License for more details. |
| 23 |
|
| 24 |
You should have received a copy of the GNU General Public License |
| 25 |
along with this program; if not, write to the Free Software |
| 26 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 27 |
|
| 28 |
*/ |
| 29 |
|
| 30 |
|
| 31 |
/** |
| 32 |
* Manages DHCP ranges for DHCP server. |
| 33 |
* |
| 34 |
* @package modules |
| 35 |
* |
| 36 |
* @author Thomas Manninger |
| 37 |
* @author Roland Gruber |
| 38 |
*/ |
| 39 |
|
| 40 |
/** |
| 41 |
* Manages DHCP ranges for DHCP server. |
| 42 |
* |
| 43 |
* @package modules |
| 44 |
*/ |
| 45 |
class range extends baseModule { |
| 46 |
|
| 47 |
/** @var array<int, array<string, string>> $ranges ranges */ |
| 48 |
public array $ranges = []; |
| 49 |
|
| 50 |
/** Range -> Function attribute_processed already running? */ |
| 51 |
public bool $processed = false; |
| 52 |
|
| 53 |
/** @var int[] $overlapped For check, if IPs overlapped. */ |
| 54 |
private array $overlapped = []; |
| 55 |
|
| 56 |
/** @var array<int, array<string, string[]|string>> $poolsOrig list of pools that currently exist in LDAP */ |
| 57 |
private array $poolsOrig = []; |
| 58 |
/** @var array<int, array<string, string[]|string>> $poolsNew list of pools that need to be updated in LDAP */ |
| 59 |
private array $poolsNew = []; |
| 60 |
|
| 61 |
/** |
| 62 |
* {@inheritDoc} |
| 63 |
*/ |
| 64 |
public function can_manage(): bool { |
| 65 |
return $this->get_scope() == 'dhcp'; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* {@inheritDoc} |
| 70 |
*/ |
| 71 |
public function get_metaData(): array { |
| 72 |
$return = []; |
| 73 |
// alias name |
| 74 |
$return["alias"] = _("Ranges"); |
| 75 |
// this is a base module |
| 76 |
$return["is_base"] = false; |
| 77 |
// icon |
| 78 |
$return['icon'] = 'world-color.svg'; |
| 79 |
// RDN attribute |
| 80 |
$return["RDN"] = ["cn" => "high"]; |
| 81 |
// LDAP filter |
| 82 |
$return["ldap_filter"] = ['or' => "(objectClass=dhcpSubnet)"]; |
| 83 |
// module dependencies |
| 84 |
$return['dependencies'] = ['depends' => ['dhcp_settings'], 'conflicts' => []]; |
| 85 |
// managed object classes |
| 86 |
$return['objectClasses'] = []; |
| 87 |
// managed attributes |
| 88 |
$return['attributes'] = ['dhcpRange']; |
| 89 |
// help Entries |
| 90 |
$return['help'] = [ |
| 91 |
'range_from' => [ |
| 92 |
"Headline" => _("Range from"), |
| 93 |
"Text" => _("The starting IP address of the range.") |
| 94 |
], |
| 95 |
'range_to' => [ |
| 96 |
"Headline" => _("Range to"), |
| 97 |
"Text" => _("The ending IP address of the range.") |
| 98 |
], |
| 99 |
'poolName' => [ |
| 100 |
"Headline" => _("Name"), 'attr' => 'cn', |
| 101 |
"Text" => _("The pool's name.") |
| 102 |
], |
| 103 |
'poolPeer' => [ |
| 104 |
"Headline" => _("Failover peer"), 'attr' => 'dhcpStatements', |
| 105 |
"Text" => _("The pool's failover peer.") |
| 106 |
], |
| 107 |
]; |
| 108 |
|
| 109 |
// available PDF fields |
| 110 |
$return['PDF_fields'] = ['ranges' => _('Ranges')]; |
| 111 |
return $return; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* {@inheritDoc} |
| 116 |
*/ |
| 117 |
protected function load_Messages(): void { |
| 118 |
$this->messages['range_errors'][0] = ['ERROR', _('One or more errors occurred. The invalid fields are marked.'), '']; |
| 119 |
$this->messages['add_range'][0] = ['ERROR', _('New range'), _('Adding the range failed because errors occurred.')]; |
| 120 |
$this->messages['drop_range'][0] = ['ERROR', _('Delete range'), _('It is not possible to delete all ranges.')]; |
| 121 |
$this->messages['pool_cn'][0] = ['ERROR', _('Name'), _('Please enter a pool name.')]; |
| 122 |
$this->messages['pool_range'][0] = ['ERROR', _('Please enter at least one range for pool "%s".'), null]; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Calculates the subnet for a given IP and netmask. |
| 127 |
* |
| 128 |
* @param string $ip IP address |
| 129 |
* @param string $mask network mask |
| 130 |
* @return string long IP |
| 131 |
*/ |
| 132 |
private static function calculateSubnet(string $ip, string $mask): string { |
| 133 |
return long2ip(ip2long($ip) & ip2long($mask)); |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Checks if the first IP is smaller than the second IP. |
| 138 |
* |
| 139 |
* @param String $first_ip first ip |
| 140 |
* @param String $second_ip second ip |
| 141 |
* |
| 142 |
* @return bool true, if it's a valid Range, else false; |
| 143 |
**/ |
| 144 |
|
| 145 |
public function check_range($first_ip, $second_ip): bool { |
| 146 |
$ex_first = explode(".", $first_ip); |
| 147 |
$ex_second = explode(".", $second_ip); |
| 148 |
return ($ex_first[0] < $ex_second[0]) |
| 149 |
|| ($ex_first[1] < $ex_second[1]) |
| 150 |
|| ($ex_first[2] < $ex_second[2]) |
| 151 |
|| ($ex_first[3] < $ex_second[3]); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Check if an IP address is in the correct subnet. |
| 156 |
* |
| 157 |
* @param String $ip IP address |
| 158 |
* @param String $subnet subnet |
| 159 |
* @param String $mask network mask |
| 160 |
* @return bool true if the range and subnet valid, else false |
| 161 |
**/ |
| 162 |
public static function check_subnet_range($ip, $subnet, $mask): bool { |
| 163 |
$ipSubnet = range::calculateSubnet($ip, $mask); |
| 164 |
return ($subnet == $ipSubnet); |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* |
| 169 |
* Checks if ranges are not overlapped. |
| 170 |
* |
| 171 |
* @param string $ip first ip |
| 172 |
* @param string $ipB second ip |
| 173 |
* |
| 174 |
* @return bool not overlapped |
| 175 |
* |
| 176 |
**/ |
| 177 |
private function isNotOverlappedRange(string $ip, string $ipB): bool { |
| 178 |
$ex = explode(".", $ip); |
| 179 |
$exB = explode(".", $ipB); |
| 180 |
for ($n = $ex[3]; $n <= $exB[3]; $n++) { |
| 181 |
if (in_array($n, $this->overlapped)) { |
| 182 |
return false; |
| 183 |
} |
| 184 |
else { |
| 185 |
$this->overlapped[] = $n; |
| 186 |
} |
| 187 |
} |
| 188 |
return true; |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* |
| 193 |
* Reset the overlapped_range() function |
| 194 |
* |
| 195 |
**/ |
| 196 |
private function reset_overlapped_range(): void { |
| 197 |
$this->overlapped = []; |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* {@inheritDoc} |
| 202 |
*/ |
| 203 |
public function getButtonStatus(): string { |
| 204 |
if (!$this->isRootNode()) { |
| 205 |
return "enabled"; |
| 206 |
} |
| 207 |
else { |
| 208 |
return "hidden"; |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* {@inheritDoc} |
| 214 |
*/ |
| 215 |
public function load_attributes(array $attributes): void { |
| 216 |
parent::load_attributes($attributes); |
| 217 |
// Load DHCP Options: |
| 218 |
if (!$this->isRootNode()) { |
| 219 |
$this->orig = $attributes; |
| 220 |
$this->attributes = $attributes; |
| 221 |
// Load DHCP Options: |
| 222 |
if (isset($attributes['dhcpRange']) && is_array($attributes['dhcpRange'])) { |
| 223 |
natcasesort($attributes['dhcpRange']); |
| 224 |
foreach ($attributes['dhcpRange'] as $id => $value) { |
| 225 |
$ex = explode(" ", $value); |
| 226 |
|
| 227 |
// DHCP Range ins Array laden: |
| 228 |
$this->ranges[$id] = ['range_start' => $ex[0], 'range_end' => $ex[1]]; |
| 229 |
} |
| 230 |
} |
| 231 |
// load pools |
| 232 |
$this->loadPools(); |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* |
| 238 |
* Adapt the Ranges with the subnet. |
| 239 |
* |
| 240 |
* @return bool true, if ranges were edit. |
| 241 |
* |
| 242 |
**/ |
| 243 |
public function reload_ranges() { |
| 244 |
$range_edit = false; // Range were edited? |
| 245 |
// Only run it, when ranges already exists: |
| 246 |
$ex_subnet = explode(".", $this->getAccountContainer()->getAccountModule('dhcp_settings')->attributes['cn'][0]); |
| 247 |
$dhcpAttrs = $this->getAccountContainer()->getAccountModule('dhcp_settings')->getAttributes(); |
| 248 |
$mask = $this->getAccountContainer()->getAccountModule('dhcp_settings')->getDHCPOption('subnet-mask'); |
| 249 |
foreach ($this->ranges as $id => $range) { |
| 250 |
if (!empty($range['range_start']) && !range::check_subnet_range($range['range_start'], $dhcpAttrs['cn'][0], $mask)) { |
| 251 |
// adapt range |
| 252 |
$ex = explode(".", $range['range_start']); |
| 253 |
$tmp = $range['range_start']; |
| 254 |
$this->ranges[$id]['range_start'] = $ex_subnet[0] . "." . $ex_subnet[1] . "." . $ex_subnet[2] . "." . $ex[3]; |
| 255 |
if ($tmp != $this->ranges[$id]['range_start']) { |
| 256 |
$range_edit = true; |
| 257 |
} |
| 258 |
} |
| 259 |
if (!empty($range['range_end']) && !range::check_subnet_range($range['range_end'], $dhcpAttrs['cn'][0], $mask)) { |
| 260 |
// adapt range |
| 261 |
$ex = explode(".", $range['range_end']); |
| 262 |
$tmp = $range['range_end']; |
| 263 |
$this->ranges[$id]['range_end'] = $ex_subnet[0] . "." . $ex_subnet[1] . "." . $ex_subnet[2] . "." . $ex[3]; |
| 264 |
if ($tmp != $this->ranges[$id]['range_end']) { |
| 265 |
$range_edit = true; |
| 266 |
} |
| 267 |
} |
| 268 |
} |
| 269 |
if ($range_edit) { |
| 270 |
// sort the range new, id it was edit. |
| 271 |
foreach ($this->ranges as $id => $range) { |
| 272 |
$this->attributes['dhcpRange'][$id] = $range['range_start'] . " " . $range['range_end']; |
| 273 |
} |
| 274 |
} |
| 275 |
return $range_edit; |
| 276 |
} |
| 277 |
|
| 278 |
/** |
| 279 |
* {@inheritDoc} |
| 280 |
*/ |
| 281 |
public function process_attributes(): array { |
| 282 |
$errors = []; |
| 283 |
if ($this->isRootNode()) { |
| 284 |
return $errors; |
| 285 |
} |
| 286 |
$subnet = $this->getAccountContainer()->getAccountModule('dhcp_settings')->attributes['cn'][0]; |
| 287 |
$mask = $this->getAccountContainer()->getAccountModule('dhcp_settings')->getDHCPOption('subnet-mask'); |
| 288 |
$errorOccurred = false; |
| 289 |
$this->reset_overlapped_range(); |
| 290 |
if ($this->getAccountContainer()->getAccountModule('dhcp_settings')->attributes['cn'][0] != "") { |
| 291 |
|
| 292 |
foreach (array_keys($this->ranges) as $id) { |
| 293 |
// Check if range is to drop |
| 294 |
if (isset($_POST['drop_range_' . $id])) { |
| 295 |
// drop Range: |
| 296 |
unset($this->ranges[$id]); |
| 297 |
unset($this->attributes['dhcpRange'][$id]); |
| 298 |
continue; |
| 299 |
} |
| 300 |
|
| 301 |
// if the inputs are empty, then do nothing: |
| 302 |
if ($_POST['range_start_' . $id] == "" && $_POST['range_end_' . $id] == "") { |
| 303 |
unset($this->attributes['dhcpRange'][$id]); |
| 304 |
} |
| 305 |
else { |
| 306 |
// Check range_start: |
| 307 |
$_POST['range_start_' . $id] = trim($_POST['range_start_' . $id]); |
| 308 |
if (!check_ip($_POST['range_start_' . $id])) { |
| 309 |
$errorOccurred = true; |
| 310 |
} |
| 311 |
$this->ranges[$id]['range_start'] = $_POST['range_start_' . $id]; |
| 312 |
|
| 313 |
// Check end: |
| 314 |
$_POST['range_end_' . $id] = trim($_POST['range_end_' . $id]); |
| 315 |
if (!check_ip($_POST['range_end_' . $id])) { |
| 316 |
$errorOccurred = true; |
| 317 |
} |
| 318 |
$this->ranges[$id]['range_end'] = $_POST['range_end_' . $id]; |
| 319 |
|
| 320 |
// Check if ip overlapped: |
| 321 |
if (!$this->isNotOverlappedRange($_POST['range_start_' . $id], $_POST['range_end_' . $id])) { |
| 322 |
$errorOccurred = true; |
| 323 |
} |
| 324 |
|
| 325 |
// Check if Subnet and range first are valid: |
| 326 |
if (!range::check_subnet_range($_POST['range_start_' . $id], $subnet, $mask)) { |
| 327 |
$errorOccurred = true; |
| 328 |
} |
| 329 |
|
| 330 |
// Check if Subnet and range last are valid: |
| 331 |
if (!range::check_subnet_range($_POST['range_end_' . $id], $subnet, $mask)) { |
| 332 |
$errorOccurred = true; |
| 333 |
} |
| 334 |
|
| 335 |
// Check if Range is valid |
| 336 |
if (!$this->check_range($_POST['range_start_' . $id], $_POST['range_end_' . $id])) { |
| 337 |
$errorOccurred = true; |
| 338 |
} |
| 339 |
|
| 340 |
// Check, if range_start and range_end are ok! |
| 341 |
if (!$errorOccurred) { |
| 342 |
$this->attributes['dhcpRange'][$id] = $_POST['range_start_' . $id] . " " . $_POST['range_end_' . $id]; |
| 343 |
} |
| 344 |
else { |
| 345 |
unset($this->attributes['dhcpRange'][$id]); |
| 346 |
} |
| 347 |
} |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
// Add new Range |
| 352 |
if (isset($_POST['add_range'])) { |
| 353 |
// Check, if there were no errors: |
| 354 |
if ($errorOccurred) { |
| 355 |
$errors[] = $this->messages['add_range'][0]; |
| 356 |
} |
| 357 |
else { |
| 358 |
// Add Range: |
| 359 |
$this->ranges[] = ['range_start' => '', 'range_end' => '']; |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
// update pool data |
| 364 |
foreach (array_keys($this->poolsNew) as $index) { |
| 365 |
// delete pool |
| 366 |
if (isset($_POST['del_pool_' . $index])) { |
| 367 |
unset($this->poolsNew[$index]); |
| 368 |
continue; |
| 369 |
} |
| 370 |
// name |
| 371 |
$this->poolsNew[$index]['cn'][0] = trim($_POST['pool_cn_' . $index]); |
| 372 |
if (empty($this->poolsNew[$index]['cn'][0])) { |
| 373 |
$errors[] = $this->messages['pool_cn'][0]; |
| 374 |
} |
| 375 |
// failover peer |
| 376 |
$peer = trim($_POST['pool_peer_' . $index]); |
| 377 |
if (!empty($this->poolsNew[$index]['dhcpstatements'])) { |
| 378 |
// remove old peer setting |
| 379 |
foreach ($this->poolsNew[$index]['dhcpstatements'] as $indexStmt => $stmt) { |
| 380 |
if (str_starts_with($stmt, 'failover peer "')) { |
| 381 |
unset($this->poolsNew[$index]['dhcpstatements'][$indexStmt]); |
| 382 |
break; |
| 383 |
} |
| 384 |
} |
| 385 |
} |
| 386 |
if (!empty($peer)) { |
| 387 |
$this->poolsNew[$index]['dhcpstatements'][] = 'failover peer "' . $peer . '"'; |
| 388 |
} |
| 389 |
if (!empty($this->poolsNew[$index]['dhcprange'])) { |
| 390 |
foreach (array_keys($this->poolsNew[$index]['dhcprange']) as $rIndex) { |
| 391 |
// delete pool range |
| 392 |
if (isset($_POST['drop_poolrange_' . $index . '_' . $rIndex])) { |
| 393 |
unset($this->poolsNew[$index]['dhcprange'][$rIndex]); |
| 394 |
continue; |
| 395 |
} |
| 396 |
$from = trim($_POST['pool_from_' . $index . '_' . $rIndex]); |
| 397 |
$to = trim($_POST['pool_to_' . $index . '_' . $rIndex]); |
| 398 |
if (empty($from) && empty($to)) { |
| 399 |
unset($this->poolsNew[$index]['dhcprange'][$rIndex]); |
| 400 |
} |
| 401 |
else { |
| 402 |
$this->poolsNew[$index]['dhcprange'][$rIndex] = $from . ' ' . $to; |
| 403 |
// check ranges |
| 404 |
if (!check_ip($from) || !check_ip($to) |
| 405 |
|| !$this->isNotOverlappedRange($from, $to) |
| 406 |
|| !range::check_subnet_range($from, $subnet, $mask) |
| 407 |
|| !range::check_subnet_range($to, $subnet, $mask) |
| 408 |
|| !$this->check_range($from, $to)) { |
| 409 |
$errorOccurred = true; |
| 410 |
} |
| 411 |
} |
| 412 |
} |
| 413 |
} |
| 414 |
// check if at least one range is still set |
| 415 |
if (empty($this->poolsNew[$index]['dhcprange'])) { |
| 416 |
$error = $this->messages['pool_range'][0]; |
| 417 |
$error[] = [htmlspecialchars($this->poolsNew[$index]['cn'][0])]; |
| 418 |
$errors[] = $error; |
| 419 |
} |
| 420 |
// add pool range |
| 421 |
if (isset($_POST['add_poolrange_' . $index])) { |
| 422 |
$this->poolsNew[$index]['dhcprange'][] = ''; |
| 423 |
} |
| 424 |
} |
| 425 |
// add a new pool |
| 426 |
if (isset($_POST['add_pool'])) { |
| 427 |
$this->poolsNew[] = [ |
| 428 |
'objectclass' => ['dhcpPool'], |
| 429 |
'dhcprange' => [''] |
| 430 |
]; |
| 431 |
} |
| 432 |
|
| 433 |
// Check if there was a error: |
| 434 |
if ($errorOccurred) { |
| 435 |
$errors[] = $this->messages['range_errors'][0]; |
| 436 |
} |
| 437 |
|
| 438 |
$this->processed = true; |
| 439 |
return $errors; |
| 440 |
} |
| 441 |
|
| 442 |
/** |
| 443 |
* {@inheritDoc} |
| 444 |
*/ |
| 445 |
public function display_html_attributes(): htmlElement|array { |
| 446 |
$return = new htmlResponsiveRow(); |
| 447 |
if ($this->getAccountContainer()->getAccountModule('dhcp_settings')->attributes['cn'][0] == "") { |
| 448 |
$return->add(new htmlStatusMessage('INFO', _("Please fill out the DHCP settings first."))); |
| 449 |
return $return; |
| 450 |
} |
| 451 |
$this->reset_overlapped_range(); |
| 452 |
$mask = $this->getAccountContainer()->getAccountModule('dhcp_settings')->getDHCPOption('subnet-mask'); |
| 453 |
$subnet = $this->getAccountContainer()->getAccountModule('dhcp_settings')->attributes['cn'][0]; |
| 454 |
foreach ($this->ranges as $id => $range) { |
| 455 |
// Range start |
| 456 |
$error = ""; |
| 457 |
if (isset($range['range_start']) && !empty($range['range_start'])) { |
| 458 |
if ($this->processed && !check_ip($range['range_start'])) { |
| 459 |
$error = _("The IP address is invalid."); |
| 460 |
} |
| 461 |
elseif ($this->processed && !$this->check_range($range['range_start'], $range['range_end'])) { |
| 462 |
$error = _("The range end needs to be greater than the range start."); |
| 463 |
} |
| 464 |
elseif ($this->processed && !range::check_subnet_range($range['range_start'], $subnet, $mask)) { |
| 465 |
$error = _("The IP does not match the subnet."); |
| 466 |
} |
| 467 |
elseif ($this->processed && !$this->isNotOverlappedRange($range['range_start'], $range['range_end'])) { |
| 468 |
$error = _("The range conflicts with another range."); |
| 469 |
} |
| 470 |
} |
| 471 |
$fromInput = new htmlResponsiveInputField(_('Range from'), 'range_start_' . $id, $range['range_start'], 'range_from', true); |
| 472 |
$return->add($fromInput); |
| 473 |
if (!empty($error)) { |
| 474 |
$return->add(new htmlOutputText($error)); |
| 475 |
} |
| 476 |
// Range end |
| 477 |
$error = ""; |
| 478 |
if (isset($range['range_end']) && !empty($range['range_end'])) { |
| 479 |
if ($this->processed && !check_ip($range['range_end'])) { |
| 480 |
$error = _("The IP address is invalid."); |
| 481 |
} |
| 482 |
elseif ($this->processed && !range::check_subnet_range($range['range_end'], $subnet, $mask)) { |
| 483 |
$error = _("The IP does not match the subnet."); |
| 484 |
} |
| 485 |
} |
| 486 |
$toInput = new htmlResponsiveInputField(_('Range to'), 'range_end_' . $id, $range['range_end'], 'range_to', true); |
| 487 |
$return->add($toInput); |
| 488 |
if (!empty($error)) { |
| 489 |
$return->add(new htmlOutputText($error)); |
| 490 |
} |
| 491 |
// Drop range: |
| 492 |
$dropButton = new htmlButton('drop_range_' . $id, _('Delete range')); |
| 493 |
$dropButton->setCSSClasses(['lam-danger']); |
| 494 |
$return->add($dropButton, 12, 12, 12, 'text-center'); |
| 495 |
|
| 496 |
$return->addVerticalSpacer('2rem'); |
| 497 |
} |
| 498 |
|
| 499 |
// add new range |
| 500 |
$addButton = new htmlButton('add_range', _('New range')); |
| 501 |
$return->add($addButton); |
| 502 |
|
| 503 |
// pools |
| 504 |
if (!empty($this->poolsNew)) { |
| 505 |
$return->add(new htmlSubTitle(_('Pools'))); |
| 506 |
foreach ($this->poolsNew as $index => $poolAttrs) { |
| 507 |
$cn = empty($poolAttrs['cn'][0]) ? '' : $poolAttrs['cn'][0]; |
| 508 |
$nameField = new htmlResponsiveInputField(_('Name'), 'pool_cn_' . $index, $cn, 'poolName', true); |
| 509 |
$return->add($nameField); |
| 510 |
$peer = ''; |
| 511 |
if (!empty($poolAttrs['dhcpstatements'])) { |
| 512 |
foreach ($poolAttrs['dhcpstatements'] as $statement) { |
| 513 |
if (str_starts_with($statement, 'failover peer "')) { |
| 514 |
$peer = substr($statement, strlen('failover peer "'), -1); |
| 515 |
} |
| 516 |
} |
| 517 |
} |
| 518 |
$return->add(new htmlResponsiveInputField(_('Failover peer'), 'pool_peer_' . $index, $peer, 'poolPeer')); |
| 519 |
$delPoolButton = new htmlButton('del_pool_' . $index, _('Delete pool')); |
| 520 |
$delPoolButton->setCSSClasses(['lam-danger']); |
| 521 |
$return->add($delPoolButton, 12, 12, 12, 'text-center'); |
| 522 |
$return->addVerticalSpacer('2rem'); |
| 523 |
if (!empty($poolAttrs['dhcprange'])) { |
| 524 |
foreach ($poolAttrs['dhcprange'] as $rIndex => $range) { |
| 525 |
$range = explode(' ', $range); |
| 526 |
$from = empty($range[0]) ? '' : $range[0]; |
| 527 |
$to = empty($range[1]) ? '' : $range[1]; |
| 528 |
$fromInput = new htmlResponsiveInputField(_('Range from'), 'pool_from_' . $index . '_' . $rIndex, $from, 'range_from', true); |
| 529 |
$toInput = new htmlResponsiveInputField(_('Range to'), 'pool_to_' . $index . '_' . $rIndex, $to, 'range_to', true); |
| 530 |
$return->add($fromInput); |
| 531 |
$message = ''; |
| 532 |
if (!empty($from) && $this->processed) { |
| 533 |
if (!check_ip($from)) { |
| 534 |
$message = _("The IP address is invalid."); |
| 535 |
} |
| 536 |
elseif (!$this->check_range($from, $to)) { |
| 537 |
$message = _("The range end needs to be greater than the range start."); |
| 538 |
} |
| 539 |
elseif (!range::check_subnet_range($from, $subnet, $mask)) { |
| 540 |
$message = _("The IP does not match the subnet."); |
| 541 |
} |
| 542 |
elseif (!$this->isNotOverlappedRange($from, $to)) { |
| 543 |
$message = _("The range conflicts with another range."); |
| 544 |
} |
| 545 |
} |
| 546 |
$return->add($toInput); |
| 547 |
if (!empty($message)) { |
| 548 |
$return->add(new htmlOutputText($message)); |
| 549 |
} |
| 550 |
$message = ''; |
| 551 |
if (!empty($to) && $this->processed) { |
| 552 |
if (!check_ip($to)) { |
| 553 |
$message = _("The IP address is invalid."); |
| 554 |
} |
| 555 |
elseif (!range::check_subnet_range($to, $subnet, $mask)) { |
| 556 |
$message = _("The IP does not match the subnet."); |
| 557 |
} |
| 558 |
} |
| 559 |
if (!empty($message)) { |
| 560 |
$return->add(new htmlOutputText($message)); |
| 561 |
} |
| 562 |
// drop range |
| 563 |
$dropButton = new htmlButton('drop_poolrange_' . $index . '_' . $rIndex, _('Delete range')); |
| 564 |
$dropButton->setCSSClasses(['lam-danger']); |
| 565 |
$return->add($dropButton, 12, 12, 12, 'text-center'); |
| 566 |
$return->addVerticalSpacer('4rem'); |
| 567 |
} |
| 568 |
} |
| 569 |
// add new range |
| 570 |
$newButton = new htmlButton('add_poolrange_' . $index, _('New range')); |
| 571 |
$return->add($newButton); |
| 572 |
$return->addVerticalSpacer('4rem'); |
| 573 |
} |
| 574 |
$return->addVerticalSpacer('4rem'); |
| 575 |
} |
| 576 |
|
| 577 |
// add new range pool |
| 578 |
$addButton = new htmlButton('add_pool', _('New pool')); |
| 579 |
$return->add($addButton); |
| 580 |
$return->addVerticalSpacer('4rem'); |
| 581 |
|
| 582 |
return $return; |
| 583 |
} |
| 584 |
|
| 585 |
/** |
| 586 |
* {@inheritDoc} |
| 587 |
*/ |
| 588 |
public function postModifyActions(bool $newAccount, array $attributes): array { |
| 589 |
$errors = []; |
| 590 |
// update pool data |
| 591 |
// add new pools |
| 592 |
foreach ($this->poolsNew as $pool) { |
| 593 |
$found = false; |
| 594 |
foreach ($this->poolsOrig as $poolOrig) { |
| 595 |
if ($pool['cn'][0] == $poolOrig['cn'][0]) { |
| 596 |
$found = true; |
| 597 |
break; |
| 598 |
} |
| 599 |
} |
| 600 |
if (!$found) { |
| 601 |
// create new pool |
| 602 |
$dn = 'cn=' . $pool['cn'][0] . ',' . $this->getAccountContainer()->finalDN; |
| 603 |
if (isset($pool['dn'])) { |
| 604 |
unset($pool['dn']); |
| 605 |
} |
| 606 |
$success = @ldap_add($_SESSION['ldap']->server(), $dn, $pool); |
| 607 |
if (!$success) { |
| 608 |
$msg = sprintf(_('Was unable to create DN: %s.'), $dn); |
| 609 |
logNewMessage(LOG_ERR, $msg . getDefaultLDAPErrorString($_SESSION['ldap']->server())); |
| 610 |
$errors[] = ['ERROR', $msg, getDefaultLDAPErrorString($_SESSION['ldap']->server())]; |
| 611 |
} |
| 612 |
} |
| 613 |
} |
| 614 |
// update existing pools |
| 615 |
foreach ($this->poolsNew as $pool) { |
| 616 |
foreach ($this->poolsOrig as $poolOrig) { |
| 617 |
if ($pool['cn'][0] == $poolOrig['cn'][0]) { |
| 618 |
// check for changes |
| 619 |
$mod = []; |
| 620 |
// check attributes |
| 621 |
$attrs = ['dhcpstatements', 'dhcprange']; |
| 622 |
foreach ($attrs as $attr) { |
| 623 |
$changed = false; |
| 624 |
if (empty($pool[$attr]) && empty($poolOrig[$attr])) { |
| 625 |
continue; |
| 626 |
} |
| 627 |
if ((!empty($pool[$attr]) && empty($poolOrig[$attr])) |
| 628 |
|| (empty($pool[$attr]) && !empty($poolOrig[$attr])) |
| 629 |
|| (count($pool[$attr]) !== count($poolOrig[$attr]))) { |
| 630 |
$changed = true; |
| 631 |
} |
| 632 |
else { |
| 633 |
sort($pool[$attr]); |
| 634 |
sort($poolOrig[$attr]); |
| 635 |
foreach ($pool[$attr] as $index => $val) { |
| 636 |
if ($val != $poolOrig[$attr][$index]) { |
| 637 |
$changed = true; |
| 638 |
} |
| 639 |
} |
| 640 |
} |
| 641 |
if ($changed && isset($pool[$attr])) { |
| 642 |
$mod[$attr] = $pool[$attr]; |
| 643 |
} |
| 644 |
elseif ($changed) { |
| 645 |
$mod[$attr] = []; |
| 646 |
} |
| 647 |
} |
| 648 |
if (!empty($mod)) { |
| 649 |
$dn = 'cn=' . $pool['cn'][0] . ',' . $this->getAccountContainer()->finalDN; |
| 650 |
$success = @ldap_modify($_SESSION['ldap']->server(), $dn, $mod); |
| 651 |
if (!$success) { |
| 652 |
$msg = sprintf(_('Was unable to modify attributes of DN: %s.'), $dn); |
| 653 |
logNewMessage(LOG_ERR, $msg . getDefaultLDAPErrorString($_SESSION['ldap']->server())); |
| 654 |
$errors[] = ['ERROR', $msg, getDefaultLDAPErrorString($_SESSION['ldap']->server())]; |
| 655 |
} |
| 656 |
} |
| 657 |
break; |
| 658 |
} |
| 659 |
} |
| 660 |
} |
| 661 |
// delete obsolete pools |
| 662 |
foreach ($this->poolsOrig as $poolOrig) { |
| 663 |
$found = false; |
| 664 |
foreach ($this->poolsNew as $pool) { |
| 665 |
if ($poolOrig['cn'][0] == $pool['cn'][0]) { |
| 666 |
$found = true; |
| 667 |
break; |
| 668 |
} |
| 669 |
} |
| 670 |
if (!$found) { |
| 671 |
// delete pool |
| 672 |
$dn = 'cn=' . $poolOrig['cn'][0] . ',' . $this->getAccountContainer()->finalDN; |
| 673 |
$success = @ldap_delete($_SESSION['ldap']->server(), $dn); |
| 674 |
if (!$success) { |
| 675 |
$msg = sprintf(_('Was unable to delete DN: %s.'), $dn); |
| 676 |
logNewMessage(LOG_ERR, $msg . getDefaultLDAPErrorString($_SESSION['ldap']->server())); |
| 677 |
$errors[] = ['ERROR', $msg, getDefaultLDAPErrorString($_SESSION['ldap']->server())]; |
| 678 |
} |
| 679 |
} |
| 680 |
} |
| 681 |
return $errors; |
| 682 |
} |
| 683 |
|
| 684 |
/** |
| 685 |
* {@inheritDoc} |
| 686 |
*/ |
| 687 |
public function save_attributes(): array { |
| 688 |
$return = []; |
| 689 |
// Get easy attributes |
| 690 |
if (!$this->isRootNode()) { |
| 691 |
$return = $this->getAccountContainer()->save_module_attributes($this->attributes, $this->orig); |
| 692 |
} |
| 693 |
// Return attributes |
| 694 |
return $return; |
| 695 |
} |
| 696 |
|
| 697 |
/** |
| 698 |
* {@inheritDoc} |
| 699 |
*/ |
| 700 |
public function get_pdfEntries(array $pdfKeys, string $typeId): array { |
| 701 |
$return = []; |
| 702 |
if ($this->ranges !== []) { |
| 703 |
$pdfTable = new PDFTable(_('Ranges')); |
| 704 |
for ($i = 0; $i < count($this->ranges); $i++) { |
| 705 |
$start = $this->ranges[$i]['range_start']; |
| 706 |
$end = $this->ranges[$i]['range_end']; |
| 707 |
$pdfRow = new PDFTableRow(); |
| 708 |
$pdfRow->cells[] = new PDFTableCell($start . " - " . $end); |
| 709 |
$pdfTable->rows[] = $pdfRow; |
| 710 |
} |
| 711 |
foreach ($this->poolsNew as $poolAttrs) { |
| 712 |
$cn = empty($poolAttrs['cn'][0]) ? '' : $poolAttrs['cn'][0]; |
| 713 |
$peer = ''; |
| 714 |
if (!empty($poolAttrs['dhcpstatements'])) { |
| 715 |
foreach ($poolAttrs['dhcpstatements'] as $statement) { |
| 716 |
if (str_starts_with($statement, 'failover peer "')) { |
| 717 |
$peer = ' (' . substr($statement, strlen('failover peer "'), -1) . ')'; |
| 718 |
} |
| 719 |
} |
| 720 |
} |
| 721 |
if (!empty($poolAttrs['dhcprange'])) { |
| 722 |
foreach ($poolAttrs['dhcprange'] as $range) { |
| 723 |
$range = explode(' ', $range); |
| 724 |
$from = empty($range[0]) ? '' : $range[0]; |
| 725 |
$to = empty($range[1]) ? '' : $range[1]; |
| 726 |
$pdfRow = new PDFTableRow(); |
| 727 |
$pdfRow->cells[] = new PDFTableCell($cn . $peer . ': ' . $from . " - " . $to); |
| 728 |
$pdfTable->rows[] = $pdfRow; |
| 729 |
} |
| 730 |
} |
| 731 |
} |
| 732 |
$this->addPDFTable($return, 'ranges', $pdfTable); |
| 733 |
} |
| 734 |
return $return; |
| 735 |
} |
| 736 |
|
| 737 |
/** |
| 738 |
* Loads the list of pools from LDAP. |
| 739 |
*/ |
| 740 |
private function loadPools(): void { |
| 741 |
$dn = $this->getAccountContainer()->dn_orig; |
| 742 |
$this->poolsOrig = searchLDAP($dn, '(objectclass=dhcpPool)', ['*']); |
| 743 |
foreach ($this->poolsOrig as &$pool) { |
| 744 |
if (!empty($pool['dhcprange'])) { |
| 745 |
sort($pool['dhcprange']); |
| 746 |
} |
| 747 |
} |
| 748 |
$this->poolsNew = $this->poolsOrig; |
| 749 |
} |
| 750 |
|
| 751 |
/** |
| 752 |
* Returns if the current DN is the root entry. |
| 753 |
* |
| 754 |
* @return bool is root |
| 755 |
*/ |
| 756 |
private function isRootNode() { |
| 757 |
$rootSuffix = $this->getAccountContainer()->get_type()->getSuffix(); |
| 758 |
return $this->getAccountContainer()->dn_orig == $rootSuffix; |
| 759 |
} |
| 760 |
|
| 761 |
/** |
| 762 |
* @inheritDoc |
| 763 |
*/ |
| 764 |
public function getListAttributeDescriptions(ConfiguredType $type): array { |
| 765 |
return [ |
| 766 |
"dhcprange" => _("Ranges"), |
| 767 |
]; |
| 768 |
} |
| 769 |
|
| 770 |
/** |
| 771 |
* @inheritDoc |
| 772 |
*/ |
| 773 |
public function getListRenderFunction(string $attributeName, ConfiguredType $type): ?callable { |
| 774 |
if ($attributeName === 'dhcprange') { |
| 775 |
return function(array $entry, string $attribute): htmlElement { |
| 776 |
if (!empty($entry[$attribute][0])) { |
| 777 |
$table = new htmlTable(); |
| 778 |
$table->setCSSClasses(['nowrap']); |
| 779 |
$ranges = []; |
| 780 |
foreach ($entry['dhcprange'] as $value) { |
| 781 |
if (!empty($value) && !is_numeric($value)) { |
| 782 |
$ex = explode(" ", $value); |
| 783 |
$row = [ |
| 784 |
new htmlOutputText($ex[0]), |
| 785 |
new htmlOutputText(' - '), |
| 786 |
new htmlOutputText($ex[1]) |
| 787 |
]; |
| 788 |
$ranges[$ex[0] . ' - ' . $ex[1]] = $row; |
| 789 |
} |
| 790 |
} |
| 791 |
$pooledRanges = searchLDAP($entry['dn'], '(objectclass=dhcpPool)', ['dhcprange']); |
| 792 |
foreach ($pooledRanges as $pool) { |
| 793 |
if (empty($pool['dhcprange'])) { |
| 794 |
continue; |
| 795 |
} |
| 796 |
foreach ($pool['dhcprange'] as $value) { |
| 797 |
if (!empty($value) && !is_numeric($value)) { |
| 798 |
$ex = explode(" ", $value); |
| 799 |
$row = [ |
| 800 |
new htmlOutputText($ex[0]), |
| 801 |
new htmlOutputText(' - '), |
| 802 |
new htmlOutputText($ex[1]) |
| 803 |
]; |
| 804 |
$ranges[$ex[0] . ' - ' . $ex[1]] = $row; |
| 805 |
} |
| 806 |
} |
| 807 |
} |
| 808 |
uksort($ranges, strnatcasecmp(...)); |
| 809 |
foreach ($ranges as $row) { |
| 810 |
$table->addElement($row[0]); |
| 811 |
$table->addElement($row[1]); |
| 812 |
$table->addElement($row[2], true); |
| 813 |
} |
| 814 |
return $table; |
| 815 |
} |
| 816 |
return new htmlGroup(); |
| 817 |
}; |
| 818 |
} |
| 819 |
return null; |
| 820 |
} |
| 821 |
|
| 822 |
} |