Project

General

Profile

Download (52.4 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-2016 Rubicon Communications, LLC (Netgate)
7
 * All rights reserved.
8
 *
9
 * originally based on m0n0wall (http://m0n0.ch/wall)
10
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
11
 * All rights reserved.
12
 *
13
 * Licensed under the Apache License, Version 2.0 (the "License");
14
 * you may not use this file except in compliance with the License.
15
 * You may obtain a copy of the License at
16
 *
17
 * http://www.apache.org/licenses/LICENSE-2.0
18
 *
19
 * Unless required by applicable law or agreed to in writing, software
20
 * distributed under the License is distributed on an "AS IS" BASIS,
21
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22
 * See the License for the specific language governing permissions and
23
 * limitations under the License.
24
 */
25

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

    
33
require_once("guiconfig.inc");
34
require_once("filter.inc");
35
require_once('rrd.inc');
36
require_once("shaper.inc");
37

    
38
if (!$g['services_dhcp_server_enable']) {
39
	header("Location: /");
40
	exit;
41
}
42

    
43
$if = $_GET['if'];
44
if (!empty($_POST['if'])) {
45
	$if = $_POST['if'];
46
}
47

    
48
/* if OLSRD is enabled, allow WAN to house DHCP. */
49
if ($config['installedpackages']['olsrd']) {
50
	foreach ($config['installedpackages']['olsrd']['config'] as $olsrd) {
51
		if ($olsrd['enable']) {
52
			$is_olsr_enabled = true;
53
			break;
54
		}
55
	}
56
}
57

    
58
$iflist = get_configured_interface_with_descr();
59

    
60
/* set the starting interface */
61
if (!$if || !isset($iflist[$if])) {
62
	$found_starting_if = false;
63
	// First look for an interface with DHCP already enabled.
64
	foreach ($iflist as $ifent => $ifname) {
65
		$oc = $config['interfaces'][$ifent];
66
		if (is_array($config['dhcpd'][$ifent]) &&
67
		    isset($config['dhcpd'][$ifent]['enable']) &&
68
		    is_ipaddrv4($oc['ipaddr']) && $oc['subnet'] < 31) {
69
			$if = $ifent;
70
			$found_starting_if = true;
71
			break;
72
		}
73
	}
74

    
75
	/*
76
	 * If there is no DHCP-enabled interface and LAN is a candidate,
77
	 * then choose LAN.
78
	 */
79
	if (!$found_starting_if && isset($iflist['lan']) &&
80
	    is_ipaddrv4($config['interfaces']['lan']['ipaddr']) &&
81
	    $config['interfaces']['lan']['subnet'] < 31) {
82
		$if = 'lan';
83
		$found_starting_if = true;
84
	}
85

    
86
	// At the last select whatever can be found.
87
	if (!$found_starting_if) {
88
		foreach ($iflist as $ifent => $ifname) {
89
			$oc = $config['interfaces'][$ifent];
90

    
91
			/* Not static IPv4 or subnet >= 31 */
92
			if (!is_ipaddrv4($oc['ipaddr']) ||
93
			    empty($oc['subnet']) || $oc['subnet'] < 31) {
94
				continue;
95
			}
96

    
97
			if (!is_array($config['dhcpd'][$ifent]) ||
98
			    !isset($config['dhcpd'][$ifent]['enable'])) {
99
				continue;
100
			}
101

    
102
			$if = $ifent;
103
			break;
104
		}
105
	}
106
}
107

    
108
$act = $_GET['act'];
109
if (!empty($_POST['act'])) {
110
	$act = $_POST['act'];
111
}
112

    
113
$a_pools = array();
114

    
115
if (is_array($config['dhcpd'][$if])) {
116
	$pool = $_GET['pool'];
117
	if (is_numeric($_POST['pool'])) {
118
		$pool = $_POST['pool'];
119
	}
120

    
121
	// If we have a pool but no interface name, that's not valid. Redirect away.
122
	if (is_numeric($pool) && empty($if)) {
123
		header("Location: services_dhcp.php");
124
		exit;
125
	}
126

    
127
	if (!is_array($config['dhcpd'][$if]['pool'])) {
128
		$config['dhcpd'][$if]['pool'] = array();
129
	}
130

    
131
	$a_pools = &$config['dhcpd'][$if]['pool'];
132

    
133
	if (is_numeric($pool) && $a_pools[$pool]) {
134
		$dhcpdconf = &$a_pools[$pool];
135
	} elseif ($act == "newpool") {
136
		$dhcpdconf = array();
137
	} else {
138
		$dhcpdconf = &$config['dhcpd'][$if];
139
	}
140

    
141
	if (!is_array($config['dhcpd'][$if]['staticmap'])) {
142
		$dhcpdconf['staticmap'] = array();
143
	}
144

    
145
	$a_maps = &$config['dhcpd'][$if]['staticmap'];
146
}
147
if (is_array($dhcpdconf)) {
148
	// Global Options
149
	if (!is_numeric($pool) && !($act == "newpool")) {
150
		$pconfig['enable'] = isset($dhcpdconf['enable']);
151
		$pconfig['staticarp'] = isset($dhcpdconf['staticarp']);
152
		// No reason to specify this per-pool, per the dhcpd.conf man page it needs to be in every
153
		//	 pool and should be specified in every pool both nodes share, so we'll treat it as global
154
		$pconfig['failover_peerip'] = $dhcpdconf['failover_peerip'];
155

    
156
		// dhcpleaseinlocaltime is global to all interfaces. So if it is selected on any interface,
157
		// then show it true/checked.
158
		foreach ($config['dhcpd'] as $dhcpdifitem) {
159
			$dhcpleaseinlocaltime = $dhcpdifitem['dhcpleaseinlocaltime'];
160
			if ($dhcpleaseinlocaltime) {
161
				break;
162
			}
163
		}
164

    
165
		$pconfig['dhcpleaseinlocaltime'] = $dhcpleaseinlocaltime;
166
	} else {
167
		// Options that exist only in pools
168
		$pconfig['descr'] = $dhcpdconf['descr'];
169
	}
170

    
171
	// Options that can be global or per-pool.
172
	if (is_array($dhcpdconf['range'])) {
173
		$pconfig['range_from'] = $dhcpdconf['range']['from'];
174
		$pconfig['range_to'] = $dhcpdconf['range']['to'];
175
	}
176

    
177
	$pconfig['deftime'] = $dhcpdconf['defaultleasetime'];
178
	$pconfig['maxtime'] = $dhcpdconf['maxleasetime'];
179
	$pconfig['gateway'] = $dhcpdconf['gateway'];
180
	$pconfig['domain'] = $dhcpdconf['domain'];
181
	$pconfig['domainsearchlist'] = $dhcpdconf['domainsearchlist'];
182
	list($pconfig['wins1'], $pconfig['wins2']) = $dhcpdconf['winsserver'];
183
	list($pconfig['dns1'], $pconfig['dns2'], $pconfig['dns3'], $pconfig['dns4']) = $dhcpdconf['dnsserver'];
184
	$pconfig['ignorebootp'] = isset($dhcpdconf['ignorebootp']);
185
	$pconfig['denyunknown'] = isset($dhcpdconf['denyunknown']);
186
	$pconfig['ignoreclientuids'] = isset($dhcpdconf['ignoreclientuids']);
187
	$pconfig['nonak'] = isset($dhcpdconf['nonak']);
188
	$pconfig['ddnsdomain'] = $dhcpdconf['ddnsdomain'];
189
	$pconfig['ddnsdomainprimary'] = $dhcpdconf['ddnsdomainprimary'];
190
	$pconfig['ddnsdomainkeyname'] = $dhcpdconf['ddnsdomainkeyname'];
191
	$pconfig['ddnsdomainkey'] = $dhcpdconf['ddnsdomainkey'];
192
	$pconfig['ddnsupdate'] = isset($dhcpdconf['ddnsupdate']);
193
	$pconfig['ddnsforcehostname'] = isset($dhcpdconf['ddnsforcehostname']);
194
	$pconfig['mac_allow'] = $dhcpdconf['mac_allow'];
195
	$pconfig['mac_deny'] = $dhcpdconf['mac_deny'];
196
	list($pconfig['ntp1'], $pconfig['ntp2']) = $dhcpdconf['ntpserver'];
197
	$pconfig['tftp'] = $dhcpdconf['tftp'];
198
	$pconfig['ldap'] = $dhcpdconf['ldap'];
199
	$pconfig['netboot'] = isset($dhcpdconf['netboot']);
200
	$pconfig['nextserver'] = $dhcpdconf['nextserver'];
201
	$pconfig['filename'] = $dhcpdconf['filename'];
202
	$pconfig['filename32'] = $dhcpdconf['filename32'];
203
	$pconfig['filename64'] = $dhcpdconf['filename64'];
204
	$pconfig['rootpath'] = $dhcpdconf['rootpath'];
205
	$pconfig['netmask'] = $dhcpdconf['netmask'];
206
	$pconfig['numberoptions'] = $dhcpdconf['numberoptions'];
207
	$pconfig['statsgraph'] = $dhcpdconf['statsgraph'];
208
}
209

    
210
$ifcfgip = $config['interfaces'][$if]['ipaddr'];
211
$ifcfgsn = $config['interfaces'][$if]['subnet'];
212

    
213
$subnet_start = gen_subnetv4($ifcfgip, $ifcfgsn);
214
$subnet_end = gen_subnetv4_max($ifcfgip, $ifcfgsn);
215

    
216
function validate_partial_mac_list($maclist) {
217
	$macs = explode(',', $maclist);
218

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

    
226
	return true;
227
}
228

    
229
if (isset($_POST['save'])) {
230

    
231
	unset($input_errors);
232

    
233
	$pconfig = $_POST;
234

    
235
	$numberoptions = array();
236
	for ($x = 0; $x < 99; $x++) {
237
		if (isset($_POST["number{$x}"]) && ctype_digit($_POST["number{$x}"])) {
238
			$numbervalue = array();
239
			$numbervalue['number'] = htmlspecialchars($_POST["number{$x}"]);
240
			$numbervalue['type'] = htmlspecialchars($_POST["itemtype{$x}"]);
241
			$numbervalue['value'] = base64_encode($_POST["value{$x}"]);
242
			$numberoptions['item'][] = $numbervalue;
243
		}
244
	}
245

    
246
	// Reload the new pconfig variable that the form uses.
247
	$pconfig['numberoptions'] = $numberoptions;
248

    
249
	/* input validation */
250

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

    
256
		do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
257
	}
258

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

    
263
	if ($_POST['range_from'] && !is_ipaddrv4($_POST['range_from'])) {
264
		$input_errors[] = gettext("A valid IPv4 address must be specified for range from.");
265
	}
266
	if ($_POST['range_to'] && !is_ipaddrv4($_POST['range_to'])) {
267
		$input_errors[] = gettext("A valid IPv4 address must be specified for range to.");
268
	}
269
	if (($_POST['range_from'] && !$_POST['range_to']) || ($_POST['range_to'] && !$_POST['range_from'])) {
270
		$input_errors[] = gettext("Range From and Range To must both be entered.");
271
	}
272
	if (($_POST['gateway'] && $_POST['gateway'] != "none" && !is_ipaddrv4($_POST['gateway']))) {
273
		$input_errors[] = gettext("A valid IP address must be specified for the gateway.");
274
	}
275
	if (($_POST['wins1'] && !is_ipaddrv4($_POST['wins1'])) || ($_POST['wins2'] && !is_ipaddrv4($_POST['wins2']))) {
276
		$input_errors[] = gettext("A valid IP address must be specified for the primary/secondary WINS servers.");
277
	}
278
	$parent_ip = get_interface_ip($_POST['if']);
279
	if (is_ipaddrv4($parent_ip) && $_POST['gateway'] && $_POST['gateway'] != "none") {
280
		$parent_sn = get_interface_subnet($_POST['if']);
281
		if (!ip_in_subnet($_POST['gateway'], gen_subnet($parent_ip, $parent_sn) . "/" . $parent_sn) && !ip_in_interface_alias_subnet($_POST['if'], $_POST['gateway'])) {
282
			$input_errors[] = sprintf(gettext("The gateway address %s does not lie within the chosen interface's subnet."), $_POST['gateway']);
283
		}
284
	}
285

    
286
	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']))) {
287
		$input_errors[] = gettext("A valid IP address must be specified for each of the DNS servers.");
288
	}
289

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

    
294
	if (isset($config['captiveportal']) && is_array($config['captiveportal'])) {
295
		$deftime = 7200; // Default value if it's empty
296
		if (is_numeric($_POST['deftime'])) {
297
			$deftime = $_POST['deftime'];
298
		}
299

    
300
		foreach ($config['captiveportal'] as $cpZone => $cpdata) {
301
			if (!isset($cpdata['enable'])) {
302
				continue;
303
			}
304
			if (!isset($cpdata['timeout']) || !is_numeric($cpdata['timeout'])) {
305
				continue;
306
			}
307
			$cp_ifs = explode(',', $cpdata['interface']);
308
			if (!in_array($if, $cp_ifs)) {
309
				continue;
310
			}
311
			if ($cpdata['timeout'] > $deftime) {
312
				$input_errors[] = sprintf(gettext(
313
					'The Captive Portal zone (%1$s) has Hard Timeout parameter set to a value bigger than Default lease time (%2$s).'), $cpZone, $deftime);
314
			}
315
		}
316
	}
317

    
318
	if ($_POST['maxtime'] && (!is_numeric($_POST['maxtime']) || ($_POST['maxtime'] < 60) || ($_POST['maxtime'] <= $_POST['deftime']))) {
319
		$input_errors[] = gettext("The maximum lease time must be at least 60 seconds and higher than the default lease time.");
320
	}
321
	if (($_POST['ddnsdomain'] && !is_domain($_POST['ddnsdomain']))) {
322
		$input_errors[] = gettext("A valid domain name must be specified for the dynamic DNS registration.");
323
	}
324
	if (($_POST['ddnsdomain'] && !is_ipaddrv4($_POST['ddnsdomainprimary']))) {
325
		$input_errors[] = gettext("A valid primary domain name server IP address must be specified for the dynamic domain name.");
326
	}
327
	if (($_POST['ddnsdomainkey'] && !$_POST['ddnsdomainkeyname']) ||
328
		($_POST['ddnsdomainkeyname'] && !$_POST['ddnsdomainkey'])) {
329
		$input_errors[] = gettext("Both a valid domain key and key name must be specified.");
330
	}
331
	if ($_POST['domainsearchlist']) {
332
		$domain_array = preg_split("/[ ;]+/", $_POST['domainsearchlist']);
333
		foreach ($domain_array as $curdomain) {
334
			if (!is_domain($curdomain)) {
335
				$input_errors[] = gettext("A valid domain search list must be specified.");
336
				break;
337
			}
338
		}
339
	}
340

    
341
	// Validate MACs
342
	if (!empty($_POST['mac_allow']) && !validate_partial_mac_list($_POST['mac_allow'])) {
343
		$input_errors[] = gettext("If a mac allow list is specified, it must contain only valid partial MAC addresses.");
344
	}
345
	if (!empty($_POST['mac_deny']) && !validate_partial_mac_list($_POST['mac_deny'])) {
346
		$input_errors[] = gettext("If a mac deny list is specified, it must contain only valid partial MAC addresses.");
347
	}
348

    
349
	if (($_POST['ntp1'] && (!is_ipaddrv4($_POST['ntp1']) && !is_hostname($_POST['ntp1']))) || ($_POST['ntp2'] && (!is_ipaddrv4($_POST['ntp2']) && !is_hostname($_POST['ntp2'])))) {
350
		$input_errors[] = gettext("A valid IP address or hostname must be specified for the primary/secondary NTP servers.");
351
	}
352
	if (($_POST['domain'] && !is_domain($_POST['domain']))) {
353
		$input_errors[] = gettext("A valid domain name must be specified for the DNS domain.");
354
	}
355
	if ($_POST['tftp'] && !is_ipaddrv4($_POST['tftp']) && !is_domain($_POST['tftp']) && !filter_var($_POST['tftp'], FILTER_VALIDATE_URL)) {
356
		$input_errors[] = gettext("A valid IP address, hostname or URL must be specified for the TFTP server.");
357
	}
358
	if (($_POST['nextserver'] && !is_ipaddrv4($_POST['nextserver']))) {
359
		$input_errors[] = gettext("A valid IP address must be specified for the network boot server.");
360
	}
361

    
362
	if (gen_subnet($ifcfgip, $ifcfgsn) == $_POST['range_from']) {
363
		$input_errors[] = gettext("The network address cannot be used in the starting subnet range.");
364
	}
365
	if (gen_subnet_max($ifcfgip, $ifcfgsn) == $_POST['range_to']) {
366
		$input_errors[] = gettext("The broadcast address cannot be used in the ending subnet range.");
367
	}
368

    
369
	// Disallow a range that includes the virtualip
370
	if (is_array($config['virtualip']['vip'])) {
371
		foreach ($config['virtualip']['vip'] as $vip) {
372
			if ($vip['interface'] == $if) {
373
				if ($vip['subnet'] && is_inrange_v4($vip['subnet'], $_POST['range_from'], $_POST['range_to'])) {
374
					$input_errors[] = sprintf(gettext("The subnet range cannot overlap with virtual IP address %s."), $vip['subnet']);
375
				}
376
			}
377
		}
378
	}
379

    
380
	$noip = false;
381
	if (is_array($a_maps)) {
382
		foreach ($a_maps as $map) {
383
			if (empty($map['ipaddr'])) {
384
				$noip = true;
385
			}
386
		}
387
	}
388

    
389
	if ($_POST['staticarp'] && $noip) {
390
		$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.");
391
	}
392

    
393
	if (is_array($pconfig['numberoptions']['item'])) {
394
		foreach ($pconfig['numberoptions']['item'] as $numberoption) {
395
			$numberoption_value = base64_decode($numberoption['value']);
396
			if ($numberoption['type'] == 'text' && strstr($numberoption_value, '"')) {
397
				$input_errors[] = gettext("Text type cannot include quotation marks.");
398
			} else if ($numberoption['type'] == 'string' && !preg_match('/^"[^"]*"$/', $numberoption_value) && !preg_match('/^[0-9a-f]{2}(?:\:[0-9a-f]{2})*$/i', $numberoption_value)) {
399
				$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");
400
			} else if ($numberoption['type'] == 'boolean' && $numberoption_value != 'true' && $numberoption_value != 'false' && $numberoption_value != 'on' && $numberoption_value != 'off') {
401
				$input_errors[] = gettext("Boolean type must be true, false, on, or off.");
402
			} else if ($numberoption['type'] == 'unsigned integer 8' && (!is_numeric($numberoption_value) || $numberoption_value < 0 || $numberoption_value > 255)) {
403
				$input_errors[] = gettext("Unsigned 8-bit integer type must be a number in the range 0 to 255.");
404
			} else if ($numberoption['type'] == 'unsigned integer 16' && (!is_numeric($numberoption_value) || $numberoption_value < 0 || $numberoption_value > 65535)) {
405
				$input_errors[] = gettext("Unsigned 16-bit integer type must be a number in the range 0 to 65535.");
406
			} else if ($numberoption['type'] == 'unsigned integer 32' && (!is_numeric($numberoption_value) || $numberoption_value < 0 || $numberoption_value > 4294967295)) {
407
				$input_errors[] = gettext("Unsigned 32-bit integer type must be a number in the range 0 to 4294967295.");
408
			} else if ($numberoption['type'] == 'signed integer 8' && (!is_numeric($numberoption_value) || $numberoption_value < -128 || $numberoption_value > 127)) {
409
				$input_errors[] = gettext("Signed 8-bit integer type must be a number in the range -128 to 127.");
410
			} else if ($numberoption['type'] == 'signed integer 16' && (!is_numeric($numberoption_value) || $numberoption_value < -32768 || $numberoption_value > 32767)) {
411
				$input_errors[] = gettext("Signed 16-bit integer type must be a number in the range -32768 to 32767.");
412
			} else if ($numberoption['type'] == 'signed integer 32' && (!is_numeric($numberoption_value) || $numberoption_value < -2147483648 || $numberoption_value > 2147483647)) {
413
				$input_errors[] = gettext("Signed 32-bit integer type must be a number in the range -2147483648 to 2147483647.");
414
			} else if ($numberoption['type'] == 'ip-address' && !is_ipaddrv4($numberoption_value) && !is_hostname($numberoption_value)) {
415
				$input_errors[] = gettext("IP address or host type must be an IP address or host name.");
416
			}
417
		}
418
	}
419

    
420
	/* If enabling DHCP Server, make sure that the DHCP Relay isn't enabled on this interface */
421
	if ($_POST['enable'] && isset($config['dhcrelay']['enable']) && (stristr($config['dhcrelay']['interface'], $if) !== false)) {
422
		$input_errors[] = sprintf(gettext("The DHCP relay on the %s interface must be disabled before enabling the DHCP server."), $iflist[$if]);
423
	}
424

    
425
	// 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.
426
	if (!$input_errors && $_POST['range_from'] && $_POST['range_to']) {
427
		/* make sure the range lies within the current subnet */
428
		if (ip_greater_than($_POST['range_from'], $_POST['range_to'])) {
429
			$input_errors[] = gettext("The range is invalid (first element higher than second element).");
430
		}
431

    
432
		if (!is_inrange_v4($_POST['range_from'], $subnet_start, $subnet_end) ||
433
			!is_inrange_v4($_POST['range_to'], $subnet_start, $subnet_end)) {
434
			$input_errors[] = gettext("The specified range lies outside of the current subnet.");
435
		}
436

    
437
		if (is_numeric($pool) || ($act == "newpool")) {
438
			if (is_inrange_v4($_POST['range_from'],
439
				$config['dhcpd'][$if]['range']['from'],
440
				$config['dhcpd'][$if]['range']['to']) ||
441
				is_inrange_v4($_POST['range_to'],
442
				$config['dhcpd'][$if]['range']['from'],
443
				$config['dhcpd'][$if]['range']['to'])) {
444
				$input_errors[] = gettext("The specified range must not be within the DHCP range for this interface.");
445
			}
446
		}
447

    
448
		foreach ($a_pools as $id => $p) {
449
			if (is_numeric($pool) && ($id == $pool)) {
450
				continue;
451
			}
452

    
453
			if (is_inrange_v4($_POST['range_from'],
454
				$p['range']['from'], $p['range']['to']) ||
455
				is_inrange_v4($_POST['range_to'],
456
				$p['range']['from'], $p['range']['to'])) {
457
				$input_errors[] = gettext("The specified range must not be within the range configured on a DHCP pool for this interface.");
458
				break;
459
			}
460
		}
461

    
462
		if (is_array($a_maps)) {
463
			foreach ($a_maps as $map) {
464
				if (empty($map['ipaddr'])) {
465
					continue;
466
				}
467
				if (is_inrange_v4($map['ipaddr'], $_POST['range_from'], $_POST['range_to'])) {
468
					$input_errors[] = sprintf(gettext("The DHCP range cannot overlap any static DHCP mappings."));
469
					break;
470
				}
471
			}
472
		}
473
	}
474

    
475
	if (!$input_errors) {
476
		if (!is_numeric($pool)) {
477
			if ($act == "newpool") {
478
				$dhcpdconf = array();
479
			} else {
480
				if (!is_array($config['dhcpd'][$if])) {
481
					$config['dhcpd'][$if] = array();
482
				}
483
				$dhcpdconf = $config['dhcpd'][$if];
484
			}
485
		} else {
486
			if (is_array($a_pools[$pool])) {
487
				$dhcpdconf = $a_pools[$pool];
488
			} else {
489
				// Someone specified a pool but it doesn't exist. Punt.
490
				header("Location: services_dhcp.php");
491
				exit;
492
			}
493
		}
494
		if (!is_array($dhcpdconf['range'])) {
495
			$dhcpdconf['range'] = array();
496
		}
497

    
498
		$dhcpd_enable_changed = false;
499

    
500
		// Global Options
501
		if (!is_numeric($pool) && !($act == "newpool")) {
502
			$old_dhcpd_enable = isset($dhcpdconf['enable']);
503
			$new_dhcpd_enable = ($_POST['enable']) ? true : false;
504
			if ($old_dhcpd_enable != $new_dhcpd_enable) {
505
				/* DHCP has been enabled or disabled. The pf ruleset will need to be rebuilt to allow or disallow DHCP. */
506
				$dhcpd_enable_changed = true;
507
			}
508

    
509
			$dhcpdconf['enable'] = $new_dhcpd_enable;
510
			$dhcpdconf['staticarp'] = ($_POST['staticarp']) ? true : false;
511
			$previous = $dhcpdconf['failover_peerip'];
512
			if ($previous != $_POST['failover_peerip']) {
513
				mwexec("/bin/rm -rf /var/dhcpd/var/db/*");
514
			}
515

    
516
			$dhcpdconf['failover_peerip'] = $_POST['failover_peerip'];
517
			// dhcpleaseinlocaltime is global to all interfaces. So update the setting on all interfaces.
518
			foreach ($config['dhcpd'] as &$dhcpdifitem) {
519
				$dhcpdifitem['dhcpleaseinlocaltime'] = $_POST['dhcpleaseinlocaltime'];
520
			}
521
		} else {
522
			// Options that exist only in pools
523
			$dhcpdconf['descr'] = $_POST['descr'];
524
		}
525

    
526
		// Options that can be global or per-pool.
527
		$dhcpdconf['range']['from'] = $_POST['range_from'];
528
		$dhcpdconf['range']['to'] = $_POST['range_to'];
529
		$dhcpdconf['defaultleasetime'] = $_POST['deftime'];
530
		$dhcpdconf['maxleasetime'] = $_POST['maxtime'];
531
		$dhcpdconf['netmask'] = $_POST['netmask'];
532

    
533
		unset($dhcpdconf['winsserver']);
534
		if ($_POST['wins1']) {
535
			$dhcpdconf['winsserver'][] = $_POST['wins1'];
536
		}
537
		if ($_POST['wins2']) {
538
			$dhcpdconf['winsserver'][] = $_POST['wins2'];
539
		}
540

    
541
		unset($dhcpdconf['dnsserver']);
542
		if ($_POST['dns1']) {
543
			$dhcpdconf['dnsserver'][] = $_POST['dns1'];
544
		}
545
		if ($_POST['dns2']) {
546
			$dhcpdconf['dnsserver'][] = $_POST['dns2'];
547
		}
548
		if ($_POST['dns3']) {
549
			$dhcpdconf['dnsserver'][] = $_POST['dns3'];
550
		}
551
		if ($_POST['dns4']) {
552
			$dhcpdconf['dnsserver'][] = $_POST['dns4'];
553
		}
554

    
555
		$dhcpdconf['gateway'] = $_POST['gateway'];
556
		$dhcpdconf['domain'] = $_POST['domain'];
557
		$dhcpdconf['domainsearchlist'] = $_POST['domainsearchlist'];
558
		$dhcpdconf['ignorebootp'] = ($_POST['ignorebootp']) ? true : false;
559
		$dhcpdconf['denyunknown'] = ($_POST['denyunknown']) ? true : false;
560
		$dhcpdconf['ignoreclientuids'] = ($_POST['ignoreclientuids']) ? true : false;
561
		$dhcpdconf['nonak'] = ($_POST['nonak']) ? true : false;
562
		$dhcpdconf['ddnsdomain'] = $_POST['ddnsdomain'];
563
		$dhcpdconf['ddnsdomainprimary'] = $_POST['ddnsdomainprimary'];
564
		$dhcpdconf['ddnsdomainkeyname'] = $_POST['ddnsdomainkeyname'];
565
		$dhcpdconf['ddnsdomainkey'] = $_POST['ddnsdomainkey'];
566
		$dhcpdconf['ddnsupdate'] = ($_POST['ddnsupdate']) ? true : false;
567
		$dhcpdconf['ddnsforcehostname'] = ($_POST['ddnsforcehostname']) ? true : false;
568
		$dhcpdconf['mac_allow'] = $_POST['mac_allow'];
569
		$dhcpdconf['mac_deny'] = $_POST['mac_deny'];
570

    
571
		unset($dhcpdconf['ntpserver']);
572
		if ($_POST['ntp1']) {
573
			$dhcpdconf['ntpserver'][] = $_POST['ntp1'];
574
		}
575
		if ($_POST['ntp2']) {
576
			$dhcpdconf['ntpserver'][] = $_POST['ntp2'];
577
		}
578

    
579
		$dhcpdconf['tftp'] = $_POST['tftp'];
580
		$dhcpdconf['ldap'] = $_POST['ldap'];
581
		$dhcpdconf['netboot'] = ($_POST['netboot']) ? true : false;
582
		$dhcpdconf['nextserver'] = $_POST['nextserver'];
583
		$dhcpdconf['filename'] = $_POST['filename'];
584
		$dhcpdconf['filename32'] = $_POST['filename32'];
585
		$dhcpdconf['filename64'] = $_POST['filename64'];
586
		$dhcpdconf['rootpath'] = $_POST['rootpath'];
587
		unset($dhcpdconf['statsgraph']);
588
		if ($_POST['statsgraph']) {
589
			$dhcpdconf['statsgraph'] = $_POST['statsgraph'];
590
			enable_rrd_graphing();
591
		}
592

    
593
		// Handle the custom options rowhelper
594
		if (isset($dhcpdconf['numberoptions']['item'])) {
595
			unset($dhcpdconf['numberoptions']['item']);
596
		}
597

    
598
		$dhcpdconf['numberoptions'] = $numberoptions;
599

    
600
		if (is_numeric($pool) && is_array($a_pools[$pool])) {
601
			$a_pools[$pool] = $dhcpdconf;
602
		} elseif ($act == "newpool") {
603
			$a_pools[] = $dhcpdconf;
604
		} else {
605
			$config['dhcpd'][$if] = $dhcpdconf;
606
		}
607

    
608
		write_config();
609
	}
610
}
611

    
612
if ((isset($_POST['save']) || isset($_POST['apply'])) && (!$input_errors)) {
613
	$changes_applied = true;
614
	$retval = 0;
615
	$retvaldhcp = 0;
616
	$retvaldns = 0;
617
	/* dnsmasq_configure calls dhcpd_configure */
618
	/* no need to restart dhcpd twice */
619
	if (isset($config['dnsmasq']['enable']) && isset($config['dnsmasq']['regdhcpstatic']))	{
620
		$retvaldns |= services_dnsmasq_configure();
621
		if ($retvaldns == 0) {
622
			clear_subsystem_dirty('hosts');
623
			clear_subsystem_dirty('staticmaps');
624
		}
625
	} else if (isset($config['unbound']['enable']) && isset($config['unbound']['regdhcpstatic'])) {
626
		$retvaldns |= services_unbound_configure();
627
		if ($retvaldns == 0) {
628
			clear_subsystem_dirty('unbound');
629
			clear_subsystem_dirty('hosts');
630
			clear_subsystem_dirty('staticmaps');
631
		}
632
	} else {
633
		$retvaldhcp |= services_dhcpd_configure();
634
		if ($retvaldhcp == 0) {
635
			clear_subsystem_dirty('staticmaps');
636
		}
637
	}
638
	if ($dhcpd_enable_changed) {
639
		$retvalfc |= filter_configure();
640
	}
641

    
642
	if ($retvaldhcp == 1 || $retvaldns == 1 || $retvalfc == 1) {
643
		$retval = 1;
644
	}
645
}
646

    
647
if ($act == "delpool") {
648
	if ($a_pools[$_GET['id']]) {
649
		unset($a_pools[$_GET['id']]);
650
		write_config();
651
		header("Location: services_dhcp.php?if={$if}");
652
		exit;
653
	}
654
}
655

    
656
if ($act == "del") {
657
	if ($a_maps[$_GET['id']]) {
658
		/* Remove static ARP entry, if necessary */
659
		if (isset($a_maps[$_GET['id']]['arp_table_static_entry'])) {
660
			mwexec("/usr/sbin/arp -d " . escapeshellarg($a_maps[$_GET['id']]['ipaddr']));
661
		}
662
		unset($a_maps[$_GET['id']]);
663
		write_config();
664
		if (isset($config['dhcpd'][$if]['enable'])) {
665
			mark_subsystem_dirty('staticmaps');
666
			if (isset($config['dnsmasq']['enable']) && isset($config['dnsmasq']['regdhcpstatic'])) {
667
				mark_subsystem_dirty('hosts');
668
			}
669
		}
670

    
671
		header("Location: services_dhcp.php?if={$if}");
672
		exit;
673
	}
674
}
675

    
676
// Build an HTML table that can be inserted into a Form_StaticText element
677
function build_pooltable() {
678
	global $a_pools, $if;
679

    
680
	$pooltbl =	'<div class="table-responsive">';
681
	$pooltbl .=		'<table class="table table-striped table-hover table-condensed">';
682
	$pooltbl .=			'<thead>';
683
	$pooltbl .=				'<tr>';
684
	$pooltbl .=					'<th>' . gettext("Pool Start") . '</th>';
685
	$pooltbl .=					'<th>' . gettext("Pool End") . '</th>';
686
	$pooltbl .=					'<th>' . gettext("Description") . '</th>';
687
	$pooltbl .=					'<th>' . gettext("Actions") . '</th>';
688
	$pooltbl .=				'</tr>';
689
	$pooltbl .=			'</thead>';
690
	$pooltbl .=			'<tbody>';
691

    
692
	if (is_array($a_pools)) {
693
		$i = 0;
694
		foreach ($a_pools as $poolent) {
695
			if (!empty($poolent['range']['from']) && !empty($poolent['range']['to'])) {
696
				$pooltbl .= '<tr>';
697
				$pooltbl .= '<td ondblclick="document.location=\'services_dhcp.php?if=' . htmlspecialchars($if) . '&pool=' . $i . '\';">' .
698
							htmlspecialchars($poolent['range']['from']) . '</td>';
699

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

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

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

    
708
				$pooltbl .= ' <a class="fa fa-trash" title="'. gettext("Delete pool") . '" href="services_dhcp.php?if=' . htmlspecialchars($if) . '&act=delpool&id=' . $i . '"></a></td>';
709
				$pooltbl .= '</tr>';
710
			}
711
		$i++;
712
		}
713
	}
714

    
715
	$pooltbl .=			'</tbody>';
716
	$pooltbl .=		'</table>';
717
	$pooltbl .= '</div>';
718

    
719
	return($pooltbl);
720
}
721

    
722
$pgtitle = array(gettext("Services"), gettext("DHCP Server"));
723

    
724
if (!empty($if) && isset($iflist[$if])) {
725
	$pgtitle[] = $iflist[$if];
726
}
727
$shortcut_section = "dhcp";
728

    
729
include("head.inc");
730

    
731
if ($input_errors) {
732
	print_input_errors($input_errors);
733
}
734

    
735
if ($changes_applied) {
736
	print_apply_result_box($retval);
737
}
738

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

    
743
/* active tabs */
744
$tab_array = array();
745
$tabscounter = 0;
746
$i = 0;
747
$have_small_subnet = false;
748

    
749
foreach ($iflist as $ifent => $ifname) {
750
	$oc = $config['interfaces'][$ifent];
751

    
752
	/* Not static IPv4 or subnet >= 31 */
753
	if ($oc['subnet'] >= 31) {
754
		$have_small_subnet = true;
755
		$example_name = $ifname;
756
		$example_cidr = $oc['subnet'];
757
		continue;
758
	}
759
	if (!is_ipaddrv4($oc['ipaddr']) || empty($oc['subnet'])) {
760
		continue;
761
	}
762

    
763
	if ($ifent == $if) {
764
		$active = true;
765
	} else {
766
		$active = false;
767
	}
768

    
769
	$tab_array[] = array($ifname, $active, "services_dhcp.php?if={$ifent}");
770
	$tabscounter++;
771
}
772

    
773
if ($tabscounter == 0) {
774
	if ($have_small_subnet) {
775
		$sentence2 = sprintf(gettext('%1$s has a CIDR mask of %2$s, which does not contain enough addresses.'), htmlspecialchars($example_name), htmlspecialchars($example_cidr));
776
	} else {
777
		$sentence2 = gettext("This system has no interfaces configured with a static IPv4 address.");
778
	}
779
	print_info_box(gettext("The DHCP Server requires a static IPv4 subnet large enough to serve addresses to clients.") . " " . $sentence2);
780
	include("foot.inc");
781
	exit;
782
}
783

    
784
display_top_tabs($tab_array);
785

    
786
$form = new Form();
787

    
788
$section = new Form_Section('General Options');
789

    
790
if (!is_numeric($pool) && !($act == "newpool")) {
791
	if (isset($config['dhcrelay']['enable'])) {
792
		$section->addInput(new Form_Checkbox(
793
			'enable',
794
			'Enable',
795
			gettext("DHCP Relay is currently enabled. DHCP Server canot be enabled while the DHCP Relay is enabled on any interface."),
796
			$pconfig['enable']
797
		))->setAttribute('disabled', true);
798
	} else {
799
		$section->addInput(new Form_Checkbox(
800
			'enable',
801
			'Enable',
802
			sprintf(gettext("Enable DHCP server on %s interface"), htmlspecialchars($iflist[$if])),
803
			$pconfig['enable']
804
		));
805
	}
806
} else {
807
	print_info_box(gettext('Editing pool-specific options. To return to the Interface, click its tab above.'), 'info', false);
808
}
809

    
810
$section->addInput(new Form_Checkbox(
811
	'ignorebootp',
812
	'BOOTP',
813
	'Ignore BOOTP queries',
814
	$pconfig['ignorebootp']
815
));
816

    
817
$section->addInput(new Form_Checkbox(
818
	'denyunknown',
819
	'Deny unknown clients',
820
	'Only the clients defined below will get DHCP leases from this server.',
821
	$pconfig['denyunknown']
822
));
823

    
824
$section->addInput(new Form_Checkbox(
825
	'nonak',
826
	'Ignore denied clients',
827
	'Denied clients will be ignored rather than rejected.',
828
	$pconfig['nonak']
829
))->setHelp("This option is not compatible with failover and cannot be enabled when a Failover Peer IP address is configured.");
830

    
831
$section->addInput(new Form_Checkbox(
832
	'ignoreclientuids',
833
	'Ignore client identifiers',
834
	'If a client includes a unique identifier in its DHCP request, that UID will not be recorded in its lease.',
835
	$pconfig['ignoreclientuids']
836
))->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.");
837

    
838

    
839
if (is_numeric($pool) || ($act == "newpool")) {
840
	$section->addInput(new Form_Input(
841
		'descr',
842
		'Pool Description',
843
		'text',
844
		$pconfig['descr']
845
	));
846
}
847

    
848
$section->addInput(new Form_StaticText(
849
	'Subnet',
850
	gen_subnet($ifcfgip, $ifcfgsn)
851
));
852

    
853
$section->addInput(new Form_StaticText(
854
	'Subnet mask',
855
	gen_subnet_mask($ifcfgsn)
856
));
857

    
858
// Compose a string to display the required address ranges
859
$rangestr = ip_after($subnet_start) . ' - ' . ip_before($subnet_end);
860

    
861
if (is_numeric($pool) || ($act == "newpool")) {
862
	$rangestr .= '<br />' . gettext('In-use DHCP Pool Ranges:');
863
	if (is_array($config['dhcpd'][$if]['range'])) {
864
		$rangestr .= '<br />' . $config['dhcpd'][$if]['range']['from'] . ' - ' . $config['dhcpd'][$if]['range']['to'];
865
	}
866

    
867
	foreach ($a_pools as $p) {
868
		if (is_array($p['range'])) {
869
			$rangestr .= '<br />' . $p['range']['from'] . ' - ' . $p['range']['to'];
870
		}
871
	}
872
}
873

    
874
$section->addInput(new Form_StaticText(
875
	'Available range',
876
	$rangestr
877
));
878

    
879
if ($is_olsr_enabled) {
880
	$section->addInput(new Form_Select(
881
		'netmask',
882
		'Subnet mask',
883
		$pconfig['netmask'],
884
		array_combine(range(32, 1, -1), range(32, 1, -1))
885
	));
886
}
887

    
888
$group = new Form_Group('Range');
889

    
890
$group->add(new Form_IpAddress(
891
	'range_from',
892
	null,
893
	$pconfig['range_from'],
894
	'V4'
895
))->setHelp('From');
896

    
897
$group->add(new Form_IpAddress(
898
	'range_to',
899
	null,
900
	$pconfig['range_to'],
901
	'V4'
902
))->setHelp('To');
903

    
904
$section->add($group);
905

    
906
$form->add($section);
907

    
908
if (!is_numeric($pool) && !($act == "newpool")) {
909
	$section = new Form_Section('Additional Pools');
910

    
911
	$btnaddpool = new Form_Button(
912
		'btnaddpool',
913
		'Add pool',
914
		'services_dhcp.php?if=' . htmlspecialchars($if) . '&act=newpool',
915
		'fa-plus'
916
	);
917
	$btnaddpool->addClass('btn-success');
918

    
919
	$section->addInput(new Form_StaticText(
920
		'Add',
921
		$btnaddpool
922
	))->setHelp('If additional pools of addresses are needed inside of this subnet outside the above Range, they may be specified here.');
923

    
924
	if (is_array($a_pools)) {
925
		$section->addInput(new Form_StaticText(
926
			null,
927
			build_pooltable()
928
		));
929
	}
930

    
931
	$form->add($section);
932
}
933

    
934
$section = new Form_Section('Servers');
935

    
936
$section->addInput(new Form_IpAddress(
937
	'wins1',
938
	'WINS servers',
939
	$pconfig['wins1'],
940
	'V4'
941
))->setAttribute('placeholder', 'WINS Server 1');
942

    
943
$section->addInput(new Form_IpAddress(
944
	'wins2',
945
	null,
946
	$pconfig['wins2'],
947
	'V4'
948
))->setAttribute('placeholder', 'WINS Server 2');
949

    
950
for ($idx=1; $idx<=4; $idx++) {
951
	$section->addInput(new Form_IpAddress(
952
		'dns' . $idx,
953
		($idx == 1) ? 'DNS servers':null,
954
		$pconfig['dns' . $idx],
955
		'V4'
956
	))->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.':'');
957
}
958

    
959
$form->add($section);
960

    
961
$section = new Form_Section('Other Options');
962

    
963
$section->addInput(new Form_IpAddress(
964
	'gateway',
965
	'Gateway',
966
	$pconfig['gateway'],
967
	'V4'
968
))->setPattern('[.a-zA-Z0-9_]+')
969
  ->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.');
