routing.subr revision 240783
1if [ ! "$_NETWORKING_ROUTING_SUBR" ]; then _NETWORKING_ROUTING_SUBR=1
2#
3# Copyright (c) 2006-2012 Devin Teske
4# All Rights Reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, THE
17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20# DAMAGES (INLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25# SUCH DAMAGE.
26#
27# $FreeBSD: head/usr.sbin/bsdconfig/networking/share/routing.subr 240783 2012-09-21 19:03:25Z dteske $
28#
29############################################################ INCLUDES
30
31BSDCFG_SHARE="/usr/share/bsdconfig"
32. $BSDCFG_SHARE/common.subr || exit 1
33f_include $BSDCFG_SHARE/sysrc.subr
34f_include $BSDCFG_SHARE/dialog.subr
35f_include $BSDCFG_SHARE/networking/common.subr
36f_include $BSDCFG_SHARE/networking/ipaddr.subr
37
38BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="120.networking"
39f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
40
41############################################################ FUNCTIONS
42
43# f_route_get_default
44#
45# Returns the IP address of the currently active default router.
46#
47f_route_get_default()
48{
49	route -n get default 2> /dev/null | awk \
50	'
51		BEGIN { found = 0 }
52		( $1 == "gateway:" ) \
53		{
54			print $2
55			found = 1
56			exit
57		}
58		END { exit ! found }
59	'
60}
61
62# f_dialog_input_defaultrouter
63#
64# Edits the default router.
65#
66f_dialog_input_defaultrouter()
67{
68	#
69	# Get the defaultrouter. When this is not configured, the default is
70	# "NO", however we don't ever want to present this default to the user
71	# in the following dialog. If the current value is "NO", then try to
72	# obtain the value from the running system using route(8).
73	#
74	# NOTE: Our `f_route_get_default' function will return NULL if the
75	# system does not have an active default router set (which is what we
76	# want).
77	#
78	local defaultrouter="$( f_sysrc_get 'defaultrouter:-NO' )"
79	local defaultrouter_orig="$defaultrouter" # for change-tracking
80	case "$defaultrouter" in
81	[Nn][Oo])
82		defaultrouter=$( f_route_get_default )
83		;;
84	esac
85
86	#
87	# Return with-error when there are NFS-mounts currently active. If the
88	# default router/gateway is changed while NFS-exported directories are
89	# mounted, the system will hang.
90	#
91	if f_nfs_mounted && ! f_jailed; then
92		local setting="$( printf "$msg_current_default_router" \
93		                         "$defaultrouter" )"
94		local message="$( printf "$msg_nfs_mounts_may_cause_hang" \
95		                         "$setting" )"
96		f_dialog_msgbox "$message"
97		return $FAILURE
98	fi
99
100	local msg="$msg_please_enter_default_router"
101	local hline="$hline_num_punc_tab_enter"
102	local size="$( f_dialog_inputbox_size \
103	               		"$DIALOG_TITLE"     \
104	               		"$DIALOG_BACKTITLE" \
105	               		"$msg"              \
106	               		"$defaultrouter"    \
107	               		"$hline"            )"
108
109	#
110	# Loop until the user provides taint-free input.
111	#
112	while :; do
113		local dialog_inputbox
114		dialog_inputbox=$( eval $DIALOG \
115			--title \"\$DIALOG_TITLE\"         \
116			--backtitle \"\$DIALOG_BACKTITLE\" \
117			--hline \"\$hline\"                \
118			--ok-label \"\$msg_ok\"            \
119			--cancel-label \"\$msg_cancel\"    \
120			--inputbox \"\$msg\" $size         \
121			\"\$defaultrouter\"                \
122			2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
123		)
124
125		local retval=$?
126		setvar DIALOG_INPUTBOX_$$ "$dialog_inputbox"
127		defaultrouter=$( f_dialog_inputstr )
128
129		[ "$defaultrouter" ] || return $SUCCESS
130		[ $retval -eq $SUCCESS ] || return $retval
131
132		# Taint-check the user's input
133		f_dialog_validate_ipaddr "$defaultrouter" && break
134	done
135
136	#
137	# Save only if the user changed the default router/gateway.
138	#
139	if [ "$defaultrouter" != "$defaultrouter_orig" ]; then
140		f_dialog_info "$msg_saving_default_router"
141
142		# Save the default router/gateway
143		f_sysrc_set defaultrouter "$defaultrouter"
144	fi
145
146	#
147	# Only ask to apply setting if the current defaultrouter is different
148	# than the stored configuration (in rc.conf(5)).
149	#
150	if [ "$( f_route_get_default )" != "$defaultrouter" ]; then
151		f_dialog_clear
152		f_dialog_yesno "$(
153			printf "$msg_activate_default_router" \
154			       "$( f_route_get_default )" "$defaultrouter"
155		)"
156
157		if [ $? -eq $SUCCESS ]; then
158			local err
159
160			# Apply the default router/gateway
161			f_quietly route delete default
162			err=$( route add default "$defaultrouter" 2>&1 )
163			if [ $? -ne $SUCCESS ]; then
164				f_dialog_msgbox "$err"
165				return $FAILURE
166			fi
167		fi
168	fi
169}
170
171fi # ! $_NETWORKING_ROUTING_SUBR
172