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}"
51 IFS
=.
read -r a1 a2 a3 a4
<<< ${addr}
53 echo "${a4}.${a3}.${a2}.${a1}.in-addr.arpa"
59 echo "$(</var/ipfire/red/dns${i})"
64 echo "# This file is automatically generated and any changes"
65 echo "# will be overwritten. DO NOT EDIT!"
70 if [ "${USE_FORWARDERS}" = "1" -a -e "/var/ipfire/red/active" ]; then
72 local broken_forwarders
75 for ns
in $
(read_name_servers
); do
76 test_name_server
${ns} &>/dev
/null
78 # Only use DNSSEC-validating or DNSSEC-aware name servers
80 forwarders
="${forwarders} ${ns}"
83 broken_forwarders
="${broken_forwarders} ${ns}"
88 # Show warning for any broken upstream name servers
89 if [ -n "${broken_forwarders}" ]; then
90 boot_mesg
"Ignoring broken upstream name server(s): ${broken_forwarders:1}" ${WARNING}
94 if [ -n "${broken_forwarders}" -a -z "${forwarders}" ]; then
95 boot_mesg
"Falling back to recursor mode" ${WARNING}
98 elif [ -n "${forwarders}" ]; then
99 boot_mesg
"Configuring upstream name server(s): ${forwarders:1}" ${INFO}
102 echo "${forwarders}" > /var
/ipfire
/red
/dns
103 unbound-control
-q forward
${forwarders}
108 # If forwarders cannot be used we run in recursor mode
109 echo "local recursor" > /var
/ipfire
/red
/dns
110 unbound-control
-q forward off
114 local hostname
=$
(hostname
-f)
115 # 1.1.1.1 is reserved for unused green, skip this
116 if [ -n "${GREEN_ADDRESS}" -a "${GREEN_ADDRESS}" != "1.1.1.1" ]; then
117 unbound-control
-q local_data
"${hostname} ${LOCAL_TTL} IN A ${GREEN_ADDRESS}"
121 for address
in ${GREEN_ADDRESS} ${BLUE_ADDRESS} ${ORANGE_ADDRESS}; do
122 [ -n "${address}" ] ||
continue
123 [ "${address}" = "1.1.1.1" ] && continue
125 address
=$
(ip_address_revptr
${address})
126 unbound-control
-q local_data
"${address} ${LOCAL_TTL} IN PTR ${hostname}"
131 local enabled address hostname domainname
133 while IFS
="," read -r enabled address hostname domainname
; do
134 [ "${enabled}" = "on" ] ||
continue
137 local fqdn
="${hostname}.${domainname}"
139 unbound-control
-q local_data
"${fqdn} ${LOCAL_TTL} IN A ${address}"
141 # Skip reverse resolution if the address equals the GREEN address
142 [ "${address}" = "${GREEN_ADDRESS}" ] && continue
145 address
=$
(ip_address_revptr
${address})
146 unbound-control
-q local_data
"${address} ${LOCAL_TTL} IN PTR ${fqdn}"
147 done < /var
/ipfire
/main
/hosts
150 write_forward_conf
() {
156 local enabled zone server remark
157 while IFS
="," read -r enabled zone server remark
; do
158 # Line must be enabled.
159 [ "${enabled}" = "on" ] ||
continue
161 # Zones that end with .local are commonly used for internal
162 # zones and therefore not signed
165 insecure_zones
="${insecure_zones} ${zone}"
170 echo " name: ${zone}"
171 echo " forward-addr: ${server}"
173 done < /var
/ipfire
/dnsforward
/config
175 if [ -n "${insecure_zones}" ]; then
178 for zone
in ${insecure_zones}; do
179 echo " domain-insecure: ${zone}"
182 ) > /etc
/unbound
/forward.conf
185 write_tuning_conf
() {
186 # https://www.unbound.net/documentation/howto_optimise.html
188 # Determine number of online processors
189 local processors
=$
(getconf _NPROCESSORS_ONLN
)
191 # Determine number of slabs
193 while [ ${slabs} -lt ${processors} ]; do
194 slabs
=$
(( ${slabs} * 2 ))
197 # Determine amount of system memory
198 local mem
=$
(get_memory_amount
)
200 # In the worst case scenario, unbound can use double the
201 # amount of memory allocated to a cache due to malloc overhead
203 # Large systems with more than 2GB of RAM
204 if [ ${mem} -ge 2048 ]; then
207 # Small systems with less than 256MB of RAM
208 elif [ ${mem} -le 256 ]; then
219 # We run one thread per processor
220 echo "num-threads: ${processors}"
222 # Adjust number of slabs
223 echo "infra-cache-slabs: ${slabs}"
224 echo "key-cache-slabs: ${slabs}"
225 echo "msg-cache-slabs: ${slabs}"
226 echo "rrset-cache-slabs: ${slabs}"
229 echo "rrset-cache-size: $(( ${mem} / 2 ))m"
230 echo "msg-cache-size: $(( ${mem} / 4 ))m"
231 echo "key-cache-size: $(( ${mem} / 4 ))m"
232 ) > /etc
/unbound
/tuning.conf
235 get_memory_amount
() {
238 while read -r key val unit
; do
242 echo "$(( ${val} / 1024 ))"
253 # 0 DNSSEC validating
254 # 1 Error: unreachable, etc.
258 # Exit when the server is not reachable
259 ns_is_online
${ns} ||
return 1
261 # Return 0 if validating
262 ns_is_validating
${ns} && return 0
265 for rr
in DNSKEY DS RRSIG
; do
266 if ! ns_forwards_
${rr} ${ns}; then
267 errors
="${errors} ${rr}"
271 if [ -n "${errors}" ]; then
272 echo >&2 "Unable to retrieve the following resource records from ${ns}: ${errors:1}"
280 # Sends an A query to the nameserver w/o DNSSEC
284 dig @
${ns} +nodnssec A
${TEST_DOMAIN} >/dev
/null
287 # Resolving ${TEST_DOMAIN_FAIL} will fail if the nameserver is validating
291 dig @
${ns} A
${TEST_DOMAIN_FAIL} |
grep -q SERVFAIL
294 # Checks if we can retrieve the DNSKEY for this domain.
295 # dig will print the SOA if nothing was found
296 ns_forwards_DNSKEY
() {
299 dig @
${ns} DNSKEY
${TEST_DOMAIN} |
grep -qv SOA
305 dig @
${ns} DS
${TEST_DOMAIN} |
grep -qv SOA
308 ns_forwards_RRSIG
() {
311 dig @
${ns} +dnssec A
${TEST_DOMAIN} |
grep -q RRSIG
317 dig @
${ns} +tcp A
${TEST_DOMAIN} >/dev
/null ||
return 1
322 # Print a nicer messagen when unbound is already running
323 if pidofproc
-s unbound
; then
324 statusproc
/usr
/sbin
/unbound
328 eval $
(/usr
/local
/bin
/readhash
/var
/ipfire
/ethernet
/settings
)
330 # Create control keys at first run
331 if [ ! -r "/etc/unbound/unbound_control.key" ]; then
332 unbound-control-setup
-d /etc
/unbound
&>/dev
/null
335 # Update configuration files
339 boot_mesg
"Starting Unbound DNS Proxy..."
340 loadproc
/usr
/sbin
/unbound ||
exit $?
342 # Make own hostname resolveable
345 # Update any known forwarding name servers
353 boot_mesg
"Stopping Unbound DNS Proxy..."
354 killproc
/usr
/sbin
/unbound
364 statusproc
/usr
/sbin
/unbound
374 test_name_server
${ns}
379 echo "${ns} is validating"
382 echo "${ns} is DNSSEC-aware"
385 echo "${ns} is NOT DNSSEC-aware"
388 echo "Test failed for an unknown reason"
392 if ns_supports_tcp
${ns}; then
393 echo "${ns} supports TCP fallback"
395 echo "${ns} does not support TCP fallback"
402 echo "Usage: $0 {start|stop|restart|status|update-forwarders|test-name-server}"
407 # End $rc_base/init.d/unbound