1if [ ! "$_MEDIA_TCPIP_SUBR" ]; then _MEDIA_TCPIP_SUBR=1 2# 3# Copyright (c) 2012-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..." media/tcpip.subr 33f_include $BSDCFG_SHARE/device.subr 34f_include $BSDCFG_SHARE/dialog.subr 35f_include $BSDCFG_SHARE/strings.subr 36f_include $BSDCFG_SHARE/struct.subr 37f_include $BSDCFG_SHARE/variable.subr 38 39BSDCFG_LIBE="/usr/libexec/bsdconfig" 40f_include_lang $BSDCFG_LIBE/include/messages.subr 41 42TCP_HELPFILE=$BSDCFG_LIBE/include/tcp.hlp 43NETWORK_DEVICE_HELPFILE=$BSDCFG_LIBE/include/network_device.hlp 44 45############################################################ GLOBALS 46 47# 48# Path to resolv.conf(5). 49# 50: ${RESOLV_CONF:="/etc/resolv.conf"} 51 52# 53# Path to nsswitch.conf(5). 54# 55: ${NSSWITCH_CONF:="/etc/nsswitch.conf"} 56 57# 58# Path to hosts(5) 59# 60: ${ETC_HOSTS:="/etc/hosts"} 61 62# 63# Structure of dhclient.leases(5) lease { ... } entry 64# 65f_struct_define DHCP_LEASE \ 66 interface \ 67 fixed_address \ 68 filename \ 69 server_name \ 70 script \ 71 medium \ 72 host_name \ 73 subnet_mask \ 74 routers \ 75 domain_name_servers \ 76 domain_name \ 77 broadcast_address \ 78 dhcp_lease_time \ 79 dhcp_message_type \ 80 dhcp_server_identifier \ 81 dhcp_renewal_time \ 82 dhcp_rebinding_time \ 83 renew \ 84 rebind \ 85 expire 86 87############################################################ FUNCTIONS 88 89# f_validate_hostname $hostname 90# 91# Returns zero if the given argument (a fully-qualified hostname) is compliant 92# with standards set-forth in RFC's 952 and 1123 of the Network Working Group: 93# 94# RFC 952 - DoD Internet host table specification 95# http://tools.ietf.org/html/rfc952 96# 97# RFC 1123 - Requirements for Internet Hosts - Application and Support 98# http://tools.ietf.org/html/rfc1123 99# 100# See http://en.wikipedia.org/wiki/Hostname for a brief overview. 101# 102# The return status for invalid hostnames is one of: 103# 255 Entire hostname exceeds the maximum length of 255 characters. 104# 63 One or more individual labels within the hostname (separated by 105# dots) exceeds the maximum of 63 characters. 106# 1 One or more individual labels within the hostname contains one 107# or more invalid characters. 108# 2 One or more individual labels within the hostname starts or 109# ends with a hyphen (hyphens are allowed, but a label cannot 110# begin or end with a hyphen). 111# 3 One or more individual labels within the hostname are null. 112# 113# To call this function and display an appropriate error message to the user 114# based on the above error codes, use the following function defined in 115# dialog.subr: 116# 117# f_dialog_validate_hostname $hostname 118# 119f_validate_hostname() 120{ 121 local fqhn="$1" 122 123 # Return error if the hostname exceeds 255 characters 124 [ ${#fqhn} -gt 255 ] && return 255 125 126 local IFS="." # Split on `dot' 127 for label in $fqhn; do 128 # Return error if the label exceeds 63 characters 129 [ ${#label} -gt 63 ] && return 63 130 131 # Return error if the label is null 132 [ "$label" ] || return 3 133 134 # Return error if label begins/ends with dash 135 case "$label" in -*|*-) return 2; esac 136 137 # Return error if the label contains any invalid chars 138 case "$label" in *[!0-9a-zA-Z-]*) return 1; esac 139 done 140 141 return $SUCCESS 142} 143 144# f_inet_atoi $ipv4_address [$var_to_set] 145# 146# Convert an IPv4 address or mask from dotted-quad notation (e.g., `127.0.0.1' 147# or `255.255.255.0') to a 32-bit unsigned integer for the purpose of network 148# and broadcast calculations. For example, one can validate that two addresses 149# are on the same network: 150# 151# f_inet_atoi 1.2.3.4 ip1num 152# f_inet_atoi 1.2.4.5 ip2num 153# f_inet_atoi 255.255.0.0 masknum 154# if [ $(( $ip1num & $masknum )) -eq \ 155# $(( $ip2num & $masknum )) ] 156# then 157# : IP addresses are on same network 158# fi 159# 160# See f_validate_ipaddr() below for an additional example usage, on calculating 161# network and broadcast addresses. 162# 163# If $var_to_set is missing or NULL, the converted IP address is printed to 164# standard output for capturing in a sub-shell (which is less-recommended 165# because of performance degredation; for example, when called in a loop). 166# 167f_inet_atoi() 168{ 169 local __addr="$1" __var_to_set="$2" __num=0 170 if f_validate_ipaddr "$__addr"; then 171 local IFS=. 172 set -- $__addr 173 __num=$(( ($1 << 24) + ($2 << 16) + ($3 << 8) + $4 )) 174 fi 175 if [ "$__var_to_set" ]; then 176 setvar "$__var_to_set" $__num 177 else 178 echo $__num 179 fi 180} 181 182# f_validate_ipaddr $ipaddr [$netmask] 183# 184# Returns zero if the given argument (an IP address) is of the proper format. 185# 186# The return status for invalid IP address is one of: 187# 1 One or more individual octets within the IP address (separated 188# by dots) contains one or more invalid characters. 189# 2 One or more individual octets within the IP address are null 190# and/or missing. 191# 3 One or more individual octets within the IP address exceeds the 192# maximum of 255 (or 2^8, being an octet comprised of 8 bits). 193# 4 The IP address has either too few or too many octets. 194# 195# If a netmask is provided, the IP address is checked further: 196# 197# 5 The IP address must not be the network or broadcast address. 198# 199f_validate_ipaddr() 200{ 201 local ip="$1" mask="$2" 202 203 # Track number of octets for error checking 204 local noctets=0 205 206 local oldIFS="$IFS" IFS="." # Split on `dot' 207 for octet in $ip; do 208 # Return error if the octet is null 209 [ "$octet" ] || return 2 210 211 # Return error if not a whole integer 212 f_isinteger "$octet" || return 1 213 214 # Return error if not a positive integer 215 [ $octet -ge 0 ] || return 1 216 217 # Return error if the octet exceeds 255 218 [ $octet -gt 255 ] && return 3 219 220 noctets=$(( $noctets + 1 )) 221 done 222 IFS="$oldIFS" 223 224 [ $noctets -eq 4 ] || return 4 225 226 # 227 # The IP address must not be network or broadcast address. 228 # 229 if [ "$mask" ]; then 230 local ipnum masknum netnum bcastnum 231 local max_addr=4294967295 # 255.255.255.255 232 233 f_inet_atoi $ip ipnum 234 f_inet_atoi $mask masknum 235 236 netnum=$(( $ipnum & $masknum )) 237 bcastnum=$(( ($ipnum & $masknum)+$max_addr-$masknum )) 238 239 if [ "$masknum" ] && 240 [ $ipnum -eq $netnum -o $ipnum -eq $bcastnum ] 241 then 242 return 5 243 fi 244 fi 245 246 return $SUCCESS 247} 248 249# f_validate_ipaddr6 $ipv6_addr 250# 251# Returns zero if the given argument (an IPv6 address) is of the proper format. 252# 253# The return status for invalid IP address is one of: 254# 1 One or more individual segments within the IP address 255# (separated by colons) contains one or more invalid characters. 256# Segments must contain only combinations of the characters 0-9, 257# A-F, or a-f. 258# 2 Too many/incorrect null segments. A single null segment is 259# allowed within the IP address (separated by colons) but not 260# allowed at the beginning or end (unless a double-null segment; 261# i.e., "::*" or "*::"). 262# 3 One or more individual segments within the IP address 263# (separated by colons) exceeds the length of 4 hex-digits. 264# 4 The IP address entered has either too few (less than 3), too 265# many (more than 8), or not enough segments, separated by 266# colons. 267# 5* The IPv4 address at the end of the IPv6 address is invalid. 268# * When there is an error with the dotted-quad IPv4 address at the 269# end of the IPv6 address, the return value of 5 is OR'd with a 270# bit-shifted (<< 4) return of f_validate_ipaddr. 271# 272f_validate_ipaddr6() 273{ 274 local ip="${1%\%*}" # removing the interface specification if-present 275 276 local IFS=":" # Split on `colon' 277 set -- $ip: 278 279 # Return error if too many or too few segments 280 # Using 9 as max in case of leading or trailing null spanner 281 [ $# -gt 9 -o $# -lt 3 ] && return 4 282 283 local h="[0-9A-Fa-f]" 284 local nulls=0 nsegments=$# contains_ipv4_segment= 285 286 while [ $# -gt 0 ]; do 287 288 segment="${1%:}" 289 shift 290 291 # 292 # Return error if this segment makes one null too-many. A 293 # single null segment is allowed anywhere in the middle as well 294 # as double null segments are allowed at the beginning or end 295 # (but not both). 296 # 297 if [ ! "$segment" ]; then 298 nulls=$(( $nulls + 1 )) 299 if [ $nulls -eq 3 ]; then 300 # Only valid syntax for 3 nulls is `::' 301 [ "$ip" = "::" ] || return 2 302 elif [ $nulls -eq 2 ]; then 303 # Only valid if begins/ends with `::' 304 case "$ip" in 305 ::*|*::) : fall thru ;; 306 *) return 2 307 esac 308 fi 309 continue 310 fi 311 312 # 313 # Return error if not a valid hexadecimal short 314 # 315 case "$segment" in 316 $h|$h$h|$h$h$h|$h$h$h$h) 317 : valid segment of 1-4 hexadecimal digits 318 ;; 319 *[!0-9A-Fa-f]*) 320 # Segment contains at least one invalid char 321 322 # Return error immediately if not last segment 323 [ $# -eq 0 ] || return 1 324 325 # Otherwise, check for legacy IPv4 notation 326 case "$segment" in 327 *[!0-9.]*) 328 # Segment contains at least one invalid 329 # character even for an IPv4 address 330 return 1 331 esac 332 333 # Return error if not enough segments 334 if [ $nulls -eq 0 ]; then 335 [ $nsegments -eq 7 ] || return 4 336 fi 337 338 contains_ipv4_segment=1 339 340 # Validate the IPv4 address 341 f_validate_ipaddr "$segment" || 342 return $(( 5 | $? << 4 )) 343 ;; 344 *) 345 # Segment characters are all valid but too many 346 return 3 347 esac 348 349 done 350 351 if [ $nulls -eq 1 ]; then 352 # Single null segment cannot be at beginning/end 353 case "$ip" in 354 :*|*:) return 2 355 esac 356 fi 357 358 # 359 # A legacy IPv4 address can span the last two 16-bit segments, 360 # reducing the amount of maximum allowable segments by-one. 361 # 362 maxsegments=8 363 if [ "$contains_ipv4_segment" ]; then 364 maxsegments=7 365 fi 366 367 case $nulls in 368 # Return error if missing segments with no null spanner 369 0) [ $nsegments -eq $maxsegments ] || return 4 ;; 370 # Return error if null spanner with too many segments 371 1) [ $nsegments -le $maxsegments ] || return 4 ;; 372 # Return error if leading/trailing `::' with too many segments 373 2) [ $nsegments -le $(( $maxsegments + 1 )) ] || return 4 ;; 374 esac 375 376 return $SUCCESS 377} 378 379# f_validate_netmask $netmask 380# 381# Returns zero if the given argument (a subnet mask) is of the proper format. 382# 383# The return status for invalid netmask is one of: 384# 1 One or more individual fields within the subnet mask (separated 385# by dots) contains one or more invalid characters. 386# 2 One or more individual fields within the subnet mask are null 387# and/or missing. 388# 3 One or more individual fields within the subnet mask exceeds 389# the maximum of 255 (a full 8-bit register). 390# 4 The subnet mask has either too few or too many fields. 391# 5 One or more individual fields within the subnet mask is an 392# invalid integer (only 0,128,192,224,240,248,252,254,255 are 393# valid integers). 394# 395f_validate_netmask() 396{ 397 local mask="$1" 398 399 # Track number of fields for error checking 400 local nfields=0 401 402 local IFS="." # Split on `dot' 403 for field in $mask; do 404 # Return error if the field is null 405 [ "$field" ] || return 2 406 407 # Return error if not a whole positive integer 408 f_isinteger "$field" || return 1 409 410 # Return error if the field exceeds 255 411 [ $field -gt 255 ] && return 3 412 413 # Return error if the field is an invalid integer 414 case "$field" in 415 0|128|192|224|240|248|252|254|255) : ;; 416 *) return 5 ;; 417 esac 418 419 nfields=$(( $nfields + 1 )) 420 done 421 422 [ $nfields -eq 4 ] || return 4 423} 424 425# f_validate_gateway $gateway $ipaddr $netmask 426# 427# Validate an IPv4 default gateway (aka router) address for a given IP address 428# making sure the two are in the same network (able to ``talk'' to each other). 429# Returns success if $ipaddr and $gateway are in the same network given subnet 430# mask $netmask. 431# 432f_validate_gateway() 433{ 434 local gateway="$1" ipaddr="$2" netmask="$3" 435 local gwnum ipnum masknum 436 437 f_validate_ipaddr "$gateway" "$netmask" || return $FAILURE 438 439 f_inet_atoi "$netmask" masknum 440 f_inet_atoi "$ipaddr" ipnum 441 f_inet_atoi "$gateway" gwnum 442 443 # Gateway must be within set of IPs reachable through interface 444 [ $(( $ipnum & $masknum )) -eq \ 445 $(( $gwnum & $masknum )) ] # Return status 446} 447 448# f_dialog_validate_tcpip $hostname $gateway $nameserver $ipaddr $netmask 449# 450# Returns success if the arguments provided are valid for accessing a TCP/IP 451# network, otherwise returns failure. 452# 453f_dialog_validate_tcpip() 454{ 455 local hostname="$1" gateway="$2" nameserver="$3" 456 local ipaddr="$4" netmask="$5" 457 local ipnum masknum 458 459 if [ ! "$hostname" ]; then 460 f_show_msg "$msg_must_specify_a_host_name_of_some_sort" 461 elif ! f_validate_hostname "$hostname"; then 462 f_show_msg "$msg_invalid_hostname_value" 463 elif [ "$netmask" ] && ! f_validate_netmask "$netmask"; then 464 f_show_msg "$msg_invalid_netmask_value" 465 elif [ "$nameserver" ] && 466 ! f_validate_ipaddr "$nameserver" && 467 ! f_validate_ipaddr6 "$nameserver"; then 468 f_show_msg "$msg_invalid_name_server_ip_address_specified" 469 elif [ "$ipaddr" ] && ! f_validate_ipaddr "$ipaddr" "$netmask"; then 470 f_show_msg "$msg_invalid_ipv4_address" 471 elif [ "$gateway" -a "$gateway" != "NO" ] && 472 ! f_validate_gateway "$gateway" "$ipaddr" "$netmask"; then 473 f_show_msg "$msg_invalid_gateway_ipv4_address_specified" 474 else 475 return $DIALOG_OK 476 fi 477 478 return $DIALOG_CANCEL 479} 480 481# f_ifconfig_inet $interface [$var_to_set] 482# 483# Returns the IPv4 address associated with $interface. If $var_to_set is 484# missing or NULL, the IP address is printed to standard output for capturing 485# in a sub-shell (which is less-recommended because of performance degredation; 486# for example, when called in a loop). 487# 488# This function is a two-parter. Below is the awk(1) portion of the function, 489# afterward is the sh(1) function which utilizes the below awk script. 490# 491f_ifconfig_inet_awk=' 492BEGIN { found = 0 } 493( $1 == "inet" ) \ 494{ 495 print $2 496 found = 1 497 exit 498} 499END { exit ! found } 500' 501f_ifconfig_inet() 502{ 503 local __interface="$1" __var_to_set="$2" 504 if [ "$__var_to_set" ]; then 505 local __ip 506 __ip=$( ifconfig "$__interface" 2> /dev/null | 507 awk "$f_ifconfig_inet_awk" ) 508 setvar "$__var_to_set" "$__ip" 509 else 510 ifconfig "$__interface" 2> /dev/null | 511 awk "$f_ifconfig_inet_awk" 512 fi 513} 514 515# f_ifconfig_inet6 $interface [$var_to_set] 516# 517# Returns the IPv6 address associated with $interface. If $var_to_set is 518# missing or NULL, the IP address is printed to standard output for capturing 519# in a sub-shell (which is less-recommended because of performance degredation; 520# for example, when called in a loop). 521# 522# This function is a two-parter. Below is the awk(1) portion of the function, 523# afterward is the sh(1) function which utilizes the below awk script. 524# 525f_ifconfig_inet6_awk=' 526BEGIN { found = 0 } 527( $1 == "inet6" ) \ 528{ 529 print $2 530 found = 1 531 exit 532} 533END { exit ! found } 534' 535f_ifconfig_inet6() 536{ 537 local __interface="$1" __var_to_set="$2" 538 if [ "$__var_to_set" ]; then 539 local __ip6 540 __ip6=$( ifconfig "$__interface" 2> /dev/null | 541 awk "$f_ifconfig_inet6_awk" ) 542 setvar "$__var_to_set" "$__ip6" 543 else 544 ifconfig "$__interface" 2> /dev/null | 545 awk "$f_ifconfig_inet6_awk" 546 fi 547} 548 549# f_ifconfig_netmask $interface [$var_to_set] 550# 551# Returns the IPv4 subnet mask associated with $interface. If $var_to_set is 552# missing or NULL, the netmask is printed to standard output for capturing in a 553# sub-shell (which is less-recommended because of performance degredation; for 554# example, when called in a loop). 555# 556f_ifconfig_netmask() 557{ 558 local __interface="$1" __var_to_set="$2" __octets 559 __octets=$( ifconfig "$__interface" 2> /dev/null | awk \ 560 ' 561 BEGIN { found = 0 } 562 ( $1 == "inet" ) \ 563 { 564 printf "%s %s %s %s\n", 565 substr($4,3,2), 566 substr($4,5,2), 567 substr($4,7,2), 568 substr($4,9,2) 569 found = 1 570 exit 571 } 572 END { exit ! found } 573 ' ) || return $FAILURE 574 575 local __octet __netmask= 576 for __octet in $__octets; do 577 f_sprintf __netmask "%s.%u" "$__netmask" "0x$__octet" 578 done 579 __netmask="${__netmask#.}" 580 if [ "$__var_to_set" ]; then 581 setvar "$__var_to_set" "$__netmask" 582 else 583 echo $__netmask 584 fi 585} 586 587# f_route_get_default [$var_to_set] 588# 589# Returns the IP address of the currently active default router. If $var_to_set 590# is missing or NULL, the IP address is printed to standard output for 591# capturing in a sub-shell (which is less-recommended because of performance 592# degredation; for example, when called in a loop). 593# 594# This function is a two-parter. Below is the awk(1) portion of the function, 595# afterward is the sh(1) function which utilizes the below awk script. 596# 597f_route_get_default_awk=' 598BEGIN { found = 0 } 599( $1 == "gateway:" ) \ 600{ 601 print $2 602 found = 1 603 exit 604} 605END { exit ! found } 606' 607f_route_get_default() 608{ 609 local __var_to_set="$1" 610 if [ "$__var_to_set" ]; then 611 local __ip 612 __ip=$( route -n get default 2> /dev/null | 613 awk "$f_route_get_default_awk" ) 614 setvar "$__var_to_set" "$__ip" 615 else 616 route -n get default 2> /dev/null | 617 awk "$f_route_get_default_awk" 618 fi 619} 620 621# f_resolv_conf_nameservers [$var_to_set] 622# 623# Returns nameserver(s) configured in resolv.conf(5). If $var_to_set is missing 624# or NULL, the list of nameservers is printed to standard output for capturing 625# in a sub-shell (which is less-recommended because of performance degredation; 626# for example, when called in a loop). 627# 628# This function is a two-parter. Below is the awk(1) portion of the function, 629# afterward is the sh(1) function which utilizes the below awk script. 630# 631f_resolv_conf_nameservers_awk=' 632BEGIN { found = 0 } 633( $1 == "nameserver" ) \ 634{ 635 print $2 636 found = 1 637} 638END { exit ! found } 639' 640f_resolv_conf_nameservers() 641{ 642 local __var_to_set="$1" 643 if [ "$__var_to_set" ]; then 644 local __ns 645 __ns=$( awk "$f_resolv_conf_nameservers_awk" "$RESOLV_CONF" \ 646 2> /dev/null ) 647 setvar "$__var_to_set" "$__ns" 648 else 649 awk "$f_resolv_conf_nameservers_awk" "$RESOLV_CONF" \ 650 2> /dev/null 651 fi 652} 653 654# f_config_resolv 655# 656# Attempts to configure resolv.conf(5) and ilk. Returns success if able to 657# write the file(s), otherwise returns error status. 658# 659# Variables from variable.subr that are used in configuring resolv.conf(5) are 660# as follows (all of which can be configured automatically through functions 661# like f_dhcp_get_info() or manually): 662# 663# VAR_NAMESERVER 664# The nameserver to add in resolv.conf(5). 665# VAR_DOMAINNAME 666# The domain to configure in resolv.conf(5). Also used in the 667# configuration of hosts(5). 668# VAR_IPADDR 669# The IPv4 address to configure in hosts(5). 670# VAR_IPV6ADDR 671# The IPv6 address to configure in hosts(5). 672# VAR_HOSTNAME 673# The hostname to associate with the IPv4 and/or IPv6 address in 674# hosts(5). 675# 676f_config_resolv() 677{ 678 local cp c6p dp hp 679 680 f_getvar $VAR_NAMESERVER cp 681 if [ "$cp" ]; then 682 case "$RESOLV_CONF" in 683 */*) f_quietly mkdir -p "${RESOLV_CONF%/*}" ;; 684 esac 685 686 # Attempt to create/truncate the file 687 ( :> "$RESOLV_CONF" ) 2> /dev/null || return $FAILURE 688 689 f_getvar $VAR_DOMAINNAME dp && 690 printf "domain\t%s\n" "$dp" >> "$RESOLV_CONF" 691 printf "nameserver\t%s\n" "$cp" >> "$RESOLV_CONF" 692 693 f_dprintf "Wrote out %s" "$RESOLV_CONF" 694 fi 695 696 f_getvar $VAR_DOMAINNAME dp 697 f_getvar $VAR_IPADDR cp 698 f_getvar $VAR_IPV6ADDR c6p 699 f_getvar $VAR_HOSTNAME hp 700 701 # Attempt to create the file if it doesn't already exist 702 if [ ! -e "$ETC_HOSTS" ]; then 703 case "$ETC_HOSTS" in 704 */*) f_quietly mkdir -p "${ETC_HOSTS%/*}" ;; 705 esac 706 707 ( :> "$ETC_HOSTS" ) 2> /dev/null || return $FAILURE 708 fi 709 710 # Scan the file and add ourselves if not already configured 711 awk -v dn="$dp" -v ip4="$cp" -v ip6="$c6p" -v hn="$hp" ' 712 BEGIN { 713 local4found = local6found = 0 714 hn4found = hn6found = h4found = h6found = 0 715 h = ( match(hn, /\./) ? substr(hn, 0, RSTART-1) : "" ) 716 } 717 ($1 == "127.0.0.1") { local4found = 1 } 718 ($1 == "::1") { local6found = 1 } 719 { 720 for (n = 2; n <= NF; n++) 721 { 722 if ( $1 == ip4 ) { 723 if ( $n == h ) h4found = 1 724 if ( $n == hn ) hn4found = 1 725 if ( $n == hn "." ) hn4found = 1 726 } 727 if ( $1 == ip6 ) { 728 if ( $n == h ) h6found = 1 729 if ( $n == hn ) hn6found = 1 730 if ( $n == hn "." ) hn6found = 1 731 } 732 } 733 } 734 END { 735 hosts = FILENAME 736 737 if ( ! local6found ) 738 printf "::1\t\t\tlocalhost%s\n", 739 ( dn ? " localhost." dn : "" ) >> hosts 740 if ( ! local4found ) 741 printf "127.0.0.1\t\tlocalhost%s\n", 742 ( dn ? " localhost." dn : "" ) >> hosts 743 744 if ( ip6 && ! (h6found && hn6found)) 745 { 746 printf "%s\t%s %s\n", ip6, hn, h >> hosts 747 printf "%s\t%s.\n", ip6, hn >> hosts 748 } 749 else if ( ip6 ) 750 { 751 if ( ! h6found ) 752 printf "%s\t%s.\n", ip6, h >> hosts 753 if ( ! hn6found ) 754 printf "%s\t%s\n", ip6, hn >> hosts 755 } 756 757 if ( ip4 && ! (h4found && hn4found)) 758 { 759 printf "%s\t\t%s %s\n", ip4, hn, h >> hosts 760 printf "%s\t\t%s.\n", ip4, hn >> hosts 761 } 762 else if ( ip4 ) 763 { 764 if ( ! h4found ) 765 printf "%s\t\t%s.\n", ip4, h >> hosts 766 if ( ! hn4found ) 767 printf "%s\t\t%s\n", ip4, hn >> hosts 768 } 769 } 770 ' "$ETC_HOSTS" 2> /dev/null || return $FAILURE 771 772 f_dprintf "Wrote out %s" "$ETC_HOSTS" 773 return $SUCCESS 774} 775 776# f_dhcp_parse_leases $leasefile struct_name 777# 778# Parse $leasefile and store the information for the most recent lease in a 779# struct (see struct.subr for additional details) named `struct_name'. See 780# DHCP_LEASE struct definition in the GLOBALS section above. 781# 782f_dhcp_parse_leases() 783{ 784 local leasefile="$1" struct_name="$2" 785 786 [ "$struct_name" ] || return $FAILURE 787 788 if [ ! -e "$leasefile" ]; then 789 f_dprintf "%s: No such file or directory" "$leasefile" 790 return $FAILURE 791 fi 792 793 f_struct "$struct_name" && f_struct_free "$struct_name" 794 f_struct_new DHCP_LEASE "$struct_name" 795 796 eval "$( awk -v struct="$struct_name" ' 797 BEGIN { 798 lease_found = 0 799 keyword_list = " \ 800 interface \ 801 fixed-address \ 802 filename \ 803 server-name \ 804 script \ 805 medium \ 806 " 807 split(keyword_list, keywords, FS) 808 809 time_list = "renew rebind expire" 810 split(time_list, times, FS) 811 812 option_list = " \ 813 host-name \ 814 subnet-mask \ 815 routers \ 816 domain-name-servers \ 817 domain-name \ 818 broadcast-address \ 819 dhcp-lease-time \ 820 dhcp-message-type \ 821 dhcp-server-identifier \ 822 dhcp-renewal-time \ 823 dhcp-rebinding-time \ 824 " 825 split(option_list, options, FS) 826 } 827 function set_value(prop,value) 828 { 829 lease_found = 1 830 gsub(/[^[:alnum:]_]/, "_", prop) 831 sub(/;$/, "", value) 832 sub(/^"/, "", value) 833 sub(/"$/, "", value) 834 sub(/,.*/, "", value) 835 printf "%s set %s \"%s\"\n", struct, prop, value 836 } 837 /^lease {$/, /^}$/ \ 838 { 839 if ( $0 ~ /^lease {$/ ) next 840 if ( $0 ~ /^}$/ ) exit 841 842 for (k in keywords) 843 { 844 keyword = keywords[k] 845 if ( $1 == keyword ) 846 { 847 set_value(keyword, $2) 848 next 849 } 850 } 851 852 for (t in times) 853 { 854 time = times[t] 855 if ( $1 == time ) 856 { 857 set_value(time, $2 " " $3 " " $4) 858 next 859 } 860 } 861 862 if ( $1 != "option" ) next 863 for (o in options) 864 { 865 option = options[o] 866 if ( $2 == option ) 867 { 868 set_value(option, $3) 869 next 870 } 871 } 872 } 873 EXIT { 874 if ( ! lease_found ) 875 { 876 printf "f_struct_free \"%s\"\n", struct 877 print "return $FAILURE" 878 } 879 } 880 ' "$leasefile" )" 881} 882 883# f_dhcp_get_info $interface 884# 885# Parse the dhclient(8) lease database for $interface to obtain all the 886# necessary IPv4 details necessary to communicate on the network. The retrieved 887# information is stored in VAR_IPADDR, VAR_NETMASK, VAR_GATEWAY, and 888# VAR_NAMESERVER. 889# 890# If reading the lease database fails, values are obtained from ifconfig(8) and 891# route(8). If the DHCP lease did not provide a nameserver (or likewise, we 892# were unable to parse the lease database), fall-back to resolv.conf(5) for 893# obtaining the nameserver. Always returns success. 894# 895f_dhcp_get_info() 896{ 897 local interface="$1" cp 898 local leasefile="/var/db/dhclient.leases.$interface" 899 900 # If it fails, do it the old-fashioned way 901 if f_dhcp_parse_leases "$leasefile" lease; then 902 lease get fixed_address $VAR_IPADDR 903 lease get subnet_mask $VAR_NETMASK 904 lease get routers cp 905 setvar $VAR_GATEWAY "${cp%%,*}" 906 lease get domain_name_servers cp 907 setvar $VAR_NAMESERVER "${cp%%,*}" 908 lease get host_name cp && 909 setvar $VAR_HOSTNAME "$cp" 910 f_struct_free lease 911 else 912 # Bah, now we have to get the information from ifconfig 913 if f_debugging; then 914 f_dprintf "DHCP configured interface returns %s" \ 915 "$( ifconfig "$interface" )" 916 fi 917 f_ifconfig_inet "$interface" $VAR_IPADDR 918 f_ifconfig_netmask "$interface" $VAR_NETMASK 919 f_route_get_default $VAR_GATEWAY 920 fi 921 922 # If we didn't get a name server value, hunt for it in resolv.conf 923 local ns 924 if [ -r "$RESOLV_CONF" ] && ! { 925 f_getvar $VAR_NAMESERVER ns || [ "$ns" ] 926 }; then 927 f_resolv_conf_nameservers cp && 928 setvar $VAR_NAMESERVER ${cp%%[$IFS]*} 929 fi 930 931 return $SUCCESS 932} 933 934# f_rtsol_get_info $interface 935# 936# Returns the rtsol-provided IPv6 address associated with $interface. The 937# retrieved IP address is stored in VAR_IPV6ADDR. Always returns success. 938# 939f_rtsol_get_info() 940{ 941 local interface="$1" cp 942 cp=$( ifconfig "$interface" 2> /dev/null | awk \ 943 ' 944 BEGIN { found = 0 } 945 ( $1 == "inet6" ) && ( $2 ~ /^fe80:/ ) \ 946 { 947 print $2 948 found = 1 949 exit 950 } 951 END { exit ! found } 952 ' ) && setvar $VAR_IPV6ADDR "$cp" 953} 954 955# f_host_lookup $host [$var_to_set] 956# 957# Use host(1) to lookup (or reverse) an Internet number from (or to) a name. 958# Multiple answers are returned separated by a single space. If host(1) does 959# not exit cleanly, its full output is provided and the return status is 1. 960# 961# If nsswitch.conf(5) has been configured to query local access first for the 962# `hosts' database, we'll manually check hosts(5) first (preventing host(1) 963# from hanging in the event that DNS goes awry). 964# 965# If $var_to_set is missing or NULL, the list of IP addresses is printed to 966# standard output for capturing in a sub-shell (which is less-recommended 967# because of performance degredation; for example, when called in a loop). 968# 969# The variables from variable.subr used in looking up the host are as follows 970# (which are set manually): 971# 972# VAR_IPV6_ENABLE [Optional] 973# If set to "YES", enables the lookup of IPv6 addresses and IPv4 974# address. IPv6 addresses, if any, will come before IPv4. Note 975# that if nsswitch.conf(5) shows an affinity for "files" for the 976# "host" database and there is a valid entry in hosts(5) for 977# $host, this setting currently has no effect (an IPv4 address 978# can supersede an IPv6 address). By design, hosts(5) overrides 979# any preferential treatment. Otherwise, if this variable is not 980# set, IPv6 addresses will not be used (IPv4 addresses will 981# specifically be requested from DNS). 982# 983# This function is a two-parter. Below is the awk(1) portion of the function, 984# afterward is the sh(1) function which utilizes the below awk script. 985# 986f_host_lookup_awk=' 987BEGIN{ addrs = "" } 988!/^[[:space:]]*(#|$)/ \ 989{ 990 for (n=1; n++ < NF;) if ($n == name) 991 addrs = addrs (addrs ? " " : "") $1 992} 993END { 994 if (addrs) print addrs 995 exit !addrs 996} 997' 998f_host_lookup() 999{ 1000 local __host="$1" __var_to_set="$2" 1001 f_dprintf "f_host_lookup: host=[%s]" "$__host" 1002 1003 # If we're configured to look at local files first, do that 1004 if awk '/^hosts:/{exit !($2=="files")}' "$NSSWITCH_CONF"; then 1005 if [ "$__var_to_set" ]; then 1006 local __cp 1007 if __cp=$( awk -v name="$__host" \ 1008 "$f_host_lookup_awk" "$ETC_HOSTS" ) 1009 then 1010 setvar "$__var_to_set" "$__cp" 1011 return $SUCCESS 1012 fi 1013 else 1014 awk -v name="$__host" \ 1015 "$f_host_lookup_awk" "$ETC_HOSTS" && 1016 return $SUCCESS 1017 fi 1018 fi 1019 1020 # 1021 # Fall back to host(1) -- which is further governed by nsswitch.conf(5) 1022 # 1023 1024 local __output __ip6 __addrs= 1025 f_getvar $VAR_IPV6_ENABLE __ip6 1026 1027 # If we have a TCP media type configured, check for an SRV record 1028 local __srvtypes= 1029 { f_quietly f_getvar $VAR_HTTP_PATH || 1030 f_quietly f_getvar $VAR_HTTP_PROXY_PATH 1031 } && __srvtypes="$__srvtypes _http._tcp" 1032 f_quietly f_getvar $VAR_FTP_PATH && __srvtypes="$__srvtypes _ftp._tcp" 1033 f_quietly f_getvar $VAR_NFS_PATH && 1034 __srvtypes="$__srvtypes _nfs._tcp _nfs._udp" 1035 1036 # Calculate wait time as dividend of total time and host(1) invocations 1037 local __host_runs __wait 1038 f_count __host_runs $__srvtypes 1039 if [ "$__ip6" = "YES" ]; then 1040 __host_runs=$(( $__host_runs + 2 )) 1041 else 1042 __host_runs=$(( $__host_runs + 1 )) 1043 fi 1044 f_getvar $VAR_MEDIA_TIMEOUT __wait 1045 [ "$__wait" ] && __wait="-W $(( $__wait / $__host_runs ))" 1046 1047 # Query SRV types first (1st host response taken as new host to query) 1048 for __type in $__srvtypes; do 1049 if __output=$( 1050 host -t SRV $__wait -- "$__type.$__host" \ 1051 2> /dev/null 1052 ); then 1053 __host=$( echo "$__output" | 1054 awk '/ SRV /{print $NF;exit}' ) 1055 break 1056 fi 1057 done 1058 1059 # Try IPv6 first (if enabled) 1060 if [ "$__ip6" = "YES" ]; then 1061 if ! __output=$( host -t AAAA $__wait -- "$__host" 2>&1 ); then 1062 # An error occurred, display in-full and return error 1063 [ "$__var_to_set" ] && 1064 setvar "$__var_to_set" "$__output" 1065 return $FAILURE 1066 fi 1067 # Add the IPv6 addresses and fall-through to collect IPv4 too 1068 __addrs=$( echo "$__output" | awk '/ address /{print $NF}' ) 1069 fi 1070 1071 # Good ol' IPv4 1072 if ! __output=$( host -t A $__wait -- "$__host" 2>&1 ); then 1073 # An error occurred, display it in-full and return error 1074 [ "$__var_to_set" ] && setvar "$__var_to_set" "$__output" 1075 return $FAILURE 1076 fi 1077 1078 __addrs="$__addrs${__addrs:+ }$( 1079 echo "$__output" | awk '/ address /{print $NF}' )" 1080 if [ "$__var_to_set" ]; then 1081 setvar "$__var_to_set" "$__addrs" 1082 else 1083 echo $__addrs 1084 fi 1085} 1086 1087# f_device_dialog_tcp $device 1088# 1089# This is it - how to get TCP setup values. Prompt the user to edit/confirm the 1090# interface, gateway, nameserver, and hostname settings -- all required for 1091# general TCP/IP access. 1092# 1093# Variables from variable.subr that can be used to sript user input: 1094# 1095# VAR_NO_INET6 1096# If set, prevents asking the user if they would like to use 1097# rtsol(8) to check for an IPv6 router. 1098# VAR_TRY_RTSOL 1099# If set to "YES" (and VAR_NONINTERACTIVE is unset), asks the 1100# user if they would like to try the IPv6 RouTer SOLicitation 1101# utility (rtsol(8)) to get IPv6 information. Ignored if 1102# VAR_NO_INET6 is set. 1103# VAR_TRY_DHCP 1104# If set to "YES" (and VAR_NONINTERACTIVE is unset), asks the 1105# user if they would like to try to acquire IPv4 connection 1106# settings from a DHCP server using dhclient(8). 1107# 1108# VAR_GATEWAY Default gateway to use. 1109# VAR_IPADDR Interface address to assign. 1110# VAR_NETMASK Interface subnet mask. 1111# VAR_EXTRAS Extra interface options to ifconfig(8). 1112# VAR_HOSTNAME Hostname to set. 1113# VAR_DOMAINNAME Domain name to use. 1114# VAR_NAMESERVER DNS nameserver to use when making lookups. 1115# VAR_IPV6ADDR IPv6 interface address. 1116# 1117# In addition, the following variables are used in acquiring network settings 1118# from the user: 1119# 1120# VAR_NONINTERACTIVE 1121# If set (such as when running in a script), prevents asking the 1122# user questions or displaying the usual prompts, etc. 1123# VAR_NETINTERACTIVE 1124# The one exception to VAR_NONINTERACTIVE is VAR_NETINTERACTIVE, 1125# which if set will prompt the user to try RTSOL (unless 1126# VAR_TRY_RTSOL has been set), try DHCP (unless VAR_TRY_DHCP has 1127# been set), and display the network verification dialog. This 1128# allows you to have a mostly non-interactive script that still 1129# prompts for network setup/confirmation. 1130# 1131# After successful execution, the following variables are set: 1132# 1133# VAR_IFCONFIG + $device (e.g., `ifconfig_em0') 1134# Defines the ifconfig(8) properties specific to $device. 1135# 1136f_device_dialog_tcp() 1137{ 1138 local dev="$1" devname cp n 1139 local use_dhcp="" use_rtsol="" 1140 local _ipaddr _netmask _extras 1141 1142 [ "$dev" ] || return $DIALOG_CANCEL 1143 f_struct "$dev" get name devname || return $DIALOG_CANCEL 1144 1145 # Initialize vars from previous device values 1146 local private 1147 $dev get private private 1148 if [ "$private" ] && f_struct "$private"; then 1149 $private get ipaddr _ipaddr 1150 $private get netmask _netmask 1151 $private get extras _extras 1152 $private get use_dhcp use_dhcp 1153 $private get use_rtsol use_rtsol 1154 else # See if there are any defaults 1155 1156 # 1157 # This is a hack so that the dialogs below are interactive in a 1158 # script if we have requested interactive behavior. 1159 # 1160 local old_interactive= 1161 if ! f_interactive && f_netinteractive; then 1162 f_getvar $VAR_NONINTERACTIVE old_interactive 1163 unset $VAR_NONINTERACTIVE 1164 fi 1165 1166 # 1167 # Try a RTSOL scan if such behavior is desired. 1168 # If the variable was configured and is YES, do it. 1169 # If it was configured to anything else, treat it as NO. 1170 # Otherwise, ask the question interactively. 1171 # 1172 local try6 1173 if ! f_isset $VAR_NO_INET6 && { 1174 { f_getvar $VAR_TRY_RTSOL try6 && [ "$try6" = "YES" ]; } || 1175 { 1176 # Only prompt the user when VAR_TRY_RTSOL is unset 1177 ! f_isset $VAR_TRY_RTSOL && 1178 f_dialog_noyes "$msg_try_ipv6_configuration" 1179 } 1180 }; then 1181 local i 1182 1183 f_quietly sysctl net.inet6.ip6.forwarding=0 1184 f_quietly sysctl net.inet6.ip6.accept_rtadv=1 1185 f_quietly ifconfig $devname up 1186 1187 i=$( sysctl -n net.inet6.ip6.dad_count ) 1188 sleep $(( $i + 1 )) 1189 1190 f_quietly mkdir -p /var/run 1191 f_dialog_info "$msg_scanning_for_ra_servers" 1192 if f_quietly rtsol $devname; then 1193 i=$( sysctl -n net.inet6.ip6.dad_count ) 1194 sleep $(( $i + 1 )) 1195 f_rtsol_get_info $devname 1196 use_rtsol=1 1197 else 1198 use_rtsol= 1199 fi 1200 fi 1201 1202 # 1203 # Try a DHCP scan if such behavior is desired. 1204 # If the variable was configured and is YES, do it. 1205 # If it was configured to anything else, treat it as NO. 1206 # Otherwise, ask the question interactively. 1207 # 1208 local try4 1209 if { f_getvar $VAR_TRY_DHCP try4 && [ "$try4" = "YES" ]; } || { 1210 # Only prompt the user when VAR_TRY_DHCP is unset 1211 ! f_isset $VAR_TRY_DHCP && 1212 f_dialog_noyes "$msg_try_dhcp_configuration" 1213 }; then 1214 f_quietly ifconfig $devname delete 1215 f_quietly mkdir -p /var/db 1216 f_quietly mkdir -p /var/run 1217 f_quietly mkdir -p /tmp 1218 1219 local msg="$msg_scanning_for_dhcp_servers" 1220 trap - SIGINT 1221 ( # Execute in sub-shell to allow/catch Ctrl-C 1222 trap 'exit $FAILURE' SIGINT 1223 if [ "$USE_XDIALOG" ]; then 1224 f_quietly dhclient $devname | 1225 f_xdialog_info "$msg" 1226 else 1227 f_dialog_info "$msg" 1228 f_quietly dhclient $devname 1229 fi 1230 ) 1231 local retval=$? 1232 trap 'f_interrupt' SIGINT 1233 if [ $retval -eq $SUCCESS ]; then 1234 f_dhcp_get_info $devname 1235 use_dhcp=1 1236 else 1237 use_dhcp= 1238 fi 1239 fi 1240 1241 # Restore old VAR_NONINTERACTIVE if needed. 1242 [ "$old_interactive" ] && 1243 setvar $VAR_NONINTERACTIVE "$old_interactive" 1244 1245 # Special hack so it doesn't show up oddly in the menu 1246 local gw 1247 if f_getvar $VAR_GATEWAY gw && [ "$gw" = "NO" ]; then 1248 setvar $VAR_GATEWAY "" 1249 fi 1250 1251 # Get old IP address from variable space, if available 1252 if [ ! "$_ipaddr" ]; then 1253 if f_getvar $VAR_IPADDR cp; then 1254 _ipaddr="$cp" 1255 elif f_getvar ${devname}_$VAR_IPADDR cp; then 1256 _ipaddr="$cp" 1257 fi 1258 fi 1259 1260 # Get old netmask from variable space, if available 1261 if [ ! "$_netmask" ]; then 1262 if f_getvar $VAR_NETMASK cp; then 1263 _netmask="$cp" 1264 elif f_getvar ${devname}_$VAR_NETMASK cp; then 1265 _netmask="$cp" 1266 fi 1267 fi 1268 1269 # Get old extras string from variable space, if available 1270 if [ ! "$_extras" ]; then 1271 if f_getvar $VAR_EXTRAS cp; then 1272 _extras="$cp" 1273 elif f_getvar ${devname}_$VAR_EXTRAS cp; then 1274 _extras="$cp" 1275 fi 1276 fi 1277 fi 1278 1279 # Look up values already recorded with the system, or blank the string 1280 # variables ready to accept some new data 1281 local _hostname _gateway _nameserver 1282 f_getvar $VAR_HOSTNAME _hostname 1283 case "$_hostname" in 1284 *.*) : do nothing ;; # Already fully-qualified 1285 *) 1286 f_getvar $VAR_DOMAINNAME cp 1287 [ "$cp" ] && _hostname="$_hostname.$cp" 1288 esac 1289 f_getvar $VAR_GATEWAY _gateway 1290 f_getvar $VAR_NAMESERVER _nameserver 1291 1292 # Re-check variables for initial inheritance before heading into dialog 1293 [ "$_hostname" ] || _hostname="${HOSTNAME:-$( hostname )}" 1294 [ "$_gateway" ] || f_route_get_default _gateway 1295 [ ! "$_nameserver" ] && 1296 f_resolv_conf_nameservers cp && _nameserver=${cp%%[$IFS]*} 1297 [ "$_ipaddr" ] || f_ifconfig_inet $devname _ipaddr 1298 [ "$_netmask" ] || f_ifconfig_netmask $devname _netmask 1299 1300 # If non-interactive, jump over dialog section and into config section 1301 if f_netinteractive || f_interactive || [ ! "$_hostname" ] 1302 then 1303 [ ! "$_hostname" ] && f_interactive && 1304 f_show_msg "$msg_hostname_variable_not_set" 1305 1306 local title=" $msg_network_configuration " 1307 local hline="$hline_alnum_arrows_punc_tab_enter" 1308 local extras_help="$tcplayout_extras_help" 1309 1310 # Modify the help line for PLIP config 1311 [ "${devname#plip}" != "$devname" ] && 1312 extras_help="$tcplayout_extras_help_for_plip" 1313 1314 f_getvar $VAR_IPV6ADDR cp && [ "$cp" ] && 1315 title="$title($msg_ipv6_ready) " 1316 1317 if [ ! "$USE_XDIALOG" ]; then 1318 local prompt="$msg_dialog_mixedform_navigation_help" 1319 # Calculate center position for displaying device label 1320 local devlabel="$msg_configuration_for_interface" 1321 devlabel="$devlabel $devname" 1322 local width=54 1323 local n=$(( $width/2 - (${#devlabel} + 4)/2 - 2 )) 1324 1325 while :; do 1326 cp=$( $DIALOG \ 1327 --title "$title" \ 1328 --backtitle "$DIALOG_BACKTITLE" \ 1329 --hline "$hline" \ 1330 --item-help \ 1331 --ok-label "$msg_ok" \ 1332 --cancel-label "$msg_cancel" \ 1333 --help-button \ 1334 --help-label "$msg_help" \ 1335 --mixedform "$prompt" 16 $width 9 \ 1336 "$msg_host_name_including_domain:" 1 2 \ 1337 "$_hostname" 2 3 45 255 0 \ 1338 "$tcplayout_hostname_help" \ 1339 "$msg_ipv4_gateway:" 3 2 \ 1340 "$_gateway" 4 3 16 15 0 \ 1341 "$tcplayout_gateway_help" \ 1342 "$msg_name_server:" 3 31 \ 1343 "$_nameserver" 4 32 16 15 0 \ 1344 "$tcplayout_nameserver_help" \ 1345 "- $devlabel -" 5 $n "" 0 0 0 0 3 "" \ 1346 "$msg_ipv4_address:" 6 6 \ 1347 "$_ipaddr" 7 7 16 15 0 \ 1348 "$tcplayout_ipaddr_help" \ 1349 "$msg_netmask:" 6 31 \ 1350 "$_netmask" 7 32 16 15 0 \ 1351 "$tcplayout_netmask_help" \ 1352 "$msg_extra_options_to_ifconfig" 8 6 \ 1353 "$_extras" 9 7 41 2048 0 \ 1354 "$extras_help" \ 1355 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD ) 1356 1357 # --mixed-form always returns 0, we have to 1358 # use the returned data to determine button 1359 if [ ! "$cp" ]; then 1360 # User either chose "Cancel", pressed 1361 # ESC, or blanked every form field 1362 return $DIALOG_CANCEL 1363 else 1364 n=$( echo "$cp" | f_number_of_lines ) 1365 [ $n -eq 1 ] && case "$cp" in HELP*) 1366 # User chose "Help" 1367 f_show_help "$TCP_HELPFILE" 1368 continue 1369 esac 1370 fi 1371 1372 # Turn mixed-form results into env variables 1373 eval "$( echo "$cp" | awk ' 1374 BEGIN { 1375 n = 0 1376 field[++n] = "_hostname" 1377 field[++n] = "_gateway" 1378 field[++n] = "_nameserver" 1379 field[++n] = "_ipaddr" 1380 field[++n] = "_netmask" 1381 field[++n] = "_extras" 1382 nfields = n 1383 n = 0 1384 } 1385 { 1386 gsub(/'\''/, "'\'\\\\\'\''") 1387 sub(/[[:space:]]*$/, "") 1388 value[field[++n]] = $0 1389 } 1390 END { 1391 for ( n = 1; n <= nfields; n++ ) 1392 { 1393 printf "%s='\''%s'\'';\n", 1394 field[n], 1395 value[field[n]] 1396 } 1397 }' )" 1398 1399 f_dialog_validate_tcpip \ 1400 "$_hostname" \ 1401 "$_gateway" \ 1402 "$_nameserver" \ 1403 "$_ipaddr" \ 1404 "$_netmask" \ 1405 && break 1406 done 1407 else 1408 # Xdialog(1) does not support --mixed-form 1409 # Create a persistent menu instead 1410 1411 f_dialog_title "$msg_network_configuration" 1412 local prompt= 1413 1414 while :; do 1415 cp=$( $DIALOG \ 1416 --title "$DIALOG_TITLE" \ 1417 --backtitle "$DIALOG_BACKTITLE" \ 1418 --hline "$hline" \ 1419 --item-help \ 1420 --ok-label "$msg_ok" \ 1421 --cancel-label "$msg_cancel" \ 1422 --help "" \ 1423 --menu "$prompt" 21 60 8 \ 1424 "$msg_accept_continue" "" \ 1425 "$tcplayout_accept_cont_help" \ 1426 "$msg_host_name_including_domain:" \ 1427 "$_hostname" \ 1428 "$tcplayout_hostname_help" \ 1429 "$msg_ipv4_gateway:" "$_gateway" \ 1430 "$tcplayout_gateway_help" \ 1431 "$msg_name_server:" "$_nameserver" \ 1432 "$tcplayout_nameserver_help" \ 1433 "$msg_ipv4_address:" "$_ipaddr" \ 1434 "$tcplayout_ipaddr_help" \ 1435 "$msg_netmask:" "$_netmask" \ 1436 "$tcplayout_netmask_help" \ 1437 "$msg_extra_options_to_ifconfig" \ 1438 "$_extras" "$extras_help" \ 1439 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD 1440 ) 1441 local retval=$? 1442 f_dialog_data_sanitize cp 1443 f_dprintf "retval=%u mtag=[%s]" $retval "$cp" 1444 1445 if [ $retval -eq $DIALOG_HELP ]; then 1446 f_show_help "$TCP_HELPFILE" 1447 continue 1448 elif [ $retval -ne $DIALOG_OK ]; then 1449 f_dialog_title_restore 1450 return $DIALOG_CANCEL 1451 fi 1452 1453 case "$cp" in 1454 "$msg_accept_continue") 1455 f_dialog_validate_tcpip \ 1456 "$_hostname" \ 1457 "$_gateway" \ 1458 "$_nameserver" \ 1459 "$_ipaddr" \ 1460 "$_netmask" \ 1461 && break ;; 1462 "$msg_host_name_including_domain:") 1463 f_dialog_input cp "$cp" "$_hostname" \ 1464 && _hostname="$cp" ;; 1465 "$msg_ipv4_gateway:") 1466 f_dialog_input cp "$cp" "$_gateway" \ 1467 && _gateway="$cp" ;; 1468 "$msg_name_server:") 1469 f_dialog_input cp "$cp" "$_nameserver" \ 1470 && _nameserver="$cp" ;; 1471 "$msg_ipv4_address:") 1472 f_dialog_input cp "$cp" "$_ipaddr" \ 1473 && _ipaddr="$cp" ;; 1474 "$msg_netmask:") 1475 f_dialog_input cp "$cp" "$_netmask" \ 1476 && _netmask="$cp" ;; 1477 "$msg_extra_options_to_ifconfig") 1478 f_dialog_input cp "$cp" "$_extras" \ 1479 && _extras="$cp" ;; 1480 esac 1481 done 1482 1483 f_dialog_title_restore 1484 1485 fi # XDIALOG 1486 1487 fi # interactive 1488 1489 # We actually need to inform the rest of bsdconfig about this 1490 # data now if the user hasn't selected cancel. 1491 1492 if [ "$_hostname" ]; then 1493 setvar $VAR_HOSTNAME "$_hostname" 1494 f_quietly hostname "$_hostname" 1495 case "$_hostname" in 1496 *.*) setvar $VAR_DOMAINNAME "${_hostname#*.}" ;; 1497 esac 1498 fi 1499 [ "$_gateway" ] && setvar $VAR_GATEWAY "$_gateway" 1500 [ "$_nameserver" ] && setvar $VAR_NAMESERVER "$_nameserver" 1501 [ "$_ipaddr" ] && setvar $VAR_IPADDR "$_ipaddr" 1502 [ "$_netmask" ] && setvar $VAR_NETMASK "$_netmask" 1503 [ "$_extras" ] && setvar $VAR_EXTRAS "$_extras" 1504 1505 f_dprintf "Creating struct DEVICE_INFO devinfo_%s" "$dev" 1506 f_struct_new DEVICE_INFO devinfo_$dev 1507 $dev set private devinfo_$dev 1508 1509 devinfo_$dev set ipaddr $_ipaddr 1510 devinfo_$dev set netmask $_netmask 1511 devinfo_$dev set extras $_extras 1512 devinfo_$dev set use_rtsol $use_rtsol 1513 devinfo_$dev set use_dhcp $use_dhcp 1514 1515 if [ "$use_dhcp" -o "$_ipaddr" ]; then 1516 if [ "$use_dhcp" ]; then 1517 cp="DHCP${extras:+ $extras}" 1518 else 1519 cp="inet $_ipaddr netmask $_netmask${extras:+ $extras}" 1520 fi 1521 setvar $VAR_IFCONFIG$devname "$cp" 1522 fi 1523 [ "$use_rtsol" ] && 1524 setvar $VAR_IPV6_ENABLE "YES" 1525 1526 [ "$use_dhcp" ] || 1527 f_config_resolv # XXX this will do it on the MFS copy 1528 1529 return $DIALOG_OK 1530} 1531 1532# f_device_scan_tcp [$var_to_set] 1533# 1534# Scan for the first active/configured TCP/IP device. The name of the interface 1535# is printed to stderr like other dialog(1)-based functions (stdout is reserved 1536# for dialog(1) interaction) if $var_to_set is missing or NULL. Returns failure 1537# if no active/configured interface 1538# 1539f_device_scan_tcp() 1540{ 1541 local __var_to_set="$1" __iface 1542 for __iface in $( ifconfig -l ); do 1543 if ifconfig $__iface | awk ' 1544 BEGIN { 1545 has_inet = has_inet6 = is_ethernet = 0 1546 is_usable = 1 1547 } 1548 ( $1 == "status:" && $2 != "active" ) { is_usable = 0; exit } 1549 ( $1 == "inet" ) { 1550 if ($2 == "0.0.0.0") { is_usable = 0; exit } 1551 has_inet++ 1552 } 1553 ( $1 == "inet6") { has_inet6++ } 1554 ( $1 == "media:" ) { 1555 if ($2 != "Ethernet") { is_usable = 0; exit } 1556 is_ethernet = 1 1557 } 1558 END { 1559 if (!(is_ethernet && (has_inet || has_inet6))) 1560 is_usable = 0 1561 exit ! is_usable 1562 }'; then 1563 f_interactive && 1564 f_show_msg "$msg_using_interface" "$__iface" 1565 f_dprintf "f_device_scan_tcp found %s" "$__iface" 1566 if [ "$__var_to_set" ]; then 1567 setvar "$__var_to_set" "$__iface" 1568 else 1569 echo "$__iface" >&2 1570 fi 1571 return $SUCCESS 1572 fi 1573 done 1574 1575 return $FAILURE 1576} 1577 1578# f_device_select_tcp 1579# 1580# Prompt the user to select network interface to use for TCP/IP access. 1581# Variables from variable.subr that can be used to script user input: 1582# 1583# VAR_NETWORK_DEVICE [Optional] 1584# Either a comma-separated list of network interfaces to try when 1585# setting up network access (e.g., "fxp0,em0") or "ANY" (case- 1586# sensitive) to indicate that the first active and configured 1587# interface is acceptable. If unset, the user is presented with a 1588# menu of all available network interfaces. 1589# 1590# Returns success if a valid network interface has been selected. 1591# 1592f_device_select_tcp() 1593{ 1594 local devs dev cnt if network_dev 1595 f_getvar $VAR_NETWORK_DEVICE network_dev 1596 1597 f_dprintf "f_device_select_tcp: %s=[%s]" \ 1598 VAR_NETWORK_DEVICE "$network_dev" 1599 1600 if [ "$network_dev" ]; then 1601 # 1602 # This can be set to several types of values. If set to ANY, 1603 # scan all network devices looking for a valid link, and go 1604 # with the first device found. Can also be specified as a 1605 # comma delimited list, with each network device tried in 1606 # order. Can also be set to a single network device. 1607 # 1608 [ "$network_dev" = "ANY" ] && f_device_scan_tcp network_dev 1609 1610 while [ "$network_dev" ]; do 1611 case "$network_dev" in 1612 *,*) if="${network_dev%%,*}" 1613 network_dev="${network_dev#*,}" 1614 ;; 1615 *) if="$network_dev" 1616 network_dev= 1617 esac 1618 1619 f_device_find -1 "$if" $DEVICE_TYPE_NETWORK dev 1620 f_device_dialog_tcp $dev 1621 if [ $? -eq $DIALOG_OK ]; then 1622 setvar $VAR_NETWORK_DEVICE $if 1623 return $DIALOG_OK 1624 fi 1625 done 1626 1627 f_interactive && f_show_msg "$msg_no_network_devices" 1628 return $DIALOG_CANCEL 1629 1630 fi # $network_dev 1631 1632 f_device_find "" $DEVICE_TYPE_NETWORK devs 1633 f_count cnt $devs 1634 dev="${devs%%[$IFS]*}" 1635 $dev get name if 1636 1637 f_quietly f_getvar NETWORK_CONFIGURED # for debugging info 1638 if ! f_running_as_init && 1639 ! [ "${NETWORK_CONFIGURED+set}" -a "$NETWORK_CONFIGURED" = "NO" ] 1640 then 1641 trap 'f_interrupt' SIGINT 1642 if f_dialog_yesno "$msg_assume_network_is_already_configured" 1643 then 1644 setvar $VAR_NETWORK_DEVICE $if 1645 return $DIALOG_OK 1646 fi 1647 fi 1648 1649 local retval=$SUCCESS 1650 if [ ${cnt:=0} -eq 0 ]; then 1651 f_show_msg "$msg_no_network_devices" 1652 retval=$DIALOG_CANCEL 1653 elif [ $cnt -eq 1 ]; then 1654 f_device_dialog_tcp $dev 1655 retval=$? 1656 [ $retval -eq $DIALOG_OK ] && setvar $VAR_NETWORK_DEVICE $if 1657 else 1658 local title="$msg_network_interface_information_required" 1659 local prompt="$msg_please_select_ethernet_device_to_configure" 1660 local hline="$hline_arrows_tab_enter" 1661 1662 dev=$( f_device_menu \ 1663 "$title" "$prompt" "$hline" $DEVICE_TYPE_NETWORK \ 1664 "$NETWORK_DEVICE_HELPFILE" \ 1665 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD ) || 1666 return $DIALOG_CANCEL 1667 1668 f_device_dialog_tcp $dev 1669 retval=$? 1670 if [ $retval -eq $DIALOG_OK ]; then 1671 f_struct_copy "$dev" device_network 1672 setvar $VAR_NETWORK_DEVICE device_network 1673 else 1674 f_struct_free device_network 1675 fi 1676 fi 1677 1678 return $retval 1679} 1680 1681# f_dialog_menu_select_tcp 1682# 1683# Like f_dialog_select_tcp() above, but do it from a menu that doesn't care 1684# about status. In other words, where f_dialog_select_tcp() will not display a 1685# menu if scripted, this function will always display the menu of available 1686# network interfaces. 1687# 1688f_dialog_menu_select_tcp() 1689{ 1690 local private use_dhcp name 1691 NETWORK_CONFIGURED=NO f_device_select_tcp 1692 if f_struct device_network && 1693 device_network get private private && 1694 f_struct_copy "$private" di && 1695 di get use_dhcp use_dhcp && 1696 [ ! "$use_dhcp" ] && 1697 device_network get name name && 1698 f_yesno "$msg_would_you_like_to_bring_interface_up" "$name" 1699 then 1700 if ! f_device_init device_network; then 1701 f_show_msg "$msg_initialization_of_device_failed" \ 1702 "$name" 1703 fi 1704 fi 1705 return $DIALOG_OK 1706} 1707 1708############################################################ MAIN 1709 1710f_dprintf "%s: Successfully loaded." media/tcpip.subr 1711 1712fi # ! $_MEDIA_TCPIP_SUBR 1713