Project

General

Profile

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

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

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

    
73
$pconfig['enable'] = isset($config['dnsmasq']['enable']);
74
$pconfig['regdhcp'] = isset($config['dnsmasq']['regdhcp']);
75
$pconfig['regdhcpstatic'] = isset($config['dnsmasq']['regdhcpstatic']);
76
$pconfig['dhcpfirst'] = isset($config['dnsmasq']['dhcpfirst']);
77
$pconfig['strict_order'] = isset($config['dnsmasq']['strict_order']);
78
$pconfig['domain_needed'] = isset($config['dnsmasq']['domain_needed']);
79
$pconfig['no_private_reverse'] = isset($config['dnsmasq']['no_private_reverse']);
80
$pconfig['port'] = $config['dnsmasq']['port'];
81
$pconfig['custom_options'] = $config['dnsmasq']['custom_options'];
82

    
83
$pconfig['strictbind'] = isset($config['dnsmasq']['strictbind']);
84
if (!empty($config['dnsmasq']['interface'])) {
85
	$pconfig['interface'] = explode(",", $config['dnsmasq']['interface']);
86
} else {
87
	$pconfig['interface'] = array();
88
}
89

    
90
if (!is_array($config['dnsmasq']['hosts'])) {
91
	$config['dnsmasq']['hosts'] = array();
92
}
93

    
94
if (!is_array($config['dnsmasq']['domainoverrides'])) {
95
	$config['dnsmasq']['domainoverrides'] = array();
96
}
97

    
98
$a_hosts = &$config['dnsmasq']['hosts'];
99
$a_domainOverrides = &$config['dnsmasq']['domainoverrides'];
100

    
101
if ($_POST) {
102
	if ($_POST['apply']) {
103
		$retval = 0;
104
		$retval = services_dnsmasq_configure();
105
		$savemsg = get_std_save_message($retval);
106

    
107
		// Reload filter (we might need to sync to CARP hosts)
108
		filter_configure();
109
		/* Update resolv.conf in case the interface bindings exclude localhost. */
110
		system_resolvconf_generate();
111
		/* Start or restart dhcpleases when it's necessary */
112
		system_dhcpleases_configure();
113

    
114
		if ($retval == 0) {
115
			clear_subsystem_dirty('hosts');
116
		}
117
	} else {
118
		$pconfig = $_POST;
119
		unset($input_errors);
120

    
121
		$config['dnsmasq']['enable'] = ($_POST['enable']) ? true : false;
122
		$config['dnsmasq']['regdhcp'] = ($_POST['regdhcp']) ? true : false;
123
		$config['dnsmasq']['regdhcpstatic'] = ($_POST['regdhcpstatic']) ? true : false;
124
		$config['dnsmasq']['dhcpfirst'] = ($_POST['dhcpfirst']) ? true : false;
125
		$config['dnsmasq']['strict_order'] = ($_POST['strict_order']) ? true : false;
126
		$config['dnsmasq']['domain_needed'] = ($_POST['domain_needed']) ? true : false;
127
		$config['dnsmasq']['no_private_reverse'] = ($_POST['no_private_reverse']) ? true : false;
128
		$config['dnsmasq']['custom_options'] = str_replace("\r\n", "\n", $_POST['custom_options']);
129
		$config['dnsmasq']['strictbind'] = ($_POST['strictbind']) ? true : false;
130

    
131
		if (isset($_POST['enable']) && isset($config['unbound']['enable'])) {
132
			if ($_POST['port'] == $config['unbound']['port']) {
133
				$input_errors[] = gettext("The DNS Resolver is enabled using this port. Choose a non-conflicting port, or disable DNS Resolver.");
134
			}
135
		}
136

    
137
		if ($_POST['port']) {
138
			if (is_port($_POST['port'])) {
139
				$config['dnsmasq']['port'] = $_POST['port'];
140
			} else {
141
				$input_errors[] = gettext("A valid port number must be specified.");
142
			}
143
		} else if (isset($config['dnsmasq']['port'])) {
144
			unset($config['dnsmasq']['port']);
145
		}
146

    
147
		if (is_array($_POST['interface'])) {
148
			$config['dnsmasq']['interface'] = implode(",", $_POST['interface']);
149
		} elseif (isset($config['dnsmasq']['interface'])) {
150
			unset($config['dnsmasq']['interface']);
151
		}
152

    
153
		if ($config['dnsmasq']['custom_options']) {
154
			$args = '';
155
			foreach (preg_split('/\s+/', $config['dnsmasq']['custom_options']) as $c) {
156
				$args .= escapeshellarg("--{$c}") . " ";
157
			}
158
			exec("/usr/local/sbin/dnsmasq --test $args", $output, $rc);
159
			if ($rc != 0) {
160
				$input_errors[] = gettext("Invalid custom options");
161
			}
162
		}
163

    
164
		if (!$input_errors) {
165
			write_config();
166
			mark_subsystem_dirty('hosts');
167
		}
168
	}
169
}
170

    
171
if ($_GET['act'] == "del") {
172
	if ($_GET['type'] == 'host') {
173
		if ($a_hosts[$_GET['id']]) {
174
			unset($a_hosts[$_GET['id']]);
175
			write_config();
176
			mark_subsystem_dirty('hosts');
177
			header("Location: services_dnsmasq.php");
178
			exit;
179
		}
180
	} elseif ($_GET['type'] == 'doverride') {
181
		if ($a_domainOverrides[$_GET['id']]) {
182
			unset($a_domainOverrides[$_GET['id']]);
183
			write_config();
184
			mark_subsystem_dirty('hosts');
185
			header("Location: services_dnsmasq.php");
186
			exit;
187
		}
188
	}
189
}
190

    
191
function build_if_list() {
192
	global $pconfig;
193

    
194
	$interface_addresses = get_possible_listen_ips(true);
195
	$iflist = array('options' => array(), 'selected' => array());
196

    
197
	$iflist['options'][""]	= "All";
198
	if (empty($pconfig['interface']) || empty($pconfig['interface'][0])) {
199
		array_push($iflist['selected'], "");
200
	}
201

    
202
	foreach ($interface_addresses as $laddr => $ldescr) {
203
		$iflist['options'][$laddr] = htmlspecialchars($ldescr);
204

    
205
		if ($pconfig['interface'] && in_array($laddr, $pconfig['interface'])) {
206
			array_push($iflist['selected'], $laddr);
207
		}
208
	}
209

    
210
	unset($interface_addresses);
211

    
212
	return($iflist);
213
}
214

    
215
$pgtitle = array(gettext("Services"), gettext("DNS Forwarder"));
216
$shortcut_section = "forwarder";
217
include("head.inc");
218

    
219
if ($input_errors) {
220
	print_input_errors($input_errors);
221
}
222

    
223
if ($savemsg) {
224
	print_info_box($savemsg, 'success');
225
}
226

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

    
231
$form = new Form();
232

    
233
$section = new Form_Section('General DNS Forwarder Options');
234

    
235
$section->addInput(new Form_Checkbox(
236
	'enable',
237
	'Enable',
238
	'Enable DNS forwarder',
239
	$pconfig['enable']
240
))->toggles('.toggle-dhcp', 'disable');
241

    
242
$section->addInput(new Form_Checkbox(
243
	'regdhcp',
244
	'DHCP Registration',
245
	'Register DHCP leases in DNS forwarder',
246
	$pconfig['regdhcp']
247
))->setHelp(sprintf("If this option is set, then machines that specify".
248
			" their hostname when requesting a DHCP lease will be registered".
249
			" in the DNS forwarder, so that their name can be resolved.".
250
			" The domain in %sSystem: General setup%s should also".
251
			" be set to the proper value.",'<a href="system.php">','</a>'))
