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