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
81 # Skip empty domainnames
82 [ "${domainname}" = "" ] && continue
84 echo "local-zone: ${domainname} typetransparent"
85 done < /var
/ipfire
/main
/hosts |
sort -u
88 while IFS
="," read -r enabled address hostname domainname generateptr
; do
89 [ "${enabled}" = "on" ] ||
continue
92 local fqdn
="${hostname}.${domainname}"
93 echo "local-data: \"${fqdn} ${LOCAL_TTL} IN A ${address}\""
95 # Skip reverse resolution if the address equals the GREEN address
96 [ "${address}" = "${GREEN_ADDRESS}" ] && continue
98 # Skip reverse resolution if user requested not to do so
99 [ "${generateptr}" = "off" ] && continue
102 address
=$
(ip_address_revptr
${address})
103 echo "local-data: \"${address} ${LOCAL_TTL} IN PTR ${fqdn}\""
104 done < /var
/ipfire
/main
/hosts
105 ) > /etc
/unbound
/hosts.conf
108 write_forward_conf
() {
112 # Enable strict QNAME minimisation
113 if [ "${QNAME_MIN}" = "strict" ]; then
115 echo " qname-minimisation-strict: yes"
119 # Force using TCP for upstream servers only
120 if [ "${PROTO}" = "TCP" ]; then
121 echo "# Force using TCP for upstream servers only"
123 echo " tcp-upstream: yes"
127 local insecure_zones
=""
129 local enabled zone server servers remark disable_dnssec rest
130 while IFS
="," read -r enabled zone servers remark disable_dnssec rest
; do
131 # Line must be enabled.
132 [ "${enabled}" = "on" ] ||
continue
134 # Zones that end with .local are commonly used for internal
135 # zones and therefore not signed
138 insecure_zones
="${insecure_zones} ${zone}"
141 if [ "${disable_dnssec}" = "on" ]; then
142 insecure_zones
="${insecure_zones} ${zone}"
148 echo " name: ${zone}"
149 for server
in ${servers//|/ }; do
150 if [[ ${server} =~ ^
[0-9]+\.
[0-9]+\.
[0-9]+\.
[0-9]+$
]]; then
151 echo " stub-addr: ${server}"
153 echo " stub-host: ${server}"
158 # Make all reverse lookup zones transparent
162 echo " local-zone: \"${zone}\" transparent"
166 done < /var
/ipfire
/dnsforward
/config
168 if [ -n "${insecure_zones}" ]; then
171 for zone
in ${insecure_zones}; do
172 echo " domain-insecure: ${zone}"
177 nameservers
=$
(read_name_servers
)
179 # Only write forward zones if any nameservers are configured.
181 # Otherwise fall-back into recursor mode.
182 if [ -n "${nameservers}" ]; then
187 # Force using TLS only
188 if [ "${PROTO}" = "TLS" ]; then
189 echo " forward-tls-upstream: yes"
192 # Add upstream name servers
194 for ns
in ${nameservers}; do
195 echo " forward-addr: ${ns}"
199 ) > /etc
/unbound
/forward.conf
202 write_tuning_conf
() {
203 # https://www.unbound.net/documentation/howto_optimise.html
205 # Determine amount of system memory
206 local mem
=$
(get_memory_amount
)
208 # In the worst case scenario, unbound can use double the
209 # amount of memory allocated to a cache due to malloc overhead
211 # Even larger systems with more than 8GB of RAM
212 if [ ${mem} -ge 8192 ]; then
215 # Extra large systems with more than 4GB of RAM
216 elif [ ${mem} -ge 4096 ]; then
219 # Large systems with more than 2GB of RAM
220 elif [ ${mem} -ge 2048 ]; then
223 # Medium systems with more than 1GB of RAM
224 elif [ ${mem} -ge 1024 ]; then
227 # Small systems with less than 256MB of RAM
228 elif [ ${mem} -le 256 ]; then
240 echo "rrset-cache-size: $(( ${mem} / 2 ))m"
241 echo "msg-cache-size: $(( ${mem} / 4 ))m"
242 echo "key-cache-size: $(( ${mem} / 4 ))m"
244 # Increase parallel queries
245 echo "outgoing-range: 8192"
246 echo "num-queries-per-thread: 4096"
248 # Use larger send/receive buffers
251 ) > /etc
/unbound
/tuning.conf
254 get_memory_amount
() {
257 while read -r key val unit
; do
261 echo "$(( ${val} / 1024 ))"
268 fix_time_if_dns_fails
() {
269 # If DNS is working, everything is fine
270 if resolve
"ping.ipfire.org" &>/dev
/null
; then
274 # Try to sync time with a known time server
275 boot_mesg
"DNS not functioning... Trying to sync time with ntp.ipfire.org (81.3.27.46)..."
276 loadproc
/usr
/local
/bin
/settime
81.3.27.46
280 local hostname
="${1}"
284 for answer
in $
(dig +short A
"${hostname}"); do
285 # Filter out non-IP addresses
286 if [[ ! "${answer}" =~ \.$
]]; then
295 # Sets up Safe Search for various search engines
296 update_safe_search
() {
493 # Cleanup previous settings
494 unbound-control local_zone_remove
"bing.com" >/dev
/null
495 unbound-control local_zone_remove
"duckduckgo.com" >/dev
/null
496 unbound-control local_zone_remove
"yandex.com" >/dev
/null
497 unbound-control local_zone_remove
"yandex.ru" >/dev
/null
498 unbound-control local_zone_remove
"youtube.com" >/dev
/null
501 for domain
in ${google_tlds[@]}; do
502 unbound-control local_zone_remove
"${domain}"
505 # Nothing to do if safe search is not enabled
506 if [ "${ENABLE_SAFE_SEARCH}" != "on" ]; then
511 unbound-control bing.com transparent
>/dev
/null
512 for address
in $
(resolve
"strict.bing.com"); do
513 unbound-control local_data
"www.bing.com ${LOCAL_TTL} IN A ${address}"
517 unbound-control local_zone duckduckgo.com typetransparent
>/dev
/null
518 for address
in $
(resolve
"safe.duckduckgo.com"); do
519 unbound-control local_data
"duckduckgo.com ${LOCAL_TTL} IN A ${address}"
523 local addresses
="$(resolve "forcesafesearch.google.com
")"
524 for domain
in ${google_tlds[@]}; do
525 unbound-control local_zone
"${domain}" transparent
>/dev
/null
526 for address
in ${addresses}; do
527 unbound-control local_data
"www.${domain} ${LOCAL_TTL} IN A ${address}"
532 for domain
in yandex.com yandex.ru
; do
533 unbound-control local_zone
"${domain}" typetransparent
>/dev
/null
534 for address
in $
(resolve
"familysearch.${domain}"); do
535 unbound-control local_data
"${domain} ${LOCAL_TTL} IN A ${address}"
540 unbound-control local_zone youtube.com transparent
>/dev
/null
541 for address
in $
(resolve
"restrictmoderate.youtube.com"); do
542 unbound-control local_data
"www.youtube.com ${LOCAL_TTL} IN A ${address}"
550 # Print a nicer messagen when unbound is already running
551 if pidofproc
-s unbound
; then
552 statusproc
/usr
/sbin
/unbound
556 # Update configuration files
561 boot_mesg
"Starting Unbound DNS Proxy..."
562 loadproc
/usr
/sbin
/unbound ||
exit $?
564 # Install Safe Search rules when the system is already online
565 if [ -e "/var/ipfire/red/active" ]; then
571 boot_mesg
"Stopping Unbound DNS Proxy..."
572 killproc
/usr
/sbin
/unbound
580 reload|update-forwarders
)
581 # Update configuration files
585 # Call unbound-control and perform the reload
586 /usr
/sbin
/unbound-control
-q reload
588 # Dummy Resolve to wait for unbound
589 resolve
"ping.ipfire.org" &>/dev
/null
591 if [ "$1" = "update-forwarders" ]; then
592 # Make sure DNS works at this point
593 fix_time_if_dns_fails
596 # Update Safe Search rules if the system is online.
597 if [ -e "/var/ipfire/red/active" ]; then
603 statusproc
/usr
/sbin
/unbound
607 resolve
"${2}" ||
exit $?
611 echo "Usage: $0 {start|stop|restart|reload|status|resolve|update-forwarders}"
616 # End $rc_base/init.d/unbound