Annotation of src/usr.bin/ftp/fetch.c, Revision 1.42
1.42 ! deraadt 1: /* $OpenBSD: fetch.c,v 1.41 2002/12/19 01:52:09 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.42 ! deraadt 41: static char rcsid[] = "$OpenBSD: fetch.c,v 1.41 2002/12/19 01:52:09 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: }
1.40 fgsch 471: free(buf);
1.1 millert 472:
1.17 millert 473: /* Open the output file. */
474: if (strcmp(savefile, "-") != 0) {
1.10 deraadt 475: out = open(savefile, O_CREAT | O_WRONLY | O_TRUNC, 0666);
476: if (out < 0) {
477: warn("Can't open %s", savefile);
478: goto cleanup_url_get;
479: }
480: } else
1.17 millert 481: out = fileno(stdout);
1.1 millert 482:
483: /* Trap signals */
484: oldintr = NULL;
485: if (setjmp(httpabort)) {
486: if (oldintr)
1.2 millert 487: (void)signal(SIGINT, oldintr);
1.6 millert 488: goto cleanup_url_get;
1.1 millert 489: }
490: oldintr = signal(SIGINT, aborthttp);
491:
492: bytes = 0;
493: hashbytes = mark;
494: progressmeter(-1);
495:
496: /* Finally, suck down the file. */
1.40 fgsch 497: if ((buf = malloc(4096)) == NULL)
498: errx(1, "Can't allocate memory for transfer buffer\n");
1.1 millert 499: i = 0;
1.40 fgsch 500: while ((len = fread(buf, sizeof(char), 4096, fin)) > 0) {
1.1 millert 501: bytes += len;
502: for (cp = buf; len > 0; len -= i, cp += i) {
503: if ((i = write(out, cp, len)) == -1) {
504: warn("Writing %s", savefile);
1.6 millert 505: goto cleanup_url_get;
1.1 millert 506: }
507: else if (i == 0)
508: break;
509: }
510: if (hash && !progress) {
511: while (bytes >= hashbytes) {
1.10 deraadt 512: (void)putc('#', ttyout);
1.1 millert 513: hashbytes += mark;
514: }
1.10 deraadt 515: (void)fflush(ttyout);
1.1 millert 516: }
517: }
518: if (hash && !progress && bytes > 0) {
519: if (bytes < mark)
1.10 deraadt 520: (void)putc('#', ttyout);
521: (void)putc('\n', ttyout);
522: (void)fflush(ttyout);
1.1 millert 523: }
524: if (len != 0) {
525: warn("Reading from socket");
1.6 millert 526: goto cleanup_url_get;
1.1 millert 527: }
528: progressmeter(1);
1.24 deraadt 529: if (filesize != -1 && len == 0 && bytes != filesize) {
530: if (verbose)
531: fputs("Read short file.\n", ttyout);
532: goto cleanup_url_get;
533: }
534:
1.1 millert 535: if (verbose)
1.10 deraadt 536: fputs("Successfully retrieved file.\n", ttyout);
1.2 millert 537: (void)signal(SIGINT, oldintr);
1.1 millert 538:
1.40 fgsch 539: rval = 0;
540: goto cleanup_url_get;
1.1 millert 541:
1.14 millert 542: noftpautologin:
543: warnx(
544: "Auto-login using ftp URLs isn't supported when using $ftp_proxy");
545: goto cleanup_url_get;
546:
1.1 millert 547: improper:
1.8 millert 548: warnx("Improper response from %s", host);
1.14 millert 549:
1.6 millert 550: cleanup_url_get:
1.40 fgsch 551: if (fin != NULL)
552: fclose(fin);
553: else if (s != -1)
1.1 millert 554: close(s);
1.40 fgsch 555: if (buf)
556: free(buf);
1.1 millert 557: if (proxy)
558: free(proxy);
1.14 millert 559: free(line);
1.40 fgsch 560: return (rval);
1.1 millert 561: }
562:
563: /*
564: * Abort a http retrieval
565: */
566: void
1.3 millert 567: aborthttp(notused)
568: int notused;
1.1 millert 569: {
570:
571: alarmtimer(0);
1.10 deraadt 572: fputs("\nhttp fetch aborted.\n", ttyout);
573: (void)fflush(ttyout);
1.1 millert 574: longjmp(httpabort, 1);
575: }
576:
577: /*
1.22 deraadt 578: * Abort a http retrieval
579: */
580: void
581: abortfile(notused)
582: int notused;
583: {
584:
585: alarmtimer(0);
586: fputs("\nfile fetch aborted.\n", ttyout);
587: (void)fflush(ttyout);
588: longjmp(httpabort, 1);
589: }
590:
591: /*
1.1 millert 592: * Retrieve multiple files from the command line, transferring
593: * files of the form "host:path", "ftp://host/path" using the
594: * ftp protocol, and files of the form "http://host/path" using
595: * the http protocol.
1.2 millert 596: * If path has a trailing "/", then return (-1);
1.1 millert 597: * the path will be cd-ed into and the connection remains open,
598: * and the function will return -1 (to indicate the connection
599: * is alive).
600: * If an error occurs the return value will be the offset+1 in
601: * argv[] of the file that caused a problem (i.e, argv[x]
602: * returns x+1)
603: * Otherwise, 0 is returned if all files retrieved successfully.
604: */
605: int
1.17 millert 606: auto_fetch(argc, argv, outfile)
1.1 millert 607: int argc;
608: char *argv[];
1.17 millert 609: char *outfile;
1.1 millert 610: {
611: static char lasthost[MAXHOSTNAMELEN];
612: char *xargv[5];
613: char *cp, *line, *host, *dir, *file, *portnum;
1.8 millert 614: char *user, *pass;
1.6 millert 615: char *ftpproxy, *httpproxy;
1.14 millert 616: int rval, xargc;
617: volatile int argpos;
1.4 millert 618: int dirhasglob, filehasglob;
1.14 millert 619: char rempath[MAXPATHLEN];
1.1 millert 620:
621: argpos = 0;
622:
623: if (setjmp(toplevel)) {
624: if (connected)
625: disconnect(0, NULL);
1.2 millert 626: return (argpos + 1);
1.1 millert 627: }
1.3 millert 628: (void)signal(SIGINT, (sig_t)intr);
629: (void)signal(SIGPIPE, (sig_t)lostpeer);
1.1 millert 630:
1.6 millert 631: ftpproxy = getenv(FTP_PROXY);
632: httpproxy = getenv(HTTP_PROXY);
633:
1.1 millert 634: /*
635: * Loop through as long as there's files to fetch.
636: */
637: for (rval = 0; (rval == 0) && (argpos < argc); free(line), argpos++) {
638: if (strchr(argv[argpos], ':') == NULL)
639: break;
1.8 millert 640: host = dir = file = portnum = user = pass = NULL;
1.1 millert 641:
642: /*
643: * We muck with the string, so we make a copy.
644: */
645: line = strdup(argv[argpos]);
646: if (line == NULL)
647: errx(1, "Can't allocate memory for auto-fetch.");
648:
649: /*
650: * Try HTTP URL-style arguments first.
651: */
1.22 deraadt 652: if (strncasecmp(line, HTTP_URL, sizeof(HTTP_URL) - 1) == 0 ||
653: strncasecmp(line, FILE_URL, sizeof(FILE_URL) - 1) == 0) {
1.17 millert 654: if (url_get(line, httpproxy, outfile) == -1)
1.1 millert 655: rval = argpos + 1;
656: continue;
657: }
658:
659: /*
1.6 millert 660: * Try FTP URL-style arguments next. If ftpproxy is
661: * set, use url_get() instead of standard ftp.
662: * Finally, try host:file.
1.1 millert 663: */
664: host = line;
1.7 millert 665: if (strncasecmp(line, FTP_URL, sizeof(FTP_URL) - 1) == 0) {
1.37 heko 666: char *passend, *passagain, *userend;
1.31 itojun 667:
1.6 millert 668: if (ftpproxy) {
1.17 millert 669: if (url_get(line, ftpproxy, outfile) == -1)
1.6 millert 670: rval = argpos + 1;
671: continue;
672: }
1.1 millert 673: host += sizeof(FTP_URL) - 1;
1.8 millert 674: dir = strchr(host, '/');
1.1 millert 675:
1.8 millert 676: /* Look for [user:pass@]host[:port] */
1.31 itojun 677:
678: /* check if we have "user:pass@" */
1.37 heko 679: userend = strchr(host, ':');
1.31 itojun 680: passend = strchr(host, '@');
681: if (passend && userend && userend < passend &&
682: (!dir || passend < dir)) {
683: user = host;
684: pass = userend + 1;
685: host = passend + 1;
686: *userend = *passend = '\0';
1.37 heko 687: passagain = strchr(host, '@');
1.42 ! deraadt 688: if (strchr(pass, '@') != NULL ||
1.37 heko 689: (passagain != NULL && passagain < dir)) {
690: warnx(at_encoding_warning);
691: goto bad_ftp_url;
1.42 ! deraadt 692: }
1.31 itojun 693:
694: if (EMPTYSTRING(user) || EMPTYSTRING(pass)) {
1.11 millert 695: bad_ftp_url:
1.31 itojun 696: warnx("Invalid URL: %s", argv[argpos]);
697: rval = argpos + 1;
698: continue;
699: }
1.37 heko 700: user = urldecode(user);
701: pass = urldecode(pass);
1.8 millert 702: }
1.31 itojun 703:
704: #ifdef INET6
705: /* check [host]:port, or [host] */
706: if (host[0] == '[') {
707: cp = strchr(host, ']');
708: if (cp && (!dir || cp < dir)) {
709: if (cp + 1 == dir || cp[1] == ':') {
710: host++;
711: *cp++ = '\0';
712: } else
713: cp = NULL;
714: } else
715: cp = host;
1.25 itojun 716: } else
717: cp = host;
1.31 itojun 718: #else
719: cp = host;
1.25 itojun 720: #endif
1.31 itojun 721:
722: /* split off host[:port] if there is */
723: if (cp) {
724: portnum = strchr(cp, ':');
725: if (!portnum)
726: ;
727: else {
728: if (!dir)
729: ;
730: else if (portnum + 1 < dir) {
731: *portnum++ = '\0';
732: /*
733: * XXX should check if portnum
734: * is decimal number
735: */
736: } else {
737: /* empty portnum */
738: goto bad_ftp_url;
739: }
740: }
741: } else
742: portnum = NULL;
1.8 millert 743: } else { /* classic style `host:file' */
744: dir = strchr(host, ':');
745: }
1.1 millert 746: if (EMPTYSTRING(host)) {
747: rval = argpos + 1;
748: continue;
749: }
750:
751: /*
1.9 millert 752: * If dir is NULL, the file wasn't specified
1.1 millert 753: * (URL looked something like ftp://host)
754: */
1.8 millert 755: if (dir != NULL)
756: *dir++ = '\0';
1.1 millert 757:
758: /*
759: * Extract the file and (if present) directory name.
760: */
1.42 ! deraadt 761: if (!EMPTYSTRING(dir)) {
1.8 millert 762: cp = strrchr(dir, '/');
1.1 millert 763: if (cp != NULL) {
764: *cp++ = '\0';
765: file = cp;
766: } else {
767: file = dir;
768: dir = NULL;
769: }
770: }
771: if (debug)
1.42 ! deraadt 772: fprintf(ttyout,
! 773: "user %s:%s host %s port %s dir %s file %s\n",
1.8 millert 774: user, pass, host, portnum, dir, file);
1.1 millert 775:
776: /*
777: * Set up the connection if we don't have one.
778: */
779: if (strcmp(host, lasthost) != 0) {
1.8 millert 780: int oautologin;
781:
1.5 millert 782: (void)strcpy(lasthost, host);
1.1 millert 783: if (connected)
784: disconnect(0, NULL);
785: xargv[0] = __progname;
786: xargv[1] = host;
787: xargv[2] = NULL;
788: xargc = 2;
1.42 ! deraadt 789: if (!EMPTYSTRING(portnum)) {
1.1 millert 790: xargv[2] = portnum;
791: xargv[3] = NULL;
792: xargc = 3;
793: }
1.8 millert 794: oautologin = autologin;
795: if (user != NULL)
796: autologin = 0;
1.1 millert 797: setpeer(xargc, xargv);
1.8 millert 798: autologin = oautologin;
799: if ((connected == 0) ||
1.40 fgsch 800: ((connected == 1) && !ftp_login(host, user, pass))) {
1.8 millert 801: warnx("Can't connect or login to host `%s'",
802: host);
1.1 millert 803: rval = argpos + 1;
804: continue;
805: }
806:
807: /* Always use binary transfers. */
808: setbinary(0, NULL);
809: }
1.8 millert 810: /* cd back to '/' */
811: xargv[0] = "cd";
812: xargv[1] = "/";
813: xargv[2] = NULL;
814: cd(2, xargv);
1.42 ! deraadt 815: if (!dirchange) {
1.8 millert 816: rval = argpos + 1;
817: continue;
1.1 millert 818: }
819:
1.4 millert 820: dirhasglob = filehasglob = 0;
821: if (doglob) {
1.42 ! deraadt 822: if (!EMPTYSTRING(dir) &&
1.4 millert 823: strpbrk(dir, "*?[]{}") != NULL)
824: dirhasglob = 1;
1.42 ! deraadt 825: if (!EMPTYSTRING(file) &&
1.4 millert 826: strpbrk(file, "*?[]{}") != NULL)
827: filehasglob = 1;
828: }
829:
1.1 millert 830: /* Change directories, if necessary. */
1.42 ! deraadt 831: if (!EMPTYSTRING(dir) && !dirhasglob) {
1.1 millert 832: xargv[0] = "cd";
833: xargv[1] = dir;
834: xargv[2] = NULL;
835: cd(2, xargv);
1.42 ! deraadt 836: if (!dirchange) {
1.1 millert 837: rval = argpos + 1;
838: continue;
839: }
840: }
841:
842: if (EMPTYSTRING(file)) {
843: rval = -1;
844: continue;
845: }
846:
1.21 marc 847: if (verbose)
1.10 deraadt 848: fprintf(ttyout, "Retrieving %s/%s\n", dir ? dir : "", file);
1.1 millert 849:
1.4 millert 850: if (dirhasglob) {
851: snprintf(rempath, sizeof(rempath), "%s/%s", dir, file);
852: file = rempath;
853: }
854:
855: /* Fetch the file(s). */
1.10 deraadt 856: xargc = 2;
1.1 millert 857: xargv[0] = "get";
858: xargv[1] = file;
859: xargv[2] = NULL;
1.4 millert 860: if (dirhasglob || filehasglob) {
861: int ointeractive;
862:
863: ointeractive = interactive;
864: interactive = 0;
865: xargv[0] = "mget";
1.10 deraadt 866: mget(xargc, xargv);
1.5 millert 867: interactive = ointeractive;
1.10 deraadt 868: } else {
1.17 millert 869: if (outfile != NULL) {
870: xargv[2] = outfile;
871: xargv[3] = NULL;
1.10 deraadt 872: xargc++;
873: }
874: get(xargc, xargv);
875: }
1.1 millert 876:
1.4 millert 877: if ((code / 100) != COMPLETE)
1.1 millert 878: rval = argpos + 1;
879: }
880: if (connected && rval != -1)
881: disconnect(0, NULL);
882: return (rval);
1.37 heko 883: }
884:
885: char *
1.42 ! deraadt 886: urldecode(str)
1.37 heko 887: const char *str;
888: {
889: char *ret;
890: char c;
891: int i, reallen;
892:
1.42 ! deraadt 893: if (str == NULL)
1.37 heko 894: return NULL;
895: if ((ret = malloc(strlen(str)+1)) == NULL)
896: err(1, "Can't allocate memory for URL decoding");
897: for (i = 0, reallen = 0; str[i] != '\0'; i++, reallen++, ret++) {
898: c = str[i];
899: if (c == '+') {
900: *ret = ' ';
901: continue;
902: }
903: /* Can't use strtol here because next char after %xx may be
904: * a digit. */
905: if (c == '%' && isxdigit(str[i+1]) && isxdigit(str[i+2])) {
906: *ret = hextochar(&str[i+1]);
907: i+=2;
908: continue;
909: }
910: *ret = c;
911: }
912: *ret = '\0';
913:
914: return ret-reallen;
915: }
916:
917: char
918: hextochar(str)
919: const char *str;
920: {
921: char c, ret;
922:
923: c = str[0];
924: ret = c;
925: if (isalpha(c))
926: ret -= isupper(c) ? 'A' - 10 : 'a' - 10;
927: else
928: ret -= '0';
929: ret *= 16;
930:
931: c = str[1];
932: ret += c;
933: if (isalpha(c))
934: ret -= isupper(c) ? 'A' - 10 : 'a' - 10;
935: else
936: ret -= '0';
937: return ret;
1.25 itojun 938: }
939:
940: int
941: isurl(p)
942: const char *p;
943: {
1.27 millert 944:
1.26 deraadt 945: if (strncasecmp(p, FTP_URL, sizeof(FTP_URL) - 1) == 0 ||
946: strncasecmp(p, HTTP_URL, sizeof(HTTP_URL) - 1) == 0 ||
1.27 millert 947: strncasecmp(p, FILE_URL, sizeof(FILE_URL) - 1) == 0 ||
948: strstr(p, ":/"))
949: return (1);
950: return (0);
1.1 millert 951: }