Project

General

Profile

Download (14.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-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("shaper.inc");
38
require_once("system.inc");
39

    
40
$pconfig['enable'] = isset($config['dnsmasq']['enable']);
41
$pconfig['regdhcp'] = isset($config['dnsmasq']['regdhcp']);
42
$pconfig['regdhcpstatic'] = isset($config['dnsmasq']['regdhcpstatic']);
43
$pconfig['dhcpfirst'] = isset($config['dnsmasq']['dhcpfirst']);
44
$pconfig['strict_order'] = isset($config['dnsmasq']['strict_order']);
45
$pconfig['domain_needed'] = isset($config['dnsmasq']['domain_needed']);
46
$pconfig['no_private_reverse'] = isset($config['dnsmasq']['no_private_reverse']);
47
$pconfig['port'] = $config['dnsmasq']['port'];
48
$pconfig['custom_options'] = $config['dnsmasq']['custom_options'];
49

    
50
$pconfig['strictbind'] = isset($config['dnsmasq']['strictbind']);
51
if (!empty($config['dnsmasq']['interface'])) {
52
	$pconfig['interface'] = explode(",", $config['dnsmasq']['interface']);
53
} else {
54
	$pconfig['interface'] = array();
55
}
56

    
57
if (!is_array($config['dnsmasq']['hosts'])) {
58
	$config['dnsmasq']['hosts'] = array();
59
}
60

    
61
if (!is_array($config['dnsmasq']['domainoverrides'])) {
62
	$config['dnsmasq']['domainoverrides'] = array();
63
}
64

    
65
$a_hosts = &$config['dnsmasq']['hosts'];
66
$a_domainOverrides = &$config['dnsmasq']['domainoverrides'];
67

    
68
if ($_POST) {
69
	if ($_POST['apply']) {
70
		$retval = 0;
71
		$retval = services_dnsmasq_configure();
72
		$savemsg = get_std_save_message($retval);
73

    
74
		// Reload filter (we might need to sync to CARP hosts)
75
		filter_configure();
76
		/* Update resolv.conf in case the interface bindings exclude localhost. */
77
		system_resolvconf_generate();
78
		/* Start or restart dhcpleases when it's necessary */
79
		system_dhcpleases_configure();
80

    
81
		if ($retval == 0) {
82
			clear_subsystem_dirty('hosts');
83
		}
84
	} else {
85
		$pconfig = $_POST;
86
		unset($input_errors);
87

    
88
		$config['dnsmasq']['enable'] = ($_POST['enable']) ? true : false;
89
		$config['dnsmasq']['regdhcp'] = ($_POST['regdhcp']) ? true : false;
90
		$config['dnsmasq']['regdhcpstatic'] = ($_POST['regdhcpstatic']) ? true : false;
91
		$config['dnsmasq']['dhcpfirst'] = ($_POST['dhcpfirst']) ? true : false;
92
		$config['dnsmasq']['strict_order'] = ($_POST['strict_order']) ? true : false;
93
		$config['dnsmasq']['domain_needed'] = ($_POST['domain_needed']) ? true : false;
94
		$config['dnsmasq']['no_private_reverse'] = ($_POST['no_private_reverse']) ? true : false;
95
		$config['dnsmasq']['custom_options'] = str_replace("\r\n", "\n", $_POST['custom_options']);
96
		$config['dnsmasq']['strictbind'] = ($_POST['strictbind']) ? true : false;
97

    
98
		if (isset($_POST['enable']) && isset($config['unbound']['enable'])) {
99
			if ($_POST['port'] == $config['unbound']['port']) {
100
				$input_errors[] = gettext("The DNS Resolver is enabled using this port. Choose a non-conflicting port, or disable DNS Resolver.");
101
			}
102
		}
103

    
104
		if ($_POST['port']) {
105
			if (is_port($_POST['port'])) {
106
				$config['dnsmasq']['port'] = $_POST['port'];
107
			} else {
108
				$input_errors[] = gettext("A valid port number must be specified.");
109
			}
110
		} else if (isset($config['dnsmasq']['port'])) {
111
			unset($config['dnsmasq']['port']);
112
		}
113

    
114
		if (is_array($_POST['interface'])) {
115
			$config['dnsmasq']['interface'] = implode(",", $_POST['interface']);
116
		} elseif (isset($config['dnsmasq']['interface'])) {
117
			unset($config['dnsmasq']['interface']);
118
		}
119

    
120
		if ($config['dnsmasq']['custom_options']) {
121
			$args = '';
122
			foreach (preg_split('/\s+/', $config['dnsmasq']['custom_options']) as $c) {
123
				$args .= escapeshellarg("--{$c}") . " ";
124
			}
125
			exec("/usr/local/sbin/dnsmasq --test $args", $output, $rc);
126
			if ($rc != 0) {
127
				$input_errors[] = gettext("Invalid custom options");
128
			}
129
		}
130

    
131
		if (!$input_errors) {
132
			write_config();
133
			mark_subsystem_dirty('hosts');
134
		}
135
	}
136
}
137

    
138
if ($_GET['act'] == "del") {
139
	if ($_GET['type'] == 'host') {
140
		if ($a_hosts[$_GET['id']]) {
141
			unset($a_hosts[$_GET['id']]);
142
			write_config();
143
			mark_subsystem_dirty('hosts');
144
			header("Location: services_dnsmasq.php");
145
			exit;
146
		}
147
	} elseif ($_GET['type'] == 'doverride') {
148
		if ($a_domainOverrides[$_GET['id']]) {
149
			unset($a_domainOverrides[$_GET['id']]);
150
			write_config();
151
			mark_subsystem_dirty('hosts');
152
			header("Location: services_dnsmasq.php");
153
			exit;
154
		}
155
	}
156
}
157

    
158
function build_if_list() {
159
	global $pconfig;
160

    
161
	$interface_addresses = get_possible_listen_ips(true);
162
	$iflist = array('options' => array(), 'selected' => array());
163

    
164
	$iflist['options'][""]	= "All";
165
	if (empty($pconfig['interface']) || empty($pconfig['interface'][0])) {
166
		array_push($iflist['selected'], "");
167
	}
168

    
169
	foreach ($interface_addresses as $laddr => $ldescr) {
170
		$iflist['options'][$laddr] = htmlspecialchars($ldescr);
171

    
172
		if ($pconfig['interface'] && in_array($laddr, $pconfig['interface'])) {
173
			array_push($iflist['selected'], $laddr);
174
		}
175
	}
176

    
177
	unset($interface_addresses);
178

    
179
	return($iflist);
180
}
181

    
182
$pgtitle = array(gettext("Services"), gettext("DNS Forwarder"));
183
$shortcut_section = "forwarder";
184
include("head.inc");
185

    
186
if ($input_errors) {
187
	print_input_errors($input_errors);
188
}
189

    
190
if ($savemsg) {
191
	print_info_box($savemsg, 'success');
192
}
193

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

    
198
$form = new Form();
199

    
200
$section = new Form_Section('General DNS Forwarder Options');
201

    
202
$section->addInput(new Form_Checkbox(
203
	'enable',
204
	'Enable',
205
	'Enable DNS forwarder',
206
	$pconfig['enable']
207
))->toggles('.toggle-dhcp', 'disable');
208

    
209
$section->addInput(new Form_Checkbox(
210
	'regdhcp',
211
	'DHCP Registration',
212
	'Register DHCP leases in DNS forwarder',
213
	$pconfig['regdhcp']
214
))->setHelp(sprintf("If this option is set, then machines that specify".
215
			" their hostname when requesting a DHCP lease will be registered".
216
			" in the DNS forwarder, so that their name can be resolved.".
217
			" The domain in %sSystem: General Setup%s should also".
218
			" be set to the proper value.",'<a href="system.php">','</a>'))
219
	->addClass('toggle-dhcp');
220

    
221
$section->addInput(new Form_Checkbox(
222
	'regdhcpstatic',
223
	'Static DHCP',
224
	'Register DHCP static mappings in DNS forwarder',
225
	$pconfig['regdhcpstatic']
226
))->setHelp(sprintf("If this option is set, then DHCP static mappings will ".
227
					"be registered in the DNS forwarder, so that their name can be ".
228
					"resolved. The domain in %sSystem: General Setup%s should also ".
229
					"be set to the proper value.",'<a href="system.php">','</a>'))
230
	->addClass('toggle-dhcp');
231

    
232
$section->addInput(new Form_Checkbox(
233
	'dhcpfirst',
234
	'Prefer DHCP',
235
	'Resolve DHCP mappings first',
236
	$pconfig['dhcpfirst']
237
))->setHelp(sprintf("If this option is set, then DHCP mappings will ".
238
					"be resolved before the manual list of names below. This only ".
239
					"affects the name given for a reverse lookup (PTR)."))
240
	->addClass('toggle-dhcp');
241

    
242
$group = new Form_Group('DNS Query Forwarding');
243

    
244
$group->add(new Form_Checkbox(
245
	'strict_order',
246
	'DNS Query Forwarding',
247
	'Query DNS servers sequentially',
248
	$pconfig['strict_order']
249
))->setHelp(sprintf("If this option is set, %s DNS Forwarder (dnsmasq) will ".
250
					"query the DNS servers sequentially in the order specified (<i>System - General Setup - DNS Servers</i>), ".
251
					"rather than all at once in parallel. ", $g['product_name']));
252

    
253
$group->add(new Form_Checkbox(
254
	'domain_needed',
255
	null,
256
	'Require domain',
257
	$pconfig['domain_needed']
258
))->setHelp(sprintf("If this option is set, %s DNS Forwarder (dnsmasq) will ".
259
					"not forward A or AAAA queries for plain names, without dots or domain parts, to upstream name servers.	 ".
260
					"If the name is not known from /etc/hosts or DHCP then a \"not found\" answer is returned. ", $g['product_name']));
261

    
262
$group->add(new Form_Checkbox(
263
	'no_private_reverse',
264
	null,
265
	'Do not forward private reverse lookups',
266
	$pconfig['no_private_reverse']
267
))->setHelp(sprintf("If this option is set, %s DNS Forwarder (dnsmasq) will ".
268
					"not forward reverse DNS lookups (PTR) for private addresses (RFC 1918) to upstream name servers.  ".
269
					"Any entries in the Domain Overrides section forwarding private \"n.n.n.in-addr.arpa\" names to a specific server are still forwarded. ".
270
					"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']));
271

    
272
$section->add($group);
273

    
274
$section->addInput(new Form_Input(
275
	'port',
276
	'Listen Port',
277
	'number',
278
	$pconfig['port'],
279
	['placeholder' => '53']
280
))->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.');
281

    
282
$iflist = build_if_list();
283

    
284
$section->addInput(new Form_Select(
285
	'interface',
286
	'Interfaces',
287
	$iflist['selected'],
288
	$iflist['options'],
289
	true
290
))->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. ' .
291
			'The default behavior is to respond to queries on every available IPv4 and IPv6 address.');
292

    
293
$section->addInput(new Form_Checkbox(
294
	'strictbind',
295
	'Strict binding',
296
	'Strict interface binding',
297
	$pconfig['strictbind']
298
))->setHelp('If this option is set, the DNS forwarder will only bind to the interfaces containing the IP addresses selected above, ' .
299
					'rather than binding to all interfaces and discarding queries to other addresses.' . '<br /><br />' .
300
					'This option does NOT work with IPv6. If set, dnsmasq will not bind to IPv6 addresses.');
301

    
302
$section->addInput(new Form_Textarea(
303
	'custom_options',
304
	'Custom options',
305
	$pconfig['custom_options']
306
))->setHelp('Enter any additional options to add to the dnsmasq configuration here, separated by a space or newline.')
307
  ->addClass('advanced');
308

    
309
$form->add($section);
310
print($form);
311

    
312
?>
313
<div class="panel panel-default">
314
	<div class="panel-heading"><h2 class="panel-title"><?=gettext("Host Overrides")?></h2></div>
315
	<div class="panel-body table-responsive">
316
		<table class="table table-striped table-hover table-condensed sortable-theme-bootstrap table-rowdblclickedit" data-sortable>
317
			<thead>
318
				<tr>
319
					<th><?=gettext("Host")?></th>
320
					<th><?=gettext("Domain")?></th>
321
					<th><?=gettext("IP")?></th>
322
					<th><?=gettext("Description")?></th>
323
					<th><?=gettext("Actions")?></th>
324
				</tr>
325
			</thead>
326
			<tbody>
327
<?php
328
foreach ($a_hosts as $i => $hostent):
329
?>
330
				<tr>
331
					<td>
332
						<?=$hostent['host']?>
333
					</td>
334
					<td>
335
						<?=$hostent['domain']?>
336
					</td>
337
					<td>
338
						<?=$hostent['ip']?>
339
					</td>
340
					<td>
341
						<?=htmlspecialchars($hostent['descr'])?>
342
					</td>
343
					<td>
344
						<a class="fa fa-pencil"	title="<?=gettext('Edit host override')?>" 	href="services_dnsmasq_edit.php?id=<?=$i?>"></a>
345
						<a class="fa fa-trash"	title="<?=gettext('Delete host override')?>"	href="services_dnsmasq.php?type=host&amp;act=del&amp;id=<?=$i?>"></a>
346
					</td>
347
				</tr>
348

    
349
<?php
350
	if ($hostent['aliases']['item'] && is_array($hostent['aliases']['item'])):
351
		foreach ($hostent['aliases']['item'] as $alias):
352
?>
353
				<tr>
354
					<td>
355
						<?=$alias['host']?>
356
					</td>
357
					<td>
358
						<?=$alias['domain']?>
359
					</td>
360
					<td>
361
						<?=gettext("Alias for ");?><?=$hostent['host'] ? $hostent['host'] . '.' . $hostent['domain'] : $hostent['domain']?>
362
					</td>
363
					<td>
364
						<i class="fa fa-angle-double-right text-info"></i>
365
						<?=htmlspecialchars($alias['description'])?>
366
					</td>
367
					<td>
368
						<a class="fa fa-pencil"	title="<?=gettext('Edit host override')?>" 	href="services_dnsmasq_edit.php?id=<?=$i?>"></a>
369
					</td>
370
				</tr>
371
<?php
372
		endforeach;
373
	endif;
374
endforeach;
375
?>
376
			</tbody>
377
		</table>
378
	</div>
379
</div>
380

    
381
<nav class="action-buttons">
382
	<a href="services_dnsmasq_edit.php" class="btn btn-sm btn-success btn-sm">
383
		<i class="fa fa-plus icon-embed-btn"></i>
384
		<?=gettext('Add')?>
385
	</a>
386
</nav>
387

    
388
<div class="panel panel-default">
389
	<div class="panel-heading"><h2 class="panel-title"><?=gettext("Domain Overrides")?></h2></div>
390
	<div class="panel-body table-responsive">
391
		<table class="table table-striped table-hover table-condensed sortable-theme-bootstrap table-rowdblclickedit" data-sortable>
392
			<thead>
393
				<tr>
394
					<th><?=gettext("Domain")?></th>
395
					<th><?=gettext("IP")?></th>
396
					<th><?=gettext("Description")?></th>
397
					<th><?=gettext("Actions")?></th>
398
				</tr>
399
			</thead>
400

    
401
			<tbody>
402
<?php
403
foreach ($a_domainOverrides as $i => $doment):
404
?>
405
				<tr>
406
					<td>
407
						<?=$doment['domain']?>
408
					</td>
409
					<td>
410
						<?=$doment['ip']?>
411
					</td>
412
					<td>
413
						<?=htmlspecialchars($doment['descr'])?>
414
					</td>
415
					<td>
416
						<a class="fa fa-pencil"	title="<?=gettext('Edit domain override')?>" href="services_dnsmasq_domainoverride_edit.php?id=<?=$i?>"></a>
417
						<a class="fa fa-trash"	title="<?=gettext('Delete domain override')?>" href="services_dnsmasq.php?act=del&amp;type=doverride&amp;id=<?=$i?>"></a>
418
					</td>
419
				</tr>
420
<?php
421
endforeach;
422
?>
423
			</tbody>
424
		</table>
425
	</div>
426
</div>
427

    
428
<nav class="action-buttons">
429
	<a href="services_dnsmasq_domainoverride_edit.php" class="btn btn-sm btn-success btn-sm">
430
		<i class="fa fa-plus icon-embed-btn"></i>
431
		<?=gettext('Add')?>
432
	</a>
433
</nav>
434
<div class="infoblock">
435
<?php
436
print_info_box(
437
	'<p>' .
438
	gettext('If the DNS forwarder is enabled, the DHCP service (if enabled) will automatically' .
439
		    ' serve the LAN IP address as a DNS server to DHCP clients so they will use the forwarder.') . '</p><p>' .
440
	sprintf(gettext('The DNS forwarder will use the DNS servers entered in %1$sSystem > General Setup%2$s or' .
441
				    ' 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.' .
442
				    ' If that option is not used (or if a static IP address is used on WAN),' .
443
				    ' at least one DNS server must be manually specified on the %1$sSystem > General Setup%2$s page.'),
444
			'<a href="system.php">',
445
			'</a>') .
446
	'</p>',
447
	'info',
448
	false
449
);
450
?>
451
</div>
452

    
453
<?php
454
include("foot.inc");
(124-124/226)