Project

General

Profile

Download (15.8 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 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
		if ($a_hosts[$_POST['id']]) {
192
			unset($a_hosts[$_POST['id']]);
193
			write_config();
194
			mark_subsystem_dirty('hosts');
195
			header("Location: services_dnsmasq.php");
196
			exit;
197
		}
198
	} elseif ($_POST['type'] == 'doverride') {
199
		if ($a_domainOverrides[$_POST['id']]) {
200
			unset($a_domainOverrides[$_POST['id']]);
201
			write_config();
202
			mark_subsystem_dirty('hosts');
203
			header("Location: services_dnsmasq.php");
204
			exit;
205
		}
206
	}
207
}
208

    
209
function build_if_list() {
210
	global $pconfig;
211

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

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

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

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

    
228
	unset($interface_addresses);
229

    
230
	return($iflist);
231
}
232

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

    
237
if ($input_errors) {
238
	print_input_errors($input_errors);
239
}
240

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

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

    
249
$form = new Form();
250

    
251
$section = new Form_Section('General DNS Forwarder Options');
252

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

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

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

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

    
293
$group = new Form_Group('DNS Query Forwarding');
294

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

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

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

    
323
$section->add($group);
324

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

    
333
$iflist = build_if_list();
334

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

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

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

    
360
$form->add($section);
361
print($form);
362

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

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

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

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

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

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

    
504
<?php
505
include("foot.inc");
(128-128/231)