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