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