Project

General

Profile

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

    
59
##|+PRIV
60
##|*IDENT=page-services-dnsforwarder
61
##|*NAME=Services: DNS Forwarder
62
##|*DESCR=Allow access to the 'Services: DNS Forwarder' page.
63
##|*MATCH=services_dnsmasq.php*
64
##|-PRIV
65

    
66
require_once("guiconfig.inc");
67
require_once("functions.inc");
68
require_once("filter.inc");
69
require_once("shaper.inc");
70
require_once("system.inc");
71

    
72
// Sort host entries for display in alphabetical order
73
function hostcmp($a, $b) {
74
	return strcasecmp($a['host'], $b['host']);
75
}
76

    
77
function hosts_sort() {
78
	global $a_hosts;
79

    
80
	if (!is_array($a_hosts)) {
81
		return;
82
	}
83

    
84
	usort($a_hosts, "hostcmp");
85
}
86

    
87
// Sort domain entries for display in alphabetical order
88
function domaincmp($a, $b) {
89
	return strcasecmp($a['domain'], $b['domain']);
90
}
91

    
92
function domains_sort() {
93
	global $a_domainOverrides;
94

    
95
	if (!is_array($a_domainOverrides)) {
96
		return;
97
	}
98

    
99
	usort($a_domainOverrides, "domaincmp");
100
}
101

    
102
$pconfig['enable'] = isset($config['dnsmasq']['enable']);
103
$pconfig['regdhcp'] = isset($config['dnsmasq']['regdhcp']);
104
$pconfig['regdhcpstatic'] = isset($config['dnsmasq']['regdhcpstatic']);
105
$pconfig['dhcpfirst'] = isset($config['dnsmasq']['dhcpfirst']);
106
$pconfig['strict_order'] = isset($config['dnsmasq']['strict_order']);
107
$pconfig['domain_needed'] = isset($config['dnsmasq']['domain_needed']);
108
$pconfig['no_private_reverse'] = isset($config['dnsmasq']['no_private_reverse']);
109
$pconfig['port'] = $config['dnsmasq']['port'];
110
$pconfig['custom_options'] = $config['dnsmasq']['custom_options'];
111
$pconfig['strictbind'] = isset($config['dnsmasq']['strictbind']);
112

    
113
if (!empty($config['dnsmasq']['interface'])) {
114
	$pconfig['interface'] = explode(",", $config['dnsmasq']['interface']);
115
} else {
116
	$pconfig['interface'] = array();
117
}
118

    
119
if (!is_array($config['dnsmasq']['hosts'])) {
120
	$config['dnsmasq']['hosts'] = array();
121
}
122

    
123
if (!is_array($config['dnsmasq']['domainoverrides'])) {
124
	$config['dnsmasq']['domainoverrides'] = array();
125
}
126

    
127
$a_hosts = &$config['dnsmasq']['hosts'];
128

    
129
// Add a temporary index so we don't lose the order after sorting
130
for ($idx=0; $idx<count($a_hosts); $idx++) {
131
	$a_hosts[$idx]['idx'] = $idx;
132
}
133

    
134
hosts_sort();
135

    
136
$a_domainOverrides = &$config['dnsmasq']['domainoverrides'];
137

    
138
// Add a temporary index so we don't lose the order after sorting
139
for ($idx=0; $idx<count($a_domainOverrides); $idx++) {
140
	$a_domainOverrides[$idx]['idx'] = $idx;
141
}
142

    
143
domains_sort();
144

    
145
if ($_POST) {
146
	if ($_POST['apply']) {
147
		$retval = 0;
148
		$retval = services_dnsmasq_configure();
149
		$savemsg = get_std_save_message($retval);
150

    
151
		// Reload filter (we might need to sync to CARP hosts)
152
		filter_configure();
153
		/* Update resolv.conf in case the interface bindings exclude localhost. */
154
		system_resolvconf_generate();
155
		/* Start or restart dhcpleases when it's necessary */
156
		system_dhcpleases_configure();
157

    
158
		if ($retval == 0) {
159
			clear_subsystem_dirty('hosts');
160
		}
161
	} else {
162
		$pconfig = $_POST;
163
		unset($input_errors);
164

    
165
		$config['dnsmasq']['enable'] = ($_POST['enable']) ? true : false;
166
		$config['dnsmasq']['regdhcp'] = ($_POST['regdhcp']) ? true : false;
167
		$config['dnsmasq']['regdhcpstatic'] = ($_POST['regdhcpstatic']) ? true : false;
168
		$config['dnsmasq']['dhcpfirst'] = ($_POST['dhcpfirst']) ? true : false;
169
		$config['dnsmasq']['strict_order'] = ($_POST['strict_order']) ? true : false;
170
		$config['dnsmasq']['domain_needed'] = ($_POST['domain_needed']) ? true : false;
171
		$config['dnsmasq']['no_private_reverse'] = ($_POST['no_private_reverse']) ? true : false;
172
		$config['dnsmasq']['custom_options'] = str_replace("\r\n", "\n", $_POST['custom_options']);
173
		$config['dnsmasq']['strictbind'] = ($_POST['strictbind']) ? true : false;
174

    
175
		if (isset($_POST['enable']) && isset($config['unbound']['enable'])) {
176
			if ($_POST['port'] == $config['unbound']['port']) {
177
				$input_errors[] = gettext("The DNS Resolver is enabled using this port. Choose a non-conflicting port, or disable DNS Resolver.");
178
			}
179
		}
180

    
181
		if ($_POST['port']) {
182
			if (is_port($_POST['port'])) {
183
				$config['dnsmasq']['port'] = $_POST['port'];
184
			} else {
185
				$input_errors[] = gettext("A valid port number must be specified.");
186
			}
187
		} else if (isset($config['dnsmasq']['port'])) {
188
			unset($config['dnsmasq']['port']);
189
		}
190

    
191
		if (is_array($_POST['interface'])) {
192
			$config['dnsmasq']['interface'] = implode(",", $_POST['interface']);
193
		} elseif (isset($config['dnsmasq']['interface'])) {
194
			unset($config['dnsmasq']['interface']);
195
		}
196

    
197
		if ($config['dnsmasq']['custom_options']) {
198
			$args = '';
199
			foreach (preg_split('/\s+/', $config['dnsmasq']['custom_options']) as $c) {
200
				$args .= escapeshellarg("--{$c}") . " ";
201
			}
202
			exec("/usr/local/sbin/dnsmasq --test $args", $output, $rc);
203
			if ($rc != 0) {
204
				$input_errors[] = gettext("Invalid custom options");
205
			}
206
		}
207

    
208
		if (!$input_errors) {
209
			write_config();
210
			mark_subsystem_dirty('hosts');
211
		}
212
	}
213
}
214

    
215
if ($_GET['act'] == "del") {
216
	if ($_GET['type'] == 'host') {
217
		if ($a_hosts[$_GET['id']]) {
218
			unset($a_hosts[$_GET['id']]);
219
			write_config();
220
			mark_subsystem_dirty('hosts');
221
			header("Location: services_dnsmasq.php");
222
			exit;
223
		}
224
	} elseif ($_GET['type'] == 'doverride') {
225
		if ($a_domainOverrides[$_GET['id']]) {
226
			unset($a_domainOverrides[$_GET['id']]);
227
			write_config();
228
			mark_subsystem_dirty('hosts');
229
			header("Location: services_dnsmasq.php");
230
			exit;
231
		}
232
	}
233
}
234

    
235
function build_if_list() {
236
	global $pconfig;
237

    
238
	$interface_addresses = get_possible_listen_ips(true);
239
	$iflist = array('options' => array(), 'selected' => array());
240

    
241
	$iflist['options'][""]	= "All";
242
	if (empty($pconfig['interface']) || empty($pconfig['interface'][0])) {
243
		array_push($iflist['selected'], "");
244
	}
245

    
246
	foreach ($interface_addresses as $laddr => $ldescr) {
247
		$iflist['options'][$laddr] = htmlspecialchars($ldescr);
248

    
249
		if ($pconfig['interface'] && in_array($laddr, $pconfig['interface'])) {
250
			array_push($iflist['selected'], $laddr);
251
		}
252
	}
253

    
254
	unset($interface_addresses);
255

    
256
	return($iflist);
257
}
258

    
259
$pgtitle = array(gettext("Services"), gettext("DNS Forwarder"));
260
$shortcut_section = "forwarder";
261
include("head.inc");
262

    
263
if ($input_errors) {
264
	print_input_errors($input_errors);
265
}
266

    
267
if ($savemsg) {
268
	print_info_box($savemsg, 'success');
269
}
270

    
271
if (is_subsystem_dirty('hosts')) {
272
	print_apply_box(gettext("The DNS forwarder configuration has been changed.") . "<br />" . gettext("The changes must be applied for them to take effect."));
273
}
274

    
275
$form = new Form();
276

    
277
$section = new Form_Section('General DNS Forwarder Options');
278

    
279
$section->addInput(new Form_Checkbox(
280
	'enable',
281
	'Enable',
282
	'Enable DNS forwarder',
283
	$pconfig['enable']
284
))->toggles('.toggle-dhcp', 'disable');
285

    
286
$section->addInput(new Form_Checkbox(
287
	'regdhcp',
288
	'DHCP Registration',
289
	'Register DHCP leases in DNS forwarder',
290
	$pconfig['regdhcp']
291
))->setHelp(sprintf("If this option is set, then machines that specify".
292
			" their hostname when requesting a DHCP lease will be registered".
293
			" in the DNS forwarder, so that their name can be resolved.".
294
			" The domain in %sSystem: General Setup%s should also".
295
			" be set to the proper value.",'<a href="system.php">','</a>'))
