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