2 # Copyright (c) 2007-2016 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 newconf
="# Generated by resolvconf$NL"
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
50 dbus_introspect
=$
(dbus-send
--print-reply --system \
51 --dest=uk.org.thekelleys.dnsmasq \
52 /uk
/org
/thekelleys
/dnsmasq \
53 org.freedesktop.DBus.Introspectable.Introspect \
57 if printf %s
"$dbus_introspect" | \
58 grep -q '<method name="SetDomainServers">'
64 for n
in $NAMESERVERS; do
65 newresolv
="${newresolv}nameserver $n$NL"
74 while [ -n "$ns" ]; do
76 if $dbus && ! $dbus_ex; then
79 SIFS
=${IFS-y} OIFS
=$IFS
82 num
="0x$(printf %02x $1 $2 $3 $4)"
83 if [ "$SIFS" = y
]; then
88 dbusdest
="$dbusdest uint32:$(printf %u $num)"
89 dbusdest
="$dbusdest string:$dn"
92 # This version of dnsmasq won't accept
93 # scoped IPv6 addresses
97 SIFS
=${IFS-y} OIFS
=$IFS bytes
= front
= back
=
101 while [ -n "$1" -o -n "$2" ]; do
104 if [ -z "$addr" ]; then
109 while [ ${#addr} -lt 4 ]; do
112 byte1
="$(printf %d 0x${addr%??})"
113 byte2
="$(printf %d 0x${addr#??})"
115 back
="$back byte:$byte1 byte:$byte2"
117 front
="$front byte:$byte1 byte:$byte2"
120 while [ $i != 8 ]; do
122 front
="$front byte:0 byte:0"
124 front
="${front}$back"
125 if [ "$SIFS" = y
]; then
130 dbusdest
="${dbusdest}$front string:$dn"
139 dbusdest_ex
="$dbusdest_ex${dbusdest_ex:+,}/$dn/$n"
140 conf
="${conf}server=/$dn/$n$NL"
141 [ "$ns" = "${ns#*,}" ] && break
147 newconf
="$newconf$NL# Domain specific servers will"
148 newconf
="$newconf be sent over dbus${NL}"
150 newconf
="$newconf$conf"
153 # Try to ensure that config dirs exist
154 if type config_mkdirs
>/dev
/null
2>&1; then
155 config_mkdirs
"$dnsmasq_conf" "$dnsmasq_resolv"
157 @SBINDIR@
/resolvconf
-D "$dnsmasq_conf" "$dnsmasq_resolv"
161 if [ -n "$dnsmasq_conf" ]; then
162 if [ ! -f "$dnsmasq_conf" ] || \
163 [ "$(cat "$dnsmasq_conf")" != "$(printf %s "$newconf")" ]
166 printf %s
"$newconf" >"$dnsmasq_conf"
169 if [ -n "$dnsmasq_resolv" ]; then
170 # dnsmasq polls this file so no need to set changed=true
171 if [ -f "$dnsmasq_resolv" ]; then
172 if [ "$(cat "$dnsmasq_resolv")" != "$(printf %s "$newresolv")" ]
174 printf %s
"$newresolv" >"$dnsmasq_resolv"
177 printf %s
"$newresolv" >"$dnsmasq_resolv"
182 # dnsmasq does not re-read the configuration file on SIGHUP
183 if [ -n "$dnsmasq_restart" ]; then
184 eval $dnsmasq_restart
185 elif [ -n "$RESTARTCMD" ]; then
186 set -- ${dnsmasq_service}
189 @SBINDIR@
/resolvconf
-r ${dnsmasq_service}
193 if [ -s "$dnsmasq_pid" ]; then
194 $changed ||
kill -HUP $
(cat "$dnsmasq_pid")
196 # Send even if empty so old servers are cleared
198 method
=SetDomainServers
199 if [ -n "$dbusdest_ex" ]; then
200 dbusdest_ex
="array:string:$dbusdest_ex"
202 dbusdest
="$dbusdest_ex"
206 dbus-send
--system --dest=uk.org.thekelleys.dnsmasq \
207 /uk
/org
/thekelleys
/dnsmasq uk.org.thekelleys.
$method \