Our test for SetDomainServers being in dnsmasq relies on dnsmasq having
[openresolv] / dnsmasq.in
1 #!/bin/sh
2 # Copyright (c) 2007-2012 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 [ -f "@SYSCONFDIR@"/resolvconf.conf ] || exit 0
30 . "@SYSCONFDIR@/resolvconf.conf" || exit 1
31 [ -z "$dnsmasq_conf" -a -z "$dnsmasq_resolv" ] && exit 0
32 [ -z "$RESOLVCONF" ] && eval "$(@PREFIX@/sbin/resolvconf -v)"
33 NL="
34 "
35
36 : ${dnsmasq_pid:=/var/run/dnsmasq.pid}
37 [ -s "$dnsmasq_pid" ] || dnsmasq_pid=/var/run/dnsmasq/dnsmasq.pid
38 : ${dnsmasq_service:=dnsmasq}
39 : ${dnsmasq_restart:=@RESTARTCMD ${dnsmasq_service}@}
40 newconf="# Generated by resolvconf$NL"
41 newresolv="$newconf"
42
43 # Using dbus means that we never have to restart the daemon
44 # This is important as it means we should not drop DNS queries
45 # whilst changing DNS options around. However, dbus support is optional
46 # so we need to validate a few things first.
47 # Check for DBus support in the binary
48 dbus=false
49 dbus_ex=false
50 : ${dbus_pid:=/var/run/dbus/dbus.pid}
51 [ -s "$dbus_pid" ] || dbus_pid=/var/run/dbus.pid
52 [ -s "$dbus_pid" ] || dbus_pid=/var/run/dbus/pid
53 if [ -s "$dbus_pid" -a -s "$dnsmasq_pid" ]; then
54         if dnsmasq --version 2>/dev/null | \
55                 grep -q "^Compile time options.*[[:space:]]DBus[[:space:]]"
56         then
57                 # Sanity - check that dnsmasq and dbus are running
58                 if kill -0 $(cat "$dbus_pid") 2>/dev/null && \
59                         kill -0 $(cat "$dnsmasq_pid") 2>/dev/null
60                 then
61                         dbus=true
62                         # Stupid test as it relies on dnsmasq running AND
63                         # being configured for DBus
64                         if dbus-send --print-reply --system --dest=uk.org.thekelleys.dnsmasq /uk/org/thekelleys/dnsmasq org.freedesktop.DBus.Introspectable.Introspect 2>/dev/null | grep -q '<method name="SetDomainServers">'
65                         then
66                                 dbus_ex=true
67                         fi
68                 fi
69         fi
70 fi
71
72 for n in $NAMESERVERS; do
73         newresolv="${newresolv}nameserver $n$NL"
74 done
75
76 dbusdest=
77 dbusdest_ex=
78 conf=
79 for d in $DOMAINS; do
80         dn="${d%%:*}"
81         ns="${d#*:}"
82         while [ -n "$ns" ]; do
83                 n="${ns%%,*}"
84                 if $dbus && ! $dbus_ex; then
85                         case "$n" in
86                         *.*.*.*)
87                                 SIFS=${IFS-y} OIFS=$IFS
88                                 IFS=.
89                                 set -- $n
90                                 num="0x$(printf %02x $1 $2 $3 $4)"
91                                 if [ "$SIFS" = y ]; then
92                                         unset IFS
93                                 else
94                                         IFS=$OIFS
95                                 fi
96                                 dbusdest="$dbusdest uint32:$(printf %u $num)"
97                                 dbusdest="$dbusdest string:$dn"
98                                 ;;
99                         *:*%*)
100                                 # This version of dnsmasq won't accept
101                                 # scoped IPv6 addresses
102                                 dbus=false
103                                 ;;
104                         *:*)
105                                 SIFS=${IFS-y} OIFS=$IFS bytes= front= back=
106                                 empty=false i=0
107                                 IFS=:
108                                 set -- $n
109                                 while [ -n "$1" -o -n "$2" ]; do
110                                         addr="$1"
111                                         shift
112                                         if [ -z "$addr" ]; then
113                                                 empty=true
114                                                 continue
115                                         fi
116                                         i=$(($i + 1))
117                                         while [ ${#addr} -lt 4 ]; do
118                                                 addr="0${addr}"
119                                         done
120                                         byte1="$(printf %d 0x${addr%??})"
121                                         byte2="$(printf %d 0x${addr#??})"
122                                         if $empty; then
123                                                 back="$back byte:$byte1 byte:$byte2"
124                                         else
125                                                 front="$front byte:$byte1 byte:$byte2"
126                                         fi
127                                 done
128                                 while [ $i != 8 ]; do
129                                 i=$(($i + 1))
130                                         front="$front byte:0 byte:0"
131                                 done
132                                 front="${front}$back"
133                                 if [ "$SIFS" = y ]; then
134                                         unset IFS
135                                 else
136                                         IFS=$OIFS
137                                 fi
138                                 dbusdest="${dbusdest}$front string:$dn"
139                                 ;;
140                         *)
141                                 if ! $dbus_ex; then
142                                         dbus=false
143                                 fi
144                                 ;;
145                         esac
146                 fi
147                 dbusdest_ex="$dbusdest_ex${dbusdest_ex:+,}/$dn/$n"
148                 conf="${conf}server=/$dn/$n$NL"
149                 [ "$ns" = "${ns#*,}" ] && break
150                 ns="${ns#*,}"
151         done
152 done
153
154 if $dbus; then
155         newconf="$newconf$NL# Domain specific servers will"
156         newconf="$newconf be sent over dbus${NL}enable-dbus$NL"
157 else
158         newconf="$newconf$conf"
159 fi
160
161 # Try to ensure that config dirs exist
162 if type config_mkdirs >/dev/null 2>&1; then
163         config_mkdirs "$dnsmasq_conf" "$dnsmasq_resolv"
164 else
165         @PREFIX@/sbin/resolvconf -D "$dnsmasq_conf" "$dnsmasq_resolv"
166 fi
167
168 changed=false
169 if [ -n "$dnsmasq_conf" ]; then
170         if [ ! -f "$dnsmasq_conf" ] || \
171                 [ "$(cat "$dnsmasq_conf")" != "$(printf %s "$newconf")" ]
172         then
173                 changed=true
174                 printf %s "$newconf" >"$dnsmasq_conf"
175         fi
176 fi
177 if [ -n "$dnsmasq_resolv" ]; then
178         # dnsmasq polls this file so no need to set changed=true
179         if [ -f "$dnsmasq_resolv" ]; then
180                 if [ "$(cat "$dnsmasq_resolv")" != "$(printf %s "$newresolv")" ]
181                 then
182                         printf %s "$newresolv" >"$dnsmasq_resolv"
183                 fi
184         else
185                 printf %s "$newresolv" >"$dnsmasq_resolv"
186         fi
187 fi
188
189 if $changed; then
190         eval $dnsmasq_restart
191 fi
192 if $dbus; then
193         $changed || kill -HUP $(cat "$dnsmasq_pid")
194         # Send even if empty so old servers are cleared
195         if $dbus_ex; then
196                 method=SetDomainServers
197                 if [ -n "$dbusdest_ex" ]; then
198                         dbusdest_ex="array:string:$dbusdest_ex"
199                 fi
200                 dbusdest="$dbusdest_ex"
201         else
202                 method=SetServers
203         fi
204         dbus-send --system --dest=uk.org.thekelleys.dnsmasq \
205                 /uk/org/thekelleys/dnsmasq uk.org.thekelleys.$method \
206                 $dbusdest
207 fi