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
)
25 IFS
=.
read -r a1 a2 a3 a4
<<< ${addr}
27 echo "${a4}.${a3}.${a2}.${a1}.in-addr.arpa"
31 # Read name servers from ISP
32 if [ "${USE_ISP_NAMESERVERS}" = "on" -a "${PROTO}" != "TLS" ]; then
35 echo "$(</var/run/dns${i})"
39 # Read configured name servers
40 local id address tls_hostname enabled remark
41 while IFS
="," read -r id address tls_hostname enabled remark
; do
42 [ "${enabled}" != "enabled" ] && continue
44 if [ "${PROTO}" = "TLS" ]; then
45 if [ -n "${tls_hostname}" ]; then
46 echo "${address}@853#${tls_hostname}"
51 done < /var
/ipfire
/dns
/servers
54 check_red_has_carrier_and_ip
() {
55 # Interface configured ?
56 [ ! -e "/var/ipfire/red/iface" ] && return 0;
59 [ ! -e "/sys/class/net/$(</var/ipfire/red/iface)" ] && return 0;
62 [ ! "$(</sys/class/net/$(</var/ipfire/red/iface)/carrier)" = "1" ] && return 0;
65 [ "$(ip address show dev $(</var/ipfire/red/iface) | grep "inet
")" = "" ] && return 0;
71 echo "# This file is automatically generated and any changes"
72 echo "# will be overwritten. DO NOT EDIT!"
77 local hostname
=$
(hostname
-f)
78 # 1.1.1.1 is reserved for unused green, skip this
79 if [ -n "${GREEN_ADDRESS}" -a "${GREEN_ADDRESS}" != "1.1.1.1" ]; then
80 unbound-control
-q local_data
"${hostname} ${LOCAL_TTL} IN A ${GREEN_ADDRESS}"
84 for address
in ${GREEN_ADDRESS} ${BLUE_ADDRESS} ${ORANGE_ADDRESS}; do
85 [ -n "${address}" ] ||
continue
86 [ "${address}" = "1.1.1.1" ] && continue
88 address
=$
(ip_address_revptr
${address})
89 unbound-control
-q local_data
"${address} ${LOCAL_TTL} IN PTR ${hostname}"
94 local enabled address hostname domainname generateptr
96 while IFS
="," read -r enabled address hostname domainname generateptr
; do
97 [ "${enabled}" = "on" ] ||
continue
100 local fqdn
="${hostname}.${domainname}"
102 unbound-control
-q local_data
"${fqdn} ${LOCAL_TTL} IN A ${address}"
104 # Skip reverse resolution if the address equals the GREEN address
105 [ "${address}" = "${GREEN_ADDRESS}" ] && continue
107 # Skip reverse resolution if user requested not to do so
108 [ "${generateptr}" = "off" ] && continue
111 address
=$
(ip_address_revptr
${address})
112 unbound-control
-q local_data
"${address} ${LOCAL_TTL} IN PTR ${fqdn}"
113 done < /var
/ipfire
/main
/hosts
116 write_forward_conf
() {
120 # Force using TCP for upstream servers only
121 if [ "${PROTO}" = "TCP" ]; then
122 echo "# Force using TCP for upstream servers only"
124 echo " tcp-upstream: yes"
128 local insecure_zones
=""
130 local enabled zone server servers remark disable_dnssec rest
131 while IFS
="," read -r enabled zone servers remark disable_dnssec rest
; do
132 # Line must be enabled.
133 [ "${enabled}" = "on" ] ||
continue
135 # Zones that end with .local are commonly used for internal
136 # zones and therefore not signed
139 insecure_zones
="${insecure_zones} ${zone}"
142 if [ "${disable_dnssec}" = "on" ]; then
143 insecure_zones
="${insecure_zones} ${zone}"
149 echo " name: ${zone}"
150 for server
in ${servers//|/ }; do
151 if [[ ${server} =~ ^
[0-9]+\.
[0-9]+\.
[0-9]+\.
[0-9]+$
]]; then
152 echo " stub-addr: ${server}"
154 echo " stub-host: ${server}"
159 # Make all reverse lookup zones transparent
163 echo " local-zone: \"${zone}\" transparent"
167 done < /var
/ipfire
/dnsforward
/config
169 if [ -n "${insecure_zones}" ]; then
172 for zone
in ${insecure_zones}; do
173 echo " domain-insecure: ${zone}"
180 # Force using TLS only
181 if [ "${PROTO}" = "TLS" ]; then
182 echo " forward-tls-upstream: yes"
185 # Add upstream name servers
187 for ns
in $
(read_name_servers
); do
188 echo " forward-addr: ${ns}"
190 ) > /etc
/unbound
/forward.conf
193 write_tuning_conf
() {
194 # https://www.unbound.net/documentation/howto_optimise.html
196 # Determine number of online processors
197 local processors
=$
(getconf _NPROCESSORS_ONLN
)
199 # Determine number of slabs
201 while [ ${slabs} -lt ${processors} ]; do
202 slabs
=$
(( ${slabs} * 2 ))
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
239 # We run one thread per processor
240 echo "num-threads: ${processors}"
241 echo "so-reuseport: yes"
243 # Adjust number of slabs
244 echo "infra-cache-slabs: ${slabs}"
245 echo "key-cache-slabs: ${slabs}"
246 echo "msg-cache-slabs: ${slabs}"
247 echo "rrset-cache-slabs: ${slabs}"
250 echo "rrset-cache-size: $(( ${mem} / 2 ))m"
251 echo "msg-cache-size: $(( ${mem} / 4 ))m"
252 echo "key-cache-size: $(( ${mem} / 4 ))m"
254 # Increase parallel queries
255 echo "outgoing-range: 8192"
256 echo "num-queries-per-thread: 4096"
258 # Use larger send/receive buffers
261 ) > /etc
/unbound
/tuning.conf
264 get_memory_amount
() {
267 while read -r key val unit
; do
271 echo "$(( ${val} / 1024 ))"
278 fix_time_if_dns_fail
() {
279 # If DNS still not work try to init ntp with
280 # hardcoded ntp.ipfire.org (81.3.27.46)
281 check_red_has_carrier_and_ip
282 if [ -e "/var/ipfire/red/iface" -a "${?}" = "1" ]; then
283 host 0.ipfire.pool.ntp.org
> /dev
/null
2>&1
284 if [ "${?}" != "0" ]; then
285 boot_mesg
"DNS still not functioning... Trying to sync time with ntp.ipfire.org (81.3.27.46)..."
286 loadproc
/usr
/local
/bin
/settime
81.3.27.46
292 local hostname
="${1}"
295 for answer
in $
(dig +short A
"${hostname}"); do
296 # Filter out non-IP addresses
297 if [[ ! "${answer}" =~ \.$
]]; then
303 update_forwarders
() {
304 # DO nothing when we do not use the ISP name servers
305 [ "${USE_ISP_NAMESERVERS}" != "on" ] && return 0
307 # Update unbound about the new servers
308 local nameservers
=( $
(read_name_servers
) )
309 if [ -n "${nameservers[*]}" ]; then
310 unbound-control
-q forward
"${nameservers[@]}"
312 unbound-control
-q forward off
316 # Sets up Safe Search for various search engines
317 update_safe_search
() {
514 # Cleanup previous settings
515 unbound-control local_zone_remove
"bing.com" >/dev
/null
516 unbound-control local_zone_remove
"duckduckgo.com" >/dev
/null
517 unbound-control local_zone_remove
"yandex.com" >/dev
/null
518 unbound-control local_zone_remove
"yandex.ru" >/dev
/null
519 unbound-control local_zone_remove
"youtube.com" >/dev
/null
522 for domain
in ${google_tlds[@]}; do
523 unbound-control local_zone_remove
"${domain}"
526 # Nothing to do if safe search is not enabled
527 if [ "${ENABLE_SAFE_SEARCH}" != "on" ]; then
532 unbound-control bing.com transparent
>/dev
/null
533 for address
in $
(resolve
"strict.bing.com"); do
534 unbound-control local_data
"www.bing.com ${LOCAL_TTL} IN A ${address}"
538 unbound-control local_zone duckduckgo.com typetransparent
>/dev
/null
539 for address
in $
(resolve
"safe.duckduckgo.com"); do
540 unbound-control local_data
"duckduckgo.com ${LOCAL_TTL} IN A ${address}"
544 local addresses
="$(resolve "forcesafesearch.google.com
")"
545 for domain
in ${google_tlds[@]}; do
546 unbound-control local_zone
"${domain}" transparent
>/dev
/null
547 for address
in ${addresses}; do
548 unbound-control local_data
: "www.${domain} ${LOCAL_TTL} IN A ${address}"
553 for domain
in yandex.com yandex.ru
; do
554 unbound-control local_zone
"${domain}" typetransparent
>/dev
/null
555 for address
in $
(resolve
"familysearch.${domain}"); do
556 unbound-control local_data
"${domain} ${LOCAL_TTL} IN A ${address}"
561 unbound-control local_zone youtube.com transparent
>/dev
/null
562 for address
in $
(resolve
"restrictmoderate.youtube.com"); do
563 unbound-control local_data
"www.youtube.com ${LOCAL_TTL} IN A ${address}"
571 # Print a nicer messagen when unbound is already running
572 if pidofproc
-s unbound
; then
573 statusproc
/usr
/sbin
/unbound
577 eval $
(/usr
/local
/bin
/readhash
/var
/ipfire
/ethernet
/settings
)
579 # Update configuration files
583 boot_mesg
"Starting Unbound DNS Proxy..."
584 loadproc
/usr
/sbin
/unbound ||
exit $?
586 # Make own hostname resolveable
589 # Install Safe Search rules when the system is already online
590 if [ -e "/var/ipfire/red/active" ]; then
601 boot_mesg
"Stopping Unbound DNS Proxy..."
602 killproc
/usr
/sbin
/unbound
612 statusproc
/usr
/sbin
/unbound
618 # Update Safe Search settings
635 echo "Usage: $0 {start|stop|restart|status|resolve|update-forwarders|remove-forwarders|update-safe-search}"
640 # End $rc_base/init.d/unbound