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