xref: /freebsd/usr.sbin/bsdconfig/networking/share/media.subr (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
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#
28############################################################ INCLUDES
29
30BSDCFG_SHARE="/usr/share/bsdconfig"
31. $BSDCFG_SHARE/common.subr || exit 1
32f_dprintf "%s: loading includes..." networking/media.subr
33f_include $BSDCFG_SHARE/dialog.subr
34f_include $BSDCFG_SHARE/networking/common.subr
35f_include $BSDCFG_SHARE/strings.subr
36f_include $BSDCFG_SHARE/sysrc.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. If the user does not press ESC or choose
104# Cancel/No, $options will hold the user's input. Default input is taken from
105# the same variable ($options).
106#
107f_dialog_input_options()
108{
109	local interface="$1"
110
111	#
112	# Return with-error when there are NFS-mounts currently active. If the
113	# options are changed while NFS-exported directories are mounted,
114	# the system may hang (if any NFS mounts are using that interface).
115	#
116	if f_nfs_mounted && ! f_jailed; then
117		local setting
118		f_sprintf setting "$msg_current_options" \
119		                  "$interface" "$options"
120		f_noyes "$msg_nfs_mounts_may_cause_hang" "$setting" ||
121			return $DIALOG_CANCEL
122	fi
123
124	local msg
125	f_sprintf msg "$msg_please_enter_mediaopts" "$interface"
126	local hline="$hline_alnum_punc_tab_enter"
127
128	local _options
129	_options=$( $DIALOG \
130		--title "$DIALOG_TITLE"         \
131		--backtitle "$DIALOG_BACKTITLE" \
132		--hline "$hline"                \
133		--ok-label "$msg_ok"            \
134		--cancel-label "$msg_cancel"    \
135		--inputbox "$msg" 9 70          \
136		"$options"                      \
137		2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
138	)
139	local retval=$?
140	f_dialog_line_sanitize _options
141
142	[ $retval -eq $DIALOG_OK ] && 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
167		f_sprintf setting "$msg_current_options" \
168		                  "$interface" "$_options"
169		f_noyes "$msg_nfs_mounts_may_cause_hang" "$setting" ||
170			return $DIALOG_CANCEL
171	fi
172
173	#
174	# Build list of additional media options
175	#
176	local opt_none="$msg_no_options"
177	local opt_cust="$msg_custom"
178	local supported_media="$(
179		f_ifconfig_media $interface | \
180		( index=1
181
182		  f_substr -v tagn "$DIALOG_MENU_TAGS" $index 1
183		  echo "'$tagn' '$opt_none'"
184		  index=$(( $index + 1 ))
185
186		  f_substr -v tagn "$DIALOG_MENU_TAGS" $index 1
187		  echo "'$tagn' '$opt_cust'"
188		  index=$(( $index + 1 ))
189
190		  while read media_options; do
191		  	[ $index -lt ${#DIALOG_MENU_TAGS} ] || break
192		  	f_substr -v tagn "$DIALOG_MENU_TAGS" $index 1
193		  	echo "'$tagn' '$media_options'"
194		  	index=$(( $index + 1 ))
195		  done
196		)
197	)"
198
199	local msg
200	if [ "$USE_XDIALOG" ]; then
201		f_sprintf msg "$xmsg_supported_media_options" \
202		              "$interface" "$interface"
203	else
204		f_sprintf msg "$msg_supported_media_options" \
205		              "$interface" "$interface"
206	fi
207
208	local hline="$hline_arrows_tab_enter"
209
210	local tag
211	tag=$( eval $DIALOG \
212		--title \"\$DIALOG_TITLE\"         \
213		--backtitle \"\$DIALOG_BACKTITLE\" \
214		--hline \"\$hline\"                \
215		--ok-label \"\$msg_ok\"            \
216		--cancel-label \"\$msg_cancel\"    \
217		--menu \"\$msg\" 21 60 12          \
218		$supported_media                   \
219		2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
220	)
221	local retval=$?
222	f_dialog_data_sanitize tag
223
224	if [ $retval -eq $DIALOG_OK ]; then
225		options=$( eval f_dialog_menutag2item \"\$tag\" \
226		                                      $supported_media )
227		case "$options" in
228		"$opt_none")
229			options=
230			;;
231		"$opt_cust")
232			options="$_options"
233			f_dialog_input_options "$interface"
234			retval=$?
235			;;
236		esac
237	fi
238
239	return $retval
240}
241
242############################################################ MAIN
243
244f_dprintf "%s: Successfully loaded." networking/media.subr
245
246fi # ! $_NETWORKING_MEDIA_SUBR
247