252
	->addClass('toggle-dhcp');
253

    
254
$section->addInput(new Form_Checkbox(
255
	'regdhcpstatic',
256
	'Static DHCP',
257
	'Register DHCP static mappings in DNS forwarder',
258
	$pconfig['regdhcpstatic']
259
))->setHelp(sprintf("If this option is set, then DHCP static mappings will ".
260
					"be registered in the DNS forwarder, so that their name can be ".
261
					"resolved. The domain in %sSystem: General setup%s should also ".
262
					"be set to the proper value.",'<a href="system.php">','</a>'))
263
	->addClass('toggle-dhcp');
264

    
265
$section->addInput(new Form_Checkbox(
266
	'dhcpfirst',
267
	'Prefer DHCP',
268
	'Resolve DHCP mappings first',
269
	$pconfig['dhcpfirst']
270
))->setHelp(sprintf("If this option is set, then DHCP mappings will ".
271
					"be resolved before the manual list of names below. This only ".
272
					"affects the name given for a reverse lookup (PTR)."))
273
	->addClass('toggle-dhcp');
274

    
275
$group = new Form_Group('DNS Query Forwarding');
276

    
277
$group->add(new Form_Checkbox(
278
	'strict_order',
279
	'DNS Query Forwarding',
280
	'Query DNS servers sequentially',
281
	$pconfig['strict_order']
282
))->setHelp(sprintf("If this option is set, %s DNS Forwarder (dnsmasq) will ".
283
					"query the DNS servers sequentially in the order specified (<i>System - General Setup - DNS Servers</i>), ".
284
					"rather than all at once in parallel. ", $g['product_name']));
