xref: /freebsd/contrib/openresolv/pdnsd.in (revision 9af6c78cd43b18e169f10802142c61638bd62bed)
1587392a5SHajimu UMEMOTO#!/bin/sh
2*9af6c78cSPedro F. Giffuni# Copyright (c) 2010-2018 Roy Marples
3587392a5SHajimu UMEMOTO# All rights reserved
4587392a5SHajimu UMEMOTO
5587392a5SHajimu UMEMOTO# pdnsd subscriber for resolvconf
6587392a5SHajimu UMEMOTO
7587392a5SHajimu UMEMOTO# Redistribution and use in source and binary forms, with or without
8587392a5SHajimu UMEMOTO# modification, are permitted provided that the following conditions
9587392a5SHajimu UMEMOTO# are met:
10587392a5SHajimu UMEMOTO#     * Redistributions of source code must retain the above copyright
11587392a5SHajimu UMEMOTO#       notice, this list of conditions and the following disclaimer.
12587392a5SHajimu UMEMOTO#     * Redistributions in binary form must reproduce the above
13587392a5SHajimu UMEMOTO#       copyright notice, this list of conditions and the following
14587392a5SHajimu UMEMOTO#       disclaimer in the documentation and/or other materials provided
15587392a5SHajimu UMEMOTO#       with the distribution.
16587392a5SHajimu UMEMOTO#
17587392a5SHajimu UMEMOTO# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18587392a5SHajimu UMEMOTO# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19587392a5SHajimu UMEMOTO# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20587392a5SHajimu UMEMOTO# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21587392a5SHajimu UMEMOTO# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22587392a5SHajimu UMEMOTO# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23587392a5SHajimu UMEMOTO# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24587392a5SHajimu UMEMOTO# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25587392a5SHajimu UMEMOTO# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26587392a5SHajimu UMEMOTO# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27587392a5SHajimu UMEMOTO# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28587392a5SHajimu UMEMOTO
29587392a5SHajimu UMEMOTO[ -f "@SYSCONFDIR@"/resolvconf.conf ] || exit 0
30587392a5SHajimu UMEMOTO. "@SYSCONFDIR@/resolvconf.conf" || exit 1
31*9af6c78cSPedro F. Giffuni[ -z "${pdnsd_conf}${pdnsd_resolv}" ] && exit 0
32d7149f4eSGlen Barber[ -z "$RESOLVCONF" ] && eval "$(@SBINDIR@/resolvconf -v)"
33d7149f4eSGlen BarberNL="
34d7149f4eSGlen Barber"
35587392a5SHajimu UMEMOTO
36587392a5SHajimu UMEMOTO: ${pdnsd_restart:=pdnsd-ctl config $pdnsd_conf}
37587392a5SHajimu UMEMOTOsignature="# Generated by resolvconf"
38587392a5SHajimu UMEMOTOsignature_end="# End of resolvconf"
39587392a5SHajimu UMEMOTO
40587392a5SHajimu UMEMOTO# We normally use sed to remove markers from a configuration file
41587392a5SHajimu UMEMOTO# but sed may not always be available at the time.
42587392a5SHajimu UMEMOTOremove_markers()
43587392a5SHajimu UMEMOTO{
44*9af6c78cSPedro F. Giffuni	m1="$1"
45*9af6c78cSPedro F. Giffuni	m2="$2"
46*9af6c78cSPedro F. Giffuni	in_marker=0
47587392a5SHajimu UMEMOTO
48587392a5SHajimu UMEMOTO	shift; shift
49587392a5SHajimu UMEMOTO	if type sed >/dev/null 2>&1; then
50587392a5SHajimu UMEMOTO		sed "/^$m1/,/^$m2/d" $@
51587392a5SHajimu UMEMOTO	else
52*9af6c78cSPedro F. Giffuni		for x do
53*9af6c78cSPedro F. Giffuni			while read line; do
54587392a5SHajimu UMEMOTO				case "$line" in
55587392a5SHajimu UMEMOTO				"$m1"*) in_marker=1;;
56587392a5SHajimu UMEMOTO				"$m2"*) in_marker=0;;
57587392a5SHajimu UMEMOTO				*) [ $in_marker = 0 ] && echo "$line";;
58587392a5SHajimu UMEMOTO				esac
59587392a5SHajimu UMEMOTO			done < "$x"
60587392a5SHajimu UMEMOTO		done
61587392a5SHajimu UMEMOTO	fi
62587392a5SHajimu UMEMOTO}
63587392a5SHajimu UMEMOTO
64587392a5SHajimu UMEMOTO# Compare two files
65587392a5SHajimu UMEMOTO# If different, replace first with second otherwise remove second
66587392a5SHajimu UMEMOTOchange_file()
67587392a5SHajimu UMEMOTO{
68587392a5SHajimu UMEMOTO	if [ -e "$1" ]; then
69587392a5SHajimu UMEMOTO		if type cmp >/dev/null 2>&1; then
70587392a5SHajimu UMEMOTO			cmp -s "$1" "$2"
71587392a5SHajimu UMEMOTO		elif type diff >/dev/null 2>&1; then
72587392a5SHajimu UMEMOTO			diff -q "$1" "$2" >/dev/null
73587392a5SHajimu UMEMOTO		else
74587392a5SHajimu UMEMOTO			# Hopefully we're only working on small text files ...
75587392a5SHajimu UMEMOTO			[ "$(cat "$1")" = "$(cat "$2")" ]
76587392a5SHajimu UMEMOTO		fi
77587392a5SHajimu UMEMOTO		if [ $? -eq 0 ]; then
78587392a5SHajimu UMEMOTO			rm -f "$2"
79587392a5SHajimu UMEMOTO			return 1
80587392a5SHajimu UMEMOTO		fi
81587392a5SHajimu UMEMOTO	fi
82587392a5SHajimu UMEMOTO	cat "$2" > "$1"
83587392a5SHajimu UMEMOTO	rm -f "$2"
84587392a5SHajimu UMEMOTO	return 0
85587392a5SHajimu UMEMOTO}
86587392a5SHajimu UMEMOTO
87d7149f4eSGlen Barbernewresolv="# Generated by resolvconf$NL"
88587392a5SHajimu UMEMOTOchanged=false
89587392a5SHajimu UMEMOTO
90d7149f4eSGlen Barber# Try to ensure that config dirs exist
91d7149f4eSGlen Barberif type config_mkdirs >/dev/null 2>&1; then
92d7149f4eSGlen Barber	config_mkdirs "$pdnsd_resolv" "$pdnsd_conf"
93d7149f4eSGlen Barberelse
94d7149f4eSGlen Barber	@SBINDIR@/resolvconf -D "$pdnsd_resolv" "$pdnsd_conf"
95d7149f4eSGlen Barberfi
96d7149f4eSGlen Barber
97587392a5SHajimu UMEMOTOif [ -n "$pdnsd_resolv" ]; then
98587392a5SHajimu UMEMOTO	for n in $NAMESERVERS; do
99d7149f4eSGlen Barber		newresolv="${newresolv}nameserver $n$NL"
100587392a5SHajimu UMEMOTO	done
101587392a5SHajimu UMEMOTOfi
102587392a5SHajimu UMEMOTO
103d7149f4eSGlen Barber# Only modify the configuration if it exists and we can write to it
104d7149f4eSGlen Barberif [ -w "$pdnsd_conf" ]; then
105587392a5SHajimu UMEMOTO	cf="$pdnsd_conf.new"
106587392a5SHajimu UMEMOTO	newconf=
107587392a5SHajimu UMEMOTO
108587392a5SHajimu UMEMOTO	if [ -z "$pdnsd_resolv" ]; then
109d7149f4eSGlen Barber		newconf="${newconf}server {$NL"
110d7149f4eSGlen Barber		newconf="${newconf}	label=resolvconf;$NL"
111587392a5SHajimu UMEMOTO		if [ -n "$NAMESERVERS" ]; then
112d7149f4eSGlen Barber			newconf="${newconf}	ip="
113587392a5SHajimu UMEMOTO			first=true
114587392a5SHajimu UMEMOTO			for n in $NAMESERVERS; do
115587392a5SHajimu UMEMOTO				if $first; then
116587392a5SHajimu UMEMOTO					first=false
117587392a5SHajimu UMEMOTO				else
118587392a5SHajimu UMEMOTO					newconf="${newconf},"
119587392a5SHajimu UMEMOTO				fi
120587392a5SHajimu UMEMOTO				newconf="$newconf$n"
121587392a5SHajimu UMEMOTO			done
122d7149f4eSGlen Barber			newconf="${newconf};$NL"
123587392a5SHajimu UMEMOTO		fi
124d7149f4eSGlen Barber		newconf="${newconf}}$NL"
125587392a5SHajimu UMEMOTO	fi
126587392a5SHajimu UMEMOTO
127587392a5SHajimu UMEMOTO	for d in $DOMAINS; do
128d7149f4eSGlen Barber		newconf="${newconf}server {$NL"
129d7149f4eSGlen Barber		newconf="${newconf}	include=.${d%%:*}.;$NL"
130d7149f4eSGlen Barber		newconf="${newconf}	policy=excluded;$NL"
131d7149f4eSGlen Barber		newconf="${newconf}	ip="
132587392a5SHajimu UMEMOTO		ns="${d#*:}"
133587392a5SHajimu UMEMOTO		while [ -n "$ns" ]; do
134587392a5SHajimu UMEMOTO			newconf="${newconf}${ns%%,*}"
135587392a5SHajimu UMEMOTO			[ "$ns" = "${ns#*,}" ] && break
136587392a5SHajimu UMEMOTO			ns="${ns#*,}"
137587392a5SHajimu UMEMOTO			newconf="${newconf},"
138587392a5SHajimu UMEMOTO		done
139d7149f4eSGlen Barber		newconf="${newconf};$NL}$NL"
140587392a5SHajimu UMEMOTO	done
141587392a5SHajimu UMEMOTO
142587392a5SHajimu UMEMOTO	rm -f "$cf"
143587392a5SHajimu UMEMOTO	remove_markers "$signature" "$signature_end" "$pdnsd_conf" > "$cf"
144587392a5SHajimu UMEMOTO	if [ -n "$newconf" ]; then
145587392a5SHajimu UMEMOTO		echo "$signature" >> "$cf"
146a02aba5fSHiroki Sato		printf %s "$newconf" >> "$cf"
147587392a5SHajimu UMEMOTO		echo "$signature_end" >> "$cf"
148587392a5SHajimu UMEMOTO	fi
149587392a5SHajimu UMEMOTO	if change_file "$pdnsd_conf" "$cf"; then
150587392a5SHajimu UMEMOTO		changed=true
151587392a5SHajimu UMEMOTO	fi
152587392a5SHajimu UMEMOTOfi
153587392a5SHajimu UMEMOTO
154587392a5SHajimu UMEMOTOif [ -n "$pdnsd_resolv" ]; then
155587392a5SHajimu UMEMOTO	if [ ! -f "$pdnsd_resolv" ] || \
156a02aba5fSHiroki Sato		[ "$(cat "$pdnsd_resolv")" != "$(printf %s "$newresolv")" ]
157587392a5SHajimu UMEMOTO	then
158587392a5SHajimu UMEMOTO		changed=true
159a02aba5fSHiroki Sato		printf %s "$newresolv" >"$pdnsd_resolv"
160587392a5SHajimu UMEMOTO	fi
161587392a5SHajimu UMEMOTOfi
162587392a5SHajimu UMEMOTO
163587392a5SHajimu UMEMOTOif $changed; then
164587392a5SHajimu UMEMOTO	eval $pdnsd_restart
165587392a5SHajimu UMEMOTOfi
166