296
	->addClass('toggle-dhcp');
297

    
298
$section->addInput(new Form_Checkbox(
299
	'regdhcpstatic',
300
	'Static DHCP',
301
	'Register DHCP static mappings in DNS forwarder',
302
	$pconfig['regdhcpstatic']
303
))->setHelp(sprintf("If this option is set, then DHCP static mappings will ".
304
					"be registered in the DNS forwarder, so that their name can be ".
305
					"resolved. The domain in %sSystem: General Setup%s should also ".
306
					"be set to the proper value.",'<a href="system.php">','</a>'))
307
	->addClass('toggle-dhcp');
308

    
309
$section->addInput(new Form_Checkbox(
310
	'dhcpfirst',
311
	'Prefer DHCP',
312
	'Resolve DHCP mappings first',
313
	$pconfig['dhcpfirst']
314
))->setHelp(sprintf("If this option is set, then DHCP mappings will ".
315
					"be resolved before the manual list of names below. This only ".
316
					"affects the name given for a reverse lookup (PTR)."))
317
	->addClass('toggle-dhcp');
318

    
319
$group = new Form_Group('DNS Query Forwarding');
320

    
321
$group->add(new Form_Checkbox(
322
	'strict_order',
323
	'DNS Query Forwarding',
324
	'Query DNS servers sequentially',
325
	$pconfig['strict_order']
326
))->setHelp(sprintf("If this option is set, %s DNS Forwarder (dnsmasq) will ".
327
					"query the DNS servers sequentially in the order specified (<i>System - General Setup - DNS Servers</i>), ".
328
					"rather than all at once in parallel. ", $g['product_name']));