285

    
286
$group->add(new Form_Checkbox(
287
	'domain_needed',
288
	null,
289
	'Require domain',
290
	$pconfig['domain_needed']
291
))->setHelp(sprintf("If this option is set, %s DNS Forwarder (dnsmasq) will ".
292
					"not forward A or AAAA queries for plain names, without dots or domain parts, to upstream name servers.	 ".
293
					"If the name is not known from /etc/hosts or DHCP then a \"not found\" answer is returned. ", $g['product_name']));
294

    
295
$group->add(new Form_Checkbox(
296
	'no_private_reverse',
297
	null,
298
	'Do not forward private reverse lookups',
299
	$pconfig['no_private_reverse']
300
))->setHelp(sprintf("If this option is set, %s DNS Forwarder (dnsmasq) will ".
301
					"not forward reverse DNS lookups (PTR) for private addresses (RFC 1918) to upstream name servers.  ".
302
					"Any entries in the Domain Overrides section forwarding private \"n.n.n.in-addr.arpa\" names to a specific server are still forwarded. ".
303
					"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']));
304

    
305
$section->add($group);
306

    
307
$section->addInput(new Form_Input(
308
	'port',
309
	'Listen Port',
310
	'number',
311
	$pconfig['port'],
312
	['placeholder' => '53']
313
))->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.');
314

    
315
$iflist = build_if_list();
316

    
317
$section->addInput(new Form_Select(
318
	'interface',
319
	'Interfaces',
320
	$iflist['selected'],
321
	$iflist['options'],
322
	true
323
))->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. ' .
324
			'The default behavior is to respond to queries on every available IPv4 and IPv6 address.');
325

    
326
$section->addInput(new Form_Checkbox(
327
	'strictbind',
328
	'Strict binding',
329
	'Strict interface binding',
330
	$pconfig['strictbind']
331
))->setHelp('If this option is set, the DNS forwarder will only bind to the interfaces containing the IP addresses selected above, ' .
332
					'rather than binding to all interfaces and discarding queries to other addresses.' . '<br /><br />' .
333
					'This option does NOT work with IPv6. If set, dnsmasq will not bind to IPv6 addresses.');
334

    
335
$section->addInput(new Form_Textarea(
336
	'custom_options',
337
	'Custom options',
338
	$pconfig['custom_options']
339
))->setHelp('Enter any additional options to add to the dnsmasq configuration here, separated by a space or newline.')
340
  ->addClass('advanced');
341

    
342
$form->add($section);
343
print($form);
344

    
345
?>
346
<div class="panel panel-default">
347
	<div class="panel-heading"><h2 class="panel-title"><?=gettext("Host Overrides")?></h2></div>
348
	<div class="panel-body table-responsive">
349
		<table class="table table-striped table-hover table-condensed sortable-theme-bootstrap" data-sortable>
350
			<thead>
351
				<tr>
352
					<th><?=gettext("Host")?></th>
353
					<th><?=gettext("Domain")?></th>
354
					<th><?=gettext("IP")?></th>
355
					<th><?=gettext("Description")?></th>
356
					<th><?=gettext("Actions")?></th>
357
				</tr>
358
			</thead>
359
			<tbody>
360
<?php
361
foreach ($a_hosts as $i => $hostent):
362
?>
363
				<tr>
364
					<td>
365
						<?=$hostent['host']?>
366
					</td>
367
					<td>
368
						<?=$hostent['domain']?>
369
					</td>
370
					<td>
371
						<?=$hostent['ip']?>
372
					</td>
373
					<td>
374
						<?=htmlspecialchars($hostent['descr'])?>
375
					</td>
376
					<td>
377
						<a class="fa fa-pencil"	title="<?=gettext('Edit host override')?>" 	href="services_dnsmasq_edit.php?id=<?=$i?>"></a>
378
						<a class="fa fa-trash"	title="<?=gettext('Delete host override')?>"	href="services_dnsmasq.php?type=host&amp;act=del&amp;id=<?=$i?>"></a>
379
					</td>
380
				</tr>
381

    
382
<?php
383
	if ($hostent['aliases']['item'] && is_array($hostent['aliases']['item'])):
384
		foreach ($hostent['aliases']['item'] as $alias):
385
?>
386
				<tr>
387
					<td>
388
						<?=$alias['host']?>
389
					</td>
390
					<td>
391
						<?=$alias['domain']?>
392
					</td>
393
					<td>
394
						<?=gettext("Alias for ");?><?=$hostent['host'] ? $hostent['host'] . '.' . $hostent['domain'] : $hostent['domain']?>
395
					</td>
396
					<td>
397
						<i class="fa fa-angle-double-right text-info"></i>
398
						<?=htmlspecialchars($alias['description'])?>
399
					</td>
400
					<td>
401
						<a class="fa fa-pencil"	title="<?=gettext('Edit host override')?>" 	href="services_dnsmasq_edit.php?id=<?=$i?>"></a>
402
					</td>
403
				</tr>
404
<?php
405
		endforeach;
406
	endif;
407
endforeach;
408
?>
409
			</tbody>
410
		</table>
411
	</div>
412
</div>
413

    
414
<nav class="action-buttons">
415
	<a href="services_dnsmasq_edit.php" class="btn btn-sm btn-success btn-sm">
416
		<i class="fa fa-plus icon-embed-btn"></i>
417
		<?=gettext('Add')?>
418
	</a>
419
</nav>
420

    
421
<div class="panel panel-default">
422
	<div class="panel-heading"><h2 class="panel-title"><?=gettext("Domain Overrides")?></h2></div>
423
	<div class="panel-body table-responsive">
424
		<table class="table table-striped table-hover table-condensed sortable-theme-bootstrap" data-sortable>
425
			<thead>
426
				<tr>
427
					<th><?=gettext("Domain")?></th>
428
					<th><?=gettext("IP")?></th>
429
					<th><?=gettext("Description")?></th>
430
					<th><?=gettext("Actions")?></th>
431
				</tr>
432
			</thead>
433

    
434
			<tbody>
435
<?php
436
foreach ($a_domainOverrides as $i => $doment):
437
?>
438
				<tr>
439
					<td>
440
						<?=$doment['domain']?>
441
					</td>
442
					<td>
443
						<?=$doment['ip']?>
444
					</td>
445
					<td>
446
						<?=htmlspecialchars($doment['descr'])?>
447
					</td>
448
					<td>
449
						<a class="fa fa-pencil"	title="<?=gettext('Edit domain override')?>" href="services_dnsmasq_domainoverride_edit.php?id=<?=$i?>"></a>
450
						<a class="fa fa-trash"	title="<?=gettext('Delete domain override')?>" href="services_dnsmasq.php?act=del&amp;type=doverride&amp;id=<?=$i?>"></a>
451
					</td>
452
				</tr>
453
<?php
454
endforeach;
455
?>
456
			</tbody>
457
		</table>
458
	</div>
459
</div>
460

    
461
<nav class="action-buttons">
462
	<a href="services_dnsmasq_domainoverride_edit.php" class="btn btn-sm btn-success btn-sm">
463
		<i class="fa fa-plus icon-embed-btn"></i>
464
		<?=gettext('Add')?>
465
	</a>
466
</nav>
467
<div class="infoblock">
468
<?php
469
print_info_box(
470
	'<p>' .
471
	gettext('If the DNS forwarder is enabled, the DHCP service (if enabled) will automatically' .
472
		    ' serve the LAN IP address as a DNS server to DHCP clients so they will use the forwarder.') . '</p><p>' .
473
	sprintf(gettext('The DNS forwarder will use the DNS servers entered in %1$sSystem > General Setup%2$s or' .
474
				    ' 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.' .
475
				    ' If that option is not used (or if a static IP address is used on WAN),' .
476
				    ' at least one DNS server must be manually specified on the %1$sSystem > General Setup%2$s page.'),
477
			'<a href="system.php">',
478
			'</a>') .
479
	'</p>',
480
	'info',
481
	false
482
);
483
?>
484
</div>
485

    
486
<?php
487
include("foot.inc");
(124-124/225)