970

    
971
$section->addInput(new Form_Input(
972
	'domain',
973
	'Domain name',
974
	'text',
975
	$pconfig['domain']
976
))->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.');
977

    
978
$section->addInput(new Form_Input(
979
	'domainsearchlist',
980
	'Domain search list',
981
	'text',
982
	$pconfig['domainsearchlist']
983
))->setHelp('The DHCP server can optionally provide a domain search list. Use the semicolon character as separator.');
984

    
985
$section->addInput(new Form_Input(
986
	'deftime',
987
	'Default lease time',
988
	'number',
989
	$pconfig['deftime']
990
))->setHelp('This is used for clients that do not ask for a specific expiration time. The default is 7200 seconds.');
991

    
992
$section->addInput(new Form_Input(
993
	'maxtime',
994
	'Maximum lease time',
995
	'number',
996
	$pconfig['maxtime']
997
))->setHelp('This is the maximum lease time for clients that ask for a specific expiration time. The default is 86400 seconds.');
998

    
999
if (!is_numeric($pool) && !($act == "newpool")) {
1000
	$section->addInput(new Form_IpAddress(
1001
		'failover_peerip',
1002
		'Failover peer IP',
1003
		$pconfig['failover_peerip'],
1004
		'V4'
1005
	))->setHelp('Leave blank to disable. Enter the interface IP address of the other machine. Machines must be using CARP. ' .
1006
				'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).');
1007
}
1008

    
1009
if (!is_numeric($pool) && !($act == "newpool")) {
1010
	$section->addInput(new Form_Checkbox(
1011
		'staticarp',
1012
		'Static ARP',
1013
		'Enable Static ARP entries',
1014
		$pconfig['staticarp']
1015
	))->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.');
1016

    
1017
	$section->addInput(new Form_Checkbox(
1018
		'dhcpleaseinlocaltime',
1019
		'Time format change',
1020
		'Change DHCP display lease time from UTC to local time',
1021
		$pconfig['dhcpleaseinlocaltime']
1022
	))->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.' .
1023
				' This will be used for all DHCP interfaces lease time.');
