xref: /freebsd/usr.sbin/bsdconfig/networking/share/routing.subr (revision 0b3105a37d7adcadcb720112fed4dc4e8040be99)
1if [ ! "$_NETWORKING_ROUTING_SUBR" ]; then _NETWORKING_ROUTING_SUBR=1
2#
3# Copyright (c) 2006-2013 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 (INCLUDING, 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$
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/dialog.subr
35f_include $BSDCFG_SHARE/media/tcpip.subr
36f_include $BSDCFG_SHARE/networking/common.subr
37f_include $BSDCFG_SHARE/networking/ipaddr.subr
38f_include $BSDCFG_SHARE/strings.subr
39f_include $BSDCFG_SHARE/sysrc.subr
40
41BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="120.networking"
42f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
43
44############################################################ FUNCTIONS
45
46# f_dialog_input_defaultrouter
47#
48# Edits the default router.
49#
50f_dialog_input_defaultrouter()
51{
52	local funcname=f_dialog_input_defaultrouter
53
54	#
55	# Get the defaultrouter. When this is not configured, the default is
56	# "NO", however we don't ever want to present this default to the user
57	# in the following dialog. If the current value is "NO", then try to
58	# obtain the value from the running system using route(8).
59	#
60	# NOTE: Our `f_route_get_default' function will return NULL if the
61	# system does not have an active default router set (which is what we
62	# want).
63	#
64	local defaultrouter="$( f_sysrc_get 'defaultrouter:-NO' )"
65	local defaultrouter_orig="$defaultrouter" # for change-tracking
66	case "$defaultrouter" in
67	[Nn][Oo]) f_route_get_default defaultrouter ;;
68	esac
69
70	#
71	# Return with-error when there are NFS-mounts currently active. If the
72	# default router/gateway is changed while NFS-exported directories are
73	# mounted, the system will hang.
74	#
75	if f_nfs_mounted && ! f_jailed; then
76		local setting
77		f_sprintf setting "$msg_current_default_router" \
78		                  "$defaultrouter"
79		f_noyes "$msg_nfs_mounts_may_cause_hang" "$setting" ||
80			return $DIALOG_CANCEL
81	fi
82
83	#
84	# Loop until the user provides taint-free input.
85	#
86	local retval
87	while :; do
88		f_dialog_input defaultrouter \
89		               "$msg_please_enter_default_router" \
90		               "$defaultrouter" "$hline_num_punc_tab_enter"
91		retval=$?
92		[ "$defaultrouter" ] || return $DIALOG_OK
93		[ $retval -eq $DIALOG_OK ] || return $retval
94
95		# Taint-check the user's input
96		f_dialog_validate_ipaddr "$defaultrouter" && break
97	done
98
99	#
100	# Save only if the user changed the default router/gateway.
101	#
102	if [ "$defaultrouter" != "$defaultrouter_orig" ]; then
103		f_dialog_info "$msg_saving_default_router"
104
105		# Save the default router/gateway
106		f_eval_catch $funcname f_sysrc_set \
107			'f_sysrc_set defaultrouter "%s"' "$defaultrouter"
108	fi
109
110	#
111	# Only ask to apply setting if the current defaultrouter is different
112	# than the stored configuration (in rc.conf(5)).
113	#
114	local dr
115	f_route_get_default dr
116	if [ "$dr" != "$defaultrouter" ]; then
117		f_dialog_clear
118		f_yesno "$msg_activate_default_router" "$dr" "$defaultrouter"
119		if [ $? -eq $DIALOG_OK ]; then
120			# Apply the default router/gateway
121			f_eval_catch -d $funcname route 'route delete default'
122			f_eval_catch $funcname route \
123				'route add default "%s"' "$defaultrouter" ||
124				return $DIALOG_CANCEL
125		fi
126	fi
127}
128
129############################################################ MAIN
130
131f_dprintf "%s: Successfully loaded." networking/routing.subr
132
133fi # ! $_NETWORKING_ROUTING_SUBR
134