Annotation of src/usr.bin/ftp/fetch.c, Revision 1.43
1.43 ! millert 1: /* $OpenBSD: fetch.c,v 1.42 2002/12/30 23:04:42 deraadt 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.43 ! millert 41: static char rcsid[] = "$OpenBSD: fetch.c,v 1.42 2002/12/30 23:04:42 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>
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>
1.40 fgsch 69: #include <util.h>
1.1 millert 70:
71: #include "ftp_var.h"
72:
1.38 millert 73: static int url_get(const char *, const char *, const char *);
74: void aborthttp(int);
75: void abortfile(int);
1.42 deraadt 76: char hextochar(const char *);
77: char *urldecode(const char *);
1.14 millert 78:
1.1 millert 79: #define FTP_URL "ftp://" /* ftp URL prefix */
80: #define HTTP_URL "http://" /* http URL prefix */
1.22 deraadt 81: #define FILE_URL "file:" /* file URL prefix */
1.6 millert 82: #define FTP_PROXY "ftp_proxy" /* env var with ftp proxy location */
1.1 millert 83: #define HTTP_PROXY "http_proxy" /* env var with http proxy location */
84:
85:
86: #define EMPTYSTRING(x) ((x) == NULL || (*(x) == '\0'))
87:
1.37 heko 88: static const char *at_encoding_warning =
89: "Extra `@' characters in usernames and passwords should be encoded as %%40";
90:
1.1 millert 91: jmp_buf httpabort;
92:
93: /*
1.6 millert 94: * Retrieve URL, via the proxy in $proxyvar if necessary.
1.1 millert 95: * Modifies the string argument given.
96: * Returns -1 on failure, 0 on success
97: */
1.14 millert 98: static int
1.17 millert 99: url_get(origline, proxyenv, outfile)
1.14 millert 100: const char *origline;
101: const char *proxyenv;
1.17 millert 102: const char *outfile;
1.1 millert 103: {
1.25 itojun 104: struct addrinfo hints, *res0, *res;
105: int error;
1.40 fgsch 106: int i, isftpurl, isfileurl, isredirect;
1.34 millert 107: volatile int s, out;
1.14 millert 108: size_t len;
1.40 fgsch 109: char *cp, *ep, *portnum, *path;
1.29 itojun 110: char pbuf[NI_MAXSERV];
1.34 millert 111: const char * volatile savefile;
1.40 fgsch 112: char *line, *host, *port, *buf;
1.34 millert 113: char * volatile proxy;
1.28 itojun 114: char *hosttail;
1.14 millert 115: volatile sig_t oldintr;
1.1 millert 116: off_t hashbytes;
1.25 itojun 117: char *cause = "unknown";
1.40 fgsch 118: FILE *fin;
119: int rval;
1.1 millert 120:
121: s = -1;
122: proxy = NULL;
1.40 fgsch 123: fin = NULL;
124: buf = NULL;
1.14 millert 125: isftpurl = 0;
1.22 deraadt 126: isfileurl = 0;
1.40 fgsch 127: isredirect = 0;
128: rval = -1;
1.14 millert 129:
130: line = strdup(origline);
131: if (line == NULL)
132: errx(1, "Can't allocate memory to parse URL");
1.7 millert 133: if (strncasecmp(line, HTTP_URL, sizeof(HTTP_URL) - 1) == 0)
1.6 millert 134: host = line + sizeof(HTTP_URL) - 1;
1.14 millert 135: else if (strncasecmp(line, FTP_URL, sizeof(FTP_URL) - 1) == 0) {
1.6 millert 136: host = line + sizeof(FTP_URL) - 1;
1.14 millert 137: isftpurl = 1;
1.22 deraadt 138: } else if (strncasecmp(line, FILE_URL, sizeof(FILE_URL) - 1) == 0) {
139: host = line + sizeof(FILE_URL) - 1;
140: isfileurl = 1;
1.14 millert 141: } else
142: errx(1, "url_get: Invalid URL '%s'", line);
1.6 millert 143:
1.22 deraadt 144: if (isfileurl) {
145: path = host;
146: } else {
147: path = strchr(host, '/'); /* find path */
148: if (EMPTYSTRING(path)) {
149: if (isftpurl)
150: goto noftpautologin;
151: warnx("Invalid URL (no `/' after host): %s", origline);
152: goto cleanup_url_get;
153: }
154: *path++ = '\0';
155: if (EMPTYSTRING(path)) {
156: if (isftpurl)
157: goto noftpautologin;
158: warnx("Invalid URL (no file after host): %s", origline);
159: goto cleanup_url_get;
160: }
1.14 millert 161: }
1.1 millert 162:
1.17 millert 163: if (outfile)
164: savefile = outfile;
1.1 millert 165: else
1.17 millert 166: savefile = basename(path);
167:
1.14 millert 168: if (EMPTYSTRING(savefile)) {
169: if (isftpurl)
170: goto noftpautologin;
171: warnx("Invalid URL (no file after directory): %s", origline);
1.6 millert 172: goto cleanup_url_get;
1.14 millert 173: }
1.1 millert 174:
175: if (proxyenv != NULL) { /* use proxy */
176: proxy = strdup(proxyenv);
177: if (proxy == NULL)
1.14 millert 178: errx(1, "Can't allocate memory for proxy URL.");
1.7 millert 179: if (strncasecmp(proxy, HTTP_URL, sizeof(HTTP_URL) - 1) == 0)
1.6 millert 180: host = proxy + sizeof(HTTP_URL) - 1;
1.7 millert 181: else if (strncasecmp(proxy, FTP_URL, sizeof(FTP_URL) - 1) == 0)
1.6 millert 182: host = proxy + sizeof(FTP_URL) - 1;
183: else {
1.14 millert 184: warnx("Malformed proxy URL: %s", proxyenv);
1.6 millert 185: goto cleanup_url_get;
186: }
1.14 millert 187: if (EMPTYSTRING(host)) {
188: warnx("Malformed proxy URL: %s", proxyenv);
1.6 millert 189: goto cleanup_url_get;
1.14 millert 190: }
1.1 millert 191: *--path = '/'; /* add / back to real path */
192: path = strchr(host, '/'); /* remove trailing / on host */
1.42 deraadt 193: if (!EMPTYSTRING(path))
1.1 millert 194: *path++ = '\0';
195: path = line;
196: }
197:
1.22 deraadt 198: if (isfileurl) {
199: struct stat st;
200:
201: s = open(path, O_RDONLY);
202: if (s == -1) {
203: warn("Can't open file %s", path);
204: goto cleanup_url_get;
205: }
206:
207: if (fstat(s, &st) == -1)
208: filesize = -1;
209: else
210: filesize = st.st_size;
211:
212: /* Open the output file. */
213: if (strcmp(savefile, "-") != 0) {
214: out = open(savefile, O_CREAT | O_WRONLY | O_TRUNC, 0666);
215: if (out < 0) {
216: warn("Can't open %s", savefile);
217: goto cleanup_url_get;
218: }
219: } else
220: out = fileno(stdout);
221:
222: /* Trap signals */
223: oldintr = NULL;
224: if (setjmp(httpabort)) {
225: if (oldintr)
226: (void)signal(SIGINT, oldintr);
227: goto cleanup_url_get;
228: }
229: oldintr = signal(SIGINT, abortfile);
1.42 deraadt 230:
1.22 deraadt 231: bytes = 0;
232: hashbytes = mark;
233: progressmeter(-1);
1.40 fgsch 234:
235: if ((buf = malloc(4096)) == NULL)
236: errx(1, "Can't allocate memory for transfer buffer\n");
1.42 deraadt 237:
1.22 deraadt 238: /* Finally, suck down the file. */
239: i = 0;
1.40 fgsch 240: while ((len = read(s, buf, 4096)) > 0) {
1.22 deraadt 241: bytes += len;
242: for (cp = buf; len > 0; len -= i, cp += i) {
243: if ((i = write(out, cp, len)) == -1) {
244: warn("Writing %s", savefile);
245: goto cleanup_url_get;
246: }
247: else if (i == 0)
248: break;
249: }
250: if (hash && !progress) {
251: while (bytes >= hashbytes) {
252: (void)putc('#', ttyout);
253: hashbytes += mark;
254: }
255: (void)fflush(ttyout);
256: }
257: }
258: if (hash && !progress && bytes > 0) {
259: if (bytes < mark)
260: (void)putc('#', ttyout);
261: (void)putc('\n', ttyout);
262: (void)fflush(ttyout);
263: }
264: if (len != 0) {
265: warn("Reading from file");
266: goto cleanup_url_get;
267: }
268: progressmeter(1);
269: if (verbose)
270: fputs("Successfully retrieved file.\n", ttyout);
271: (void)signal(SIGINT, oldintr);
1.42 deraadt 272:
1.40 fgsch 273: rval = 0;
274: goto cleanup_url_get;
1.22 deraadt 275: }
276:
1.28 itojun 277: if (*host == '[' && (hosttail = strrchr(host, ']')) != NULL &&
278: (hosttail[1] == '\0' || hosttail[1] == ':')) {
279: host++;
280: *hosttail++ = '\0';
281: } else
282: hosttail = host;
283:
284: portnum = strrchr(hosttail, ':'); /* find portnum */
1.1 millert 285: if (portnum != NULL)
286: *portnum++ = '\0';
287:
288: if (debug)
1.10 deraadt 289: fprintf(ttyout, "host %s, port %s, path %s, save as %s.\n",
1.1 millert 290: host, portnum, path, savefile);
291:
1.25 itojun 292: memset(&hints, 0, sizeof(hints));
1.39 deraadt 293: hints.ai_family = family;
1.25 itojun 294: hints.ai_socktype = SOCK_STREAM;
295: port = portnum ? portnum : httpport;
296: error = getaddrinfo(host, port, &hints, &res0);
1.30 deraadt 297: if (error == EAI_SERVICE && port == httpport) {
298: /*
299: * If the services file is corrupt/missing, fall back
300: * on our hard-coded defines.
301: */
302: snprintf(pbuf, sizeof(pbuf), "%d", HTTP_PORT);
303: error = getaddrinfo(host, pbuf, &hints, &res0);
304: }
1.25 itojun 305: if (error) {
306: warnx("%s: %s", gai_strerror(error), host);
307: goto cleanup_url_get;
1.1 millert 308: }
309:
1.25 itojun 310: s = -1;
311: for (res = res0; res; res = res->ai_next) {
1.40 fgsch 312: getnameinfo(res->ai_addr, res->ai_addrlen, pbuf, sizeof(pbuf),
1.41 deraadt 313: NULL, 0, NI_NUMERICHOST);
314: if (verbose)
315: fprintf(ttyout, "Trying %s...\n", pbuf);
1.14 millert 316:
1.25 itojun 317: s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
318: if (s == -1) {
319: cause = "socket";
320: continue;
1.1 millert 321: }
322:
1.25 itojun 323: again:
324: if (connect(s, res->ai_addr, res->ai_addrlen) < 0) {
325: if (errno == EINTR)
326: goto again;
327: close(s);
328: s = -1;
329: cause = "connect";
1.19 deraadt 330: continue;
331: }
1.25 itojun 332:
1.29 itojun 333: /* get port in numeric */
334: if (getnameinfo(res->ai_addr, res->ai_addrlen, NULL, 0,
335: pbuf, sizeof(pbuf), NI_NUMERICSERV) == 0)
336: port = pbuf;
337: else
338: port = NULL;
339:
1.25 itojun 340: break;
341: }
342: freeaddrinfo(res0);
343: if (s < 0) {
1.33 millert 344: warn("%s", cause);
1.6 millert 345: goto cleanup_url_get;
1.1 millert 346: }
347:
1.40 fgsch 348: fin = fdopen(s, "r+");
349:
1.1 millert 350: /*
1.40 fgsch 351: * Construct and send the request. Proxy requests don't want leading /.
1.1 millert 352: */
1.32 itojun 353: if (proxy) {
354: /*
355: * Host: directive must use the destination host address for
356: * the original URI (path). We do not attach it at this moment.
357: */
358: if (verbose)
1.23 deraadt 359: fprintf(ttyout, "Requesting %s (via %s)\n",
360: origline, proxyenv);
1.40 fgsch 361: fprintf(fin, "GET %s HTTP/1.0\r\n%s\r\n\r\n", path, HTTP_USER_AGENT);
1.28 itojun 362: } else {
1.32 itojun 363: if (verbose)
364: fprintf(ttyout, "Requesting %s\n", origline);
365: if (strchr(host, ':')) {
366: char *h, *p;
367:
368: /* strip off scoped address portion, since it's local to node */
369: h = strdup(host);
370: if (h == NULL)
371: errx(1, "Can't allocate memory.");
372: if ((p = strchr(h, '%')) != NULL)
373: *p = '\0';
1.35 heko 374: /*
375: * Send port number only if it's specified and does not equal
376: * 80. Some broken HTTP servers get confused if you explicitly
377: * send them the port number.
378: */
379: if (port && strcmp(port, "80") != 0)
1.42 deraadt 380: fprintf(fin,
381: "GET /%s HTTP/1.0\r\nHost: [%s]:%s\r\n%s\r\n\r\n",
1.36 heko 382: path, h, port, HTTP_USER_AGENT);
1.35 heko 383: else
1.42 deraadt 384: fprintf(fin,
385: "GET /%s HTTP/1.0\r\nHost: [%s]\r\n%s\r\n\r\n",
1.36 heko 386: path, h, HTTP_USER_AGENT);
1.32 itojun 387: free(h);
388: } else {
1.35 heko 389: if (port && strcmp(port, "80") != 0)
1.42 deraadt 390: fprintf(fin,
391: "GET /%s HTTP/1.0\r\nHost: %s:%s\r\n%s\r\n\r\n",
1.36 heko 392: path, host, port, HTTP_USER_AGENT);
1.35 heko 393: else
1.42 deraadt 394: fprintf(fin,
395: "GET /%s HTTP/1.0\r\nHost: %s\r\n%s\r\n\r\n",
1.36 heko 396: path, host, HTTP_USER_AGENT);
1.32 itojun 397: }
1.28 itojun 398: }
1.40 fgsch 399: if (fflush(fin) == EOF) {
1.14 millert 400: warn("Writing HTTP request");
1.6 millert 401: goto cleanup_url_get;
1.1 millert 402: }
1.40 fgsch 403:
404: if ((buf = fparseln(fin, &len, NULL, "\0\0\0", 0)) == NULL) {
405: warn("Receiving HTTP reply");
406: goto cleanup_url_get;
1.1 millert 407: }
1.40 fgsch 408:
409: while (len > 0 && (buf[len-1] == '\r' || buf[len-1] == '\n'))
410: buf[--len] = '\0';
411: if (debug)
412: fprintf(ttyout, "received '%s'\n", buf);
413:
1.1 millert 414: cp = strchr(buf, ' ');
415: if (cp == NULL)
416: goto improper;
417: else
418: cp++;
1.40 fgsch 419: if (strncmp(cp, "301", 3) == 0 || strncmp(cp, "302", 3) == 0) {
420: isredirect++;
421: } else if (strncmp(cp, "200", 3)) {
1.1 millert 422: warnx("Error retrieving file: %s", cp);
1.6 millert 423: goto cleanup_url_get;
1.1 millert 424: }
425:
426: /*
427: * Read the rest of the header.
428: */
1.40 fgsch 429: free(buf);
430: filesize = -1;
431:
432: while (1) {
433: if ((buf = fparseln(fin, &len, NULL, "\0\0\0", 0)) == NULL) {
434: warn("Receiving HTTP reply");
435: goto cleanup_url_get;
436: }
437: while (len > 0 && (buf[len-1] == '\r' || buf[len-1] == '\n'))
438: buf[--len] = '\0';
439: if (len == 0)
1.1 millert 440: break;
1.40 fgsch 441: if (debug)
442: fprintf(ttyout, "received '%s'\n", buf);
1.1 millert 443:
1.40 fgsch 444: /* Look for some headers */
445: cp = buf;
1.1 millert 446: #define CONTENTLEN "Content-Length: "
1.40 fgsch 447: if (strncasecmp(cp, CONTENTLEN, sizeof(CONTENTLEN) - 1) == 0) {
448: cp += sizeof(CONTENTLEN) - 1;
449: filesize = strtol(cp, &ep, 10);
450: if (filesize < 1 || *ep != '\0')
451: goto improper;
452: #define LOCATION "Location: "
453: } else if (isredirect &&
454: strncasecmp(cp, LOCATION, sizeof(LOCATION) - 1) == 0) {
455: cp += sizeof(LOCATION) - 1;
456: if (verbose)
457: fprintf(ttyout, "Redirected to %s\n", cp);
458: if (fin != NULL)
459: fclose(fin);
460: else if (s != -1)
461: close(s);
462: if (proxy)
463: free(proxy);
464: free(line);
465: rval = url_get(cp, proxyenv, outfile);
466: if (buf)
467: free(buf);
468: return (rval);
469: }
1.1 millert 470: }
471:
1.17 millert 472: /* Open the output file. */
473: if (strcmp(savefile, "-") != 0) {
1.10 deraadt 474: out = open(savefile, O_CREAT | O_WRONLY | O_TRUNC, 0666);
475: if (out < 0) {
476: warn("Can't open %s", savefile);
477: goto cleanup_url_get;
478: }
479: } else
1.17 millert 480: out = fileno(stdout);
1.1 millert 481:
482: /* Trap signals */
483: oldintr = NULL;
484: if (setjmp(httpabort)) {
485: if (oldintr)
1.2 millert 486: (void)signal(SIGINT, oldintr);
1.6 millert 487: goto cleanup_url_get;
1.1 millert 488: }
489: oldintr = signal(SIGINT, aborthttp);
490:
491: bytes = 0;
492: hashbytes = mark;
493: progressmeter(-1);
1.43 ! millert 494:
! 495: free(buf);
1.1 millert 496:
497: /* Finally, suck down the file. */
1.40 fgsch 498: if ((buf = malloc(4096)) == NULL)
499: errx(1, "Can't allocate memory for transfer buffer\n");
1.1 millert 500: i = 0;
1.40 fgsch 501: while ((len = fread(buf, sizeof(char), 4096, fin)) > 0) {
1.1 millert 502: bytes += len;
503: for (cp = buf; len > 0; len -= i, cp += i) {
504: if ((i = write(out, cp, len)) == -1) {
505: warn("Writing %s", savefile);
1.6 millert 506: goto cleanup_url_get;
1.1 millert 507: }
508: else if (i == 0)
509: break;
510: }
511: if (hash && !progress) {
512: while (bytes >= hashbytes) {
1.10 deraadt 513: (void)putc('#', ttyout);
1.1 millert 514: hashbytes += mark;
515: }
1.10 deraadt 516: (void)fflush(ttyout);
1.1 millert 517: }
518: }
519: if (hash && !progress && bytes > 0) {
520: if (bytes < mark)
1.10 deraadt 521: (void)putc('#', ttyout);
522: (void)putc('\n', ttyout);
523: (void)fflush(ttyout);
1.1 millert 524: }
525: if (len != 0) {
526: warn("Reading from socket");
1.6 millert 527: goto cleanup_url_get;
1.1 millert 528: }
529: progressmeter(1);
1.24 deraadt 530: if (filesize != -1 && len == 0 && bytes != filesize) {
531: if (verbose)
532: fputs("Read short file.\n", ttyout);
533: goto cleanup_url_get;
534: }
535:
1.1 millert 536: if (verbose)
1.10 deraadt 537: fputs("Successfully retrieved file.\n", ttyout);
1.2 millert 538: (void)signal(SIGINT, oldintr);
1.1 millert 539:
1.40 fgsch 540: rval = 0;
541: goto cleanup_url_get;
1.1 millert 542:
1.14 millert 543: noftpautologin:
544: warnx(
545: "Auto-login using ftp URLs isn't supported when using $ftp_proxy");
546: goto cleanup_url_get;
547:
1.1 millert 548: improper:
1.8 millert 549: warnx("Improper response from %s", host);
1.14 millert 550:
1.6 millert 551: cleanup_url_get:
1.40 fgsch 552: if (fin != NULL)
553: fclose(fin);
554: else if (s != -1)
1.1 millert 555: close(s);
1.40 fgsch 556: if (buf)
557: free(buf);
1.1 millert 558: if (proxy)
559: free(proxy);
1.14 millert 560: free(line);
1.40 fgsch 561: return (rval);
1.1 millert 562: }
563:
564: /*
565: * Abort a http retrieval
566: */
567: void
1.3 millert 568: aborthttp(notused)
569: int notused;
1.1 millert 570: {
571:
572: alarmtimer(0);
1.10 deraadt 573: fputs("\nhttp fetch aborted.\n", ttyout);
574: (void)fflush(ttyout);
1.1 millert 575: longjmp(httpabort, 1);
576: }
577:
578: /*
1.22 deraadt 579: * Abort a http retrieval
580: */
581: void
582: abortfile(notused)
583: int notused;
584: {
585:
586: alarmtimer(0);
587: fputs("\nfile fetch aborted.\n", ttyout);
588: (void)fflush(ttyout);
589: longjmp(httpabort, 1);
590: }
591:
592: /*
1.1 millert 593: * Retrieve multiple files from the command line, transferring
594: * files of the form "host:path", "ftp://host/path" using the
595: * ftp protocol, and files of the form "http://host/path" using
596: * the http protocol.
1.2 millert 597: * If path has a trailing "/", then return (-1);
1.1 millert 598: * the path will be cd-ed into and the connection remains open,
599: * and the function will return -1 (to indicate the connection
600: * is alive).
601: * If an error occurs the return value will be the offset+1 in
602: * argv[] of the file that caused a problem (i.e, argv[x]
603: * returns x+1)
604: * Otherwise, 0 is returned if all files retrieved successfully.
605: */
606: int
1.17 millert 607: auto_fetch(argc, argv, outfile)
1.1 millert 608: int argc;
609: char *argv[];
1.17 millert 610: char *outfile;
1.1 millert 611: {
612: static char lasthost[MAXHOSTNAMELEN];
613: char *xargv[5];
614: char *cp, *line, *host, *dir, *file, *portnum;
1.8 millert 615: char *user, *pass;
1.6 millert 616: char *ftpproxy, *httpproxy;
1.14 millert 617: int rval, xargc;
618: volatile int argpos;
1.4 millert 619: int dirhasglob, filehasglob;
1.14 millert 620: char rempath[MAXPATHLEN];
1.1 millert 621:
622: argpos = 0;
623:
624: if (setjmp(toplevel)) {
625: if (connected)
626: disconnect(0, NULL);
1.2 millert 627: return (argpos + 1);
1.1 millert 628: }
1.3 millert 629: (void)signal(SIGINT, (sig_t)intr);
630: (void)signal(SIGPIPE, (sig_t)lostpeer);
1.1 millert 631:
1.6 millert 632: ftpproxy = getenv(FTP_PROXY);
633: httpproxy = getenv(HTTP_PROXY);
634:
1.1 millert 635: /*
636: * Loop through as long as there's files to fetch.
637: */
638: for (rval = 0; (rval == 0) && (argpos < argc); free(line), argpos++) {
639: if (strchr(argv[argpos], ':') == NULL)
640: break;
1.8 millert 641: host = dir = file = portnum = user = pass = NULL;
1.1 millert 642:
643: /*
644: * We muck with the string, so we make a copy.
645: */
646: line = strdup(argv[argpos]);
647: if (line == NULL)
648: errx(1, "Can't allocate memory for auto-fetch.");
649:
650: /*
651: * Try HTTP URL-style arguments first.
652: */
1.22 deraadt 653: if (strncasecmp(line, HTTP_URL, sizeof(HTTP_URL) - 1) == 0 ||
654: strncasecmp(line, FILE_URL, sizeof(FILE_URL) - 1) == 0) {
1.17 millert 655: if (url_get(line, httpproxy, outfile) == -1)
1.1 millert 656: rval = argpos + 1;
657: continue;
658: }
659:
660: /*
1.6 millert 661: * Try FTP URL-style arguments next. If ftpproxy is
662: * set, use url_get() instead of standard ftp.
663: * Finally, try host:file.
1.1 millert 664: */
665: host = line;
1.7 millert 666: if (strncasecmp(line, FTP_URL, sizeof(FTP_URL) - 1) == 0) {
1.37 heko 667: char *passend, *passagain, *userend;
1.31 itojun 668:
1.6 millert 669: if (ftpproxy) {
1.17 millert 670: if (url_get(line, ftpproxy, outfile) == -1)
1.6 millert 671: rval = argpos + 1;
672: continue;
673: }
1.1 millert 674: host += sizeof(FTP_URL) - 1;
1.8 millert 675: dir = strchr(host, '/');
1.1 millert 676:
1.8 millert 677: /* Look for [user:pass@]host[:port] */
1.31 itojun 678:
679: /* check if we have "user:pass@" */
1.37 heko 680: userend = strchr(host, ':');
1.31 itojun 681: passend = strchr(host, '@');
682: if (passend && userend && userend < passend &&
683: (!dir || passend < dir)) {
684: user = host;
685: pass = userend + 1;
686: host = passend + 1;
687: *userend = *passend = '\0';
1.37 heko 688: passagain = strchr(host, '@');
1.42 deraadt 689: if (strchr(pass, '@') != NULL ||
1.37 heko 690: (passagain != NULL && passagain < dir)) {
691: warnx(at_encoding_warning);
692: goto bad_ftp_url;
1.42 deraadt 693: }
1.31 itojun 694:
695: if (EMPTYSTRING(user) || EMPTYSTRING(pass)) {
1.11 millert 696: bad_ftp_url:
1.31 itojun 697: warnx("Invalid URL: %s", argv[argpos]);
698: rval = argpos + 1;
699: continue;
700: }
1.37 heko 701: user = urldecode(user);
702: pass = urldecode(pass);
1.8 millert 703: }
1.31 itojun 704:
705: #ifdef INET6
706: /* check [host]:port, or [host] */
707: if (host[0] == '[') {
708: cp = strchr(host, ']');
709: if (cp && (!dir || cp < dir)) {
710: if (cp + 1 == dir || cp[1] == ':') {
711: host++;
712: *cp++ = '\0';
713: } else
714: cp = NULL;
715: } else
716: cp = host;
1.25 itojun 717: } else
718: cp = host;
1.31 itojun 719: #else
720: cp = host;
1.25 itojun 721: #endif
1.31 itojun 722:
723: /* split off host[:port] if there is */
724: if (cp) {
725: portnum = strchr(cp, ':');
726: if (!portnum)
727: ;
728: else {
729: if (!dir)
730: ;
731: else if (portnum + 1 < dir) {
732: *portnum++ = '\0';
733: /*
734: * XXX should check if portnum
735: * is decimal number
736: */
737: } else {
738: /* empty portnum */
739: goto bad_ftp_url;
740: }
741: }
742: } else
743: portnum = NULL;
1.8 millert 744: } else { /* classic style `host:file' */
745: dir = strchr(host, ':');
746: }
1.1 millert 747: if (EMPTYSTRING(host)) {
748: rval = argpos + 1;
749: continue;
750: }
751:
752: /*
1.9 millert 753: * If dir is NULL, the file wasn't specified
1.1 millert 754: * (URL looked something like ftp://host)
755: */
1.8 millert 756: if (dir != NULL)
757: *dir++ = '\0';
1.1 millert 758:
759: /*
760: * Extract the file and (if present) directory name.
761: */
1.42 deraadt 762: if (!EMPTYSTRING(dir)) {
1.8 millert 763: cp = strrchr(dir, '/');
1.1 millert 764: if (cp != NULL) {
765: *cp++ = '\0';
766: file = cp;
767: } else {
768: file = dir;
769: dir = NULL;
770: }
771: }
772: if (debug)
1.42 deraadt 773: fprintf(ttyout,
774: "user %s:%s host %s port %s dir %s file %s\n",
1.8 millert 775: user, pass, host, portnum, dir, file);
1.1 millert 776:
777: /*
778: * Set up the connection if we don't have one.
779: */
780: if (strcmp(host, lasthost) != 0) {
1.8 millert 781: int oautologin;
782:
1.5 millert 783: (void)strcpy(lasthost, host);
1.1 millert 784: if (connected)
785: disconnect(0, NULL);
786: xargv[0] = __progname;
787: xargv[1] = host;
788: xargv[2] = NULL;
789: xargc = 2;
1.42 deraadt 790: if (!EMPTYSTRING(portnum)) {
1.1 millert 791: xargv[2] = portnum;
792: xargv[3] = NULL;
793: xargc = 3;
794: }
1.8 millert 795: oautologin = autologin;
796: if (user != NULL)
797: autologin = 0;
1.1 millert 798: setpeer(xargc, xargv);
1.8 millert 799: autologin = oautologin;
800: if ((connected == 0) ||
1.40 fgsch 801: ((connected == 1) && !ftp_login(host, user, pass))) {
1.8 millert 802: warnx("Can't connect or login to host `%s'",
803: host);
1.1 millert 804: rval = argpos + 1;
805: continue;
806: }
807:
808: /* Always use binary transfers. */
809: setbinary(0, NULL);
810: }
1.8 millert 811: /* cd back to '/' */
812: xargv[0] = "cd";
813: xargv[1] = "/";
814: xargv[2] = NULL;
815: cd(2, xargv);
1.42 deraadt 816: if (!dirchange) {
1.8 millert 817: rval = argpos + 1;
818: continue;
1.1 millert 819: }
820:
1.4 millert 821: dirhasglob = filehasglob = 0;
822: if (doglob) {
1.42 deraadt 823: if (!EMPTYSTRING(dir) &&
1.4 millert 824: strpbrk(dir, "*?[]{}") != NULL)
825: dirhasglob = 1;
1.42 deraadt 826: if (!EMPTYSTRING(file) &&
1.4 millert 827: strpbrk(file, "*?[]{}") != NULL)
828: filehasglob = 1;
829: }
830:
1.1 millert 831: /* Change directories, if necessary. */
1.42 deraadt 832: if (!EMPTYSTRING(dir) && !dirhasglob) {
1.1 millert 833: xargv[0] = "cd";
834: xargv[1] = dir;
835: xargv[2] = NULL;
836: cd(2, xargv);
1.42 deraadt 837: if (!dirchange) {
1.1 millert 838: rval = argpos + 1;
839: continue;
840: }
841: }
842:
843: if (EMPTYSTRING(file)) {
844: rval = -1;
845: continue;
846: }
847:
1.21 marc 848: if (verbose)
1.10 deraadt 849: fprintf(ttyout, "Retrieving %s/%s\n", dir ? dir : "", file);
1.1 millert 850:
1.4 millert 851: if (dirhasglob) {
852: snprintf(rempath, sizeof(rempath), "%s/%s", dir, file);
853: file = rempath;
854: }
855:
856: /* Fetch the file(s). */
1.10 deraadt 857: xargc = 2;
1.1 millert 858: xargv[0] = "get";
859: xargv[1] = file;
860: xargv[2] = NULL;
1.4 millert 861: if (dirhasglob || filehasglob) {
862: int ointeractive;
863:
864: ointeractive = interactive;
865: interactive = 0;
866: xargv[0] = "mget";
1.10 deraadt 867: mget(xargc, xargv);
1.5 millert 868: interactive = ointeractive;
1.10 deraadt 869: } else {
1.17 millert 870: if (outfile != NULL) {
871: xargv[2] = outfile;
872: xargv[3] = NULL;
1.10 deraadt 873: xargc++;
874: }
875: get(xargc, xargv);
876: }
1.1 millert 877:
1.4 millert 878: if ((code / 100) != COMPLETE)
1.1 millert 879: rval = argpos + 1;
880: }
881: if (connected && rval != -1)
882: disconnect(0, NULL);
883: return (rval);
1.37 heko 884: }
885:
886: char *
1.42 deraadt 887: urldecode(str)
1.37 heko 888: const char *str;
889: {
890: char *ret;
891: char c;
892: int i, reallen;
893:
1.42 deraadt 894: if (str == NULL)
1.37 heko 895: return NULL;
896: if ((ret = malloc(strlen(str)+1)) == NULL)
897: err(1, "Can't allocate memory for URL decoding");
898: for (i = 0, reallen = 0; str[i] != '\0'; i++, reallen++, ret++) {
899: c = str[i];
900: if (c == '+') {
901: *ret = ' ';
902: continue;
903: }
904: /* Can't use strtol here because next char after %xx may be
905: * a digit. */
906: if (c == '%' && isxdigit(str[i+1]) && isxdigit(str[i+2])) {
907: *ret = hextochar(&str[i+1]);
908: i+=2;
909: continue;
910: }
911: *ret = c;
912: }
913: *ret = '\0';
914:
915: return ret-reallen;
916: }
917:
918: char
919: hextochar(str)
920: const char *str;
921: {
922: char c, ret;
923:
924: c = str[0];
925: ret = c;
926: if (isalpha(c))
927: ret -= isupper(c) ? 'A' - 10 : 'a' - 10;
928: else
929: ret -= '0';
930: ret *= 16;
931:
932: c = str[1];
933: ret += c;
934: if (isalpha(c))
935: ret -= isupper(c) ? 'A' - 10 : 'a' - 10;
936: else
937: ret -= '0';
938: return ret;
1.25 itojun 939: }
940:
941: int
942: isurl(p)
943: const char *p;
944: {
1.27 millert 945:
1.26 deraadt 946: if (strncasecmp(p, FTP_URL, sizeof(FTP_URL) - 1) == 0 ||
947: strncasecmp(p, HTTP_URL, sizeof(HTTP_URL) - 1) == 0 ||
1.27 millert 948: strncasecmp(p, FILE_URL, sizeof(FILE_URL) - 1) == 0 ||
949: strstr(p, ":/"))
950: return (1);
951: return (0);
1.1 millert 952: }