1024
	$section->addInput(new Form_Checkbox(
1025
		'statsgraph',
1026
		'Statistics graphs',
1027
		'Enable RRD statistics graphs',
1028
		$pconfig['statsgraph']
1029
	))->setHelp('Enable this to add DHCP leases statistics to the RRD graphs. Disabled by default.');
1030
}
1031

    
1032
// DDNS
1033
$btnadv = new Form_Button(
1034
	'btnadvdns',
1035
	'Display Advanced',
1036
	null,
1037
	'fa-cog'
1038
);
1039

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

    
1042
$section->addInput(new Form_StaticText(
1043
	'Dynamic DNS',
1044
	$btnadv
1045
));
1046

    
1047
$section->addInput(new Form_Checkbox(
1048
	'ddnsupdate',
1049
	null,
1050
	'Enable registration of DHCP client names in DNS',
1051
	$pconfig['ddnsupdate']
1052
));
1053

    
1054
$section->addInput(new Form_Input(
1055
	'ddnsdomain',
1056
	'DDNS Domain',
1057
	'text',
1058
	$pconfig['ddnsdomain']
1059
))->setHelp('Leave blank to disable dynamic DNS registration.' . '<br />' .
1060
			'Enter the dynamic DNS domain which will be used to register client names in the DNS server.');
