]> git.ipfire.org Git - ipfire-2.x.git/blame - src/initscripts/init.d/unbound
Merge branch 'next' of git.ipfire.org:/pub/git/ipfire-2.x into next
[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
b8f5eda8 15USE_FORWARDERS=1
d0e5f71f 16
36792be6
MT
17# Cache any local zones for 60 seconds
18LOCAL_TTL=60
19
b8f5eda8
MT
20# Load optional configuration
21[ -e "/etc/sysconfig/unbound" ] && . /etc/sysconfig/unbound
d0e5f71f
ML
22
23function cidr() {
24 local cidr nbits IFS;
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))")
28 nbits=0
29 IFS=.
30 for dec in $2 ; do
31 case $dec in
32 255) let nbits+=8;;
33 254) let nbits+=7;;
34 252) let nbits+=6;;
35 248) let nbits+=5;;
36 240) let nbits+=4;;
37 224) let nbits+=3;;
38 192) let nbits+=2;;
39 128) let nbits+=1;;
40 0);;
41 *) echo "Error: $dec is not recognised"; exit 1
42 esac
43 done
44 echo "${cidr}/${nbits}"
45}
46
b8f5eda8
MT
47read_name_servers() {
48 local i
49 for i in 1 2; do
50 echo "$(</var/ipfire/red/dns${i})"
51 done | xargs echo
52}
53
54config_header() {
55 echo "# This file is automatically generated and any changes"
56 echo "# will be overwritten. DO NOT EDIT!"
57 echo
58}
59
60update_forwarders() {
b29c97b1
AF
61 if [ "${USE_FORWARDERS}" = "1" -a -e "/var/ipfire/red/active" ]; then
62 local forwarders
63 local broken_forwarders
64
65 local ns
66 for ns in $(read_name_servers); do
67 test_name_server ${ns} &>/dev/null
68 case "$?" in
69 # Only use DNSSEC-validating or DNSSEC-aware name servers
70 0|2)
71 forwarders="${forwarders} ${ns}"
72 ;;
73 *)
74 broken_forwarders="${broken_forwarders} ${ns}"
75 ;;
76 esac
77 done
78
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}
82 echo_warning
83 fi
b8f5eda8 84
b29c97b1
AF
85 if [ -n "${broken_forwarders}" -a -z "${forwarders}" ]; then
86 boot_mesg "Falling back to recursor mode" ${WARNING}
87 echo_warning
b8f5eda8 88
b29c97b1
AF
89 elif [ -n "${forwarders}" ]; then
90 boot_mesg "Configuring upstream name server(s): ${forwarders:1}" ${INFO}
91 echo_ok
b8f5eda8 92
e24d6112 93 echo "${forwarders}" > /var/ipfire/red/dns
b29c97b1
AF
94 unbound-control -q forward ${forwarders}
95 return 0
96 fi
b8f5eda8 97 fi
b29c97b1
AF
98
99 # If forwarders cannot be used we run in recursor mode
e24d6112 100 echo "local recursor" > /var/ipfire/red/dns
b29c97b1 101 unbound-control -q forward off
b8f5eda8
MT
102}
103
36792be6
MT
104update_hosts() {
105 local enabled address hostname domainname
106
107 while IFS="," read -r enabled address hostname domainname; do
108 [ "${enabled}" = "on" ] || continue
109
110 # Build FQDN
111 local fqdn="${hostname}.${domainname}"
112
113 unbound-control -q local_data "${fqdn} ${LOCAL_TTL} IN A ${address}"
114 done < /var/ipfire/main/hosts
115}
116
b8f5eda8
MT
117write_interfaces_conf() {
118 (
119 config_header
120
121 if [ -n "${GREEN_ADDRESS}" ]; then
122 echo "# GREEN"
123 echo "interface: ${GREEN_ADDRESS}"
124 echo "access-control: $(cidr ${GREEN_NETADDRESS} ${GREEN_NETMASK}) allow"
125 fi
126
127 if [ -n "${BLUE_ADDRESS}" ]; then
128 echo "# BLUE"
129 echo "interface: ${BLUE_ADDRESS}"
130 echo "access-control: $(cidr ${BLUE_NETADDRESS} ${BLUE_NETMASK}) allow"
131 fi
132 ) > /etc/unbound/interfaces.conf
133}
134
135write_forward_conf() {
136 (
137 config_header
138
139 local enabled zone server remark
140 while IFS="," read -r enabled zone server remark; do
141 # Line must be enabled.
142 [ "${enabled}" = "on" ] || continue
143
144 echo "forward-zone:"
145 echo " name: ${zone}"
146 echo " forward-addr: ${server}"
147 echo
148 done < /var/ipfire/dnsforward/config
149 ) > /etc/unbound/forward.conf
150}
151
b658a451
MT
152write_tuning_conf() {
153 # https://www.unbound.net/documentation/howto_optimise.html
154
155 # Determine number of online processors
156 local processors=$(getconf _NPROCESSORS_ONLN)
157
158 # Determine number of slabs
159 local slabs=1
160 while [ ${slabs} -lt ${processors} ]; do
161 slabs=$(( ${slabs} * 2 ))
162 done
163
164 # Determine amount of system memory
165 local mem=$(get_memory_amount)
166
167 # In the worst case scenario, unbound can use double the
168 # amount of memory allocated to a cache due to malloc overhead
169
170 # Large systems with more than 2GB of RAM
171 if [ ${mem} -ge 2048 ]; then
172 mem=128
173
174 # Small systems with less than 256MB of RAM
175 elif [ ${mem} -le 256 ]; then
176 mem=8
177
178 # Everything else
179 else
180 mem=32
181 fi
182
183 (
184 config_header
185
186 # We run one thread per processor
187 echo "num-threads: ${processors}"
188
189 # Adjust number of slabs
190 echo "infra-cache-slabs: ${slabs}"
191 echo "key-cache-slabs: ${slabs}"
192 echo "msg-cache-slabs: ${slabs}"
193 echo "rrset-cache-slabs: ${slabs}"
194
195 # Slice up the cache
196 echo "rrset-cache-size: $(( ${mem} / 2 ))m"
197 echo "msg-cache-size: $(( ${mem} / 4 ))m"
198 echo "key-cache-size: $(( ${mem} / 4 ))m"
199 ) > /etc/unbound/tuning.conf
200}
201
202get_memory_amount() {
203 local key val unit
204
205 while read -r key val unit; do
206 case "${key}" in
207 MemTotal:*)
208 # Convert to MB
209 echo "$(( ${val} / 1024 ))"
210 break
211 ;;
212 esac
213 done < /proc/meminfo
214}
b8f5eda8 215
b29c97b1
AF
216test_name_server() {
217 local ns=${1}
218
219 # Return codes:
220 # 0 DNSSEC validating
221 # 1 Error: unreachable, etc.
222 # 2 DNSSEC aware
223 # 3 NOT DNSSEC-aware
224
225 # Exit when the server is not reachable
226 ns_is_online ${ns} || return 1
227
228 # Return 0 if validating
229 ns_is_validating ${ns} && return 0
230
231 local errors
232 for rr in DNSKEY DS RRSIG; do
233 if ! ns_forwards_${rr} ${ns}; then
234 errors="${errors} ${rr}"
235 fi
236 done
237
238 if [ -n "${errors}" ]; then
239 echo >&2 "Unable to retrieve the following resource records from ${ns}: ${errors:1}"
240 return 3
241 fi
242
243 # Is DNSSEC-aware
244 return 2
245}
246
247# Sends an A query to the nameserver w/o DNSSEC
248ns_is_online() {
249 local ns=${1}
250
251 dig @${ns} +nodnssec A ${TEST_DOMAIN} >/dev/null
252}
253
254# Resolving ${TEST_DOMAIN_FAIL} will fail if the nameserver is validating
255ns_is_validating() {
256 local ns=${1}
257
258 dig @${ns} A ${TEST_DOMAIN_FAIL} | grep -q SERVFAIL
259}
260
261# Checks if we can retrieve the DNSKEY for this domain.
262# dig will print the SOA if nothing was found
263ns_forwards_DNSKEY() {
264 local ns=${1}
265
266 dig @${ns} DNSKEY ${TEST_DOMAIN} | grep -qv SOA
267}
268
269ns_forwards_DS() {
270 local ns=${1}
271
272 dig @${ns} DS ${TEST_DOMAIN} | grep -qv SOA
273}
274
275ns_forwards_RRSIG() {
276 local ns=${1}
277
278 dig @${ns} +dnssec A ${TEST_DOMAIN} | grep -q RRSIG
279}
280
281ns_supports_tcp() {
282 local ns=${1}
283
284 dig @${ns} +tcp A ${TEST_DOMAIN} >/dev/null || return 1
285}
286
d0e5f71f
ML
287case "$1" in
288 start)
80bc6022
MT
289 # Print a nicer messagen when unbound is already running
290 if pidofproc -s unbound; then
291 statusproc /usr/sbin/unbound
292 exit 0
293 fi
294
b8f5eda8 295 eval $(/usr/local/bin/readhash /var/ipfire/ethernet/settings)
d0e5f71f 296
b8f5eda8
MT
297 # Create control keys at first run
298 if [ ! -r "/etc/unbound/unbound_control.key" ]; then
299 unbound-control-setup -d /etc/unbound &>/dev/null
300 fi
301
302 # Update configuration files
b658a451 303 write_tuning_conf
b8f5eda8
MT
304 write_interfaces_conf
305 write_forward_conf
306
307 boot_mesg "Starting Unbound DNS Proxy..."
308 loadproc /usr/sbin/unbound || exit $?
309
310 # Update any known forwarding name servers
311 update_forwarders
36792be6
MT
312
313 # Update hosts
314 update_hosts
b8f5eda8 315 ;;
d0e5f71f
ML
316
317 stop)
b8f5eda8
MT
318 boot_mesg "Stopping Unbound DNS Proxy..."
319 killproc /usr/sbin/unbound
320 ;;
d0e5f71f
ML
321
322 restart)
b8f5eda8
MT
323 $0 stop
324 sleep 1
325 $0 start
326 ;;
d0e5f71f
ML
327
328 status)
b8f5eda8 329 statusproc /usr/sbin/unbound
b8f5eda8
MT
330 ;;
331
332 update-forwarders)
333 update_forwarders
334 ;;
d0e5f71f 335
b29c97b1
AF
336 test-name-server)
337 ns=${2}
338
339 test_name_server ${ns}
340 ret=${?}
341
342 case "${ret}" in
343 0)
344 echo "${ns} is validating"
345 ;;
346 2)
347 echo "${ns} is DNSSEC-aware"
348 ;;
349 3)
350 echo "${ns} is NOT DNSSEC-aware"
351 ;;
352 *)
353 echo "Test failed for an unknown reason"
354 ;;
355 esac
356
357 if ns_supports_tcp ${ns}; then
358 echo "${ns} supports TCP fallback"
359 else
360 echo "${ns} does not support TCP fallback"
361 fi
362
363 exit ${ret}
364 ;;
365
d0e5f71f 366 *)
b29c97b1 367 echo "Usage: $0 {start|stop|restart|status|update-forwarders|test-name-server}"
b8f5eda8
MT
368 exit 1
369 ;;
d0e5f71f
ML
370esac
371
372# End $rc_base/init.d/unbound