Send dnsmasq IPv6 addresses constructed like A:B::C
[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_pid:=/var/run/dbus/dbus.pid}
50 [ -s "$dbus_pid" ] || dbus_pid=/var/run/dbus.pid
51 [ -s "$dbus_pid" ] || dbus_pid=/var/run/dbus/pid
52 if [ -s "$dbus_pid" -a -s "$dnsmasq_pid" ]; then
53         if dnsmasq --version 2>/dev/null | \
54                 grep -q "^Compile time options.*[[:space:]]DBus[[:space:]]"
55         then
56                 # Sanity - check that dnsmasq and dbus are running
57                 if kill -0 $(cat "$dbus_pid") 2>/dev/null && \
58                         kill -0 $(cat "$dnsmasq_pid") 2>/dev/null
59                 then
60                         dbus=true
61                 fi
62         fi
63 fi
64
65 for n in $NAMESERVERS; do
66         newresolv="${newresolv}nameserver $n$NL"
67 done
68
69 dbusdest=
70 conf=
71 for d in $DOMAINS; do
72         dn="${d%%:*}"
73         ns="${d#*:}"
74         while [ -n "$ns" ]; do
75                 n="${ns%%,*}"
76                 case "$n" in
77                 *.*.*.*)
78                         SIFS=${IFS-y} OIFS=$IFS
79                         IFS=.
80                         set -- $n
81                         num="0x$(printf %02x $1 $2 $3 $4)"
82                         if [ "$SIFS" = y ]; then
83                                 unset IFS
84                         else
85                                 IFS=$OIFS
86                         fi
87                         dbusdest="$dbusdest uint32:$(printf %u $num)"
88                         dbusdest="$dbusdest string:$dn"
89                         ;;
90                 *:*)
91                         SIFS=${IFS-y} OIFS=$IFS bytes= front= back=
92                         empty=false i=0
93                         IFS=:
94                         set -- $n
95                         while [ -n "$1" -o -n "$2" ]; do
96                                 addr="$1"
97                                 shift
98                                 if [ -z "$addr" ]; then
99                                         empty=true
100                                         continue
101                                 fi
102                                 i=$(($i + 1))
103                                 while [ ${#addr} -lt 4 ]; do
104                                         addr="0${addr}"
105                                 done
106                                 byte1="$(printf %d 0x${addr%??})"
107                                 byte2="$(printf %d 0x${addr#??})"
108                                 if $empty; then
109                                         back="$back byte:$byte1 byte:$byte2"
110                                 else
111                                         front="$front byte:$byte1 byte:$byte2"
112                                 fi
113                         done
114                         while [ $i != 8 ]; do
115                                 i=$(($i + 1))
116                                 front="$front byte:0 byte:0"
117                         done
118                         front="${front}$back"
119                         if [ "$SIFS" = y ]; then
120                                 unset IFS
121                         else
122                                 IFS=$OIFS
123                         fi
124                         dbusdest="${dbusdest}$front string:$dn"
125                         ;;
126                 *)
127                         dbus=false
128                         ;;
129                 esac
130                 conf="${conf}server=/$dn/$n$NL"
131                 [ "$ns" = "${ns#*,}" ] && break
132                 ns="${ns#*,}"
133         done
134 done
135
136 if $dbus; then
137         newconf="$newconf$NL# Domain specific servers will"
138         newconf="$newconf be sent over dbus${NL}enable-dbus$NL"
139 else
140         newconf="$newconf$conf"
141 fi
142
143 # Try to ensure that config dirs exist
144 if type config_mkdirs >/dev/null 2>&1; then
145         config_mkdirs "$dnsmasq_conf" "$dnsmasq_resolv"
146 else
147         @PREFIX@/sbin/resolvconf -D "$dnsmasq_conf" "$dnsmasq_resolv"
148 fi
149
150 changed=false
151 if [ -n "$dnsmasq_conf" ]; then
152         if [ ! -f "$dnsmasq_conf" ] || \
153                 [ "$(cat "$dnsmasq_conf")" != "$(printf %s "$newconf")" ]
154         then
155                 changed=true
156                 printf %s "$newconf" >"$dnsmasq_conf"
157         fi
158 fi
159 if [ -n "$dnsmasq_resolv" ]; then
160         # dnsmasq polls this file so no need to set changed=true
161         if [ -f "$dnsmasq_resolv" ]; then
162                 if [ "$(cat "$dnsmasq_resolv")" != "$(printf %s "$newresolv")" ]
163                 then
164                         printf %s "$newresolv" >"$dnsmasq_resolv"
165                 fi
166         else
167                 printf %s "$newresolv" >"$dnsmasq_resolv"
168         fi
169 fi
170
171 if $changed; then
172         eval $dnsmasq_restart
173 fi
174 if $dbus; then
175         $changed || kill -HUP $(cat "$dnsmasq_pid")
176         # Send even if empty so old servers are cleared
177         dbus-send --system --dest=uk.org.thekelleys.dnsmasq \
178                 /uk/org/thekelleys/dnsmasq uk.org.thekelleys.SetServers \
179                 $dbusdest
180 fi