1061

    
1062
$section->addInput(new Form_Checkbox(
1063
	'ddnsforcehostname',
1064
	'DDNS Hostnames',
1065
	'Force dynamic DNS hostname to be the same as configured hostname for Static Mappings',
1066
	$pconfig['ddnsforcehostname']
1067
))->setHelp('Default registers host name option supplied by DHCP client.');
1068

    
1069
$section->addInput(new Form_IpAddress(
1070
	'ddnsdomainprimary',
1071
	'Primary DDNS address',
1072
	$pconfig['ddnsdomainprimary'],
1073
	'V4'
1074
))->setHelp('Primary domain name server IP address for the dynamic domain name.');
1075

    
1076
$section->addInput(new Form_Input(
1077
	'ddnsdomainkeyname',
1078
	'DNS Domain key',
1079
	'text',
1080
	$pconfig['ddnsdomainkeyname']
1081
))->setHelp('Dynamic DNS domain key name which will be used to register client names in the DNS server.');
1082

    
1083
$section->addInput(new Form_Input(
1084
	'ddnsdomainkey',
1085
	'DNS Domain key secret',
1086
	'text',
1087
	$pconfig['ddnsdomainkey']
1088
))->setHelp('Dynamic DNS domain key secret (HMAC-MD5) which will be used to register client names in the DNS server.');
1089

    
1090
// Advanced MAC
1091
$btnadv = new Form_Button(
1092
	'btnadvmac',
1093
	'Display Advanced',
1094
	null,
1095
	'fa-cog'
1096
);
1097

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

    
1100
$section->addInput(new Form_StaticText(
1101
	'MAC address control',
1102
	$btnadv
1103
));
1104

    
1105
$section->addInput(new Form_Input(
1106
	'mac_allow',
1107
	'MAC Allow',
1108
	'text',
1109
	$pconfig['mac_allow']
1110
))->setHelp('List of partial MAC addresses to allow, comma separated, no spaces, e.g.: 00:00:00,01:E5:FF');
1111

    
1112
$section->addInput(new Form_Input(
1113
	'mac_deny',
1114
	'MAC Deny',
1115
	'text',
1116
	$pconfig['mac_deny']
1117
))->setHelp('List of partial MAC addresses to deny access, comma separated, no spaces, e.g.: 00:00:00,01:E5:FF');
1118

    
1119
// Advanced NTP
1120
$btnadv = new Form_Button(
1121
	'btnadvntp',
1122
	'Display Advanced',
1123
	null,
1124
	'fa-cog'
1125
);
1126

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

    
1129
$section->addInput(new Form_StaticText(
1130
	'NTP',
1131
	$btnadv
1132
));
1133

    
1134
$section->addInput(new Form_IpAddress(
1135
	'ntp1',
1136
	'NTP Server 1',
1137
	$pconfig['ntp1'],
1138
	'HOSTV4'
1139
));
1140

    
1141
$section->addInput(new Form_IpAddress(
1142
	'ntp2',
1143
	'NTP Server 2',
1144
	$pconfig['ntp2'],
1145
	'HOSTV4'
1146
));
1147

    
1148
// Advanced TFTP
1149
$btnadv = new Form_Button(
1150
	'btnadvtftp',
1151
	'Display Advanced',
1152
	null,
1153
	'fa-cog'
1154
);
1155

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

    
1158
$section->addInput(new Form_StaticText(
1159
	'TFTP',
1160
	$btnadv
1161
));
1162

    
1163
$section->addInput(new Form_Input(
1164
	'tftp',
1165
	'TFTP Server',
1166
	'text',
1167
	$pconfig['tftp']
1168
))->setHelp('Leave blank to disable. Enter a valid IP address, hostname or URL for the TFTP server.');
1169

    
1170
// Advanced LDAP
1171
$btnadv = new Form_Button(
1172
	'btnadvldap',
1173
	'Display Advanced',
1174
	null,
1175
	'fa-cog'
1176
);
1177

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

    
1180
$section->addInput(new Form_StaticText(
1181
	'LDAP',
1182
	$btnadv
1183
));
1184

    
1185
$section->addInput(new Form_Input(
1186
	'ldap',
1187
	'LDAP Server URI',
1188
	'text',
1189
	$pconfig['ldap']
1190
))->setHelp('Leave blank to disable. Enter a full URI for the LDAP server in the form ldap://ldap.example.com/dc=example,dc=com ');
1191

    
1192
// Advanced Network Booting options
1193
$btnadv = new Form_Button(
1194
	'btnadvnwkboot',
1195
	'Display Advanced',
1196
	null,
1197
	'fa-cog'
1198
);
1199

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

    
1202
$section->addInput(new Form_StaticText(
1203
	'Network Booting',
1204
	$btnadv
1205
));
1206

    
1207
$section->addInput(new Form_Checkbox(
1208
	'netboot',
1209
	'Enable',
1210
	'Enables network booting',
1211
	$pconfig['netboot']
1212
));
1213

    
1214
$section->addInput(new Form_IpAddress(
1215
	'nextserver',
1216
	'Next Server',
1217
	$pconfig['nextserver'],
1218
	'V4'
1219
))->setHelp('Enter the IP address of the next server');
1220

    
1221
$section->addInput(new Form_Input(
1222
	'filename',
1223
	'Default BIOS file name',
1224
	'text',
1225
	$pconfig['filename']
1226
));
1227

    
1228
$section->addInput(new Form_Input(
1229
	'filename32',
1230
	'UEFI 32 bit file name',
1231
	'text',
1232
	$pconfig['filename32']
1233
));
1234

    
1235
$section->addInput(new Form_Input(
1236
	'filename64',
1237
	'UEFI 64 bit file name',
1238
	'text',
1239
	$pconfig['filename64']
1240
))->setHelp('Both a filename and a boot server must be configured for this to work! ' .
1241
			'All three filenames and a configured boot server are necessary for UEFI to work! ');
