xref: /freebsd/contrib/openresolv/pdnsd.in (revision 1b99d52f261bfacfb9bb149d33ed6444364ac219)
1#!/bin/sh
2# Copyright (c) 2010-2013 Roy Marples
3# All rights reserved
4
5# pdnsd 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 "$pdnsd_conf" -a -z "$pdnsd_resolv" ] && exit 0
32[ -z "$RESOLVCONF" ] && eval "$(@SBINDIR@/resolvconf -v)"
33NL="
34"
35
36: ${pdnsd_restart:=pdnsd-ctl config $pdnsd_conf}
37signature="# Generated by resolvconf"
38signature_end="# End of resolvconf"
39
40# We normally use sed to remove markers from a configuration file
41# but sed may not always be available at the time.
42remove_markers()
43{
44	local m1="$1" m2="$2" x= line= in_marker=0
45
46	shift; shift
47	if type sed >/dev/null 2>&1; then
48		sed "/^$m1/,/^$m2/d" $@
49	else
50		for x; do
51			while read -r line; do
52				case "$line" in
53				"$m1"*) in_marker=1;;
54				"$m2"*) in_marker=0;;
55				*) [ $in_marker = 0 ] && echo "$line";;
56				esac
57			done < "$x"
58		done
59	fi
60}
61
62# Compare two files
63# If different, replace first with second otherwise remove second
64change_file()
65{
66	if [ -e "$1" ]; then
67		if type cmp >/dev/null 2>&1; then
68			cmp -s "$1" "$2"
69		elif type diff >/dev/null 2>&1; then
70			diff -q "$1" "$2" >/dev/null
71		else
72			# Hopefully we're only working on small text files ...
73			[ "$(cat "$1")" = "$(cat "$2")" ]
74		fi
75		if [ $? -eq 0 ]; then
76			rm -f "$2"
77			return 1
78		fi
79	fi
80	cat "$2" > "$1"
81	rm -f "$2"
82	return 0
83}
84
85newresolv="# Generated by resolvconf$NL"
86changed=false
87
88# Try to ensure that config dirs exist
89if type config_mkdirs >/dev/null 2>&1; then
90	config_mkdirs "$pdnsd_resolv" "$pdnsd_conf"
91else
92	@SBINDIR@/resolvconf -D "$pdnsd_resolv" "$pdnsd_conf"
93fi
94
95if [ -n "$pdnsd_resolv" ]; then
96	for n in $NAMESERVERS; do
97		newresolv="${newresolv}nameserver $n$NL"
98	done
99fi
100
101# Only modify the configuration if it exists and we can write to it
102if [ -w "$pdnsd_conf" ]; then
103	cf="$pdnsd_conf.new"
104	newconf=
105
106	if [ -z "$pdnsd_resolv" ]; then
107		newconf="${newconf}server {$NL"
108		newconf="${newconf}	label=resolvconf;$NL"
109		if [ -n "$NAMESERVERS" ]; then
110			newconf="${newconf}	ip="
111			first=true
112			for n in $NAMESERVERS; do
113				if $first; then
114					first=false
115				else
116					newconf="${newconf},"
117				fi
118				newconf="$newconf$n"
119			done
120			newconf="${newconf};$NL"
121		fi
122		newconf="${newconf}}$NL"
123	fi
124
125	for d in $DOMAINS; do
126		newconf="${newconf}server {$NL"
127		newconf="${newconf}	include=.${d%%:*}.;$NL"
128		newconf="${newconf}	policy=excluded;$NL"
129		newconf="${newconf}	ip="
130		ns="${d#*:}"
131		while [ -n "$ns" ]; do
132			newconf="${newconf}${ns%%,*}"
133			[ "$ns" = "${ns#*,}" ] && break
134			ns="${ns#*,}"
135			newconf="${newconf},"
136		done
137		newconf="${newconf};$NL}$NL"
138	done
139
140	rm -f "$cf"
141	remove_markers "$signature" "$signature_end" "$pdnsd_conf" > "$cf"
142	if [ -n "$newconf" ]; then
143		echo "$signature" >> "$cf"
144		printf %s "$newconf" >> "$cf"
145		echo "$signature_end" >> "$cf"
146	fi
147	if change_file "$pdnsd_conf" "$cf"; then
148		changed=true
149	fi
150fi
151
152if [ -n "$pdnsd_resolv" ]; then
153	if [ ! -f "$pdnsd_resolv" ] || \
154		[ "$(cat "$pdnsd_resolv")" != "$(printf %s "$newresolv")" ]
155	then
156		changed=true
157		printf %s "$newresolv" >"$pdnsd_resolv"
158	fi
159fi
160
161if $changed; then
162	eval $pdnsd_restart
163fi
164