329

    
330
$group->add(new Form_Checkbox(
331
	'domain_needed',
332
	null,
333
	'Require domain',
334
	$pconfig['domain_needed']
335
))->setHelp(sprintf("If this option is set, %s DNS Forwarder (dnsmasq) will ".
336
					"not forward A or AAAA queries for plain names, without dots or domain parts, to upstream name servers.	 ".
337
					"If the name is not known from /etc/hosts or DHCP then a \"not found\" answer is returned. ", $g['product_name']));
338

    
339
$group->add(new Form_Checkbox(
340
	'no_private_reverse',
341
	null,
342
	'Do not forward private reverse lookups',
343
	$pconfig['no_private_reverse']
344
))->setHelp(sprintf("If this option is set, %s DNS Forwarder (dnsmasq) will ".
345
					"not forward reverse DNS lookups (PTR) for private addresses (RFC 1918) to upstream name servers.  ".
346
					"Any entries in the Domain Overrides section forwarding private \"n.n.n.in-addr.arpa\" names to a specific server are still forwarded. ".
347
					"If the IP to name is not known from /etc/hosts, DHCP or a specific domain override then a \"not found\" answer is immediately returned. ", $g['product_name']));
348

    
349
$section->add($group);
350

    
351
$section->addInput(new Form_Input(
352
	'port',
353
	'Listen Port',
354
	'number',
355
	$pconfig['port'],
356
	['placeholder' => '53']
357
))->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.');
358

    
359
$iflist = build_if_list();
360

    
361
$section->addInput(new Form_Select(
362
	'interface',
363
	'Interfaces',
364
	$iflist['selected'],
365
	$iflist['options'],
366
	true
367
))->setHelp('Interface IPs used by the DNS Forwarder 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. ' .
368
			'The default behavior is to respond to queries on every available IPv4 and IPv6 address.');
