Add the dnsmasq subscriber.
authorRoy Marples <roy@marples.name>
Fri, 9 Nov 2007 23:18:05 +0000 (23:18 +0000)
committerRoy Marples <roy@marples.name>
Fri, 9 Nov 2007 23:18:05 +0000 (23:18 +0000)
Makefile
dnsmasq [new file with mode: 0755]

index d0ae99fa272b73f5c5e260ae3b293d5e5ad98ab4..dd927fd79e5eb6fac7be4f8916a5888bd63eae83 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,3 +1,4 @@
+VERSION = 1.0
 DESTDIR =
 PREFIX = /
 ROOT = $(DESTDIR)$(PREFIX)
@@ -9,7 +10,7 @@ BINDIR = $(ROOT)/sbin
 VARDIR = $(DESTDIR)/var/run
 UPDATEDIR = $(ETCDIR)/update.d
 
-default:
+.PHONY: all default clean
 
 install:
        $(INSTALL) -d $(ETCDIR)/resolv.conf.d
@@ -19,11 +20,20 @@ install:
        $(INSTALL) -d $(BINDIR)
        $(INSTALL) -d $(VARDIR)/resolvconf
        $(INSTALL) resolvconf $(BINDIR)
-       $(INSTALL) libc $(UPDATEDIR)
+       $(INSTALL) libc dnsmasq $(UPDATEDIR)
        if test "$(PREFIX)" "!=" "/"; then \
-               for x in $(BINDIR)/resolvconf $(UPDATEDIR)/libc; do \
+               for x in $(BINDIR)/resolvconf $(UPDATEDIR)/libc $(UPDATEDIR)/dnsmasq; do \
                sed -i.bak -e s':^PREFIX=.*:PREFIX="$(PREFIX)":' "$$x"; rm "$$x".bak; \
                done; \
                fi;
        $(INSTALL) -m 644 resolvconf.8 $(MANDIR)
        ln -snf /var/run/resolvconf $(ETCDIR)/run
+
+dist:
+       $(INSTALL) -m 0755 -d /tmp/openresolv-$(VERSION)
+       cp -RPp . /tmp/openresolv-$(VERSION)
+       (cd /tmp/openresolv-$(VERSION); $(MAKE) clean)
+       rm -rf /tmp/openresolv-$(VERSION)/*.bz2 /tmp/openresolv-$(VERSION)/.git
+       tar cvjpf openresolv-$(VERSION).tar.bz2 -C /tmp openresolv-$(VERSION)
+       rm -rf /tmp/openresolv-$(VERSION)
+       ls -l openresolv-$(VERSION).tar.bz2
diff --git a/dnsmasq b/dnsmasq
new file mode 100755 (executable)
index 0000000..604ab89
--- /dev/null
+++ b/dnsmasq
@@ -0,0 +1,174 @@
+#!/bin/sh
+# Copyright 2006 Gentoo Foundation
+# Copyright 2007 Roy Marples
+
+# dnsmasq-resolv.conf updater
+
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are met:
+# 
+#    * Redistributions of source code must retain the above copyright
+#      notice, this list of conditions and the following disclaimer.
+#    * Redistributions in binary form must reproduce the above copyright
+#      notice, this list of conditions and the following disclaimer in the
+#      documentation and/or other materials provided with the distribution.
+#    * Neither the name of the above listed copyright holder nor the
+#      names of its contributors may be used to endorse or promote products
+#      derived from this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (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
+#domain-needed
+#interface=lo
+#resolv-file=/etc/dnsmasq-resolv.conf
+#conf-file=/etc/dnsmasq-resolvconf.conf
+
+# The last step is to configure dns configuration for /etc/resolv.conf
+# for the lo interface. In Gentoo we set it up like so in /etc/conf.d/net
+#dns_servers_lo="127.0.0.1"
+
+# Load our variables from resolvconf
+VARS="$(resolvconf -v)"
+eval "${VARS}"
+
+PREFIX=
+DNSMASQRESOLV="${PREFIX}/etc/dnsmasq-resolv.conf"
+DNSMASQCONF="${PREFIX}/etc/dnsmasq-resolvconf.conf"
+
+NEWCONF="# Generated by resolvconf\n"
+NEWRESOLV="${NEWCONF}"
+
+# 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
+# so we need to validate a few things first.
+# Check for DBus support in the binary
+DBUS=no
+if [ -s "${PREFIX}"/var/run/dbus.pid -a "${PREFIX}"/var/run/dnsmasq.pid ]; then
+       if dnsmasq --version 2>/dev/null | \
+               grep -q "^Compile time options.*[[:space:]]DBus[[:space:]]" \
+               ; then
+               # Sanity - check that dnsmasq and dbus are running
+               if kill -0 $(cat "${PREFIX}"/var/run/dbus.pid) \
+                       && kill -0 $(cat "${PREFIX}"/var/run/dnsmasq.pid); then
+                       DBUS=yes
+                       NEWCONF="${NEWCONF}\n# Domain specific servers will be sent over dbus\n\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" ;;
+       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
+                       IFS=$OIFS
+               else
+                       unset IFS
+               fi
+               DBUSDEST="${DBUSDEST} uint32:$(printf "%d" ${NUM}) string:${DN%,*}"
+       else
+               NEWCONF="${NEWCONF}server=/${DN%,*}/${DN#*,}\n"
+       fi
+done
+
+RELOAD="no"
+if [ -e "${DNSMASQCONF}" ]; then
+       if [ "$(cat "${DNSMASQCONF}")" != "$(printf "${NEWCONF}")" ]; then
+               RELOAD="yes"
+               printf "${NEWCONF}" > "${DNSMASQCONF}"
+       fi
+else
+       RELOAD="yes"
+       printf "${NEWCONF}" > "${DNSMASQCONF}"
+fi
+if [ -e "${DNSMASQRESOLV}" ]; then
+       if [ "$(cat "${DNSMASQRESOLV}")" != "$(printf "${NEWRESOLV}")" ]; then
+               RELOAD="yes"
+               printf "${NEWRESOLV}" > "${DNSMASQRESOLV}"
+       fi
+else
+       # dnsmasq polls this file so no need to set RELOAD="yes"
+       printf "${NEWRESOLV}" > "${DNSMASQRESOLV}"
+fi
+
+if [ "${RELOAD}" = "yes" ]; then
+       if [ -x "${PREFIX}"/etc/init.d/dnsmasq ]; then
+               /etc/init.d/dnsmasq --quiet --nodeps conditionalrestart
+       elif [ -x "${PREFIX}"/etc/rc.d/dnsmasq ]; then
+               "${PREFIX}"/etc/rc.d/dnsmasq restart
+       fi
+fi
+
+if [ "${DBUS}" = "yes" ]; then
+       if [ "${RELOAD}" != "yes" ]; then
+               if [ -x "${PREFIX}"/etc/init.d/dnsmasq ]; then
+                       /etc/init.d/dnsmasq --quiet reload
+               elif [ -x "${PREFIX}"/etc/rc.d/dnsmasq ]; then
+                       "${PREFIX}"/etc/rc.d/dnsmasq reload
+               fi
+       fi
+       # Send even if emtpy so old servers are cleared
+       dbus-send --system --dest=uk.org.thekelleys.dnsmasq \
+               /uk/org/thekelleys/dnsmasq uk.org.thekelleys.SetServers \
+               ${DBUSDEST}
+fi
+
+# vim: ts=4 :