2 # Begin $rc_base/init.d/unbound
4 # Description : Unbound DNS resolver boot script for IPfire
5 # Author : Marcel Lorenz <marcel.lorenz@ipfire.org>
10 TEST_DOMAIN
="ipfire.org"
12 # This domain will never validate
13 TEST_DOMAIN_FAIL
="dnssec-failed.org"
15 # Cache any local zones for 60 seconds
19 eval $
(/usr
/local
/bin
/readhash
/var
/ipfire
/dns
/settings
)
20 eval $
(/usr
/local
/bin
/readhash
/var
/ipfire
/ethernet
/settings
)
26 IFS
=.
read -r a1 a2 a3 a4
<<< ${addr}
28 echo "${a4}.${a3}.${a2}.${a1}.in-addr.arpa"
32 # Read name servers from ISP
33 if [ "${USE_ISP_NAMESERVERS}" = "on" -a "${PROTO}" != "TLS" ]; then
36 echo "$(</var/run/dns${i})"
40 # Read configured name servers
41 local id address tls_hostname enabled remark
42 while IFS
="," read -r id address tls_hostname enabled remark
; do
43 [ "${enabled}" != "enabled" ] && continue
45 if [ "${PROTO}" = "TLS" ]; then
46 if [ -n "${tls_hostname}" ]; then
47 echo "${address}@853#${tls_hostname}"
52 done < /var
/ipfire
/dns
/servers
56 echo "# This file is automatically generated and any changes"
57 echo "# will be overwritten. DO NOT EDIT!"
65 # Make own hostname resolveable
66 # 1.1.1.1 is reserved for unused green, skip this
67 if [ -n "${GREEN_ADDRESS}" -a "${GREEN_ADDRESS}" != "1.1.1.1" ]; then
68 echo "local-data: \"${HOSTNAME} ${LOCAL_TTL} IN A ${GREEN_ADDRESS}\""
72 for address
in ${GREEN_ADDRESS} ${BLUE_ADDRESS} ${ORANGE_ADDRESS}; do
73 [ -n "${address}" ] ||
continue
74 [ "${address}" = "1.1.1.1" ] && continue
76 address
=$
(ip_address_revptr
${address})
77 echo "local-data: \"${address} ${LOCAL_TTL} IN PTR ${HOSTNAME}\""
81 local enabled address hostname domainname generateptr
82 while IFS
="," read -r enabled address hostname domainname generateptr
; do
83 [ "${enabled}" = "on" ] ||
continue
86 local fqdn
="${hostname}.${domainname}"
87 echo "local-data: \"${fqdn} ${LOCAL_TTL} IN A ${address}\""
89 # Skip reverse resolution if the address equals the GREEN address
90 [ "${address}" = "${GREEN_ADDRESS}" ] && continue
92 # Skip reverse resolution if user requested not to do so
93 [ "${generateptr}" = "off" ] && continue
96 address
=$
(ip_address_revptr
${address})
97 echo "local-data: \"${address} ${LOCAL_TTL} IN PTR ${fqdn}\""
98 done < /var
/ipfire
/main
/hosts
99 ) > /etc
/unbound
/hosts.conf
102 write_forward_conf
() {
106 # Enable strict QNAME minimisation
107 if [ "${QNAME_MIN}" = "strict" ]; then
109 echo " qname-minimisation-strict: yes"
113 # Force using TCP for upstream servers only
114 if [ "${PROTO}" = "TCP" ]; then
115 echo "# Force using TCP for upstream servers only"
117 echo " tcp-upstream: yes"
121 local insecure_zones
=""
123 local enabled zone server servers remark disable_dnssec rest
124 while IFS
="," read -r enabled zone servers remark disable_dnssec rest
; do
125 # Line must be enabled.
126 [ "${enabled}" = "on" ] ||
continue
128 # Zones that end with .local are commonly used for internal
129 # zones and therefore not signed
132 insecure_zones
="${insecure_zones} ${zone}"
135 if [ "${disable_dnssec}" = "on" ]; then
136 insecure_zones
="${insecure_zones} ${zone}"
142 echo " name: ${zone}"
143 for server
in ${servers//|/ }; do
144 if [[ ${server} =~ ^
[0-9]+\.
[0-9]+\.
[0-9]+\.
[0-9]+$
]]; then
145 echo " stub-addr: ${server}"
147 echo " stub-host: ${server}"
152 # Make all reverse lookup zones transparent
156 echo " local-zone: \"${zone}\" transparent"
160 done < /var
/ipfire
/dnsforward
/config
162 if [ -n "${insecure_zones}" ]; then
165 for zone
in ${insecure_zones}; do
166 echo " domain-insecure: ${zone}"
173 # Force using TLS only
174 if [ "${PROTO}" = "TLS" ]; then
175 echo " forward-tls-upstream: yes"
178 # Add upstream name servers
180 for ns
in $
(read_name_servers
); do
181 echo " forward-addr: ${ns}"
183 ) > /etc
/unbound
/forward.conf
186 write_tuning_conf
() {
187 # https://www.unbound.net/documentation/howto_optimise.html
189 # Determine number of online processors
190 local processors
=$
(getconf _NPROCESSORS_ONLN
)
192 # Determine number of slabs
194 while [ ${slabs} -lt ${processors} ]; do
195 slabs
=$
(( ${slabs} * 2 ))
198 # Determine amount of system memory
199 local mem
=$
(get_memory_amount
)
201 # In the worst case scenario, unbound can use double the
202 # amount of memory allocated to a cache due to malloc overhead
204 # Even larger systems with more than 8GB of RAM
205 if [ ${mem} -ge 8192 ]; then
208 # Extra large systems with more than 4GB of RAM
209 elif [ ${mem} -ge 4096 ]; then
212 # Large systems with more than 2GB of RAM
213 elif [ ${mem} -ge 2048 ]; then
216 # Medium systems with more than 1GB of RAM
217 elif [ ${mem} -ge 1024 ]; then
220 # Small systems with less than 256MB of RAM
221 elif [ ${mem} -le 256 ]; then
232 # We run one thread per processor
233 echo "num-threads: ${processors}"
234 echo "so-reuseport: yes"
236 # Adjust number of slabs
237 echo "infra-cache-slabs: ${slabs}"
238 echo "key-cache-slabs: ${slabs}"
239 echo "msg-cache-slabs: ${slabs}"
240 echo "rrset-cache-slabs: ${slabs}"
243 echo "rrset-cache-size: $(( ${mem} / 2 ))m"
244 echo "msg-cache-size: $(( ${mem} / 4 ))m"
245 echo "key-cache-size: $(( ${mem} / 4 ))m"
247 # Increase parallel queries
248 echo "outgoing-range: 8192"
249 echo "num-queries-per-thread: 4096"
251 # Use larger send/receive buffers
254 ) > /etc
/unbound
/tuning.conf
257 get_memory_amount
() {
260 while read -r key val unit
; do
264 echo "$(( ${val} / 1024 ))"
271 fix_time_if_dns_fails
() {
272 # If DNS is working, everything is fine
273 if resolve
"ping.ipfire.org" &>/dev
/null
; then
277 # Try to sync time with a known time server
278 boot_mesg
"DNS not functioning... Trying to sync time with ntp.ipfire.org (81.3.27.46)..."
279 loadproc
/usr
/local
/bin
/settime
81.3.27.46
283 local hostname
="${1}"
286 for answer
in $
(dig +short A
"${hostname}"); do
287 # Filter out non-IP addresses
288 if [[ ! "${answer}" =~ \.$
]]; then
294 update_forwarders
() {
295 # Do nothing when we do not use the ISP name servers
296 [ "${USE_ISP_NAMESERVERS}" != "on" ] && return 0
298 # We cannot update anything when using TLS
299 # Unbound will then try to connect to the servers using UDP on port 853
300 [ "${PROTO}" = "TLS" ] && return 0
302 # Update unbound about the new servers
303 local nameservers
=( $
(read_name_servers
) )
304 if [ -n "${nameservers[*]}" ]; then
305 unbound-control
-q forward
"${nameservers[@]}"
307 unbound-control
-q forward off
311 # Sets up Safe Search for various search engines
312 update_safe_search
() {
509 # Cleanup previous settings
510 unbound-control local_zone_remove
"bing.com" >/dev
/null
511 unbound-control local_zone_remove
"duckduckgo.com" >/dev
/null
512 unbound-control local_zone_remove
"yandex.com" >/dev
/null
513 unbound-control local_zone_remove
"yandex.ru" >/dev
/null
514 unbound-control local_zone_remove
"youtube.com" >/dev
/null
517 for domain
in ${google_tlds[@]}; do
518 unbound-control local_zone_remove
"${domain}"
521 # Nothing to do if safe search is not enabled
522 if [ "${ENABLE_SAFE_SEARCH}" != "on" ]; then
527 unbound-control bing.com transparent
>/dev
/null
528 for address
in $
(resolve
"strict.bing.com"); do
529 unbound-control local_data
"www.bing.com ${LOCAL_TTL} IN A ${address}"
533 unbound-control local_zone duckduckgo.com typetransparent
>/dev
/null
534 for address
in $
(resolve
"safe.duckduckgo.com"); do
535 unbound-control local_data
"duckduckgo.com ${LOCAL_TTL} IN A ${address}"
539 local addresses
="$(resolve "forcesafesearch.google.com
")"
540 for domain
in ${google_tlds[@]}; do
541 unbound-control local_zone
"${domain}" transparent
>/dev
/null
542 for address
in ${addresses}; do
543 unbound-control local_data
: "www.${domain} ${LOCAL_TTL} IN A ${address}"
548 for domain
in yandex.com yandex.ru
; do
549 unbound-control local_zone
"${domain}" typetransparent
>/dev
/null
550 for address
in $
(resolve
"familysearch.${domain}"); do
551 unbound-control local_data
"${domain} ${LOCAL_TTL} IN A ${address}"
556 unbound-control local_zone youtube.com transparent
>/dev
/null
557 for address
in $
(resolve
"restrictmoderate.youtube.com"); do
558 unbound-control local_data
"www.youtube.com ${LOCAL_TTL} IN A ${address}"
566 # Print a nicer messagen when unbound is already running
567 if pidofproc
-s unbound
; then
568 statusproc
/usr
/sbin
/unbound
572 # Update configuration files
577 boot_mesg
"Starting Unbound DNS Proxy..."
578 loadproc
/usr
/sbin
/unbound ||
exit $?
580 # Install Safe Search rules when the system is already online
581 if [ -e "/var/ipfire/red/active" ]; then
587 boot_mesg
"Stopping Unbound DNS Proxy..."
588 killproc
/usr
/sbin
/unbound
597 # Update configuration files
601 # Update Safe Search rules if the system is online.
602 if [ -e "/var/ipfire/red/active" ]; then
606 # Call unbound-control and perform the reload
607 /usr
/sbin
/unbound-control
-q reload
611 statusproc
/usr
/sbin
/unbound
617 # Make sure DNS works at this point
618 fix_time_if_dns_fails
620 # Update Safe Search settings
637 echo "Usage: $0 {start|stop|restart|reload|status|resolve|update-forwarders|remove-forwarders|update-safe-search}"
642 # End $rc_base/init.d/unbound