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