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