Project

General

Profile

Download (15.7 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-2018 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
 * Licensed under the Apache License, Version 2.0 (the "License");
15
 * you may not use this file except in compliance with the License.
16
 * You may obtain a copy of the License at
17
 *
18
 * http://www.apache.org/licenses/LICENSE-2.0
19
 *
20
 * Unless required by applicable law or agreed to in writing, software
21
 * distributed under the License is distributed on an "AS IS" BASIS,
22
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23
 * See the License for the specific language governing permissions and
24
 * limitations under the License.
25
 */
26

    
27
##|+PRIV
28
##|*IDENT=page-services-dnsforwarder
29
##|*NAME=Services: DNS Forwarder
30
##|*DESCR=Allow access to the 'Services: DNS Forwarder' page.
31
##|*MATCH=services_dnsmasq.php*
32
##|-PRIV
33

    
34
require_once("guiconfig.inc");
35
require_once("functions.inc");
36
require_once("filter.inc");
37
require_once("pfsense-utils.inc");
38
require_once("shaper.inc");
39
require_once("system.inc");
40

    
41
// Sort host entries for display in alphabetical order
42
function hostcmp($a, $b) {
43
	return strcasecmp($a['host'], $b['host']);
44
}
45

    
46
function hosts_sort() {
47
	global $a_hosts;
48

    
49
	if (!is_array($a_hosts)) {
50
		return;
51
	}
52

    
53
	uasort($a_hosts, "hostcmp");
54
}
55

    
56
// Sort domain entries for display in alphabetical order
57
function domaincmp($a, $b) {
58
	return strcasecmp($a['domain'], $b['domain']);
59
}
60

    
61
function domains_sort() {
62
	global $a_domainOverrides;
63

    
64
	if (!is_array($a_domainOverrides)) {
65
		return;
66
	}
67

    
68
	uasort($a_domainOverrides, "domaincmp");
69
}
70

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

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

    
88
init_config_arr(array('dnsmasq', 'hosts'));
89
$a_hosts = &$config['dnsmasq']['hosts'];
90

    
91
// Add a temporary index so we don't lose the order after sorting
92
for ($idx=0; $idx<count($a_hosts); $idx++) {
93
	$a_hosts[$idx]['idx'] = $idx;
94
}
95

    
96
hosts_sort();
97

    
98
init_config_arr(array('dnsmasq', 'domainoverrides'));
99
$a_domainOverrides = &$config['dnsmasq']['domainoverrides'];
100

    
101
// Add a temporary index so we don't lose the order after sorting
102
for ($idx=0; $idx<count($a_domainOverrides); $idx++) {
103
	$a_domainOverrides[$idx]['idx'] = $idx;
104
}
105

    
106
domains_sort();
107

    
108

    
109
if ($_POST['apply']) {
110
	$retval = 0;
111
	$retval |= services_dnsmasq_configure();
112

    
113
	// Reload filter (we might need to sync to CARP hosts)
114
	filter_configure();
115
	/* Update resolv.conf in case the interface bindings exclude localhost. */
116
	system_resolvconf_generate();
117
	/* Start or restart dhcpleases when it's necessary */
118
	system_dhcpleases_configure();
119

    
120
	if ($retval == 0) {
121
		clear_subsystem_dirty('hosts');
122
	}
123
}
124

    
125
if ($_POST['save']) {
126
	$pconfig = $_POST;
127
	unset($input_errors);
128

    
129
	$config['dnsmasq']['enable'] = ($_POST['enable']) ? true : false;
130
	$config['dnsmasq']['regdhcp'] = ($_POST['regdhcp']) ? true : false;
131
	$config['dnsmasq']['regdhcpstatic'] = ($_POST['regdhcpstatic']) ? true : false;
132
	$config['dnsmasq']['dhcpfirst'] = ($_POST['dhcpfirst']) ? true : false;
133
	$config['dnsmasq']['strict_order'] = ($_POST['strict_order']) ? true : false;
134
	$config['dnsmasq']['domain_needed'] = ($_POST['domain_needed']) ? true : false;
135
	$config['dnsmasq']['no_private_reverse'] = ($_POST['no_private_reverse']) ? true : false;
136
	$config['dnsmasq']['custom_options'] = str_replace("\r\n", "\n", $_POST['custom_options']);
137
	$config['dnsmasq']['strictbind'] = ($_POST['strictbind']) ? true : false;
138

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

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

    
149
	if ($_POST['port']) {
150
		if (is_port($_POST['port'])) {
151
			$config['dnsmasq']['port'] = $_POST['port'];
152
		} else {
153
			$input_errors[] = gettext("A valid port number must be specified.");
154
		}
155
	} else if (isset($config['dnsmasq']['port'])) {
156
		unset($config['dnsmasq']['port']);
157
	}
158

    
159
	if (is_array($_POST['interface'])) {
160
		$config['dnsmasq']['interface'] = implode(",", $_POST['interface']);
161
	} elseif (isset($config['dnsmasq']['interface'])) {
162
		unset($config['dnsmasq']['interface']);
163
	}
164

    
165
	if ($config['dnsmasq']['custom_options']) {
166
		$args = '';
167
		foreach (preg_split('/\s+/', $config['dnsmasq']['custom_options']) as $c) {
168
			$args .= escapeshellarg("--{$c}") . " ";
169
		}
170
		exec("/usr/local/sbin/dnsmasq --test $args", $output, $rc);
171
		if ($rc != 0) {
172
			$input_errors[] = gettext("Invalid custom options");
173
		}
174
	}
175

    
176
	if (!$input_errors) {
177
		write_config();
178
		mark_subsystem_dirty('hosts');
179
	}
180
}
181

    
182

    
183
if ($_POST['act'] == "del") {
184
	if ($_POST['type'] == 'host') {
185
		if ($a_hosts[$_POST['id']]) {
186
			unset($a_hosts[$_POST['id']]);
187
			write_config();
188
			mark_subsystem_dirty('hosts');
189
			header("Location: services_dnsmasq.php");
190
			exit;
191
		}
192
	} elseif ($_POST['type'] == 'doverride') {
193
		if ($a_domainOverrides[$_POST['id']]) {
194
			unset($a_domainOverrides[$_POST['id']]);
195
			write_config();
196
			mark_subsystem_dirty('hosts');
197
			header("Location: services_dnsmasq.php");
198
			exit;
199
		}
200
	}
201
}
202

    
203
function build_if_list() {
204
	global $pconfig;
205

    
206
	$interface_addresses = get_possible_listen_ips(true);
207
	$iflist = array('options' => array(), 'selected' => array());
208

    
209
	$iflist['options'][""]	= "All";
210
	if (empty($pconfig['interface']) || empty($pconfig['interface'][0])) {
211
		array_push($iflist['selected'], "");
212
	}
213

    
214
	foreach ($interface_addresses as $laddr => $ldescr) {
215
		$iflist['options'][$laddr] = htmlspecialchars($ldescr);
216

    
217
		if ($pconfig['interface'] && in_array($laddr, $pconfig['interface'])) {
218
			array_push($iflist['selected'], $laddr);
219
		}
220
	}
221

    
222
	unset($interface_addresses);
223

    
224
	return($iflist);
225
}
226

    
227
$pgtitle = array(gettext("Services"), gettext("DNS Forwarder"));
228
$shortcut_section = "forwarder";
229
include("head.inc");
230

    
231
if ($input_errors) {
232
	print_input_errors($input_errors);
233
}
234

    
235
if ($_POST['apply']) {
236
	print_apply_result_box($retval);
237
}
238

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

    
243
$form = new Form();
244

    
245
$section = new Form_Section('General DNS Forwarder Options');
246

    
247
$section->addInput(new Form_Checkbox(
248
	'enable',
249
	'Enable',
250
	'Enable DNS forwarder',
251
	$pconfig['enable']
252
))->toggles('.toggle-dhcp', 'disable');
253

    
254
$section->addInput(new Form_Checkbox(
255
	'regdhcp',
256
	'DHCP Registration',
257
	'Register DHCP leases in DNS forwarder',
258
	$pconfig['regdhcp']
259
))->setHelp('If this option is set machines that specify'.
260
			' their hostname when requesting a DHCP lease will be registered'.
261
			' in the DNS forwarder, so that their name can be resolved.'.
262
			' The domain in %1$sSystem: General Setup%2$s should also'.
263
			' be set to the proper value.', '<a href="system.php">', '</a>')
264
	->addClass('toggle-dhcp');
265

    
266
$section->addInput(new Form_Checkbox(
267
	'regdhcpstatic',
268
	'Static DHCP',
269
	'Register DHCP static mappings in DNS forwarder',
270
	$pconfig['regdhcpstatic']
271
))->setHelp('If this option is set, IPv4 DHCP static mappings will '.
272
					'be registered in the DNS forwarder so that their name can be '.
273
					'resolved. The domain in %1$sSystem: General Setup%2$s should also '.
274
					'be set to the proper value.', '<a href="system.php">', '</a>')
275
	->addClass('toggle-dhcp');
276

    
277
$section->addInput(new Form_Checkbox(
278
	'dhcpfirst',
279
	'Prefer DHCP',
280
	'Resolve DHCP mappings first',
281
	$pconfig['dhcpfirst']
282
))->setHelp("If this option is set DHCP mappings will ".
283
					"be resolved before the manual list of names below. This only ".
284
					"affects the name given for a reverse lookup (PTR).")
285
	->addClass('toggle-dhcp');
286

    
287
$group = new Form_Group('DNS Query Forwarding');
288

    
289
$group->add(new Form_Checkbox(
290
	'strict_order',
291
	'DNS Query Forwarding',
292
	'Query DNS servers sequentially',
293
	$pconfig['strict_order']
294
))->setHelp('If this option is set %1$s DNS Forwarder (dnsmasq) will '.
295
					'query the DNS servers sequentially in the order specified (%2$sSystem - General Setup - DNS Servers%3$s), '.
296
					'rather than all at once in parallel. ', $g['product_name'], '<i>', '</i>');
297

    
298
$group->add(new Form_Checkbox(
299
	'domain_needed',
300
	null,
301
	'Require domain',
302
	$pconfig['domain_needed']
303
))->setHelp("If this option is set %s DNS Forwarder (dnsmasq) will ".
304
					"not forward A or AAAA queries for plain names, without dots or domain parts, to upstream name servers.	 ".
305
					"If the name is not known from /etc/hosts or DHCP then a \"not found\" answer is returned. ", $g['product_name']);
306

    
307
$group->add(new Form_Checkbox(
308
	'no_private_reverse',
309
	null,
310
	'Do not forward private reverse lookups',
311
	$pconfig['no_private_reverse']
312
))->setHelp("If this option is set %s DNS Forwarder (dnsmasq) will ".
313
					"not forward reverse DNS lookups (PTR) for private addresses (RFC 1918) to upstream name servers.  ".
314
					"Any entries in the Domain Overrides section forwarding private \"n.n.n.in-addr.arpa\" names to a specific server are still forwarded. ".
315
					"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']);
316

    
317
$section->add($group);
318

    
319
$section->addInput(new Form_Input(
320
	'port',
321
	'Listen Port',
322
	'number',
323
	$pconfig['port'],
324
	['placeholder' => '53']
325
))->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.');
326

    
327
$iflist = build_if_list();
328

    
329
$section->addInput(new Form_Select(
330
	'interface',
331
	'*Interfaces',
332
	$iflist['selected'],
333
	$iflist['options'],
334
	true
335
))->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. ' .
336
			'The default behavior is to respond to queries on every available IPv4 and IPv6 address.');
337

    
338
$section->addInput(new Form_Checkbox(
339
	'strictbind',
340
	'Strict binding',
341
	'Strict interface binding',
342
	$pconfig['strictbind']
343
))->setHelp('If this option is set, the DNS forwarder will only bind to the interfaces containing the IP addresses selected above, ' .
344
					'rather than binding to all interfaces and discarding queries to other addresses.%1$s' .
345
					'This option does NOT work with IPv6. If set, dnsmasq will not bind to IPv6 addresses.', '<br /><br />');
346

    
347
$section->addInput(new Form_Textarea(
348
	'custom_options',
349
	'Custom options',
350
	$pconfig['custom_options']
351
))->setHelp('Enter any additional options to add to the dnsmasq configuration here, separated by a space or newline.')
352
  ->addClass('advanced');
353

    
354
$form->add($section);
355
print($form);
356

    
357
?>
358
<div class="panel panel-default">
359
	<div class="panel-heading"><h2 class="panel-title"><?=gettext("Host Overrides")?></h2></div>
360
	<div class="panel-body table-responsive">
361
		<table class="table table-striped table-hover table-condensed sortable-theme-bootstrap table-rowdblclickedit" data-sortable>
362
			<thead>
363
				<tr>
364
					<th><?=gettext("Host")?></th>
365
					<th><?=gettext("Domain")?></th>
366
					<th><?=gettext("IP")?></th>
367
					<th><?=gettext("Description")?></th>
368
					<th><?=gettext("Actions")?></th>
369
				</tr>
370
			</thead>
371
			<tbody>
372
<?php
373
foreach ($a_hosts as $i => $hostent):
374
?>
375
				<tr>
376
					<td>
377
						<?=$hostent['host']?>
378
					</td>
379
					<td>
380
						<?=$hostent['domain']?>
381
					</td>
382
					<td>
383
						<?=$hostent['ip']?>
384
					</td>
385
					<td>
386
						<?=htmlspecialchars($hostent['descr'])?>
387
					</td>
388
					<td>
389
						<a class="fa fa-pencil"	title="<?=gettext('Edit host override')?>" 	href="services_dnsmasq_edit.php?id=<?=$hostent['idx']?>"></a>
390
						<a class="fa fa-trash"	title="<?=gettext('Delete host override')?>"	href="services_dnsmasq.php?type=host&amp;act=del&amp;id=<?=$hostent['idx']?>" usepost></a>
391
					</td>
392
				</tr>
393

    
394
<?php
395
	if ($hostent['aliases']['item'] && is_array($hostent['aliases']['item'])):
396
		foreach ($hostent['aliases']['item'] as $alias):
397
?>
398
				<tr>
399
					<td>
400
						<?=$alias['host']?>
401
					</td>
402
					<td>
403
						<?=$alias['domain']?>
404
					</td>
405
					<td>
406
						<?=gettext("Alias for ");?><?=$hostent['host'] ? $hostent['host'] . '.' . $hostent['domain'] : $hostent['domain']?>
407
					</td>
408
					<td>
409
						<i class="fa fa-angle-double-right text-info"></i>
410
						<?=htmlspecialchars($alias['description'])?>
411
					</td>
412
					<td>
413
						<a class="fa fa-pencil"	title="<?=gettext('Edit host override')?>" 	href="services_dnsmasq_edit.php?id=<?=$i?>"></a>
414
					</td>
415
				</tr>
416
<?php
417
		endforeach;
418
	endif;
419
endforeach;
420
?>
421
			</tbody>
422
		</table>
423
	</div>
424
</div>
425

    
426
<nav class="action-buttons">
427
	<a href="services_dnsmasq_edit.php" class="btn btn-sm btn-success btn-sm">
428
		<i class="fa fa-plus icon-embed-btn"></i>
429
		<?=gettext('Add')?>
430
	</a>
431
</nav>
432

    
433
<div class="panel panel-default">
434
	<div class="panel-heading"><h2 class="panel-title"><?=gettext("Domain Overrides")?></h2></div>
435
	<div class="panel-body table-responsive">
436
		<table class="table table-striped table-hover table-condensed sortable-theme-bootstrap table-rowdblclickedit" data-sortable>
437
			<thead>
438
				<tr>
439
					<th><?=gettext("Domain")?></th>
440
					<th><?=gettext("IP")?></th>
441
					<th><?=gettext("Description")?></th>
442
					<th><?=gettext("Actions")?></th>
443
				</tr>
444
			</thead>
445

    
446
			<tbody>
447
<?php
448
foreach ($a_domainOverrides as $i => $doment):
449
?>
450
				<tr>
451
					<td>
452
						<?=$doment['domain']?>
453
					</td>
454
					<td>
455
						<?=$doment['ip']?>
456
					</td>
457
					<td>
458
						<?=htmlspecialchars($doment['descr'])?>
459
					</td>
460
					<td>
461
						<a class="fa fa-pencil"	title="<?=gettext('Edit domain override')?>" href="services_dnsmasq_domainoverride_edit.php?id=<?=$doment['idx']?>"></a>
462
						<a class="fa fa-trash"	title="<?=gettext('Delete domain override')?>" href="services_dnsmasq.php?act=del&amp;type=doverride&amp;id=<?=$doment['idx']?>" usepost></a>
463
					</td>
464
				</tr>
465
<?php
466
endforeach;
467
?>
468
			</tbody>
469
		</table>
470
	</div>
471
</div>
472

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

    
498
<?php
499
include("foot.inc");
(130-130/234)