Project

General

Profile

Download (16.4 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 Electric Sheep Fencing, LLC
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
$pconfig['enable'] = isset($config['dnsmasq']['enable']);
73
$pconfig['regdhcp'] = isset($config['dnsmasq']['regdhcp']);
74
$pconfig['regdhcpstatic'] = isset($config['dnsmasq']['regdhcpstatic']);
75
$pconfig['dhcpfirst'] = isset($config['dnsmasq']['dhcpfirst']);
76
$pconfig['strict_order'] = isset($config['dnsmasq']['strict_order']);
77
$pconfig['domain_needed'] = isset($config['dnsmasq']['domain_needed']);
78
$pconfig['no_private_reverse'] = isset($config['dnsmasq']['no_private_reverse']);
79
$pconfig['port'] = $config['dnsmasq']['port'];
80
$pconfig['custom_options'] = $config['dnsmasq']['custom_options'];
81

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
209
	unset($interface_addresses);
210

    
211
	return($iflist);
212
}
213

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

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

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

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

    
230
$form = new Form();
231

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

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

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

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

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

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

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

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

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

    
304
$section->add($group);
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
$iflist = build_if_list();
315

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

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

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

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

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

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

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

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

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

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

    
485
<?php
486
include("foot.inc");
(123-123/225)