369

    
370
$section->addInput(new Form_Checkbox(
371
	'strictbind',
372
	'Strict binding',
373
	'Strict interface binding',
374
	$pconfig['strictbind']
375
))->setHelp('If this option is set, the DNS forwarder will only bind to the interfaces containing the IP addresses selected above, ' .
376
					'rather than binding to all interfaces and discarding queries to other addresses.' . '<br /><br />' .
377
					'This option does NOT work with IPv6. If set, dnsmasq will not bind to IPv6 addresses.');
378

    
379
$section->addInput(new Form_Textarea(
380
	'custom_options',
381
	'Custom options',
382
	$pconfig['custom_options']
383
))->setHelp('Enter any additional options to add to the dnsmasq configuration here, separated by a space or newline.')
384
  ->addClass('advanced');
385

    
386
$form->add($section);
387
print($form);
388

    
389
?>
390
<div class="panel panel-default">
391
	<div class="panel-heading"><h2 class="panel-title"><?=gettext("Host Overrides")?></h2></div>
392
	<div class="panel-body table-responsive">
393
		<table class="table table-striped table-hover table-condensed sortable-theme-bootstrap table-rowdblclickedit" data-sortable>
394
			<thead>
395
				<tr>
396
					<th><?=gettext("Host")?></th>
397
					<th><?=gettext("Domain")?></th>
398
					<th><?=gettext("IP")?></th>
399
					<th><?=gettext("Description")?></th>
400
					<th><?=gettext("Actions")?></th>
401
				</tr>
402
			</thead>
403
			<tbody>
404
<?php
405
foreach ($a_hosts as $i => $hostent):
406
?>
407
				<tr>
408
					<td>
409
						<?=$hostent['host']?>
410
					</td>
411
					<td>
412
						<?=$hostent['domain']?>
413
					</td>
414
					<td>
415
						<?=$hostent['ip']?>
416
					</td>
417
					<td>
418
						<?=htmlspecialchars($hostent['descr'])?>
419
					</td>
420
					<td>
421
						<a class="fa fa-pencil"	title="<?=gettext('Edit host override')?>" 	href="services_dnsmasq_edit.php?id=<?=$hostent['idx']?>"></a>
422
						<a class="fa fa-trash"	title="<?=gettext('Delete host override')?>"	href="services_dnsmasq.php?type=host&amp;act=del&amp;id=<?=$hostent['idx']?>"></a>
423
					</td>
424
				</tr>
425

    
426
<?php
427
	if ($hostent['aliases']['item'] && is_array($hostent['aliases']['item'])):
428
		foreach ($hostent['aliases']['item'] as $alias):
429
?>
430
				<tr>
431
					<td>
432
						<?=$alias['host']?>
433
					</td>
434
					<td>
