devices revision 252001
133965Sjdp#!/bin/sh
277298Sobrien#-
360484Sobrien# Copyright (c) 2006-2013 Devin Teske
433965Sjdp# All Rights Reserved.
533965Sjdp#
633965Sjdp# Redistribution and use in source and binary forms, with or without
733965Sjdp# modification, are permitted provided that the following conditions
833965Sjdp# are met:
933965Sjdp# 1. Redistributions of source code must retain the above copyright
1033965Sjdp#    notice, this list of conditions and the following disclaimer.
1133965Sjdp# 2. Redistributions in binary form must reproduce the above copyright
1233965Sjdp#    notice, this list of conditions and the following disclaimer in the
1333965Sjdp#    documentation and/or other materials provided with the distribution.
1433965Sjdp#
1533965Sjdp# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1633965Sjdp# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, THE
1733965Sjdp# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1833965Sjdp# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1933965Sjdp# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2033965Sjdp# DAMAGES (INLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2133965Sjdp# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2260484Sobrien# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2360484Sobrien# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2460484Sobrien# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2560484Sobrien# SUCH DAMAGE.
2660484Sobrien#
2760484Sobrien# $FreeBSD: head/usr.sbin/bsdconfig/networking/devices 252001 2013-06-19 18:32:18Z dteske $
2860484Sobrien#
2960484Sobrien############################################################ INCLUDES
3060484Sobrien
3160484SobrienBSDCFG_SHARE="/usr/share/bsdconfig"
3260484Sobrien. $BSDCFG_SHARE/common.subr || exit 1
3360484Sobrienf_dprintf "%s: loading includes..." "$0"
3477298Sobrienf_include $BSDCFG_SHARE/dialog.subr
3533965Sjdpf_include $BSDCFG_SHARE/mustberoot.subr
3633965Sjdpf_include $BSDCFG_SHARE/sysrc.subr
3733965Sjdpf_include $BSDCFG_SHARE/media/tcpip.subr
3833965Sjdpf_include $BSDCFG_SHARE/networking/device.subr
3933965Sjdpf_include $BSDCFG_SHARE/networking/ipaddr.subr
4033965Sjdpf_include $BSDCFG_SHARE/networking/media.subr
4133965Sjdpf_include $BSDCFG_SHARE/networking/netmask.subr
4233965Sjdp
4333965SjdpBSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="120.networking"
4433965Sjdpf_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
4533965Sjdp
4633965Sjdpipgm=$( f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
4733965Sjdp[ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
4833965Sjdp
4933965Sjdp############################################################ MAIN
5033965Sjdp
5133965Sjdp# Incorporate rc-file if it exists
5233965Sjdp[ -f "$HOME/.bsdconfigrc" ] && f_include "$HOME/.bsdconfigrc"
5333965Sjdp
5433965Sjdp#
5533965Sjdp# Process command-line options
5633965Sjdp#
5733965Sjdpwhile getopts h$GETOPTS_STDARGS flag; do
5833965Sjdp	case "$flag" in
5933965Sjdp	h|\?) f_usage $BSDCFG_LIBE/$APP_DIR/USAGE "PROGRAM_NAME" "$pgm";;
6033965Sjdp	esac
6133965Sjdpdone
6233965Sjdpshift $(( $OPTIND - 1 ))
6333965Sjdp
6438889Sjdp#
6560484Sobrien# Initialize
6633965Sjdp#
6733965Sjdpf_dialog_title "$msg_networking_devices"
6833965Sjdpf_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
6933965Sjdpf_mustberoot_init
7033965Sjdp
7133965Sjdp#
7233965Sjdp# Launch application main menu
7333965Sjdp#
7433965Sjdpdefaultitem=
7533965Sjdpwhile :; do
7633965Sjdp	f_dialog_menu_netdev "$defaultitem" || break
7733965Sjdp	f_dialog_menutag_fetch interface
7833965Sjdp	defaultitem="$interface"
7933965Sjdp
8033965Sjdp	#
8133965Sjdp	# dialog_menu_netdev adds an asterisk (*) to the right of the
8233965Sjdp	# device name if the interface is active. Remove the asterisk
8333965Sjdp	# from the device name if present.
8433965Sjdp	#
8533965Sjdp	case "$interface" in
8633965Sjdp	*\*) interface="${interface%?}" ;;
8760484Sobrien	esac
8833965Sjdp
8933965Sjdp	#
9033965Sjdp	# Obtain initial interface settings to be configured. These will be
9133965Sjdp	# passed to the f_dialog_menu_netdev_edit function-call below which
9233965Sjdp	# will block until the user has either cancelled or finished editing
9333965Sjdp	# the values.
9433965Sjdp	#
9533965Sjdp	# First, attempt to read stored configuration from rc.conf(5) and
9633965Sjdp	# fallback to reading the active configuration if not configured in
9733965Sjdp	# the rc.conf(5) file(s).
9833965Sjdp	#
9933965Sjdp	dhcp=
10033965Sjdp	_ipaddr=
10133965Sjdp	_netmask=
10233965Sjdp	_ifconfig=$( f_sysrc_get ifconfig_$interface )
10333965Sjdp	if [ "$_ifconfig" ]; then
10433965Sjdp		# If DHCP, get IP address/netmask later from ifconfig(8)
10560484Sobrien		glob="[Dd][Hh][Cc][Pp]"
10660484Sobrien		case "$_ifconfig" in
10760484Sobrien		$glob) dhcp=1 ;;
10860484Sobrien		[Ss][Yy][Nn][Cc]$glob) dhcp=1 ;;
10960484Sobrien		[Nn][Oo][Ss][Yy][Nn][Cc]$glob) dhcp=1 ;;
11060484Sobrien		*)
11160484Sobrien			#
11260484Sobrien			# Get IP address/netmask from rc.conf(5) configuration
11360484Sobrien			#
11460484Sobrien			dhcp=
11560484Sobrien			eval "$(
11660484Sobrien				exec 2> /dev/null
11760484Sobrien				set -- $_ifconfig
11860484Sobrien				while [ $# -gt 0 ]; do
11960484Sobrien					case "$1" in
12060484Sobrien					inet)
12160484Sobrien						shift 1
12260484Sobrien						echo "_ipaddr='$1'"
12360484Sobrien						;;
12460484Sobrien					netmask)
12560484Sobrien						shift 1
12660484Sobrien						echo "_netmask='$1'"
12760484Sobrien						;;
12860484Sobrien					esac
12960484Sobrien					shift 1
13060484Sobrien				done
13160484Sobrien			)"
13260484Sobrien			;;
13360484Sobrien		esac
13460484Sobrien	fi
13560484Sobrien
13660484Sobrien	#
13760484Sobrien	# Fill in IP address/netmask from active settings if no
13860484Sobrien	# configuration could be extrapolated from rc.conf(5)
13960484Sobrien	#
14060484Sobrien	[ "$_ipaddr"  ] || _ipaddr=$( f_ifconfig_inet $interface )
14160484Sobrien	[ "$_netmask" ] || _netmask=$( f_ifconfig_netmask $interface )
14260484Sobrien
14360484Sobrien	# Get the extra options (this always comes from rc.conf(5))
14460484Sobrien	_options=$( f_ifconfig_options $interface )
14560484Sobrien
14660484Sobrien	# Block on user-configuration of the probed settings
14760484Sobrien	f_dialog_menu_netdev_edit \
14860484Sobrien		"$interface" "$_ipaddr" "$_netmask" "$_options" $dhcp
14960484Sobrien
15060484Sobrien	# Return to root menu if above returns success
15160484Sobrien	[ $? -eq $SUCCESS ] && break
15260484Sobriendone
15360484Sobrien
15460484Sobrienexit $SUCCESS
15560484Sobrien
15660484Sobrien################################################################################
15760484Sobrien# END
15860484Sobrien################################################################################
15960484Sobrien