xref: /freebsd/contrib/openresolv/libc.in (revision 9af6c78c)
1587392a5SHajimu UMEMOTO#!/bin/sh
2*9af6c78cSPedro F. Giffuni# Copyright (c) 2007-2019 Roy Marples
3587392a5SHajimu UMEMOTO# All rights reserved
4587392a5SHajimu UMEMOTO
5587392a5SHajimu UMEMOTO# libc 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 UMEMOTOSYSCONFDIR=@SYSCONFDIR@
30587392a5SHajimu UMEMOTOLIBEXECDIR=@LIBEXECDIR@
31587392a5SHajimu UMEMOTOVARDIR=@VARDIR@
32587392a5SHajimu UMEMOTOIFACEDIR="$VARDIR/interfaces"
33a02aba5fSHiroki SatoNL="
34a02aba5fSHiroki Sato"
35587392a5SHajimu UMEMOTO
36587392a5SHajimu UMEMOTO# sed may not be available, and this is faster on small files
37587392a5SHajimu UMEMOTOkey_get_value()
38587392a5SHajimu UMEMOTO{
39*9af6c78cSPedro F. Giffuni	key="$1"
40587392a5SHajimu UMEMOTO	shift
41*9af6c78cSPedro F. Giffuni
42587392a5SHajimu UMEMOTO	if [ $# -eq 0 ]; then
43d7149f4eSGlen Barber		while read -r line; do
44587392a5SHajimu UMEMOTO			case "$line" in
45587392a5SHajimu UMEMOTO			"$key"*) echo "${line##$key}";;
46587392a5SHajimu UMEMOTO			esac
47587392a5SHajimu UMEMOTO		done
48587392a5SHajimu UMEMOTO	else
49d7149f4eSGlen Barber		for x do
50d7149f4eSGlen Barber			while read -r line; do
51587392a5SHajimu UMEMOTO				case "$line" in
52587392a5SHajimu UMEMOTO				"$key"*) echo "${line##$key}";;
53587392a5SHajimu UMEMOTO				esac
54587392a5SHajimu UMEMOTO			done < "$x"
55587392a5SHajimu UMEMOTO		done
56587392a5SHajimu UMEMOTO	fi
57587392a5SHajimu UMEMOTO}
58587392a5SHajimu UMEMOTO
59d7149f4eSGlen Barberkeys_remove()
60d7149f4eSGlen Barber{
61d7149f4eSGlen Barber	while read -r line; do
62d7149f4eSGlen Barber		found=false
63d7149f4eSGlen Barber		for key do
64d7149f4eSGlen Barber			case "$line" in
65d7149f4eSGlen Barber			"$key"*|"#"*|" "*|"	"*|"") found=true;;
66d7149f4eSGlen Barber			esac
67d7149f4eSGlen Barber			$found && break
68d7149f4eSGlen Barber		done
69d7149f4eSGlen Barber		$found || echo "$line"
70d7149f4eSGlen Barber	done
71d7149f4eSGlen Barber}
72d7149f4eSGlen Barber
73d7149f4eSGlen Barberlocal_nameservers="127.* 0.0.0.0 255.255.255.255 ::1"
74d7149f4eSGlen Barber
75587392a5SHajimu UMEMOTO# Support original resolvconf configuration layout
76587392a5SHajimu UMEMOTO# as well as the openresolv config file
77587392a5SHajimu UMEMOTOif [ -f "$SYSCONFDIR"/resolvconf.conf ]; then
78587392a5SHajimu UMEMOTO	. "$SYSCONFDIR"/resolvconf.conf
79587392a5SHajimu UMEMOTOelif [ -d "$SYSCONFDIR"/resolvconf ]; then
80*9af6c78cSPedro F. Giffuni	SYSCONFDIR="$SYSCONFDIR/resolvconf"
81587392a5SHajimu UMEMOTO	base="$SYSCONFDIR/resolv.conf.d/base"
82587392a5SHajimu UMEMOTO	if [ -f "$base" ]; then
83d7149f4eSGlen Barber		prepend_nameservers="$(key_get_value "nameserver " "$base")"
84d7149f4eSGlen Barber		domain="$(key_get_value "domain " "$base")"
85d7149f4eSGlen Barber		prepend_search="$(key_get_value "search " "$base")"
86587392a5SHajimu UMEMOTO		resolv_conf_options="$(key_get_value "options " "$base")"
87d7149f4eSGlen Barber		resolv_conf_sortlist="$(key_get_value "sortlist " "$base")"
88587392a5SHajimu UMEMOTO	fi
89587392a5SHajimu UMEMOTO	if [ -f "$SYSCONFDIR"/resolv.conf.d/head ]; then
90587392a5SHajimu UMEMOTO		resolv_conf_head="$(cat "${SYSCONFDIR}"/resolv.conf.d/head)"
91587392a5SHajimu UMEMOTO	fi
92587392a5SHajimu UMEMOTO	if [ -f "$SYSCONFDIR"/resolv.conf.d/tail ]; then
93587392a5SHajimu UMEMOTO		resolv_conf_tail="$(cat "$SYSCONFDIR"/resolv.conf.d/tail)"
94587392a5SHajimu UMEMOTO	fi
95587392a5SHajimu UMEMOTOfi
96587392a5SHajimu UMEMOTO: ${resolv_conf:=/etc/resolv.conf}
97587392a5SHajimu UMEMOTO: ${libc_service:=nscd}
98d7149f4eSGlen Barber: ${list_resolv:=@SBINDIR@/resolvconf -l}
99*9af6c78cSPedro F. Giffuniif [ "${resolv_conf_head-x}" = x ] && [ -f "$SYSCONFDIR"/resolv.conf.head ]
100*9af6c78cSPedro F. Giffunithen
101587392a5SHajimu UMEMOTO	resolv_conf_head="$(cat "${SYSCONFDIR}"/resolv.conf.head)"
102587392a5SHajimu UMEMOTOfi
103*9af6c78cSPedro F. Giffuniif [ "${resolv_conf_tail-x}" = x ] && [ -f "$SYSCONFDIR"/resolv.conf.tail ]
104*9af6c78cSPedro F. Giffunithen
105587392a5SHajimu UMEMOTO	resolv_conf_tail="$(cat "$SYSCONFDIR"/resolv.conf.tail)"
106587392a5SHajimu UMEMOTOfi
107587392a5SHajimu UMEMOTO
108d7149f4eSGlen Barberbackup=true
109d7149f4eSGlen Barbersignature="# Generated by resolvconf"
110d7149f4eSGlen Barber
111587392a5SHajimu UMEMOTOuniqify()
112587392a5SHajimu UMEMOTO{
113*9af6c78cSPedro F. Giffuni	result=
114587392a5SHajimu UMEMOTO	while [ -n "$1" ]; do
115587392a5SHajimu UMEMOTO		case " $result " in
116587392a5SHajimu UMEMOTO		*" $1 "*);;
117587392a5SHajimu UMEMOTO		*) result="$result $1";;
118587392a5SHajimu UMEMOTO		esac
119587392a5SHajimu UMEMOTO		shift
120587392a5SHajimu UMEMOTO	done
121587392a5SHajimu UMEMOTO	echo "${result# *}"
122587392a5SHajimu UMEMOTO}
123587392a5SHajimu UMEMOTO
124587392a5SHajimu UMEMOTOcase "${resolv_conf_passthrough:-NO}" in
125587392a5SHajimu UMEMOTO[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
126d7149f4eSGlen Barber	backup=false
127587392a5SHajimu UMEMOTO	newest=
128587392a5SHajimu UMEMOTO	for conf in "$IFACEDIR"/*; do
129*9af6c78cSPedro F. Giffuni		if [ -z "$newest" ] || [ "$conf" -nt "$newest" ]; then
130587392a5SHajimu UMEMOTO			newest="$conf"
131587392a5SHajimu UMEMOTO		fi
132587392a5SHajimu UMEMOTO	done
133587392a5SHajimu UMEMOTO	[ -z "$newest" ] && exit 0
134a02aba5fSHiroki Sato	newconf="$(cat "$newest")$NL"
135587392a5SHajimu UMEMOTO	;;
136d7149f4eSGlen Barber/dev/null|[Nn][Uu][Ll][Ll])
137d7149f4eSGlen Barber	: ${resolv_conf_local_only:=NO}
138d7149f4eSGlen Barber	if [ "$local_nameservers" = "127.* 0.0.0.0 255.255.255.255 ::1" ]; then
139d7149f4eSGlen Barber		local_nameservers=
140d7149f4eSGlen Barber	fi
141d7149f4eSGlen Barber	# Need to overwrite our variables.
142d7149f4eSGlen Barber	eval "$(@SBINDIR@/resolvconf -V)"
143d7149f4eSGlen Barber	;;
144d7149f4eSGlen Barber
145587392a5SHajimu UMEMOTO*)
146d7149f4eSGlen Barber	[ -z "$RESOLVCONF" ] && eval "$(@SBINDIR@/resolvconf -v)"
147d7149f4eSGlen Barber	;;
148d7149f4eSGlen Barberesac
149d7149f4eSGlen Barbercase "${resolv_conf_passthrough:-NO}" in
150d7149f4eSGlen Barber[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) ;;
151d7149f4eSGlen Barber*)
152d7149f4eSGlen Barber	: ${domain:=$DOMAIN}
153d7149f4eSGlen Barber	newsearch="$(uniqify $prepend_search $SEARCH $append_search)"
154587392a5SHajimu UMEMOTO	NS="$LOCALNAMESERVERS $NAMESERVERS"
155d7149f4eSGlen Barber	newns=
156d7149f4eSGlen Barber	gotlocal=false
157d7149f4eSGlen Barber	for n in $(uniqify $prepend_nameservers $NS $append_nameservers); do
158d7149f4eSGlen Barber		add=true
159d7149f4eSGlen Barber		islocal=false
160d7149f4eSGlen Barber		for l in $local_nameservers; do
161d7149f4eSGlen Barber			case "$n" in
162d7149f4eSGlen Barber			$l) islocal=true; gotlocal=true; break;;
163d7149f4eSGlen Barber			esac
164d7149f4eSGlen Barber		done
165d7149f4eSGlen Barber		if ! $islocal; then
166d7149f4eSGlen Barber			case "${resolv_conf_local_only:-YES}" in
167d7149f4eSGlen Barber			[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
168d7149f4eSGlen Barber				$gotlocal && add=false;;
169d7149f4eSGlen Barber			esac
170d7149f4eSGlen Barber		fi
171d7149f4eSGlen Barber		$add && newns="$newns $n"
172d7149f4eSGlen Barber	done
173587392a5SHajimu UMEMOTO
174587392a5SHajimu UMEMOTO	# Hold our new resolv.conf in a variable to save on temporary files
175d7149f4eSGlen Barber	newconf="$signature$NL"
176587392a5SHajimu UMEMOTO	if [ -n "$resolv_conf_head" ]; then
177a02aba5fSHiroki Sato		newconf="$newconf$resolv_conf_head$NL"
178587392a5SHajimu UMEMOTO	fi
179d7149f4eSGlen Barber
180d7149f4eSGlen Barber	[ -n "$domain" ] && newconf="${newconf}domain $domain$NL"
181*9af6c78cSPedro F. Giffuni	if [ -n "$newsearch" ] && [ "$newsearch" != "$domain" ]; then
182d7149f4eSGlen Barber		newconf="${newconf}search $newsearch$NL"
183d7149f4eSGlen Barber	fi
184587392a5SHajimu UMEMOTO	for n in $newns; do
185a02aba5fSHiroki Sato		newconf="${newconf}nameserver $n$NL"
186587392a5SHajimu UMEMOTO	done
187587392a5SHajimu UMEMOTO
188d7149f4eSGlen Barber	# Now add anything we don't care about such as sortlist and options
189d7149f4eSGlen Barber	stuff="$($list_resolv | keys_remove nameserver domain search)"
190d7149f4eSGlen Barber	if [ -n "$stuff" ]; then
191d7149f4eSGlen Barber		newconf="$newconf$stuff$NL"
192d7149f4eSGlen Barber	fi
193d7149f4eSGlen Barber
194d7149f4eSGlen Barber	# Append any user defined ones
195d7149f4eSGlen Barber	if [ -n "$resolv_conf_options" ]; then
196d7149f4eSGlen Barber		newconf="${newconf}options $resolv_conf_options$NL"
197d7149f4eSGlen Barber	fi
198d7149f4eSGlen Barber	if [ -n "$resolv_conf_sortlist" ]; then
199d7149f4eSGlen Barber		newconf="${newconf}sortlist $resolv_conf_sortlist$NL"
200587392a5SHajimu UMEMOTO	fi
201587392a5SHajimu UMEMOTO
202587392a5SHajimu UMEMOTO	if [ -n "$resolv_conf_tail" ]; then
203a02aba5fSHiroki Sato		newconf="$newconf$resolv_conf_tail$NL"
204587392a5SHajimu UMEMOTO	fi
205587392a5SHajimu UMEMOTO	;;
206587392a5SHajimu UMEMOTOesac
207587392a5SHajimu UMEMOTO
208587392a5SHajimu UMEMOTO# Check if the file has actually changed or not
209587392a5SHajimu UMEMOTOif [ -e "$resolv_conf" ]; then
210a02aba5fSHiroki Sato	[ "$(cat "$resolv_conf")" = "$(printf %s "$newconf")" ] && exit 0
211587392a5SHajimu UMEMOTOfi
212587392a5SHajimu UMEMOTO
213d7149f4eSGlen Barber# Change is good.
214d7149f4eSGlen Barber# If the old file does not have our signature, back it up.
215d7149f4eSGlen Barber# If the new file just has our signature, restore the backup.
216d7149f4eSGlen Barberif $backup; then
217d7149f4eSGlen Barber	if [ "$newconf" = "$signature$NL" ]; then
218d7149f4eSGlen Barber		if [ -e "$resolv_conf.bak" ]; then
21987b2cfceSPedro F. Giffuni			newconf="$(cat "$resolv_conf.bak")$NL"
220d7149f4eSGlen Barber		fi
221d7149f4eSGlen Barber	elif [ -e "$resolv_conf" ]; then
222d7149f4eSGlen Barber		read line <"$resolv_conf"
223d7149f4eSGlen Barber		if [ "$line" != "$signature" ]; then
224d7149f4eSGlen Barber			cp "$resolv_conf" "$resolv_conf.bak"
225d7149f4eSGlen Barber		fi
226d7149f4eSGlen Barber	fi
227d7149f4eSGlen Barberfi
228d7149f4eSGlen Barber
229587392a5SHajimu UMEMOTO# Create our resolv.conf now
230a02aba5fSHiroki Sato(umask 022; echo "$newconf" >"$resolv_conf")
2313f2a60a1SPedro F. Giffuniif [ -n "$libc_restart" ]; then
232587392a5SHajimu UMEMOTO	eval $libc_restart
2333f2a60a1SPedro F. Giffunielif [ -n "$RESTARTCMD" ]; then
2343f2a60a1SPedro F. Giffuni	set -- ${libc_service}
235*9af6c78cSPedro F. Giffuni	eval "$RESTARTCMD"
2363f2a60a1SPedro F. Giffunielse
2373f2a60a1SPedro F. Giffuni	@SBINDIR@/resolvconf -r ${libc_service}
2383f2a60a1SPedro F. Giffunifi
239587392a5SHajimu UMEMOTO
240587392a5SHajimu UMEMOTOretval=0
241587392a5SHajimu UMEMOTO# Notify users of the resolver
242587392a5SHajimu UMEMOTOfor script in "$LIBEXECDIR"/libc.d/*; do
243587392a5SHajimu UMEMOTO	if [ -f "$script" ]; then
244587392a5SHajimu UMEMOTO		if [ -x "$script" ]; then
245587392a5SHajimu UMEMOTO			"$script" "$@"
246587392a5SHajimu UMEMOTO		else
247d7149f4eSGlen Barber			(. "$script")
248587392a5SHajimu UMEMOTO		fi
249587392a5SHajimu UMEMOTO		retval=$(($retval + $?))
250587392a5SHajimu UMEMOTO	fi
251587392a5SHajimu UMEMOTOdone
252587392a5SHajimu UMEMOTOexit $retval
253