media.subr revision 240768
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: head/usr.sbin/bsdconfig/networking/share/media.subr 240768 2012-09-20 23:44:13Z dteske $
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		local message="$( printf "$msg_nfs_mounts_may_cause_hang" \
118		                         "$setting" )"
119		f_dialog_msgbox "$message"
120		return $FAILURE
121	fi
122
123	local msg="$( printf "$msg_please_enter_mediaopts" "$interface" )"
124	local hline="$hline_alnum_punc_tab_enter"
125
126	local dialog_inputbox
127	dialog_inputbox=$( $DIALOG \
128		--title "$DIALOG_TITLE"         \
129		--backtitle "$DIALOG_BACKTITLE" \
130		--hline "$hline"                \
131		--ok-label "$msg_ok"            \
132		--cancel-label "$msg_cancel"    \
133		--inputbox "$msg" 9 70          \
134		"$options"                      \
135		2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
136	)
137
138	local retval=$?
139	setvar DIALOG_INPUTBOX_$$ "$dialog_inputbox"
140	local _options="$( f_dialog_inputstr )"
141
142	[ $retval -eq $SUCCESS ] && options="$_options"
143
144	return $retval
145}
146
147# f_dialog_menu_media_options $interface
148#
149# Display a menu of additional media options for the given network interface.
150#
151f_dialog_menu_media_options()
152{
153	local interface="$1" _options="$2"
154	#
155	# Not all network interfaces support additional media options, but
156	# when available we should prompt the user to select from a list
157	# of available options (or none, as is the first/default option).
158	#
159
160	#
161	# Return with-error when there are NFS-mounts currently active. If the
162	# media options are changed while NFS-exported directories are mounted,
163	# the system may hang (if any NFS mounts are using that interface).
164	#
165	if f_nfs_mounted && ! f_jailed; then
166		local setting="$( printf "$msg_current_options" \
167		                         "$interface" "$_options" )"
168		local message="$( printf "$msg_nfs_mounts_may_cause_hang" \
169		                         "$setting" )"
170		f_dialog_msgbox "$message"
171		return $FAILURE
172	fi
173
174	#
175	# Build list of additional media options
176	#
177	local opt_none="$msg_no_options"
178	local opt_cust="$msg_custom"
179	local supported_media="$(
180		f_ifconfig_media $interface | \
181		( index=1
182
183		  echo "'$( f_substr "$DIALOG_MENU_TAGS" $index 1 )'"
184		  echo "'$opt_none'" 
185		  index=$(( $index + 1 ))
186
187		  echo "'$( f_substr "$DIALOG_MENU_TAGS" $index 1 )'"
188		  echo "'$opt_cust'" 
189		  index=$(( $index + 1 ))
190
191		  while read media_options; do
192		  	[ $index -lt ${#DIALOG_MENU_TAGS} ] || break
193		  	echo "'$( f_substr "$DIALOG_MENU_TAGS" $index 1 )'"
194		  	echo "'$media_options'"
195		  	index=$(( $index + 1 ))
196		  done
197		)
198	)"
199
200	local msg
201	if [ "$USE_XDIALOG" ]; then
202		msg=$( printf "$xmsg_supported_media_options" \
203		       		"$interface" "$interface" )
204	else
205		msg=$( printf "$msg_supported_media_options" \
206		       		"$interface" "$interface" )
207	fi
208
209	local hline="$hline_arrows_tab_enter"
210
211	local dialog_menu
212	dialog_menu=$( eval $DIALOG \
213		--clear --title \"\$DIALOG_TITLE\" \
214		--backtitle \"\$DIALOG_BACKTITLE\" \
215		--hline \"\$hline\"                \
216		--ok-label \"\$msg_ok\"            \
217		--cancel-label \"\$msg_cancel\"    \
218		--menu \"\$msg\" 21 60 12          \
219		$supported_media                   \
220		2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
221	)
222
223	local retval=$?
224	setvar DIALOG_MENU_$$ "$dialog_menu"
225	if [ $retval -eq $SUCCESS ]; then
226		local tag="$( f_dialog_menutag )"
227		options=$( eval f_dialog_menutag2item \"\$tag\" \
228		                                      $supported_media )
229		[ "$options" = "$opt_none" ] && options=
230
231		if [ "$options" = "$opt_cust" ]; then
232			options="$_options"
233			f_dialog_input_options "$interface"
234			retval=$?
235		fi
236	fi
237
238	return $retval
239}
240
241fi # ! $_NETWORKING_MEDIA_SUBR
242