1 if [ ! "$_MEDIA_HTTP_SUBR" ]; then _MEDIA_HTTP_SUBR=1
3 # Copyright (c) 2012-2013 Devin Teske
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
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.
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
29 ############################################################ INCLUDES
31 BSDCFG_SHARE="/usr/share/bsdconfig"
32 . $BSDCFG_SHARE/common.subr || exit 1
33 f_dprintf "%s: loading includes..." media/http.subr
34 f_include $BSDCFG_SHARE/device.subr
35 f_include $BSDCFG_SHARE/dialog.subr
36 f_include $BSDCFG_SHARE/media/common.subr
37 f_include $BSDCFG_SHARE/media/tcpip.subr
38 f_include $BSDCFG_SHARE/strings.subr
39 f_include $BSDCFG_SHARE/struct.subr
40 f_include $BSDCFG_SHARE/variable.subr
42 BSDCFG_LIBE="/usr/libexec/bsdconfig"
43 f_include_lang $BSDCFG_LIBE/include/messages.subr
45 ############################################################ GLOBALS
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.
59 pub/FreeBSD/releases/$UNAME_P
60 pub/FreeBSD/snapshots/$UNAME_P
61 pub/FreeBSD-Archive/old-releases/$UNAME_P
64 ############################################################ FUNCTIONS
66 # f_dialog_menu_media_http
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
73 f_dialog_menu_media_http()
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"
80 'URL' '$msg_specify_some_other_http_site'
82 local hline="$msg_select_a_site_thats_close"
84 local height width rows
85 eval f_dialog_menu_size height width rows \
93 mtag=$( eval $DIALOG \
95 --backtitle \"\$btitle\" \
97 --ok-label \"\$msg_ok\" \
98 --cancel-label \"\$msg_cancel\" \
100 $height $width $rows \
102 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
104 f_dialog_data_sanitize mtag
107 URL) setvar $VAR_HTTP_PATH "other" ;;
110 value=$( eval f_dialog_menutag2item \"\$mtag\" $menu_list )
111 setvar $VAR_HTTP_PATH "http://$value"
119 # Return success if we both found and set the media type to be an HTTP server.
121 # Variables from variable.subr that can be used to script user input:
124 # URL containing host and optionally a target path to the release
125 # repository on the HTTP server. Valid examples include:
127 # http://somename:80/pub/
128 # http://192.168.2.3/pub/
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
137 # Meanwhile, the following variables from variable.subr are set after
138 # successful execution:
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).
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/*
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").
163 f_getvar $VAR_HTTP_PATH url
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=
170 if [ ! "$url" ]; then
171 f_dialog_menu_media_http || return $FAILURE
172 f_getvar $VAR_HTTP_PATH url
174 [ "$url" ] || return $FAILURE
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
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}
193 http://*) : valid URL ;;
195 f_show_msg "$msg_sorry_invalid_url" "$url"
201 http://*) : valid URL ;;
203 f_show_msg "$msg_sorry_invalid_url" "$url"
208 # Set the name of the HTTP device to the URL
209 f_struct_new DEVICE device_http
210 device_http set name "$url"
212 if ! f_struct device_network ||
213 ! f_dialog_yesno "$msg_youve_already_done_the_network_configuration"
215 f_struct device_network &&
216 f_device_shutdown network
217 if ! f_device_select_tcp; then
222 f_getvar $VAR_NETWORK_DEVICE dev
223 f_struct_copy "device_$dev" device_network
225 if ! f_device_init network; then
226 f_dprintf "f_media_set_http: %s" "$msg_net_device_init_failed"
231 local hostname="${url#*://}" port=80 dir=/
234 # The order in-which the below individual cases appear is important!
236 "["*"]":*/*) # IPv6 address with port and directory
237 f_dprintf "Looks like an IPv6 addr with port/dir: %s" \
239 hostname="${hostname#\[}"
240 port="${hostname#*\]:}"
241 port="${port%%[!0-9]*}"
242 dir="/${hostname#*/}"
243 hostname="${hostname%%\]:*}"
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%%\]:*}"
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%%\]*}"
258 "["*"]") # IPv6 address
259 f_dprintf "Looks like an IPv6 addr: %s" "$hostname"
260 hostname="${hostname#\[}"
261 hostname="${hostname%\]}"
264 # ^^^ IPv6 above / DNS Name or IPv4 below vvv
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%%:*}"
274 *:*) # DNS name or IPv4 address with port
275 f_dprintf "Looks like a DNS name or IPv4 addr with port: %s" \
277 port="${hostname#*:}"
278 hostname="${hostname%%:*}"
280 */*) # DNS name or IPv4 address with directory
281 f_dprintf "Looks like a DNS name or IPv4 addr with dir: %s" \
283 dir="/${hostname#*/}"
284 hostname="${hostname%%/*}"
286 *) # DNS name or IPv4 address
287 f_dprintf "Looks like a DNS name or IPv4 addr: %s" "$hostname"
288 : leave hostname as-is
291 f_dprintf "hostname = \`%s'" "$hostname"
292 f_dprintf "dir = \`%s'" "$dir"
293 f_dprintf "port \# = \`%d'" "$port"
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"
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
313 f_dprintf "Found DNS entry for %s successfully." "$hostname"
316 setvar $VAR_HTTP_HOST "$hostname"
317 setvar $VAR_HTTP_PORT "$port"
318 setvar $VAR_HTTP_DIR "$dir"
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
331 # f_http_check_access [$connect_only]
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:
338 # The HTTP server host name, IPv4 address or IPv6 address.
339 # Valid examples include:
344 # The TCP port to connect to when communicating with the server.
346 # The HTTP path sent to the server. Unused if $connect_only is
347 # present and non-NULL.
349 f_http_check_access()
351 local connect_only="$1" hosts=
353 local http_host http_port
354 f_getvar $VAR_HTTP_HOST http_host
355 f_getvar $VAR_HTTP_PORT http_port
358 f_validate_ipaddr "$http_host" ||
359 f_validate_ipaddr6 "$http_host" ||
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
366 # All the above validations failed
367 [ "$hosts" ] && f_dialog_msgbox "$hosts"
370 elif [ ! "$hosts" ]; then
371 # One of the first two validations passed
375 local host connected=
376 for host in $hosts; do
377 f_quietly nc -nz "$host" "$http_port" || continue
380 if [ ! "$connected" ]; then
381 f_show_msg "$msg_couldnt_connect_to_server http://%s:%s/" \
382 "$http_host" "$http_port"
386 [ "$connect_only" ] && return $SUCCESS
389 f_getvar $VAR_HTTP_PATH http_path
390 f_show_info "$msg_checking_access_to" "$http_path"
394 printf "GET /%s/ HTTP/1.0\r\n\r\n" "${http_path%/}" |
395 nc -n "$host" "$http_port"
397 f_show_msg "$msg_couldnt_connect_to_server http://%s:%s/" \
398 "$http_host" "$http_port"
404 hdr=$( echo "$rx" | awk '/^\r$/{exit}{print}' )
406 local http_found=$FAILURE
407 if echo "$hdr" | awk '
421 # f_media_init_http $device
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.
427 # Variables from variable.subr that can be used to script user input:
430 # The HTTP server to connect to. Must be set. Also see
431 # f_http_check_access() for additional variables.
433 # Usually set to `uname -r' but can be overridden.
435 # The HTTP path sent to the server. Usually set by calling
436 # f_media_set_http().
438 # Meanwhile, after successful execution, the following variables (also from
439 # variable.subr) are set:
442 # The [possibly] adjusted VAR_HTTP_PATH that was found to contain
443 # a valid FreeBSD repository.
448 f_dprintf "Init routine called for HTTP device. dev=[%s]" "$dev"
451 # First verify access
454 f_http_check_access $connect_only
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
464 local http_path http_found=$FAILURE
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.
474 f_getvar $VAR_RELNAME rel
475 f_dprintf "f_media_init_http: rel=[%s]" "$rel"
479 setvar $VAR_HTTP_PATH "$VAR_HTTP_DIR"
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.
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
500 [ $http_found -eq $SUCCESS ] && break
502 f_getvar $VAR_HTTP_PATH http_path
503 f_show_msg "$msg_please_check_the_url_and_try_again" \
507 f_media_set_http || break
513 # f_media_get_http $device $file [$probe_only]
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_only 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).
521 # The variables used to configure the connection are as follows (all of which
522 # are configured by f_media_set_http above):
525 # HTTP server which to connect. Can be an IPv4 address, IPv6
526 # address, or DNS hostname of your choice.
528 # TCP port to connect on; see f_media_set_http above.
530 # Directory prefix to use when requesting $file. Default is `/'
531 # unless f_media_init_http was able to use f_http_check_access
532 # to validate one of the defaults in $HTTP_DIRS (see GLOBALS at
533 # the top of this file); assuming VAR_RELNAME was not set to
534 # either `__RELEASE' or `any' (indicating that the global set of
535 # $HTTP_DIRS should be ignored).
537 # See variable.subr for additional information.
541 # f_media_get_http media $file
545 local dev="$1" file="$2" probe_only="$3" hosts=
547 f_dprintf "f_media_get_http: dev=[%s] file=[%s] probe_only=%s" \
548 "$dev" "$file" "$probe_only"
550 local http_host http_port
551 f_getvar $VAR_HTTP_HOST http_host
552 f_getvar $VAR_HTTP_PORT http_port
555 f_validate_ipaddr "$http_host" ||
556 f_validate_ipaddr6 "$http_host" ||
558 f_dprintf "%s: Looking up hostname, %s, using host(1)" \
559 "f_media_get_http" "$http_host"
560 f_host_lookup "$http_host" hosts
563 # All the above validations failed
564 [ "$hosts" ] && f_dialog_msgbox "$hosts"
566 elif [ ! "$hosts" ]; then
567 # One of the first two validations passed
571 local host connected=
572 for host in $hosts; do
573 f_quietly nc -nz "$host" "$http_port" || continue
576 if [ ! "$connected" ]; then
577 f_show_msg "$msg_couldnt_connect_to_server http://%s:%s/" \
578 "$http_host" "$http_port"
583 f_getvar $VAR_HTTP_PATH%/ http_path
584 local url="/$http_path/$file" rx
586 f_dprintf "sending http request for: %s" "$url"
587 printf "GET %s HTTP/1.0\r\n\r\n" "$url" | nc -n "$host" "$http_port" |
590 # scan the headers of the response
591 # this is extremely quick'n dirty
598 f_dprintf "received response: %s" "$LINE"
600 f_isinteger "$rv" || rv=0
603 [ "${LINE%
\r}" ] || break # End of headers
607 [ $rv -ge 500 ] && exit 5
608 [ $rv -eq 404 ] && exit 44
609 [ $rv -ge 400 ] && exit 4
610 [ $rv -ge 300 ] && exit 3
611 [ $rv -eq 200 ] || exit $FAILURE
613 if [ ! "$probe_only" ]; then
614 cat # output the rest ``as-is''
619 [ $retval -eq 200 ] && return $SUCCESS
620 [ "$probe_only" ] && return $FAILURE
623 5) f_show_msg "$msg_server_error_when_requesting_url" "$url" ;;
624 44) f_show_msg "$msg_url_was_not_found" "$url" ;;
625 4) f_show_msg "$msg_client_error" ;;
626 *) f_show_msg "$msg_error_when_requesting_url" "$url" ;;
631 ############################################################ MAIN
633 f_dprintf "%s: Successfully loaded." media/http.subr
635 fi # ! $_MEDIA_HTTP_SUBR