Home | History | Annotate | Line # | Download | only in ftp
fetch.c revision 1.204
      1 /*	$NetBSD: fetch.c,v 1.204 2013/11/03 14:45:50 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997-2009 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Luke Mewburn.
      9  *
     10  * This code is derived from software contributed to The NetBSD Foundation
     11  * by Scott Aaron Bamford.
     12  *
     13  * Redistribution and use in source and binary forms, with or without
     14  * modification, are permitted provided that the following conditions
     15  * are met:
     16  * 1. Redistributions of source code must retain the above copyright
     17  *    notice, this list of conditions and the following disclaimer.
     18  * 2. Redistributions in binary form must reproduce the above copyright
     19  *    notice, this list of conditions and the following disclaimer in the
     20  *    documentation and/or other materials provided with the distribution.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     32  * POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 
     35 #include <sys/cdefs.h>
     36 #ifndef lint
     37 __RCSID("$NetBSD: fetch.c,v 1.204 2013/11/03 14:45:50 christos Exp $");
     38 #endif /* not lint */
     39 
     40 /*
     41  * FTP User Program -- Command line file retrieval
     42  */
     43 
     44 #include <sys/types.h>
     45 #include <sys/param.h>
     46 #include <sys/socket.h>
     47 #include <sys/stat.h>
     48 #include <sys/time.h>
     49 
     50 #include <netinet/in.h>
     51 
     52 #include <arpa/ftp.h>
     53 #include <arpa/inet.h>
     54 
     55 #include <assert.h>
     56 #include <ctype.h>
     57 #include <err.h>
     58 #include <errno.h>
     59 #include <netdb.h>
     60 #include <fcntl.h>
     61 #include <stdio.h>
     62 #include <stdlib.h>
     63 #include <string.h>
     64 #include <unistd.h>
     65 #include <time.h>
     66 
     67 #include "ssl.h"
     68 #include "ftp_var.h"
     69 #include "version.h"
     70 
     71 typedef enum {
     72 	UNKNOWN_URL_T=-1,
     73 	HTTP_URL_T,
     74 #ifdef WITH_SSL
     75 	HTTPS_URL_T,
     76 #endif
     77 	FTP_URL_T,
     78 	FILE_URL_T,
     79 	CLASSIC_URL_T
     80 } url_t;
     81 
     82 __dead static void	aborthttp(int);
     83 __dead static void	timeouthttp(int);
     84 #ifndef NO_AUTH
     85 static int	auth_url(const char *, char **, const char *, const char *);
     86 static void	base64_encode(const unsigned char *, size_t, unsigned char *);
     87 #endif
     88 static int	go_fetch(const char *);
     89 static int	fetch_ftp(const char *);
     90 static int	fetch_url(const char *, const char *, char *, char *);
     91 static const char *match_token(const char **, const char *);
     92 static int	parse_url(const char *, const char *, url_t *, char **,
     93 			    char **, char **, char **, in_port_t *, char **);
     94 static void	url_decode(char *);
     95 
     96 static int	redirect_loop;
     97 
     98 
     99 #define	STRNEQUAL(a,b)	(strncasecmp((a), (b), sizeof((b))-1) == 0)
    100 #define	ISLWS(x)	((x)=='\r' || (x)=='\n' || (x)==' ' || (x)=='\t')
    101 #define	SKIPLWS(x)	do { while (ISLWS((*x))) x++; } while (0)
    102 
    103 
    104 #define	ABOUT_URL	"about:"	/* propaganda */
    105 #define	FILE_URL	"file://"	/* file URL prefix */
    106 #define	FTP_URL		"ftp://"	/* ftp URL prefix */
    107 #define	HTTP_URL	"http://"	/* http URL prefix */
    108 #ifdef WITH_SSL
    109 #define	HTTPS_URL	"https://"	/* https URL prefix */
    110 
    111 #define	IS_HTTP_TYPE(urltype) \
    112 	(((urltype) == HTTP_URL_T) || ((urltype) == HTTPS_URL_T))
    113 #else
    114 #define	IS_HTTP_TYPE(urltype) \
    115 	((urltype) == HTTP_URL_T)
    116 #endif
    117 
    118 /*
    119  * Determine if token is the next word in buf (case insensitive).
    120  * If so, advance buf past the token and any trailing LWS, and
    121  * return a pointer to the token (in buf).  Otherwise, return NULL.
    122  * token may be preceded by LWS.
    123  * token must be followed by LWS or NUL.  (I.e, don't partial match).
    124  */
    125 static const char *
    126 match_token(const char **buf, const char *token)
    127 {
    128 	const char	*p, *orig;
    129 	size_t		tlen;
    130 
    131 	tlen = strlen(token);
    132 	p = *buf;
    133 	SKIPLWS(p);
    134 	orig = p;
    135 	if (strncasecmp(p, token, tlen) != 0)
    136 		return NULL;
    137 	p += tlen;
    138 	if (*p != '\0' && !ISLWS(*p))
    139 		return NULL;
    140 	SKIPLWS(p);
    141 	orig = *buf;
    142 	*buf = p;
    143 	return orig;
    144 }
    145 
    146 #ifndef NO_AUTH
    147 /*
    148  * Generate authorization response based on given authentication challenge.
    149  * Returns -1 if an error occurred, otherwise 0.
    150  * Sets response to a malloc(3)ed string; caller should free.
    151  */
    152 static int
    153 auth_url(const char *challenge, char **response, const char *guser,
    154 	const char *gpass)
    155 {
    156 	const char	*cp, *scheme, *errormsg;
    157 	char		*ep, *clear, *realm;
    158 	char		 uuser[BUFSIZ], *gotpass;
    159 	const char	*upass;
    160 	int		 rval;
    161 	size_t		 len, clen, rlen;
    162 
    163 	*response = NULL;
    164 	clear = realm = NULL;
    165 	rval = -1;
    166 	cp = challenge;
    167 	scheme = "Basic";	/* only support Basic authentication */
    168 	gotpass = NULL;
    169 
    170 	DPRINTF("auth_url: challenge `%s'\n", challenge);
    171 
    172 	if (! match_token(&cp, scheme)) {
    173 		warnx("Unsupported authentication challenge `%s'",
    174 		    challenge);
    175 		goto cleanup_auth_url;
    176 	}
    177 
    178 #define	REALM "realm=\""
    179 	if (STRNEQUAL(cp, REALM))
    180 		cp += sizeof(REALM) - 1;
    181 	else {
    182 		warnx("Unsupported authentication challenge `%s'",
    183 		    challenge);
    184 		goto cleanup_auth_url;
    185 	}
    186 /* XXX: need to improve quoted-string parsing to support \ quoting, etc. */
    187 	if ((ep = strchr(cp, '\"')) != NULL) {
    188 		len = ep - cp;
    189 		realm = (char *)ftp_malloc(len + 1);
    190 		(void)strlcpy(realm, cp, len + 1);
    191 	} else {
    192 		warnx("Unsupported authentication challenge `%s'",
    193 		    challenge);
    194 		goto cleanup_auth_url;
    195 	}
    196 
    197 	fprintf(ttyout, "Username for `%s': ", realm);
    198 	if (guser != NULL) {
    199 		(void)strlcpy(uuser, guser, sizeof(uuser));
    200 		fprintf(ttyout, "%s\n", uuser);
    201 	} else {
    202 		(void)fflush(ttyout);
    203 		if (get_line(stdin, uuser, sizeof(uuser), &errormsg) < 0) {
    204 			warnx("%s; can't authenticate", errormsg);
    205 			goto cleanup_auth_url;
    206 		}
    207 	}
    208 	if (gpass != NULL)
    209 		upass = gpass;
    210 	else {
    211 		gotpass = getpass("Password: ");
    212 		if (gotpass == NULL) {
    213 			warnx("Can't read password");
    214 			goto cleanup_auth_url;
    215 		}
    216 		upass = gotpass;
    217 	}
    218 
    219 	clen = strlen(uuser) + strlen(upass) + 2;	/* user + ":" + pass + "\0" */
    220 	clear = (char *)ftp_malloc(clen);
    221 	(void)strlcpy(clear, uuser, clen);
    222 	(void)strlcat(clear, ":", clen);
    223 	(void)strlcat(clear, upass, clen);
    224 	if (gotpass)
    225 		memset(gotpass, 0, strlen(gotpass));
    226 
    227 						/* scheme + " " + enc + "\0" */
    228 	rlen = strlen(scheme) + 1 + (clen + 2) * 4 / 3 + 1;
    229 	*response = (char *)ftp_malloc(rlen);
    230 	(void)strlcpy(*response, scheme, rlen);
    231 	len = strlcat(*response, " ", rlen);
    232 			/* use  `clen - 1'  to not encode the trailing NUL */
    233 	base64_encode((unsigned char *)clear, clen - 1,
    234 	    (unsigned char *)*response + len);
    235 	memset(clear, 0, clen);
    236 	rval = 0;
    237 
    238  cleanup_auth_url:
    239 	FREEPTR(clear);
    240 	FREEPTR(realm);
    241 	return (rval);
    242 }
    243 
    244 /*
    245  * Encode len bytes starting at clear using base64 encoding into encoded,
    246  * which should be at least ((len + 2) * 4 / 3 + 1) in size.
    247  */
    248 static void
    249 base64_encode(const unsigned char *clear, size_t len, unsigned char *encoded)
    250 {
    251 	static const unsigned char enc[] =
    252 	    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    253 	unsigned char	*cp;
    254 	size_t	 i;
    255 
    256 	cp = encoded;
    257 	for (i = 0; i < len; i += 3) {
    258 		*(cp++) = enc[((clear[i + 0] >> 2))];
    259 		*(cp++) = enc[((clear[i + 0] << 4) & 0x30)
    260 			    | ((clear[i + 1] >> 4) & 0x0f)];
    261 		*(cp++) = enc[((clear[i + 1] << 2) & 0x3c)
    262 			    | ((clear[i + 2] >> 6) & 0x03)];
    263 		*(cp++) = enc[((clear[i + 2]     ) & 0x3f)];
    264 	}
    265 	*cp = '\0';
    266 	while (i-- > len)
    267 		*(--cp) = '=';
    268 }
    269 #endif
    270 
    271 /*
    272  * Decode %xx escapes in given string, `in-place'.
    273  */
    274 static void
    275 url_decode(char *url)
    276 {
    277 	unsigned char *p, *q;
    278 
    279 	if (EMPTYSTRING(url))
    280 		return;
    281 	p = q = (unsigned char *)url;
    282 
    283 #define	HEXTOINT(x) (x - (isdigit(x) ? '0' : (islower(x) ? 'a' : 'A') - 10))
    284 	while (*p) {
    285 		if (p[0] == '%'
    286 		    && p[1] && isxdigit((unsigned char)p[1])
    287 		    && p[2] && isxdigit((unsigned char)p[2])) {
    288 			*q++ = HEXTOINT(p[1]) * 16 + HEXTOINT(p[2]);
    289 			p+=3;
    290 		} else
    291 			*q++ = *p++;
    292 	}
    293 	*q = '\0';
    294 }
    295 
    296 
    297 /*
    298  * Parse URL of form (per RFC 3986):
    299  *	<type>://[<user>[:<password>]@]<host>[:<port>][/<path>]
    300  * Returns -1 if a parse error occurred, otherwise 0.
    301  * It's the caller's responsibility to url_decode() the returned
    302  * user, pass and path.
    303  *
    304  * Sets type to url_t, each of the given char ** pointers to a
    305  * malloc(3)ed strings of the relevant section, and port to
    306  * the number given, or ftpport if ftp://, or httpport if http://.
    307  *
    308  * XXX: this is not totally RFC 3986 compliant; <path> will have the
    309  * leading `/' unless it's an ftp:// URL, as this makes things easier
    310  * for file:// and http:// URLs.  ftp:// URLs have the `/' between the
    311  * host and the URL-path removed, but any additional leading slashes
    312  * in the URL-path are retained (because they imply that we should
    313  * later do "CWD" with a null argument).
    314  *
    315  * Examples:
    316  *	 input URL			 output path
    317  *	 ---------			 -----------
    318  *	"http://host"			"/"
    319  *	"http://host/"			"/"
    320  *	"http://host/path"		"/path"
    321  *	"file://host/dir/file"		"dir/file"
    322  *	"ftp://host"			""
    323  *	"ftp://host/"			""
    324  *	"ftp://host//"			"/"
    325  *	"ftp://host/dir/file"		"dir/file"
    326  *	"ftp://host//dir/file"		"/dir/file"
    327  */
    328 static int
    329 parse_url(const char *url, const char *desc, url_t *utype,
    330 		char **uuser, char **pass, char **host, char **port,
    331 		in_port_t *portnum, char **path)
    332 {
    333 	const char	*origurl, *tport;
    334 	char		*cp, *ep, *thost;
    335 	size_t		 len;
    336 
    337 	if (url == NULL || desc == NULL || utype == NULL || uuser == NULL
    338 	    || pass == NULL || host == NULL || port == NULL || portnum == NULL
    339 	    || path == NULL)
    340 		errx(1, "parse_url: invoked with NULL argument!");
    341 	DPRINTF("parse_url: %s `%s'\n", desc, url);
    342 
    343 	origurl = url;
    344 	*utype = UNKNOWN_URL_T;
    345 	*uuser = *pass = *host = *port = *path = NULL;
    346 	*portnum = 0;
    347 	tport = NULL;
    348 
    349 	if (STRNEQUAL(url, HTTP_URL)) {
    350 		url += sizeof(HTTP_URL) - 1;
    351 		*utype = HTTP_URL_T;
    352 		*portnum = HTTP_PORT;
    353 		tport = httpport;
    354 	} else if (STRNEQUAL(url, FTP_URL)) {
    355 		url += sizeof(FTP_URL) - 1;
    356 		*utype = FTP_URL_T;
    357 		*portnum = FTP_PORT;
    358 		tport = ftpport;
    359 	} else if (STRNEQUAL(url, FILE_URL)) {
    360 		url += sizeof(FILE_URL) - 1;
    361 		*utype = FILE_URL_T;
    362 #ifdef WITH_SSL
    363 	} else if (STRNEQUAL(url, HTTPS_URL)) {
    364 		url += sizeof(HTTPS_URL) - 1;
    365 		*utype = HTTPS_URL_T;
    366 		*portnum = HTTPS_PORT;
    367 		tport = httpsport;
    368 #endif
    369 	} else {
    370 		warnx("Invalid %s `%s'", desc, url);
    371  cleanup_parse_url:
    372 		FREEPTR(*uuser);
    373 		if (*pass != NULL)
    374 			memset(*pass, 0, strlen(*pass));
    375 		FREEPTR(*pass);
    376 		FREEPTR(*host);
    377 		FREEPTR(*port);
    378 		FREEPTR(*path);
    379 		return (-1);
    380 	}
    381 
    382 	if (*url == '\0')
    383 		return (0);
    384 
    385 			/* find [user[:pass]@]host[:port] */
    386 	ep = strchr(url, '/');
    387 	if (ep == NULL)
    388 		thost = ftp_strdup(url);
    389 	else {
    390 		len = ep - url;
    391 		thost = (char *)ftp_malloc(len + 1);
    392 		(void)strlcpy(thost, url, len + 1);
    393 		if (*utype == FTP_URL_T)	/* skip first / for ftp URLs */
    394 			ep++;
    395 		*path = ftp_strdup(ep);
    396 	}
    397 
    398 	cp = strchr(thost, '@');	/* look for user[:pass]@ in URLs */
    399 	if (cp != NULL) {
    400 		if (*utype == FTP_URL_T)
    401 			anonftp = 0;	/* disable anonftp */
    402 		*uuser = thost;
    403 		*cp = '\0';
    404 		thost = ftp_strdup(cp + 1);
    405 		cp = strchr(*uuser, ':');
    406 		if (cp != NULL) {
    407 			*cp = '\0';
    408 			*pass = ftp_strdup(cp + 1);
    409 		}
    410 		url_decode(*uuser);
    411 		if (*pass)
    412 			url_decode(*pass);
    413 	}
    414 
    415 #ifdef INET6
    416 			/*
    417 			 * Check if thost is an encoded IPv6 address, as per
    418 			 * RFC 3986:
    419 			 *	`[' ipv6-address ']'
    420 			 */
    421 	if (*thost == '[') {
    422 		cp = thost + 1;
    423 		if ((ep = strchr(cp, ']')) == NULL ||
    424 		    (ep[1] != '\0' && ep[1] != ':')) {
    425 			warnx("Invalid address `%s' in %s `%s'",
    426 			    thost, desc, origurl);
    427 			goto cleanup_parse_url;
    428 		}
    429 		len = ep - cp;		/* change `[xyz]' -> `xyz' */
    430 		memmove(thost, thost + 1, len);
    431 		thost[len] = '\0';
    432 		if (! isipv6addr(thost)) {
    433 			warnx("Invalid IPv6 address `%s' in %s `%s'",
    434 			    thost, desc, origurl);
    435 			goto cleanup_parse_url;
    436 		}
    437 		cp = ep + 1;
    438 		if (*cp == ':')
    439 			cp++;
    440 		else
    441 			cp = NULL;
    442 	} else
    443 #endif /* INET6 */
    444 		if ((cp = strchr(thost, ':')) != NULL)
    445 			*cp++ = '\0';
    446 	*host = thost;
    447 
    448 			/* look for [:port] */
    449 	if (cp != NULL) {
    450 		unsigned long	nport;
    451 
    452 		nport = strtoul(cp, &ep, 10);
    453 		if (*cp == '\0' || *ep != '\0' ||
    454 		    nport < 1 || nport > MAX_IN_PORT_T) {
    455 			warnx("Unknown port `%s' in %s `%s'",
    456 			    cp, desc, origurl);
    457 			goto cleanup_parse_url;
    458 		}
    459 		*portnum = nport;
    460 		tport = cp;
    461 	}
    462 
    463 	if (tport != NULL)
    464 		*port = ftp_strdup(tport);
    465 	if (*path == NULL) {
    466 		const char *emptypath = "/";
    467 		if (*utype == FTP_URL_T)	/* skip first / for ftp URLs */
    468 			emptypath++;
    469 		*path = ftp_strdup(emptypath);
    470 	}
    471 
    472 	DPRINTF("parse_url: user `%s' pass `%s' host %s port %s(%d) "
    473 	    "path `%s'\n",
    474 	    STRorNULL(*uuser), STRorNULL(*pass),
    475 	    STRorNULL(*host), STRorNULL(*port),
    476 	    *portnum ? *portnum : -1, STRorNULL(*path));
    477 
    478 	return (0);
    479 }
    480 
    481 sigjmp_buf	httpabort;
    482 
    483 /*
    484  * Retrieve URL, via a proxy if necessary, using HTTP.
    485  * If proxyenv is set, use that for the proxy, otherwise try ftp_proxy or
    486  * http_proxy/https_proxy as appropriate.
    487  * Supports HTTP redirects.
    488  * Returns 1 on failure, 0 on completed xfer, -1 if ftp connection
    489  * is still open (e.g, ftp xfer with trailing /)
    490  */
    491 static int
    492 fetch_url(const char *url, const char *proxyenv, char *proxyauth, char *wwwauth)
    493 {
    494 	struct addrinfo		hints, *res, *res0 = NULL;
    495 	int			error;
    496 	sigfunc volatile	oldint;
    497 	sigfunc volatile	oldpipe;
    498 	sigfunc volatile	oldalrm;
    499 	sigfunc volatile	oldquit;
    500 	int volatile		s;
    501 	struct stat		sb;
    502 	int volatile		ischunked;
    503 	int volatile		isproxy;
    504 	int volatile		rval;
    505 	int volatile		hcode;
    506 	int			len;
    507 	size_t			flen;
    508 	static size_t		bufsize;
    509 	static char		*xferbuf;
    510 	const char		*cp, *token;
    511 	char			*ep;
    512 	char			buf[FTPBUFLEN];
    513 	const char		*errormsg;
    514 	char			*volatile savefile;
    515 	char			*volatile auth;
    516 	char			*volatile location;
    517 	char			*volatile message;
    518 	char			*uuser, *pass, *host, *port, *path;
    519 	char			*volatile decodedpath;
    520 	char			*puser, *ppass, *useragent;
    521 	off_t			hashbytes, rangestart, rangeend, entitylen;
    522 	int			(*volatile closefunc)(FILE *);
    523 	FETCH			*volatile fin;
    524 	FILE			*volatile fout;
    525 	const char		*volatile penv = proxyenv;
    526 	time_t			mtime;
    527 	url_t			urltype;
    528 	in_port_t		portnum;
    529 #ifdef WITH_SSL
    530 	void			*ssl;
    531 #endif
    532 
    533 	DPRINTF("%s: `%s' proxyenv `%s'\n", __func__, url, STRorNULL(penv));
    534 
    535 	oldquit = oldalrm = oldint = oldpipe = NULL;
    536 	closefunc = NULL;
    537 	fin = NULL;
    538 	fout = NULL;
    539 	s = -1;
    540 	savefile = NULL;
    541 	auth = location = message = NULL;
    542 	ischunked = isproxy = hcode = 0;
    543 	rval = 1;
    544 	uuser = pass = host = path = decodedpath = puser = ppass = NULL;
    545 
    546 	if (sigsetjmp(httpabort, 1))
    547 		goto cleanup_fetch_url;
    548 
    549 	if (parse_url(url, "URL", &urltype, &uuser, &pass, &host, &port,
    550 	    &portnum, &path) == -1)
    551 		goto cleanup_fetch_url;
    552 
    553 	if (urltype == FILE_URL_T && ! EMPTYSTRING(host)
    554 	    && strcasecmp(host, "localhost") != 0) {
    555 		warnx("No support for non local file URL `%s'", url);
    556 		goto cleanup_fetch_url;
    557 	}
    558 
    559 	if (EMPTYSTRING(path)) {
    560 		if (urltype == FTP_URL_T) {
    561 			rval = fetch_ftp(url);
    562 			goto cleanup_fetch_url;
    563 		}
    564 		if (!IS_HTTP_TYPE(urltype) || outfile == NULL)  {
    565 			warnx("Invalid URL (no file after host) `%s'", url);
    566 			goto cleanup_fetch_url;
    567 		}
    568 	}
    569 
    570 	decodedpath = ftp_strdup(path);
    571 	url_decode(decodedpath);
    572 
    573 	if (outfile)
    574 		savefile = ftp_strdup(outfile);
    575 	else {
    576 		cp = strrchr(decodedpath, '/');		/* find savefile */
    577 		if (cp != NULL)
    578 			savefile = ftp_strdup(cp + 1);
    579 		else
    580 			savefile = ftp_strdup(decodedpath);
    581 	}
    582 	DPRINTF("%s: savefile `%s'\n", __func__, savefile);
    583 	if (EMPTYSTRING(savefile)) {
    584 		if (urltype == FTP_URL_T) {
    585 			rval = fetch_ftp(url);
    586 			goto cleanup_fetch_url;
    587 		}
    588 		warnx("No file after directory (you must specify an "
    589 		    "output file) `%s'", url);
    590 		goto cleanup_fetch_url;
    591 	}
    592 
    593 	restart_point = 0;
    594 	filesize = -1;
    595 	rangestart = rangeend = entitylen = -1;
    596 	mtime = -1;
    597 	if (restartautofetch) {
    598 		if (strcmp(savefile, "-") != 0 && *savefile != '|' &&
    599 		    stat(savefile, &sb) == 0)
    600 			restart_point = sb.st_size;
    601 	}
    602 	if (urltype == FILE_URL_T) {		/* file:// URLs */
    603 		direction = "copied";
    604 		fin = fetch_open(decodedpath, "r");
    605 		if (fin == NULL) {
    606 			warn("Can't open `%s'", decodedpath);
    607 			goto cleanup_fetch_url;
    608 		}
    609 		if (fstat(fetch_fileno(fin), &sb) == 0) {
    610 			mtime = sb.st_mtime;
    611 			filesize = sb.st_size;
    612 		}
    613 		if (restart_point) {
    614 			if (lseek(fetch_fileno(fin), restart_point, SEEK_SET) < 0) {
    615 				warn("Can't seek to restart `%s'",
    616 				    decodedpath);
    617 				goto cleanup_fetch_url;
    618 			}
    619 		}
    620 		if (verbose) {
    621 			fprintf(ttyout, "Copying %s", decodedpath);
    622 			if (restart_point)
    623 				fprintf(ttyout, " (restarting at " LLF ")",
    624 				    (LLT)restart_point);
    625 			fputs("\n", ttyout);
    626 		}
    627 		if (0 == rcvbuf_size) {
    628 			rcvbuf_size = 8 * 1024; /* XXX */
    629 		}
    630 	} else {				/* ftp:// or http:// URLs */
    631 		const char *leading;
    632 		int hasleading;
    633 
    634 		if (penv == NULL) {
    635 #ifdef WITH_SSL
    636 			if (urltype == HTTPS_URL_T)
    637 				penv = getoptionvalue("https_proxy");
    638 #endif
    639 			if (penv == NULL && IS_HTTP_TYPE(urltype))
    640 				penv = getoptionvalue("http_proxy");
    641 			else if (urltype == FTP_URL_T)
    642 				penv = getoptionvalue("ftp_proxy");
    643 		}
    644 		direction = "retrieved";
    645 		if (! EMPTYSTRING(penv)) {			/* use proxy */
    646 			url_t purltype;
    647 			char *phost, *ppath;
    648 			char *pport, *no_proxy;
    649 			in_port_t pportnum;
    650 
    651 			isproxy = 1;
    652 
    653 				/* check URL against list of no_proxied sites */
    654 			no_proxy = getoptionvalue("no_proxy");
    655 			if (! EMPTYSTRING(no_proxy)) {
    656 				char *np, *np_copy, *np_iter;
    657 				unsigned long np_port;
    658 				size_t hlen, plen;
    659 
    660 				np_iter = np_copy = ftp_strdup(no_proxy);
    661 				hlen = strlen(host);
    662 				while ((cp = strsep(&np_iter, " ,")) != NULL) {
    663 					if (*cp == '\0')
    664 						continue;
    665 					if ((np = strrchr(cp, ':')) != NULL) {
    666 						*np++ =  '\0';
    667 						np_port = strtoul(np, &ep, 10);
    668 						if (*np == '\0' || *ep != '\0')
    669 							continue;
    670 						if (np_port != portnum)
    671 							continue;
    672 					}
    673 					plen = strlen(cp);
    674 					if (hlen < plen)
    675 						continue;
    676 					if (strncasecmp(host + hlen - plen,
    677 					    cp, plen) == 0) {
    678 						isproxy = 0;
    679 						break;
    680 					}
    681 				}
    682 				FREEPTR(np_copy);
    683 				if (isproxy == 0 && urltype == FTP_URL_T) {
    684 					rval = fetch_ftp(url);
    685 					goto cleanup_fetch_url;
    686 				}
    687 			}
    688 
    689 			if (isproxy) {
    690 				if (restart_point) {
    691 					warnx("Can't restart via proxy URL `%s'",
    692 					    penv);
    693 					goto cleanup_fetch_url;
    694 				}
    695 				if (parse_url(penv, "proxy URL", &purltype,
    696 				    &puser, &ppass, &phost, &pport, &pportnum,
    697 				    &ppath) == -1)
    698 					goto cleanup_fetch_url;
    699 
    700 				if ((!IS_HTTP_TYPE(purltype)
    701 				     && purltype != FTP_URL_T) ||
    702 				    EMPTYSTRING(phost) ||
    703 				    (! EMPTYSTRING(ppath)
    704 				     && strcmp(ppath, "/") != 0)) {
    705 					warnx("Malformed proxy URL `%s'", penv);
    706 					FREEPTR(phost);
    707 					FREEPTR(pport);
    708 					FREEPTR(ppath);
    709 					goto cleanup_fetch_url;
    710 				}
    711 				if (isipv6addr(host) &&
    712 				    strchr(host, '%') != NULL) {
    713 					warnx(
    714 "Scoped address notation `%s' disallowed via web proxy",
    715 					    host);
    716 					FREEPTR(phost);
    717 					FREEPTR(pport);
    718 					FREEPTR(ppath);
    719 					goto cleanup_fetch_url;
    720 				}
    721 
    722 				FREEPTR(host);
    723 				host = phost;
    724 				FREEPTR(port);
    725 				port = pport;
    726 				FREEPTR(path);
    727 				path = ftp_strdup(url);
    728 				FREEPTR(ppath);
    729 				urltype = purltype;
    730 			}
    731 		} /* ! EMPTYSTRING(penv) */
    732 
    733 		memset(&hints, 0, sizeof(hints));
    734 		hints.ai_flags = 0;
    735 		hints.ai_family = family;
    736 		hints.ai_socktype = SOCK_STREAM;
    737 		hints.ai_protocol = 0;
    738 		error = getaddrinfo(host, port, &hints, &res0);
    739 		if (error) {
    740 			warnx("Can't LOOKUP `%s:%s': %s", host, port,
    741 			    (error == EAI_SYSTEM) ? strerror(errno)
    742 						  : gai_strerror(error));
    743 			goto cleanup_fetch_url;
    744 		}
    745 		if (res0->ai_canonname)
    746 			host = res0->ai_canonname;
    747 
    748 		s = -1;
    749 #ifdef WITH_SSL
    750 		ssl = NULL;
    751 #endif
    752 		for (res = res0; res; res = res->ai_next) {
    753 			char	hname[NI_MAXHOST], sname[NI_MAXSERV];
    754 
    755 			ai_unmapped(res);
    756 			if (getnameinfo(res->ai_addr, res->ai_addrlen,
    757 			    hname, sizeof(hname), sname, sizeof(sname),
    758 			    NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
    759 				strlcpy(hname, "?", sizeof(hname));
    760 				strlcpy(sname, "?", sizeof(sname));
    761 			}
    762 
    763 			if (verbose && res0->ai_next) {
    764 				fprintf(ttyout, "Trying %s:%s ...\n",
    765 				    hname, sname);
    766 			}
    767 
    768 			s = socket(res->ai_family, SOCK_STREAM,
    769 			    res->ai_protocol);
    770 			if (s < 0) {
    771 				warn(
    772 				    "Can't create socket for connection to "
    773 				    "`%s:%s'", hname, sname);
    774 				continue;
    775 			}
    776 
    777 			if (ftp_connect(s, res->ai_addr, res->ai_addrlen,
    778 			    verbose || !res->ai_next) < 0) {
    779 				close(s);
    780 				s = -1;
    781 				continue;
    782 			}
    783 
    784 #ifdef WITH_SSL
    785 			if (urltype == HTTPS_URL_T) {
    786 				if ((ssl = fetch_start_ssl(s)) == NULL) {
    787 					close(s);
    788 					s = -1;
    789 					continue;
    790 				}
    791 			}
    792 #endif
    793 
    794 			/* success */
    795 			break;
    796 		}
    797 
    798 		if (s < 0) {
    799 			warnx("Can't connect to `%s:%s'", host, port);
    800 			goto cleanup_fetch_url;
    801 		}
    802 
    803 		oldalrm = xsignal(SIGALRM, timeouthttp);
    804 		alarmtimer(quit_time ? quit_time : 60);
    805 		fin = fetch_fdopen(s, "r+");
    806 		fetch_set_ssl(fin, ssl);
    807 		alarmtimer(0);
    808 
    809 		alarmtimer(quit_time ? quit_time : 60);
    810 		/*
    811 		 * Construct and send the request.
    812 		 */
    813 		if (verbose)
    814 			fprintf(ttyout, "Requesting %s\n", url);
    815 		leading = "  (";
    816 		hasleading = 0;
    817 		if (isproxy) {
    818 			if (verbose) {
    819 				fprintf(ttyout, "%svia %s:%s", leading,
    820 				    host, port);
    821 				leading = ", ";
    822 				hasleading++;
    823 			}
    824 			fetch_printf(fin, "GET %s HTTP/1.0\r\n", path);
    825 			if (flushcache)
    826 				fetch_printf(fin, "Pragma: no-cache\r\n");
    827 		} else {
    828 			fetch_printf(fin, "GET %s HTTP/1.1\r\n", path);
    829 			if (strchr(host, ':')) {
    830 				char *h, *p;
    831 
    832 				/*
    833 				 * strip off IPv6 scope identifier, since it is
    834 				 * local to the node
    835 				 */
    836 				h = ftp_strdup(host);
    837 				if (isipv6addr(h) &&
    838 				    (p = strchr(h, '%')) != NULL) {
    839 					*p = '\0';
    840 				}
    841 				fetch_printf(fin, "Host: [%s]", h);
    842 				free(h);
    843 			} else
    844 				fetch_printf(fin, "Host: %s", host);
    845 #ifdef WITH_SSL
    846 			if ((urltype == HTTP_URL_T && portnum != HTTP_PORT) ||
    847 			    (urltype == HTTPS_URL_T && portnum != HTTPS_PORT))
    848 #else
    849 			if (portnum != HTTP_PORT)
    850 #endif
    851 				fetch_printf(fin, ":%u", portnum);
    852 			fetch_printf(fin, "\r\n");
    853 			fetch_printf(fin, "Accept: */*\r\n");
    854 			fetch_printf(fin, "Connection: close\r\n");
    855 			if (restart_point) {
    856 				fputs(leading, ttyout);
    857 				fetch_printf(fin, "Range: bytes=" LLF "-\r\n",
    858 				    (LLT)restart_point);
    859 				fprintf(ttyout, "restarting at " LLF,
    860 				    (LLT)restart_point);
    861 				leading = ", ";
    862 				hasleading++;
    863 			}
    864 			if (flushcache)
    865 				fetch_printf(fin, "Cache-Control: no-cache\r\n");
    866 		}
    867 		if ((useragent=getenv("FTPUSERAGENT")) != NULL) {
    868 			fetch_printf(fin, "User-Agent: %s\r\n", useragent);
    869 		} else {
    870 			fetch_printf(fin, "User-Agent: %s/%s\r\n",
    871 			    FTP_PRODUCT, FTP_VERSION);
    872 		}
    873 		if (wwwauth) {
    874 			if (verbose) {
    875 				fprintf(ttyout, "%swith authorization",
    876 				    leading);
    877 				leading = ", ";
    878 				hasleading++;
    879 			}
    880 			fetch_printf(fin, "Authorization: %s\r\n", wwwauth);
    881 		}
    882 		if (proxyauth) {
    883 			if (verbose) {
    884 				fprintf(ttyout,
    885 				    "%swith proxy authorization", leading);
    886 				leading = ", ";
    887 				hasleading++;
    888 			}
    889 			fetch_printf(fin, "Proxy-Authorization: %s\r\n", proxyauth);
    890 		}
    891 		if (verbose && hasleading)
    892 			fputs(")\n", ttyout);
    893 		fetch_printf(fin, "\r\n");
    894 		if (fetch_flush(fin) == EOF) {
    895 			warn("Writing HTTP request");
    896 			alarmtimer(0);
    897 			goto cleanup_fetch_url;
    898 		}
    899 		alarmtimer(0);
    900 
    901 				/* Read the response */
    902 		alarmtimer(quit_time ? quit_time : 60);
    903 		len = fetch_getline(fin, buf, sizeof(buf), &errormsg);
    904 		alarmtimer(0);
    905 		if (len < 0) {
    906 			if (*errormsg == '\n')
    907 				errormsg++;
    908 			warnx("Receiving HTTP reply: %s", errormsg);
    909 			goto cleanup_fetch_url;
    910 		}
    911 		while (len > 0 && (ISLWS(buf[len-1])))
    912 			buf[--len] = '\0';
    913 		DPRINTF("%s: received `%s'\n", __func__, buf);
    914 
    915 				/* Determine HTTP response code */
    916 		cp = strchr(buf, ' ');
    917 		if (cp == NULL)
    918 			goto improper;
    919 		else
    920 			cp++;
    921 		hcode = strtol(cp, &ep, 10);
    922 		if (*ep != '\0' && !isspace((unsigned char)*ep))
    923 			goto improper;
    924 		message = ftp_strdup(cp);
    925 
    926 				/* Read the rest of the header. */
    927 		while (1) {
    928 			alarmtimer(quit_time ? quit_time : 60);
    929 			len = fetch_getline(fin, buf, sizeof(buf), &errormsg);
    930 			alarmtimer(0);
    931 			if (len < 0) {
    932 				if (*errormsg == '\n')
    933 					errormsg++;
    934 				warnx("Receiving HTTP reply: %s", errormsg);
    935 				goto cleanup_fetch_url;
    936 			}
    937 			while (len > 0 && (ISLWS(buf[len-1])))
    938 				buf[--len] = '\0';
    939 			if (len == 0)
    940 				break;
    941 			DPRINTF("%s: received `%s'\n", __func__, buf);
    942 
    943 		/*
    944 		 * Look for some headers
    945 		 */
    946 
    947 			cp = buf;
    948 
    949 			if (match_token(&cp, "Content-Length:")) {
    950 				filesize = STRTOLL(cp, &ep, 10);
    951 				if (filesize < 0 || *ep != '\0')
    952 					goto improper;
    953 				DPRINTF("%s: parsed len as: " LLF "\n",
    954 				    __func__, (LLT)filesize);
    955 
    956 			} else if (match_token(&cp, "Content-Range:")) {
    957 				if (! match_token(&cp, "bytes"))
    958 					goto improper;
    959 
    960 				if (*cp == '*')
    961 					cp++;
    962 				else {
    963 					rangestart = STRTOLL(cp, &ep, 10);
    964 					if (rangestart < 0 || *ep != '-')
    965 						goto improper;
    966 					cp = ep + 1;
    967 					rangeend = STRTOLL(cp, &ep, 10);
    968 					if (rangeend < 0 || rangeend < rangestart)
    969 						goto improper;
    970 					cp = ep;
    971 				}
    972 				if (*cp != '/')
    973 					goto improper;
    974 				cp++;
    975 				if (*cp == '*')
    976 					cp++;
    977 				else {
    978 					entitylen = STRTOLL(cp, &ep, 10);
    979 					if (entitylen < 0)
    980 						goto improper;
    981 					cp = ep;
    982 				}
    983 				if (*cp != '\0')
    984 					goto improper;
    985 
    986 #ifndef NO_DEBUG
    987 				if (ftp_debug) {
    988 					fprintf(ttyout, "parsed range as: ");
    989 					if (rangestart == -1)
    990 						fprintf(ttyout, "*");
    991 					else
    992 						fprintf(ttyout, LLF "-" LLF,
    993 						    (LLT)rangestart,
    994 						    (LLT)rangeend);
    995 					fprintf(ttyout, "/" LLF "\n", (LLT)entitylen);
    996 				}
    997 #endif
    998 				if (! restart_point) {
    999 					warnx(
   1000 				    "Received unexpected Content-Range header");
   1001 					goto cleanup_fetch_url;
   1002 				}
   1003 
   1004 			} else if (match_token(&cp, "Last-Modified:")) {
   1005 				struct tm parsed;
   1006 				const char *t;
   1007 
   1008 				memset(&parsed, 0, sizeof(parsed));
   1009 				t = parse_rfc2616time(&parsed, cp);
   1010 				if (t != NULL) {
   1011 					parsed.tm_isdst = -1;
   1012 					if (*t == '\0')
   1013 						mtime = timegm(&parsed);
   1014 #ifndef NO_DEBUG
   1015 					if (ftp_debug && mtime != -1) {
   1016 						fprintf(ttyout,
   1017 						    "parsed time as: %s",
   1018 						rfc2822time(localtime(&mtime)));
   1019 					}
   1020 #endif
   1021 				}
   1022 
   1023 			} else if (match_token(&cp, "Location:")) {
   1024 				location = ftp_strdup(cp);
   1025 				DPRINTF("%s: parsed location as `%s'\n",
   1026 				    __func__, cp);
   1027 
   1028 			} else if (match_token(&cp, "Transfer-Encoding:")) {
   1029 				if (match_token(&cp, "binary")) {
   1030 					warnx(
   1031 			"Bogus transfer encoding `binary' (fetching anyway)");
   1032 					continue;
   1033 				}
   1034 				if (! (token = match_token(&cp, "chunked"))) {
   1035 					warnx(
   1036 				    "Unsupported transfer encoding `%s'",
   1037 					    token);
   1038 					goto cleanup_fetch_url;
   1039 				}
   1040 				ischunked++;
   1041 				DPRINTF("%s: using chunked encoding\n",
   1042 				    __func__);
   1043 
   1044 			} else if (match_token(&cp, "Proxy-Authenticate:")
   1045 				|| match_token(&cp, "WWW-Authenticate:")) {
   1046 				if (! (token = match_token(&cp, "Basic"))) {
   1047 					DPRINTF("%s: skipping unknown auth "
   1048 					    "scheme `%s'\n", __func__, token);
   1049 					continue;
   1050 				}
   1051 				FREEPTR(auth);
   1052 				auth = ftp_strdup(token);
   1053 				DPRINTF("%s: parsed auth as `%s'\n",
   1054 				    __func__, cp);
   1055 			}
   1056 
   1057 		}
   1058 				/* finished parsing header */
   1059 
   1060 		switch (hcode) {
   1061 		case 200:
   1062 			break;
   1063 		case 206:
   1064 			if (! restart_point) {
   1065 				warnx("Not expecting partial content header");
   1066 				goto cleanup_fetch_url;
   1067 			}
   1068 			break;
   1069 		case 300:
   1070 		case 301:
   1071 		case 302:
   1072 		case 303:
   1073 		case 305:
   1074 		case 307:
   1075 			if (EMPTYSTRING(location)) {
   1076 				warnx(
   1077 				"No redirection Location provided by server");
   1078 				goto cleanup_fetch_url;
   1079 			}
   1080 			if (redirect_loop++ > 5) {
   1081 				warnx("Too many redirections requested");
   1082 				goto cleanup_fetch_url;
   1083 			}
   1084 			if (hcode == 305) {
   1085 				if (verbose)
   1086 					fprintf(ttyout, "Redirected via %s\n",
   1087 					    location);
   1088 				rval = fetch_url(url, location,
   1089 				    proxyauth, wwwauth);
   1090 			} else {
   1091 				if (verbose)
   1092 					fprintf(ttyout, "Redirected to %s\n",
   1093 					    location);
   1094 				rval = go_fetch(location);
   1095 			}
   1096 			goto cleanup_fetch_url;
   1097 #ifndef NO_AUTH
   1098 		case 401:
   1099 		case 407:
   1100 		    {
   1101 			char **authp;
   1102 			char *auser, *apass;
   1103 
   1104 			if (hcode == 401) {
   1105 				authp = &wwwauth;
   1106 				auser = uuser;
   1107 				apass = pass;
   1108 			} else {
   1109 				authp = &proxyauth;
   1110 				auser = puser;
   1111 				apass = ppass;
   1112 			}
   1113 			if (verbose || *authp == NULL ||
   1114 			    auser == NULL || apass == NULL)
   1115 				fprintf(ttyout, "%s\n", message);
   1116 			if (EMPTYSTRING(auth)) {
   1117 				warnx(
   1118 			    "No authentication challenge provided by server");
   1119 				goto cleanup_fetch_url;
   1120 			}
   1121 			if (*authp != NULL) {
   1122 				char reply[10];
   1123 
   1124 				fprintf(ttyout,
   1125 				    "Authorization failed. Retry (y/n)? ");
   1126 				if (get_line(stdin, reply, sizeof(reply), NULL)
   1127 				    < 0) {
   1128 					goto cleanup_fetch_url;
   1129 				}
   1130 				if (tolower((unsigned char)reply[0]) != 'y')
   1131 					goto cleanup_fetch_url;
   1132 				auser = NULL;
   1133 				apass = NULL;
   1134 			}
   1135 			if (auth_url(auth, authp, auser, apass) == 0) {
   1136 				rval = fetch_url(url, penv,
   1137 				    proxyauth, wwwauth);
   1138 				memset(*authp, 0, strlen(*authp));
   1139 				FREEPTR(*authp);
   1140 			}
   1141 			goto cleanup_fetch_url;
   1142 		    }
   1143 #endif
   1144 		default:
   1145 			if (message)
   1146 				warnx("Error retrieving file `%s'", message);
   1147 			else
   1148 				warnx("Unknown error retrieving file");
   1149 			goto cleanup_fetch_url;
   1150 		}
   1151 	}		/* end of ftp:// or http:// specific setup */
   1152 
   1153 			/* Open the output file. */
   1154 	if (strcmp(savefile, "-") == 0) {
   1155 		fout = stdout;
   1156 	} else if (*savefile == '|') {
   1157 		oldpipe = xsignal(SIGPIPE, SIG_IGN);
   1158 		fout = popen(savefile + 1, "w");
   1159 		if (fout == NULL) {
   1160 			warn("Can't execute `%s'", savefile + 1);
   1161 			goto cleanup_fetch_url;
   1162 		}
   1163 		closefunc = pclose;
   1164 	} else {
   1165 		if ((rangeend != -1 && rangeend <= restart_point) ||
   1166 		    (rangestart == -1 && filesize != -1 && filesize <= restart_point)) {
   1167 			/* already done */
   1168 			if (verbose)
   1169 				fprintf(ttyout, "already done\n");
   1170 			rval = 0;
   1171 			goto cleanup_fetch_url;
   1172 		}
   1173 		if (restart_point && rangestart != -1) {
   1174 			if (entitylen != -1)
   1175 				filesize = entitylen;
   1176 			if (rangestart != restart_point) {
   1177 				warnx(
   1178 				    "Size of `%s' differs from save file `%s'",
   1179 				    url, savefile);
   1180 				goto cleanup_fetch_url;
   1181 			}
   1182 			fout = fopen(savefile, "a");
   1183 		} else
   1184 			fout = fopen(savefile, "w");
   1185 		if (fout == NULL) {
   1186 			warn("Can't open `%s'", savefile);
   1187 			goto cleanup_fetch_url;
   1188 		}
   1189 		closefunc = fclose;
   1190 	}
   1191 
   1192 			/* Trap signals */
   1193 	oldquit = xsignal(SIGQUIT, psummary);
   1194 	oldint = xsignal(SIGINT, aborthttp);
   1195 
   1196 	assert(rcvbuf_size > 0);
   1197 	if ((size_t)rcvbuf_size > bufsize) {
   1198 		if (xferbuf)
   1199 			(void)free(xferbuf);
   1200 		bufsize = rcvbuf_size;
   1201 		xferbuf = ftp_malloc(bufsize);
   1202 	}
   1203 
   1204 	bytes = 0;
   1205 	hashbytes = mark;
   1206 	if (oldalrm) {
   1207 		(void)xsignal(SIGALRM, oldalrm);
   1208 		oldalrm = NULL;
   1209 	}
   1210 	progressmeter(-1);
   1211 
   1212 			/* Finally, suck down the file. */
   1213 	do {
   1214 		long chunksize;
   1215 		short lastchunk;
   1216 
   1217 		chunksize = 0;
   1218 		lastchunk = 0;
   1219 					/* read chunk-size */
   1220 		if (ischunked) {
   1221 			if (fetch_getln(xferbuf, bufsize, fin) == NULL) {
   1222 				warnx("Unexpected EOF reading chunk-size");
   1223 				goto cleanup_fetch_url;
   1224 			}
   1225 			errno = 0;
   1226 			chunksize = strtol(xferbuf, &ep, 16);
   1227 			if (ep == xferbuf) {
   1228 				warnx("Invalid chunk-size");
   1229 				goto cleanup_fetch_url;
   1230 			}
   1231 			if (errno == ERANGE || chunksize < 0) {
   1232 				errno = ERANGE;
   1233 				warn("Chunk-size `%.*s'",
   1234 				    (int)(ep-xferbuf), xferbuf);
   1235 				goto cleanup_fetch_url;
   1236 			}
   1237 
   1238 				/*
   1239 				 * XXX:	Work around bug in Apache 1.3.9 and
   1240 				 *	1.3.11, which incorrectly put trailing
   1241 				 *	space after the chunk-size.
   1242 				 */
   1243 			while (*ep == ' ')
   1244 				ep++;
   1245 
   1246 					/* skip [ chunk-ext ] */
   1247 			if (*ep == ';') {
   1248 				while (*ep && *ep != '\r')
   1249 					ep++;
   1250 			}
   1251 
   1252 			if (strcmp(ep, "\r\n") != 0) {
   1253 				warnx("Unexpected data following chunk-size");
   1254 				goto cleanup_fetch_url;
   1255 			}
   1256 			DPRINTF("%s: got chunk-size of " LLF "\n", __func__,
   1257 			    (LLT)chunksize);
   1258 			if (chunksize == 0) {
   1259 				lastchunk = 1;
   1260 				goto chunkdone;
   1261 			}
   1262 		}
   1263 					/* transfer file or chunk */
   1264 		while (1) {
   1265 			struct timeval then, now, td;
   1266 			off_t bufrem;
   1267 
   1268 			if (rate_get)
   1269 				(void)gettimeofday(&then, NULL);
   1270 			bufrem = rate_get ? rate_get : (off_t)bufsize;
   1271 			if (ischunked)
   1272 				bufrem = MIN(chunksize, bufrem);
   1273 			while (bufrem > 0) {
   1274 				flen = fetch_read(xferbuf, sizeof(char),
   1275 				    MIN((off_t)bufsize, bufrem), fin);
   1276 				if (flen <= 0)
   1277 					goto chunkdone;
   1278 				bytes += flen;
   1279 				bufrem -= flen;
   1280 				if (fwrite(xferbuf, sizeof(char), flen, fout)
   1281 				    != flen) {
   1282 					warn("Writing `%s'", savefile);
   1283 					goto cleanup_fetch_url;
   1284 				}
   1285 				if (hash && !progress) {
   1286 					while (bytes >= hashbytes) {
   1287 						(void)putc('#', ttyout);
   1288 						hashbytes += mark;
   1289 					}
   1290 					(void)fflush(ttyout);
   1291 				}
   1292 				if (ischunked) {
   1293 					chunksize -= flen;
   1294 					if (chunksize <= 0)
   1295 						break;
   1296 				}
   1297 			}
   1298 			if (rate_get) {
   1299 				while (1) {
   1300 					(void)gettimeofday(&now, NULL);
   1301 					timersub(&now, &then, &td);
   1302 					if (td.tv_sec > 0)
   1303 						break;
   1304 					usleep(1000000 - td.tv_usec);
   1305 				}
   1306 			}
   1307 			if (ischunked && chunksize <= 0)
   1308 				break;
   1309 		}
   1310 					/* read CRLF after chunk*/
   1311  chunkdone:
   1312 		if (ischunked) {
   1313 			if (fetch_getln(xferbuf, bufsize, fin) == NULL) {
   1314 				alarmtimer(0);
   1315 				warnx("Unexpected EOF reading chunk CRLF");
   1316 				goto cleanup_fetch_url;
   1317 			}
   1318 			if (strcmp(xferbuf, "\r\n") != 0) {
   1319 				warnx("Unexpected data following chunk");
   1320 				goto cleanup_fetch_url;
   1321 			}
   1322 			if (lastchunk)
   1323 				break;
   1324 		}
   1325 	} while (ischunked);
   1326 
   1327 /* XXX: deal with optional trailer & CRLF here? */
   1328 
   1329 	if (hash && !progress && bytes > 0) {
   1330 		if (bytes < mark)
   1331 			(void)putc('#', ttyout);
   1332 		(void)putc('\n', ttyout);
   1333 	}
   1334 	if (fetch_error(fin)) {
   1335 		warn("Reading file");
   1336 		goto cleanup_fetch_url;
   1337 	}
   1338 	progressmeter(1);
   1339 	(void)fflush(fout);
   1340 	if (closefunc == fclose && mtime != -1) {
   1341 		struct timeval tval[2];
   1342 
   1343 		(void)gettimeofday(&tval[0], NULL);
   1344 		tval[1].tv_sec = mtime;
   1345 		tval[1].tv_usec = 0;
   1346 		(*closefunc)(fout);
   1347 		fout = NULL;
   1348 
   1349 		if (utimes(savefile, tval) == -1) {
   1350 			fprintf(ttyout,
   1351 			    "Can't change modification time to %s",
   1352 			    rfc2822time(localtime(&mtime)));
   1353 		}
   1354 	}
   1355 	if (bytes > 0)
   1356 		ptransfer(0);
   1357 	bytes = 0;
   1358 
   1359 	rval = 0;
   1360 	goto cleanup_fetch_url;
   1361 
   1362  improper:
   1363 	warnx("Improper response from `%s:%s'", host, port);
   1364 
   1365  cleanup_fetch_url:
   1366 	if (oldint)
   1367 		(void)xsignal(SIGINT, oldint);
   1368 	if (oldpipe)
   1369 		(void)xsignal(SIGPIPE, oldpipe);
   1370 	if (oldalrm)
   1371 		(void)xsignal(SIGALRM, oldalrm);
   1372 	if (oldquit)
   1373 		(void)xsignal(SIGQUIT, oldpipe);
   1374 	if (fin != NULL)
   1375 		fetch_close(fin);
   1376 	else if (s != -1)
   1377 		close(s);
   1378 	if (closefunc != NULL && fout != NULL)
   1379 		(*closefunc)(fout);
   1380 	if (res0)
   1381 		freeaddrinfo(res0);
   1382 	FREEPTR(savefile);
   1383 	FREEPTR(uuser);
   1384 	if (pass != NULL)
   1385 		memset(pass, 0, strlen(pass));
   1386 	FREEPTR(pass);
   1387 	FREEPTR(host);
   1388 	FREEPTR(port);
   1389 	FREEPTR(path);
   1390 	FREEPTR(decodedpath);
   1391 	FREEPTR(puser);
   1392 	if (ppass != NULL)
   1393 		memset(ppass, 0, strlen(ppass));
   1394 	FREEPTR(ppass);
   1395 	FREEPTR(auth);
   1396 	FREEPTR(location);
   1397 	FREEPTR(message);
   1398 	return (rval);
   1399 }
   1400 
   1401 /*
   1402  * Abort a HTTP retrieval
   1403  */
   1404 static void
   1405 aborthttp(int notused)
   1406 {
   1407 	char msgbuf[100];
   1408 	int len;
   1409 
   1410 	sigint_raised = 1;
   1411 	alarmtimer(0);
   1412 	if (fromatty) {
   1413 		len = snprintf(msgbuf, sizeof(msgbuf),
   1414 		    "\n%s: HTTP fetch aborted.\n", getprogname());
   1415 		if (len > 0)
   1416 			write(fileno(ttyout), msgbuf, len);
   1417 	}
   1418 	siglongjmp(httpabort, 1);
   1419 }
   1420 
   1421 static void
   1422 timeouthttp(int notused)
   1423 {
   1424 	char msgbuf[100];
   1425 	int len;
   1426 
   1427 	alarmtimer(0);
   1428 	if (fromatty) {
   1429 		len = snprintf(msgbuf, sizeof(msgbuf),
   1430 		    "\n%s: HTTP fetch timeout.\n", getprogname());
   1431 		if (len > 0)
   1432 			write(fileno(ttyout), msgbuf, len);
   1433 	}
   1434 	siglongjmp(httpabort, 1);
   1435 }
   1436 
   1437 /*
   1438  * Retrieve ftp URL or classic ftp argument using FTP.
   1439  * Returns 1 on failure, 0 on completed xfer, -1 if ftp connection
   1440  * is still open (e.g, ftp xfer with trailing /)
   1441  */
   1442 static int
   1443 fetch_ftp(const char *url)
   1444 {
   1445 	char		*cp, *xargv[5], rempath[MAXPATHLEN];
   1446 	char		*host, *path, *dir, *file, *uuser, *pass;
   1447 	char		*port;
   1448 	char		 cmdbuf[MAXPATHLEN];
   1449 	char		 dirbuf[4];
   1450 	int		 dirhasglob, filehasglob, rval, transtype, xargc;
   1451 	int		 oanonftp, oautologin;
   1452 	in_port_t	 portnum;
   1453 	url_t		 urltype;
   1454 
   1455 	DPRINTF("fetch_ftp: `%s'\n", url);
   1456 	host = path = dir = file = uuser = pass = NULL;
   1457 	port = NULL;
   1458 	rval = 1;
   1459 	transtype = TYPE_I;
   1460 
   1461 	if (STRNEQUAL(url, FTP_URL)) {
   1462 		if ((parse_url(url, "URL", &urltype, &uuser, &pass,
   1463 		    &host, &port, &portnum, &path) == -1) ||
   1464 		    (uuser != NULL && *uuser == '\0') ||
   1465 		    EMPTYSTRING(host)) {
   1466 			warnx("Invalid URL `%s'", url);
   1467 			goto cleanup_fetch_ftp;
   1468 		}
   1469 		/*
   1470 		 * Note: Don't url_decode(path) here.  We need to keep the
   1471 		 * distinction between "/" and "%2F" until later.
   1472 		 */
   1473 
   1474 					/* check for trailing ';type=[aid]' */
   1475 		if (! EMPTYSTRING(path) && (cp = strrchr(path, ';')) != NULL) {
   1476 			if (strcasecmp(cp, ";type=a") == 0)
   1477 				transtype = TYPE_A;
   1478 			else if (strcasecmp(cp, ";type=i") == 0)
   1479 				transtype = TYPE_I;
   1480 			else if (strcasecmp(cp, ";type=d") == 0) {
   1481 				warnx(
   1482 			    "Directory listing via a URL is not supported");
   1483 				goto cleanup_fetch_ftp;
   1484 			} else {
   1485 				warnx("Invalid suffix `%s' in URL `%s'", cp,
   1486 				    url);
   1487 				goto cleanup_fetch_ftp;
   1488 			}
   1489 			*cp = 0;
   1490 		}
   1491 	} else {			/* classic style `[user@]host:[file]' */
   1492 		urltype = CLASSIC_URL_T;
   1493 		host = ftp_strdup(url);
   1494 		cp = strchr(host, '@');
   1495 		if (cp != NULL) {
   1496 			*cp = '\0';
   1497 			uuser = host;
   1498 			anonftp = 0;	/* disable anonftp */
   1499 			host = ftp_strdup(cp + 1);
   1500 		}
   1501 		cp = strchr(host, ':');
   1502 		if (cp != NULL) {
   1503 			*cp = '\0';
   1504 			path = ftp_strdup(cp + 1);
   1505 		}
   1506 	}
   1507 	if (EMPTYSTRING(host))
   1508 		goto cleanup_fetch_ftp;
   1509 
   1510 			/* Extract the file and (if present) directory name. */
   1511 	dir = path;
   1512 	if (! EMPTYSTRING(dir)) {
   1513 		/*
   1514 		 * If we are dealing with classic `[user@]host:[path]' syntax,
   1515 		 * then a path of the form `/file' (resulting from input of the
   1516 		 * form `host:/file') means that we should do "CWD /" before
   1517 		 * retrieving the file.  So we set dir="/" and file="file".
   1518 		 *
   1519 		 * But if we are dealing with URLs like `ftp://host/path' then
   1520 		 * a path of the form `/file' (resulting from a URL of the form
   1521 		 * `ftp://host//file') means that we should do `CWD ' (with an
   1522 		 * empty argument) before retrieving the file.  So we set
   1523 		 * dir="" and file="file".
   1524 		 *
   1525 		 * If the path does not contain / at all, we set dir=NULL.
   1526 		 * (We get a path without any slashes if we are dealing with
   1527 		 * classic `[user@]host:[file]' or URL `ftp://host/file'.)
   1528 		 *
   1529 		 * In all other cases, we set dir to a string that does not
   1530 		 * include the final '/' that separates the dir part from the
   1531 		 * file part of the path.  (This will be the empty string if
   1532 		 * and only if we are dealing with a path of the form `/file'
   1533 		 * resulting from an URL of the form `ftp://host//file'.)
   1534 		 */
   1535 		cp = strrchr(dir, '/');
   1536 		if (cp == dir && urltype == CLASSIC_URL_T) {
   1537 			file = cp + 1;
   1538 			(void)strlcpy(dirbuf, "/", sizeof(dirbuf));
   1539 			dir = dirbuf;
   1540 		} else if (cp != NULL) {
   1541 			*cp++ = '\0';
   1542 			file = cp;
   1543 		} else {
   1544 			file = dir;
   1545 			dir = NULL;
   1546 		}
   1547 	} else
   1548 		dir = NULL;
   1549 	if (urltype == FTP_URL_T && file != NULL) {
   1550 		url_decode(file);
   1551 		/* but still don't url_decode(dir) */
   1552 	}
   1553 	DPRINTF("fetch_ftp: user `%s' pass `%s' host %s port %s "
   1554 	    "path `%s' dir `%s' file `%s'\n",
   1555 	    STRorNULL(uuser), STRorNULL(pass),
   1556 	    STRorNULL(host), STRorNULL(port),
   1557 	    STRorNULL(path), STRorNULL(dir), STRorNULL(file));
   1558 
   1559 	dirhasglob = filehasglob = 0;
   1560 	if (doglob && urltype == CLASSIC_URL_T) {
   1561 		if (! EMPTYSTRING(dir) && strpbrk(dir, "*?[]{}") != NULL)
   1562 			dirhasglob = 1;
   1563 		if (! EMPTYSTRING(file) && strpbrk(file, "*?[]{}") != NULL)
   1564 			filehasglob = 1;
   1565 	}
   1566 
   1567 			/* Set up the connection */
   1568 	oanonftp = anonftp;
   1569 	if (connected)
   1570 		disconnect(0, NULL);
   1571 	anonftp = oanonftp;
   1572 	(void)strlcpy(cmdbuf, getprogname(), sizeof(cmdbuf));
   1573 	xargv[0] = cmdbuf;
   1574 	xargv[1] = host;
   1575 	xargv[2] = NULL;
   1576 	xargc = 2;
   1577 	if (port) {
   1578 		xargv[2] = port;
   1579 		xargv[3] = NULL;
   1580 		xargc = 3;
   1581 	}
   1582 	oautologin = autologin;
   1583 		/* don't autologin in setpeer(), use ftp_login() below */
   1584 	autologin = 0;
   1585 	setpeer(xargc, xargv);
   1586 	autologin = oautologin;
   1587 	if ((connected == 0) ||
   1588 	    (connected == 1 && !ftp_login(host, uuser, pass))) {
   1589 		warnx("Can't connect or login to host `%s:%s'",
   1590 			host, port ? port : "?");
   1591 		goto cleanup_fetch_ftp;
   1592 	}
   1593 
   1594 	switch (transtype) {
   1595 	case TYPE_A:
   1596 		setascii(1, xargv);
   1597 		break;
   1598 	case TYPE_I:
   1599 		setbinary(1, xargv);
   1600 		break;
   1601 	default:
   1602 		errx(1, "fetch_ftp: unknown transfer type %d", transtype);
   1603 	}
   1604 
   1605 		/*
   1606 		 * Change directories, if necessary.
   1607 		 *
   1608 		 * Note: don't use EMPTYSTRING(dir) below, because
   1609 		 * dir=="" means something different from dir==NULL.
   1610 		 */
   1611 	if (dir != NULL && !dirhasglob) {
   1612 		char *nextpart;
   1613 
   1614 		/*
   1615 		 * If we are dealing with a classic `[user@]host:[path]'
   1616 		 * (urltype is CLASSIC_URL_T) then we have a raw directory
   1617 		 * name (not encoded in any way) and we can change
   1618 		 * directories in one step.
   1619 		 *
   1620 		 * If we are dealing with an `ftp://host/path' URL
   1621 		 * (urltype is FTP_URL_T), then RFC 3986 says we need to
   1622 		 * send a separate CWD command for each unescaped "/"
   1623 		 * in the path, and we have to interpret %hex escaping
   1624 		 * *after* we find the slashes.  It's possible to get
   1625 		 * empty components here, (from multiple adjacent
   1626 		 * slashes in the path) and RFC 3986 says that we should
   1627 		 * still do `CWD ' (with a null argument) in such cases.
   1628 		 *
   1629 		 * Many ftp servers don't support `CWD ', so if there's an
   1630 		 * error performing that command, bail out with a descriptive
   1631 		 * message.
   1632 		 *
   1633 		 * Examples:
   1634 		 *
   1635 		 * host:			dir="", urltype=CLASSIC_URL_T
   1636 		 *		logged in (to default directory)
   1637 		 * host:file			dir=NULL, urltype=CLASSIC_URL_T
   1638 		 *		"RETR file"
   1639 		 * host:dir/			dir="dir", urltype=CLASSIC_URL_T
   1640 		 *		"CWD dir", logged in
   1641 		 * ftp://host/			dir="", urltype=FTP_URL_T
   1642 		 *		logged in (to default directory)
   1643 		 * ftp://host/dir/		dir="dir", urltype=FTP_URL_T
   1644 		 *		"CWD dir", logged in
   1645 		 * ftp://host/file		dir=NULL, urltype=FTP_URL_T
   1646 		 *		"RETR file"
   1647 		 * ftp://host//file		dir="", urltype=FTP_URL_T
   1648 		 *		"CWD ", "RETR file"
   1649 		 * host:/file			dir="/", urltype=CLASSIC_URL_T
   1650 		 *		"CWD /", "RETR file"
   1651 		 * ftp://host///file		dir="/", urltype=FTP_URL_T
   1652 		 *		"CWD ", "CWD ", "RETR file"
   1653 		 * ftp://host/%2F/file		dir="%2F", urltype=FTP_URL_T
   1654 		 *		"CWD /", "RETR file"
   1655 		 * ftp://host/foo/file		dir="foo", urltype=FTP_URL_T
   1656 		 *		"CWD foo", "RETR file"
   1657 		 * ftp://host/foo/bar/file	dir="foo/bar"
   1658 		 *		"CWD foo", "CWD bar", "RETR file"
   1659 		 * ftp://host//foo/bar/file	dir="/foo/bar"
   1660 		 *		"CWD ", "CWD foo", "CWD bar", "RETR file"
   1661 		 * ftp://host/foo//bar/file	dir="foo//bar"
   1662 		 *		"CWD foo", "CWD ", "CWD bar", "RETR file"
   1663 		 * ftp://host/%2F/foo/bar/file	dir="%2F/foo/bar"
   1664 		 *		"CWD /", "CWD foo", "CWD bar", "RETR file"
   1665 		 * ftp://host/%2Ffoo/bar/file	dir="%2Ffoo/bar"
   1666 		 *		"CWD /foo", "CWD bar", "RETR file"
   1667 		 * ftp://host/%2Ffoo%2Fbar/file	dir="%2Ffoo%2Fbar"
   1668 		 *		"CWD /foo/bar", "RETR file"
   1669 		 * ftp://host/%2Ffoo%2Fbar%2Ffile	dir=NULL
   1670 		 *		"RETR /foo/bar/file"
   1671 		 *
   1672 		 * Note that we don't need `dir' after this point.
   1673 		 */
   1674 		do {
   1675 			if (urltype == FTP_URL_T) {
   1676 				nextpart = strchr(dir, '/');
   1677 				if (nextpart) {
   1678 					*nextpart = '\0';
   1679 					nextpart++;
   1680 				}
   1681 				url_decode(dir);
   1682 			} else
   1683 				nextpart = NULL;
   1684 			DPRINTF("fetch_ftp: dir `%s', nextpart `%s'\n",
   1685 			    STRorNULL(dir), STRorNULL(nextpart));
   1686 			if (urltype == FTP_URL_T || *dir != '\0') {
   1687 				(void)strlcpy(cmdbuf, "cd", sizeof(cmdbuf));
   1688 				xargv[0] = cmdbuf;
   1689 				xargv[1] = dir;
   1690 				xargv[2] = NULL;
   1691 				dirchange = 0;
   1692 				cd(2, xargv);
   1693 				if (! dirchange) {
   1694 					if (*dir == '\0' && code == 500)
   1695 						fprintf(stderr,
   1696 "\n"
   1697 "ftp: The `CWD ' command (without a directory), which is required by\n"
   1698 "     RFC 3986 to support the empty directory in the URL pathname (`//'),\n"
   1699 "     conflicts with the server's conformance to RFC 959.\n"
   1700 "     Try the same URL without the `//' in the URL pathname.\n"
   1701 "\n");
   1702 					goto cleanup_fetch_ftp;
   1703 				}
   1704 			}
   1705 			dir = nextpart;
   1706 		} while (dir != NULL);
   1707 	}
   1708 
   1709 	if (EMPTYSTRING(file)) {
   1710 		rval = -1;
   1711 		goto cleanup_fetch_ftp;
   1712 	}
   1713 
   1714 	if (dirhasglob) {
   1715 		(void)strlcpy(rempath, dir,	sizeof(rempath));
   1716 		(void)strlcat(rempath, "/",	sizeof(rempath));
   1717 		(void)strlcat(rempath, file,	sizeof(rempath));
   1718 		file = rempath;
   1719 	}
   1720 
   1721 			/* Fetch the file(s). */
   1722 	xargc = 2;
   1723 	(void)strlcpy(cmdbuf, "get", sizeof(cmdbuf));
   1724 	xargv[0] = cmdbuf;
   1725 	xargv[1] = file;
   1726 	xargv[2] = NULL;
   1727 	if (dirhasglob || filehasglob) {
   1728 		int ointeractive;
   1729 
   1730 		ointeractive = interactive;
   1731 		interactive = 0;
   1732 		if (restartautofetch)
   1733 			(void)strlcpy(cmdbuf, "mreget", sizeof(cmdbuf));
   1734 		else
   1735 			(void)strlcpy(cmdbuf, "mget", sizeof(cmdbuf));
   1736 		xargv[0] = cmdbuf;
   1737 		mget(xargc, xargv);
   1738 		interactive = ointeractive;
   1739 	} else {
   1740 		if (outfile == NULL) {
   1741 			cp = strrchr(file, '/');	/* find savefile */
   1742 			if (cp != NULL)
   1743 				outfile = cp + 1;
   1744 			else
   1745 				outfile = file;
   1746 		}
   1747 		xargv[2] = (char *)outfile;
   1748 		xargv[3] = NULL;
   1749 		xargc++;
   1750 		if (restartautofetch)
   1751 			reget(xargc, xargv);
   1752 		else
   1753 			get(xargc, xargv);
   1754 	}
   1755 
   1756 	if ((code / 100) == COMPLETE)
   1757 		rval = 0;
   1758 
   1759  cleanup_fetch_ftp:
   1760 	FREEPTR(port);
   1761 	FREEPTR(host);
   1762 	FREEPTR(path);
   1763 	FREEPTR(uuser);
   1764 	if (pass)
   1765 		memset(pass, 0, strlen(pass));
   1766 	FREEPTR(pass);
   1767 	return (rval);
   1768 }
   1769 
   1770 /*
   1771  * Retrieve the given file to outfile.
   1772  * Supports arguments of the form:
   1773  *	"host:path", "ftp://host/path"	if $ftpproxy, call fetch_url() else
   1774  *					call fetch_ftp()
   1775  *	"http://host/path"		call fetch_url() to use HTTP
   1776  *	"file:///path"			call fetch_url() to copy
   1777  *	"about:..."			print a message
   1778  *
   1779  * Returns 1 on failure, 0 on completed xfer, -1 if ftp connection
   1780  * is still open (e.g, ftp xfer with trailing /)
   1781  */
   1782 static int
   1783 go_fetch(const char *url)
   1784 {
   1785 	char *proxyenv;
   1786 	char *p;
   1787 
   1788 #ifndef NO_ABOUT
   1789 	/*
   1790 	 * Check for about:*
   1791 	 */
   1792 	if (STRNEQUAL(url, ABOUT_URL)) {
   1793 		url += sizeof(ABOUT_URL) -1;
   1794 		if (strcasecmp(url, "ftp") == 0 ||
   1795 		    strcasecmp(url, "tnftp") == 0) {
   1796 			fputs(
   1797 "This version of ftp has been enhanced by Luke Mewburn <lukem (at) NetBSD.org>\n"
   1798 "for the NetBSD project.  Execute `man ftp' for more details.\n", ttyout);
   1799 		} else if (strcasecmp(url, "lukem") == 0) {
   1800 			fputs(
   1801 "Luke Mewburn is the author of most of the enhancements in this ftp client.\n"
   1802 "Please email feedback to <lukem (at) NetBSD.org>.\n", ttyout);
   1803 		} else if (strcasecmp(url, "netbsd") == 0) {
   1804 			fputs(
   1805 "NetBSD is a freely available and redistributable UNIX-like operating system.\n"
   1806 "For more information, see http://www.NetBSD.org/\n", ttyout);
   1807 		} else if (strcasecmp(url, "version") == 0) {
   1808 			fprintf(ttyout, "Version: %s %s%s\n",
   1809 			    FTP_PRODUCT, FTP_VERSION,
   1810 #ifdef INET6
   1811 			    ""
   1812 #else
   1813 			    " (-IPv6)"
   1814 #endif
   1815 			);
   1816 		} else {
   1817 			fprintf(ttyout, "`%s' is an interesting topic.\n", url);
   1818 		}
   1819 		fputs("\n", ttyout);
   1820 		return (0);
   1821 	}
   1822 #endif
   1823 
   1824 	/*
   1825 	 * Check for file:// and http:// URLs.
   1826 	 */
   1827 	if (STRNEQUAL(url, HTTP_URL)
   1828 #ifdef WITH_SSL
   1829 	    || STRNEQUAL(url, HTTPS_URL)
   1830 #endif
   1831 	    || STRNEQUAL(url, FILE_URL))
   1832 		return (fetch_url(url, NULL, NULL, NULL));
   1833 
   1834 	/*
   1835 	 * If it contains "://" but does not begin with ftp://
   1836 	 * or something that was already handled, then it's
   1837 	 * unsupported.
   1838 	 *
   1839 	 * If it contains ":" but not "://" then we assume the
   1840 	 * part before the colon is a host name, not an URL scheme,
   1841 	 * so we don't try to match that here.
   1842 	 */
   1843 	if ((p = strstr(url, "://")) != NULL && ! STRNEQUAL(url, FTP_URL))
   1844 		errx(1, "Unsupported URL scheme `%.*s'", (int)(p - url), url);
   1845 
   1846 	/*
   1847 	 * Try FTP URL-style and host:file arguments next.
   1848 	 * If ftpproxy is set with an FTP URL, use fetch_url()
   1849 	 * Othewise, use fetch_ftp().
   1850 	 */
   1851 	proxyenv = getoptionvalue("ftp_proxy");
   1852 	if (!EMPTYSTRING(proxyenv) && STRNEQUAL(url, FTP_URL))
   1853 		return (fetch_url(url, NULL, NULL, NULL));
   1854 
   1855 	return (fetch_ftp(url));
   1856 }
   1857 
   1858 /*
   1859  * Retrieve multiple files from the command line,
   1860  * calling go_fetch() for each file.
   1861  *
   1862  * If an ftp path has a trailing "/", the path will be cd-ed into and
   1863  * the connection remains open, and the function will return -1
   1864  * (to indicate the connection is alive).
   1865  * If an error occurs the return value will be the offset+1 in
   1866  * argv[] of the file that caused a problem (i.e, argv[x]
   1867  * returns x+1)
   1868  * Otherwise, 0 is returned if all files retrieved successfully.
   1869  */
   1870 int
   1871 auto_fetch(int argc, char *argv[])
   1872 {
   1873 	volatile int	argpos, rval;
   1874 
   1875 	argpos = rval = 0;
   1876 
   1877 	if (sigsetjmp(toplevel, 1)) {
   1878 		if (connected)
   1879 			disconnect(0, NULL);
   1880 		if (rval > 0)
   1881 			rval = argpos + 1;
   1882 		return (rval);
   1883 	}
   1884 	(void)xsignal(SIGINT, intr);
   1885 	(void)xsignal(SIGPIPE, lostpeer);
   1886 
   1887 	/*
   1888 	 * Loop through as long as there's files to fetch.
   1889 	 */
   1890 	for (; (rval == 0) && (argpos < argc); argpos++) {
   1891 		if (strchr(argv[argpos], ':') == NULL)
   1892 			break;
   1893 		redirect_loop = 0;
   1894 		if (!anonftp)
   1895 			anonftp = 2;	/* Handle "automatic" transfers. */
   1896 		rval = go_fetch(argv[argpos]);
   1897 		if (outfile != NULL && strcmp(outfile, "-") != 0
   1898 		    && outfile[0] != '|')
   1899 			outfile = NULL;
   1900 		if (rval > 0)
   1901 			rval = argpos + 1;
   1902 	}
   1903 
   1904 	if (connected && rval != -1)
   1905 		disconnect(0, NULL);
   1906 	return (rval);
   1907 }
   1908 
   1909 
   1910 /*
   1911  * Upload multiple files from the command line.
   1912  *
   1913  * If an error occurs the return value will be the offset+1 in
   1914  * argv[] of the file that caused a problem (i.e, argv[x]
   1915  * returns x+1)
   1916  * Otherwise, 0 is returned if all files uploaded successfully.
   1917  */
   1918 int
   1919 auto_put(int argc, char **argv, const char *uploadserver)
   1920 {
   1921 	char	*uargv[4], *path, *pathsep;
   1922 	int	 uargc, rval, argpos;
   1923 	size_t	 len;
   1924 	char	 cmdbuf[MAX_C_NAME];
   1925 
   1926 	(void)strlcpy(cmdbuf, "mput", sizeof(cmdbuf));
   1927 	uargv[0] = cmdbuf;
   1928 	uargv[1] = argv[0];
   1929 	uargc = 2;
   1930 	uargv[2] = uargv[3] = NULL;
   1931 	pathsep = NULL;
   1932 	rval = 1;
   1933 
   1934 	DPRINTF("auto_put: target `%s'\n", uploadserver);
   1935 
   1936 	path = ftp_strdup(uploadserver);
   1937 	len = strlen(path);
   1938 	if (path[len - 1] != '/' && path[len - 1] != ':') {
   1939 			/*
   1940 			 * make sure we always pass a directory to auto_fetch
   1941 			 */
   1942 		if (argc > 1) {		/* more than one file to upload */
   1943 			len = strlen(uploadserver) + 2;	/* path + "/" + "\0" */
   1944 			free(path);
   1945 			path = (char *)ftp_malloc(len);
   1946 			(void)strlcpy(path, uploadserver, len);
   1947 			(void)strlcat(path, "/", len);
   1948 		} else {		/* single file to upload */
   1949 			(void)strlcpy(cmdbuf, "put", sizeof(cmdbuf));
   1950 			uargv[0] = cmdbuf;
   1951 			pathsep = strrchr(path, '/');
   1952 			if (pathsep == NULL) {
   1953 				pathsep = strrchr(path, ':');
   1954 				if (pathsep == NULL) {
   1955 					warnx("Invalid URL `%s'", path);
   1956 					goto cleanup_auto_put;
   1957 				}
   1958 				pathsep++;
   1959 				uargv[2] = ftp_strdup(pathsep);
   1960 				pathsep[0] = '/';
   1961 			} else
   1962 				uargv[2] = ftp_strdup(pathsep + 1);
   1963 			pathsep[1] = '\0';
   1964 			uargc++;
   1965 		}
   1966 	}
   1967 	DPRINTF("auto_put: URL `%s' argv[2] `%s'\n",
   1968 	    path, STRorNULL(uargv[2]));
   1969 
   1970 			/* connect and cwd */
   1971 	rval = auto_fetch(1, &path);
   1972 	if(rval >= 0)
   1973 		goto cleanup_auto_put;
   1974 
   1975 	rval = 0;
   1976 
   1977 			/* target filename provided; upload 1 file */
   1978 			/* XXX : is this the best way? */
   1979 	if (uargc == 3) {
   1980 		uargv[1] = argv[0];
   1981 		put(uargc, uargv);
   1982 		if ((code / 100) != COMPLETE)
   1983 			rval = 1;
   1984 	} else {	/* otherwise a target dir: upload all files to it */
   1985 		for(argpos = 0; argv[argpos] != NULL; argpos++) {
   1986 			uargv[1] = argv[argpos];
   1987 			mput(uargc, uargv);
   1988 			if ((code / 100) != COMPLETE) {
   1989 				rval = argpos + 1;
   1990 				break;
   1991 			}
   1992 		}
   1993 	}
   1994 
   1995  cleanup_auto_put:
   1996 	free(path);
   1997 	FREEPTR(uargv[2]);
   1998 	return (rval);
   1999 }
   2000