dnsmasq.in revision 1.1.1.7
1#!/bin/sh
2# Copyright (c) 2007-2012 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)"
33NL="
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}@}
40newconf="# Generated by resolvconf$NL"
41newresolv="$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
48dbus=false
49dbus_ex=false
50: ${dbus_pid:=/var/run/dbus/dbus.pid}
51[ -s "$dbus_pid" ] || dbus_pid=/var/run/dbus.pid
52[ -s "$dbus_pid" ] || dbus_pid=/var/run/dbus/pid
53if [ -s "$dbus_pid" -a -s "$dnsmasq_pid" ]; then
54	if dnsmasq --version 2>/dev/null | \
55		grep -q "^Compile time options.*[[:space:]]DBus[[:space:]]"
56	then
57		# Sanity - check that dnsmasq and dbus are running
58		if kill -0 $(cat "$dbus_pid") 2>/dev/null && \
59			kill -0 $(cat "$dnsmasq_pid") 2>/dev/null
60		then
61			dbus=true
62			if dbus-send --print-reply --system --dest=uk.org.thekelleys.dnsmasq /uk/org/thekelleys/dnsmasq org.freedesktop.DBus.Introspectable.Introspect | grep -q '<method name="SetDomainServers">'
63			then
64				dbus_ex=true
65			fi
66		fi
67	fi
68fi
69
70for n in $NAMESERVERS; do
71	newresolv="${newresolv}nameserver $n$NL"
72done
73
74dbusdest=
75dbusdest_ex=
76conf=
77for d in $DOMAINS; do
78	dn="${d%%:*}"
79	ns="${d#*:}"
80	while [ -n "$ns" ]; do
81		n="${ns%%,*}"
82		if $dbus && ! $dbus_ex; then
83			case "$n" in
84			*.*.*.*)
85				SIFS=${IFS-y} OIFS=$IFS
86				IFS=.
87				set -- $n
88				num="0x$(printf %02x $1 $2 $3 $4)"
89				if [ "$SIFS" = y ]; then
90					unset IFS
91				else
92					IFS=$OIFS
93				fi
94				dbusdest="$dbusdest uint32:$(printf %u $num)"
95				dbusdest="$dbusdest string:$dn"
96				;;
97			*:*%*)
98				# This version of dnsmasq won't accept
99				# scoped IPv6 addresses
100				dbus=false
101				;;
102			*:*)
103				SIFS=${IFS-y} OIFS=$IFS bytes= front= back=
104				empty=false i=0
105				IFS=:
106				set -- $n
107				while [ -n "$1" -o -n "$2" ]; do
108					addr="$1"
109					shift
110					if [ -z "$addr" ]; then
111						empty=true
112						continue
113					fi
114					i=$(($i + 1))
115					while [ ${#addr} -lt 4 ]; do
116						addr="0${addr}"
117					done
118					byte1="$(printf %d 0x${addr%??})"
119					byte2="$(printf %d 0x${addr#??})"
120					if $empty; then
121						back="$back byte:$byte1 byte:$byte2"
122					else
123						front="$front byte:$byte1 byte:$byte2"
124					fi
125				done
126				while [ $i != 8 ]; do
127				i=$(($i + 1))
128					front="$front byte:0 byte:0"
129				done
130				front="${front}$back"
131				if [ "$SIFS" = y ]; then
132					unset IFS
133				else
134					IFS=$OIFS
135				fi
136				dbusdest="${dbusdest}$front string:$dn"
137				;;
138			*)
139				if ! $dbus_ex; then
140					dbus=false
141				fi
142				;;
143			esac
144		fi
145		dbusdest_ex="$dbusdest_ex${dbusdest_ex:+,}/$dn/$n"
146		conf="${conf}server=/$dn/$n$NL"
147		[ "$ns" = "${ns#*,}" ] && break
148		ns="${ns#*,}"
149	done
150done
151
152if $dbus; then
153	newconf="$newconf$NL# Domain specific servers will"
154	newconf="$newconf be sent over dbus${NL}enable-dbus$NL"
155else
156	newconf="$newconf$conf"
157fi
158
159# Try to ensure that config dirs exist
160if type config_mkdirs >/dev/null 2>&1; then
161	config_mkdirs "$dnsmasq_conf" "$dnsmasq_resolv"
162else
163	@PREFIX@/sbin/resolvconf -D "$dnsmasq_conf" "$dnsmasq_resolv"
164fi
165
166changed=false
167if [ -n "$dnsmasq_conf" ]; then
168	if [ ! -f "$dnsmasq_conf" ] || \
169		[ "$(cat "$dnsmasq_conf")" != "$(printf %s "$newconf")" ]
170	then
171		changed=true
172		printf %s "$newconf" >"$dnsmasq_conf"
173	fi
174fi
175if [ -n "$dnsmasq_resolv" ]; then
176	# dnsmasq polls this file so no need to set changed=true
177	if [ -f "$dnsmasq_resolv" ]; then
178		if [ "$(cat "$dnsmasq_resolv")" != "$(printf %s "$newresolv")" ]
179		then
180			printf %s "$newresolv" >"$dnsmasq_resolv"
181		fi
182	else
183		printf %s "$newresolv" >"$dnsmasq_resolv"
184	fi
185fi
186
187if $changed; then
188	eval $dnsmasq_restart
189fi
190if $dbus; then
191	$changed || kill -HUP $(cat "$dnsmasq_pid")
192	# Send even if empty so old servers are cleared
193	if $dbus_ex; then
194		method=SetDomainServers
195		if [ -n "$dbusdest_ex" ]; then
196			dbusdest_ex="array:string:$dbusdest_ex"
197		fi
198		dbusdest="$dbusdest_ex"
199	else
200		method=SetServers
201	fi
202	dbus-send --system --dest=uk.org.thekelleys.dnsmasq \
203 		/uk/org/thekelleys/dnsmasq uk.org.thekelleys.$method \
204  		$dbusdest
205fi
206