Subject: bin/3643: ftp auto-fetch of directories fails when ftp_proxy is used
To: None <gnats-bugs@gnats.netbsd.org>
From: Alan Barrett <apb@iafrica.com>
List: netbsd-bugs
Date: 05/18/1997 12:30:51
>Number:         3643
>Category:       bin
>Synopsis:       ftp auto-fetch of directories fails when ftp_proxy is used
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    bin-bug-people (Utility Bug People)
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Sun May 18 03:35:02 1997
>Last-Modified:
>Originator:     Alan Barrett
>Organization:
not much
>Release:        NetBSD/i386-current 19970516
>Environment:
NetBSD/i386-current 19970516 (NetBSD 1.2E)
>Description:
	When the ftp_proxy environment variable is set,
	commands like

		ftp ftp://ftp.example.org/pub/

	fail.  ftp exits with status 1, but does not print any error.

	If the same command is attempted without the ftp_proxy
	environment variable being set, ftp connects to the specified
	host and changes to the specified directory as advertised.

>How-To-Repeat:
	Set your ftp_proxy environment variable to teh URL of an HTTP
	server that is willing to perform ftp proxying for you.  Then
	attempt to use the ftp(1) command to connect to an FTP site and
	cwd to a directory.  The ftp command will exit with an
	error status, but will not print any error message.

	shell$ ftp_proxy="http://www-proxy.example.org:8000/"
	shell$ export ftp_proxy
	shell$ ftp ftp://ftp.example.org/pub/ ; echo exit status $?
	exit status 1
	shell$

	Now try the same ftp command without the ftp_proxy environment
	variable being set.  The ftp command will connect to the remote
	host and cwd to the directory you specified.

	shell$ unset ftp_proxy
	shell$ ftp ftp://ftp.example.org/pub/ ; echo exit status $?
	Connected to ftp.example.org
	220 ftp.example.org FTP server ready
	331 Guest login ok, send your complete e-mail address as password.
	230 Guest login ok, access restrictions apply.
	Remote system type is UNIX.
	Using binary mode to transfer files.
	200 Type set to I.
	250 CWD command successful.
	250 CWD command successful.
	ftp> quit
	shell$

	A similar problem arises when using ftp to fetch HTTP URLs
	that do not contain a filename.  In cases like the following,
	ftp will exit with an error status but will not print an
	error message:

		ftp http://www.example.org
		ftp http://www.example.org/
		ftp http://www.example.org/directory/


>Fix:
	The following patch makes ftp ignore the ftp_proxy environment
	variable when it is asked to "auto-fetch" an FTP URL that ends
	with a "/".  It also adds some error messages for the HTTP URLs
	that don't work.

--- src/usr.bin/ftp/fetch.c	Tue Apr 22 13:23:16 1997
+++ src/usr.bin/ftp/fetch.c	Sun May 18 11:32:46 1997
@@ -104,19 +104,25 @@
 		errx(1, "url_get: invalid url '%s'", line);
 
 	path = strchr(host, '/');		/* find path */
-	if (EMPTYSTRING(path))
+	if (EMPTYSTRING(path)) {
+		warnx("url_get: missing '/' after hostname in URL: %s", line);
 		goto cleanup_url_get;
+	}
 	*path++ = '\0';
-	if (EMPTYSTRING(path))
+	if (EMPTYSTRING(path)) {
+		warnx("url_get: I don't like URLs that end so soon after the hostname: %s/", line);
 		goto cleanup_url_get;
+	}
 
 	savefile = strrchr(path, '/');			/* find savefile */
 	if (savefile != NULL)
 		savefile++;
 	else
 		savefile = path;
-	if (EMPTYSTRING(savefile))
+	if (EMPTYSTRING(savefile)) {
+		warnx("url_get: I don't like URLs that end with a '/': %s/%s", line, path);
 		goto cleanup_url_get;
+	}
 
 	if (proxyenv != NULL) {				/* use proxy */
 		proxy = strdup(proxyenv);
@@ -422,9 +428,13 @@
 		host = line;
 		if (strncasecmp(line, FTP_URL, sizeof(FTP_URL) - 1) == 0) {
 			if (ftpproxy) {
+			    if (line[strlen(line)-1] == '/') {
+				warnx("Not using ftp_proxy to get URL that ends with '/': %s", line);
+			    } else {
 				if (url_get(line, ftpproxy) == -1)
 					rval = argpos + 1;
 				continue;
+			    }
 			}
 			host += sizeof(FTP_URL) - 1;
 			dir = strchr(host, '/');
--- src/usr.bin/ftp/ftp.1	Mon Apr 14 13:30:03 1997
+++ src/usr.bin/ftp/ftp.1	Sun May 18 12:22:26 1997
@@ -1040,6 +1040,10 @@
 .Ic cd
 to the directory given as the path, and leave the user in interactive
 mode ready for further input.
+(This is done using the FTP protocol directly
+to the remote site, regardless of whether 
+.Ev ftp_proxy
+is defined.)
 .Pp
 If successive auto-fetch ftp elements refer to the same host, then
 the connection is maintained between transfers, reducing overhead on
>Audit-Trail:
>Unformatted: