xref: /freebsd/usr.sbin/bsdconfig/networking/share/netmask.subr (revision 8a166cafe0965f6bd72cd3d2f5372704f05cb5e8)
1if [ ! "$_NETWORKING_NETMASK_SUBR" ]; then _NETWORKING_NETMASK_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$
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/strings.subr
36f_include $BSDCFG_SHARE/networking/common.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_ifconfig_netmask $interface
44#
45# Returns the IPv4 subnet mask associated with $interface.
46#
47f_ifconfig_netmask()
48{
49	local interface="$1" octets
50	octets=$( ifconfig "$interface" 2> /dev/null | awk \
51	'
52		BEGIN { found = 0 }
53		( $1 == "inet" ) \
54		{
55			printf "%s %s %s %s\n",
56				substr($4,3,2),
57				substr($4,5,2),
58				substr($4,7,2),
59				substr($4,9,2)
60			found = 1
61			exit
62		}
63		END { exit ! found }
64	' ) || return $FAILURE
65
66	local octet netmask=
67	for octet in $octets; do
68		netmask="$netmask${netmask:+.}$( printf "%u" "0x$octet" )"
69	done
70	echo $netmask
71}
72
73# f_validate_netmask $netmask
74#
75# Returns zero if the given argument (a subnet mask) is of the proper format.
76#
77# The return status for invalid IP address is one of:
78# 	1	One or more individual fields within the subnet mask (separated
79# 	 	by dots) contains one or more invalid characters.
80# 	2	One or more individual fields within the subnet mask are null
81# 	 	and/or missing.
82# 	3	One or more individual fields within the subnet mask exceeds
83# 	 	the maximum of 255 (a full 8-bit register).
84# 	4	The subnet mask has either too few or too many fields.
85# 	5	One or more individual fields within the subnet mask is an
86# 	 	invalid integer (only 0,128,192,224,240,248,252,254,255 are
87# 	 	valid integers).
88#
89f_validate_netmask()
90{
91	local mask="$1"
92
93	( # Operate within a sub-shell to protect the parent environment
94
95		# Track number of fields for error checking
96		nfields=0
97
98		IFS="." # Split on `dot'
99		for field in $mask; do
100
101			# Return error if the field is null
102			[ "$field" ] || exit 2
103
104			# Return error if not a whole positive integer
105			f_isinteger "$field" || exit 1
106
107			# Return error if the field exceeds 255
108			[ $field -gt 255 ] && exit 3
109
110			# Return error if the field is an invalid integer
111			case "$field" in
112			0|128|192|224|240|248|252|254|255) :;;
113			*) exit 5;;
114			esac
115
116			nfields=$(( $nfields + 1 ))
117
118		done
119
120		[ $nfields -eq 4 ] || exit 4
121	)
122}
123
124# f_dialog_maskerror $error $netmask
125#
126# Display a msgbox with the appropriate error message for an error returned by
127# the f_validate_netmask function.
128#
129f_dialog_maskerror()
130{
131	local error="$1" netmask="$2"
132
133	[ ${error:-0} -ne 0 ] || return $SUCCESS
134
135	case "$error" in
136	1) f_show_msg "$msg_ipv4_mask_field_contains_invalid_chars" "$mask";;
137	2) f_show_msg "$msg_ipv4_mask_field_is_null" "$mask";;
138	3) f_show_msg "$msg_ipv4_mask_field_exceeds_max_value" "$mask";;
139	4) f_show_msg "$msg_ipv4_mask_field_missing_or_extra" "$mask";;
140	5) f_show_msg "$msg_ipv4_mask_field_invalid_value" "$mask";;
141	esac
142}
143
144# f_dialog_validate_netmask $netmask
145#
146# Returns zero if the given argument (a subnet mask) is of the proper format.
147#
148# If the subnet mask is determined to be invalid, the appropriate error will be
149# displayed using the f_dialog_maskerror function above.
150#
151f_dialog_validate_netmask()
152{
153	local netmask="$1"
154
155	f_validate_netmask "$netmask"
156	local retval=$?
157
158	# Produce an appropriate error message if necessary.
159	[ $retval -eq $SUCCESS ] || f_dialog_maskerror $retval "$netmask"
160
161	return $retval
162}
163
164# f_dialog_input_netmask $interface $netmask
165#
166# Edits the IP netmask of the given interface.
167#
168f_dialog_input_netmask()
169{
170	local interface="$1" _netmask="$2" _input
171
172	#
173	# Return with-error when there are NFS-mounts currently active. If the
174	# subnet mask is changed while NFS-exported directories are mounted,
175	# the system may hang (if any NFS mounts are using that interface).
176	#
177	if f_nfs_mounted && ! f_jailed; then
178		local setting="$( printf "$msg_current_subnet" \
179		                         "$interface" "$_netmask" )"
180		f_show_msg "$msg_nfs_mounts_may_cause_hang" "$setting"
181		return $FAILURE
182	fi
183
184	#
185	# Loop until the user provides taint-free input.
186	#
187	local msg="$( printf "$msg_please_enter_subnet_mask" "$interface" )"
188	while :; do
189		#
190		# Return error status if:
191		# - User has either pressed ESC or chosen Cancel/No
192		# - User has not made any changes to the given value
193		#
194		_input=$( f_dialog_input "$msg" "$_netmask" \
195		                         "$hline_num_punc_tab_enter"
196		        ) || return
197		[ "$_netmask" = "$_input" ] && return $FAILURE
198
199		# Return success if NULL value was entered
200		[ "$_input" ] || return $SUCCESS
201
202		# Take only the first "word" of the user's input
203		_netmask="$_input"
204		_netmask="${_netmask%%[$IFS]*}"
205
206		# Taint-check the user's input
207		f_dialog_validate_netmask "$_netmask" && break
208	done
209
210	netmask="$_netmask"
211}
212
213############################################################ MAIN
214
215f_dprintf "%s: Successfully loaded." networking/netmask.subr
216
217fi # ! $_NETWORKING_NETMASK_SUBR
218