1242

    
1243
$section->addInput(new Form_Input(
1244
	'rootpath',
1245
	'Root path',
1246
	'text',
1247
	$pconfig['rootpath']
1248
))->setHelp('string-format: iscsi:(servername):(protocol):(port):(LUN):targetname ');
1249

    
1250
// Advanced Additional options
1251
$btnadv = new Form_Button(
1252
	'btnadvopts',
1253
	'Display Advanced',
1254
	null,
1255
	'fa-cog'
1256
);
1257

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

    
1260
$section->addInput(new Form_StaticText(
1261
	'Additional BOOTP/DHCP Options',
1262
	$btnadv
1263
));
1264

    
1265
$form->add($section);
1266

    
1267
$section = new Form_Section('Additional BOOTP/DHCP Options');
1268
$section->addClass('adnlopts');
1269

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

    
1276
if (!$pconfig['numberoptions']) {
1277
	$pconfig['numberoptions']['item']  = array(array('number' => '', 'type' => 'text', 'value' => ''));
1278
}
1279

    
1280
$customitemtypes = array(
1281
	'text' => gettext('Text'), 'string' => gettext('String'), 'boolean' => gettext('Boolean'),
1282
	'unsigned integer 8' => gettext('Unsigned 8-bit integer'), 'unsigned integer 16' => gettext('Unsigned 16-bit integer'), 'unsigned integer 32' => gettext('Unsigned 32-bit integer'),
1283
	'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')
