]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - contrib/openresolv/dnsmasq.in
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / contrib / openresolv / dnsmasq.in
1 #!/bin/sh
2 # Copyright (c) 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" -a -z "$dnsmasq_resolv" ] && exit 0
32 [ -z "$RESOLVCONF" ] && eval "$(@PREFIX@/sbin/resolvconf -v)"
33 NL="
34 "
35
36 : ${dnsmasq_pid:=/var/run/dnsmasq.pid}
37 [ -s "$dnsmasq_pid" ] || dnsmasq_pid=/var/run/dnsmasq/dnsmasq.pid
38 : ${dnsmasq_service:=dnsmasq}
39 : ${dnsmasq_restart:=@RESTARTCMD ${dnsmasq_service}@}
40 newconf="# Generated by resolvconf$NL"
41 newresolv="$newconf"
42
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
48 dbus=false
49 : ${dbus_pid:=/var/run/dbus/dbus.pid}
50 [ -s "$dbus_pid" ] || dbus_pid=/var/run/dbus.pid
51 [ -s "$dbus_pid" ] || dbus_pid=/var/run/dbus/pid
52 if [ -s "$dbus_pid" -a -s "$dnsmasq_pid" ]; then
53         if dnsmasq --version 2>/dev/null | \
54                 grep -q "^Compile time options.*[[:space:]]DBus[[:space:]]"
55         then
56                 # Sanity - check that dnsmasq and dbus are running
57                 if kill -0 $(cat "$dbus_pid") 2>/dev/null && \
58                         kill -0 $(cat "$dnsmasq_pid") 2>/dev/null
59                 then
60                         dbus=true
61                         newconf="$newconf$NL# Domain specific servers will"
62                         newconf="$newconf be sent over dbus${NL}enable-dbus$NL"
63                 fi
64         fi
65 fi
66
67 for n in $NAMESERVERS; do
68         newresolv="${newresolv}nameserver $n$NL"
69 done
70
71 dbusdest=
72 for d in $DOMAINS; do
73         dn="${d%%:*}"
74         ns="${d#*:}"
75         while [ -n "$ns" ]; do
76                 if $dbus; then
77                         SIFS=${IFS-y} OIFS=$IFS
78                         IFS=.
79                         set -- ${ns%%,*}
80                         num="0x$(printf %02x $1 $2 $3 $4)"
81                         if [ "$SIFS" = yi ]; then
82                                 unset IFS
83                         else
84                                 IFS=$OIFS
85                         fi
86                         dbusdest="$dbusdest uint32:$(printf %u $num)"
87                         dbusdest="$dbusdest string:$dn"
88                 else
89                         newconf="${newconf}server=/$dn/${ns%%,*}$NL"
90                 fi
91                 [ "$ns" = "${ns#*,}" ] && break
92                 ns="${ns#*,}"
93         done
94 done
95
96 changed=false
97 if [ -n "$dnsmasq_conf" ]; then
98         if [ ! -f "$dnsmasq_conf" ] || \
99                 [ "$(cat "$dnsmasq_conf")" != "$(printf %s "$newconf")" ]
100         then
101                 changed=true
102                 printf %s "$newconf" >"$dnsmasq_conf"
103         fi
104 fi
105 if [ -n "$dnsmasq_resolv" ]; then
106         if [ -f "$dnsmasq_resolv" ]; then
107                 if [ "$(cat "$dnsmasq_resolv")" != "$(printf %s "$newresolv")" ]
108                 then
109                         changed=true
110                         printf %s "$newresolv" >"$dnsmasq_resolv"
111                 fi
112         else
113                 # dnsmasq polls this file so no need to set changed=true
114                 printf %s "$newresolv" >"$dnsmasq_resolv"
115         fi
116 fi
117
118 if $changed; then
119         eval $dnsmasq_restart
120 fi
121 if $dbus; then
122         $changed || kill -HUP $(cat "$dnsmasq_pid")
123         # Send even if empty so old servers are cleared
124         dbus-send --system --dest=uk.org.thekelleys.dnsmasq \
125                 /uk/org/thekelleys/dnsmasq uk.org.thekelleys.SetServers \
126                 $dbusdest
127 fi