echo "local-data: \"${address} ${LOCAL_TTL} IN PTR ${HOSTNAME}\""
done
- # Add all hosts
local enabled address hostname domainname generateptr
+
+ # Find all unique domain names
+ while IFS="," read -r enabled address hostname domainname generateptr; do
+ [ "${enabled}" = "on" ] || continue
+
+ # Skip empty domainnames
+ [ "${domainname}" = "" ] && continue
+
+ echo "local-zone: ${domainname} typetransparent"
+ done < /var/ipfire/main/hosts | sort -u
+
+ # Add all hosts
while IFS="," read -r enabled address hostname domainname generateptr; do
[ "${enabled}" = "on" ] || continue
done
fi
- echo "forward-zone:"
- echo " name: \".\""
+ # Read name servers.
+ nameservers=$(read_name_servers)
- # Force using TLS only
- if [ "${PROTO}" = "TLS" ]; then
- echo " forward-tls-upstream: yes"
+ # Only write forward zones if any nameservers are configured.
+ #
+ # Otherwise fall-back into recursor mode.
+ if [ -n "${nameservers}" ]; then
+
+ echo "forward-zone:"
+ echo " name: \".\""
+
+ # Force using TLS only
+ if [ "${PROTO}" = "TLS" ]; then
+ echo " forward-tls-upstream: yes"
+ fi
+
+ # Add upstream name servers
+ local ns
+ for ns in ${nameservers}; do
+ echo " forward-addr: ${ns}"
+ done
fi
- # Add upstream name servers
- local ns
- for ns in $(read_name_servers); do
- echo " forward-addr: ${ns}"
- done
) > /etc/unbound/forward.conf
}
write_tuning_conf() {
# https://www.unbound.net/documentation/howto_optimise.html
- # Determine number of online processors
- local processors=$(getconf _NPROCESSORS_ONLN)
-
- # Determine number of slabs
- local slabs=1
- while [ ${slabs} -lt ${processors} ]; do
- slabs=$(( ${slabs} * 2 ))
- done
-
# Determine amount of system memory
local mem=$(get_memory_amount)
(
config_header
- # We run one thread per processor
- echo "num-threads: ${processors}"
- echo "so-reuseport: yes"
-
- # Adjust number of slabs
- echo "infra-cache-slabs: ${slabs}"
- echo "key-cache-slabs: ${slabs}"
- echo "msg-cache-slabs: ${slabs}"
- echo "rrset-cache-slabs: ${slabs}"
-
# Slice up the cache
echo "rrset-cache-size: $(( ${mem} / 2 ))m"
echo "msg-cache-size: $(( ${mem} / 4 ))m"
resolve() {
local hostname="${1}"
+ local found=1
local answer
for answer in $(dig +short A "${hostname}"); do
# Filter out non-IP addresses
if [[ ! "${answer}" =~ \.$ ]]; then
+ found=0
echo "${answer}"
fi
done
+
+ return ${found}
}
# Sets up Safe Search for various search engines
unbound-control local_zone_remove "${domain}"
done >/dev/null
- # Nothing to do if safe search is not enabled
- if [ "${ENABLE_SAFE_SEARCH}" != "on" ]; then
- return 0
- fi
-
- # Bing
- unbound-control bing.com transparent >/dev/null
- for address in $(resolve "strict.bing.com"); do
- unbound-control local_data "www.bing.com ${LOCAL_TTL} IN A ${address}"
- done >/dev/null
-
- # DuckDuckGo
- unbound-control local_zone duckduckgo.com typetransparent >/dev/null
- for address in $(resolve "safe.duckduckgo.com"); do
- unbound-control local_data "duckduckgo.com ${LOCAL_TTL} IN A ${address}"
- done >/dev/null
-
- # Google
- local addresses="$(resolve "forcesafesearch.google.com")"
- for domain in ${google_tlds[@]}; do
- unbound-control local_zone "${domain}" transparent >/dev/null
- for address in ${addresses}; do
- unbound-control local_data: "www.${domain} ${LOCAL_TTL} IN A ${address}"
+ if [ "${ENABLE_SAFE_SEARCH}" = "on" ]; then
+ # Bing
+ unbound-control bing.com transparent >/dev/null
+ for address in $(resolve "strict.bing.com"); do
+ unbound-control local_data "www.bing.com ${LOCAL_TTL} IN A ${address}"
done >/dev/null
- done
- # Yandex
- for domain in yandex.com yandex.ru; do
- unbound-control local_zone "${domain}" typetransparent >/dev/null
- for address in $(resolve "familysearch.${domain}"); do
- unbound-control local_data "${domain} ${LOCAL_TTL} IN A ${address}"
+ # DuckDuckGo
+ unbound-control local_zone duckduckgo.com typetransparent >/dev/null
+ for address in $(resolve "safe.duckduckgo.com"); do
+ unbound-control local_data "duckduckgo.com ${LOCAL_TTL} IN A ${address}"
done >/dev/null
- done
- # YouTube
- unbound-control local_zone youtube.com transparent >/dev/null
- for address in $(resolve "restrictmoderate.youtube.com"); do
- unbound-control local_data "www.youtube.com ${LOCAL_TTL} IN A ${address}"
- done >/dev/null
+ # Google
+ local addresses="$(resolve "forcesafesearch.google.com")"
+ for domain in ${google_tlds[@]}; do
+ unbound-control local_zone "${domain}" transparent >/dev/null
+ for address in ${addresses}; do
+ unbound-control local_data "www.${domain} ${LOCAL_TTL} IN A ${address}"
+ done >/dev/null
+ done
+
+ # Yandex
+ for domain in yandex.com yandex.ru; do
+ unbound-control local_zone "${domain}" typetransparent >/dev/null
+ for address in $(resolve "familysearch.${domain}"); do
+ unbound-control local_data "${domain} ${LOCAL_TTL} IN A ${address}"
+ done >/dev/null
+ done
+
+ # YouTube
+ if [ "${ENABLE_SAFE_SEARCH_YOUTUBE}" = "on" ]; then
+ unbound-control local_zone youtube.com transparent >/dev/null
+ for address in $(resolve "restrictmoderate.youtube.com"); do
+ unbound-control local_data "www.youtube.com ${LOCAL_TTL} IN A ${address}"
+ done >/dev/null
+ fi
+ fi
return 0
}
sleep 1
$0 start
;;
- reload|remove-forwarders)
+ reload|update-forwarders)
# Update configuration files
write_forward_conf
write_hosts_conf
+ # Call unbound-control and perform the reload
+ /usr/sbin/unbound-control -q reload
+
+ # Dummy Resolve to wait for unbound
+ resolve "ping.ipfire.org" &>/dev/null
+
+ if [ "$1" = "update-forwarders" ]; then
+ # Make sure DNS works at this point
+ fix_time_if_dns_fails
+ fi
+
# Update Safe Search rules if the system is online.
if [ -e "/var/ipfire/red/active" ]; then
update_safe_search
fi
-
- # Call unbound-control and perform the reload
- /usr/sbin/unbound-control -q reload
;;
status)
statusproc /usr/sbin/unbound
;;
- update-forwarders)
- $0 reload
-
- # Make sure DNS works at this point
- fix_time_if_dns_fails
- ;;
-
resolve)
- resolve "${2}"
+ resolve "${2}" || exit $?
;;
*)
- echo "Usage: $0 {start|stop|restart|reload|status|resolve|update-forwarders|remove-forwarders}"
+ echo "Usage: $0 {start|stop|restart|reload|status|resolve|update-forwarders}"
exit 1
;;
esac