routing.subr revision 244675
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 244675 2012-12-25 10:47:45Z dteske $
28#
29############################################################ INCLUDES
30
31BSDCFG_SHARE="/usr/share/bsdconfig"
32. $BSDCFG_SHARE/common.subr || exit 1
33f_dprintf "%s: loading includes..." networking/routing.subr
34f_include $BSDCFG_SHARE/sysrc.subr
35f_include $BSDCFG_SHARE/dialog.subr
36f_include $BSDCFG_SHARE/networking/common.subr
37f_include $BSDCFG_SHARE/networking/ipaddr.subr
38
39BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="120.networking"
40f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
41
42############################################################ FUNCTIONS
43
44# f_route_get_default
45#
46# Returns the IP address of the currently active default router.
47#
48f_route_get_default()
49{
50	route -n get default 2> /dev/null | awk \
51	'
52		BEGIN { found = 0 }
53		( $1 == "gateway:" ) \
54		{
55			print $2
56			found = 1
57			exit
58		}
59		END { exit ! found }
60	'
61}
62
63# f_dialog_input_defaultrouter
64#
65# Edits the default router.
66#
67f_dialog_input_defaultrouter()
68{
69	#
70	# Get the defaultrouter. When this is not configured, the default is
71	# "NO", however we don't ever want to present this default to the user
72	# in the following dialog. If the current value is "NO", then try to
73	# obtain the value from the running system using route(8).
74	#
75	# NOTE: Our `f_route_get_default' function will return NULL if the
76	# system does not have an active default router set (which is what we
77	# want).
78	#
79	local defaultrouter="$( f_sysrc_get 'defaultrouter:-NO' )"
80	local defaultrouter_orig="$defaultrouter" # for change-tracking
81	case "$defaultrouter" in
82	[Nn][Oo])
83		defaultrouter=$( f_route_get_default )
84		;;
85	esac
86
87	#
88	# Return with-error when there are NFS-mounts currently active. If the
89	# default router/gateway is changed while NFS-exported directories are
90	# mounted, the system will hang.
91	#
92	if f_nfs_mounted && ! f_jailed; then
93		local setting="$( printf "$msg_current_default_router" \
94		                         "$defaultrouter" )"
95		f_show_msg "$msg_nfs_mounts_may_cause_hang" "$setting"
96		return $FAILURE
97	fi
98
99	#
100	# Loop until the user provides taint-free input.
101	#
102	local retval
103	while :; do
104		defaultrouter=$( f_dialog_input \
105			"$msg_please_enter_default_router" \
106			"$defaultrouter" "$hline_num_punc_tab_enter"
107		)
108		retval=$?
109		[ "$defaultrouter" ] || return $SUCCESS
110		[ $retval -eq $SUCCESS ] || return $retval
111
112		# Taint-check the user's input
113		f_dialog_validate_ipaddr "$defaultrouter" && break
114	done
115
116	#
117	# Save only if the user changed the default router/gateway.
118	#
119	if [ "$defaultrouter" != "$defaultrouter_orig" ]; then
120		f_dialog_info "$msg_saving_default_router"
121
122		# Save the default router/gateway
123		f_sysrc_set defaultrouter "$defaultrouter"
124	fi
125
126	#
127	# Only ask to apply setting if the current defaultrouter is different
128	# than the stored configuration (in rc.conf(5)).
129	#
130	if [ "$( f_route_get_default )" != "$defaultrouter" ]; then
131		f_dialog_clear
132		f_dialog_yesno "$(
133			printf "$msg_activate_default_router" \
134			       "$( f_route_get_default )" "$defaultrouter"
135		)"
136
137		if [ $? -eq $SUCCESS ]; then
138			local err
139
140			# Apply the default router/gateway
141			f_quietly route delete default
142			err=$( route add default "$defaultrouter" 2>&1 )
143			if [ $? -ne $SUCCESS ]; then
144				f_dialog_msgbox "$err"
145				return $FAILURE
146			fi
147		fi
148	fi
149}
150
151############################################################ MAIN
152
153f_dprintf "%s: Successfully loaded." networking/routing.subr
154
155fi # ! $_NETWORKING_ROUTING_SUBR
156