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
)
23 if [ "${PROTO}" = "TCP" ]; then
31 IFS
=.
read -r a1 a2 a3 a4
<<< ${addr}
33 echo "${a4}.${a3}.${a2}.${a1}.in-addr.arpa"
39 echo "$(</var/ipfire/red/dns${i})"
40 done 2>/dev
/null |
xargs echo
43 check_red_has_carrier_and_ip
() {
44 # Interface configured ?
45 [ ! -e "/var/ipfire/red/iface" ] && return 0;
48 [ ! -e "/sys/class/net/$(</var/ipfire/red/iface)" ] && return 0;
51 [ ! "$(</sys/class/net/$(</var/ipfire/red/iface)/carrier)" = "1" ] && return 0;
54 [ "$(ip address show dev $(</var/ipfire/red/iface) | grep "inet
")" = "" ] && return 0;
60 echo "# This file is automatically generated and any changes"
61 echo "# will be overwritten. DO NOT EDIT!"
66 local hostname
=$
(hostname
-f)
67 # 1.1.1.1 is reserved for unused green, skip this
68 if [ -n "${GREEN_ADDRESS}" -a "${GREEN_ADDRESS}" != "1.1.1.1" ]; then
69 unbound-control
-q local_data
"${hostname} ${LOCAL_TTL} IN A ${GREEN_ADDRESS}"
73 for address
in ${GREEN_ADDRESS} ${BLUE_ADDRESS} ${ORANGE_ADDRESS}; do
74 [ -n "${address}" ] ||
continue
75 [ "${address}" = "1.1.1.1" ] && continue
77 address
=$
(ip_address_revptr
${address})
78 unbound-control
-q local_data
"${address} ${LOCAL_TTL} IN PTR ${hostname}"
83 local enabled address hostname domainname generateptr
85 while IFS
="," read -r enabled address hostname domainname generateptr
; do
86 [ "${enabled}" = "on" ] ||
continue
89 local fqdn
="${hostname}.${domainname}"
91 unbound-control
-q local_data
"${fqdn} ${LOCAL_TTL} IN A ${address}"
93 # Skip reverse resolution if the address equals the GREEN address
94 [ "${address}" = "${GREEN_ADDRESS}" ] && continue
96 # Skip reverse resolution if user requested not to do so
97 [ "${generateptr}" = "off" ] && continue
100 address
=$
(ip_address_revptr
${address})
101 unbound-control
-q local_data
"${address} ${LOCAL_TTL} IN PTR ${fqdn}"
102 done < /var
/ipfire
/main
/hosts
105 write_forward_conf
() {
109 # Force using TCP for upstream servers only
110 if [ "${PROTO}" = "TCP" ]; then
111 echo "# Force using TCP for upstream servers only"
113 echo " tcp-upstream: yes"
117 local insecure_zones
=""
119 local enabled zone server servers remark disable_dnssec rest
120 while IFS
="," read -r enabled zone servers remark disable_dnssec rest
; do
121 # Line must be enabled.
122 [ "${enabled}" = "on" ] ||
continue
124 # Zones that end with .local are commonly used for internal
125 # zones and therefore not signed
128 insecure_zones
="${insecure_zones} ${zone}"
131 if [ "${disable_dnssec}" = "on" ]; then
132 insecure_zones
="${insecure_zones} ${zone}"
138 echo " name: ${zone}"
139 for server
in ${servers//|/ }; do
140 if [[ ${server} =~ ^
[0-9]+\.
[0-9]+\.
[0-9]+\.
[0-9]+$
]]; then
141 echo " stub-addr: ${server}"
143 echo " stub-host: ${server}"
148 # Make all reverse lookup zones transparent
152 echo " local-zone: \"${zone}\" transparent"
156 done < /var
/ipfire
/dnsforward
/config
158 if [ -n "${insecure_zones}" ]; then
161 for zone
in ${insecure_zones}; do
162 echo " domain-insecure: ${zone}"
169 # Force using TLS only
170 if [ "${PROTO}" = "TLS" ]; then
171 echo " forward-tls-upstream: yes"
174 # Add upstream name servers
175 local id address tls_hostname enabled remark
176 while IFS
="," read -r id address tls_hostname enabled remark
; do
177 # Skip disabled servers
178 [ "${enabled}" != "enabled" ] && continue
181 if [ "${PROTO}" = "TLS" ]; then
182 if [ -n "${tls_hostname}" ]; then
183 echo " forward-addr: ${address}@853#${tls_hostname}"
186 echo " forward-addr: ${address}"
188 done < /var
/ipfire
/dns
/servers
189 ) > /etc
/unbound
/forward.conf
192 write_tuning_conf
() {
193 # https://www.unbound.net/documentation/howto_optimise.html
195 # Determine number of online processors
196 local processors
=$
(getconf _NPROCESSORS_ONLN
)
198 # Determine number of slabs
200 while [ ${slabs} -lt ${processors} ]; do
201 slabs
=$
(( ${slabs} * 2 ))
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
238 # We run one thread per processor
239 echo "num-threads: ${processors}"
240 echo "so-reuseport: yes"
242 # Adjust number of slabs
243 echo "infra-cache-slabs: ${slabs}"
244 echo "key-cache-slabs: ${slabs}"
245 echo "msg-cache-slabs: ${slabs}"
246 echo "rrset-cache-slabs: ${slabs}"
249 echo "rrset-cache-size: $(( ${mem} / 2 ))m"
250 echo "msg-cache-size: $(( ${mem} / 4 ))m"
251 echo "key-cache-size: $(( ${mem} / 4 ))m"
253 # Increase parallel queries
254 echo "outgoing-range: 8192"
255 echo "num-queries-per-thread: 4096"
257 # Use larger send/receive buffers
260 ) > /etc
/unbound
/tuning.conf
263 get_memory_amount
() {
266 while read -r key val unit
; do
270 echo "$(( ${val} / 1024 ))"
277 fix_time_if_dns_fail
() {
278 # If DNS still not work try to init ntp with
279 # hardcoded ntp.ipfire.org (81.3.27.46)
280 check_red_has_carrier_and_ip
281 if [ -e "/var/ipfire/red/iface" -a "${?}" = "1" ]; then
282 host 0.ipfire.pool.ntp.org
> /dev
/null
2>&1
283 if [ "${?}" != "0" ]; then
284 boot_mesg
"DNS still not functioning... Trying to sync time with ntp.ipfire.org (81.3.27.46)..."
285 loadproc
/usr
/local
/bin
/settime
81.3.27.46
291 local hostname
="${1}"
295 for ns
in $
(read_name_servers
); do
297 for answer
in $
(dig "${DIG_ARGS[@]}" +short "@${ns}" A "${hostname}"); do
300 # Filter out non-IP addresses
301 if [[ ! "${answer}" =~ \.$
]]; then
306 # End loop when we have got something
307 [ ${found} -eq 1 ] && break
311 # Sets up Safe Search for various search engines
312 update_safe_search
() {
509 # Cleanup previous settings
510 unbound-control local_zone_remove
"bing.com" >/dev
/null
511 unbound-control local_zone_remove
"duckduckgo.com" >/dev
/null
512 unbound-control local_zone_remove
"yandex.com" >/dev
/null
513 unbound-control local_zone_remove
"yandex.ru" >/dev
/null
514 unbound-control local_zone_remove
"youtube.com" >/dev
/null
517 for domain
in ${google_tlds[@]}; do
518 unbound-control local_zone_remove
"${domain}"
521 # Nothing to do if safe search is not enabled
522 if [ "${ENABLE_SAFE_SEARCH}" != "on" ]; then
527 unbound-control bing.com transparent
>/dev
/null
528 for address
in $
(resolve
"strict.bing.com"); do
529 unbound-control local_data
"www.bing.com ${LOCAL_TTL} IN A ${address}"
533 unbound-control local_zone duckduckgo.com typetransparent
>/dev
/null
534 for address
in $
(resolve
"safe.duckduckgo.com"); do
535 unbound-control local_data
"duckduckgo.com ${LOCAL_TTL} IN A ${address}"
539 local addresses
="$(resolve "forcesafesearch.google.com
")"
540 for domain
in ${google_tlds[@]}; do
541 unbound-control local_zone
"${domain}" transparent
>/dev
/null
542 for address
in ${addresses}; do
543 unbound-control local_data
: "www.${domain} ${LOCAL_TTL} IN A ${address}"
548 for domain
in yandex.com yandex.ru
; do
549 unbound-control local_zone
"${domain}" typetransparent
>/dev
/null
550 for address
in $
(resolve
"familysearch.${domain}"); do
551 unbound-control local_data
"${domain} ${LOCAL_TTL} IN A ${address}"
556 unbound-control local_zone youtube.com transparent
>/dev
/null
557 for address
in $
(resolve
"restrictmoderate.youtube.com"); do
558 unbound-control local_data
"www.youtube.com ${LOCAL_TTL} IN A ${address}"
566 # Print a nicer messagen when unbound is already running
567 if pidofproc
-s unbound
; then
568 statusproc
/usr
/sbin
/unbound
572 eval $
(/usr
/local
/bin
/readhash
/var
/ipfire
/ethernet
/settings
)
574 # Update configuration files
578 boot_mesg
"Starting Unbound DNS Proxy..."
579 loadproc
/usr
/sbin
/unbound ||
exit $?
581 # Make own hostname resolveable
584 # Install Safe Search rules when the system is already online
585 if [ -e "/var/ipfire/red/active" ]; then
596 boot_mesg
"Stopping Unbound DNS Proxy..."
597 killproc
/usr
/sbin
/unbound
607 statusproc
/usr
/sbin
/unbound
611 : # XXX must set ISP name servers if necessary
615 : # XXX must remove ISP name servers
627 echo "Usage: $0 {start|stop|restart|status|resolve|update-forwarders|remove-forwarders|update-safe-search}"
632 # End $rc_base/init.d/unbound