xref: /netbsd-src/usr.bin/ftp/fetch.c (revision 73704c4ce4ee2a60eb617e693ce7e9f03902613e)
1 /*	$NetBSD: fetch.c,v 1.144 2003/07/31 05:23:59 lukem Exp $	*/
2 
3 /*-
4  * Copyright (c) 1997-2003 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.144 2003/07/31 05:23:59 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, (u_char *)*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, *useragent;
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("no file after directory (you must specify an "
513 		    "output file) `%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, NI_NUMERICHOST) != 0)
669 				strlcpy(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 		if ((useragent=getenv("FTPUSERAGENT")) != NULL) {
754 			fprintf(fin, "User-Agent: %s\r\n", useragent);
755 		} else {
756 			fprintf(fin, "User-Agent: %s/%s\r\n",
757 			    FTP_PRODUCT, FTP_VERSION);
758 		}
759 		if (wwwauth) {
760 			if (verbose) {
761 				fprintf(ttyout, "%swith authorization",
762 				    leading);
763 				leading = ", ";
764 				hasleading++;
765 			}
766 			fprintf(fin, "Authorization: %s\r\n", wwwauth);
767 		}
768 		if (proxyauth) {
769 			if (verbose) {
770 				fprintf(ttyout,
771 				    "%swith proxy authorization", leading);
772 				leading = ", ";
773 				hasleading++;
774 			}
775 			fprintf(fin, "Proxy-Authorization: %s\r\n", proxyauth);
776 		}
777 		if (verbose && hasleading)
778 			fputs(")\n", ttyout);
779 		fprintf(fin, "\r\n");
780 		if (fflush(fin) == EOF) {
781 			warn("Writing HTTP request");
782 			goto cleanup_fetch_url;
783 		}
784 
785 				/* Read the response */
786 		if ((buf = fparseln(fin, &len, NULL, "\0\0\0", 0)) == NULL) {
787 			warn("Receiving HTTP reply");
788 			goto cleanup_fetch_url;
789 		}
790 		while (len > 0 && (buf[len-1] == '\r' || buf[len-1] == '\n'))
791 			buf[--len] = '\0';
792 		if (debug)
793 			fprintf(ttyout, "received `%s'\n", buf);
794 
795 				/* Determine HTTP response code */
796 		cp = strchr(buf, ' ');
797 		if (cp == NULL)
798 			goto improper;
799 		else
800 			cp++;
801 		hcode = strtol(cp, &ep, 10);
802 		if (*ep != '\0' && !isspace((unsigned char)*ep))
803 			goto improper;
804 		message = xstrdup(cp);
805 
806 				/* Read the rest of the header. */
807 		FREEPTR(buf);
808 		while (1) {
809 			if ((buf = fparseln(fin, &len, NULL, "\0\0\0", 0))
810 			    == NULL) {
811 				warn("Receiving HTTP reply");
812 				goto cleanup_fetch_url;
813 			}
814 			while (len > 0 &&
815 			    (buf[len-1] == '\r' || buf[len-1] == '\n'))
816 				buf[--len] = '\0';
817 			if (len == 0)
818 				break;
819 			if (debug)
820 				fprintf(ttyout, "received `%s'\n", buf);
821 
822 				/* Look for some headers */
823 			cp = buf;
824 
825 #define	CONTENTLEN "Content-Length: "
826 			if (strncasecmp(cp, CONTENTLEN,
827 					sizeof(CONTENTLEN) - 1) == 0) {
828 				cp += sizeof(CONTENTLEN) - 1;
829 				filesize = STRTOLL(cp, &ep, 10);
830 				if (filesize < 0 || *ep != '\0')
831 					goto improper;
832 				if (debug)
833 					fprintf(ttyout,
834 					    "parsed len as: " LLF "\n",
835 					    (LLT)filesize);
836 
837 #define CONTENTRANGE "Content-Range: bytes "
838 			} else if (strncasecmp(cp, CONTENTRANGE,
839 					sizeof(CONTENTRANGE) - 1) == 0) {
840 				cp += sizeof(CONTENTRANGE) - 1;
841 				if (*cp == '*') {
842 					ep = cp + 1;
843 				}
844 				else {
845 					rangestart = STRTOLL(cp, &ep, 10);
846 					if (rangestart < 0 || *ep != '-')
847 						goto improper;
848 					cp = ep + 1;
849 					rangeend = STRTOLL(cp, &ep, 10);
850 					if (rangeend < 0 || rangeend < rangestart)
851 						goto improper;
852 				}
853 				if (*ep != '/')
854 					goto improper;
855 				cp = ep + 1;
856 				if (*cp == '*') {
857 					ep = cp + 1;
858 				}
859 				else {
860 					entitylen = STRTOLL(cp, &ep, 10);
861 					if (entitylen < 0)
862 						goto improper;
863 				}
864 				if (*ep != '\0')
865 					goto improper;
866 
867 				if (debug) {
868 					fprintf(ttyout, "parsed range as: ");
869 					if (rangestart == -1)
870 						fprintf(ttyout, "*");
871 					else
872 						fprintf(ttyout, LLF "-" LLF,
873 						    (LLT)rangestart,
874 						    (LLT)rangeend);
875 					fprintf(ttyout, "/" LLF "\n", (LLT)entitylen);
876 				}
877 				if (! restart_point) {
878 					warnx(
879 				    "Received unexpected Content-Range header");
880 					goto cleanup_fetch_url;
881 				}
882 
883 #define	LASTMOD "Last-Modified: "
884 			} else if (strncasecmp(cp, LASTMOD,
885 						sizeof(LASTMOD) - 1) == 0) {
886 				struct tm parsed;
887 				char *t;
888 
889 				cp += sizeof(LASTMOD) - 1;
890 							/* RFC 1123 */
891 				if ((t = strptime(cp,
892 						"%a, %d %b %Y %H:%M:%S GMT",
893 						&parsed))
894 							/* RFC 850 */
895 				    || (t = strptime(cp,
896 						"%a, %d-%b-%y %H:%M:%S GMT",
897 						&parsed))
898 							/* asctime */
899 				    || (t = strptime(cp,
900 						"%a, %b %d %H:%M:%S %Y",
901 						&parsed))) {
902 					parsed.tm_isdst = -1;
903 					if (*t == '\0')
904 						mtime = timegm(&parsed);
905 					if (debug && mtime != -1) {
906 						fprintf(ttyout,
907 						    "parsed date as: %s",
908 						    ctime(&mtime));
909 					}
910 				}
911 
912 #define	LOCATION "Location: "
913 			} else if (strncasecmp(cp, LOCATION,
914 						sizeof(LOCATION) - 1) == 0) {
915 				cp += sizeof(LOCATION) - 1;
916 				location = xstrdup(cp);
917 				if (debug)
918 					fprintf(ttyout,
919 					    "parsed location as: %s\n", cp);
920 
921 #define	TRANSENC "Transfer-Encoding: "
922 			} else if (strncasecmp(cp, TRANSENC,
923 						sizeof(TRANSENC) - 1) == 0) {
924 				cp += sizeof(TRANSENC) - 1;
925 				if (strcasecmp(cp, "binary") == 0) {
926 					warnx(
927 			"Bogus transfer encoding - `%s' (fetching anyway)",
928 					    cp);
929 					continue;
930 				}
931 				if (strcasecmp(cp, "chunked") != 0) {
932 					warnx(
933 				    "Unsupported transfer encoding - `%s'",
934 					    cp);
935 					goto cleanup_fetch_url;
936 				}
937 				ischunked++;
938 				if (debug)
939 					fprintf(ttyout,
940 					    "using chunked encoding\n");
941 
942 #define	PROXYAUTH "Proxy-Authenticate: "
943 			} else if (strncasecmp(cp, PROXYAUTH,
944 						sizeof(PROXYAUTH) - 1) == 0) {
945 				cp += sizeof(PROXYAUTH) - 1;
946 				FREEPTR(auth);
947 				auth = xstrdup(cp);
948 				if (debug)
949 					fprintf(ttyout,
950 					    "parsed proxy-auth as: %s\n", cp);
951 
952 #define	WWWAUTH	"WWW-Authenticate: "
953 			} else if (strncasecmp(cp, WWWAUTH,
954 			    sizeof(WWWAUTH) - 1) == 0) {
955 				cp += sizeof(WWWAUTH) - 1;
956 				FREEPTR(auth);
957 				auth = xstrdup(cp);
958 				if (debug)
959 					fprintf(ttyout,
960 					    "parsed www-auth as: %s\n", cp);
961 
962 			}
963 
964 		}
965 				/* finished parsing header */
966 		FREEPTR(buf);
967 
968 		switch (hcode) {
969 		case 200:
970 			break;
971 		case 206:
972 			if (! restart_point) {
973 				warnx("Not expecting partial content header");
974 				goto cleanup_fetch_url;
975 			}
976 			break;
977 		case 300:
978 		case 301:
979 		case 302:
980 		case 303:
981 		case 305:
982 			if (EMPTYSTRING(location)) {
983 				warnx(
984 				"No redirection Location provided by server");
985 				goto cleanup_fetch_url;
986 			}
987 			if (redirect_loop++ > 5) {
988 				warnx("Too many redirections requested");
989 				goto cleanup_fetch_url;
990 			}
991 			if (hcode == 305) {
992 				if (verbose)
993 					fprintf(ttyout, "Redirected via %s\n",
994 					    location);
995 				rval = fetch_url(url, location,
996 				    proxyauth, wwwauth);
997 			} else {
998 				if (verbose)
999 					fprintf(ttyout, "Redirected to %s\n",
1000 					    location);
1001 				rval = go_fetch(location);
1002 			}
1003 			goto cleanup_fetch_url;
1004 		case 401:
1005 		case 407:
1006 		    {
1007 			char **authp;
1008 			char *auser, *apass;
1009 
1010 			fprintf(ttyout, "%s\n", message);
1011 			if (EMPTYSTRING(auth)) {
1012 				warnx(
1013 			    "No authentication challenge provided by server");
1014 				goto cleanup_fetch_url;
1015 			}
1016 			if (hcode == 401) {
1017 				authp = &wwwauth;
1018 				auser = user;
1019 				apass = pass;
1020 			} else {
1021 				authp = &proxyauth;
1022 				auser = puser;
1023 				apass = ppass;
1024 			}
1025 			if (*authp != NULL) {
1026 				char reply[10];
1027 
1028 				fprintf(ttyout,
1029 				    "Authorization failed. Retry (y/n)? ");
1030 				if (fgets(reply, sizeof(reply), stdin)
1031 				    == NULL) {
1032 					clearerr(stdin);
1033 					goto cleanup_fetch_url;
1034 				} else {
1035 					if (tolower(reply[0]) != 'y')
1036 						goto cleanup_fetch_url;
1037 				}
1038 				auser = NULL;
1039 				apass = NULL;
1040 			}
1041 			if (auth_url(auth, authp, auser, apass) == 0) {
1042 				rval = fetch_url(url, proxyenv,
1043 				    proxyauth, wwwauth);
1044 				memset(*authp, 0, strlen(*authp));
1045 				FREEPTR(*authp);
1046 			}
1047 			goto cleanup_fetch_url;
1048 		    }
1049 		default:
1050 			if (message)
1051 				warnx("Error retrieving file - `%s'", message);
1052 			else
1053 				warnx("Unknown error retrieving file");
1054 			goto cleanup_fetch_url;
1055 		}
1056 	}		/* end of ftp:// or http:// specific setup */
1057 
1058 			/* Open the output file. */
1059 	if (strcmp(savefile, "-") == 0) {
1060 		fout = stdout;
1061 	} else if (*savefile == '|') {
1062 		oldintp = xsignal(SIGPIPE, SIG_IGN);
1063 		fout = popen(savefile + 1, "w");
1064 		if (fout == NULL) {
1065 			warn("Can't run `%s'", savefile + 1);
1066 			goto cleanup_fetch_url;
1067 		}
1068 		closefunc = pclose;
1069 	} else {
1070 		if ((rangeend != -1 && rangeend <= restart_point) ||
1071 		    (rangestart == -1 && filesize != -1 && filesize <= restart_point)) {
1072 			/* already done */
1073 			if (verbose)
1074 				fprintf(ttyout, "already done\n");
1075 			rval = 0;
1076 			goto cleanup_fetch_url;
1077 		}
1078 		if (restart_point && rangestart != -1) {
1079 			if (entitylen != -1)
1080 				filesize = entitylen;
1081 			if (rangestart != restart_point) {
1082 				warnx(
1083 				    "Size of `%s' differs from save file `%s'",
1084 				    url, savefile);
1085 				goto cleanup_fetch_url;
1086 			}
1087 			fout = fopen(savefile, "a");
1088 		} else
1089 			fout = fopen(savefile, "w");
1090 		if (fout == NULL) {
1091 			warn("Can't open `%s'", savefile);
1092 			goto cleanup_fetch_url;
1093 		}
1094 		closefunc = fclose;
1095 	}
1096 
1097 			/* Trap signals */
1098 	if (sigsetjmp(httpabort, 1))
1099 		goto cleanup_fetch_url;
1100 	(void)xsignal(SIGQUIT, psummary);
1101 	oldintr = xsignal(SIGINT, aborthttp);
1102 
1103 	if (rcvbuf_size > bufsize) {
1104 		if (xferbuf)
1105 			(void)free(xferbuf);
1106 		bufsize = rcvbuf_size;
1107 		xferbuf = xmalloc(bufsize);
1108 	}
1109 
1110 	bytes = 0;
1111 	hashbytes = mark;
1112 	progressmeter(-1);
1113 
1114 			/* Finally, suck down the file. */
1115 	do {
1116 		long chunksize;
1117 
1118 		chunksize = 0;
1119 					/* read chunksize */
1120 		if (ischunked) {
1121 			if (fgets(xferbuf, bufsize, fin) == NULL) {
1122 				warnx("Unexpected EOF reading chunksize");
1123 				goto cleanup_fetch_url;
1124 			}
1125 			chunksize = strtol(xferbuf, &ep, 16);
1126 
1127 				/*
1128 				 * XXX:	Work around bug in Apache 1.3.9 and
1129 				 *	1.3.11, which incorrectly put trailing
1130 				 *	space after the chunksize.
1131 				 */
1132 			while (*ep == ' ')
1133 				ep++;
1134 
1135 			if (strcmp(ep, "\r\n") != 0) {
1136 				warnx("Unexpected data following chunksize");
1137 				goto cleanup_fetch_url;
1138 			}
1139 			if (debug)
1140 				fprintf(ttyout, "got chunksize of " LLF "\n",
1141 				    (LLT)chunksize);
1142 			if (chunksize == 0)
1143 				break;
1144 		}
1145 					/* transfer file or chunk */
1146 		while (1) {
1147 			struct timeval then, now, td;
1148 			off_t bufrem;
1149 
1150 			if (rate_get)
1151 				(void)gettimeofday(&then, NULL);
1152 			bufrem = rate_get ? rate_get : bufsize;
1153 			if (ischunked)
1154 				bufrem = MIN(chunksize, bufrem);
1155 			while (bufrem > 0) {
1156 				len = fread(xferbuf, sizeof(char),
1157 				    MIN(bufsize, bufrem), fin);
1158 				if (len <= 0)
1159 					goto chunkdone;
1160 				bytes += len;
1161 				bufrem -= len;
1162 				if (fwrite(xferbuf, sizeof(char), len, fout)
1163 				    != len) {
1164 					warn("Writing `%s'", savefile);
1165 					goto cleanup_fetch_url;
1166 				}
1167 				if (hash && !progress) {
1168 					while (bytes >= hashbytes) {
1169 						(void)putc('#', ttyout);
1170 						hashbytes += mark;
1171 					}
1172 					(void)fflush(ttyout);
1173 				}
1174 				if (ischunked) {
1175 					chunksize -= len;
1176 					if (chunksize <= 0)
1177 						break;
1178 				}
1179 			}
1180 			if (rate_get) {
1181 				while (1) {
1182 					(void)gettimeofday(&now, NULL);
1183 					timersub(&now, &then, &td);
1184 					if (td.tv_sec > 0)
1185 						break;
1186 					usleep(1000000 - td.tv_usec);
1187 				}
1188 			}
1189 			if (ischunked && chunksize <= 0)
1190 				break;
1191 		}
1192 					/* read CRLF after chunk*/
1193  chunkdone:
1194 		if (ischunked) {
1195 			if (fgets(xferbuf, bufsize, fin) == NULL)
1196 				break;
1197 			if (strcmp(xferbuf, "\r\n") != 0) {
1198 				warnx("Unexpected data following chunk");
1199 				goto cleanup_fetch_url;
1200 			}
1201 		}
1202 	} while (ischunked);
1203 	if (hash && !progress && bytes > 0) {
1204 		if (bytes < mark)
1205 			(void)putc('#', ttyout);
1206 		(void)putc('\n', ttyout);
1207 	}
1208 	if (ferror(fin)) {
1209 		warn("Reading file");
1210 		goto cleanup_fetch_url;
1211 	}
1212 	progressmeter(1);
1213 	(void)fflush(fout);
1214 	if (closefunc == fclose && mtime != -1) {
1215 		struct timeval tval[2];
1216 
1217 		(void)gettimeofday(&tval[0], NULL);
1218 		tval[1].tv_sec = mtime;
1219 		tval[1].tv_usec = 0;
1220 		(*closefunc)(fout);
1221 		fout = NULL;
1222 
1223 		if (utimes(savefile, tval) == -1) {
1224 			fprintf(ttyout,
1225 			    "Can't change modification time to %s",
1226 			    asctime(localtime(&mtime)));
1227 		}
1228 	}
1229 	if (bytes > 0)
1230 		ptransfer(0);
1231 	bytes = 0;
1232 
1233 	rval = 0;
1234 	goto cleanup_fetch_url;
1235 
1236  improper:
1237 	warnx("Improper response from `%s'", host);
1238 
1239  cleanup_fetch_url:
1240 	if (oldintr)
1241 		(void)xsignal(SIGINT, oldintr);
1242 	if (oldintp)
1243 		(void)xsignal(SIGPIPE, oldintp);
1244 	if (fin != NULL)
1245 		fclose(fin);
1246 	else if (s != -1)
1247 		close(s);
1248 	if (closefunc != NULL && fout != NULL)
1249 		(*closefunc)(fout);
1250 	FREEPTR(savefile);
1251 	FREEPTR(user);
1252 	FREEPTR(pass);
1253 	FREEPTR(host);
1254 	FREEPTR(port);
1255 	FREEPTR(path);
1256 	FREEPTR(decodedpath);
1257 	FREEPTR(puser);
1258 	FREEPTR(ppass);
1259 	FREEPTR(buf);
1260 	FREEPTR(auth);
1261 	FREEPTR(location);
1262 	FREEPTR(message);
1263 	return (rval);
1264 }
1265 
1266 /*
1267  * Abort a HTTP retrieval
1268  */
1269 void
1270 aborthttp(int notused)
1271 {
1272 	char msgbuf[100];
1273 	int len;
1274 
1275 	alarmtimer(0);
1276 	len = strlcpy(msgbuf, "\nHTTP fetch aborted.\n", sizeof(msgbuf));
1277 	write(fileno(ttyout), msgbuf, len);
1278 	siglongjmp(httpabort, 1);
1279 }
1280 
1281 /*
1282  * Retrieve ftp URL or classic ftp argument using FTP.
1283  * Returns 1 on failure, 0 on completed xfer, -1 if ftp connection
1284  * is still open (e.g, ftp xfer with trailing /)
1285  */
1286 static int
1287 fetch_ftp(const char *url)
1288 {
1289 	char		*cp, *xargv[5], rempath[MAXPATHLEN];
1290 	char		*host, *path, *dir, *file, *user, *pass;
1291 	char		*port;
1292 	int		 dirhasglob, filehasglob, oautologin, rval, type, xargc;
1293 	in_port_t	 portnum;
1294 	url_t		 urltype;
1295 
1296 	host = path = dir = file = user = pass = NULL;
1297 	port = NULL;
1298 	rval = 1;
1299 	type = TYPE_I;
1300 
1301 	if (strncasecmp(url, FTP_URL, sizeof(FTP_URL) - 1) == 0) {
1302 		if ((parse_url(url, "URL", &urltype, &user, &pass,
1303 		    &host, &port, &portnum, &path) == -1) ||
1304 		    (user != NULL && *user == '\0') ||
1305 		    (pass != NULL && *pass == '\0') ||
1306 		    EMPTYSTRING(host)) {
1307 			warnx("Invalid URL `%s'", url);
1308 			goto cleanup_fetch_ftp;
1309 		}
1310 		url_decode(user);
1311 		url_decode(pass);
1312 		/*
1313 		 * Note: Don't url_decode(path) here.  We need to keep the
1314 		 * distinction between "/" and "%2F" until later.
1315 		 */
1316 
1317 					/* check for trailing ';type=[aid]' */
1318 		if (! EMPTYSTRING(path) && (cp = strrchr(path, ';')) != NULL) {
1319 			if (strcasecmp(cp, ";type=a") == 0)
1320 				type = TYPE_A;
1321 			else if (strcasecmp(cp, ";type=i") == 0)
1322 				type = TYPE_I;
1323 			else if (strcasecmp(cp, ";type=d") == 0) {
1324 				warnx(
1325 			    "Directory listing via a URL is not supported");
1326 				goto cleanup_fetch_ftp;
1327 			} else {
1328 				warnx("Invalid suffix `%s' in URL `%s'", cp,
1329 				    url);
1330 				goto cleanup_fetch_ftp;
1331 			}
1332 			*cp = 0;
1333 		}
1334 	} else {			/* classic style `[user@]host:[file]' */
1335 		urltype = CLASSIC_URL_T;
1336 		host = xstrdup(url);
1337 		cp = strchr(host, '@');
1338 		if (cp != NULL) {
1339 			*cp = '\0';
1340 			user = host;
1341 			anonftp = 0;	/* disable anonftp */
1342 			host = xstrdup(cp + 1);
1343 		}
1344 		cp = strchr(host, ':');
1345 		if (cp != NULL) {
1346 			*cp = '\0';
1347 			path = xstrdup(cp + 1);
1348 		}
1349 	}
1350 	if (EMPTYSTRING(host))
1351 		goto cleanup_fetch_ftp;
1352 
1353 			/* Extract the file and (if present) directory name. */
1354 	dir = path;
1355 	if (! EMPTYSTRING(dir)) {
1356 		/*
1357 		 * If we are dealing with classic `[user@]host:[path]' syntax,
1358 		 * then a path of the form `/file' (resulting from input of the
1359 		 * form `host:/file') means that we should do "CWD /" before
1360 		 * retrieving the file.  So we set dir="/" and file="file".
1361 		 *
1362 		 * But if we are dealing with URLs like `ftp://host/path' then
1363 		 * a path of the form `/file' (resulting from a URL of the form
1364 		 * `ftp://host//file') means that we should do `CWD ' (with an
1365 		 * empty argument) before retrieving the file.  So we set
1366 		 * dir="" and file="file".
1367 		 *
1368 		 * If the path does not contain / at all, we set dir=NULL.
1369 		 * (We get a path without any slashes if we are dealing with
1370 		 * classic `[user@]host:[file]' or URL `ftp://host/file'.)
1371 		 *
1372 		 * In all other cases, we set dir to a string that does not
1373 		 * include the final '/' that separates the dir part from the
1374 		 * file part of the path.  (This will be the empty string if
1375 		 * and only if we are dealing with a path of the form `/file'
1376 		 * resulting from an URL of the form `ftp://host//file'.)
1377 		 */
1378 		cp = strrchr(dir, '/');
1379 		if (cp == dir && urltype == CLASSIC_URL_T) {
1380 			file = cp + 1;
1381 			dir = "/";
1382 		} else if (cp != NULL) {
1383 			*cp++ = '\0';
1384 			file = cp;
1385 		} else {
1386 			file = dir;
1387 			dir = NULL;
1388 		}
1389 	} else
1390 		dir = NULL;
1391 	if (urltype == FTP_URL_T && file != NULL) {
1392 		url_decode(file);
1393 		/* but still don't url_decode(dir) */
1394 	}
1395 	if (debug)
1396 		fprintf(ttyout,
1397 		    "fetch_ftp: user `%s' pass `%s' host %s port %s "
1398 		    "path `%s' dir `%s' file `%s'\n",
1399 		    user ? user : "<null>", pass ? pass : "<null>",
1400 		    host ? host : "<null>", port ? port : "<null>",
1401 		    path ? path : "<null>",
1402 		    dir ? dir : "<null>", file ? file : "<null>");
1403 
1404 	dirhasglob = filehasglob = 0;
1405 	if (doglob && urltype == CLASSIC_URL_T) {
1406 		if (! EMPTYSTRING(dir) && strpbrk(dir, "*?[]{}") != NULL)
1407 			dirhasglob = 1;
1408 		if (! EMPTYSTRING(file) && strpbrk(file, "*?[]{}") != NULL)
1409 			filehasglob = 1;
1410 	}
1411 
1412 			/* Set up the connection */
1413 	if (connected)
1414 		disconnect(0, NULL);
1415 	xargv[0] = (char *)getprogname();	/* XXX discards const */
1416 	xargv[1] = host;
1417 	xargv[2] = NULL;
1418 	xargc = 2;
1419 	if (port) {
1420 		xargv[2] = port;
1421 		xargv[3] = NULL;
1422 		xargc = 3;
1423 	}
1424 	oautologin = autologin;
1425 		/* don't autologin in setpeer(), use ftp_login() below */
1426 	autologin = 0;
1427 	setpeer(xargc, xargv);
1428 	autologin = oautologin;
1429 	if ((connected == 0) ||
1430 	    (connected == 1 && !ftp_login(host, user, pass))) {
1431 		warnx("Can't connect or login to host `%s'", host);
1432 		goto cleanup_fetch_ftp;
1433 	}
1434 
1435 	switch (type) {
1436 	case TYPE_A:
1437 		setascii(1, xargv);
1438 		break;
1439 	case TYPE_I:
1440 		setbinary(1, xargv);
1441 		break;
1442 	default:
1443 		errx(1, "fetch_ftp: unknown transfer type %d", type);
1444 	}
1445 
1446 		/*
1447 		 * Change directories, if necessary.
1448 		 *
1449 		 * Note: don't use EMPTYSTRING(dir) below, because
1450 		 * dir=="" means something different from dir==NULL.
1451 		 */
1452 	if (dir != NULL && !dirhasglob) {
1453 		char *nextpart;
1454 
1455 		/*
1456 		 * If we are dealing with a classic `[user@]host:[path]'
1457 		 * (urltype is CLASSIC_URL_T) then we have a raw directory
1458 		 * name (not encoded in any way) and we can change
1459 		 * directories in one step.
1460 		 *
1461 		 * If we are dealing with an `ftp://host/path' URL
1462 		 * (urltype is FTP_URL_T), then RFC 1738 says we need to
1463 		 * send a separate CWD command for each unescaped "/"
1464 		 * in the path, and we have to interpret %hex escaping
1465 		 * *after* we find the slashes.  It's possible to get
1466 		 * empty components here, (from multiple adjacent
1467 		 * slashes in the path) and RFC 1738 says that we should
1468 		 * still do `CWD ' (with a null argument) in such cases.
1469 		 *
1470 		 * Many ftp servers don't support `CWD ', so if there's an
1471 		 * error performing that command, bail out with a descriptive
1472 		 * message.
1473 		 *
1474 		 * Examples:
1475 		 *
1476 		 * host:			dir="", urltype=CLASSIC_URL_T
1477 		 *		logged in (to default directory)
1478 		 * host:file			dir=NULL, urltype=CLASSIC_URL_T
1479 		 *		"RETR file"
1480 		 * host:dir/			dir="dir", urltype=CLASSIC_URL_T
1481 		 *		"CWD dir", logged in
1482 		 * ftp://host/			dir="", urltype=FTP_URL_T
1483 		 *		logged in (to default directory)
1484 		 * ftp://host/dir/		dir="dir", urltype=FTP_URL_T
1485 		 *		"CWD dir", logged in
1486 		 * ftp://host/file		dir=NULL, urltype=FTP_URL_T
1487 		 *		"RETR file"
1488 		 * ftp://host//file		dir="", urltype=FTP_URL_T
1489 		 *		"CWD ", "RETR file"
1490 		 * host:/file			dir="/", urltype=CLASSIC_URL_T
1491 		 *		"CWD /", "RETR file"
1492 		 * ftp://host///file		dir="/", urltype=FTP_URL_T
1493 		 *		"CWD ", "CWD ", "RETR file"
1494 		 * ftp://host/%2F/file		dir="%2F", urltype=FTP_URL_T
1495 		 *		"CWD /", "RETR file"
1496 		 * ftp://host/foo/file		dir="foo", urltype=FTP_URL_T
1497 		 *		"CWD foo", "RETR file"
1498 		 * ftp://host/foo/bar/file	dir="foo/bar"
1499 		 *		"CWD foo", "CWD bar", "RETR file"
1500 		 * ftp://host//foo/bar/file	dir="/foo/bar"
1501 		 *		"CWD ", "CWD foo", "CWD bar", "RETR file"
1502 		 * ftp://host/foo//bar/file	dir="foo//bar"
1503 		 *		"CWD foo", "CWD ", "CWD bar", "RETR file"
1504 		 * ftp://host/%2F/foo/bar/file	dir="%2F/foo/bar"
1505 		 *		"CWD /", "CWD foo", "CWD bar", "RETR file"
1506 		 * ftp://host/%2Ffoo/bar/file	dir="%2Ffoo/bar"
1507 		 *		"CWD /foo", "CWD bar", "RETR file"
1508 		 * ftp://host/%2Ffoo%2Fbar/file	dir="%2Ffoo%2Fbar"
1509 		 *		"CWD /foo/bar", "RETR file"
1510 		 * ftp://host/%2Ffoo%2Fbar%2Ffile	dir=NULL
1511 		 *		"RETR /foo/bar/file"
1512 		 *
1513 		 * Note that we don't need `dir' after this point.
1514 		 */
1515 		do {
1516 			if (urltype == FTP_URL_T) {
1517 				nextpart = strchr(dir, '/');
1518 				if (nextpart) {
1519 					*nextpart = '\0';
1520 					nextpart++;
1521 				}
1522 				url_decode(dir);
1523 			} else
1524 				nextpart = NULL;
1525 			if (debug)
1526 				fprintf(ttyout, "dir `%s', nextpart `%s'\n",
1527 				    dir ? dir : "<null>",
1528 				    nextpart ? nextpart : "<null>");
1529 			if (urltype == FTP_URL_T || *dir != '\0') {
1530 				xargv[0] = "cd";
1531 				xargv[1] = dir;
1532 				xargv[2] = NULL;
1533 				dirchange = 0;
1534 				cd(2, xargv);
1535 				if (! dirchange) {
1536 					if (*dir == '\0' && code == 500)
1537 						fprintf(stderr,
1538 "\n"
1539 "ftp: The `CWD ' command (without a directory), which is required by\n"
1540 "     RFC 1738 to support the empty directory in the URL pathname (`//'),\n"
1541 "     conflicts with the server's conformance to RFC 959.\n"
1542 "     Try the same URL without the `//' in the URL pathname.\n"
1543 "\n");
1544 					goto cleanup_fetch_ftp;
1545 				}
1546 			}
1547 			dir = nextpart;
1548 		} while (dir != NULL);
1549 	}
1550 
1551 	if (EMPTYSTRING(file)) {
1552 		rval = -1;
1553 		goto cleanup_fetch_ftp;
1554 	}
1555 
1556 	if (dirhasglob) {
1557 		(void)strlcpy(rempath, dir,	sizeof(rempath));
1558 		(void)strlcat(rempath, "/",	sizeof(rempath));
1559 		(void)strlcat(rempath, file,	sizeof(rempath));
1560 		file = rempath;
1561 	}
1562 
1563 			/* Fetch the file(s). */
1564 	xargc = 2;
1565 	xargv[0] = "get";
1566 	xargv[1] = file;
1567 	xargv[2] = NULL;
1568 	if (dirhasglob || filehasglob) {
1569 		int ointeractive;
1570 
1571 		ointeractive = interactive;
1572 		interactive = 0;
1573 		xargv[0] = "mget";
1574 		mget(xargc, xargv);
1575 		interactive = ointeractive;
1576 	} else {
1577 		if (outfile == NULL) {
1578 			cp = strrchr(file, '/');	/* find savefile */
1579 			if (cp != NULL)
1580 				outfile = cp + 1;
1581 			else
1582 				outfile = file;
1583 		}
1584 		xargv[2] = (char *)outfile;
1585 		xargv[3] = NULL;
1586 		xargc++;
1587 		if (restartautofetch)
1588 			reget(xargc, xargv);
1589 		else
1590 			get(xargc, xargv);
1591 	}
1592 
1593 	if ((code / 100) == COMPLETE)
1594 		rval = 0;
1595 
1596  cleanup_fetch_ftp:
1597 	FREEPTR(host);
1598 	FREEPTR(path);
1599 	FREEPTR(user);
1600 	FREEPTR(pass);
1601 	return (rval);
1602 }
1603 
1604 /*
1605  * Retrieve the given file to outfile.
1606  * Supports arguments of the form:
1607  *	"host:path", "ftp://host/path"	if $ftpproxy, call fetch_url() else
1608  *					call fetch_ftp()
1609  *	"http://host/path"		call fetch_url() to use HTTP
1610  *	"file:///path"			call fetch_url() to copy
1611  *	"about:..."			print a message
1612  *
1613  * Returns 1 on failure, 0 on completed xfer, -1 if ftp connection
1614  * is still open (e.g, ftp xfer with trailing /)
1615  */
1616 static int
1617 go_fetch(const char *url)
1618 {
1619 	char *proxy;
1620 
1621 	/*
1622 	 * Check for about:*
1623 	 */
1624 	if (strncasecmp(url, ABOUT_URL, sizeof(ABOUT_URL) - 1) == 0) {
1625 		url += sizeof(ABOUT_URL) -1;
1626 		if (strcasecmp(url, "ftp") == 0 ||
1627 		    strcasecmp(url, "tnftp") == 0) {
1628 			fputs(
1629 "This version of ftp has been enhanced by Luke Mewburn <lukem@NetBSD.org>\n"
1630 "for the NetBSD project.  Execute `man ftp' for more details.\n", ttyout);
1631 		} else if (strcasecmp(url, "lukem") == 0) {
1632 			fputs(
1633 "Luke Mewburn is the author of most of the enhancements in this ftp client.\n"
1634 "Please email feedback to <lukem@NetBSD.org>.\n", ttyout);
1635 		} else if (strcasecmp(url, "netbsd") == 0) {
1636 			fputs(
1637 "NetBSD is a freely available and redistributable UNIX-like operating system.\n"
1638 "For more information, see http://www.NetBSD.org/\n", ttyout);
1639 		} else if (strcasecmp(url, "version") == 0) {
1640 			fprintf(ttyout, "Version: %s %s%s\n",
1641 			    FTP_PRODUCT, FTP_VERSION,
1642 #ifdef INET6
1643 			    ""
1644 #else
1645 			    " (-IPv6)"
1646 #endif
1647 			);
1648 		} else {
1649 			fprintf(ttyout, "`%s' is an interesting topic.\n", url);
1650 		}
1651 		fputs("\n", ttyout);
1652 		return (0);
1653 	}
1654 
1655 	/*
1656 	 * Check for file:// and http:// URLs.
1657 	 */
1658 	if (strncasecmp(url, HTTP_URL, sizeof(HTTP_URL) - 1) == 0 ||
1659 	    strncasecmp(url, FILE_URL, sizeof(FILE_URL) - 1) == 0)
1660 		return (fetch_url(url, NULL, NULL, NULL));
1661 
1662 	/*
1663 	 * Try FTP URL-style and host:file arguments next.
1664 	 * If ftpproxy is set with an FTP URL, use fetch_url()
1665 	 * Othewise, use fetch_ftp().
1666 	 */
1667 	proxy = getoptionvalue("ftp_proxy");
1668 	if (!EMPTYSTRING(proxy) &&
1669 	    strncasecmp(url, FTP_URL, sizeof(FTP_URL) - 1) == 0)
1670 		return (fetch_url(url, NULL, NULL, NULL));
1671 
1672 	return (fetch_ftp(url));
1673 }
1674 
1675 /*
1676  * Retrieve multiple files from the command line,
1677  * calling go_fetch() for each file.
1678  *
1679  * If an ftp path has a trailing "/", the path will be cd-ed into and
1680  * the connection remains open, and the function will return -1
1681  * (to indicate the connection is alive).
1682  * If an error occurs the return value will be the offset+1 in
1683  * argv[] of the file that caused a problem (i.e, argv[x]
1684  * returns x+1)
1685  * Otherwise, 0 is returned if all files retrieved successfully.
1686  */
1687 int
1688 auto_fetch(int argc, char *argv[])
1689 {
1690 	volatile int	argpos;
1691 	int		rval;
1692 
1693 	argpos = 0;
1694 
1695 	if (sigsetjmp(toplevel, 1)) {
1696 		if (connected)
1697 			disconnect(0, NULL);
1698 		return (argpos + 1);
1699 	}
1700 	(void)xsignal(SIGINT, intr);
1701 	(void)xsignal(SIGPIPE, lostpeer);
1702 
1703 	/*
1704 	 * Loop through as long as there's files to fetch.
1705 	 */
1706 	for (rval = 0; (rval == 0) && (argpos < argc); argpos++) {
1707 		if (strchr(argv[argpos], ':') == NULL)
1708 			break;
1709 		redirect_loop = 0;
1710 		if (!anonftp)
1711 			anonftp = 2;	/* Handle "automatic" transfers. */
1712 		rval = go_fetch(argv[argpos]);
1713 		if (outfile != NULL && strcmp(outfile, "-") != 0
1714 		    && outfile[0] != '|')
1715 			outfile = NULL;
1716 		if (rval > 0)
1717 			rval = argpos + 1;
1718 	}
1719 
1720 	if (connected && rval != -1)
1721 		disconnect(0, NULL);
1722 	return (rval);
1723 }
1724 
1725 
1726 int
1727 auto_put(int argc, char **argv, const char *uploadserver)
1728 {
1729 	char	*uargv[4], *path, *pathsep;
1730 	int	 uargc, rval, len;
1731 
1732 	uargc = 0;
1733 	uargv[uargc++] = "mput";
1734 	uargv[uargc++] = argv[0];
1735 	uargv[2] = uargv[3] = NULL;
1736 	pathsep = NULL;
1737 	rval = 1;
1738 
1739 	if (debug)
1740 		fprintf(ttyout, "auto_put: target `%s'\n", uploadserver);
1741 
1742 	path = xstrdup(uploadserver);
1743 	len = strlen(path);
1744 	if (path[len - 1] != '/' && path[len - 1] != ':') {
1745 			/*
1746 			 * make sure we always pass a directory to auto_fetch
1747 			 */
1748 		if (argc > 1) {		/* more than one file to upload */
1749 			int len;
1750 
1751 			len = strlen(uploadserver) + 2;	/* path + "/" + "\0" */
1752 			free(path);
1753 			path = (char *)xmalloc(len);
1754 			(void)strlcpy(path, uploadserver, len);
1755 			(void)strlcat(path, "/", len);
1756 		} else {		/* single file to upload */
1757 			uargv[0] = "put";
1758 			pathsep = strrchr(path, '/');
1759 			if (pathsep == NULL) {
1760 				pathsep = strrchr(path, ':');
1761 				if (pathsep == NULL) {
1762 					warnx("Invalid URL `%s'", path);
1763 					goto cleanup_auto_put;
1764 				}
1765 				pathsep++;
1766 				uargv[2] = xstrdup(pathsep);
1767 				pathsep[0] = '/';
1768 			} else
1769 				uargv[2] = xstrdup(pathsep + 1);
1770 			pathsep[1] = '\0';
1771 			uargc++;
1772 		}
1773 	}
1774 	if (debug)
1775 		fprintf(ttyout, "auto_put: URL `%s' argv[2] `%s'\n",
1776 		    path, uargv[2] ? uargv[2] : "<null>");
1777 
1778 			/* connect and cwd */
1779 	rval = auto_fetch(1, &path);
1780 	free(path);
1781 	if(rval >= 0)
1782 		goto cleanup_auto_put;
1783 
1784 			/* XXX : is this the best way? */
1785 	if (uargc == 3) {
1786 		uargv[1] = argv[0];
1787 		put(uargc, uargv);
1788 		goto cleanup_auto_put;
1789 	}
1790 
1791 	for(; argv[0] != NULL; argv++) {
1792 		uargv[1] = argv[0];
1793 		mput(uargc, uargv);
1794 	}
1795 	rval = 0;
1796 
1797  cleanup_auto_put:
1798 	FREEPTR(uargv[2]);
1799 	return (rval);
1800 }
1801