Project

General

Profile

Download (64.5 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * services_dhcp.php
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2013 BSD Perimeter
7
 * Copyright (c) 2013-2016 Electric Sheep Fencing
8
 * Copyright (c) 2014-2021 Rubicon Communications, LLC (Netgate)
9
 * All rights reserved.
10
 *
11
 * originally based on m0n0wall (http://m0n0.ch/wall)
12
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
13
 * All rights reserved.
14
 *
15
 * Licensed under the Apache License, Version 2.0 (the "License");
16
 * you may not use this file except in compliance with the License.
17
 * You may obtain a copy of the License at
18
 *
19
 * http://www.apache.org/licenses/LICENSE-2.0
20
 *
21
 * Unless required by applicable law or agreed to in writing, software
22
 * distributed under the License is distributed on an "AS IS" BASIS,
23
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
 * See the License for the specific language governing permissions and
25
 * limitations under the License.
26
 */
27

    
28
##|+PRIV
29
##|*IDENT=page-services-dhcpserver
30
##|*NAME=Services: DHCP Server
31
##|*DESCR=Allow access to the 'Services: DHCP Server' page.
32
##|*MATCH=services_dhcp.php*
33
##|-PRIV
34

    
35
require_once("guiconfig.inc");
36
require_once("filter.inc");
37
require_once('rrd.inc');
38
require_once("shaper.inc");
39
require_once("util.inc");
40

    
41
global $ddnsdomainkeyalgorithms;
42

    
43
if (!$g['services_dhcp_server_enable']) {
44
	header("Location: /");
45
	exit;
46
}
47

    
48
$if = $_REQUEST['if'];
49
$iflist = get_configured_interface_with_descr();
50

    
51
/* set the starting interface */
52
if (!$if || !isset($iflist[$if])) {
53
	$found_starting_if = false;
54
	// First look for an interface with DHCP already enabled.
55
	foreach ($iflist as $ifent => $ifname) {
56
		$oc = $config['interfaces'][$ifent];
57
		if (is_array($config['dhcpd'][$ifent]) &&
58
		    isset($config['dhcpd'][$ifent]['enable']) &&
59
		    is_ipaddrv4($oc['ipaddr']) && $oc['subnet'] < 31) {
60
			$if = $ifent;
61
			$found_starting_if = true;
62
			break;
63
		}
64
	}
65

    
66
	/*
67
	 * If there is no DHCP-enabled interface and LAN is a candidate,
68
	 * then choose LAN.
69
	 */
70
	if (!$found_starting_if && isset($iflist['lan']) &&
71
	    is_ipaddrv4($config['interfaces']['lan']['ipaddr']) &&
72
	    $config['interfaces']['lan']['subnet'] < 31) {
73
		$if = 'lan';
74
		$found_starting_if = true;
75
	}
76

    
77
	// At the last select whatever can be found.
78
	if (!$found_starting_if) {
79
		foreach ($iflist as $ifent => $ifname) {
80
			$oc = $config['interfaces'][$ifent];
81

    
82
			/* Not static IPv4 or subnet >= 31 */
83
			if (!is_ipaddrv4($oc['ipaddr']) ||
84
			    empty($oc['subnet']) || $oc['subnet'] < 31) {
85
				continue;
86
			}
87

    
88
			if (!is_array($config['dhcpd'][$ifent]) ||
89
			    !isset($config['dhcpd'][$ifent]['enable'])) {
90
				continue;
91
			}
92

    
93
			$if = $ifent;
94
			break;
95
		}
96
	}
97
}
98

    
99
$act = $_REQUEST['act'];
100

    
101
$a_pools = array();
102

    
103
if (is_array($config['dhcpd'][$if])) {
104
	$pool = $_REQUEST['pool'];
105
	if (is_numeric($_POST['pool'])) {
106
		$pool = $_POST['pool'];
107
	}
108

    
109
	// If we have a pool but no interface name, that's not valid. Redirect away.
110
	if (is_numeric($pool) && empty($if)) {
111
		header("Location: services_dhcp.php");
112
		exit;
113
	}
114

    
115
	init_config_arr(array('dhcpd', $if, 'pool'));
116
	$a_pools = &$config['dhcpd'][$if]['pool'];
117

    
118
	if (is_numeric($pool) && $a_pools[$pool]) {
119
		$dhcpdconf = &$a_pools[$pool];
120
	} elseif ($act == "newpool") {
121
		$dhcpdconf = array();
122
	} else {
123
		$dhcpdconf = &$config['dhcpd'][$if];
124
	}
125

    
126
	init_config_arr(array('dhcpd', $if, 'staticmap'));
127
	$a_maps = &$config['dhcpd'][$if]['staticmap'];
128
}
129

    
130
if (is_array($dhcpdconf)) {
131
	// Global Options
132
	if (!is_numeric($pool) && !($act == "newpool")) {
133
		$pconfig['enable'] = isset($dhcpdconf['enable']);
134
		$pconfig['staticarp'] = isset($dhcpdconf['staticarp']);
135
		// No reason to specify this per-pool, per the dhcpd.conf man page it needs to be in every
136
		//	 pool and should be specified in every pool both nodes share, so we'll treat it as global
137
		$pconfig['failover_peerip'] = $dhcpdconf['failover_peerip'];
138

    
139
		// dhcpleaseinlocaltime is global to all interfaces. So if it is selected on any interface,
140
		// then show it true/checked.
141
		foreach ($config['dhcpd'] as $dhcpdifitem) {
142
			$dhcpleaseinlocaltime = $dhcpdifitem['dhcpleaseinlocaltime'];
143
			if ($dhcpleaseinlocaltime) {
144
				break;
145
			}
146
		}
147

    
148
		$pconfig['dhcpleaseinlocaltime'] = $dhcpleaseinlocaltime;
149
	} else {
150
		// Options that exist only in pools
151
		$pconfig['descr'] = $dhcpdconf['descr'];
152
	}
153

    
154
	// Options that can be global or per-pool.
155
	if (is_array($dhcpdconf['range'])) {
156
		$pconfig['range_from'] = $dhcpdconf['range']['from'];
157
		$pconfig['range_to'] = $dhcpdconf['range']['to'];
158
	}
159

    
160
	$pconfig['deftime'] = $dhcpdconf['defaultleasetime'];
161
	$pconfig['maxtime'] = $dhcpdconf['maxleasetime'];
162
	$pconfig['gateway'] = $dhcpdconf['gateway'];
163
	$pconfig['domain'] = $dhcpdconf['domain'];
164
	$pconfig['domainsearchlist'] = $dhcpdconf['domainsearchlist'];
165
	list($pconfig['wins1'], $pconfig['wins2']) = $dhcpdconf['winsserver'];
166
	list($pconfig['dns1'], $pconfig['dns2'], $pconfig['dns3'], $pconfig['dns4']) = $dhcpdconf['dnsserver'];
167
	$pconfig['ignorebootp'] = isset($dhcpdconf['ignorebootp']);
168

    
169
	if (isset($dhcpdconf['denyunknown'])) {
170
		$pconfig['denyunknown'] = empty($dhcpdconf['denyunknown']) ? "enabled" : $dhcpdconf['denyunknown'];
171
	} else {
172
		$pconfig['denyunknown'] = "disabled";
173
	}
174

    
175
	$pconfig['ignoreclientuids'] = isset($dhcpdconf['ignoreclientuids']);
176
	$pconfig['nonak'] = isset($dhcpdconf['nonak']);
177
	$pconfig['ddnsdomain'] = $dhcpdconf['ddnsdomain'];
178
	$pconfig['ddnsdomainprimary'] = $dhcpdconf['ddnsdomainprimary'];
179
	$pconfig['ddnsdomainsecondary'] = $dhcpdconf['ddnsdomainsecondary'];
180
	$pconfig['ddnsdomainkeyname'] = $dhcpdconf['ddnsdomainkeyname'];
181
	$pconfig['ddnsdomainkeyalgorithm'] = $dhcpdconf['ddnsdomainkeyalgorithm'];
182
	$pconfig['ddnsdomainkey'] = $dhcpdconf['ddnsdomainkey'];
183
	$pconfig['ddnsupdate'] = isset($dhcpdconf['ddnsupdate']);
184
	$pconfig['ddnsforcehostname'] = isset($dhcpdconf['ddnsforcehostname']);
185
	$pconfig['mac_allow'] = $dhcpdconf['mac_allow'];
186
	$pconfig['mac_deny'] = $dhcpdconf['mac_deny'];
187
	list($pconfig['ntp1'], $pconfig['ntp2'], $pconfig['ntp3'] ) = $dhcpdconf['ntpserver'];
188
	$pconfig['tftp'] = $dhcpdconf['tftp'];
189
	$pconfig['ldap'] = $dhcpdconf['ldap'];
190
	$pconfig['netboot'] = isset($dhcpdconf['netboot']);
191
	$pconfig['nextserver'] = $dhcpdconf['nextserver'];
192
	$pconfig['filename'] = $dhcpdconf['filename'];
193
	$pconfig['filename32'] = $dhcpdconf['filename32'];
194
	$pconfig['filename64'] = $dhcpdconf['filename64'];
195
	$pconfig['filename32arm'] = $dhcpdconf['filename32arm'];
196
	$pconfig['filename64arm'] = $dhcpdconf['filename64arm'];
197
	$pconfig['uefihttpboot'] = $dhcpdconf['uefihttpboot'];
198
	$pconfig['rootpath'] = $dhcpdconf['rootpath'];
199
	$pconfig['netmask'] = $dhcpdconf['netmask'];
200
	$pconfig['numberoptions'] = $dhcpdconf['numberoptions'];
201
	$pconfig['statsgraph'] = $dhcpdconf['statsgraph'];
202
	$pconfig['disablepingcheck'] = $dhcpdconf['disablepingcheck'];
203
	$pconfig['ddnsclientupdates'] = $dhcpdconf['ddnsclientupdates'];
204

    
205
	// OMAPI Settings
206
	if(isset($dhcpdconf['omapi_port'])) {
207
		$pconfig['omapi_port'] = $dhcpdconf['omapi_port'];
208
		$pconfig['omapi_key'] = $dhcpdconf['omapi_key'];
209
		$pconfig['omapi_key_algorithm'] = $dhcpdconf['omapi_key_algorithm'];
210
	}
211
}
212

    
213
$ifcfgip = $config['interfaces'][$if]['ipaddr'];
214
$ifcfgsn = $config['interfaces'][$if]['subnet'];
215

    
216
$subnet_start = gen_subnetv4($ifcfgip, $ifcfgsn);
217
$subnet_end = gen_subnetv4_max($ifcfgip, $ifcfgsn);
218

    
219
function validate_partial_mac_list($maclist) {
220
	$macs = explode(',', $maclist);
221

    
222
	// Loop through and look for invalid MACs.
223
	foreach ($macs as $mac) {
224
		if (!is_macaddr($mac, true)) {
225
			return false;
226
		}
227
	}
228

    
229
	return true;
230
}
231

    
232
if (isset($_POST['save'])) {
233

    
234
	unset($input_errors);
235

    
236
	$pconfig = $_POST;
237

    
238
	$numberoptions = array();
239
	for ($x = 0; $x < 99; $x++) {
240
		if (isset($_POST["number{$x}"]) && ctype_digit($_POST["number{$x}"])) {
241
			if ($_POST["number{$x}"] < 1 || $_POST["number{$x}"] > 254) {
242
				$input_errors[] = gettext("The DHCP option must be a number between 1 and 254.");
243
				continue;
244
			}
245
			$numbervalue = array();
246
			$numbervalue['number'] = htmlspecialchars($_POST["number{$x}"]);
247
			$numbervalue['type'] = htmlspecialchars($_POST["itemtype{$x}"]);
248
			$numbervalue['value'] = base64_encode($_POST["value{$x}"]);
249
			$numberoptions['item'][] = $numbervalue;
250
		}
251
	}
252

    
253
	// Reload the new pconfig variable that the form uses.
254
	$pconfig['numberoptions'] = $numberoptions;
255

    
256
	/* input validation */
257

    
258
	/*
259
	 * Check the OMAPI settings
260
	 * - Make sure that if the port is defined, that it is valid and isn't in use
261
	 * - Make sure the key is defined and the length is appropriate for the selected algorithm
262
	 * - Generate a new key if selected
263
	 */
264
	if (!empty($_POST['omapi_port'])) {
265
		// Check the port entry
266
		switch(true){
267
			case !is_port($_POST['omapi_port']) || $_POST['omapi_port'] <= 1024:
268
				$input_errors[] = gettext("The specified OMAPI port number is invalid. Port number must be between 1024 and 65635.");
269
				break;
270
			case is_port_in_use($_POST['omapi_port']) && $_POST['omapi_port'] != $dhcpdconf['omapi_port']:
271
				$input_errors[] = gettext("Specified port number for OMAPI is in use. Please choose another port or consider using the default.");
272
				break;
273
		}
274

    
275
		// Define the minimum base64 character length for each algorithm
276
		$key_char_len_by_alg = array(
277
			'hmac-md5' => 24,
278
			'hmac-sha1' => 28,
279
			'hmac-sha224' => 40,
280
			'hmac-sha256' => 44,
281
			'hmac-sha384' => 64,
282
			'hmac-sha512' => 88
283
		);
284

    
285
		// Generate a key if checked
286
		if ($_POST['omapi_gen_key'] == "yes") {
287
			// Figure out the key bits from the selected algorithm
288
			switch ($_POST['omapi_key_algorithm']) {
289
				case "hmac-md5":
290
					$key_bit_len = 128;
291
					break;
292
				case "hmac-sha1":
293
					$key_bit_len = 160;
294
					break;
295
				default:
296
					$key_bit_len = str_replace("hmac-sha","",$_POST['omapi_key_algorithm']);
297
					break;
298
			}
299

    
300
			// Convert the bits to bytes
301
			$key_bytes_len = $key_bit_len / 8; // 8 bits = 1 Byte
302

    
303
			// Generate random bytes based on key length
304
			$ran_bytes = openssl_random_pseudo_bytes($key_bytes_len);
305

    
306
			// Encode the bytes to get the key string
307
			$key_str = base64_encode($ran_bytes);
308

    
309
			// Set the key
310
			$_POST['omapi_key'] = $key_str;
311
			$pconfig['omapi_key'] = $key_str;
312

    
313
			// Uncheck the generate box
314
			unset($_POST['omapi_gen_key']);
315
			unset($pconfig['omapi_gen_key']);
316
		} elseif (!empty($_POST['omapi_key'])) { // Check the key if it's not being generated
317
			if (strlen($_POST['omapi_key']) < $key_char_len_by_alg[$_POST['omapi_key_algorithm']]) {
318
				$input_errors[] = gettext("Please specify a valid OMAPI key. Key does not meet the minimum length requirement of {$key_char_len_by_alg[$_POST['omapi_key_algorithm']]} for the selected algorithm {$_POST['omapi_key_algorithm']}.");
319
			}
320
		} else {
321
			$input_errors[] = gettext("A key is required when OMAPI is enabled (port specified).");
322
		}
323
	}
324

    
325
	// Note: if DHCP Server is not enabled, then it is OK to adjust other parameters without specifying range from-to.
326
	if ($_POST['enable'] || is_numeric($pool) || $act == "newpool") {
327
		$reqdfields = explode(" ", "range_from range_to");
328
		$reqdfieldsn = array(gettext("Range begin"), gettext("Range end"));
329

    
330
		do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
331
	}
332

    
333
	if (($_POST['nonak']) && !empty($_POST['failover_peerip'])) {
334
		$input_errors[] = gettext("Ignore Denied Clients may not be used when a Failover Peer IP is defined.");
335
	}
336

    
337
	if ($_POST['range_from'] && !is_ipaddrv4($_POST['range_from'])) {
338
		$input_errors[] = gettext("A valid IPv4 address must be specified for range from.");
339
	}
340
	if ($_POST['range_to'] && !is_ipaddrv4($_POST['range_to'])) {
341
		$input_errors[] = gettext("A valid IPv4 address must be specified for range to.");
342
	}
343
	if (($_POST['range_from'] && !$_POST['range_to']) || ($_POST['range_to'] && !$_POST['range_from'])) {
344
		$input_errors[] = gettext("Range From and Range To must both be entered.");
345
	}
346
	if (($_POST['gateway'] && $_POST['gateway'] != "none" && !is_ipaddrv4($_POST['gateway']))) {
347
		$input_errors[] = gettext("A valid IP address must be specified for the gateway.");
348
	}
349
	if (($_POST['wins1'] && !is_ipaddrv4($_POST['wins1'])) || ($_POST['wins2'] && !is_ipaddrv4($_POST['wins2']))) {
350
		$input_errors[] = gettext("A valid IP address must be specified for the primary/secondary WINS servers.");
351
	}
352
	$parent_ip = get_interface_ip($_POST['if']);
353
	if (is_ipaddrv4($parent_ip) && $_POST['gateway'] && $_POST['gateway'] != "none") {
354
		$parent_sn = get_interface_subnet($_POST['if']);
355
		if (!ip_in_subnet($_POST['gateway'], gen_subnet($parent_ip, $parent_sn) . "/" . $parent_sn) && !ip_in_interface_alias_subnet($_POST['if'], $_POST['gateway'])) {
356
			$input_errors[] = sprintf(gettext("The gateway address %s does not lie within the chosen interface's subnet."), $_POST['gateway']);
357
		}
358
	}
359

    
360
	if (($_POST['dns1'] && !is_ipaddrv4($_POST['dns1'])) || ($_POST['dns2'] && !is_ipaddrv4($_POST['dns2'])) || ($_POST['dns3'] && !is_ipaddrv4($_POST['dns3'])) || ($_POST['dns4'] && !is_ipaddrv4($_POST['dns4']))) {
361
		$input_errors[] = gettext("A valid IP address must be specified for each of the DNS servers.");
362
	}
363

    
364
	if ($_POST['deftime'] && (!is_numeric($_POST['deftime']) || ($_POST['deftime'] < 60))) {
365
		$input_errors[] = gettext("The default lease time must be at least 60 seconds.");
366
	}
367

    
368
	if (isset($config['captiveportal']) && is_array($config['captiveportal'])) {
369
		$deftime = 7200; // Default value if it's empty
370
		if (is_numeric($_POST['deftime'])) {
371
			$deftime = $_POST['deftime'];
372
		}
373

    
374
		foreach ($config['captiveportal'] as $cpZone => $cpdata) {
375
			if (!isset($cpdata['enable'])) {
376
				continue;
377
			}
378
			if (!isset($cpdata['timeout']) || !is_numeric($cpdata['timeout'])) {
379
				continue;
380
			}
381
			$cp_ifs = explode(',', $cpdata['interface']);
382
			if (!in_array($if, $cp_ifs)) {
383
				continue;
384
			}
385
			if ($cpdata['timeout'] > $deftime) {
386
				$input_errors[] = sprintf(gettext(
387
					'The Captive Portal zone (%1$s) has Hard Timeout parameter set to a value bigger than Default lease time (%2$s).'), $cpZone, $deftime);
388
			}
389
		}
390
	}
391

    
392
	if ($_POST['maxtime'] && (!is_numeric($_POST['maxtime']) || ($_POST['maxtime'] < 60) || ($_POST['maxtime'] <= $_POST['deftime']))) {
393
		$input_errors[] = gettext("The maximum lease time must be at least 60 seconds and higher than the default lease time.");
394
	}
395
	if ($_POST['ddnsupdate']) {
396
		if (!is_domain($_POST['ddnsdomain'])) {
397
			$input_errors[] = gettext("A valid domain name must be specified for the dynamic DNS registration.");
398
		}
399
		if (!is_ipaddr($_POST['ddnsdomainprimary'])) {
400
			$input_errors[] = gettext("A valid primary domain name server IP address must be specified for the dynamic domain name.");
401
		}
402
		if (!empty($_POST['ddnsdomainsecondary']) && !is_ipaddr($_POST['ddnsdomainsecondary'])) {
403
			$input_errors[] = gettext("A valid secondary domain name server IP address must be specified for the dynamic domain name.");
404
		}
405
		if (!$_POST['ddnsdomainkeyname'] || !$_POST['ddnsdomainkeyalgorithm'] || !$_POST['ddnsdomainkey']) {
406
			$input_errors[] = gettext("A valid domain key name, algorithm and secret must be specified.");
407
		}
408
		if (preg_match('/[^A-Za-z0-9\.\-\_]/', $_POST['ddnsdomainkeyname'])) {
409
			$input_errors[] = gettext("The domain key name may only contain the characters a-z, A-Z, 0-9, '-', '_' and '.'");
410
		}
411
		if ($_POST['ddnsdomainkey'] && !base64_decode($_POST['ddnsdomainkey'], true)) {
412
			$input_errors[] = gettext("The domain key secret must be a Base64 encoded value.");
413
		}
414
	}
415
	if ($_POST['domainsearchlist']) {
416
		$domain_array = preg_split("/[ ;]+/", $_POST['domainsearchlist']);
417
		foreach ($domain_array as $curdomain) {
418
			if (!is_domain($curdomain)) {
419
				$input_errors[] = gettext("A valid domain search list must be specified.");
420
				break;
421
			}
422
		}
423
	}
424

    
425
	// Validate MACs
426
	if (!empty($_POST['mac_allow']) && !validate_partial_mac_list($_POST['mac_allow'])) {
427
		$input_errors[] = gettext("If a mac allow list is specified, it must contain only valid partial MAC addresses.");
428
	}
429
	if (!empty($_POST['mac_deny']) && !validate_partial_mac_list($_POST['mac_deny'])) {
430
		$input_errors[] = gettext("If a mac deny list is specified, it must contain only valid partial MAC addresses.");
431
	}
432

    
433
	if (($_POST['ntp1'] && (!is_ipaddrv4($_POST['ntp1']) && !is_hostname($_POST['ntp1']))) ||
434
	    ($_POST['ntp2'] && (!is_ipaddrv4($_POST['ntp2']) && !is_hostname($_POST['ntp2']))) ||
435
	    ($_POST['ntp3'] && (!is_ipaddrv4($_POST['ntp3']) && !is_hostname($_POST['ntp3'])))) {
436
		$input_errors[] = gettext("A valid IP address or hostname must be specified for the primary/secondary NTP servers.");
437
	}
438
	if ($_POST['domain'] && (!is_domain($_POST['domain'], false, false))) {
439
		$input_errors[] = gettext("A valid domain name must be specified for the DNS domain.");
440
	}
441
	if ($_POST['tftp'] && !is_ipaddrv4($_POST['tftp']) && !is_domain($_POST['tftp']) && !filter_var($_POST['tftp'], FILTER_VALIDATE_URL)) {
442
		$input_errors[] = gettext("A valid IP address, hostname or URL must be specified for the TFTP server.");
443
	}
444
	if (($_POST['nextserver'] && !is_ipaddrv4($_POST['nextserver']))) {
445
		$input_errors[] = gettext("A valid IP address must be specified for the network boot server.");
446
	}
447

    
448
	if (gen_subnet($ifcfgip, $ifcfgsn) == $_POST['range_from']) {
449
		$input_errors[] = gettext("The network address cannot be used in the starting subnet range.");
450
	}
451
	if (gen_subnet_max($ifcfgip, $ifcfgsn) == $_POST['range_to']) {
452
		$input_errors[] = gettext("The broadcast address cannot be used in the ending subnet range.");
453
	}
454

    
455
	// Disallow a range that includes the virtualip
456
	if (is_array($config['virtualip']['vip'])) {
457
		foreach ($config['virtualip']['vip'] as $vip) {
458
			if ($vip['interface'] == $if) {
459
				if ($vip['subnet'] && is_inrange_v4($vip['subnet'], $_POST['range_from'], $_POST['range_to'])) {
460
					$input_errors[] = sprintf(gettext("The subnet range cannot overlap with virtual IP address %s."), $vip['subnet']);
461
				}
462
			}
463
		}
464
	}
465

    
466
	$noip = false;
467
	if (is_array($a_maps)) {
468
		foreach ($a_maps as $map) {
469
			if (empty($map['ipaddr'])) {
470
				$noip = true;
471
			}
472
		}
473
	}
474

    
475
	if ($_POST['staticarp'] && $noip) {
476
		$input_errors[] = gettext("Cannot enable static ARP when there are static map entries without IP addresses. Ensure all static maps have IP addresses and try again.");
477
	}
478

    
479
	if (is_array($pconfig['numberoptions']['item'])) {
480
		foreach ($pconfig['numberoptions']['item'] as $numberoption) {
481
			$numberoption_value = base64_decode($numberoption['value']);
482
			if ($numberoption['type'] == 'text' && strstr($numberoption_value, '"')) {
483
				$input_errors[] = gettext("Text type cannot include quotation marks.");
484
			} else if ($numberoption['type'] == 'string' && !preg_match('/^"[^"]*"$/', $numberoption_value) && !preg_match('/^[0-9a-f]{2}(?:\:[0-9a-f]{2})*$/i', $numberoption_value)) {
485
				$input_errors[] = gettext("String type must be enclosed in quotes like \"this\" or must be a series of octets specified in hexadecimal, separated by colons, like 01:23:45:67:89:ab:cd:ef");
486
			} else if ($numberoption['type'] == 'boolean' && $numberoption_value != 'true' && $numberoption_value != 'false' && $numberoption_value != 'on' && $numberoption_value != 'off') {
487
				$input_errors[] = gettext("Boolean type must be true, false, on, or off.");
488
			} else if ($numberoption['type'] == 'unsigned integer 8' && (!is_numeric($numberoption_value) || $numberoption_value < 0 || $numberoption_value > 255)) {
489
				$input_errors[] = gettext("Unsigned 8-bit integer type must be a number in the range 0 to 255.");
490
			} else if ($numberoption['type'] == 'unsigned integer 16' && (!is_numeric($numberoption_value) || $numberoption_value < 0 || $numberoption_value > 65535)) {
491
				$input_errors[] = gettext("Unsigned 16-bit integer type must be a number in the range 0 to 65535.");
492
			} else if ($numberoption['type'] == 'unsigned integer 32' && (!is_numeric($numberoption_value) || $numberoption_value < 0 || $numberoption_value > 4294967295)) {
493
				$input_errors[] = gettext("Unsigned 32-bit integer type must be a number in the range 0 to 4294967295.");
494
			} else if ($numberoption['type'] == 'signed integer 8' && (!is_numeric($numberoption_value) || $numberoption_value < -128 || $numberoption_value > 127)) {
495
				$input_errors[] = gettext("Signed 8-bit integer type must be a number in the range -128 to 127.");
496
			} else if ($numberoption['type'] == 'signed integer 16' && (!is_numeric($numberoption_value) || $numberoption_value < -32768 || $numberoption_value > 32767)) {
497
				$input_errors[] = gettext("Signed 16-bit integer type must be a number in the range -32768 to 32767.");
498
			} else if ($numberoption['type'] == 'signed integer 32' && (!is_numeric($numberoption_value) || $numberoption_value < -2147483648 || $numberoption_value > 2147483647)) {
499
				$input_errors[] = gettext("Signed 32-bit integer type must be a number in the range -2147483648 to 2147483647.");
500
			} else if ($numberoption['type'] == 'ip-address' && !is_ipaddrv4($numberoption_value) && !is_hostname($numberoption_value)) {
501
				$input_errors[] = gettext("IP address or host type must be an IP address or host name.");
502
			}
503
		}
504
	}
505

    
506
	if ((!isset($pool) || !is_numeric($pool)) && $act != "newpool") {
507
		/* If enabling DHCP Server, make sure that the DHCP Relay isn't enabled on this interface */
508
		if ($_POST['enable'] && isset($config['dhcrelay']['enable']) &&
509
		    (stristr($config['dhcrelay']['interface'], $if) !== false)) {
510
			$input_errors[] = sprintf(gettext(
511
			    "The DHCP relay on the %s interface must be disabled before enabling the DHCP server."),
512
			    $iflist[$if]);
513
		}
514

    
515
		/* If disabling DHCP Server, make sure that DHCP registration isn't enabled for DNS forwarder/resolver */
516
		if (!$_POST['enable']) {
517
			/* Find out how many other interfaces have DHCP enabled. */
518
			$dhcp_enabled_count = 0;
519
			foreach ($config['dhcpd'] as $dhif => $dhcps) {
520
				if ($dhif == $if) {
521
					/* Skip this interface, we only want to know how many others are enabled. */
522
					continue;
523
				}
524
				if (isset($dhcps['enable'])) {
525
					$dhcp_enabled_count++;
526
				}
527
			}
528

    
529
			if (isset($config['dnsmasq']['enable']) &&
530
			    ($dhcp_enabled_count == 0) &&
531
			    (isset($config['dnsmasq']['regdhcp']) ||
532
			    isset($config['dnsmasq']['regdhcpstatic']) ||
533
			    isset($config['dnsmasq']['dhcpfirst']))) {
534
				$input_errors[] = gettext(
535
				    "DHCP Registration features in the DNS Forwarder are active and require at least one enabled DHCP Server.");
536
			}
537
			if (isset($config['unbound']['enable']) &&
538
			    ($dhcp_enabled_count == 0) &&
539
			    (isset($config['unbound']['regdhcp']) ||
540
			    isset($config['unbound']['regdhcpstatic']))) {
541
				$input_errors[] = gettext(
542
				    "DHCP Registration features in the DNS Resolver are active and require at least one enabled DHCP Server.");
543
			}
544
		}
545
	}
546

    
547
	// If nothing is wrong so far, and we have range from and to, then check conditions related to the values of range from and to.
548
	if (!$input_errors && $_POST['range_from'] && $_POST['range_to']) {
549
		/* make sure the range lies within the current subnet */
550
		if (ip_greater_than($_POST['range_from'], $_POST['range_to'])) {
551
			$input_errors[] = gettext("The range is invalid (first element higher than second element).");
552
		}
553

    
554
		if (!is_inrange_v4($_POST['range_from'], $subnet_start, $subnet_end) ||
555
			!is_inrange_v4($_POST['range_to'], $subnet_start, $subnet_end)) {
556
			$input_errors[] = gettext("The specified range lies outside of the current subnet.");
557
		}
558

    
559
		if (is_numeric($pool) || ($act == "newpool")) {
560
			if (is_inrange_v4($_POST['range_from'],
561
				$config['dhcpd'][$if]['range']['from'],
562
				$config['dhcpd'][$if]['range']['to']) ||
563
				is_inrange_v4($_POST['range_to'],
564
				$config['dhcpd'][$if]['range']['from'],
565
				$config['dhcpd'][$if]['range']['to'])) {
566
				$input_errors[] = gettext("The specified range must not be within the DHCP range for this interface.");
567
			}
568
		}
569

    
570
		foreach ($a_pools as $id => $p) {
571
			if (is_numeric($pool) && ($id == $pool)) {
572
				continue;
573
			}
574

    
575
			if (is_inrange_v4($_POST['range_from'],
576
				$p['range']['from'], $p['range']['to']) ||
577
				is_inrange_v4($_POST['range_to'],
578
				$p['range']['from'], $p['range']['to'])) {
579
				$input_errors[] = gettext("The specified range must not be within the range configured on a DHCP pool for this interface.");
580
				break;
581
			}
582
		}
583

    
584
		if (is_array($a_maps)) {
585
			foreach ($a_maps as $map) {
586
				if (empty($map['ipaddr'])) {
587
					continue;
588
				}
589
				if (is_inrange_v4($map['ipaddr'], $_POST['range_from'], $_POST['range_to'])) {
590
					$input_errors[] = sprintf(gettext("The DHCP range cannot overlap any static DHCP mappings."));
591
					break;
592
				}
593
			}
594
		}
595
	}
596

    
597
	if (!$input_errors) {
598
		if (!is_numeric($pool)) {
599
			if ($act == "newpool") {
600
				$dhcpdconf = array();
601
			} else {
602
				if (!is_array($config['dhcpd'])) {
603
					$config['dhcpd']= array();
604
				}
605
				if (!is_array($config['dhcpd'][$if])) {
606
					$config['dhcpd'][$if] = array();
607
				}
608
				$dhcpdconf = $config['dhcpd'][$if];
609
			}
610
		} else {
611
			if (is_array($a_pools[$pool])) {
612
				$dhcpdconf = $a_pools[$pool];
613
			} else {
614
				// Someone specified a pool but it doesn't exist. Punt.
615
				header("Location: services_dhcp.php");
616
				exit;
617
			}
618
		}
619
		if (!is_array($dhcpdconf)) {
620
			$dhcpdconf = array();
621
		}
622
		if (!is_array($dhcpdconf['range'])) {
623
			$dhcpdconf['range'] = array();
624
		}
625

    
626
		$dhcpd_enable_changed = false;
627

    
628
		// Global Options
629
		if (!is_numeric($pool) && !($act == "newpool")) {
630
			$old_dhcpd_enable = isset($dhcpdconf['enable']);
631
			$new_dhcpd_enable = ($_POST['enable']) ? true : false;
632
			if ($old_dhcpd_enable != $new_dhcpd_enable) {
633
				/* DHCP has been enabled or disabled. The pf ruleset will need to be rebuilt to allow or disallow DHCP. */
634
				$dhcpd_enable_changed = true;
635
			}
636

    
637
			$dhcpdconf['enable'] = $new_dhcpd_enable;
638
			$dhcpdconf['staticarp'] = ($_POST['staticarp']) ? true : false;
639
			$previous = $dhcpdconf['failover_peerip'];
640
			if ($previous != $_POST['failover_peerip']) {
641
				mwexec("/bin/rm -rf /var/dhcpd/var/db/*");
642
			}
643

    
644
			$dhcpdconf['failover_peerip'] = $_POST['failover_peerip'];
645
			// dhcpleaseinlocaltime is global to all interfaces. So update the setting on all interfaces.
646
			foreach ($config['dhcpd'] as &$dhcpdifitem) {
647
				$dhcpdifitem['dhcpleaseinlocaltime'] = $_POST['dhcpleaseinlocaltime'];
648
			}
649
		} else {
650
			// Options that exist only in pools
651
			$dhcpdconf['descr'] = $_POST['descr'];
652
		}
653

    
654
		// Options that can be global or per-pool.
655
		$dhcpdconf['range']['from'] = $_POST['range_from'];
656
		$dhcpdconf['range']['to'] = $_POST['range_to'];
657
		$dhcpdconf['defaultleasetime'] = $_POST['deftime'];
658
		$dhcpdconf['maxleasetime'] = $_POST['maxtime'];
659
		$dhcpdconf['netmask'] = $_POST['netmask'];
660

    
661
		unset($dhcpdconf['winsserver']);
662
		if ($_POST['wins1']) {
663
			$dhcpdconf['winsserver'][] = $_POST['wins1'];
664
		}
665
		if ($_POST['wins2']) {
666
			$dhcpdconf['winsserver'][] = $_POST['wins2'];
667
		}
668

    
669
		unset($dhcpdconf['dnsserver']);
670
		if ($_POST['dns1']) {
671
			$dhcpdconf['dnsserver'][] = $_POST['dns1'];
672
		}
673
		if ($_POST['dns2']) {
674
			$dhcpdconf['dnsserver'][] = $_POST['dns2'];
675
		}
676
		if ($_POST['dns3']) {
677
			$dhcpdconf['dnsserver'][] = $_POST['dns3'];
678
		}
679
		if ($_POST['dns4']) {
680
			$dhcpdconf['dnsserver'][] = $_POST['dns4'];
681
		}
682

    
683
		$dhcpdconf['gateway'] = $_POST['gateway'];
684
		$dhcpdconf['domain'] = $_POST['domain'];
685
		$dhcpdconf['domainsearchlist'] = $_POST['domainsearchlist'];
686
		$dhcpdconf['ignorebootp'] = ($_POST['ignorebootp']) ? true : false;
687

    
688
		if (in_array($_POST['denyunknown'], array("enabled", "class"))) {
689
			$dhcpdconf['denyunknown'] = $_POST['denyunknown'];
690
		} else {
691
			unset($dhcpdconf['denyunknown']);
692
		}
693

    
694
		$dhcpdconf['ignoreclientuids'] = ($_POST['ignoreclientuids']) ? true : false;
695
		$dhcpdconf['nonak'] = ($_POST['nonak']) ? true : false;
696
		$dhcpdconf['ddnsdomain'] = $_POST['ddnsdomain'];
697
		$dhcpdconf['ddnsdomainprimary'] = $_POST['ddnsdomainprimary'];
698
		$dhcpdconf['ddnsdomainsecondary'] = (!empty($_POST['ddnsdomainsecondary'])) ? $_POST['ddnsdomainsecondary'] : ''; 
699
		$dhcpdconf['ddnsdomainkeyname'] = $_POST['ddnsdomainkeyname'];
700
		$dhcpdconf['ddnsdomainkeyalgorithm'] = $_POST['ddnsdomainkeyalgorithm'];
701
		$dhcpdconf['ddnsdomainkey'] = $_POST['ddnsdomainkey'];
702
		$dhcpdconf['ddnsupdate'] = ($_POST['ddnsupdate']) ? true : false;
703
		$dhcpdconf['ddnsforcehostname'] = ($_POST['ddnsforcehostname']) ? true : false;
704
		$dhcpdconf['mac_allow'] = $_POST['mac_allow'];
705
		$dhcpdconf['mac_deny'] = $_POST['mac_deny'];
706
		$dhcpdconf['ddnsclientupdates'] = $_POST['ddnsclientupdates'];
707

    
708
		unset($dhcpdconf['ntpserver']);
709
		if ($_POST['ntp1']) {
710
			$dhcpdconf['ntpserver'][] = $_POST['ntp1'];
711
		}
712
		if ($_POST['ntp2']) {
713
			$dhcpdconf['ntpserver'][] = $_POST['ntp2'];
714
		}
715
		if ($_POST['ntp3']) {
716
			$dhcpdconf['ntpserver'][] = $_POST['ntp3'];
717
		}
718

    
719
		$dhcpdconf['tftp'] = $_POST['tftp'];
720
		$dhcpdconf['ldap'] = $_POST['ldap'];
721
		$dhcpdconf['netboot'] = ($_POST['netboot']) ? true : false;
722
		$dhcpdconf['nextserver'] = $_POST['nextserver'];
723
		$dhcpdconf['filename'] = $_POST['filename'];
724
		$dhcpdconf['filename32'] = $_POST['filename32'];
725
		$dhcpdconf['filename64'] = $_POST['filename64'];
726
		$dhcpdconf['filename32arm'] = $_POST['filename32arm'];
727
		$dhcpdconf['filename64arm'] = $_POST['filename64arm'];
728
		$dhcpdconf['uefihttpboot'] = $_POST['uefihttpboot'];
729
		$dhcpdconf['rootpath'] = $_POST['rootpath'];
730
		unset($dhcpdconf['statsgraph']);
731
		if ($_POST['statsgraph']) {
732
			$dhcpdconf['statsgraph'] = $_POST['statsgraph'];
733
			enable_rrd_graphing();
734
		}
735
		unset($dhcpdconf['disablepingcheck']);
736
		if ($_POST['disablepingcheck']) {
737
			$dhcpdconf['disablepingcheck'] = $_POST['disablepingcheck'];
738
		}
739

    
740
		// Handle the custom options rowhelper
741
		if (isset($dhcpdconf['numberoptions']['item'])) {
742
			unset($dhcpdconf['numberoptions']['item']);
743
		}
744

    
745
		$dhcpdconf['numberoptions'] = $numberoptions;
746

    
747
		if (is_numeric($pool) && is_array($a_pools[$pool])) {
748
			$a_pools[$pool] = $dhcpdconf;
749
		} elseif ($act == "newpool") {
750
			$a_pools[] = $dhcpdconf;
751
		} else {
752
			$config['dhcpd'][$if] = $dhcpdconf;
753
		}
754

    
755
		// OMAPI Settings
756
		if ($_POST['omapi_port'] == ""){
757
			unset($dhcpdconf['omapi_port']);
758
			unset($dhcpdconf['omapi_key']);
759
			unset($dhcpdconf['omapi_key_algorithm']);
760

    
761
			unset($pconfig['omapi_port']);
762
			unset($pconfig['omapi_key']);
763
			unset($pconfig['omapi_key_algorithm']);
764
		} else {
765
			$dhcpdconf['omapi_port'] = $_POST['omapi_port'];
766
			$dhcpdconf['omapi_key'] = $_POST['omapi_key'];
767
			$dhcpdconf['omapi_key_algorithm'] = $_POST['omapi_key_algorithm'];
768
		}
769

    
770
		write_config(gettext("DHCP Server - Settings changed for interface " . strtoupper($if)));
771
	}
772
}
773

    
774
if ((isset($_POST['save']) || isset($_POST['apply'])) && (!$input_errors)) {
775
	$changes_applied = true;
776
	$retval = 0;
777
	$retvaldhcp = 0;
778
	$retvaldns = 0;
779
	/* dnsmasq_configure calls dhcpd_configure */
780
	/* no need to restart dhcpd twice */
781
	if (isset($config['dnsmasq']['enable']) && isset($config['dnsmasq']['regdhcpstatic']))	{
782
		$retvaldns |= services_dnsmasq_configure();
783
		if ($retvaldns == 0) {
784
			clear_subsystem_dirty('hosts');
785
			clear_subsystem_dirty('staticmaps');
786
		}
787
	} else if (isset($config['unbound']['enable']) && isset($config['unbound']['regdhcpstatic'])) {
788
		$retvaldns |= services_unbound_configure();
789
		if ($retvaldns == 0) {
790
			clear_subsystem_dirty('unbound');
791
			clear_subsystem_dirty('hosts');
792
			clear_subsystem_dirty('staticmaps');
793
		}
794
	} else {
795
		$retvaldhcp |= services_dhcpd_configure();
796
		if ($retvaldhcp == 0) {
797
			clear_subsystem_dirty('staticmaps');
798
		}
799
	}
800
	/* BIND package - Bug #3710 */
801
	if (!function_exists('is_package_installed')) {
802
		require_once('pkg-utils.inc');
803
	}
804
	if (is_package_installed('pfSense-pkg-bind') && isset($config['installedpackages']['bind']['config'][0]['enable_bind'])) {
805
		$reloadbind = false;
806
		if (is_array($config['installedpackages']['bindzone'])) {
807
			$bindzone = $config['installedpackages']['bindzone']['config'];
808
		} else {
809
			$bindzone = array();
810
		}
811
		for ($x = 0; $x < sizeof($bindzone); $x++) {
812
			$zone = $bindzone[$x];
813
			if ($zone['regdhcpstatic'] == 'on') {
814
				$reloadbind = true;
815
				break;
816
			}
817
		}
818
		if ($reloadbind === true) {
819
			if (file_exists("/usr/local/pkg/bind.inc")) {
820
				require_once("/usr/local/pkg/bind.inc");
821
				bind_sync();
822
			}
823
		}
824
	}
825
	if ($dhcpd_enable_changed) {
826
		$retvalfc |= filter_configure();
827
	}
828

    
829
	if ($retvaldhcp == 1 || $retvaldns == 1 || $retvalfc == 1) {
830
		$retval = 1;
831
	}
832
}
833

    
834
if ($act == "delpool") {
835
	if ($a_pools[$_POST['id']]) {
836
		unset($a_pools[$_POST['id']]);
837
		write_config("DHCP Server pool deleted");
838
		header("Location: services_dhcp.php?if={$if}");
839
		exit;
840
	}
841
}
842

    
843
if ($act == "del") {
844
	if (isset($a_maps[$_POST['id']])) {
845
		/* Remove static ARP entry, if necessary */
846
		if (isset($a_maps[$_POST['id']]['arp_table_static_entry'])) {
847
			mwexec("/usr/sbin/arp -d " . escapeshellarg($a_maps[$_POST['id']]['ipaddr']));
848
		}
849
		unset($a_maps[$_POST['id']]);
850
		write_config("DHCP Server static map deleted");
851
		if (isset($config['dhcpd'][$if]['enable'])) {
852
			mark_subsystem_dirty('staticmaps');
853
			if (isset($config['dnsmasq']['enable']) && isset($config['dnsmasq']['regdhcpstatic'])) {
854
				mark_subsystem_dirty('hosts');
855
			}
856
		}
857

    
858
		header("Location: services_dhcp.php?if={$if}");
859
		exit;
860
	}
861
}
862

    
863
// Build an HTML table that can be inserted into a Form_StaticText element
864
function build_pooltable() {
865
	global $a_pools, $if;
866

    
867
	$pooltbl =	'<div class="table-responsive">';
868
	$pooltbl .=		'<table class="table table-striped table-hover table-condensed">';
869
	$pooltbl .=			'<thead>';
870
	$pooltbl .=				'<tr>';
871
	$pooltbl .=					'<th>' . gettext("Pool Start") . '</th>';
872
	$pooltbl .=					'<th>' . gettext("Pool End") . '</th>';
873
	$pooltbl .=					'<th>' . gettext("Description") . '</th>';
874
	$pooltbl .=					'<th>' . gettext("Actions") . '</th>';
875
	$pooltbl .=				'</tr>';
876
	$pooltbl .=			'</thead>';
877
	$pooltbl .=			'<tbody>';
878

    
879
	if (is_array($a_pools)) {
880
		$i = 0;
881
		foreach ($a_pools as $poolent) {
882
			if (!empty($poolent['range']['from']) && !empty($poolent['range']['to'])) {
883
				$pooltbl .= '<tr>';
884
				$pooltbl .= '<td ondblclick="document.location=\'services_dhcp.php?if=' . htmlspecialchars($if) . '&pool=' . $i . '\';">' .
885
							htmlspecialchars($poolent['range']['from']) . '</td>';
886

    
887
				$pooltbl .= '<td ondblclick="document.location=\'services_dhcp.php?if=' . htmlspecialchars($if) . '&pool=' . $i . '\';">' .
888
							htmlspecialchars($poolent['range']['to']) . '</td>';
889

    
890
				$pooltbl .= '<td ondblclick="document.location=\'services_dhcp.php?if=' . htmlspecialchars($if) . '&pool=' . $i . '\';">' .
891
							htmlspecialchars($poolent['descr']) . '</td>';
892

    
893
				$pooltbl .= '<td><a class="fa fa-pencil" title="'. gettext("Edit pool") . '" href="services_dhcp.php?if=' . htmlspecialchars($if) . '&pool=' . $i . '"></a>';
894

    
895
				$pooltbl .= ' <a class="fa fa-trash" title="'. gettext("Delete pool") . '" href="services_dhcp.php?if=' . htmlspecialchars($if) . '&act=delpool&id=' . $i . '" usepost></a></td>';
896
				$pooltbl .= '</tr>';
897
			}
898
		$i++;
899
		}
900
	}
901

    
902
	$pooltbl .=			'</tbody>';
903
	$pooltbl .=		'</table>';
904
	$pooltbl .= '</div>';
905

    
906
	return($pooltbl);
907
}
908

    
909
$pgtitle = array(gettext("Services"), gettext("DHCP Server"));
910
$pglinks = array("", "services_dhcp.php");
911

    
912
if (!empty($if) && isset($iflist[$if])) {
913
	$pgtitle[] = $iflist[$if];
914
	$pglinks[] = "@self";
915
}
916
$shortcut_section = "dhcp";
917

    
918
include("head.inc");
919

    
920
if ($input_errors) {
921
	print_input_errors($input_errors);
922
}
923

    
924
if ($changes_applied) {
925
	print_apply_result_box($retval);
926
}
927

    
928
if (is_subsystem_dirty('staticmaps')) {
929
	print_apply_box(gettext("The static mapping configuration has been changed.") . "<br />" . gettext("The changes must be applied for them to take effect."));
930
}
931

    
932
/* active tabs */
933
$tab_array = array();
934
$tabscounter = 0;
935
$i = 0;
936
$have_small_subnet = false;
937

    
938
foreach ($iflist as $ifent => $ifname) {
939
	$oc = $config['interfaces'][$ifent];
940

    
941
	/* Not static IPv4 or subnet >= 31 */
942
	if ($oc['subnet'] >= 31) {
943
		$have_small_subnet = true;
944
		$example_name = $ifname;
945
		$example_cidr = $oc['subnet'];
946
		continue;
947
	}
948
	if (!is_ipaddrv4($oc['ipaddr']) || empty($oc['subnet'])) {
949
		continue;
950
	}
951

    
952
	if ($ifent == $if) {
953
		$active = true;
954
	} else {
955
		$active = false;
956
	}
957

    
958
	$tab_array[] = array($ifname, $active, "services_dhcp.php?if={$ifent}");
959
	$tabscounter++;
960
}
961

    
962
if ($tabscounter == 0) {
963
	if ($have_small_subnet) {
964
		$sentence2 = sprintf(gettext('%1$s has a CIDR mask of %2$s, which does not contain enough addresses.'), htmlspecialchars($example_name), htmlspecialchars($example_cidr));
965
	} else {
966
		$sentence2 = gettext("This system has no interfaces configured with a static IPv4 address.");
967
	}
968
	print_info_box(gettext("The DHCP Server requires a static IPv4 subnet large enough to serve addresses to clients.") . " " . $sentence2);
969
	include("foot.inc");
970
	exit;
971
}
972

    
973
display_top_tabs($tab_array);
974

    
975
$form = new Form();
976

    
977
$section = new Form_Section('General Options');
978

    
979
if (!is_numeric($pool) && !($act == "newpool")) {
980
	if (isset($config['dhcrelay']['enable'])) {
981
		$section->addInput(new Form_Checkbox(
982
			'enable',
983
			'Enable',
984
			gettext("DHCP Relay is currently enabled. DHCP Server canot be enabled while the DHCP Relay is enabled on any interface."),
985
			$pconfig['enable']
986
		))->setAttribute('disabled', true);
987
	} else {
988
		$section->addInput(new Form_Checkbox(
989
			'enable',
990
			'Enable',
991
			sprintf(gettext("Enable DHCP server on %s interface"), htmlspecialchars($iflist[$if])),
992
			$pconfig['enable']
993
		));
994
	}
995
} else {
996
	print_info_box(gettext('Editing pool-specific options. To return to the Interface, click its tab above.'), 'info', false);
997
}
998

    
999
$section->addInput(new Form_Checkbox(
1000
	'ignorebootp',
1001
	'BOOTP',
1002
	'Ignore BOOTP queries',
1003
	$pconfig['ignorebootp']
1004
));
1005

    
1006
$section->addInput(new Form_Select(
1007
	'denyunknown',
1008
	'Deny unknown clients',
1009
	$pconfig['denyunknown'],
1010
	array(
1011
		"disabled" => "Allow all clients",
1012
		"enabled" => "Allow known clients from any interface",
1013
		"class" => "Allow known clients from only this interface",
1014
	)
1015
))->setHelp('When set to %3$sAllow all clients%4$s, any DHCP client will get an IP address within this scope/range on this interface. '.
1016
	'If set to %3$sAllow known clients from any interface%4$s, any DHCP client with a MAC address listed on %1$s%3$sany%4$s%2$s scope(s)/interface(s) will get an IP address. ' .
1017
	'If set to %3$sAllow known clients from only this interface%4$s, only MAC addresses listed below (i.e. for this interface) will get an IP address within this scope/range.',
1018
	'<i>', '</i>', '<b>', '</b>');
1019

    
1020
$section->addInput(new Form_Checkbox(
1021
	'nonak',
1022
	'Ignore denied clients',
1023
	'Denied clients will be ignored rather than rejected.',
1024
	$pconfig['nonak']
1025
))->setHelp("This option is not compatible with failover and cannot be enabled when a Failover Peer IP address is configured.");
1026

    
1027
$section->addInput(new Form_Checkbox(
1028
	'ignoreclientuids',
1029
	'Ignore client identifiers',
1030
	'If a client includes a unique identifier in its DHCP request, that UID will not be recorded in its lease.',
1031
	$pconfig['ignoreclientuids']
1032
))->setHelp("This option may be useful when a client can dual boot using different client identifiers but the same hardware (MAC) address.  Note that the resulting server behavior violates the official DHCP specification.");
1033

    
1034

    
1035
if (is_numeric($pool) || ($act == "newpool")) {
1036
	$section->addInput(new Form_Input(
1037
		'descr',
1038
		'Pool Description',
1039
		'text',
1040
		$pconfig['descr']
1041
	));
1042
}
1043

    
1044
$section->addInput(new Form_StaticText(
1045
	'Subnet',
1046
	gen_subnet($ifcfgip, $ifcfgsn)
1047
));
1048

    
1049
$section->addInput(new Form_StaticText(
1050
	'Subnet mask',
1051
	gen_subnet_mask($ifcfgsn)
1052
));
1053

    
1054
// Compose a string to display the required address ranges
1055
$rangestr = ip_after($subnet_start) . ' - ' . ip_before($subnet_end);
1056

    
1057
if (is_numeric($pool) || ($act == "newpool")) {
1058
	$rangestr .= '<br />' . gettext('In-use DHCP Pool Ranges:');
1059
	if (is_array($config['dhcpd'][$if]['range'])) {
1060
		$rangestr .= '<br />' . $config['dhcpd'][$if]['range']['from'] . ' - ' . $config['dhcpd'][$if]['range']['to'];
1061
	}
1062

    
1063
	foreach ($a_pools as $p) {
1064
		if (is_array($p['range'])) {
1065
			$rangestr .= '<br />' . $p['range']['from'] . ' - ' . $p['range']['to'];
1066
		}
1067
	}
1068
}
1069

    
1070
$section->addInput(new Form_StaticText(
1071
	'Available range',
1072
	$rangestr
1073
));
1074

    
1075
$group = new Form_Group('*Range');
1076

    
1077
$group->add(new Form_IpAddress(
1078
	'range_from',
1079
	null,
1080
	$pconfig['range_from'],
1081
	'V4'
1082
))->setHelp('From');
1083

    
1084
$group->add(new Form_IpAddress(
1085
	'range_to',
1086
	null,
1087
	$pconfig['range_to'],
1088
	'V4'
1089
))->setHelp('To');
1090

    
1091
$section->add($group);
1092

    
1093
$form->add($section);
1094

    
1095
if (!is_numeric($pool) && !($act == "newpool")) {
1096
	$section = new Form_Section('Additional Pools');
1097

    
1098
	$btnaddpool = new Form_Button(
1099
		'btnaddpool',
1100
		'Add pool',
1101
		'services_dhcp.php?if=' . htmlspecialchars($if) . '&act=newpool',
1102
		'fa-plus'
1103
	);
1104
	$btnaddpool->addClass('btn-success');
1105

    
1106
	$section->addInput(new Form_StaticText(
1107
		'Add',
1108
		$btnaddpool
1109
	))->setHelp('If additional pools of addresses are needed inside of this subnet outside the above Range, they may be specified here.');
1110

    
1111
	if (is_array($a_pools)) {
1112
		$section->addInput(new Form_StaticText(
1113
			null,
1114
			build_pooltable()
1115
		));
1116
	}
1117

    
1118
	$form->add($section);
1119
}
1120

    
1121
$section = new Form_Section('Servers');
1122

    
1123
$section->addInput(new Form_IpAddress(
1124
	'wins1',
1125
	'WINS servers',
1126
	$pconfig['wins1'],
1127
	'V4'
1128
))->setAttribute('placeholder', 'WINS Server 1');
1129

    
1130
$section->addInput(new Form_IpAddress(
1131
	'wins2',
1132
	null,
1133
	$pconfig['wins2'],
1134
	'V4'
1135
))->setAttribute('placeholder', 'WINS Server 2');
1136

    
1137
for ($idx=1; $idx<=4; $idx++) {
1138
	$section->addInput(new Form_IpAddress(
1139
		'dns' . $idx,
1140
		($idx == 1) ? 'DNS servers':null,
1141
		$pconfig['dns' . $idx],
1142
		'V4'
1143
	))->setAttribute('placeholder', 'DNS Server ' . $idx)->setHelp(($idx == 4) ? 'Leave blank to use the system default DNS servers: this interface\'s IP if DNS Forwarder or Resolver is enabled, otherwise the servers configured on the System / General Setup page.':'');
1144
}
1145

    
1146
$form->add($section);
1147

    
1148
//OMAPI
1149
$section = new Form_Section('OMAPI');
1150

    
1151
$section->addInput(new Form_Input(
1152
	'omapi_port',
1153
	'OMAPI Port',
1154
	'text',
1155
	$pconfig['omapi_port']
1156
))->setAttribute('placeholder', 'OMAPI Port')
1157
  ->setHelp('Set the port that OMAPI will listen on. The default port is 7911, leave blank to disable.' .
1158
	    'Only the first OMAPI configuration is used.');
1159

    
1160
$group = new Form_Group('OMAPI Key');
1161

    
1162
$group->add(new Form_Input(
1163
	'omapi_key',
1164
	'OMAPI Key',
1165
	'text',
1166
	$pconfig['omapi_key']
1167
))->setAttribute('placeholder', 'OMAPI Key')
1168
  ->setHelp('Enter a key matching the selected algorithm<br />to secure connections to the OMAPI endpoint.');
1169

    
1170
$group->add(new Form_Checkbox(
1171
	'omapi_gen_key',
1172
	'',
1173
	'Generate New Key',
1174
	$pconfig['omapi_gen_key']
1175
))->setHelp('Generate a new key based<br />on the selected algorithm.');
1176

    
1177
$section->add($group);
1178

    
1179
$section->addInput(new Form_Select(
1180
	'omapi_key_algorithm',
1181
	'Key Algorithm',
1182
	empty($pconfig['omapi_key_algorithm']) ? 'hmac-sha256' : $pconfig['omapi_key_algorithm'], // Set the default algorithm if not previous defined
1183
	array(
1184
		'hmac-md5' => 'HMAC-MD5 (legacy default)',
1185
		'hmac-sha1' => 'HMAC-SHA1',
1186
		'hmac-sha224' => 'HMAC-SHA224',
1187
		'hmac-sha256' => 'HMAC-SHA256 (current bind9 default)',
1188
		'hmac-sha384' => 'HMAC-SHA384',
1189
		'hmac-sha512' => 'HMAC-SHA512 (most secure)',
1190
	)
1191
))->setHelp('Set the algorithm that OMAPI key will use.');
1192

    
1193
$form->add($section);
1194

    
1195
$section = new Form_Section('Other Options');
1196

    
1197
$section->addInput(new Form_IpAddress(
1198
	'gateway',
1199
	'Gateway',
1200
	$pconfig['gateway'],
1201
	'V4'
1202
))->setPattern('[.a-zA-Z0-9_]+')
1203
  ->setHelp('The default is to use the IP on this interface of the firewall as the gateway. Specify an alternate gateway here if this is not the correct gateway for the network. Type "none" for no gateway assignment.');
1204

    
1205
$section->addInput(new Form_Input(
1206
	'domain',
1207
	'Domain name',
1208
	'text',
1209
	$pconfig['domain']
1210
))->setHelp('The default is to use the domain name of this system as the default domain name provided by DHCP. An alternate domain name may be specified here.');
1211

    
1212
$section->addInput(new Form_Input(
1213
	'domainsearchlist',
1214
	'Domain search list',
1215
	'text',
1216
	$pconfig['domainsearchlist']
1217
))->setHelp('The DHCP server can optionally provide a domain search list. Use the semicolon character as separator.');
1218

    
1219
$section->addInput(new Form_Input(
1220
	'deftime',
1221
	'Default lease time',
1222
	'number',
1223
	$pconfig['deftime']
1224
))->setHelp('This is used for clients that do not ask for a specific expiration time. The default is 7200 seconds.');
1225

    
1226
$section->addInput(new Form_Input(
1227
	'maxtime',
1228
	'Maximum lease time',
1229
	'number',
1230
	$pconfig['maxtime']
1231
))->setHelp('This is the maximum lease time for clients that ask for a specific expiration time. The default is 86400 seconds.');
1232

    
1233
if (!is_numeric($pool) && !($act == "newpool")) {
1234
	$section->addInput(new Form_IpAddress(
1235
		'failover_peerip',
1236
		'Failover peer IP',
1237
		$pconfig['failover_peerip'],
1238
		'V4'
1239
	))->setHelp('Leave blank to disable. Enter the interface IP address of the other machine. Machines must be using CARP. ' .
1240
				'Interface\'s advskew determines whether the DHCPd process is Primary or Secondary. Ensure one machine\'s advskew &lt; 20 (and the other is &gt; 20).');
1241

    
1242
	$section->addInput(new Form_Checkbox(
1243
		'staticarp',
1244
		'Static ARP',
1245
		'Enable Static ARP entries',
1246
		$pconfig['staticarp']
1247
	))->setHelp('This option persists even if DHCP server is disabled. Only the machines listed below will be able to communicate with the firewall on this interface.');
1248

    
1249
	$section->addInput(new Form_Checkbox(
1250
		'dhcpleaseinlocaltime',
1251
		'Time format change',
1252
		'Change DHCP display lease time from UTC to local time',
1253
		$pconfig['dhcpleaseinlocaltime']
1254
	))->setHelp('By default DHCP leases are displayed in UTC time.	By checking this box DHCP lease time will be displayed in local time and set to the time zone selected.' .
1255
				' This will be used for all DHCP interfaces lease time.');
1256

    
1257
	$section->addInput(new Form_Checkbox(
1258
		'statsgraph',
1259
		'Statistics graphs',
1260
		'Enable RRD statistics graphs',
1261
		$pconfig['statsgraph']
1262
	))->setHelp('Enable this to add DHCP leases statistics to the RRD graphs. Disabled by default.');
1263

    
1264
	$section->addInput(new Form_Checkbox(
1265
		'disablepingcheck',
1266
		'Ping check',
1267
		'Disable ping check',
1268
		$pconfig['disablepingcheck']
1269
	))->setHelp('When enabled dhcpd sends a ping to the address being assigned, and if no response has been heard, it assigns the address. Enabled by default.');
1270
}
1271

    
1272
// DDNS
1273
$btnadv = new Form_Button(
1274
	'btnadvdns',
1275
	'Display Advanced',
1276
	null,
1277
	'fa-cog'
1278
);
1279

    
1280
$btnadv->setAttribute('type','button')->addClass('btn-info btn-sm');
1281

    
1282
$section->addInput(new Form_StaticText(
1283
	'Dynamic DNS',
1284
	$btnadv
1285
));
1286

    
1287
$section->addInput(new Form_Checkbox(
1288
	'ddnsupdate',
1289
	null,
1290
	'Enable registration of DHCP client names in DNS',
1291
	$pconfig['ddnsupdate']
1292
));
1293

    
1294
$section->addInput(new Form_Input(
1295
	'ddnsdomain',
1296
	'DDNS Domain',
1297
	'text',
1298
	$pconfig['ddnsdomain']
1299
))->setHelp('Enter the dynamic DNS domain which will be used to register client names in the DNS server.');
1300

    
1301
$section->addInput(new Form_Checkbox(
1302
	'ddnsforcehostname',
1303
	'DDNS Hostnames',
1304
	'Force dynamic DNS hostname to be the same as configured hostname for Static Mappings',
1305
	$pconfig['ddnsforcehostname']
1306
))->setHelp('Default registers host name option supplied by DHCP client.');
1307

    
1308
$section->addInput(new Form_IpAddress(
1309
	'ddnsdomainprimary',
1310
	'Primary DDNS address',
1311
	$pconfig['ddnsdomainprimary'],
1312
	'BOTH'
1313
))->setHelp('Primary domain name server IP address for the dynamic domain name.');
1314

    
1315
$section->addInput(new Form_IpAddress(
1316
	'ddnsdomainsecondary',
1317
	'Secondary DDNS address',
1318
	$pconfig['ddnsdomainsecondary'],
1319
	'BOTH'
1320
))->setHelp('Secondary domain name server IP address for the dynamic domain name.');
1321

    
1322
$section->addInput(new Form_Input(
1323
	'ddnsdomainkeyname',
1324
	'DNS Domain key',
1325
	'text',
1326
	$pconfig['ddnsdomainkeyname']
1327
))->setHelp('Dynamic DNS domain key name which will be used to register client names in the DNS server.');
1328

    
1329
$section->addInput(new Form_Select(
1330
	'ddnsdomainkeyalgorithm',
1331
	'Key algorithm',
1332
	$pconfig['ddnsdomainkeyalgorithm'],
1333
	$ddnsdomainkeyalgorithms
1334
));
1335

    
1336
$section->addInput(new Form_Input(
1337
	'ddnsdomainkey',
1338
	'DNS Domain key secret',
1339
	'text',
1340
	$pconfig['ddnsdomainkey']
1341
))->setAttribute('placeholder', 'Base64 encoded string')
1342
->setHelp('Dynamic DNS domain key secret which will be used to register client names in the DNS server.');
1343

    
1344
$section->addInput(new Form_Select(
1345
	'ddnsclientupdates',
1346
	'DDNS Client Updates',
1347
	$pconfig['ddnsclientupdates'],
1348
	array(
1349
	    'allow' => gettext('Allow'),
1350
	    'deny' => gettext('Deny'),
1351
	    'ignore' => gettext('Ignore'))
1352
))->setHelp('How Forward entries are handled when client indicates they wish to update DNS.  ' .
1353
	    'Allow prevents DHCP from updating Forward entries, Deny indicates that DHCP will ' .
1354
	    'do the updates and the client should not, Ignore specifies that DHCP will do the ' .
1355
	    'update and the client can also attempt the update usually using a different domain name.');
1356

    
1357
// Advanced MAC
1358
$btnadv = new Form_Button(
1359
	'btnadvmac',
1360
	'Display Advanced',
1361
	null,
1362
	'fa-cog'
1363
);
1364

    
1365
$btnadv->setAttribute('type','button')->addClass('btn-info btn-sm');
1366

    
1367
$section->addInput(new Form_StaticText(
1368
	'MAC address control',
1369
	$btnadv
1370
));
1371

    
1372
$section->addInput(new Form_Input(
1373
	'mac_allow',
1374
	'MAC Allow',
1375
	'text',
1376
	$pconfig['mac_allow']
1377
))->setHelp('List of partial MAC addresses to allow, comma separated, no spaces, e.g.: 00:00:00,01:E5:FF');
1378

    
1379
$section->addInput(new Form_Input(
1380
	'mac_deny',
1381
	'MAC Deny',
1382
	'text',
1383
	$pconfig['mac_deny']
1384
))->setHelp('List of partial MAC addresses to deny access, comma separated, no spaces, e.g.: 00:00:00,01:E5:FF');
1385

    
1386
// Advanced NTP
1387
$btnadv = new Form_Button(
1388
	'btnadvntp',
1389
	'Display Advanced',
1390
	null,
1391
	'fa-cog'
1392
);
1393

    
1394
$btnadv->setAttribute('type','button')->addClass('btn-info btn-sm');
1395

    
1396
$section->addInput(new Form_StaticText(
1397
	'NTP',
1398
	$btnadv
1399
));
1400

    
1401
$section->addInput(new Form_IpAddress(
1402
	'ntp1',
1403
	'NTP Server 1',
1404
	$pconfig['ntp1'],
1405
	'HOSTV4'
1406
));
1407

    
1408
$section->addInput(new Form_IpAddress(
1409
	'ntp2',
1410
	'NTP Server 2',
1411
	$pconfig['ntp2'],
1412
	'HOSTV4'
1413
));
1414

    
1415
$section->addInput(new Form_IpAddress(
1416
	'ntp3',
1417
	'NTP Server 3',
1418
	$pconfig['ntp3'],
1419
	'HOSTV4'
1420
));
1421

    
1422
// Advanced TFTP
1423
$btnadv = new Form_Button(
1424
	'btnadvtftp',
1425
	'Display Advanced',
1426
	null,
1427
	'fa-cog'
1428
);
1429

    
1430
$btnadv->setAttribute('type','button')->addClass('btn-info btn-sm');
1431

    
1432
$section->addInput(new Form_StaticText(
1433
	'TFTP',
1434
	$btnadv
1435
));
1436

    
1437
$section->addInput(new Form_Input(
1438
	'tftp',
1439
	'TFTP Server',
1440
	'text',
1441
	$pconfig['tftp']
1442
))->setHelp('Leave blank to disable. Enter a valid IP address, hostname or URL for the TFTP server.');
1443

    
1444
// Advanced LDAP
1445
$btnadv = new Form_Button(
1446
	'btnadvldap',
1447
	'Display Advanced',
1448
	null,
1449
	'fa-cog'
1450
);
1451

    
1452
$btnadv->setAttribute('type','button')->addClass('btn-info btn-sm');
1453

    
1454
$section->addInput(new Form_StaticText(
1455
	'LDAP',
1456
	$btnadv
1457
));
1458

    
1459
$section->addInput(new Form_Input(
1460
	'ldap',
1461
	'LDAP Server URI',
1462
	'text',
1463
	$pconfig['ldap']
1464
))->setHelp('Leave blank to disable. Enter a full URI for the LDAP server in the form ldap://ldap.example.com/dc=example,dc=com ');
1465

    
1466
// Advanced Network Booting options
1467
$btnadv = new Form_Button(
1468
	'btnadvnwkboot',
1469
	'Display Advanced',
1470
	null,
1471
	'fa-cog'
1472
);
1473

    
1474
$btnadv->setAttribute('type','button')->addClass('btn-info btn-sm');
1475

    
1476
$section->addInput(new Form_StaticText(
1477
	'Network Booting',
1478
	$btnadv
1479
));
1480

    
1481
$section->addInput(new Form_Checkbox(
1482
	'netboot',
1483
	'Enable',
1484
	'Enables network booting',
1485
	$pconfig['netboot']
1486
));
1487

    
1488
$section->addInput(new Form_IpAddress(
1489
	'nextserver',
1490
	'Next Server',
1491
	$pconfig['nextserver'],
1492
	'V4'
1493
))->setHelp('Enter the IP address of the next server');
1494

    
1495
$section->addInput(new Form_Input(
1496
	'filename',
1497
	'Default BIOS file name',
1498
	'text',
1499
	$pconfig['filename']
1500
));
1501

    
1502
$section->addInput(new Form_Input(
1503
	'filename32',
1504
	'UEFI 32 bit file name',
1505
	'text',
1506
	$pconfig['filename32']
1507
));
1508

    
1509
$section->addInput(new Form_Input(
1510
	'filename64',
1511
	'UEFI 64 bit file name',
1512
	'text',
1513
	$pconfig['filename64']
1514
));
1515

    
1516
$section->addInput(new Form_Input(
1517
	'filename32arm',
1518
	'ARM 32 bit file name',
1519
	'text',
1520
	$pconfig['filename32arm']
1521
));
1522

    
1523
$section->addInput(new Form_Input(
1524
	'filename64arm',
1525
	'ARM 64 bit file name',
1526
	'text',
1527
	$pconfig['filename64arm']
1528
))->setHelp('Both a filename and a boot server must be configured for this to work! ' .
1529
			'All five filenames and a configured boot server are necessary for UEFI & ARM to work! ');
1530

    
1531
$section->addInput(new Form_Input(
1532
	'uefihttpboot',
1533
	'UEFI HTTPBoot URL',
1534
	'text',
1535
	$pconfig['uefihttpboot']
1536
))->setHelp('string-format: http://(servername)/(firmwarepath)');
1537

    
1538
$section->addInput(new Form_Input(
1539
	'rootpath',
1540
	'Root path',
1541
	'text',
1542
	$pconfig['rootpath']
1543
))->setHelp('string-format: iscsi:(servername):(protocol):(port):(LUN):targetname ');
1544

    
1545
// Advanced Additional options
1546
$btnadv = new Form_Button(
1547
	'btnadvopts',
1548
	'Display Advanced',
1549
	null,
1550
	'fa-cog'
1551
);
1552

    
1553
$btnadv->setAttribute('type','button')->addClass('btn-info btn-sm');
1554

    
1555
$section->addInput(new Form_StaticText(
1556
	'Additional BOOTP/DHCP Options',
1557
	$btnadv
1558
));
1559

    
1560
$form->add($section);
1561

    
1562
$section = new Form_Section('Additional BOOTP/DHCP Options');
1563
$section->addClass('adnlopts');
1564

    
1565
$section->addInput(new Form_StaticText(
1566
	null,
1567
	'<div class="alert alert-info"> ' . gettext('Enter the DHCP option number and the value for each item to include in the DHCP lease information.') . ' ' .
1568
	sprintf(gettext('For a list of available options please visit this %1$s URL%2$s.%3$s'), '<a href="https://www.iana.org/assignments/bootp-dhcp-parameters/" target="_blank">', '</a>', '</div>')
1569
));
1570

    
1571
if (!$pconfig['numberoptions']) {
1572
	$pconfig['numberoptions'] = array();
1573
	$pconfig['numberoptions']['item']  = array(array('number' => '', 'type' => 'text', 'value' => ''));
1574
}
1575

    
1576
$customitemtypes = array(
1577
	'text' => gettext('Text'), 'string' => gettext('String'), 'boolean' => gettext('Boolean'),
1578
	'unsigned integer 8' => gettext('Unsigned 8-bit integer'), 'unsigned integer 16' => gettext('Unsigned 16-bit integer'), 'unsigned integer 32' => gettext('Unsigned 32-bit integer'),
1579
	'signed integer 8' => gettext('Signed 8-bit integer'), 'signed integer 16' => gettext('Signed 16-bit integer'), 'signed integer 32' => gettext('Signed 32-bit integer'), 'ip-address' => gettext('IP address or host')
1580
);
1581

    
1582
$numrows = count($item) -1;
1583
$counter = 0;
1584

    
1585
$numrows = count($pconfig['numberoptions']['item']) -1;
1586

    
1587
foreach ($pconfig['numberoptions']['item'] as $item) {
1588
	$number = $item['number'];
1589
	$itemtype = $item['type'];
1590
	$value = base64_decode($item['value']);
1591

    
1592
	$group = new Form_Group(($counter == 0) ? 'Option':null);
1593
	$group->addClass('repeatable');
1594

    
1595
	$group->add(new Form_Input(
1596
		'number' . $counter,
1597
		null,
1598
		'number',
1599
		$number,
1600
		['min'=>'1', 'max'=>'254']
1601
	))->setHelp($numrows == $counter ? 'Number':null);
1602

    
1603

    
1604
	$group->add(new Form_Select(
1605
		'itemtype' . $counter,
1606
		null,
1607
		$itemtype,
1608
		$customitemtypes
1609
	))->setWidth(3)->setHelp($numrows == $counter ? 'Type':null);
1610

    
1611
	$group->add(new Form_Input(
1612
		'value' . $counter,
1613
		null,
1614
		'text',
1615
		$value
1616
	))->setHelp($numrows == $counter ? 'Value':null);
1617

    
1618
	$group->add(new Form_Button(
1619
		'deleterow' . $counter,
1620
		'Delete',
1621
		null,
1622
		'fa-trash'
1623
	))->addClass('btn-warning');
1624

    
1625
	$section->add($group);
1626

    
1627
	$counter++;
1628
}
1629

    
1630
$section->addInput(new Form_Button(
1631
	'addrow',
1632
	'Add',
1633
	null,
1634
	'fa-plus'
1635
))->addClass('btn-success');
1636

    
1637
$form->add($section);
1638

    
1639
if ($act == "newpool") {
1640
	$form->addGlobal(new Form_Input(
1641
		'act',
1642
		null,
1643
		'hidden',
1644
		'newpool'
1645
	));
1646
}
1647

    
1648
if (is_numeric($pool)) {
1649
	$form->addGlobal(new Form_Input(
1650
		'pool',
1651
		null,
1652
		'hidden',
1653
		$pool
1654
	));
1655
}
1656

    
1657
$form->addGlobal(new Form_Input(
1658
	'if',
1659
	null,
1660
	'hidden',
1661
	$if
1662
));
1663

    
1664
print($form);
1665

    
1666
// DHCP Static Mappings table
1667

    
1668
if (!is_numeric($pool) && !($act == "newpool")) {
1669

    
1670
	// Decide whether display of the Client Id column is needed.
1671
	$got_cid = false;
1672
	if (is_array($a_maps)) {
1673
		foreach ($a_maps as $map) {
1674
			if (!empty($map['cid'])) {
1675
				$got_cid = true;
1676
				break;
1677
			}
1678
		}
1679
	}
1680
?>
1681

    
1682
<div class="panel panel-default">
1683
<?php
1684
	if (is_array($a_maps) && (count($a_maps) > 0)) {
1685
		$title = sprintf(gettext('DHCP Static Mappings for this Interface (total: %d)'), count($a_maps));
1686
	} else {
1687
		$title = gettext("DHCP Static Mappings for this Interface");
1688
	}
1689
?>
1690
	<div class="panel-heading"><h2 class="panel-title"><?=$title?></h2></div>
1691
	<div class="table-responsive">
1692
			<table class="table table-striped table-hover table-condensed sortable-theme-bootstrap" data-sortable>
1693
				<thead>
1694
					<tr>
1695
						<th><?=gettext("Static ARP")?></th>
1696
						<th><?=gettext("MAC address")?></th>
1697
<?php
1698
	if ($got_cid):
1699
?>
1700
						<th><?=gettext("Client Id")?></th>
1701
<?php
1702
	endif;
1703
?>
1704
						<th><?=gettext("IP address")?></th>
1705
						<th><?=gettext("Hostname")?></th>
1706
						<th><?=gettext("Description")?></th>
1707
						<th></th>
1708
					</tr>
1709
				</thead>
1710
<?php
1711
	if (is_array($a_maps)) {
1712
		$i = 0;
1713
?>
1714
				<tbody>
1715
<?php
1716
		foreach ($a_maps as $mapent) {
1717
?>
1718
					<tr>
1719
						<td class="text-center" ondblclick="document.location='services_dhcp_edit.php?if=<?=htmlspecialchars($if)?>&amp;id=<?=$i?>';">
1720
							<?php if (isset($mapent['arp_table_static_entry'])): ?>
1721
								<i class="fa fa-check"></i>
1722
							<?php endif; ?>
1723
						</td>
1724
						<td ondblclick="document.location='services_dhcp_edit.php?if=<?=htmlspecialchars($if)?>&amp;id=<?=$i?>';">
1725
							<?=htmlspecialchars($mapent['mac'])?>
1726
						</td>
1727
<?php
1728
			if ($got_cid):
1729
?>
1730
						<td ondblclick="document.location='services_dhcp_edit.php?if=<?=htmlspecialchars($if)?>&amp;id=<?=$i?>';">
1731
							<?=htmlspecialchars($mapent['cid'])?>
1732
						</td>
1733
<?php
1734
			endif;
1735
?>
1736
						<td ondblclick="document.location='services_dhcp_edit.php?if=<?=htmlspecialchars($if)?>&amp;id=<?=$i?>';">
1737
							<?=htmlspecialchars($mapent['ipaddr'])?>
1738
						</td>
1739
						<td ondblclick="document.location='services_dhcp_edit.php?if=<?=htmlspecialchars($if)?>&amp;id=<?=$i?>';">
1740
							<?=htmlspecialchars($mapent['hostname'])?>
1741
						</td>
1742
						<td ondblclick="document.location='services_dhcp_edit.php?if=<?=htmlspecialchars($if)?>&amp;id=<?=$i?>';">
1743
							<?=htmlspecialchars($mapent['descr'])?>
1744
						</td>
1745
						<td>
1746
							<a class="fa fa-pencil"	title="<?=gettext('Edit static mapping')?>"	href="services_dhcp_edit.php?if=<?=htmlspecialchars($if)?>&amp;id=<?=$i?>"></a>
1747
							<a class="fa fa-trash"	title="<?=gettext('Delete static mapping')?>"	href="services_dhcp.php?if=<?=htmlspecialchars($if)?>&amp;act=del&amp;id=<?=$i?>" usepost></a>
1748
						</td>
1749
					</tr>
1750
<?php
1751
		$i++;
1752
		}
1753
?>
1754
				</tbody>
1755
<?php
1756
	}
1757
?>
1758
		</table>
1759
	</div>
1760
</div>
1761

    
1762
<nav class="action-buttons">
1763
	<a href="services_dhcp_edit.php?if=<?=htmlspecialchars($if)?>" class="btn btn-sm btn-success">
1764
		<i class="fa fa-plus icon-embed-btn"></i>
1765
		<?=gettext("Add")?>
1766
	</a>
1767
</nav>
1768
<?php
1769
}
1770
?>
1771

    
1772
<script type="text/javascript">
1773
//<![CDATA[
1774
events.push(function() {
1775

    
1776
	// Show advanced DNS options ======================================================================================
1777
	var showadvdns = false;
1778

    
1779
	function show_advdns(ispageload) {
1780
		var text;
1781
		// On page load decide the initial state based on the data.
1782
		if (ispageload) {
1783
<?php
1784
			if (!$pconfig['ddnsupdate'] &&
1785
				!$pconfig['ddnsforcehostname'] &&
1786
				empty($pconfig['ddnsdomain']) &&
1787
				empty($pconfig['ddnsdomainprimary']) &&
1788
				empty($pconfig['ddnsdomainsecondary']) &&
1789
			    empty($pconfig['ddnsdomainkeyname']) &&
1790
			    (empty($pconfig['ddnsdomainkeyalgorithm']) || ($pconfig['ddnsdomainkeyalgorithm'] == "hmac-md5")) &&
1791
			    (empty($pconfig['ddnsclientupdates']) || ($pconfig['ddnsclientupdates'] == "allow")) &&
1792
			    empty($pconfig['ddnsdomainkey'])) {
1793
				$showadv = false;
1794
			} else {
1795
				$showadv = true;
1796
			}
1797
?>
1798
			showadvdns = <?php if ($showadv) {echo 'true';} else {echo 'false';} ?>;
1799
		} else {
1800
			// It was a click, swap the state.
1801
			showadvdns = !showadvdns;
1802
		}
1803

    
1804
		hideCheckbox('ddnsupdate', !showadvdns);
1805
		hideInput('ddnsdomain', !showadvdns);
1806
		hideCheckbox('ddnsforcehostname', !showadvdns);
1807
		hideInput('ddnsdomainprimary', !showadvdns);
1808
		hideInput('ddnsdomainsecondary', !showadvdns);
1809
		hideInput('ddnsdomainkeyname', !showadvdns);
1810
		hideInput('ddnsdomainkeyalgorithm', !showadvdns);
1811
		hideInput('ddnsdomainkey', !showadvdns);
1812
		hideInput('ddnsclientupdates', !showadvdns);
1813

    
1814
		if (showadvdns) {
1815
			text = "<?=gettext('Hide Advanced');?>";
1816
		} else {
1817
			text = "<?=gettext('Display Advanced');?>";
1818
		}
1819
		$('#btnadvdns').html('<i class="fa fa-cog"></i> ' + text);
1820
	}
1821

    
1822
	$('#btnadvdns').click(function(event) {
1823
		show_advdns();
1824
	});
1825

    
1826
	// Show advanced MAC options ======================================================================================
1827
	var showadvmac = false;
1828

    
1829
	function show_advmac(ispageload) {
1830
		var text;
1831
		// On page load decide the initial state based on the data.
1832
		if (ispageload) {
1833
<?php
1834
			if (empty($pconfig['mac_allow']) && empty($pconfig['mac_deny'])) {
1835
				$showadv = false;
1836
			} else {
1837
				$showadv = true;
1838
			}
1839
?>
1840
			showadvmac = <?php if ($showadv) {echo 'true';} else {echo 'false';} ?>;
1841
		} else {
1842
			// It was a click, swap the state.
1843
			showadvmac = !showadvmac;
1844
		}
1845

    
1846
		hideInput('mac_allow', !showadvmac);
1847
		hideInput('mac_deny', !showadvmac);
1848

    
1849
		if (showadvmac) {
1850
			text = "<?=gettext('Hide Advanced');?>";
1851
		} else {
1852
			text = "<?=gettext('Display Advanced');?>";
1853
		}
1854
		$('#btnadvmac').html('<i class="fa fa-cog"></i> ' + text);
1855
	}
1856

    
1857
	$('#btnadvmac').click(function(event) {
1858
		show_advmac();
1859
	});
1860

    
1861
	// Show advanced NTP options ======================================================================================
1862
	var showadvntp = false;
1863

    
1864
	function show_advntp(ispageload) {
1865
		var text;
1866
		// On page load decide the initial state based on the data.
1867
		if (ispageload) {
1868
<?php
1869
			if (empty($pconfig['ntp1']) && empty($pconfig['ntp2']) && empty($pconfig['ntp3']) ) {
1870
				$showadv = false;
1871
			} else {
1872
				$showadv = true;
1873
			}
1874
?>
1875
			showadvntp = <?php if ($showadv) {echo 'true';} else {echo 'false';} ?>;
1876
		} else {
1877
			// It was a click, swap the state.
1878
			showadvntp = !showadvntp;
1879
		}
1880

    
1881
		hideInput('ntp1', !showadvntp);
1882
		hideInput('ntp2', !showadvntp);
1883
		hideInput('ntp3', !showadvntp);
1884

    
1885
		if (showadvntp) {
1886
			text = "<?=gettext('Hide Advanced');?>";
1887
		} else {
1888
			text = "<?=gettext('Display Advanced');?>";
1889
		}
1890
		$('#btnadvntp').html('<i class="fa fa-cog"></i> ' + text);
1891
	}
1892

    
1893
	$('#btnadvntp').click(function(event) {
1894
		show_advntp();
1895
	});
1896

    
1897
	// Show advanced TFTP options ======================================================================================
1898
	var showadvtftp = false;
1899

    
1900
	function show_advtftp(ispageload) {
1901
		var text;
1902
		// On page load decide the initial state based on the data.
1903
		if (ispageload) {
1904
<?php
1905
			if (empty($pconfig['tftp'])) {
1906
				$showadv = false;
1907
			} else {
1908
				$showadv = true;
1909
			}
1910
?>
1911
			showadvtftp = <?php if ($showadv) {echo 'true';} else {echo 'false';} ?>;
1912
		} else {
1913
			// It was a click, swap the state.
1914
			showadvtftp = !showadvtftp;
1915
		}
1916

    
1917
		hideInput('tftp', !showadvtftp);
1918

    
1919
		if (showadvtftp) {
1920
			text = "<?=gettext('Hide Advanced');?>";
1921
		} else {
1922
			text = "<?=gettext('Display Advanced');?>";
1923
		}
1924
		$('#btnadvtftp').html('<i class="fa fa-cog"></i> ' + text);
1925
	}
1926

    
1927
	$('#btnadvtftp').click(function(event) {
1928
		show_advtftp();
1929
	});
1930

    
1931
	// Show advanced LDAP options ======================================================================================
1932
	var showadvldap = false;
1933

    
1934
	function show_advldap(ispageload) {
1935
		var text;
1936
		// On page load decide the initial state based on the data.
1937
		if (ispageload) {
1938
<?php
1939
			if (empty($pconfig['ldap'])) {
1940
				$showadv = false;
1941
			} else {
1942
				$showadv = true;
1943
			}
1944
?>
1945
			showadvldap = <?php if ($showadv) {echo 'true';} else {echo 'false';} ?>;
1946
		} else {
1947
			// It was a click, swap the state.
1948
			showadvldap = !showadvldap;
1949
		}
1950

    
1951
		hideInput('ldap', !showadvldap);
1952

    
1953
		if (showadvldap) {
1954
			text = "<?=gettext('Hide Advanced');?>";
1955
		} else {
1956
			text = "<?=gettext('Display Advanced');?>";
1957
		}
1958
		$('#btnadvldap').html('<i class="fa fa-cog"></i> ' + text);
1959
	}
1960

    
1961
	$('#btnadvldap').click(function(event) {
1962
		show_advldap();
1963
	});
1964

    
1965
	// Show advanced additional opts options ===========================================================================
1966
	var showadvopts = false;
1967

    
1968
	function show_advopts(ispageload) {
1969
		var text;
1970
		// On page load decide the initial state based on the data.
1971
		if (ispageload) {
1972
<?php
1973
			if (empty($pconfig['numberoptions']) ||
1974
			    (empty($pconfig['numberoptions']['item'][0]['number']) && (empty($pconfig['numberoptions']['item'][0]['value'])))) {
1975
				$showadv = false;
1976
			} else {
1977
				$showadv = true;
1978
			}
1979
?>
1980
			showadvopts = <?php if ($showadv) {echo 'true';} else {echo 'false';} ?>;
1981
		} else {
1982
			// It was a click, swap the state.
1983
			showadvopts = !showadvopts;
1984
		}
1985

    
1986
		hideClass('adnlopts', !showadvopts);
1987

    
1988
		if (showadvopts) {
1989
			text = "<?=gettext('Hide Advanced');?>";
1990
		} else {
1991
			text = "<?=gettext('Display Advanced');?>";
1992
		}
1993
		$('#btnadvopts').html('<i class="fa fa-cog"></i> ' + text);
1994
	}
1995

    
1996
	$('#btnadvopts').click(function(event) {
1997
		show_advopts();
1998
	});
1999

    
2000
	// Show advanced Network Booting options ===========================================================================
2001
	var showadvnwkboot = false;
2002

    
2003
	function show_advnwkboot(ispageload) {
2004
		var text;
2005
		// On page load decide the initial state based on the data.
2006
		if (ispageload) {
2007
<?php
2008
			if (empty($pconfig['netboot'])) {
2009
				$showadv = false;
2010
			} else {
2011
				$showadv = true;
2012
			}
2013
?>
2014
			showadvnwkboot = <?php if ($showadv) {echo 'true';} else {echo 'false';} ?>;
2015
		} else {
2016
			// It was a click, swap the state.
2017
			showadvnwkboot = !showadvnwkboot;
2018
		}
2019

    
2020
		hideCheckbox('netboot', !showadvnwkboot);
2021
		hideInput('nextserver', !showadvnwkboot);
2022
		hideInput('filename', !showadvnwkboot);
2023
		hideInput('filename32', !showadvnwkboot);
2024
		hideInput('filename64', !showadvnwkboot);
2025
		hideInput('filename32arm', !showadvnwkboot);
2026
		hideInput('filename64arm', !showadvnwkboot);
2027
		hideInput('uefihttpboot', !showadvnwkboot);
2028
		hideInput('rootpath', !showadvnwkboot);
2029

    
2030
		if (showadvnwkboot) {
2031
			text = "<?=gettext('Hide Advanced');?>";
2032
		} else {
2033
			text = "<?=gettext('Display Advanced');?>";
2034
		}
2035
		$('#btnadvnwkboot').html('<i class="fa fa-cog"></i> ' + text);
2036
	}
2037

    
2038
	$('#btnadvnwkboot').click(function(event) {
2039
		show_advnwkboot();
2040
	});
2041

    
2042
	// ---------- On initial page load ------------------------------------------------------------
2043

    
2044
	show_advdns(true);
2045
	show_advmac(true);
2046
	show_advntp(true);
2047
	show_advtftp(true);
2048
	show_advldap(true);
2049
	show_advopts(true);
2050
	show_advnwkboot(true);
2051

    
2052
	// Suppress "Delete row" button if there are fewer than two rows
2053
	checkLastRow();
2054
});
2055
//]]>
2056
</script>
2057

    
2058
<?php include("foot.inc");
(118-118/227)