We should use lower case for normal variables.
[openresolv] / dnsmasq.in
old mode 100755 (executable)
new mode 100644 (file)
index b26973d..21450f3
@@ -1,5 +1,5 @@
 #!/bin/sh
-# Copyright 2007-2008 Roy Marples
+# Copyright 2007-2009 Roy Marples
 # All rights reserved
 
 # dnsmasq subscriber for resolvconf
 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
-# This is very important!
-# We assume that we are a local dns cache - after all, why would a server
-# use resolvconf?
-# Now that we have assumed this, we also assume that generic DHCP clients
-# will enter their domains and search domains ONLY in the "search" field
-# in their resolv.confs and VPN clients will put the domain they are for
-# into the domain field only.
-# This allows dnsmasq to forward domains for a specific VPN domain to the
-# VPN nameserver and everything else to the standard name servers.
-
-# A sample dnsmasq config that works as above is like so.
-# NOTE: The loopback interface on some systems maybe lo0.
-#domain-needed
-#interface=lo
-#resolv-file=@PREFIX@/etc/dnsmasq-resolv.conf
-#conf-file=@PREFIX@/etc/dnsmasq-resolvconf.conf
-
-# The last step is to configure dns configuration for /etc/resolv.conf
-# for the lo interface. You can do this in resolvconf as well by adding
-#nameserver 127.0.0.1
-# to resolv.conf.d/base
-
-# Load our variables from resolvconf
-VARS="$(resolvconf -v)"
-eval "${VARS}"
+[ -f "@SYSCONFDIR@"/resolvconf.conf ] || exit 0
+. "@SYSCONFDIR@/resolvconf.conf" || exit 1
+[ -z "${dnsmasq_conf}" -o -z "${dnsmasq_resolv}" ] && exit 0
+eval "$(${RESOLVCONF:-resolvconf} -v)"
 
 PREFIX=@PREFIX@
-DNSMASQRESOLV="${PREFIX}/etc/dnsmasq-resolv.conf"
-DNSMASQCONF="${PREFIX}/etc/dnsmasq-resolvconf.conf"
-DNSMASQPID=/var/run/dnsmasq.pid
+dnsmasqpid="${dnsmasq_pid:-/var/run/dnsmasq.pid}"
 
-NEWCONF="# Generated by resolvconf\n"
-NEWRESOLV="${NEWCONF}"
+newconf="# Generated by resolvconf\n"
+newresolv="${newconf}"
 
-# Using DBUS means that we never have to restart the daemon
+# Using dbus means that we never have to restart the daemon
 # This is important as it means we should not drop DNS queries
-# whilst changing DNS options around. However, DBUS support is optional
+# whilst changing DNS options around. However, dbus support is optional
 # so we need to validate a few things first.
 # Check for DBus support in the binary
-DBUS=no
-DBUSPID=/var/run/dbus/dbus.pid
-[ -s "${DBUSPID}" ] || DBUSPID=/var/run/dbus.pid
-[ -s "${DBUSPID}" ] || DBUSPID=/var/run/dbus/pid
-if [ -s "${DBUSPID}" -a -s ${DNSMASQPID} ]; then
+
+dbus=false
+dbuspid="${dbus_pid:-/var/run/dbus/dbus.pid}"
+[ -s "${dbuspid}" ] || dbuspid=/var/run/dbus.pid
+[ -s "${dbuspid}" ] || dbuspid=/var/run/dbus/pid
+if [ -s "${dbuspid}" -a -s ${dnsmasqpid} ]; then
        if dnsmasq --version 2>/dev/null | \
-               grep -q "^Compile time options.*[[:space:]]DBus[[:space:]]" \
-               ; then
+               grep -q "^Compile time options.*[[:space:]]DBus[[:space:]]"
+       then
                # Sanity - check that dnsmasq and dbus are running
-               if kill -0 $(cat "${DBUSPID}") 2>/dev/null \
-                       && kill -0 $(cat ${DNSMASQPID}) 2>/dev/null; then
-                       DBUS=yes
-                       NEWCONF="${NEWCONF}\n# Domain specific servers will be sent over dbus\nenable-dbus\n"
+               if kill -0 $(cat "${dbuspid}") 2>/dev/null && \
+                       kill -0 $(cat ${dnsmasqpid}) 2>/dev/null
+               then
+                       dbus=true
+                       newconf="${newconf}\n# Domain specific servers will"
+                       newconf="${newconf} be sent over dbus\nenable-dbus\n"
                fi
        fi
 fi
 
