1if [ ! "$_NETWORKING_IPADDR_SUBR" ]; then _NETWORKING_IPADDR_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#
| 1if [ ! "$_NETWORKING_IPADDR_SUBR" ]; then _NETWORKING_IPADDR_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: head/usr.sbin/bsdconfig/networking/share/ipaddr.subr 240684 2012-09-18 22:28:42Z dteske $
| 27# $FreeBSD: head/usr.sbin/bsdconfig/networking/share/ipaddr.subr 240768 2012-09-20 23:44:13Z dteske $
|
28# 29############################################################ INCLUDES 30 31BSDCFG_SHARE="/usr/share/bsdconfig" 32. $BSDCFG_SHARE/common.subr || exit 1 33f_include $BSDCFG_SHARE/sysrc.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_inet $interface 44# 45# Returns the IPv4 address associated with $interface. 46# 47f_ifconfig_inet() 48{ 49 local interface="$1" 50 ifconfig "$interface" 2> /dev/null | awk \ 51 ' 52 BEGIN { found = 0 } 53 ( $1 == "inet" ) \ 54 { 55 print $2 56 found = 1 57 exit 58 } 59 END { exit ! found } 60 ' 61} 62 63# f_dialog_validate_ipaddr $ipaddr 64# 65# Returns zero if the given argument (an IP address) is of the proper format. 66# 67# The return status for invalid IP address is one of: 68# 1 One or more individual octets within the IP address (separated 69# by dots) contains one or more invalid characters. 70# 2 One or more individual octets within the IP address are null 71# and/or missing. 72# 3 One or more individual octets within the IP address exceeds the 73# maximum of 255 (or 2^8, being an octet comprised of 8 bits). 74# 4 The IP address has either too few or too many octets. 75# 76# If the IP address is determined to be invalid, the appropriate error will be 77# displayed using the above dialog_msgbox function. 78# 79f_dialog_validate_ipaddr() 80{ 81 local ip="$1" 82 83 ( # Operate within a sub-shell to protect the parent environment 84 85 # Track number of octets for error checking 86 noctets=0 87 88 IFS="." # Split on `dot' 89 for octet in $ip; do 90 91 # Return error if the octet is null 92 [ "$octet" ] || exit 2 93 94 # Return error if not a whole integer 95 f_isinteger "$octet" || exit 1 96 97 # Return error if not a positive integer 98 [ $octet -ge 0 ] || exit 1 99 100 # Return error if the octet exceeds 255 101 [ $octet -gt 255 ] && exit 3 102 103 noctets=$(( $noctets + 1 )) 104 105 done 106 107 [ $noctets -eq 4 ] || exit 4 108 ) 109 110 # 111 # Produce an appropriate error message if necessary. 112 # 113 local retval=$? 114 case $retval in 115 1) f_dialog_msgbox "$( printf \ 116 "$msg_ipv4_addr_octet_contains_invalid_chars" "$ip" )";; 117 2) f_dialog_msgbox "$( printf \ 118 "$msg_ipv4_addr_octet_is_null" "$ip" )";; 119 3) f_dialog_msgbox "$( printf \ 120 "$msg_ipv4_addr_octet_exceeds_max_value" "$ip" )";; 121 4) f_dialog_msgbox "$( printf \ 122 "$msg_ipv4_addr_octet_missing_or_extra" "$ip" )";; 123 esac 124 125 return $retval 126} 127 128# f_dialog_input_ipaddr $interface $ipaddr 129# 130# Allows the user to edit a given IP address. If the user does not cancel or 131# press ESC, the $ipaddr environment variable will hold the newly-configured 132# value upon return. 133# 134# Optionally, the user can enter the format "IP_ADDRESS/NBITS" to set the 135# netmask at the same time as the IP address. If such a format is entered by 136# the user, the $netmask environment variable will hold the newly-configured 137# netmask upon return. 138# 139f_dialog_input_ipaddr() 140{ 141 local interface="$1" _ipaddr="$2" _input 142 143 # 144 # Return with-error when there are NFS-mounts currently active. If the 145 # IP address is changed while NFS-exported directories are mounted, the 146 # system may hang (if any NFS mounts are using that interface). 147 # 148 if f_nfs_mounted && ! f_jailed; then 149 local setting="$( printf "$msg_current_ipaddr" \ 150 "$interface" "$_ipaddr" )" 151 local message="$( printf "$msg_nfs_mounts_may_cause_hang" \ 152 "$setting" )" 153 f_dialog_msgbox "$message" 154 return $FAILURE 155 fi 156 157 local msg="$( printf "$msg_please_enter_new_ip_addr" "$interface" )" 158 local hline="$hline_num_punc_tab_enter" 159 local size="$( f_dialog_inputbox_size \ 160 "$DIALOG_TITLE" \ 161 "$DIALOG_BACKTITLE" \ 162 "$msg" \ 163 "$_ipaddr" \ 164 "$hline" )" 165 166 # 167 # Loop until the user provides taint-free input. 168 # 169 while :; do
| 28# 29############################################################ INCLUDES 30 31BSDCFG_SHARE="/usr/share/bsdconfig" 32. $BSDCFG_SHARE/common.subr || exit 1 33f_include $BSDCFG_SHARE/sysrc.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_inet $interface 44# 45# Returns the IPv4 address associated with $interface. 46# 47f_ifconfig_inet() 48{ 49 local interface="$1" 50 ifconfig "$interface" 2> /dev/null | awk \ 51 ' 52 BEGIN { found = 0 } 53 ( $1 == "inet" ) \ 54 { 55 print $2 56 found = 1 57 exit 58 } 59 END { exit ! found } 60 ' 61} 62 63# f_dialog_validate_ipaddr $ipaddr 64# 65# Returns zero if the given argument (an IP address) is of the proper format. 66# 67# The return status for invalid IP address is one of: 68# 1 One or more individual octets within the IP address (separated 69# by dots) contains one or more invalid characters. 70# 2 One or more individual octets within the IP address are null 71# and/or missing. 72# 3 One or more individual octets within the IP address exceeds the 73# maximum of 255 (or 2^8, being an octet comprised of 8 bits). 74# 4 The IP address has either too few or too many octets. 75# 76# If the IP address is determined to be invalid, the appropriate error will be 77# displayed using the above dialog_msgbox function. 78# 79f_dialog_validate_ipaddr() 80{ 81 local ip="$1" 82 83 ( # Operate within a sub-shell to protect the parent environment 84 85 # Track number of octets for error checking 86 noctets=0 87 88 IFS="." # Split on `dot' 89 for octet in $ip; do 90 91 # Return error if the octet is null 92 [ "$octet" ] || exit 2 93 94 # Return error if not a whole integer 95 f_isinteger "$octet" || exit 1 96 97 # Return error if not a positive integer 98 [ $octet -ge 0 ] || exit 1 99 100 # Return error if the octet exceeds 255 101 [ $octet -gt 255 ] && exit 3 102 103 noctets=$(( $noctets + 1 )) 104 105 done 106 107 [ $noctets -eq 4 ] || exit 4 108 ) 109 110 # 111 # Produce an appropriate error message if necessary. 112 # 113 local retval=$? 114 case $retval in 115 1) f_dialog_msgbox "$( printf \ 116 "$msg_ipv4_addr_octet_contains_invalid_chars" "$ip" )";; 117 2) f_dialog_msgbox "$( printf \ 118 "$msg_ipv4_addr_octet_is_null" "$ip" )";; 119 3) f_dialog_msgbox "$( printf \ 120 "$msg_ipv4_addr_octet_exceeds_max_value" "$ip" )";; 121 4) f_dialog_msgbox "$( printf \ 122 "$msg_ipv4_addr_octet_missing_or_extra" "$ip" )";; 123 esac 124 125 return $retval 126} 127 128# f_dialog_input_ipaddr $interface $ipaddr 129# 130# Allows the user to edit a given IP address. If the user does not cancel or 131# press ESC, the $ipaddr environment variable will hold the newly-configured 132# value upon return. 133# 134# Optionally, the user can enter the format "IP_ADDRESS/NBITS" to set the 135# netmask at the same time as the IP address. If such a format is entered by 136# the user, the $netmask environment variable will hold the newly-configured 137# netmask upon return. 138# 139f_dialog_input_ipaddr() 140{ 141 local interface="$1" _ipaddr="$2" _input 142 143 # 144 # Return with-error when there are NFS-mounts currently active. If the 145 # IP address is changed while NFS-exported directories are mounted, the 146 # system may hang (if any NFS mounts are using that interface). 147 # 148 if f_nfs_mounted && ! f_jailed; then 149 local setting="$( printf "$msg_current_ipaddr" \ 150 "$interface" "$_ipaddr" )" 151 local message="$( printf "$msg_nfs_mounts_may_cause_hang" \ 152 "$setting" )" 153 f_dialog_msgbox "$message" 154 return $FAILURE 155 fi 156 157 local msg="$( printf "$msg_please_enter_new_ip_addr" "$interface" )" 158 local hline="$hline_num_punc_tab_enter" 159 local size="$( f_dialog_inputbox_size \ 160 "$DIALOG_TITLE" \ 161 "$DIALOG_BACKTITLE" \ 162 "$msg" \ 163 "$_ipaddr" \ 164 "$hline" )" 165 166 # 167 # Loop until the user provides taint-free input. 168 # 169 while :; do
|
170 eval $DIALOG \
| 170 local dialog_inputbox 171 dialog_inputbox=$( eval $DIALOG \
|
171 --title \"\$DIALOG_TITLE\" \ 172 --backtitle \"\$DIALOG_BACKTITLE\" \ 173 --hline \"\$hline\" \ 174 --ok-label \"\$msg_ok\" \ 175 --cancel-label \"\$msg_cancel\" \ 176 --inputbox \"\$msg\" $size \ 177 \"\$_ipaddr\" \
| 172 --title \"\$DIALOG_TITLE\" \ 173 --backtitle \"\$DIALOG_BACKTITLE\" \ 174 --hline \"\$hline\" \ 175 --ok-label \"\$msg_ok\" \ 176 --cancel-label \"\$msg_cancel\" \ 177 --inputbox \"\$msg\" $size \ 178 \"\$_ipaddr\" \
|
178 2> "$DIALOG_TMPDIR/dialog.inputbox.$$"
| 179 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD 180 )
|
179 180 local retval=$?
| 181 182 local retval=$?
|
| 183 setvar DIALOG_INPUTBOX_$$ "$dialog_inputbox"
|
181 _input=$( f_dialog_inputstr ) 182 183 # 184 # Return error status if: 185 # - User has not made any changes to the given value 186 # - User has either pressed ESC or chosen Cancel/No 187 # 188 [ "$_ipaddr" = "$_input" ] && return $FAILURE 189 [ $retval -eq $SUCCESS ] || return $retval 190 191 # Return success if NULL value was entered 192 [ "$_input" ] || return $SUCCESS 193 194 # Take only the first "word" of the user's input 195 _ipaddr="$_input" 196 _ipaddr="${_ipaddr%%[$IFS]*}" 197 198 # Taint-check the user's input 199 f_dialog_validate_ipaddr "${_ipaddr%%/*}" && break 200 done 201 202 # 203 # Support the syntax: IP_ADDRESS/NBITS 204 # 205 local _netmask="" 206 case "$_ipaddr" in 207 */*) 208 local nbits="${_ipaddr#*/}" n=0 209 _ipaddr="${_ipaddr%%/*}" 210 211 # 212 # Taint-check $nbits to be (a) a positive whole-integer, 213 # and (b) to be less than or equal to 32. Otherwise, set 214 # $n so that the below loop never executes. 215 # 216 ( f_isinteger "$nbits" && [ $nbits -ge 0 -a $nbits -le 32 ] ) \ 217 || n=4 218 219 while [ $n -lt 4 ]; do 220 _netmask="$_netmask${_netmask:+.}$(( 221 (65280 >> ($nbits - 8 * $n) & 255) 222 * ((8*$n) < $nbits & $nbits <= (8*($n+1))) 223 + 255 * ($nbits > (8*($n+1))) 224 ))" 225 n=$(( $n + 1 )) 226 done 227 ;; 228 esac 229 230 ipaddr="$_ipaddr" 231 [ "$_netmask" ] && netmask="$_netmask" 232 233 return $SUCCESS 234} 235 236fi # ! $_NETWORKING_IPADDR_SUBR
| 184 _input=$( f_dialog_inputstr ) 185 186 # 187 # Return error status if: 188 # - User has not made any changes to the given value 189 # - User has either pressed ESC or chosen Cancel/No 190 # 191 [ "$_ipaddr" = "$_input" ] && return $FAILURE 192 [ $retval -eq $SUCCESS ] || return $retval 193 194 # Return success if NULL value was entered 195 [ "$_input" ] || return $SUCCESS 196 197 # Take only the first "word" of the user's input 198 _ipaddr="$_input" 199 _ipaddr="${_ipaddr%%[$IFS]*}" 200 201 # Taint-check the user's input 202 f_dialog_validate_ipaddr "${_ipaddr%%/*}" && break 203 done 204 205 # 206 # Support the syntax: IP_ADDRESS/NBITS 207 # 208 local _netmask="" 209 case "$_ipaddr" in 210 */*) 211 local nbits="${_ipaddr#*/}" n=0 212 _ipaddr="${_ipaddr%%/*}" 213 214 # 215 # Taint-check $nbits to be (a) a positive whole-integer, 216 # and (b) to be less than or equal to 32. Otherwise, set 217 # $n so that the below loop never executes. 218 # 219 ( f_isinteger "$nbits" && [ $nbits -ge 0 -a $nbits -le 32 ] ) \ 220 || n=4 221 222 while [ $n -lt 4 ]; do 223 _netmask="$_netmask${_netmask:+.}$(( 224 (65280 >> ($nbits - 8 * $n) & 255) 225 * ((8*$n) < $nbits & $nbits <= (8*($n+1))) 226 + 255 * ($nbits > (8*($n+1))) 227 ))" 228 n=$(( $n + 1 )) 229 done 230 ;; 231 esac 232 233 ipaddr="$_ipaddr" 234 [ "$_netmask" ] && netmask="$_netmask" 235 236 return $SUCCESS 237} 238 239fi # ! $_NETWORKING_IPADDR_SUBR
|