Project

General

Profile

Download (16.1 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
	usort($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
	usort($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
if (!is_array($config['dnsmasq']['hosts'])) {
89
	$config['dnsmasq']['hosts'] = array();
90
}
91

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

    
96
$a_hosts = &$config['dnsmasq']['hosts'];
97

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

    
103
hosts_sort();
104

    
105
$a_domainOverrides = &$config['dnsmasq']['domainoverrides'];
106

    
107
// Add a temporary index so we don't lose the order after sorting
108
for ($idx=0; $idx<count($a_domainOverrides); $idx++) {
109
	$a_domainOverrides[$idx]['idx'] = $idx;
110
}
111

    
112
domains_sort();
113

    
114

    
115
if ($_POST['apply']) {
116
	$retval = 0;
117
	$retval |= services_dnsmasq_configure();
118

    
119
	// Reload filter (we might need to sync to CARP hosts)
120
	filter_configure();
121
	/* Update resolv.conf in case the interface bindings exclude localhost. */
122
	system_resolvconf_generate();
123
	/* Start or restart dhcpleases when it's necessary */
124
	system_dhcpleases_configure();
125

    
126
	if ($retval == 0) {
127
		clear_subsystem_dirty('hosts');
128
	}
129
}
130

    
131
if ($_POST['save']) {
132
	$pconfig = $_POST;
133
	unset($input_errors);
134

    
135
	$config['dnsmasq']['enable'] = ($_POST['enable']) ? true : false;
136
	$config['dnsmasq']['regdhcp'] = ($_POST['regdhcp']) ? true : false;
137
	$config['dnsmasq']['regdhcpstatic'] = ($_POST['regdhcpstatic']) ? true : false;
138
	$config['dnsmasq']['dhcpfirst'] = ($_POST['dhcpfirst']) ? true : false;
139
	$config['dnsmasq']['strict_order'] = ($_POST['strict_order']) ? true : false;
140
	$config['dnsmasq']['domain_needed'] = ($_POST['domain_needed']) ? true : false;
141
	$config['dnsmasq']['no_private_reverse'] = ($_POST['no_private_reverse']) ? true : false;
142
	$config['dnsmasq']['custom_options'] = str_replace("\r\n", "\n", $_POST['custom_options']);
143
	$config['dnsmasq']['strictbind'] = ($_POST['strictbind']) ? true : false;
144

    
145
	if (isset($_POST['enable']) && isset($config['unbound']['enable'])) {
146
		if ($_POST['port'] == $config['unbound']['port']) {
147
			$input_errors[] = gettext("The DNS Resolver is enabled using this port. Choose a non-conflicting port, or disable DNS Resolver.");
148
		}
149
	}
150

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

    
155
	if ($_POST['port']) {
156
		if (is_port($_POST['port'])) {
157
			$config['dnsmasq']['port'] = $_POST['port'];
158
		} else {
159
			$input_errors[] = gettext("A valid port number must be specified.");
160
		}
161
	} else if (isset($config['dnsmasq']['port'])) {
162
		unset($config['dnsmasq']['port']);
163
	}
164

    
165
	if (is_array($_POST['interface'])) {
166
		$config['dnsmasq']['interface'] = implode(",", $_POST['interface']);
167
	} elseif (isset($config['dnsmasq']['interface'])) {
168
		unset($config['dnsmasq']['interface']);
169
	}
170

    
171
	if ($config['dnsmasq']['custom_options']) {
172
		$args = '';
173
		foreach (preg_split('/\s+/', $config['dnsmasq']['custom_options']) as $c) {
174
			$args .= escapeshellarg("--{$c}") . " ";
175
		}
176
		exec("/usr/local/sbin/dnsmasq --test $args", $output, $rc);
177
		if ($rc != 0) {
178
			$input_errors[] = gettext("Invalid custom options");
179
		}
180
	}
181

    
182
	if (!$input_errors) {
183
		write_config();
184
		mark_subsystem_dirty('hosts');
185
	}
186
}
187

    
188

    
189
if ($_POST['act'] == "del") {
190
	if ($_POST['type'] == 'host') {
191
		// it gets sorted by hostname on load
192
		// sort it by index so it deletes the correct one.
193
		usort($a_hosts, function($a,$b){
194
			return($a['idx'] > $b['idx']);
195
		});
196

    
197
		if ($a_hosts[$_POST['id']]) {
198
			unset($a_hosts[$_POST['id']]);
199
			write_config();
200
			mark_subsystem_dirty('hosts');
201
			header("Location: services_dnsmasq.php");
202
			exit;
203
		}
204
	} elseif ($_POST['type'] == 'doverride') {
205
		// gets sorted by name on load
206
		// sort by index to delete the correct one.
207
		usort($a_domainOverrides, function($a,$b){
208
			return($a['idx'] > $b['idx']);
209
		});
210

    
211
		if ($a_domainOverrides[$_POST['id']]) {
212
			unset($a_domainOverrides[$_POST['id']]);
213
			write_config();
214
			mark_subsystem_dirty('hosts');
215
			header("Location: services_dnsmasq.php");
216
			exit;
217
		}
218
	}
219
}
220

    
221
function build_if_list() {
222
	global $pconfig;
223

    
224
	$interface_addresses = get_possible_listen_ips(true);
225
	$iflist = array('options' => array(), 'selected' => array());
226

    
227
	$iflist['options'][""]	= "All";
228
	if (empty($pconfig['interface']) || empty($pconfig['interface'][0])) {
229
		array_push($iflist['selected'], "");
230
	}
231

    
232
	foreach ($interface_addresses as $laddr => $ldescr) {
233
		$iflist['options'][$laddr] = htmlspecialchars($ldescr);
234

    
235
		if ($pconfig['interface'] && in_array($laddr, $pconfig['interface'])) {
236
			array_push($iflist['selected'], $laddr);
237
		}
238
	}
239

    
240
	unset($interface_addresses);
241

    
242
	return($iflist);
243
}
244

    
245
$pgtitle = array(gettext("Services"), gettext("DNS Forwarder"));
246
$shortcut_section = "forwarder";
247
include("head.inc");
248

    
249
if ($input_errors) {
250
	print_input_errors($input_errors);
251
}
252

    
253
if ($_POST['apply']) {
254
	print_apply_result_box($retval);
255
}
256

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

    
261
$form = new Form();
262

    
263
$section = new Form_Section('General DNS Forwarder Options');
264

    
265
$section->addInput(new Form_Checkbox(
266
	'enable',
267
	'Enable',
268
	'Enable DNS forwarder',
269
	$pconfig['enable']
270
))->toggles('.toggle-dhcp', 'disable');
271

    
272
$section->addInput(new Form_Checkbox(
273
	'regdhcp',
274
	'DHCP Registration',
275
	'Register DHCP leases in DNS forwarder',
276
	$pconfig['regdhcp']
277
))->setHelp('If this option is set machines that specify'.
278
			' their hostname when requesting a DHCP lease will be registered'.
279
			' in the DNS forwarder, so that their name can be resolved.'.
280
			' The domain in %1$sSystem: General Setup%2$s should also'.
281
			' be set to the proper value.', '<a href="system.php">', '</a>')
282
	->addClass('toggle-dhcp');
283

    
284
$section->addInput(new Form_Checkbox(
285
	'regdhcpstatic',
286
	'Static DHCP',
287
	'Register DHCP static mappings in DNS forwarder',
288
	$pconfig['regdhcpstatic']
289
))->setHelp('If this option is set, DHCP static mappings will '.
290
					'be registered in the DNS forwarder, so that their name can be '.
291
					'resolved. The domain in %1$sSystem: General Setup%2$s should also '.
292
					'be set to the proper value.', '<a href="system.php">', '</a>')
293
	->addClass('toggle-dhcp');
294

    
295
$section->addInput(new Form_Checkbox(
296
	'dhcpfirst',
297
	'Prefer DHCP',
298
	'Resolve DHCP mappings first',
299
	$pconfig['dhcpfirst']
300
))->setHelp("If this option is set DHCP mappings will ".
301
					"be resolved before the manual list of names below. This only ".
302
					"affects the name given for a reverse lookup (PTR).")
303
	->addClass('toggle-dhcp');
304

    
305
$group = new Form_Group('DNS Query Forwarding');
306

    
307
$group->add(new Form_Checkbox(
308
	'strict_order',
309
	'DNS Query Forwarding',
310
	'Query DNS servers sequentially',
311
	$pconfig['strict_order']
312
))->setHelp('If this option is set %1$s DNS Forwarder (dnsmasq) will '.
313
					'query the DNS servers sequentially in the order specified (%2$sSystem - General Setup - DNS Servers%3$s), '.
314
					'rather than all at once in parallel. ', $g['product_name'], '<i>', '</i>');
315

    
316
$group->add(new Form_Checkbox(
317
	'domain_needed',
318
	null,
319
	'Require domain',
320
	$pconfig['domain_needed']
321
))->setHelp("If this option is set %s DNS Forwarder (dnsmasq) will ".
322
					"not forward A or AAAA queries for plain names, without dots or domain parts, to upstream name servers.	 ".
323
					"If the name is not known from /etc/hosts or DHCP then a \"not found\" answer is returned. ", $g['product_name']);
324

    
325
$group->add(new Form_Checkbox(
326
	'no_private_reverse',
327
	null,
328
	'Do not forward private reverse lookups',
329
	$pconfig['no_private_reverse']
330
))->setHelp("If this option is set %s DNS Forwarder (dnsmasq) will ".
331
					"not forward reverse DNS lookups (PTR) for private addresses (RFC 1918) to upstream name servers.  ".
332
					"Any entries in the Domain Overrides section forwarding private \"n.n.n.in-addr.arpa\" names to a specific server are still forwarded. ".
333
					"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']);
334

    
335
$section->add($group);
336

    
337
$section->addInput(new Form_Input(
338
	'port',
339
	'Listen Port',
340
	'number',
341
	$pconfig['port'],
342
	['placeholder' => '53']
343
))->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.');
344

    
345
$iflist = build_if_list();
346

    
347
$section->addInput(new Form_Select(
348
	'interface',
349
	'*Interfaces',
350
	$iflist['selected'],
351
	$iflist['options'],
352
	true
353
))->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. ' .
354
			'The default behavior is to respond to queries on every available IPv4 and IPv6 address.');
355

    
356
$section->addInput(new Form_Checkbox(
357
	'strictbind',
358
	'Strict binding',
359
	'Strict interface binding',
360
	$pconfig['strictbind']
361
))->setHelp('If this option is set, the DNS forwarder will only bind to the interfaces containing the IP addresses selected above, ' .
362
					'rather than binding to all interfaces and discarding queries to other addresses.%1$s' .
363
					'This option does NOT work with IPv6. If set, dnsmasq will not bind to IPv6 addresses.', '<br /><br />');
364

    
365
$section->addInput(new Form_Textarea(
366
	'custom_options',
367
	'Custom options',
368
	$pconfig['custom_options']
369
))->setHelp('Enter any additional options to add to the dnsmasq configuration here, separated by a space or newline.')
370
  ->addClass('advanced');
371

    
372
$form->add($section);
373
print($form);
374

    
375
?>
376
<div class="panel panel-default">
377
	<div class="panel-heading"><h2 class="panel-title"><?=gettext("Host Overrides")?></h2></div>
378
	<div class="panel-body table-responsive">
379
		<table class="table table-striped table-hover table-condensed sortable-theme-bootstrap table-rowdblclickedit" data-sortable>
380
			<thead>
381
				<tr>
382
					<th><?=gettext("Host")?></th>
383
					<th><?=gettext("Domain")?></th>
384
					<th><?=gettext("IP")?></th>
385
					<th><?=gettext("Description")?></th>
386
					<th><?=gettext("Actions")?></th>
387
				</tr>
388
			</thead>
389
			<tbody>
390
<?php
391
foreach ($a_hosts as $i => $hostent):
392
?>
393
				<tr>
394
					<td>
395
						<?=$hostent['host']?>
396
					</td>
397
					<td>
398
						<?=$hostent['domain']?>
399
					</td>
400
					<td>
401
						<?=$hostent['ip']?>
402
					</td>
403
					<td>
404
						<?=htmlspecialchars($hostent['descr'])?>
405
					</td>
406
					<td>
407
						<a class="fa fa-pencil"	title="<?=gettext('Edit host override')?>" 	href="services_dnsmasq_edit.php?id=<?=$hostent['idx']?>"></a>
408
						<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>
409
					</td>
410
				</tr>
411

    
412
<?php
413
	if ($hostent['aliases']['item'] && is_array($hostent['aliases']['item'])):
414
		foreach ($hostent['aliases']['item'] as $alias):
415
?>
416
				<tr>
417
					<td>
418
						<?=$alias['host']?>
419
					</td>
420
					<td>
421
						<?=$alias['domain']?>
422
					</td>
423
					<td>
424
						<?=gettext("Alias for ");?><?=$hostent['host'] ? $hostent['host'] . '.' . $hostent['domain'] : $hostent['domain']?>
425
					</td>
426
					<td>
427
						<i class="fa fa-angle-double-right text-info"></i>
428
						<?=htmlspecialchars($alias['description'])?>
429
					</td>
430
					<td>
431
						<a class="fa fa-pencil"	title="<?=gettext('Edit host override')?>" 	href="services_dnsmasq_edit.php?id=<?=$i?>"></a>
432
					</td>
433
				</tr>
434
<?php
435
		endforeach;
436
	endif;
437
endforeach;
438
?>
439
			</tbody>
440
		</table>
441
	</div>
442
</div>
443

    
444
<nav class="action-buttons">
445
	<a href="services_dnsmasq_edit.php" class="btn btn-sm btn-success btn-sm">
446
		<i class="fa fa-plus icon-embed-btn"></i>
447
		<?=gettext('Add')?>
448
	</a>
449
</nav>
450

    
451
<div class="panel panel-default">
452
	<div class="panel-heading"><h2 class="panel-title"><?=gettext("Domain Overrides")?></h2></div>
453
	<div class="panel-body table-responsive">
454
		<table class="table table-striped table-hover table-condensed sortable-theme-bootstrap table-rowdblclickedit" data-sortable>
455
			<thead>
456
				<tr>
457
					<th><?=gettext("Domain")?></th>
458
					<th><?=gettext("IP")?></th>
459
					<th><?=gettext("Description")?></th>
460
					<th><?=gettext("Actions")?></th>
461
				</tr>
462
			</thead>
463

    
464
			<tbody>
465
<?php
466
foreach ($a_domainOverrides as $i => $doment):
467
?>
468
				<tr>
469
					<td>
470
						<?=$doment['domain']?>
471
					</td>
472
					<td>
473
						<?=$doment['ip']?>
474
					</td>
475
					<td>
476
						<?=htmlspecialchars($doment['descr'])?>
477
					</td>
478
					<td>
479
						<a class="fa fa-pencil"	title="<?=gettext('Edit domain override')?>" href="services_dnsmasq_domainoverride_edit.php?id=<?=$doment['idx']?>"></a>
480
						<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>
481
					</td>
482
				</tr>
483
<?php
484
endforeach;
485
?>
486
			</tbody>
487
		</table>
488
	</div>
489
</div>
490

    
491
<nav class="action-buttons">
492
	<a href="services_dnsmasq_domainoverride_edit.php" class="btn btn-sm btn-success btn-sm">
493
		<i class="fa fa-plus icon-embed-btn"></i>
494
		<?=gettext('Add')?>
495
	</a>
496
</nav>
497
<div class="infoblock">
498
<?php
499
print_info_box(
500
	'<p>' .
501
	gettext('If the DNS forwarder is enabled, the DHCP service (if enabled) will automatically' .
502
		    ' serve the LAN IP address as a DNS server to DHCP clients so they will use the forwarder.') . '</p><p>' .
503
	sprintf(gettext('The DNS forwarder will use the DNS servers entered in %1$sSystem > General Setup%2$s or' .
504
				    ' 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.' .
505
				    ' If that option is not used (or if a static IP address is used on WAN),' .
506
				    ' at least one DNS server must be manually specified on the %1$sSystem > General Setup%2$s page.'),
507
			'<a href="system.php">',
508
			'</a>') .
509
	'</p>',
510
	'info',
511
	false
512
);
513
?>
514
</div>
515

    
516
<?php
517
include("foot.inc");
(128-128/232)