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