-uniqify() {
-    local result=
-    while [ -n "$1" ]; do
-               case " ${result} " in
-                       *" $1 "*);;
-                       *) result="${result} $1";;
-               esac
-               shift
-       done
-    echo "${result# *}"
-}
-
-# If we only have domain information then put it in search too
-[ -z "${NEWSEARCH}" -a -z "${NEWNS}" ] && NEWSEARCH="${NEWDOMAIN}"
-
-for N in ${NEWSEARCH}; do
-       case " ${NEWSL} " in
-               *" ${N%,*} "*);;
-               *) NEWSL="${NEWSL} ${N%,*}";;
-       esac
-       case "\n${NEWRESOLV}\n" in
-               *"\nnameserver ${N#*,}\n"*);;
-               *) NEWRESOLV="${NEWRESOLV}nameserver ${N#*,}\n";;
-       esac
-done
-for N in ${NEWNS}; do
-       case "\n${NEWRESOLV}\n" in
-               *"\nnameserver ${N}\n");;
-               *) NEWRESOLV="${NEWRESOLV}nameserver ${N}\n";;
+for n in ${NAMESERVERS}; do
+       case "\n${newresolv}\n" in
+       *"\nnameserver ${n}\n");;
+       *) newresolv="${newresolv}nameserver ${n}\n";;
        esac
 done
-[ -n "${NEWSL}" ] && NEWRESOLV="${NEWRESOLV}search${NEWSL}\n"
 
-DBUSDEST=
-for DN in $(uniqify ${NEWDOMAIN}); do
-       if [ "${DBUS}" = "yes" ]; then
-               IP=${DN#*,}
-               SIFS=${IFS-y} OIFS=$IFS
-               IFS=.
-               set -- ${IP}
-               NUM="0x$(printf "%02x" $1 $2 $3 $4)"
-               if [ "${SIFS}" = "y" ]; then
-                       unset IFS
+dbusdest=
+for d in ${DOMAINS}; do
+       dn="${d%%:*}"
+       ns="${d#*:}"
+       while [ -n "${ns}" ]; do
+               if ${dbus}; then
+                       SIFS=${IFS-y} OIFS=$IFS
+                       IFS=.
+                       set -- ${ns%%,*}
+                       num="0x$(printf "%02x" $1 $2 $3 $4)"
+                       if [ "${SIFS}" = "y" ]; then
+                               unset IFS
+                       else
+                               IFS=$OIFS
+                       fi
+                       dbusdest="${dbusdest} uint32:$(printf "%u" ${num})"
+                       dbusdest="${dbusdest} string:${dn}"
                else
-                       IFS=$OIFS
+                       newconf="${newconf}server=/${db}/${ns%%,*}\n"
                fi
-               DBUSDEST="${DBUSDEST} uint32:$(printf "%d" ${NUM}) string:${DN%,*}"
-       else
-               NEWCONF="${NEWCONF}server=/${DN%,*}/${DN#*,}\n"
-       fi
+               [ "${ns}" = "${ns#*,}" ] && break
+               ns="${ns#*,}"
+       done
 done
 
-RELOAD="no"
-if [ -e "${DNSMASQCONF}" ]; then
-       if [ "$(cat "${DNSMASQCONF}")" != "$(printf "${NEWCONF}")" ]; then
-               RELOAD="yes"
-               printf "${NEWCONF}" > "${DNSMASQCONF}"
+changed=false
+if [ -f "${dnsmasq_conf}" ]; then
+       if [ "$(cat "${dnsmasq_conf}")" != "$(printf "${newconf}")" ]; then
+               changes=true
+               printf "${newconf}" > "${dnsmasq_conf}"
        fi
 else
-       RELOAD="yes"
-       printf "${NEWCONF}" > "${DNSMASQCONF}"
+       changed=true
+       printf "${newconf}" > "${dnsmasq_conf}"
 fi
-if [ -e "${DNSMASQRESOLV}" ]; then
-       if [ "$(cat "${DNSMASQRESOLV}")" != "$(printf "${NEWRESOLV}")" ]; then
-               RELOAD="yes"
-               printf "${NEWRESOLV}" > "${DNSMASQRESOLV}"
+if [ -f "${dnsmasq_resolv}" ]; then
+       if [ "$(cat "${dnsmasq_resolv}")" != "$(printf "${newresolv}")" ]; then
+               changed=true
+               printf "${newresolv}" > "${dnsmasq_resolv}"
        fi
 else
-       # dnsmasq polls this file so no need to set RELOAD="yes"
-       printf "${NEWRESOLV}" > "${DNSMASQRESOLV}"
+       # dnsmasq polls this file so no need to set changed=true
+       printf "${newresolv}" > "${dnsmasq_resolv}"
 fi
 
-[ "${RELOAD}" = "yes" ] && resolvconf -s dnsmasq restart
-if [ "${DBUS}" = "yes" ]; then
-       [ "${RELOAD}" != "yes" ] && kill -HUP $(cat ${DNSMASQPID})
+${changed} && resolvconf -s dnsmasq restart
+if ${dbus}; then
+       ${restart} || kill -HUP $(cat ${dnsmasqpid})
        # Send even if empty so old servers are cleared
        dbus-send --system --dest=uk.org.thekelleys.dnsmasq \
                /uk/org/thekelleys/dnsmasq uk.org.thekelleys.SetServers \
-               ${DBUSDEST}
+               ${dbusdest}
 fi