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