Debian Codegraph @

lib/modules/ddns.inc

raw
1 <?php
2
3 use LAM\TYPES\ConfiguredType;
4
5 /*
6
7 This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
8 Copyright (C) 2008 Thomas Manninger
9 2008 - 2026 Roland Gruber
10
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24
25 */
26
27 /**
28 * Manages DDNS entries.
29 *
30 * @package modules
31 *
32 * @author Thomas Manninger
33 * @author Roland Gruber
34 */
35
36 /**
37 * Manages DDNS entries.
38 *
39 * @package modules
40 */
41 class ddns extends baseModule {
42
43 /**
44 * {@inheritDoc}
45 */
46 public function can_manage(): bool {
47 return $this->get_scope() === 'dhcp';
48 }
49
50 /**
51 * {@inheritDoc}
52 */
53 public function get_metaData(): array {
54 $return = [];
55 // alias name
56 $return["alias"] = _("DDNS");
57 // this is a base module
58 $return["is_base"] = false;
59 // icon
60 $return['icon'] = 'world-color.svg';
61 // RDN attribute
62 $return["RDN"] = ["cn" => "high"];
63 // LDAP filter
64 $return["ldap_filter"] = [];
65 // module dependencies
66 $return['dependencies'] = ['depends' => ['dhcp_settings'], 'conflicts' => []];
67 // managed object classes
68 $return['objectClasses'] = [];
69 // managed attributes
70 $return['attributes'] = ['dhcpOption', 'dhcpStatements'];
71 // help Entries
72 $return['help'] = [
73 'active' => [
74 "Headline" => _("Activate DynDNS"),
75 "Text" => _("Should DDNS (Dynamic DNS) be activated?")
76 ],
77 'fixed_ips' => [
78 "Headline" => _("Fix IP addresses"),
79 "Text" => _("Should fix IP addresses be added to the DNS server?")
80 ],
81 'client_insert' => [
82 "Headline" => _("Disable client updates"),
83 "Text" => _("Disables the client to update DNS entries.")
84 ],
85 'keypath' => [
86 "Headline" => _("Path to key for DNS updates"),
87 "Text" => _("The key enables the DHCP server to perform DNS updates." .
88 " " . "The key is generated with \"genDDNSkey\".")
89 ],
90 'dns' => [
91 "Headline" => _("IP address of the DNS server"),
92 "Text" => _("Please enter the IP address of your DNS server.")
93 ],
94 'zone' => [
95 "Headline" => _("Zone name"),
96 "Text" => _("Zone name for the DNS server (e.g. company.local).")
97 ],
98 'zone_reverse' => [
99 "Headline" => _("Reverse zone name"),
100 "Text" => ("Name of the reverse zone of the DNS server (e.g. 0.168.192.in-addr.arpa).")
101 ],
102 ];
103 // available PDF fields
104 $return['PDF_fields'] = [
105 'DNSserver' => _('IP address of the DNS server'),
106 'zone' => _('Zone name'),
107 'reverseZone' => _('Reverse zone name'),
108 ];
109 // upload fields
110 if (isLoggedIn() && $this->check_if_ddns_is_enable()) {
111 $return['upload_columns'] = [
112 [
113 'name' => 'ddns_DNSserver',
114 'description' => _('IP address of the DNS server'),
115 'help' => 'dns',
116 'example' => '123.123.123.123',
117 'required' => true
118 ],
119 [
120 'name' => 'ddns_zone',
121 'description' => _('Zone name'),
122 'help' => 'zone',
123 'example' => 'company.local',
124 'required' => true
125 ],
126 [
127 'name' => 'ddns_reverseZone',
128 'description' => _('Reverse zone name'),
129 'help' => 'zone_reverse',
130 'example' => '0.168.192.in-addr.arpa',
131 'required' => true
132 ],
133 ];
134 }
135 return $return;
136 }
137
138 /**
139 * {@inheritDoc}
140 */
141 protected function load_Messages(): void {
142 $this->messages['key_path'][0] = ['ERROR', 'Please enter the path to the key.', ''];
143 $this->messages['key_path'][1] = ['ERROR', 'The key path contains invalid characters.', ''];
144 $this->messages['ip'][0] = ['ERROR', 'The IP address of the DNS server is invalid.'];
145 $this->messages['ip'][1] = ['ERROR', _('Account %s:') . ' ddns_DNSserver', 'The IP address of the DNS server is invalid.'];
146 $this->messages['zone'][0] = ['ERROR', 'Please enter a zone name.'];
147 $this->messages['zone_reverse'][0] = ['ERROR', 'Please enter the reverse zone.'];
148 }
149
150 /**
151 * {@inheritDoc}
152 */
153 public function module_complete(): bool {
154 $this->attributes = &$this->getAccountContainer()->getAccountModule('dhcp_settings')->attributes;
155 $this->orig = &$this->getAccountContainer()->getAccountModule('dhcp_settings')->orig;
156 if ($this->isRootNode()) {
157 // check if DHCP main settings and valid DHCP entry
158 if (!in_array_ignore_case('dhcpService', $this->attributes['objectClass']) && !in_array_ignore_case('dhcpServer', $this->attributes['objectClass'])) {
159 return false;
160 }
161 }
162 else {
163 if (!$this->check_if_ddns_is_enable()) {
164 return true;
165 }
166 // Account settings
167 $ip = $this->getDNSServer();
168 if (!empty($ip) && !check_ip($ip)) {
169 return false;
170 }
171
172 $zones = $this->getZoneNames();
173 if (count($zones) < 2) {
174 return false;
175 }
176 }
177
178 return true;
179 }
180
181 /**
182 * Returns if the current DN is the root entry.
183 *
184 * @return bool is root
185 */
186 private function isRootNode(): bool {
187 $rootSuffix = $this->getAccountContainer()->get_type()->getSuffix();
188 return $this->getAccountContainer()->dn_orig == $rootSuffix;
189 }
190
191 /**
192 * {@inheritDoc}
193 */
194 public function save_attributes(): array {
195 // done by dhcp_server object
196 return [];
197 }
198
199 /**
200 * This function check if ddns is enabled.
201 *
202 * @return bool is enabled
203 */
204 private function check_if_ddns_is_enable(): bool {
205 if ($this->getAccountContainer() == null) {
206 return false;
207 }
208 $type = $this->getAccountContainer()->get_type();
209 if ($type == null) {
210 return false;
211 }
212 $suffix = $type->getSuffix();
213
214 $results = searchLDAP($suffix, "dhcpStatements=ddns-update-style interim", ['dn']);
215 return (!empty($results));
216 }
217
218 /**
219 * {@inheritDoc}
220 */
221 public function process_attributes(): array {
222 // Main Settings and Account have to different processes.
223 if ($this->isRootNode()) {
224 // main settings:
225 $errors = $this->process_attributes_mainSettings();
226 }
227 else {
228 // account
229 if (!$this->check_if_ddns_is_enable()) {
230 return [];
231 }
232 $errors = $this->process_attributes_account();
233 }
234 return $errors;
235 }
236
237 /**
238 * Process for mainsettings
239 *
240 * @return array<string|string[]>[] messages
241 */
242 public function process_attributes_mainSettings(): array {
243 if (!in_array_ignore_case('dhcpService', $this->attributes['objectClass']) && !in_array_ignore_case('dhcpServer', $this->attributes['objectClass'])) {
244 return [];
245 }
246 $errors = [];
247
248 // Is DDNS active?
249 $active = $_POST['active'];
250
251 // Insert fixed IPs into DNS?
252 $insert_fixed = $_POST['insert_fixed'];
253
254 // Client can contribute which is registered into DNS
255 $client_insert = $_POST['client_insert'];
256
257 // The path to the key:
258 $key_path = trim($_POST['key_path']);
259
260 $this->setDynDNSActivated(($active == 'on'));
261
262 $this->setFixIPs(($insert_fixed == 'on'));
263
264 $this->setIgnoreClientUpdates(($client_insert == 'on'));
265
266 $this->setUpdateKey($key_path);
267 if (!empty($key_path) && (str_replace("\"", "", $_POST['key_path']) != $key_path)) {
268 $errors[] = $this->messages['key_path'][1];
269 }
270
271 return $errors;
272 }
273
274 /**
275 * Process for account
276 *
277 * @return array<string|string[]>[] messages
278 */
279 public function process_attributes_account(): array {
280 $errors = [];
281
282 $ip = trim($_POST['ip']);
283 $zone = trim($_POST['zone']);
284 $zone_reverse = trim($_POST['zone_reverse']);
285
286 // ip correct???
287 if (!empty($ip) && !check_ip($ip)) {
288 $errors[] = $this->messages['ip'][0];
289 }
290
291 for ($i = 0; $i < count($this->attributes['dhcpStatements']); $i++) {
292 if (str_starts_with($this->attributes['dhcpStatements'][$i], 'zone ')) {
293 unset($this->attributes['dhcpStatements'][$i]);
294 $this->attributes['dhcpStatements'] = array_values($this->attributes['dhcpStatements']);
295 $i--;
296 }
297 }
298
299 // Zone inserted?
300 if (!empty($zone)) {
301 if (!empty($ip)) {
302 $this->attributes['dhcpStatements'][] = "zone {$zone}. { primary {$ip}; key DHCP_UPDATER; }";
303 }
304 else {
305 $this->attributes['dhcpStatements'][] = "zone {$zone}. { key DHCP_UPDATER; }";
306 }
307 }
308 elseif (!empty($ip)) {
309 $errors[] = $this->messages['zone'][0];
310 }
311
312 // Zone reverse inserted?
313 if (!empty($zone_reverse)) {
314 if (!empty($ip)) {
315 $this->attributes['dhcpStatements'][] = "zone {$zone_reverse}. { primary {$ip}; key DHCP_UPDATER; }";
316 }
317 else {
318 $this->attributes['dhcpStatements'][] = "zone {$zone_reverse}. { key DHCP_UPDATER; }";
319 }
320 }
321 elseif (!empty($ip)) {
322 $errors[] = $this->messages['zone_reverse'][0];
323 }
324 return $errors;
325 }
326
327 /**
328 * {@inheritDoc}
329 */
330 public function display_html_attributes(): htmlElement|array {
331 $this->attributes = &$this->getAccountContainer()->getAccountModule('dhcp_settings')->attributes;
332 $this->orig = &$this->getAccountContainer()->getAccountModule('dhcp_settings')->orig;
333 $return = new htmlResponsiveRow();
334 // check if DHCP main settings and valid DHCP entry
335 if ($this->isRootNode()) {
336 if (!in_array_ignore_case('dhcpService', $this->attributes['objectClass']) && !in_array_ignore_case('dhcpServer', $this->attributes['objectClass'])) {
337 $return->add(new htmlStatusMessage('ERROR', _('Please set your LDAP suffix to an LDAP entry with object class "dhcpService" or "dhcpServer".')));
338 return $return;
339 }
340 // DHCP main settings
341 $return->add(new htmlResponsiveInputCheckbox('active', $this->isDynDNSActivated(), _('Activate DynDNS'), 'active'));
342 $return->add(new htmlResponsiveInputCheckbox('insert_fixed', $this->addFixIPs(), _('Add fix IP addresses to DNS'), 'fixed_ips'));
343 $return->add(new htmlResponsiveInputCheckbox('client_insert', $this->isIgnoreClientUpdates(), _('Disable client updates'), 'client_insert'));
344 $keyInput = new htmlResponsiveInputField(_('Path to key for DNS updates'), 'key_path', $this->getUpdateKey(), 'keypath');
345 $return->add($keyInput);
346 }
347 elseif (!$this->check_if_ddns_is_enable()) {
348 // Account edit
349 $return->add(new htmlOutputText(_("DDNS is not activated. You can activate it in the DHCP settings (DDNS).")));
350 }
351 else {
352 // DNS server
353 $serverInput = new htmlResponsiveInputField(_('IP address of the DNS server'), 'ip', $this->getDNSServer(), 'dns');
354 $return->add($serverInput);
355
356 $zones = $this->getZoneNames();
357 $zone = '';
358 $revzone = '';
359 if (isset($zones[0])) {
360 $zone = $zones[0];
361 }
362 if (isset($zones[1])) {
363 $revzone = $zones[1];
364 }
365 // zone name
366 $zoneInput = new htmlResponsiveInputField(_('Zone name'), 'zone', $zone, 'zone');
367 $return->add($zoneInput);
368 // reverse zone name
369 $revZoneInput = new htmlResponsiveInputField(_('Reverse zone name'), 'zone_reverse', $revzone, 'zone_reverse');
370 $return->add($revZoneInput);
371 }
372
373 return $return;
374 }
375
376 /**
377 * {@inheritDoc}
378 */
379 public function get_pdfEntries(array $pdfKeys, string $typeId): array {
380 // attributes are taken from DHCP server object
381 $this->attributes = &$this->getAccountContainer()->getAccountModule('dhcp_settings')->attributes;
382 $this->orig = &$this->getAccountContainer()->getAccountModule('dhcp_settings')->orig;
383 $zones = $this->getZoneNames();
384 $zone = '';
385 $revzone = '';
386 if (isset($zones[0])) {
387 $zone = $zones[0];
388 }
389 if (isset($zones[1])) {
390 $revzone = $zones[1];
391 }
392 $result = [];
393 $this->addPDFKeyValue($result, 'DNSserver', _('IP address of the DNS server'), $this->getDNSServer());
394 $this->addPDFKeyValue($result, 'zone', _('Zone name'), $zone);
395 $this->addPDFKeyValue($result, 'reverseZone', _('Reverse zone name'), $revzone);
396 return $result;
397 }
398
399 /**
400 * Returns the IP of the DNS server.
401 *
402 * @return string|null IP address
403 */
404 private function getDNSServer() {
405 if (isset($this->attributes['dhcpStatements'][0])) {
406 for ($i = 0; $i < count($this->attributes['dhcpStatements']); $i++) {
407 if (str_starts_with($this->attributes['dhcpStatements'][$i], 'zone ')) {
408 if (!str_contains($this->attributes['dhcpStatements'][$i], ' primary ')) {
409 continue;
410 }
411 $parts = explode(". { primary ", $this->attributes['dhcpStatements'][$i]);
412 $temp = array_pop($parts);
413 $temp = explode(";", $temp);
414 return array_shift($temp);
415 }
416 }
417 }
418 return null;
419 }
420
421 /**
422 * Returns the zone names.
423 *
424 * @return string[] zone names array(zone, reverse zone)
425 */
426 private function getZoneNames(): array {
427 $return = [];
428 $zone = '';
429 $revZone = '';
430 if (is_array($this->attributes['dhcpStatements'])) {
431 for ($i = 0; $i < count($this->attributes['dhcpStatements']); $i++) {
432 if (str_starts_with($this->attributes['dhcpStatements'][$i], 'zone ')) {
433 $parts = explode(" ", substr($this->attributes['dhcpStatements'][$i], 5));
434 $value = substr(array_shift($parts), 0, -1);
435 if (!str_contains($value, 'in-addr.arpa')) {
436 $zone = $value;
437 }
438 else {
439 $revZone = $value;
440 }
441 }
442 }
443 }
444 $return[0] = $zone;
445 $return[1] = $revZone;
446 return $return;
447 }
448
449 /**
450 * Returns if DDNS is activated.
451 *
452 * @return bool activated
453 */
454 private function isDynDNSActivated(): bool {
455 $return = false;
456 if (is_array($this->attributes['dhcpStatements'])) {
457 for ($i = 0; $i < count($this->attributes['dhcpStatements']); $i++) {
458 if ($this->attributes['dhcpStatements'][$i] == 'ddns-update-style interim') {
459 $return = true;
460 break;
461 }
462 }
463 }
464 return $return;
465 }
466
467 /**
468 * Sets if DDNS is activated.
469 *
470 * @param bool $activated activated
471 */
472 private function setDynDNSActivated($activated): void {
473 if (is_array($this->attributes['dhcpStatements'])) {
474 for ($i = 0; $i < count($this->attributes['dhcpStatements']); $i++) {
475 if (str_starts_with($this->attributes['dhcpStatements'][$i], 'ddns-update-style ')) {
476 unset($this->attributes['dhcpStatements'][$i]);
477 $this->attributes['dhcpStatements'] = array_values($this->attributes['dhcpStatements']);
478 }
479 }
480 }
481 $this->attributes['dhcpStatements'][] = $activated ? 'ddns-update-style interim' : 'ddns-update-style none';
482 }
483
484 /**
485 * Returns if fixed IPs are added to DDNS.
486 *
487 * @return bool add fixed IPs
488 */
489 private function addFixIPs(): bool {
490 $return = false;
491 if (is_array($this->attributes['dhcpStatements'])) {
492 for ($i = 0; $i < count($this->attributes['dhcpStatements']); $i++) {
493 if ($this->attributes['dhcpStatements'][$i] == 'update-static-leases true') {
494 $return = true;
495 break;
496 }
497 }
498 }
499 return $return;
500 }
501
502 /**
503 * Sets if client updates are ignored.
504 *
505 * @param bool $add add fixed IPs
506 */
507 private function setFixIPs($add): void {
508 if (is_array($this->attributes['dhcpStatements'])) {
509 for ($i = 0; $i < count($this->attributes['dhcpStatements']); $i++) {
510 if (str_starts_with($this->attributes['dhcpStatements'][$i], 'update-static-leases ')) {
511 unset($this->attributes['dhcpStatements'][$i]);
512 $this->attributes['dhcpStatements'] = array_values($this->attributes['dhcpStatements']);
513 }
514 }
515 }
516 if ($add) {
517 $this->attributes['dhcpStatements'][] = 'update-static-leases true';
518 }
519 }
520
521 /**
522 * Returns if client updates are ignored.
523 *
524 * @return bool ignore client updates
525 */
526 private function isIgnoreClientUpdates(): bool {
527 $return = false;
528 if (is_array($this->attributes['dhcpStatements'])) {
529 for ($i = 0; $i < count($this->attributes['dhcpStatements']); $i++) {
530 if (preg_replace('/[ ]+/', ' ', $this->attributes['dhcpStatements'][$i]) == 'ignore client-updates') {
531 $return = true;
532 break;
533 }
534 }
535 }
536 return $return;
537 }
538
539 /**
540 * Sets if client updates are ignored.
541 *
542 * @param boolean $ignore ignore client updates
543 */
544 private function setIgnoreClientUpdates($ignore): void {
545 if (is_array($this->attributes['dhcpStatements'])) {
546 for ($i = 0; $i < count($this->attributes['dhcpStatements']); $i++) {
547 if (preg_replace('/[ ]+/', ' ', $this->attributes['dhcpStatements'][$i]) == 'ignore client-updates') {
548 if ($ignore) {
549 return; // option already set, no change
550 }
551 unset($this->attributes['dhcpStatements'][$i]);
552 $this->attributes['dhcpStatements'] = array_values($this->attributes['dhcpStatements']);
553 }
554 }
555 }
556 if ($ignore) {
557 $this->attributes['dhcpStatements'][] = 'ignore client-updates';
558 }
559 }
560
561 /**
562 * Returns the key for DNS updates.
563 *
564 * @return String key
565 */
566 private function getUpdateKey() {
567 $return = null;
568 if (is_array($this->attributes['dhcpStatements'])) {
569 for ($i = 0; $i < count($this->attributes['dhcpStatements']); $i++) {
570 if (str_starts_with($this->attributes['dhcpStatements'][$i], 'include ')) {
571 $return = substr($this->attributes['dhcpStatements'][$i], 9, strlen($this->attributes['dhcpStatements'][$i]) - 10);
572 break;
573 }
574 }
575 }
576 return $return;
577 }
578
579 /**
580 * Sets the key for DNS updates.
581 *
582 * @param string|null $key key
583 */
584 private function setUpdateKey($key): void {
585 if (!is_array($this->attributes['dhcpStatements'])) {
586 $this->attributes['dhcpStatements'] = [];
587 }
588 for ($i = 0; $i < count($this->attributes['dhcpStatements']); $i++) {
589 if (str_starts_with($this->attributes['dhcpStatements'][$i], 'include ')) {
590 unset($this->attributes['dhcpStatements'][$i]);
591 $this->attributes['dhcpStatements'] = array_values($this->attributes['dhcpStatements']);
592 }
593 }
594 if (($key !== null) && ($key !== '')) {
595 $this->attributes['dhcpStatements'][] = 'include "' . $key . '"';
596 }
597 }
598
599 /**
600 * {@inheritDoc}
601 */
602 public function load_attributes(array $attributes): void {
603 // load nothing, attributes are saved in "dhcp_settings" module
604 }
605
606 /**
607 * {@inheritDoc}
608 */
609 public function build_uploadAccounts(array $rawAccounts, array $ids, array &$partialAccounts, array $selectedModules, ConfiguredType &$type): array {
610 $messages = [];
611 if (!$this->check_if_ddns_is_enable()) {
612 return $messages;
613 }
614 for ($i = 0; $i < count($rawAccounts); $i++) {
615 // principal name
616 if (!check_ip($rawAccounts[$i][$ids['ddns_DNSserver']])) {
617 $error = $this->messages['ip'][1];
618 $error[] = [$i];
619 $messages[] = $error;
620 }
621 else {
622 $partialAccounts[$i]['dhcpStatements'][] = "zone {$rawAccounts[$i][$ids['ddns_zone']]}. { primary {$rawAccounts[$i][$ids['ddns_DNSserver']]}; key DHCP_UPDATER; }";
623 $partialAccounts[$i]['dhcpStatements'][] = "zone {$rawAccounts[$i][$ids['ddns_reverseZone']]}. { primary {$rawAccounts[$i][$ids['ddns_DNSserver']]}; key DHCP_UPDATER; }";
624 }
625 }
626 return $messages;
627 }
628
629 }