whitespace
[openresolv] / dnsmasq.in
1 #!/bin/sh
2 # Copyright 2006 Gentoo Foundation
3 # Copyright 2007 Roy Marples
4 # All rights reserved
5
6 # dnsmasq subscriber for resolvconf
7
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions
10 # are met:
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.
17 #
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.
29
30 # This is very important!
31 # We assume that we are a local dns cache - after all, why would a server
32 # use resolvconf?
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.
39
40 # A sample dnsmasq config that works as above is like so
41 #domain-needed
42 #interface=lo
43 #resolv-file=/etc/dnsmasq-resolv.conf
44 #conf-file=/etc/dnsmasq-resolvconf.conf
45
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"
49
50 # Load our variables from resolvconf
51 VARS="$(resolvconf -v)"
52 eval "${VARS}"
53
54 PREFIX=
55 DNSMASQRESOLV="${PREFIX}/etc/dnsmasq-resolv.conf"
56 DNSMASQCONF="${PREFIX}/etc/dnsmasq-resolvconf.conf"
57
58 NEWCONF="# Generated by resolvconf\n"
59 NEWRESOLV="${NEWCONF}"
60
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
66 DBUS=no
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:]]" \
70                 ; then
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
74                         DBUS=yes
75                         NEWCONF="${NEWCONF}\n# Domain specific servers will be sent over dbus\n\nenable-dbus\n"
76                 fi
77         fi
78 fi
79
80 uniqify() {
81     local result=
82     while [ -n "$1" ]; do
83                 case " ${result} " in
84                         *" $1 "*);;
85                         *) result="${result} $1";;
86                 esac
87                 shift
88         done
89     echo "${result# *}"
90 }
91
92 # If we only have domain information then put it in search too
93 [ -z "${NEWSEARCH}" -a -z "${NEWNS}" ] && NEWSEARCH="${NEWDOMAIN}"
94
95 for N in ${NEWSEARCH}; do
96         case " ${NEWSL} " in
97                 *" ${N%,*} "*);;
98                 *) NEWSL="${NEWSL} ${N%,*}";;
99         esac
100         case "\n${NEWRESOLV}\n" in
101                 *"\nnameserver ${N#*,}\n"*);;
102                 *) NEWRESOLV="${NEWRESOLV}nameserver ${N#*,}\n";;
103         esac
104 done
105 for N in ${NEWNS}; do
106         case "\n${NEWRESOLV}\n" in
107                 *"\nnameserver ${N}\n");;
108                 *) NEWRESOLV="${NEWRESOLV}nameserver ${N}\n";;
109         esac
110 done
111 [ -n "${NEWSL}" ] && NEWRESOLV="${NEWRESOLV}search${NEWSL}\n"
112
113 DBUSDEST=
114 for DN in $(uniqify ${NEWDOMAIN}); do
115         if [ "${DBUS}" = "yes" ]; then
116                 IP=${DN#*,}
117                 SIFS=${IFS-y} OIFS=$IFS
118                 IFS=.
119                 set -- ${IP}
120                 NUM="0x$(printf "%02x" $1 $2 $3 $4)"
121                 if [ "${SIFS}" = "y" ]; then
122                         IFS=$OIFS
123                 else
124                         unset IFS
125                 fi
126                 DBUSDEST="${DBUSDEST} uint32:$(printf "%d" ${NUM}) string:${DN%,*}"
127         else
128                 NEWCONF="${NEWCONF}server=/${DN%,*}/${DN#*,}\n"
129         fi
130 done
131
132 RELOAD="no"
133 if [ -e "${DNSMASQCONF}" ]; then
134         if [ "$(cat "${DNSMASQCONF}")" != "$(printf "${NEWCONF}")" ]; then
135                 RELOAD="yes"
136                 printf "${NEWCONF}" > "${DNSMASQCONF}"
137         fi
138 else
139         RELOAD="yes"
140         printf "${NEWCONF}" > "${DNSMASQCONF}"
141 fi
142 if [ -e "${DNSMASQRESOLV}" ]; then
143         if [ "$(cat "${DNSMASQRESOLV}")" != "$(printf "${NEWRESOLV}")" ]; then
144                 RELOAD="yes"
145                 printf "${NEWRESOLV}" > "${DNSMASQRESOLV}"
146         fi
147 else
148         # dnsmasq polls this file so no need to set RELOAD="yes"
149         printf "${NEWRESOLV}" > "${DNSMASQRESOLV}"
150 fi
151
152 [ "${RELOAD}" = "yes" ] && resolvconf -s dnsmasq restart
153 if [ "${DBUS}" = "yes" ]; then
154         [ "${RELOAD}" != "yes" ] && resolvconf -s dnsmasq reload
155         # Send even if emtpy so old servers are cleared
156         dbus-send --system --dest=uk.org.thekelleys.dnsmasq \
157                 /uk/org/thekelleys/dnsmasq uk.org.thekelleys.SetServers \
158                 ${DBUSDEST}
159 fi
160
161 # vim: ts=4 :