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