whitespace
[openresolv] / dnsmasq.in
1 #!/bin/sh
2 # Copyright 2007-2008 Roy Marples
3 # All rights reserved
4
5 # dnsmasq subscriber for resolvconf
6
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
9 # are met:
10 #     * Redistributions of source code must retain the above copyright
11 #       notice, this list of conditions and the following disclaimer.
12 #     * Redistributions in binary form must reproduce the above
13 #       copyright notice, this list of conditions and the following
14 #       disclaimer in the documentation and/or other materials provided
15 #       with the distribution.
16 #
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 # This is very important!
30 # We assume that we are a local dns cache - after all, why would a server
31 # use resolvconf?
32 # Now that we have assumed this, we also assume that generic DHCP clients
33 # will enter their domains and search domains ONLY in the "search" field
34 # in their resolv.confs and VPN clients will put the domain they are for
35 # into the domain field only.
36 # This allows dnsmasq to forward domains for a specific VPN domain to the
37 # VPN nameserver and everything else to the standard name servers.
38
39 # A sample dnsmasq config that works as above is like so.
40 # NOTE: The loopback interface on some systems maybe lo0.
41 #domain-needed
42 #interface=lo
43 #resolv-file=@PREFIX@/etc/dnsmasq-resolv.conf
44 #conf-file=@PREFIX@/etc/dnsmasq-resolvconf.conf
45
46 # The last step is to configure dns configuration for /etc/resolv.conf
47 # for the lo interface. You can do this in resolvconf as well by adding
48 #nameserver 127.0.0.1
49 # to resolv.conf.d/base
50
51 # Load our variables from resolvconf
52 VARS="$(resolvconf -v)"
53 eval "${VARS}"
54
55 PREFIX=@PREFIX@
56 DNSMASQRESOLV="${PREFIX}/etc/dnsmasq-resolv.conf"
57 DNSMASQCONF="${PREFIX}/etc/dnsmasq-resolvconf.conf"
58 DNSMASQPID=/var/run/dnsmasq.pid
59
60 NEWCONF="# Generated by resolvconf\n"
61 NEWRESOLV="${NEWCONF}"
62
63 # Using DBUS means that we never have to restart the daemon
64 # This is important as it means we should not drop DNS queries
65 # whilst changing DNS options around. However, DBUS support is optional
66 # so we need to validate a few things first.
67 # Check for DBus support in the binary
68 DBUS=no
69 DBUSPID=/var/run/dbus/dbus.pid
70 [ -s "${DBUSPID}" ] || DBUSPID=/var/run/dbus.pid
71 [ -s "${DBUSPID}" ] || DBUSPID=/var/run/dbus/pid
72 if [ -s "${DBUSPID}" -a -s ${DNSMASQPID} ]; then
73         if dnsmasq --version 2>/dev/null | \
74                 grep -q "^Compile time options.*[[:space:]]DBus[[:space:]]" \
75                 ; then
76                 # Sanity - check that dnsmasq and dbus are running
77                 if kill -0 $(cat "${DBUSPID}") 2>/dev/null \
78                         && kill -0 $(cat ${DNSMASQPID}) 2>/dev/null; then
79                         DBUS=yes
80                         NEWCONF="${NEWCONF}\n# Domain specific servers will be sent over dbus\nenable-dbus\n"
81                 fi
82         fi
83 fi
84
85 uniqify() {
86     local result=
87     while [ -n "$1" ]; do
88                 case " ${result} " in
89                         *" $1 "*);;
90                         *) result="${result} $1";;
91                 esac
92                 shift
93         done
94     echo "${result# *}"
95 }
96
97 # If we only have domain information then put it in search too
98 [ -z "${NEWSEARCH}" -a -z "${NEWNS}" ] && NEWSEARCH="${NEWDOMAIN}"
99
100 for N in ${NEWSEARCH}; do
101         case " ${NEWSL} " in
102                 *" ${N%,*} "*);;
103                 *) NEWSL="${NEWSL} ${N%,*}";;
104         esac
105         case "\n${NEWRESOLV}\n" in
106                 *"\nnameserver ${N#*,}\n"*);;
107                 *) NEWRESOLV="${NEWRESOLV}nameserver ${N#*,}\n";;
108         esac
109 done
110 for N in ${NEWNS}; do
111         case "\n${NEWRESOLV}\n" in
112                 *"\nnameserver ${N}\n");;
113                 *) NEWRESOLV="${NEWRESOLV}nameserver ${N}\n";;
114         esac
115 done
116 [ -n "${NEWSL}" ] && NEWRESOLV="${NEWRESOLV}search${NEWSL}\n"
117
118 DBUSDEST=
119 for DN in $(uniqify ${NEWDOMAIN}); do
120         if [ "${DBUS}" = "yes" ]; then
121                 IP=${DN#*,}
122                 SIFS=${IFS-y} OIFS=$IFS
123                 IFS=.
124                 set -- ${IP}
125                 NUM="0x$(printf "%02x" $1 $2 $3 $4)"
126                 if [ "${SIFS}" = "y" ]; then
127                         unset IFS
128                 else
129                         IFS=$OIFS
130                 fi
131                 DBUSDEST="${DBUSDEST} uint32:$(printf "%d" ${NUM}) string:${DN%,*}"
132         else
133                 NEWCONF="${NEWCONF}server=/${DN%,*}/${DN#*,}\n"
134         fi
135 done
136
137 RELOAD="no"
138 if [ -e "${DNSMASQCONF}" ]; then
139         if [ "$(cat "${DNSMASQCONF}")" != "$(printf "${NEWCONF}")" ]; then
140                 RELOAD="yes"
141                 printf "${NEWCONF}" > "${DNSMASQCONF}"
142         fi
143 else
144         RELOAD="yes"
145         printf "${NEWCONF}" > "${DNSMASQCONF}"
146 fi
147 if [ -e "${DNSMASQRESOLV}" ]; then
148         if [ "$(cat "${DNSMASQRESOLV}")" != "$(printf "${NEWRESOLV}")" ]; then
149                 RELOAD="yes"
150                 printf "${NEWRESOLV}" > "${DNSMASQRESOLV}"
151         fi
152 else
153         # dnsmasq polls this file so no need to set RELOAD="yes"
154         printf "${NEWRESOLV}" > "${DNSMASQRESOLV}"
155 fi
156
157 [ "${RELOAD}" = "yes" ] && resolvconf -s dnsmasq restart
158 if [ "${DBUS}" = "yes" ]; then
159         [ "${RELOAD}" != "yes" ] && kill -HUP $(cat ${DNSMASQPID})
160         # Send even if empty so old servers are cleared
161         dbus-send --system --dest=uk.org.thekelleys.dnsmasq \
162                 /uk/org/thekelleys/dnsmasq uk.org.thekelleys.SetServers \
163                 ${DBUSDEST}
164 fi