devices revision 238438
1249259Sdim#!/bin/sh
2249259Sdim#-
3249259Sdim# Copyright (c) 2006-2012 Devin Teske
4249259Sdim# All Rights Reserved.
5249259Sdim#
6249259Sdim# Redistribution and use in source and binary forms, with or without
7249259Sdim# modification, are permitted provided that the following conditions
8249259Sdim# are met:
9249259Sdim# 1. Redistributions of source code must retain the above copyright
10249259Sdim#    notice, this list of conditions and the following disclaimer.
11249259Sdim# 2. Redistributions in binary form must reproduce the above copyright
12249259Sdim#    notice, this list of conditions and the following disclaimer in the
13249259Sdim#    documentation and/or other materials provided with the distribution.
14249259Sdim#
15249259Sdim# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16249259Sdim# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, THE
17249259Sdim# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18249259Sdim# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19249259Sdim# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20249259Sdim# DAMAGES (INLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21249259Sdim# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22249259Sdim# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23249259Sdim# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24249259Sdim# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25249259Sdim# SUCH DAMAGE.
26249259Sdim#
27249259Sdim# $FreeBSD: head/usr.sbin/bsdconfig/networking/devices 238438 2012-07-14 03:16:57Z dteske $
28249259Sdim#
29249259Sdim############################################################ INCLUDES
30249259Sdim
31249259SdimBSDCFG_LIBE="/usr/libexec/bsdconfig"
32249259Sdim. $BSDCFG_LIBE/include/common.subr || exit 1
33249259Sdimf_include $BSDCFG_LIBE/include/dialog.subr
34249259Sdimf_include $BSDCFG_LIBE/include/mustberoot.subr
35249259Sdimf_include $BSDCFG_LIBE/include/sysrc.subr
36249259Sdim
37249259SdimAPP_DIR="120.networking"
38249259Sdimf_include $BSDCFG_LIBE/$APP_DIR/include/device.subr
39249259Sdimf_include $BSDCFG_LIBE/$APP_DIR/include/ipaddr.subr
40249259Sdimf_include $BSDCFG_LIBE/$APP_DIR/include/media.subr
41263508Sdimf_include $BSDCFG_LIBE/$APP_DIR/include/netmask.subr
42249259Sdimf_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
43249259Sdim
44249259Sdimipgm=$( f_index_menu_selection $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
45249259Sdim[ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
46249259Sdim
47249259Sdim############################################################ MAIN
48249259Sdim
49249259Sdim# Incorporate rc-file if it exists
50249259Sdim[ -f "$HOME/.bsdconfigrc" ] && f_include "$HOME/.bsdconfigrc"
51249259Sdim
52249259Sdim#
53249259Sdim# Process command-line options
54249259Sdim#
55249259Sdimwhile getopts hSX flag; do
56249259Sdim	case "$flag" in
57249259Sdim	h|\?) f_usage $BSDCFG_LIBE/$APP_DIR/USAGE "PROGRAM_NAME" "$pgm";;
58249259Sdim	esac
59249259Sdimdone
60249259Sdimshift $(( $OPTIND - 1 ))
61249259Sdim
62249259Sdim#
63249259Sdim# Initialize
64249259Sdim#
65249259Sdimf_dialog_init
66249259Sdimf_dialog_title "$msg_networking_devices"
67249259Sdimf_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
68249259Sdimf_mustberoot_init
69249259Sdim
70249259Sdim#
71249259Sdim# Launch application main menu
72249259Sdim#
73249259Sdimwhile :; do
74249259Sdim	f_dialog_menu_netdev
75249259Sdim	retval=$?
76249259Sdim	interface=$( f_dialog_menutag )
77249259Sdim
78249259Sdim	[ $retval -eq 0 ] || break
79249259Sdim
80249259Sdim	#
81249259Sdim	# dialog_menu_netdev adds an asterisk (*) to the right of the
82249259Sdim	# device name if the interface is active. Remove the asterisk
83	# from the device name if present.
84	#
85	case "$interface" in
86	*\*) interface="${interface%?}";;
87	esac
88
89	#
90	# Obtain initial interface settings to be configured. These will be
91	# passed to the f_dialog_menu_netdev_edit function-call below which
92	# will block until the user has either cancelled or finished editing
93	# the values.
94	#
95	# First, attempt to read stored configuration from rc.conf(5) and
96	# fallback to reading the active configuration if not configured in
97	# the rc.conf(5) file(s).
98	#
99	_ipaddr=
100	_netmask=
101	_ifconfig=$( f_sysrc_get ifconfig_$interface )
102	if [ "$_ifconfig" ]; then
103		# If DHCP get IP address/netmask later from ifconfig(8)
104		glob="[Dd][Hh][Cc][Pp]"
105		case "$_ifconfig" in
106		$glob) dhcp=1;;
107		[Ss][Yy][Nn][Cc]$glob) dhcp=1;;
108		[Nn][Oo][Ss][Yy][Nn][Cc]$glob) dhcp=1;;
109		*)
110			#
111			# Get IP address/netmask from rc.conf(5)
112			# configuration
113			#
114			dhcp=
115			eval "$( exec 2> /dev/null
116			         set -- $_ifconfig
117			         while [ $# -gt 0 ]; do
118			         	case "$1" in
119			         	inet)
120			         		shift 1
121			         		echo "_ipaddr='$1'"
122			         		;;
123			         	netmask)
124			         		shift 1
125			         		echo "_netmask='$1'"
126			         		;;
127			         	esac
128			         	shift 1
129			         done
130			       )"
131			;;
132		esac
133	fi
134
135	#
136	# Fill in IP address/netmask from active settings if no
137	# configuration could be extrapolated from rc.conf(5)
138	#
139	[ "$_ipaddr"  ] || _ipaddr=$( f_ifconfig_inet $interface )
140	[ "$_netmask" ] || _netmask=$( f_ifconfig_netmask $interface )
141
142	# Get the extra options (this always comes from rc.conf(5))
143	_options=$( f_ifconfig_options $interface )
144
145	# Block on user-configuration of the probed settings
146	f_dialog_menu_netdev_edit \
147		$interface $_ipaddr $_netmask "$_options" $dhcp
148
149	# Return to root menu if above returns success
150	[ $? -eq $SUCCESS ] && break
151
152done
153
154exit $SUCCESS
155
156################################################################################
157# END
158################################################################################
159