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