]> git.ipfire.org Git - ipfire-2.x.git/blobdiff - src/initscripts/system/unbound
unbound: fix typo at safesearch for googe
[ipfire-2.x.git] / src / initscripts / system / unbound
index 5ae020584262178bfe2c20db90e23fdc003c0542..b3185feabb873fe743ff878016be68d919309643 100644 (file)
@@ -7,11 +7,6 @@
 . /etc/sysconfig/rc
 . ${rc_functions}
 
-TEST_DOMAIN="ipfire.org"
-
-# This domain will never validate
-TEST_DOMAIN_FAIL="dnssec-failed.org"
-
 # Cache any local zones for 60 seconds
 LOCAL_TTL=60
 
@@ -58,47 +53,45 @@ config_header() {
        echo
 }
 
-own_hostname() {
-       local hostname=$(hostname -f)
-       # 1.1.1.1 is reserved for unused green, skip this
-       if [ -n "${GREEN_ADDRESS}" -a "${GREEN_ADDRESS}" != "1.1.1.1" ]; then
-               unbound-control -q local_data "${hostname} ${LOCAL_TTL} IN A ${GREEN_ADDRESS}"
-       fi
-
-       local address
-       for address in ${GREEN_ADDRESS} ${BLUE_ADDRESS} ${ORANGE_ADDRESS}; do
-               [ -n "${address}" ] || continue
-               [ "${address}" = "1.1.1.1" ] && continue
-
-               address=$(ip_address_revptr ${address})
-               unbound-control -q local_data "${address} ${LOCAL_TTL} IN PTR ${hostname}"
-       done
-}
+write_hosts_conf() {
+       (
+               config_header
 
-update_hosts() {
-       # Make own hostname resolveable
-       own_hostname
+               # Make own hostname resolveable
+               # 1.1.1.1 is reserved for unused green, skip this
+               if [ -n "${GREEN_ADDRESS}" -a "${GREEN_ADDRESS}" != "1.1.1.1" ]; then
+                       echo "local-data: \"${HOSTNAME} ${LOCAL_TTL} IN A ${GREEN_ADDRESS}\""
+               fi
 
-       local enabled address hostname domainname generateptr
+               local address
+               for address in ${GREEN_ADDRESS} ${BLUE_ADDRESS} ${ORANGE_ADDRESS}; do
+                       [ -n "${address}" ] || continue
+                       [ "${address}" = "1.1.1.1" ] && continue
 
-       while IFS="," read -r enabled address hostname domainname generateptr; do
-               [ "${enabled}" = "on" ] || continue
+                       address=$(ip_address_revptr ${address})
+                       echo "local-data: \"${address} ${LOCAL_TTL} IN PTR ${HOSTNAME}\""
+               done
 
-               # Build FQDN
-               local fqdn="${hostname}.${domainname}"
+               # Add all hosts
+               local enabled address hostname domainname generateptr
+               while IFS="," read -r enabled address hostname domainname generateptr; do
+                       [ "${enabled}" = "on" ] || continue
 
-               unbound-control -q local_data "${fqdn} ${LOCAL_TTL} IN A ${address}"
+                       # Build FQDN
+                       local fqdn="${hostname}.${domainname}"
+                       echo "local-data: \"${fqdn} ${LOCAL_TTL} IN A ${address}\""
 
-               # Skip reverse resolution if the address equals the GREEN address
-               [ "${address}" = "${GREEN_ADDRESS}" ] && continue
+                       # Skip reverse resolution if the address equals the GREEN address
+                       [ "${address}" = "${GREEN_ADDRESS}" ] && continue
 
-               # Skip reverse resolution if user requested not to do so
-               [ "${generateptr}" = "off" ] && continue
+                       # Skip reverse resolution if user requested not to do so
+                       [ "${generateptr}" = "off" ] && continue
 
-               # Add RDNS
-               address=$(ip_address_revptr ${address})
-               unbound-control -q local_data "${address} ${LOCAL_TTL} IN PTR ${fqdn}"
-       done < /var/ipfire/main/hosts
+                       # Add RDNS
+                       address=$(ip_address_revptr ${address})
+                       echo "local-data: \"${address} ${LOCAL_TTL} IN PTR ${fqdn}\""
+               done < /var/ipfire/main/hosts
+       ) > /etc/unbound/hosts.conf
 }
 
 write_forward_conf() {
@@ -169,19 +162,29 @@ write_forward_conf() {
                        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
 }
 
@@ -271,6 +274,8 @@ get_memory_amount() {
 }
 
 fix_time_if_dns_fails() {
+       # Sometimes the first try fails so do it twice
+       resolve "ping.ipfire.org" &>/dev/null
        # If DNS is working, everything is fine
        if resolve "ping.ipfire.org" &>/dev/null; then
                return 0
@@ -283,31 +288,18 @@ fix_time_if_dns_fails() {
 
 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
-}
-
-update_forwarders() {
-       # Do nothing when we do not use the ISP name servers
-       [ "${USE_ISP_NAMESERVERS}" != "on" ] && return 0
 
-       # We cannot update anything when using TLS
-       # Unbound will then try to connect to the servers using UDP on port 853
-       [ "${PROTO}" = "TLS" ] && return 0
-
-       # Update unbound about the new servers
-       local nameservers=( $(read_name_servers) )
-       if [ -n "${nameservers[*]}" ]; then
-               unbound-control -q forward "${nameservers[@]}"
-       else
-               unbound-control -q forward off
-       fi
+       return ${found}
 }
 
 # Sets up Safe Search for various search engines
@@ -542,7 +534,7 @@ update_safe_search() {
        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}"
+                       unbound-control local_data "www.${domain} ${LOCAL_TTL} IN A ${address}"
                done >/dev/null
        done
 
@@ -573,6 +565,7 @@ case "$1" in
 
                # Update configuration files
                write_tuning_conf
+               write_hosts_conf
                write_forward_conf
 
                boot_mesg "Starting Unbound DNS Proxy..."
@@ -582,9 +575,6 @@ case "$1" in
                if [ -e "/var/ipfire/red/active" ]; then
                        update_safe_search
                fi
-
-               # Update hosts
-               update_hosts
                ;;
 
        stop)
@@ -597,51 +587,42 @@ case "$1" in
                sleep 1
                $0 start
                ;;
-       reload)
+       reload|remove-forwarders|update-forwarders)
                # Update configuration files
-               write_tuning_conf
                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
-
-               # Update hosts.
-               update_hosts
-
-               # Call unbound-control and perform the reload
-               /usr/sbin/unbound-control -q reload
                ;;
 
        status)
                statusproc /usr/sbin/unbound
                ;;
 
-       update-forwarders)
-               update_forwarders
-
                # Make sure DNS works at this point
                fix_time_if_dns_fails
-
-               # Update Safe Search settings
-               update_safe_search
-               ;;
-
-       remove-forwarders)
-               update_forwarders
                ;;
 
        resolve)
-               resolve "${2}"
-               ;;
-
-       update-safe-search)
-               update_safe_search
+               resolve "${2}" || exit $?
                ;;
 
        *)
-               echo "Usage: $0 {start|stop|restart|reload|status|resolve|update-forwarders|remove-forwarders|update-safe-search}"
+               echo "Usage: $0 {start|stop|restart|reload|status|resolve|update-forwarders|remove-forwarders}"
                exit 1
                ;;
 esac