Project

General

Profile

Download (17.5 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("pfsense-utils.inc");
70
require_once("shaper.inc");
71
require_once("system.inc");
72

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
135
hosts_sort();
136

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

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

    
144
domains_sort();
145

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

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

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

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

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

    
182
		if ((isset($_POST['regdhcp']) || isset($_POST['regdhcpstatic']) || isset($_POST['dhcpfirst'])) && !is_dhcp_server_enabled()) {
183
			$input_errors[] = gettext("DHCP Server must be enabled for DHCP Registration to work in DNS Forwarder.");
184
		}
185

    
186
		if ($_POST['port']) {
187
			if (is_port($_POST['port'])) {
188
				$config['dnsmasq']['port'] = $_POST['port'];
189
			} else {
190
				$input_errors[] = gettext("A valid port number must be specified.");
191
			}
192
		} else if (isset($config['dnsmasq']['port'])) {
193
			unset($config['dnsmasq']['port']);
194
		}
195

    
196
		if (is_array($_POST['interface'])) {
197
			$config['dnsmasq']['interface'] = implode(",", $_POST['interface']);
198
		} elseif (isset($config['dnsmasq']['interface'])) {
199
			unset($config['dnsmasq']['interface']);
200
		}
201

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

    
213
		if (!$input_errors) {
214
			write_config();
215
			mark_subsystem_dirty('hosts');
216
		}
217
	}
218
}
219

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

    
240
function build_if_list() {
241
	global $pconfig;
242

    
243
	$interface_addresses = get_possible_listen_ips(true);
244
	$iflist = array('options' => array(), 'selected' => array());
245

    
246
	$iflist['options'][""]	= "All";
247
	if (empty($pconfig['interface']) || empty($pconfig['interface'][0])) {
248
		array_push($iflist['selected'], "");
249
	}
250

    
251
	foreach ($interface_addresses as $laddr => $ldescr) {
252
		$iflist['options'][$laddr] = htmlspecialchars($ldescr);
253

    
254
		if ($pconfig['interface'] && in_array($laddr, $pconfig['interface'])) {
255
			array_push($iflist['selected'], $laddr);
256
		}
257
	}
258

    
259
	unset($interface_addresses);
260

    
261
	return($iflist);
262
}
263

    
264
$pgtitle = array(gettext("Services"), gettext("DNS Forwarder"));
265
$shortcut_section = "forwarder";
266
include("head.inc");
267

    
268
if ($input_errors) {
269
	print_input_errors($input_errors);
270
}
271

    
272
if ($savemsg) {
273
	print_info_box($savemsg, 'success');
274
}
275

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

    
280
$form = new Form();
281

    
282
$section = new Form_Section('General DNS Forwarder Options');
283

    
284
$section->addInput(new Form_Checkbox(
285
	'enable',
286
	'Enable',
287
	'Enable DNS forwarder',
288
	$pconfig['enable']
289
))->toggles('.toggle-dhcp', 'disable');
290

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

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

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

    
324
$group = new Form_Group('DNS Query Forwarding');
325

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

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

    
344
$group->add(new Form_Checkbox(
345
	'no_private_reverse',
346
	null,
347
	'Do not forward private reverse lookups',
348
	$pconfig['no_private_reverse']
349
))->setHelp(sprintf("If this option is set, %s DNS Forwarder (dnsmasq) will ".
350
					"not forward reverse DNS lookups (PTR) for private addresses (RFC 1918) to upstream name servers.  ".
351
					"Any entries in the Domain Overrides section forwarding private \"n.n.n.in-addr.arpa\" names to a specific server are still forwarded. ".
352
					"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']));
353

    
354
$section->add($group);
355

    
356
$section->addInput(new Form_Input(
357
	'port',
358
	'Listen Port',
359
	'number',
360
	$pconfig['port'],
361
	['placeholder' => '53']
362
))->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.');
363

    
364
$iflist = build_if_list();
365

    
366
$section->addInput(new Form_Select(
367
	'interface',
368
	'*Interfaces',
369
	$iflist['selected'],
370
	$iflist['options'],
371
	true
372
))->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. ' .
373
			'The default behavior is to respond to queries on every available IPv4 and IPv6 address.');
374

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

    
384
$section->addInput(new Form_Textarea(
385
	'custom_options',
386
	'Custom options',
387
	$pconfig['custom_options']
388
))->setHelp('Enter any additional options to add to the dnsmasq configuration here, separated by a space or newline.')
389
  ->addClass('advanced');
390

    
391
$form->add($section);
392
print($form);
393

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

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

    
463
<nav class="action-buttons">
464
	<a href="services_dnsmasq_edit.php" class="btn btn-sm btn-success btn-sm">
465
		<i class="fa fa-plus icon-embed-btn"></i>
466
		<?=gettext('Add')?>
467
	</a>
468
</nav>
469

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

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

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

    
535
<?php
536
include("foot.inc");
(128-128/230)