dnsmasq.in revision 1.1.1.2
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
34: ${dnsmasq_pid:=/var/run/dnsmasq.pid}
35: ${dnsmasq_restart:=@RESTARTCMD dnsmasq@}
36newconf="# Generated by resolvconf\n"
37newresolv="$newconf"
38
39# Using dbus means that we never have to restart the daemon
40# This is important as it means we should not drop DNS queries
41# whilst changing DNS options around. However, dbus support is optional
42# so we need to validate a few things first.
43# Check for DBus support in the binary
44dbus=false
45: ${dbus_pid:=/var/run/dbus/dbus.pid}
46[ -s "$dbus_pid" ] || dbus_pid=/var/run/dbus.pid
47[ -s "$dbus_pid" ] || dbus_pid=/var/run/dbus/pid
48if [ -s "$dbus_pid" -a -s "$dnsmasq_pid" ]; then
49	if dnsmasq --version 2>/dev/null | \
50		grep -q "^Compile time options.*[[:space:]]DBus[[:space:]]"
51	then
52		# Sanity - check that dnsmasq and dbus are running
53		if kill -0 $(cat "$dbus_pid") 2>/dev/null && \
54			kill -0 $(cat "$dnsmasq_pid") 2>/dev/null
55		then
56			dbus=true
57			newconf="$newconf\n# Domain specific servers will"
58			newconf="$newconf be sent over dbus\nenable-dbus\n"
59		fi
60	fi
61fi
62
63for n in $NAMESERVERS; do
64	newresolv="${newresolv}nameserver $n\n"
65done
66
67dbusdest=
68for d in $DOMAINS; do
69	dn="${d%%:*}"
70	ns="${d#*:}"
71	while [ -n "$ns" ]; do
72		if $dbus; then
73			SIFS=${IFS-y} OIFS=$IFS
74			IFS=.
75			set -- ${ns%%,*}
76			num="0x$(printf "%02x" $1 $2 $3 $4)"
77			if [ "$SIFS" = yi ]; then
78				unset IFS
79			else
80				IFS=$OIFS
81			fi
82			dbusdest="$dbusdest uint32:$(printf "%u" $num)"
83			dbusdest="$dbusdest string:$dn"
84		else
85			newconf="${newconf}server=/$dn/${ns%%,*}\n"
86		fi
87		[ "$ns" = "${ns#*,}" ] && break
88		ns="${ns#*,}"
89	done
90done
91
92changed=false
93if [ -n "$dnsmasq_conf" ]; then
94	if [ ! -f "$dnsmasq_conf" ] || \
95		[ "$(cat "$dnsmasq_conf")" != "$(printf "$newconf")" ]
96	then
97		changed=true
98		printf "$newconf" >"$dnsmasq_conf"
99	fi
100fi
101if [ -n "$dnsmasq_resolv" ]; then
102	if [ -f "$dnsmasq_resolv" ]; then
103		if [ "$(cat "$dnsmasq_resolv")" != "$(printf "$newresolv")" ]
104		then
105			changed=true
106			printf "$newresolv" >"$dnsmasq_resolv"
107		fi
108	else
109		# dnsmasq polls this file so no need to set changed=true
110		printf "$newresolv" >"$dnsmasq_resolv"
111	fi
112fi
113
114if $changed; then
115	eval $dnsmasq_restart
116fi
117if $dbus; then
118	$changed || kill -HUP $(cat "$dnsmasq_pid")
119	# Send even if empty so old servers are cleared
120	dbus-send --system --dest=uk.org.thekelleys.dnsmasq \
121 		/uk/org/thekelleys/dnsmasq uk.org.thekelleys.SetServers \
122  		$dbusdest
123fi
124