freebsd-dev/usr.sbin/bsdconfig/networking/share/device.subr
2013-07-07 18:51:44 +00:00

394 lines
12 KiB
Plaintext

if [ ! "$_NETWORKING_DEVICE_SUBR" ]; then _NETWORKING_DEVICE_SUBR=1
#
# Copyright (c) 2006-2013 Devin Teske
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# $FreeBSD$
#
############################################################ INCLUDES
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
f_dprintf "%s: loading includes..." networking/device.subr
f_include $BSDCFG_SHARE/device.subr
f_include $BSDCFG_SHARE/dialog.subr
f_include $BSDCFG_SHARE/media/tcpip.subr
f_include $BSDCFG_SHARE/networking/common.subr
f_include $BSDCFG_SHARE/networking/ipaddr.subr
f_include $BSDCFG_SHARE/networking/media.subr
f_include $BSDCFG_SHARE/networking/netmask.subr
f_include $BSDCFG_SHARE/networking/resolv.subr
f_include $BSDCFG_SHARE/networking/routing.subr
f_include $BSDCFG_SHARE/sysrc.subr
BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="120.networking"
f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
############################################################ GLOBALS
#
# Settings used while interacting with various dialog(1) menus
#
: ${DIALOG_MENU_NETDEV_KICK_INTERFACES=1}
: ${DIALOG_MENU_NETDEV_SLEEP_AFTER_KICK=3}
############################################################ FUNCTIONS
# f_dialog_menu_netdev [$default]
#
# Display a list of network devices with descriptions. Optionally, if present
# and non-NULL, initially highlight $default interface.
#
f_dialog_menu_netdev()
{
local defaultitem="${1%\*}" # Tim trailing asterisk if present
#
# Display a message to let the user know we're working...
# (message will remain until we throw up the next dialog)
#
f_dialog_info "$msg_probing_network_interfaces"
#
# Get list of usable network interfaces
#
local d='[[:digit:]]+:'
local iflist="`echo "$(ifconfig -l):" | sed -E -e "
# Convert all spaces to colons
y/ /:/
# Prune unsavory interfaces
s/lo$d//g
s/ppp$d//g
s/sl$d//g
s/faith$d//g
# Convert all colons back into spaces
y/:/ /
"`"
#
# Optionally kick interfaces in the head to get them to accurately
# track the carrier status in realtime (required on FreeBSD).
#
if [ "$DIALOG_MENU_NETDEV_KICK_INTERFACES" ]; then
DIALOG_MENU_NETDEV_KICK_INTERFACES=
local ifn
for ifn in $iflist; do
f_quietly ifconfig $ifn up
done
if [ "$DIALOG_MENU_NETDEV_SLEEP_AFTER_KICK" ]; then
# interfaces need time to update carrier status
sleep $DIALOG_MENU_NETDEV_SLEEP_AFTER_KICK
fi
fi
#
# Mark any "active" interfaces with an asterisk (*)
# to the right of the device name.
#
interfaces=$(
for ifn in $iflist; do
active=$( ifconfig $ifn | awk \
'
( $1 == "status:" ) \
{
if ( $2 == "active" ) { print 1; exit }
}
' )
printf "'%s%s' '%s'\n" \
$ifn "${active:+*}" "$( f_device_desc $ifn )"
done
)
if [ ! "$interfaces" ]; then
f_show_msg "$msg_no_network_interfaces"
return $FAILURE
fi
#
# Maybe the default item was marked as active
#
if [ "$defaultitem" ]; then
ifconfig "$defaultitem" 2> /dev/null | awk \
'( $1 == "status:" && $2 != "active" ) { exit 0 }' ||
defaultitem="$defaultitem*"
fi
local hline="$hline_arrows_tab_enter"
#
# Ask user to select an interface
#
local prompt="$msg_select_network_interface"
local height width rows
eval f_dialog_menu_size height width rows \
\"\$DIALOG_TITLE\" \
\"\$DIALOG_BACKTITLE\" \
\"\$prompt\" \
\"\$hline\" \
$interfaces
local menu_choice
menu_choice=$( eval $DIALOG \
--title \"\$DIALOG_TITLE\" \
--backtitle \"\$DIALOG_BACKTITLE\" \
--hline \"\$hline\" \
--ok-label \"\$msg_ok\" \
--cancel-label \"\$msg_cancel\" \
--default-item \"\$defaultitem\" \
--menu \"\$prompt\" \
$height $width $rows \
$interfaces \
2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
)
local retval=$?
f_dialog_menutag_store -s "$menu_choice"
return $retval
}
# f_dialog_menu_netdev_edit $interface $ipaddr $netmask $options $dhcp
#
# Allow a user to edit network interface settings. Current values are not
# probed but rather taken from the positional arguments.
#
f_dialog_menu_netdev_edit()
{
local interface="$1" ipaddr="$2" netmask="$3" options="$4" dhcp="$5"
local prompt menu_list height width rows
#
# Create a duplicate set of variables for change-tracking...
#
local ipaddr_orig="$2" \
netmask_orig="$3" \
options_orig="$4" \
dhcp_orig="$5"
local hline="$hline_arrows_tab_enter"
prompt=$( printf "$msg_network_configuration" "$interface" )
#
# Loop forever until the user has finished configuring the different
# components of the network interface.
#
# To apply the settings, we need to know each of the following:
# - IP Address
# - Network subnet mask
# - Additional ifconfig(8) options
#
# It is only when we have all of the above values that we can make the
# changes effective because all three options must be specified at-once
# to ifconfig(8).
#
local defaultitem=
while :; do
local dhcp_status="$msg_disabled"
[ "$dhcp" ] && dhcp_status="$msg_enabled"
#
# Display configuration-edit menu
#
menu_list="
'X $msg_save_exit' '$msg_return_to_previous_menu'
'2 $msg_dhcp' '$dhcp_status'
'3 $msg_ipaddr4' '$ipaddr'
'4 $msg_netmask' '$netmask'
'5 $msg_options' '$options'
"
eval f_dialog_menu_size height width rows \
\"\$DIALOG_TITLE\" \
\"\$DIALOG_BACKTITLE\" \
\"\$prompt\" \
\"\$hline\" \
$menu_list
local tag
tag=$( eval $DIALOG \
--title \"\$DIALOG_TITLE\" \
--backtitle \"\$DIALOG_BACKTITLE\" \
--hline \"\$hline\" \
--ok-label \"\$msg_ok\" \
--cancel-label \"\$msg_cancel\" \
--help-button \
--help-label \"\$msg_help\" \
${USE_XDIALOG:+--help \"\"} \
--default-item \"\$defaultitem\" \
--menu \"\$prompt\" \
$height $width $rows \
$menu_list \
2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
)
local retval=$?
f_dialog_data_sanitize tag
if [ $retval -eq 2 ]; then
# The Help button was pressed
f_show_help "$TCP_HELPFILE"
continue
elif [ $retval -ne $SUCCESS ]; then
# "Cancel" was chosen (-1) or ESC was pressed (255)
return $retval
else
# Only update default-item on success
defaultitem="$tag"
fi
#
# Call the below ``modifier functions'' whose job it is to take
# input from the user and assign the newly-acquired values back
# to the ipaddr, netmask, and options variables for us to re-
# read and display in the summary dialog.
#
case "$tag" in
X\ *) break ;;
2\ *) #
# Proceed cautiously (confirm with the user) if/when NFS-
# mounts are active. If the network on which these mounts
# are made is changed parts of the system may hang.
#
if f_nfs_mounted && ! f_jailed; then
local setting="$( printf "$msg_current_dhcp_status" \
"$interface" "$dhcp_status" )"
f_noyes "$msg_nfs_mounts_may_cause_hang" "$setting" ||
continue
fi
#
# Toggle DHCP status
#
if [ "$dhcp_status" = "$msg_enabled" ]; then
dhcp=
else
trap - SIGINT
( # Execute within sub-shell to allow/catch Ctrl-C
trap 'exit $FAILURE' SIGINT
msg=$( printf "$msg_scanning_for_dhcp" "$interface" )
if [ "$USE_XDIALOG" ]; then
(
f_quietly ifconfig $interface delete
f_quietly dhclient $interface
) |
f_xdialog_info "$msg"
else
f_dialog_info "$msg"
f_quietly ifconfig $interface delete
f_quietly dhclient $interface
fi
)
retval=$?
trap 'interrupt' SIGINT
if [ $retval -eq $SUCCESS ]; then
dhcp=1
ipaddr=$( f_ifconfig_inet $interface )
netmask=$( f_ifconfig_netmask $interface )
options=
# Fixup search/domain in resolv.conf(5)
hostname=$( f_sysrc_get \
'hostname:-$(hostname)' )
f_dialog_resolv_conf_update "$hostname"
fi
fi
;;
3\ *) f_dialog_input_ipaddr "$interface" "$ipaddr"
[ $? -eq $SUCCESS ] && dhcp= ;;
4\ *) f_dialog_input_netmask "$interface" "$netmask"
[ $? -eq $SUCCESS -a "$_netmask" ] && dhcp= ;;
5\ *) f_dialog_menu_media_options "$interface" "$options"
[ $? -eq $SUCCESS ] && dhcp= ;;
esac
done
#
# Save only if the user changed at least one feature of the interface
#
if [ "$ipaddr" != "$ipaddr_orig" -o \
"$netmask" != "$netmask_orig" -o \
"$options" != "$options_orig" -o \
"$dhcp" != "$dhcp_orig" ]
then
f_show_info "$msg_saving_network_interface" "$interface"
local value=
if [ "$dhcp" ]; then
f_sysrc_delete defaultrouter
value=DHCP
else
value="inet $ipaddr netmask $netmask"
value="$value${options:+ }$options"
fi
f_sysrc_set ifconfig_$interface "$value"
fi
#
# Re/Apply the settings if desired
#
if [ ! "$dhcp" ]; then
if f_yesno "$msg_bring_interface_up" "$interface"
then
f_show_info "$msg_bring_interface_up" "$interface"
local dr="$( f_sysrc_get defaultrouter )" err
if [ "$dr" = "NO" -o ! "$dr" ]; then
dr=$( f_route_get_default )
[ "$dr" ] && f_sysrc_set defaultrouter "$dr"
fi
#
# Make a backup of resolv.conf(5) before using
# ifconfig(8) and then restore it afterward. This
# allows preservation of nameservers acquired via
# DHCP on FreeBSD-8.x (normally lost as ifconfig(8)
# usage causes dhclient(8) to exit which scrubs
# resolv.conf(5) by-default upon termination).
#
f_quietly cp -fp "$RESOLV_CONF" "$RESOLV_CONF.$$"
err=$( ifconfig $interface inet $ipaddr \
netmask $netmask $options 2>&1 )
if [ $? -eq $SUCCESS ]; then
if [ "$dr" -a "$dr" != "NO" ]; then
err=$( route add default "$dr" 2>&1 )
[ $? -eq $SUCCESS ] || \
dialog_msgbox "$err"
fi
else
dialog_msgbox "$err"
fi
if cmp -s "$RESOLV_CONF" "$RESOLV_CONF.$$"; then
f_quietly rm -f "$RESOLV_CONF.$$"
else
f_quietly mv -f "$RESOLV_CONF.$$" "$RESOLV_CONF"
fi
fi
fi
return $SUCCESS
}
############################################################ MAIN
f_dprintf "%s: Successfully loaded." networking/device.subr
fi # ! $_NETWORKING_DEVICE_SUBR