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
55 check_red_has_carrier_and_ip
() {
56 # Interface configured ?
57 [ ! -e "/var/ipfire/red/iface" ] && return 0;
60 [ ! -e "/sys/class/net/$(</var/ipfire/red/iface)" ] && return 0;
63 [ ! "$(</sys/class/net/$(</var/ipfire/red/iface)/carrier)" = "1" ] && return 0;
66 [ "$(ip address show dev $(</var/ipfire/red/iface) | grep "inet
")" = "" ] && return 0;
72 echo "# This file is automatically generated and any changes"
73 echo "# will be overwritten. DO NOT EDIT!"
78 local hostname
=$
(hostname
-f)
79 # 1.1.1.1 is reserved for unused green, skip this
80 if [ -n "${GREEN_ADDRESS}" -a "${GREEN_ADDRESS}" != "1.1.1.1" ]; then
81 unbound-control
-q local_data
"${hostname} ${LOCAL_TTL} IN A ${GREEN_ADDRESS}"
85 for address
in ${GREEN_ADDRESS} ${BLUE_ADDRESS} ${ORANGE_ADDRESS}; do
86 [ -n "${address}" ] ||
continue
87 [ "${address}" = "1.1.1.1" ] && continue
89 address
=$
(ip_address_revptr
${address})
90 unbound-control
-q local_data
"${address} ${LOCAL_TTL} IN PTR ${hostname}"
95 local enabled address hostname domainname generateptr
97 while IFS
="," read -r enabled address hostname domainname generateptr
; do
98 [ "${enabled}" = "on" ] ||
continue
101 local fqdn
="${hostname}.${domainname}"
103 unbound-control
-q local_data
"${fqdn} ${LOCAL_TTL} IN A ${address}"
105 # Skip reverse resolution if the address equals the GREEN address
106 [ "${address}" = "${GREEN_ADDRESS}" ] && continue
108 # Skip reverse resolution if user requested not to do so
109 [ "${generateptr}" = "off" ] && continue
112 address
=$
(ip_address_revptr
${address})
113 unbound-control
-q local_data
"${address} ${LOCAL_TTL} IN PTR ${fqdn}"
114 done < /var
/ipfire
/main
/hosts
117 write_forward_conf
() {
121 # Force using TCP for upstream servers only
122 if [ "${PROTO}" = "TCP" ]; then
123 echo "# Force using TCP for upstream servers only"
125 echo " tcp-upstream: yes"
129 local insecure_zones
=""
131 local enabled zone server servers remark disable_dnssec rest
132 while IFS
="," read -r enabled zone servers remark disable_dnssec rest
; do
133 # Line must be enabled.
134 [ "${enabled}" = "on" ] ||
continue
136 # Zones that end with .local are commonly used for internal
137 # zones and therefore not signed
140 insecure_zones
="${insecure_zones} ${zone}"
143 if [ "${disable_dnssec}" = "on" ]; then
144 insecure_zones
="${insecure_zones} ${zone}"
150 echo " name: ${zone}"
151 for server
in ${servers//|/ }; do
152 if [[ ${server} =~ ^
[0-9]+\.
[0-9]+\.
[0-9]+\.
[0-9]+$
]]; then
153 echo " stub-addr: ${server}"
155 echo " stub-host: ${server}"
160 # Make all reverse lookup zones transparent
164 echo " local-zone: \"${zone}\" transparent"
168 done < /var
/ipfire
/dnsforward
/config
170 if [ -n "${insecure_zones}" ]; then
173 for zone
in ${insecure_zones}; do
174 echo " domain-insecure: ${zone}"
181 # Force using TLS only
182 if [ "${PROTO}" = "TLS" ]; then
183 echo " forward-tls-upstream: yes"
186 # Add upstream name servers
188 for ns
in $
(read_name_servers
); do
189 echo " forward-addr: ${ns}"
191 ) > /etc
/unbound
/forward.conf
194 write_tuning_conf
() {
195 # https://www.unbound.net/documentation/howto_optimise.html
197 # Determine number of online processors
198 local processors
=$
(getconf _NPROCESSORS_ONLN
)
200 # Determine number of slabs
202 while [ ${slabs} -lt ${processors} ]; do
203 slabs
=$
(( ${slabs} * 2 ))
206 # Determine amount of system memory
207 local mem
=$
(get_memory_amount
)
209 # In the worst case scenario, unbound can use double the
210 # amount of memory allocated to a cache due to malloc overhead
212 # Even larger systems with more than 8GB of RAM
213 if [ ${mem} -ge 8192 ]; then
216 # Extra large systems with more than 4GB of RAM
217 elif [ ${mem} -ge 4096 ]; then
220 # Large systems with more than 2GB of RAM
221 elif [ ${mem} -ge 2048 ]; then
224 # Medium systems with more than 1GB of RAM
225 elif [ ${mem} -ge 1024 ]; then
228 # Small systems with less than 256MB of RAM
229 elif [ ${mem} -le 256 ]; then
240 # We run one thread per processor
241 echo "num-threads: ${processors}"
242 echo "so-reuseport: yes"
244 # Adjust number of slabs
245 echo "infra-cache-slabs: ${slabs}"
246 echo "key-cache-slabs: ${slabs}"
247 echo "msg-cache-slabs: ${slabs}"
248 echo "rrset-cache-slabs: ${slabs}"
251 echo "rrset-cache-size: $(( ${mem} / 2 ))m"
252 echo "msg-cache-size: $(( ${mem} / 4 ))m"
253 echo "key-cache-size: $(( ${mem} / 4 ))m"
255 # Increase parallel queries
256 echo "outgoing-range: 8192"
257 echo "num-queries-per-thread: 4096"
259 # Use larger send/receive buffers
262 ) > /etc
/unbound
/tuning.conf
265 get_memory_amount
() {
268 while read -r key val unit
; do
272 echo "$(( ${val} / 1024 ))"
279 fix_time_if_dns_fail
() {
280 # If DNS still not work try to init ntp with
281 # hardcoded ntp.ipfire.org (81.3.27.46)
282 check_red_has_carrier_and_ip
283 if [ -e "/var/ipfire/red/iface" -a "${?}" = "1" ]; then
284 host 0.ipfire.pool.ntp.org
> /dev
/null
2>&1
285 if [ "${?}" != "0" ]; then
286 boot_mesg
"DNS still not functioning... Trying to sync time with ntp.ipfire.org (81.3.27.46)..."
287 loadproc
/usr
/local
/bin
/settime
81.3.27.46
293 local hostname
="${1}"
296 for answer
in $
(dig +short A
"${hostname}"); do
297 # Filter out non-IP addresses
298 if [[ ! "${answer}" =~ \.$
]]; then
304 update_forwarders
() {
305 # Do nothing when we do not use the ISP name servers
306 [ "${USE_ISP_NAMESERVERS}" != "on" ] && return 0
308 # We cannot update anything when using TLS
309 # Unbound will then try to connect to the servers using UDP on port 853
310 [ "${PROTO}" = "TLS" ] && return 0
312 # Update unbound about the new servers
313 local nameservers
=( $
(read_name_servers
) )
314 if [ -n "${nameservers[*]}" ]; then
315 unbound-control
-q forward
"${nameservers[@]}"
317 unbound-control
-q forward off
321 # Sets up Safe Search for various search engines
322 update_safe_search
() {
519 # Cleanup previous settings
520 unbound-control local_zone_remove
"bing.com" >/dev
/null
521 unbound-control local_zone_remove
"duckduckgo.com" >/dev
/null
522 unbound-control local_zone_remove
"yandex.com" >/dev
/null
523 unbound-control local_zone_remove
"yandex.ru" >/dev
/null
524 unbound-control local_zone_remove
"youtube.com" >/dev
/null
527 for domain
in ${google_tlds[@]}; do
528 unbound-control local_zone_remove
"${domain}"
531 # Nothing to do if safe search is not enabled
532 if [ "${ENABLE_SAFE_SEARCH}" != "on" ]; then
537 unbound-control bing.com transparent
>/dev
/null
538 for address
in $
(resolve
"strict.bing.com"); do
539 unbound-control local_data
"www.bing.com ${LOCAL_TTL} IN A ${address}"
543 unbound-control local_zone duckduckgo.com typetransparent
>/dev
/null
544 for address
in $
(resolve
"safe.duckduckgo.com"); do
545 unbound-control local_data
"duckduckgo.com ${LOCAL_TTL} IN A ${address}"
549 local addresses
="$(resolve "forcesafesearch.google.com
")"
550 for domain
in ${google_tlds[@]}; do
551 unbound-control local_zone
"${domain}" transparent
>/dev
/null
552 for address
in ${addresses}; do
553 unbound-control local_data
: "www.${domain} ${LOCAL_TTL} IN A ${address}"
558 for domain
in yandex.com yandex.ru
; do
559 unbound-control local_zone
"${domain}" typetransparent
>/dev
/null
560 for address
in $
(resolve
"familysearch.${domain}"); do
561 unbound-control local_data
"${domain} ${LOCAL_TTL} IN A ${address}"
566 unbound-control local_zone youtube.com transparent
>/dev
/null
567 for address
in $
(resolve
"restrictmoderate.youtube.com"); do
568 unbound-control local_data
"www.youtube.com ${LOCAL_TTL} IN A ${address}"
576 # Print a nicer messagen when unbound is already running
577 if pidofproc
-s unbound
; then
578 statusproc
/usr
/sbin
/unbound
582 # Update configuration files
586 boot_mesg
"Starting Unbound DNS Proxy..."
587 loadproc
/usr
/sbin
/unbound ||
exit $?
589 # Make own hostname resolveable
592 # Install Safe Search rules when the system is already online
593 if [ -e "/var/ipfire/red/active" ]; then
604 boot_mesg
"Stopping Unbound DNS Proxy..."
605 killproc
/usr
/sbin
/unbound
615 statusproc
/usr
/sbin
/unbound
621 # Update Safe Search settings
638 echo "Usage: $0 {start|stop|restart|status|resolve|update-forwarders|remove-forwarders|update-safe-search}"
643 # End $rc_base/init.d/unbound