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 # Make own hostname resolveable
82 local enabled address hostname domainname generateptr
84 while IFS
="," read -r enabled address hostname domainname generateptr
; do
85 [ "${enabled}" = "on" ] ||
continue
88 local fqdn
="${hostname}.${domainname}"
90 unbound-control
-q local_data
"${fqdn} ${LOCAL_TTL} IN A ${address}"
92 # Skip reverse resolution if the address equals the GREEN address
93 [ "${address}" = "${GREEN_ADDRESS}" ] && continue
95 # Skip reverse resolution if user requested not to do so
96 [ "${generateptr}" = "off" ] && continue
99 address
=$
(ip_address_revptr
${address})
100 unbound-control
-q local_data
"${address} ${LOCAL_TTL} IN PTR ${fqdn}"
101 done < /var
/ipfire
/main
/hosts
104 write_forward_conf
() {
108 # Enable strict QNAME minimisation
109 if [ "${QNAME_MIN}" = "strict" ]; then
111 echo " qname-minimisation-strict: yes"
115 # Force using TCP for upstream servers only
116 if [ "${PROTO}" = "TCP" ]; then
117 echo "# Force using TCP for upstream servers only"
119 echo " tcp-upstream: yes"
123 local insecure_zones
=""
125 local enabled zone server servers remark disable_dnssec rest
126 while IFS
="," read -r enabled zone servers remark disable_dnssec rest
; do
127 # Line must be enabled.
128 [ "${enabled}" = "on" ] ||
continue
130 # Zones that end with .local are commonly used for internal
131 # zones and therefore not signed
134 insecure_zones
="${insecure_zones} ${zone}"
137 if [ "${disable_dnssec}" = "on" ]; then
138 insecure_zones
="${insecure_zones} ${zone}"
144 echo " name: ${zone}"
145 for server
in ${servers//|/ }; do
146 if [[ ${server} =~ ^
[0-9]+\.
[0-9]+\.
[0-9]+\.
[0-9]+$
]]; then
147 echo " stub-addr: ${server}"
149 echo " stub-host: ${server}"
154 # Make all reverse lookup zones transparent
158 echo " local-zone: \"${zone}\" transparent"
162 done < /var
/ipfire
/dnsforward
/config
164 if [ -n "${insecure_zones}" ]; then
167 for zone
in ${insecure_zones}; do
168 echo " domain-insecure: ${zone}"
175 # Force using TLS only
176 if [ "${PROTO}" = "TLS" ]; then
177 echo " forward-tls-upstream: yes"
180 # Add upstream name servers
182 for ns
in $
(read_name_servers
); do
183 echo " forward-addr: ${ns}"
185 ) > /etc
/unbound
/forward.conf
188 write_tuning_conf
() {
189 # https://www.unbound.net/documentation/howto_optimise.html
191 # Determine number of online processors
192 local processors
=$
(getconf _NPROCESSORS_ONLN
)
194 # Determine number of slabs
196 while [ ${slabs} -lt ${processors} ]; do
197 slabs
=$
(( ${slabs} * 2 ))
200 # Determine amount of system memory
201 local mem
=$
(get_memory_amount
)
203 # In the worst case scenario, unbound can use double the
204 # amount of memory allocated to a cache due to malloc overhead
206 # Even larger systems with more than 8GB of RAM
207 if [ ${mem} -ge 8192 ]; then
210 # Extra large systems with more than 4GB of RAM
211 elif [ ${mem} -ge 4096 ]; then
214 # Large systems with more than 2GB of RAM
215 elif [ ${mem} -ge 2048 ]; then
218 # Medium systems with more than 1GB of RAM
219 elif [ ${mem} -ge 1024 ]; then
222 # Small systems with less than 256MB of RAM
223 elif [ ${mem} -le 256 ]; then
234 # We run one thread per processor
235 echo "num-threads: ${processors}"
236 echo "so-reuseport: yes"
238 # Adjust number of slabs
239 echo "infra-cache-slabs: ${slabs}"
240 echo "key-cache-slabs: ${slabs}"
241 echo "msg-cache-slabs: ${slabs}"
242 echo "rrset-cache-slabs: ${slabs}"
245 echo "rrset-cache-size: $(( ${mem} / 2 ))m"
246 echo "msg-cache-size: $(( ${mem} / 4 ))m"
247 echo "key-cache-size: $(( ${mem} / 4 ))m"
249 # Increase parallel queries
250 echo "outgoing-range: 8192"
251 echo "num-queries-per-thread: 4096"
253 # Use larger send/receive buffers
256 ) > /etc
/unbound
/tuning.conf
259 get_memory_amount
() {
262 while read -r key val unit
; do
266 echo "$(( ${val} / 1024 ))"
273 fix_time_if_dns_fails
() {
274 # If DNS is working, everything is fine
275 if resolve
"ping.ipfire.org" &>/dev
/null
; then
279 # Try to sync time with a known time server
280 boot_mesg
"DNS not functioning... Trying to sync time with ntp.ipfire.org (81.3.27.46)..."
281 loadproc
/usr
/local
/bin
/settime
81.3.27.46
285 local hostname
="${1}"
288 for answer
in $
(dig +short A
"${hostname}"); do
289 # Filter out non-IP addresses
290 if [[ ! "${answer}" =~ \.$
]]; then
296 update_forwarders
() {
297 # Do nothing when we do not use the ISP name servers
298 [ "${USE_ISP_NAMESERVERS}" != "on" ] && return 0
300 # We cannot update anything when using TLS
301 # Unbound will then try to connect to the servers using UDP on port 853
302 [ "${PROTO}" = "TLS" ] && return 0
304 # Update unbound about the new servers
305 local nameservers
=( $
(read_name_servers
) )
306 if [ -n "${nameservers[*]}" ]; then
307 unbound-control
-q forward
"${nameservers[@]}"
309 unbound-control
-q forward off
313 # Sets up Safe Search for various search engines
314 update_safe_search
() {
511 # Cleanup previous settings
512 unbound-control local_zone_remove
"bing.com" >/dev
/null
513 unbound-control local_zone_remove
"duckduckgo.com" >/dev
/null
514 unbound-control local_zone_remove
"yandex.com" >/dev
/null
515 unbound-control local_zone_remove
"yandex.ru" >/dev
/null
516 unbound-control local_zone_remove
"youtube.com" >/dev
/null
519 for domain
in ${google_tlds[@]}; do
520 unbound-control local_zone_remove
"${domain}"
523 # Nothing to do if safe search is not enabled
524 if [ "${ENABLE_SAFE_SEARCH}" != "on" ]; then
529 unbound-control bing.com transparent
>/dev
/null
530 for address
in $
(resolve
"strict.bing.com"); do
531 unbound-control local_data
"www.bing.com ${LOCAL_TTL} IN A ${address}"
535 unbound-control local_zone duckduckgo.com typetransparent
>/dev
/null
536 for address
in $
(resolve
"safe.duckduckgo.com"); do
537 unbound-control local_data
"duckduckgo.com ${LOCAL_TTL} IN A ${address}"
541 local addresses
="$(resolve "forcesafesearch.google.com
")"
542 for domain
in ${google_tlds[@]}; do
543 unbound-control local_zone
"${domain}" transparent
>/dev
/null
544 for address
in ${addresses}; do
545 unbound-control local_data
: "www.${domain} ${LOCAL_TTL} IN A ${address}"
550 for domain
in yandex.com yandex.ru
; do
551 unbound-control local_zone
"${domain}" typetransparent
>/dev
/null
552 for address
in $
(resolve
"familysearch.${domain}"); do
553 unbound-control local_data
"${domain} ${LOCAL_TTL} IN A ${address}"
558 unbound-control local_zone youtube.com transparent
>/dev
/null
559 for address
in $
(resolve
"restrictmoderate.youtube.com"); do
560 unbound-control local_data
"www.youtube.com ${LOCAL_TTL} IN A ${address}"
568 # Print a nicer messagen when unbound is already running
569 if pidofproc
-s unbound
; then
570 statusproc
/usr
/sbin
/unbound
574 # Update configuration files
578 boot_mesg
"Starting Unbound DNS Proxy..."
579 loadproc
/usr
/sbin
/unbound ||
exit $?
581 # Install Safe Search rules when the system is already online
582 if [ -e "/var/ipfire/red/active" ]; then
591 boot_mesg
"Stopping Unbound DNS Proxy..."
592 killproc
/usr
/sbin
/unbound
601 # Update configuration files
604 # Update Safe Search rules if the system is online.
605 if [ -e "/var/ipfire/red/active" ]; then
612 # Call unbound-control and perform the reload
613 /usr
/sbin
/unbound-control
-q reload
617 statusproc
/usr
/sbin
/unbound
623 # Make sure DNS works at this point
624 fix_time_if_dns_fails
626 # Update Safe Search settings
643 echo "Usage: $0 {start|stop|restart|reload|status|resolve|update-forwarders|remove-forwarders|update-safe-search}"
648 # End $rc_base/init.d/unbound