2 # Begin $rc_base/init.d/unbound
4 # Description : Unbound DNS resolver boot script for IPfire
5 # Author : Marcel Lorenz <marcel.lorenz@ipfire.org>
10 TEST_DOMAIN
="ipfire.org"
12 # This domain will never validate
13 TEST_DOMAIN_FAIL
="dnssec-failed.org"
18 # Cache any local zones for 60 seconds
22 EDNS_DEFAULT_BUFFER_SIZE
=4096
24 # Load optional configuration
25 [ -e "/etc/sysconfig/unbound" ] && .
/etc
/sysconfig
/unbound
31 IFS
=.
read -r a1 a2 a3 a4
<<< ${addr}
33 echo "${a4}.${a3}.${a2}.${a1}.in-addr.arpa"
39 echo "$(</var/ipfire/red/dns${i})"
40 done 2>/dev
/null |
xargs echo
44 echo "# This file is automatically generated and any changes"
45 echo "# will be overwritten. DO NOT EDIT!"
50 if [ "${USE_FORWARDERS}" = "1" -a -e "/var/ipfire/red/active" ]; then
52 local broken_forwarders
55 for ns
in $
(read_name_servers
); do
56 test_name_server
${ns} &>/dev
/null
58 # Only use DNSSEC-validating or DNSSEC-aware name servers
60 forwarders
="${forwarders} ${ns}"
63 broken_forwarders
="${broken_forwarders} ${ns}"
68 # Determine EDNS buffer size
69 local new_edns_buffer_size
=${EDNS_DEFAULT_BUFFER_SIZE}
71 for ns
in ${forwarders}; do
72 local edns_buffer_size
=$
(ns_determine_edns_buffer_size
${ns})
73 if [ -n "${edns_buffer_size}" ]; then
74 if [ ${edns_buffer_size} -lt ${new_edns_buffer_size} ]; then
75 new_edns_buffer_size
=${edns_buffer_size}
80 if [ ${new_edns_buffer_size} -lt ${EDNS_DEFAULT_BUFFER_SIZE} ]; then
81 boot_mesg
"EDNS buffer size reduced to ${new_edns_buffer_size}" ${WARNING}
84 unbound-control
-q set_option edns-buffer-size
: ${new_edns_buffer_size}
87 # Show warning for any broken upstream name servers
88 if [ -n "${broken_forwarders}" ]; then
89 boot_mesg
"Ignoring broken upstream name server(s): ${broken_forwarders:1}" ${WARNING}
93 if [ -n "${forwarders}" ]; then
94 boot_mesg
"Configuring upstream name server(s): ${forwarders:1}" ${INFO}
97 # Make sure DNSSEC is activated
100 echo "${forwarders}" > /var
/ipfire
/red
/dns
101 unbound-control
-q forward
${forwarders}
104 # In case we have found no working forwarders
106 # Test if the recursor mode is available
107 if can_resolve_root
+bufsize
=${new_edns_buffer_size}; then
108 # Make sure DNSSEC is activated
111 boot_mesg
"Falling back to recursor mode" ${WARNING}
114 # If not, we set DNSSEC in permissive mode and allow using all recursors
115 elif [ -n "${broken_forwarders}" ]; then
118 boot_mesg
"DNSSEC has been set to permissive mode" ${FAILURE}
121 echo "${broken_forwarders}" > /var
/ipfire
/red
/dns
122 unbound-control
-q forward
${broken_forwarders}
128 # If forwarders cannot be used we run in recursor mode
129 echo "local recursor" > /var
/ipfire
/red
/dns
130 unbound-control
-q forward off
134 local hostname
=$
(hostname
-f)
135 # 1.1.1.1 is reserved for unused green, skip this
136 if [ -n "${GREEN_ADDRESS}" -a "${GREEN_ADDRESS}" != "1.1.1.1" ]; then
137 unbound-control
-q local_data
"${hostname} ${LOCAL_TTL} IN A ${GREEN_ADDRESS}"
141 for address
in ${GREEN_ADDRESS} ${BLUE_ADDRESS} ${ORANGE_ADDRESS}; do
142 [ -n "${address}" ] ||
continue
143 [ "${address}" = "1.1.1.1" ] && continue
145 address
=$
(ip_address_revptr
${address})
146 unbound-control
-q local_data
"${address} ${LOCAL_TTL} IN PTR ${hostname}"
151 local enabled address hostname domainname generateptr
153 while IFS
="," read -r enabled address hostname domainname generateptr
; do
154 [ "${enabled}" = "on" ] ||
continue
157 local fqdn
="${hostname}.${domainname}"
159 unbound-control
-q local_data
"${fqdn} ${LOCAL_TTL} IN A ${address}"
161 # Skip reverse resolution if the address equals the GREEN address
162 [ "${address}" = "${GREEN_ADDRESS}" ] && continue
164 # Skip reverse resolution if user requested not to do so
165 [ "${generateptr}" = "off" ] && continue
168 address
=$
(ip_address_revptr
${address})
169 unbound-control
-q local_data
"${address} ${LOCAL_TTL} IN PTR ${fqdn}"
170 done < /var
/ipfire
/main
/hosts
173 write_forward_conf
() {
177 local insecure_zones
="${INSECURE_ZONES}"
179 local enabled zone server servers remark disable_dnssec rest
180 while IFS
="," read -r enabled zone servers remark disable_dnssec rest
; do
181 # Line must be enabled.
182 [ "${enabled}" = "on" ] ||
continue
184 # Zones that end with .local are commonly used for internal
185 # zones and therefore not signed
188 insecure_zones
="${insecure_zones} ${zone}"
191 if [ "${disable_dnssec}" = "on" ]; then
192 insecure_zones
="${insecure_zones} ${zone}"
197 # Reverse-lookup zones must be stubs
201 echo " name: ${zone}"
202 for server
in ${servers//|/ }; do
203 if [[ ${server} =~ ^
[0-9]+\.
[0-9]+\.
[0-9]+\.
[0-9]+$
]]; then
204 echo " stub-addr: ${server}"
206 echo " stub-host: ${server}"
211 echo " local-zone: \"${zone}\" transparent"
216 echo " name: ${zone}"
217 for server
in ${servers//|/ }; do
218 if [[ ${server} =~ ^
[0-9]+\.
[0-9]+\.
[0-9]+\.
[0-9]+$
]]; then
219 echo " forward-addr: ${server}"
221 echo " forward-host: ${server}"
227 done < /var
/ipfire
/dnsforward
/config
229 if [ -n "${insecure_zones}" ]; then
232 for zone
in ${insecure_zones}; do
233 echo " domain-insecure: ${zone}"
236 ) > /etc
/unbound
/forward.conf
239 write_tuning_conf
() {
240 # https://www.unbound.net/documentation/howto_optimise.html
242 # Determine number of online processors
243 local processors
=$
(getconf _NPROCESSORS_ONLN
)
245 # Determine number of slabs
247 while [ ${slabs} -lt ${processors} ]; do
248 slabs
=$
(( ${slabs} * 2 ))
251 # Determine amount of system memory
252 local mem
=$
(get_memory_amount
)
254 # In the worst case scenario, unbound can use double the
255 # amount of memory allocated to a cache due to malloc overhead
257 # Even larger systems with more than 8GB of RAM
258 if [ ${mem} -ge 8192 ]; then
261 # Extra large systems with more than 4GB of RAM
262 elif [ ${mem} -ge 4096 ]; then
265 # Large systems with more than 2GB of RAM
266 elif [ ${mem} -ge 2048 ]; then
269 # Medium systems with more than 1GB of RAM
270 elif [ ${mem} -ge 1024 ]; then
273 # Small systems with less than 256MB of RAM
274 elif [ ${mem} -le 256 ]; then
285 # We run one thread per processor
286 echo "num-threads: ${processors}"
287 echo "so-reuseport: yes"
289 # Adjust number of slabs
290 echo "infra-cache-slabs: ${slabs}"
291 echo "key-cache-slabs: ${slabs}"
292 echo "msg-cache-slabs: ${slabs}"
293 echo "rrset-cache-slabs: ${slabs}"
296 echo "rrset-cache-size: $(( ${mem} / 2 ))m"
297 echo "msg-cache-size: $(( ${mem} / 4 ))m"
298 echo "key-cache-size: $(( ${mem} / 4 ))m"
300 # Increase parallel queries
301 echo "outgoing-range: 8192"
302 echo "num-queries-per-thread: 4096"
304 # Use larger send/receive buffers
307 ) > /etc
/unbound
/tuning.conf
310 get_memory_amount
() {
313 while read -r key val unit
; do
317 echo "$(( ${val} / 1024 ))"
329 # 0 DNSSEC validating
330 # 1 Error: unreachable, etc.
334 # Exit when the server is not reachable
335 ns_is_online
${ns} ||
return 1
337 # Determine the maximum edns buffer size that works
338 local edns_buffer_size
=$
(ns_determine_edns_buffer_size
${ns})
339 if [ -n "${edns_buffer_size}" ]; then
340 args
="${args} +bufsize=${edns_buffer_size}"
344 for rr
in DNSKEY DS RRSIG
; do
345 if ! ns_forwards_
${rr} ${ns} ${args}; then
346 errors
="${errors} ${rr}"
350 if [ -n "${errors}" ]; then
351 echo >&2 "Unable to retrieve the following resource records from ${ns}: ${errors:1}"
355 if ns_is_validating
${ns} ${args}; then
356 # Return 0 if validating
364 # Sends an A query to the nameserver w/o DNSSEC
369 dig @
${ns} +nodnssec A
${TEST_DOMAIN} $@
>/dev
/null
372 # Resolving ${TEST_DOMAIN_FAIL} will fail if the nameserver is validating
377 if ! dig @
${ns} A
${TEST_DOMAIN_FAIL} $@ |
grep -q SERVFAIL
; then
380 # Determine if NS replies with "ad" data flag if DNSSEC enabled
381 dig @
${ns} +dnssec SOA ${TEST_DOMAIN} $@ | awk -F: '/\;\;\ flags\:/ { s=1; if (/\ ad/) s=0; exit s }'
385 # Checks if we can retrieve the DNSKEY for this domain.
386 # dig will print the SOA if nothing was found
387 ns_forwards_DNSKEY() {
391 dig @${ns} DNSKEY ${TEST_DOMAIN} $@ | grep -qv SOA
398 dig @${ns} DS ${TEST_DOMAIN} $@ | grep -qv SOA
401 ns_forwards_RRSIG() {
405 dig @${ns} +dnssec A ${TEST_DOMAIN} $@ | grep -q RRSIG
412 dig @${ns} +tcp A ${TEST_DOMAIN} $@ >/dev/null || return 1
415 ns_determine_edns_buffer_size() {
420 for b in 4096 2048 1500 1480 1464 1400 1280 512; do
421 if dig @${ns} +dnssec +bufsize=${b} A ${TEST_DOMAIN} $@ >/dev/null; then
430 get_root_nameservers() {
431 while read -r hostname ttl record address; do
432 # Searching for A records
433 [ "${record}" = "A" ] || continue
436 done < /etc/unbound/root.hints
441 for ns in $(get_root_nameservers); do
442 if dig @${ns} +dnssec SOA . $@ >/dev/null; then
447 # none of the servers was reachable
452 local status=$(unbound-control get_option val-permissive-mode)
455 echo "on" > /var/ipfire/red/dnssec-status
457 # Don't
do anything
if DNSSEC is already activated
458 [ "${status}" = "no" ] && return 0
460 # Activate DNSSEC and flush cache with any stale and unvalidated data
461 unbound-control
-q set_option val-permissive-mode
: no
462 unbound-control
-q flush_zone .
467 echo "off" > /var
/ipfire
/red
/dnssec-status
469 unbound-control
-q set_option val-permissive-mode
: yes
472 fix_time_if_dns_fail
() {
473 # If DNS still not work try to init ntp with
474 # hardcoded ntp.ipfire.org (81.3.27.46)
475 if [ -e /var
/ipfire
/red
/active
]; then
476 host 0.ipfire.pool.ntp.org
> /dev
/null
2>&1
477 if [ "${?}" != "0" ]; then
478 boot_mesg
"DNS still not functioning... Trying to sync time with ntp.ipfire.org (81.3.27.46)..."
479 loadproc
/usr
/local
/bin
/settime
81.3.27.46
486 # Print a nicer messagen when unbound is already running
487 if pidofproc
-s unbound
; then
488 statusproc
/usr
/sbin
/unbound
492 eval $
(/usr
/local
/bin
/readhash
/var
/ipfire
/ethernet
/settings
)
494 # Update configuration files
498 boot_mesg
"Starting Unbound DNS Proxy..."
499 loadproc
/usr
/sbin
/unbound ||
exit $?
501 # Make own hostname resolveable
504 # Update any known forwarding name servers
514 boot_mesg
"Stopping Unbound DNS Proxy..."
515 killproc
/usr
/sbin
/unbound
525 statusproc
/usr
/sbin
/unbound
529 # Do not try updating forwarders when unbound is not running
530 if ! pgrep unbound
&>/dev
/null
; then
536 unbound-control flush_negative
> /dev
/null
537 unbound-control flush_bogus
> /dev
/null
545 test_name_server
${ns}
550 echo "${ns} is validating"
553 echo "${ns} is DNSSEC-aware"
556 echo "${ns} is NOT DNSSEC-aware"
559 echo "Test failed for an unknown reason"
564 if ns_supports_tcp
${ns}; then
565 echo "${ns} supports TCP fallback"
567 echo "${ns} does not support TCP fallback"
570 edns_buffer_size
=$
(ns_determine_edns_buffer_size
${ns})
571 if [ -n "${edns_buffer_size}" ]; then
572 echo "EDNS buffer size for ${ns}: ${edns_buffer_size}"
579 echo "Usage: $0 {start|stop|restart|status|update-forwarders|test-name-server}"
584 # End $rc_base/init.d/unbound