]> git.ipfire.org Git - ipfire-2.x.git/blame - src/initscripts/init.d/unbound
Start Core Update 110
[ipfire-2.x.git] / src / initscripts / init.d / unbound
CommitLineData
d0e5f71f
ML
1#!/bin/sh
2# Begin $rc_base/init.d/unbound
3
4# Description : Unbound DNS resolver boot script for IPfire
5# Author : Marcel Lorenz <marcel.lorenz@ipfire.org>
d0e5f71f
ML
6
7. /etc/sysconfig/rc
8. ${rc_functions}
9
b29c97b1
AF
10TEST_DOMAIN="ipfire.org"
11
12# This domain will never validate
13TEST_DOMAIN_FAIL="dnssec-failed.org"
14
7ebc0a16 15INSECURE_ZONES=
b8f5eda8 16USE_FORWARDERS=1
d0e5f71f 17
36792be6
MT
18# Cache any local zones for 60 seconds
19LOCAL_TTL=60
20
b2f96a94
MT
21# EDNS buffer size
22EDNS_DEFAULT_BUFFER_SIZE=4096
23
b8f5eda8
MT
24# Load optional configuration
25[ -e "/etc/sysconfig/unbound" ] && . /etc/sysconfig/unbound
d0e5f71f
ML
26
27function cidr() {
28 local cidr nbits IFS;
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))")
32 nbits=0
33 IFS=.
34 for dec in $2 ; do
35 case $dec in
36 255) let nbits+=8;;
37 254) let nbits+=7;;
38 252) let nbits+=6;;
39 248) let nbits+=5;;
40 240) let nbits+=4;;
41 224) let nbits+=3;;
42 192) let nbits+=2;;
43 128) let nbits+=1;;
44 0);;
45 *) echo "Error: $dec is not recognised"; exit 1
46 esac
47 done
48 echo "${cidr}/${nbits}"
49}
50
f75c279b
AF
51ip_address_revptr() {
52 local addr=${1}
53
54 local a1 a2 a3 a4
55 IFS=. read -r a1 a2 a3 a4 <<< ${addr}
56
57 echo "${a4}.${a3}.${a2}.${a1}.in-addr.arpa"
58}
59
b8f5eda8
MT
60read_name_servers() {
61 local i
62 for i in 1 2; do
63 echo "$(</var/ipfire/red/dns${i})"
64 done | xargs echo
65}
66
67config_header() {
68 echo "# This file is automatically generated and any changes"
69 echo "# will be overwritten. DO NOT EDIT!"
70 echo
71}
72
73update_forwarders() {
b29c97b1
AF
74 if [ "${USE_FORWARDERS}" = "1" -a -e "/var/ipfire/red/active" ]; then
75 local forwarders
76 local broken_forwarders
77
78 local ns
79 for ns in $(read_name_servers); do
80 test_name_server ${ns} &>/dev/null
81 case "$?" in
82 # Only use DNSSEC-validating or DNSSEC-aware name servers
83 0|2)
84 forwarders="${forwarders} ${ns}"
85 ;;
86 *)
87 broken_forwarders="${broken_forwarders} ${ns}"
88 ;;
89 esac
90 done
91
8f3034d0 92 # Determine EDNS buffer size
b2f96a94 93 local new_edns_buffer_size=${EDNS_DEFAULT_BUFFER_SIZE}
8f3034d0 94
b2f96a94
MT
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}
8f3034d0 100 fi
b2f96a94
MT
101 fi
102 done
103
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}
106 echo_warning
8f3034d0
MT
107
108 unbound-control -q set_option edns-buffer-size: ${new_edns_buffer_size}
109 fi
110
b29c97b1
AF
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}
114 echo_warning
115 fi
b8f5eda8 116
b29c97b1
AF
117 if [ -n "${broken_forwarders}" -a -z "${forwarders}" ]; then
118 boot_mesg "Falling back to recursor mode" ${WARNING}
119 echo_warning
b8f5eda8 120
b29c97b1
AF
121 elif [ -n "${forwarders}" ]; then
122 boot_mesg "Configuring upstream name server(s): ${forwarders:1}" ${INFO}
123 echo_ok
b8f5eda8 124
e24d6112 125 echo "${forwarders}" > /var/ipfire/red/dns
b29c97b1
AF
126 unbound-control -q forward ${forwarders}
127 return 0
128 fi
b8f5eda8 129 fi
b29c97b1
AF
130
131 # If forwarders cannot be used we run in recursor mode
e24d6112 132 echo "local recursor" > /var/ipfire/red/dns
b29c97b1 133 unbound-control -q forward off
b8f5eda8
MT
134}
135
f75c279b
AF
136own_hostname() {
137 local hostname=$(hostname -f)
0d7ca700 138 # 1.1.1.1 is reserved for unused green, skip this
f75c279b
AF
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}"
141 fi
142
143 local address
144 for address in ${GREEN_ADDRESS} ${BLUE_ADDRESS} ${ORANGE_ADDRESS}; do
145 [ -n "${address}" ] || continue
146 [ "${address}" = "1.1.1.1" ] && continue
147
148 address=$(ip_address_revptr ${address})
149 unbound-control -q local_data "${address} ${LOCAL_TTL} IN PTR ${hostname}"
150 done
151}
152
36792be6
MT
153update_hosts() {
154 local enabled address hostname domainname
155
156 while IFS="," read -r enabled address hostname domainname; do
157 [ "${enabled}" = "on" ] || continue
158
159 # Build FQDN
160 local fqdn="${hostname}.${domainname}"
161
162 unbound-control -q local_data "${fqdn} ${LOCAL_TTL} IN A ${address}"
f75c279b 163
868d2a1f
MT
164 # Skip reverse resolution if the address equals the GREEN address
165 [ "${address}" = "${GREEN_ADDRESS}" ] && continue
166
f75c279b
AF
167 # Add RDNS
168 address=$(ip_address_revptr ${address})
169 unbound-control -q local_data "${address} ${LOCAL_TTL} IN PTR ${fqdn}"
36792be6
MT
170 done < /var/ipfire/main/hosts
171}
172
b8f5eda8
MT
173write_forward_conf() {
174 (
175 config_header
176
7ebc0a16 177 local insecure_zones="${INSECURE_ZONES}"
a6dcc5bb 178
b8f5eda8
MT
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
183
a6dcc5bb
MT
184 # Zones that end with .local are commonly used for internal
185 # zones and therefore not signed
186 case "${zone}" in
187 *.local)
188 insecure_zones="${insecure_zones} ${zone}"
189 ;;
190 esac
191
b8f5eda8
MT
192 echo "forward-zone:"
193 echo " name: ${zone}"
194 echo " forward-addr: ${server}"
195 echo
196 done < /var/ipfire/dnsforward/config
a6dcc5bb
MT
197
198 if [ -n "${insecure_zones}" ]; then
199 echo "server:"
200
201 for zone in ${insecure_zones}; do
202 echo " domain-insecure: ${zone}"
203 done
204 fi
b8f5eda8
MT
205 ) > /etc/unbound/forward.conf
206}
207
b658a451
MT
208write_tuning_conf() {
209 # https://www.unbound.net/documentation/howto_optimise.html
210
211 # Determine number of online processors
212 local processors=$(getconf _NPROCESSORS_ONLN)
213
214 # Determine number of slabs
215 local slabs=1
216 while [ ${slabs} -lt ${processors} ]; do
217 slabs=$(( ${slabs} * 2 ))
218 done
219
220 # Determine amount of system memory
221 local mem=$(get_memory_amount)
222
223 # In the worst case scenario, unbound can use double the
224 # amount of memory allocated to a cache due to malloc overhead
225
226 # Large systems with more than 2GB of RAM
227 if [ ${mem} -ge 2048 ]; then
228 mem=128
229
230 # Small systems with less than 256MB of RAM
231 elif [ ${mem} -le 256 ]; then
232 mem=8
233
234 # Everything else
235 else
236 mem=32
237 fi
238
239 (
240 config_header
241
242 # We run one thread per processor
243 echo "num-threads: ${processors}"
244
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}"
250
251 # Slice up the cache
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
256}
257
258get_memory_amount() {
259 local key val unit
260
261 while read -r key val unit; do
262 case "${key}" in
263 MemTotal:*)
264 # Convert to MB
265 echo "$(( ${val} / 1024 ))"
266 break
267 ;;
268 esac
269 done < /proc/meminfo
270}
b8f5eda8 271
b29c97b1
AF
272test_name_server() {
273 local ns=${1}
8f3034d0 274 local args
b29c97b1
AF
275
276 # Return codes:
277 # 0 DNSSEC validating
278 # 1 Error: unreachable, etc.
279 # 2 DNSSEC aware
280 # 3 NOT DNSSEC-aware
281
282 # Exit when the server is not reachable
283 ns_is_online ${ns} || return 1
284
8f3034d0
MT
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}"
289 fi
290
b29c97b1
AF
291 local errors
292 for rr in DNSKEY DS RRSIG; do
8f3034d0 293 if ! ns_forwards_${rr} ${ns} ${args}; then
b29c97b1
AF
294 errors="${errors} ${rr}"
295 fi
296 done
297
298 if [ -n "${errors}" ]; then
299 echo >&2 "Unable to retrieve the following resource records from ${ns}: ${errors:1}"
300 return 3
301 fi
302
8f3034d0 303 if ns_is_validating ${ns} ${args}; then
2aa15dee
MT
304 # Return 0 if validating
305 return 0
306 else
307 # Is DNSSEC-aware
308 return 2
309 fi
b29c97b1
AF
310}
311
312# Sends an A query to the nameserver w/o DNSSEC
313ns_is_online() {
314 local ns=${1}
8f3034d0 315 shift
b29c97b1 316
8f3034d0 317 dig @${ns} +nodnssec A ${TEST_DOMAIN} $@ >/dev/null
b29c97b1
AF
318}
319
320# Resolving ${TEST_DOMAIN_FAIL} will fail if the nameserver is validating
321ns_is_validating() {
322 local ns=${1}
8f3034d0 323 shift
b29c97b1 324
8f3034d0 325 dig @${ns} A ${TEST_DOMAIN_FAIL} $@ | grep -q SERVFAIL
b29c97b1
AF
326}
327
328# Checks if we can retrieve the DNSKEY for this domain.
329# dig will print the SOA if nothing was found
330ns_forwards_DNSKEY() {
331 local ns=${1}
8f3034d0 332 shift
b29c97b1 333
8f3034d0 334 dig @${ns} DNSKEY ${TEST_DOMAIN} $@ | grep -qv SOA
b29c97b1
AF
335}
336
337ns_forwards_DS() {
338 local ns=${1}
8f3034d0 339 shift
b29c97b1 340
8f3034d0 341 dig @${ns} DS ${TEST_DOMAIN} $@ | grep -qv SOA
b29c97b1
AF
342}
343
344ns_forwards_RRSIG() {
345 local ns=${1}
8f3034d0 346 shift
b29c97b1 347
8f3034d0 348 dig @${ns} +dnssec A ${TEST_DOMAIN} $@ | grep -q RRSIG
b29c97b1
AF
349}
350
351ns_supports_tcp() {
352 local ns=${1}
8f3034d0
MT
353 shift
354
355 dig @${ns} +tcp A ${TEST_DOMAIN} $@ >/dev/null || return 1
356}
357
358ns_determine_edns_buffer_size() {
359 local ns=${1}
360 shift
361
362 local b
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
365 echo "${b}"
366 return 0
367 fi
368 done
b29c97b1 369
8f3034d0 370 return 1
b29c97b1
AF
371}
372
d0e5f71f
ML
373case "$1" in
374 start)
80bc6022
MT
375 # Print a nicer messagen when unbound is already running
376 if pidofproc -s unbound; then
377 statusproc /usr/sbin/unbound
378 exit 0
379 fi
380
b8f5eda8 381 eval $(/usr/local/bin/readhash /var/ipfire/ethernet/settings)
d0e5f71f 382
b8f5eda8
MT
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
386 fi
387
388 # Update configuration files
b658a451 389 write_tuning_conf
b8f5eda8
MT
390 write_forward_conf
391
392 boot_mesg "Starting Unbound DNS Proxy..."
393 loadproc /usr/sbin/unbound || exit $?
394
f75c279b
AF
395 # Make own hostname resolveable
396 own_hostname
397
b8f5eda8
MT
398 # Update any known forwarding name servers
399 update_forwarders
36792be6
MT
400
401 # Update hosts
402 update_hosts
b8f5eda8 403 ;;
d0e5f71f
ML
404
405 stop)
b8f5eda8
MT
406 boot_mesg "Stopping Unbound DNS Proxy..."
407 killproc /usr/sbin/unbound
408 ;;
d0e5f71f
ML
409
410 restart)
b8f5eda8
MT
411 $0 stop
412 sleep 1
413 $0 start
414 ;;
d0e5f71f
ML
415
416 status)
b8f5eda8 417 statusproc /usr/sbin/unbound
b8f5eda8
MT
418 ;;
419
420 update-forwarders)
cd812106
MT
421 # Do not try updating forwarders when unbound is not running
422 if ! pgrep unbound &>/dev/null; then
423 exit 0
424 fi
425
b8f5eda8
MT
426 update_forwarders
427 ;;
d0e5f71f 428
b29c97b1
AF
429 test-name-server)
430 ns=${2}
431
432 test_name_server ${ns}
433 ret=${?}
434
435 case "${ret}" in
436 0)
437 echo "${ns} is validating"
438 ;;
439 2)
440 echo "${ns} is DNSSEC-aware"
441 ;;
442 3)
443 echo "${ns} is NOT DNSSEC-aware"
444 ;;
445 *)
446 echo "Test failed for an unknown reason"
8f3034d0 447 exit ${ret}
b29c97b1
AF
448 ;;
449 esac
450
451 if ns_supports_tcp ${ns}; then
452 echo "${ns} supports TCP fallback"
453 else
454 echo "${ns} does not support TCP fallback"
455 fi
456
8f3034d0
MT
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}"
460 fi
461
b29c97b1
AF
462 exit ${ret}
463 ;;
464
d0e5f71f 465 *)
b29c97b1 466 echo "Usage: $0 {start|stop|restart|status|update-forwarders|test-name-server}"
b8f5eda8
MT
467 exit 1
468 ;;
d0e5f71f
ML
469esac
470
471# End $rc_base/init.d/unbound