Home | History | Annotate | Line # | Download | only in ftp
fetch.c revision 1.112
      1 /*	$NetBSD: fetch.c,v 1.112 2000/05/25 15:35:51 itojun 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.112 2000/05/25 15:35:51 itojun 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 				if (isipv6addr(host) &&
    641 				    strchr(host, '%') != NULL) {
    642 					warnx(
    643 "Scoped address notation `%s' disallowed via web proxy",
    644 					    host);
    645 					FREEPTR(phost);
    646 					FREEPTR(pport);
    647 					FREEPTR(ppath);
    648 					goto cleanup_fetch_url;
    649 				}
    650 
    651 				FREEPTR(host);
    652 				host = phost;
    653 				FREEPTR(port);
    654 				port = pport;
    655 				FREEPTR(path);
    656 				path = xstrdup(url);
    657 				FREEPTR(ppath);
    658 			}
    659 		} /* ! EMPTYSTRING(proxyenv) */
    660 
    661 #if !defined(NI_NUMERICHOST) || !defined(INET6)
    662 		memset(&sin, 0, sizeof(sin));
    663 		sin.sin_family = AF_INET;
    664 
    665 		if (isdigit((unsigned char)host[0])) {
    666 			if (inet_aton(host, &sin.sin_addr) == 0) {
    667 				warnx("Invalid IP address `%s'", host);
    668 				goto cleanup_fetch_url;
    669 			}
    670 		} else {
    671 			hp = gethostbyname(host);
    672 			if (hp == NULL) {
    673 				warnx("%s: %s", host, hstrerror(h_errno));
    674 				goto cleanup_fetch_url;
    675 			}
    676 			if (hp->h_addrtype != AF_INET) {
    677 				warnx("`%s': not an Internet address?", host);
    678 				goto cleanup_fetch_url;
    679 			}
    680 			if (hp->h_length > sizeof(sin.sin_addr))
    681 				hp->h_length = sizeof(sin.sin_addr);
    682 			memcpy(&sin.sin_addr, hp->h_addr, hp->h_length);
    683 		}
    684 		sin.sin_port = htons(portnum);
    685 
    686 		s = socket(AF_INET, SOCK_STREAM, 0);
    687 		if (s == -1) {
    688 			warn("Can't create socket");
    689 			goto cleanup_fetch_url;
    690 		}
    691 
    692 		while (xconnect(s, (struct sockaddr *)&sin,
    693 		    sizeof(sin)) == -1) {
    694 			if (errno == EINTR)
    695 				continue;
    696 			if (hp && hp->h_addr_list[1]) {
    697 				int oerrno = errno;
    698 				char *ia;
    699 
    700 				ia = inet_ntoa(sin.sin_addr);
    701 				errno = oerrno;
    702 				warn("Connect to address `%s'", ia);
    703 				hp->h_addr_list++;
    704 				memcpy(&sin.sin_addr, hp->h_addr_list[0],
    705 				    (size_t)hp->h_length);
    706 				if (verbose)
    707 					fprintf(ttyout, "Trying %s...\n",
    708 					    inet_ntoa(sin.sin_addr));
    709 				(void)close(s);
    710 				s = socket(AF_INET, SOCK_STREAM, 0);
    711 				if (s < 0) {
    712 					warn("Can't create socket");
    713 					goto cleanup_fetch_url;
    714 				}
    715 				continue;
    716 			}
    717 			warn("Can't connect to `%s'", host);
    718 			goto cleanup_fetch_url;
    719 		}
    720 #else
    721 		memset(&hints, 0, sizeof(hints));
    722 		hints.ai_flags = 0;
    723 		hints.ai_family = AF_UNSPEC;
    724 		hints.ai_socktype = SOCK_STREAM;
    725 		hints.ai_protocol = 0;
    726 		error = getaddrinfo(host, port, &hints, &res0);
    727 		if (error) {
    728 			warnx(gai_strerror(error));
    729 			goto cleanup_fetch_url;
    730 		}
    731 		if (res0->ai_canonname)
    732 			host = res0->ai_canonname;
    733 
    734 		s = -1;
    735 		for (res = res0; res; res = res->ai_next) {
    736 			if (getnameinfo(res->ai_addr, res->ai_addrlen,
    737 					hbuf, sizeof(hbuf), NULL, 0,
    738 					NI_NUMERICHOST) != 0)
    739 				strncpy(hbuf, "invalid", sizeof(hbuf));
    740 
    741 			if (verbose && res != res0)
    742 				fprintf(ttyout, "Trying %s...\n", hbuf);
    743 
    744 			s = socket(res->ai_family, res->ai_socktype,
    745 				res->ai_protocol);
    746 			if (s < 0) {
    747 				warn("Can't create socket");
    748 				continue;
    749 			}
    750 
    751 			if (xconnect(s, res->ai_addr, res->ai_addrlen) < 0) {
    752 				warn("Connect to address `%s'", hbuf);
    753 				close(s);
    754 				s = -1;
    755 				continue;
    756 			}
    757 
    758 			/* success */
    759 			break;
    760 		}
    761 		freeaddrinfo(res0);
    762 
    763 		if (s < 0) {
    764 			warn("Can't connect to %s", host);
    765 			goto cleanup_fetch_url;
    766 		}
    767 #endif
    768 
    769 		fin = fdopen(s, "r+");
    770 		/*
    771 		 * Construct and send the request.
    772 		 */
    773 		if (verbose)
    774 			fprintf(ttyout, "Requesting %s\n", url);
    775 		leading = "  (";
    776 		hasleading = 0;
    777 		if (isproxy) {
    778 			if (verbose) {
    779 				fprintf(ttyout, "%svia %s:%s", leading,
    780 				    host, port);
    781 				leading = ", ";
    782 				hasleading++;
    783 			}
    784 			fprintf(fin, "GET %s HTTP/1.0\r\n", path);
    785 			if (flushcache)
    786 				fprintf(fin, "Pragma: no-cache\r\n");
    787 		} else {
    788 			fprintf(fin, "GET %s HTTP/1.1\r\n", path);
    789 			if (strchr(host, ':')) {
    790 				char *h, *p;
    791 
    792 				/*
    793 				 * strip off IPv6 scope identifier, since it is
    794 				 * local to the node
    795 				 */
    796 				h = xstrdup(host);
    797 				if (isipv6addr(h) &&
    798 				    (p = strchr(h, '%')) != NULL) {
    799 					*p = '\0';
    800 				}
    801 				fprintf(fin, "Host: [%s]:%d\r\n", h, portnum);
    802 				free(h);
    803 			} else
    804 				fprintf(fin, "Host: %s:%d\r\n", host, portnum);
    805 			fprintf(fin, "Accept: */*\r\n");
    806 			fprintf(fin, "Connection: close\r\n");
    807 			if (restart_point) {
    808 				fputs(leading, ttyout);
    809 #ifndef NO_QUAD
    810 				fprintf(fin, "Range: bytes=%lld-\r\n",
    811 				    (long long)restart_point);
    812 				fprintf(ttyout, "restarting at %lld",
    813 				    (long long)restart_point);
    814 #else
    815 				fprintf(fin, "Range: bytes=%ld-\r\n",
    816 				    (long)restart_point);
    817 				fprintf(ttyout, "restarting at %ld",
    818 				    (long)restart_point);
    819 #endif
    820 				leading = ", ";
    821 				hasleading++;
    822 			}
    823 			if (flushcache)
    824 				fprintf(fin, "Cache-Control: no-cache\r\n");
    825 		}
    826 		fprintf(fin, "User-Agent: %s/%s\r\n", FTP_PRODUCT, FTP_VERSION);
    827 		if (wwwauth) {
    828 			if (verbose) {
    829 				fprintf(ttyout, "%swith authorization",
    830 				    leading);
    831 				leading = ", ";
    832 				hasleading++;
    833 			}
    834 			fprintf(fin, "Authorization: %s\r\n", wwwauth);
    835 		}
    836 		if (proxyauth) {
    837 			if (verbose) {
    838 				fprintf(ttyout,
    839 				    "%swith proxy authorization", leading);
    840 				leading = ", ";
    841 				hasleading++;
    842 			}
    843 			fprintf(fin, "Proxy-Authorization: %s\r\n", proxyauth);
    844 		}
    845 		if (verbose && hasleading)
    846 			fputs(")\n", ttyout);
    847 		fprintf(fin, "\r\n");
    848 		if (fflush(fin) == EOF) {
    849 			warn("Writing HTTP request");
    850 			goto cleanup_fetch_url;
    851 		}
    852 
    853 				/* Read the response */
    854 		if ((buf = fparseln(fin, &len, NULL, "\0\0\0", 0)) == NULL) {
    855 			warn("Receiving HTTP reply");
    856 			goto cleanup_fetch_url;
    857 		}
    858 		while (len > 0 && (buf[len-1] == '\r' || buf[len-1] == '\n'))
    859 			buf[--len] = '\0';
    860 		if (debug)
    861 			fprintf(ttyout, "received `%s'\n", buf);
    862 
    863 				/* Determine HTTP response code */
    864 		cp = strchr(buf, ' ');
    865 		if (cp == NULL)
    866 			goto improper;
    867 		else
    868 			cp++;
    869 		hcode = strtol(cp, &ep, 10);
    870 		if (*ep != '\0' && !isspace((unsigned char)*ep))
    871 			goto improper;
    872 		message = xstrdup(cp);
    873 
    874 				/* Read the rest of the header. */
    875 		FREEPTR(buf);
    876 		while (1) {
    877 			if ((buf = fparseln(fin, &len, NULL, "\0\0\0", 0))
    878 			    == NULL) {
    879 				warn("Receiving HTTP reply");
    880 				goto cleanup_fetch_url;
    881 			}
    882 			while (len > 0 &&
    883 			    (buf[len-1] == '\r' || buf[len-1] == '\n'))
    884 				buf[--len] = '\0';
    885 			if (len == 0)
    886 				break;
    887 			if (debug)
    888 				fprintf(ttyout, "received `%s'\n", buf);
    889 
    890 				/* Look for some headers */
    891 			cp = buf;
    892 
    893 #define	CONTENTLEN "Content-Length: "
    894 			if (strncasecmp(cp, CONTENTLEN,
    895 					sizeof(CONTENTLEN) - 1) == 0) {
    896 				cp += sizeof(CONTENTLEN) - 1;
    897 #ifndef NO_QUAD
    898 				filesize = strtoll(cp, &ep, 10);
    899 #else
    900 				filesize = strtol(cp, &ep, 10);
    901 #endif
    902 				if (filesize < 0 || *ep != '\0')
    903 					goto improper;
    904 				if (debug)
    905 #ifndef NO_QUAD
    906 					fprintf(ttyout, "parsed len as: %lld\n",
    907 					    (long long)filesize);
    908 #else
    909 					fprintf(ttyout, "parsed len as: %ld\n",
    910 					    (long)filesize);
    911 #endif
    912 
    913 #define CONTENTRANGE "Content-Range: bytes "
    914 			} else if (strncasecmp(cp, CONTENTRANGE,
    915 					sizeof(CONTENTRANGE) - 1) == 0) {
    916 				cp += sizeof(CONTENTRANGE) - 1;
    917 #ifndef NO_QUAD
    918 				rangestart = strtoll(cp, &ep, 10);
    919 #else
    920 				rangestart = strtol(cp, &ep, 10);
    921 #endif
    922 				if (rangestart < 0 || *ep != '-')
    923 					goto improper;
    924 				cp = ep + 1;
    925 
    926 #ifndef NO_QUAD
    927 				rangeend = strtoll(cp, &ep, 10);
    928 #else
    929 				rangeend = strtol(cp, &ep, 10);
    930 #endif
    931 				if (rangeend < 0 || *ep != '/' ||
    932 				    rangeend < rangestart)
    933 					goto improper;
    934 				cp = ep + 1;
    935 
    936 #ifndef NO_QUAD
    937 				entitylen = strtoll(cp, &ep, 10);
    938 #else
    939 				entitylen = strtol(cp, &ep, 10);
    940 #endif
    941 				if (entitylen < 0 || *ep != '\0')
    942 					goto improper;
    943 
    944 				if (debug)
    945 #ifndef NO_QUAD
    946 					fprintf(ttyout,
    947 					    "parsed range as: %lld-%lld/%lld\n",
    948 					    (long long)rangestart,
    949 					    (long long)rangeend,
    950 					    (long long)entitylen);
    951 #else
    952 					fprintf(ttyout,
    953 					    "parsed range as: %ld-%ld/%ld\n",
    954 					    (long)rangestart,
    955 					    (long)rangeend,
    956 					    (long)entitylen);
    957 #endif
    958 				if (! restart_point) {
    959 					warnx(
    960 				    "Received unexpected Content-Range header");
    961 					goto cleanup_fetch_url;
    962 				}
    963 
    964 #define	LASTMOD "Last-Modified: "
    965 			} else if (strncasecmp(cp, LASTMOD,
    966 						sizeof(LASTMOD) - 1) == 0) {
    967 				struct tm parsed;
    968 				char *t;
    969 
    970 				cp += sizeof(LASTMOD) - 1;
    971 							/* RFC 1123 */
    972 				if ((t = strptime(cp,
    973 						"%a, %d %b %Y %H:%M:%S GMT",
    974 						&parsed))
    975 							/* RFC 850 */
    976 				    || (t = strptime(cp,
    977 						"%a, %d-%b-%y %H:%M:%S GMT",
    978 						&parsed))
    979 							/* asctime */
    980 				    || (t = strptime(cp,
    981 						"%a, %b %d %H:%M:%S %Y",
    982 						&parsed))) {
    983 					parsed.tm_isdst = -1;
    984 					if (*t == '\0')
    985 						mtime = timegm(&parsed);
    986 					if (debug && mtime != -1) {
    987 						fprintf(ttyout,
    988 						    "parsed date as: %s",
    989 						    ctime(&mtime));
    990 					}
    991 				}
    992 
    993 #define	LOCATION "Location: "
    994 			} else if (strncasecmp(cp, LOCATION,
    995 						sizeof(LOCATION) - 1) == 0) {
    996 				cp += sizeof(LOCATION) - 1;
    997 				location = xstrdup(cp);
    998 				if (debug)
    999 					fprintf(ttyout,
   1000 					    "parsed location as: %s\n", cp);
   1001 
   1002 #define	TRANSENC "Transfer-Encoding: "
   1003 			} else if (strncasecmp(cp, TRANSENC,
   1004 						sizeof(TRANSENC) - 1) == 0) {
   1005 				cp += sizeof(TRANSENC) - 1;
   1006 				if (strcasecmp(cp, "binary") == 0) {
   1007 					warnx(
   1008 			"Bogus transfer encoding - `%s' (fetching anyway)",
   1009 					    cp);
   1010 					continue;
   1011 				}
   1012 				if (strcasecmp(cp, "chunked") != 0) {
   1013 					warnx(
   1014 				    "Unsupported transfer encoding - `%s'",
   1015 					    cp);
   1016 					goto cleanup_fetch_url;
   1017 				}
   1018 				ischunked++;
   1019 				if (debug)
   1020 					fprintf(ttyout,
   1021 					    "using chunked encoding\n");
   1022 
   1023 #define	PROXYAUTH "Proxy-Authenticate: "
   1024 			} else if (strncasecmp(cp, PROXYAUTH,
   1025 						sizeof(PROXYAUTH) - 1) == 0) {
   1026 				cp += sizeof(PROXYAUTH) - 1;
   1027 				FREEPTR(auth);
   1028 				auth = xstrdup(cp);
   1029 				if (debug)
   1030 					fprintf(ttyout,
   1031 					    "parsed proxy-auth as: %s\n", cp);
   1032 
   1033 #define	WWWAUTH	"WWW-Authenticate: "
   1034 			} else if (strncasecmp(cp, WWWAUTH,
   1035 			    sizeof(WWWAUTH) - 1) == 0) {
   1036 				cp += sizeof(WWWAUTH) - 1;
   1037 				FREEPTR(auth);
   1038 				auth = xstrdup(cp);
   1039 				if (debug)
   1040 					fprintf(ttyout,
   1041 					    "parsed www-auth as: %s\n", cp);
   1042 
   1043 			}
   1044 
   1045 		}
   1046 				/* finished parsing header */
   1047 		FREEPTR(buf);
   1048 
   1049 		switch (hcode) {
   1050 		case 200:
   1051 			break;
   1052 		case 206:
   1053 			if (! restart_point) {
   1054 				warnx("Not expecting partial content header");
   1055 				goto cleanup_fetch_url;
   1056 			}
   1057 			break;
   1058 		case 300:
   1059 		case 301:
   1060 		case 302:
   1061 		case 303:
   1062 		case 305:
   1063 			if (EMPTYSTRING(location)) {
   1064 				warnx(
   1065 				"No redirection Location provided by server");
   1066 				goto cleanup_fetch_url;
   1067 			}
   1068 			if (redirect_loop++ > 5) {
   1069 				warnx("Too many redirections requested");
   1070 				goto cleanup_fetch_url;
   1071 			}
   1072 			if (hcode == 305) {
   1073 				if (verbose)
   1074 					fprintf(ttyout, "Redirected via %s\n",
   1075 					    location);
   1076 				rval = fetch_url(url, location,
   1077 				    proxyauth, wwwauth);
   1078 			} else {
   1079 				if (verbose)
   1080 					fprintf(ttyout, "Redirected to %s\n",
   1081 					    location);
   1082 				rval = go_fetch(location);
   1083 			}
   1084 			goto cleanup_fetch_url;
   1085 		case 401:
   1086 		case 407:
   1087 		    {
   1088 			char **authp;
   1089 			char *auser, *apass;
   1090 
   1091 			fprintf(ttyout, "%s\n", message);
   1092 			if (EMPTYSTRING(auth)) {
   1093 				warnx(
   1094 			    "No authentication challenge provided by server");
   1095 				goto cleanup_fetch_url;
   1096 			}
   1097 			if (hcode == 401) {
   1098 				authp = &wwwauth;
   1099 				auser = user;
   1100 				apass = pass;
   1101 			} else {
   1102 				authp = &proxyauth;
   1103 				auser = puser;
   1104 				apass = ppass;
   1105 			}
   1106 			if (*authp != NULL) {
   1107 				char reply[10];
   1108 
   1109 				fprintf(ttyout,
   1110 				    "Authorization failed. Retry (y/n)? ");
   1111 				if (fgets(reply, sizeof(reply), stdin)
   1112 				    == NULL) {
   1113 					clearerr(stdin);
   1114 					goto cleanup_fetch_url;
   1115 				} else {
   1116 					if (tolower(reply[0]) != 'y')
   1117 						goto cleanup_fetch_url;
   1118 				}
   1119 				auser = NULL;
   1120 				apass = NULL;
   1121 			}
   1122 			if (auth_url(auth, authp, auser, apass) == 0) {
   1123 				rval = fetch_url(url, proxyenv,
   1124 				    proxyauth, wwwauth);
   1125 				memset(*authp, 0, strlen(*authp));
   1126 				FREEPTR(*authp);
   1127 			}
   1128 			goto cleanup_fetch_url;
   1129 		    }
   1130 		default:
   1131 			if (message)
   1132 				warnx("Error retrieving file - `%s'", message);
   1133 			else
   1134 				warnx("Unknown error retrieving file");
   1135 			goto cleanup_fetch_url;
   1136 		}
   1137 	}		/* end of ftp:// or http:// specific setup */
   1138 
   1139 			/* Open the output file. */
   1140 	if (strcmp(savefile, "-") == 0) {
   1141 		fout = stdout;
   1142 	} else if (*savefile == '|') {
   1143 		oldintp = xsignal(SIGPIPE, SIG_IGN);
   1144 		fout = popen(savefile + 1, "w");
   1145 		if (fout == NULL) {
   1146 			warn("Can't run `%s'", savefile + 1);
   1147 			goto cleanup_fetch_url;
   1148 		}
   1149 		closefunc = pclose;
   1150 	} else {
   1151 		if (restart_point){
   1152 			if (entitylen != -1)
   1153 				filesize = entitylen;
   1154 			if (rangestart != -1 && rangestart != restart_point) {
   1155 				warnx(
   1156 				    "Size of `%s' differs from save file `%s'",
   1157 				    url, savefile);
   1158 				goto cleanup_fetch_url;
   1159 			}
   1160 			fout = fopen(savefile, "a");
   1161 		} else
   1162 			fout = fopen(savefile, "w");
   1163 		if (fout == NULL) {
   1164 			warn("Can't open `%s'", savefile);
   1165 			goto cleanup_fetch_url;
   1166 		}
   1167 		closefunc = fclose;
   1168 	}
   1169 
   1170 			/* Trap signals */
   1171 	if (sigsetjmp(httpabort, 1))
   1172 		goto cleanup_fetch_url;
   1173 	(void)xsignal(SIGQUIT, psummary);
   1174 	oldintr = xsignal(SIGINT, aborthttp);
   1175 
   1176 	if (rcvbuf_size > bufsize) {
   1177 		if (xferbuf)
   1178 			(void)free(xferbuf);
   1179 		bufsize = rcvbuf_size;
   1180 		xferbuf = xmalloc(bufsize);
   1181 	}
   1182 
   1183 	bytes = 0;
   1184 	hashbytes = mark;
   1185 	progressmeter(-1);
   1186 
   1187 			/* Finally, suck down the file. */
   1188 	do {
   1189 		long chunksize;
   1190 
   1191 		chunksize = 0;
   1192 					/* read chunksize */
   1193 		if (ischunked) {
   1194 			if (fgets(xferbuf, bufsize, fin) == NULL) {
   1195 				warnx("Unexpected EOF reading chunksize");
   1196 				goto cleanup_fetch_url;
   1197 			}
   1198 			chunksize = strtol(xferbuf, &ep, 16);
   1199 
   1200 				/*
   1201 				 * XXX:	Work around bug in Apache 1.3.9, which
   1202 				 *	incorrectly puts a trailing space after
   1203 				 *	the chunksize.
   1204 				 */
   1205 			if (*ep == ' ')
   1206 				ep++;
   1207 
   1208 			if (strcmp(ep, "\r\n") != 0) {
   1209 				warnx("Unexpected data following chunksize");
   1210 				goto cleanup_fetch_url;
   1211 			}
   1212 			if (debug)
   1213 				fprintf(ttyout,
   1214 #ifndef NO_QUAD
   1215 				    "got chunksize of %lld\n",
   1216 				    (long long)chunksize);
   1217 #else
   1218 				    "got chunksize of %ld\n",
   1219 				    (long)chunksize);
   1220 #endif
   1221 			if (chunksize == 0)
   1222 				break;
   1223 		}
   1224 					/* transfer file or chunk */
   1225 		while (1) {
   1226 			struct timeval then, now, td;
   1227 			off_t bufrem;
   1228 
   1229 			if (rate_get)
   1230 				(void)gettimeofday(&then, NULL);
   1231 			bufrem = rate_get ? rate_get : bufsize;
   1232 			if (ischunked)
   1233 				bufrem = MIN(chunksize, bufrem);
   1234 			while (bufrem > 0) {
   1235 				len = fread(xferbuf, sizeof(char),
   1236 				    MIN(bufsize, bufrem), fin);
   1237 				if (len <= 0)
   1238 					goto chunkdone;
   1239 				bytes += len;
   1240 				bufrem -= len;
   1241 				if (fwrite(xferbuf, sizeof(char), len, fout)
   1242 				    != len) {
   1243 					warn("Writing `%s'", savefile);
   1244 					goto cleanup_fetch_url;
   1245 				}
   1246 				if (hash && !progress) {
   1247 					while (bytes >= hashbytes) {
   1248 						(void)putc('#', ttyout);
   1249 						hashbytes += mark;
   1250 					}
   1251 					(void)fflush(ttyout);
   1252 				}
   1253 				if (ischunked) {
   1254 					chunksize -= len;
   1255 					if (chunksize <= 0)
   1256 						break;
   1257 				}
   1258 			}
   1259 			if (rate_get) {
   1260 				while (1) {
   1261 					(void)gettimeofday(&now, NULL);
   1262 					timersub(&now, &then, &td);
   1263 					if (td.tv_sec > 0)
   1264 						break;
   1265 					usleep(1000000 - td.tv_usec);
   1266 				}
   1267 			}
   1268 			if (ischunked && chunksize <= 0)
   1269 				break;
   1270 		}
   1271 					/* read CRLF after chunk*/
   1272  chunkdone:
   1273 		if (ischunked) {
   1274 			if (fgets(xferbuf, bufsize, fin) == NULL)
   1275 				break;
   1276 			if (strcmp(xferbuf, "\r\n") != 0) {
   1277 				warnx("Unexpected data following chunk");
   1278 				goto cleanup_fetch_url;
   1279 			}
   1280 		}
   1281 	} while (ischunked);
   1282 	if (hash && !progress && bytes > 0) {
   1283 		if (bytes < mark)
   1284 			(void)putc('#', ttyout);
   1285 		(void)putc('\n', ttyout);
   1286 	}
   1287 	if (ferror(fin)) {
   1288 		warn("Reading file");
   1289 		goto cleanup_fetch_url;
   1290 	}
   1291 	progressmeter(1);
   1292 	bytes = 0;
   1293 	(void)fflush(fout);
   1294 	if (closefunc == fclose && mtime != -1) {
   1295 		struct timeval tval[2];
   1296 
   1297 		(void)gettimeofday(&tval[0], NULL);
   1298 		tval[1].tv_sec = mtime;
   1299 		tval[1].tv_usec = 0;
   1300 		(*closefunc)(fout);
   1301 		fout = NULL;
   1302 
   1303 		if (utimes(savefile, tval) == -1) {
   1304 			fprintf(ttyout,
   1305 			    "Can't change modification time to %s",
   1306 			    asctime(localtime(&mtime)));
   1307 		}
   1308 	}
   1309 	if (bytes > 0)
   1310 		ptransfer(0);
   1311 
   1312 	rval = 0;
   1313 	goto cleanup_fetch_url;
   1314 
   1315 improper:
   1316 	warnx("Improper response from `%s'", host);
   1317 
   1318 cleanup_fetch_url:
   1319 	if (oldintr)
   1320 		(void)xsignal(SIGINT, oldintr);
   1321 	if (oldintp)
   1322 		(void)xsignal(SIGPIPE, oldintp);
   1323 	if (fin != NULL)
   1324 		fclose(fin);
   1325 	else if (s != -1)
   1326 		close(s);
   1327 	if (closefunc != NULL && fout != NULL)
   1328 		(*closefunc)(fout);
   1329 	FREEPTR(savefile);
   1330 	FREEPTR(user);
   1331 	FREEPTR(pass);
   1332 	FREEPTR(host);
   1333 	FREEPTR(port);
   1334 	FREEPTR(path);
   1335 	FREEPTR(decodedpath);
   1336 	FREEPTR(puser);
   1337 	FREEPTR(ppass);
   1338 	FREEPTR(buf);
   1339 	FREEPTR(auth);
   1340 	FREEPTR(location);
   1341 	FREEPTR(message);
   1342 	return (rval);
   1343 }
   1344 
   1345 /*
   1346  * Abort a HTTP retrieval
   1347  */
   1348 void
   1349 aborthttp(int notused)
   1350 {
   1351 	char msgbuf[100];
   1352 	int len;
   1353 
   1354 	alarmtimer(0);
   1355 	len = strlcpy(msgbuf, "\nHTTP fetch aborted.\n", sizeof(msgbuf));
   1356 	write(fileno(ttyout), msgbuf, len);
   1357 	siglongjmp(httpabort, 1);
   1358 }
   1359 
   1360 /*
   1361  * Retrieve ftp URL or classic ftp argument using FTP.
   1362  * Returns 1 on failure, 0 on completed xfer, -1 if ftp connection
   1363  * is still open (e.g, ftp xfer with trailing /)
   1364  */
   1365 static int
   1366 fetch_ftp(const char *url)
   1367 {
   1368 	char		*cp, *xargv[5], rempath[MAXPATHLEN];
   1369 	char		*host, *path, *dir, *file, *user, *pass;
   1370 	char		*port;
   1371 	int		 dirhasglob, filehasglob, oautologin, rval, type, xargc;
   1372 	in_port_t	 portnum;
   1373 	url_t		 urltype;
   1374 
   1375 	host = path = dir = file = user = pass = NULL;
   1376 	port = NULL;
   1377 	rval = 1;
   1378 	type = TYPE_I;
   1379 
   1380 	if (strncasecmp(url, FTP_URL, sizeof(FTP_URL) - 1) == 0) {
   1381 		if ((parse_url(url, "URL", &urltype, &user, &pass,
   1382 		    &host, &port, &portnum, &path) == -1) ||
   1383 		    (user != NULL && *user == '\0') ||
   1384 		    (pass != NULL && *pass == '\0') ||
   1385 		    EMPTYSTRING(host)) {
   1386 			warnx("Invalid URL `%s'", url);
   1387 			goto cleanup_fetch_ftp;
   1388 		}
   1389 		url_decode(user);
   1390 		url_decode(pass);
   1391 		/*
   1392 		 * Note: Don't url_decode(path) here.  We need to keep the
   1393 		 * distinction between "/" and "%2F" until later.
   1394 		 */
   1395 
   1396 					/* check for trailing ';type=[aid]' */
   1397 		if (! EMPTYSTRING(path) && (cp = strrchr(path, ';')) != NULL) {
   1398 			if (strcasecmp(cp, ";type=a") == 0)
   1399 				type = TYPE_A;
   1400 			else if (strcasecmp(cp, ";type=i") == 0)
   1401 				type = TYPE_I;
   1402 			else if (strcasecmp(cp, ";type=d") == 0) {
   1403 				warnx(
   1404 			    "Directory listing via a URL is not supported");
   1405 				goto cleanup_fetch_ftp;
   1406 			} else {
   1407 				warnx("Invalid suffix `%s' in URL `%s'", cp,
   1408 				    url);
   1409 				goto cleanup_fetch_ftp;
   1410 			}
   1411 			*cp = 0;
   1412 		}
   1413 	} else {			/* classic style `[user@]host:[file]' */
   1414 		urltype = CLASSIC_URL_T;
   1415 		host = xstrdup(url);
   1416 		cp = strchr(host, '@');
   1417 		if (cp != NULL) {
   1418 			*cp = '\0';
   1419 			user = host;
   1420 			anonftp = 0;	/* disable anonftp */
   1421 			host = xstrdup(cp + 1);
   1422 		}
   1423 		cp = strchr(host, ':');
   1424 		if (cp != NULL) {
   1425 			*cp = '\0';
   1426 			path = xstrdup(cp + 1);
   1427 		}
   1428 	}
   1429 	if (EMPTYSTRING(host))
   1430 		goto cleanup_fetch_ftp;
   1431 
   1432 			/* Extract the file and (if present) directory name. */
   1433 	dir = path;
   1434 	if (! EMPTYSTRING(dir)) {
   1435 		/*
   1436 		 * If we are dealing with classic `[user@]host:[path]' syntax,
   1437 		 * then a path of the form `/file' (resulting from input of the
   1438 		 * form `host:/file') means that we should do "CWD /" before
   1439 		 * retrieving the file.  So we set dir="/" and file="file".
   1440 		 *
   1441 		 * But if we are dealing with URLs like `ftp://host/path' then
   1442 		 * a path of the form `/file' (resulting from a URL of the form
   1443 		 * `ftp://host//file') means that we should do `CWD ' (with an
   1444 		 * empty argument) before retrieving the file.  So we set
   1445 		 * dir="" and file="file".
   1446 		 *
   1447 		 * If the path does not contain / at all, we set dir=NULL.
   1448 		 * (We get a path without any slashes if we are dealing with
   1449 		 * classic `[user@]host:[file]' or URL `ftp://host/file'.)
   1450 		 *
   1451 		 * In all other cases, we set dir to a string that does not
   1452 		 * include the final '/' that separates the dir part from the
   1453 		 * file part of the path.  (This will be the empty string if
   1454 		 * and only if we are dealing with a path of the form `/file'
   1455 		 * resulting from an URL of the form `ftp://host//file'.)
   1456 		 */
   1457 		cp = strrchr(dir, '/');
   1458 		if (cp == dir && urltype == CLASSIC_URL_T) {
   1459 			file = cp + 1;
   1460 			dir = "/";
   1461 		} else if (cp != NULL) {
   1462 			*cp++ = '\0';
   1463 			file = cp;
   1464 		} else {
   1465 			file = dir;
   1466 			dir = NULL;
   1467 		}
   1468 	} else
   1469 		dir = NULL;
   1470 	if (urltype == FTP_URL_T && file != NULL) {
   1471 		url_decode(file);
   1472 		/* but still don't url_decode(dir) */
   1473 	}
   1474 	if (debug)
   1475 		fprintf(ttyout,
   1476 		    "fetch_ftp: user `%s' pass `%s' host %s port %s "
   1477 		    "path `%s' dir `%s' file `%s'\n",
   1478 		    user ? user : "<null>", pass ? pass : "<null>",
   1479 		    host ? host : "<null>", port ? port : "<null>",
   1480 		    path ? path : "<null>",
   1481 		    dir ? dir : "<null>", file ? file : "<null>");
   1482 
   1483 	dirhasglob = filehasglob = 0;
   1484 	if (doglob && urltype == CLASSIC_URL_T) {
   1485 		if (! EMPTYSTRING(dir) && strpbrk(dir, "*?[]{}") != NULL)
   1486 			dirhasglob = 1;
   1487 		if (! EMPTYSTRING(file) && strpbrk(file, "*?[]{}") != NULL)
   1488 			filehasglob = 1;
   1489 	}
   1490 
   1491 			/* Set up the connection */
   1492 	if (connected)
   1493 		disconnect(0, NULL);
   1494 	xargv[0] = __progname;
   1495 	xargv[1] = host;
   1496 	xargv[2] = NULL;
   1497 	xargc = 2;
   1498 	if (port) {
   1499 		xargv[2] = port;
   1500 		xargv[3] = NULL;
   1501 		xargc = 3;
   1502 	}
   1503 	oautologin = autologin;
   1504 	if (user != NULL)
   1505 		autologin = 0;
   1506 	setpeer(xargc, xargv);
   1507 	autologin = oautologin;
   1508 	if ((connected == 0) || ((connected == 1)
   1509 	    && !ftp_login(host, user, pass))) {
   1510 		warnx("Can't connect or login to host `%s'", host);
   1511 		goto cleanup_fetch_ftp;
   1512 	}
   1513 
   1514 	switch (type) {
   1515 	case TYPE_A:
   1516 		setascii(0, NULL);
   1517 		break;
   1518 	case TYPE_I:
   1519 		setbinary(0, NULL);
   1520 		break;
   1521 	default:
   1522 		errx(1, "fetch_ftp: unknown transfer type %d", type);
   1523 	}
   1524 
   1525 		/*
   1526 		 * Change directories, if necessary.
   1527 		 *
   1528 		 * Note: don't use EMPTYSTRING(dir) below, because
   1529 		 * dir=="" means something different from dir==NULL.
   1530 		 */
   1531 	if (dir != NULL && !dirhasglob) {
   1532 		char *nextpart;
   1533 
   1534 		/*
   1535 		 * If we are dealing with a classic `[user@]host:[path]'
   1536 		 * (urltype is CLASSIC_URL_T) then we have a raw directory
   1537 		 * name (not encoded in any way) and we can change
   1538 		 * directories in one step.
   1539 		 *
   1540 		 * If we are dealing with an `ftp://host/path' URL
   1541 		 * (urltype is FTP_URL_T), then RFC 1738 says we need to
   1542 		 * send a separate CWD command for each unescaped "/"
   1543 		 * in the path, and we have to interpret %hex escaping
   1544 		 * *after* we find the slashes.  It's possible to get
   1545 		 * empty components here, (from multiple adjacent
   1546 		 * slashes in the path) and RFC 1738 says that we should
   1547 		 * still do `CWD ' (with a null argument) in such cases.
   1548 		 *
   1549 		 * Many ftp servers don't support `CWD ', so if there's an
   1550 		 * error performing that command, bail out with a descriptive
   1551 		 * message.
   1552 		 *
   1553 		 * Examples:
   1554 		 *
   1555 		 * host:			dir="", urltype=CLASSIC_URL_T
   1556 		 *		logged in (to default directory)
   1557 		 * host:file			dir=NULL, urltype=CLASSIC_URL_T
   1558 		 *		"RETR file"
   1559 		 * host:dir/			dir="dir", urltype=CLASSIC_URL_T
   1560 		 *		"CWD dir", logged in
   1561 		 * ftp://host/			dir="", urltype=FTP_URL_T
   1562 		 *		logged in (to default directory)
   1563 		 * ftp://host/dir/		dir="dir", urltype=FTP_URL_T
   1564 		 *		"CWD dir", logged in
   1565 		 * ftp://host/file		dir=NULL, urltype=FTP_URL_T
   1566 		 *		"RETR file"
   1567 		 * ftp://host//file		dir="", urltype=FTP_URL_T
   1568 		 *		"CWD ", "RETR file"
   1569 		 * host:/file			dir="/", urltype=CLASSIC_URL_T
   1570 		 *		"CWD /", "RETR file"
   1571 		 * ftp://host///file		dir="/", urltype=FTP_URL_T
   1572 		 *		"CWD ", "CWD ", "RETR file"
   1573 		 * ftp://host/%2F/file		dir="%2F", urltype=FTP_URL_T
   1574 		 *		"CWD /", "RETR file"
   1575 		 * ftp://host/foo/file		dir="foo", urltype=FTP_URL_T
   1576 		 *		"CWD foo", "RETR file"
   1577 		 * ftp://host/foo/bar/file	dir="foo/bar"
   1578 		 *		"CWD foo", "CWD bar", "RETR file"
   1579 		 * ftp://host//foo/bar/file	dir="/foo/bar"
   1580 		 *		"CWD ", "CWD foo", "CWD bar", "RETR file"
   1581 		 * ftp://host/foo//bar/file	dir="foo//bar"
   1582 		 *		"CWD foo", "CWD ", "CWD bar", "RETR file"
   1583 		 * ftp://host/%2F/foo/bar/file	dir="%2F/foo/bar"
   1584 		 *		"CWD /", "CWD foo", "CWD bar", "RETR file"
   1585 		 * ftp://host/%2Ffoo/bar/file	dir="%2Ffoo/bar"
   1586 		 *		"CWD /foo", "CWD bar", "RETR file"
   1587 		 * ftp://host/%2Ffoo%2Fbar/file	dir="%2Ffoo%2Fbar"
   1588 		 *		"CWD /foo/bar", "RETR file"
   1589 		 * ftp://host/%2Ffoo%2Fbar%2Ffile	dir=NULL
   1590 		 *		"RETR /foo/bar/file"
   1591 		 *
   1592 		 * Note that we don't need `dir' after this point.
   1593 		 */
   1594 		do {
   1595 			if (urltype == FTP_URL_T) {
   1596 				nextpart = strchr(dir, '/');
   1597 				if (nextpart) {
   1598 					*nextpart = '\0';
   1599 					nextpart++;
   1600 				}
   1601 				url_decode(dir);
   1602 			} else
   1603 				nextpart = NULL;
   1604 			if (debug)
   1605 				fprintf(ttyout, "dir `%s', nextpart `%s'\n",
   1606 				    dir ? dir : "<null>",
   1607 				    nextpart ? nextpart : "<null>");
   1608 			if (urltype == FTP_URL_T || *dir != '\0') {
   1609 				xargv[0] = "cd";
   1610 				xargv[1] = dir;
   1611 				xargv[2] = NULL;
   1612 				dirchange = 0;
   1613 				cd(2, xargv);
   1614 				if (! dirchange) {
   1615 					if (*dir == '\0' && code == 500)
   1616 						fprintf(stderr,
   1617 "\n"
   1618 "ftp: The `CWD ' command (without a directory), which is required by\n"
   1619 "     RFC 1738 to support the empty directory in the URL pathname (`//'),\n"
   1620 "     conflicts with the server's conformance to RFC 959.\n"
   1621 "     Try the same URL without the `//' in the URL pathname.\n"
   1622 "\n");
   1623 					goto cleanup_fetch_ftp;
   1624 				}
   1625 			}
   1626 			dir = nextpart;
   1627 		} while (dir != NULL);
   1628 	}
   1629 
   1630 	if (EMPTYSTRING(file)) {
   1631 		rval = -1;
   1632 		goto cleanup_fetch_ftp;
   1633 	}
   1634 
   1635 	if (dirhasglob) {
   1636 		(void)strlcpy(rempath, dir,	sizeof(rempath));
   1637 		(void)strlcat(rempath, "/",	sizeof(rempath));
   1638 		(void)strlcat(rempath, file,	sizeof(rempath));
   1639 		file = rempath;
   1640 	}
   1641 
   1642 			/* Fetch the file(s). */
   1643 	xargc = 2;
   1644 	xargv[0] = "get";
   1645 	xargv[1] = file;
   1646 	xargv[2] = NULL;
   1647 	if (dirhasglob || filehasglob) {
   1648 		int ointeractive;
   1649 
   1650 		ointeractive = interactive;
   1651 		interactive = 0;
   1652 		xargv[0] = "mget";
   1653 		mget(xargc, xargv);
   1654 		interactive = ointeractive;
   1655 	} else {
   1656 		if (outfile == NULL) {
   1657 			cp = strrchr(file, '/');	/* find savefile */
   1658 			if (cp != NULL)
   1659 				outfile = cp + 1;
   1660 			else
   1661 				outfile = file;
   1662 		}
   1663 		xargv[2] = (char *)outfile;
   1664 		xargv[3] = NULL;
   1665 		xargc++;
   1666 		if (restartautofetch)
   1667 			reget(xargc, xargv);
   1668 		else
   1669 			get(xargc, xargv);
   1670 	}
   1671 
   1672 	if ((code / 100) == COMPLETE)
   1673 		rval = 0;
   1674 
   1675 cleanup_fetch_ftp:
   1676 	FREEPTR(host);
   1677 	FREEPTR(path);
   1678 	FREEPTR(user);
   1679 	FREEPTR(pass);
   1680 	return (rval);
   1681 }
   1682 
   1683 /*
   1684  * Retrieve the given file to outfile.
   1685  * Supports arguments of the form:
   1686  *	"host:path", "ftp://host/path"	if $ftpproxy, call fetch_url() else
   1687  *					call fetch_ftp()
   1688  *	"http://host/path"		call fetch_url() to use HTTP
   1689  *	"file:///path"			call fetch_url() to copy
   1690  *	"about:..."			print a message
   1691  *
   1692  * Returns 1 on failure, 0 on completed xfer, -1 if ftp connection
   1693  * is still open (e.g, ftp xfer with trailing /)
   1694  */
   1695 static int
   1696 go_fetch(const char *url)
   1697 {
   1698 	char *proxy;
   1699 
   1700 	/*
   1701 	 * Check for about:*
   1702 	 */
   1703 	if (strncasecmp(url, ABOUT_URL, sizeof(ABOUT_URL) - 1) == 0) {
   1704 		url += sizeof(ABOUT_URL) -1;
   1705 		if (strcasecmp(url, "ftp") == 0) {
   1706 			fputs(
   1707 "This version of ftp has been enhanced by Luke Mewburn <lukem (at) netbsd.org>\n"
   1708 "for the NetBSD project.  Execute `man ftp' for more details.\n", ttyout);
   1709 		} else if (strcasecmp(url, "lukem") == 0) {
   1710 			fputs(
   1711 "Luke Mewburn is the author of most of the enhancements in this ftp client.\n"
   1712 "Please email feedback to <lukem (at) netbsd.org>.\n", ttyout);
   1713 		} else if (strcasecmp(url, "netbsd") == 0) {
   1714 			fputs(
   1715 "NetBSD is a freely available and redistributable UNIX-like operating system.\n"
   1716 "For more information, see http://www.netbsd.org/index.html\n", ttyout);
   1717 		} else if (strcasecmp(url, "version") == 0) {
   1718 			fprintf(ttyout, "Version: %s %s\n",
   1719 			    FTP_PRODUCT, FTP_VERSION);
   1720 		} else {
   1721 			fprintf(ttyout, "`%s' is an interesting topic.\n", url);
   1722 		}
   1723 		fputs("\n", ttyout);
   1724 		return (0);
   1725 	}
   1726 
   1727 	/*
   1728 	 * Check for file:// and http:// URLs.
   1729 	 */
   1730 	if (strncasecmp(url, HTTP_URL, sizeof(HTTP_URL) - 1) == 0 ||
   1731 	    strncasecmp(url, FILE_URL, sizeof(FILE_URL) - 1) == 0)
   1732 		return (fetch_url(url, NULL, NULL, NULL));
   1733 
   1734 	/*
   1735 	 * Try FTP URL-style and host:file arguments next.
   1736 	 * If ftpproxy is set with an FTP URL, use fetch_url()
   1737 	 * Othewise, use fetch_ftp().
   1738 	 */
   1739 	proxy = getoptionvalue("ftp_proxy");
   1740 	if (!EMPTYSTRING(proxy) &&
   1741 	    strncasecmp(url, FTP_URL, sizeof(FTP_URL) - 1) == 0)
   1742 		return (fetch_url(url, NULL, NULL, NULL));
   1743 
   1744 	return (fetch_ftp(url));
   1745 }
   1746 
   1747 /*
   1748  * Retrieve multiple files from the command line,
   1749  * calling go_fetch() for each file.
   1750  *
   1751  * If an ftp path has a trailing "/", the path will be cd-ed into and
   1752  * the connection remains open, and the function will return -1
   1753  * (to indicate the connection is alive).
   1754  * If an error occurs the return value will be the offset+1 in
   1755  * argv[] of the file that caused a problem (i.e, argv[x]
   1756  * returns x+1)
   1757  * Otherwise, 0 is returned if all files retrieved successfully.
   1758  */
   1759 int
   1760 auto_fetch(int argc, char *argv[])
   1761 {
   1762 	volatile int	argpos;
   1763 	int		rval;
   1764 
   1765 	argpos = 0;
   1766 
   1767 	if (sigsetjmp(toplevel, 1)) {
   1768 		if (connected)
   1769 			disconnect(0, NULL);
   1770 		return (argpos + 1);
   1771 	}
   1772 	(void)xsignal(SIGINT, intr);
   1773 	(void)xsignal(SIGPIPE, lostpeer);
   1774 
   1775 	/*
   1776 	 * Loop through as long as there's files to fetch.
   1777 	 */
   1778 	for (rval = 0; (rval == 0) && (argpos < argc); argpos++) {
   1779 		if (strchr(argv[argpos], ':') == NULL)
   1780 			break;
   1781 		redirect_loop = 0;
   1782 		if (!anonftp)
   1783 			anonftp = 2;	/* Handle "automatic" transfers. */
   1784 		rval = go_fetch(argv[argpos]);
   1785 		if (outfile != NULL && strcmp(outfile, "-") != 0
   1786 		    && outfile[0] != '|')
   1787 			outfile = NULL;
   1788 		if (rval > 0)
   1789 			rval = argpos + 1;
   1790 	}
   1791 
   1792 	if (connected && rval != -1)
   1793 		disconnect(0, NULL);
   1794 	return (rval);
   1795 }
   1796