Annotation of src/usr.bin/ftp/fetch.c, Revision 1.12
1.12 ! millert 1: /* $OpenBSD: fetch.c,v 1.11 1997/04/28 21:10:06 millert Exp $ */
1.11 millert 2: /* $NetBSD: fetch.c,v 1.8 1997/04/21 18:45:47 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
30: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
31: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37: * POSSIBILITY OF SUCH DAMAGE.
38: */
39:
40: #ifndef lint
1.12 ! millert 41: static char rcsid[] = "$OpenBSD: fetch.c,v 1.11 1997/04/28 21:10:06 millert 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>
51:
52: #include <netinet/in.h>
53:
54: #include <arpa/ftp.h>
55: #include <arpa/inet.h>
56:
57: #include <ctype.h>
58: #include <err.h>
59: #include <netdb.h>
60: #include <fcntl.h>
1.3 millert 61: #include <signal.h>
1.1 millert 62: #include <stdio.h>
63: #include <stdlib.h>
64: #include <string.h>
65: #include <unistd.h>
66:
67: #include "ftp_var.h"
68:
69: #define FTP_URL "ftp://" /* ftp URL prefix */
70: #define HTTP_URL "http://" /* http URL prefix */
1.6 millert 71: #define FTP_PROXY "ftp_proxy" /* env var with ftp proxy location */
1.1 millert 72: #define HTTP_PROXY "http_proxy" /* env var with http proxy location */
73:
74:
75: #define EMPTYSTRING(x) ((x) == NULL || (*(x) == '\0'))
76:
77: jmp_buf httpabort;
78:
79: /*
1.6 millert 80: * Retrieve URL, via the proxy in $proxyvar if necessary.
1.1 millert 81: * Modifies the string argument given.
82: * Returns -1 on failure, 0 on success
83: */
84: int
1.10 deraadt 85: url_get(line, proxyenv, fd)
1.1 millert 86: char *line;
1.6 millert 87: char *proxyenv;
1.10 deraadt 88: int fd;
1.1 millert 89: {
90: struct sockaddr_in sin;
91: int i, out, port, s;
92: size_t buflen, len;
93: char c, *cp, *cp2, *savefile, *portnum, *path, buf[4096];
1.6 millert 94: char *proxy, *host;
1.1 millert 95: sig_t oldintr;
96: off_t hashbytes;
97:
98: s = -1;
99: proxy = NULL;
100:
1.7 millert 101: if (strncasecmp(line, HTTP_URL, sizeof(HTTP_URL) - 1) == 0)
1.6 millert 102: host = line + sizeof(HTTP_URL) - 1;
1.7 millert 103: else if (strncasecmp(line, FTP_URL, sizeof(FTP_URL) - 1) == 0)
1.6 millert 104: host = line + sizeof(FTP_URL) - 1;
105: else
106: errx(1, "url_get: invalid url '%s'", line);
107:
1.1 millert 108: path = strchr(host, '/'); /* find path */
109: if (EMPTYSTRING(path))
1.6 millert 110: goto cleanup_url_get;
1.1 millert 111: *path++ = '\0';
112: if (EMPTYSTRING(path))
1.6 millert 113: goto cleanup_url_get;
1.1 millert 114:
115: savefile = strrchr(path, '/'); /* find savefile */
116: if (savefile != NULL)
117: savefile++;
118: else
119: savefile = path;
120: if (EMPTYSTRING(savefile))
1.6 millert 121: goto cleanup_url_get;
1.1 millert 122:
123: if (proxyenv != NULL) { /* use proxy */
124: proxy = strdup(proxyenv);
125: if (proxy == NULL)
126: errx(1, "Can't allocate memory for proxy url.");
1.7 millert 127: if (strncasecmp(proxy, HTTP_URL, sizeof(HTTP_URL) - 1) == 0)
1.6 millert 128: host = proxy + sizeof(HTTP_URL) - 1;
1.7 millert 129: else if (strncasecmp(proxy, FTP_URL, sizeof(FTP_URL) - 1) == 0)
1.6 millert 130: host = proxy + sizeof(FTP_URL) - 1;
131: else {
1.8 millert 132: warnx("Malformed proxy URL: %s", proxy);
1.6 millert 133: goto cleanup_url_get;
134: }
1.1 millert 135: if (EMPTYSTRING(host))
1.6 millert 136: goto cleanup_url_get;
1.1 millert 137: *--path = '/'; /* add / back to real path */
138: path = strchr(host, '/'); /* remove trailing / on host */
139: if (! EMPTYSTRING(path))
140: *path++ = '\0';
141: path = line;
142: }
143:
144: portnum = strchr(host, ':'); /* find portnum */
145: if (portnum != NULL)
146: *portnum++ = '\0';
147:
148: if (debug)
1.10 deraadt 149: fprintf(ttyout, "host %s, port %s, path %s, save as %s.\n",
1.1 millert 150: host, portnum, path, savefile);
151:
152: memset(&sin, 0, sizeof(sin));
153: sin.sin_family = AF_INET;
154:
155: if (isdigit(host[0])) {
156: if (inet_aton(host, &sin.sin_addr) == 0) {
1.8 millert 157: warnx("Invalid IP address: %s", host);
1.6 millert 158: goto cleanup_url_get;
1.1 millert 159: }
160: } else {
161: struct hostent *hp;
162:
163: hp = gethostbyname(host);
164: if (hp == NULL) {
165: warnx("%s: %s", host, hstrerror(h_errno));
1.6 millert 166: goto cleanup_url_get;
1.1 millert 167: }
168: if (hp->h_addrtype != AF_INET) {
169: warnx("%s: not an Internet address?", host);
1.6 millert 170: goto cleanup_url_get;
1.1 millert 171: }
172: memcpy(&sin.sin_addr, hp->h_addr, hp->h_length);
173: }
174:
175: if (! EMPTYSTRING(portnum)) {
176: port = atoi(portnum);
177: if (port < 1 || (port & 0xffff) != port) {
1.8 millert 178: warnx("Invalid port: %s", portnum);
1.6 millert 179: goto cleanup_url_get;
1.1 millert 180: }
181: port = htons(port);
182: } else
183: port = httpport;
184: sin.sin_port = port;
185:
186: s = socket(AF_INET, SOCK_STREAM, 0);
187: if (s == -1) {
188: warnx("Can't create socket");
1.6 millert 189: goto cleanup_url_get;
1.1 millert 190: }
191:
192: if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
193: warn("Can't connect to %s", host);
1.6 millert 194: goto cleanup_url_get;
1.1 millert 195: }
196:
197: /*
198: * Construct and send the request. We're expecting a return
199: * status of "200". Proxy requests don't want leading /.
200: */
201: if (!proxy)
1.10 deraadt 202: fprintf(ttyout, "Requesting %s:%d/%s\n", line, ntohs(port),
203: path);
1.1 millert 204: else
1.10 deraadt 205: fprintf(ttyout, "Requesting %s (via %s)\n", line, proxyenv);
1.1 millert 206: snprintf(buf, sizeof(buf), "GET %s%s HTTP/1.0\n\n",
207: proxy ? "" : "/", path);
208: buflen = strlen(buf);
209: if (write(s, buf, buflen) < buflen) {
210: warn("write");
1.6 millert 211: goto cleanup_url_get;
1.1 millert 212: }
213: memset(buf, 0, sizeof(buf));
214: for (i = 0, buflen = sizeof(buf), cp = buf; i < buflen; cp++, i++) {
215: if (read(s, cp, 1) != 1)
216: goto improper;
1.4 millert 217: if (*cp == '\r')
218: continue;
1.1 millert 219: if (*cp == '\n')
220: break;
221: }
222: buf[buflen - 1] = '\0'; /* sanity */
223: cp = strchr(buf, ' ');
224: if (cp == NULL)
225: goto improper;
226: else
227: cp++;
228: if (strncmp(cp, "200", 3)) {
229: warnx("Error retrieving file: %s", cp);
1.6 millert 230: goto cleanup_url_get;
1.1 millert 231: }
232:
233: /*
234: * Read the rest of the header.
235: */
236: memset(buf, 0, sizeof(buf));
237: c = '\0';
238: for (i = 0, buflen = sizeof(buf), cp = buf; i < buflen; cp++, i++) {
239: if (read(s, cp, 1) != 1)
240: goto improper;
1.4 millert 241: if (*cp == '\r')
242: continue;
1.1 millert 243: if (*cp == '\n' && c == '\n')
244: break;
245: c = *cp;
246: }
247: buf[buflen - 1] = '\0'; /* sanity */
248:
1.9 millert 249: /* Look for the "Content-length: " header. */
1.1 millert 250: #define CONTENTLEN "Content-Length: "
251: for (cp = buf; *cp != '\0'; cp++) {
252: if (tolower(*cp) == 'c' &&
253: strncasecmp(cp, CONTENTLEN, sizeof(CONTENTLEN) - 1) == 0)
254: break;
255: }
256: if (*cp == '\0')
257: goto improper;
258: cp += sizeof(CONTENTLEN) - 1;
259: cp2 = strchr(cp, '\n');
260: if (cp2 == NULL)
261: goto improper;
262: else
263: *cp2 = '\0';
264: filesize = atoi(cp);
265: if (filesize < 1)
266: goto improper;
267:
268: /* Open the output file. */
1.10 deraadt 269: if (fd == -1) {
270: out = open(savefile, O_CREAT | O_WRONLY | O_TRUNC, 0666);
271: if (out < 0) {
272: warn("Can't open %s", savefile);
273: goto cleanup_url_get;
274: }
275: } else
276: out = fd;
1.1 millert 277:
278: /* Trap signals */
279: oldintr = NULL;
280: if (setjmp(httpabort)) {
281: if (oldintr)
1.2 millert 282: (void)signal(SIGINT, oldintr);
1.6 millert 283: goto cleanup_url_get;
1.1 millert 284: }
285: oldintr = signal(SIGINT, aborthttp);
286:
287: bytes = 0;
288: hashbytes = mark;
289: progressmeter(-1);
290:
291: /* Finally, suck down the file. */
292: i = 0;
293: while ((len = read(s, buf, sizeof(buf))) > 0) {
294: bytes += len;
295: for (cp = buf; len > 0; len -= i, cp += i) {
296: if ((i = write(out, cp, len)) == -1) {
297: warn("Writing %s", savefile);
1.6 millert 298: goto cleanup_url_get;
1.1 millert 299: }
300: else if (i == 0)
301: break;
302: }
303: if (hash && !progress) {
304: while (bytes >= hashbytes) {
1.10 deraadt 305: (void)putc('#', ttyout);
1.1 millert 306: hashbytes += mark;
307: }
1.10 deraadt 308: (void)fflush(ttyout);
1.1 millert 309: }
310: }
311: if (hash && !progress && bytes > 0) {
312: if (bytes < mark)
1.10 deraadt 313: (void)putc('#', ttyout);
314: (void)putc('\n', ttyout);
315: (void)fflush(ttyout);
1.1 millert 316: }
317: if (len != 0) {
318: warn("Reading from socket");
1.6 millert 319: goto cleanup_url_get;
1.1 millert 320: }
321: progressmeter(1);
322: if (verbose)
1.10 deraadt 323: fputs("Successfully retrieved file.\n", ttyout);
1.2 millert 324: (void)signal(SIGINT, oldintr);
1.1 millert 325:
326: close(s);
1.10 deraadt 327: if (fd != -1)
328: close(out);
1.1 millert 329: if (proxy)
330: free(proxy);
1.2 millert 331: return (0);
1.1 millert 332:
333: improper:
1.8 millert 334: warnx("Improper response from %s", host);
1.6 millert 335: cleanup_url_get:
1.1 millert 336: if (s != -1)
337: close(s);
338: if (proxy)
339: free(proxy);
1.2 millert 340: return (-1);
1.1 millert 341: }
342:
343: /*
344: * Abort a http retrieval
345: */
346: void
1.3 millert 347: aborthttp(notused)
348: int notused;
1.1 millert 349: {
350:
351: alarmtimer(0);
1.10 deraadt 352: fputs("\nhttp fetch aborted.\n", ttyout);
353: (void)fflush(ttyout);
1.1 millert 354: longjmp(httpabort, 1);
355: }
356:
357: /*
358: * Retrieve multiple files from the command line, transferring
359: * files of the form "host:path", "ftp://host/path" using the
360: * ftp protocol, and files of the form "http://host/path" using
361: * the http protocol.
1.2 millert 362: * If path has a trailing "/", then return (-1);
1.1 millert 363: * the path will be cd-ed into and the connection remains open,
364: * and the function will return -1 (to indicate the connection
365: * is alive).
366: * If an error occurs the return value will be the offset+1 in
367: * argv[] of the file that caused a problem (i.e, argv[x]
368: * returns x+1)
369: * Otherwise, 0 is returned if all files retrieved successfully.
370: */
371: int
1.10 deraadt 372: auto_fetch(argc, argv, fd)
1.1 millert 373: int argc;
374: char *argv[];
1.10 deraadt 375: int fd;
1.1 millert 376: {
377: static char lasthost[MAXHOSTNAMELEN];
378: char *xargv[5];
379: char *cp, *line, *host, *dir, *file, *portnum;
1.8 millert 380: char *user, *pass;
1.6 millert 381: char *ftpproxy, *httpproxy;
1.1 millert 382: int rval, xargc, argpos;
1.4 millert 383: int dirhasglob, filehasglob;
1.10 deraadt 384: char rempath[MAXPATHLEN], fakedev[MAXPATHLEN];
1.1 millert 385:
386: argpos = 0;
387:
388: if (setjmp(toplevel)) {
389: if (connected)
390: disconnect(0, NULL);
1.2 millert 391: return (argpos + 1);
1.1 millert 392: }
1.3 millert 393: (void)signal(SIGINT, (sig_t)intr);
394: (void)signal(SIGPIPE, (sig_t)lostpeer);
1.1 millert 395:
1.6 millert 396: ftpproxy = getenv(FTP_PROXY);
397: httpproxy = getenv(HTTP_PROXY);
398:
1.1 millert 399: /*
400: * Loop through as long as there's files to fetch.
401: */
402: for (rval = 0; (rval == 0) && (argpos < argc); free(line), argpos++) {
403: if (strchr(argv[argpos], ':') == NULL)
404: break;
1.8 millert 405: host = dir = file = portnum = user = pass = NULL;
1.1 millert 406:
407: /*
408: * We muck with the string, so we make a copy.
409: */
410: line = strdup(argv[argpos]);
411: if (line == NULL)
412: errx(1, "Can't allocate memory for auto-fetch.");
413:
414: /*
415: * Try HTTP URL-style arguments first.
416: */
1.7 millert 417: if (strncasecmp(line, HTTP_URL, sizeof(HTTP_URL) - 1) == 0) {
1.10 deraadt 418: if (url_get(line, httpproxy, fd) == -1)
1.1 millert 419: rval = argpos + 1;
420: continue;
421: }
422:
423: /*
1.6 millert 424: * Try FTP URL-style arguments next. If ftpproxy is
425: * set, use url_get() instead of standard ftp.
426: * Finally, try host:file.
1.1 millert 427: */
428: host = line;
1.7 millert 429: if (strncasecmp(line, FTP_URL, sizeof(FTP_URL) - 1) == 0) {
1.6 millert 430: if (ftpproxy) {
1.10 deraadt 431: if (url_get(line, ftpproxy, fd) == -1)
1.6 millert 432: rval = argpos + 1;
433: continue;
434: }
1.1 millert 435: host += sizeof(FTP_URL) - 1;
1.8 millert 436: dir = strchr(host, '/');
1.1 millert 437:
1.8 millert 438: /* Look for [user:pass@]host[:port] */
1.11 millert 439: pass = strpbrk(host, ":@/");
1.9 millert 440: if (pass == NULL || *pass == '/') {
1.11 millert 441: pass = NULL;
1.8 millert 442: goto parsed_url;
1.9 millert 443: }
1.11 millert 444: if (pass == host || *pass == '@') {
445: bad_ftp_url:
1.8 millert 446: warnx("Bad ftp URL: %s", argv[argpos]);
447: rval = argpos + 1;
448: continue;
449: }
450: *pass++ = '\0';
1.12 ! millert 451: /* XXX - assumes no '@' in pathname */
! 452: if ((cp = strrchr(pass, '@')) == NULL)
! 453: cp = strpbrk(pass, ":@/");
1.8 millert 454: if (cp == NULL || *cp == '/') {
455: portnum = pass;
1.11 millert 456: pass = NULL;
1.8 millert 457: goto parsed_url;
458: }
1.11 millert 459: if (EMPTYSTRING(cp) || *cp == ':')
460: goto bad_ftp_url;
1.8 millert 461: *cp++ = '\0';
1.11 millert 462: user = host;
463: if (EMPTYSTRING(user))
464: goto bad_ftp_url;
1.8 millert 465: host = cp;
1.1 millert 466: portnum = strchr(host, ':');
467: if (portnum != NULL)
468: *portnum++ = '\0';
1.8 millert 469: parsed_url:
470: } else { /* classic style `host:file' */
471: dir = strchr(host, ':');
472: }
1.1 millert 473: if (EMPTYSTRING(host)) {
474: rval = argpos + 1;
475: continue;
476: }
477:
478: /*
1.9 millert 479: * If dir is NULL, the file wasn't specified
1.1 millert 480: * (URL looked something like ftp://host)
481: */
1.8 millert 482: if (dir != NULL)
483: *dir++ = '\0';
1.1 millert 484:
485: /*
486: * Extract the file and (if present) directory name.
487: */
488: if (! EMPTYSTRING(dir)) {
1.8 millert 489: cp = strrchr(dir, '/');
1.1 millert 490: if (cp != NULL) {
491: *cp++ = '\0';
492: file = cp;
493: } else {
494: file = dir;
495: dir = NULL;
496: }
497: }
498: if (debug)
1.10 deraadt 499: fprintf(ttyout, "user %s:%s host %s port %s dir %s file %s\n",
1.8 millert 500: user, pass, host, portnum, dir, file);
1.1 millert 501:
502: /*
503: * Set up the connection if we don't have one.
504: */
505: if (strcmp(host, lasthost) != 0) {
1.8 millert 506: int oautologin;
507:
1.5 millert 508: (void)strcpy(lasthost, host);
1.1 millert 509: if (connected)
510: disconnect(0, NULL);
511: xargv[0] = __progname;
512: xargv[1] = host;
513: xargv[2] = NULL;
514: xargc = 2;
1.8 millert 515: if (! EMPTYSTRING(portnum)) {
1.1 millert 516: xargv[2] = portnum;
517: xargv[3] = NULL;
518: xargc = 3;
519: }
1.8 millert 520: oautologin = autologin;
521: if (user != NULL)
522: autologin = 0;
1.1 millert 523: setpeer(xargc, xargv);
1.8 millert 524: autologin = oautologin;
525: if ((connected == 0) ||
526: ((connected == 1) && !login(host, user, pass)) ) {
527: warnx("Can't connect or login to host `%s'",
528: host);
1.1 millert 529: rval = argpos + 1;
530: continue;
531: }
532:
533: /* Always use binary transfers. */
534: setbinary(0, NULL);
535: }
1.8 millert 536: /* cd back to '/' */
537: xargv[0] = "cd";
538: xargv[1] = "/";
539: xargv[2] = NULL;
540: cd(2, xargv);
541: if (! dirchange) {
542: rval = argpos + 1;
543: continue;
1.1 millert 544: }
545:
1.4 millert 546: dirhasglob = filehasglob = 0;
547: if (doglob) {
548: if (! EMPTYSTRING(dir) &&
549: strpbrk(dir, "*?[]{}") != NULL)
550: dirhasglob = 1;
551: if (! EMPTYSTRING(file) &&
552: strpbrk(file, "*?[]{}") != NULL)
553: filehasglob = 1;
554: }
555:
1.1 millert 556: /* Change directories, if necessary. */
1.4 millert 557: if (! EMPTYSTRING(dir) && !dirhasglob) {
1.1 millert 558: xargv[0] = "cd";
559: xargv[1] = dir;
560: xargv[2] = NULL;
561: cd(2, xargv);
562: if (! dirchange) {
563: rval = argpos + 1;
564: continue;
565: }
566: }
567:
568: if (EMPTYSTRING(file)) {
569: rval = -1;
570: continue;
571: }
572:
573: if (!verbose)
1.10 deraadt 574: fprintf(ttyout, "Retrieving %s/%s\n", dir ? dir : "", file);
1.1 millert 575:
1.4 millert 576: if (dirhasglob) {
577: snprintf(rempath, sizeof(rempath), "%s/%s", dir, file);
578: file = rempath;
579: }
580:
581: /* Fetch the file(s). */
1.10 deraadt 582: xargc = 2;
1.1 millert 583: xargv[0] = "get";
584: xargv[1] = file;
585: xargv[2] = NULL;
1.4 millert 586: if (dirhasglob || filehasglob) {
587: int ointeractive;
588:
589: ointeractive = interactive;
590: interactive = 0;
591: xargv[0] = "mget";
1.10 deraadt 592: mget(xargc, xargv);
1.5 millert 593: interactive = ointeractive;
1.10 deraadt 594: } else {
595: if (fd != -1) {
596: xargv[2] = "-";
597: xargc++;
598: }
599: xargv[3] = NULL;
600: get(xargc, xargv);
601: }
1.1 millert 602:
1.4 millert 603: if ((code / 100) != COMPLETE)
1.1 millert 604: rval = argpos + 1;
605: }
606: if (connected && rval != -1)
607: disconnect(0, NULL);
608: return (rval);
609: }