1219732Sume#!/bin/sh
2219732Sume# Copyright (c) 2007-2009 Roy Marples
3219732Sume# All rights reserved
4219732Sume
5219732Sume# dnsmasq subscriber for resolvconf
6219732Sume
7219732Sume# Redistribution and use in source and binary forms, with or without
8219732Sume# modification, are permitted provided that the following conditions
9219732Sume# are met:
10219732Sume#     * Redistributions of source code must retain the above copyright
11219732Sume#       notice, this list of conditions and the following disclaimer.
12219732Sume#     * Redistributions in binary form must reproduce the above
13219732Sume#       copyright notice, this list of conditions and the following
14219732Sume#       disclaimer in the documentation and/or other materials provided
15219732Sume#       with the distribution.
16219732Sume#
17219732Sume# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18219732Sume# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19219732Sume# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20219732Sume# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21219732Sume# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22219732Sume# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23219732Sume# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24219732Sume# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25219732Sume# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26219732Sume# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27219732Sume# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28219732Sume
29219732Sume[ -f "@SYSCONFDIR@"/resolvconf.conf ] || exit 0
30219732Sume. "@SYSCONFDIR@/resolvconf.conf" || exit 1
31219732Sume[ -z "$dnsmasq_conf" -a -z "$dnsmasq_resolv" ] && exit 0
32219732Sume[ -z "$RESOLVCONF" ] && eval "$(@PREFIX@/sbin/resolvconf -v)"
33225524ShrsNL="
34225524Shrs"
35219732Sume
36219732Sume: ${dnsmasq_pid:=/var/run/dnsmasq.pid}
37219732Sume[ -s "$dnsmasq_pid" ] || dnsmasq_pid=/var/run/dnsmasq/dnsmasq.pid
38219732Sume: ${dnsmasq_service:=dnsmasq}
39219732Sume: ${dnsmasq_restart:=@RESTARTCMD ${dnsmasq_service}@}
40225524Shrsnewconf="# Generated by resolvconf$NL"
41219732Sumenewresolv="$newconf"
42219732Sume
43219732Sume# Using dbus means that we never have to restart the daemon
44219732Sume# This is important as it means we should not drop DNS queries
45219732Sume# whilst changing DNS options around. However, dbus support is optional
46219732Sume# so we need to validate a few things first.
47219732Sume# Check for DBus support in the binary
48219732Sumedbus=false
49219732Sume: ${dbus_pid:=/var/run/dbus/dbus.pid}
50219732Sume[ -s "$dbus_pid" ] || dbus_pid=/var/run/dbus.pid
51219732Sume[ -s "$dbus_pid" ] || dbus_pid=/var/run/dbus/pid
52219732Sumeif [ -s "$dbus_pid" -a -s "$dnsmasq_pid" ]; then
53219732Sume	if dnsmasq --version 2>/dev/null | \
54219732Sume		grep -q "^Compile time options.*[[:space:]]DBus[[:space:]]"
55219732Sume	then
56219732Sume		# Sanity - check that dnsmasq and dbus are running
57219732Sume		if kill -0 $(cat "$dbus_pid") 2>/dev/null && \
58219732Sume			kill -0 $(cat "$dnsmasq_pid") 2>/dev/null
59219732Sume		then
60219732Sume			dbus=true
61225524Shrs			newconf="$newconf$NL# Domain specific servers will"
62225524Shrs			newconf="$newconf be sent over dbus${NL}enable-dbus$NL"
63219732Sume		fi
64219732Sume	fi
65219732Sumefi
66219732Sume
67219732Sumefor n in $NAMESERVERS; do
68225524Shrs	newresolv="${newresolv}nameserver $n$NL"
69219732Sumedone
70219732Sume
71219732Sumedbusdest=
72219732Sumefor d in $DOMAINS; do
73219732Sume	dn="${d%%:*}"
74219732Sume	ns="${d#*:}"
75219732Sume	while [ -n "$ns" ]; do
76219732Sume		if $dbus; then
77219732Sume			SIFS=${IFS-y} OIFS=$IFS
78219732Sume			IFS=.
79219732Sume			set -- ${ns%%,*}
80225524Shrs			num="0x$(printf %02x $1 $2 $3 $4)"
81219732Sume			if [ "$SIFS" = yi ]; then
82219732Sume				unset IFS
83219732Sume			else
84219732Sume				IFS=$OIFS
85219732Sume			fi
86225524Shrs			dbusdest="$dbusdest uint32:$(printf %u $num)"
87219732Sume			dbusdest="$dbusdest string:$dn"
88219732Sume		else
89225524Shrs			newconf="${newconf}server=/$dn/${ns%%,*}$NL"
90219732Sume		fi
91219732Sume		[ "$ns" = "${ns#*,}" ] && break
92219732Sume		ns="${ns#*,}"
93219732Sume	done
94219732Sumedone
95219732Sume
96219732Sumechanged=false
97219732Sumeif [ -n "$dnsmasq_conf" ]; then
98219732Sume	if [ ! -f "$dnsmasq_conf" ] || \
99225524Shrs		[ "$(cat "$dnsmasq_conf")" != "$(printf %s "$newconf")" ]
100219732Sume	then
101219732Sume		changed=true
102225524Shrs		printf %s "$newconf" >"$dnsmasq_conf"
103219732Sume	fi
104219732Sumefi
105219732Sumeif [ -n "$dnsmasq_resolv" ]; then
106219732Sume	if [ -f "$dnsmasq_resolv" ]; then
107225524Shrs		if [ "$(cat "$dnsmasq_resolv")" != "$(printf %s "$newresolv")" ]
108219732Sume		then
109219732Sume			changed=true
110225524Shrs			printf %s "$newresolv" >"$dnsmasq_resolv"
111219732Sume		fi
112219732Sume	else
113219732Sume		# dnsmasq polls this file so no need to set changed=true
114225524Shrs		printf %s "$newresolv" >"$dnsmasq_resolv"
115219732Sume	fi
116219732Sumefi
117219732Sume
118219732Sumeif $changed; then
119219732Sume	eval $dnsmasq_restart
120219732Sumefi
121219732Sumeif $dbus; then
122219732Sume	$changed || kill -HUP $(cat "$dnsmasq_pid")
123219732Sume	# Send even if empty so old servers are cleared
124219732Sume	dbus-send --system --dest=uk.org.thekelleys.dnsmasq \
125219732Sume 		/uk/org/thekelleys/dnsmasq uk.org.thekelleys.SetServers \
126219732Sume  		$dbusdest
127219732Sumefi
128