We need to process dynamic interfaces without metrics before ones
[openresolv] / dnsmasq.in
1 #!/bin/sh
2 # Copyright 2007-2009 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}" -o -z "${dnsmasq_resolv}" ] && exit 0
32 eval "$(${RESOLVCONF:-resolvconf} -v)"
33
34 PREFIX=@PREFIX@
35 DNSMASQPID="${dnsmasq_pid:-/var/run/dnsmasq.pid}"
36
37 NEWCONF="# Generated by resolvconf\n"
38 NEWRESOLV="${NEWCONF}"
39
40 # Using DBUS means that we never have to restart the daemon
41 # This is important as it means we should not drop DNS queries
42 # whilst changing DNS options around. However, DBUS support is optional
43 # so we need to validate a few things first.
44 # Check for DBus support in the binary
45
46 DBUS=no
47 DBUSPID="${dbus_pid:-/var/run/dbus/dbus.pid}"
48 [ -s "${DBUSPID}" ] || DBUSPID=/var/run/dbus.pid
49 [ -s "${DBUSPID}" ] || DBUSPID=/var/run/dbus/pid
50 if [ -s "${DBUSPID}" -a -s ${DNSMASQPID} ]; then
51         if dnsmasq --version 2>/dev/null | \
52                 grep -q "^Compile time options.*[[:space:]]DBus[[:space:]]"
53         then
54                 # Sanity - check that dnsmasq and dbus are running
55                 if kill -0 $(cat "${DBUSPID}") 2>/dev/null && \
56                         kill -0 $(cat ${DNSMASQPID}) 2>/dev/null
57                 then
58                         DBUS=yes
59                         NEWCONF="${NEWCONF}\n# Domain specific servers will"
60                         NEWCONF="${NEWCONF} be sent over dbus\nenable-dbus\n"
61                 fi
62         fi
63 fi
64
65 for N in ${NAMESERVERS}; do
66         case "\n${NEWRESOLV}\n" in
67         *"\nnameserver ${N}\n");;
68         *) NEWRESOLV="${NEWRESOLV}nameserver ${N}\n";;
69         esac
70 done
71
72 DBUSDEST=
73 for D in ${DOMAINS}; do
74         DN="${D%%:*}"
75         NS="${D#*:}"
76         while [ -n "${NS}" ]; do
77                 if [ "${DBUS}" = "yes" ]; then
78                         SIFS=${IFS-y} OIFS=$IFS
79                         IFS=.
80                         set -- ${NS%%,*}
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                 else
90                         NEWCONF="${NEWCONF}server=/${DN}/${NS%%,*}\n"
91                 fi
92                 [ "${NS}" = "${NS#*,}" ] && break
93                 NS="${NS#*,}"
94         done
95 done
96
97 RELOAD="no"
98 if [ -f "${dnsmasq_conf}" ]; then
99         if [ "$(cat "${dnsmasq_conf}")" != "$(printf "${NEWCONF}")" ]; then
100                 RELOAD="yes"
101                 printf "${NEWCONF}" > "${dnsmasq_conf}"
102         fi
103 else
104         RELOAD="yes"
105         printf "${NEWCONF}" > "${dnsmasq_conf}"
106 fi
107 if [ -f "${dnsmasq_resolv}" ]; then
108         if [ "$(cat "${dnsmasq_resolv}")" != "$(printf "${NEWRESOLV}")" ]; then
109                 RELOAD="yes"
110                 printf "${NEWRESOLV}" > "${dnsmasq_resolv}"
111         fi
112 else
113         # dnsmasq polls this file so no need to set RELOAD="yes"
114         printf "${NEWRESOLV}" > "${dnsmasq_resolv}"
115 fi
116
117 [ "${RELOAD}" = "yes" ] && resolvconf -s dnsmasq restart
118 if [ "${DBUS}" = "yes" ]; then
119         [ "${RELOAD}" != "yes" ] && kill -HUP $(cat ${DNSMASQPID})
120         # Send even if empty so old servers are cleared
121         dbus-send --system --dest=uk.org.thekelleys.dnsmasq \
122                 /uk/org/thekelleys/dnsmasq uk.org.thekelleys.SetServers \
123                 ${DBUSDEST}
124 fi