Annotation of src/usr.bin/ftp/fetch.c, Revision 1.24
1.24 ! deraadt 1: /* $OpenBSD: fetch.c,v 1.23 1998/09/30 07:49:36 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.24 ! deraadt 41: static char rcsid[] = "$OpenBSD: fetch.c,v 1.23 1998/09/30 07:49:36 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);
1.24 ! deraadt 477: if (filesize != -1 && len == 0 && bytes != filesize) {
! 478: if (verbose)
! 479: fputs("Read short file.\n", ttyout);
! 480: goto cleanup_url_get;
! 481: }
! 482:
1.1 millert 483: if (verbose)
1.10 deraadt 484: fputs("Successfully retrieved file.\n", ttyout);
1.2 millert 485: (void)signal(SIGINT, oldintr);
1.1 millert 486:
487: close(s);
1.17 millert 488: if (out != fileno(stdout))
1.10 deraadt 489: close(out);
1.1 millert 490: if (proxy)
491: free(proxy);
1.14 millert 492: free(line);
1.2 millert 493: return (0);
1.1 millert 494:
1.14 millert 495: noftpautologin:
496: warnx(
497: "Auto-login using ftp URLs isn't supported when using $ftp_proxy");
498: goto cleanup_url_get;
499:
1.1 millert 500: improper:
1.8 millert 501: warnx("Improper response from %s", host);
1.14 millert 502:
1.6 millert 503: cleanup_url_get:
1.1 millert 504: if (s != -1)
505: close(s);
506: if (proxy)
507: free(proxy);
1.14 millert 508: free(line);
1.2 millert 509: return (-1);
1.1 millert 510: }
511:
512: /*
513: * Abort a http retrieval
514: */
515: void
1.3 millert 516: aborthttp(notused)
517: int notused;
1.1 millert 518: {
519:
520: alarmtimer(0);
1.10 deraadt 521: fputs("\nhttp fetch aborted.\n", ttyout);
522: (void)fflush(ttyout);
1.1 millert 523: longjmp(httpabort, 1);
524: }
525:
526: /*
1.22 deraadt 527: * Abort a http retrieval
528: */
529: void
530: abortfile(notused)
531: int notused;
532: {
533:
534: alarmtimer(0);
535: fputs("\nfile fetch aborted.\n", ttyout);
536: (void)fflush(ttyout);
537: longjmp(httpabort, 1);
538: }
539:
540: /*
1.1 millert 541: * Retrieve multiple files from the command line, transferring
542: * files of the form "host:path", "ftp://host/path" using the
543: * ftp protocol, and files of the form "http://host/path" using
544: * the http protocol.
1.2 millert 545: * If path has a trailing "/", then return (-1);
1.1 millert 546: * the path will be cd-ed into and the connection remains open,
547: * and the function will return -1 (to indicate the connection
548: * is alive).
549: * If an error occurs the return value will be the offset+1 in
550: * argv[] of the file that caused a problem (i.e, argv[x]
551: * returns x+1)
552: * Otherwise, 0 is returned if all files retrieved successfully.
553: */
554: int
1.17 millert 555: auto_fetch(argc, argv, outfile)
1.1 millert 556: int argc;
557: char *argv[];
1.17 millert 558: char *outfile;
1.1 millert 559: {
560: static char lasthost[MAXHOSTNAMELEN];
561: char *xargv[5];
562: char *cp, *line, *host, *dir, *file, *portnum;
1.8 millert 563: char *user, *pass;
1.6 millert 564: char *ftpproxy, *httpproxy;
1.14 millert 565: int rval, xargc;
566: volatile int argpos;
1.4 millert 567: int dirhasglob, filehasglob;
1.14 millert 568: char rempath[MAXPATHLEN];
1.1 millert 569:
570: argpos = 0;
571:
572: if (setjmp(toplevel)) {
573: if (connected)
574: disconnect(0, NULL);
1.2 millert 575: return (argpos + 1);
1.1 millert 576: }
1.3 millert 577: (void)signal(SIGINT, (sig_t)intr);
578: (void)signal(SIGPIPE, (sig_t)lostpeer);
1.1 millert 579:
1.6 millert 580: ftpproxy = getenv(FTP_PROXY);
581: httpproxy = getenv(HTTP_PROXY);
582:
1.1 millert 583: /*
584: * Loop through as long as there's files to fetch.
585: */
586: for (rval = 0; (rval == 0) && (argpos < argc); free(line), argpos++) {
587: if (strchr(argv[argpos], ':') == NULL)
588: break;
1.8 millert 589: host = dir = file = portnum = user = pass = NULL;
1.1 millert 590:
591: /*
592: * We muck with the string, so we make a copy.
593: */
594: line = strdup(argv[argpos]);
595: if (line == NULL)
596: errx(1, "Can't allocate memory for auto-fetch.");
597:
598: /*
599: * Try HTTP URL-style arguments first.
600: */
1.22 deraadt 601: if (strncasecmp(line, HTTP_URL, sizeof(HTTP_URL) - 1) == 0 ||
602: strncasecmp(line, FILE_URL, sizeof(FILE_URL) - 1) == 0) {
1.17 millert 603: if (url_get(line, httpproxy, outfile) == -1)
1.1 millert 604: rval = argpos + 1;
605: continue;
606: }
607:
608: /*
1.6 millert 609: * Try FTP URL-style arguments next. If ftpproxy is
610: * set, use url_get() instead of standard ftp.
611: * Finally, try host:file.
1.1 millert 612: */
613: host = line;
1.7 millert 614: if (strncasecmp(line, FTP_URL, sizeof(FTP_URL) - 1) == 0) {
1.6 millert 615: if (ftpproxy) {
1.17 millert 616: if (url_get(line, ftpproxy, outfile) == -1)
1.6 millert 617: rval = argpos + 1;
618: continue;
619: }
1.1 millert 620: host += sizeof(FTP_URL) - 1;
1.8 millert 621: dir = strchr(host, '/');
1.1 millert 622:
1.8 millert 623: /* Look for [user:pass@]host[:port] */
1.11 millert 624: pass = strpbrk(host, ":@/");
1.9 millert 625: if (pass == NULL || *pass == '/') {
1.11 millert 626: pass = NULL;
1.8 millert 627: goto parsed_url;
1.9 millert 628: }
1.11 millert 629: if (pass == host || *pass == '@') {
630: bad_ftp_url:
1.14 millert 631: warnx("Invalid URL: %s", argv[argpos]);
1.8 millert 632: rval = argpos + 1;
633: continue;
634: }
635: *pass++ = '\0';
1.12 millert 636: /* XXX - assumes no '@' in pathname */
637: if ((cp = strrchr(pass, '@')) == NULL)
638: cp = strpbrk(pass, ":@/");
1.8 millert 639: if (cp == NULL || *cp == '/') {
640: portnum = pass;
1.11 millert 641: pass = NULL;
1.8 millert 642: goto parsed_url;
643: }
1.11 millert 644: if (EMPTYSTRING(cp) || *cp == ':')
645: goto bad_ftp_url;
1.8 millert 646: *cp++ = '\0';
1.11 millert 647: user = host;
648: if (EMPTYSTRING(user))
649: goto bad_ftp_url;
1.8 millert 650: host = cp;
1.1 millert 651: portnum = strchr(host, ':');
652: if (portnum != NULL)
653: *portnum++ = '\0';
1.8 millert 654: } else { /* classic style `host:file' */
655: dir = strchr(host, ':');
656: }
1.16 millert 657: parsed_url:
1.1 millert 658: if (EMPTYSTRING(host)) {
659: rval = argpos + 1;
660: continue;
661: }
662:
663: /*
1.9 millert 664: * If dir is NULL, the file wasn't specified
1.1 millert 665: * (URL looked something like ftp://host)
666: */
1.8 millert 667: if (dir != NULL)
668: *dir++ = '\0';
1.1 millert 669:
670: /*
671: * Extract the file and (if present) directory name.
672: */
673: if (! EMPTYSTRING(dir)) {
1.8 millert 674: cp = strrchr(dir, '/');
1.1 millert 675: if (cp != NULL) {
676: *cp++ = '\0';
677: file = cp;
678: } else {
679: file = dir;
680: dir = NULL;
681: }
682: }
683: if (debug)
1.10 deraadt 684: fprintf(ttyout, "user %s:%s host %s port %s dir %s file %s\n",
1.8 millert 685: user, pass, host, portnum, dir, file);
1.1 millert 686:
687: /*
688: * Set up the connection if we don't have one.
689: */
690: if (strcmp(host, lasthost) != 0) {
1.8 millert 691: int oautologin;
692:
1.5 millert 693: (void)strcpy(lasthost, host);
1.1 millert 694: if (connected)
695: disconnect(0, NULL);
696: xargv[0] = __progname;
697: xargv[1] = host;
698: xargv[2] = NULL;
699: xargc = 2;
1.8 millert 700: if (! EMPTYSTRING(portnum)) {
1.1 millert 701: xargv[2] = portnum;
702: xargv[3] = NULL;
703: xargc = 3;
704: }
1.8 millert 705: oautologin = autologin;
706: if (user != NULL)
707: autologin = 0;
1.1 millert 708: setpeer(xargc, xargv);
1.8 millert 709: autologin = oautologin;
710: if ((connected == 0) ||
1.14 millert 711: ((connected == 1) && !login(host, user, pass))) {
1.8 millert 712: warnx("Can't connect or login to host `%s'",
713: host);
1.1 millert 714: rval = argpos + 1;
715: continue;
716: }
717:
718: /* Always use binary transfers. */
719: setbinary(0, NULL);
720: }
1.8 millert 721: /* cd back to '/' */
722: xargv[0] = "cd";
723: xargv[1] = "/";
724: xargv[2] = NULL;
725: cd(2, xargv);
726: if (! dirchange) {
727: rval = argpos + 1;
728: continue;
1.1 millert 729: }
730:
1.4 millert 731: dirhasglob = filehasglob = 0;
732: if (doglob) {
733: if (! EMPTYSTRING(dir) &&
734: strpbrk(dir, "*?[]{}") != NULL)
735: dirhasglob = 1;
736: if (! EMPTYSTRING(file) &&
737: strpbrk(file, "*?[]{}") != NULL)
738: filehasglob = 1;
739: }
740:
1.1 millert 741: /* Change directories, if necessary. */
1.4 millert 742: if (! EMPTYSTRING(dir) && !dirhasglob) {
1.1 millert 743: xargv[0] = "cd";
744: xargv[1] = dir;
745: xargv[2] = NULL;
746: cd(2, xargv);
747: if (! dirchange) {
748: rval = argpos + 1;
749: continue;
750: }
751: }
752:
753: if (EMPTYSTRING(file)) {
754: rval = -1;
755: continue;
756: }
757:
1.21 marc 758: if (verbose)
1.10 deraadt 759: fprintf(ttyout, "Retrieving %s/%s\n", dir ? dir : "", file);
1.1 millert 760:
1.4 millert 761: if (dirhasglob) {
762: snprintf(rempath, sizeof(rempath), "%s/%s", dir, file);
763: file = rempath;
764: }
765:
766: /* Fetch the file(s). */
1.10 deraadt 767: xargc = 2;
1.1 millert 768: xargv[0] = "get";
769: xargv[1] = file;
770: xargv[2] = NULL;
1.4 millert 771: if (dirhasglob || filehasglob) {
772: int ointeractive;
773:
774: ointeractive = interactive;
775: interactive = 0;
776: xargv[0] = "mget";
1.10 deraadt 777: mget(xargc, xargv);
1.5 millert 778: interactive = ointeractive;
1.10 deraadt 779: } else {
1.17 millert 780: if (outfile != NULL) {
781: xargv[2] = outfile;
782: xargv[3] = NULL;
1.10 deraadt 783: xargc++;
784: }
785: get(xargc, xargv);
786: }
1.1 millert 787:
1.4 millert 788: if ((code / 100) != COMPLETE)
1.1 millert 789: rval = argpos + 1;
790: }
791: if (connected && rval != -1)
792: disconnect(0, NULL);
793: return (rval);
794: }