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 # Cache any local zones for 60 seconds
14 eval $
(/usr
/local
/bin
/readhash
/var
/ipfire
/dns
/settings
)
15 eval $
(/usr
/local
/bin
/readhash
/var
/ipfire
/ethernet
/settings
)
21 IFS
=.
read -r a1 a2 a3 a4
<<< ${addr}
23 echo "${a4}.${a3}.${a2}.${a1}.in-addr.arpa"
27 # Read name servers from ISP
28 if [ "${USE_ISP_NAMESERVERS}" = "on" -a "${PROTO}" != "TLS" ]; then
31 echo "$(</var/run/dns${i})"
35 # Read configured name servers
36 local id address tls_hostname enabled remark
37 while IFS
="," read -r id address tls_hostname enabled remark
; do
38 [ "${enabled}" != "enabled" ] && continue
40 if [ "${PROTO}" = "TLS" ]; then
41 if [ -n "${tls_hostname}" ]; then
42 echo "${address}@853#${tls_hostname}"
47 done < /var
/ipfire
/dns
/servers
51 echo "# This file is automatically generated and any changes"
52 echo "# will be overwritten. DO NOT EDIT!"
60 # Make own hostname resolveable
61 # 1.1.1.1 is reserved for unused green, skip this
62 if [ -n "${GREEN_ADDRESS}" -a "${GREEN_ADDRESS}" != "1.1.1.1" ]; then
63 echo "local-data: \"${HOSTNAME} ${LOCAL_TTL} IN A ${GREEN_ADDRESS}\""
67 for address
in ${GREEN_ADDRESS} ${BLUE_ADDRESS} ${ORANGE_ADDRESS}; do
68 [ -n "${address}" ] ||
continue
69 [ "${address}" = "1.1.1.1" ] && continue
71 address
=$
(ip_address_revptr
${address})
72 echo "local-data: \"${address} ${LOCAL_TTL} IN PTR ${HOSTNAME}\""
76 local enabled address hostname domainname generateptr
77 while IFS
="," read -r enabled address hostname domainname generateptr
; do
78 [ "${enabled}" = "on" ] ||
continue
81 local fqdn
="${hostname}.${domainname}"
82 echo "local-data: \"${fqdn} ${LOCAL_TTL} IN A ${address}\""
84 # Skip reverse resolution if the address equals the GREEN address
85 [ "${address}" = "${GREEN_ADDRESS}" ] && continue
87 # Skip reverse resolution if user requested not to do so
88 [ "${generateptr}" = "off" ] && continue
91 address
=$
(ip_address_revptr
${address})
92 echo "local-data: \"${address} ${LOCAL_TTL} IN PTR ${fqdn}\""
93 done < /var
/ipfire
/main
/hosts
94 ) > /etc
/unbound
/hosts.conf
97 write_forward_conf
() {
101 # Enable strict QNAME minimisation
102 if [ "${QNAME_MIN}" = "strict" ]; then
104 echo " qname-minimisation-strict: yes"
108 # Force using TCP for upstream servers only
109 if [ "${PROTO}" = "TCP" ]; then
110 echo "# Force using TCP for upstream servers only"
112 echo " tcp-upstream: yes"
116 local insecure_zones
=""
118 local enabled zone server servers remark disable_dnssec rest
119 while IFS
="," read -r enabled zone servers remark disable_dnssec rest
; do
120 # Line must be enabled.
121 [ "${enabled}" = "on" ] ||
continue
123 # Zones that end with .local are commonly used for internal
124 # zones and therefore not signed
127 insecure_zones
="${insecure_zones} ${zone}"
130 if [ "${disable_dnssec}" = "on" ]; then
131 insecure_zones
="${insecure_zones} ${zone}"
137 echo " name: ${zone}"
138 for server
in ${servers//|/ }; do
139 if [[ ${server} =~ ^
[0-9]+\.
[0-9]+\.
[0-9]+\.
[0-9]+$
]]; then
140 echo " stub-addr: ${server}"
142 echo " stub-host: ${server}"
147 # Make all reverse lookup zones transparent
151 echo " local-zone: \"${zone}\" transparent"
155 done < /var
/ipfire
/dnsforward
/config
157 if [ -n "${insecure_zones}" ]; then
160 for zone
in ${insecure_zones}; do
161 echo " domain-insecure: ${zone}"
166 nameservers
=$
(read_name_servers
)
168 # Only write forward zones if any nameservers are configured.
170 # Otherwise fall-back into recursor mode.
171 if [ -n "${nameservers}" ]; then
176 # Force using TLS only
177 if [ "${PROTO}" = "TLS" ]; then
178 echo " forward-tls-upstream: yes"
181 # Add upstream name servers
183 for ns
in ${nameservers}; do
184 echo " forward-addr: ${ns}"
188 ) > /etc
/unbound
/forward.conf
191 write_tuning_conf
() {
192 # https://www.unbound.net/documentation/howto_optimise.html
194 # Determine number of online processors
195 local processors
=$
(getconf _NPROCESSORS_ONLN
)
197 # Determine number of slabs
199 while [ ${slabs} -lt ${processors} ]; do
200 slabs
=$
(( ${slabs} * 2 ))
203 # Determine amount of system memory
204 local mem
=$
(get_memory_amount
)
206 # In the worst case scenario, unbound can use double the
207 # amount of memory allocated to a cache due to malloc overhead
209 # Even larger systems with more than 8GB of RAM
210 if [ ${mem} -ge 8192 ]; then
213 # Extra large systems with more than 4GB of RAM
214 elif [ ${mem} -ge 4096 ]; then
217 # Large systems with more than 2GB of RAM
218 elif [ ${mem} -ge 2048 ]; then
221 # Medium systems with more than 1GB of RAM
222 elif [ ${mem} -ge 1024 ]; then
225 # Small systems with less than 256MB of RAM
226 elif [ ${mem} -le 256 ]; then
237 # We run one thread per processor
238 echo "num-threads: ${processors}"
239 echo "so-reuseport: yes"
241 # Adjust number of slabs
242 echo "infra-cache-slabs: ${slabs}"
243 echo "key-cache-slabs: ${slabs}"
244 echo "msg-cache-slabs: ${slabs}"
245 echo "rrset-cache-slabs: ${slabs}"
248 echo "rrset-cache-size: $(( ${mem} / 2 ))m"
249 echo "msg-cache-size: $(( ${mem} / 4 ))m"
250 echo "key-cache-size: $(( ${mem} / 4 ))m"
252 # Increase parallel queries
253 echo "outgoing-range: 8192"
254 echo "num-queries-per-thread: 4096"
256 # Use larger send/receive buffers
259 ) > /etc
/unbound
/tuning.conf
262 get_memory_amount
() {
265 while read -r key val unit
; do
269 echo "$(( ${val} / 1024 ))"
276 fix_time_if_dns_fails
() {
277 # If DNS is working, everything is fine
278 if resolve
"ping.ipfire.org" &>/dev
/null
; then
282 # Try to sync time with a known time server
283 boot_mesg
"DNS not functioning... Trying to sync time with ntp.ipfire.org (81.3.27.46)..."
284 loadproc
/usr
/local
/bin
/settime
81.3.27.46
288 local hostname
="${1}"
292 for answer
in $
(dig +short A
"${hostname}"); do
293 # Filter out non-IP addresses
294 if [[ ! "${answer}" =~ \.$
]]; then
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
569 boot_mesg
"Starting Unbound DNS Proxy..."
570 loadproc
/usr
/sbin
/unbound ||
exit $?
572 # Install Safe Search rules when the system is already online
573 if [ -e "/var/ipfire/red/active" ]; then
579 boot_mesg
"Stopping Unbound DNS Proxy..."
580 killproc
/usr
/sbin
/unbound
588 reload|update-forwarders
)
589 # Update configuration files
593 # Call unbound-control and perform the reload
594 /usr
/sbin
/unbound-control
-q reload
596 # Dummy Resolve to wait for unbound
597 resolve
"ping.ipfire.org" &>/dev
/null
599 if [ "$1" = "update-forwarders" ]; then
600 # Make sure DNS works at this point
601 fix_time_if_dns_fails
604 # Update Safe Search rules if the system is online.
605 if [ -e "/var/ipfire/red/active" ]; then
611 statusproc
/usr
/sbin
/unbound
615 resolve
"${2}" ||
exit $?
619 echo "Usage: $0 {start|stop|restart|reload|status|resolve|update-forwarders}"
624 # End $rc_base/init.d/unbound