xref: /freebsd/usr.sbin/bsdconfig/networking/share/ipaddr.subr (revision 257e70f1d5ee61037c8c59b116538d3b6b1427a2)
1if [ ! "$_NETWORKING_IPADDR_SUBR" ]; then _NETWORKING_IPADDR_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#
28############################################################ INCLUDES
29
30BSDCFG_SHARE="/usr/share/bsdconfig"
31. $BSDCFG_SHARE/common.subr || exit 1
32f_dprintf "%s: loading includes..." networking/ipaddr.subr
33f_include $BSDCFG_SHARE/dialog.subr
34f_include $BSDCFG_SHARE/networking/common.subr
35f_include $BSDCFG_SHARE/strings.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_dialog_iperror $error $ipaddr
43#
44# Display a msgbox with the appropriate error message for an error returned by
45# the f_validate_ipaddr function.
46#
47f_dialog_iperror()
48{
49	local error="$1" ip="$2"
50
51	[ ${error:-0} -ne 0 ] || return $SUCCESS
52
53	case "$error" in
54	1) f_show_msg "$msg_ipv4_addr_octet_contains_invalid_chars" "$ip" ;;
55	2) f_show_msg "$msg_ipv4_addr_octet_is_null" "$ip" ;;
56	3) f_show_msg "$msg_ipv4_addr_octet_exceeds_max_value" "$ip" ;;
57	4) f_show_msg "$msg_ipv4_addr_octet_missing_or_extra" "$ip" ;;
58	esac
59}
60
61# f_dialog_validate_ipaddr $ipaddr
62#
63# Returns zero if the given argument (an IP address) is of the proper format.
64#
65# If the IP address is determined to be invalid, the appropriate error will be
66# displayed using the f_dialog_iperror function above.
67#
68f_dialog_validate_ipaddr()
69{
70	local ip="$1"
71
72	f_validate_ipaddr "$ip"
73	local retval=$?
74
75	# Produce an appropriate error message if necessary.
76	[ $retval -eq $SUCCESS ] || f_dialog_iperror $retval "$ip"
77
78	return $retval
79}
80
81# f_dialog_ip6error $error $ipv6_addr
82#
83# Display a msgbox with the appropriate error message for an error returned by
84# the f_validate_ipaddr6 function above.
85#
86f_dialog_ip6error()
87{
88	local error="$1" ip="$2"
89
90	[ ${error:-0} -ne 0 ] || return $SUCCESS
91
92	case "$error" in
93	1) f_show_msg "$msg_ipv6_addr_segment_contains_invalid_chars" "$ip" ;;
94	2) f_show_msg "$msg_ipv6_addr_too_many_null_segments" "$ip" ;;
95	3) f_show_msg "$msg_ipv6_addr_segment_contains_too_many_chars" "$ip" ;;
96	4) f_show_msg "$msg_ipv6_addr_too_few_or_extra_segments" "$ip" ;;
97	*)
98		if [ $(( $error & 0xF )) -eq 5 ]; then
99			# IPv4 at the end of IPv6 address is invalid
100			f_dialog_iperror $(( $error >> 4 )) "$ip"
101		fi
102	esac
103}
104
105# f_dialog_validate_ipaddr6 $ipv6_addr
106#
107# Returns zero if the given argument (an IPv6 address) is of the proper format.
108#
109# If the IP address is determined to be invalid, the appropriate error will be
110# displayed using the f_dialog_ip6error function above.
111#
112f_dialog_validate_ipaddr6()
113{
114	local ip="$1"
115
116	f_validate_ipaddr6 "$ip"
117	local retval=$?
118
119	# Produce an appropriate error message if necessary.
120	[ $retval -eq $SUCCESS ] || f_dialog_ip6error $retval "$ip"
121
122	return $retval
123}
124
125# f_dialog_input_ipaddr $interface $ipaddr
126#
127# Allows the user to edit a given IP address. If the user does not cancel or
128# press ESC, the $ipaddr environment variable will hold the newly-configured
129# value upon return.
130#
131# Optionally, the user can enter the format "IP_ADDRESS/NBITS" to set the
132# netmask at the same time as the IP address. If such a format is entered by
133# the user, the $netmask environment variable will hold the newly-configured
134# netmask upon return.
135#
136f_dialog_input_ipaddr()
137{
138	local interface="$1" _ipaddr="$2" _input
139
140	#
141	# Return with-error when there are NFS-mounts currently active. If the
142	# IP address is changed while NFS-exported directories are mounted, the
143	# system may hang (if any NFS mounts are using that interface).
144	#
145	if f_nfs_mounted && ! f_jailed; then
146		local setting
147		f_sprintf setting "$msg_current_ipaddr" "$interface" "$_ipaddr"
148		f_noyes "$msg_nfs_mounts_may_cause_hang" "$setting" ||
149			return $DIALOG_CANCEL
150	fi
151
152	local msg
153	f_sprintf msg "$msg_please_enter_new_ip_addr" "$interface"
154
155	#
156	# Loop until the user provides taint-free input.
157	#
158	local retval
159	while :; do
160		#
161		# Return error status if:
162		# - User has either pressed ESC or chosen Cancel/No
163		# - User has not made any changes to the given value
164		#
165		f_dialog_input _input "$msg" "$_ipaddr" \
166		               "$hline_num_punc_tab_enter" || return $?
167		[ "$_ipaddr" = "$_input" ] && return $DIALOG_CANCEL
168
169		# Return success if NULL value was entered
170		[ "$_input" ] || return $DIALOG_OK
171
172		# Take only the first "word" of the user's input
173		_ipaddr="$_input"
174		_ipaddr="${_ipaddr%%[$IFS]*}"
175
176		# Taint-check the user's input
177		f_dialog_validate_ipaddr "${_ipaddr%%/*}" && break
178	done
179
180	#
181	# Support the syntax: IP_ADDRESS/NBITS
182	#
183	local _netmask=""
184	case "$_ipaddr" in
185	*/*)
186		local nbits="${_ipaddr#*/}" n=0
187		_ipaddr="${_ipaddr%%/*}"
188
189		#
190		# Taint-check $nbits to be (a) a positive whole-integer,
191		# and (b) to be less than or equal to 32. Otherwise, set
192		# $n so that the below loop never executes.
193		#
194		( f_isinteger "$nbits" && [ $nbits -ge 0 -a $nbits -le 32 ] ) \
195			|| n=4
196
197		while [ $n -lt 4 ]; do
198			_netmask="$_netmask${_netmask:+.}$((
199				(65280 >> ($nbits - 8 * $n) & 255)
200				* ((8*$n) < $nbits & $nbits <= (8*($n+1)))
201				+ 255 * ($nbits > (8*($n+1)))
202			))"
203			n=$(( $n + 1 ))
204		done
205		;;
206	esac
207
208	ipaddr="$_ipaddr"
209	[ "$_netmask" ] && netmask="$_netmask"
210
211	return $DIALOG_OK
212}
213
214############################################################ MAIN
215
216f_dprintf "%s: Successfully loaded." networking/ipaddr.subr
217
218fi # ! $_NETWORKING_IPADDR_SUBR
219