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