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 "${broken_forwarders}" -a -z "${forwarders}" ]; then
118 boot_mesg
"Falling back to recursor mode" ${WARNING}
121 elif [ -n "${forwarders}" ]; then
122 boot_mesg
"Configuring upstream name server(s): ${forwarders:1}" ${INFO}
125 echo "${forwarders}" > /var
/ipfire
/red
/dns
126 unbound-control
-q forward
${forwarders}
131 # If forwarders cannot be used we run in recursor mode
132 echo "local recursor" > /var
/ipfire
/red
/dns
133 unbound-control
-q forward off
137 local hostname
=$
(hostname
-f)
138 # 1.1.1.1 is reserved for unused green, skip this
139 if [ -n "${GREEN_ADDRESS}" -a "${GREEN_ADDRESS}" != "1.1.1.1" ]; then
140 unbound-control
-q local_data
"${hostname} ${LOCAL_TTL} IN A ${GREEN_ADDRESS}"
144 for address
in ${GREEN_ADDRESS} ${BLUE_ADDRESS} ${ORANGE_ADDRESS}; do
145 [ -n "${address}" ] ||
continue
146 [ "${address}" = "1.1.1.1" ] && continue
148 address
=$
(ip_address_revptr
${address})
149 unbound-control
-q local_data
"${address} ${LOCAL_TTL} IN PTR ${hostname}"
154 local enabled address hostname domainname
156 while IFS
="," read -r enabled address hostname domainname
; do
157 [ "${enabled}" = "on" ] ||
continue
160 local fqdn
="${hostname}.${domainname}"
162 unbound-control
-q local_data
"${fqdn} ${LOCAL_TTL} IN A ${address}"
164 # Skip reverse resolution if the address equals the GREEN address
165 [ "${address}" = "${GREEN_ADDRESS}" ] && 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 remark
180 while IFS
="," read -r enabled zone server remark
; 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}"
193 echo " name: ${zone}"
194 echo " forward-addr: ${server}"
196 done < /var
/ipfire
/dnsforward
/config
198 if [ -n "${insecure_zones}" ]; then
201 for zone
in ${insecure_zones}; do
202 echo " domain-insecure: ${zone}"
205 ) > /etc
/unbound
/forward.conf
208 write_tuning_conf
() {
209 # https://www.unbound.net/documentation/howto_optimise.html
211 # Determine number of online processors
212 local processors
=$
(getconf _NPROCESSORS_ONLN
)
214 # Determine number of slabs
216 while [ ${slabs} -lt ${processors} ]; do
217 slabs
=$
(( ${slabs} * 2 ))
220 # Determine amount of system memory
221 local mem
=$
(get_memory_amount
)
223 # In the worst case scenario, unbound can use double the
224 # amount of memory allocated to a cache due to malloc overhead
226 # Large systems with more than 2GB of RAM
227 if [ ${mem} -ge 2048 ]; then
230 # Small systems with less than 256MB of RAM
231 elif [ ${mem} -le 256 ]; then
242 # We run one thread per processor
243 echo "num-threads: ${processors}"
245 # Adjust number of slabs
246 echo "infra-cache-slabs: ${slabs}"
247 echo "key-cache-slabs: ${slabs}"
248 echo "msg-cache-slabs: ${slabs}"
249 echo "rrset-cache-slabs: ${slabs}"
252 echo "rrset-cache-size: $(( ${mem} / 2 ))m"
253 echo "msg-cache-size: $(( ${mem} / 4 ))m"
254 echo "key-cache-size: $(( ${mem} / 4 ))m"
255 ) > /etc
/unbound
/tuning.conf
258 get_memory_amount
() {
261 while read -r key val unit
; do
265 echo "$(( ${val} / 1024 ))"
277 # 0 DNSSEC validating
278 # 1 Error: unreachable, etc.
282 # Exit when the server is not reachable
283 ns_is_online
${ns} ||
return 1
285 # Determine the maximum edns buffer size that works
286 local edns_buffer_size
=$
(ns_determine_edns_buffer_size
${ns})
287 if [ -n "${edns_buffer_size}" ]; then
288 args
="${args} +bufsize=${edns_buffer_size}"
292 for rr
in DNSKEY DS RRSIG
; do
293 if ! ns_forwards_
${rr} ${ns} ${args}; then
294 errors
="${errors} ${rr}"
298 if [ -n "${errors}" ]; then
299 echo >&2 "Unable to retrieve the following resource records from ${ns}: ${errors:1}"
303 if ns_is_validating
${ns} ${args}; then
304 # Return 0 if validating
312 # Sends an A query to the nameserver w/o DNSSEC
317 dig @
${ns} +nodnssec A
${TEST_DOMAIN} $@
>/dev
/null
320 # Resolving ${TEST_DOMAIN_FAIL} will fail if the nameserver is validating
325 dig @
${ns} A
${TEST_DOMAIN_FAIL} $@ |
grep -q SERVFAIL
328 # Checks if we can retrieve the DNSKEY for this domain.
329 # dig will print the SOA if nothing was found
330 ns_forwards_DNSKEY
() {
334 dig @
${ns} DNSKEY
${TEST_DOMAIN} $@ |
grep -qv SOA
341 dig @
${ns} DS
${TEST_DOMAIN} $@ |
grep -qv SOA
344 ns_forwards_RRSIG
() {
348 dig @
${ns} +dnssec A
${TEST_DOMAIN} $@ |
grep -q RRSIG
355 dig @
${ns} +tcp A
${TEST_DOMAIN} $@
>/dev
/null ||
return 1
358 ns_determine_edns_buffer_size
() {
363 for b
in 4096 2048 1500 1480 1464 1400 1280 512; do
364 if dig @
${ns} +dnssec +bufsize=${b} A ${TEST_DOMAIN} $@
>/dev
/null
; then
375 # Print a nicer messagen when unbound is already running
376 if pidofproc
-s unbound
; then
377 statusproc
/usr
/sbin
/unbound
381 eval $
(/usr
/local
/bin
/readhash
/var
/ipfire
/ethernet
/settings
)
383 # Create control keys at first run
384 if [ ! -r "/etc/unbound/unbound_control.key" ]; then
385 unbound-control-setup
-d /etc
/unbound
&>/dev
/null
388 # Update configuration files
392 boot_mesg
"Starting Unbound DNS Proxy..."
393 loadproc
/usr
/sbin
/unbound ||
exit $?
395 # Make own hostname resolveable
398 # Update any known forwarding name servers
406 boot_mesg
"Stopping Unbound DNS Proxy..."
407 killproc
/usr
/sbin
/unbound
417 statusproc
/usr
/sbin
/unbound
421 # Do not try updating forwarders when unbound is not running
422 if ! pgrep unbound
&>/dev
/null
; then
432 test_name_server
${ns}
437 echo "${ns} is validating"
440 echo "${ns} is DNSSEC-aware"
443 echo "${ns} is NOT DNSSEC-aware"
446 echo "Test failed for an unknown reason"
451 if ns_supports_tcp
${ns}; then
452 echo "${ns} supports TCP fallback"
454 echo "${ns} does not support TCP fallback"
457 edns_buffer_size
=$
(ns_determine_edns_buffer_size
${ns})
458 if [ -n "${edns_buffer_size}" ]; then
459 echo "EDNS buffer size for ${ns}: ${edns_buffer_size}"
466 echo "Usage: $0 {start|stop|restart|status|update-forwarders|test-name-server}"
471 # End $rc_base/init.d/unbound