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