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