xref: /freebsd/usr.sbin/bsdconfig/networking/share/netmask.subr (revision e6bfd18d21b225af6a0ed67ceeaf1293b7b9eba5)
1if [ ! "$_NETWORKING_NETMASK_SUBR" ]; then _NETWORKING_NETMASK_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/netmask.subr
34f_include $BSDCFG_SHARE/dialog.subr
35f_include $BSDCFG_SHARE/networking/common.subr
36f_include $BSDCFG_SHARE/strings.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_dialog_maskerror $error $netmask
44#
45# Display a msgbox with the appropriate error message for an error returned by
46# the f_validate_netmask function.
47#
48f_dialog_maskerror()
49{
50	local error="$1" netmask="$2"
51
52	[ ${error:-0} -ne 0 ] || return $SUCCESS
53
54	case "$error" in
55	1) f_show_msg "$msg_ipv4_mask_field_contains_invalid_chars" "$mask" ;;
56	2) f_show_msg "$msg_ipv4_mask_field_is_null" "$mask" ;;
57	3) f_show_msg "$msg_ipv4_mask_field_exceeds_max_value" "$mask" ;;
58	4) f_show_msg "$msg_ipv4_mask_field_missing_or_extra" "$mask" ;;
59	5) f_show_msg "$msg_ipv4_mask_field_invalid_value" "$mask" ;;
60	esac
61}
62
63# f_dialog_validate_netmask $netmask
64#
65# Returns zero if the given argument (a subnet mask) is of the proper format.
66#
67# If the subnet mask is determined to be invalid, the appropriate error will be
68# displayed using the f_dialog_maskerror function above.
69#
70f_dialog_validate_netmask()
71{
72	local netmask="$1"
73
74	f_validate_netmask "$netmask"
75	local retval=$?
76
77	# Produce an appropriate error message if necessary.
78	[ $retval -eq $SUCCESS ] || f_dialog_maskerror $retval "$netmask"
79
80	return $retval
81}
82
83# f_dialog_input_netmask $interface $netmask
84#
85# Edits the IP netmask of the given interface.
86#
87f_dialog_input_netmask()
88{
89	local interface="$1" _netmask="$2" _input
90
91	#
92	# Return with-error when there are NFS-mounts currently active. If the
93	# subnet mask is changed while NFS-exported directories are mounted,
94	# the system may hang (if any NFS mounts are using that interface).
95	#
96	if f_nfs_mounted && ! f_jailed; then
97		local setting
98		f_sprintf setting "$msg_current_subnet" \
99		                  "$interface" "$_netmask"
100		f_noyes "$msg_nfs_mounts_may_cause_hang" "$setting" ||
101			return $DIALOG_CANCEL
102	fi
103
104	#
105	# Loop until the user provides taint-free input.
106	#
107	local msg
108	f_sprintf msg "$msg_please_enter_subnet_mask" "$interface"
109	while :; do
110		#
111		# Return error status if:
112		# - User has either pressed ESC or chosen Cancel/No
113		# - User has not made any changes to the given value
114		#
115		f_dialog_input _input "$msg" "$_netmask" \
116		               "$hline_num_punc_tab_enter" || return $?
117		[ "$_netmask" = "$_input" ] && return $DIALOG_CANCEL
118
119		# Return success if NULL value was entered
120		[ "$_input" ] || return $DIALOG_OK
121
122		# Take only the first "word" of the user's input
123		_netmask="$_input"
124		_netmask="${_netmask%%[$IFS]*}"
125
126		# Taint-check the user's input
127		f_dialog_validate_netmask "$_netmask" && break
128	done
129
130	netmask="$_netmask"
131}
132
133############################################################ MAIN
134
135f_dprintf "%s: Successfully loaded." networking/netmask.subr
136
137fi # ! $_NETWORKING_NETMASK_SUBR
138