xref: /freebsd/usr.sbin/bsdconfig/networking/share/media.subr (revision c6ec7d31830ab1c80edae95ad5e4b9dba10c47ac)
1if [ ! "$_NETWORKING_MEDIA_SUBR" ]; then _NETWORKING_MEDIA_SUBR=1
2#
3# Copyright (c) 2006-2012 Devin Teske
4# All Rights Reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, THE
17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20# DAMAGES (INLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25# SUCH DAMAGE.
26#
27# $FreeBSD$
28#
29############################################################ INCLUDES
30
31BSDCFG_SHARE="/usr/share/bsdconfig"
32. $BSDCFG_SHARE/common.subr || exit 1
33f_include $BSDCFG_SHARE/dialog.subr
34f_include $BSDCFG_SHARE/strings.subr
35f_include $BSDCFG_SHARE/sysrc.subr
36f_include $BSDCFG_SHARE/networking/common.subr
37
38BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="120.networking"
39f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
40
41############################################################ FUNCTIONS
42
43# f_ifconfig_options $interface
44#
45# Returns any/all extra ifconfig(8) parameters associated with $interface.
46#
47f_ifconfig_options()
48{
49	local interface="$1"
50	[ "$interface" ] || return $SUCCESS
51
52	#
53	# Loop over the options, removing what we don't want
54	#
55	(
56		set -- $( f_sysrc_get ifconfig_$interface )
57
58		#
59		# Return if the interface is configured for DHCP
60		#
61		glob="[Dd][Hh][Cc][Pp]"
62		case "$*" in
63		$glob|[Ss][Yy][Nn][Cc]$glob|[Nn][Oo][Ss][Yy][Nn][Cc]$glob)
64			exit $SUCCESS
65		esac
66
67		output=
68		while [ $# -gt 0 ]; do
69			case "$1" in
70			inet|netmask) shift 1;;
71			*) output="$output${output:+ }$1"
72			esac
73			shift 1
74		done
75		echo "$output"
76	)
77}
78
79# f_ifconfig_media $interface
80#
81# Returns list of supported media for $interface.
82#
83f_ifconfig_media()
84{
85	local interface="$1"
86	ifconfig -m "$interface" 2> /dev/null | awk \
87	'
88		BEGIN { media_found = 0 }
89		{
90			if ( media_found == 1 ) { print; next }
91		}
92		( $1 $2 == "supported" "media:" ) \
93		{
94			media_found = 1
95			next
96		}
97		END { exit ! media_found }
98	'
99}
100
101# f_dialog_input_options $interface
102#
103# Input custom interface options.
104#
105f_dialog_input_options()
106{
107	local interface="$1"
108
109	#
110	# Return with-error when there are NFS-mounts currently active. If the
111	# options are changed while NFS-exported directories are mounted,
112	# the system may hang (if any NFS mounts are using that interface).
113	#
114	if f_nfs_mounted && ! f_jailed; then
115		local setting="$( printf "$msg_current_options" \
116		                         "$interface" "$options" )"
117		f_show_msg "$msg_nfs_mounts_may_cause_hang" "$setting"
118		return $FAILURE
119	fi
120
121	local msg="$( printf "$msg_please_enter_mediaopts" "$interface" )"
122	local hline="$hline_alnum_punc_tab_enter"
123
124	local dialog_inputbox
125	dialog_inputbox=$( $DIALOG \
126		--title "$DIALOG_TITLE"         \
127		--backtitle "$DIALOG_BACKTITLE" \
128		--hline "$hline"                \
129		--ok-label "$msg_ok"            \
130		--cancel-label "$msg_cancel"    \
131		--inputbox "$msg" 9 70          \
132		"$options"                      \
133		2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
134	)
135
136	local retval=$?
137	setvar DIALOG_INPUTBOX_$$ "$dialog_inputbox"
138	local _options="$( f_dialog_inputstr )"
139
140	[ $retval -eq $SUCCESS ] && options="$_options"
141
142	return $retval
143}
144
145# f_dialog_menu_media_options $interface
146#
147# Display a menu of additional media options for the given network interface.
148#
149f_dialog_menu_media_options()
150{
151	local interface="$1" _options="$2"
152	#
153	# Not all network interfaces support additional media options, but
154	# when available we should prompt the user to select from a list
155	# of available options (or none, as is the first/default option).
156	#
157
158	#
159	# Return with-error when there are NFS-mounts currently active. If the
160	# media options are changed while NFS-exported directories are mounted,
161	# the system may hang (if any NFS mounts are using that interface).
162	#
163	if f_nfs_mounted && ! f_jailed; then
164		local setting="$( printf "$msg_current_options" \
165		                         "$interface" "$_options" )"
166		f_show_msg "$msg_nfs_mounts_may_cause_hang" "$setting"
167		return $FAILURE
168	fi
169
170	#
171	# Build list of additional media options
172	#
173	local opt_none="$msg_no_options"
174	local opt_cust="$msg_custom"
175	local supported_media="$(
176		f_ifconfig_media $interface | \
177		( index=1
178
179		  echo "'$( f_substr "$DIALOG_MENU_TAGS" $index 1 )'"
180		  echo "'$opt_none'"
181		  index=$(( $index + 1 ))
182
183		  echo "'$( f_substr "$DIALOG_MENU_TAGS" $index 1 )'"
184		  echo "'$opt_cust'"
185		  index=$(( $index + 1 ))
186
187		  while read media_options; do
188		  	[ $index -lt ${#DIALOG_MENU_TAGS} ] || break
189		  	echo "'$( f_substr "$DIALOG_MENU_TAGS" $index 1 )'"
190		  	echo "'$media_options'"
191		  	index=$(( $index + 1 ))
192		  done
193		)
194	)"
195
196	local msg
197	if [ "$USE_XDIALOG" ]; then
198		msg=$( printf "$xmsg_supported_media_options" \
199		       		"$interface" "$interface" )
200	else
201		msg=$( printf "$msg_supported_media_options" \
202		       		"$interface" "$interface" )
203	fi
204
205	local hline="$hline_arrows_tab_enter"
206
207	local dialog_menu
208	dialog_menu=$( eval $DIALOG \
209		--clear --title \"\$DIALOG_TITLE\" \
210		--backtitle \"\$DIALOG_BACKTITLE\" \
211		--hline \"\$hline\"                \
212		--ok-label \"\$msg_ok\"            \
213		--cancel-label \"\$msg_cancel\"    \
214		--menu \"\$msg\" 21 60 12          \
215		$supported_media                   \
216		2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
217	)
218
219	local retval=$?
220	setvar DIALOG_MENU_$$ "$dialog_menu"
221	if [ $retval -eq $SUCCESS ]; then
222		local tag="$( f_dialog_menutag )"
223		options=$( eval f_dialog_menutag2item \"\$tag\" \
224		                                      $supported_media )
225		[ "$options" = "$opt_none" ] && options=
226
227		if [ "$options" = "$opt_cust" ]; then
228			options="$_options"
229			f_dialog_input_options "$interface"
230			retval=$?
231		fi
232	fi
233
234	return $retval
235}
236
237fi # ! $_NETWORKING_MEDIA_SUBR
238