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