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