xref: /openbsd-src/usr.bin/ftp/fetch.c (revision 0eea0d082377cb9c3ec583313dc4d52b7b6a4d6d)
1 /*	$OpenBSD: fetch.c,v 1.50 2004/07/20 03:50:25 deraadt Exp $	*/
2 /*	$NetBSD: fetch.c,v 1.14 1997/08/18 10:20:20 lukem Exp $	*/
3 
4 /*-
5  * Copyright (c) 1997 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Jason Thorpe and Luke Mewburn.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *        This product includes software developed by the NetBSD
22  *        Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 #if !defined(lint) && !defined(SMALL)
41 static char rcsid[] = "$OpenBSD: fetch.c,v 1.50 2004/07/20 03:50:25 deraadt Exp $";
42 #endif /* not lint and not SMALL */
43 
44 /*
45  * FTP User Program -- Command line file retrieval
46  */
47 
48 #include <sys/types.h>
49 #include <sys/param.h>
50 #include <sys/socket.h>
51 #include <sys/stat.h>
52 
53 #include <netinet/in.h>
54 
55 #include <arpa/ftp.h>
56 #include <arpa/inet.h>
57 
58 #include <ctype.h>
59 #include <err.h>
60 #include <libgen.h>
61 #include <netdb.h>
62 #include <fcntl.h>
63 #include <signal.h>
64 #include <stdio.h>
65 #include <errno.h>
66 #include <stdlib.h>
67 #include <string.h>
68 #include <unistd.h>
69 #include <util.h>
70 
71 #include "ftp_var.h"
72 
73 static int	url_get(const char *, const char *, const char *);
74 void		aborthttp(int);
75 void		abortfile(int);
76 char		hextochar(const char *);
77 char		*urldecode(const char *);
78 
79 #define	FTP_URL		"ftp://"	/* ftp URL prefix */
80 #define	HTTP_URL	"http://"	/* http URL prefix */
81 #define	FILE_URL	"file:"		/* file URL prefix */
82 #define FTP_PROXY	"ftp_proxy"	/* env var with ftp proxy location */
83 #define HTTP_PROXY	"http_proxy"	/* env var with http proxy location */
84 
85 
86 #define EMPTYSTRING(x)	((x) == NULL || (*(x) == '\0'))
87 
88 static const char *at_encoding_warning =
89    "Extra `@' characters in usernames and passwords should be encoded as %%40";
90 
91 jmp_buf	httpabort;
92 
93 /*
94  * Retrieve URL, via the proxy in $proxyvar if necessary.
95  * Modifies the string argument given.
96  * Returns -1 on failure, 0 on success
97  */
98 static int
99 url_get(const char *origline, const char *proxyenv, const char *outfile)
100 {
101 	struct addrinfo hints, *res0, *res;
102 	int error;
103 	int i, isftpurl, isfileurl, isredirect;
104 	volatile int s, out;
105 	size_t len;
106 	char *cp, *ep, *portnum, *path;
107 	char pbuf[NI_MAXSERV], hbuf[NI_MAXHOST];
108 	const char * volatile savefile;
109 	char *line, *host, *port, *buf;
110 	char * volatile proxy;
111 	char *hosttail;
112 	volatile sig_t oldintr;
113 	off_t hashbytes;
114 	char *cause = "unknown";
115 	FILE *fin;
116 	int rval;
117 
118 	s = -1;
119 	proxy = NULL;
120 	fin = NULL;
121 	buf = NULL;
122 	isftpurl = 0;
123 	isfileurl = 0;
124 	isredirect = 0;
125 	rval = -1;
126 
127 	line = strdup(origline);
128 	if (line == NULL)
129 		errx(1, "Can't allocate memory to parse URL");
130 	if (strncasecmp(line, HTTP_URL, sizeof(HTTP_URL) - 1) == 0)
131 		host = line + sizeof(HTTP_URL) - 1;
132 	else if (strncasecmp(line, FTP_URL, sizeof(FTP_URL) - 1) == 0) {
133 		host = line + sizeof(FTP_URL) - 1;
134 		isftpurl = 1;
135 	} else if (strncasecmp(line, FILE_URL, sizeof(FILE_URL) - 1) == 0) {
136 		host = line + sizeof(FILE_URL) - 1;
137 		isfileurl = 1;
138 	} else
139 		errx(1, "url_get: Invalid URL '%s'", line);
140 
141 	if (isfileurl) {
142 		path = host;
143 	} else {
144 		path = strchr(host, '/');		/* find path */
145 		if (EMPTYSTRING(path)) {
146 			if (isftpurl)
147 				goto noftpautologin;
148 			warnx("Invalid URL (no `/' after host): %s", origline);
149 			goto cleanup_url_get;
150 		}
151 		*path++ = '\0';
152 		if (EMPTYSTRING(path)) {
153 			if (isftpurl)
154 				goto noftpautologin;
155 			warnx("Invalid URL (no file after host): %s", origline);
156 			goto cleanup_url_get;
157 		}
158 	}
159 
160 	if (outfile)
161 		savefile = outfile;
162 	else
163 		savefile = basename(path);
164 
165 	if (EMPTYSTRING(savefile)) {
166 		if (isftpurl)
167 			goto noftpautologin;
168 		warnx("Invalid URL (no file after directory): %s", origline);
169 		goto cleanup_url_get;
170 	}
171 
172 	if (proxyenv != NULL) {				/* use proxy */
173 		proxy = strdup(proxyenv);
174 		if (proxy == NULL)
175 			errx(1, "Can't allocate memory for proxy URL.");
176 		if (strncasecmp(proxy, HTTP_URL, sizeof(HTTP_URL) - 1) == 0)
177 			host = proxy + sizeof(HTTP_URL) - 1;
178 		else if (strncasecmp(proxy, FTP_URL, sizeof(FTP_URL) - 1) == 0)
179 			host = proxy + sizeof(FTP_URL) - 1;
180 		else {
181 			warnx("Malformed proxy URL: %s", proxyenv);
182 			goto cleanup_url_get;
183 		}
184 		if (EMPTYSTRING(host)) {
185 			warnx("Malformed proxy URL: %s", proxyenv);
186 			goto cleanup_url_get;
187 		}
188 		*--path = '/';			/* add / back to real path */
189 		path = strchr(host, '/');	/* remove trailing / on host */
190 		if (!EMPTYSTRING(path))
191 			*path++ = '\0';
192 		path = line;
193 	}
194 
195 	if (isfileurl) {
196 		struct stat st;
197 
198 		s = open(path, O_RDONLY);
199 		if (s == -1) {
200 			warn("Can't open file %s", path);
201 			goto cleanup_url_get;
202 		}
203 
204 		if (fstat(s, &st) == -1)
205 			filesize = -1;
206 		else
207 			filesize = st.st_size;
208 
209 		/* Open the output file.  */
210 		if (strcmp(savefile, "-") != 0) {
211 			out = open(savefile, O_CREAT | O_WRONLY | O_TRUNC, 0666);
212 			if (out < 0) {
213 				warn("Can't open %s", savefile);
214 				goto cleanup_url_get;
215 			}
216 		} else
217 			out = fileno(stdout);
218 
219 		/* Trap signals */
220 		oldintr = NULL;
221 		if (setjmp(httpabort)) {
222 			if (oldintr)
223 				(void)signal(SIGINT, oldintr);
224 			goto cleanup_url_get;
225 		}
226 		oldintr = signal(SIGINT, abortfile);
227 
228 		bytes = 0;
229 		hashbytes = mark;
230 		progressmeter(-1);
231 
232 		if ((buf = malloc(4096)) == NULL)
233 			errx(1, "Can't allocate memory for transfer buffer");
234 
235 		/* Finally, suck down the file. */
236 		i = 0;
237 		while ((len = read(s, buf, 4096)) > 0) {
238 			bytes += len;
239 			for (cp = buf; len > 0; len -= i, cp += i) {
240 				if ((i = write(out, cp, len)) == -1) {
241 					warn("Writing %s", savefile);
242 					goto cleanup_url_get;
243 				}
244 				else if (i == 0)
245 					break;
246 			}
247 			if (hash && !progress) {
248 				while (bytes >= hashbytes) {
249 					(void)putc('#', ttyout);
250 					hashbytes += mark;
251 				}
252 				(void)fflush(ttyout);
253 			}
254 		}
255 		if (hash && !progress && bytes > 0) {
256 			if (bytes < mark)
257 				(void)putc('#', ttyout);
258 			(void)putc('\n', ttyout);
259 			(void)fflush(ttyout);
260 		}
261 		if (len != 0) {
262 			warn("Reading from file");
263 			goto cleanup_url_get;
264 		}
265 		progressmeter(1);
266 		if (verbose)
267 			fputs("Successfully retrieved file.\n", ttyout);
268 		(void)signal(SIGINT, oldintr);
269 
270 		rval = 0;
271 		goto cleanup_url_get;
272 	}
273 
274 	if (*host == '[' && (hosttail = strrchr(host, ']')) != NULL &&
275 	    (hosttail[1] == '\0' || hosttail[1] == ':')) {
276 		host++;
277 		*hosttail++ = '\0';
278 	} else
279 		hosttail = host;
280 
281 	portnum = strrchr(hosttail, ':');		/* find portnum */
282 	if (portnum != NULL)
283 		*portnum++ = '\0';
284 
285 	if (debug)
286 		fprintf(ttyout, "host %s, port %s, path %s, save as %s.\n",
287 		    host, portnum, path, savefile);
288 
289 	memset(&hints, 0, sizeof(hints));
290 	hints.ai_family = family;
291 	hints.ai_socktype = SOCK_STREAM;
292 	port = portnum ? portnum : httpport;
293 	error = getaddrinfo(host, port, &hints, &res0);
294 	if (error == EAI_SERVICE && port == httpport) {
295 		/*
296 		 * If the services file is corrupt/missing, fall back
297 		 * on our hard-coded defines.
298 		 */
299 		snprintf(pbuf, sizeof(pbuf), "%d", HTTP_PORT);
300 		error = getaddrinfo(host, pbuf, &hints, &res0);
301 	}
302 	if (error) {
303 		warnx("%s: %s", gai_strerror(error), host);
304 		goto cleanup_url_get;
305 	}
306 
307 	s = -1;
308 	for (res = res0; res; res = res->ai_next) {
309 		if (getnameinfo(res->ai_addr, res->ai_addrlen, hbuf,
310 		    sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0)
311 			strlcpy(hbuf, "(unknown)", sizeof(hbuf));
312 		if (verbose)
313 			fprintf(ttyout, "Trying %s...\n", hbuf);
314 
315 		s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
316 		if (s == -1) {
317 			cause = "socket";
318 			continue;
319 		}
320 
321 again:
322 		if (connect(s, res->ai_addr, res->ai_addrlen) < 0) {
323 			if (errno == EINTR)
324 				goto again;
325 			close(s);
326 			s = -1;
327 			cause = "connect";
328 			continue;
329 		}
330 
331 		/* get port in numeric */
332 		if (getnameinfo(res->ai_addr, res->ai_addrlen, NULL, 0,
333 		    pbuf, sizeof(pbuf), NI_NUMERICSERV) == 0)
334 			port = pbuf;
335 		else
336 			port = NULL;
337 
338 		break;
339 	}
340 	freeaddrinfo(res0);
341 	if (s < 0) {
342 		warn("%s", cause);
343 		goto cleanup_url_get;
344 	}
345 
346 	fin = fdopen(s, "r+");
347 
348 	/*
349 	 * Construct and send the request. Proxy requests don't want leading /.
350 	 */
351 	if (proxy) {
352 		/*
353 		 * Host: directive must use the destination host address for
354 		 * the original URI (path).  We do not attach it at this moment.
355 		 */
356 		if (verbose)
357 			fprintf(ttyout, "Requesting %s (via %s)\n",
358 			    origline, proxyenv);
359 		fprintf(fin, "GET %s HTTP/1.0\r\n%s\r\n\r\n", path, HTTP_USER_AGENT);
360 	} else {
361 		if (verbose)
362 			fprintf(ttyout, "Requesting %s\n", origline);
363 		if (strchr(host, ':')) {
364 			char *h, *p;
365 
366 			/* strip off scoped address portion, since it's local to node */
367 			h = strdup(host);
368 			if (h == NULL)
369 				errx(1, "Can't allocate memory.");
370 			if ((p = strchr(h, '%')) != NULL)
371 				*p = '\0';
372 			/*
373 			 * Send port number only if it's specified and does not equal
374 			 * 80. Some broken HTTP servers get confused if you explicitly
375 			 * send them the port number.
376 			 */
377 			if (port && strcmp(port, "80") != 0)
378 				fprintf(fin,
379 				    "GET /%s HTTP/1.0\r\nHost: [%s]:%s\r\n%s\r\n\r\n",
380 				    path, h, port, HTTP_USER_AGENT);
381 			else
382 				fprintf(fin,
383 				    "GET /%s HTTP/1.0\r\nHost: [%s]\r\n%s\r\n\r\n",
384 				    path, h, HTTP_USER_AGENT);
385 			free(h);
386 		} else {
387 			if (port && strcmp(port, "80") != 0)
388 				fprintf(fin,
389 				    "GET /%s HTTP/1.0\r\nHost: %s:%s\r\n%s\r\n\r\n",
390 				    path, host, port, HTTP_USER_AGENT);
391 			else
392 				fprintf(fin,
393 				    "GET /%s HTTP/1.0\r\nHost: %s\r\n%s\r\n\r\n",
394 				    path, host, HTTP_USER_AGENT);
395 		}
396 	}
397 	if (fflush(fin) == EOF) {
398 		warn("Writing HTTP request");
399 		goto cleanup_url_get;
400 	}
401 
402 	if ((buf = fparseln(fin, &len, NULL, "\0\0\0", 0)) == NULL) {
403 		warn("Receiving HTTP reply");
404 		goto cleanup_url_get;
405 	}
406 
407 	while (len > 0 && (buf[len-1] == '\r' || buf[len-1] == '\n'))
408 		buf[--len] = '\0';
409 	if (debug)
410 		fprintf(ttyout, "received '%s'\n", buf);
411 
412 	cp = strchr(buf, ' ');
413 	if (cp == NULL)
414 		goto improper;
415 	else
416 		cp++;
417 	if (strncmp(cp, "301", 3) == 0 || strncmp(cp, "302", 3) == 0) {
418 		isredirect++;
419 	} else if (strncmp(cp, "200", 3)) {
420 		warnx("Error retrieving file: %s", cp);
421 		goto cleanup_url_get;
422 	}
423 
424 	/*
425 	 * Read the rest of the header.
426 	 */
427 	free(buf);
428 	filesize = -1;
429 
430 	while (1) {
431 		if ((buf = fparseln(fin, &len, NULL, "\0\0\0", 0)) == NULL) {
432 			warn("Receiving HTTP reply");
433 			goto cleanup_url_get;
434 		}
435 		while (len > 0 && (buf[len-1] == '\r' || buf[len-1] == '\n'))
436 			buf[--len] = '\0';
437 		if (len == 0)
438 			break;
439 		if (debug)
440 			fprintf(ttyout, "received '%s'\n", buf);
441 
442 		/* Look for some headers */
443 		cp = buf;
444 #define CONTENTLEN "Content-Length: "
445 		if (strncasecmp(cp, CONTENTLEN, sizeof(CONTENTLEN) - 1) == 0) {
446 			cp += sizeof(CONTENTLEN) - 1;
447 			filesize = strtol(cp, &ep, 10);
448 			if (filesize < 1 || *ep != '\0')
449 				goto improper;
450 #define LOCATION "Location: "
451 		} else if (isredirect &&
452 		    strncasecmp(cp, LOCATION, sizeof(LOCATION) - 1) == 0) {
453 			cp += sizeof(LOCATION) - 1;
454 			if (verbose)
455 				fprintf(ttyout, "Redirected to %s\n", cp);
456 			if (fin != NULL)
457 				fclose(fin);
458 			else if (s != -1)
459 				close(s);
460 			if (proxy)
461 				free(proxy);
462 			free(line);
463 			rval = url_get(cp, proxyenv, outfile);
464 			if (buf)
465 				free(buf);
466 			return (rval);
467 		}
468 	}
469 
470 	/* Open the output file.  */
471 	if (strcmp(savefile, "-") != 0) {
472 		out = open(savefile, O_CREAT | O_WRONLY | O_TRUNC, 0666);
473 		if (out < 0) {
474 			warn("Can't open %s", savefile);
475 			goto cleanup_url_get;
476 		}
477 	} else
478 		out = fileno(stdout);
479 
480 	/* Trap signals */
481 	oldintr = NULL;
482 	if (setjmp(httpabort)) {
483 		if (oldintr)
484 			(void)signal(SIGINT, oldintr);
485 		goto cleanup_url_get;
486 	}
487 	oldintr = signal(SIGINT, aborthttp);
488 
489 	bytes = 0;
490 	hashbytes = mark;
491 	progressmeter(-1);
492 
493 	free(buf);
494 
495 	/* Finally, suck down the file. */
496 	if ((buf = malloc(4096)) == NULL)
497 		errx(1, "Can't allocate memory for transfer buffer");
498 	i = 0;
499 	while ((len = fread(buf, sizeof(char), 4096, fin)) > 0) {
500 		bytes += len;
501 		for (cp = buf; len > 0; len -= i, cp += i) {
502 			if ((i = write(out, cp, len)) == -1) {
503 				warn("Writing %s", savefile);
504 				goto cleanup_url_get;
505 			}
506 			else if (i == 0)
507 				break;
508 		}
509 		if (hash && !progress) {
510 			while (bytes >= hashbytes) {
511 				(void)putc('#', ttyout);
512 				hashbytes += mark;
513 			}
514 			(void)fflush(ttyout);
515 		}
516 	}
517 	if (hash && !progress && bytes > 0) {
518 		if (bytes < mark)
519 			(void)putc('#', ttyout);
520 		(void)putc('\n', ttyout);
521 		(void)fflush(ttyout);
522 	}
523 	if (len != 0) {
524 		warn("Reading from socket");
525 		goto cleanup_url_get;
526 	}
527 	progressmeter(1);
528 	if (filesize != -1 && len == 0 && bytes != filesize) {
529 		if (verbose)
530 			fputs("Read short file.\n", ttyout);
531 		goto cleanup_url_get;
532 	}
533 
534 	if (verbose)
535 		fputs("Successfully retrieved file.\n", ttyout);
536 	(void)signal(SIGINT, oldintr);
537 
538 	rval = 0;
539 	goto cleanup_url_get;
540 
541 noftpautologin:
542 	warnx(
543 	    "Auto-login using ftp URLs isn't supported when using $ftp_proxy");
544 	goto cleanup_url_get;
545 
546 improper:
547 	warnx("Improper response from %s", host);
548 
549 cleanup_url_get:
550 	if (fin != NULL)
551 		fclose(fin);
552 	else if (s != -1)
553 		close(s);
554 	if (buf)
555 		free(buf);
556 	if (proxy)
557 		free(proxy);
558 	free(line);
559 	return (rval);
560 }
561 
562 /*
563  * Abort a http retrieval
564  */
565 void
566 aborthttp(int notused)
567 {
568 
569 	alarmtimer(0);
570 	fputs("\nhttp fetch aborted.\n", ttyout);
571 	(void)fflush(ttyout);
572 	longjmp(httpabort, 1);
573 }
574 
575 /*
576  * Abort a http retrieval
577  */
578 void
579 abortfile(int notused)
580 {
581 
582 	alarmtimer(0);
583 	fputs("\nfile fetch aborted.\n", ttyout);
584 	(void)fflush(ttyout);
585 	longjmp(httpabort, 1);
586 }
587 
588 /*
589  * Retrieve multiple files from the command line, transferring
590  * files of the form "host:path", "ftp://host/path" using the
591  * ftp protocol, and files of the form "http://host/path" using
592  * the http protocol.
593  * If path has a trailing "/", then return (-1);
594  * the path will be cd-ed into and the connection remains open,
595  * and the function will return -1 (to indicate the connection
596  * is alive).
597  * If an error occurs the return value will be the offset+1 in
598  * argv[] of the file that caused a problem (i.e, argv[x]
599  * returns x+1)
600  * Otherwise, 0 is returned if all files retrieved successfully.
601  */
602 int
603 auto_fetch(int argc, char *argv[], char *outfile)
604 {
605 	char *xargv[5];
606 	char *cp, *line, *host, *dir, *file, *portnum;
607 	char *user, *pass;
608 	char *ftpproxy, *httpproxy;
609 	int rval, xargc;
610 	volatile int argpos;
611 	int dirhasglob, filehasglob, oautologin;
612 	char rempath[MAXPATHLEN];
613 
614 	argpos = 0;
615 
616 	if (setjmp(toplevel)) {
617 		if (connected)
618 			disconnect(0, NULL);
619 		return (argpos + 1);
620 	}
621 	(void)signal(SIGINT, (sig_t)intr);
622 	(void)signal(SIGPIPE, (sig_t)lostpeer);
623 
624 	if ((ftpproxy = getenv(FTP_PROXY)) != NULL && *ftpproxy == '\0')
625 		ftpproxy = NULL;
626 	if ((httpproxy = getenv(HTTP_PROXY)) != NULL && *httpproxy == '\0')
627 		httpproxy = NULL;
628 
629 	/*
630 	 * Loop through as long as there's files to fetch.
631 	 */
632 	for (rval = 0; (rval == 0) && (argpos < argc); free(line), argpos++) {
633 		if (strchr(argv[argpos], ':') == NULL)
634 			break;
635 		host = dir = file = portnum = user = pass = NULL;
636 
637 		/*
638 		 * We muck with the string, so we make a copy.
639 		 */
640 		line = strdup(argv[argpos]);
641 		if (line == NULL)
642 			errx(1, "Can't allocate memory for auto-fetch.");
643 
644 		/*
645 		 * Try HTTP URL-style arguments first.
646 		 */
647 		if (strncasecmp(line, HTTP_URL, sizeof(HTTP_URL) - 1) == 0 ||
648 		    strncasecmp(line, FILE_URL, sizeof(FILE_URL) - 1) == 0) {
649 			if (url_get(line, httpproxy, outfile) == -1)
650 				rval = argpos + 1;
651 			continue;
652 		}
653 
654 		/*
655 		 * Try FTP URL-style arguments next. If ftpproxy is
656 		 * set, use url_get() instead of standard ftp.
657 		 * Finally, try host:file.
658 		 */
659 		host = line;
660 		if (strncasecmp(line, FTP_URL, sizeof(FTP_URL) - 1) == 0) {
661 			char *passend, *passagain, *userend;
662 
663 			if (ftpproxy) {
664 				if (url_get(line, ftpproxy, outfile) == -1)
665 					rval = argpos + 1;
666 				continue;
667 			}
668 			host += sizeof(FTP_URL) - 1;
669 			dir = strchr(host, '/');
670 
671 			/* Look for [user:pass@]host[:port] */
672 
673 			/* check if we have "user:pass@" */
674 			userend = strchr(host, ':');
675 			passend = strchr(host, '@');
676 			if (passend && userend && userend < passend &&
677 			    (!dir || passend < dir)) {
678 				user = host;
679 				pass = userend + 1;
680 				host = passend + 1;
681 				*userend = *passend = '\0';
682 				passagain = strchr(host, '@');
683 				if (strchr(pass, '@') != NULL ||
684 				    (passagain != NULL && passagain < dir)) {
685 					warnx(at_encoding_warning);
686 					goto bad_ftp_url;
687 				}
688 
689 				if (EMPTYSTRING(user) || EMPTYSTRING(pass)) {
690 bad_ftp_url:
691 					warnx("Invalid URL: %s", argv[argpos]);
692 					rval = argpos + 1;
693 					continue;
694 				}
695 				user = urldecode(user);
696 				pass = urldecode(pass);
697 			}
698 
699 #ifdef INET6
700 			/* check [host]:port, or [host] */
701 			if (host[0] == '[') {
702 				cp = strchr(host, ']');
703 				if (cp && (!dir || cp < dir)) {
704 					if (cp + 1 == dir || cp[1] == ':') {
705 						host++;
706 						*cp++ = '\0';
707 					} else
708 						cp = NULL;
709 				} else
710 					cp = host;
711 			} else
712 				cp = host;
713 #else
714 			cp = host;
715 #endif
716 
717 			/* split off host[:port] if there is */
718 			if (cp) {
719 				portnum = strchr(cp, ':');
720 				if (!portnum)
721 					;
722 				else {
723 					if (!dir)
724 						;
725 					else if (portnum + 1 < dir) {
726 						*portnum++ = '\0';
727 						/*
728 						 * XXX should check if portnum
729 						 * is decimal number
730 						 */
731 					} else {
732 						/* empty portnum */
733 						goto bad_ftp_url;
734 					}
735 				}
736 			} else
737 				portnum = NULL;
738 		} else {			/* classic style `host:file' */
739 			dir = strchr(host, ':');
740 		}
741 		if (EMPTYSTRING(host)) {
742 			rval = argpos + 1;
743 			continue;
744 		}
745 
746 		/*
747 		 * If dir is NULL, the file wasn't specified
748 		 * (URL looked something like ftp://host)
749 		 */
750 		if (dir != NULL)
751 			*dir++ = '\0';
752 
753 		/*
754 		 * Extract the file and (if present) directory name.
755 		 */
756 		if (!EMPTYSTRING(dir)) {
757 			cp = strrchr(dir, '/');
758 			if (cp != NULL) {
759 				*cp++ = '\0';
760 				file = cp;
761 			} else {
762 				file = dir;
763 				dir = NULL;
764 			}
765 		}
766 		if (debug)
767 			fprintf(ttyout,
768 			    "user %s:%s host %s port %s dir %s file %s\n",
769 			    user, pass, host, portnum, dir, file);
770 
771 		/*
772 		 * Set up the connection.
773 		 */
774 		if (connected)
775 			disconnect(0, NULL);
776 		xargv[0] = __progname;
777 		xargv[1] = host;
778 		xargv[2] = NULL;
779 		xargc = 2;
780 		if (!EMPTYSTRING(portnum)) {
781 			xargv[2] = portnum;
782 			xargv[3] = NULL;
783 			xargc = 3;
784 		}
785 		oautologin = autologin;
786 		if (user != NULL)
787 			autologin = 0;
788 		setpeer(xargc, xargv);
789 		autologin = oautologin;
790 		if ((connected == 0) ||
791 		    ((connected == 1) && !ftp_login(host, user, pass))) {
792 			warnx("Can't connect or login to host `%s'", host);
793 			rval = argpos + 1;
794 			continue;
795 		}
796 
797 		/* Always use binary transfers. */
798 		setbinary(0, NULL);
799 
800 		dirhasglob = filehasglob = 0;
801 		if (doglob) {
802 			if (!EMPTYSTRING(dir) &&
803 			    strpbrk(dir, "*?[]{}") != NULL)
804 				dirhasglob = 1;
805 			if (!EMPTYSTRING(file) &&
806 			    strpbrk(file, "*?[]{}") != NULL)
807 				filehasglob = 1;
808 		}
809 
810 		/* Change directories, if necessary. */
811 		if (!EMPTYSTRING(dir) && !dirhasglob) {
812 			xargv[0] = "cd";
813 			xargv[1] = dir;
814 			xargv[2] = NULL;
815 			cd(2, xargv);
816 			if (!dirchange) {
817 				rval = argpos + 1;
818 				continue;
819 			}
820 		}
821 
822 		if (EMPTYSTRING(file)) {
823 			rval = -1;
824 			continue;
825 		}
826 
827 		if (verbose)
828 			fprintf(ttyout, "Retrieving %s/%s\n", dir ? dir : "", file);
829 
830 		if (dirhasglob) {
831 			snprintf(rempath, sizeof(rempath), "%s/%s", dir, file);
832 			file = rempath;
833 		}
834 
835 		/* Fetch the file(s). */
836 		xargc = 2;
837 		xargv[0] = "get";
838 		xargv[1] = file;
839 		xargv[2] = NULL;
840 		if (dirhasglob || filehasglob) {
841 			int ointeractive;
842 
843 			ointeractive = interactive;
844 			interactive = 0;
845 			xargv[0] = "mget";
846 			mget(xargc, xargv);
847 			interactive = ointeractive;
848 		} else {
849 			if (outfile != NULL) {
850 				xargv[2] = outfile;
851 				xargv[3] = NULL;
852 				xargc++;
853 			}
854 			get(xargc, xargv);
855 		}
856 
857 		if ((code / 100) != COMPLETE)
858 			rval = argpos + 1;
859 	}
860 	if (connected && rval != -1)
861 		disconnect(0, NULL);
862 	return (rval);
863 }
864 
865 char *
866 urldecode(const char *str)
867 {
868         char *ret;
869         char c;
870         int i, reallen;
871 
872         if (str == NULL)
873                 return NULL;
874         if ((ret = malloc(strlen(str)+1)) == NULL)
875                 err(1, "Can't allocate memory for URL decoding");
876         for (i = 0, reallen = 0; str[i] != '\0'; i++, reallen++, ret++) {
877                 c = str[i];
878                 if (c == '+') {
879                         *ret = ' ';
880                         continue;
881                 }
882                 /* Can't use strtol here because next char after %xx may be
883                  * a digit. */
884                 if (c == '%' && isxdigit(str[i+1]) && isxdigit(str[i+2])) {
885                         *ret = hextochar(&str[i+1]);
886                         i+=2;
887                         continue;
888                 }
889                 *ret = c;
890         }
891         *ret = '\0';
892 
893         return ret-reallen;
894 }
895 
896 char
897 hextochar(const char *str)
898 {
899         char c, ret;
900 
901         c = str[0];
902         ret = c;
903         if (isalpha(c))
904                 ret -= isupper(c) ? 'A' - 10 : 'a' - 10;
905         else
906                 ret -= '0';
907         ret *= 16;
908 
909         c = str[1];
910         ret += c;
911         if (isalpha(c))
912                 ret -= isupper(c) ? 'A' - 10 : 'a' - 10;
913         else
914                 ret -= '0';
915         return ret;
916 }
917 
918 int
919 isurl(const char *p)
920 {
921 
922 	if (strncasecmp(p, FTP_URL, sizeof(FTP_URL) - 1) == 0 ||
923 	    strncasecmp(p, HTTP_URL, sizeof(HTTP_URL) - 1) == 0 ||
924 	    strncasecmp(p, FILE_URL, sizeof(FILE_URL) - 1) == 0 ||
925 	    strstr(p, ":/"))
926 		return (1);
927 	return (0);
928 }
929