]>
Commit | Line | Data |
---|---|---|
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 |
10 | TEST_DOMAIN="ipfire.org" |
11 | ||
12 | # This domain will never validate | |
13 | TEST_DOMAIN_FAIL="dnssec-failed.org" | |
14 | ||
7ebc0a16 | 15 | INSECURE_ZONES= |
b8f5eda8 | 16 | USE_FORWARDERS=1 |
d0e5f71f | 17 | |
36792be6 MT |
18 | # Cache any local zones for 60 seconds |
19 | LOCAL_TTL=60 | |
20 | ||
b2f96a94 MT |
21 | # EDNS buffer size |
22 | EDNS_DEFAULT_BUFFER_SIZE=4096 | |
23 | ||
b8f5eda8 MT |
24 | # Load optional configuration |
25 | [ -e "/etc/sysconfig/unbound" ] && . /etc/sysconfig/unbound | |
d0e5f71f ML |
26 | |
27 | function 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 |
51 | ip_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 |
60 | read_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 | ||
67 | config_header() { | |
68 | echo "# This file is automatically generated and any changes" | |
69 | echo "# will be overwritten. DO NOT EDIT!" | |
70 | echo | |
71 | } | |
72 | ||
73 | update_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 | |
e432689a | 117 | if [ -n "${forwarders}" ]; then |
b29c97b1 AF |
118 | boot_mesg "Configuring upstream name server(s): ${forwarders:1}" ${INFO} |
119 | echo_ok | |
b8f5eda8 | 120 | |
e432689a MT |
121 | # Make sure DNSSEC is activated |
122 | enable_dnssec | |
123 | ||
e24d6112 | 124 | echo "${forwarders}" > /var/ipfire/red/dns |
b29c97b1 AF |
125 | unbound-control -q forward ${forwarders} |
126 | return 0 | |
e432689a MT |
127 | |
128 | # In case we have found no working forwarders | |
129 | else | |
130 | # Test if the recursor mode is available | |
131 | if can_resolve_root +bufsize=${new_edns_buffer_size}; then | |
132 | # Make sure DNSSEC is activated | |
133 | enable_dnssec | |
134 | ||
135 | boot_mesg "Falling back to recursor mode" ${WARNING} | |
136 | echo_warning | |
137 | ||
138 | # If not, we set DNSSEC in permissive mode and allow using all recursors | |
139 | elif [ -n "${broken_forwarders}" ]; then | |
140 | disable_dnssec | |
141 | ||
142 | boot_mesg "DNSSEC has been set to permissive mode" ${FAILURE} | |
143 | echo_failure | |
144 | ||
145 | echo "${broken_forwarders}" > /var/ipfire/red/dns | |
146 | unbound-control -q forward ${broken_forwarders} | |
147 | return 0 | |
148 | fi | |
b29c97b1 | 149 | fi |
b8f5eda8 | 150 | fi |
b29c97b1 AF |
151 | |
152 | # If forwarders cannot be used we run in recursor mode | |
e24d6112 | 153 | echo "local recursor" > /var/ipfire/red/dns |
b29c97b1 | 154 | unbound-control -q forward off |
b8f5eda8 MT |
155 | } |
156 | ||
f75c279b AF |
157 | own_hostname() { |
158 | local hostname=$(hostname -f) | |
0d7ca700 | 159 | # 1.1.1.1 is reserved for unused green, skip this |
f75c279b AF |
160 | if [ -n "${GREEN_ADDRESS}" -a "${GREEN_ADDRESS}" != "1.1.1.1" ]; then |
161 | unbound-control -q local_data "${hostname} ${LOCAL_TTL} IN A ${GREEN_ADDRESS}" | |
162 | fi | |
163 | ||
164 | local address | |
165 | for address in ${GREEN_ADDRESS} ${BLUE_ADDRESS} ${ORANGE_ADDRESS}; do | |
166 | [ -n "${address}" ] || continue | |
167 | [ "${address}" = "1.1.1.1" ] && continue | |
168 | ||
169 | address=$(ip_address_revptr ${address}) | |
170 | unbound-control -q local_data "${address} ${LOCAL_TTL} IN PTR ${hostname}" | |
171 | done | |
172 | } | |
173 | ||
36792be6 MT |
174 | update_hosts() { |
175 | local enabled address hostname domainname | |
176 | ||
177 | while IFS="," read -r enabled address hostname domainname; do | |
178 | [ "${enabled}" = "on" ] || continue | |
179 | ||
180 | # Build FQDN | |
181 | local fqdn="${hostname}.${domainname}" | |
182 | ||
183 | unbound-control -q local_data "${fqdn} ${LOCAL_TTL} IN A ${address}" | |
f75c279b | 184 | |
868d2a1f MT |
185 | # Skip reverse resolution if the address equals the GREEN address |
186 | [ "${address}" = "${GREEN_ADDRESS}" ] && continue | |
187 | ||
f75c279b AF |
188 | # Add RDNS |
189 | address=$(ip_address_revptr ${address}) | |
190 | unbound-control -q local_data "${address} ${LOCAL_TTL} IN PTR ${fqdn}" | |
36792be6 MT |
191 | done < /var/ipfire/main/hosts |
192 | } | |
193 | ||
b8f5eda8 MT |
194 | write_forward_conf() { |
195 | ( | |
196 | config_header | |
197 | ||
7ebc0a16 | 198 | local insecure_zones="${INSECURE_ZONES}" |
a6dcc5bb | 199 | |
b8f5eda8 MT |
200 | local enabled zone server remark |
201 | while IFS="," read -r enabled zone server remark; do | |
202 | # Line must be enabled. | |
203 | [ "${enabled}" = "on" ] || continue | |
204 | ||
a6dcc5bb MT |
205 | # Zones that end with .local are commonly used for internal |
206 | # zones and therefore not signed | |
207 | case "${zone}" in | |
208 | *.local) | |
209 | insecure_zones="${insecure_zones} ${zone}" | |
210 | ;; | |
211 | esac | |
212 | ||
b8f5eda8 MT |
213 | echo "forward-zone:" |
214 | echo " name: ${zone}" | |
215 | echo " forward-addr: ${server}" | |
216 | echo | |
217 | done < /var/ipfire/dnsforward/config | |
a6dcc5bb MT |
218 | |
219 | if [ -n "${insecure_zones}" ]; then | |
220 | echo "server:" | |
221 | ||
222 | for zone in ${insecure_zones}; do | |
223 | echo " domain-insecure: ${zone}" | |
224 | done | |
225 | fi | |
b8f5eda8 MT |
226 | ) > /etc/unbound/forward.conf |
227 | } | |
228 | ||
b658a451 MT |
229 | write_tuning_conf() { |
230 | # https://www.unbound.net/documentation/howto_optimise.html | |
231 | ||
232 | # Determine number of online processors | |
233 | local processors=$(getconf _NPROCESSORS_ONLN) | |
234 | ||
235 | # Determine number of slabs | |
236 | local slabs=1 | |
237 | while [ ${slabs} -lt ${processors} ]; do | |
238 | slabs=$(( ${slabs} * 2 )) | |
239 | done | |
240 | ||
241 | # Determine amount of system memory | |
242 | local mem=$(get_memory_amount) | |
243 | ||
244 | # In the worst case scenario, unbound can use double the | |
245 | # amount of memory allocated to a cache due to malloc overhead | |
246 | ||
4a0d69ca MT |
247 | # Even larger systems with more than 8GB of RAM |
248 | if [ ${mem} -ge 8192 ]; then | |
249 | mem=1024 | |
250 | ||
251 | # Extra large systems with more than 4GB of RAM | |
252 | elif [ ${mem} -ge 4096 ]; then | |
253 | mem=512 | |
254 | ||
b658a451 | 255 | # Large systems with more than 2GB of RAM |
4a0d69ca | 256 | elif [ ${mem} -ge 2048 ]; then |
128db1a3 | 257 | mem=256 |
b658a451 | 258 | |
4a0d69ca MT |
259 | # Medium systems with more than 1GB of RAM |
260 | elif [ ${mem} -ge 1024 ]; then | |
261 | mem=128 | |
262 | ||
b658a451 MT |
263 | # Small systems with less than 256MB of RAM |
264 | elif [ ${mem} -le 256 ]; then | |
128db1a3 | 265 | mem=16 |
b658a451 MT |
266 | |
267 | # Everything else | |
268 | else | |
128db1a3 | 269 | mem=64 |
b658a451 MT |
270 | fi |
271 | ||
272 | ( | |
273 | config_header | |
274 | ||
275 | # We run one thread per processor | |
276 | echo "num-threads: ${processors}" | |
5012e53c | 277 | echo "so-reuseport: yes" |
b658a451 MT |
278 | |
279 | # Adjust number of slabs | |
280 | echo "infra-cache-slabs: ${slabs}" | |
281 | echo "key-cache-slabs: ${slabs}" | |
282 | echo "msg-cache-slabs: ${slabs}" | |
283 | echo "rrset-cache-slabs: ${slabs}" | |
284 | ||
285 | # Slice up the cache | |
286 | echo "rrset-cache-size: $(( ${mem} / 2 ))m" | |
287 | echo "msg-cache-size: $(( ${mem} / 4 ))m" | |
288 | echo "key-cache-size: $(( ${mem} / 4 ))m" | |
0a7dca2c MT |
289 | |
290 | # Increase parallel queries | |
291 | echo "outgoing-range: 8192" | |
292 | echo "num-queries-per-thread: 4096" | |
c20b2009 MT |
293 | |
294 | # Use larger send/receive buffers | |
295 | echo "so-sndbuf: 4m" | |
296 | echo "so-rcvbuf: 4m" | |
b658a451 MT |
297 | ) > /etc/unbound/tuning.conf |
298 | } | |
299 | ||
300 | get_memory_amount() { | |
301 | local key val unit | |
302 | ||
303 | while read -r key val unit; do | |
304 | case "${key}" in | |
305 | MemTotal:*) | |
306 | # Convert to MB | |
307 | echo "$(( ${val} / 1024 ))" | |
308 | break | |
309 | ;; | |
310 | esac | |
311 | done < /proc/meminfo | |
312 | } | |
b8f5eda8 | 313 | |
b29c97b1 AF |
314 | test_name_server() { |
315 | local ns=${1} | |
8f3034d0 | 316 | local args |
b29c97b1 AF |
317 | |
318 | # Return codes: | |
319 | # 0 DNSSEC validating | |
320 | # 1 Error: unreachable, etc. | |
321 | # 2 DNSSEC aware | |
322 | # 3 NOT DNSSEC-aware | |
323 | ||
324 | # Exit when the server is not reachable | |
325 | ns_is_online ${ns} || return 1 | |
326 | ||
8f3034d0 MT |
327 | # Determine the maximum edns buffer size that works |
328 | local edns_buffer_size=$(ns_determine_edns_buffer_size ${ns}) | |
329 | if [ -n "${edns_buffer_size}" ]; then | |
330 | args="${args} +bufsize=${edns_buffer_size}" | |
331 | fi | |
332 | ||
b29c97b1 AF |
333 | local errors |
334 | for rr in DNSKEY DS RRSIG; do | |
8f3034d0 | 335 | if ! ns_forwards_${rr} ${ns} ${args}; then |
b29c97b1 AF |
336 | errors="${errors} ${rr}" |
337 | fi | |
338 | done | |
339 | ||
340 | if [ -n "${errors}" ]; then | |
341 | echo >&2 "Unable to retrieve the following resource records from ${ns}: ${errors:1}" | |
342 | return 3 | |
343 | fi | |
344 | ||
8f3034d0 | 345 | if ns_is_validating ${ns} ${args}; then |
2aa15dee MT |
346 | # Return 0 if validating |
347 | return 0 | |
348 | else | |
349 | # Is DNSSEC-aware | |
350 | return 2 | |
351 | fi | |
b29c97b1 AF |
352 | } |
353 | ||
354 | # Sends an A query to the nameserver w/o DNSSEC | |
355 | ns_is_online() { | |
356 | local ns=${1} | |
8f3034d0 | 357 | shift |
b29c97b1 | 358 | |
8f3034d0 | 359 | dig @${ns} +nodnssec A ${TEST_DOMAIN} $@ >/dev/null |
b29c97b1 AF |
360 | } |
361 | ||
362 | # Resolving ${TEST_DOMAIN_FAIL} will fail if the nameserver is validating | |
363 | ns_is_validating() { | |
364 | local ns=${1} | |
8f3034d0 | 365 | shift |
b29c97b1 | 366 | |
8f3034d0 | 367 | dig @${ns} A ${TEST_DOMAIN_FAIL} $@ | grep -q SERVFAIL |
b29c97b1 AF |
368 | } |
369 | ||
370 | # Checks if we can retrieve the DNSKEY for this domain. | |
371 | # dig will print the SOA if nothing was found | |
372 | ns_forwards_DNSKEY() { | |
373 | local ns=${1} | |
8f3034d0 | 374 | shift |
b29c97b1 | 375 | |
8f3034d0 | 376 | dig @${ns} DNSKEY ${TEST_DOMAIN} $@ | grep -qv SOA |
b29c97b1 AF |
377 | } |
378 | ||
379 | ns_forwards_DS() { | |
380 | local ns=${1} | |
8f3034d0 | 381 | shift |
b29c97b1 | 382 | |
8f3034d0 | 383 | dig @${ns} DS ${TEST_DOMAIN} $@ | grep -qv SOA |
b29c97b1 AF |
384 | } |
385 | ||
386 | ns_forwards_RRSIG() { | |
387 | local ns=${1} | |
8f3034d0 | 388 | shift |
b29c97b1 | 389 | |
8f3034d0 | 390 | dig @${ns} +dnssec A ${TEST_DOMAIN} $@ | grep -q RRSIG |
b29c97b1 AF |
391 | } |
392 | ||
393 | ns_supports_tcp() { | |
394 | local ns=${1} | |
8f3034d0 MT |
395 | shift |
396 | ||
397 | dig @${ns} +tcp A ${TEST_DOMAIN} $@ >/dev/null || return 1 | |
398 | } | |
399 | ||
400 | ns_determine_edns_buffer_size() { | |
401 | local ns=${1} | |
402 | shift | |
403 | ||
404 | local b | |
405 | for b in 4096 2048 1500 1480 1464 1400 1280 512; do | |
406 | if dig @${ns} +dnssec +bufsize=${b} A ${TEST_DOMAIN} $@ >/dev/null; then | |
407 | echo "${b}" | |
408 | return 0 | |
409 | fi | |
410 | done | |
b29c97b1 | 411 | |
8f3034d0 | 412 | return 1 |
b29c97b1 AF |
413 | } |
414 | ||
e432689a MT |
415 | get_root_nameservers() { |
416 | while read -r hostname ttl record address; do | |
417 | # Searching for A records | |
418 | [ "${record}" = "A" ] || continue | |
419 | ||
420 | echo "${address}" | |
421 | done < /etc/unbound/root.hints | |
422 | } | |
423 | ||
424 | can_resolve_root() { | |
425 | local ns | |
426 | for ns in $(get_root_nameservers); do | |
427 | if dig @${ns} +dnssec SOA . $@ >/dev/null; then | |
428 | return 0 | |
429 | fi | |
430 | done | |
431 | ||
432 | # none of the servers was reachable | |
433 | return 1 | |
434 | } | |
435 | ||
436 | enable_dnssec() { | |
437 | local status=$(unbound-control get_option val-permissive-mode) | |
438 | ||
439 | # Don't do anything if DNSSEC is already activated | |
440 | [ "${status}" = "no" ] && return 0 | |
441 | ||
183b23b5 MT |
442 | # Log DNSSEC status |
443 | echo "on" > /var/ipfire/red/dnssec-status | |
444 | ||
e432689a MT |
445 | # Activate DNSSEC and flush cache with any stale and unvalidated data |
446 | unbound-control -q set_option val-permissive-mode: no | |
447 | unbound-control -q flush_zone . | |
448 | } | |
449 | ||
450 | disable_dnssec() { | |
183b23b5 MT |
451 | # Log DNSSEC status |
452 | echo "off" > /var/ipfire/red/dnssec-status | |
453 | ||
e432689a MT |
454 | unbound-control -q set_option val-permissive-mode: yes |
455 | } | |
456 | ||
d0e5f71f ML |
457 | case "$1" in |
458 | start) | |
80bc6022 MT |
459 | # Print a nicer messagen when unbound is already running |
460 | if pidofproc -s unbound; then | |
461 | statusproc /usr/sbin/unbound | |
462 | exit 0 | |
463 | fi | |
464 | ||
b8f5eda8 | 465 | eval $(/usr/local/bin/readhash /var/ipfire/ethernet/settings) |
d0e5f71f | 466 | |
b8f5eda8 MT |
467 | # Create control keys at first run |
468 | if [ ! -r "/etc/unbound/unbound_control.key" ]; then | |
469 | unbound-control-setup -d /etc/unbound &>/dev/null | |
470 | fi | |
471 | ||
472 | # Update configuration files | |
b658a451 | 473 | write_tuning_conf |
b8f5eda8 MT |
474 | write_forward_conf |
475 | ||
476 | boot_mesg "Starting Unbound DNS Proxy..." | |
477 | loadproc /usr/sbin/unbound || exit $? | |
478 | ||
f75c279b AF |
479 | # Make own hostname resolveable |
480 | own_hostname | |
481 | ||
b8f5eda8 MT |
482 | # Update any known forwarding name servers |
483 | update_forwarders | |
36792be6 MT |
484 | |
485 | # Update hosts | |
486 | update_hosts | |
b8f5eda8 | 487 | ;; |
d0e5f71f ML |
488 | |
489 | stop) | |
b8f5eda8 MT |
490 | boot_mesg "Stopping Unbound DNS Proxy..." |
491 | killproc /usr/sbin/unbound | |
492 | ;; | |
d0e5f71f ML |
493 | |
494 | restart) | |
b8f5eda8 MT |
495 | $0 stop |
496 | sleep 1 | |
497 | $0 start | |
498 | ;; | |
d0e5f71f ML |
499 | |
500 | status) | |
b8f5eda8 | 501 | statusproc /usr/sbin/unbound |
b8f5eda8 MT |
502 | ;; |
503 | ||
504 | update-forwarders) | |
cd812106 MT |
505 | # Do not try updating forwarders when unbound is not running |
506 | if ! pgrep unbound &>/dev/null; then | |
507 | exit 0 | |
508 | fi | |
509 | ||
b8f5eda8 MT |
510 | update_forwarders |
511 | ;; | |
d0e5f71f | 512 | |
b29c97b1 AF |
513 | test-name-server) |
514 | ns=${2} | |
515 | ||
516 | test_name_server ${ns} | |
517 | ret=${?} | |
518 | ||
519 | case "${ret}" in | |
520 | 0) | |
521 | echo "${ns} is validating" | |
522 | ;; | |
523 | 2) | |
524 | echo "${ns} is DNSSEC-aware" | |
525 | ;; | |
526 | 3) | |
527 | echo "${ns} is NOT DNSSEC-aware" | |
528 | ;; | |
529 | *) | |
530 | echo "Test failed for an unknown reason" | |
8f3034d0 | 531 | exit ${ret} |
b29c97b1 AF |
532 | ;; |
533 | esac | |
534 | ||
535 | if ns_supports_tcp ${ns}; then | |
536 | echo "${ns} supports TCP fallback" | |
537 | else | |
538 | echo "${ns} does not support TCP fallback" | |
539 | fi | |
540 | ||
8f3034d0 MT |
541 | edns_buffer_size=$(ns_determine_edns_buffer_size ${ns}) |
542 | if [ -n "${edns_buffer_size}" ]; then | |
543 | echo "EDNS buffer size for ${ns}: ${edns_buffer_size}" | |
544 | fi | |
545 | ||
b29c97b1 AF |
546 | exit ${ret} |
547 | ;; | |
548 | ||
d0e5f71f | 549 | *) |
b29c97b1 | 550 | echo "Usage: $0 {start|stop|restart|status|update-forwarders|test-name-server}" |
b8f5eda8 MT |
551 | exit 1 |
552 | ;; | |
d0e5f71f ML |
553 | esac |
554 | ||
555 | # End $rc_base/init.d/unbound |