2 # Copyright 2006 Gentoo Foundation
3 # Copyright 2007 Roy Marples
6 # dnsmasq subscriber for resolvconf
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions
11 # * Redistributions of source code must retain the above copyright
12 # notice, this list of conditions and the following disclaimer.
13 # * Redistributions in binary form must reproduce the above
14 # copyright notice, this list of conditions and the following
15 # disclaimer in the documentation and/or other materials provided
16 # with the distribution.
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 # This is very important!
31 # We assume that we are a local dns cache - after all, why would a server
33 # Now that we have assumed this, we also assume that generic DHCP clients
34 # will enter their domains and search domains ONLY in the "search" field
35 # in their resolv.confs and VPN clients will put the domain they are for
36 # into the domain field only.
37 # This allows dnsmasq to forward domains for a specific VPN domain to the
38 # VPN nameserver and everything else to the standard name servers.
40 # A sample dnsmasq config that works as above is like so
43 #resolv-file=/etc/dnsmasq-resolv.conf
44 #conf-file=/etc/dnsmasq-resolvconf.conf
46 # The last step is to configure dns configuration for /etc/resolv.conf
47 # for the lo interface. In Gentoo we set it up like so in /etc/conf.d/net
48 #dns_servers_lo="127.0.0.1"
50 # Load our variables from resolvconf
51 VARS="$(resolvconf -v)"
55 DNSMASQRESOLV="${PREFIX}/etc/dnsmasq-resolv.conf"
56 DNSMASQCONF="${PREFIX}/etc/dnsmasq-resolvconf.conf"
58 NEWCONF="# Generated by resolvconf\n"
59 NEWRESOLV="${NEWCONF}"
61 # Using DBUS means that we never have to restart the daemon
62 # This is important as it means we should not drop DNS queries
63 # whilst changing DNS options around. However, DBUS support is optional
64 # so we need to validate a few things first.
65 # Check for DBus support in the binary
67 if [ -s "${PREFIX}"/var/run/dbus.pid -a -s "${PREFIX}"/var/run/dnsmasq.pid ]; then
68 if dnsmasq --version 2>/dev/null | \
69 grep -q "^Compile time options.*[[:space:]]DBus[[:space:]]" \
71 # Sanity - check that dnsmasq and dbus are running
72 if kill -0 $(cat "${PREFIX}"/var/run/dbus.pid) \
73 && kill -0 $(cat "${PREFIX}"/var/run/dnsmasq.pid); then
75 NEWCONF="${NEWCONF}\n# Domain specific servers will be sent over dbus\n\nenable-dbus\n"
85 *) result="${result} $1" ;;
92 # If we only have domain information then put it in search too
93 [ -z "${NEWSEARCH}" -a -z "${NEWNS}" ] && NEWSEARCH="${NEWDOMAIN}"
95 for N in ${NEWSEARCH}; do
98 *) NEWSL="${NEWSL} ${N%,*}" ;;
100 case "\n${NEWRESOLV}\n" in
101 *"\nnameserver ${N#*,}\n"*) ;;
102 *) NEWRESOLV="${NEWRESOLV}nameserver ${N#*,}\n" ;;
105 for N in ${NEWNS}; do
106 case "\n${NEWRESOLV}\n" in
107 *"\nnameserver ${N}\n") ;;
108 *) NEWRESOLV="${NEWRESOLV}nameserver ${N}\n" ;;
111 [ -n "${NEWSL}" ] && NEWRESOLV="${NEWRESOLV}search${NEWSL}\n"
114 for DN in $(uniqify ${NEWDOMAIN}) ; do
115 if [ "${DBUS}" = "yes" ]; then
117 SIFS=${IFS-y} OIFS=$IFS
120 NUM="0x$(printf "%02x" $1 $2 $3 $4)"
121 if [ "${SIFS}" = "y" ]; then
126 DBUSDEST="${DBUSDEST} uint32:$(printf "%d" ${NUM}) string:${DN%,*}"
128 NEWCONF="${NEWCONF}server=/${DN%,*}/${DN#*,}\n"
133 if [ -e "${DNSMASQCONF}" ]; then
134 if [ "$(cat "${DNSMASQCONF}")" != "$(printf "${NEWCONF}")" ]; then
136 printf "${NEWCONF}" > "${DNSMASQCONF}"
140 printf "${NEWCONF}" > "${DNSMASQCONF}"
142 if [ -e "${DNSMASQRESOLV}" ]; then
143 if [ "$(cat "${DNSMASQRESOLV}")" != "$(printf "${NEWRESOLV}")" ]; then
145 printf "${NEWRESOLV}" > "${DNSMASQRESOLV}"
148 # dnsmasq polls this file so no need to set RELOAD="yes"
149 printf "${NEWRESOLV}" > "${DNSMASQRESOLV}"
152 if [ "${RELOAD}" = "yes" ]; then
153 if [ -x "${PREFIX}"/etc/init.d/dnsmasq ]; then
154 /etc/init.d/dnsmasq --quiet --nodeps conditionalrestart
155 elif [ -x "${PREFIX}"/etc/rc.d/dnsmasq ]; then
156 "${PREFIX}"/etc/rc.d/dnsmasq restart
160 if [ "${DBUS}" = "yes" ]; then
161 if [ "${RELOAD}" != "yes" ]; then
162 if [ -x "${PREFIX}"/etc/init.d/dnsmasq ]; then
163 /etc/init.d/dnsmasq --quiet reload
164 elif [ -x "${PREFIX}"/etc/rc.d/dnsmasq ]; then
165 "${PREFIX}"/etc/rc.d/dnsmasq reload
168 # Send even if emtpy so old servers are cleared
169 dbus-send --system --dest=uk.org.thekelleys.dnsmasq \
170 /uk/org/thekelleys/dnsmasq uk.org.thekelleys.SetServers \