devices revision 251269
1238438Sdteske#!/bin/sh
2238438Sdteske#-
3247280Sdteske# Copyright (c) 2006-2013 Devin Teske
4238438Sdteske# All Rights Reserved.
5238438Sdteske#
6238438Sdteske# Redistribution and use in source and binary forms, with or without
7238438Sdteske# modification, are permitted provided that the following conditions
8238438Sdteske# are met:
9238438Sdteske# 1. Redistributions of source code must retain the above copyright
10238438Sdteske#    notice, this list of conditions and the following disclaimer.
11238438Sdteske# 2. Redistributions in binary form must reproduce the above copyright
12238438Sdteske#    notice, this list of conditions and the following disclaimer in the
13238438Sdteske#    documentation and/or other materials provided with the distribution.
14238438Sdteske#
15238438Sdteske# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16238438Sdteske# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, THE
17238438Sdteske# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18238438Sdteske# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19238438Sdteske# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20238438Sdteske# DAMAGES (INLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21238438Sdteske# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22238438Sdteske# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23238438Sdteske# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24238438Sdteske# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25238438Sdteske# SUCH DAMAGE.
26238438Sdteske#
27238438Sdteske# $FreeBSD: head/usr.sbin/bsdconfig/networking/devices 251269 2013-06-02 23:02:56Z dteske $
28238438Sdteske#
29238438Sdteske############################################################ INCLUDES
30238438Sdteske
31240684SdteskeBSDCFG_SHARE="/usr/share/bsdconfig"
32240684Sdteske. $BSDCFG_SHARE/common.subr || exit 1
33244675Sdteskef_dprintf "%s: loading includes..." "$0"
34240684Sdteskef_include $BSDCFG_SHARE/dialog.subr
35240684Sdteskef_include $BSDCFG_SHARE/mustberoot.subr
36240684Sdteskef_include $BSDCFG_SHARE/sysrc.subr
37247280Sdteskef_include $BSDCFG_SHARE/media/tcpip.subr
38240684Sdteskef_include $BSDCFG_SHARE/networking/device.subr
39240684Sdteskef_include $BSDCFG_SHARE/networking/ipaddr.subr
40240684Sdteskef_include $BSDCFG_SHARE/networking/media.subr
41240684Sdteskef_include $BSDCFG_SHARE/networking/netmask.subr
42238438Sdteske
43240684SdteskeBSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="120.networking"
44238438Sdteskef_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
45238438Sdteske
46243112Sdteskeipgm=$( f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
47238438Sdteske[ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
48238438Sdteske
49238438Sdteske############################################################ MAIN
50238438Sdteske
51238438Sdteske# Incorporate rc-file if it exists
52238438Sdteske[ -f "$HOME/.bsdconfigrc" ] && f_include "$HOME/.bsdconfigrc"
53238438Sdteske
54238438Sdteske#
55238438Sdteske# Process command-line options
56238438Sdteske#
57250633Sdteskewhile getopts h$GETOPTS_STDARGS flag; do
58238438Sdteske	case "$flag" in
59238438Sdteske	h|\?) f_usage $BSDCFG_LIBE/$APP_DIR/USAGE "PROGRAM_NAME" "$pgm";;
60238438Sdteske	esac
61238438Sdteskedone
62238438Sdteskeshift $(( $OPTIND - 1 ))
63238438Sdteske
64238438Sdteske#
65238438Sdteske# Initialize
66238438Sdteske#
67238438Sdteskef_dialog_title "$msg_networking_devices"
68238438Sdteskef_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
69238438Sdteskef_mustberoot_init
70238438Sdteske
71238438Sdteske#
72238438Sdteske# Launch application main menu
73238438Sdteske#
74251269Sdteskedefaultitem=
75238438Sdteskewhile :; do
76251236Sdteske	f_dialog_menu_netdev "$defaultitem" || break
77251236Sdteske	f_dialog_menutag_fetch interface
78249751Sdteske	defaultitem="$interface"
79238438Sdteske
80238438Sdteske	#
81238438Sdteske	# dialog_menu_netdev adds an asterisk (*) to the right of the
82238438Sdteske	# device name if the interface is active. Remove the asterisk
83238438Sdteske	# from the device name if present.
84238438Sdteske	#
85238438Sdteske	case "$interface" in
86238438Sdteske	*\*) interface="${interface%?}";;
87238438Sdteske	esac
88238438Sdteske
89238438Sdteske	#
90238438Sdteske	# Obtain initial interface settings to be configured. These will be
91238438Sdteske	# passed to the f_dialog_menu_netdev_edit function-call below which
92238438Sdteske	# will block until the user has either cancelled or finished editing
93238438Sdteske	# the values.
94238438Sdteske	#
95238438Sdteske	# First, attempt to read stored configuration from rc.conf(5) and
96238438Sdteske	# fallback to reading the active configuration if not configured in
97238438Sdteske	# the rc.conf(5) file(s).
98238438Sdteske	#
99249753Sdteske	dhcp=
100238438Sdteske	_ipaddr=
101238438Sdteske	_netmask=
102238438Sdteske	_ifconfig=$( f_sysrc_get ifconfig_$interface )
103238438Sdteske	if [ "$_ifconfig" ]; then
104238438Sdteske		# If DHCP get IP address/netmask later from ifconfig(8)
105238438Sdteske		glob="[Dd][Hh][Cc][Pp]"
106238438Sdteske		case "$_ifconfig" in
107238438Sdteske		$glob) dhcp=1;;
108238438Sdteske		[Ss][Yy][Nn][Cc]$glob) dhcp=1;;
109238438Sdteske		[Nn][Oo][Ss][Yy][Nn][Cc]$glob) dhcp=1;;
110238438Sdteske		*)
111238438Sdteske			#
112238438Sdteske			# Get IP address/netmask from rc.conf(5)
113238438Sdteske			# configuration
114238438Sdteske			#
115238438Sdteske			dhcp=
116240783Sdteske			eval "$( exec 2> /dev/null
117238438Sdteske			         set -- $_ifconfig
118238438Sdteske			         while [ $# -gt 0 ]; do
119238438Sdteske			         	case "$1" in
120238438Sdteske			         	inet)
121238438Sdteske			         		shift 1
122238438Sdteske			         		echo "_ipaddr='$1'"
123238438Sdteske			         		;;
124238438Sdteske			         	netmask)
125238438Sdteske			         		shift 1
126238438Sdteske			         		echo "_netmask='$1'"
127238438Sdteske			         		;;
128238438Sdteske			         	esac
129238438Sdteske			         	shift 1
130238438Sdteske			         done
131238438Sdteske			       )"
132238438Sdteske			;;
133238438Sdteske		esac
134238438Sdteske	fi
135238438Sdteske
136238438Sdteske	#
137238438Sdteske	# Fill in IP address/netmask from active settings if no
138238438Sdteske	# configuration could be extrapolated from rc.conf(5)
139238438Sdteske	#
140238438Sdteske	[ "$_ipaddr"  ] || _ipaddr=$( f_ifconfig_inet $interface )
141238438Sdteske	[ "$_netmask" ] || _netmask=$( f_ifconfig_netmask $interface )
142238438Sdteske
143238438Sdteske	# Get the extra options (this always comes from rc.conf(5))
144238438Sdteske	_options=$( f_ifconfig_options $interface )
145238438Sdteske
146238438Sdteske	# Block on user-configuration of the probed settings
147238438Sdteske	f_dialog_menu_netdev_edit \
148249753Sdteske		"$interface" "$_ipaddr" "$_netmask" "$_options" $dhcp
149238438Sdteske
150238438Sdteske	# Return to root menu if above returns success
151238438Sdteske	[ $? -eq $SUCCESS ] && break
152238438Sdteske
153238438Sdteskedone
154238438Sdteske
155238438Sdteskeexit $SUCCESS
156238438Sdteske
157238438Sdteske################################################################################
158238438Sdteske# END
159238438Sdteske################################################################################
160