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