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