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
29 IFS
=.
read -r i1 i2 i3 i4
<<< ${1}
30 IFS
=.
read -r m1 m2 m3
m4 <<< ${2}
31 cidr
=$
(printf "%d.%d.%d.%d\n" "$((i1 & m1))" "$((i2 & m2))" "$((i3 & m3))" "$((i4 & m4))")
45 *) echo "Error: $dec is not recognised"; exit 1
48 echo "${cidr}/${nbits}"
55 IFS
=.
read -r a1 a2 a3 a4
<<< ${addr}
57 echo "${a4}.${a3}.${a2}.${a1}.in-addr.arpa"
63 echo "$(</var/ipfire/red/dns${i})"
68 echo "# This file is automatically generated and any changes"
69 echo "# will be overwritten. DO NOT EDIT!"
74 if [ "${USE_FORWARDERS}" = "1" -a -e "/var/ipfire/red/active" ]; then
76 local broken_forwarders
79 for ns
in $
(read_name_servers
); do
80 test_name_server
${ns} &>/dev
/null
82 # Only use DNSSEC-validating or DNSSEC-aware name servers
84 forwarders
="${forwarders} ${ns}"
87 broken_forwarders
="${broken_forwarders} ${ns}"
92 # Determine EDNS buffer size
93 local new_edns_buffer_size
=${EDNS_DEFAULT_BUFFER_SIZE}
95 for ns
in ${forwarders}; do
96 local edns_buffer_size
=$
(ns_determine_edns_buffer_size
${ns})
97 if [ -n "${edns_buffer_size}" ]; then
98 if [ ${edns_buffer_size} -lt ${new_edns_buffer_size} ]; then
99 new_edns_buffer_size
=${edns_buffer_size}
104 if [ ${new_edns_buffer_size} -lt ${EDNS_DEFAULT_BUFFER_SIZE} ]; then
105 boot_mesg
"EDNS buffer size reduced to ${new_edns_buffer_size}" ${WARNING}
108 unbound-control
-q set_option edns-buffer-size
: ${new_edns_buffer_size}
111 # Show warning for any broken upstream name servers
112 if [ -n "${broken_forwarders}" ]; then
113 boot_mesg
"Ignoring broken upstream name server(s): ${broken_forwarders:1}" ${WARNING}
117 if [ -n "${forwarders}" ]; then
118 boot_mesg
"Configuring upstream name server(s): ${forwarders:1}" ${INFO}
121 # Make sure DNSSEC is activated
124 echo "${forwarders}" > /var
/ipfire
/red
/dns
125 unbound-control
-q forward
${forwarders}
128 # In case we have found no working forwarders
130 # Test if the recursor mode is available
131 if can_resolve_root
+bufsize
=${new_edns_buffer_size}; then
132 # Make sure DNSSEC is activated
135 boot_mesg
"Falling back to recursor mode" ${WARNING}
138 # If not, we set DNSSEC in permissive mode and allow using all recursors
139 elif [ -n "${broken_forwarders}" ]; then
142 boot_mesg
"DNSSEC has been set to permissive mode" ${FAILURE}
145 echo "${broken_forwarders}" > /var
/ipfire
/red
/dns
146 unbound-control
-q forward
${broken_forwarders}
152 # If forwarders cannot be used we run in recursor mode
153 echo "local recursor" > /var
/ipfire
/red
/dns
154 unbound-control
-q forward off
158 local hostname
=$
(hostname
-f)
159 # 1.1.1.1 is reserved for unused green, skip this
160 if [ -n "${GREEN_ADDRESS}" -a "${GREEN_ADDRESS}" != "1.1.1.1" ]; then
161 unbound-control
-q local_data
"${hostname} ${LOCAL_TTL} IN A ${GREEN_ADDRESS}"
165 for address
in ${GREEN_ADDRESS} ${BLUE_ADDRESS} ${ORANGE_ADDRESS}; do
166 [ -n "${address}" ] ||
continue
167 [ "${address}" = "1.1.1.1" ] && continue
169 address
=$
(ip_address_revptr
${address})
170 unbound-control
-q local_data
"${address} ${LOCAL_TTL} IN PTR ${hostname}"
175 local enabled address hostname domainname
177 while IFS
="," read -r enabled address hostname domainname
; do
178 [ "${enabled}" = "on" ] ||
continue
181 local fqdn
="${hostname}.${domainname}"
183 unbound-control
-q local_data
"${fqdn} ${LOCAL_TTL} IN A ${address}"
185 # Skip reverse resolution if the address equals the GREEN address
186 [ "${address}" = "${GREEN_ADDRESS}" ] && continue
189 address
=$
(ip_address_revptr
${address})
190 unbound-control
-q local_data
"${address} ${LOCAL_TTL} IN PTR ${fqdn}"
191 done < /var
/ipfire
/main
/hosts
194 write_forward_conf
() {
198 local insecure_zones
="${INSECURE_ZONES}"
200 local enabled zone server remark
201 while IFS
="," read -r enabled zone server remark
; do
202 # Line must be enabled.
203 [ "${enabled}" = "on" ] ||
continue
205 # Zones that end with .local are commonly used for internal
206 # zones and therefore not signed
209 insecure_zones
="${insecure_zones} ${zone}"
214 echo " name: ${zone}"
215 echo " forward-addr: ${server}"
217 done < /var
/ipfire
/dnsforward
/config
219 if [ -n "${insecure_zones}" ]; then
222 for zone
in ${insecure_zones}; do
223 echo " domain-insecure: ${zone}"
226 ) > /etc
/unbound
/forward.conf
229 write_tuning_conf
() {
230 # https://www.unbound.net/documentation/howto_optimise.html
232 # Determine number of online processors
233 local processors
=$
(getconf _NPROCESSORS_ONLN
)
235 # Determine number of slabs
237 while [ ${slabs} -lt ${processors} ]; do
238 slabs
=$
(( ${slabs} * 2 ))
241 # Determine amount of system memory
242 local mem
=$
(get_memory_amount
)
244 # In the worst case scenario, unbound can use double the
245 # amount of memory allocated to a cache due to malloc overhead
247 # Even larger systems with more than 8GB of RAM
248 if [ ${mem} -ge 8192 ]; then
251 # Extra large systems with more than 4GB of RAM
252 elif [ ${mem} -ge 4096 ]; then
255 # Large systems with more than 2GB of RAM
256 elif [ ${mem} -ge 2048 ]; then
259 # Medium systems with more than 1GB of RAM
260 elif [ ${mem} -ge 1024 ]; then
263 # Small systems with less than 256MB of RAM
264 elif [ ${mem} -le 256 ]; then
275 # We run one thread per processor
276 echo "num-threads: ${processors}"
277 echo "so-reuseport: yes"
279 # Adjust number of slabs
280 echo "infra-cache-slabs: ${slabs}"
281 echo "key-cache-slabs: ${slabs}"
282 echo "msg-cache-slabs: ${slabs}"
283 echo "rrset-cache-slabs: ${slabs}"
286 echo "rrset-cache-size: $(( ${mem} / 2 ))m"
287 echo "msg-cache-size: $(( ${mem} / 4 ))m"
288 echo "key-cache-size: $(( ${mem} / 4 ))m"
290 # Increase parallel queries
291 echo "outgoing-range: 8192"
292 echo "num-queries-per-thread: 4096"
294 # Use larger send/receive buffers
297 ) > /etc
/unbound
/tuning.conf
300 get_memory_amount
() {
303 while read -r key val unit
; do
307 echo "$(( ${val} / 1024 ))"
319 # 0 DNSSEC validating
320 # 1 Error: unreachable, etc.
324 # Exit when the server is not reachable
325 ns_is_online
${ns} ||
return 1
327 # Determine the maximum edns buffer size that works
328 local edns_buffer_size
=$
(ns_determine_edns_buffer_size
${ns})
329 if [ -n "${edns_buffer_size}" ]; then
330 args
="${args} +bufsize=${edns_buffer_size}"
334 for rr
in DNSKEY DS RRSIG
; do
335 if ! ns_forwards_
${rr} ${ns} ${args}; then
336 errors
="${errors} ${rr}"
340 if [ -n "${errors}" ]; then
341 echo >&2 "Unable to retrieve the following resource records from ${ns}: ${errors:1}"
345 if ns_is_validating
${ns} ${args}; then
346 # Return 0 if validating
354 # Sends an A query to the nameserver w/o DNSSEC
359 dig @
${ns} +nodnssec A
${TEST_DOMAIN} $@
>/dev
/null
362 # Resolving ${TEST_DOMAIN_FAIL} will fail if the nameserver is validating
367 dig @
${ns} A
${TEST_DOMAIN_FAIL} $@ |
grep -q SERVFAIL
370 # Checks if we can retrieve the DNSKEY for this domain.
371 # dig will print the SOA if nothing was found
372 ns_forwards_DNSKEY
() {
376 dig @
${ns} DNSKEY
${TEST_DOMAIN} $@ |
grep -qv SOA
383 dig @
${ns} DS
${TEST_DOMAIN} $@ |
grep -qv SOA
386 ns_forwards_RRSIG
() {
390 dig @
${ns} +dnssec A
${TEST_DOMAIN} $@ |
grep -q RRSIG
397 dig @
${ns} +tcp A
${TEST_DOMAIN} $@
>/dev
/null ||
return 1
400 ns_determine_edns_buffer_size
() {
405 for b
in 4096 2048 1500 1480 1464 1400 1280 512; do
406 if dig @
${ns} +dnssec +bufsize=${b} A ${TEST_DOMAIN} $@
>/dev
/null
; then
415 get_root_nameservers
() {
416 while read -r hostname ttl record address
; do
417 # Searching for A records
418 [ "${record}" = "A" ] ||
continue
421 done < /etc
/unbound
/root.hints
426 for ns
in $
(get_root_nameservers
); do
427 if dig @
${ns} +dnssec SOA . $@
>/dev
/null
; then
432 # none of the servers was reachable
437 local status
=$
(unbound-control get_option val-permissive-mode
)
439 # Don't do anything if DNSSEC is already activated
440 [ "${status}" = "no" ] && return 0
443 echo "on" > /var
/ipfire
/red
/dnssec-status
445 # Activate DNSSEC and flush cache with any stale and unvalidated data
446 unbound-control
-q set_option val-permissive-mode
: no
447 unbound-control
-q flush_zone .
452 echo "off" > /var
/ipfire
/red
/dnssec-status
454 unbound-control
-q set_option val-permissive-mode
: yes
459 # Print a nicer messagen when unbound is already running
460 if pidofproc
-s unbound
; then
461 statusproc
/usr
/sbin
/unbound
465 eval $
(/usr
/local
/bin
/readhash
/var
/ipfire
/ethernet
/settings
)
467 # Create control keys at first run
468 if [ ! -r "/etc/unbound/unbound_control.key" ]; then
469 unbound-control-setup
-d /etc
/unbound
&>/dev
/null
472 # Update configuration files
476 boot_mesg
"Starting Unbound DNS Proxy..."
477 loadproc
/usr
/sbin
/unbound ||
exit $?
479 # Make own hostname resolveable
482 # Update any known forwarding name servers
490 boot_mesg
"Stopping Unbound DNS Proxy..."
491 killproc
/usr
/sbin
/unbound
501 statusproc
/usr
/sbin
/unbound
505 # Do not try updating forwarders when unbound is not running
506 if ! pgrep unbound
&>/dev
/null
; then
516 test_name_server
${ns}
521 echo "${ns} is validating"
524 echo "${ns} is DNSSEC-aware"
527 echo "${ns} is NOT DNSSEC-aware"
530 echo "Test failed for an unknown reason"
535 if ns_supports_tcp
${ns}; then
536 echo "${ns} supports TCP fallback"
538 echo "${ns} does not support TCP fallback"
541 edns_buffer_size
=$
(ns_determine_edns_buffer_size
${ns})
542 if [ -n "${edns_buffer_size}" ]; then
543 echo "EDNS buffer size for ${ns}: ${edns_buffer_size}"
550 echo "Usage: $0 {start|stop|restart|status|update-forwarders|test-name-server}"
555 # End $rc_base/init.d/unbound