Move subscribers to libexec as they really don't belong in /etc
[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 # A sample dnsmasq config that works as above is like so.
30 # NOTE: The loopback interface on some systems maybe lo0.
31 #domain-needed
32 #interface=lo
33 #resolv-file=@PREFIX@/etc/dnsmasq-resolv.conf
34 #conf-file=@PREFIX@/etc/dnsmasq-resolvconf.conf
35
36 # The last step is to configure dns configuration for /etc/resolv.conf
37 # for the lo interface. You can do this in resolvconf as well by adding
38 #nameserver 127.0.0.1
39 # to @SYSCONFDIR@/resolv.conf.d/base
40
41 # Load our variables from resolvconf
42 eval "$(resolvconf -v)"
43
44 PREFIX=@PREFIX@
45 DNSMASQRESOLV="${PREFIX}/etc/dnsmasq-resolv.conf"
46 DNSMASQCONF="${PREFIX}/etc/dnsmasq-resolvconf.conf"
47 DNSMASQPID=/var/run/dnsmasq.pid
48
49 NEWCONF="# Generated by resolvconf\n"
50 NEWRESOLV="${NEWCONF}"
51
52 # Using DBUS means that we never have to restart the daemon
53 # This is important as it means we should not drop DNS queries
54 # whilst changing DNS options around. However, DBUS support is optional
55 # so we need to validate a few things first.
56 # Check for DBus support in the binary
57
58 DBUS=no
59 DBUSPID=/var/run/dbus/dbus.pid
60 [ -s "${DBUSPID}" ] || DBUSPID=/var/run/dbus.pid
61 [ -s "${DBUSPID}" ] || DBUSPID=/var/run/dbus/pid
62 if [ -s "${DBUSPID}" -a -s ${DNSMASQPID} ]; then
63         if dnsmasq --version 2>/dev/null | \
64                 grep -q "^Compile time options.*[[:space:]]DBus[[:space:]]" \
65                 ; then
66                 # Sanity - check that dnsmasq and dbus are running
67                 if kill -0 $(cat "${DBUSPID}") 2>/dev/null \
68                         && kill -0 $(cat ${DNSMASQPID}) 2>/dev/null; then
69                         DBUS=yes
70                         NEWCONF="${NEWCONF}\n# Domain specific servers will be sent over dbus\nenable-dbus\n"
71                 fi
72         fi
73 fi
74
75 for N in ${NAMESERVERS}; do
76         case "\n${NEWRESOLV}\n" in
77                 *"\nnameserver ${N}\n");;
78                 *) NEWRESOLV="${NEWRESOLV}nameserver ${N}\n";;
79         esac
80 done
81
82 DBUSDEST=
83 for D in ${DOMAINS}; do
84         DN="${D%%:*}"
85         NS="${D#*:}"
86         while [ -n "${NS}" ]; do
87                 if [ "${DBUS}" = "yes" ]; then
88                         SIFS=${IFS-y} OIFS=$IFS
89                         IFS=.
90                         set -- ${NS%%,*}
91                         NUM="0x$(printf "%02x" $1 $2 $3 $4)"
92                         if [ "${SIFS}" = "y" ]; then
93                                 unset IFS
94                         else
95                                 IFS=$OIFS
96                         fi
97                         DBUSDEST="${DBUSDEST} uint32:$(printf "%u" ${NUM}) string:${DN}"
98                 else
99                         NEWCONF="${NEWCONF}server=/${DN}/${NS%%,*}\n"
100                 fi
101                 [ "${NS}" = "${NS#*,}" ] && break
102                 NS="${NS#*,}"
103         done
104 done
105
106 RELOAD="no"
107 if [ -e "${DNSMASQCONF}" ]; then
108         if [ "$(cat "${DNSMASQCONF}")" != "$(printf "${NEWCONF}")" ]; then
109                 RELOAD="yes"
110                 printf "${NEWCONF}" > "${DNSMASQCONF}"
111         fi
112 else
113         RELOAD="yes"
114         printf "${NEWCONF}" > "${DNSMASQCONF}"
115 fi
116 if [ -e "${DNSMASQRESOLV}" ]; then
117         if [ "$(cat "${DNSMASQRESOLV}")" != "$(printf "${NEWRESOLV}")" ]; then
118                 RELOAD="yes"
119                 printf "${NEWRESOLV}" > "${DNSMASQRESOLV}"
120         fi
121 else
122         # dnsmasq polls this file so no need to set RELOAD="yes"
123         printf "${NEWRESOLV}" > "${DNSMASQRESOLV}"
124 fi
125
126 [ "${RELOAD}" = "yes" ] && resolvconf -s dnsmasq restart
127 if [ "${DBUS}" = "yes" ]; then
128         [ "${RELOAD}" != "yes" ] && kill -HUP $(cat ${DNSMASQPID})
129         # Send even if empty so old servers are cleared
130         dbus-send --system --dest=uk.org.thekelleys.dnsmasq \
131                 /uk/org/thekelleys/dnsmasq uk.org.thekelleys.SetServers \
132                 ${DBUSDEST}
133 fi