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