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