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