xref: /freebsd/usr.sbin/bsdconfig/networking/share/media.subr (revision 193d9e768ba63fcfb187cfd17f461f7d41345048)
1if [ ! "$_NETWORKING_MEDIA_SUBR" ]; then _NETWORKING_MEDIA_SUBR=1
2#
3# Copyright (c) 2006-2013 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 (INCLUDING, 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/networking/common.subr
36f_include $BSDCFG_SHARE/strings.subr
37f_include $BSDCFG_SHARE/sysrc.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. If the user does not press ESC or choose
105# Cancel/No, $options will hold the user's input. Default input is taken from
106# the same variable ($options).
107#
108f_dialog_input_options()
109{
110	local interface="$1"
111
112	#
113	# Return with-error when there are NFS-mounts currently active. If the
114	# options are changed while NFS-exported directories are mounted,
115	# the system may hang (if any NFS mounts are using that interface).
116	#
117	if f_nfs_mounted && ! f_jailed; then
118		local setting
119		f_sprintf setting "$msg_current_options" \
120		                  "$interface" "$options"
121		f_noyes "$msg_nfs_mounts_may_cause_hang" "$setting" ||
122			return $DIALOG_CANCEL
123	fi
124
125	local msg
126	f_sprintf msg "$msg_please_enter_mediaopts" "$interface"
127	local hline="$hline_alnum_punc_tab_enter"
128
129	local _options
130	_options=$( $DIALOG \
131		--title "$DIALOG_TITLE"         \
132		--backtitle "$DIALOG_BACKTITLE" \
133		--hline "$hline"                \
134		--ok-label "$msg_ok"            \
135		--cancel-label "$msg_cancel"    \
136		--inputbox "$msg" 9 70          \
137		"$options"                      \
138		2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
139	)
140	local retval=$?
141	f_dialog_line_sanitize _options
142
143	[ $retval -eq $DIALOG_OK ] && options="$_options"
144
145	return $retval
146}
147
148# f_dialog_menu_media_options $interface
149#
150# Display a menu of additional media options for the given network interface.
151#
152f_dialog_menu_media_options()
153{
154	local interface="$1" _options="$2"
155	#
156	# Not all network interfaces support additional media options, but
157	# when available we should prompt the user to select from a list
158	# of available options (or none, as is the first/default option).
159	#
160
161	#
162	# Return with-error when there are NFS-mounts currently active. If the
163	# media options are changed while NFS-exported directories are mounted,
164	# the system may hang (if any NFS mounts are using that interface).
165	#
166	if f_nfs_mounted && ! f_jailed; then
167		local setting
168		f_sprintf setting "$msg_current_options" \
169		                  "$interface" "$_options"
170		f_noyes "$msg_nfs_mounts_may_cause_hang" "$setting" ||
171			return $DIALOG_CANCEL
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		  f_substr -v tagn "$DIALOG_MENU_TAGS" $index 1
184		  echo "'$tagn' '$opt_none'"
185		  index=$(( $index + 1 ))
186
187		  f_substr -v tagn "$DIALOG_MENU_TAGS" $index 1
188		  echo "'$tagn' '$opt_cust'"
189		  index=$(( $index + 1 ))
190
191		  while read media_options; do
192		  	[ $index -lt ${#DIALOG_MENU_TAGS} ] || break
193		  	f_substr -v tagn "$DIALOG_MENU_TAGS" $index 1
194		  	echo "'$tagn' '$media_options'"
195		  	index=$(( $index + 1 ))
196		  done
197		)
198	)"
199
200	local msg
201	if [ "$USE_XDIALOG" ]; then
202		f_sprintf msg "$xmsg_supported_media_options" \
203		              "$interface" "$interface"
204	else
205		f_sprintf msg "$msg_supported_media_options" \
206		              "$interface" "$interface"
207	fi
208
209	local hline="$hline_arrows_tab_enter"
210
211	local tag
212	tag=$( eval $DIALOG \
213		--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	local retval=$?
223	f_dialog_data_sanitize tag
224
225	if [ $retval -eq $DIALOG_OK ]; then
226		options=$( eval f_dialog_menutag2item \"\$tag\" \
227		                                      $supported_media )
228		case "$options" in
229		"$opt_none")
230			options=
231			;;
232		"$opt_cust")
233			options="$_options"
234			f_dialog_input_options "$interface"
235			retval=$?
236			;;
237		esac
238	fi
239
240	return $retval
241}
242
243############################################################ MAIN
244
245f_dprintf "%s: Successfully loaded." networking/media.subr
246
247fi # ! $_NETWORKING_MEDIA_SUBR
248