xref: /openbsd-src/usr.bin/ftp/fetch.c (revision 47911bd667ac77dc523b8a13ef40b012dbffa741)
1 /*	$OpenBSD: fetch.c,v 1.40 2002/11/08 03:30:17 fgsch 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.40 2002/11/08 03:30:17 fgsch 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 #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(origline, proxyenv, outfile)
100 	const char *origline;
101 	const char *proxyenv;
102 	const char *outfile;
103 {
104 	struct addrinfo hints, *res0, *res;
105 	int error;
106 	int i, isftpurl, isfileurl, isredirect;
107 	volatile int s, out;
108 	size_t len;
109 	char *cp, *ep, *portnum, *path;
110 	char pbuf[NI_MAXSERV];
111 	const char * volatile savefile;
112 	char *line, *host, *port, *buf;
113 	char * volatile proxy;
114 	char *hosttail;
115 	volatile sig_t oldintr;
116 	off_t hashbytes;
117 	char *cause = "unknown";
118 	FILE *fin;
119 	int rval;
120 
121 	s = -1;
122 	proxy = NULL;
123 	fin = NULL;
124 	buf = NULL;
125 	isftpurl = 0;
126 	isfileurl = 0;
127 	isredirect = 0;
128 	rval = -1;
129 
130 	line = strdup(origline);
131 	if (line == NULL)
132 		errx(1, "Can't allocate memory to parse URL");
133 	if (strncasecmp(line, HTTP_URL, sizeof(HTTP_URL) - 1) == 0)
134 		host = line + sizeof(HTTP_URL) - 1;
135 	else if (strncasecmp(line, FTP_URL, sizeof(FTP_URL) - 1) == 0) {
136 		host = line + sizeof(FTP_URL) - 1;
137 		isftpurl = 1;
138 	} else if (strncasecmp(line, FILE_URL, sizeof(FILE_URL) - 1) == 0) {
139 		host = line + sizeof(FILE_URL) - 1;
140 		isfileurl = 1;
141 	} else
142 		errx(1, "url_get: Invalid URL '%s'", line);
143 
144 	if (isfileurl) {
145 		path = host;
146 	} else {
147 		path = strchr(host, '/');		/* find path */
148 		if (EMPTYSTRING(path)) {
149 			if (isftpurl)
150 				goto noftpautologin;
151 			warnx("Invalid URL (no `/' after host): %s", origline);
152 			goto cleanup_url_get;
153 		}
154 		*path++ = '\0';
155 		if (EMPTYSTRING(path)) {
156 			if (isftpurl)
157 				goto noftpautologin;
158 			warnx("Invalid URL (no file after host): %s", origline);
159 			goto cleanup_url_get;
160 		}
161 	}
162 
163 	if (outfile)
164 		savefile = outfile;
165 	else
166 		savefile = basename(path);
167 
168 	if (EMPTYSTRING(savefile)) {
169 		if (isftpurl)
170 			goto noftpautologin;
171 		warnx("Invalid URL (no file after directory): %s", origline);
172 		goto cleanup_url_get;
173 	}
174 
175 	if (proxyenv != NULL) {				/* use proxy */
176 		proxy = strdup(proxyenv);
177 		if (proxy == NULL)
178 			errx(1, "Can't allocate memory for proxy URL.");
179 		if (strncasecmp(proxy, HTTP_URL, sizeof(HTTP_URL) - 1) == 0)
180 			host = proxy + sizeof(HTTP_URL) - 1;
181 		else if (strncasecmp(proxy, FTP_URL, sizeof(FTP_URL) - 1) == 0)
182 			host = proxy + sizeof(FTP_URL) - 1;
183 		else {
184 			warnx("Malformed proxy URL: %s", proxyenv);
185 			goto cleanup_url_get;
186 		}
187 		if (EMPTYSTRING(host)) {
188 			warnx("Malformed proxy URL: %s", proxyenv);
189 			goto cleanup_url_get;
190 		}
191 		*--path = '/';			/* add / back to real path */
192 		path = strchr(host, '/');	/* remove trailing / on host */
193 		if (! EMPTYSTRING(path))
194 			*path++ = '\0';
195 		path = line;
196 	}
197 
198 	if (isfileurl) {
199 		struct stat st;
200 
201 		s = open(path, O_RDONLY);
202 		if (s == -1) {
203 			warn("Can't open file %s", path);
204 			goto cleanup_url_get;
205 		}
206 
207 		if (fstat(s, &st) == -1)
208 			filesize = -1;
209 		else
210 			filesize = st.st_size;
211 
212 		/* Open the output file.  */
213 		if (strcmp(savefile, "-") != 0) {
214 			out = open(savefile, O_CREAT | O_WRONLY | O_TRUNC, 0666);
215 			if (out < 0) {
216 				warn("Can't open %s", savefile);
217 				goto cleanup_url_get;
218 			}
219 		} else
220 			out = fileno(stdout);
221 
222 		/* Trap signals */
223 		oldintr = NULL;
224 		if (setjmp(httpabort)) {
225 			if (oldintr)
226 				(void)signal(SIGINT, oldintr);
227 			goto cleanup_url_get;
228 		}
229 		oldintr = signal(SIGINT, abortfile);
230 
231 		bytes = 0;
232 		hashbytes = mark;
233 		progressmeter(-1);
234 
235 		if ((buf = malloc(4096)) == NULL)
236 			errx(1, "Can't allocate memory for transfer buffer\n");
237 
238 		/* Finally, suck down the file. */
239 		i = 0;
240 		while ((len = read(s, buf, 4096)) > 0) {
241 			bytes += len;
242 			for (cp = buf; len > 0; len -= i, cp += i) {
243 				if ((i = write(out, cp, len)) == -1) {
244 					warn("Writing %s", savefile);
245 					goto cleanup_url_get;
246 				}
247 				else if (i == 0)
248 					break;
249 			}
250 			if (hash && !progress) {
251 				while (bytes >= hashbytes) {
252 					(void)putc('#', ttyout);
253 					hashbytes += mark;
254 				}
255 				(void)fflush(ttyout);
256 			}
257 		}
258 		if (hash && !progress && bytes > 0) {
259 			if (bytes < mark)
260 				(void)putc('#', ttyout);
261 			(void)putc('\n', ttyout);
262 			(void)fflush(ttyout);
263 		}
264 		if (len != 0) {
265 			warn("Reading from file");
266 			goto cleanup_url_get;
267 		}
268 		progressmeter(1);
269 		if (verbose)
270 			fputs("Successfully retrieved file.\n", ttyout);
271 		(void)signal(SIGINT, oldintr);
272 
273 		rval = 0;
274 		goto cleanup_url_get;
275 	}
276 
277 	if (*host == '[' && (hosttail = strrchr(host, ']')) != NULL &&
278 	    (hosttail[1] == '\0' || hosttail[1] == ':')) {
279 		host++;
280 		*hosttail++ = '\0';
281 	} else
282 		hosttail = host;
283 
284 	portnum = strrchr(hosttail, ':');		/* find portnum */
285 	if (portnum != NULL)
286 		*portnum++ = '\0';
287 
288 	if (debug)
289 		fprintf(ttyout, "host %s, port %s, path %s, save as %s.\n",
290 		    host, portnum, path, savefile);
291 
292 	memset(&hints, 0, sizeof(hints));
293 	hints.ai_family = family;
294 	hints.ai_socktype = SOCK_STREAM;
295 	port = portnum ? portnum : httpport;
296 	error = getaddrinfo(host, port, &hints, &res0);
297 	if (error == EAI_SERVICE && port == httpport) {
298 		/*
299 		 * If the services file is corrupt/missing, fall back
300 		 * on our hard-coded defines.
301 		 */
302 		snprintf(pbuf, sizeof(pbuf), "%d", HTTP_PORT);
303 		error = getaddrinfo(host, pbuf, &hints, &res0);
304 	}
305 	if (error) {
306 		warnx("%s: %s", gai_strerror(error), host);
307 		goto cleanup_url_get;
308 	}
309 
310 	s = -1;
311 	for (res = res0; res; res = res->ai_next) {
312 		getnameinfo(res->ai_addr, res->ai_addrlen, pbuf, sizeof(pbuf),
313 			NULL, 0, NI_NUMERICHOST);
314 		fprintf(ttyout, "Trying %s...\n", pbuf);
315 
316 		s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
317 		if (s == -1) {
318 			cause = "socket";
319 			continue;
320 		}
321 
322 again:
323 		if (connect(s, res->ai_addr, res->ai_addrlen) < 0) {
324 			if (errno == EINTR)
325 				goto again;
326 			close(s);
327 			s = -1;
328 			cause = "connect";
329 			continue;
330 		}
331 
332 		/* get port in numeric */
333 		if (getnameinfo(res->ai_addr, res->ai_addrlen, NULL, 0,
334 		    pbuf, sizeof(pbuf), NI_NUMERICSERV) == 0)
335 			port = pbuf;
336 		else
337 			port = NULL;
338 
339 		break;
340 	}
341 	freeaddrinfo(res0);
342 	if (s < 0) {
343 		warn("%s", cause);
344 		goto cleanup_url_get;
345 	}
346 
347 	fin = fdopen(s, "r+");
348 
349 	/*
350 	 * Construct and send the request. Proxy requests don't want leading /.
351 	 */
352 	if (proxy) {
353 		/*
354 		 * Host: directive must use the destination host address for
355 		 * the original URI (path).  We do not attach it at this moment.
356 		 */
357 		if (verbose)
358 			fprintf(ttyout, "Requesting %s (via %s)\n",
359 			    origline, proxyenv);
360 		fprintf(fin, "GET %s HTTP/1.0\r\n%s\r\n\r\n", path, HTTP_USER_AGENT);
361 	} else {
362 		if (verbose)
363 			fprintf(ttyout, "Requesting %s\n", origline);
364 		if (strchr(host, ':')) {
365 			char *h, *p;
366 
367 			/* strip off scoped address portion, since it's local to node */
368 			h = strdup(host);
369 			if (h == NULL)
370 				errx(1, "Can't allocate memory.");
371 			if ((p = strchr(h, '%')) != NULL)
372 				*p = '\0';
373 			/*
374 			 * Send port number only if it's specified and does not equal
375 			 * 80. Some broken HTTP servers get confused if you explicitly
376 			 * send them the port number.
377 			 */
378 			if (port && strcmp(port, "80") != 0)
379 				fprintf(fin, "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, "GET /%s HTTP/1.0\r\nHost: [%s]\r\n%s\r\n\r\n",
383 				    path, h, HTTP_USER_AGENT);
384 			free(h);
385 		} else {
386 			if (port && strcmp(port, "80") != 0)
387 				fprintf(fin, "GET /%s HTTP/1.0\r\nHost: %s:%s\r\n%s\r\n\r\n",
388 				    path, host, port, HTTP_USER_AGENT);
389 			else
390 				fprintf(fin, "GET /%s HTTP/1.0\r\nHost: %s\r\n%s\r\n\r\n",
391 				    path, host, HTTP_USER_AGENT);
392 		}
393 	}
394 	if (fflush(fin) == EOF) {
395 		warn("Writing HTTP request");
396 		goto cleanup_url_get;
397 	}
398 
399 	if ((buf = fparseln(fin, &len, NULL, "\0\0\0", 0)) == NULL) {
400 		warn("Receiving HTTP reply");
401 		goto cleanup_url_get;
402 	}
403 
404 	while (len > 0 && (buf[len-1] == '\r' || buf[len-1] == '\n'))
405 		buf[--len] = '\0';
406 	if (debug)
407 		fprintf(ttyout, "received '%s'\n", buf);
408 
409 	cp = strchr(buf, ' ');
410 	if (cp == NULL)
411 		goto improper;
412 	else
413 		cp++;
414 	if (strncmp(cp, "301", 3) == 0 || strncmp(cp, "302", 3) == 0) {
415 		isredirect++;
416 	} else if (strncmp(cp, "200", 3)) {
417 		warnx("Error retrieving file: %s", cp);
418 		goto cleanup_url_get;
419 	}
420 
421 	/*
422 	 * Read the rest of the header.
423 	 */
424 	free(buf);
425 	filesize = -1;
426 
427 	while (1) {
428 		if ((buf = fparseln(fin, &len, NULL, "\0\0\0", 0)) == NULL) {
429 			warn("Receiving HTTP reply");
430 			goto cleanup_url_get;
431 		}
432 		while (len > 0 && (buf[len-1] == '\r' || buf[len-1] == '\n'))
433 			buf[--len] = '\0';
434 		if (len == 0)
435 			break;
436 		if (debug)
437 			fprintf(ttyout, "received '%s'\n", buf);
438 
439 		/* Look for some headers */
440 		cp = buf;
441 #define CONTENTLEN "Content-Length: "
442 		if (strncasecmp(cp, CONTENTLEN, sizeof(CONTENTLEN) - 1) == 0) {
443 			cp += sizeof(CONTENTLEN) - 1;
444 			filesize = strtol(cp, &ep, 10);
445 			if (filesize < 1 || *ep != '\0')
446 				goto improper;
447 #define LOCATION "Location: "
448 		} else if (isredirect &&
449 		    strncasecmp(cp, LOCATION, sizeof(LOCATION) - 1) == 0) {
450 			cp += sizeof(LOCATION) - 1;
451 			if (verbose)
452 				fprintf(ttyout, "Redirected to %s\n", cp);
453 			if (fin != NULL)
454 				fclose(fin);
455 			else if (s != -1)
456 				close(s);
457 			if (proxy)
458 				free(proxy);
459 			free(line);
460 			rval = url_get(cp, proxyenv, outfile);
461 			if (buf)
462 				free(buf);
463 			return (rval);
464 		}
465 	}
466 	free(buf);
467 
468 	/* Open the output file.  */
469 	if (strcmp(savefile, "-") != 0) {
470 		out = open(savefile, O_CREAT | O_WRONLY | O_TRUNC, 0666);
471 		if (out < 0) {
472 			warn("Can't open %s", savefile);
473 			goto cleanup_url_get;
474 		}
475 	} else
476 		out = fileno(stdout);
477 
478 	/* Trap signals */
479 	oldintr = NULL;
480 	if (setjmp(httpabort)) {
481 		if (oldintr)
482 			(void)signal(SIGINT, oldintr);
483 		goto cleanup_url_get;
484 	}
485 	oldintr = signal(SIGINT, aborthttp);
486 
487 	bytes = 0;
488 	hashbytes = mark;
489 	progressmeter(-1);
490 
491 	/* Finally, suck down the file. */
492 	if ((buf = malloc(4096)) == NULL)
493 		errx(1, "Can't allocate memory for transfer buffer\n");
494 	i = 0;
495 	while ((len = fread(buf, sizeof(char), 4096, fin)) > 0) {
496 		bytes += len;
497 		for (cp = buf; len > 0; len -= i, cp += i) {
498 			if ((i = write(out, cp, len)) == -1) {
499 				warn("Writing %s", savefile);
500 				goto cleanup_url_get;
501 			}
502 			else if (i == 0)
503 				break;
504 		}
505 		if (hash && !progress) {
506 			while (bytes >= hashbytes) {
507 				(void)putc('#', ttyout);
508 				hashbytes += mark;
509 			}
510 			(void)fflush(ttyout);
511 		}
512 	}
513 	if (hash && !progress && bytes > 0) {
514 		if (bytes < mark)
515 			(void)putc('#', ttyout);
516 		(void)putc('\n', ttyout);
517 		(void)fflush(ttyout);
518 	}
519 	if (len != 0) {
520 		warn("Reading from socket");
521 		goto cleanup_url_get;
522 	}
523 	progressmeter(1);
524 	if (filesize != -1 && len == 0 && bytes != filesize) {
525 		if (verbose)
526 			fputs("Read short file.\n", ttyout);
527 		goto cleanup_url_get;
528 	}
529 
530 	if (verbose)
531 		fputs("Successfully retrieved file.\n", ttyout);
532 	(void)signal(SIGINT, oldintr);
533 
534 	rval = 0;
535 	goto cleanup_url_get;
536 
537 noftpautologin:
538 	warnx(
539 	    "Auto-login using ftp URLs isn't supported when using $ftp_proxy");
540 	goto cleanup_url_get;
541 
542 improper:
543 	warnx("Improper response from %s", host);
544 
545 cleanup_url_get:
546 	if (fin != NULL)
547 		fclose(fin);
548 	else if (s != -1)
549 		close(s);
550 	if (buf)
551 		free(buf);
552 	if (proxy)
553 		free(proxy);
554 	free(line);
555 	return (rval);
556 }
557 
558 /*
559  * Abort a http retrieval
560  */
561 void
562 aborthttp(notused)
563 	int notused;
564 {
565 
566 	alarmtimer(0);
567 	fputs("\nhttp fetch aborted.\n", ttyout);
568 	(void)fflush(ttyout);
569 	longjmp(httpabort, 1);
570 }
571 
572 /*
573  * Abort a http retrieval
574  */
575 void
576 abortfile(notused)
577 	int notused;
578 {
579 
580 	alarmtimer(0);
581 	fputs("\nfile fetch aborted.\n", ttyout);
582 	(void)fflush(ttyout);
583 	longjmp(httpabort, 1);
584 }
585 
586 /*
587  * Retrieve multiple files from the command line, transferring
588  * files of the form "host:path", "ftp://host/path" using the
589  * ftp protocol, and files of the form "http://host/path" using
590  * the http protocol.
591  * If path has a trailing "/", then return (-1);
592  * the path will be cd-ed into and the connection remains open,
593  * and the function will return -1 (to indicate the connection
594  * is alive).
595  * If an error occurs the return value will be the offset+1 in
596  * argv[] of the file that caused a problem (i.e, argv[x]
597  * returns x+1)
598  * Otherwise, 0 is returned if all files retrieved successfully.
599  */
600 int
601 auto_fetch(argc, argv, outfile)
602 	int argc;
603 	char *argv[];
604 	char *outfile;
605 {
606 	static char lasthost[MAXHOSTNAMELEN];
607 	char *xargv[5];
608 	char *cp, *line, *host, *dir, *file, *portnum;
609 	char *user, *pass;
610 	char *ftpproxy, *httpproxy;
611 	int rval, xargc;
612 	volatile int argpos;
613 	int dirhasglob, filehasglob;
614 	char rempath[MAXPATHLEN];
615 
616 	argpos = 0;
617 
618 	if (setjmp(toplevel)) {
619 		if (connected)
620 			disconnect(0, NULL);
621 		return (argpos + 1);
622 	}
623 	(void)signal(SIGINT, (sig_t)intr);
624 	(void)signal(SIGPIPE, (sig_t)lostpeer);
625 
626 	ftpproxy = getenv(FTP_PROXY);
627 	httpproxy = getenv(HTTP_PROXY);
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, "user %s:%s host %s port %s dir %s file %s\n",
768 			    user, pass, host, portnum, dir, file);
769 
770 		/*
771 		 * Set up the connection if we don't have one.
772 		 */
773 		if (strcmp(host, lasthost) != 0) {
774 			int oautologin;
775 
776 			(void)strcpy(lasthost, host);
777 			if (connected)
778 				disconnect(0, NULL);
779 			xargv[0] = __progname;
780 			xargv[1] = host;
781 			xargv[2] = NULL;
782 			xargc = 2;
783 			if (! EMPTYSTRING(portnum)) {
784 				xargv[2] = portnum;
785 				xargv[3] = NULL;
786 				xargc = 3;
787 			}
788 			oautologin = autologin;
789 			if (user != NULL)
790 				autologin = 0;
791 			setpeer(xargc, xargv);
792 			autologin = oautologin;
793 			if ((connected == 0) ||
794 			    ((connected == 1) && !ftp_login(host, user, pass))) {
795 				warnx("Can't connect or login to host `%s'",
796 				    host);
797 				rval = argpos + 1;
798 				continue;
799 			}
800 
801 			/* Always use binary transfers. */
802 			setbinary(0, NULL);
803 		}
804 		/* cd back to '/' */
805 		xargv[0] = "cd";
806 		xargv[1] = "/";
807 		xargv[2] = NULL;
808 		cd(2, xargv);
809 		if (! dirchange) {
810 			rval = argpos + 1;
811 			continue;
812 		}
813 
814 		dirhasglob = filehasglob = 0;
815 		if (doglob) {
816 			if (! EMPTYSTRING(dir) &&
817 			    strpbrk(dir, "*?[]{}") != NULL)
818 				dirhasglob = 1;
819 			if (! EMPTYSTRING(file) &&
820 			    strpbrk(file, "*?[]{}") != NULL)
821 				filehasglob = 1;
822 		}
823 
824 		/* Change directories, if necessary. */
825 		if (! EMPTYSTRING(dir) && !dirhasglob) {
826 			xargv[0] = "cd";
827 			xargv[1] = dir;
828 			xargv[2] = NULL;
829 			cd(2, xargv);
830 			if (! dirchange) {
831 				rval = argpos + 1;
832 				continue;
833 			}
834 		}
835 
836 		if (EMPTYSTRING(file)) {
837 			rval = -1;
838 			continue;
839 		}
840 
841 		if (verbose)
842 			fprintf(ttyout, "Retrieving %s/%s\n", dir ? dir : "", file);
843 
844 		if (dirhasglob) {
845 			snprintf(rempath, sizeof(rempath), "%s/%s", dir, file);
846 			file = rempath;
847 		}
848 
849 		/* Fetch the file(s). */
850 		xargc = 2;
851 		xargv[0] = "get";
852 		xargv[1] = file;
853 		xargv[2] = NULL;
854 		if (dirhasglob || filehasglob) {
855 			int ointeractive;
856 
857 			ointeractive = interactive;
858 			interactive = 0;
859 			xargv[0] = "mget";
860 			mget(xargc, xargv);
861 			interactive = ointeractive;
862 		} else {
863 			if (outfile != NULL) {
864 				xargv[2] = outfile;
865 				xargv[3] = NULL;
866 				xargc++;
867 			}
868 			get(xargc, xargv);
869 		}
870 
871 		if ((code / 100) != COMPLETE)
872 			rval = argpos + 1;
873 	}
874 	if (connected && rval != -1)
875 		disconnect(0, NULL);
876 	return (rval);
877 }
878 
879 char *
880 urldecode(str)
881         const char *str;
882 {
883         char *ret;
884         char c;
885         int i, reallen;
886 
887         if (str == NULL)
888                 return NULL;
889         if ((ret = malloc(strlen(str)+1)) == NULL)
890                 err(1, "Can't allocate memory for URL decoding");
891         for (i = 0, reallen = 0; str[i] != '\0'; i++, reallen++, ret++) {
892                 c = str[i];
893                 if (c == '+') {
894                         *ret = ' ';
895                         continue;
896                 }
897                 /* Can't use strtol here because next char after %xx may be
898                  * a digit. */
899                 if (c == '%' && isxdigit(str[i+1]) && isxdigit(str[i+2])) {
900                         *ret = hextochar(&str[i+1]);
901                         i+=2;
902                         continue;
903                 }
904                 *ret = c;
905         }
906         *ret = '\0';
907 
908         return ret-reallen;
909 }
910 
911 char
912 hextochar(str)
913         const char *str;
914 {
915         char c, ret;
916 
917         c = str[0];
918         ret = c;
919         if (isalpha(c))
920                 ret -= isupper(c) ? 'A' - 10 : 'a' - 10;
921         else
922                 ret -= '0';
923         ret *= 16;
924 
925         c = str[1];
926         ret += c;
927         if (isalpha(c))
928                 ret -= isupper(c) ? 'A' - 10 : 'a' - 10;
929         else
930                 ret -= '0';
931         return ret;
932 }
933 
934 int
935 isurl(p)
936 	const char *p;
937 {
938 
939 	if (strncasecmp(p, FTP_URL, sizeof(FTP_URL) - 1) == 0 ||
940 	    strncasecmp(p, HTTP_URL, sizeof(HTTP_URL) - 1) == 0 ||
941 	    strncasecmp(p, FILE_URL, sizeof(FILE_URL) - 1) == 0 ||
942 	    strstr(p, ":/"))
943 		return (1);
944 	return (0);
945 }
946