Home | History | Annotate | Line # | Download | only in ftp
fetch.c revision 1.50
      1 /*	$NetBSD: fetch.c,v 1.50 1999/03/08 04:36:12 lukem Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997, 1998, 1999 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason Thorpe and Luke Mewburn.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 #ifndef lint
     41 __RCSID("$NetBSD: fetch.c,v 1.50 1999/03/08 04:36:12 lukem Exp $");
     42 #endif /* not lint */
     43 
     44 /*
     45  * FTP User Program -- Command line file retrieval
     46  */
     47 
     48 #include <sys/types.h>
     49 #include <sys/param.h>
     50 #include <sys/socket.h>
     51 #include <sys/stat.h>
     52 #include <sys/time.h>
     53 #include <sys/utsname.h>
     54 
     55 #include <netinet/in.h>
     56 
     57 #include <arpa/ftp.h>
     58 #include <arpa/inet.h>
     59 
     60 #include <ctype.h>
     61 #include <err.h>
     62 #include <errno.h>
     63 #include <netdb.h>
     64 #include <fcntl.h>
     65 #include <signal.h>
     66 #include <stdio.h>
     67 #include <stdlib.h>
     68 #include <string.h>
     69 #include <unistd.h>
     70 #include <util.h>
     71 
     72 #include "ftp_var.h"
     73 
     74 typedef enum {
     75 	UNKNOWN_URL_T=-1,
     76 	HTTP_URL_T,
     77 	FTP_URL_T,
     78 	FILE_URL_T
     79 } url_t;
     80 
     81 void    	aborthttp __P((int));
     82 static int	auth_url __P((const char *, char **));
     83 static void	base64_encode __P((const char *, size_t, char *));
     84 static int	go_fetch __P((const char *, const char *));
     85 static int	fetch_ftp __P((const char *, const char *));
     86 static int	fetch_url __P((const char *, const char *, const char *,
     87 				char *, char *));
     88 static int	parse_url __P((const char *, const char *, url_t *, char **,
     89 				char **, char **, in_port_t *, char **));
     90 static void	url_decode __P((char *));
     91 
     92 static int	redirect_loop;
     93 
     94 
     95 #define	ABOUT_URL	"about:"	/* propaganda */
     96 #define	FILE_URL	"file://"	/* file URL prefix */
     97 #define	FTP_URL		"ftp://"	/* ftp URL prefix */
     98 #define	HTTP_URL	"http://"	/* http URL prefix */
     99 
    100 
    101 #define EMPTYSTRING(x)	((x) == NULL || (*(x) == '\0'))
    102 #define FREEPTR(x)	if ((x) != NULL) { free(x); (x) = NULL; }
    103 
    104 /*
    105  * Generate authorization response based on given authentication challenge.
    106  * Returns -1 if an error occurred, otherwise 0.
    107  * Sets response to a malloc(3)ed string; caller should free.
    108  */
    109 static int
    110 auth_url(challenge, response)
    111 	const char	 *challenge;
    112 	char		**response;
    113 {
    114 	char		*cp, *ep, *clear, *line, *realm, *scheme;
    115 	char		user[BUFSIZ], *pass;
    116 	int		rval;
    117 	size_t		len;
    118 
    119 	*response = NULL;
    120 	clear = realm = scheme = NULL;
    121 	rval = -1;
    122 	line = xstrdup(challenge);
    123 	cp = line;
    124 
    125 	if (debug)
    126 		fprintf(ttyout, "auth_url: challenge `%s'\n", challenge);
    127 
    128 	scheme = strsep(&cp, " ");
    129 #define SCHEME_BASIC "Basic"
    130 	if (strncasecmp(scheme, SCHEME_BASIC, sizeof(SCHEME_BASIC) - 1) != 0) {
    131 		warnx("Unsupported WWW Authentication challenge - `%s'",
    132 		    challenge);
    133 		goto cleanup_auth_url;
    134 	}
    135 	cp += strspn(cp, " ");
    136 
    137 #define REALM "realm=\""
    138 	if (strncasecmp(cp, REALM, sizeof(REALM) - 1) == 0)
    139 		cp += sizeof(REALM) - 1;
    140 	else {
    141 		warnx("Unsupported WWW Authentication challenge - `%s'",
    142 		    challenge);
    143 		goto cleanup_auth_url;
    144 	}
    145 	if ((ep = strchr(cp, '\"')) != NULL) {
    146 		size_t len = ep - cp;
    147 
    148 		realm = (char *)xmalloc(len + 1);
    149 		strncpy(realm, cp, len);
    150 		realm[len] = '\0';
    151 	} else {
    152 		warnx("Unsupported WWW Authentication challenge - `%s'",
    153 		    challenge);
    154 		goto cleanup_auth_url;
    155 	}
    156 
    157 	fprintf(ttyout, "Username for `%s': ", realm);
    158 	(void)fflush(ttyout);
    159 	if (fgets(user, sizeof(user) - 1, stdin) == NULL)
    160 		goto cleanup_auth_url;
    161 	user[strlen(user) - 1] = '\0';
    162 	pass = getpass("Password: ");
    163 
    164 	len = strlen(user) + strlen(pass) + 1;		/* user + ":" + pass */
    165 	clear = (char *)xmalloc(len + 1);
    166 	sprintf(clear, "%s:%s", user, pass);
    167 	memset(pass, '\0', strlen(pass));
    168 
    169 						/* scheme + " " + enc */
    170 	len = strlen(scheme) + 1 + (len + 2) * 4 / 3;
    171 	*response = (char *)xmalloc(len + 1);
    172 	len = sprintf(*response, "%s ", scheme);
    173 	base64_encode(clear, strlen(clear), *response + len);
    174 	rval = 0;
    175 
    176 cleanup_auth_url:
    177 	FREEPTR(clear);
    178 	FREEPTR(line);
    179 	FREEPTR(realm);
    180 	return (rval);
    181 }
    182 
    183 /*
    184  * Encode len bytes starting at clear using base64 encoding into encoded,
    185  * which should be at least ((len + 2) * 4 / 3 + 1) in size.
    186  */
    187 void
    188 base64_encode(clear, len, encoded)
    189 	const char	*clear;
    190 	size_t		 len;
    191 	char		*encoded;
    192 {
    193 	static const char enc[] =
    194 	    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    195 	char *cp;
    196 	int i;
    197 
    198 	cp = encoded;
    199 	for (i = 0; i < len; i += 3) {
    200 		*(cp++) = enc[((clear[i + 0] >> 2))];
    201 		*(cp++) = enc[((clear[i + 0] << 4) & 0x30)
    202 			    | ((clear[i + 1] >> 4) & 0x0f)];
    203 		*(cp++) = enc[((clear[i + 1] << 2) & 0x3c)
    204 			    | ((clear[i + 2] >> 6) & 0x03)];
    205 		*(cp++) = enc[((clear[i + 2]     ) & 0x3f)];
    206 	}
    207 	*cp = '\0';
    208 	while (i-- > len)
    209 		*(--cp) = '=';
    210 }
    211 
    212 /*
    213  * Decode %xx escapes in given string, `in-place'.
    214  */
    215 static void
    216 url_decode(url)
    217 	char *url;
    218 {
    219 	unsigned char *p, *q;
    220 
    221 	if (EMPTYSTRING(url))
    222 		return;
    223 	p = q = url;
    224 
    225 #define HEXTOINT(x) (x - (isdigit(x) ? '0' : (islower(x) ? 'a' : 'A') - 10))
    226 	while (*p) {
    227 		if (p[0] == '%'
    228 		    && p[1] && isxdigit((unsigned char)p[1])
    229 		    && p[2] && isxdigit((unsigned char)p[2])) {
    230 			*q++ = HEXTOINT(p[1]) * 16 + HEXTOINT(p[2]);
    231 			p+=3;
    232 		} else
    233 			*q++ = *p++;
    234 	}
    235 	*q = '\0';
    236 }
    237 
    238 
    239 /*
    240  * Parse URL of form:
    241  *	<type>://[<user>[:<password>@]]<host>[:<port>]/<url-path>
    242  * Returns -1 if a parse error occurred, otherwise 0.
    243  * Only permit [<user>[:<password>@]] for ftp:// URLs
    244  * It's the caller's responsibility to url_decode() the returned
    245  * user, pass and path.
    246  * Sets type to url_t, each of the given char ** pointers to a
    247  * malloc(3)ed strings of the relevant section, and port to
    248  * the number given, or ftpport if ftp://, or httpport if http://.
    249  */
    250 static int
    251 parse_url(url, desc, type, user, pass, host, port, path)
    252 	const char	 *url;
    253 	const char	 *desc;
    254 	url_t		 *type;
    255 	char		**user;
    256 	char		**pass;
    257 	char		**host;
    258 	in_port_t	 *port;
    259 	char		**path;
    260 {
    261 	char *cp, *ep, *thost;
    262 	size_t len;
    263 
    264 	if (url == NULL || desc == NULL || type == NULL || user == NULL
    265 	    || pass == NULL || host == NULL || port == NULL || path == NULL)
    266 		errx(1, "parse_url: invoked with NULL argument!");
    267 
    268 	*type = UNKNOWN_URL_T;
    269 	*user = *pass = *host = *path = NULL;
    270 	*port = 0;
    271 
    272 	if (strncasecmp(url, HTTP_URL, sizeof(HTTP_URL) - 1) == 0) {
    273 		url += sizeof(HTTP_URL) - 1;
    274 		*type = HTTP_URL_T;
    275 		*port = httpport;
    276 	} else if (strncasecmp(url, FTP_URL, sizeof(FTP_URL) - 1) == 0) {
    277 		url += sizeof(FTP_URL) - 1;
    278 		*type = FTP_URL_T;
    279 		*port = ftpport;
    280 	} else if (strncasecmp(url, FILE_URL, sizeof(FILE_URL) - 1) == 0) {
    281 		url += sizeof(FILE_URL) - 1;
    282 		*type = FILE_URL_T;
    283 	} else {
    284 		warnx("Invalid %s `%s'", desc, url);
    285 cleanup_parse_url:
    286 		FREEPTR(*user);
    287 		FREEPTR(*pass);
    288 		FREEPTR(*host);
    289 		FREEPTR(*path);
    290 		return (-1);
    291 	}
    292 
    293 	if (*url == '\0')
    294 		return (0);
    295 
    296 			/* find [user[:pass]@]host[:port] */
    297 	ep = strchr(url, '/');
    298 	if (ep == NULL)
    299 		thost = xstrdup(url);
    300 	else {
    301 		len = ep - url;
    302 		thost = (char *)xmalloc(len + 1);
    303 		strncpy(thost, url, len);
    304 		thost[len] = '\0';
    305 		ep++;			/* skip first / for all URLs */
    306 		if (*type == FTP_URL_T)	/* skip all leading /'s for ftp URLs */
    307 			while (*ep && *ep == '/')
    308 				ep++;
    309 		*path = xstrdup(ep);
    310 	}
    311 
    312 	cp = strchr(thost, '@');
    313 					/* look for user[:pass]@ in ftp URLs */
    314 	if (*type == FTP_URL_T && cp != NULL) {
    315 		anonftp = 0;		/* disable anonftp */
    316 		*user = thost;
    317 		*cp = '\0';
    318 		*host = xstrdup(cp + 1);
    319 		cp = strchr(*user, ':');
    320 		if (cp != NULL) {
    321 			*cp = '\0';
    322 			*pass = xstrdup(cp + 1);
    323 		}
    324 	} else
    325 		*host = thost;
    326 
    327 			/* look for [:port] */
    328 	cp = strrchr(*host, ':');
    329 	if (cp != NULL) {
    330 		long nport;
    331 
    332 		*cp = '\0';
    333 		nport = strtol(cp + 1, &ep, 10);
    334 		if (nport < 1 || nport > MAX_IN_PORT_T || *ep != '\0') {
    335 			warnx("Invalid port `%s' in %s `%s'", cp, desc, url);
    336 			goto cleanup_parse_url;
    337 		}
    338 		*port = htons((in_port_t)nport);
    339 	}
    340 
    341 	if (debug)
    342 		fprintf(ttyout,
    343 		    "parse_url: user `%s' pass `%s' host %s:%d path `%s'\n",
    344 		    *user ? *user : "", *pass ? *pass : "", *host ? *host : "",
    345 		    ntohs(*port), *path ? *path : "");
    346 
    347 	return (0);
    348 }
    349 
    350 
    351 jmp_buf	httpabort;
    352 
    353 /*
    354  * Retrieve URL, via a proxy if necessary, using HTTP.
    355  * If proxyenv is set, use that for the proxy, otherwise try ftp_proxy or
    356  * http_proxy as appropriate.
    357  * Supports HTTP redirects.
    358  * Returns -1 on failure, 0 on completed xfer, 1 if ftp connection
    359  * is still open (e.g, ftp xfer with trailing /)
    360  */
    361 static int
    362 fetch_url(url, outfile, proxyenv, proxyauth, wwwauth)
    363 	const char	*url;
    364 	const char	*outfile;
    365 	const char	*proxyenv;
    366 	char		*proxyauth;
    367 	char		*wwwauth;
    368 {
    369 	struct sockaddr_in	sin;
    370 	struct hostent		*hp;
    371 	volatile sig_t		oldintr, oldintp;
    372 	volatile int		s;
    373 	int 			ischunked, isproxy, rval, hcode;
    374 	size_t			len;
    375 	char			*cp, *ep, *buf, *savefile;
    376 	char			*auth, *location, *message;
    377 	char			*user, *pass, *host, *path, *decodedpath;
    378 	off_t			hashbytes;
    379 	int			 (*closefunc) __P((FILE *));
    380 	FILE			*fin, *fout;
    381 	time_t			mtime;
    382 	url_t			urltype;
    383 	in_port_t		port;
    384 
    385 	closefunc = NULL;
    386 	fin = fout = NULL;
    387 	s = -1;
    388 	buf = savefile = NULL;
    389 	auth = location = message = NULL;
    390 	ischunked = isproxy = 0;
    391 	rval = 1;
    392 	hp = NULL;
    393 	user = pass = host = path = decodedpath = NULL;
    394 
    395 #ifdef __GNUC__			/* shut up gcc warnings */
    396 	(void)&closefunc;
    397 	(void)&fin;
    398 	(void)&fout;
    399 	(void)&buf;
    400 	(void)&savefile;
    401 	(void)&rval;
    402 	(void)&isproxy;
    403 #endif
    404 
    405 	if (parse_url(url, "URL", &urltype, &user, &pass, &host, &port, &path)
    406 	    == -1)
    407 		goto cleanup_fetch_url;
    408 
    409 	if (urltype == FILE_URL_T && ! EMPTYSTRING(host)
    410 	    && strcasecmp(host, "localhost") != 0) {
    411 		warnx("No support for non local file URL `%s'", url);
    412 		goto cleanup_fetch_url;
    413 	}
    414 
    415 	if (EMPTYSTRING(path)) {
    416 		if (urltype == FTP_URL_T) {
    417 			rval = fetch_ftp(url, outfile);
    418 			goto cleanup_fetch_url;
    419 		}
    420 		if (urltype != HTTP_URL_T || outfile == NULL)  {
    421 			warnx("Invalid URL (no file after host) `%s'", url);
    422 			goto cleanup_fetch_url;
    423 		}
    424 	}
    425 
    426 	decodedpath = xstrdup(path);
    427 	url_decode(decodedpath);
    428 
    429 	if (outfile)
    430 		savefile = xstrdup(outfile);
    431 	else {
    432 		cp = strrchr(decodedpath, '/');		/* find savefile */
    433 		if (cp != NULL)
    434 			savefile = xstrdup(cp + 1);
    435 		else
    436 			savefile = xstrdup(decodedpath);
    437 	}
    438 	if (EMPTYSTRING(savefile)) {
    439 		if (urltype == FTP_URL_T) {
    440 			rval = fetch_ftp(url, outfile);
    441 			goto cleanup_fetch_url;
    442 		}
    443 		warnx("Invalid URL (no file after directory) `%s'", url);
    444 		goto cleanup_fetch_url;
    445 	}
    446 
    447 	filesize = -1;
    448 	mtime = -1;
    449 	if (urltype == FILE_URL_T) {		/* file:// URLs */
    450 		struct stat sb;
    451 
    452 		direction = "copied";
    453 		fin = fopen(decodedpath, "r");
    454 		if (fin == NULL) {
    455 			warn("Cannot open file `%s'", decodedpath);
    456 			goto cleanup_fetch_url;
    457 		}
    458 		if (fstat(fileno(fin), &sb) == 0) {
    459 			mtime = sb.st_mtime;
    460 			filesize = sb.st_size;
    461 		}
    462 		if (verbose)
    463 			fprintf(ttyout, "Copying %s\n", decodedpath);
    464 	} else {				/* ftp:// or http:// URLs */
    465 		if (proxyenv == NULL) {
    466 			if (urltype == HTTP_URL_T)
    467 				proxyenv = httpproxy;
    468 			else if (urltype == FTP_URL_T)
    469 				proxyenv = ftpproxy;
    470 		}
    471 		direction = "retrieved";
    472 		if (proxyenv != NULL) {				/* use proxy */
    473 			url_t purltype;
    474 			char *puser, *ppass, *phost;
    475 			char *ppath;
    476 
    477 			isproxy = 1;
    478 
    479 				/* check URL against list of no_proxied sites */
    480 			if (no_proxy != NULL) {
    481 				char *np, *np_copy;
    482 				long np_port;
    483 				size_t hlen, plen;
    484 
    485 				np_copy = xstrdup(no_proxy);
    486 				hlen = strlen(host);
    487 				while ((cp = strsep(&np_copy, " ,")) != NULL) {
    488 					if (*cp == '\0')
    489 						continue;
    490 					if ((np = strchr(cp, ':')) != NULL) {
    491 						*np = '\0';
    492 						np_port =
    493 						    strtol(np + 1, &ep, 10);
    494 						if (*ep != '\0')
    495 							continue;
    496 						if (port !=
    497 						    htons((in_port_t)np_port))
    498 							continue;
    499 					}
    500 					plen = strlen(cp);
    501 					if (strncasecmp(host + hlen - plen,
    502 					    cp, plen) == 0) {
    503 						isproxy = 0;
    504 						break;
    505 					}
    506 				}
    507 				FREEPTR(np_copy);
    508 			}
    509 
    510 			if (isproxy) {
    511 				if (parse_url(proxyenv, "proxy URL", &purltype,
    512 				    &puser, &ppass, &phost, &port, &ppath)
    513 				    == -1)
    514 					goto cleanup_fetch_url;
    515 
    516 				if ((purltype != HTTP_URL_T
    517 				     && purltype != FTP_URL_T) ||
    518 				    EMPTYSTRING(phost) ||
    519 				    (! EMPTYSTRING(ppath)
    520 				     && strcmp(ppath, "/") != 0)) {
    521 					warnx("Malformed proxy URL `%s'",
    522 					    proxyenv);
    523 					FREEPTR(puser);
    524 					FREEPTR(ppass);
    525 					FREEPTR(phost);
    526 					FREEPTR(ppath);
    527 					goto cleanup_fetch_url;
    528 				}
    529 
    530 				FREEPTR(user);
    531 				user = puser;
    532 				FREEPTR(pass);
    533 				pass = ppass;
    534 				FREEPTR(host);
    535 				host = phost;
    536 				FREEPTR(path);
    537 				FREEPTR(ppath);
    538 				path = xstrdup(url);
    539 			}
    540 		} /* proxyenv != NULL */
    541 
    542 		memset(&sin, 0, sizeof(sin));
    543 		sin.sin_family = AF_INET;
    544 
    545 		if (isdigit((unsigned char)host[0])) {
    546 			if (inet_aton(host, &sin.sin_addr) == 0) {
    547 				warnx("Invalid IP address `%s'", host);
    548 				goto cleanup_fetch_url;
    549 			}
    550 		} else {
    551 			hp = gethostbyname(host);
    552 			if (hp == NULL) {
    553 				warnx("%s: %s", host, hstrerror(h_errno));
    554 				goto cleanup_fetch_url;
    555 			}
    556 			if (hp->h_addrtype != AF_INET) {
    557 				warnx("`%s': not an Internet address?", host);
    558 				goto cleanup_fetch_url;
    559 			}
    560 			memcpy(&sin.sin_addr, hp->h_addr, hp->h_length);
    561 		}
    562 
    563 		if (port == 0) {
    564 			warnx("Unknown port for URL `%s'", url);
    565 			goto cleanup_fetch_url;
    566 		}
    567 		sin.sin_port = port;
    568 
    569 		s = socket(AF_INET, SOCK_STREAM, 0);
    570 		if (s == -1) {
    571 			warn("Can't create socket");
    572 			goto cleanup_fetch_url;
    573 		}
    574 
    575 		while (xconnect(s, (struct sockaddr *)&sin,
    576 		    sizeof(sin)) == -1) {
    577 			if (errno == EINTR)
    578 				continue;
    579 			if (hp && hp->h_addr_list[1]) {
    580 				int oerrno = errno;
    581 				char *ia;
    582 
    583 				ia = inet_ntoa(sin.sin_addr);
    584 				errno = oerrno;
    585 				warn("Connect to address `%s'", ia);
    586 				hp->h_addr_list++;
    587 				memcpy(&sin.sin_addr, hp->h_addr_list[0],
    588 				    (size_t)hp->h_length);
    589 				if (verbose)
    590 					fprintf(ttyout, "Trying %s...\n",
    591 					    inet_ntoa(sin.sin_addr));
    592 				(void)close(s);
    593 				s = socket(AF_INET, SOCK_STREAM, 0);
    594 				if (s < 0) {
    595 					warn("Can't create socket");
    596 					goto cleanup_fetch_url;
    597 				}
    598 				continue;
    599 			}
    600 			warn("Can't connect to `%s'", host);
    601 			goto cleanup_fetch_url;
    602 		}
    603 
    604 		fin = fdopen(s, "r+");
    605 		/*
    606 		 * Construct and send the request.
    607 		 * Proxy requests don't want leading /.
    608 		 */
    609 		if (isproxy) {
    610 			if (verbose)
    611 				fprintf(ttyout, "Requesting %s\n  (via %s)\n",
    612 				    url, proxyenv);
    613 			fprintf(fin, "GET %s HTTP/1.0\r\n", path);
    614 			if (flushcache)
    615 				fprintf(fin, "Pragma: no-cache\r\n");
    616 		} else {
    617 			struct utsname unam;
    618 
    619 			if (verbose)
    620 				fprintf(ttyout, "Requesting %s\n", url);
    621 			fprintf(fin, "GET %s HTTP/1.1\r\n", path);
    622 			fprintf(fin, "Host: %s:%d\r\n", host, ntohs(port));
    623 			fprintf(fin, "Accept: */*\r\n");
    624 			if (uname(&unam) != -1) {
    625 				fprintf(fin, "User-Agent: %s-%s/ftp\r\n",
    626 				    unam.sysname, unam.release);
    627 			}
    628 			fprintf(fin, "Connection: close\r\n");
    629 			if (flushcache)
    630 				fprintf(fin, "Cache-Control: no-cache\r\n");
    631 		}
    632 		if (wwwauth) {
    633 			if (verbose)
    634 				fprintf(ttyout, "  (with authorization)\n");
    635 			fprintf(fin, "Authorization: %s\r\n", wwwauth);
    636 		}
    637 		if (proxyauth) {
    638 			if (verbose)
    639 				fprintf(ttyout,
    640 				    "  (with proxy authorization)\n");
    641 			fprintf(fin, "Proxy-Authorization: %s\r\n", proxyauth);
    642 		}
    643 		fprintf(fin, "\r\n");
    644 		if (fflush(fin) == EOF) {
    645 			warn("Writing HTTP request");
    646 			goto cleanup_fetch_url;
    647 		}
    648 
    649 				/* Read the response */
    650 		if ((buf = fparseln(fin, &len, NULL, "\0\0\0", 0)) == NULL) {
    651 			warn("Receiving HTTP reply");
    652 			goto cleanup_fetch_url;
    653 		}
    654 		while (len > 0 && (buf[len-1] == '\r' || buf[len-1] == '\n'))
    655 			buf[--len] = '\0';
    656 		if (debug)
    657 			fprintf(ttyout, "received `%s'\n", buf);
    658 
    659 				/* Determine HTTP response code */
    660 		cp = strchr(buf, ' ');
    661 		if (cp == NULL)
    662 			goto improper;
    663 		else
    664 			cp++;
    665 		hcode = strtol(cp, &ep, 10);
    666 		if (*ep != '\0' && !isspace((unsigned char)*ep))
    667 			goto improper;
    668 		message = xstrdup(cp);
    669 
    670 				/* Read the rest of the header. */
    671 		FREEPTR(buf);
    672 		while (1) {
    673 			if ((buf = fparseln(fin, &len, NULL, "\0\0\0", 0))
    674 			    == NULL) {
    675 				warn("Receiving HTTP reply");
    676 				goto cleanup_fetch_url;
    677 			}
    678 			while (len > 0 &&
    679 			    (buf[len-1] == '\r' || buf[len-1] == '\n'))
    680 				buf[--len] = '\0';
    681 			if (len == 0)
    682 				break;
    683 			if (debug)
    684 				fprintf(ttyout, "received `%s'\n", buf);
    685 
    686 				/* Look for some headers */
    687 			cp = buf;
    688 
    689 #define CONTENTLEN "Content-Length: "
    690 			if (strncasecmp(cp, CONTENTLEN,
    691 					sizeof(CONTENTLEN) - 1) == 0) {
    692 				cp += sizeof(CONTENTLEN) - 1;
    693 				filesize = strtol(cp, &ep, 10);
    694 				if (filesize < 1 || *ep != '\0')
    695 					goto improper;
    696 				if (debug)
    697 					fprintf(ttyout,
    698 #ifndef NO_QUAD
    699 					    "parsed length as: %qd\n",
    700 					    (long long)filesize);
    701 #else
    702 					    "parsed length as: %ld\n",
    703 					    (long)filesize);
    704 #endif
    705 
    706 #define LASTMOD "Last-Modified: "
    707 			} else if (strncasecmp(cp, LASTMOD,
    708 						sizeof(LASTMOD) - 1) == 0) {
    709 				struct tm parsed;
    710 				char *t;
    711 
    712 				cp += sizeof(LASTMOD) - 1;
    713 							/* RFC 1123 */
    714 				if ((t = strptime(cp,
    715 						"%a, %d %b %Y %H:%M:%S GMT",
    716 						&parsed))
    717 							/* RFC 850 */
    718 				    || (t = strptime(cp,
    719 						"%a, %d-%b-%y %H:%M:%S GMT",
    720 						&parsed))
    721 							/* asctime */
    722 				    || (t = strptime(cp,
    723 						"%a, %b %d %H:%M:%S %Y",
    724 						&parsed))) {
    725 					parsed.tm_isdst = -1;
    726 					if (*t == '\0')
    727 						mtime = mkgmtime(&parsed);
    728 					if (debug && mtime != -1) {
    729 						fprintf(ttyout,
    730 						    "parsed date as: %s",
    731 						    ctime(&mtime));
    732 					}
    733 				}
    734 
    735 #define LOCATION "Location: "
    736 			} else if (strncasecmp(cp, LOCATION,
    737 						sizeof(LOCATION) - 1) == 0) {
    738 				cp += sizeof(LOCATION) - 1;
    739 				location = xstrdup(cp);
    740 				if (debug)
    741 					fprintf(ttyout,
    742 					    "parsed location as: %s\n", cp);
    743 
    744 #define TRANSENC "Transfer-Encoding: "
    745 			} else if (strncasecmp(cp, TRANSENC,
    746 						sizeof(TRANSENC) - 1) == 0) {
    747 				cp += sizeof(TRANSENC) - 1;
    748 				if (strcasecmp(cp, "chunked") != 0) {
    749 					warnx(
    750 				    "Unsupported transfer encoding - `%s'",
    751 					    cp);
    752 					goto cleanup_fetch_url;
    753 				}
    754 				ischunked++;
    755 				if (debug)
    756 					fprintf(ttyout,
    757 					    "using chunked encoding\n");
    758 
    759 #define PROXYAUTH "Proxy-Authenticate: "
    760 			} else if (strncasecmp(cp, PROXYAUTH,
    761 						sizeof(PROXYAUTH) - 1) == 0) {
    762 				cp += sizeof(PROXYAUTH) - 1;
    763 				FREEPTR(auth);
    764 				auth = xstrdup(cp);
    765 				if (debug)
    766 					fprintf(ttyout,
    767 					    "parsed proxy-auth as: %s\n", cp);
    768 
    769 #define WWWAUTH	"WWW-Authenticate: "
    770 			} else if (strncasecmp(cp, WWWAUTH,
    771 			    sizeof(WWWAUTH) - 1) == 0) {
    772 				cp += sizeof(WWWAUTH) - 1;
    773 				FREEPTR(auth);
    774 				auth = xstrdup(cp);
    775 				if (debug)
    776 					fprintf(ttyout,
    777 					    "parsed www-auth as: %s\n", cp);
    778 
    779 			}
    780 
    781 		}
    782 		FREEPTR(buf);
    783 	}
    784 
    785 	switch (hcode) {
    786 	case 200:
    787 		break;
    788 	case 300:
    789 	case 301:
    790 	case 302:
    791 	case 303:
    792 	case 305:
    793 		if (EMPTYSTRING(location)) {
    794 			warnx("No redirection Location provided by server");
    795 			goto cleanup_fetch_url;
    796 		}
    797 		if (redirect_loop++ > 5) {
    798 			warnx("Too many redirections requested");
    799 			goto cleanup_fetch_url;
    800 		}
    801 		if (hcode == 305) {
    802 			if (verbose)
    803 				fprintf(ttyout, "Redirected via %s\n",
    804 				    location);
    805 			rval = fetch_url(url, outfile, location, proxyauth,
    806 			    wwwauth);
    807 		} else {
    808 			if (verbose)
    809 				fprintf(ttyout, "Redirected to %s\n", location);
    810 			rval = go_fetch(location, outfile);
    811 		}
    812 		goto cleanup_fetch_url;
    813 	case 401:
    814 	case 407:
    815 	    {
    816 		char **authp;
    817 
    818 		fprintf(ttyout, "%s\n", message);
    819 		if (EMPTYSTRING(auth)) {
    820 			warnx("No authentication challenge provided by server");
    821 			goto cleanup_fetch_url;
    822 		}
    823 		authp = (hcode == 401) ? &wwwauth : &proxyauth;
    824 		if (*authp != NULL) {
    825 			char reply[10];
    826 
    827 			fprintf(ttyout, "Authorization failed. Retry (y/n)? ");
    828 			if (fgets(reply, sizeof(reply), stdin) != NULL &&
    829 			    tolower(reply[0]) != 'y')
    830 				goto cleanup_fetch_url;
    831 		}
    832 		if (auth_url(auth, authp) == 0) {
    833 			rval = fetch_url(url, outfile, proxyenv, proxyauth,
    834 			    wwwauth);
    835 			memset(*authp, '\0', strlen(*authp));
    836 			FREEPTR(*authp);
    837 		}
    838 		goto cleanup_fetch_url;
    839 	    }
    840 	default:
    841 		warnx("Error retrieving file - `%s'", message);
    842 		goto cleanup_fetch_url;
    843 	}
    844 
    845 	oldintr = oldintp = NULL;
    846 
    847 			/* Open the output file. */
    848 	if (strcmp(savefile, "-") == 0) {
    849 		fout = stdout;
    850 	} else if (*savefile == '|') {
    851 		oldintp = signal(SIGPIPE, SIG_IGN);
    852 		fout = popen(savefile + 1, "w");
    853 		if (fout == NULL) {
    854 			warn("Can't run `%s'", savefile + 1);
    855 			goto cleanup_fetch_url;
    856 		}
    857 		closefunc = pclose;
    858 	} else {
    859 		fout = fopen(savefile, "w");
    860 		if (fout == NULL) {
    861 			warn("Can't open `%s'", savefile);
    862 			goto cleanup_fetch_url;
    863 		}
    864 		closefunc = fclose;
    865 	}
    866 
    867 			/* Trap signals */
    868 	if (setjmp(httpabort)) {
    869 		if (oldintr)
    870 			(void)signal(SIGINT, oldintr);
    871 		if (oldintp)
    872 			(void)signal(SIGPIPE, oldintp);
    873 		goto cleanup_fetch_url;
    874 	}
    875 	oldintr = signal(SIGINT, aborthttp);
    876 
    877 	bytes = 0;
    878 	hashbytes = mark;
    879 	progressmeter(-1);
    880 
    881 			/* Finally, suck down the file. */
    882 	buf = xmalloc(BUFSIZ + 1);
    883 	do {
    884 		ssize_t chunksize;
    885 
    886 		chunksize = 0;
    887 					/* read chunksize */
    888 		if (ischunked) {
    889 			if (fgets(buf, BUFSIZ, fin) == NULL) {
    890 				warnx("Unexpected EOF reading chunksize");
    891 				goto cleanup_fetch_url;
    892 			}
    893 			chunksize = strtol(buf, &ep, 16);
    894 			if (strcmp(ep, "\r\n") != 0) {
    895 				warnx("Unexpected data following chunksize");
    896 				goto cleanup_fetch_url;
    897 			}
    898 			if (debug)
    899 				fprintf(ttyout, "got chunksize of %qd\n",
    900 				    (long long)chunksize);
    901 			if (chunksize == 0)
    902 				break;
    903 		}
    904 		while ((len = fread(buf, sizeof(char),
    905 		    ischunked ? MIN(chunksize, BUFSIZ) : BUFSIZ, fin)) > 0) {
    906 			bytes += len;
    907 			if (fwrite(buf, sizeof(char), len, fout) != len) {
    908 				warn("Writing `%s'", savefile);
    909 				goto cleanup_fetch_url;
    910 			}
    911 			if (hash && !progress) {
    912 				while (bytes >= hashbytes) {
    913 					(void)putc('#', ttyout);
    914 					hashbytes += mark;
    915 				}
    916 				(void)fflush(ttyout);
    917 			}
    918 			if (ischunked)
    919 				chunksize -= len;
    920 		}
    921 					/* read CRLF after chunk*/
    922 		if (ischunked) {
    923 			if (fgets(buf, BUFSIZ, fin) == NULL)
    924 				break;
    925 			if (strcmp(buf, "\r\n") != 0) {
    926 				warnx("Unexpected data following chunk");
    927 				goto cleanup_fetch_url;
    928 			}
    929 		}
    930 	} while (ischunked);
    931 	if (hash && !progress && bytes > 0) {
    932 		if (bytes < mark)
    933 			(void)putc('#', ttyout);
    934 		(void)putc('\n', ttyout);
    935 	}
    936 	if (ferror(fin)) {
    937 		warn("Reading file");
    938 		goto cleanup_fetch_url;
    939 	}
    940 	progressmeter(1);
    941 	(void)fflush(fout);
    942 	(void)signal(SIGINT, oldintr);
    943 	if (oldintp)
    944 		(void)signal(SIGPIPE, oldintp);
    945 	if (closefunc == fclose && mtime != -1) {
    946 		struct timeval tval[2];
    947 
    948 		(void)gettimeofday(&tval[0], NULL);
    949 		tval[1].tv_sec = mtime;
    950 		tval[1].tv_usec = 0;
    951 		(*closefunc)(fout);
    952 		fout = NULL;
    953 
    954 		if (utimes(savefile, tval) == -1) {
    955 			fprintf(ttyout,
    956 			    "Can't change modification time to %s",
    957 			    asctime(localtime(&mtime)));
    958 		}
    959 	}
    960 	if (bytes > 0)
    961 		ptransfer(0);
    962 
    963 	rval = 0;
    964 	goto cleanup_fetch_url;
    965 
    966 improper:
    967 	warnx("Improper response from `%s'", host);
    968 
    969 cleanup_fetch_url:
    970 	resetsockbufsize();
    971 	if (fin != NULL)
    972 		fclose(fin);
    973 	else if (s != -1)
    974 		close(s);
    975 	if (closefunc != NULL && fout != NULL)
    976 		(*closefunc)(fout);
    977 	FREEPTR(savefile);
    978 	FREEPTR(user);
    979 	FREEPTR(pass);
    980 	FREEPTR(host);
    981 	FREEPTR(path);
    982 	FREEPTR(decodedpath);
    983 	FREEPTR(buf);
    984 	FREEPTR(auth);
    985 	FREEPTR(location);
    986 	FREEPTR(message);
    987 	return (rval);
    988 }
    989 
    990 /*
    991  * Abort a HTTP retrieval
    992  */
    993 void
    994 aborthttp(notused)
    995 	int notused;
    996 {
    997 
    998 	alarmtimer(0);
    999 	fputs("\nHTTP fetch aborted.\n", ttyout);
   1000 	longjmp(httpabort, 1);
   1001 }
   1002 
   1003 /*
   1004  * Retrieve ftp URL or classic ftp argument using FTP.
   1005  * Returns -1 on failure, 0 on completed xfer, 1 if ftp connection
   1006  * is still open (e.g, ftp xfer with trailing /)
   1007  */
   1008 static int
   1009 fetch_ftp(url, outfile)
   1010 	const char *url;
   1011 	const char *outfile;
   1012 {
   1013 	char		*cp, *xargv[5], rempath[MAXPATHLEN];
   1014 	char		portnum[6];		/* large enough for "65535\0" */
   1015 	char		*host, *path, *dir, *file, *user, *pass;
   1016 	in_port_t	port;
   1017 	int		dirhasglob, filehasglob, oautologin, rval, xargc;
   1018 
   1019 	host = path = dir = file = user = pass = NULL;
   1020 	port = 0;
   1021 	rval = 1;
   1022 
   1023 	if (strncasecmp(url, FTP_URL, sizeof(FTP_URL) - 1) == 0) {
   1024 		url_t urltype;
   1025 
   1026 		if ((parse_url(url, "URL", &urltype, &user, &pass,
   1027 		    &host, &port, &path) == -1) ||
   1028 		    (user != NULL && *user == '\0') ||
   1029 		    (pass != NULL && *pass == '\0') ||
   1030 		    EMPTYSTRING(host)) {
   1031 			warnx("Invalid URL `%s'", url);
   1032 			goto cleanup_fetch_ftp;
   1033 		}
   1034 		url_decode(user);
   1035 		url_decode(pass);
   1036 		url_decode(path);
   1037 	} else {			/* classic style `host:file' */
   1038 		host = xstrdup(url);
   1039 		cp = strchr(host, ':');
   1040 		if (cp != NULL) {
   1041 			*cp = '\0';
   1042 			path = xstrdup(cp + 1);
   1043 		}
   1044 	}
   1045 	if (EMPTYSTRING(host))
   1046 		goto cleanup_fetch_ftp;
   1047 
   1048 			/* Extract the file and (if present) directory name. */
   1049 	dir = path;
   1050 	if (! EMPTYSTRING(dir)) {
   1051 		cp = strrchr(dir, '/');
   1052 		if (cp == dir) {
   1053 			file = cp + 1;
   1054 			dir = "/";
   1055 		} else if (cp != NULL) {
   1056 			*cp++ = '\0';
   1057 			file = cp;
   1058 		} else {
   1059 			file = dir;
   1060 			dir = NULL;
   1061 		}
   1062 	}
   1063 	if (debug)
   1064 		fprintf(ttyout,
   1065     "fetch_ftp: user `%s' pass `%s' host %s:%d path `%s' dir `%s' file `%s'\n",
   1066 		    user ? user : "", pass ? pass : "",
   1067 		    host ? host : "", ntohs(port), path ? path : "",
   1068 		    dir ? dir : "", file ? file : "");
   1069 
   1070 	dirhasglob = filehasglob = 0;
   1071 	if (doglob) {
   1072 		if (! EMPTYSTRING(dir) && strpbrk(dir, "*?[]{}") != NULL)
   1073 			dirhasglob = 1;
   1074 		if (! EMPTYSTRING(file) && strpbrk(file, "*?[]{}") != NULL)
   1075 			filehasglob = 1;
   1076 	}
   1077 
   1078 			/* Set up the connection */
   1079 	if (connected)
   1080 		disconnect(0, NULL);
   1081 	xargv[0] = __progname;
   1082 	xargv[1] = host;
   1083 	xargv[2] = NULL;
   1084 	xargc = 2;
   1085 	if (port) {
   1086 		snprintf(portnum, sizeof(portnum), "%d", ntohs(port));
   1087 		xargv[2] = portnum;
   1088 		xargv[3] = NULL;
   1089 		xargc = 3;
   1090 	}
   1091 	oautologin = autologin;
   1092 	if (user != NULL)
   1093 		autologin = 0;
   1094 	setpeer(xargc, xargv);
   1095 	autologin = oautologin;
   1096 	if ((connected == 0) || ((connected == 1)
   1097 	    && !ftp_login(host, user, pass))) {
   1098 		warnx("Can't connect or login to host `%s'", host);
   1099 		goto cleanup_fetch_ftp;
   1100 	}
   1101 
   1102 			/* Always use binary transfers. */
   1103 	setbinary(0, NULL);
   1104 
   1105 			/* Change directories, if necessary. */
   1106 	if (! EMPTYSTRING(dir) && !dirhasglob) {
   1107 		xargv[0] = "cd";
   1108 		xargv[1] = dir;
   1109 		xargv[2] = NULL;
   1110 		dirchange = 0;
   1111 		cd(2, xargv);
   1112 		if (! dirchange)
   1113 			goto cleanup_fetch_ftp;
   1114 	}
   1115 
   1116 	if (EMPTYSTRING(file)) {
   1117 		rval = -1;
   1118 		goto cleanup_fetch_ftp;
   1119 	}
   1120 
   1121 	if (dirhasglob) {
   1122 		snprintf(rempath, sizeof(rempath), "%s/%s", dir, file);
   1123 		file = rempath;
   1124 	}
   1125 
   1126 			/* Fetch the file(s). */
   1127 	xargc = 2;
   1128 	xargv[0] = "get";
   1129 	xargv[1] = file;
   1130 	xargv[2] = NULL;
   1131 	if (dirhasglob || filehasglob) {
   1132 		int ointeractive;
   1133 
   1134 		ointeractive = interactive;
   1135 		interactive = 0;
   1136 		xargv[0] = "mget";
   1137 		mget(xargc, xargv);
   1138 		interactive = ointeractive;
   1139 	} else {
   1140 		if (outfile != NULL) {
   1141 			xargv[2] = (char *)outfile;
   1142 			xargv[3] = NULL;
   1143 			xargc++;
   1144 		}
   1145 		get(xargc, xargv);
   1146 		if (outfile != NULL && strcmp(outfile, "-") != 0
   1147 		    && outfile[0] != '|')
   1148 			outfile = NULL;
   1149 	}
   1150 
   1151 	if ((code / 100) == COMPLETE)
   1152 		rval = 0;
   1153 
   1154 cleanup_fetch_ftp:
   1155 	FREEPTR(host);
   1156 	FREEPTR(path);
   1157 	FREEPTR(user);
   1158 	FREEPTR(pass);
   1159 	return (rval);
   1160 }
   1161 
   1162 /*
   1163  * Retrieve the given file to outfile.
   1164  * Supports arguments of the form:
   1165  *	"host:path", "ftp://host/path"	if $ftpproxy, call fetch_url() else
   1166  *					call fetch_ftp()
   1167  *	"http://host/path"		call fetch_url() to use HTTP
   1168  *	"file:///path"			call fetch_url() to copy
   1169  *	"about:..."			print a message
   1170  *
   1171  * Returns 1 on failure, 0 on completed xfer, -1 if ftp connection
   1172  * is still open (e.g, ftp xfer with trailing /)
   1173  */
   1174 static int
   1175 go_fetch(url, outfile)
   1176 	const char *url;
   1177 	const char *outfile;
   1178 {
   1179 
   1180 #ifndef SMALL
   1181 	/*
   1182 	 * Check for about:*
   1183 	 */
   1184 	if (strncasecmp(url, ABOUT_URL, sizeof(ABOUT_URL) - 1) == 0) {
   1185 		url += sizeof(ABOUT_URL) -1;
   1186 		if (strcasecmp(url, "ftp") == 0) {
   1187 			fprintf(ttyout, "%s\n%s\n",
   1188 "This version of ftp has been enhanced by Luke Mewburn <lukem (at) netbsd.org>.",
   1189 "Execute `man ftp' for more details");
   1190 		} else if (strcasecmp(url, "netbsd") == 0) {
   1191 			fprintf(ttyout, "%s\n%s\n",
   1192 "NetBSD is a freely available and redistributable UNIX-like operating system.",
   1193 "For more information, see http://www.netbsd.org/index.html");
   1194 		} else {
   1195 			fprintf(ttyout, "`%s' is an interesting topic.\n", url);
   1196 		}
   1197 		return (0);
   1198 	}
   1199 #endif /* SMALL */
   1200 
   1201 	/*
   1202 	 * Check for file:// and http:// URLs.
   1203 	 */
   1204 	if (strncasecmp(url, HTTP_URL, sizeof(HTTP_URL) - 1) == 0 ||
   1205 	    strncasecmp(url, FILE_URL, sizeof(FILE_URL) - 1) == 0)
   1206 		return (fetch_url(url, outfile, NULL, NULL, NULL));
   1207 
   1208 	/*
   1209 	 * Try FTP URL-style and host:file arguments next.
   1210 	 * If ftpproxy is set with an FTP URL, use fetch_url()
   1211 	 * Othewise, use fetch_ftp().
   1212 	 */
   1213 	if (ftpproxy && strncasecmp(url, FTP_URL, sizeof(FTP_URL) - 1) == 0)
   1214 		return (fetch_url(url, outfile, NULL, NULL, NULL));
   1215 
   1216 	return (fetch_ftp(url, outfile));
   1217 }
   1218 
   1219 /*
   1220  * Retrieve multiple files from the command line,
   1221  * calling go_fetch() for each file.
   1222  *
   1223  * If an ftp path has a trailing "/", the path will be cd-ed into and
   1224  * the connection remains open, and the function will return -1
   1225  * (to indicate the connection is alive).
   1226  * If an error occurs the return value will be the offset+1 in
   1227  * argv[] of the file that caused a problem (i.e, argv[x]
   1228  * returns x+1)
   1229  * Otherwise, 0 is returned if all files retrieved successfully.
   1230  */
   1231 int
   1232 auto_fetch(argc, argv, outfile)
   1233 	int argc;
   1234 	char *argv[];
   1235 	char *outfile;
   1236 {
   1237 	volatile int	argpos;
   1238 	int		rval;
   1239 
   1240 	argpos = 0;
   1241 
   1242 	if (setjmp(toplevel)) {
   1243 		if (connected)
   1244 			disconnect(0, NULL);
   1245 		return (argpos + 1);
   1246 	}
   1247 	(void)signal(SIGINT, (sig_t)intr);
   1248 	(void)signal(SIGPIPE, (sig_t)lostpeer);
   1249 
   1250 	/*
   1251 	 * Loop through as long as there's files to fetch.
   1252 	 */
   1253 	for (rval = 0; (rval == 0) && (argpos < argc); argpos++) {
   1254 		if (strchr(argv[argpos], ':') == NULL)
   1255 			break;
   1256 		redirect_loop = 0;
   1257 		anonftp = 1;		/* Handle "automatic" transfers. */
   1258 		rval = go_fetch(argv[argpos], outfile);
   1259 		if (rval > 0)
   1260 			rval = argpos + 1;
   1261 	}
   1262 
   1263 	if (connected && rval != -1)
   1264 		disconnect(0, NULL);
   1265 	return (rval);
   1266 }
   1267