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