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