http.subr revision 253333
1if [ ! "$_MEDIA_HTTP_SUBR" ]; then _MEDIA_HTTP_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: head/usr.sbin/bsdconfig/share/media/http.subr 253333 2013-07-14 03:08:52Z dteske $ 28# 29############################################################ INCLUDES 30 31BSDCFG_SHARE="/usr/share/bsdconfig" 32. $BSDCFG_SHARE/common.subr || exit 1 33f_dprintf "%s: loading includes..." media/http.subr 34f_include $BSDCFG_SHARE/device.subr 35f_include $BSDCFG_SHARE/dialog.subr 36f_include $BSDCFG_SHARE/media/common.subr 37f_include $BSDCFG_SHARE/media/tcpip.subr 38f_include $BSDCFG_SHARE/strings.subr 39f_include $BSDCFG_SHARE/struct.subr 40f_include $BSDCFG_SHARE/variable.subr 41 42BSDCFG_LIBE="/usr/libexec/bsdconfig" 43f_include_lang $BSDCFG_LIBE/include/messages.subr 44 45############################################################ GLOBALS 46 47HTTP_SKIP_RESOLV= 48 49URL_MAX=261261 50 # NOTE: This is according to actual fetch(1) test-results. We actually 51 # use nc(1) to retrieve files, but it's still a good idea to keep the 52 # URLs short enough that fetch(1) won't complain. 53 54HTTP_DIRS=" 55 . 56 releases/$UNAME_P 57 snapshots/$UNAME_P 58 pub/FreeBSD 59 pub/FreeBSD/releases/$UNAME_P 60 pub/FreeBSD/snapshots/$UNAME_P 61 pub/FreeBSD-Archive/old-releases/$UNAME_P 62" # END-QUOTE 63 64############################################################ FUNCTIONS 65 66# f_dialog_menu_media_http 67# 68# Prompt the user to select from a range of ``built-in'' HTTP servers or 69# specify their own. If the user makes a choice and doesn't cancel or press 70# Esc, stores the user's choice in VAR_FTP_PATH (see variable.subr) and returns 71# success. 72# 73f_dialog_menu_media_http() 74{ 75 f_dialog_title "$msg_please_select_a_freebsd_http_distribution_site" 76 local title="$DIALOG_TITLE" btitle="$DIALOG_BACKTITLE" 77 f_dialog_title_restore 78 local prompt="$msg_please_select_the_site_closest_to_you_or_other" 79 local menu_list=" 80 'URL' '$msg_specify_some_other_http_site' 81 " # END-QUOTE 82 local hline="$msg_select_a_site_thats_close" 83 84 local height width rows 85 eval f_dialog_menu_size height width rows \ 86 \"\$title\" \ 87 \"\$btitle\" \ 88 \"\$prompt\" \ 89 \"\$hline\" \ 90 $menu_list 91 92 local mtag 93 mtag=$( eval $DIALOG \ 94 --title \"\$title\" \ 95 --backtitle \"\$btitle\" \ 96 --hline \"\$hline\" \ 97 --ok-label \"\$msg_ok\" \ 98 --cancel-label \"\$msg_cancel\" \ 99 --menu \"\$prompt\" \ 100 $height $width $rows \ 101 $menu_list \ 102 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD 103 ) || return $FAILURE 104 f_dialog_data_sanitize mtag 105 106 case "$mtag" in 107 URL) setvar $VAR_HTTP_PATH "other" ;; 108 *) 109 local value 110 value=$( eval f_dialog_menutag2item \"\$mtag\" $menu_list ) 111 setvar $VAR_HTTP_PATH "http://$value" 112 esac 113 114 return $SUCCESS 115} 116 117# f_media_set_http 118# 119# Return success if we both found and set the media type to be an HTTP server. 120# 121# Variables from variable.subr that can be used to script user input: 122# 123# VAR_HTTP_PATH 124# URL containing host and optionally a target path to the release 125# repository on the HTTP server. Valid examples include: 126# http://myhost 127# http://somename:80/pub/ 128# http://192.168.2.3/pub/ 129# http://[::1]:8000/ 130# The default port if not specified is 80. 131# VAR_NAMESERVER [Optional] 132# If set, overrides resolv.conf(5) and sets the nameserver that 133# is used to convert names into addresses (when a name converts 134# into multiple addresses, the first address to successfully 135# connect is used). 136# 137# Meanwhile, the following variables from variable.subr are set after 138# successful execution: 139# 140# VAR_HTTP_HOST 141# The HTTP host to connect to, parsed from VAR_HTTP_PATH. In the 142# example case of IPv6 where VAR_HTTP_PATH is "http://[::1]" this 143# variable will be set to "::1" (the outer brackets are removed). 144# VAR_HTTP_PORT 145# The TCP port to connect to, parsed from VAR_HTTP_PATH. Usually 146# 80 unless VAR_HTTP_PATH was one of the following forms: 147# http://hostname:OTHER_PORT 148# http://hostname:OTHER_PORT/* 149# http://ip:OTHER_PORT 150# http://ip:OTHER_PORT/* 151# http://[ip6]:OTHER_PORT 152# http://[ip6]:OTHER_PORT/* 153# VAR_HTTP_DIR 154# If VAR_HTTP_PATH contained a directory element (e.g., 155# "http://localhost/pub") this variable contains only the 156# directory element (e.g., "/pub"). 157# 158f_media_set_http() 159{ 160 f_media_close 161 162 local url 163 f_getvar $VAR_HTTP_PATH url 164 165 # If we've been through here before ... 166 if f_struct device_network && [ "${url#$msg_other}" ]; then 167 f_dialog_yesno "$msg_reuse_old_http_site_settings" || url= 168 fi 169 170 if [ ! "$url" ]; then 171 f_dialog_menu_media_http || return $FAILURE 172 f_getvar $VAR_HTTP_PATH url 173 fi 174 [ "$url" ] || return $FAILURE 175 176 case "$url" in 177 other) 178 setvar $VAR_HTTP_PATH "http://" 179 f_variable_get_value $VAR_HTTP_PATH \ 180 "$msg_please_specify_url_of_freebsd_http_distribution" 181 f_getvar $VAR_HTTP_PATH url 182 if [ ! "${url#http://}" ]; then 183 unset $VAR_HTTP_PATH 184 return $FAILURE 185 fi 186 if [ ${#url} -gt ${URL_MAX:-261261} ]; then 187 f_show_msg "$msg_length_of_specified_url_is_too_long" \ 188 ${#url} ${URL_MAX:-261261} 189 unset $VAR_HTTP_PATH 190 return $FAILURE 191 fi 192 case "$url" in 193 http://*) : valid URL ;; 194 *) 195 f_show_msg "$msg_sorry_invalid_url" "$url" 196 unset $VAR_HTTP_PATH 197 return $FAILURE 198 esac 199 esac 200 case "$url" in 201 http://*) : valid URL ;; 202 *) 203 f_show_msg "$msg_sorry_invalid_url" "$url" 204 unset $VAR_HTTP_PATH 205 return $FAILURE 206 esac 207 208 # Set the name of the HTTP device to the URL 209 f_struct_new DEVICE device_http 210 device_http set name "$url" 211 212 if ! f_struct device_network || 213 ! f_dialog_yesno "$msg_youve_already_done_the_network_configuration" 214 then 215 f_struct device_network && 216 f_device_shutdown network 217 if ! f_device_select_tcp; then 218 unset $VAR_HTTP_PATH 219 return $FAILURE 220 fi 221 local dev 222 f_getvar $VAR_NETWORK_DEVICE dev 223 f_struct_copy "device_$dev" device_network 224 fi 225 if ! f_device_init network; then 226 f_dprintf "f_media_set_http: %s" "$msg_net_device_init_failed" 227 unset $VAR_HTTP_PATH 228 return $FAILURE 229 fi 230 231 local hostname="${url#*://}" port=80 dir=/ 232 case "$hostname" in 233 # 234 # The order in-which the below individual cases appear is important! 235 # 236 "["*"]":*/*) # IPv6 address with port and directory 237 f_dprintf "Looks like an IPv6 addr with port/dir: %s" \ 238 "$hostname" 239 hostname="${hostname#\[}" 240 port="${hostname#*\]:}" 241 port="${port%%[!0-9]*}" 242 dir="/${hostname#*/}" 243 hostname="${hostname%%\]:*}" 244 ;; 245 "["*"]":*) # IPv6 address with port 246 f_dprintf "Looks like an IPv6 addr with port: %s" "$hostname" 247 hostname="${hostname#\[}" 248 port="${hostname#*\]:}" 249 port="${port%%[!0-9]*}" 250 hostname="${hostname%%\]:*}" 251 ;; 252 "["*"]"/*) # IPv6 address with directory 253 f_dprintf "Looks like an IPv6 addr with dir: %s" "$hostname" 254 hostname="${hostname#\[}" 255 dir="/${hostname#*/}" 256 hostname="${hostname%%\]*}" 257 ;; 258 "["*"]") # IPv6 address 259 f_dprintf "Looks like an IPv6 addr: %s" "$hostname" 260 hostname="${hostname#\[}" 261 hostname="${hostname%\]}" 262 ;; 263 # 264 # ^^^ IPv6 above / DNS Name or IPv4 below vvv 265 # 266 *:*/*) # DNS name or IPv4 address with port and directory 267 f_dprintf "Looks like a %s with port/dir: %s" \ 268 "DNS name or IPv4 addr" "$hostname" 269 port="${hostname#*:}" 270 port="${port%%[!0-9]*}" 271 dir="/${hostname#*/}" 272 hostname="${hostname%%:*}" 273 ;; 274 *:*) # DNS name or IPv4 address with port 275 f_dprintf "Looks like a DNS name or IPv4 addr with port: %s" \ 276 "$hostname" 277 port="${hostname#*:}" 278 hostname="${hostname%%:*}" 279 ;; 280 */*) # DNS name or IPv4 address with directory 281 f_dprintf "Looks like a DNS name or IPv4 addr with dir: %s" \ 282 "$hostname" 283 dir="/${hostname#*/}" 284 hostname="${hostname%%/*}" 285 ;; 286 *) # DNS name or IPv4 address 287 f_dprintf "Looks like a DNS name or IPv4 addr: %s" "$hostname" 288 : leave hostname as-is 289 esac 290 291 f_dprintf "hostname = \`%s'" "$hostname" 292 f_dprintf "dir = \`%s'" "$dir" 293 f_dprintf "port \# = \`%d'" "$port" 294 295 local ns 296 f_getvar $VAR_NAMESERVER ns 297 [ "$ns" ] || f_resolv_conf_nameservers ns 298 if [ "$ns" -a ! "$HTTP_SKIP_RESOLV" ] && ! { 299 f_validate_ipaddr "$hostname" || 300 f_validate_ipaddr6 "$hostname" 301 }; then 302 f_show_info "$msg_looking_up_host" "$hostname" 303 f_dprintf "%s: Looking up hostname, %s, using host(1)" \ 304 "f_media_set_http" "$hostname" 305 if ! f_quietly f_host_lookup "$hostname"; then 306 f_show_msg "$msg_cannot_resolve_hostname" "$hostname" 307 f_struct device_network && 308 f_device_shutdown network 309 f_struct_free device_network 310 unset $VAR_HTTP_PATH 311 return $FAILURE 312 fi 313 f_dprintf "Found DNS entry for %s successfully." "$hostname" 314 fi 315 316 setvar $VAR_HTTP_HOST "$hostname" 317 setvar $VAR_HTTP_PORT "$port" 318 setvar $VAR_HTTP_DIR "$dir" 319 320 device_http set type $DEVICE_TYPE_HTTP 321 device_http set init f_media_init_http 322 device_http set get f_media_get_http 323 device_http set shutdown f_media_shutdown_http 324 device_http set private network 325 f_struct_copy device_http device_media 326 f_struct_free device_http 327 328 return $SUCCESS 329} 330 331# f_http_check_access [$connect_only] 332# 333# Return success if able list a remote HTTP directory. If $connect_only is 334# present and non-null, then returns success if a connection can be made. 335# Variables from variable.subr that can be used to script user input: 336# 337# VAR_HTTP_HOST 338# The HTTP server host name, IPv4 address or IPv6 address. 339# Valid examples include: 340# myhost 341# 192.168.2.3 342# ::1 343# VAR_HTTP_PORT 344# The TCP port to connect to when communicating with the server. 345# VAR_HTTP_PATH 346# The HTTP path sent to the server. Unused if $connect_only is 347# present and non-NULL. 348# 349f_http_check_access() 350{ 351 local connect_only="$1" hosts= 352 353 local http_host http_port 354 f_getvar $VAR_HTTP_HOST http_host 355 f_getvar $VAR_HTTP_PORT http_port 356 357 if ! { 358 f_validate_ipaddr "$http_host" || 359 f_validate_ipaddr6 "$http_host" || 360 { 361 f_dprintf "%s: Looking up hostname, %s, using host(1)" \ 362 "f_http_check_access" "$http_host" 363 f_host_lookup "$http_host" hosts 364 } 365 }; then 366 # All the above validations failed 367 [ "$hosts" ] && f_dialog_msgbox "$hosts" 368 unset $VAR_HTTP_HOST 369 return $FAILURE 370 elif [ ! "$hosts" ]; then 371 # One of the first two validations passed 372 hosts="$http_host" 373 fi 374 375 local host connected= 376 for host in $hosts; do 377 f_quietly nc -nz "$host" "$http_port" || continue 378 connected=1; break 379 done 380 if [ ! "$connected" ]; then 381 f_show_msg "$msg_couldnt_connect_to_server http://%s:%s/" \ 382 "$http_host" "$http_port" 383 unset $VAR_HTTP_HOST 384 return $FAILURE 385 fi 386 [ "$connect_only" ] && return $SUCCESS 387 388 local http_path 389 f_getvar $VAR_HTTP_PATH http_path 390 f_show_info "$msg_checking_access_to" "$http_path" 391 392 local rx 393 if ! rx=$( 394 printf "GET /%s/ HTTP/1.0\r\n\r\n" "${http_path%/}" | 395 nc -n "$host" "$http_port" 396 ); then 397 f_show_msg "$msg_couldnt_connect_to_server http://%s:%s/" \ 398 "$http_host" "$http_port" 399 unset $VAR_HTTP_HOST 400 return $FAILURE 401 fi 402 403 local hdr 404 hdr=$( echo "$rx" | awk '/^\r$/{exit}{print}' ) 405 406 local http_found=$FAILURE 407 if echo "$hdr" | awk ' 408 BEGIN { found = 0 } 409 /^HTTP.... 200 / { 410 found = 1 411 exit 412 } 413 END { exit ! found } 414 '; then 415 http_found=$SUCCESS 416 fi 417 418 return $http_found 419} 420 421# f_media_init_http $device 422# 423# Initializes the HTTP media device. Returns success if able to confirm the 424# existence of at least one known HTTP server release path directly via HTTP 425# using f_http_check_access(), above. 426# 427# Variables from variable.subr that can be used to script user input: 428# 429# VAR_HTTP_HOST 430# The HTTP server to connect to. Must be set. Also see 431# f_http_check_access() for additional variables. 432# VAR_RELNAME 433# Usually set to `uname -r' but can be overridden. 434# VAR_HTTP_PATH 435# The HTTP path sent to the server. Usually set by calling 436# f_media_set_http(). 437# 438# Meanwhile, after successful execution, the following variables (also from 439# variable.subr) are set: 440# 441# VAR_HTTP_PATH 442# The [possibly] adjusted VAR_HTTP_PATH that was found to contain 443# a valid FreeBSD repository. 444# 445f_media_init_http() 446{ 447 local dev="$1" 448 f_dprintf "Init routine called for HTTP device. dev=[%s]" "$dev" 449 450 # 451 # First verify access 452 # 453 local connect_only=1 454 f_http_check_access $connect_only 455 456 local http_host 457 f_getvar $VAR_HTTP_HOST http_host 458 while [ ! "$http_host" ]; do 459 f_media_set_http || return $FAILURE 460 f_http_check_access $connect_only 461 f_getvar $VAR_HTTP_HOST http_host 462 done 463 464 local http_path http_found=$FAILURE 465 while :; do 466 # 467 # Now that we've verified that the path we're given is ok, 468 # let's try to be a bit intelligent in locating the release we 469 # are looking for. First off, if the release is specified as 470 # "__RELEASE" or "any", then just assume that the current 471 # directory is the one we want and give up. 472 # 473 local rel 474 f_getvar $VAR_RELNAME rel 475 f_dprintf "f_media_init_http: rel=[%s]" "$rel" 476 477 case "$rel" in 478 __RELEASE|any) 479 setvar $VAR_HTTP_PATH "$VAR_HTTP_DIR" 480 f_http_check_access 481 http_found=$? 482 ;; 483 *) 484 # 485 # Ok, since we have a release variable, let's walk 486 # through the list of directories looking for a release 487 # directory. First successful path wins. 488 # 489 local fdir hp 490 f_getvar $VAR_HTTP_PATH%/ hp 491 for fdir in $HTTP_DIRS; do 492 setvar $VAR_HTTP_PATH "$hp/$fdir/$rel" 493 if f_http_check_access; then 494 http_found=$SUCCESS 495 break 496 fi 497 done 498 esac 499 500 [ $http_found -eq $SUCCESS ] && break 501 502 f_getvar $VAR_HTTP_PATH http_path 503 f_show_msg "$msg_please_check_the_url_and_try_again" \ 504 "$http_path" 505 506 unset $VAR_HTTP_PATH 507 f_media_set_http || break 508 done 509 510 return $http_found 511} 512 513# f_media_get_http $device $file [$probe_type] 514# 515# Returns data from $file on an HTTP server using nc(1). Please note that 516# $device is unused but must be present (even if null). Information is instead 517# gathered from the environment. If $probe_type is both present and non-NULL, 518# this function exits after receiving the HTTP header response from the server 519# (if the HTTP response code is 200, success is returned; otherwise failure). 520# If $probe_type is equal to $PROBE_SIZE, prints the content-length in bytes 521# from the response (or -1 if not found) to standard-out. 522# 523# The variables used to configure the connection are as follows (all of which 524# are configured by f_media_set_http above): 525# 526# VAR_HTTP_HOST 527# HTTP server which to connect. Can be an IPv4 address, IPv6 528# address, or DNS hostname of your choice. 529# VAR_HTTP_PORT 530# TCP port to connect on; see f_media_set_http above. 531# VAR_HTTP_PATH 532# Directory prefix to use when requesting $file. Default is `/' 533# unless f_media_init_http was able to use f_http_check_access 534# to validate one of the defaults in $HTTP_DIRS (see GLOBALS at 535# the top of this file); assuming VAR_RELNAME was not set to 536# either `__RELEASE' or `any' (indicating that the global set of 537# $HTTP_DIRS should be ignored). 538# 539# See variable.subr for additional information. 540# 541# Example usage: 542# f_media_set_http 543# f_media_get_http media $file 544# 545f_media_get_http() 546{ 547 local dev="$1" file="$2" probe_type="$3" hosts= 548 549 f_dprintf "f_media_get_http: dev=[%s] file=[%s] probe_type=%s" \ 550 "$dev" "$file" "$probe_type" 551 552 local http_host http_port 553 f_getvar $VAR_HTTP_HOST http_host 554 f_getvar $VAR_HTTP_PORT http_port 555 556 if ! { 557 f_validate_ipaddr "$http_host" || 558 f_validate_ipaddr6 "$http_host" || 559 { 560 f_dprintf "%s: Looking up hostname, %s, using host(1)" \ 561 "f_media_get_http" "$http_host" 562 f_host_lookup "$http_host" hosts 563 } 564 }; then 565 # All the above validations failed 566 [ "$hosts" ] && f_dialog_msgbox "$hosts" 567 return $FAILURE 568 elif [ ! "$hosts" ]; then 569 # One of the first two validations passed 570 hosts="$http_host" 571 fi 572 573 local host connected= 574 for host in $hosts; do 575 f_quietly nc -nz "$host" "$http_port" || continue 576 connected=1; break 577 done 578 if [ ! "$connected" ]; then 579 f_show_msg "$msg_couldnt_connect_to_server http://%s:%s/" \ 580 "$http_host" "$http_port" 581 return $FAILURE 582 fi 583 584 local http_path 585 f_getvar $VAR_HTTP_PATH%/ http_path 586 local url="/$http_path/$file" rx 587 588 f_dprintf "sending http request for: %s" "$url" 589 printf "GET %s HTTP/1.0\r\n\r\n" "$url" | nc -n "$host" "$http_port" | 590 ( 591 # 592 # scan the headers of the response 593 # this is extremely quick'n dirty 594 # 595 596 rv=0 length=-1 597 while read LINE; do 598 case "$LINE" in 599 HTTP*) 600 f_dprintf "received response: %s" "$LINE" 601 set -- $LINE; rv=$2 602 f_isinteger "$rv" || rv=0 603 ;; 604 "Content-Length: "*) 605 length="${LINE% 606}" 607 length="${length#Content-Length: }" 608 f_dprintf "received content-length: %s" \ 609 "$length" 610 ;; 611 *) 612 [ "${LINE% 613}" ] || break # End of headers 614 esac 615 done 616 617 [ $rv -ge 500 ] && exit 5 618 [ $rv -eq 404 ] && exit 44 619 [ $rv -ge 400 ] && exit 4 620 [ $rv -ge 300 ] && exit 3 621 [ $rv -eq 200 ] || exit $FAILURE 622 623 if [ ! "$probe_type" ]; then 624 cat # output the rest ``as-is'' 625 elif [ "$probe_type" = "$PROBE_SIZE" ]; then 626 f_isinteger "$length" || length=-1 627 echo "$length" 628 fi 629 exit 200 630 ) 631 local retval=$? 632 [ $retval -eq 200 ] && return $SUCCESS 633 [ "$probe_type" ] && return $FAILURE 634 635 case "$retval" in 636 5) f_show_msg "$msg_server_error_when_requesting_url" "$url" ;; 637 44) f_show_msg "$msg_url_was_not_found" "$url" ;; 638 4) f_show_msg "$msg_client_error" ;; 639 *) f_show_msg "$msg_error_when_requesting_url" "$url" ;; 640 esac 641 return $FAILURE 642} 643 644############################################################ MAIN 645 646f_dprintf "%s: Successfully loaded." media/http.subr 647 648fi # ! $_MEDIA_HTTP_SUBR 649