1
|
<?php
|
2
|
/*
|
3
|
unbound.inc
|
4
|
part of the pfSense project (https://www.pfsense.org)
|
5
|
Copyright (C) 2014 Warren Baker <warren@decoy.co.za>
|
6
|
All rights reserved.
|
7
|
|
8
|
Redistribution and use in source and binary forms, with or without
|
9
|
modification, are permitted provided that the following conditions are met:
|
10
|
|
11
|
1. Redistributions of source code must retain the above copyright notice,
|
12
|
this list of conditions and the following disclaimer.
|
13
|
|
14
|
2. Redistributions in binary form must reproduce the above copyright
|
15
|
notice, this list of conditions and the following disclaimer in the
|
16
|
documentation and/or other materials provided with the distribution.
|
17
|
|
18
|
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
19
|
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
20
|
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
21
|
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
22
|
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
23
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
24
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
25
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
26
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27
|
POSSIBILITY OF SUCH DAMAGE.
|
28
|
|
29
|
pfSense_BUILDER_BINARIES: /usr/sbin/unbound /usr/sbin/unbound-anchor /usr/sbin/unbound-checkconf
|
30
|
pfSense_BUILDER_BINARIES: /usr/sbin/unbound-control /usr/sbin/unbound-control-setup
|
31
|
pfSense_MODULE: unbound
|
32
|
*/
|
33
|
|
34
|
/* include all configuration functions */
|
35
|
require_once("config.inc");
|
36
|
require_once("functions.inc");
|
37
|
|
38
|
/* Optimize Unbound for environment */
|
39
|
function unbound_optimization() {
|
40
|
global $config;
|
41
|
|
42
|
$optimization_settings = array();
|
43
|
|
44
|
/*
|
45
|
* Set the number of threads equal to number of CPUs.
|
46
|
* Use 1 to disable threading, if for some reason this sysctl fails.
|
47
|
*/
|
48
|
$numprocs = intval(trim(`/sbin/sysctl kern.smp.cpus | /usr/bin/cut -d" " -f2`));
|
49
|
if ($numprocs > 0)
|
50
|
$optimization['number_threads'] = "num-threads: {$numprocs}";
|
51
|
else
|
52
|
$optimization['number_threads'] = "num-threads: 1";
|
53
|
|
54
|
// Slabs to help reduce lock contention.
|
55
|
if ($numprocs > 4) {
|
56
|
$optimization['msg_cache_slabs'] = "msg-cache-slabs: {$numprocs}";
|
57
|
$optimization['rrset_cache_slabs'] = "rrset-cache-slabs: {$numprocs}";
|
58
|
$optimization['infra_cache_slabs'] = "infra-cache-slabs: {$numprocs}";
|
59
|
$optimization['key_cache_slabs'] = "key-cache-slabs: {$numprocs}";
|
60
|
} else {
|
61
|
$optimization['msg_cache_slabs'] = "msg-cache-slabs: 4";
|
62
|
$optimization['rrset_cache_slabs'] = "rrset-cache-slabs: 4";
|
63
|
$optimization['infra_cache_slabs'] = "infra-cache-slabs: 4";
|
64
|
$optimization['key_cache_slabs'] = "key-cache-slabs: 4";
|
65
|
}
|
66
|
|
67
|
// Memory usage default of 4MB
|
68
|
$optimization['msg_cache_size'] = "msg-cache-size: 4m";
|
69
|
$optimization['rrset_cache_size'] = "rrset-cache-size: 8m";
|
70
|
|
71
|
// More outgoing connections per thread otherwise assign a default of 4096 for a single thread
|
72
|
if ($numprocs > 0) {
|
73
|
$or = (1024/$numprocs) - 50;
|
74
|
$optimization['outgoing_range'] = "outgoing-range: {$or}";
|
75
|
} else
|
76
|
$optimization['outgoing_range'] = "outgoing-range: {4096}";
|
77
|
|
78
|
/*
|
79
|
* Larger socket buffer for busy servers
|
80
|
* Check that it is set to 4MB (by default the OS has it configured to 4MB)
|
81
|
*/
|
82
|
foreach ($config['sysctl']['item'] as $tunable) {
|
83
|
if ($tunable['tunable'] == 'kern.ipc.maxsockbuf') {
|
84
|
$so = floor(($tunable['value']/1024/1024)-1);
|
85
|
// Check to ensure that the number is not a negative
|
86
|
if ($so > 0)
|
87
|
$optimization['so_rcvbuf'] = "so-rcvbuf: {$so}m";
|
88
|
else
|
89
|
unset($optimization['so_rcvbuf']);
|
90
|
}
|
91
|
}
|
92
|
// Safety check in case kern.ipc.maxsockbuf is not available.
|
93
|
if (!isset($optimization['so_rcvbuf']))
|
94
|
$optimization['so_rcvbuf'] = "#so-rcvbuf: 4m";
|
95
|
|
96
|
return $optimization;
|
97
|
|
98
|
}
|
99
|
|
100
|
function unbound_generate_config() {
|
101
|
global $config, $g;
|
102
|
|
103
|
// Setup optimization
|
104
|
$optimization = unbound_optimization();
|
105
|
|
106
|
// Setup DNSSEC support
|
107
|
if (isset($config['unbound']['dnssec'])) {
|
108
|
$module_config = "validator iterator";
|
109
|
$anchor_file = "auto-trust-anchor-file: {$g['unbound_chroot_path']}/root.key";
|
110
|
} else
|
111
|
$module_config = "iterator";
|
112
|
|
113
|
// Setup DNS Rebinding
|
114
|
if (!isset($config['system']['webgui']['nodnsrebindcheck'])) {
|
115
|
// Private-addresses for DNS Rebinding
|
116
|
$private_addr = <<<EOF
|
117
|
# For DNS Rebinding prevention
|
118
|
private-address: 10.0.0.0/8
|
119
|
private-address: 172.16.0.0/12
|
120
|
private-address: 192.168.0.0/16
|
121
|
private-address: 192.254.0.0/16
|
122
|
private-address: fd00::/8
|
123
|
private-address: fe80::/10
|
124
|
EOF;
|
125
|
}
|
126
|
|
127
|
// Determine interfaces to run on
|
128
|
$bindints = "";
|
129
|
if (!empty($config['unbound']['active_interface'])) {
|
130
|
$active_interfaces = explode(",", $config['unbound']['active_interface']);
|
131
|
foreach($active_interfaces as $ubif) {
|
132
|
$intip = get_interface_ip($ubif);
|
133
|
if (!is_null($intip))
|
134
|
$bindints .= "interface: $intip\n";
|
135
|
}
|
136
|
} else {
|
137
|
$bindints .= "interface: 0.0.0.0\n";
|
138
|
$bindints .= "interface: ::0\n";
|
139
|
}
|
140
|
|
141
|
// Determine interfaces to run on
|
142
|
$outgoingints = "";
|
143
|
if (!empty($config['unbound']['outgoing_interface'])) {
|
144
|
$outgoingints = "# Outgoing interfaces to be used\n";
|
145
|
$outgoing_interfaces = explode(",", $config['unbound']['outgoing_interface']);
|
146
|
foreach($outgoing_interfaces as $outif) {
|
147
|
$outip = get_interface_ip($outif);
|
148
|
if (!is_null($outip))
|
149
|
$outgoingints .= "outgoing-interface: $intip\n";
|
150
|
}
|
151
|
}
|
152
|
|
153
|
// Allow DNS Rebind for forwarded domains
|
154
|
if ((isset($config['unbound']['domainoverrides']) && is_array($config['unbound']['domainoverrides'])) && !isset($config['system']['webgui']['nodnsrebindcheck'])) {
|
155
|
$private_domains = "# Set private domains in case authoritative name server returns a Private IP address\n";
|
156
|
$private_domains .= unbound_add_domain_overrides(true);
|
157
|
}
|
158
|
|
159
|
// Configure static Host entries
|
160
|
unbound_add_host_entries();
|
161
|
|
162
|
// Configure Domain Overrides
|
163
|
unbound_add_domain_overrides();
|
164
|
|
165
|
// Configure Unbound statistics
|
166
|
$statistics = unbound_statistics();
|
167
|
|
168
|
// Configure Unbound access-lists
|
169
|
unbound_acls_config();
|
170
|
|
171
|
// Add custom Unbound options
|
172
|
if ($config['unbound']['custom_options']) {
|
173
|
$custom_option = "# Unbound custom option";
|
174
|
foreach (preg_split('/\s+/', $config['unbound']['custom_options']) as $ent)
|
175
|
$custom_option .= $ent."\n";
|
176
|
}
|
177
|
|
178
|
// Server configuration variables
|
179
|
$port = (is_port($config['unbound']['port'])) ? $config['unbound']['port'] : "53";
|
180
|
$hide_id = ($config['unbound']['hide_id'] == "on") ? "yes" : "no";
|
181
|
$hide_version = ($config['unbound']['hide_version'] == "on") ? "yes" : "no";
|
182
|
$harden_glue = ($config['unbound']['harden_glue'] == "on") ? "yes" : "no";
|
183
|
$harden_dnssec_stripped = ($config['unbound']['harden_dnssec_stripped'] == "on") ? "yes" : "no";
|
184
|
$prefetch = ($config['unbound']['prefetch'] == "on") ? "yes" : "no";
|
185
|
$prefetch_key = ($config['unbound']['prefetch_key'] == "on") ? "yes" : "no";
|
186
|
$outgoing_num_tcp = (!empty($config['unbound']['outgoing_num_tcp'])) ? $config['unbound']['outgoing_num_tcp'] : "10";
|
187
|
$incoming_num_tcp = (!empty($config['unbound']['incoming_num_tcp'])) ? $config['unbound']['incoming_num_tcp'] : "10";
|
188
|
$edns_buffer_size = (!empty($config['unbound']['edns_buffer_size'])) ? $config['unbound']['edns_buffer_size'] : "4096";
|
189
|
$num_queries_per_thread = (!empty($config['unbound']['num_queries_per_thread'])) ? $config['unbound']['num_queries_per_thread'] : "4096";
|
190
|
$jostle_timeout = (!empty($config['unbound']['jostle_timeout'])) ? $config['unbound']['jostle_timeout'] : "200";
|
191
|
$cache_max_ttl = (!empty($config['unbound']['cache_max_ttl'])) ? $config['unbound']['cache_max_ttl'] : "86400";
|
192
|
$cache_min_ttl = (!empty($config['unbound']['cache_min_ttl'])) ? $config['unbound']['cache_min_ttl'] : "0";
|
193
|
$infra_host_ttl = (!empty($config['unbound']['infra_host_ttl'])) ? $config['unbound']['infra_host_ttl'] : "900";
|
194
|
$infra_lame_ttl = (!empty($config['unbound']['infra_lame_ttl'])) ? $config['unbound']['infra_lame_ttl'] : "900";
|
195
|
$infra_cache_numhosts = (!empty($config['unbound']['infra_cache_numhosts'])) ? $config['unbound']['infra_cache_numhosts'] : "10000";
|
196
|
$unwanted_reply_threshold = (!empty($config['unbound']['unwanted_reply_threshold'])) ? $config['unbound']['unwanted_reply_threshold'] : "0";
|
197
|
$verbosity = isset($config['unbound']['loglevel']) ? $config['unbound']['loglevel'] : 1;
|
198
|
|
199
|
// Set up forwarding if it configured
|
200
|
if (isset($config['unbound']['forwarding'])) {
|
201
|
$dnsservers = array();
|
202
|
if (isset($config['system']['dnsallowoverride'])) {
|
203
|
$ns = array_unique(get_nameservers());
|
204
|
foreach($ns as $nameserver) {
|
205
|
if ($nameserver)
|
206
|
$dnsservers[] = $nameserver;
|
207
|
}
|
208
|
} else {
|
209
|
$ns = array_unique(get_dns_servers());
|
210
|
foreach($ns as $nameserver) {
|
211
|
if ($nameserver)
|
212
|
$dnsservers[] = $nameserver;
|
213
|
}
|
214
|
}
|
215
|
|
216
|
if (!empty($dnsservers)) {
|
217
|
$forward_conf .=<<<EOD
|
218
|
# Forwarding
|
219
|
forward-zone:
|
220
|
name: "."
|
221
|
|
222
|
EOD;
|
223
|
foreach($dnsservers as $dnsserver)
|
224
|
$forward_conf .= "\tforward-addr: $dnsserver\n";
|
225
|
}
|
226
|
} else
|
227
|
$forward_conf = "";
|
228
|
|
229
|
$unboundconf = <<<EOD
|
230
|
##########################
|
231
|
# Unbound Configuration
|
232
|
##########################
|
233
|
|
234
|
##
|
235
|
# Server configuration
|
236
|
##
|
237
|
server:
|
238
|
chroot: {$g['unbound_chroot_path']}
|
239
|
username: "unbound"
|
240
|
directory: "{$g['unbound_chroot_path']}"
|
241
|
pidfile: "/var/run/unbound.pid"
|
242
|
use-syslog: yes
|
243
|
port: {$port}
|
244
|
verbosity: {$verbosity}
|
245
|
harden-referral-path: no
|
246
|
do-ip4: yes
|
247
|
do-ip6: yes
|
248
|
do-udp: yes
|
249
|
do-tcp: yes
|
250
|
do-daemonize: yes
|
251
|
module-config: "{$module_config}"
|
252
|
unwanted-reply-threshold: 0
|
253
|
num-queries-per-thread: 1024
|
254
|
jostle-timeout: 200
|
255
|
infra-host-ttl: 900
|
256
|
infra-lame-ttl: 900
|
257
|
infra-cache-numhosts: 10000
|
258
|
outgoing-num-tcp: 10
|
259
|
incoming-num-tcp: 10
|
260
|
edns-buffer-size: 4096
|
261
|
cache-max-ttl: {$cache_max_ttl}
|
262
|
cache-min-ttl: {$cache_min_ttl}
|
263
|
harden-dnssec-stripped: yes
|
264
|
{$optimization['number_threads']}
|
265
|
{$optimization['msg_cache_slabs']}
|
266
|
{$optimization['rrset_cache_slabs']}
|
267
|
{$optimization['infra_cache_slabs']}
|
268
|
{$optimization['key_cache_slabs']}
|
269
|
{$optimization['msg_cache_size']}
|
270
|
{$optimization['rrset_cache_size']}
|
271
|
{$optimization['outgoing_range']}
|
272
|
{$optimization['so_rcvbuf']}
|
273
|
{$anchor_file}
|
274
|
prefetch: {$prefetch}
|
275
|
prefetch-key: {$prefetch_key}
|
276
|
# Statistics
|
277
|
{$statistics}
|
278
|
# Interface IP(s) to bind to
|
279
|
{$bindints}
|
280
|
{$outgoingints}
|
281
|
|
282
|
# DNS Rebinding
|
283
|
{$private_addr}
|
284
|
{$private_domains}
|
285
|
|
286
|
# Access lists
|
287
|
include: {$g['unbound_chroot_path']}/access_lists.conf
|
288
|
|
289
|
# Static host entries
|
290
|
include: {$g['unbound_chroot_path']}/host_entries.conf
|
291
|
|
292
|
# Domain overrides
|
293
|
include: {$g['unbound_chroot_path']}/domainoverrides.conf
|
294
|
{$forward_conf}
|
295
|
|
296
|
{$custom_options}
|
297
|
|
298
|
###
|
299
|
# Remote Control Config
|
300
|
###
|
301
|
include: {$g['unbound_chroot_path']}/remotecontrol.conf
|
302
|
|
303
|
EOD;
|
304
|
|
305
|
file_put_contents("{$g['unbound_chroot_path']}/unbound.conf", $unboundconf);
|
306
|
|
307
|
return 0;
|
308
|
}
|
309
|
|
310
|
function unbound_remote_control_setup() {
|
311
|
global $g;
|
312
|
|
313
|
if (!file_exists("{$g['unbound_chroot_path']}/remotecontrol.conf") || !file_exists("{$g['unbound_chroot_path']}/unbound_control.key")) {
|
314
|
$remotcfg = <<<EOF
|
315
|
remote-control:
|
316
|
control-enable: yes
|
317
|
control-interface: 127.0.0.1
|
318
|
control-port: 953
|
319
|
server-key-file: "{$g['unbound_chroot_path']}/unbound_server.key"
|
320
|
server-cert-file: "{$g['unbound_chroot_path']}/unbound_server.pem"
|
321
|
control-key-file: "{$g['unbound_chroot_path']}/unbound_control.key"
|
322
|
control-cert-file: "{$g['unbound_chroot_path']}/unbound_control.pem"
|
323
|
|
324
|
EOF;
|
325
|
|
326
|
file_put_contents("{$g['unbound_chroot_path']}/remotecontrol.conf", $remotcfg);
|
327
|
|
328
|
// Generate our keys
|
329
|
do_as_unbound_user("unbound-control-setup");
|
330
|
|
331
|
}
|
332
|
}
|
333
|
|
334
|
|
335
|
// Read /etc/hosts
|
336
|
function read_hosts() {
|
337
|
|
338
|
/* Open /etc/hosts and extract the only dhcpleases info
|
339
|
* XXX - to convert to an unbound C library which reads /etc/hosts automatically
|
340
|
*/
|
341
|
$etc_hosts = array();
|
342
|
foreach (file('/etc/hosts') as $line) {
|
343
|
$d = preg_split('/\s/', $line, -1, PREG_SPLIT_NO_EMPTY);
|
344
|
if (empty($d) || substr(reset($d), 0, 1) == "#")
|
345
|
continue;
|
346
|
if ($d[3] == "#") {
|
347
|
$ip = array_shift($d);
|
348
|
$fqdn = array_shift($d);
|
349
|
$name = array_shift($d);
|
350
|
if ($fqdn != "empty") {
|
351
|
if ($name != "empty")
|
352
|
array_push($etc_hosts, array(ipaddr => "$ip", fqdn => "$fqdn", name => "$name"));
|
353
|
else
|
354
|
array_push($etc_hosts, array(ipaddr => "$ip", fqdn => "$fqdn"));
|
355
|
}
|
356
|
}
|
357
|
}
|
358
|
return $etc_hosts;
|
359
|
}
|
360
|
|
361
|
function sync_unbound_service() {
|
362
|
global $config, $g;
|
363
|
|
364
|
// Configure chroot
|
365
|
if (!is_dir($g['unbound_chroot_path'])) {
|
366
|
mkdir($g['unbound_chroot_path']);
|
367
|
chown($g['unbound_chroot_path'], "unbound");
|
368
|
chgrp($g['unbound_chroot_path'], "unbound");
|
369
|
}
|
370
|
|
371
|
// Configure our Unbound service
|
372
|
do_as_unbound_user("unbound-anchor");
|
373
|
unbound_remote_control_setup();
|
374
|
unbound_generate_config();
|
375
|
do_as_unbound_user("start");
|
376
|
require_once("service-utils.inc");
|
377
|
if (is_service_running("unbound"))
|
378
|
do_as_unbound_user("restore_cache");
|
379
|
|
380
|
}
|
381
|
|
382
|
function unbound_acl_id_used($id) {
|
383
|
global $config;
|
384
|
|
385
|
if (is_array($config['unbound']['acls']))
|
386
|
foreach($config['unbound']['acls'] as & $acls)
|
387
|
if ($id == $acls['aclid'])
|
388
|
return true;
|
389
|
|
390
|
return false;
|
391
|
}
|
392
|
|
393
|
function unbound_get_next_id() {
|
394
|
$aclid = 0;
|
395
|
while(unbound_acl_id_used($aclid))
|
396
|
$aclid++;
|
397
|
return $aclid;
|
398
|
}
|
399
|
|
400
|
// Execute commands as the user unbound
|
401
|
function do_as_unbound_user($cmd) {
|
402
|
global $g;
|
403
|
|
404
|
switch ($cmd) {
|
405
|
case "start":
|
406
|
mwexec("/usr/sbin/unbound -c {$g['unbound_chroot_path']}/unbound.conf");
|
407
|
break;
|
408
|
case "stop":
|
409
|
mwexec("echo '/usr/sbin/unbound-control stop' | /usr/bin/su -m unbound", true);
|
410
|
break;
|
411
|
case "unbound-anchor":
|
412
|
mwexec("echo '/usr/sbin/unbound-anchor -a {$g['unbound_chroot_path']}/root.key' | /usr/bin/su -m unbound", true);
|
413
|
break;
|
414
|
case "unbound-control-setup":
|
415
|
mwexec("echo '/usr/sbin/unbound-control-setup -d {$g['unbound_chroot_path']}' | /usr/bin/su -m unbound", true);
|
416
|
break;
|
417
|
default:
|
418
|
break;
|
419
|
}
|
420
|
}
|
421
|
|
422
|
function unbound_add_domain_overrides($pvt=false) {
|
423
|
global $config, $g;
|
424
|
|
425
|
$domains = $config['unbound']['domainoverrides'];
|
426
|
|
427
|
$sorted_domains = msort($domains, "domain");
|
428
|
$result = array();
|
429
|
foreach($sorted_domains as $domain) {
|
430
|
$domain_key = current($domain);
|
431
|
if (!isset($result[$domain_key]))
|
432
|
$result[$domain_key] = array();
|
433
|
$result[$domain_key][] = $domain['ip'];
|
434
|
}
|
435
|
|
436
|
// Domain overrides that have multiple entries need multiple stub-addr: added
|
437
|
$domain_entries = "";
|
438
|
foreach($result as $domain=>$ips) {
|
439
|
if ($pvt == true) {
|
440
|
$domain_entries .= "private-domain: \"$domain\"\n";
|
441
|
$domain_entries .= "domain-insecure: \"$domain\"\n";
|
442
|
} else {
|
443
|
$domain_entries .= "stub-zone:\n";
|
444
|
$domain_entries .= "\tname: \"$domain\"\n";
|
445
|
foreach($ips as $ip)
|
446
|
$domain_entries .= "\tstub-addr: $ip\n";
|
447
|
$domain_entries .= "\tstub-prime: no\n";
|
448
|
}
|
449
|
}
|
450
|
|
451
|
if ($pvt == true)
|
452
|
return $domain_entries;
|
453
|
else
|
454
|
file_put_contents("{$g['unbound_chroot_path']}/domainoverrides.conf", $domain_entries);
|
455
|
}
|
456
|
|
457
|
function unbound_add_host_entries() {
|
458
|
global $config, $g;
|
459
|
|
460
|
$unbound_entries = "local-zone: \"{$config['system']['domain']}\" transparent\n";
|
461
|
// IPv4 entries
|
462
|
$unbound_entries .= "local-data-ptr: \"127.0.0.1 localhost\"\n";
|
463
|
$unbound_entries .= "local-data: \"localhost A 127.0.0.1\"\n";
|
464
|
$unbound_entries .= "local-data: \"localhost.{$config['system']['domain']} A 127.0.0.1\"\n";
|
465
|
// IPv6 entries
|
466
|
$unbound_entries .= "local-data-ptr: \"::1 localhost\"\n";
|
467
|
$unbound_entries .= "local-data: \"localhost AAAA ::1\"\n";
|
468
|
$unbound_entries .= "local-data: \"localhost.{$config['system']['domain']} AAAA ::1\"\n";
|
469
|
|
470
|
$listen_addresses = "";
|
471
|
if (isset($config['unbound']['interface'])) {
|
472
|
$interfaces = explode(",", $config['unbound']['interface']);
|
473
|
foreach ($interfaces as $interface) {
|
474
|
if (is_ipaddrv4($interface)) {
|
475
|
$unbound_entries .= "local-data-ptr: \"{$interface} {$config['system']['hostname']}.{$config['system']['domain']}\"\n";
|
476
|
$unbound_entries .= "local-data: \"{$config['system']['hostname']}.{$config['system']['domain']} A {$interface}\"\n";
|
477
|
$unbound_entries .= "local-data: \"{$config['system']['hostname']} A {$interface}\"\n";
|
478
|
} else if (is_ipaddrv6($interface)) {
|
479
|
$unbound_entries .= "local-data: \"{$config['system']['hostname']}.{$config['system']['domain']} AAAA {$interface}\"\n";
|
480
|
$unbound_entries .= "local-data: \"{$config['system']['hostname']} AAAA {$interface}\"\n";
|
481
|
} else {
|
482
|
$if = get_real_interface($interface);
|
483
|
if (does_interface_exist($if)) {
|
484
|
$laddr = find_interface_ip($if);
|
485
|
if (is_ipaddrv4($laddr)) {
|
486
|
$unbound_entries .= "local-data-ptr: \"{$laddr} {$config['system']['hostname']}.{$config['system']['domain']}\"\n";
|
487
|
$unbound_entries .= "local-data: \"{$config['system']['hostname']}.{$config['system']['domain']} A {$laddr}\"\n";
|
488
|
$unbound_entries .= "local-data: \"{$config['system']['hostname']} A {$laddr}\"\n";
|
489
|
}
|
490
|
$laddr6 = find_interface_ipv6($if);
|
491
|
if (is_ipaddrv6($laddr6) && !isset($config['dnsmasq']['strictbind'])) {
|
492
|
$unbound_entries .= "local-data-ptr: \"{$laddr6} {$config['system']['hostname']}.{$config['system']['domain']}\"\n";
|
493
|
$unbound_entries .= "local-data: \"{$config['system']['hostname']}.{$config['system']['domain']} AAAA {$laddr}\"\n";
|
494
|
$unbound_entries .= "local-data: \"{$config['system']['hostname']} AAAA {$laddr}\"\n";
|
495
|
}
|
496
|
}
|
497
|
}
|
498
|
}
|
499
|
}
|
500
|
|
501
|
// Static Host entries
|
502
|
if (isset($config['unbound']['hosts'])) {
|
503
|
$host_entries = "";
|
504
|
$added_item = array();
|
505
|
foreach($config['unbound']['hosts'] as $host) {
|
506
|
$current_host = $host['host'];
|
507
|
if ($host['host'] != "")
|
508
|
$host['host'] = $host['host'].".";
|
509
|
if (!$added_item[$current_host]) {
|
510
|
$host_entries .= "local-data-ptr: \"{$host['ip']} {$host['host']}{$host['domain']}\"\n";
|
511
|
if (is_ipaddrv6($host['ip']))
|
512
|
$host_entries .= "local-data: \"{$host['host']}{$host['domain']} IN AAAA {$host['ip']}\"\n";
|
513
|
else
|
514
|
$host_entries .= "local-data: \"{$host['host']}{$host['domain']} IN A {$host['ip']}\"\n";
|
515
|
if (!empty($host['descr']) && isset($config['unbound']['txtsupport']))
|
516
|
$host_entries .= "local-data: '{$host['host']}{$host['domain']} TXT \"".addslashes($host['descr'])."\"'\n";
|
517
|
|
518
|
// Do not add duplicate entries
|
519
|
$added_item[$current_host] = true;
|
520
|
}
|
521
|
}
|
522
|
$unbound_entries .= $host_entries;
|
523
|
}
|
524
|
|
525
|
// Static DHCP entries
|
526
|
$host_entries = "";
|
527
|
if (isset($config['unbound']['regdhcpstatic']) && is_array($config['dhcpd'])) {
|
528
|
foreach ($config['dhcpd'] as $dhcpif => $dhcpifconf)
|
529
|
if (is_array($dhcpifconf['staticmap']) && isset($dhcpifconf['enable']))
|
530
|
foreach ($dhcpifconf['staticmap'] as $host)
|
531
|
if ($host['ipaddr'] && $host['hostname']) {
|
532
|
$host_entries .= "local-data-ptr: \"{$host['ipaddr']} {$host['hostname']}.{$config['system']['domain']}\"\n";
|
533
|
$host_entries .= "local-data: \"{$host['hostname']}.{$config['system']['domain']} IN A {$host['ipaddr']}\"\n";
|
534
|
if (!empty($host['descr']) && $unboundcfg['txtsupport'] == 'on')
|
535
|
$host_entries .= "local-data: '{$host['hostname']}.{$config['system']['domain']} TXT \"".addslashes($host['descr'])."\"'\n";
|
536
|
}
|
537
|
$unbound_entries .= $host_entries;
|
538
|
}
|
539
|
|
540
|
// Handle DHCPLeases added host entries
|
541
|
$dhcplcfg = read_hosts();
|
542
|
$host_entries = "";
|
543
|
if (is_array($dhcplcfg)) {
|
544
|
foreach($dhcplcfg as $key=>$host) {
|
545
|
$host_entries .= "local-data-ptr: \"{$host['ipaddr']} {$host['fqdn']}\"\n";
|
546
|
$host_entries .= "local-data: \"{$host['fqdn']} IN A {$host['ipaddr']}\"\n";
|
547
|
if (!empty($host['name'])) {
|
548
|
$host_entries .= "local-data-ptr: \"{$host['ipaddr']} {$host['name']}\"\n";
|
549
|
$host_entries .= "local-data: \"{$host['name']} IN A {$host['ipaddr']}\"\n";
|
550
|
}
|
551
|
}
|
552
|
$unbound_entries .= $host_entries;
|
553
|
}
|
554
|
|
555
|
// Write out entries
|
556
|
file_put_contents("{$g['unbound_chroot_path']}/host_entries.conf", $unbound_entries);
|
557
|
}
|
558
|
|
559
|
function unbound_control($action) {
|
560
|
global $config, $g;
|
561
|
|
562
|
$cache_dumpfile = "/var/tmp/unbound_cache";
|
563
|
|
564
|
switch ($action) {
|
565
|
case "start":
|
566
|
// Start Unbound
|
567
|
if ($config['unbound']['enable'] == "on") {
|
568
|
if (!is_service_running("unbound"))
|
569
|
do_as_unbound_user("start");
|
570
|
}
|
571
|
break;
|
572
|
case "stop":
|
573
|
if ($config['unbound']['enable'] == "on")
|
574
|
do_as_unbound_user("stop");
|
575
|
break;
|
576
|
case "reload":
|
577
|
if ($config['unbound']['enable'] == "on")
|
578
|
do_as_unbound_user("reload");
|
579
|
break;
|
580
|
case "dump_cache":
|
581
|
// Dump Unbound's Cache
|
582
|
if ($config['unbound']['dumpcache'] == "on")
|
583
|
do_as_unbound_user("dump_cache");
|
584
|
break;
|
585
|
case "restore_cache":
|
586
|
// Restore Unbound's Cache
|
587
|
if ((is_service_running("unbound")) && ($config['unbound']['dumpcache'] == "on")) {
|
588
|
if (file_exists($cache_dumpfile) && filesize($cache_dumpfile) > 0)
|
589
|
do_as_unbound_user("load_cache < /var/tmp/unbound_cache");
|
590
|
}
|
591
|
break;
|
592
|
default:
|
593
|
break;
|
594
|
|
595
|
}
|
596
|
}
|
597
|
|
598
|
// Generation of Unbound statistics
|
599
|
function unbound_statistics() {
|
600
|
global $config;
|
601
|
|
602
|
if ($config['stats'] == "on") {
|
603
|
$stats_interval = $config['unbound']['stats_interval'];
|
604
|
$cumulative_stats = $config['cumulative_stats'];
|
605
|
if ($config['extended_stats'] == "on")
|
606
|
$extended_stats = "yes";
|
607
|
else
|
608
|
$extended_stats = "no";
|
609
|
} else {
|
610
|
$stats_interval = "0";
|
611
|
$cumulative_stats = "no";
|
612
|
$extended_stats = "no";
|
613
|
}
|
614
|
/* XXX To do - add RRD graphs */
|
615
|
$stats = <<<EOF
|
616
|
# Unbound Statistics
|
617
|
statistics-interval: {$stats_interval}
|
618
|
extended-statistics: yes
|
619
|
statistics-cumulative: yes
|
620
|
|
621
|
EOF;
|
622
|
|
623
|
return $stats;
|
624
|
}
|
625
|
|
626
|
// Unbound Access lists
|
627
|
function unbound_acls_config() {
|
628
|
global $g, $config;
|
629
|
|
630
|
$aclcfg = "access-control: 127.0.0.1/32 allow\n";
|
631
|
$aclcfg .= "access-control: ::1 allow\n";
|
632
|
// Add our networks for active interfaces including localhost
|
633
|
if (!empty($config['unbound']['active_interface']))
|
634
|
$active_interfaces = array_flip(explode(",", $config['unbound']['active_interface']));
|
635
|
else
|
636
|
$active_interfaces = get_configured_interface_with_descr();
|
637
|
|
638
|
$bindints = "";
|
639
|
foreach($active_interfaces as $ubif => $ifdesc) {
|
640
|
$ifip = get_interface_ip($ubif);
|
641
|
if (!is_null($ifip)) {
|
642
|
$subnet_bits = get_interface_subnet($ubif);
|
643
|
$subnet_ip = gen_subnet($ifip, $subnet_bits);
|
644
|
$aclcfg .= "access-control: {$subnet_ip}/{$subnet_bits} allow\n";
|
645
|
}
|
646
|
}
|
647
|
|
648
|
// Configure the custom ACLs
|
649
|
if (is_array($config['unbound']['acls'])) {
|
650
|
foreach($config['unbound']['acls'] as $unbound_acl) {
|
651
|
$aclcfg .= "#{$unbound_acl['aclname']}\n";
|
652
|
foreach($unbound_acl['row'] as $network) {
|
653
|
if ($unbound_acl['aclaction'] == "allow snoop")
|
654
|
$unbound_acl['aclaction'] = "allow_snoop";
|
655
|
$aclcfg .= "access-control: {$network['acl_network']}/{$network['mask']} {$unbound_acl['aclaction']}\n";
|
656
|
}
|
657
|
}
|
658
|
}
|
659
|
// Write out Access list
|
660
|
file_put_contents("{$g['unbound_chroot_path']}/access_lists.conf", $aclcfg);
|
661
|
|
662
|
}
|
663
|
|
664
|
// Generate hosts and reload services
|
665
|
function unbound_hosts_generate() {
|
666
|
// Generate our hosts file
|
667
|
unbound_add_host_entries();
|
668
|
|
669
|
// Reload our service to read the updates
|
670
|
unbound_control("reload");
|
671
|
}
|
672
|
|
673
|
?>
|