2 # Copyright (c) 2007-2012 Roy Marples
5 # dnsmasq subscriber for resolvconf
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
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.
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.
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 "$(@SBINDIR@/resolvconf -v)"
36 : ${dnsmasq_pid:=/var/run/dnsmasq.pid}
37 [ -s "$dnsmasq_pid" ] || dnsmasq_pid
=/var
/run
/dnsmasq
/dnsmasq.pid
38 [ -s "$dnsmasq_pid" ] ||
unset dnsmasq_pid
39 : ${dnsmasq_service:=dnsmasq}
40 : ${dnsmasq_restart:=@RESTARTCMD ${dnsmasq_service}@}
41 newconf
="# Generated by resolvconf$NL"
44 # Using dbus means that we never have to restart the daemon
45 # This is important as it means we should not drop DNS queries
46 # whilst changing DNS options around. However, dbus support is optional
47 # so we need to validate a few things first.
48 # Check for DBus support in the binary
51 dbus_introspect
=$
(dbus-send
--print-reply --system \
52 --dest=uk.org.thekelleys.dnsmasq \
53 /uk
/org
/thekelleys
/dnsmasq \
54 org.freedesktop.DBus.Introspectable.Introspect \
58 if printf %s
"$dbus_introspect" | \
59 grep -q '<method name="SetDomainServers">'
65 for n
in $NAMESERVERS; do
66 newresolv
="${newresolv}nameserver $n$NL"
75 while [ -n "$ns" ]; do
77 if $dbus && ! $dbus_ex; then
80 SIFS
=${IFS-y} OIFS
=$IFS
83 num
="0x$(printf %02x $1 $2 $3 $4)"
84 if [ "$SIFS" = y
]; then
89 dbusdest
="$dbusdest uint32:$(printf %u $num)"
90 dbusdest
="$dbusdest string:$dn"
93 # This version of dnsmasq won't accept
94 # scoped IPv6 addresses
98 SIFS
=${IFS-y} OIFS
=$IFS bytes
= front
= back
=
102 while [ -n "$1" -o -n "$2" ]; do
105 if [ -z "$addr" ]; then
110 while [ ${#addr} -lt 4 ]; do
113 byte1
="$(printf %d 0x${addr%??})"
114 byte2
="$(printf %d 0x${addr#??})"
116 back
="$back byte:$byte1 byte:$byte2"
118 front
="$front byte:$byte1 byte:$byte2"
121 while [ $i != 8 ]; do
123 front
="$front byte:0 byte:0"
125 front
="${front}$back"
126 if [ "$SIFS" = y
]; then
131 dbusdest
="${dbusdest}$front string:$dn"
140 dbusdest_ex
="$dbusdest_ex${dbusdest_ex:+,}/$dn/$n"
141 conf
="${conf}server=/$dn/$n$NL"
142 [ "$ns" = "${ns#*,}" ] && break
148 newconf
="$newconf$NL# Domain specific servers will"
149 newconf
="$newconf be sent over dbus${NL}"
151 newconf
="$newconf$conf"
154 # Try to ensure that config dirs exist
155 if type config_mkdirs
>/dev
/null
2>&1; then
156 config_mkdirs
"$dnsmasq_conf" "$dnsmasq_resolv"
158 @SBINDIR@
/resolvconf
-D "$dnsmasq_conf" "$dnsmasq_resolv"
162 if [ -n "$dnsmasq_conf" ]; then
163 if [ ! -f "$dnsmasq_conf" ] || \
164 [ "$(cat "$dnsmasq_conf")" != "$(printf %s "$newconf")" ]
167 printf %s
"$newconf" >"$dnsmasq_conf"
170 if [ -n "$dnsmasq_resolv" ]; then
171 # dnsmasq polls this file so no need to set changed=true
172 if [ -f "$dnsmasq_resolv" ]; then
173 if [ "$(cat "$dnsmasq_resolv")" != "$(printf %s "$newresolv")" ]
175 printf %s
"$newresolv" >"$dnsmasq_resolv"
178 printf %s
"$newresolv" >"$dnsmasq_resolv"
183 eval $dnsmasq_restart
186 if [ -s "$dnsmasq_pid" ]; then
187 $changed ||
kill -HUP $
(cat "$dnsmasq_pid")
189 # Send even if empty so old servers are cleared
191 method
=SetDomainServers
192 if [ -n "$dbusdest_ex" ]; then
193 dbusdest_ex
="array:string:$dbusdest_ex"
195 dbusdest
="$dbusdest_ex"
199 dbus-send
--system --dest=uk.org.thekelleys.dnsmasq \
200 /uk
/org
/thekelleys
/dnsmasq uk.org.thekelleys.
$method \