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"
17 # Cache any local zones for 60 seconds
20 # Load optional configuration
21 [ -e "/etc/sysconfig/unbound" ] && .
/etc
/sysconfig
/unbound
25 IFS
=.
read -r i1 i2 i3 i4
<<< ${1}
26 IFS
=.
read -r m1 m2 m3
m4 <<< ${2}
27 cidr
=$
(printf "%d.%d.%d.%d\n" "$((i1 & m1))" "$((i2 & m2))" "$((i3 & m3))" "$((i4 & m4))")
41 *) echo "Error: $dec is not recognised"; exit 1
44 echo "${cidr}/${nbits}"
50 echo "$(</var/ipfire/red/dns${i})"
55 echo "# This file is automatically generated and any changes"
56 echo "# will be overwritten. DO NOT EDIT!"
61 if [ "${USE_FORWARDERS}" = "1" -a -e "/var/ipfire/red/active" ]; then
63 local broken_forwarders
66 for ns
in $
(read_name_servers
); do
67 test_name_server
${ns} &>/dev
/null
69 # Only use DNSSEC-validating or DNSSEC-aware name servers
71 forwarders
="${forwarders} ${ns}"
74 broken_forwarders
="${broken_forwarders} ${ns}"
79 # Show warning for any broken upstream name servers
80 if [ -n "${broken_forwarders}" ]; then
81 boot_mesg
"Ignoring broken upstream name server(s): ${broken_forwarders:1}" ${WARNING}
85 if [ -n "${broken_forwarders}" -a -z "${forwarders}" ]; then
86 boot_mesg
"Falling back to recursor mode" ${WARNING}
89 elif [ -n "${forwarders}" ]; then
90 boot_mesg
"Configuring upstream name server(s): ${forwarders:1}" ${INFO}
93 unbound-control
-q forward
${forwarders}
98 # If forwarders cannot be used we run in recursor mode
99 unbound-control
-q forward off
103 local enabled address hostname domainname
105 while IFS
="," read -r enabled address hostname domainname
; do
106 [ "${enabled}" = "on" ] ||
continue
109 local fqdn
="${hostname}.${domainname}"
111 unbound-control
-q local_data
"${fqdn} ${LOCAL_TTL} IN A ${address}"
112 done < /var
/ipfire
/main
/hosts
115 write_interfaces_conf
() {
119 if [ -n "${GREEN_ADDRESS}" ]; then
121 echo "interface: ${GREEN_ADDRESS}"
122 echo "access-control: $(cidr ${GREEN_NETADDRESS} ${GREEN_NETMASK}) allow"
125 if [ -n "${BLUE_ADDRESS}" ]; then
127 echo "interface: ${BLUE_ADDRESS}"
128 echo "access-control: $(cidr ${BLUE_NETADDRESS} ${BLUE_NETMASK}) allow"
130 ) > /etc
/unbound
/interfaces.conf
133 write_forward_conf
() {
137 local enabled zone server remark
138 while IFS
="," read -r enabled zone server remark
; do
139 # Line must be enabled.
140 [ "${enabled}" = "on" ] ||
continue
143 echo " name: ${zone}"
144 echo " forward-addr: ${server}"
146 done < /var
/ipfire
/dnsforward
/config
147 ) > /etc
/unbound
/forward.conf
150 write_tuning_conf
() {
151 # https://www.unbound.net/documentation/howto_optimise.html
153 # Determine number of online processors
154 local processors
=$
(getconf _NPROCESSORS_ONLN
)
156 # Determine number of slabs
158 while [ ${slabs} -lt ${processors} ]; do
159 slabs
=$
(( ${slabs} * 2 ))
162 # Determine amount of system memory
163 local mem
=$
(get_memory_amount
)
165 # In the worst case scenario, unbound can use double the
166 # amount of memory allocated to a cache due to malloc overhead
168 # Large systems with more than 2GB of RAM
169 if [ ${mem} -ge 2048 ]; then
172 # Small systems with less than 256MB of RAM
173 elif [ ${mem} -le 256 ]; then
184 # We run one thread per processor
185 echo "num-threads: ${processors}"
187 # Adjust number of slabs
188 echo "infra-cache-slabs: ${slabs}"
189 echo "key-cache-slabs: ${slabs}"
190 echo "msg-cache-slabs: ${slabs}"
191 echo "rrset-cache-slabs: ${slabs}"
194 echo "rrset-cache-size: $(( ${mem} / 2 ))m"
195 echo "msg-cache-size: $(( ${mem} / 4 ))m"
196 echo "key-cache-size: $(( ${mem} / 4 ))m"
197 ) > /etc
/unbound
/tuning.conf
200 get_memory_amount
() {
203 while read -r key val unit
; do
207 echo "$(( ${val} / 1024 ))"
218 # 0 DNSSEC validating
219 # 1 Error: unreachable, etc.
223 # Exit when the server is not reachable
224 ns_is_online
${ns} ||
return 1
226 # Return 0 if validating
227 ns_is_validating
${ns} && return 0
230 for rr
in DNSKEY DS RRSIG
; do
231 if ! ns_forwards_
${rr} ${ns}; then
232 errors
="${errors} ${rr}"
236 if [ -n "${errors}" ]; then
237 echo >&2 "Unable to retrieve the following resource records from ${ns}: ${errors:1}"
245 # Sends an A query to the nameserver w/o DNSSEC
249 dig @
${ns} +nodnssec A
${TEST_DOMAIN} >/dev
/null
252 # Resolving ${TEST_DOMAIN_FAIL} will fail if the nameserver is validating
256 dig @
${ns} A
${TEST_DOMAIN_FAIL} |
grep -q SERVFAIL
259 # Checks if we can retrieve the DNSKEY for this domain.
260 # dig will print the SOA if nothing was found
261 ns_forwards_DNSKEY
() {
264 dig @
${ns} DNSKEY
${TEST_DOMAIN} |
grep -qv SOA
270 dig @
${ns} DS
${TEST_DOMAIN} |
grep -qv SOA
273 ns_forwards_RRSIG
() {
276 dig @
${ns} +dnssec A
${TEST_DOMAIN} |
grep -q RRSIG
282 dig @
${ns} +tcp A
${TEST_DOMAIN} >/dev
/null ||
return 1
287 # Print a nicer messagen when unbound is already running
288 if pidofproc
-s unbound
; then
289 statusproc
/usr
/sbin
/unbound
293 eval $
(/usr
/local
/bin
/readhash
/var
/ipfire
/ethernet
/settings
)
295 # Create control keys at first run
296 if [ ! -r "/etc/unbound/unbound_control.key" ]; then
297 unbound-control-setup
-d /etc
/unbound
&>/dev
/null
300 # Update configuration files
302 write_interfaces_conf
305 boot_mesg
"Starting Unbound DNS Proxy..."
306 loadproc
/usr
/sbin
/unbound ||
exit $?
308 # Update any known forwarding name servers
316 boot_mesg
"Stopping Unbound DNS Proxy..."
317 killproc
/usr
/sbin
/unbound
327 statusproc
/usr
/sbin
/unbound
337 test_name_server
${ns}
342 echo "${ns} is validating"
345 echo "${ns} is DNSSEC-aware"
348 echo "${ns} is NOT DNSSEC-aware"
351 echo "Test failed for an unknown reason"
355 if ns_supports_tcp
${ns}; then
356 echo "${ns} supports TCP fallback"
358 echo "${ns} does not support TCP fallback"
365 echo "Usage: $0 {start|stop|restart|status|update-forwarders|test-name-server}"
370 # End $rc_base/init.d/unbound