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