Annotation of src/usr.bin/ftp/fetch.c, Revision 1.11
1.11 ! millert 1: /* $OpenBSD: fetch.c,v 1.10 1997/04/23 20:33:06 deraadt Exp $ */
! 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.11 ! millert 41: static char rcsid[] = "$OpenBSD: fetch.c,v 1.10 1997/04/23 20:33:06 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>
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';
451: cp = strpbrk(pass, ":@/");
452: if (cp == NULL || *cp == '/') {
453: portnum = pass;
1.11 ! millert 454: pass = NULL;
1.8 millert 455: goto parsed_url;
456: }
1.11 ! millert 457: if (EMPTYSTRING(cp) || *cp == ':')
! 458: goto bad_ftp_url;
1.8 millert 459: *cp++ = '\0';
1.11 ! millert 460: user = host;
! 461: if (EMPTYSTRING(user))
! 462: goto bad_ftp_url;
1.8 millert 463: host = cp;
1.1 millert 464: portnum = strchr(host, ':');
465: if (portnum != NULL)
466: *portnum++ = '\0';
1.8 millert 467: parsed_url:
468: } else { /* classic style `host:file' */
469: dir = strchr(host, ':');
470: }
1.1 millert 471: if (EMPTYSTRING(host)) {
472: rval = argpos + 1;
473: continue;
474: }
475:
476: /*
1.9 millert 477: * If dir is NULL, the file wasn't specified
1.1 millert 478: * (URL looked something like ftp://host)
479: */
1.8 millert 480: if (dir != NULL)
481: *dir++ = '\0';
1.1 millert 482:
483: /*
484: * Extract the file and (if present) directory name.
485: */
486: if (! EMPTYSTRING(dir)) {
1.8 millert 487: cp = strrchr(dir, '/');
1.1 millert 488: if (cp != NULL) {
489: *cp++ = '\0';
490: file = cp;
491: } else {
492: file = dir;
493: dir = NULL;
494: }
495: }
496: if (debug)
1.10 deraadt 497: fprintf(ttyout, "user %s:%s host %s port %s dir %s file %s\n",
1.8 millert 498: user, pass, host, portnum, dir, file);
1.1 millert 499:
500: /*
501: * Set up the connection if we don't have one.
502: */
503: if (strcmp(host, lasthost) != 0) {
1.8 millert 504: int oautologin;
505:
1.5 millert 506: (void)strcpy(lasthost, host);
1.1 millert 507: if (connected)
508: disconnect(0, NULL);
509: xargv[0] = __progname;
510: xargv[1] = host;
511: xargv[2] = NULL;
512: xargc = 2;
1.8 millert 513: if (! EMPTYSTRING(portnum)) {
1.1 millert 514: xargv[2] = portnum;
515: xargv[3] = NULL;
516: xargc = 3;
517: }
1.8 millert 518: oautologin = autologin;
519: if (user != NULL)
520: autologin = 0;
1.1 millert 521: setpeer(xargc, xargv);
1.8 millert 522: autologin = oautologin;
523: if ((connected == 0) ||
524: ((connected == 1) && !login(host, user, pass)) ) {
525: warnx("Can't connect or login to host `%s'",
526: host);
1.1 millert 527: rval = argpos + 1;
528: continue;
529: }
530:
531: /* Always use binary transfers. */
532: setbinary(0, NULL);
533: }
1.8 millert 534: /* cd back to '/' */
535: xargv[0] = "cd";
536: xargv[1] = "/";
537: xargv[2] = NULL;
538: cd(2, xargv);
539: if (! dirchange) {
540: rval = argpos + 1;
541: continue;
1.1 millert 542: }
543:
1.4 millert 544: dirhasglob = filehasglob = 0;
545: if (doglob) {
546: if (! EMPTYSTRING(dir) &&
547: strpbrk(dir, "*?[]{}") != NULL)
548: dirhasglob = 1;
549: if (! EMPTYSTRING(file) &&
550: strpbrk(file, "*?[]{}") != NULL)
551: filehasglob = 1;
552: }
553:
1.1 millert 554: /* Change directories, if necessary. */
1.4 millert 555: if (! EMPTYSTRING(dir) && !dirhasglob) {
1.1 millert 556: xargv[0] = "cd";
557: xargv[1] = dir;
558: xargv[2] = NULL;
559: cd(2, xargv);
560: if (! dirchange) {
561: rval = argpos + 1;
562: continue;
563: }
564: }
565:
566: if (EMPTYSTRING(file)) {
567: rval = -1;
568: continue;
569: }
570:
571: if (!verbose)
1.10 deraadt 572: fprintf(ttyout, "Retrieving %s/%s\n", dir ? dir : "", file);
1.1 millert 573:
1.4 millert 574: if (dirhasglob) {
575: snprintf(rempath, sizeof(rempath), "%s/%s", dir, file);
576: file = rempath;
577: }
578:
579: /* Fetch the file(s). */
1.10 deraadt 580: xargc = 2;
1.1 millert 581: xargv[0] = "get";
582: xargv[1] = file;
583: xargv[2] = NULL;
1.4 millert 584: if (dirhasglob || filehasglob) {
585: int ointeractive;
586:
587: ointeractive = interactive;
588: interactive = 0;
589: xargv[0] = "mget";
1.10 deraadt 590: mget(xargc, xargv);
1.5 millert 591: interactive = ointeractive;
1.10 deraadt 592: } else {
593: if (fd != -1) {
594: xargv[2] = "-";
595: xargc++;
596: }
597: xargv[3] = NULL;
598: get(xargc, xargv);
599: }
1.1 millert 600:
1.4 millert 601: if ((code / 100) != COMPLETE)
1.1 millert 602: rval = argpos + 1;
603: }
604: if (connected && rval != -1)
605: disconnect(0, NULL);
606: return (rval);
607: }