xref: /freebsd/usr.sbin/bsdconfig/networking/share/resolv.subr (revision 7079fc4e0236ff334c2fb0f4367159be6df39332)
1ab2043b8SDevin Teskeif [ ! "$_NETWORKING_RESOLV_SUBR" ]; then _NETWORKING_RESOLV_SUBR=1
2ab2043b8SDevin Teske#
37323adacSDevin Teske# Copyright (c) 2006-2013 Devin Teske
4ab2043b8SDevin Teske# All Rights Reserved.
5ab2043b8SDevin Teske#
6ab2043b8SDevin Teske# Redistribution and use in source and binary forms, with or without
7ab2043b8SDevin Teske# modification, are permitted provided that the following conditions
8ab2043b8SDevin Teske# are met:
9ab2043b8SDevin Teske# 1. Redistributions of source code must retain the above copyright
10ab2043b8SDevin Teske#    notice, this list of conditions and the following disclaimer.
11ab2043b8SDevin Teske# 2. Redistributions in binary form must reproduce the above copyright
12ab2043b8SDevin Teske#    notice, this list of conditions and the following disclaimer in the
13ab2043b8SDevin Teske#    documentation and/or other materials provided with the distribution.
14ab2043b8SDevin Teske#
15ab2043b8SDevin Teske# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16ab2043b8SDevin Teske# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, THE
17ab2043b8SDevin Teske# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18ab2043b8SDevin Teske# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19ab2043b8SDevin Teske# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20ab2043b8SDevin Teske# DAMAGES (INLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21ab2043b8SDevin Teske# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22ab2043b8SDevin Teske# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23ab2043b8SDevin Teske# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24ab2043b8SDevin Teske# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25ab2043b8SDevin Teske# SUCH DAMAGE.
26ab2043b8SDevin Teske#
27ab2043b8SDevin Teske# $FreeBSD$
28ab2043b8SDevin Teske#
29ab2043b8SDevin Teske############################################################ INCLUDES
30ab2043b8SDevin Teske
31ab2043b8SDevin TeskeBSDCFG_SHARE="/usr/share/bsdconfig"
32ab2043b8SDevin Teske. $BSDCFG_SHARE/common.subr || exit 1
3356961fd7SDevin Teskef_dprintf "%s: loading includes..." networking/resolv.subr
34ab2043b8SDevin Teskef_include $BSDCFG_SHARE/dialog.subr
357323adacSDevin Teskef_include $BSDCFG_SHARE/media/tcpip.subr
36ab2043b8SDevin Teskef_include $BSDCFG_SHARE/networking/common.subr
37ab2043b8SDevin Teskef_include $BSDCFG_SHARE/networking/ipaddr.subr
381de60ff0SDevin Teskef_include $BSDCFG_SHARE/strings.subr
39ab2043b8SDevin Teske
40ab2043b8SDevin TeskeBSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="120.networking"
41ab2043b8SDevin Teskef_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
42ab2043b8SDevin Teske
43ab2043b8SDevin Teske############################################################ CONFIGURATION
44ab2043b8SDevin Teske
45ab2043b8SDevin Teske#
46ab2043b8SDevin Teske# When updating resolv.conf(5), should we populate the `search' directive with
47ab2043b8SDevin Teske# all possible sub-domains? In example, if the domain is "sub.domain.com", when
48ab2043b8SDevin Teske# the below option is set to 1, include both "sub.domain.com" and "domain.com"
49ab2043b8SDevin Teske# in the `search' directive, otherwise use only "sub.domain.com".
50ab2043b8SDevin Teske#
51ab2043b8SDevin Teske# When enabled (set to 1), specify the minimum number of dots required for each
52ab2043b8SDevin Teske# `search' domain by setting the second option below, `RESOLVER_SEARCH_NDOTS'.
53ab2043b8SDevin Teske#
54ab2043b8SDevin Teske: ${RESOLVER_SEARCH_DOMAINS_ALL:=1}
55ab2043b8SDevin Teske: ${RESOLVER_SEARCH_NDOTS:=1}
56ab2043b8SDevin Teske
57ab2043b8SDevin Teske############################################################ FUNCTIONS
58ab2043b8SDevin Teske
59ab2043b8SDevin Teske# f_resolv_conf_domain
60ab2043b8SDevin Teske#
61ab2043b8SDevin Teske# Returns the domain configured in resolv.conf(5).
62ab2043b8SDevin Teske#
63ab2043b8SDevin Teskef_resolv_conf_domain()
64ab2043b8SDevin Teske{
65fb7d723eSDevin Teske	tail -r "$RESOLV_CONF" 2> /dev/null | awk \
66ab2043b8SDevin Teske	'
67ab2043b8SDevin Teske		BEGIN { found = 0 }
68ab2043b8SDevin Teske		( tolower($1) == "domain" ) \
69ab2043b8SDevin Teske		{
70ab2043b8SDevin Teske			print $2
71ab2043b8SDevin Teske			found = 1
72ab2043b8SDevin Teske			exit
73ab2043b8SDevin Teske		}
74ab2043b8SDevin Teske		END { exit ! found }
75ab2043b8SDevin Teske	'
76ab2043b8SDevin Teske}
77ab2043b8SDevin Teske
78ab2043b8SDevin Teske# f_resolv_conf_search
79ab2043b8SDevin Teske#
80ab2043b8SDevin Teske# Returns the search configured in resolv.conf(5).
81ab2043b8SDevin Teske#
82ab2043b8SDevin Teskef_resolv_conf_search()
83ab2043b8SDevin Teske{
84fb7d723eSDevin Teske	tail -r "$RESOLV_CONF" 2> /dev/null | awk \
85ab2043b8SDevin Teske	'
86ab2043b8SDevin Teske		BEGIN { found = 0 }
87ab2043b8SDevin Teske		{
88ab2043b8SDevin Teske			tl0 = tolower($0)
89ab2043b8SDevin Teske			if ( match(tl0, /^[[:space:]]*search[[:space:]]+/) ) {
90ab2043b8SDevin Teske				search = substr($0, RLENGTH + 1)
91ab2043b8SDevin Teske				sub(/[[:space:]]*#.*$/, "", search)
92ab2043b8SDevin Teske				gsub(/[[:space:]]+/, " ", search)
93ab2043b8SDevin Teske				print search
94ab2043b8SDevin Teske				found = 1
95ab2043b8SDevin Teske				exit
96ab2043b8SDevin Teske			}
97ab2043b8SDevin Teske		}
98ab2043b8SDevin Teske		END { exit ! found }
99ab2043b8SDevin Teske	'
100ab2043b8SDevin Teske}
101ab2043b8SDevin Teske
102ab2043b8SDevin Teske# f_dialog_resolv_conf_update $hostname
103ab2043b8SDevin Teske#
104ab2043b8SDevin Teske# Updates the search/domain directives in resolv.conf(5) given a valid fully-
105ab2043b8SDevin Teske# qualified hostname.
106ab2043b8SDevin Teske#
107ab2043b8SDevin Teske# This function is a two-parter. Below is the awk(1) portion of the function,
108ab2043b8SDevin Teske# afterward is the sh(1) function which utilizes the below awk script.
109ab2043b8SDevin Teske#
110ab2043b8SDevin Teskef_dialog_resolv_conf_update_awk='
111ab2043b8SDevin Teske# Variables that should be defined on the invocation line:
112ab2043b8SDevin Teske# 	-v domain="domain"
113ab2043b8SDevin Teske# 	-v search_all="0|1"
114ab2043b8SDevin Teske# 	-v search_ndots="1+"
115ab2043b8SDevin Teske#
116ab2043b8SDevin TeskeBEGIN {
117ab2043b8SDevin Teske	domain_found = search_found = 0
118ab2043b8SDevin Teske
119ab2043b8SDevin Teske	if ( search_all ) {
120ab2043b8SDevin Teske		search = ""
121ab2043b8SDevin Teske		subdomain = domain
122ab2043b8SDevin Teske		if ( search_ndots < 1 )
123ab2043b8SDevin Teske			search_ndots = 1
124ab2043b8SDevin Teske
125ab2043b8SDevin Teske		ndots = split(subdomain, labels, ".") - 1
126ab2043b8SDevin Teske		while ( ndots-- >= search_ndots ) {
127ab2043b8SDevin Teske			if ( length(search) ) search = search " "
128ab2043b8SDevin Teske			search = search subdomain
129ab2043b8SDevin Teske			sub(/[^.]*\./, "", subdomain)
130ab2043b8SDevin Teske		}
131ab2043b8SDevin Teske	}
132ab2043b8SDevin Teske	else search = domain
133ab2043b8SDevin Teske}
134ab2043b8SDevin Teske{
135ab2043b8SDevin Teske	if ( domain_found && search_found ) { print; next }
136ab2043b8SDevin Teske
137ab2043b8SDevin Teske	tl0 = tolower($0)
138ab2043b8SDevin Teske	if ( ! domain_found && \
139ab2043b8SDevin Teske	     match(tl0, /^[[:space:]]*domain[[:space:]]+/) ) \
140ab2043b8SDevin Teske	{
141ab2043b8SDevin Teske		if ( length(domain) ) {
142ab2043b8SDevin Teske			printf "%s%s\n", substr($0, 0, RLENGTH), domain
143ab2043b8SDevin Teske			domain_found = 1
144ab2043b8SDevin Teske		}
145ab2043b8SDevin Teske	}
146ab2043b8SDevin Teske	else if ( ! search_found && \
147ab2043b8SDevin Teske	          match(tl0, /^[[:space:]]*search[[:space:]]+/) ) \
148ab2043b8SDevin Teske	{
149ab2043b8SDevin Teske		if ( length(search) ) {
150ab2043b8SDevin Teske			printf "%s%s\n", substr($0, 0, RLENGTH), search
151ab2043b8SDevin Teske			search_found = 1
152ab2043b8SDevin Teske		}
153ab2043b8SDevin Teske	}
154ab2043b8SDevin Teske	else print
155ab2043b8SDevin Teske}
156ab2043b8SDevin TeskeEND {
157ab2043b8SDevin Teske	if ( ! search_found && length(search) )
158ab2043b8SDevin Teske		printf "search\t%s\n", search
159ab2043b8SDevin Teske	if ( ! domain_found && length(domain) )
160ab2043b8SDevin Teske		printf "domain\t%s\n", domain
161ab2043b8SDevin Teske}
162ab2043b8SDevin Teske'
163ab2043b8SDevin Teskef_dialog_resolv_conf_update()
164ab2043b8SDevin Teske{
165ab2043b8SDevin Teske	local hostname="$1"
166ab2043b8SDevin Teske
167ab2043b8SDevin Teske	#
168ab2043b8SDevin Teske	# Extrapolate the desired domain search parameter for resolv.conf(5)
169ab2043b8SDevin Teske	#
170ab2043b8SDevin Teske	local search ndots domain="${hostname#*.}"
171ab2043b8SDevin Teske	if [ "$RESOLVER_SEARCH_DOMAINS_ALL" = "1" ]; then
172ab2043b8SDevin Teske		search=""
173ab2043b8SDevin Teske		ndots=$( IFS=.; set -- $domain; echo $(( $# - 1 )) )
174ab2043b8SDevin Teske		while [ $ndots -ge ${RESOLVER_SEARCH_NDOTS:-1} ]; do
175ab2043b8SDevin Teske			search="$search${search:+ }$domain"
176ab2043b8SDevin Teske			domain="${domain#*.}"
177ab2043b8SDevin Teske			ndots=$(( $ndots - 1 ))
178ab2043b8SDevin Teske		done
179ab2043b8SDevin Teske		domain="${hostname#*.}"
180ab2043b8SDevin Teske	else
181ab2043b8SDevin Teske		search="$domain"
182ab2043b8SDevin Teske	fi
183ab2043b8SDevin Teske
184ab2043b8SDevin Teske	#
185ab2043b8SDevin Teske	# Save domain/search information only if different from resolv.conf(5)
186ab2043b8SDevin Teske	#
187ab2043b8SDevin Teske	if [ "$domain" != "$( f_resolv_conf_domain )" -o \
188ab2043b8SDevin Teske	     "$search" != "$( f_resolv_conf_search )" ]
189ab2043b8SDevin Teske	then
190ab2043b8SDevin Teske		f_dialog_info "Saving new domain/search settings" \
191ab2043b8SDevin Teske		              "to resolv.conf(5)..."
192ab2043b8SDevin Teske
193ab2043b8SDevin Teske		#
194ab2043b8SDevin Teske		# Create a new temporary file to write our resolv.conf(5)
195ab2043b8SDevin Teske		# update with our new `domain' and `search' directives.
196ab2043b8SDevin Teske		#
197ab2043b8SDevin Teske		local tmpfile="$( mktemp -t "$pgm" )"
198ab2043b8SDevin Teske		[ "$tmpfile" ] || return $FAILURE
199ab2043b8SDevin Teske
200ab2043b8SDevin Teske		#
201ab2043b8SDevin Teske		# Fixup permissions and ownership (mktemp(1) creates the
202ab2043b8SDevin Teske		# temporary file with 0600 permissions -- change the
203ab2043b8SDevin Teske		# permissions and ownership to match resolv.conf(5) before
204ab2043b8SDevin Teske		# we write it out and mv(1) it into place).
205ab2043b8SDevin Teske		#
206fb7d723eSDevin Teske		local mode="$( stat -f '%#Lp' "$RESOLV_CONF" 2> /dev/null )"
207fb7d723eSDevin Teske		local owner="$( stat -f '%u:%g' "$RESOLV_CONF" 2> /dev/null )"
208ab2043b8SDevin Teske		f_quietly chmod "${mode:-0644}" "$tmpfile"
209ab2043b8SDevin Teske		f_quietly chown "${owner:-root:wheel}" "$tmpfile"
210ab2043b8SDevin Teske
211ab2043b8SDevin Teske		#
212ab2043b8SDevin Teske		# Operate on resolv.conf(5), replacing only the last
213ab2043b8SDevin Teske		# occurrences of `domain' and `search' directives (or add
214ab2043b8SDevin Teske		# them to the top if not found), in strict-adherence to the
215ab2043b8SDevin Teske		# following entry in resolver(5):
216ab2043b8SDevin Teske		#
217ab2043b8SDevin Teske		# 	The domain and search keywords are mutually exclusive.
218ab2043b8SDevin Teske		# 	If more than one instance of these keywords is present,
219ab2043b8SDevin Teske		# 	the last instance will override.
220ab2043b8SDevin Teske		#
221ab2043b8SDevin Teske		# NOTE: If RESOLVER_SEARCH_DOMAINS_ALL is set to `1' in the
222ab2043b8SDevin Teske		# environment, all sub-domains will be added to the `search'
223ab2043b8SDevin Teske		# directive, not just the FQDN.
224ab2043b8SDevin Teske		#
225ab2043b8SDevin Teske		local domain="${hostname#*.}" new_contents
226ab2043b8SDevin Teske		[ "$domain" = "$hostname" ] && domain=
227fb7d723eSDevin Teske		new_contents=$( tail -r "$RESOLV_CONF" 2> /dev/null )
228ab2043b8SDevin Teske		new_contents=$( echo "$new_contents" | awk \
229ab2043b8SDevin Teske			-v domain="$domain" \
230ab2043b8SDevin Teske			-v search_all="${RESOLVER_SEARCH_DOMAINS_ALL:-1}" \
231ab2043b8SDevin Teske			-v search_ndots="${RESOLVER_SEARCH_NDOTS:-1}" \
232ab2043b8SDevin Teske			"$f_dialog_resolv_conf_update_awk" )
233ab2043b8SDevin Teske
234ab2043b8SDevin Teske		#
235ab2043b8SDevin Teske		# Write the temporary file contents and move the temporary
236ab2043b8SDevin Teske		# file into place.
237ab2043b8SDevin Teske		#
238ab2043b8SDevin Teske		echo "$new_contents" | tail -r > "$tmpfile" || return $FAILURE
239ab2043b8SDevin Teske		f_quietly mv "$tmpfile" "$RESOLV_CONF"
240ab2043b8SDevin Teske
241ab2043b8SDevin Teske	fi
242ab2043b8SDevin Teske}
243ab2043b8SDevin Teske
244ab2043b8SDevin Teske# f_dialog_input_nameserver [ $n $nameserver ]
245ab2043b8SDevin Teske#
246ab2043b8SDevin Teske# Allows the user to edit a given nameserver. The first argument is the
247ab2043b8SDevin Teske# resolv.conf(5) nameserver ``instance'' integer. For example, this will be one
248ab2043b8SDevin Teske# if editing the first nameserver instance, two if editing the second, three if
249ab2043b8SDevin Teske# the third, ad nauseum. If this argument is zero, null, or missing, the value
250ab2043b8SDevin Teske# entered by the user (if non-null) will be added to resolv.conf(5) as a new
251ab2043b8SDevin Teske# `nameserver' entry. The second argument is the IPv4 address of the nameserver
252ab2043b8SDevin Teske# to be edited -- this will be displayed as the initial value during the edit.
253ab2043b8SDevin Teske#
254ab2043b8SDevin Teske# Taint-checking is performed when editing an existing entry (when the second
255ab2043b8SDevin Teske# argument is one or higher) in that the first argument must match the current
256ab2043b8SDevin Teske# value of the Nth `nameserver' instance in resolv.conf(5) else an error is
257ab2043b8SDevin Teske# generated discarding any/all changes.
258ab2043b8SDevin Teske#
259ab2043b8SDevin Teske# This function is a two-parter. Below is the awk(1) portion of the function,
260ab2043b8SDevin Teske# afterward is the sh(1) function which utilizes the below awk script.
261ab2043b8SDevin Teske#
262ab2043b8SDevin Teskef_dialog_input_nameserver_edit_awk='
263ab2043b8SDevin Teske# Variables that should be defined on the invocation line:
264ab2043b8SDevin Teske# 	-v nsindex="1+"
265ab2043b8SDevin Teske# 	-v old_value="..."
266ab2043b8SDevin Teske# 	-v new_value="..."
267ab2043b8SDevin Teske#
268ab2043b8SDevin TeskeBEGIN {
269ab2043b8SDevin Teske	if ( nsindex < 1 ) exit 1
270ab2043b8SDevin Teske	found = n = 0
271ab2043b8SDevin Teske}
272ab2043b8SDevin Teske{
273ab2043b8SDevin Teske	if ( found ) { print; next }
274ab2043b8SDevin Teske
275ab2043b8SDevin Teske	if ( match(tolower($0), /^[[:space:]]*nameserver[[:space:]]+/)) {
276ab2043b8SDevin Teske		if ( ++n == nsindex ) {
277ab2043b8SDevin Teske			if ( $2 != old_value ) exit 2
278ab2043b8SDevin Teske			if ( new_value != "" ) printf "%s%s\n", \
279ab2043b8SDevin Teske				substr($0, 0, RLENGTH), new_value
280ab2043b8SDevin Teske			found = 1
281ab2043b8SDevin Teske		}
282ab2043b8SDevin Teske		else print
283ab2043b8SDevin Teske	}
284ab2043b8SDevin Teske	else print
285ab2043b8SDevin Teske}
286ab2043b8SDevin TeskeEND { if ( ! found ) exit 3 }
287ab2043b8SDevin Teske'
288ab2043b8SDevin Teskef_dialog_input_nameserver()
289ab2043b8SDevin Teske{
290ab2043b8SDevin Teske	local index="${1:-0}" old_ns="$2" new_ns
291ab2043b8SDevin Teske	local ns="$old_ns"
292ab2043b8SDevin Teske
293ab2043b8SDevin Teske	#
294ab2043b8SDevin Teske	# Perform sanity checks
295ab2043b8SDevin Teske	#
296ab2043b8SDevin Teske	f_isinteger "$index" || return $FAILURE
297ab2043b8SDevin Teske	[ $index -ge 0 ] || return $FAILURE
298ab2043b8SDevin Teske
299ab2043b8SDevin Teske	local msg
300ab2043b8SDevin Teske	if [ $index -gt 0 ]; then
301ab2043b8SDevin Teske		if [ "$USE_XDIALOG" ]; then
302ab2043b8SDevin Teske			msg="$xmsg_please_enter_nameserver_existing"
303ab2043b8SDevin Teske		else
304ab2043b8SDevin Teske			msg="$msg_please_enter_nameserver_existing"
305ab2043b8SDevin Teske		fi
306ab2043b8SDevin Teske	else
307ab2043b8SDevin Teske		msg="$msg_please_enter_nameserver"
308ab2043b8SDevin Teske	fi
309ab2043b8SDevin Teske
310ab2043b8SDevin Teske	#
311ab2043b8SDevin Teske	# Loop until the user provides taint-free input.
312ab2043b8SDevin Teske	#
313ab2043b8SDevin Teske	while :; do
314ec7120b5SDevin Teske		f_dialog_input new_ns "$msg" "$ns" \
315ec7120b5SDevin Teske		               "$hline_num_punc_tab_enter" || return
316ab2043b8SDevin Teske
317ab2043b8SDevin Teske		# Take only the first "word" of the user's input
318ab2043b8SDevin Teske		new_ns="${new_ns%%[$IFS]*}"
319ab2043b8SDevin Teske
320ab2043b8SDevin Teske		# Taint-check the user's input
321ab2043b8SDevin Teske		[ "$new_ns" ] || break
322ab2043b8SDevin Teske		f_dialog_validate_ipaddr "$new_ns" && break
323ab2043b8SDevin Teske
324ab2043b8SDevin Teske		# Update prompt to allow user to re-edit previous entry
325ab2043b8SDevin Teske		ns="$new_ns"
326ab2043b8SDevin Teske	done
327ab2043b8SDevin Teske
328ab2043b8SDevin Teske	#
329ab2043b8SDevin Teske	# Save only if the user changed the nameserver.
330ab2043b8SDevin Teske	#
331ab2043b8SDevin Teske	if [ $index -eq "0" -a "$new_ns" ]; then
332ab2043b8SDevin Teske		f_dialog_info "$msg_saving_nameserver"
333ab2043b8SDevin Teske		printf "nameserver\t%s\n" "$new_ns" >> "$RESOLV_CONF"
334ab2043b8SDevin Teske		return $SUCCESS
335ab2043b8SDevin Teske	elif [ $index -gt 0 -a "$old_ns" != "$new_ns" ]; then
336ab2043b8SDevin Teske		if [ "$new_ns" ]; then
337ab2043b8SDevin Teske			msg="$msg_saving_nameserver_existing"
338ab2043b8SDevin Teske		else
339ab2043b8SDevin Teske			msg="$msg_removing_nameserver"
340ab2043b8SDevin Teske		fi
341ab2043b8SDevin Teske		f_dialog_info "$msg"
342ab2043b8SDevin Teske
343ab2043b8SDevin Teske		#
344ab2043b8SDevin Teske		# Create a new temporary file to write our new resolv.conf(5)
345ab2043b8SDevin Teske		#
346ab2043b8SDevin Teske		local tmpfile="$( mktemp -t "$pgm" )"
347ab2043b8SDevin Teske		[ "$tmpfile" ] || return $FAILURE
348ab2043b8SDevin Teske
349ab2043b8SDevin Teske		#
350ab2043b8SDevin Teske		# Quietly fixup permissions and ownership
351ab2043b8SDevin Teske		#
352ab2043b8SDevin Teske		local mode owner
353fb7d723eSDevin Teske		mode=$( stat -f '%#Lp' "$RESOLV_CONF" 2> /dev/null )
354fb7d723eSDevin Teske		owner=$( stat -f '%u:%g' "$RESOLV_CONF" 2> /dev/null )
355ab2043b8SDevin Teske		f_quietly chmod "${mode:-0644}" "$tmpfile"
356ab2043b8SDevin Teske		f_quietly chown "${owner:-root:wheel}" "$tmpfile"
357ab2043b8SDevin Teske
358ab2043b8SDevin Teske		#
359ab2043b8SDevin Teske		# Operate on resolv.conf(5)
360ab2043b8SDevin Teske		#
361ab2043b8SDevin Teske		local new_contents
362ab2043b8SDevin Teske		new_contents=$( awk -v nsindex="$index"    \
363ab2043b8SDevin Teske		                    -v old_value="$old_ns" \
364ab2043b8SDevin Teske		                    -v new_value="$new_ns" \
365ab2043b8SDevin Teske		                    "$f_dialog_input_nameserver_edit_awk" \
366ab2043b8SDevin Teske		                    "$RESOLV_CONF" )
367ab2043b8SDevin Teske
368ab2043b8SDevin Teske		#
369ab2043b8SDevin Teske		# Produce an appropriate error message if necessary.
370ab2043b8SDevin Teske		#
371ab2043b8SDevin Teske		local retval=$?
372ab2043b8SDevin Teske		case $retval in
373ab2043b8SDevin Teske		1) f_die 1 "$msg_internal_error_nsindex_value" "$nsindex" ;;
374*7079fc4eSDevin Teske		2) f_show_msg "$msg_resolv_conf_changed_while_editing"
375ab2043b8SDevin Teske		   return $retval ;;
376*7079fc4eSDevin Teske		3) f_show_msg "$msg_resolv_conf_entry_no_longer_exists"
377ab2043b8SDevin Teske		   return $retval ;;
378ab2043b8SDevin Teske		esac
379ab2043b8SDevin Teske
380ab2043b8SDevin Teske		#
381ab2043b8SDevin Teske		# Write the temporary file contents and move the temporary
382ab2043b8SDevin Teske		# file into place.
383ab2043b8SDevin Teske		#
384ab2043b8SDevin Teske		echo "$new_contents" > "$tmpfile" || return $FAILURE
385ab2043b8SDevin Teske		f_quietly mv "$tmpfile" "$RESOLV_CONF"
386ab2043b8SDevin Teske	fi
387ab2043b8SDevin Teske}
388ab2043b8SDevin Teske
389ab2043b8SDevin Teske# f_dialog_menu_nameservers
390ab2043b8SDevin Teske#
391ab2043b8SDevin Teske# Edit the nameservers in resolv.conf(5).
392ab2043b8SDevin Teske#
393ab2043b8SDevin Teskef_dialog_menu_nameservers()
394ab2043b8SDevin Teske{
395052f8969SDevin Teske	local prompt="$msg_dns_configuration"
396052f8969SDevin Teske	local menu_list # Calculated below
397052f8969SDevin Teske	local hline="$hline_arrows_tab_enter"
398052f8969SDevin Teske	local defaultitem=
39974036c4dSDevin Teske
40074036c4dSDevin Teske	local height width rows
401ab2043b8SDevin Teske	local opt_exit="$msg_return_to_previous_menu"
402ab2043b8SDevin Teske	local opt_add="$msg_add_nameserver"
403ab2043b8SDevin Teske
404ab2043b8SDevin Teske	#
405ab2043b8SDevin Teske	# Loop forever until the user has finished configuring nameservers
406ab2043b8SDevin Teske	#
407ab2043b8SDevin Teske	while :; do
408ab2043b8SDevin Teske		#
409ab2043b8SDevin Teske		# Re/Build list of nameservers
410ab2043b8SDevin Teske		#
411ab2043b8SDevin Teske		local nameservers="$( f_resolv_conf_nameservers )"
412052f8969SDevin Teske		menu_list=$(
413ab2043b8SDevin Teske			index=1
414ab2043b8SDevin Teske
415ab2043b8SDevin Teske			echo "'X $msg_exit' '$opt_exit'"
416ab2043b8SDevin Teske			index=$(( $index + 1 ))
417ab2043b8SDevin Teske
418ab2043b8SDevin Teske			echo "'A $msg_add'  '$opt_add'"
419ab2043b8SDevin Teske			index=$(( $index + 1 ))
420ab2043b8SDevin Teske
421ab2043b8SDevin Teske			for ns in $nameservers; do
422ab2043b8SDevin Teske				[ $index -lt ${#DIALOG_MENU_TAGS} ] || break
423ab2043b8SDevin Teske				tag=$( f_substr "$DIALOG_MENU_TAGS" $index 1 )
424ab2043b8SDevin Teske				echo "'$tag nameserver' '$ns'"
425ab2043b8SDevin Teske				index=$(( $index + 1 ))
426ab2043b8SDevin Teske			done
427052f8969SDevin Teske		)
428ab2043b8SDevin Teske
429ab2043b8SDevin Teske		#
430ab2043b8SDevin Teske		# Display configuration-edit menu
431ab2043b8SDevin Teske		#
43274036c4dSDevin Teske		eval f_dialog_menu_size height width rows \
433ab2043b8SDevin Teske		                        \"\$DIALOG_TITLE\"     \
434ab2043b8SDevin Teske		                        \"\$DIALOG_BACKTITLE\" \
435ab2043b8SDevin Teske		                        \"\$prompt\"           \
436ab2043b8SDevin Teske		                        \"\$hline\"            \
43774036c4dSDevin Teske		                        $menu_list
438fd962ac6SDevin Teske		local tag
439fd962ac6SDevin Teske		tag=$( eval $DIALOG \
440d3a0f918SDevin Teske			--title \"\$DIALOG_TITLE\"         \
441ab2043b8SDevin Teske			--backtitle \"\$DIALOG_BACKTITLE\" \
442ab2043b8SDevin Teske			--hline \"\$hline\"                \
443ab2043b8SDevin Teske			--ok-label \"\$msg_ok\"            \
444ab2043b8SDevin Teske			--cancel-label \"\$msg_cancel\"    \
445d3a0f918SDevin Teske			--default-item \"\$defaultitem\"   \
44674036c4dSDevin Teske			--menu \"\$prompt\"                \
44774036c4dSDevin Teske			$height $width $rows               \
448ab2043b8SDevin Teske			$menu_list                         \
44989498fdfSDevin Teske			2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
45089498fdfSDevin Teske		)
451ab2043b8SDevin Teske		local retval=$?
452fd962ac6SDevin Teske		f_dialog_data_sanitize tag
453ab2043b8SDevin Teske
454ab2043b8SDevin Teske		# Return if "Cancel" was chosen (-1) or ESC was pressed (255)
455d3a0f918SDevin Teske		if [ $retval -ne $SUCCESS ]; then
456d3a0f918SDevin Teske			return $retval
457d3a0f918SDevin Teske		else
458d3a0f918SDevin Teske			# Only update default-item on success
459d3a0f918SDevin Teske			defaultitem="$tag"
460d3a0f918SDevin Teske		fi
461ab2043b8SDevin Teske
462ab2043b8SDevin Teske		case "$tag" in
463ab2043b8SDevin Teske		"X $msg_exit") break ;;
464ab2043b8SDevin Teske		"A $msg_add")
465ab2043b8SDevin Teske			f_dialog_input_nameserver
466ab2043b8SDevin Teske			;;
467ab2043b8SDevin Teske		*)
468fd962ac6SDevin Teske			local n ns
469ab2043b8SDevin Teske			n=$( eval f_dialog_menutag2index \"\$tag\" $menu_list )
470ab2043b8SDevin Teske			ns=$( eval f_dialog_menutag2item \"\$tag\" $menu_list )
471ab2043b8SDevin Teske			f_dialog_input_nameserver $(( $n - 2 )) "$ns"
472ab2043b8SDevin Teske			;;
473ab2043b8SDevin Teske		esac
474ab2043b8SDevin Teske	done
475ab2043b8SDevin Teske}
476ab2043b8SDevin Teske
47756961fd7SDevin Teske############################################################ MAIN
47856961fd7SDevin Teske
47956961fd7SDevin Teskef_dprintf "%s: Successfully loaded." networking/resolv.subr
48056961fd7SDevin Teske
481ab2043b8SDevin Teskefi # ! $_NETWORKING_RESOLV_SUBR
482