435
						<?=$alias['domain']?>
436
					</td>
437
					<td>
438
						<?=gettext("Alias for ");?><?=$hostent['host'] ? $hostent['host'] . '.' . $hostent['domain'] : $hostent['domain']?>
439
					</td>
440
					<td>
441
						<i class="fa fa-angle-double-right text-info"></i>
442
						<?=htmlspecialchars($alias['description'])?>
443
					</td>
444
					<td>
445
						<a class="fa fa-pencil"	title="<?=gettext('Edit host override')?>" 	href="services_dnsmasq_edit.php?id=<?=$i?>"></a>
446
					</td>
447
				</tr>
448
<?php
449
		endforeach;
450
	endif;
451
endforeach;
452
?>
453
			</tbody>
454
		</table>
455
	</div>
456
</div>
457

    
458
<nav class="action-buttons">
459
	<a href="services_dnsmasq_edit.php" class="btn btn-sm btn-success btn-sm">
460
		<i class="fa fa-plus icon-embed-btn"></i>
461
		<?=gettext('Add')?>
462
	</a>
463
</nav>
464

    
465
<div class="panel panel-default">
466
	<div class="panel-heading"><h2 class="panel-title"><?=gettext("Domain Overrides")?></h2></div>
467
	<div class="panel-body table-responsive">
468
		<table class="table table-striped table-hover table-condensed sortable-theme-bootstrap table-rowdblclickedit" data-sortable>
469
			<thead>
470
				<tr>
471
					<th><?=gettext("Domain")?></th>
472
					<th><?=gettext("IP")?></th>
473
					<th><?=gettext("Description")?></th>
474
					<th><?=gettext("Actions")?></th>
475
				</tr>
476
			</thead>
477

    
478
			<tbody>
479
<?php
480
foreach ($a_domainOverrides as $i => $doment):
481
?>
482
				<tr>
483
					<td>
484
						<?=$doment['domain']?>
485
					</td>
486
					<td>
487
						<?=$doment['ip']?>
488
					</td>
489
					<td>
490
						<?=htmlspecialchars($doment['descr'])?>
491
					</td>
492
					<td>
493
						<a class="fa fa-pencil"	title="<?=gettext('Edit domain override')?>" href="services_dnsmasq_domainoverride_edit.php?id=<?=$doment['idx']?>"></a>
494
						<a class="fa fa-trash"	title="<?=gettext('Delete domain override')?>" href="services_dnsmasq.php?act=del&amp;type=doverride&amp;id=<?=$doment['idx']?>"></a>
495
					</td>
496
				</tr>
497
<?php
498
endforeach;
499
?>
500
			</tbody>
501
		</table>
502
	</div>
503
</div>
504

    
505
<nav class="action-buttons">
506
	<a href="services_dnsmasq_domainoverride_edit.php" class="btn btn-sm btn-success btn-sm">
507
		<i class="fa fa-plus icon-embed-btn"></i>
508
		<?=gettext('Add')?>
509
	</a>
510
</nav>
511
<div class="infoblock">
512
<?php
513
print_info_box(
514
	'<p>' .
515
	gettext('If the DNS forwarder is enabled, the DHCP service (if enabled) will automatically' .
516
		    ' serve the LAN IP address as a DNS server to DHCP clients so they will use the forwarder.') . '</p><p>' .
517
	sprintf(gettext('The DNS forwarder will use the DNS servers entered in %1$sSystem > General Setup%2$s or' .
518
				    ' those obtained via DHCP or PPP on WAN if &quot;Allow DNS server list to be overridden by DHCP/PPP on WAN&quot; is checked.' .
519
				    ' If that option is not used (or if a static IP address is used on WAN),' .
520
				    ' at least one DNS server must be manually specified on the %1$sSystem > General Setup%2$s page.'),
521
			'<a href="system.php">',
522
			'</a>') .
523
	'</p>',
524
	'info',
525
	false
526
);
527
?>
528
</div>
529

    
530
<?php
531
include("foot.inc");
(125-125/227)