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