Annotation of src/usr.bin/ftp/fetch.c, Revision 1.33
1.33 ! millert 1: /* $OpenBSD: fetch.c,v 1.32 2000/05/25 16:09:26 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.33 ! millert 41: static char rcsid[] = "$OpenBSD: fetch.c,v 1.32 2000/05/25 16:09:26 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);
1.30 deraadt 293: if (error == EAI_SERVICE && port == httpport) {
294: /*
295: * If the services file is corrupt/missing, fall back
296: * on our hard-coded defines.
297: */
298: char pbuf[NI_MAXSERV];
299:
300: snprintf(pbuf, sizeof(pbuf), "%d", HTTP_PORT);
301: error = getaddrinfo(host, pbuf, &hints, &res0);
302: }
1.25 itojun 303: if (error) {
304: warnx("%s: %s", gai_strerror(error), host);
305: goto cleanup_url_get;
1.1 millert 306: }
307:
1.25 itojun 308: s = -1;
309: for (res = res0; res; res = res->ai_next) {
310: getnameinfo(res->ai_addr, res->ai_addrlen, buf, sizeof(buf),
311: NULL, 0, NI_NUMERICHOST);
312: fprintf(ttyout, "Trying %s...\n", buf);
1.14 millert 313:
1.25 itojun 314: s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
315: if (s == -1) {
316: cause = "socket";
317: continue;
1.1 millert 318: }
319:
1.25 itojun 320: again:
321: if (connect(s, res->ai_addr, res->ai_addrlen) < 0) {
322: if (errno == EINTR)
323: goto again;
324: close(s);
325: s = -1;
326: cause = "connect";
1.19 deraadt 327: continue;
328: }
1.25 itojun 329:
1.29 itojun 330: /* get port in numeric */
331: if (getnameinfo(res->ai_addr, res->ai_addrlen, NULL, 0,
332: pbuf, sizeof(pbuf), NI_NUMERICSERV) == 0)
333: port = pbuf;
334: else
335: port = NULL;
336:
1.25 itojun 337: break;
338: }
339: freeaddrinfo(res0);
340: if (s < 0) {
1.33 ! millert 341: warn("%s", cause);
1.6 millert 342: goto cleanup_url_get;
1.1 millert 343: }
344:
345: /*
346: * Construct and send the request. We're expecting a return
347: * status of "200". Proxy requests don't want leading /.
348: */
1.32 itojun 349: if (proxy) {
350: /*
351: * Host: directive must use the destination host address for
352: * the original URI (path). We do not attach it at this moment.
353: */
354: if (verbose)
1.23 deraadt 355: fprintf(ttyout, "Requesting %s (via %s)\n",
356: origline, proxyenv);
1.32 itojun 357: snprintf(buf, sizeof(buf), "GET %s HTTP/1.0\r\n\r\n", path);
1.28 itojun 358: } else {
1.32 itojun 359: if (verbose)
360: fprintf(ttyout, "Requesting %s\n", origline);
361: if (strchr(host, ':')) {
362: char *h, *p;
363:
364: /* strip off scoped address portion, since it's local to node */
365: h = strdup(host);
366: if (h == NULL)
367: errx(1, "Can't allocate memory.");
368: if ((p = strchr(h, '%')) != NULL)
369: *p = '\0';
370: snprintf(buf, sizeof(buf),
371: "GET /%s HTTP/1.0\r\nHost: [%s]%s%s\r\n\r\n",
372: path, h, port ? ":" : "", port ? port : "");
373: free(h);
374: } else {
375: snprintf(buf, sizeof(buf),
376: "GET /%s HTTP/1.0\r\nHost: %s%s%s\r\n\r\n",
377: path, host, port ? ":" : "", port ? port : "");
378: }
1.28 itojun 379: }
1.14 millert 380: len = strlen(buf);
381: if (write(s, buf, len) < len) {
382: warn("Writing HTTP request");
1.6 millert 383: goto cleanup_url_get;
1.1 millert 384: }
385: memset(buf, 0, sizeof(buf));
1.14 millert 386: for (cp = buf; cp < buf + sizeof(buf); ) {
1.1 millert 387: if (read(s, cp, 1) != 1)
388: goto improper;
1.4 millert 389: if (*cp == '\r')
390: continue;
1.1 millert 391: if (*cp == '\n')
392: break;
1.14 millert 393: cp++;
1.1 millert 394: }
1.14 millert 395: buf[sizeof(buf) - 1] = '\0'; /* sanity */
1.1 millert 396: cp = strchr(buf, ' ');
397: if (cp == NULL)
398: goto improper;
399: else
400: cp++;
401: if (strncmp(cp, "200", 3)) {
402: warnx("Error retrieving file: %s", cp);
1.6 millert 403: goto cleanup_url_get;
1.1 millert 404: }
405:
406: /*
407: * Read the rest of the header.
408: */
409: memset(buf, 0, sizeof(buf));
410: c = '\0';
1.14 millert 411: for (cp = buf; cp < buf + sizeof(buf); ) {
1.1 millert 412: if (read(s, cp, 1) != 1)
413: goto improper;
1.4 millert 414: if (*cp == '\r')
415: continue;
1.1 millert 416: if (*cp == '\n' && c == '\n')
417: break;
418: c = *cp;
1.14 millert 419: cp++;
1.1 millert 420: }
1.14 millert 421: buf[sizeof(buf) - 1] = '\0'; /* sanity */
1.1 millert 422:
1.9 millert 423: /* Look for the "Content-length: " header. */
1.1 millert 424: #define CONTENTLEN "Content-Length: "
425: for (cp = buf; *cp != '\0'; cp++) {
426: if (tolower(*cp) == 'c' &&
427: strncasecmp(cp, CONTENTLEN, sizeof(CONTENTLEN) - 1) == 0)
428: break;
429: }
1.13 deraadt 430: if (*cp != '\0') {
431: cp += sizeof(CONTENTLEN) - 1;
1.14 millert 432: ep = strchr(cp, '\n');
433: if (ep == NULL)
1.13 deraadt 434: goto improper;
435: else
1.14 millert 436: *ep = '\0';
437: filesize = strtol(cp, &ep, 10);
438: if (filesize < 1 || *ep != '\0')
1.13 deraadt 439: goto improper;
440: } else
441: filesize = -1;
1.1 millert 442:
1.17 millert 443: /* Open the output file. */
444: if (strcmp(savefile, "-") != 0) {
1.10 deraadt 445: out = open(savefile, O_CREAT | O_WRONLY | O_TRUNC, 0666);
446: if (out < 0) {
447: warn("Can't open %s", savefile);
448: goto cleanup_url_get;
449: }
450: } else
1.17 millert 451: out = fileno(stdout);
1.1 millert 452:
453: /* Trap signals */
454: oldintr = NULL;
455: if (setjmp(httpabort)) {
456: if (oldintr)
1.2 millert 457: (void)signal(SIGINT, oldintr);
1.6 millert 458: goto cleanup_url_get;
1.1 millert 459: }
460: oldintr = signal(SIGINT, aborthttp);
461:
462: bytes = 0;
463: hashbytes = mark;
464: progressmeter(-1);
465:
466: /* Finally, suck down the file. */
467: i = 0;
468: while ((len = read(s, buf, sizeof(buf))) > 0) {
469: bytes += len;
470: for (cp = buf; len > 0; len -= i, cp += i) {
471: if ((i = write(out, cp, len)) == -1) {
472: warn("Writing %s", savefile);
1.6 millert 473: goto cleanup_url_get;
1.1 millert 474: }
475: else if (i == 0)
476: break;
477: }
478: if (hash && !progress) {
479: while (bytes >= hashbytes) {
1.10 deraadt 480: (void)putc('#', ttyout);
1.1 millert 481: hashbytes += mark;
482: }
1.10 deraadt 483: (void)fflush(ttyout);
1.1 millert 484: }
485: }
486: if (hash && !progress && bytes > 0) {
487: if (bytes < mark)
1.10 deraadt 488: (void)putc('#', ttyout);
489: (void)putc('\n', ttyout);
490: (void)fflush(ttyout);
1.1 millert 491: }
492: if (len != 0) {
493: warn("Reading from socket");
1.6 millert 494: goto cleanup_url_get;
1.1 millert 495: }
496: progressmeter(1);
1.24 deraadt 497: if (filesize != -1 && len == 0 && bytes != filesize) {
498: if (verbose)
499: fputs("Read short file.\n", ttyout);
500: goto cleanup_url_get;
501: }
502:
1.1 millert 503: if (verbose)
1.10 deraadt 504: fputs("Successfully retrieved file.\n", ttyout);
1.2 millert 505: (void)signal(SIGINT, oldintr);
1.1 millert 506:
507: close(s);
1.17 millert 508: if (out != fileno(stdout))
1.10 deraadt 509: close(out);
1.1 millert 510: if (proxy)
511: free(proxy);
1.14 millert 512: free(line);
1.2 millert 513: return (0);
1.1 millert 514:
1.14 millert 515: noftpautologin:
516: warnx(
517: "Auto-login using ftp URLs isn't supported when using $ftp_proxy");
518: goto cleanup_url_get;
519:
1.1 millert 520: improper:
1.8 millert 521: warnx("Improper response from %s", host);
1.14 millert 522:
1.6 millert 523: cleanup_url_get:
1.1 millert 524: if (s != -1)
525: close(s);
526: if (proxy)
527: free(proxy);
1.14 millert 528: free(line);
1.2 millert 529: return (-1);
1.1 millert 530: }
531:
532: /*
533: * Abort a http retrieval
534: */
535: void
1.3 millert 536: aborthttp(notused)
537: int notused;
1.1 millert 538: {
539:
540: alarmtimer(0);
1.10 deraadt 541: fputs("\nhttp fetch aborted.\n", ttyout);
542: (void)fflush(ttyout);
1.1 millert 543: longjmp(httpabort, 1);
544: }
545:
546: /*
1.22 deraadt 547: * Abort a http retrieval
548: */
549: void
550: abortfile(notused)
551: int notused;
552: {
553:
554: alarmtimer(0);
555: fputs("\nfile fetch aborted.\n", ttyout);
556: (void)fflush(ttyout);
557: longjmp(httpabort, 1);
558: }
559:
560: /*
1.1 millert 561: * Retrieve multiple files from the command line, transferring
562: * files of the form "host:path", "ftp://host/path" using the
563: * ftp protocol, and files of the form "http://host/path" using
564: * the http protocol.
1.2 millert 565: * If path has a trailing "/", then return (-1);
1.1 millert 566: * the path will be cd-ed into and the connection remains open,
567: * and the function will return -1 (to indicate the connection
568: * is alive).
569: * If an error occurs the return value will be the offset+1 in
570: * argv[] of the file that caused a problem (i.e, argv[x]
571: * returns x+1)
572: * Otherwise, 0 is returned if all files retrieved successfully.
573: */
574: int
1.17 millert 575: auto_fetch(argc, argv, outfile)
1.1 millert 576: int argc;
577: char *argv[];
1.17 millert 578: char *outfile;
1.1 millert 579: {
580: static char lasthost[MAXHOSTNAMELEN];
581: char *xargv[5];
582: char *cp, *line, *host, *dir, *file, *portnum;
1.8 millert 583: char *user, *pass;
1.6 millert 584: char *ftpproxy, *httpproxy;
1.14 millert 585: int rval, xargc;
586: volatile int argpos;
1.4 millert 587: int dirhasglob, filehasglob;
1.14 millert 588: char rempath[MAXPATHLEN];
1.1 millert 589:
590: argpos = 0;
591:
592: if (setjmp(toplevel)) {
593: if (connected)
594: disconnect(0, NULL);
1.2 millert 595: return (argpos + 1);
1.1 millert 596: }
1.3 millert 597: (void)signal(SIGINT, (sig_t)intr);
598: (void)signal(SIGPIPE, (sig_t)lostpeer);
1.1 millert 599:
1.6 millert 600: ftpproxy = getenv(FTP_PROXY);
601: httpproxy = getenv(HTTP_PROXY);
602:
1.1 millert 603: /*
604: * Loop through as long as there's files to fetch.
605: */
606: for (rval = 0; (rval == 0) && (argpos < argc); free(line), argpos++) {
607: if (strchr(argv[argpos], ':') == NULL)
608: break;
1.8 millert 609: host = dir = file = portnum = user = pass = NULL;
1.1 millert 610:
611: /*
612: * We muck with the string, so we make a copy.
613: */
614: line = strdup(argv[argpos]);
615: if (line == NULL)
616: errx(1, "Can't allocate memory for auto-fetch.");
617:
618: /*
619: * Try HTTP URL-style arguments first.
620: */
1.22 deraadt 621: if (strncasecmp(line, HTTP_URL, sizeof(HTTP_URL) - 1) == 0 ||
622: strncasecmp(line, FILE_URL, sizeof(FILE_URL) - 1) == 0) {
1.17 millert 623: if (url_get(line, httpproxy, outfile) == -1)
1.1 millert 624: rval = argpos + 1;
625: continue;
626: }
627:
628: /*
1.6 millert 629: * Try FTP URL-style arguments next. If ftpproxy is
630: * set, use url_get() instead of standard ftp.
631: * Finally, try host:file.
1.1 millert 632: */
633: host = line;
1.7 millert 634: if (strncasecmp(line, FTP_URL, sizeof(FTP_URL) - 1) == 0) {
1.31 itojun 635: char *passend, *userend;
636:
1.6 millert 637: if (ftpproxy) {
1.17 millert 638: if (url_get(line, ftpproxy, outfile) == -1)
1.6 millert 639: rval = argpos + 1;
640: continue;
641: }
1.1 millert 642: host += sizeof(FTP_URL) - 1;
1.8 millert 643: dir = strchr(host, '/');
1.1 millert 644:
1.8 millert 645: /* Look for [user:pass@]host[:port] */
1.31 itojun 646:
647: /* check if we have "user:pass@" */
648: passend = strchr(host, '@');
649: userend = strchr(host, ':');
650: if (passend && userend && userend < passend &&
651: (!dir || passend < dir)) {
652: user = host;
653: pass = userend + 1;
654: host = passend + 1;
655: *userend = *passend = '\0';
656:
657: if (EMPTYSTRING(user) || EMPTYSTRING(pass)) {
1.11 millert 658: bad_ftp_url:
1.31 itojun 659: warnx("Invalid URL: %s", argv[argpos]);
660: rval = argpos + 1;
661: continue;
662: }
1.8 millert 663: }
1.31 itojun 664:
665: #ifdef INET6
666: /* check [host]:port, or [host] */
667: if (host[0] == '[') {
668: cp = strchr(host, ']');
669: if (cp && (!dir || cp < dir)) {
670: if (cp + 1 == dir || cp[1] == ':') {
671: host++;
672: *cp++ = '\0';
673: } else
674: cp = NULL;
675: } else
676: cp = host;
1.25 itojun 677: } else
678: cp = host;
1.31 itojun 679: #else
680: cp = host;
1.25 itojun 681: #endif
1.31 itojun 682:
683: /* split off host[:port] if there is */
684: if (cp) {
685: portnum = strchr(cp, ':');
686: if (!portnum)
687: ;
688: else {
689: if (!dir)
690: ;
691: else if (portnum + 1 < dir) {
692: *portnum++ = '\0';
693: /*
694: * XXX should check if portnum
695: * is decimal number
696: */
697: } else {
698: /* empty portnum */
699: goto bad_ftp_url;
700: }
701: }
702: } else
703: portnum = NULL;
1.8 millert 704: } else { /* classic style `host:file' */
705: dir = strchr(host, ':');
706: }
1.1 millert 707: if (EMPTYSTRING(host)) {
708: rval = argpos + 1;
709: continue;
710: }
711:
712: /*
1.9 millert 713: * If dir is NULL, the file wasn't specified
1.1 millert 714: * (URL looked something like ftp://host)
715: */
1.8 millert 716: if (dir != NULL)
717: *dir++ = '\0';
1.1 millert 718:
719: /*
720: * Extract the file and (if present) directory name.
721: */
722: if (! EMPTYSTRING(dir)) {
1.8 millert 723: cp = strrchr(dir, '/');
1.1 millert 724: if (cp != NULL) {
725: *cp++ = '\0';
726: file = cp;
727: } else {
728: file = dir;
729: dir = NULL;
730: }
731: }
732: if (debug)
1.10 deraadt 733: fprintf(ttyout, "user %s:%s host %s port %s dir %s file %s\n",
1.8 millert 734: user, pass, host, portnum, dir, file);
1.1 millert 735:
736: /*
737: * Set up the connection if we don't have one.
738: */
739: if (strcmp(host, lasthost) != 0) {
1.8 millert 740: int oautologin;
741:
1.5 millert 742: (void)strcpy(lasthost, host);
1.1 millert 743: if (connected)
744: disconnect(0, NULL);
745: xargv[0] = __progname;
746: xargv[1] = host;
747: xargv[2] = NULL;
748: xargc = 2;
1.8 millert 749: if (! EMPTYSTRING(portnum)) {
1.1 millert 750: xargv[2] = portnum;
751: xargv[3] = NULL;
752: xargc = 3;
753: }
1.8 millert 754: oautologin = autologin;
755: if (user != NULL)
756: autologin = 0;
1.1 millert 757: setpeer(xargc, xargv);
1.8 millert 758: autologin = oautologin;
759: if ((connected == 0) ||
1.14 millert 760: ((connected == 1) && !login(host, user, pass))) {
1.8 millert 761: warnx("Can't connect or login to host `%s'",
762: host);
1.1 millert 763: rval = argpos + 1;
764: continue;
765: }
766:
767: /* Always use binary transfers. */
768: setbinary(0, NULL);
769: }
1.8 millert 770: /* cd back to '/' */
771: xargv[0] = "cd";
772: xargv[1] = "/";
773: xargv[2] = NULL;
774: cd(2, xargv);
775: if (! dirchange) {
776: rval = argpos + 1;
777: continue;
1.1 millert 778: }
779:
1.4 millert 780: dirhasglob = filehasglob = 0;
781: if (doglob) {
782: if (! EMPTYSTRING(dir) &&
783: strpbrk(dir, "*?[]{}") != NULL)
784: dirhasglob = 1;
785: if (! EMPTYSTRING(file) &&
786: strpbrk(file, "*?[]{}") != NULL)
787: filehasglob = 1;
788: }
789:
1.1 millert 790: /* Change directories, if necessary. */
1.4 millert 791: if (! EMPTYSTRING(dir) && !dirhasglob) {
1.1 millert 792: xargv[0] = "cd";
793: xargv[1] = dir;
794: xargv[2] = NULL;
795: cd(2, xargv);
796: if (! dirchange) {
797: rval = argpos + 1;
798: continue;
799: }
800: }
801:
802: if (EMPTYSTRING(file)) {
803: rval = -1;
804: continue;
805: }
806:
1.21 marc 807: if (verbose)
1.10 deraadt 808: fprintf(ttyout, "Retrieving %s/%s\n", dir ? dir : "", file);
1.1 millert 809:
1.4 millert 810: if (dirhasglob) {
811: snprintf(rempath, sizeof(rempath), "%s/%s", dir, file);
812: file = rempath;
813: }
814:
815: /* Fetch the file(s). */
1.10 deraadt 816: xargc = 2;
1.1 millert 817: xargv[0] = "get";
818: xargv[1] = file;
819: xargv[2] = NULL;
1.4 millert 820: if (dirhasglob || filehasglob) {
821: int ointeractive;
822:
823: ointeractive = interactive;
824: interactive = 0;
825: xargv[0] = "mget";
1.10 deraadt 826: mget(xargc, xargv);
1.5 millert 827: interactive = ointeractive;
1.10 deraadt 828: } else {
1.17 millert 829: if (outfile != NULL) {
830: xargv[2] = outfile;
831: xargv[3] = NULL;
1.10 deraadt 832: xargc++;
833: }
834: get(xargc, xargv);
835: }
1.1 millert 836:
1.4 millert 837: if ((code / 100) != COMPLETE)
1.1 millert 838: rval = argpos + 1;
839: }
840: if (connected && rval != -1)
841: disconnect(0, NULL);
842: return (rval);
1.25 itojun 843: }
844:
845: int
846: isurl(p)
847: const char *p;
848: {
1.27 millert 849:
1.26 deraadt 850: if (strncasecmp(p, FTP_URL, sizeof(FTP_URL) - 1) == 0 ||
851: strncasecmp(p, HTTP_URL, sizeof(HTTP_URL) - 1) == 0 ||
1.27 millert 852: strncasecmp(p, FILE_URL, sizeof(FILE_URL) - 1) == 0 ||
853: strstr(p, ":/"))
854: return (1);
855: return (0);
1.1 millert 856: }