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}\""
75 local enabled address hostname domainname generateptr
77 # Find all unique domain names
78 while IFS
="," read -r enabled address hostname domainname generateptr
; do
79 [ "${enabled}" = "on" ] ||
continue
82 done < /var
/ipfire
/main
/hosts |
sort -u |
while read -r domainname
; do
83 echo "local-zone: ${domainname} typetransparent"
87 while IFS
="," read -r enabled address hostname domainname generateptr
; do
88 [ "${enabled}" = "on" ] ||
continue
91 local fqdn
="${hostname}.${domainname}"
92 echo "local-data: \"${fqdn} ${LOCAL_TTL} IN A ${address}\""
94 # Skip reverse resolution if the address equals the GREEN address
95 [ "${address}" = "${GREEN_ADDRESS}" ] && continue
97 # Skip reverse resolution if user requested not to do so
98 [ "${generateptr}" = "off" ] && continue
101 address
=$
(ip_address_revptr
${address})
102 echo "local-data: \"${address} ${LOCAL_TTL} IN PTR ${fqdn}\""
103 done < /var
/ipfire
/main
/hosts
104 ) > /etc
/unbound
/hosts.conf
107 write_forward_conf
() {
111 # Enable strict QNAME minimisation
112 if [ "${QNAME_MIN}" = "strict" ]; then
114 echo " qname-minimisation-strict: yes"
118 # Force using TCP for upstream servers only
119 if [ "${PROTO}" = "TCP" ]; then
120 echo "# Force using TCP for upstream servers only"
122 echo " tcp-upstream: yes"
126 local insecure_zones
=""
128 local enabled zone server servers remark disable_dnssec rest
129 while IFS
="," read -r enabled zone servers remark disable_dnssec rest
; do
130 # Line must be enabled.
131 [ "${enabled}" = "on" ] ||
continue
133 # Zones that end with .local are commonly used for internal
134 # zones and therefore not signed
137 insecure_zones
="${insecure_zones} ${zone}"
140 if [ "${disable_dnssec}" = "on" ]; then
141 insecure_zones
="${insecure_zones} ${zone}"
147 echo " name: ${zone}"
148 for server
in ${servers//|/ }; do
149 if [[ ${server} =~ ^
[0-9]+\.
[0-9]+\.
[0-9]+\.
[0-9]+$
]]; then
150 echo " stub-addr: ${server}"
152 echo " stub-host: ${server}"
157 # Make all reverse lookup zones transparent
161 echo " local-zone: \"${zone}\" transparent"
165 done < /var
/ipfire
/dnsforward
/config
167 if [ -n "${insecure_zones}" ]; then
170 for zone
in ${insecure_zones}; do
171 echo " domain-insecure: ${zone}"
176 nameservers
=$
(read_name_servers
)
178 # Only write forward zones if any nameservers are configured.
180 # Otherwise fall-back into recursor mode.
181 if [ -n "${nameservers}" ]; then
186 # Force using TLS only
187 if [ "${PROTO}" = "TLS" ]; then
188 echo " forward-tls-upstream: yes"
191 # Add upstream name servers
193 for ns
in ${nameservers}; do
194 echo " forward-addr: ${ns}"
198 ) > /etc
/unbound
/forward.conf
201 write_tuning_conf
() {
202 # https://www.unbound.net/documentation/howto_optimise.html
204 # Determine amount of system memory
205 local mem
=$
(get_memory_amount
)
207 # In the worst case scenario, unbound can use double the
208 # amount of memory allocated to a cache due to malloc overhead
210 # Even larger systems with more than 8GB of RAM
211 if [ ${mem} -ge 8192 ]; then
214 # Extra large systems with more than 4GB of RAM
215 elif [ ${mem} -ge 4096 ]; then
218 # Large systems with more than 2GB of RAM
219 elif [ ${mem} -ge 2048 ]; then
222 # Medium systems with more than 1GB of RAM
223 elif [ ${mem} -ge 1024 ]; then
226 # Small systems with less than 256MB of RAM
227 elif [ ${mem} -le 256 ]; then
239 echo "rrset-cache-size: $(( ${mem} / 2 ))m"
240 echo "msg-cache-size: $(( ${mem} / 4 ))m"
241 echo "key-cache-size: $(( ${mem} / 4 ))m"
243 # Increase parallel queries
244 echo "outgoing-range: 8192"
245 echo "num-queries-per-thread: 4096"
247 # Use larger send/receive buffers
250 ) > /etc
/unbound
/tuning.conf
253 get_memory_amount
() {
256 while read -r key val unit
; do
260 echo "$(( ${val} / 1024 ))"
267 fix_time_if_dns_fails
() {
268 # If DNS is working, everything is fine
269 if resolve
"ping.ipfire.org" &>/dev
/null
; then
273 # Try to sync time with a known time server
274 boot_mesg
"DNS not functioning... Trying to sync time with ntp.ipfire.org (81.3.27.46)..."
275 loadproc
/usr
/local
/bin
/settime
81.3.27.46
279 local hostname
="${1}"
283 for answer
in $
(dig +short A
"${hostname}"); do
284 # Filter out non-IP addresses
285 if [[ ! "${answer}" =~ \.$
]]; then
294 # Sets up Safe Search for various search engines
295 update_safe_search
() {
492 # Cleanup previous settings
493 unbound-control local_zone_remove
"bing.com" >/dev
/null
494 unbound-control local_zone_remove
"duckduckgo.com" >/dev
/null
495 unbound-control local_zone_remove
"yandex.com" >/dev
/null
496 unbound-control local_zone_remove
"yandex.ru" >/dev
/null
497 unbound-control local_zone_remove
"youtube.com" >/dev
/null
500 for domain
in ${google_tlds[@]}; do
501 unbound-control local_zone_remove
"${domain}"
504 # Nothing to do if safe search is not enabled
505 if [ "${ENABLE_SAFE_SEARCH}" != "on" ]; then
510 unbound-control bing.com transparent
>/dev
/null
511 for address
in $
(resolve
"strict.bing.com"); do
512 unbound-control local_data
"www.bing.com ${LOCAL_TTL} IN A ${address}"
516 unbound-control local_zone duckduckgo.com typetransparent
>/dev
/null
517 for address
in $
(resolve
"safe.duckduckgo.com"); do
518 unbound-control local_data
"duckduckgo.com ${LOCAL_TTL} IN A ${address}"
522 local addresses
="$(resolve "forcesafesearch.google.com
")"
523 for domain
in ${google_tlds[@]}; do
524 unbound-control local_zone
"${domain}" transparent
>/dev
/null
525 for address
in ${addresses}; do
526 unbound-control local_data
"www.${domain} ${LOCAL_TTL} IN A ${address}"
531 for domain
in yandex.com yandex.ru
; do
532 unbound-control local_zone
"${domain}" typetransparent
>/dev
/null
533 for address
in $
(resolve
"familysearch.${domain}"); do
534 unbound-control local_data
"${domain} ${LOCAL_TTL} IN A ${address}"
539 unbound-control local_zone youtube.com transparent
>/dev
/null
540 for address
in $
(resolve
"restrictmoderate.youtube.com"); do
541 unbound-control local_data
"www.youtube.com ${LOCAL_TTL} IN A ${address}"
549 # Print a nicer messagen when unbound is already running
550 if pidofproc
-s unbound
; then
551 statusproc
/usr
/sbin
/unbound
555 # Update configuration files
560 boot_mesg
"Starting Unbound DNS Proxy..."
561 loadproc
/usr
/sbin
/unbound ||
exit $?
563 # Install Safe Search rules when the system is already online
564 if [ -e "/var/ipfire/red/active" ]; then
570 boot_mesg
"Stopping Unbound DNS Proxy..."
571 killproc
/usr
/sbin
/unbound
579 reload|update-forwarders
)
580 # Update configuration files
584 # Call unbound-control and perform the reload
585 /usr
/sbin
/unbound-control
-q reload
587 # Dummy Resolve to wait for unbound
588 resolve
"ping.ipfire.org" &>/dev
/null
590 if [ "$1" = "update-forwarders" ]; then
591 # Make sure DNS works at this point
592 fix_time_if_dns_fails
595 # Update Safe Search rules if the system is online.
596 if [ -e "/var/ipfire/red/active" ]; then
602 statusproc
/usr
/sbin
/unbound
606 resolve
"${2}" ||
exit $?
610 echo "Usage: $0 {start|stop|restart|reload|status|resolve|update-forwarders}"
615 # End $rc_base/init.d/unbound