ftp.subr revision 253566
1if [ ! "$_MEDIA_FTP_SUBR" ]; then _MEDIA_FTP_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/ftp.subr 253566 2013-07-23 12:20:27Z gavin $ 28# 29############################################################ INCLUDES 30 31BSDCFG_SHARE="/usr/share/bsdconfig" 32. $BSDCFG_SHARE/common.subr || exit 1 33f_dprintf "%s: loading includes..." media/ftp.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 47FTP_SKIP_RESOLV= 48 49URL_MAX=261261 # according to actual fetch(1) test-results 50 51FTP_DIRS=" 52 . 53 releases/$UNAME_P 54 snapshots/$UNAME_P 55 pub/FreeBSD 56 pub/FreeBSD/releases/$UNAME_P 57 pub/FreeBSD/snapshots/$UNAME_P 58 pub/FreeBSD-Archive/old-releases/$UNAME_P 59" # END-QUOTE 60 61############################################################ FUNCTIONS 62 63# f_dialog_menu_media_ftp 64# 65# Prompt the user to select from a range of ``built-in'' FTP servers or specify 66# their own. If the user makes a choice and doesn't cancel or press Esc, stores 67# the user's choice in VAR_FTP_PATH (see variables.subr) and returns success. 68# 69f_dialog_menu_media_ftp() 70{ 71 f_dialog_title "$msg_please_select_a_freebsd_ftp_distribution_site" 72 local title="$DIALOG_TITLE" btitle="$DIALOG_BACKTITLE" 73 f_dialog_title_restore 74 local prompt="$msg_please_select_the_site_closest_to_you_or_other" 75 local menu_list=" 76 '$msg_main_site' 'ftp.freebsd.org' 77 'URL' '$msg_specify_some_other_ftp_site' 78 'IPv6 $msg_main_site' 'ftp.freebsd.org' 79 ' IPv6 $msg_france' 'ftp4.fr.freebsd.org' 80 ' IPv6 $msg_france #8' 'ftp8.fr.freebsd.org' 81 ' IPv6 $msg_ireland' 'ftp3.ie.freebsd.org' 82 ' IPv6 $msg_japan' 'ftp2.jp.freebsd.org' 83 ' IPv6 $msg_sweden' 'ftp4.se.freebsd.org' 84 ' IPv6 $msg_usa' 'ftp4.us.freebsd.org' 85 ' IPv6 $msg_turkey' 'ftp2.tr.freebsd.org' 86 '$msg_primary' 'ftp1.freebsd.org' 87 ' $msg_primary #2' 'ftp2.freebsd.org' 88 ' $msg_primary #3' 'ftp3.freebsd.org' 89 ' $msg_primary #4' 'ftp4.freebsd.org' 90 ' $msg_primary #5' 'ftp5.freebsd.org' 91 ' $msg_primary #6' 'ftp6.freebsd.org' 92 ' $msg_primary #7' 'ftp7.freebsd.org' 93 ' $msg_primary #10' 'ftp10.freebsd.org' 94 ' $msg_primary #11' 'ftp11.freebsd.org' 95 ' $msg_primary #12' 'ftp12.freebsd.org' 96 ' $msg_primary #13' 'ftp13.freebsd.org' 97 ' $msg_primary #14' 'ftp14.freebsd.org' 98 '$msg_armenia' 'ftp1.am.freebsd.org' 99 '$msg_australia' 'ftp.au.freebsd.org' 100 ' $msg_australia #2' 'ftp2.au.freebsd.org' 101 ' $msg_australia #3' 'ftp3.au.freebsd.org' 102 '$msg_austria' 'ftp.at.freebsd.org' 103 '$msg_brazil' 'ftp2.br.freebsd.org' 104 ' $msg_brazil #3' 'ftp3.br.freebsd.org' 105 ' $msg_brazil #4' 'ftp4.br.freebsd.org' 106 '$msg_canada' 'ftp.ca.freebsd.org' 107 '$msg_china' 'ftp.cn.freebsd.org' 108 '$msg_czech_republic' 'ftp.cz.freebsd.org' 109 '$msg_denmark' 'ftp.dk.freebsd.org' 110 '$msg_estonia' 'ftp.ee.freebsd.org' 111 '$msg_finland' 'ftp.fi.freebsd.org' 112 '$msg_france' 'ftp.fr.freebsd.org' 113 ' $msg_france #3' 'ftp3.fr.freebsd.org' 114 ' $msg_france #4' 'ftp4.fr.freebsd.org' 115 ' $msg_france #5' 'ftp5.fr.freebsd.org' 116 ' $msg_france #6' 'ftp6.fr.freebsd.org' 117 ' $msg_france #7' 'ftp7.fr.freebsd.org' 118 ' $msg_france #8' 'ftp8.fr.freebsd.org' 119 '$msg_germany' 'ftp.de.freebsd.org' 120 ' $msg_germany #2' 'ftp2.de.freebsd.org' 121 ' $msg_germany #4' 'ftp4.de.freebsd.org' 122 ' $msg_germany #5' 'ftp5.de.freebsd.org' 123 ' $msg_germany #6' 'ftp6.de.freebsd.org' 124 ' $msg_germany #7' 'ftp7.de.freebsd.org' 125 ' $msg_germany #8' 'ftp8.de.freebsd.org' 126 '$msg_greece' 'ftp.gr.freebsd.org' 127 ' $msg_greece #2' 'ftp2.gr.freebsd.org' 128 '$msg_iceland' 'ftp.is.freebsd.org' 129 '$msg_ireland' 'ftp3.ie.freebsd.org' 130 '$msg_israel' 'ftp.il.freebsd.org' 131 '$msg_italy' 'ftp.it.freebsd.org' 132 '$msg_japan' 'ftp.jp.freebsd.org' 133 ' $msg_japan #2' 'ftp2.jp.freebsd.org' 134 ' $msg_japan #3' 'ftp3.jp.freebsd.org' 135 ' $msg_japan #4' 'ftp4.jp.freebsd.org' 136 ' $msg_japan #5' 'ftp5.jp.freebsd.org' 137 ' $msg_japan #6' 'ftp6.jp.freebsd.org' 138 ' $msg_japan #7' 'ftp7.jp.freebsd.org' 139 ' $msg_japan #8' 'ftp8.jp.freebsd.org' 140 ' $msg_japan #9' 'ftp9.jp.freebsd.org' 141 '$msg_korea' 'ftp.kr.freebsd.org' 142 ' $msg_korea #2' 'ftp2.kr.freebsd.org' 143 '$msg_latvia' 'ftp.lv.freebsd.org' 144 '$msg_lithuania' 'ftp.lt.freebsd.org' 145 '$msg_netherlands' 'ftp.nl.freebsd.org' 146 ' $msg_netherlands #2' 'ftp2.nl.freebsd.org' 147 '$msg_new_zealand' 'ftp.nz.freebsd.org' 148 '$msg_norway' 'ftp.no.freebsd.org' 149 '$msg_poland' 'ftp.pl.freebsd.org' 150 ' $msg_poland #2' 'ftp2.pl.freebsd.org' 151 '$msg_russia' 'ftp.ru.freebsd.org' 152 ' $msg_russia #2' 'ftp2.ru.freebsd.org' 153 ' $msg_russia #4' 'ftp4.ru.freebsd.org' 154 ' $msg_russia #5' 'ftp5.ru.freebsd.org' 155 ' $msg_russia #6' 'ftp6.ru.freebsd.org' 156 '$msg_slovak_republic' 'ftp.sk.freebsd.org' 157 ' $msg_slovak_republic #2' 'ftp2.sk.freebsd.org' 158 '$msg_slovenia' 'ftp.si.freebsd.org' 159 '$msg_south_africa' 'ftp.za.freebsd.org' 160 ' $msg_south_africa #2' 'ftp2.za.freebsd.org' 161 ' $msg_south_africa #4' 'ftp4.za.freebsd.org' 162 '$msg_spain' 'ftp.es.freebsd.org' 163 ' $msg_spain #3' 'ftp3.es.freebsd.org' 164 '$msg_sweden' 'ftp.se.freebsd.org' 165 ' $msg_sweden #2' 'ftp2.se.freebsd.org' 166 ' $msg_sweden #3' 'ftp3.se.freebsd.org' 167 ' $msg_sweden #4' 'ftp4.se.freebsd.org' 168 ' $msg_sweden #5' 'ftp5.se.freebsd.org' 169 ' $msg_sweden #6' 'ftp6.se.freebsd.org' 170 '$msg_switzerland' 'ftp.ch.freebsd.org' 171 '$msg_taiwan' 'ftp.tw.freebsd.org' 172 ' $msg_taiwan #2' 'ftp2.tw.freebsd.org' 173 ' $msg_taiwan #3' 'ftp3.tw.freebsd.org' 174 ' $msg_taiwan #4' 'ftp4.tw.freebsd.org' 175 ' $msg_taiwan #6' 'ftp6.tw.freebsd.org' 176 ' $msg_taiwan #11' 'ftp11.tw.freebsd.org' 177 '$msg_uk' 'ftp.uk.freebsd.org' 178 ' $msg_uk #2' 'ftp2.uk.freebsd.org' 179 ' $msg_uk #3' 'ftp3.uk.freebsd.org' 180 ' $msg_uk #4' 'ftp4.uk.freebsd.org' 181 ' $msg_uk #5' 'ftp5.uk.freebsd.org' 182 '$msg_ukraine' 'ftp.ua.freebsd.org' 183 ' $msg_ukraine #7' 'ftp7.ua.freebsd.org' 184 '$msg_usa #1' 'ftp1.us.freebsd.org' 185 ' $msg_usa #2' 'ftp2.us.freebsd.org' 186 ' $msg_usa #3' 'ftp3.us.freebsd.org' 187 ' $msg_usa #4' 'ftp4.us.freebsd.org' 188 ' $msg_usa #5' 'ftp5.us.freebsd.org' 189 ' $msg_usa #6' 'ftp6.us.freebsd.org' 190 ' $msg_usa #8' 'ftp8.us.freebsd.org' 191 ' $msg_usa #10' 'ftp10.us.freebsd.org' 192 ' $msg_usa #11' 'ftp11.us.freebsd.org' 193 ' $msg_usa #13' 'ftp13.us.freebsd.org' 194 ' $msg_usa #14' 'ftp14.us.freebsd.org' 195 ' $msg_usa #15' 'ftp15.us.freebsd.org' 196 " # END-QUOTE 197 local hline="$msg_select_a_site_thats_close" 198 199 local height width rows 200 eval f_dialog_menu_size height width rows \ 201 \"\$title\" \ 202 \"\$btitle\" \ 203 \"\$prompt\" \ 204 \"\$hline\" \ 205 $menu_list 206 207 local mtag 208 mtag=$( eval $DIALOG \ 209 --title \"\$title\" \ 210 --backtitle \"\$btitle\" \ 211 --hline \"\$hline\" \ 212 --ok-label \"\$msg_ok\" \ 213 --cancel-label \"\$msg_cancel\" \ 214 --menu \"\$prompt\" \ 215 $height $width $rows \ 216 $menu_list \ 217 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD 218 ) || return $FAILURE 219 f_dialog_data_sanitize mtag 220 221 case "$mtag" in 222 URL) setvar $VAR_FTP_PATH "other" ;; 223 *) 224 local value 225 value=$( eval f_dialog_menutag2item \"\$mtag\" $menu_list ) 226 setvar $VAR_FTP_PATH "ftp://$value" 227 esac 228 229 return $SUCCESS 230} 231 232# f_media_set_ftp 233# 234# Return success if we both found and set the media type to be an FTP server. 235# Variables from variable.subr that can be used to script user input: 236# 237# VAR_FTP_PATH 238# Can be a URL (including "ftp://" protocol-prefix) or "other" 239# (user is prompted to enter FTP URL). If a URL, can optionally 240# contain directory prefix after hostname/port. Valid examples 241# include: 242# ftp://myhost 243# ftp://somename:21/pub/ 244# ftp://192.168.2.3/pub/ 245# ftp://[::1]:21/ 246# The default port if not specified is 21. 247# VAR_NAMESERVER [Optional] 248# If set, overrides resolv.conf(5) and sets the nameserver that 249# is used to convert names into addresses (when a name converts 250# into multiple addresses, the first address to successfully 251# connect is used). 252# 253# Meanwhile, the following variables from variable.subr are set after 254# successful execution: 255# 256# VAR_FTP_HOST 257# The FTP host to connect to, parsed from VAR_FTP_PATH. In the 258# example case of IPv6 where VAR_FTP_PATH is "ftp://[::1]", this 259# variable will be set to "::1" (the outer brackets are removed). 260# VAR_FTP_PORT 261# The TCP port to connect to, parsed from VAR_FTP_PATH. Usually 262# 21 unless VAR_FTP_PATH was of one of the following forms: 263# ftp://hostname:OTHER_PORT 264# ftp://hostname:OTHER_PORT/* 265# ftp://ip:OTHER_PORT 266# ftp://ip:OTHER_PORT/* 267# ftp://[ip6]:OTHER_PORT 268# ftp://[ip6]:OTHER_PORT/* 269# VAR_FTP_DIR 270# If VAR_FTP_PATH contained a directory element (e.g., 271# "ftp://localhost/pub") this variable contains only the 272# directory element (e.g., "/pub"). 273# 274f_media_set_ftp() 275{ 276 f_media_close 277 278 local url 279 f_getvar $VAR_FTP_PATH url 280 281 # If we've been through here before ... 282 if f_struct device_network && [ "${url#$msg_other}" ]; then 283 f_dialog_yesno "$msg_reuse_old_ftp_site_selection_values" || 284 url= 285 fi 286 287 if [ ! "$url" ]; then 288 f_dialog_menu_media_ftp || return $FAILURE 289 f_getvar $VAR_FTP_PATH url 290 fi 291 [ "$url" ] || return $FAILURE 292 293 case "$url" in 294 other) 295 setvar $VAR_FTP_PATH "ftp://" 296 f_variable_get_value $VAR_FTP_PATH \ 297 "$msg_please_specify_url_of_a_freebsd_distribution" 298 f_getvar $VAR_FTP_PATH url 299 if [ ! "${url#ftp://}" ]; then 300 unset $VAR_FTP_PATH 301 return $FAILURE 302 fi 303 if [ ${#url} -gt ${URL_MAX:-261261} ]; then 304 f_show_msg "$msg_length_of_specified_url_is_too_long" \ 305 ${#url} ${URL_MAX:-261261} 306 unset $VAR_FTP_PATH 307 return $FAILURE 308 fi 309 case "$url" in 310 ftp://*) : valid URL ;; 311 *) 312 f_show_msg "$msg_sorry_invalid_url" "$url" 313 unset $VAR_FTP_PATH 314 return $FAILURE 315 esac 316 esac 317 case "$url" in 318 ftp://*) : valid URL ;; 319 *) 320 f_show_msg "$msg_sorry_invalid_url" "$url" 321 unset $VAR_FTP_PATH 322 return $FAILURE 323 esac 324 325 # Set the name of the FTP device to the URL 326 f_struct_new DEVICE device_ftp 327 device_ftp set name "$url" 328 329 if ! f_struct device_network || 330 ! f_dialog_yesno "$msg_youve_already_done_the_network_configuration" 331 then 332 f_struct device_network && 333 f_device_shutdown network 334 if ! f_device_select_tcp; then 335 unset $VAR_FTP_PATH 336 return $FAILURE 337 fi 338 local dev 339 f_getvar $VAR_NETWORK_DEVICE dev 340 f_struct_copy "device_$dev" device_network 341 fi 342 if ! f_device_init network; then 343 f_dprintf "f_media_set_ftp: %s" "$msg_net_device_init_failed" 344 unset $VAR_FTP_PATH 345 return $FAILURE 346 fi 347 348 local hostname="${url#*://}" port=21 dir=/ 349 case "$hostname" in 350 # 351 # The order in-which the below individual cases appear is important! 352 # 353 "["*"]":*/*) # IPv6 address with port and directory 354 f_dprintf "Looks like an IPv6 addr with port/dir: %s" \ 355 "$hostname" 356 hostname="${hostname#\[}" 357 port="${hostname#*\]:}" 358 port="${port%%[!0-9]*}" 359 dir="/${hostname#*/}" 360 hostname="${hostname%%\]:*}" 361 ;; 362 "["*"]":*) # IPv6 address with port 363 f_dprintf "Looks like an IPv6 addr with port: %s" "$hostname" 364 hostname="${hostname#\[}" 365 port="${hostname#*\]:}" 366 port="${port%%[!0-9]*}" 367 hostname="${hostname%%\]:*}" 368 ;; 369 "["*"]"/*) # IPv6 address with directory 370 f_dprintf "Looks like an IPv6 addr with dir: %s" "$hostname" 371 hostname="${hostname#\[}" 372 dir="/${hostname#*/}" 373 hostname="${hostname%%\]*}" 374 ;; 375 "["*"]") # IPv6 address 376 f_dprintf "Looks like an IPv6 addr: %s" "$hostname" 377 hostname="${hostname#\[}" 378 hostname="${hostname%\]}" 379 ;; 380 # 381 # ^^^ IPv6 above / DNS Name or IPv4 below vvv 382 # 383 *:*/*) # DNS name or IPv4 address with port and directory 384 f_dprintf "Looks like a %s with port/dir: %s" \ 385 "DNS name or IPv4 addr" "$hostname" 386 port="${hostname#*:}" 387 port="${port%%[!0-9]*}" 388 dir="/${hostname#*/}" 389 hostname="${hostname%%:*}" 390 ;; 391 *:*) # DNS name or IPv4 address with port 392 f_dprintf "Looks like a DNS name or IPv4 addr with port: %s" \ 393 "$hostname" 394 port="${hostname#*:}" 395 hostname="${hostname%%:*}" 396 ;; 397 */*) # DNS name or IPv4 address with directory 398 f_dprintf "Looks like a DNS name or IPv4 addr with dir: %s" \ 399 "$hostname" 400 dir="/${hostname#*/}" 401 hostname="${hostname%%/*}" 402 ;; 403 *) # DNS name or IPv4 address 404 f_dprintf "Looks like a DNS name or IPv4 addr: %s" "$hostname" 405 : leave hostname as-is 406 esac 407 408 f_dprintf "hostname = \`%s'" "$hostname" 409 f_dprintf "dir = \`%s'" "$dir" 410 f_dprintf "port \# = \`%d'" "$port" 411 412 local ns 413 f_getvar $VAR_NAMESERVER ns 414 [ "$ns" ] || f_resolv_conf_nameservers ns 415 if [ "$ns" -a ! "$FTP_SKIP_RESOLV" ] && ! { 416 f_validate_ipaddr "$hostname" || 417 f_validate_ipaddr6 "$hostname" 418 }; then 419 f_show_info "$msg_looking_up_host" "$hostname" 420 f_dprintf "%s: Looking up hostname, %s, using host(1)" \ 421 "f_media_set_ftp" "$hostname" 422 if ! f_quietly f_host_lookup "$hostname"; then 423 f_show_msg "$msg_cannot_resolve_hostname" "$hostname" 424 f_struct device_network && 425 f_device_shutdown network 426 f_struct_free device_network 427 unset $VAR_FTP_PATH 428 return $FAILURE 429 fi 430 f_dprintf "Found DNS entry for %s successfully." "$hostname" 431 fi 432 433 setvar $VAR_FTP_HOST "$hostname" 434 setvar $VAR_FTP_PORT "$port" 435 setvar $VAR_FTP_DIR "$dir" 436 437 device_ftp set type $DEVICE_TYPE_FTP 438 device_ftp set init f_media_init_ftp 439 device_ftp set get f_media_get_ftp 440 device_ftp set shutdown f_media_shutdown_ftp 441 device_ftp set private network 442 f_struct_copy device_ftp device_media 443 f_struct_free device_ftp 444 445 return $SUCCESS 446} 447 448# f_media_set_ftp_active 449# 450# Wrapper to f_media_set_ftp to access FTP servers actively. 451# 452f_media_set_ftp_active() 453{ 454 setvar $VAR_FTP_STATE "active" 455 f_media_set_ftp 456} 457 458# f_media_set_ftp_passive 459# 460# Wrapper to f_media_set_ftp to access FTP servers passively. 461# 462f_media_set_ftp_passive() 463{ 464 setvar $VAR_FTP_STATE "passive" 465 f_media_set_ftp 466} 467 468# f_media_set_ftp_userpass 469# 470# Prompt the user to enter/confirm the username/password variables that will 471# be used to communicate with the FTP servers. Returns success if the user does 472# not cancel or press Esc to either username or password. 473# 474# Variables from variable.subr that can be used to script user input: 475# 476# VAR_FTP_USER 477# The username to send via ftp(1) when connecting to an FTP 478# server. 479# VAR_FTP_PASS 480# The password to send with the above username. 481# 482# Does not prompt for confirmation of values if VAR_NONINTERACTIVE is set (see 483# variable.subr for more information). 484# 485f_media_set_ftp_userpass() 486{ 487 local user pass 488 f_variable_get_value $VAR_FTP_USER \ 489 "$msg_please_enter_the_username_you_wish_to_login_as" 490 f_getvar $VAR_FTP_USER user 491 if [ "$user" ]; then 492 f_variable_get_value $VAR_FTP_PASS \ 493 "$msg_please_enter_the_password_for_this_user" 494 f_getvar $VAR_FTP_PASS pass 495 else 496 pass= 497 fi 498 [ "$pass" ] # Return status 499} 500 501# f_device_network_up $device 502# 503# Brings up attached network device, if any - takes FTP device as arg. 504# 505f_device_network_up() 506{ 507 local dev="$1" netDev 508 f_struct device_$dev || return $FAILURE 509 device_$dev get private netDev || return $SUCCESS # No net == happy net 510 f_device_init $netDev 511} 512 513# f_device_network_down $device 514# 515# Brings down attached network device, if any - takes FTP device as arg. 516# 517f_device_network_down() 518{ 519 local dev="$1" netDev 520 f_struct device_$dev || return $FAILURE 521 device_$dev get private netDev || return $SUCCESS 522 f_device_shutdown $netDev 523} 524 525# f_media_init_ftp $device 526# 527# Initializes the FTP media device. Returns success if both able to log into 528# the FTP server and confirm the existence of at least one known release path 529# using ftp(1). 530# 531# Variables from variable.subr used to initialize the connection are as follows 532# (all of which are configured by f_media_set_ftp above): 533# 534# VAR_FTP_PATH 535# The unparsed FTP URL representing the server to contact. 536# Usually "ftp://server" for example. Can contain TCP port number 537# and/or directory path (but should not contain username/password 538# info). 539# VAR_FTP_HOST 540# The FTP host to connect to. Can be an IPv4 address (e.g., 541# 127.0.0.1), IPv6 address (e.g., ::1), or DNS hostname. Usually 542# set automatically in f_media_set_ftp() by parsing VAR_FTP_PATH. 543# VAR_FTP_PORT 544# The TCP port to connect to. Usually set automatically in 545# f_media_set_ftp() by parsing VAR_FTP_PATH. 546# VAR_FTP_DIR 547# The base FTP directory to use when downloading files from the 548# FTP server. Usually set automatically in f_media_set_ftp() by 549# parsing VAR_FTP_PATH. 550# VAR_FTP_USER [Optional] 551# If unset, defaults to using anonymous access. 552# VAR_FTP_PASS [Optional] 553# If unset, defaults to a sensible value. 554# 555# In addition, the following (managed either manually or by f_media_set_ftp_*): 556# 557# VAR_FTP_STATE 558# Sets FTPMODE for ftp(1) and can be one of: 559# active active mode FTP only 560# auto automatic determination of passive or active 561# (this is the default) 562# gate gate-ftp mode 563# passive passive mode FTP only 564# See ftp(1) for additional information. 565# 566# And last, but not least (managed automatically or manually): 567# 568# VAR_RELNAME 569# Defaults to being set to $(uname -r) but can be overridden. 570# This sets the name of a release to look for as part of a well 571# known set of paths to search for release data once connected 572# via FTP. If set to "__RELEASE" or "any" then the VAR_FTP_DIR is 573# taken as the absolute path to the release and no further 574# searching is done (see FTP_DIRS above in the GLOBALS section 575# for a list of well known paths that are used when searching for 576# a VAR_RELNAME sub-directory). 577# 578f_media_init_ftp() 579{ 580 local dev="$1" 581 582 local url 583 device_$dev get name url 584 f_dprintf "Init routine called for FTP device. url=[%s]" "$url" 585 586 if [ "$FTP_INITIALIZED" ]; then 587 f_dprintf "FTP device already initialized." 588 return $SUCCESS 589 fi 590 591 # If we can't initialize the network, bag it! 592 f_device_network_up $dev || return $FAILURE 593 594 local cp 595 while :; do 596 f_getvar $VAR_FTP_PATH cp 597 if [ ! "$cp" ]; then 598 if ! f_media_set_ftp || 599 ! f_getvar $VAR_FTP_PATH cp || 600 [ ! "$cp" ] 601 then 602 f_show_msg "$msg_unable_to_get_proper_ftp_path" 603 f_device_network_down $dev 604 return $FAILURE 605 fi 606 fi 607 608 local ftp_host ftp_dir 609 if ! { 610 f_getvar $VAR_FTP_HOST ftp_host && 611 f_getvar $VAR_FTP_DIR ftp_dir 612 }; then 613 f_show_msg "$msg_missing_ftp_host_or_directory" 614 f_device_network_down $dev 615 return $FAILURE 616 fi 617 618 local ftp_port 619 f_getvar $VAR_FTP_PORT ftp_port 620 local host="$ftp_host" port="${ftp_port:+:$ftp_port}" 621 case "$host" in *:*) host="[$host]"; esac 622 623 local user pass use_anon= 624 f_getvar $VAR_FTP_USER user 625 if [ ! "$user" ]; then 626 user="anonymous" 627 use_anon=1 628 fi 629 if ! f_getvar $VAR_FTP_PASS pass; then 630 f_getvar $VAR_HOSTNAME cp 631 if f_running_as_init; then 632 pass="installer@$cp" 633 else 634 local name="$( id -un 2> /dev/null )" 635 pass="${name:-ftp}@$cp" 636 fi 637 fi 638 639 f_show_info "$msg_logging_in_to_user_at_host" \ 640 "$user" "$ftp_host" 641 642 local userpass="" 643 if [ ! "$use_anon" ] && [ "$user" -o "$pass" ]; then 644 userpass="$user${pass:+:$( f_uriencode "$pass" )}" 645 userpass="$userpass${userpass:+@}" 646 fi 647 648 local mode rx 649 f_getvar $VAR_FTP_STATE mode 650 651 if [ "$ftp_dir" ]; then 652 if ! rx=$( 653 printf 'cd "%s"\npwd\n' "$ftp_dir" | eval \ 654 FTPMODE=\"\$mode\" \ 655 ${use_anon:+FTPANONPASS=\"\$pass\"} \ 656 ftp -V ${use_anon:+-a} \ 657 \"ftp://\$userpass\$host\$port\" \ 658 2>&1 659 ); then 660 f_show_msg "$msg_couldnt_open_ftp_connection" \ 661 "$ftp_host" "$rx" 662 break # to failure 663 fi 664 if echo "$rx" | awk -v dir="/${ftp_dir#/}" ' 665 BEGIN { 666 found = 0 667 if ( dir != "/" ) sub("/$", "", dir) 668 } 669 /^Remote directory: / { 670 sub(/^[^:]*:[[:space:]]*/, "") 671 if ($0 != dir) next 672 found = 1; exit 673 } 674 END { exit ! found } 675 '; then 676 setvar $VAR_FTP_DIR "$ftp_dir" 677 setvar $VAR_FTP_PATH \ 678 "ftp://$ftp_host/${ftp_dir#/}" 679 else 680 f_show_msg \ 681 "$msg_please_check_the_url_and_try_again" \ 682 "ftp://$ftp_host/${ftp_dir#/}" 683 break # to failure 684 fi 685 fi 686 687 # 688 # Now that we've verified that the path we're given is ok, 689 # let's try to be a bit intelligent in locating the release we 690 # are looking for. First off, if the release is specified as 691 # "__RELEASE" or "any", then just assume that the current 692 # directory is the one we want and give up. 693 # 694 local rel 695 f_getvar $VAR_RELNAME rel 696 f_dprintf "f_media_init_ftp: rel=[%s]" "$rel" 697 698 case "$rel" in 699 __RELEASE|any) 700 FTP_INITIALIZED=YES 701 return $SUCCESS 702 ;; 703 *) 704 # 705 # Ok, since we have a release variable, let's walk 706 # through the list of directories looking for a release 707 # directory. First successful CWD wins. 708 # 709 if ! rx=$( 710 for dir in $FTP_DIRS; do 711 # Avoid confusing some servers 712 [ "$dir" = "." ] && continue 713 printf 'cd "/%s/%s"\npwd\n' \ 714 "$dir" "$rel" 715 done | eval \ 716 FTPMODE=\"\$mode\" \ 717 ${use_anon:+FTPANONPASS=\"\$pass\"} \ 718 ftp -V ${use_anon:+-a} \ 719 \"ftp://\$userpass\$host\$port\" \ 720 2>&1 721 ); then 722 f_show_msg "$msg_couldnt_open_ftp_connection" \ 723 "$ftp_host" "$rx" 724 break # to failure 725 fi 726 727 local fdir 728 if fdir=$( echo "$rx" | awk ' 729 BEGIN { found = 0 } 730 /^Remote directory: / { 731 sub(/^[^:]*:[[:space:]]*/, "") 732 if ($0 == "/") next 733 # Exit after the first dir 734 found++; print; exit 735 } 736 END { exit ! found } 737 ' ); then 738 setvar $VAR_FTP_DIR "$fdir" 739 setvar $VAR_FTP_PATH "ftp://$ftp_host$fdir" 740 FTP_INITIALIZED=YES 741 return $SUCCESS 742 else 743 f_yesno "$msg_cant_find_distribution" \ 744 "$rel" "$ftp_host" 745 if [ $? -eq $SUCCESS ]; then 746 unset $VAR_FTP_PATH 747 f_media_set_ftp && continue 748 fi 749 fi 750 esac 751 break # to failure 752 done 753 754 unset FTP_INITIALIZED $VAR_FTP_PATH 755 f_device_network_down $dev 756 return $FAILURE 757} 758 759# f_media_get_ftp $device $file [$probe_type] 760# 761# Returns data from $file on an FTP server using ftp(1). Please note that 762# $device is unused but must be present (even if null). Information is instead 763# gathered from the environment. If $probe_type is present and non-NULL, 764# returns success if $file exists. If $probe_type is equal to $PROBE_SIZE, 765# prints the size of $file in bytes to standard-out. 766# 767# Variables from variable.subr used to configure the connection are as follows 768# (all of which are configured by f_media_set_ftp above): 769# 770# VAR_FTP_HOST 771# FTP host to connect to. Can be an IPv4 address, IPv6 address, 772# or DNS hostname of your choice. 773# VAR_FTP_PORT 774# TCP port to connect on; see f_media_set_ftp() above. 775# VAR_FTP_USER [Optional] 776# If unset, defaults to using anonymous access. 777# VAR_FTP_PASS [Optional] 778# If unset, defaults to a sensible value. 779# 780# In addition, the following (managed either manually or by f_media_set_ftp_*): 781# 782# VAR_FTP_STATE 783# Sets FTPMODE for ftp(1) and can be one of: 784# active active mode FTP only 785# auto automatic determination of passive or active 786# (this is the default) 787# gate gate-ftp mode 788# passive passive mode FTP only 789# See ftp(1) for additional information. 790# 791# See variable.subr for additional information. 792# 793# Example usage: 794# f_media_set_ftp 795# f_media_get_ftp media $file 796# 797f_media_get_ftp() 798{ 799 local dev="$1" file="$2" probe_type="$3" hosts= 800 801 f_dprintf "f_media_get_ftp: dev=[%s] file=[%s] probe_type=%s" \ 802 "$dev" "$file" "$probe_type" 803 804 local ftp_host ftp_port 805 f_getvar $VAR_FTP_HOST ftp_host 806 f_getvar $VAR_FTP_PORT ftp_port 807 808 if [ ! "$FTP_INITIALIZED" ]; then 809 f_dprintf "No FTP connection open, can't get file %s" "$file" 810 return $FAILURE 811 fi 812 813 if ! { 814 f_validate_ipaddr "$ftp_host" || 815 f_validate_ipaddr6 "$ftp_host" || 816 { 817 f_dprintf "%s: Looking up hostname, %s, using host(1)" \ 818 "f_media_get_ftp" "$ftp_host" 819 f_host_lookup "$ftp_host" hosts 820 } 821 }; then 822 # All the above validations failed 823 [ "$hosts" ] && f_dialog_msgbox "$hosts" 824 return $FAILURE 825 elif [ ! "$hosts" ]; then 826 # One of the first two validations passed 827 hosts="$ftp_host" 828 fi 829 830 local host connected= 831 for host in $hosts; do 832 f_quietly nc -nz "$host" "$ftp_port" || continue 833 connected=1; break 834 done 835 if [ ! "$connected" ]; then 836 f_show_msg "$msg_couldnt_connect_to_ftp_server %s:%s" \ 837 "$ftp_host" "$ftp_port" 838 return $FAILURE 839 fi 840 841 local user pass use_anon= 842 f_getvar $VAR_FTP_USER user 843 if [ ! "$user" ]; then 844 user="anonymous" 845 use_anon=1 846 fi 847 if ! f_getvar $VAR_FTP_PASS pass; then 848 f_getvar $VAR_HOSTNAME cp 849 if f_running_as_init; then 850 pass="installer@$cp" 851 else 852 local name="$( id -un 2> /dev/null )" 853 pass="${name:-ftp}@$cp" 854 fi 855 fi 856 857 local userpass="" 858 if [ ! "$use_anon" ] && [ "$user" -o "$pass" ]; then 859 userpass="$user${pass:+:$( f_uriencode "$pass" )}" 860 userpass="$userpass${userpass:+@}" 861 fi 862 863 local dir mode rx 864 f_getvar $VAR_FTP_DIR\#/ dir 865 f_getvar $VAR_FTP_STATE mode 866 867 local port="${ftp_port:+:$ftp_port}" 868 case "$host" in *:*) host="[$host]"; esac 869 870 f_dprintf "sending ftp request for: %s" "ftp://$host$port/$dir/$file" 871 872 if [ "$probe_type" ]; then 873 local url="ftp://$userpass$host$port/$dir/$file" size 874 [ "$use_anon" ] && url="ftp://$host$port/$dir/$file" 875 if ! size=$( fetch -s "$url" 2>&1 ) || ! f_isinteger "$size" 876 then 877 f_dprintf "request failed! size response=[%s]" "$size" 878 [ "$probe_type" = "$PROBE_SIZE" ] && echo "-1" 879 return $FAILURE 880 fi 881 [ "$probe_type" = "$PROBE_SIZE" ] && echo "$size" 882 return $SUCCESS 883 fi 884 885 eval FTPMODE=\"\$mode\" ${use_anon:+FTPANONPASS=\"\$pass\"} \ 886 ftp -V ${use_anon:+-a} -o - \ 887 \"ftp://\$userpass\$host\$port/\$dir/\$file\" 2> /dev/null 888 local retval=$? 889 890 [ $retval -eq $SUCCESS ] || f_dprintf "request failed!" 891 return $retval 892} 893 894# f_media_shutdown_ftp $device 895# 896# Shuts down the FTP device. Return status should be ignored. Note that since 897# we don't maintain an open connection to the FTP server there's nothing to do. 898# 899f_media_shutdown_ftp() 900{ 901 [ "$FTP_INITIALIZED" ] || return $SUCCESS 902 903 unset FTP_INITIALIZED 904} 905 906############################################################ MAIN 907 908f_dprintf "%s: Successfully loaded." media/ftp.subr 909 910fi # ! $_MEDIA_FTP_SUBR 911