dbus pidfile location varies a little
[openresolv] / dnsmasq.in
1 #!/bin/sh
2 # Copyright 2006 Gentoo Foundation
3 # Copyright 2007 Roy Marples
4 # All rights reserved
5
6 # dnsmasq subscriber for resolvconf
7
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions
10 # are met:
11 #     * Redistributions of source code must retain the above copyright
12 #       notice, this list of conditions and the following disclaimer.
13 #     * Redistributions in binary form must reproduce the above
14 #       copyright notice, this list of conditions and the following
15 #       disclaimer in the documentation and/or other materials provided
16 #       with the distribution.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 # This is very important!
31 # We assume that we are a local dns cache - after all, why would a server
32 # use resolvconf?
33 # Now that we have assumed this, we also assume that generic DHCP clients
34 # will enter their domains and search domains ONLY in the "search" field
35 # in their resolv.confs and VPN clients will put the domain they are for
36 # into the domain field only.
37 # This allows dnsmasq to forward domains for a specific VPN domain to the
38 # VPN nameserver and everything else to the standard name servers.
39
40 # A sample dnsmasq config that works as above is like so
41 #domain-needed
42 #interface=lo
43 #resolv-file=/etc/dnsmasq-resolv.conf
44 #conf-file=/etc/dnsmasq-resolvconf.conf
45
46 # The last step is to configure dns configuration for /etc/resolv.conf
47 # for the lo interface. In Gentoo we set it up like so in /etc/conf.d/net
48 #dns_servers_lo="127.0.0.1"
49
50 # Load our variables from resolvconf
51 VARS="$(resolvconf -v)"
52 eval "${VARS}"
53
54 PREFIX=
55 DNSMASQRESOLV="${PREFIX}/etc/dnsmasq-resolv.conf"
56 DNSMASQCONF="${PREFIX}/etc/dnsmasq-resolvconf.conf"
57
58 NEWCONF="# Generated by resolvconf\n"
59 NEWRESOLV="${NEWCONF}"
60
61 # Using DBUS means that we never have to restart the daemon
62 # This is important as it means we should not drop DNS queries
63 # whilst changing DNS options around. However, DBUS support is optional
64 # so we need to validate a few things first.
65 # Check for DBus support in the binary
66 DBUS=no
67 dbuspid=/var/run/dbus/dbus.pid
68 [ -s "${dbuspid}" ] || dbuspid=/var/run/dbus.pid
69 if [ -s "${dbuspid}" -a -s /var/run/dnsmasq.pid ]; then
70         if dnsmasq --version 2>/dev/null | \
71                 grep -q "^Compile time options.*[[:space:]]DBus[[:space:]]" \
72                 ; then
73                 # Sanity - check that dnsmasq and dbus are running
74                 if kill -0 $(cat "${dbuspid}") \
75                         && kill -0 $(cat /var/run/dnsmasq.pid); then
76                         DBUS=yes
77                         NEWCONF="${NEWCONF}\n# Domain specific servers will be sent over dbus\nenable-dbus\n"
78                 fi
79         fi
80 fi
81
82 uniqify() {
83     local result=
84     while [ -n "$1" ]; do
85                 case " ${result} " in
86                         *" $1 "*);;
87                         *) result="${result} $1";;
88                 esac
89                 shift
90         done
91     echo "${result# *}"
92 }
93
94 # If we only have domain information then put it in search too
95 [ -z "${NEWSEARCH}" -a -z "${NEWNS}" ] && NEWSEARCH="${NEWDOMAIN}"
96
97 for N in ${NEWSEARCH}; do
98         case " ${NEWSL} " in
99                 *" ${N%,*} "*);;
100                 *) NEWSL="${NEWSL} ${N%,*}";;
101         esac
102         case "\n${NEWRESOLV}\n" in
103                 *"\nnameserver ${N#*,}\n"*);;
104                 *) NEWRESOLV="${NEWRESOLV}nameserver ${N#*,}\n";;
105         esac
106 done
107 for N in ${NEWNS}; do
108         case "\n${NEWRESOLV}\n" in
109                 *"\nnameserver ${N}\n");;
110                 *) NEWRESOLV="${NEWRESOLV}nameserver ${N}\n";;
111         esac
112 done
113 [ -n "${NEWSL}" ] && NEWRESOLV="${NEWRESOLV}search${NEWSL}\n"
114
115 DBUSDEST=
116 for DN in $(uniqify ${NEWDOMAIN}); do
117         if [ "${DBUS}" = "yes" ]; then
118                 IP=${DN#*,}
119                 SIFS=${IFS-y} OIFS=$IFS
120                 IFS=.
121                 set -- ${IP}
122                 NUM="0x$(printf "%02x" $1 $2 $3 $4)"
123                 if [ "${SIFS}" = "y" ]; then
124                         IFS=$OIFS
125                 else
126                         unset IFS
127                 fi
128                 DBUSDEST="${DBUSDEST} uint32:$(printf "%d" ${NUM}) string:${DN%,*}"
129         else
130                 NEWCONF="${NEWCONF}server=/${DN%,*}/${DN#*,}\n"
131         fi
132 done
133
134 RELOAD="no"
135 if [ -e "${DNSMASQCONF}" ]; then
136         if [ "$(cat "${DNSMASQCONF}")" != "$(printf "${NEWCONF}")" ]; then
137                 RELOAD="yes"
138                 printf "${NEWCONF}" > "${DNSMASQCONF}"
139         fi
140 else
141         RELOAD="yes"
142         printf "${NEWCONF}" > "${DNSMASQCONF}"
143 fi
144 if [ -e "${DNSMASQRESOLV}" ]; then
145         if [ "$(cat "${DNSMASQRESOLV}")" != "$(printf "${NEWRESOLV}")" ]; then
146                 RELOAD="yes"
147                 printf "${NEWRESOLV}" > "${DNSMASQRESOLV}"
148         fi
149 else
150         # dnsmasq polls this file so no need to set RELOAD="yes"
151         printf "${NEWRESOLV}" > "${DNSMASQRESOLV}"
152 fi
153
154 [ "${RELOAD}" = "yes" ] && resolvconf -s dnsmasq restart
155 if [ "${DBUS}" = "yes" ]; then
156         [ "${RELOAD}" != "yes" ] && resolvconf -s dnsmasq reload
157         # Send even if emtpy so old servers are cleared
158         dbus-send --system --dest=uk.org.thekelleys.dnsmasq \
159                 /uk/org/thekelleys/dnsmasq uk.org.thekelleys.SetServers \
160                 ${DBUSDEST}
161 fi
162
163 # vim: ts=4 :