1284
);
1285

    
1286
$numrows = count($item) -1;
1287
$counter = 0;
1288

    
1289
$numrows = count($pconfig['numberoptions']['item']) -1;
1290

    
1291
foreach ($pconfig['numberoptions']['item'] as $item) {
1292
	$number = $item['number'];
1293
	$itemtype = $item['type'];
1294
	$value = base64_decode($item['value']);
1295

    
1296
	$group = new Form_Group(($counter == 0) ? 'Option':null);
1297
	$group->addClass('repeatable');
1298

    
1299
	$group->add(new Form_Input(
1300
		'number' . $counter,
1301
		null,
1302
		'text',
1303
		$number
1304
	))->setHelp($numrows == $counter ? 'Number':null);
1305

    
1306

    
1307
	$group->add(new Form_Select(
1308
		'itemtype' . $counter,
1309
		null,
1310
		$itemtype,
1311
		$customitemtypes
1312
	))->setWidth(3)->setHelp($numrows == $counter ? 'Type':null);
1313

    
1314
	$group->add(new Form_Input(
1315
		'value' . $counter,
1316
		null,
1317
		'text',
1318
		$value
1319
	))->setHelp($numrows == $counter ? 'Value':null);
1320

    
1321
	$group->add(new Form_Button(
1322
		'deleterow' . $counter,
1323
		'Delete',
1324
		null,
1325
		'fa-trash'
1326
	))->addClass('btn-warning');
1327

    
1328
	$section->add($group);
1329

    
1330
	$counter++;
1331
}
1332

    
1333
$section->addInput(new Form_Button(
1334
	'addrow',
1335
	'Add',
1336
	null,
1337
	'fa-plus'
1338
))->addClass('btn-success');
1339

    
1340
$form->add($section);
1341

    
1342
if ($act == "newpool") {
1343
	$form->addGlobal(new Form_Input(
1344
		'act',
1345
		null,
1346
		'hidden',
1347
		'newpool'
1348
	));
1349
}
1350

    
1351
if (is_numeric($pool)) {
1352
	$form->addGlobal(new Form_Input(
1353
		'pool',
1354
		null,
1355
		'hidden',
1356
		$pool
1357
	));
1358
}
1359

    
1360
$form->addGlobal(new Form_Input(
1361
	'if',
1362
	null,
1363
	'hidden',
1364
	$if
1365
));
1366

    
1367
print($form);
1368

    
1369
// DHCP Static Mappings table
1370

    
1371
if (!is_numeric($pool) && !($act == "newpool")) {
1372
?>
1373

    
1374
<div class="panel panel-default">
1375
	<div class="panel-heading"><h2 class="panel-title"><?=gettext("DHCP Static Mappings for this Interface")?></h2></div>
1376
	<div class="table-responsive">
1377
			<table class="table table-striped table-hover table-condensed sortable-theme-bootstrap" data-sortable>
1378
				<thead>
1379
					<tr>
1380
						<th><?=gettext("Static ARP")?></th>
1381
						<th><?=gettext("MAC address")?></th>
1382
						<th><?=gettext("IP address")?></th>
1383
						<th><?=gettext("Hostname")?></th>
1384
						<th><?=gettext("Description")?></th>
1385
						<th></th>
1386
					</tr>
1387
				</thead>
1388
<?php
1389
	if (is_array($a_maps)) {
1390
		$i = 0;
1391
?>
1392
				<tbody>
1393
<?php
1394
		foreach ($a_maps as $mapent) {
1395
?>
1396
					<tr>
1397
						<td class="text-center" ondblclick="document.location='services_dhcp_edit.php?if=<?=htmlspecialchars($if)?>&amp;id=<?=$i?>';">
1398
							<?php if (isset($mapent['arp_table_static_entry'])): ?>
1399
								<i class="fa fa-check"></i>
1400
							<?php endif; ?>
1401
						</td>
1402
						<td ondblclick="document.location='services_dhcp_edit.php?if=<?=htmlspecialchars($if)?>&amp;id=<?=$i?>';">
1403
							<?=htmlspecialchars($mapent['mac'])?>
1404
						</td>
1405
						<td ondblclick="document.location='services_dhcp_edit.php?if=<?=htmlspecialchars($if)?>&amp;id=<?=$i?>';">
1406
							<?=htmlspecialchars($mapent['ipaddr'])?>
1407
						</td>
1408
						<td ondblclick="document.location='services_dhcp_edit.php?if=<?=htmlspecialchars($if)?>&amp;id=<?=$i?>';">
1409
							<?=htmlspecialchars($mapent['hostname'])?>
1410
						</td>
1411
						<td ondblclick="document.location='services_dhcp_edit.php?if=<?=htmlspecialchars($if)?>&amp;id=<?=$i?>';">
1412
							<?=htmlspecialchars($mapent['descr'])?>
1413
						</td>
1414
						<td>
1415
							<a class="fa fa-pencil"	title="<?=gettext('Edit static mapping')?>"	href="services_dhcp_edit.php?if=<?=htmlspecialchars($if)?>&amp;id=<?=$i?>"></a>
1416
							<a class="fa fa-trash"	title="<?=gettext('Delete static mapping')?>"	href="services_dhcp.php?if=<?=htmlspecialchars($if)?>&amp;act=del&amp;id=<?=$i?>"></a>
1417
						</td>
1418
					</tr>
1419
<?php
1420
		$i++;
1421
		}
1422
?>
1423
				</tbody>
1424
<?php
1425
	}
1426
?>
1427
		</table>
1428
	</div>
1429
</div>
1430

    
1431
<nav class="action-buttons">
1432
	<a href="services_dhcp_edit.php?if=<?=htmlspecialchars($if)?>" class="btn btn-sm btn-success">
1433
		<i class="fa fa-plus icon-embed-btn"></i>
1434
		<?=gettext("Add")?>
1435
	</a>
1436
</nav>
1437
<?php
1438
}
1439
?>
1440

    
1441
<script type="text/javascript">
1442
//<![CDATA[
1443
events.push(function() {
1444

    
1445
	// Show advanced DNS options ======================================================================================
1446
	var showadvdns = false;
1447

    
1448
	function show_advdns(ispageload) {
1449
		var text;
1450
		// On page load decide the initial state based on the data.
1451
		if (ispageload) {
1452
<?php
1453
			if (!$pconfig['ddnsupdate'] && !$pconfig['ddnsforcehostname'] && empty($pconfig['ddnsdomain']) && empty($pconfig['ddnsdomainprimary']) &&
1454
			    empty($pconfig['ddnsdomainkeyname']) && empty($pconfig['ddnsdomainkey'])) {
1455
				$showadv = false;
1456
			} else {
1457
				$showadv = true;
1458
			}
1459
?>
1460
			showadvdns = <?php if ($showadv) {echo 'true';} else {echo 'false';} ?>;
1461
		} else {
1462
			// It was a click, swap the state.
1463
			showadvdns = !showadvdns;
1464
		}
1465

    
1466
		hideCheckbox('ddnsupdate', !showadvdns);
1467
		hideInput('ddnsdomain', !showadvdns);
1468
		hideCheckbox('ddnsforcehostname', !showadvdns);
1469
		hideInput('ddnsdomainprimary', !showadvdns);
1470
		hideInput('ddnsdomainkeyname', !showadvdns);
1471
		hideInput('ddnsdomainkey', !showadvdns);
1472

    
1473
		if (showadvdns) {
1474
			text = "<?=gettext('Hide Advanced');?>";
1475
		} else {
1476
			text = "<?=gettext('Display Advanced');?>";
1477
		}
1478
		$('#btnadvdns').html('<i class="fa fa-cog"></i> ' + text);
1479
	}
1480

    
1481
	$('#btnadvdns').click(function(event) {
1482
		show_advdns();
1483
	});
1484

    
1485
	// Show advanced MAC options ======================================================================================
1486
	var showadvmac = false;
1487

    
1488
	function show_advmac(ispageload) {
1489
		var text;
1490
		// On page load decide the initial state based on the data.
1491
		if (ispageload) {
1492
<?php
1493
			if (empty($pconfig['mac_allow']) && empty($pconfig['mac_deny'])) {
1494
				$showadv = false;
1495
			} else {
1496
				$showadv = true;
1497
			}
1498
?>
1499
			showadvmac = <?php if ($showadv) {echo 'true';} else {echo 'false';} ?>;
1500
		} else {
1501
			// It was a click, swap the state.
1502
			showadvmac = !showadvmac;
1503
		}
1504

    
1505
		hideInput('mac_allow', !showadvmac);
1506
		hideInput('mac_deny', !showadvmac);
1507

    
1508
		if (showadvmac) {
1509
			text = "<?=gettext('Hide Advanced');?>";
1510
		} else {
1511
			text = "<?=gettext('Display Advanced');?>";
1512
		}
1513
		$('#btnadvmac').html('<i class="fa fa-cog"></i> ' + text);
1514
	}
1515

    
1516
	$('#btnadvmac').click(function(event) {
1517
		show_advmac();
1518
	});
1519

    
1520
	// Show advanced NTP options ======================================================================================
1521
	var showadvntp = false;
1522

    
1523
	function show_advntp(ispageload) {
1524
		var text;
1525
		// On page load decide the initial state based on the data.
1526
		if (ispageload) {
1527
<?php
1528
			if (empty($pconfig['ntp1']) && empty($pconfig['ntp2'])) {
1529
				$showadv = false;
1530
			} else {
1531
				$showadv = true;
1532
			}
1533
?>
1534
			showadvntp = <?php if ($showadv) {echo 'true';} else {echo 'false';} ?>;
1535
		} else {
1536
			// It was a click, swap the state.
1537
			showadvntp = !showadvntp;
1538
		}
1539

    
1540
		hideInput('ntp1', !showadvntp);
1541
		hideInput('ntp2', !showadvntp);
1542

    
1543
		if (showadvntp) {
1544
			text = "<?=gettext('Hide Advanced');?>";
1545
		} else {
1546
			text = "<?=gettext('Display Advanced');?>";
1547
		}
1548
		$('#btnadvntp').html('<i class="fa fa-cog"></i> ' + text);
1549
	}
1550

    
1551
	$('#btnadvntp').click(function(event) {
1552
		show_advntp();
1553
	});
1554

    
1555
	// Show advanced TFTP options ======================================================================================
1556
	var showadvtftp = false;
1557

    
1558
	function show_advtftp(ispageload) {
1559
		var text;
1560
		// On page load decide the initial state based on the data.
1561
		if (ispageload) {
1562
<?php
1563
			if (empty($pconfig['tftp'])) {
1564
				$showadv = false;
1565
			} else {
1566
				$showadv = true;
1567
			}
1568
?>
1569
			showadvtftp = <?php if ($showadv) {echo 'true';} else {echo 'false';} ?>;
1570
		} else {
1571
			// It was a click, swap the state.
1572
			showadvtftp = !showadvtftp;
1573
		}
1574

    
1575
		hideInput('tftp', !showadvtftp);
1576

    
1577
		if (showadvtftp) {
1578
			text = "<?=gettext('Hide Advanced');?>";
1579
		} else {
1580
			text = "<?=gettext('Display Advanced');?>";
1581
		}
1582
		$('#btnadvtftp').html('<i class="fa fa-cog"></i> ' + text);
1583
	}
1584

    
1585
	$('#btnadvtftp').click(function(event) {
1586
		show_advtftp();
1587
	});
1588

    
1589
	// Show advanced LDAP options ======================================================================================
1590
	var showadvldap = false;
1591

    
1592
	function show_advldap(ispageload) {
1593
		var text;
1594
		// On page load decide the initial state based on the data.
1595
		if (ispageload) {
1596
<?php
1597
			if (empty($pconfig['ldap'])) {
1598
				$showadv = false;
1599
			} else {
1600
				$showadv = true;
1601
			}
1602
?>
1603
			showadvldap = <?php if ($showadv) {echo 'true';} else {echo 'false';} ?>;
1604
		} else {
1605
			// It was a click, swap the state.
1606
			showadvldap = !showadvldap;
1607
		}
1608

    
1609
		hideInput('ldap', !showadvldap);
1610

    
1611
		if (showadvldap) {
1612
			text = "<?=gettext('Hide Advanced');?>";
1613
		} else {
1614
			text = "<?=gettext('Display Advanced');?>";
1615
		}
1616
		$('#btnadvldap').html('<i class="fa fa-cog"></i> ' + text);
1617
	}
1618

    
1619
	$('#btnadvldap').click(function(event) {
1620
		show_advldap();
1621
	});
1622

    
1623
	// Show advanced additional opts options ===========================================================================
1624
	var showadvopts = false;
1625

    
1626
	function show_advopts(ispageload) {
1627
		var text;
1628
		// On page load decide the initial state based on the data.
1629
		if (ispageload) {
1630
<?php
1631
			if (empty($pconfig['numberoptions']) ||
1632
			    (empty($pconfig['numberoptions']['item'][0]['number']) && (empty($pconfig['numberoptions']['item'][0]['value'])))) {
1633
				$showadv = false;
1634
			} else {
1635
				$showadv = true;
1636
			}
1637
?>
1638
			showadvopts = <?php if ($showadv) {echo 'true';} else {echo 'false';} ?>;
1639
		} else {
1640
			// It was a click, swap the state.
1641
			showadvopts = !showadvopts;
1642
		}
1643

    
1644
		hideClass('adnlopts', !showadvopts);
1645

    
1646
		if (showadvopts) {
1647
			text = "<?=gettext('Hide Advanced');?>";
1648
		} else {
1649
			text = "<?=gettext('Display Advanced');?>";
1650
		}
1651
		$('#btnadvopts').html('<i class="fa fa-cog"></i> ' + text);
1652
	}
1653

    
1654
	$('#btnadvopts').click(function(event) {
1655
		show_advopts();
1656
	});
1657

    
1658
	// Show advanced Network Booting options ===========================================================================
1659
	var showadvnwkboot = false;
1660

    
1661
	function show_advnwkboot(ispageload) {
1662
		var text;
1663
		// On page load decide the initial state based on the data.
1664
		if (ispageload) {
1665
<?php
1666
			if (empty($pconfig['netboot'])) {
1667
				$showadv = false;
1668
			} else {
1669
				$showadv = true;
1670
			}
1671
?>
1672
			showadvnwkboot = <?php if ($showadv) {echo 'true';} else {echo 'false';} ?>;
1673
		} else {
1674
			// It was a click, swap the state.
1675
			showadvnwkboot = !showadvnwkboot;
1676
		}
1677

    
1678
		hideCheckbox('netboot', !showadvnwkboot);
1679
		hideInput('nextserver', !showadvnwkboot);
1680
		hideInput('filename', !showadvnwkboot);
1681
		hideInput('filename32', !showadvnwkboot);
1682
		hideInput('filename64', !showadvnwkboot);
1683
		hideInput('rootpath', !showadvnwkboot);
1684

    
1685
		if (showadvnwkboot) {
1686
			text = "<?=gettext('Hide Advanced');?>";
1687
		} else {
1688
			text = "<?=gettext('Display Advanced');?>";
1689
		}
1690
		$('#btnadvnwkboot').html('<i class="fa fa-cog"></i> ' + text);
1691
	}
1692

    
1693
	$('#btnadvnwkboot').click(function(event) {
1694
		show_advnwkboot();
1695
	});
1696

    
1697
	// ---------- On initial page load ------------------------------------------------------------
1698

    
1699
	show_advdns(true);
1700
	show_advmac(true);
1701
	show_advntp(true);
1702
	show_advtftp(true);
1703
	show_advldap(true);
1704
	show_advopts(true);
1705
	show_advnwkboot(true);
1706

    
1707
	// Suppress "Delete row" button if there are fewer than two rows
1708
	checkLastRow();
1709
});
1710
//]]>
1711
</script>
1712

    
1713
<?php include("foot.inc");
(117-117/225)