summaryrefslogtreecommitdiffstats
path: root/contrib/openresolv/pdnsd.in
blob: 61dc5e1394b58cb76613e650908d82f51fbd8326 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/bin/sh
# Copyright (c) 2010 Roy Marples
# All rights reserved

# pdnsd subscriber for resolvconf

# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above
#       copyright notice, this list of conditions and the following
#       disclaimer in the documentation and/or other materials provided
#       with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

[ -f "@SYSCONFDIR@"/resolvconf.conf ] || exit 0
. "@SYSCONFDIR@/resolvconf.conf" || exit 1
[ -z "$pdnsd_conf" -a -z "$pdnsd_resolv" ] && exit 0
[ -z "$RESOLVCONF" ] && eval "$(@PREFIX@/sbin/resolvconf -v)"

: ${pdnsd_restart:=pdnsd-ctl config $pdnsd_conf}
signature="# Generated by resolvconf"
signature_end="# End of resolvconf"

# We normally use sed to remove markers from a configuration file
# but sed may not always be available at the time.
remove_markers()
{
	local m1="$1" m2="$2" x= line= in_marker=0

	shift; shift
	if type sed >/dev/null 2>&1; then
		sed "/^$m1/,/^$m2/d" $@
	else
		for x; do
			while read line; do
				case "$line" in
				"$m1"*) in_marker=1;;
				"$m2"*) in_marker=0;;
				*) [ $in_marker = 0 ] && echo "$line";;
				esac
			done < "$x"
		done
	fi
}

# Compare two files
# If different, replace first with second otherwise remove second
change_file()
{
	if [ -e "$1" ]; then
		if type cmp >/dev/null 2>&1; then
			cmp -s "$1" "$2"
		elif type diff >/dev/null 2>&1; then
			diff -q "$1" "$2" >/dev/null
		else
			# Hopefully we're only working on small text files ...
			[ "$(cat "$1")" = "$(cat "$2")" ]
		fi
		if [ $? -eq 0 ]; then
			rm -f "$2"
			return 1
		fi
	fi
	cat "$2" > "$1"
	rm -f "$2"
	return 0
}

newresolv="# Generated by resolvconf\n"
changed=false

if [ -n "$pdnsd_resolv" ]; then
	for n in $NAMESERVERS; do
		newresolv="${newresolv}nameserver $n\n"
	done
fi

if [ -n "$pdnsd_conf" ]; then
	cf="$pdnsd_conf.new"
	newconf=

	if [ -z "$pdnsd_resolv" ]; then
		newconf="${newconf}server {\n"
		newconf="${newconf}\tlabel=resolvconf;\n"
		if [ -n "$NAMESERVERS" ]; then
			newconf="${newconf}\tip="
			first=true
			for n in $NAMESERVERS; do
				if $first; then
					first=false
				else
					newconf="${newconf},"
				fi
				newconf="$newconf$n"
			done
			newconf="${newconf};\n"
		fi
		newconf="${newconf}}\n"
	fi

	for d in $DOMAINS; do
		newconf="${newconf}server {\n"
		newconf="${newconf}\tinclude=.${d%%:*}.;\n"
		newconf="${newconf}\tpolicy=excluded;\n"
		newconf="${newconf}\tip="
		ns="${d#*:}"
		while [ -n "$ns" ]; do
			newconf="${newconf}${ns%%,*}"
			[ "$ns" = "${ns#*,}" ] && break
			ns="${ns#*,}"
			newconf="${newconf},"
		done
		newconf="${newconf};\n}\n"
	done

	rm -f "$cf"
	remove_markers "$signature" "$signature_end" "$pdnsd_conf" > "$cf"
	if [ -n "$newconf" ]; then
		echo "$signature" >> "$cf"
		printf %s "$newconf" >> "$cf"
		echo "$signature_end" >> "$cf"
	fi
	if change_file "$pdnsd_conf" "$cf"; then
		changed=true
	fi	
fi

if [ -n "$pdnsd_resolv" ]; then
	if [ ! -f "$pdnsd_resolv" ] || \
		[ "$(cat "$pdnsd_resolv")" != "$(printf %s "$newresolv")" ]
	then
		changed=true
		printf %s "$newresolv" >"$pdnsd_resolv"
	fi
fi

if $changed; then
	eval $pdnsd_restart
fi
OpenPOWER on IntegriCloud