]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blobdiff - src/initscripts/system/unbound
unbound: run "time-fix" before savesearch resolves
[people/pmueller/ipfire-2.x.git] / src / initscripts / system / unbound
index 138b9623565a047de577309909ebad0135663c1c..7bf2ffc2cfed62b215d5813bad7096b4af5ea9c5 100644 (file)
@@ -7,22 +7,12 @@
 . /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
 
 # Load configuration
 eval $(/usr/local/bin/readhash /var/ipfire/dns/settings)
-
-DIG_ARGS=()
-
-if [ "${PROTO}" = "TCP" ]; then
-       DIG_ARGS+=( "+tcp" )
-fi
+eval $(/usr/local/bin/readhash /var/ipfire/ethernet/settings)
 
 ip_address_revptr() {
        local addr=${1}
@@ -34,26 +24,27 @@ ip_address_revptr() {
 }
 
 read_name_servers() {
-       local i
-       for i in 1 2; do
-               echo "$(</var/ipfire/red/dns${i})"
-       done 2>/dev/null | xargs echo
-}
-
-check_red_has_carrier_and_ip() {
-       # Interface configured ?
-       [ ! -e "/var/ipfire/red/iface" ] && return 0;
-
-       # Interface present ?
-       [ ! -e "/sys/class/net/$(</var/ipfire/red/iface)" ] && return 0;
-
-       # has carrier ?
-       [ ! "$(</sys/class/net/$(</var/ipfire/red/iface)/carrier)" = "1" ] && return 0;
+       # Read name servers from ISP
+       if [ "${USE_ISP_NAMESERVERS}" = "on" -a "${PROTO}" != "TLS" ]; then
+               local i
+               for i in 1 2; do
+                       echo "$(</var/run/dns${i})"
+               done 2>/dev/null
+       fi
 
-       # has ip ?
-       [ "$(ip address show dev $(</var/ipfire/red/iface) | grep "inet")" = "" ] && return 0;
+       # Read configured name servers
+       local id address tls_hostname enabled remark
+       while IFS="," read -r id address tls_hostname enabled remark; do
+               [ "${enabled}" != "enabled" ] && continue
 
-       return 1;
+               if [ "${PROTO}" = "TLS" ]; then
+                       if [ -n "${tls_hostname}" ]; then
+                               echo "${address}@853#${tls_hostname}"
+                       fi
+               else
+                       echo "${address}"
+               fi
+       done < /var/ipfire/dns/servers
 }
 
 config_header() {
@@ -62,50 +53,58 @@ 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
+write_hosts_conf() {
+       (
+               config_header
 
-               address=$(ip_address_revptr ${address})
-               unbound-control -q local_data "${address} ${LOCAL_TTL} IN PTR ${hostname}"
-       done
-}
+               # 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
 
-update_hosts() {
-       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() {
        (
                config_header
 
+               # Enable strict QNAME minimisation
+               if [ "${QNAME_MIN}" = "strict" ]; then
+                       echo "server:"
+                       echo "  qname-minimisation-strict: yes"
+                       echo
+               fi
+
                # Force using TCP for upstream servers only
                if [ "${PROTO}" = "TCP" ]; then
                        echo "# Force using TCP for upstream servers only"
@@ -163,29 +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"
-               fi
+               # Only write forward zones if any nameservers are configured.
+               #
+               # Otherwise fall-back into recursor mode.
+               if [ -n "${nameservers}" ]; then
 
-               # Add upstream name servers
-               local id address tls_hostname enabled remark
-               while IFS="," read -r id address tls_hostname enabled remark; do
-                       # Skip disabled servers
-                       [ "${enabled}" != "enabled" ] && continue
+                       echo "forward-zone:"
+                       echo "  name: \".\""
 
-                       # Set DNS server
+                       # Force using TLS only
                        if [ "${PROTO}" = "TLS" ]; then
-                               if [ -n "${tls_hostname}" ]; then
-                                       echo "  forward-addr: ${address}@853#${tls_hostname}"
-                               fi
-                       else
-                               echo "  forward-addr: ${address}"
+                               echo "  forward-tls-upstream: yes"
                        fi
-               done < /var/ipfire/dns/servers
+
+                       # Add upstream name servers
+                       local ns
+                       for ns in ${nameservers}; do
+                               echo "  forward-addr: ${ns}"
+                       done
+               fi
+
        ) > /etc/unbound/forward.conf
 }
 
@@ -274,38 +273,33 @@ get_memory_amount() {
        done < /proc/meminfo
 }
 
-fix_time_if_dns_fail() {
-       # If DNS still not work try to init ntp with
-       # hardcoded ntp.ipfire.org (81.3.27.46)
-       check_red_has_carrier_and_ip
-       if [ -e "/var/ipfire/red/iface" -a "${?}" = "1" ]; then
-               host 0.ipfire.pool.ntp.org > /dev/null 2>&1
-               if [ "${?}" != "0" ]; then
-                       boot_mesg "DNS still not functioning... Trying to sync time with ntp.ipfire.org (81.3.27.46)..."
-                       loadproc /usr/local/bin/settime 81.3.27.46
-               fi
+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
        fi
+
+       # Try to sync time with a known time server
+       boot_mesg "DNS not functioning... Trying to sync time with ntp.ipfire.org (81.3.27.46)..."
+       loadproc /usr/local/bin/settime 81.3.27.46
 }
 
 resolve() {
        local hostname="${1}"
-
-       local found=0
-       local ns
-       for ns in $(read_name_servers); do
-               local answer
-               for answer in $(dig "${DIG_ARGS[@]}" +short "@${ns}" A "${hostname}"); do
-                       found=1
-
-                       # Filter out non-IP addresses
-                       if [[ ! "${answer}" =~ \.$ ]]; then
-                               echo "${answer}"
-                       fi
-               done
-
-               # End loop when we have got something
-               [ ${found} -eq 1 ] && break
+       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
@@ -569,27 +563,18 @@ case "$1" in
                        exit 0
                fi
 
-               eval $(/usr/local/bin/readhash /var/ipfire/ethernet/settings)
-
                # Update configuration files
                write_tuning_conf
+               write_hosts_conf
                write_forward_conf
 
                boot_mesg "Starting Unbound DNS Proxy..."
                loadproc /usr/sbin/unbound || exit $?
 
-               # Make own hostname resolveable
-               own_hostname
-
                # Install Safe Search rules when the system is already online
                if [ -e "/var/ipfire/red/active" ]; then
                        update_safe_search
                fi
-
-               # Update hosts
-               update_hosts
-
-               fix_time_if_dns_fail
                ;;
 
        stop)
@@ -602,29 +587,42 @@ case "$1" in
                sleep 1
                $0 start
                ;;
+       reload|remove-forwarders|update-forwarders)
+               # Update configuration files
+               write_forward_conf
+               write_hosts_conf
 
-       status)
-               statusproc /usr/sbin/unbound
-               ;;
+               # 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-forwarders)
-               : # XXX must set ISP name servers if necessary
+               # Update Safe Search rules if the system is online.
+               if [ -e "/var/ipfire/red/active" ]; then
+                       update_safe_search
+               fi
                ;;
 
-       remove-forwarders)
-               : # XXX must remove ISP name servers
+       status)
+               statusproc /usr/sbin/unbound
                ;;
 
-       resolve)
-               resolve "${2}"
+               # Make sure DNS works at this point
+               fix_time_if_dns_fails
                ;;
 
-       update-safe-search)
-               update_safe_search
+       resolve)
+               resolve "${2}" || exit $?
                ;;
 
        *)
-               echo "Usage: $0 {start|stop|restart|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