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