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!"
62 local hostname
=$
(hostname
-f)
63 # 1.1.1.1 is reserved for unused green, skip this
64 if [ -n "${GREEN_ADDRESS}" -a "${GREEN_ADDRESS}" != "1.1.1.1" ]; then
65 unbound-control
-q local_data
"${hostname} ${LOCAL_TTL} IN A ${GREEN_ADDRESS}"
69 for address
in ${GREEN_ADDRESS} ${BLUE_ADDRESS} ${ORANGE_ADDRESS}; do
70 [ -n "${address}" ] ||
continue
71 [ "${address}" = "1.1.1.1" ] && continue
73 address
=$
(ip_address_revptr
${address})
74 unbound-control
-q local_data
"${address} ${LOCAL_TTL} IN PTR ${hostname}"
79 local enabled address hostname domainname generateptr
81 while IFS
="," read -r enabled address hostname domainname generateptr
; do
82 [ "${enabled}" = "on" ] ||
continue
85 local fqdn
="${hostname}.${domainname}"
87 unbound-control
-q 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 unbound-control
-q local_data
"${address} ${LOCAL_TTL} IN PTR ${fqdn}"
98 done < /var
/ipfire
/main
/hosts
101 write_forward_conf
() {
105 # Force using TCP for upstream servers only
106 if [ "${PROTO}" = "TCP" ]; then
107 echo "# Force using TCP for upstream servers only"
109 echo " tcp-upstream: yes"
113 local insecure_zones
=""
115 local enabled zone server servers remark disable_dnssec rest
116 while IFS
="," read -r enabled zone servers remark disable_dnssec rest
; do
117 # Line must be enabled.
118 [ "${enabled}" = "on" ] ||
continue
120 # Zones that end with .local are commonly used for internal
121 # zones and therefore not signed
124 insecure_zones
="${insecure_zones} ${zone}"
127 if [ "${disable_dnssec}" = "on" ]; then
128 insecure_zones
="${insecure_zones} ${zone}"
134 echo " name: ${zone}"
135 for server
in ${servers//|/ }; do
136 if [[ ${server} =~ ^
[0-9]+\.
[0-9]+\.
[0-9]+\.
[0-9]+$
]]; then
137 echo " stub-addr: ${server}"
139 echo " stub-host: ${server}"
144 # Make all reverse lookup zones transparent
148 echo " local-zone: \"${zone}\" transparent"
152 done < /var
/ipfire
/dnsforward
/config
154 if [ -n "${insecure_zones}" ]; then
157 for zone
in ${insecure_zones}; do
158 echo " domain-insecure: ${zone}"
165 # Force using TLS only
166 if [ "${PROTO}" = "TLS" ]; then
167 echo " forward-tls-upstream: yes"
170 # Add upstream name servers
172 for ns
in $
(read_name_servers
); do
173 echo " forward-addr: ${ns}"
175 ) > /etc
/unbound
/forward.conf
178 write_tuning_conf
() {
179 # https://www.unbound.net/documentation/howto_optimise.html
181 # Determine number of online processors
182 local processors
=$
(getconf _NPROCESSORS_ONLN
)
184 # Determine number of slabs
186 while [ ${slabs} -lt ${processors} ]; do
187 slabs
=$
(( ${slabs} * 2 ))
190 # Determine amount of system memory
191 local mem
=$
(get_memory_amount
)
193 # In the worst case scenario, unbound can use double the
194 # amount of memory allocated to a cache due to malloc overhead
196 # Even larger systems with more than 8GB of RAM
197 if [ ${mem} -ge 8192 ]; then
200 # Extra large systems with more than 4GB of RAM
201 elif [ ${mem} -ge 4096 ]; then
204 # Large systems with more than 2GB of RAM
205 elif [ ${mem} -ge 2048 ]; then
208 # Medium systems with more than 1GB of RAM
209 elif [ ${mem} -ge 1024 ]; then
212 # Small systems with less than 256MB of RAM
213 elif [ ${mem} -le 256 ]; then
224 # We run one thread per processor
225 echo "num-threads: ${processors}"
226 echo "so-reuseport: yes"
228 # Adjust number of slabs
229 echo "infra-cache-slabs: ${slabs}"
230 echo "key-cache-slabs: ${slabs}"
231 echo "msg-cache-slabs: ${slabs}"
232 echo "rrset-cache-slabs: ${slabs}"
235 echo "rrset-cache-size: $(( ${mem} / 2 ))m"
236 echo "msg-cache-size: $(( ${mem} / 4 ))m"
237 echo "key-cache-size: $(( ${mem} / 4 ))m"
239 # Increase parallel queries
240 echo "outgoing-range: 8192"
241 echo "num-queries-per-thread: 4096"
243 # Use larger send/receive buffers
246 ) > /etc
/unbound
/tuning.conf
249 get_memory_amount
() {
252 while read -r key val unit
; do
256 echo "$(( ${val} / 1024 ))"
263 fix_time_if_dns_fails
() {
264 # If DNS is working, everything is fine
265 if resolve
"ping.ipfire.org" &>/dev
/null
; then
269 # Try to sync time with a known time server
270 boot_mesg
"DNS not functioning... Trying to sync time with ntp.ipfire.org (81.3.27.46)..."
271 loadproc
/usr
/local
/bin
/settime
81.3.27.46
275 local hostname
="${1}"
278 for answer
in $
(dig +short A
"${hostname}"); do
279 # Filter out non-IP addresses
280 if [[ ! "${answer}" =~ \.$
]]; then
286 update_forwarders
() {
287 # Do nothing when we do not use the ISP name servers
288 [ "${USE_ISP_NAMESERVERS}" != "on" ] && return 0
290 # We cannot update anything when using TLS
291 # Unbound will then try to connect to the servers using UDP on port 853
292 [ "${PROTO}" = "TLS" ] && return 0
294 # Update unbound about the new servers
295 local nameservers
=( $
(read_name_servers
) )
296 if [ -n "${nameservers[*]}" ]; then
297 unbound-control
-q forward
"${nameservers[@]}"
299 unbound-control
-q forward off
303 # Sets up Safe Search for various search engines
304 update_safe_search
() {
501 # Cleanup previous settings
502 unbound-control local_zone_remove
"bing.com" >/dev
/null
503 unbound-control local_zone_remove
"duckduckgo.com" >/dev
/null
504 unbound-control local_zone_remove
"yandex.com" >/dev
/null
505 unbound-control local_zone_remove
"yandex.ru" >/dev
/null
506 unbound-control local_zone_remove
"youtube.com" >/dev
/null
509 for domain
in ${google_tlds[@]}; do
510 unbound-control local_zone_remove
"${domain}"
513 # Nothing to do if safe search is not enabled
514 if [ "${ENABLE_SAFE_SEARCH}" != "on" ]; then
519 unbound-control bing.com transparent
>/dev
/null
520 for address
in $
(resolve
"strict.bing.com"); do
521 unbound-control local_data
"www.bing.com ${LOCAL_TTL} IN A ${address}"
525 unbound-control local_zone duckduckgo.com typetransparent
>/dev
/null
526 for address
in $
(resolve
"safe.duckduckgo.com"); do
527 unbound-control local_data
"duckduckgo.com ${LOCAL_TTL} IN A ${address}"
531 local addresses
="$(resolve "forcesafesearch.google.com
")"
532 for domain
in ${google_tlds[@]}; do
533 unbound-control local_zone
"${domain}" transparent
>/dev
/null
534 for address
in ${addresses}; do
535 unbound-control local_data
: "www.${domain} ${LOCAL_TTL} IN A ${address}"
540 for domain
in yandex.com yandex.ru
; do
541 unbound-control local_zone
"${domain}" typetransparent
>/dev
/null
542 for address
in $
(resolve
"familysearch.${domain}"); do
543 unbound-control local_data
"${domain} ${LOCAL_TTL} IN A ${address}"
548 unbound-control local_zone youtube.com transparent
>/dev
/null
549 for address
in $
(resolve
"restrictmoderate.youtube.com"); do
550 unbound-control local_data
"www.youtube.com ${LOCAL_TTL} IN A ${address}"
558 # Print a nicer messagen when unbound is already running
559 if pidofproc
-s unbound
; then
560 statusproc
/usr
/sbin
/unbound
564 # Update configuration files
568 boot_mesg
"Starting Unbound DNS Proxy..."
569 loadproc
/usr
/sbin
/unbound ||
exit $?
571 # Make own hostname resolveable
574 # Install Safe Search rules when the system is already online
575 if [ -e "/var/ipfire/red/active" ]; then
584 boot_mesg
"Stopping Unbound DNS Proxy..."
585 killproc
/usr
/sbin
/unbound
595 statusproc
/usr
/sbin
/unbound
601 # Make sure DNS works at this point
602 fix_time_if_dns_fails
604 # Update Safe Search settings
621 echo "Usage: $0 {start|stop|restart|status|resolve|update-forwarders|remove-forwarders|update-safe-search}"
626 # End $rc_base/init.d/unbound