Debian Codegraph @

lib/modules/dhcp_settings.inc

raw
1 <?php
2 /*
3
4 This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
5 Copyright (C) 2008 Thomas Manninger
6 2008 - 2026 Roland Gruber
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22 */
23
24 /**
25 * Manages DHCP entries.
26 *
27 * @package modules
28 *
29 * @author Thomas Manninger
30 * @author Roland Gruber
31 */
32
33 use LAM\TYPES\ConfiguredType;
34
35 if (!function_exists('check_ip')) {
36
37 /**
38 * Checks if the given IP is valid.
39 *
40 * @param string $ip IP address
41 * @param boolean $subnet IP must be a subnet
42 * @return bool IP matches subnet
43 */
44 function check_ip($ip, $subnet = false): bool {
45 $part = explode(".", $ip);
46 // IP must contain 4 segments
47 if (count($part) !== 4) {
48 return false;
49 }
50 else {
51 // check each segment
52 for ($i = 0; $i < count($part); $i++) {
53 // only numbers are allowed
54 if (!is_numeric($part[$i])
55 || ($part[$i] > 255)
56 // non-subnet must be > 0 on last digit
57 || (!$subnet && ($i === 3) && ($part[$i] < 1))
58 // subnet must be >= 0 on last digit
59 || ($subnet && ($i === 3) && ($part[$i] < 0))) {
60 return false;
61 }
62 }
63 }
64 return true;
65 }
66 }
67
68 /**
69 * Manages DHCP entries.
70 *
71 * @package modules
72 */
73 class dhcp_settings extends baseModule {
74
75 private const UNKNOWN_CLIENTS = 'unknown-clients';
76 private const DEFAULT_LEASE_TIME = 'default-lease-time';
77 private const MAX_LEASE_TIME = 'max-lease-time';
78 private const AUTHORITATIVE = 'authoritative';
79 private const DOMAIN_SEARCH = 'domain-search';
80 private const DOMAIN_NAME = 'domain-name';
81 private const DOMAIN_NAME_SERVERS = 'domain-name-servers';
82 private const ROUTERS = 'routers';
83 private const NETBIOS_NAME_SERVERS = 'netbios-name-servers';
84 private const NETBIOS_NODE_TYPE = 'netbios-node-type';
85 private const SUBNET_MASK = 'subnet-mask';
86
87 /** @var array<string|int, string> $all_netbios_node_types all netbios node types */
88 private array $all_netbios_node_types;
89 /** @var array<string, string> $allowDenyOptions unknown-client options */
90 private array $allowDenyOptions;
91 /** LDAP attributes */
92 public ?array $attributes = null;
93
94 /**
95 * Creates a new dhcp_settings object.
96 *
97 * @param string $scope account type
98 */
99 public function __construct($scope) {
100 // list of node types
101 $this->all_netbios_node_types = [
102 "1" => _("B-Node (0x01)"),
103 "2" => _("P-Node (0x02)"),
104 "4" => _("M-Node (0x04)"),
105 "8" => _("H-Node (0x08)")
106 ];
107 $this->allowDenyOptions = [
108 '-' => '',
109 'allow' => _("Allow"),
110 'deny' => _("Deny"),
111 ];
112 // call parent constructor
113 parent::__construct($scope);
114 }
115
116 /**
117 * {@inheritDoc}
118 */
119 public function can_manage(): bool {
120 return $this->get_scope() === 'dhcp';
121 }
122
123 /**
124 * {@inheritDoc}
125 */
126 public function get_metaData(): array {
127 $return = [];
128 // alias name
129 $return["alias"] = _("DHCP settings");
130 // this is a base module
131 $return["is_base"] = true;
132 // icon
133 $return['icon'] = 'world-color.svg';
134 // RDN attribute
135 $return["RDN"] = ["cn" => "high"];
136 // LDAP filter
137 $return["ldap_filter"] = [];
138 // module dependencies
139 $return['dependencies'] = ['depends' => [], 'conflicts' => []];
140 // managed object classes
141 $return['objectClasses'] = ['top', 'dhcpOptions', 'dhcpSubnet'];
142 // managed attributes
143 $return['attributes'] = ['cn', 'dhcpOption', 'dhcpComments', 'dhcpNetMask', 'dhcpStatements'];
144 // help Entries
145 $return['help'] = [
146 'domainname' => [
147 "Headline" => _("Domain name"),
148 "Text" => _("The domain name of the subnet.")
149 ],
150 'subnet' => [
151 "Headline" => _("Subnet"), 'attr' => 'cn',
152 "Text" => _("The name of the subnet. Example: 192.168.10.0")
153 ],
154 'leasetime' => [
155 "Headline" => _("Lease time"),
156 "Text" => _("The lease time specifies after how many seconds the client should request a new IP address.")
157 ],
158 'max_leasetime' => [
159 "Headline" => _("Maximum lease time"),
160 "Text" => _("The maximum lease time specifies after how many seconds the client must request a new IP address.")
161 ],
162 'dns' => [
163 "Headline" => _("DNS (Domain Name System)"),
164 "Text" => _("The IP addresses of the DNS servers. Multiple addresses are separated by \",\". Example: 192.168.0.10, 192.168.0.11")
165 ],
166 'gateway' => [
167 "Headline" => _("Default gateway"),
168 "Text" => _("Packets are sent to the default gateway if the receiver does not reside in the same network. The default gateway routes them to the target network.")
169 ],
170 'netbios' => [
171 "Headline" => _("Netbios name servers"),
172 "Text" => _("The IP addresses of the Netbios name servers (e.g. \"123.123.123.123, 123.123.123.124\").")
173 ],
174 'netbios_type' => [
175 "Headline" => _("Netbios node type"),
176 "Text" => _("<b>B-Node (0x01): Broadcast.</b><br/>The client tries to find other workstations via broadcasting
177 (works only inside the same collision domain, viz. the same subnet).<br/><br/>
178
179 <b>P-Node (0x02): Point-To-Point</b><br />
180 The client contacts a Netbios name server (NBNS) from Microsoft Windows Name Service (WINS) for name resolution.<br/><br/>
181
182 <b>M-Node (0x04): Mixed</b><br />
183 The node tries broadcasting first. If that fails then it tries WINS.<br/><br/>
184
185 <b>H-Node (0x08): Hybrid</b><br />
186 The node tries WINS first. If that fails it tries broadcasting.<br/><br/>
187
188 By default, the nodes are configured as H-Nodes which fits for small networks. In large networks Point-to-Point (0x02) should be used.")
189 ],
190 'subnetmask' => [
191 "Headline" => _("Subnet mask"),
192 "Text" => _("The subnet mask of the network.")
193 ],
194 'netmask' => [
195 "Headline" => _("Net mask"), 'attr' => 'dhcpNetMask',
196 "Text" => _("The net mask is derived from the subnet mask. LAM will calculate it automatically.")
197 ],
198 'description' => [
199 "Headline" => _("Description"), 'attr' => 'dhcpComments',
200 "Text" => _("Here you can enter a description for this DHCP entry.")
201 ],
202 'unknownClients' => [
203 "Headline" => _("Unknown clients"), 'attr' => 'dhcpStatements',
204 "Text" => _("Specifies if unknown clients are allowed.")
205 ],
206 'INFO_domain-search' => [
207 'Headline' => _('Search domains'), 'attr' => 'dhcpOptions',
208 'Text' => _('This is a list of domain names to be used by the client to locate not-fully-qualified domain names.')
209 ],
210 'domain-search' => [
211 'Headline' => _('Search domains'), 'attr' => 'dhcpOptions',
212 'Text' => _('This is a list of domain names to be used by the client to locate not-fully-qualified domain names.')
213 . ' ' . _("Multiple values are separated by semicolon.")
214 ],
215 'authoritative' => [
216 'Headline' => _('Authoritative'), 'attr' => 'dhcpStatements',
217 'Text' => _('Authoritative servers reject requests for invalid IP addresses.')
218 ],
219 'extraStatements' => [
220 'Headline' => _('Extra statements'), 'attr' => 'dhcpStatements',
221 'Text' => _('Please enter any additional DHCP statements.')
222 ],
223 'extraOptions' => [
224 'Headline' => _('Extra statements'), 'attr' => 'dhcpOptions',
225 'Text' => _('Please enter any additional DHCP options.')
226 ],
227 ];
228 // available PDF fields
229 $return['PDF_fields'] = [
230 'subnet' => _('Subnet'),
231 'domainName' => _('Domain name'),
232 'leaseTime' => _('Lease time'),
233 'maxLeaseTime' => _('Maximum lease time'),
234 'DNSserver' => _('DNS'),
235 'gateway' => _('Default gateway'),
236 'netbiosServer' => _('Netbios name servers'),
237 'netbiosType' => _('Netbios node type'),
238 'subnetMask' => _('Subnet mask'),
239 'netMask' => _('Net mask'),
240 'description' => _('Description'),
241 'unknownClients' => _('Unknown clients'),
242 self::DOMAIN_SEARCH => _('Search domains'),
243 self::AUTHORITATIVE => _('Authoritative')
244 ];
245 // profile elements
246 $profileContainer = new htmlResponsiveRow();
247 $profileContainer->add(new htmlResponsiveInputField(_('Subnet'), 'cn', null, 'subnet'));
248 $profileContainer->add(new htmlResponsiveInputField(_('Domain name'), 'domainname', null, 'domainname'));
249 $profileContainer->add(new htmlResponsiveInputField(_('Lease time'), 'lease_time', null, 'leasetime'));
250 $profileContainer->add(new htmlResponsiveInputField(_('Maximum lease time'), 'max_lease_time', null, 'max_leasetime'));
251 $profileContainer->add(new htmlResponsiveInputField(_('DNS'), 'dns', null, 'dns'));
252 $profileContainer->add(new htmlResponsiveInputField(_('Search domains'), self::DOMAIN_SEARCH, null, 'domain-search'));
253 $profileContainer->add(new htmlResponsiveInputField(_('Default gateway'), self::ROUTERS, null, 'gateway'));
254 $profileContainer->add(new htmlResponsiveInputField(_('Netbios name servers'), 'netbios', null, 'netbios'));
255 $nodeList = array_flip($this->all_netbios_node_types);
256 $profileNodeSelect = new htmlResponsiveSelect('netbios_node_type', $nodeList, [], _('Netbios node type'), 'netbios_type');
257 $profileNodeSelect->setHasDescriptiveElements(true);
258 $profileContainer->add($profileNodeSelect);
259 $profileContainer->add(new htmlResponsiveInputField(_('Subnet mask'), 'subnet', null, 'subnetmask'));
260 $profileContainer->add(new htmlResponsiveInputCheckbox(self::AUTHORITATIVE, false, _('Authoritative'), 'authoritative'));
261 $return['profile_options'] = $profileContainer;
262 // upload fields
263 $uploadAllowDenyOption = $this->allowDenyOptions;
264 unset($uploadAllowDenyOption['-']);
265 $uploadAllowDenyOption = implode(', ', $uploadAllowDenyOption);
266 $return['upload_columns'] = [
267 [
268 'name' => 'dhcp_settings_subnet',
269 'description' => _('Subnet'),
270 'help' => 'subnet',
271 'example' => '192.168.10.0',
272 'required' => true,
273 'unique' => true
274 ],
275 [
276 'name' => 'dhcp_settings_domainName',
277 'description' => _('Domain name'),
278 'help' => 'domainname',
279 'example' => 'mydomain.com',
280 ],
281 [
282 'name' => 'dhcp_settings_leaseTime',
283 'description' => _('Lease time'),
284 'help' => 'leasetime',
285 'example' => '86400',
286 ],
287 [
288 'name' => 'dhcp_settings_maxLeaseTime',
289 'description' => _('Maximum lease time'),
290 'help' => 'max_leasetime',
291 'example' => '172800',
292 ],
293 [
294 'name' => 'dhcp_settings_DNSserver',
295 'description' => _('DNS'),
296 'help' => 'dns',
297 'example' => '192.168.10.250',
298 ],
299 [
300 'name' => 'dhcp_settings_domain-search',
301 'description' => _('Search domains'),
302 'help' => 'domain-search',
303 'example' => 'example.com',
304 ],
305 [
306 'name' => 'dhcp_settings_gateway',
307 'description' => _('Default gateway'),
308 'help' => 'gateway',
309 'example' => '192.168.10.251',
310 ],
311 [
312 'name' => 'dhcp_settings_netbiosServer',
313 'description' => _('Netbios name servers'),
314 'help' => 'netbios',
315 'example' => '192.168.10.252, 192.168.10.253',
316 ],
317 [
318 'name' => 'dhcp_settings_netbiosType',
319 'description' => _('Netbios node type'),
320 'help' => 'netbios_type',
321 'example' => 'M',
322 'default' => 'H',
323 'values' => 'B, H, M, P'
324 ],
325 [
326 'name' => 'dhcp_settings_unknownClients',
327 'description' => _('Unknown clients'),
328 'help' => 'unknownClients',
329 'values' => $uploadAllowDenyOption
330 ],
331 [
332 'name' => 'dhcp_settings_subnetMask',
333 'description' => _('Subnet mask'),
334 'help' => 'subnetmask',
335 'required' => true,
336 'example' => '255.255.255.0',
337 ],
338 [
339 'name' => 'dhcp_settings_authoritative',
340 'description' => _('Authoritative'),
341 'help' => 'authoritative',
342 'values' => 'true, false',
343 'default' => 'false'
344 ],
345 [
346 'name' => 'dhcp_settings_description',
347 'description' => _('Description'),
348 'help' => 'description',
349 ],
350 ];
351 return $return;
352 }
353
354 /**
355 * {@inheritDoc}
356 */
357 protected function load_Messages(): void {
358 $this->messages['cn'][0] = ['ERROR', _('No subnet entered.')];
359 $this->messages['cn'][1] = ['ERROR', _('The subnet is already in use.')];
360 $this->messages['cn'][2] = ['ERROR', _('The subnet is invalid.')];
361 $this->messages['cn'][3] = ['ERROR', _('Account %s:') . ' dhcp_settings_subnet', _('The subnet is invalid.')];
362 $this->messages['dns'][0] = ['ERROR', _('You entered one or more invalid DNS servers.')];
363 $this->messages['dns'][1] = ['ERROR', _('Account %s:') . ' dhcp_settings_DNSserver', _('You entered one or more invalid DNS servers.')];
364 $this->messages['lease_time'][0] = ['ERROR', _('The lease time is invalid.')];
365 $this->messages['lease_time'][1] = ['ERROR', _('Account %s:') . ' dhcp_settings_leaseTime', _('The lease time is invalid.')];
366 $this->messages['routers'][0] = ['ERROR', _('The default gateway is invalid.')];
367 $this->messages['routers'][1] = ['ERROR', _('Account %s:') . ' dhcp_settings_gateway', _('The default gateway is invalid.')];
368 $this->messages['netbios'][0] = ['ERROR', _('The Netbios server is invalid.')];
369 $this->messages['netbios'][1] = ['ERROR', _('Account %s:') . ' dhcp_settings_netbiosServer', _('The Netbios server is invalid.')];
370 $this->messages['netbios_node_type'][1] = ['ERROR', _('Account %s:') . ' dhcp_settings_netbiosType', _('The entered Netbios node type does not exist.')];
371 $this->messages['unknownClients'][0] = ['ERROR', _('Account %s:') . ' dhcp_settings_unknownClients', _('Please enter a valid option.')];
372 $this->messages['max_lease_time'][0] = ['ERROR', _('The maximum lease time is invalid.')];
373 $this->messages['max_lease_time'][1] = ['ERROR', _('Account %s:') . ' dhcp_settings_maxLeaseTime', _('The maximum lease time is invalid.')];
374 $this->messages['subnet'][0] = ['ERROR', _('The subnet mask is invalid.')];
375 $this->messages['subnet'][1] = ['ERROR', _('Account %s:') . ' dhcp_settings_subnetMask', _('The subnet mask is invalid.')];
376 $this->messages['ranges_reload'][0] = ['INFO', _('The DHCP ranges were changed to fit for the new subnet.')];
377 $this->messages['ips_reload'][0] = ['INFO', _('The fixed IP addresses were changed to fit for the new subnet.')];
378 $this->messages['domainname'][2] = ['ERROR', _('The domain name includes invalid characters. Valid characters are A-Z, a-z, 0-9, ".", "_","-".')];
379 $this->messages['domainname'][5] = ['ERROR', _('Account %s:') . ' dhcp_settings_domainName', _('The domain name includes invalid characters. Valid characters are A-Z, a-z, 0-9, ".", "_","-".')];
380 $this->messages['INFO_domain-search'][0] = ['ERROR', _('Please enter valid domain names for search domains.')];
381 $this->messages['INFO_domain-search'][1] = ['ERROR', _('Account %s:') . ' dhcp_settings_domain-search', _('Please enter valid domain names for search domains.')];
382 $this->messages['authoritative'][0] = ['ERROR', _('Account %s:') . ' dhcp_settings_authoritative', _('This value can only be "true" or "false".')];
383 }
384
385 /**
386 * {@inheritDoc}
387 */
388 public function save_attributes(): array {
389 // remove dhcpSubnet object class if only the DHCP settings were changed
390 if ($this->isRootNode()
391 && !in_array_ignore_case("dhcpSubnet", $this->orig['objectClass'])
392 && in_array_ignore_case("dhcpSubnet", $this->attributes['objectClass'])) {
393 $this->attributes['objectClass'] = array_delete(["dhcpSubnet"], $this->attributes['objectClass']);
394 }
395 return parent::save_attributes();
396 }
397
398 /**
399 * {@inheritDoc}
400 */
401 public function process_attributes(): array {
402 // check if DHCP main settings and valid DHCP entry
403 if ($this->isRootNode()
404 && !in_array_ignore_case('dhcpService', $this->attributes['objectClass'])
405 && !in_array_ignore_case('dhcpServer', $this->attributes['objectClass'])) {
406 return [];
407 }
408 $errors = [];
409
410 // Check if cn is not empty
411 if (!$this->isRootNode()) {
412 if (!empty($_POST['cn'])) {
413 $_POST['cn'] = trim($_POST['cn']);
414 }
415 $this->attributes['cn'][0] = $_POST['cn'];
416 if (empty($_POST['cn'])) {
417 $errors[] = $this->messages['cn'][0];
418 }
419 // Check, if cn is not already use:
420 elseif ((count(searchLDAPByAttribute('cn', $_POST['cn'], 'dhcpOptions', ['cn'], ['dhcp'])) > 0) && $this->orig['cn']['0'] != $_POST['cn']) {
421 $errors[] = $this->messages['cn'][1];
422 }
423 elseif (!check_ip($_POST['cn'], true)) {
424 $errors[] = $this->messages['cn'][2];
425 }
426 else {
427 // if the cn was edit, reload the Ranges:
428 if ($this->getAccountContainer()->getAccountModule('range')->reload_ranges()) {
429 $errors[] = $this->messages['ranges_reload'][0];
430 }
431 // if the cn was edit, reload the ips:
432 if ($this->getAccountContainer()->getAccountModule('fixed_ip')->reload_ips()) {
433 $errors[] = $this->messages['ips_reload'][0];
434 }
435 }
436 $this->setUnknownClients($_POST['unknownClients']);
437 }
438
439 $this->setAuthoritative(isset($_POST['authoritative']) && ($_POST['authoritative'] === 'on'));
440
441 // Check domainname:
442 if (!empty($_POST['domainname'])) {
443 $_POST['domainname'] = trim($_POST['domainname']);
444 }
445 if (!empty($_POST['domainname']) && !preg_match("/^[A-Za-z0-9\\._-]*$/", $_POST['domainname'])) {
446 $errors[] = $this->messages['domainname'][2];
447 }
448 $this->setDHCPOption(self::DOMAIN_NAME, '"' . $_POST['domainname'] . '"');
449
450 // Check DNS
451 if (!empty($_POST['dns'])) {
452 $_POST['dns'] = trim($_POST['dns']);
453 $ex = explode(",", $_POST['dns']);
454 $is_first = true;
455 $invalid = false;
456 foreach ($ex as $string) {
457 if ($is_first) {
458 $is_first = false;
459 }
460
461 if (!check_ip($string)) {
462 $invalid = true;
463 }
464 }
465 if ($invalid) {
466 $errors[] = $this->messages['dns'][0];
467 }
468 }
469 $this->setDHCPOption(self::DOMAIN_NAME_SERVERS, $_POST['dns']);
470 // domain search
471 $this->processMultiValueInputTextField('INFO_domain-search', $errors, 'domainname');
472 foreach ($this->attributes['INFO_domain-search'] as $key => $domainName) {
473 $this->attributes['INFO_domain-search'][$key] = '"' . $domainName . '"';
474 }
475 $domainSearch = implode(', ', $this->attributes['INFO_domain-search']);
476 $this->setDHCPOption(self::DOMAIN_SEARCH, $domainSearch);
477 unset($this->attributes['INFO_domain-search']);
478
479 // Lease Time
480 if (!empty($_POST['lease_time'])) {
481 $_POST['lease_time'] = trim($_POST['lease_time']);
482 }
483 $this->setDefaultLeaseTime($_POST['lease_time']);
484 if (!is_numeric($_POST['lease_time']) && !empty($_POST['lease_time'])) {
485 $errors[] = $this->messages['lease_time'][0];
486 }
487
488 // Max lease Time
489 if (!empty($_POST['max_lease_time'])) {
490 $_POST['max_lease_time'] = trim($_POST['max_lease_time']);
491 }
492 $this->setMaxLeaseTime($_POST['max_lease_time']);
493 if (!is_numeric($_POST['max_lease_time']) && !empty($_POST['max_lease_time'])) {
494 $errors[] = $this->messages['max_lease_time'][0];
495 }
496
497 // Default Gateway
498 if (!empty($_POST['routers'])) {
499 $_POST['routers'] = trim($_POST['routers']);
500 if (!check_ip($_POST['routers'])) {
501 $errors[] = $this->messages['routers'][0];
502 }
503 }
504 $this->setDHCPOption(self::ROUTERS, $_POST['routers']);
505
506 // Netbios
507 if (!empty($_POST['netbios'])) {
508 $_POST['netbios'] = trim($_POST['netbios']);
509 }
510 $netbiosServers = explode(', ', $_POST['netbios']);
511 $netbiosServersOk = true;
512 for ($i = 0; $i < count($netbiosServers); $i++) {
513 if (!check_ip($netbiosServers[$i]) && !get_preg($netbiosServers[$i], 'DNSname')) {
514 $netbiosServersOk = false;
515 break;
516 }
517 }
518 if (!$netbiosServersOk && !empty($_POST['netbios'])) {
519 $errors[] = $this->messages['netbios'][0];
520 }
521 $this->setDHCPOption(self::NETBIOS_NAME_SERVERS, $_POST['netbios']);
522
523 $this->setDHCPOption(self::NETBIOS_NODE_TYPE, $_POST['netbios_node_type']);
524
525 if (!$this->isRootNode()) {
526 // Check subnet
527 $_POST['subnet'] = trim($_POST['subnet']);
528 if (!$this->checkSubnetMask($_POST['subnet'])) {
529 $errors[] = $this->messages['subnet'][0];
530 }
531 $this->setDHCPOption(self::SUBNET_MASK, $_POST['subnet']);
532
533 // calculate netmask from subnet:
534 if (!empty($_POST['subnet'])) {
535 $this->attributes['dhcpNetMask'][0] = $this->calculateNetMask($_POST['subnet']);
536 }
537 }
538
539 // description
540 $this->attributes['dhcpComments'][0] = $_POST['description'];
541
542 // extra statements
543 $extraStatements = isset($_POST['extraStatements']) ? explode("\r\n", $_POST['extraStatements']) : [];
544 $this->setExtraStatements($extraStatements);
545
546 // extra options
547 $extraOptions = isset($_POST['extraOptions']) ? explode("\r\n", $_POST['extraOptions']) : [];
548 $this->setExtraOptions($extraOptions);
549
550 return $errors;
551 }
552
553 /**
554 * Checks if the subnet mask is valid.
555 *
556 * @param String $mask subnet mask
557 * @return boolean correct or incorrect
558 */
559 private function checkSubnetMask($mask) {
560 // check basic format
561 if (!check_ip($mask, true)) {
562 return false;
563 }
564 // check if bit order is 11...00...
565 $parts = explode('.', $mask);
566 $bits = '';
567 for ($i = 0; $i < count($parts); $i++) {
568 $bits .= decbin((int) $parts[$i]);
569 }
570 return preg_match('/^1*0*$/', $bits);
571 }
572
573 /**
574 * Calculates the net mask from the subnet.
575 *
576 * @param String $subnet subnet
577 * @return integer netmask
578 */
579 private function calculateNetMask($subnet) {
580 $ex = explode(".", $subnet);
581 $num = 0;
582 foreach ($ex as $mask) {
583 $binary = decbin((int) $mask);
584 $num += substr_count($binary, '1');
585 }
586 return $num;
587 }
588
589 /**
590 * {@inheritDoc}
591 */
592 public function display_html_attributes(): htmlElement|array {
593 $return = new htmlResponsiveRow();
594 // check if DHCP main settings and valid DHCP entry
595 if ($this->isRootNode()
596 && !in_array_ignore_case('dhcpService', $this->attributes['objectClass'])
597 && !in_array_ignore_case('dhcpServer', $this->attributes['objectClass'])) {
598 $return->add(new htmlStatusMessage('ERROR', _('Please set your LDAP suffix to an LDAP entry with object class "dhcpService" or "dhcpServer".')));
599 return $return;
600 }
601 // Subnet name
602 if (!$this->isRootNode()) {
603 $cn = '';
604 if (isset($this->attributes['cn'][0])) {
605 $cn = $this->attributes['cn'][0];
606 }
607 $subnetInput = new htmlResponsiveInputField(_('Subnet'), 'cn', $cn, 'subnet');
608 $subnetInput->setRequired(true);
609 $return->add($subnetInput);
610 }
611 // description
612 $description = '';
613 if (isset($this->attributes['dhcpComments'][0])) {
614 $description = $this->attributes['dhcpComments'][0];
615 }
616 $return->add(new htmlResponsiveInputField(_('Description'), 'description', $description, 'description'));
617 // domain name
618 $return->add(new htmlResponsiveInputField(_('Domain name'), 'domainname', $this->getDHCPOption(self::DOMAIN_NAME), 'domainname'));
619 // lease Time
620 $leasetimeInput = new htmlResponsiveInputField(_('Lease time'), 'lease_time', $this->getDefaultLeaseTime(), 'leasetime');
621 $leasetimeInput->setMinimumAndMaximumNumber();
622 $return->add($leasetimeInput);
623 // max lease time
624 $max_leasetimeInput = new htmlResponsiveInputField(_('Maximum lease time'), 'max_lease_time', $this->getMaxLeaseTime(), 'max_leasetime');
625 $max_leasetimeInput->setMinimumAndMaximumNumber();
626 $return->add($max_leasetimeInput);
627 // DNS
628 $return->add(new htmlResponsiveInputField(_('DNS'), 'dns', $this->getDHCPOption(self::DOMAIN_NAME_SERVERS), 'dns'));
629 // domain search
630 $domainSearchEntries = $this->getDHCPOption(self::DOMAIN_SEARCH);
631 if ($domainSearchEntries === null) {
632 $domainSearchEntries = '';
633 }
634 $domainSearchEntries = explode(', ', $domainSearchEntries);
635 $this->attributes['INFO_domain-search'] = $domainSearchEntries;
636 $this->addMultiValueInputTextField($return, 'INFO_domain-search', _('Search domains'));
637 // gateway
638 $return->add(new htmlResponsiveInputField(_('Default gateway'), 'routers', $this->getDHCPOption(self::ROUTERS), 'gateway'));
639 // netbios name servers
640 $return->add(new htmlResponsiveInputField(_('Netbios name servers'), 'netbios', $this->getDHCPOption(self::NETBIOS_NAME_SERVERS), 'netbios'));
641 // netbios node type
642 $nodeType = $this->getDHCPOption(self::NETBIOS_NODE_TYPE);
643 if ($nodeType == '') {
644 $nodeType = 8;
645 }
646 $nodeOptions = [];
647 foreach ($this->all_netbios_node_types as $key => $value) {
648 $nodeOptions[$value] = $key;
649 }
650 $nodeSelect = new htmlResponsiveSelect('netbios_node_type', $nodeOptions, [$nodeType], _('Netbios node type'), 'netbios_type');
651 $nodeSelect->setHasDescriptiveElements(true);
652 $return->add($nodeSelect);
653
654 if (!$this->isRootNode()) {
655 // unknown clients
656 $unknownClients = $this->getUnknownClients();
657 if (empty($unknownClients)) {
658 $unknownClients = '-';
659 }
660 $unknownClientsOptions = array_flip($this->allowDenyOptions);
661 $unknownClientsSelect = new htmlResponsiveSelect('unknownClients', $unknownClientsOptions, [$unknownClients], _('Unknown clients'), 'unknownClients');
662 $unknownClientsSelect->setHasDescriptiveElements(true);
663 $return->add($unknownClientsSelect);
664 // subnetmask
665 $subnetMaskInput = new htmlResponsiveInputField(_('Subnet mask'), 'subnet', $this->getDHCPOption(self::SUBNET_MASK), 'subnetmask');
666 $subnetMaskInput->setRequired(true);
667 $return->add($subnetMaskInput);
668 }
669
670 if (!$this->isRootNode()) {
671 // netmask
672 $return->addLabel(new htmlOutputText(_('Net mask')));
673 $return->addField(new htmlOutputText($this->attributes['dhcpNetMask'][0]));
674 }
675 if (!$this->isRootNode()) {
676 $return->add(new htmlResponsiveInputCheckbox('authoritative', $this->isAuthoritative(), _('Authoritative'), 'authoritative'));
677 }
678 $return->add(new htmlResponsiveInputTextarea('extraStatements', implode("\r\n", $this->getExtraStatements()), 30, 4, _('Extra statements'), 'extraStatements'));
679 $return->add(new htmlResponsiveInputTextarea('extraOptions', implode("\r\n", $this->getExtraOptions()), 30, 4, _('Extra options'), 'extraOptions'));
680 return $return;
681 }
682
683 /**
684 * {@inheritDoc}
685 */
686 public function load_profile(array $profile): void {
687 // profile mappings in meta data
688 parent::load_profile($profile);
689 $this->attributes['cn'][0] = $profile['cn'][0];
690
691 $this->setDefaultLeaseTime($profile['lease_time'][0]);
692 $this->setMaxLeaseTime($profile['max_lease_time'][0]);
693 $this->setAuthoritative(isset($profile['authoritative'][0]) && ($profile['authoritative'][0] === 'true'));
694
695 $this->setDHCPOption(self::DOMAIN_NAME, $profile['domainname'][0]);
696 $this->setDHCPOption(self::DOMAIN_NAME_SERVERS, $profile['dns'][0]);
697 $this->setDHCPOption(self::ROUTERS, $profile[self::ROUTERS][0]);
698 $this->setDHCPOption(self::NETBIOS_NAME_SERVERS, $profile['netbios'][0]);
699 $this->setDHCPOption(self::NETBIOS_NODE_TYPE, $profile['netbios_node_type'][0]);
700 if (!empty($profile[self::DOMAIN_SEARCH][0])) {
701 $domains = '"' . $profile[self::DOMAIN_SEARCH][0] . '"';
702 $domains = str_replace(';', '", "', $domains);
703 $this->setDHCPOption(self::DOMAIN_SEARCH, $domains);
704 }
705
706 if (!$this->isRootNode()) {
707 $this->setDHCPOption(self::SUBNET_MASK, $profile['subnet'][0]);
708
709 // calc the netmask:
710 $ex = explode(".", $profile['subnet'][0]);
711 $num = 0;
712 foreach ($ex as $mask) {
713 $binary = decbin((int) $mask);
714 $num += substr_count($binary, '1');
715 }
716 $this->attributes['dhcpNetMask'][0] = $num;
717 }
718 }
719
720 /**
721 * {@inheritDoc}
722 */
723 public function get_pdfEntries(array $pdfKeys, string $typeId): array {
724 $nodeType = $this->getDHCPOption(self::NETBIOS_NODE_TYPE);
725 $nodeTypeValue = '';
726 if (isset($this->all_netbios_node_types[$nodeType])) {
727 $nodeTypeValue = $this->all_netbios_node_types[$nodeType];
728 }
729 $unknownClients = '';
730 $unknownClientsVal = $this->getUnknownClients();
731 if (!empty($unknownClientsVal)) {
732 $unknownClients = $this->allowDenyOptions[$unknownClientsVal];
733 }
734 $authoritative = $this->isAuthoritative() ? _('Yes') : _('No');
735 $return = [];
736 $this->addPDFKeyValue($return, 'domainName', _('Domain name'), $this->getDHCPOption(self::DOMAIN_NAME));
737 $this->addPDFKeyValue($return, 'leaseTime', _('Lease time'), $this->getDefaultLeaseTime());
738 $this->addPDFKeyValue($return, 'maxLeaseTime', _('Maximum lease time'), $this->getMaxLeaseTime());
739 $this->addPDFKeyValue($return, 'DNSserver', _('DNS'), $this->getDHCPOption(self::DOMAIN_NAME_SERVERS));
740 $this->addPDFKeyValue($return, self::DOMAIN_SEARCH, _('Search domains'), $this->getDHCPOption(self::DOMAIN_SEARCH));
741 $this->addPDFKeyValue($return, 'gateway', _('Default gateway'), $this->getDHCPOption(self::ROUTERS));
742 $this->addPDFKeyValue($return, 'netbiosServer', _('Netbios name servers'), $this->getDHCPOption(self::NETBIOS_NAME_SERVERS));
743 $this->addPDFKeyValue($return, 'netbiosType', _('Netbios node type'), $nodeTypeValue);
744 $this->addPDFKeyValue($return, 'subnetMask', _('Subnet mask'), $this->getDHCPOption(self::SUBNET_MASK));
745 $this->addPDFKeyValue($return, 'unknownClients', _('Unknown clients'), $unknownClients);
746 $this->addSimplePDFField($return, 'description', _('Description'), 'dhcpComments');
747 $this->addSimplePDFField($return, 'subnet', _('Subnet'), 'cn');
748 $this->addSimplePDFField($return, 'netMask', _('Net mask'), 'dhcpNetMask');
749 $this->addPDFKeyValue($return, self::AUTHORITATIVE, _('Authoritative'), $authoritative);
750 return $return;
751 }
752
753 /**
754 * Returns a DHCP option.
755 *
756 * @param string $name option name
757 * @return string|null value
758 */
759 public function getDHCPOption($name) {
760 $return = null;
761 if (is_array($this->attributes['dhcpOption'])) {
762 for ($i = 0; $i < count($this->attributes['dhcpOption']); $i++) {
763 $val = $this->attributes['dhcpOption'][$i];
764 if (substr($val, 0, strlen($name) + 1) === $name . ' ') {
765 $return = substr($val, strlen($name) + 1);
766 $return = str_replace('"', '', $return);
767 break;
768 }
769 }
770 }
771 return $return;
772 }
773
774 /**
775 * Sets a DHCP option.
776 *
777 * @param string $name option name
778 * @param string|null $value option value
779 */
780 private function setDHCPOption($name, $value): void {
781 if (empty($this->attributes['dhcpOption'])) {
782 $this->attributes['dhcpOption'] = [];
783 }
784 for ($i = 0; $i < count($this->attributes['dhcpOption']); $i++) {
785 if (substr($this->attributes['dhcpOption'][$i], 0, strlen($name) + 1) === $name . ' ') {
786 unset($this->attributes['dhcpOption'][$i]);
787 $this->attributes['dhcpOption'] = array_values($this->attributes['dhcpOption']);
788 }
789 }
790 if (($value !== null) && ($value !== '')) {
791 $this->attributes['dhcpOption'][] = $name . ' ' . $value;
792 }
793 }
794
795 /**
796 * Returns the default lease time.
797 *
798 * @return String time
799 */
800 private function getDefaultLeaseTime() {
801 $return = null;
802 if (!empty($this->attributes['dhcpStatements'])) {
803 for ($i = 0; $i < count($this->attributes['dhcpStatements']); $i++) {
804 if (substr($this->attributes['dhcpStatements'][$i], 0, 19) === self::DEFAULT_LEASE_TIME . ' ') {
805 $return = substr($this->attributes['dhcpStatements'][$i], 19);
806 break;
807 }
808 }
809 }
810 return $return;
811 }
812
813 /**
814 * Sets the default lease time.
815 *
816 * @param string|null $time time
817 */
818 private function setDefaultLeaseTime($time): void {
819 if (empty($this->attributes['dhcpStatements'])) {
820 $this->attributes['dhcpStatements'] = [];
821 }
822 for ($i = 0; $i < count($this->attributes['dhcpStatements']); $i++) {
823 if (substr($this->attributes['dhcpStatements'][$i], 0, 19) === self::DEFAULT_LEASE_TIME . ' ') {
824 unset($this->attributes['dhcpStatements'][$i]);
825 $this->attributes['dhcpStatements'] = array_values($this->attributes['dhcpStatements']);
826 }
827 }
828 if (($time !== null) && ($time !== '')) {
829 $this->attributes['dhcpStatements'][] = self::DEFAULT_LEASE_TIME . ' ' . $time;
830 }
831 }
832
833 /**
834 * Returns the unknown clients option.
835 *
836 * @return string unknown clients value
837 */
838 private function getUnknownClients() {
839 $return = null;
840 if (!empty($this->attributes['dhcpStatements'])) {
841 for ($i = 0; $i < count($this->attributes['dhcpStatements']); $i++) {
842 $val = $this->attributes['dhcpStatements'][$i];
843 if (strpos($val, self::UNKNOWN_CLIENTS) === (strlen($val) - strlen(self::UNKNOWN_CLIENTS))) {
844 $return = substr($val, 0, (strlen($val) - strlen(self::UNKNOWN_CLIENTS) - 1));
845 break;
846 }
847 }
848 }
849 return $return;
850 }
851
852 /**
853 * Sets the unknown clients option.
854 *
855 * @param string $option allow/deny
856 */
857 private function setUnknownClients($option): void {
858 if (!isset($this->attributes['dhcpStatements'])) {
859 $this->attributes['dhcpStatements'] = [];
860 }
861 for ($i = 0; $i < count($this->attributes['dhcpStatements']); $i++) {
862 $val = $this->attributes['dhcpStatements'][$i];
863 if (strpos($val, self::UNKNOWN_CLIENTS) === (strlen($val) - strlen(self::UNKNOWN_CLIENTS))) {
864 unset($this->attributes['dhcpStatements'][$i]);
865 $this->attributes['dhcpStatements'] = array_values($this->attributes['dhcpStatements']);
866 }
867 }
868 if (!empty($option) && ($option != '-')) {
869 $this->attributes['dhcpStatements'][] = $option . ' ' . self::UNKNOWN_CLIENTS;
870 }
871 }
872
873 /**
874 * Returns the maximum lease time.
875 *
876 * @return String time
877 */
878 private function getMaxLeaseTime() {
879 $return = null;
880 if (!empty($this->attributes['dhcpStatements'])) {
881 for ($i = 0; $i < count($this->attributes['dhcpStatements']); $i++) {
882 if (substr($this->attributes['dhcpStatements'][$i], 0, 15) === self::MAX_LEASE_TIME . ' ') {
883 $return = substr($this->attributes['dhcpStatements'][$i], 15);
884 break;
885 }
886 }
887 }
888 return $return;
889 }
890
891 /**
892 * Sets the maximum lease time.
893 *
894 * @param string|null $time time
895 */
896 private function setMaxLeaseTime($time): void {
897 if (!is_array($this->attributes['dhcpStatements'])) {
898 $this->attributes['dhcpStatements'] = [];
899 }
900 for ($i = 0; $i < count($this->attributes['dhcpStatements']); $i++) {
901 if (substr($this->attributes['dhcpStatements'][$i], 0, 15) === self::MAX_LEASE_TIME . ' ') {
902 unset($this->attributes['dhcpStatements'][$i]);
903 $this->attributes['dhcpStatements'] = array_values($this->attributes['dhcpStatements']);
904 }
905 }
906 if (($time !== null) && ($time !== '')) {
907 $this->attributes['dhcpStatements'][] = self::MAX_LEASE_TIME . ' ' . $time;
908 }
909 }
910
911 /**
912 * Returns if the subnet is authoritative.
913 *
914 * @return bool authoritative
915 */
916 private function isAuthoritative(): bool {
917 if (!empty($this->attributes['dhcpStatements'])) {
918 for ($i = 0; $i < count($this->attributes['dhcpStatements']); $i++) {
919 if ($this->attributes['dhcpStatements'][$i] === self::AUTHORITATIVE) {
920 return true;
921 }
922 }
923 }
924 return false;
925 }
926
927 /**
928 * Sets if the subnet is authoritative.
929 *
930 * @param bool $isAuthoritative authoritative
931 */
932 private function setAuthoritative(bool $isAuthoritative): void {
933 if (!is_array($this->attributes['dhcpStatements'])) {
934 $this->attributes['dhcpStatements'] = [];
935 }
936 for ($i = 0; $i < count($this->attributes['dhcpStatements']); $i++) {
937 if ($this->attributes['dhcpStatements'][$i] === self::AUTHORITATIVE) {
938 unset($this->attributes['dhcpStatements'][$i]);
939 $this->attributes['dhcpStatements'] = array_values($this->attributes['dhcpStatements']);
940 break;
941 }
942 }
943 if ($isAuthoritative) {
944 $this->attributes['dhcpStatements'][] = self::AUTHORITATIVE;
945 }
946 }
947
948 /**
949 * Returns a list of extra DHCP statements.
950 *
951 * @return string[] statements
952 */
953 private function getExtraStatements(): array {
954 $extraStatements = [];
955 if (!empty($this->attributes['dhcpStatements'])) {
956 foreach ($this->attributes['dhcpStatements'] as $statement) {
957 if (($statement !== self::AUTHORITATIVE)
958 && (!str_starts_with($statement, self::DEFAULT_LEASE_TIME))
959 && (!str_starts_with($statement, self::MAX_LEASE_TIME))
960 && (strpos($statement, self::UNKNOWN_CLIENTS) !== (strlen($statement) - strlen(self::UNKNOWN_CLIENTS)))) {
961 $extraStatements[] = $statement;
962 }
963 }
964 }
965 return $extraStatements;
966 }
967
968 /**
969 * Sets extra DHCP statements.
970 *
971 * @param string[] $statements statements
972 */
973 private function setExtraStatements(array $statements): void {
974 if (!is_array($this->attributes['dhcpStatements'])) {
975 $this->attributes['dhcpStatements'] = [];
976 }
977 $toDelete = [];
978 foreach ($this->attributes['dhcpStatements'] as $statement) {
979 if (($statement !== self::AUTHORITATIVE)
980 && (!str_starts_with($statement, self::DEFAULT_LEASE_TIME))
981 && (!str_starts_with($statement, self::MAX_LEASE_TIME))
982 && (strpos($statement, self::UNKNOWN_CLIENTS) !== (strlen($statement) - strlen(self::UNKNOWN_CLIENTS)))) {
983 $toDelete[] = $statement;
984 }
985 }
986 $this->attributes['dhcpStatements'] = array_delete($toDelete, $this->attributes['dhcpStatements']);
987 $this->attributes['dhcpStatements'] = array_merge($this->attributes['dhcpStatements'], $statements);
988 }
989
990 /**
991 * Returns a list of extra DHCP options.
992 *
993 * @return string[] options
994 */
995 private function getExtraOptions(): array {
996 $extraOptions = [];
997 if (!empty($this->attributes['dhcpOption'])) {
998 foreach ($this->attributes['dhcpOption'] as $option) {
999 if ((!str_starts_with($option, self::DOMAIN_NAME))
1000 && (!str_starts_with($option, self::DOMAIN_NAME_SERVERS))
1001 && (!str_starts_with($option, self::DOMAIN_SEARCH))
1002 && (!str_starts_with($option, self::ROUTERS))
1003 && (!str_starts_with($option, self::NETBIOS_NAME_SERVERS))
1004 && (!str_starts_with($option, self::NETBIOS_NODE_TYPE))
1005 && (!str_starts_with($option, self::SUBNET_MASK))
1006 ) {
1007 $extraOptions[] = $option;
1008 }
1009 }
1010 }
1011 return $extraOptions;
1012 }
1013
1014 /**
1015 * Sets extra DHCP options.
1016 *
1017 * @param string[] $options options
1018 */
1019 private function setExtraOptions(array $options): void {
1020 if (!is_array($this->attributes['dhcpOption'])) {
1021 $this->attributes['dhcpOption'] = [];
1022 }
1023 $toDelete = [];
1024 foreach ($this->attributes['dhcpOption'] as $option) {
1025 if ((!str_starts_with($option, self::DOMAIN_NAME))
1026 && (!str_starts_with($option, self::DOMAIN_NAME_SERVERS))
1027 && (!str_starts_with($option, self::DOMAIN_SEARCH))
1028 && (!str_starts_with($option, self::ROUTERS))
1029 && (!str_starts_with($option, self::NETBIOS_NAME_SERVERS))
1030 && (!str_starts_with($option, self::NETBIOS_NODE_TYPE))
1031 && (!str_starts_with($option, self::SUBNET_MASK))
1032 ) {
1033 $toDelete[] = $option;
1034 }
1035 }
1036 $this->attributes['dhcpOption'] = array_delete($toDelete, $this->attributes['dhcpOption']);
1037 $this->attributes['dhcpOption'] = array_merge($this->attributes['dhcpOption'], $options);
1038 }
1039
1040 /**
1041 * {@inheritDoc}
1042 */
1043 public function build_uploadAccounts(array $rawAccounts, array $ids, array &$partialAccounts, array $selectedModules, ConfiguredType &$type): array {
1044 $messages = [];
1045 for ($i = 0; $i < count($rawAccounts); $i++) {
1046 // add object class
1047 if (!in_array("dhcpOptions", $partialAccounts[$i]['objectClass'])) {
1048 $partialAccounts[$i]['objectClass'][] = "dhcpOptions";
1049 }
1050 if (!in_array("dhcpSubnet", $partialAccounts[$i]['objectClass'])) {
1051 $partialAccounts[$i]['objectClass'][] = "dhcpSubnet";
1052 }
1053 // subnet
1054 if (check_ip($rawAccounts[$i][$ids['dhcp_settings_subnet']], true)) {
1055 $partialAccounts[$i]['cn'] = $rawAccounts[$i][$ids['dhcp_settings_subnet']];
1056 }
1057 else {
1058 $error = $this->messages['cn'][3];
1059 $error[] = [$i];
1060 $messages[] = $error;
1061 }
1062 // domain name
1063 if ($rawAccounts[$i][$ids['dhcp_settings_domainName']] != "") {
1064 if (preg_match("/^[A-Za-z0-9\\._-]*$/", $rawAccounts[$i][$ids['dhcp_settings_domainName']])) {
1065 $partialAccounts[$i]['dhcpOption'][] = self::DOMAIN_NAME . ' "' . $rawAccounts[$i][$ids['dhcp_settings_domainName']] . '"';
1066 }
1067 else {
1068 $error = $this->messages['domainname'][5];
1069 $error[] = [$i];
1070 $messages[] = $error;
1071 }
1072 }
1073 // lease time
1074 if ($rawAccounts[$i][$ids['dhcp_settings_leaseTime']] != '') {
1075 if (is_numeric($rawAccounts[$i][$ids['dhcp_settings_leaseTime']])) {
1076 $partialAccounts[$i]['dhcpStatements'][] = self::DEFAULT_LEASE_TIME . ' ' . $rawAccounts[$i][$ids['dhcp_settings_leaseTime']];
1077 }
1078 else {
1079 $error = $this->messages['lease_time'][1];
1080 $error[] = [$i];
1081 $messages[] = $error;
1082 }
1083 }
1084 // max lease time
1085 if ($rawAccounts[$i][$ids['dhcp_settings_maxLeaseTime']] != '') {
1086 if (is_numeric($rawAccounts[$i][$ids['dhcp_settings_maxLeaseTime']])) {
1087 $partialAccounts[$i]['dhcpStatements'][] = self::MAX_LEASE_TIME . ' ' . $rawAccounts[$i][$ids['dhcp_settings_maxLeaseTime']];
1088 }
1089 else {
1090 $error = $this->messages['max_lease_time'][1];
1091 $error[] = [$i];
1092 $messages[] = $error;
1093 }
1094 }
1095 // DNS
1096 if ($rawAccounts[$i][$ids['dhcp_settings_DNSserver']] != '') {
1097 $ex = explode(",", $rawAccounts[$i][$ids['dhcp_settings_DNSserver']]);
1098 $invalid = false;
1099 foreach ($ex as $string) {
1100 if (!check_ip($string)) {
1101 $invalid = true;
1102 }
1103 }
1104 if (!$invalid) {
1105 $partialAccounts[$i]['dhcpOption'][] = self::DOMAIN_NAME_SERVERS . ' ' . $rawAccounts[$i][$ids['dhcp_settings_DNSserver']];
1106 }
1107 else {
1108 $error = $this->messages['dns'][1];
1109 $error[] = [$i];
1110 $messages[] = $error;
1111 }
1112 }
1113 // search domains
1114 if ($rawAccounts[$i][$ids['dhcp_settings_domain-search']] != '') {
1115 $domains = preg_split('/;[ ]*/', $rawAccounts[$i][$ids['dhcp_settings_domain-search']]);
1116 $invalid = false;
1117 foreach ($domains as $key => $name) {
1118 if (!get_preg($name, 'domainname')) {
1119 $invalid = true;
1120 }
1121 else {
1122 $domains[$key] = '"' . $name . '"';
1123 }
1124 }
1125 if (!$invalid) {
1126 $partialAccounts[$i]['dhcpOption'][] = self::DOMAIN_SEARCH . ' ' . implode(', ', $domains);
1127 }
1128 else {
1129 $error = $this->messages['INFO_domain-search'][1];
1130 $error[] = [$i];
1131 $messages[] = $error;
1132 }
1133 }
1134 // gateway
1135 if ($rawAccounts[$i][$ids['dhcp_settings_gateway']] != '') {
1136 if (check_ip($rawAccounts[$i][$ids['dhcp_settings_gateway']])) {
1137 $partialAccounts[$i]['dhcpOption'][] = self::ROUTERS . ' ' . $rawAccounts[$i][$ids['dhcp_settings_gateway']];
1138 }
1139 else {
1140 $error = $this->messages['routers'][1];
1141 $error[] = [$i];
1142 $messages[] = $error;
1143 }
1144 }
1145 // netbios name servers
1146 if ($rawAccounts[$i][$ids['dhcp_settings_netbiosServer']] != '') {
1147 $ex = explode(",", $rawAccounts[$i][$ids['dhcp_settings_netbiosServer']]);
1148 $invalid = false;
1149 foreach ($ex as $string) {
1150 if (!check_ip($string) && !get_preg($string, 'DNSname')) {
1151 $invalid = true;
1152 }
1153 }
1154 if (!$invalid) {
1155 $partialAccounts[$i]['dhcpOption'][] = self::NETBIOS_NAME_SERVERS . ' ' . $rawAccounts[$i][$ids['dhcp_settings_netbiosServer']];
1156 }
1157 else {
1158 $error = $this->messages['netbios'][1];
1159 $error[] = [$i];
1160 $messages[] = $error;
1161 }
1162 }
1163 // node type
1164 if ($rawAccounts[$i][$ids['dhcp_settings_netbiosType']] != '') {
1165 if (in_array($rawAccounts[$i][$ids['dhcp_settings_netbiosType']], ['B', 'P', 'M', 'H'])) {
1166 if ($rawAccounts[$i][$ids['dhcp_settings_netbiosType']] == 'B') {
1167 $partialAccounts[$i]['dhcpOption'][] = self::NETBIOS_NODE_TYPE . ' 1';
1168 }
1169 elseif ($rawAccounts[$i][$ids['dhcp_settings_netbiosType']] == 'P') {
1170 $partialAccounts[$i]['dhcpOption'][] = self::NETBIOS_NODE_TYPE . ' 2';
1171 }
1172 elseif ($rawAccounts[$i][$ids['dhcp_settings_netbiosType']] == 'M') {
1173 $partialAccounts[$i]['dhcpOption'][] = self::NETBIOS_NODE_TYPE . ' 4';
1174 }
1175 elseif ($rawAccounts[$i][$ids['dhcp_settings_netbiosType']] == 'H') {
1176 $partialAccounts[$i]['dhcpOption'][] = self::NETBIOS_NODE_TYPE . ' 8';
1177 }
1178 }
1179 else {
1180 $error = $this->messages['netbios_node_type'][1];
1181 $error[] = [$i];
1182 $messages[] = $error;
1183 }
1184 }
1185 else {
1186 $partialAccounts[$i]['dhcpOption'][] = self::NETBIOS_NODE_TYPE . ' 8'; // default H
1187 }
1188 // unknown clients
1189 if (!empty($rawAccounts[$i][$ids['dhcp_settings_unknownClients']])) {
1190 $unknownClients = $rawAccounts[$i][$ids['dhcp_settings_unknownClients']];
1191 if (in_array($unknownClients, $this->allowDenyOptions)) {
1192 $allowDenyOptions = array_flip($this->allowDenyOptions);
1193 $partialAccounts[$i]['dhcpStatements'][] = $allowDenyOptions[$unknownClients] . ' ' . self::UNKNOWN_CLIENTS;
1194 }
1195 else {
1196 $error = $this->messages['unknownClients'][0];
1197 $error[] = [$i];
1198 $messages[] = $error;
1199 }
1200 }
1201 // node type
1202 if ($rawAccounts[$i][$ids['dhcp_settings_authoritative']] !== '') {
1203 if (in_array($rawAccounts[$i][$ids['dhcp_settings_authoritative']], ['true', 'false'])) {
1204 if ($rawAccounts[$i][$ids['dhcp_settings_authoritative']] === 'true') {
1205 $partialAccounts[$i]['dhcpStatements'][] = self::AUTHORITATIVE;
1206 }
1207 }
1208 else {
1209 $error = $this->messages['authoritative'][0];
1210 $error[] = [$i];
1211 $messages[] = $error;
1212 }
1213 }
1214 // subnet mask
1215 if (check_ip($rawAccounts[$i][$ids['dhcp_settings_subnetMask']], true)) {
1216 $partialAccounts[$i]['dhcpOption'][] = self::SUBNET_MASK . ' ' . $rawAccounts[$i][$ids['dhcp_settings_subnetMask']];
1217 }
1218 else {
1219 $error = $this->messages['subnet'][1];
1220 $error[] = [$i];
1221 $messages[] = $error;
1222 }
1223 // net mask
1224 $mask = $this->calculateNetMask($rawAccounts[$i][$ids['dhcp_settings_subnetMask']]);
1225 $partialAccounts[$i]['dhcpNetMask'][0] = $mask;
1226 // description
1227 if (isset($rawAccounts[$i][$ids['dhcp_settings_description']]) && ($rawAccounts[$i][$ids['dhcp_settings_description']] != '')) {
1228 $partialAccounts[$i]['dhcpComments'][0] = $rawAccounts[$i][$ids['dhcp_settings_description']];
1229 }
1230 }
1231 return $messages;
1232 }
1233
1234 /**
1235 * Returns if the current DN is the root entry.
1236 *
1237 * @return bool is root
1238 */
1239 private function isRootNode() {
1240 $rootSuffix = $this->getAccountContainer()->get_type()->getSuffix();
1241 return $this->getAccountContainer()->dn_orig == $rootSuffix;
1242 }
1243
1244 /**
1245 * @inheritDoc
1246 */
1247 public function getListAttributeDescriptions(ConfiguredType $type): array {
1248 return [
1249 "cn" => _("Subnet"),
1250 'dhcpcomments' => _('Description'),
1251 ];
1252 }
1253
1254 }