devices revision 251269
1239310Sdim#!/bin/sh
2239310Sdim#-
3239310Sdim# Copyright (c) 2006-2013 Devin Teske
4239310Sdim# All Rights Reserved.
5239310Sdim#
6239310Sdim# Redistribution and use in source and binary forms, with or without
7239310Sdim# modification, are permitted provided that the following conditions
8239310Sdim# are met:
9239310Sdim# 1. Redistributions of source code must retain the above copyright
10239310Sdim#    notice, this list of conditions and the following disclaimer.
11239310Sdim# 2. Redistributions in binary form must reproduce the above copyright
12239310Sdim#    notice, this list of conditions and the following disclaimer in the
13239310Sdim#    documentation and/or other materials provided with the distribution.
14239310Sdim#
15239310Sdim# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16239310Sdim# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, THE
17239310Sdim# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18239310Sdim# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19239310Sdim# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20239310Sdim# DAMAGES (INLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21239310Sdim# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22239310Sdim# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23239310Sdim# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24239310Sdim# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25239310Sdim# SUCH DAMAGE.
26239310Sdim#
27239310Sdim# $FreeBSD: head/usr.sbin/bsdconfig/networking/devices 251269 2013-06-02 23:02:56Z dteske $
28239310Sdim#
29239310Sdim############################################################ INCLUDES
30239310Sdim
31239310SdimBSDCFG_SHARE="/usr/share/bsdconfig"
32239310Sdim. $BSDCFG_SHARE/common.subr || exit 1
33239310Sdimf_dprintf "%s: loading includes..." "$0"
34239310Sdimf_include $BSDCFG_SHARE/dialog.subr
35239310Sdimf_include $BSDCFG_SHARE/mustberoot.subr
36239310Sdimf_include $BSDCFG_SHARE/sysrc.subr
37239310Sdimf_include $BSDCFG_SHARE/media/tcpip.subr
38239310Sdimf_include $BSDCFG_SHARE/networking/device.subr
39239310Sdimf_include $BSDCFG_SHARE/networking/ipaddr.subr
40239310Sdimf_include $BSDCFG_SHARE/networking/media.subr
41239310Sdimf_include $BSDCFG_SHARE/networking/netmask.subr
42239310Sdim
43239310SdimBSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="120.networking"
44239310Sdimf_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
45239310Sdim
46239310Sdimipgm=$( f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
47239310Sdim[ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
48239310Sdim
49239310Sdim############################################################ MAIN
50239310Sdim
51239310Sdim# Incorporate rc-file if it exists
52239310Sdim[ -f "$HOME/.bsdconfigrc" ] && f_include "$HOME/.bsdconfigrc"
53239310Sdim
54239310Sdim#
55239310Sdim# Process command-line options
56239310Sdim#
57239310Sdimwhile getopts h$GETOPTS_STDARGS flag; do
58239310Sdim	case "$flag" in
59239310Sdim	h|\?) f_usage $BSDCFG_LIBE/$APP_DIR/USAGE "PROGRAM_NAME" "$pgm";;
60239310Sdim	esac
61239310Sdimdone
62239310Sdimshift $(( $OPTIND - 1 ))
63239310Sdim
64239310Sdim#
65239310Sdim# Initialize
66239310Sdim#
67239310Sdimf_dialog_title "$msg_networking_devices"
68239310Sdimf_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
69239310Sdimf_mustberoot_init
70239310Sdim
71239310Sdim#
72239310Sdim# Launch application main menu
73239310Sdim#
74239310Sdimdefaultitem=
75239310Sdimwhile :; do
76239310Sdim	f_dialog_menu_netdev "$defaultitem" || break
77239310Sdim	f_dialog_menutag_fetch interface
78239310Sdim	defaultitem="$interface"
79239310Sdim
80239310Sdim	#
81239310Sdim	# dialog_menu_netdev adds an asterisk (*) to the right of the
82239310Sdim	# device name if the interface is active. Remove the asterisk
83239310Sdim	# from the device name if present.
84239310Sdim	#
85239310Sdim	case "$interface" in
86239310Sdim	*\*) interface="${interface%?}";;
87239310Sdim	esac
88239310Sdim
89239310Sdim	#
90239310Sdim	# Obtain initial interface settings to be configured. These will be
91239310Sdim	# passed to the f_dialog_menu_netdev_edit function-call below which
92239310Sdim	# will block until the user has either cancelled or finished editing
93239310Sdim	# the values.
94239310Sdim	#
95239310Sdim	# First, attempt to read stored configuration from rc.conf(5) and
96239310Sdim	# fallback to reading the active configuration if not configured in
97239310Sdim	# the rc.conf(5) file(s).
98239310Sdim	#
99239310Sdim	dhcp=
100239310Sdim	_ipaddr=
101239310Sdim	_netmask=
102239310Sdim	_ifconfig=$( f_sysrc_get ifconfig_$interface )
103239310Sdim	if [ "$_ifconfig" ]; then
104239310Sdim		# If DHCP get IP address/netmask later from ifconfig(8)
105239310Sdim		glob="[Dd][Hh][Cc][Pp]"
106239310Sdim		case "$_ifconfig" in
107239310Sdim		$glob) dhcp=1;;
108239310Sdim		[Ss][Yy][Nn][Cc]$glob) dhcp=1;;
109239310Sdim		[Nn][Oo][Ss][Yy][Nn][Cc]$glob) dhcp=1;;
110239310Sdim		*)
111239310Sdim			#
112239310Sdim			# Get IP address/netmask from rc.conf(5)
113239310Sdim			# configuration
114239310Sdim			#
115239310Sdim			dhcp=
116239310Sdim			eval "$( exec 2> /dev/null
117239310Sdim			         set -- $_ifconfig
118239310Sdim			         while [ $# -gt 0 ]; do
119239310Sdim			         	case "$1" in
120239310Sdim			         	inet)
121239310Sdim			         		shift 1
122239310Sdim			         		echo "_ipaddr='$1'"
123239310Sdim			         		;;
124239310Sdim			         	netmask)
125239310Sdim			         		shift 1
126239310Sdim			         		echo "_netmask='$1'"
127239310Sdim			         		;;
128239310Sdim			         	esac
129239310Sdim			         	shift 1
130239310Sdim			         done
131239310Sdim			       )"
132239310Sdim			;;
133239310Sdim		esac
134239310Sdim	fi
135239310Sdim
136239310Sdim	#
137239310Sdim	# Fill in IP address/netmask from active settings if no
138239310Sdim	# configuration could be extrapolated from rc.conf(5)
139239310Sdim	#
140239310Sdim	[ "$_ipaddr"  ] || _ipaddr=$( f_ifconfig_inet $interface )
141239310Sdim	[ "$_netmask" ] || _netmask=$( f_ifconfig_netmask $interface )
142239310Sdim
143239310Sdim	# Get the extra options (this always comes from rc.conf(5))
144239310Sdim	_options=$( f_ifconfig_options $interface )
145239310Sdim
146239310Sdim	# Block on user-configuration of the probed settings
147239310Sdim	f_dialog_menu_netdev_edit \
148239310Sdim		"$interface" "$_ipaddr" "$_netmask" "$_options" $dhcp
149239310Sdim
150239310Sdim	# Return to root menu if above returns success
151239310Sdim	[ $? -eq $SUCCESS ] && break
152239310Sdim
153239310Sdimdone
154239310Sdim
155239310Sdimexit $SUCCESS
156239310Sdim
157239310Sdim################################################################################
158239310Sdim# END
159239310Sdim################################################################################
160239310Sdim