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