Project

General

Profile

Download (15.9 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
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
init_config_arr(array('dnsmasq', 'hosts'));
97
$a_hosts = &$config['dnsmasq']['hosts'];
98

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

    
104
hosts_sort();
105

    
106
init_config_arr(array('dnsmasq', 'domainoverrides'));
107
$a_domainOverrides = &$config['dnsmasq']['domainoverrides'];
108

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

    
114
domains_sort();
115

    
116

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

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

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

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

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

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

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

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

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

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

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

    
190

    
191
if ($_POST['act'] == "del") {
192
	if ($_POST['type'] == 'host') {
193
		if ($a_hosts[$_POST['id']]) {
194
			unset($a_hosts[$_POST['id']]);
195
			write_config();
196
			mark_subsystem_dirty('hosts');
197
			header("Location: services_dnsmasq.php");
198
			exit;
199
		}
200
	} elseif ($_POST['type'] == 'doverride') {
201
		if ($a_domainOverrides[$_POST['id']]) {
202
			unset($a_domainOverrides[$_POST['id']]);
203
			write_config();
204
			mark_subsystem_dirty('hosts');
205
			header("Location: services_dnsmasq.php");
206
			exit;
207
		}
208
	}
209
}
210

    
211
function build_if_list() {
212
	global $pconfig;
213

    
214
	$interface_addresses = get_possible_listen_ips(true);
215
	$iflist = array('options' => array(), 'selected' => array());
216

    
217
	$iflist['options'][""]	= "All";
218
	if (empty($pconfig['interface']) || empty($pconfig['interface'][0])) {
219
		array_push($iflist['selected'], "");
220
	}
221

    
222
	foreach ($interface_addresses as $laddr => $ldescr) {
223
		$iflist['options'][$laddr] = htmlspecialchars($ldescr);
224

    
225
		if ($pconfig['interface'] && in_array($laddr, $pconfig['interface'])) {
226
			array_push($iflist['selected'], $laddr);
227
		}
228
	}
229

    
230
	unset($interface_addresses);
231

    
232
	return($iflist);
233
}
234

    
235
$pgtitle = array(gettext("Services"), gettext("DNS Forwarder"));
236
$shortcut_section = "forwarder";
237
include("head.inc");
238

    
239
if ($input_errors) {
240
	print_input_errors($input_errors);
241
}
242

    
243
if ($_POST['apply']) {
244
	print_apply_result_box($retval);
245
}
246

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

    
251
$form = new Form();
252

    
253
$section = new Form_Section('General DNS Forwarder Options');
254

    
255
$section->addInput(new Form_Checkbox(
256
	'enable',
257
	'Enable',
258
	'Enable DNS forwarder',
259
	$pconfig['enable']
260
))->toggles('.toggle-dhcp', 'disable');
261

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

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

    
285
$section->addInput(new Form_Checkbox(
286
	'dhcpfirst',
287
	'Prefer DHCP',
288
	'Resolve DHCP mappings first',
289
	$pconfig['dhcpfirst']
290
))->setHelp("If this option is set DHCP mappings will ".
291
					"be resolved before the manual list of names below. This only ".
292
					"affects the name given for a reverse lookup (PTR).")
293
	->addClass('toggle-dhcp');
294

    
295
$group = new Form_Group('DNS Query Forwarding');
296

    
297
$group->add(new Form_Checkbox(
298
	'strict_order',
299
	'DNS Query Forwarding',
300
	'Query DNS servers sequentially',
301
	$pconfig['strict_order']
302
))->setHelp('If this option is set %1$s DNS Forwarder (dnsmasq) will '.
303
					'query the DNS servers sequentially in the order specified (%2$sSystem - General Setup - DNS Servers%3$s), '.
304
					'rather than all at once in parallel. ', $g['product_name'], '<i>', '</i>');
305

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

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

    
325
$section->add($group);
326

    
327
$section->addInput(new Form_Input(
328
	'port',
329
	'Listen Port',
330
	'number',
331
	$pconfig['port'],
332
	['placeholder' => '53']
333
))->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.');
334

    
335
$iflist = build_if_list();
336

    
337
$section->addInput(new Form_Select(
338
	'interface',
339
	'*Interfaces',
340
	$iflist['selected'],
341
	$iflist['options'],
342
	true
343
))->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. ' .
344
			'The default behavior is to respond to queries on every available IPv4 and IPv6 address.');
345

    
346
$section->addInput(new Form_Checkbox(
347
	'strictbind',
348
	'Strict binding',
349
	'Strict interface binding',
350
	$pconfig['strictbind']
351
))->setHelp('If this option is set, the DNS forwarder will only bind to the interfaces containing the IP addresses selected above, ' .
352
					'rather than binding to all interfaces and discarding queries to other addresses.%1$s' .
353
					'This option does NOT work with IPv6. If set, dnsmasq will not bind to IPv6 addresses.', '<br /><br />');
354

    
355
$section->addInput(new Form_Textarea(
356
	'custom_options',
357
	'Custom options',
358
	$pconfig['custom_options']
359
))->setHelp('Enter any additional options to add to the dnsmasq configuration here, separated by a space or newline.')
360
  ->addClass('advanced');
361

    
362
$form->add($section);
363
print($form);
364

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

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

    
434
<nav class="action-buttons">
435
	<a href="services_dnsmasq_edit.php" class="btn btn-sm btn-success btn-sm">
436
		<i class="fa fa-plus icon-embed-btn"></i>
437
		<?=gettext('Add')?>
438
	</a>
439
</nav>
440

    
441
<div class="panel panel-default">
442
	<div class="panel-heading"><h2 class="panel-title"><?=gettext("Domain Overrides")?></h2></div>
443
	<div class="panel-body table-responsive">
444
		<table class="table table-striped table-hover table-condensed sortable-theme-bootstrap table-rowdblclickedit" data-sortable>
445
			<thead>
446
				<tr>
447
					<th><?=gettext("Domain")?></th>
448
					<th><?=gettext("IP")?></th>
449
					<th><?=gettext("Description")?></th>
450
					<th><?=gettext("Actions")?></th>
451
				</tr>
452
			</thead>
453

    
454
			<tbody>
455
<?php
456
foreach ($a_domainOverrides as $i => $doment):
457
?>
458
				<tr>
459
					<td>
460
						<?=$doment['domain']?>
461
					</td>
462
					<td>
463
						<?=$doment['ip']?>
464
					</td>
465
					<td>
466
						<?=htmlspecialchars($doment['descr'])?>
467
					</td>
468
					<td>
469
						<a class="fa fa-pencil"	title="<?=gettext('Edit domain override')?>" href="services_dnsmasq_domainoverride_edit.php?id=<?=$doment['idx']?>"></a>
470
						<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>
471
					</td>
472
				</tr>
473
<?php
474
endforeach;
475
?>
476
			</tbody>
477
		</table>
478
	</div>
479
</div>
480

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

    
506
<?php
507
include("foot.inc");
(130-130/234)