1 if [ ! "$_MEDIA_HTTPPROXY_SUBR" ]; then _MEDIA_HTTPPROXY_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/httpproxy.subr
34 f_include $BSDCFG_SHARE/dialog.subr
35 f_include $BSDCFG_SHARE/media/ftp.subr
36 f_include $BSDCFG_SHARE/media/tcpip.subr
37 f_include $BSDCFG_SHARE/variable.subr
39 BSDCFG_LIBE="/usr/libexec/bsdconfig"
40 f_include_lang $BSDCFG_LIBE/include/messages.subr
42 ############################################################ FUNCTIONS
44 # f_media_set_http_proxy
46 # Return success if we both found and set the media type to be an ftp server,
47 # accessed via http proxy.
49 # Variables from variable.subr that can be used to script user input:
52 # HTTP Proxy server to use. Valid examples include:
57 # The default port if not specified is 3128.
59 # Variables from variable.subr that are set after successful execution include
62 # VAR_HTTP_PROXY_HOST The host portion of VAR_HTTP_PROXY.
63 # VAR_HTTP_PROXY_PORT The TCP port parsed from VAR_HTTP_PROXY.
65 # See also f_media_set_ftp() for additional variables.
67 f_media_set_http_proxy()
69 FTP_SKIP_RESOLV=1 f_media_set_ftp || return $FAILURE
71 f_variable_get_value $VAR_HTTP_PROXY \
72 "$msg_please_enter_the_address_of_the_http_proxy"
75 f_getvar $VAR_HTTP_PROXY proxy
76 [ "$proxy" ] || return $FAILURE
78 local hostname="$proxy" port=3128
81 # The order in-which the below individual cases appear is important!
83 "["*"]":*) # IPv6 address with port
84 f_dprintf "Looks like an IPv6 addr with port: %s" "$hostname"
85 hostname="${hostname#\[}"
86 port="${hostname#*\]:}"
87 port="${port%%[!0-9]*}"
88 hostname="${hostname%%\]:*}"
90 "["*"]") # IPv6 address
91 f_dprintf "Looks like an IPv6 addr: %s" "$hostname"
92 hostname="${hostname#\[}"
93 hostname="${hostname%\]}"
96 # ^^^ IPv6 above / DNS Name or IPv4 below vvv
98 *:*) # DNS name or IPv4 address with port
99 f_dprintf "Looks like a DNS name or IPv4 addr with port: %s" \
101 port="${hostname#*:}"
102 hostname="${hostname%%:*}"
104 *) # DNS name or IPv4 address
105 f_dprintf "Looks like a DNS name or IPv4 addr: %s" "$hostname"
106 : leave hostname as-is
109 setvar $VAR_HTTP_PROXY_HOST "$hostname"
110 setvar $VAR_HTTP_PROXY_PORT "$port"
113 f_dprintf "VAR_FTP_PATH : %s" "$( f_getvar $VAR_FTP_PATH )"
114 f_dprintf "VAR_HTTP_PROXY_HOST, _PORT: %s:%s" \
115 "$( f_getvar $VAR_HTTP_PROXY_HOST )" \
116 "$( f_getvar $VAR_HTTP_PROXY_PORT )"
119 # media device has been set by f_media_set_ftp(), overwrite partly:
120 device_media set type $DEVICE_TYPE_HTTP_PROXY
121 device_media set init f_media_init_http_proxy
122 device_media set get f_media_get_http_proxy
123 device_media unset shutdown
128 # f_http_proxy_check_access [$connect_only]
130 # Return success if able list a remote FTP directory via HTTP proxy. If
131 # $connect_only is present and non-null, then returns success if a connection
132 # can be made. Variables from variable.subr that can be used to script user
135 # VAR_HTTP_PROXY_HOST
136 # The HTTP proxy server host name, IPv4 address or IPv6 address.
137 # Valid examples include:
141 # VAR_HTTP_PROXY_PORT
142 # The TCP port to connect to when communicating with the HTTP
144 # VAR_HTTP_PROXY_PATH
145 # The FTP URL sent to the HTTP proxy server. Unused if
146 # $connect_only is present and non-NULL.
148 f_http_proxy_check_access()
150 local connect_only="$1" hosts=
152 local proxy_host proxy_port
153 f_getvar $VAR_HTTP_PROXY_HOST proxy_host
154 f_getvar $VAR_HTTP_PROXY_PORT proxy_port
157 f_validate_ipaddr "$proxy_host" ||
158 f_validate_ipaddr6 "$proxy_host" ||
160 f_dprintf "%s: Looking up hostname, %s, using host(1)" \
161 "f_http_proxy_check_access" "$proxy_host"
162 f_host_lookup "$proxy_host" hosts
165 # All the above validations failed
166 [ "$hosts" ] && f_dialog_msgbox "$hosts"
167 unset $VAR_HTTP_PROXY_HOST
169 elif [ ! "$hosts" ]; then
170 # One of the first two validations passed
174 local host connected=
175 for host in $hosts; do
176 f_quietly nc -nz "$host" "$proxy_port" || continue
179 if [ ! "$connected" ]; then
180 f_show_msg "$msg_couldnt_connect_to_proxy %s:%s" \
181 "$proxy_host" "$proxy_port"
182 unset $VAR_HTTP_PROXY_HOST
185 [ "$connect_only" ] && return $SUCCESS
188 # Some proxies fetch files with certain extensions in "ascii mode"
189 # instead of "binary mode" for FTP. The FTP server then translates all
192 # You can force Squid to use binary mode by appending ";type=i" to the
193 # URL, which is what sysinstall(8) has traditionally done.
197 f_getvar $VAR_HTTP_PROXY_PATH proxy_path
198 f_show_info "$msg_checking_access_to" "$proxy_path"
202 printf "GET %s/ HTTP/1.0\r\n\r\n" "${proxy_path%/}" |
203 nc -n "$host" "$proxy_port"
205 f_show_msg "$msg_couldnt_connect_to_proxy %s:%s" \
206 "$proxy_host" "$proxy_port"
207 unset $VAR_HTTP_PROXY_HOST
212 hdr=$( echo "$rx" | awk '/^\r$/{exit}{print}' )
214 local http_found=$FAILURE
215 if echo "$hdr" | awk '
227 # Scan the headers of the response
228 # this is extremely quick'n dity
231 unset $VAR_HTTP_FTP_MODE
232 if echo "$hdr" | awk '
235 if (!match($0, /^Server: /)) next
236 found = ( substr($0, 9, 5) ~ /[Ss]quid/ )
240 setvar $VAR_HTTP_FTP_MODE ";type=i"
242 setvar $VAR_HTTP_FTP_MODE ""
248 # f_media_init_http_proxy $device
250 # Initializes the HTTP Proxy media device. Returns success if able to confirm
251 # the existence of at least one known FTP server release path via HTTP proxy
252 # using f_http_proxy_check_access(), above.
254 # Variables from variable.subr that can be used to script user input:
256 # VAR_HTTP_PROXY_HOST
257 # The HTTP proxy server to connect to. Usually set by having
258 # f_media_set_http_proxy() parse VAR_HTTP_PROXY. Must be set.
259 # Also see f_http_proxy_check_access() for additional variables.
261 # Usually set to `uname -r' but can be overridden.
263 # The FTP URL to send to the HTTP proxy server. Usually set by
264 # calling f_media_set_ftp().
266 # Meanwhile, after successful execution, the following variables (also from
267 # variable.subr) are set:
269 # VAR_HTTP_PROXY_PATH
270 # The [possibly] adjusted VAR_FTP_PATH that was found to contain
271 # a valid FreeBSD repository.
273 f_media_init_http_proxy()
276 f_dprintf "Init routine called for HTTP Proxy device. dev=[%s]" "$dev"
279 # First verify access
282 f_http_proxy_check_access $connect_only
285 f_getvar $VAR_HTTP_PROXY_HOST proxy_host
286 while [ ! "$proxy_host" ]; do
287 f_media_set_http_proxy || return $FAILURE
288 f_http_proxy_check_access $connect_only
289 f_getvar $VAR_HTTP_PROXY_HOST proxy_host
292 local rel proxy_path http_found=$FAILURE
295 # If the release is specified as "__RELEASE" or "any", then
296 # just assume that the path the user gave is ok.
298 f_getvar $VAR_RELNAME rel
299 f_dprintf "f_media_init_http_proxy: rel=[%s]" "$rel"
303 f_getvar $VAR_FTP_PATH $VAR_HTTP_PROXY_PATH
304 f_http_proxy_check_access
309 f_getvar $VAR_FTP_PATH%/ fp
310 for fdir in $FTP_DIRS; do
311 setvar $VAR_HTTP_PROXY_PATH "$fp/$fdir/$rel"
312 if f_http_proxy_check_access; then
319 [ $http_found -eq $SUCCESS ] && break
321 f_getvar $VAR_HTTP_PROXY_PATH proxy_path
322 f_show_msg "$msg_please_check_the_url_and_try_again" \
325 unset $VAR_HTTP_PROXY_PATH
326 f_media_set_http_proxy || break
332 # f_media_get_http_proxy $device $file [$probe_type]
334 # Returns data from $file on an FTP server via HTTP proxy using nc(1). Please
335 # note that $device is unused but must be present (even if null). Information
336 # is instead gathered from the environment. If $probe_type is both present and
337 # non-NULL, this function exits after receiving the HTTP header response from
338 # the proxy server (if the HTTP response code is 200, success is returned;
339 # otherwise failure). If $probe_type is equal to $PROBE_SIZE, prints the
340 # content-length in bytes from the response (or -1 if not found) to standard-
343 # The variables used to configure the connection are as follows (all of which
344 # are configured by f_media_set_http_proxy above):
346 # VAR_HTTP_PROXY_HOST
347 # HTTP proxy host to connect. Can be an IPv4 address, IPv6
348 # address, or DNS hostname of your choice.
349 # VAR_HTTP_PROXY_PORT
350 # TCP port to connect on; see f_media_set_http_proxy above.
351 # VAR_HTTP_PROXY_PATH
352 # URL (including "ftp://" protocol-prefix) of FTP directory to
353 # use as a prefix when requesting $file via HTTP proxy.
355 # See variable.subr for additional information.
358 # f_media_set_http_proxy
359 # f_media_get_http_proxy media $file
361 f_media_get_http_proxy()
363 local dev="$1" file="$2" probe_type="$3" hosts=
365 f_dprintf "f_media_get_http_proxy: dev=[%s] file=[%s] probe_type=%s" \
366 "$dev" "$file" "$probe_type"
368 local proxy_host proxy_port
369 f_getvar $VAR_HTTP_PROXY_HOST proxy_host
370 f_getvar $VAR_HTTP_PROXY_PORT proxy_port
373 f_validate_ipaddr "$proxy_host" ||
374 f_validate_ipaddr6 "$proxy_host" ||
376 f_dprintf "%s: Looking up hostname, %s, using host(1)" \
377 "f_media_get_http_proxy" "$proxy_host"
378 f_host_lookup "$proxy_host" hosts
381 # All the above validations failed
382 [ "$hosts" ] && f_dialog_msgbox "$hosts"
384 elif [ ! "$hosts" ]; then
385 # One of the first two validations passed
389 local host connected=
390 for host in $hosts; do
391 f_quietly nc -nz "$host" "$proxy_port" || continue
394 if [ ! "$connected" ]; then
395 f_show_msg "$msg_couldnt_connect_to_proxy %s:%s" \
396 "$proxy_host" "$proxy_port"
400 local proxy_path mode
401 f_getvar $VAR_HTTP_PROXY_PATH%/ proxy_path
402 f_getvar $VAR_HTTP_FTP_MODE mode
403 local url="$proxy_path/$file$mode" rx
405 f_dprintf "sending http request for: %s" "$url"
406 printf "GET %s HTTP/1.0\r\n\r\n" "$url" | nc -n "$host" "$proxy_port" |
409 # scan the headers of the response
410 # this is extremely quick'n dirty
417 f_dprintf "received response: %s" "$LINE"
419 f_isinteger "$rv" || rv=0
423 length="${length#Content-Length: }"
424 f_dprintf "received content-length: %s" \
428 [ "${LINE%
\r}" ] || break # End of headers
432 [ $rv -ge 500 ] && exit 5
433 [ $rv -eq 404 ] && exit 44
434 [ $rv -ge 400 ] && exit 4
435 [ $rv -ge 300 ] && exit 3
436 [ $rv -eq 200 ] || exit $FAILURE
438 if [ ! "$probe_type" ]; then
439 cat # output the rest ``as-is''
440 elif [ "$probe_type" = "$PROBE_SIZE" ]; then
441 f_isinteger "$length" || length=-1
447 [ $retval -eq 200 ] && return $SUCCESS
448 [ "$probe_type" ] && return $FAILURE
451 5) f_show_msg "$msg_server_error_when_requesting_url" "$url" ;;
452 44) f_show_msg "$msg_url_was_not_found" "$url" ;;
453 4) f_show_msg "$msg_client_error" ;;
454 *) f_show_msg "$msg_error_when_requesting_url" "$url" ;;
455 esac 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
459 ############################################################ MAIN
461 f_dprintf "%s: Successfully loaded." media/httpproxy.subr
463 fi # ! $_MEDIA_HTTPPROXY_SUBR