Annotation of src/usr.bin/ftp/fetch.c, Revision 1.23
1.23 ! deraadt 1: /* $OpenBSD: fetch.c,v 1.22 1998/09/28 13:38:22 deraadt Exp $ */
1.15 millert 2: /* $NetBSD: fetch.c,v 1.14 1997/08/18 10:20:20 lukem Exp $ */
1.1 millert 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
1.15 millert 30: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
1.1 millert 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
1.23 ! deraadt 41: static char rcsid[] = "$OpenBSD: fetch.c,v 1.22 1998/09/28 13:38:22 deraadt Exp $";
1.1 millert 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>
1.22 deraadt 51: #include <sys/stat.h>
1.1 millert 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>
1.17 millert 60: #include <libgen.h>
1.1 millert 61: #include <netdb.h>
62: #include <fcntl.h>
1.3 millert 63: #include <signal.h>
1.1 millert 64: #include <stdio.h>
1.19 deraadt 65: #include <errno.h>
1.1 millert 66: #include <stdlib.h>
67: #include <string.h>
68: #include <unistd.h>
69:
70: #include "ftp_var.h"
71:
1.17 millert 72: static int url_get __P((const char *, const char *, const char *));
1.14 millert 73: void aborthttp __P((int));
1.22 deraadt 74: void abortfile __P((int));
1.14 millert 75:
76:
1.1 millert 77: #define FTP_URL "ftp://" /* ftp URL prefix */
78: #define HTTP_URL "http://" /* http URL prefix */
1.22 deraadt 79: #define FILE_URL "file:" /* file URL prefix */
1.6 millert 80: #define FTP_PROXY "ftp_proxy" /* env var with ftp proxy location */
1.1 millert 81: #define HTTP_PROXY "http_proxy" /* env var with http proxy location */
82:
83:
84: #define EMPTYSTRING(x) ((x) == NULL || (*(x) == '\0'))
85:
86: jmp_buf httpabort;
87:
88: /*
1.6 millert 89: * Retrieve URL, via the proxy in $proxyvar if necessary.
1.1 millert 90: * Modifies the string argument given.
91: * Returns -1 on failure, 0 on success
92: */
1.14 millert 93: static int
1.17 millert 94: url_get(origline, proxyenv, outfile)
1.14 millert 95: const char *origline;
96: const char *proxyenv;
1.17 millert 97: const char *outfile;
1.1 millert 98: {
99: struct sockaddr_in sin;
1.22 deraadt 100: int i, out, isftpurl, isfileurl;
1.14 millert 101: in_port_t port;
102: volatile int s;
103: size_t len;
104: char c, *cp, *ep, *portnum, *path, buf[4096];
105: const char *savefile;
106: char *line, *proxy, *host;
107: volatile sig_t oldintr;
1.1 millert 108: off_t hashbytes;
1.19 deraadt 109: struct hostent *hp = NULL;
1.1 millert 110:
111: s = -1;
112: proxy = NULL;
1.14 millert 113: isftpurl = 0;
1.22 deraadt 114: isfileurl = 0;
1.1 millert 115:
1.14 millert 116: #ifdef __GNUC__ /* XXX: to shut up gcc warnings */
117: (void)&out;
118: (void)&proxy;
119: (void)&savefile;
120: #endif
121:
122: line = strdup(origline);
123: if (line == NULL)
124: errx(1, "Can't allocate memory to parse URL");
1.7 millert 125: if (strncasecmp(line, HTTP_URL, sizeof(HTTP_URL) - 1) == 0)
1.6 millert 126: host = line + sizeof(HTTP_URL) - 1;
1.14 millert 127: else if (strncasecmp(line, FTP_URL, sizeof(FTP_URL) - 1) == 0) {
1.6 millert 128: host = line + sizeof(FTP_URL) - 1;
1.14 millert 129: isftpurl = 1;
1.22 deraadt 130: } else if (strncasecmp(line, FILE_URL, sizeof(FILE_URL) - 1) == 0) {
131: host = line + sizeof(FILE_URL) - 1;
132: isfileurl = 1;
1.14 millert 133: } else
134: errx(1, "url_get: Invalid URL '%s'", line);
1.6 millert 135:
1.22 deraadt 136: if (isfileurl) {
137: path = host;
138: } else {
139: path = strchr(host, '/'); /* find path */
140: if (EMPTYSTRING(path)) {
141: if (isftpurl)
142: goto noftpautologin;
143: warnx("Invalid URL (no `/' after host): %s", origline);
144: goto cleanup_url_get;
145: }
146: *path++ = '\0';
147: if (EMPTYSTRING(path)) {
148: if (isftpurl)
149: goto noftpautologin;
150: warnx("Invalid URL (no file after host): %s", origline);
151: goto cleanup_url_get;
152: }
1.14 millert 153: }
1.1 millert 154:
1.17 millert 155: if (outfile)
156: savefile = outfile;
1.1 millert 157: else
1.17 millert 158: savefile = basename(path);
159:
1.14 millert 160: if (EMPTYSTRING(savefile)) {
161: if (isftpurl)
162: goto noftpautologin;
163: warnx("Invalid URL (no file after directory): %s", origline);
1.6 millert 164: goto cleanup_url_get;
1.14 millert 165: }
1.1 millert 166:
167: if (proxyenv != NULL) { /* use proxy */
168: proxy = strdup(proxyenv);
169: if (proxy == NULL)
1.14 millert 170: errx(1, "Can't allocate memory for proxy URL.");
1.7 millert 171: if (strncasecmp(proxy, HTTP_URL, sizeof(HTTP_URL) - 1) == 0)
1.6 millert 172: host = proxy + sizeof(HTTP_URL) - 1;
1.7 millert 173: else if (strncasecmp(proxy, FTP_URL, sizeof(FTP_URL) - 1) == 0)
1.6 millert 174: host = proxy + sizeof(FTP_URL) - 1;
175: else {
1.14 millert 176: warnx("Malformed proxy URL: %s", proxyenv);
1.6 millert 177: goto cleanup_url_get;
178: }
1.14 millert 179: if (EMPTYSTRING(host)) {
180: warnx("Malformed proxy URL: %s", proxyenv);
1.6 millert 181: goto cleanup_url_get;
1.14 millert 182: }
1.1 millert 183: *--path = '/'; /* add / back to real path */
184: path = strchr(host, '/'); /* remove trailing / on host */
185: if (! EMPTYSTRING(path))
186: *path++ = '\0';
187: path = line;
188: }
189:
1.22 deraadt 190: if (isfileurl) {
191: struct stat st;
192:
193: s = open(path, O_RDONLY);
194: if (s == -1) {
195: warn("Can't open file %s", path);
196: goto cleanup_url_get;
197: }
198:
199: if (fstat(s, &st) == -1)
200: filesize = -1;
201: else
202: filesize = st.st_size;
203:
204: /* Open the output file. */
205: if (strcmp(savefile, "-") != 0) {
206: out = open(savefile, O_CREAT | O_WRONLY | O_TRUNC, 0666);
207: if (out < 0) {
208: warn("Can't open %s", savefile);
209: goto cleanup_url_get;
210: }
211: } else
212: out = fileno(stdout);
213:
214: /* Trap signals */
215: oldintr = NULL;
216: if (setjmp(httpabort)) {
217: if (oldintr)
218: (void)signal(SIGINT, oldintr);
219: goto cleanup_url_get;
220: }
221: oldintr = signal(SIGINT, abortfile);
222:
223: bytes = 0;
224: hashbytes = mark;
225: progressmeter(-1);
226:
227: /* Finally, suck down the file. */
228: i = 0;
229: while ((len = read(s, buf, sizeof(buf))) > 0) {
230: bytes += len;
231: for (cp = buf; len > 0; len -= i, cp += i) {
232: if ((i = write(out, cp, len)) == -1) {
233: warn("Writing %s", savefile);
234: goto cleanup_url_get;
235: }
236: else if (i == 0)
237: break;
238: }
239: if (hash && !progress) {
240: while (bytes >= hashbytes) {
241: (void)putc('#', ttyout);
242: hashbytes += mark;
243: }
244: (void)fflush(ttyout);
245: }
246: }
247: if (hash && !progress && bytes > 0) {
248: if (bytes < mark)
249: (void)putc('#', ttyout);
250: (void)putc('\n', ttyout);
251: (void)fflush(ttyout);
252: }
253: if (len != 0) {
254: warn("Reading from file");
255: goto cleanup_url_get;
256: }
257: progressmeter(1);
258: if (verbose)
259: fputs("Successfully retrieved file.\n", ttyout);
260: (void)signal(SIGINT, oldintr);
261:
262: close(s);
263: if (out != fileno(stdout))
264: close(out);
265: if (proxy)
266: free(proxy);
267: free(line);
268: return (0);
269: }
270:
1.1 millert 271: portnum = strchr(host, ':'); /* find portnum */
272: if (portnum != NULL)
273: *portnum++ = '\0';
274:
275: if (debug)
1.10 deraadt 276: fprintf(ttyout, "host %s, port %s, path %s, save as %s.\n",
1.1 millert 277: host, portnum, path, savefile);
278:
279: memset(&sin, 0, sizeof(sin));
280: sin.sin_family = AF_INET;
281:
282: if (isdigit(host[0])) {
283: if (inet_aton(host, &sin.sin_addr) == 0) {
1.8 millert 284: warnx("Invalid IP address: %s", host);
1.6 millert 285: goto cleanup_url_get;
1.1 millert 286: }
287: } else {
288: hp = gethostbyname(host);
289: if (hp == NULL) {
290: warnx("%s: %s", host, hstrerror(h_errno));
1.6 millert 291: goto cleanup_url_get;
1.1 millert 292: }
293: if (hp->h_addrtype != AF_INET) {
294: warnx("%s: not an Internet address?", host);
1.6 millert 295: goto cleanup_url_get;
1.1 millert 296: }
1.14 millert 297: memcpy(&sin.sin_addr, hp->h_addr, (size_t)hp->h_length);
1.1 millert 298: }
299:
300: if (! EMPTYSTRING(portnum)) {
1.14 millert 301: char *ep;
302: long nport;
303:
304: nport = strtol(portnum, &ep, 10);
1.15 millert 305: if (nport < 1 || nport > USHRT_MAX || *ep != '\0') {
1.8 millert 306: warnx("Invalid port: %s", portnum);
1.6 millert 307: goto cleanup_url_get;
1.1 millert 308: }
1.14 millert 309: port = htons((in_port_t)nport);
1.1 millert 310: } else
311: port = httpport;
312: sin.sin_port = port;
313:
314: s = socket(AF_INET, SOCK_STREAM, 0);
315: if (s == -1) {
1.14 millert 316: warn("Can't create socket");
1.6 millert 317: goto cleanup_url_get;
1.1 millert 318: }
319:
1.19 deraadt 320: while (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
321: if (errno == EINTR)
322: continue;
323: if (hp && hp->h_addr_list[1]) {
324: int oerrno = errno;
325: char *ia;
326:
327: ia = inet_ntoa(sin.sin_addr);
328: errno = oerrno;
329: warn("connect to address %s", ia);
330: hp->h_addr_list++;
331: memcpy(&sin.sin_addr, hp->h_addr_list[0],
332: (size_t)hp->h_length);
333: fprintf(ttyout, "Trying %s...\n",
334: inet_ntoa(sin.sin_addr));
335: (void)close(s);
336: s = socket(AF_INET, SOCK_STREAM, 0);
337: if (s < 0) {
338: warn("socket");
339: goto cleanup_url_get;
340: }
341: continue;
342: }
343: warn("connect");
1.6 millert 344: goto cleanup_url_get;
1.1 millert 345: }
346:
347: /*
348: * Construct and send the request. We're expecting a return
349: * status of "200". Proxy requests don't want leading /.
350: */
1.23 ! deraadt 351: if (verbose) {
! 352: if (!proxy)
! 353: fprintf(ttyout, "Requesting %s\n", origline);
! 354: else
! 355: fprintf(ttyout, "Requesting %s (via %s)\n",
! 356: origline, proxyenv);
! 357: }
1.18 marc 358: snprintf(buf, sizeof(buf), "GET %s%s HTTP/1.0\r\nHost: %s\r\n\r\n",
359: proxy ? "" : "/", path, host);
1.14 millert 360: len = strlen(buf);
361: if (write(s, buf, len) < len) {
362: warn("Writing HTTP request");
1.6 millert 363: goto cleanup_url_get;
1.1 millert 364: }
365: memset(buf, 0, sizeof(buf));
1.14 millert 366: for (cp = buf; cp < buf + sizeof(buf); ) {
1.1 millert 367: if (read(s, cp, 1) != 1)
368: goto improper;
1.4 millert 369: if (*cp == '\r')
370: continue;
1.1 millert 371: if (*cp == '\n')
372: break;
1.14 millert 373: cp++;
1.1 millert 374: }
1.14 millert 375: buf[sizeof(buf) - 1] = '\0'; /* sanity */
1.1 millert 376: cp = strchr(buf, ' ');
377: if (cp == NULL)
378: goto improper;
379: else
380: cp++;
381: if (strncmp(cp, "200", 3)) {
382: warnx("Error retrieving file: %s", cp);
1.6 millert 383: goto cleanup_url_get;
1.1 millert 384: }
385:
386: /*
387: * Read the rest of the header.
388: */
389: memset(buf, 0, sizeof(buf));
390: c = '\0';
1.14 millert 391: for (cp = buf; cp < buf + sizeof(buf); ) {
1.1 millert 392: if (read(s, cp, 1) != 1)
393: goto improper;
1.4 millert 394: if (*cp == '\r')
395: continue;
1.1 millert 396: if (*cp == '\n' && c == '\n')
397: break;
398: c = *cp;
1.14 millert 399: cp++;
1.1 millert 400: }
1.14 millert 401: buf[sizeof(buf) - 1] = '\0'; /* sanity */
1.1 millert 402:
1.9 millert 403: /* Look for the "Content-length: " header. */
1.1 millert 404: #define CONTENTLEN "Content-Length: "
405: for (cp = buf; *cp != '\0'; cp++) {
406: if (tolower(*cp) == 'c' &&
407: strncasecmp(cp, CONTENTLEN, sizeof(CONTENTLEN) - 1) == 0)
408: break;
409: }
1.13 deraadt 410: if (*cp != '\0') {
411: cp += sizeof(CONTENTLEN) - 1;
1.14 millert 412: ep = strchr(cp, '\n');
413: if (ep == NULL)
1.13 deraadt 414: goto improper;
415: else
1.14 millert 416: *ep = '\0';
417: filesize = strtol(cp, &ep, 10);
418: if (filesize < 1 || *ep != '\0')
1.13 deraadt 419: goto improper;
420: } else
421: filesize = -1;
1.1 millert 422:
1.17 millert 423: /* Open the output file. */
424: if (strcmp(savefile, "-") != 0) {
1.10 deraadt 425: out = open(savefile, O_CREAT | O_WRONLY | O_TRUNC, 0666);
426: if (out < 0) {
427: warn("Can't open %s", savefile);
428: goto cleanup_url_get;
429: }
430: } else
1.17 millert 431: out = fileno(stdout);
1.1 millert 432:
433: /* Trap signals */
434: oldintr = NULL;
435: if (setjmp(httpabort)) {
436: if (oldintr)
1.2 millert 437: (void)signal(SIGINT, oldintr);
1.6 millert 438: goto cleanup_url_get;
1.1 millert 439: }
440: oldintr = signal(SIGINT, aborthttp);
441:
442: bytes = 0;
443: hashbytes = mark;
444: progressmeter(-1);
445:
446: /* Finally, suck down the file. */
447: i = 0;
448: while ((len = read(s, buf, sizeof(buf))) > 0) {
449: bytes += len;
450: for (cp = buf; len > 0; len -= i, cp += i) {
451: if ((i = write(out, cp, len)) == -1) {
452: warn("Writing %s", savefile);
1.6 millert 453: goto cleanup_url_get;
1.1 millert 454: }
455: else if (i == 0)
456: break;
457: }
458: if (hash && !progress) {
459: while (bytes >= hashbytes) {
1.10 deraadt 460: (void)putc('#', ttyout);
1.1 millert 461: hashbytes += mark;
462: }
1.10 deraadt 463: (void)fflush(ttyout);
1.1 millert 464: }
465: }
466: if (hash && !progress && bytes > 0) {
467: if (bytes < mark)
1.10 deraadt 468: (void)putc('#', ttyout);
469: (void)putc('\n', ttyout);
470: (void)fflush(ttyout);
1.1 millert 471: }
472: if (len != 0) {
473: warn("Reading from socket");
1.6 millert 474: goto cleanup_url_get;
1.1 millert 475: }
476: progressmeter(1);
477: if (verbose)
1.10 deraadt 478: fputs("Successfully retrieved file.\n", ttyout);
1.2 millert 479: (void)signal(SIGINT, oldintr);
1.1 millert 480:
481: close(s);
1.17 millert 482: if (out != fileno(stdout))
1.10 deraadt 483: close(out);
1.1 millert 484: if (proxy)
485: free(proxy);
1.14 millert 486: free(line);
1.2 millert 487: return (0);
1.1 millert 488:
1.14 millert 489: noftpautologin:
490: warnx(
491: "Auto-login using ftp URLs isn't supported when using $ftp_proxy");
492: goto cleanup_url_get;
493:
1.1 millert 494: improper:
1.8 millert 495: warnx("Improper response from %s", host);
1.14 millert 496:
1.6 millert 497: cleanup_url_get:
1.1 millert 498: if (s != -1)
499: close(s);
500: if (proxy)
501: free(proxy);
1.14 millert 502: free(line);
1.2 millert 503: return (-1);
1.1 millert 504: }
505:
506: /*
507: * Abort a http retrieval
508: */
509: void
1.3 millert 510: aborthttp(notused)
511: int notused;
1.1 millert 512: {
513:
514: alarmtimer(0);
1.10 deraadt 515: fputs("\nhttp fetch aborted.\n", ttyout);
516: (void)fflush(ttyout);
1.1 millert 517: longjmp(httpabort, 1);
518: }
519:
520: /*
1.22 deraadt 521: * Abort a http retrieval
522: */
523: void
524: abortfile(notused)
525: int notused;
526: {
527:
528: alarmtimer(0);
529: fputs("\nfile fetch aborted.\n", ttyout);
530: (void)fflush(ttyout);
531: longjmp(httpabort, 1);
532: }
533:
534: /*
1.1 millert 535: * Retrieve multiple files from the command line, transferring
536: * files of the form "host:path", "ftp://host/path" using the
537: * ftp protocol, and files of the form "http://host/path" using
538: * the http protocol.
1.2 millert 539: * If path has a trailing "/", then return (-1);
1.1 millert 540: * the path will be cd-ed into and the connection remains open,
541: * and the function will return -1 (to indicate the connection
542: * is alive).
543: * If an error occurs the return value will be the offset+1 in
544: * argv[] of the file that caused a problem (i.e, argv[x]
545: * returns x+1)
546: * Otherwise, 0 is returned if all files retrieved successfully.
547: */
548: int
1.17 millert 549: auto_fetch(argc, argv, outfile)
1.1 millert 550: int argc;
551: char *argv[];
1.17 millert 552: char *outfile;
1.1 millert 553: {
554: static char lasthost[MAXHOSTNAMELEN];
555: char *xargv[5];
556: char *cp, *line, *host, *dir, *file, *portnum;
1.8 millert 557: char *user, *pass;
1.6 millert 558: char *ftpproxy, *httpproxy;
1.14 millert 559: int rval, xargc;
560: volatile int argpos;
1.4 millert 561: int dirhasglob, filehasglob;
1.14 millert 562: char rempath[MAXPATHLEN];
1.1 millert 563:
564: argpos = 0;
565:
566: if (setjmp(toplevel)) {
567: if (connected)
568: disconnect(0, NULL);
1.2 millert 569: return (argpos + 1);
1.1 millert 570: }
1.3 millert 571: (void)signal(SIGINT, (sig_t)intr);
572: (void)signal(SIGPIPE, (sig_t)lostpeer);
1.1 millert 573:
1.6 millert 574: ftpproxy = getenv(FTP_PROXY);
575: httpproxy = getenv(HTTP_PROXY);
576:
1.1 millert 577: /*
578: * Loop through as long as there's files to fetch.
579: */
580: for (rval = 0; (rval == 0) && (argpos < argc); free(line), argpos++) {
581: if (strchr(argv[argpos], ':') == NULL)
582: break;
1.8 millert 583: host = dir = file = portnum = user = pass = NULL;
1.1 millert 584:
585: /*
586: * We muck with the string, so we make a copy.
587: */
588: line = strdup(argv[argpos]);
589: if (line == NULL)
590: errx(1, "Can't allocate memory for auto-fetch.");
591:
592: /*
593: * Try HTTP URL-style arguments first.
594: */
1.22 deraadt 595: if (strncasecmp(line, HTTP_URL, sizeof(HTTP_URL) - 1) == 0 ||
596: strncasecmp(line, FILE_URL, sizeof(FILE_URL) - 1) == 0) {
1.17 millert 597: if (url_get(line, httpproxy, outfile) == -1)
1.1 millert 598: rval = argpos + 1;
599: continue;
600: }
601:
602: /*
1.6 millert 603: * Try FTP URL-style arguments next. If ftpproxy is
604: * set, use url_get() instead of standard ftp.
605: * Finally, try host:file.
1.1 millert 606: */
607: host = line;
1.7 millert 608: if (strncasecmp(line, FTP_URL, sizeof(FTP_URL) - 1) == 0) {
1.6 millert 609: if (ftpproxy) {
1.17 millert 610: if (url_get(line, ftpproxy, outfile) == -1)
1.6 millert 611: rval = argpos + 1;
612: continue;
613: }
1.1 millert 614: host += sizeof(FTP_URL) - 1;
1.8 millert 615: dir = strchr(host, '/');
1.1 millert 616:
1.8 millert 617: /* Look for [user:pass@]host[:port] */
1.11 millert 618: pass = strpbrk(host, ":@/");
1.9 millert 619: if (pass == NULL || *pass == '/') {
1.11 millert 620: pass = NULL;
1.8 millert 621: goto parsed_url;
1.9 millert 622: }
1.11 millert 623: if (pass == host || *pass == '@') {
624: bad_ftp_url:
1.14 millert 625: warnx("Invalid URL: %s", argv[argpos]);
1.8 millert 626: rval = argpos + 1;
627: continue;
628: }
629: *pass++ = '\0';
1.12 millert 630: /* XXX - assumes no '@' in pathname */
631: if ((cp = strrchr(pass, '@')) == NULL)
632: cp = strpbrk(pass, ":@/");
1.8 millert 633: if (cp == NULL || *cp == '/') {
634: portnum = pass;
1.11 millert 635: pass = NULL;
1.8 millert 636: goto parsed_url;
637: }
1.11 millert 638: if (EMPTYSTRING(cp) || *cp == ':')
639: goto bad_ftp_url;
1.8 millert 640: *cp++ = '\0';
1.11 millert 641: user = host;
642: if (EMPTYSTRING(user))
643: goto bad_ftp_url;
1.8 millert 644: host = cp;
1.1 millert 645: portnum = strchr(host, ':');
646: if (portnum != NULL)
647: *portnum++ = '\0';
1.8 millert 648: } else { /* classic style `host:file' */
649: dir = strchr(host, ':');
650: }
1.16 millert 651: parsed_url:
1.1 millert 652: if (EMPTYSTRING(host)) {
653: rval = argpos + 1;
654: continue;
655: }
656:
657: /*
1.9 millert 658: * If dir is NULL, the file wasn't specified
1.1 millert 659: * (URL looked something like ftp://host)
660: */
1.8 millert 661: if (dir != NULL)
662: *dir++ = '\0';
1.1 millert 663:
664: /*
665: * Extract the file and (if present) directory name.
666: */
667: if (! EMPTYSTRING(dir)) {
1.8 millert 668: cp = strrchr(dir, '/');
1.1 millert 669: if (cp != NULL) {
670: *cp++ = '\0';
671: file = cp;
672: } else {
673: file = dir;
674: dir = NULL;
675: }
676: }
677: if (debug)
1.10 deraadt 678: fprintf(ttyout, "user %s:%s host %s port %s dir %s file %s\n",
1.8 millert 679: user, pass, host, portnum, dir, file);
1.1 millert 680:
681: /*
682: * Set up the connection if we don't have one.
683: */
684: if (strcmp(host, lasthost) != 0) {
1.8 millert 685: int oautologin;
686:
1.5 millert 687: (void)strcpy(lasthost, host);
1.1 millert 688: if (connected)
689: disconnect(0, NULL);
690: xargv[0] = __progname;
691: xargv[1] = host;
692: xargv[2] = NULL;
693: xargc = 2;
1.8 millert 694: if (! EMPTYSTRING(portnum)) {
1.1 millert 695: xargv[2] = portnum;
696: xargv[3] = NULL;
697: xargc = 3;
698: }
1.8 millert 699: oautologin = autologin;
700: if (user != NULL)
701: autologin = 0;
1.1 millert 702: setpeer(xargc, xargv);
1.8 millert 703: autologin = oautologin;
704: if ((connected == 0) ||
1.14 millert 705: ((connected == 1) && !login(host, user, pass))) {
1.8 millert 706: warnx("Can't connect or login to host `%s'",
707: host);
1.1 millert 708: rval = argpos + 1;
709: continue;
710: }
711:
712: /* Always use binary transfers. */
713: setbinary(0, NULL);
714: }
1.8 millert 715: /* cd back to '/' */
716: xargv[0] = "cd";
717: xargv[1] = "/";
718: xargv[2] = NULL;
719: cd(2, xargv);
720: if (! dirchange) {
721: rval = argpos + 1;
722: continue;
1.1 millert 723: }
724:
1.4 millert 725: dirhasglob = filehasglob = 0;
726: if (doglob) {
727: if (! EMPTYSTRING(dir) &&
728: strpbrk(dir, "*?[]{}") != NULL)
729: dirhasglob = 1;
730: if (! EMPTYSTRING(file) &&
731: strpbrk(file, "*?[]{}") != NULL)
732: filehasglob = 1;
733: }
734:
1.1 millert 735: /* Change directories, if necessary. */
1.4 millert 736: if (! EMPTYSTRING(dir) && !dirhasglob) {
1.1 millert 737: xargv[0] = "cd";
738: xargv[1] = dir;
739: xargv[2] = NULL;
740: cd(2, xargv);
741: if (! dirchange) {
742: rval = argpos + 1;
743: continue;
744: }
745: }
746:
747: if (EMPTYSTRING(file)) {
748: rval = -1;
749: continue;
750: }
751:
1.21 marc 752: if (verbose)
1.10 deraadt 753: fprintf(ttyout, "Retrieving %s/%s\n", dir ? dir : "", file);
1.1 millert 754:
1.4 millert 755: if (dirhasglob) {
756: snprintf(rempath, sizeof(rempath), "%s/%s", dir, file);
757: file = rempath;
758: }
759:
760: /* Fetch the file(s). */
1.10 deraadt 761: xargc = 2;
1.1 millert 762: xargv[0] = "get";
763: xargv[1] = file;
764: xargv[2] = NULL;
1.4 millert 765: if (dirhasglob || filehasglob) {
766: int ointeractive;
767:
768: ointeractive = interactive;
769: interactive = 0;
770: xargv[0] = "mget";
1.10 deraadt 771: mget(xargc, xargv);
1.5 millert 772: interactive = ointeractive;
1.10 deraadt 773: } else {
1.17 millert 774: if (outfile != NULL) {
775: xargv[2] = outfile;
776: xargv[3] = NULL;
1.10 deraadt 777: xargc++;
778: }
779: get(xargc, xargv);
780: }
1.1 millert 781:
1.4 millert 782: if ((code / 100) != COMPLETE)
1.1 millert 783: rval = argpos + 1;
784: }
785: if (connected && rval != -1)
786: disconnect(0, NULL);
787: return (rval);
788: }