Project

General

Profile

Download (18.9 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	services_unbound.php
4
*/
5
/* ====================================================================
6
 *	Copyright (c)  2004-2015  Electric Sheep Fencing, LLC. All rights reserved.
7
 *	Copyright (c)  2014 Warren Baker (warren@pfsense.org)
8
 *
9
 *	Redistribution and use in source and binary forms, with or without modification,
10
 *	are permitted provided that the following conditions are met:
11
 *
12
 *	1. Redistributions of source code must retain the above copyright notice,
13
 *		this list of conditions and the following disclaimer.
14
 *
15
 *	2. Redistributions in binary form must reproduce the above copyright
16
 *		notice, this list of conditions and the following disclaimer in
17
 *		the documentation and/or other materials provided with the
18
 *		distribution.
19
 *
20
 *	3. All advertising materials mentioning features or use of this software
21
 *		must display the following acknowledgment:
22
 *		"This product includes software developed by the pfSense Project
23
 *		 for use in the pfSense software distribution. (http://www.pfsense.org/).
24
 *
25
 *	4. The names "pfSense" and "pfSense Project" must not be used to
26
 *		 endorse or promote products derived from this software without
27
 *		 prior written permission. For written permission, please contact
28
 *		 coreteam@pfsense.org.
29
 *
30
 *	5. Products derived from this software may not be called "pfSense"
31
 *		nor may "pfSense" appear in their names without prior written
32
 *		permission of the Electric Sheep Fencing, LLC.
33
 *
34
 *	6. Redistributions of any form whatsoever must retain the following
35
 *		acknowledgment:
36
 *
37
 *	"This product includes software developed by the pfSense Project
38
 *	for use in the pfSense software distribution (http://www.pfsense.org/).
39
 *
40
 *	THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
41
 *	EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
 *	IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43
 *	PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
44
 *	ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45
 *	SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46
 *	NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47
 *	LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48
 *	HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49
 *	STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50
 *	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51
 *	OF THE POSSIBILITY OF SUCH DAMAGE.
52
 *
53
 *	====================================================================
54
 *
55
 */
56

    
57
##|+PRIV
58
##|*IDENT=page-services-dnsresolver
59
##|*NAME=Services: DNS Resolver
60
##|*DESCR=Allow access to the 'Services: DNS Resolver' page.
61
##|*MATCH=services_unbound.php*
62
##|-PRIV
63

    
64
require_once("guiconfig.inc");
65
require_once("unbound.inc");
66
require_once("system.inc");
67

    
68
if (!is_array($config['unbound'])) {
69
	$config['unbound'] = array();
70
}
71

    
72
$a_unboundcfg =& $config['unbound'];
73

    
74
if (!is_array($a_unboundcfg['hosts'])) {
75
	$a_unboundcfg['hosts'] = array();
76
}
77

    
78
$a_hosts =& $a_unboundcfg['hosts'];
79

    
80
if (!is_array($a_unboundcfg['domainoverrides'])) {
81
	$a_unboundcfg['domainoverrides'] = array();
82
}
83

    
84
$a_domainOverrides = &$a_unboundcfg['domainoverrides'];
85

    
86
if (isset($a_unboundcfg['enable'])) {
87
	$pconfig['enable'] = true;
88
}
89
if (isset($a_unboundcfg['dnssec'])) {
90
	$pconfig['dnssec'] = true;
91
}
92
if (isset($a_unboundcfg['forwarding'])) {
93
	$pconfig['forwarding'] = true;
94
}
95
if (isset($a_unboundcfg['regdhcp'])) {
96
	$pconfig['regdhcp'] = true;
97
}
98
if (isset($a_unboundcfg['regdhcpstatic'])) {
99
	$pconfig['regdhcpstatic'] = true;
100
}
101

    
102
$pconfig['port'] = $a_unboundcfg['port'];
103
$pconfig['custom_options'] = base64_decode($a_unboundcfg['custom_options']);
104

    
105
if (empty($a_unboundcfg['active_interface'])) {
106
	$pconfig['active_interface'] = array();
107
} else {
108
	$pconfig['active_interface'] = explode(",", $a_unboundcfg['active_interface']);
109
}
110

    
111
if (empty($a_unboundcfg['outgoing_interface'])) {
112
	$pconfig['outgoing_interface'] = array();
113
} else {
114
	$pconfig['outgoing_interface'] = explode(",", $a_unboundcfg['outgoing_interface']);
115
}
116

    
117
if (empty($a_unboundcfg['system_domain_local_zone_type'])) {
118
	$pconfig['system_domain_local_zone_type'] = "transparent";
119
} else {
120
	$pconfig['system_domain_local_zone_type'] = $a_unboundcfg['system_domain_local_zone_type'];
121
}
122

    
123
if ($_POST) {
124
	if ($_POST['apply']) {
125
		$retval = services_unbound_configure();
126
		$savemsg = get_std_save_message($retval);
127
		if ($retval == 0) {
128
			clear_subsystem_dirty('unbound');
129
		}
130
		/* Update resolv.conf in case the interface bindings exclude localhost. */
131
		system_resolvconf_generate();
132
		/* Start or restart dhcpleases when it's necessary */
133
		system_dhcpleases_configure();
134
	} else {
135
		$pconfig = $_POST;
136
		unset($input_errors);
137

    
138
		if (isset($pconfig['enable']) && isset($config['dnsmasq']['enable'])) {
139
			if ($pconfig['port'] == $config['dnsmasq']['port']) {
140
				$input_errors[] = gettext("The DNS Forwarder is enabled using this port. Choose a non-conflicting port, or disable the DNS Forwarder.");
141
			}
142
		}
143

    
144
		// forwarding mode requires having valid DNS servers
145
		if (isset($pconfig['forwarding'])) {
146
			$founddns = false;
147
			if (isset($config['system']['dnsallowoverride'])) {
148
				$dns_servers = get_dns_servers();
149
				if (is_array($dns_servers)) {
150
					foreach ($dns_servers as $dns_server) {
151
						if (!ip_in_subnet($dns_server, "127.0.0.0/8")) {
152
							$founddns = true;
153
						}
154
					}
155
				}
156
			}
157
			if (is_array($config['system']['dnsserver'])) {
158
				foreach ($config['system']['dnsserver'] as $dnsserver) {
159
					if (is_ipaddr($dnsserver)) {
160
						$founddns = true;
161
					}
162
				}
163
			}
164
			if ($founddns == false) {
165
				$input_errors[] = gettext("At least one DNS server must be specified under System>General Setup to enable Forwarding mode.");
166
			}
167
		}
168

    
169
		if (empty($pconfig['active_interface'])) {
170
			$input_errors[] = gettext("One or more Network Interfaces must be selected for binding.");
171
		} else if (!isset($config['system']['dnslocalhost']) && (!in_array("lo0", $pconfig['active_interface']) && !in_array("all", $pconfig['active_interface']))) {
172
			$input_errors[] = gettext("This system is configured to use the DNS Resolver as its DNS server, so Localhost or All must be selected in Network Interfaces.");
173
		}
174

    
175
		if (empty($pconfig['outgoing_interface'])) {
176
			$input_errors[] = gettext("One or more Outgoing Network Interfaces must be selected.");
177
		}
178

    
179
		if ($pconfig['port'] && !is_port($pconfig['port'])) {
180
			$input_errors[] = gettext("A valid port number must be specified.");
181
		}
182

    
183
		if (is_array($pconfig['active_interface']) && !empty($pconfig['active_interface'])) {
184
			$display_active_interface = $pconfig['active_interface'];
185
			$pconfig['active_interface'] = implode(",", $pconfig['active_interface']);
186
		}
187

    
188
		$display_custom_options = $pconfig['custom_options'];
189
		$pconfig['custom_options'] = base64_encode(str_replace("\r\n", "\n", $pconfig['custom_options']));
190

    
191
		if (is_array($pconfig['outgoing_interface']) && !empty($pconfig['outgoing_interface'])) {
192
			$display_outgoing_interface = $pconfig['outgoing_interface'];
193
			$pconfig['outgoing_interface'] = implode(",", $pconfig['outgoing_interface']);
194
		}
195

    
196
		$test_output = array();
197
		if (test_unbound_config($pconfig, $test_output)) {
198
			$input_errors[] = gettext("The generated config file cannot be parsed by unbound. Please correct the following errors:");
199
			$input_errors = array_merge($input_errors, $test_output);
200
		}
201

    
202
		if (!$input_errors) {
203
			$a_unboundcfg['enable'] = isset($pconfig['enable']);
204
			$a_unboundcfg['port'] = $pconfig['port'];
205
			$a_unboundcfg['dnssec'] = isset($pconfig['dnssec']);
206
			$a_unboundcfg['forwarding'] = isset($pconfig['forwarding']);
207
			$a_unboundcfg['regdhcp'] = isset($pconfig['regdhcp']);
208
			$a_unboundcfg['regdhcpstatic'] = isset($pconfig['regdhcpstatic']);
209
			$a_unboundcfg['active_interface'] = $pconfig['active_interface'];
210
			$a_unboundcfg['outgoing_interface'] = $pconfig['outgoing_interface'];
211
			$a_unboundcfg['system_domain_local_zone_type'] = $pconfig['system_domain_local_zone_type'];
212
			$a_unboundcfg['custom_options'] = $pconfig['custom_options'];
213

    
214
			write_config(gettext("DNS Resolver configured."));
215
			mark_subsystem_dirty('unbound');
216
		}
217

    
218
		$pconfig['active_interface'] = $display_active_interface;
219
		$pconfig['outgoing_interface'] = $display_outgoing_interface;
220
		$pconfig['custom_options'] = $display_custom_options;
221
	}
222
}
223

    
224
if ($pconfig['custom_options']) {
225
	$customoptions = true;
226
} else {
227
	$customoptions = false;
228
}
229

    
230
if ($_GET['act'] == "del") {
231
	if ($_GET['type'] == 'host') {
232
		if ($a_hosts[$_GET['id']]) {
233
			unset($a_hosts[$_GET['id']]);
234
			write_config();
235
			mark_subsystem_dirty('unbound');
236
			header("Location: services_unbound.php");
237
			exit;
238
		}
239
	} elseif ($_GET['type'] == 'doverride') {
240
		if ($a_domainOverrides[$_GET['id']]) {
241
			unset($a_domainOverrides[$_GET['id']]);
242
			write_config();
243
			mark_subsystem_dirty('unbound');
244
			header("Location: services_unbound.php");
245
			exit;
246
		}
247
	}
248
}
249

    
250
function build_if_list($selectedifs) {
251
	$interface_addresses = get_possible_listen_ips(true);
252
	$iflist = array('options' => array(), 'selected' => array());
253

    
254
	$iflist['options']['all']	= gettext("All");
255
	if (empty($selectedifs) || empty($selectedifs[0]) || in_array("all", $selectedifs)) {
256
		array_push($iflist['selected'], "all");
257
	}
258

    
259
	foreach ($interface_addresses as $laddr => $ldescr) {
260
		$iflist['options'][$laddr] = htmlspecialchars($ldescr);
261

    
262
		if ($selectedifs && in_array($laddr, $selectedifs)) {
263
			array_push($iflist['selected'], $laddr);
264
		}
265
	}
266

    
267
	unset($interface_addresses);
268

    
269
	return($iflist);
270
}
271

    
272
$pgtitle = array(gettext("Services"), gettext("DNS Resolver"), gettext("General Settings"));
273
$shortcut_section = "resolver";
274

    
275
include_once("head.inc");
276

    
277
if ($input_errors) {
278
	print_input_errors($input_errors);
279
}
280

    
281
if ($savemsg) {
282
	print_info_box($savemsg, 'success');
283
}
284

    
285
if (is_subsystem_dirty('unbound')) {
286
	print_apply_box(gettext("The DNS resolver configuration has been changed.") . "<br />" . gettext("The changes must be applied for them to take effect."));
287
}
288

    
289
$tab_array = array();
290
$tab_array[] = array(gettext("General Settings"), true, "services_unbound.php");
291
$tab_array[] = array(gettext("Advanced Settings"), false, "services_unbound_advanced.php");
292
$tab_array[] = array(gettext("Access Lists"), false, "/services_unbound_acls.php");
293
display_top_tabs($tab_array, true);
294

    
295
$form = new Form();
296

    
297
$section = new Form_Section('General DNS Resolver Options');
298

    
299
$section->addInput(new Form_Checkbox(
300
	'enable',
301
	'Enable',
302
	'Enable DNS resolver',
303
	$pconfig['enable']
304
));
305

    
306
$section->addInput(new Form_Input(
307
	'port',
308
	'Listen Port',
309
	'number',
310
	$pconfig['port'],
311
	['placeholder' => '53']
312
))->setHelp('The port used for responding to DNS queries. It should normally be left blank unless another service needs to bind to TCP/UDP port 53.');
313

    
314
$activeiflist = build_if_list($pconfig['active_interface']);
315

    
316
$section->addInput(new Form_Select(
317
	'active_interface',
318
	'Network Interfaces',
319
	$activeiflist['selected'],
320
	$activeiflist['options'],
321
	true
322
))->addClass('general')->setHelp('Interface IPs used by the DNS Resolver for responding to queries from clients. If an interface has both IPv4 and IPv6 IPs, both are used. Queries to other interface IPs not selected below are discarded. ' .
323
			'The default behavior is to respond to queries on every available IPv4 and IPv6 address.');
324

    
325
$outiflist = build_if_list($pconfig['outgoing_interface']);
326

    
327
$section->addInput(new Form_Select(
328
	'outgoing_interface',
329
	'Outgoing Network Interfaces',
330
	$outiflist['selected'],
331
	$outiflist['options'],
332
	true
333
))->addClass('general')->setHelp('Utilize different network interface(s) that the DNS Resolver will use to send queries to authoritative servers and receive their replies. By default all interfaces are used.');
334

    
335
$section->addInput(new Form_Select(
336
	'system_domain_local_zone_type',
337
	'System Domain Local Zone Type',
338
	$pconfig['system_domain_local_zone_type'],
339
	unbound_local_zone_types()
340
))->setHelp('The local-zone type used for the pfSense system domain (System | General Setup | Domain).  Transparent is the default.  Local-Zone type descriptions are available in the unbound.conf(5) manual pages.');
341

    
342
$section->addInput(new Form_Checkbox(
343
	'dnssec',
344
	'DNSSEC',
345
	'Enable DNSSEC Support',
346
	$pconfig['dnssec']
347
));
348

    
349
$section->addInput(new Form_Checkbox(
350
	'forwarding',
351
	'DNS Query Forwarding',
352
	'Enable Forwarding Mode',
353
	$pconfig['forwarding']
354
));
355

    
356
$section->addInput(new Form_Checkbox(
357
	'regdhcp',
358
	'DHCP Registration',
359
	'Register DHCP leases in the DNS Resolver',
360
	$pconfig['regdhcp']
361
))->setHelp(sprintf('If this option is set, then machines that specify their hostname when requesting a DHCP lease will be registered'.
362
					' in the DNS Resolver, so that their name can be resolved.'.
363
					' The domain in %sSystem: General setup%s should also be set to the proper value.','<a href="system.php">','</a>'));
364

    
365
$section->addInput(new Form_Checkbox(
366
	'regdhcpstatic',
367
	'Static DHCP',
368
	'Register DHCP static mappings in the DNS Resolver',
369
	$pconfig['regdhcpstatic']
370
))->setHelp(sprintf('If this option is set, then DHCP static mappings will be registered in the DNS Resolver, so that their name can be resolved. '.
371
					'The domain in %sSystem: General setup%s should also be set to the proper value.','<a href="system.php">','</a>'));
372

    
373
$btnadv = new Form_Button(
374
	'btnadvcustom',
375
	'Custom options',
376
	null,
377
	'fa-cog'
378
);
379

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

    
382
$section->addInput(new Form_StaticText(
383
	'Display Custom Options',
384
	$btnadv
385
));
386

    
387
$section->addInput(new Form_Textarea (
388
	'custom_options',
389
	'Custom options',
390
	$pconfig['custom_options']
391
))->setHelp('Enter any additional configuration parameters to add to the DNS Resolver configuration here, separated by a newline');
392

    
393
$form->add($section);
394
print($form);
395
?>
396

    
397
<script type="text/javascript">
398
//<![CDATA[
399
events.push(function() {
400

    
401
	// Show advanced custom options ==============================================
402
	var showadvcustom = false;
403

    
404
	function show_advcustom(ispageload) {
405
		var text;
406
		// On page load decide the initial state based on the data.
407
		if (ispageload) {
408
			showadvcustom = <?=($customoptions ? 'true' : 'false');?>;
409
		} else {
410
			// It was a click, swap the state.
411
			showadvcustom = !showadvcustom;
412
		}
413

    
414
		hideInput('custom_options', !showadvcustom);
415

    
416
		if (showadvcustom) {
417
			text = "<?=gettext('Hide Custom Options');?>";
418
		} else {
419
			text = "<?=gettext('Display Custom Options');?>";
420
		}
421
		$('#btnadvcustom').html('<i class="fa fa-cog"></i> ' + text);
422
	}
423

    
424
	// If the enable checkbox is not checked, hide all inputs
425
	function hideGeneral() {
426
		var hide = ! $('#enable').prop('checked');
427

    
428
		hideMultiClass('general', hide);
429
		hideInput('port', hide);
430
		hideSelect('system_domain_local_zone_type', hide);
431
		hideCheckbox('dnssec', hide);
432
		hideCheckbox('forwarding', hide);
433
		hideCheckbox('regdhcp', hide);
434
		hideCheckbox('regdhcpstatic', hide);
435
		hideInput('btnadvcustom', hide);
436
		hideInput('custom_options', hide || !showadvcustom);
437
	}
438

    
439
	// Un-hide additional controls
440
	$('#btnadvcustom').click(function(event) {
441
		show_advcustom();
442
	});
443

    
444
	// When 'enable' is clicked, disable/enable the following hide inputs
445
	$('#enable').click(function() {
446
		hideGeneral();
447
	});
448

    
449
	// On initial load
450
	if ($('#custom_options').val().length == 0) {
451
		hideInput('custom_options', true);
452
	}
453

    
454
	hideGeneral();
455
	show_advcustom(true);
456

    
457
});
458
//]]>
459
</script>
460

    
461
<div class="panel panel-default">
462
	<div class="panel-heading"><h2 class="panel-title"><?=gettext("Host Overrides")?></h2></div>
463
	<div class="panel-body table-responsive">
464
		<table class="table table-striped table-hover table-condensed sortable-theme-bootstrap" data-sortable>
465
			<thead>
466
				<tr>
467
					<th><?=gettext("Host")?></th>
468
					<th><?=gettext("Domain")?></th>
469
					<th><?=gettext("IP")?></th>
470
					<th><?=gettext("Description")?></th>
471
					<th><?=gettext("Actions")?></th>
472
				</tr>
473
			</thead>
474
			<tbody>
475
<?php
476
$i = 0;
477
foreach ($a_hosts as $hostent):
478
?>
479
				<tr>
480
					<td>
481
						<?=$hostent['host']?>
482
					</td>
483
					<td>
484
						<?=$hostent['domain']?>
485
					</td>
486
					<td>
487
						<?=$hostent['ip']?>
488
					</td>
489
					<td>
490
						<?=htmlspecialchars($hostent['descr'])?>
491
					</td>
492
					<td>
493
						<a class="fa fa-pencil"	title="<?=gettext('Edit host override')?>" href="services_unbound_host_edit.php?id=<?=$i?>"></a>
494
						<a class="fa fa-trash"	title="<?=gettext('Delete host override')?>" href="services_unbound.php?type=host&amp;act=del&amp;id=<?=$i?>"></a>
495
					</td>
496
				</tr>
497

    
498
<?php
499
	if ($hostent['aliases']['item'] && is_array($hostent['aliases']['item'])):
500
		foreach ($hostent['aliases']['item'] as $alias):
501
?>
502
				<tr>
503
					<td>
504
						<?=$alias['host']?>
505
					</td>
506
					<td>
507
						<?=$alias['domain']?>
508
					</td>
509
					<td>
510
						<?=gettext("Alias for ");?><?=$hostent['host'] ? $hostent['host'] . '.' . $hostent['domain'] : $hostent['domain']?>
511
					</td>
512
					<td>
513
						<i class="fa fa-angle-double-right text-info"></i>
514
						<?=htmlspecialchars($alias['description'])?>
515
					</td>
516
					<td>
517
						<a a class="fa fa-pencil"	title="<?=gettext('Edit host override')?>" 	href="services_unbound_host_edit.php?id=<?=$i?>"></a>
518
					</td>
519
				</tr>
520
<?php
521
		endforeach;
522
	endif;
523
	$i++;
524
endforeach;
525
?>
526
			</tbody>
527
		</table>
528
	</div>
529
</div>
530

    
531
<nav class="action-buttons">
532
	<a href="services_unbound_host_edit.php" class="btn btn-sm btn-success">
533
		<i class="fa fa-plus icon-embed-btn"></i>
534
		<?=gettext('Add')?>
535
	</a>
536
</nav>
537

    
538
<div class="panel panel-default">
539
	<div class="panel-heading"><h2 class="panel-title"><?=gettext("Domain Overrides")?></h2></div>
540
	<div class="panel-body table-responsive">
541
		<table class="table table-striped table-hover table-condensed sortable-theme-bootstrap" data-sortable>
542
			<thead>
543
				<tr>
544
					<th><?=gettext("Domain")?></th>
545
					<th><?=gettext("IP")?></th>
546
					<th><?=gettext("Description")?></th>
547
					<th><?=gettext("Actions")?></th>
548
				</tr>
549
			</thead>
550

    
551
			<tbody>
552
<?php
553
$i = 0;
554
foreach ($a_domainOverrides as $doment):
555
?>
556
				<tr>
557
					<td>
558
						<?=$doment['domain']?>&nbsp;
559
					</td>
560
					<td>
561
						<?=$doment['ip']?>&nbsp;
562
					</td>
563
					<td>
564
						<?=htmlspecialchars($doment['descr'])?>&nbsp;
565
					</td>
566
					<td>
567
						<a class="fa fa-pencil"	title="<?=gettext('Edit domain override')?>" href="services_unbound_domainoverride_edit.php?id=<?=$i?>"></a>
568
						<a class="fa fa-trash"	title="<?=gettext('Delete domain override')?>" href="services_unbound.php?act=del&amp;type=doverride&amp;id=<?=$i?>"></a>
569
					</td>
570
				</tr>
571
<?php
572
	$i++;
573
endforeach;
574
?>
575
			</tbody>
576
		</table>
577
	</div>
578
</div>
579

    
580
<nav class="action-buttons">
581
	<a href="services_unbound_domainoverride_edit.php" class="btn btn-sm btn-success">
582
		<i class="fa fa-plus icon-embed-btn"></i>
583
		<?=gettext('Add')?>
584
	</a>
585
</nav>
586

    
587
<div class="infoblock">
588
	<?php print_info_box(sprintf(gettext("If the DNS Resolver is enabled, the DHCP".
589
		" service (if enabled) will automatically serve the LAN IP".
590
		" address as a DNS server to DHCP clients so they will use".
591
		" the DNS Resolver. If Forwarding is enabled, the DNS Resolver will use the DNS servers".
592
		" entered in %sSystem: General setup%s".
593
		" or those obtained via DHCP or PPP on WAN if &quot;Allow".
594
		" DNS server list to be overridden by DHCP/PPP on WAN&quot;".
595
		" is checked."), '<a href="system.php">', '</a>'), 'info', false); ?>
596
</div>
597

    
598
<?php include("foot.inc");
(141-141/225)