xref: /freebsd/usr.sbin/bsdconfig/networking/share/netmask.subr (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
1ab2043b8SDevin Teskeif [ ! "$_NETWORKING_NETMASK_SUBR" ]; then _NETWORKING_NETMASK_SUBR=1
2ab2043b8SDevin Teske#
37323adacSDevin Teske# Copyright (c) 2006-2013 Devin Teske
4f8ea072aSDevin Teske# All rights reserved.
5ab2043b8SDevin Teske#
6ab2043b8SDevin Teske# Redistribution and use in source and binary forms, with or without
7ab2043b8SDevin Teske# modification, are permitted provided that the following conditions
8ab2043b8SDevin Teske# are met:
9ab2043b8SDevin Teske# 1. Redistributions of source code must retain the above copyright
10ab2043b8SDevin Teske#    notice, this list of conditions and the following disclaimer.
11ab2043b8SDevin Teske# 2. Redistributions in binary form must reproduce the above copyright
12ab2043b8SDevin Teske#    notice, this list of conditions and the following disclaimer in the
13ab2043b8SDevin Teske#    documentation and/or other materials provided with the distribution.
14ab2043b8SDevin Teske#
15ab2043b8SDevin Teske# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
168e37a7c8SDevin Teske# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17ab2043b8SDevin Teske# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18ab2043b8SDevin Teske# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19ab2043b8SDevin Teske# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
208e37a7c8SDevin Teske# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21ab2043b8SDevin Teske# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22ab2043b8SDevin Teske# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23ab2043b8SDevin Teske# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24ab2043b8SDevin Teske# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25ab2043b8SDevin Teske# SUCH DAMAGE.
26ab2043b8SDevin Teske#
27ab2043b8SDevin Teske#
28ab2043b8SDevin Teske############################################################ INCLUDES
29ab2043b8SDevin Teske
30ab2043b8SDevin TeskeBSDCFG_SHARE="/usr/share/bsdconfig"
31ab2043b8SDevin Teske. $BSDCFG_SHARE/common.subr || exit 1
3256961fd7SDevin Teskef_dprintf "%s: loading includes..." networking/netmask.subr
33ab2043b8SDevin Teskef_include $BSDCFG_SHARE/dialog.subr
34ab2043b8SDevin Teskef_include $BSDCFG_SHARE/networking/common.subr
351de60ff0SDevin Teskef_include $BSDCFG_SHARE/strings.subr
36ab2043b8SDevin Teske
37ab2043b8SDevin TeskeBSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="120.networking"
38ab2043b8SDevin Teskef_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
39ab2043b8SDevin Teske
40ab2043b8SDevin Teske############################################################ FUNCTIONS
41ab2043b8SDevin Teske
4235a157a0SDevin Teske# f_dialog_maskerror $error $netmask
43ab2043b8SDevin Teske#
4435a157a0SDevin Teske# Display a msgbox with the appropriate error message for an error returned by
4535a157a0SDevin Teske# the f_validate_netmask function.
46ab2043b8SDevin Teske#
4735a157a0SDevin Teskef_dialog_maskerror()
4835a157a0SDevin Teske{
4935a157a0SDevin Teske	local error="$1" netmask="$2"
5035a157a0SDevin Teske
5135a157a0SDevin Teske	[ ${error:-0} -ne 0 ] || return $SUCCESS
5235a157a0SDevin Teske
5335a157a0SDevin Teske	case "$error" in
54fa2e39c8SDevin Teske	1) f_show_msg "$msg_ipv4_mask_field_contains_invalid_chars" "$mask" ;;
55fa2e39c8SDevin Teske	2) f_show_msg "$msg_ipv4_mask_field_is_null" "$mask" ;;
56fa2e39c8SDevin Teske	3) f_show_msg "$msg_ipv4_mask_field_exceeds_max_value" "$mask" ;;
57fa2e39c8SDevin Teske	4) f_show_msg "$msg_ipv4_mask_field_missing_or_extra" "$mask" ;;
58fa2e39c8SDevin Teske	5) f_show_msg "$msg_ipv4_mask_field_invalid_value" "$mask" ;;
59ab2043b8SDevin Teske	esac
6035a157a0SDevin Teske}
6135a157a0SDevin Teske
6235a157a0SDevin Teske# f_dialog_validate_netmask $netmask
6335a157a0SDevin Teske#
6435a157a0SDevin Teske# Returns zero if the given argument (a subnet mask) is of the proper format.
6535a157a0SDevin Teske#
6635a157a0SDevin Teske# If the subnet mask is determined to be invalid, the appropriate error will be
6735a157a0SDevin Teske# displayed using the f_dialog_maskerror function above.
6835a157a0SDevin Teske#
6935a157a0SDevin Teskef_dialog_validate_netmask()
7035a157a0SDevin Teske{
7135a157a0SDevin Teske	local netmask="$1"
7235a157a0SDevin Teske
7335a157a0SDevin Teske	f_validate_netmask "$netmask"
7435a157a0SDevin Teske	local retval=$?
7535a157a0SDevin Teske
7635a157a0SDevin Teske	# Produce an appropriate error message if necessary.
7735a157a0SDevin Teske	[ $retval -eq $SUCCESS ] || f_dialog_maskerror $retval "$netmask"
78ab2043b8SDevin Teske
79ab2043b8SDevin Teske	return $retval
80ab2043b8SDevin Teske}
81ab2043b8SDevin Teske
82ab2043b8SDevin Teske# f_dialog_input_netmask $interface $netmask
83ab2043b8SDevin Teske#
84ab2043b8SDevin Teske# Edits the IP netmask of the given interface.
85ab2043b8SDevin Teske#
86ab2043b8SDevin Teskef_dialog_input_netmask()
87ab2043b8SDevin Teske{
88ab2043b8SDevin Teske	local interface="$1" _netmask="$2" _input
89ab2043b8SDevin Teske
90ab2043b8SDevin Teske	#
91ab2043b8SDevin Teske	# Return with-error when there are NFS-mounts currently active. If the
92ab2043b8SDevin Teske	# subnet mask is changed while NFS-exported directories are mounted,
93ab2043b8SDevin Teske	# the system may hang (if any NFS mounts are using that interface).
94ab2043b8SDevin Teske	#
95ab2043b8SDevin Teske	if f_nfs_mounted && ! f_jailed; then
96*d4ae33f0SDevin Teske		local setting
97*d4ae33f0SDevin Teske		f_sprintf setting "$msg_current_subnet" \
98*d4ae33f0SDevin Teske		                  "$interface" "$_netmask"
9939c22926SDevin Teske		f_noyes "$msg_nfs_mounts_may_cause_hang" "$setting" ||
100f677a9e2SDevin Teske			return $DIALOG_CANCEL
101ab2043b8SDevin Teske	fi
102ab2043b8SDevin Teske
103ab2043b8SDevin Teske	#
104ab2043b8SDevin Teske	# Loop until the user provides taint-free input.
105ab2043b8SDevin Teske	#
106*d4ae33f0SDevin Teske	local msg
107*d4ae33f0SDevin Teske	f_sprintf msg "$msg_please_enter_subnet_mask" "$interface"
108ab2043b8SDevin Teske	while :; do
109ab2043b8SDevin Teske		#
110ab2043b8SDevin Teske		# Return error status if:
111ab2043b8SDevin Teske		# - User has either pressed ESC or chosen Cancel/No
112298cf604SDevin Teske		# - User has not made any changes to the given value
113ab2043b8SDevin Teske		#
114ec7120b5SDevin Teske		f_dialog_input _input "$msg" "$_netmask" \
115f677a9e2SDevin Teske		               "$hline_num_punc_tab_enter" || return $?
116f677a9e2SDevin Teske		[ "$_netmask" = "$_input" ] && return $DIALOG_CANCEL
117ab2043b8SDevin Teske
118ab2043b8SDevin Teske		# Return success if NULL value was entered
119f677a9e2SDevin Teske		[ "$_input" ] || return $DIALOG_OK
120ab2043b8SDevin Teske
121ab2043b8SDevin Teske		# Take only the first "word" of the user's input
122ab2043b8SDevin Teske		_netmask="$_input"
123ab2043b8SDevin Teske		_netmask="${_netmask%%[$IFS]*}"
124ab2043b8SDevin Teske
125ab2043b8SDevin Teske		# Taint-check the user's input
126ab2043b8SDevin Teske		f_dialog_validate_netmask "$_netmask" && break
127ab2043b8SDevin Teske	done
128ab2043b8SDevin Teske
129ab2043b8SDevin Teske	netmask="$_netmask"
130ab2043b8SDevin Teske}
131ab2043b8SDevin Teske
13256961fd7SDevin Teske############################################################ MAIN
13356961fd7SDevin Teske
13456961fd7SDevin Teskef_dprintf "%s: Successfully loaded." networking/netmask.subr
13556961fd7SDevin Teske
136ab2043b8SDevin Teskefi # ! $_NETWORKING_NETMASK_SUBR
137