xref: /freebsd/usr.sbin/bsdconfig/networking/share/media.subr (revision 1de60ff0f42859d11580dc1a6978148460b8c6f0)
1ab2043b8SDevin Teskeif [ ! "$_NETWORKING_MEDIA_SUBR" ]; then _NETWORKING_MEDIA_SUBR=1
2ab2043b8SDevin Teske#
3d3a0f918SDevin 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/media.subr
34ab2043b8SDevin Teskef_include $BSDCFG_SHARE/dialog.subr
35*1de60ff0SDevin Teskef_include $BSDCFG_SHARE/networking/common.subr
36ab2043b8SDevin Teskef_include $BSDCFG_SHARE/strings.subr
37ab2043b8SDevin Teskef_include $BSDCFG_SHARE/sysrc.subr
38ab2043b8SDevin Teske
39ab2043b8SDevin TeskeBSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="120.networking"
40ab2043b8SDevin Teskef_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
41ab2043b8SDevin Teske
42ab2043b8SDevin Teske############################################################ FUNCTIONS
43ab2043b8SDevin Teske
44ab2043b8SDevin Teske# f_ifconfig_options $interface
45ab2043b8SDevin Teske#
46ab2043b8SDevin Teske# Returns any/all extra ifconfig(8) parameters associated with $interface.
47ab2043b8SDevin Teske#
48ab2043b8SDevin Teskef_ifconfig_options()
49ab2043b8SDevin Teske{
50ab2043b8SDevin Teske	local interface="$1"
51ab2043b8SDevin Teske	[ "$interface" ] || return $SUCCESS
52ab2043b8SDevin Teske
53ab2043b8SDevin Teske	#
54ab2043b8SDevin Teske	# Loop over the options, removing what we don't want
55ab2043b8SDevin Teske	#
56ab2043b8SDevin Teske	(
57ab2043b8SDevin Teske		set -- $( f_sysrc_get ifconfig_$interface )
58ab2043b8SDevin Teske
59ab2043b8SDevin Teske		#
60ab2043b8SDevin Teske		# Return if the interface is configured for DHCP
61ab2043b8SDevin Teske		#
62ab2043b8SDevin Teske		glob="[Dd][Hh][Cc][Pp]"
63ab2043b8SDevin Teske		case "$*" in
64ab2043b8SDevin Teske		$glob|[Ss][Yy][Nn][Cc]$glob|[Nn][Oo][Ss][Yy][Nn][Cc]$glob)
65ab2043b8SDevin Teske			exit $SUCCESS
66ab2043b8SDevin Teske		esac
67ab2043b8SDevin Teske
68ab2043b8SDevin Teske		output=
69ab2043b8SDevin Teske		while [ $# -gt 0 ]; do
70ab2043b8SDevin Teske			case "$1" in
71ab2043b8SDevin Teske			inet|netmask) shift 1;;
72ab2043b8SDevin Teske			*) output="$output${output:+ }$1"
73ab2043b8SDevin Teske			esac
74ab2043b8SDevin Teske			shift 1
75ab2043b8SDevin Teske		done
76ab2043b8SDevin Teske		echo "$output"
77ab2043b8SDevin Teske	)
78ab2043b8SDevin Teske}
79ab2043b8SDevin Teske
80ab2043b8SDevin Teske# f_ifconfig_media $interface
81ab2043b8SDevin Teske#
82ab2043b8SDevin Teske# Returns list of supported media for $interface.
83ab2043b8SDevin Teske#
84ab2043b8SDevin Teskef_ifconfig_media()
85ab2043b8SDevin Teske{
86ab2043b8SDevin Teske	local interface="$1"
87fb7d723eSDevin Teske	ifconfig -m "$interface" 2> /dev/null | awk \
88ab2043b8SDevin Teske	'
89ab2043b8SDevin Teske		BEGIN { media_found = 0 }
90ab2043b8SDevin Teske		{
91ab2043b8SDevin Teske			if ( media_found == 1 ) { print; next }
92ab2043b8SDevin Teske		}
93ab2043b8SDevin Teske		( $1 $2 == "supported" "media:" ) \
94ab2043b8SDevin Teske		{
95ab2043b8SDevin Teske			media_found = 1
96ab2043b8SDevin Teske			next
97ab2043b8SDevin Teske		}
98ab2043b8SDevin Teske		END { exit ! media_found }
99ab2043b8SDevin Teske	'
100ab2043b8SDevin Teske}
101ab2043b8SDevin Teske
102ab2043b8SDevin Teske# f_dialog_input_options $interface
103ab2043b8SDevin Teske#
104f4844f65SDevin Teske# Input custom interface options. If the user does not press ESC or choose
105f4844f65SDevin Teske# Cancel/No, $options will hold the user's input. Default input is taken from
106f4844f65SDevin Teske# the same variable ($options).
107ab2043b8SDevin Teske#
108ab2043b8SDevin Teskef_dialog_input_options()
109ab2043b8SDevin Teske{
110ab2043b8SDevin Teske	local interface="$1"
111ab2043b8SDevin Teske
112ab2043b8SDevin Teske	#
113ab2043b8SDevin Teske	# Return with-error when there are NFS-mounts currently active. If the
114ab2043b8SDevin Teske	# options are changed while NFS-exported directories are mounted,
115ab2043b8SDevin Teske	# the system may hang (if any NFS mounts are using that interface).
116ab2043b8SDevin Teske	#
117ab2043b8SDevin Teske	if f_nfs_mounted && ! f_jailed; then
118ab2043b8SDevin Teske		local setting="$( printf "$msg_current_options" \
119ab2043b8SDevin Teske		                         "$interface" "$options" )"
120fa2e39c8SDevin Teske		f_show_msg "$msg_nfs_mounts_may_cause_hang" "$setting"
121ab2043b8SDevin Teske		return $FAILURE
122ab2043b8SDevin Teske	fi
123ab2043b8SDevin Teske
124ab2043b8SDevin Teske	local msg="$( printf "$msg_please_enter_mediaopts" "$interface" )"
125ab2043b8SDevin Teske	local hline="$hline_alnum_punc_tab_enter"
126ab2043b8SDevin Teske
127ec7120b5SDevin Teske	local _options
128ec7120b5SDevin Teske	_options=$( $DIALOG \
129ab2043b8SDevin Teske		--title "$DIALOG_TITLE"         \
130ab2043b8SDevin Teske		--backtitle "$DIALOG_BACKTITLE" \
131ab2043b8SDevin Teske		--hline "$hline"                \
132ab2043b8SDevin Teske		--ok-label "$msg_ok"            \
133ab2043b8SDevin Teske		--cancel-label "$msg_cancel"    \
134ab2043b8SDevin Teske		--inputbox "$msg" 9 70          \
135ab2043b8SDevin Teske		"$options"                      \
13689498fdfSDevin Teske		2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
13789498fdfSDevin Teske	)
138ab2043b8SDevin Teske	local retval=$?
139ec7120b5SDevin Teske	f_dialog_line_sanitize _options
140ab2043b8SDevin Teske
141ab2043b8SDevin Teske	[ $retval -eq $SUCCESS ] && options="$_options"
142ab2043b8SDevin Teske
143ab2043b8SDevin Teske	return $retval
144ab2043b8SDevin Teske}
145ab2043b8SDevin Teske
146ab2043b8SDevin Teske# f_dialog_menu_media_options $interface
147ab2043b8SDevin Teske#
148ab2043b8SDevin Teske# Display a menu of additional media options for the given network interface.
149ab2043b8SDevin Teske#
150ab2043b8SDevin Teskef_dialog_menu_media_options()
151ab2043b8SDevin Teske{
152ab2043b8SDevin Teske	local interface="$1" _options="$2"
153ab2043b8SDevin Teske	#
154ab2043b8SDevin Teske	# Not all network interfaces support additional media options, but
155ab2043b8SDevin Teske	# when available we should prompt the user to select from a list
156ab2043b8SDevin Teske	# of available options (or none, as is the first/default option).
157ab2043b8SDevin Teske	#
158ab2043b8SDevin Teske
159ab2043b8SDevin Teske	#
160ab2043b8SDevin Teske	# Return with-error when there are NFS-mounts currently active. If the
161ab2043b8SDevin Teske	# media options are changed while NFS-exported directories are mounted,
162ab2043b8SDevin Teske	# the system may hang (if any NFS mounts are using that interface).
163ab2043b8SDevin Teske	#
164ab2043b8SDevin Teske	if f_nfs_mounted && ! f_jailed; then
165ab2043b8SDevin Teske		local setting="$( printf "$msg_current_options" \
166ab2043b8SDevin Teske		                         "$interface" "$_options" )"
167fa2e39c8SDevin Teske		f_show_msg "$msg_nfs_mounts_may_cause_hang" "$setting"
168ab2043b8SDevin Teske		return $FAILURE
169ab2043b8SDevin Teske	fi
170ab2043b8SDevin Teske
171ab2043b8SDevin Teske	#
172ab2043b8SDevin Teske	# Build list of additional media options
173ab2043b8SDevin Teske	#
174ab2043b8SDevin Teske	local opt_none="$msg_no_options"
175ab2043b8SDevin Teske	local opt_cust="$msg_custom"
176ab2043b8SDevin Teske	local supported_media="$(
177ab2043b8SDevin Teske		f_ifconfig_media $interface | \
178ab2043b8SDevin Teske		( index=1
179ab2043b8SDevin Teske
180ab2043b8SDevin Teske		  echo "'$( f_substr "$DIALOG_MENU_TAGS" $index 1 )'"
181ab2043b8SDevin Teske		  echo "'$opt_none'"
182ab2043b8SDevin Teske		  index=$(( $index + 1 ))
183ab2043b8SDevin Teske
184ab2043b8SDevin Teske		  echo "'$( f_substr "$DIALOG_MENU_TAGS" $index 1 )'"
185ab2043b8SDevin Teske		  echo "'$opt_cust'"
186ab2043b8SDevin Teske		  index=$(( $index + 1 ))
187ab2043b8SDevin Teske
188ab2043b8SDevin Teske		  while read media_options; do
189ab2043b8SDevin Teske		  	[ $index -lt ${#DIALOG_MENU_TAGS} ] || break
190ab2043b8SDevin Teske		  	echo "'$( f_substr "$DIALOG_MENU_TAGS" $index 1 )'"
191ab2043b8SDevin Teske		  	echo "'$media_options'"
192ab2043b8SDevin Teske		  	index=$(( $index + 1 ))
193ab2043b8SDevin Teske		  done
194ab2043b8SDevin Teske		)
195ab2043b8SDevin Teske	)"
196ab2043b8SDevin Teske
197ab2043b8SDevin Teske	local msg
198ab2043b8SDevin Teske	if [ "$USE_XDIALOG" ]; then
199ab2043b8SDevin Teske		msg=$( printf "$xmsg_supported_media_options" \
200ab2043b8SDevin Teske		       		"$interface" "$interface" )
201ab2043b8SDevin Teske	else
202ab2043b8SDevin Teske		msg=$( printf "$msg_supported_media_options" \
203ab2043b8SDevin Teske		       		"$interface" "$interface" )
204ab2043b8SDevin Teske	fi
205ab2043b8SDevin Teske
206ab2043b8SDevin Teske	local hline="$hline_arrows_tab_enter"
207ab2043b8SDevin Teske
208fd962ac6SDevin Teske	local tag
209fd962ac6SDevin Teske	tag=$( eval $DIALOG \
210d3a0f918SDevin Teske		--title \"\$DIALOG_TITLE\"         \
211ab2043b8SDevin Teske		--backtitle \"\$DIALOG_BACKTITLE\" \
212ab2043b8SDevin Teske		--hline \"\$hline\"                \
213ab2043b8SDevin Teske		--ok-label \"\$msg_ok\"            \
214ab2043b8SDevin Teske		--cancel-label \"\$msg_cancel\"    \
215ab2043b8SDevin Teske		--menu \"\$msg\" 21 60 12          \
216ab2043b8SDevin Teske		$supported_media                   \
21789498fdfSDevin Teske		2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
21889498fdfSDevin Teske	)
219ab2043b8SDevin Teske	local retval=$?
220fd962ac6SDevin Teske	f_dialog_data_sanitize tag
221fd962ac6SDevin Teske
222ab2043b8SDevin Teske	if [ $retval -eq $SUCCESS ]; then
223ab2043b8SDevin Teske		options=$( eval f_dialog_menutag2item \"\$tag\" \
224ab2043b8SDevin Teske		                                      $supported_media )
22597142e6aSDevin Teske		case "$options" in
22697142e6aSDevin Teske		"$opt_none")
22797142e6aSDevin Teske			options=
22897142e6aSDevin Teske			;;
22997142e6aSDevin Teske		"$opt_cust")
230ab2043b8SDevin Teske			options="$_options"
231ab2043b8SDevin Teske			f_dialog_input_options "$interface"
232ab2043b8SDevin Teske			retval=$?
23397142e6aSDevin Teske			;;
23497142e6aSDevin Teske		esac
235ab2043b8SDevin Teske	fi
236ab2043b8SDevin Teske
237ab2043b8SDevin Teske	return $retval
238ab2043b8SDevin Teske}
239ab2043b8SDevin Teske
24056961fd7SDevin Teske############################################################ MAIN
24156961fd7SDevin Teske
24256961fd7SDevin Teskef_dprintf "%s: Successfully loaded." networking/media.subr
24356961fd7SDevin Teske
244ab2043b8SDevin Teskefi # ! $_NETWORKING_MEDIA_SUBR
245