Annotation of src/usr.bin/ftp/fetch.c, Revision 1.78
1.78 ! martynas 1: /* $OpenBSD: fetch.c,v 1.77 2008/06/15 04:43:20 martynas 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.78 ! martynas 41: static const char rcsid[] = "$OpenBSD: fetch.c,v 1.77 2008/06/15 04:43:20 martynas 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>
1.78 ! martynas 77: #else /* !SMALL */
1.61 deraadt 78: #define SSL void
1.78 ! martynas 79: #endif /* !SMALL */
1.61 deraadt 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 *);
1.78 ! martynas 95: #endif /* !SMALL */
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;
1.78 ! martynas 140: #endif /* !SMALL */
1.61 deraadt 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;
1.78 ! martynas 159: #endif /* !SMALL */
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: }
1.78 ! martynas 192: #endif /* !SMALL */
1.75 martynas 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: }
1.78 ! martynas 209: #endif /* !SMALL */
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
1.78 ! martynas 273: #endif /* !SMALL */
1.75 martynas 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: }
1.78 ! martynas 295: #endif /* !SMALL */
1.75 martynas 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);
1.78 ! martynas 372: #else /* !SMALL */
1.25 itojun 373: port = portnum ? portnum : httpport;
1.78 ! martynas 374: #endif /* !SMALL */
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);
1.78 ! martynas 387: #endif /* !SMALL */
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);
1.78 ! martynas 432: #endif /* !SMALL */
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: }
1.78 ! martynas 468: #else /* !SMALL */
1.40 fgsch 469: fin = fdopen(s, "r+");
1.78 ! martynas 470: #endif /* !SMALL */
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);
1.78 ! martynas 479: #endif /* !SMALL */
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" :
1.78 ! martynas 499: #endif /* !SMALL */
1.75 martynas 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.78 ! martynas 541: #else /* !SMALL */
1.55 fgsch 542: if (port && strcmp(port, "80") != 0)
1.61 deraadt 543: ftp_printf(fin, ssl, ":%s", port);
1.78 ! martynas 544: #endif /* !SMALL */
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);
1.78 ! martynas 554: #endif /* !SMALL */
1.74 pyr 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.78 ! martynas 589: #endif /* !SMALL */
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;
1.78 ! martynas 604: #endif /* !SMALL */
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;
1.78 ! martynas 640: #endif /* !SMALL */
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
1.78 ! martynas 665: #endif /* !SMALL */
1.75 martynas 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 &&
1.78 ! martynas 728: #endif /* !SMALL */
1.75 martynas 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: }
1.78 ! martynas 756: #endif /* !SMALL */
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.78 ! martynas 858: #endif /* !SMALL */
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.77 martynas 901: if (EMPTYSTRING(username)) {
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.76 martynas 987: username, pass ? "XXXX" : NULL, host, portnum,
988: dir, file);
1.1 millert 989:
990: /*
1.49 krw 991: * Set up the connection.
1.1 millert 992: */
1.49 krw 993: if (connected)
994: disconnect(0, NULL);
995: xargv[0] = __progname;
996: xargv[1] = host;
1.8 millert 997: xargv[2] = NULL;
1.49 krw 998: xargc = 2;
999: if (!EMPTYSTRING(portnum)) {
1000: xargv[2] = portnum;
1001: xargv[3] = NULL;
1002: xargc = 3;
1003: }
1004: oautologin = autologin;
1.62 ray 1005: if (username != NULL)
1.49 krw 1006: autologin = 0;
1007: setpeer(xargc, xargv);
1008: autologin = oautologin;
1009: if ((connected == 0) ||
1.62 ray 1010: ((connected == 1) && !ftp_login(host, username, pass))) {
1.49 krw 1011: warnx("Can't connect or login to host `%s'", host);
1.8 millert 1012: rval = argpos + 1;
1013: continue;
1.1 millert 1014: }
1.49 krw 1015:
1016: /* Always use binary transfers. */
1017: setbinary(0, NULL);
1.1 millert 1018:
1.4 millert 1019: dirhasglob = filehasglob = 0;
1020: if (doglob) {
1.42 deraadt 1021: if (!EMPTYSTRING(dir) &&
1.4 millert 1022: strpbrk(dir, "*?[]{}") != NULL)
1023: dirhasglob = 1;
1.42 deraadt 1024: if (!EMPTYSTRING(file) &&
1.4 millert 1025: strpbrk(file, "*?[]{}") != NULL)
1026: filehasglob = 1;
1027: }
1028:
1.1 millert 1029: /* Change directories, if necessary. */
1.42 deraadt 1030: if (!EMPTYSTRING(dir) && !dirhasglob) {
1.1 millert 1031: xargv[0] = "cd";
1032: xargv[1] = dir;
1033: xargv[2] = NULL;
1034: cd(2, xargv);
1.42 deraadt 1035: if (!dirchange) {
1.1 millert 1036: rval = argpos + 1;
1037: continue;
1038: }
1039: }
1040:
1041: if (EMPTYSTRING(file)) {
1042: rval = -1;
1043: continue;
1044: }
1045:
1.21 marc 1046: if (verbose)
1.10 deraadt 1047: fprintf(ttyout, "Retrieving %s/%s\n", dir ? dir : "", file);
1.1 millert 1048:
1.4 millert 1049: if (dirhasglob) {
1050: snprintf(rempath, sizeof(rempath), "%s/%s", dir, file);
1051: file = rempath;
1052: }
1053:
1054: /* Fetch the file(s). */
1.10 deraadt 1055: xargc = 2;
1.1 millert 1056: xargv[0] = "get";
1057: xargv[1] = file;
1058: xargv[2] = NULL;
1.4 millert 1059: if (dirhasglob || filehasglob) {
1060: int ointeractive;
1061:
1062: ointeractive = interactive;
1063: interactive = 0;
1064: xargv[0] = "mget";
1.78 ! martynas 1065: #ifndef SMALL
! 1066: if (resume) {
! 1067: xargc = 3;
! 1068: xargv[1] = "-c";
! 1069: xargv[2] = file;
! 1070: xargv[3] = NULL;
! 1071: }
! 1072: #endif /* !SMALL */
1.10 deraadt 1073: mget(xargc, xargv);
1.5 millert 1074: interactive = ointeractive;
1.10 deraadt 1075: } else {
1.17 millert 1076: if (outfile != NULL) {
1077: xargv[2] = outfile;
1078: xargv[3] = NULL;
1.10 deraadt 1079: xargc++;
1080: }
1.75 martynas 1081: #ifndef SMALL
1082: if (resume)
1083: reget(xargc, xargv);
1084: else
1.78 ! martynas 1085: #endif /* !SMALL */
1.75 martynas 1086: get(xargc, xargv);
1.10 deraadt 1087: }
1.1 millert 1088:
1.4 millert 1089: if ((code / 100) != COMPLETE)
1.1 millert 1090: rval = argpos + 1;
1091: }
1092: if (connected && rval != -1)
1093: disconnect(0, NULL);
1094: return (rval);
1.37 heko 1095: }
1096:
1097: char *
1.50 deraadt 1098: urldecode(const char *str)
1.37 heko 1099: {
1.53 deraadt 1100: char *ret, c;
1101: int i, reallen;
1.37 heko 1102:
1.53 deraadt 1103: if (str == NULL)
1104: return NULL;
1105: if ((ret = malloc(strlen(str)+1)) == NULL)
1106: err(1, "Can't allocate memory for URL decoding");
1107: for (i = 0, reallen = 0; str[i] != '\0'; i++, reallen++, ret++) {
1108: c = str[i];
1109: if (c == '+') {
1110: *ret = ' ';
1111: continue;
1112: }
1.61 deraadt 1113:
1114: /* Cannot use strtol here because next char
1115: * after %xx may be a digit.
1116: */
1.53 deraadt 1117: if (c == '%' && isxdigit(str[i+1]) && isxdigit(str[i+2])) {
1118: *ret = hextochar(&str[i+1]);
1119: i+=2;
1120: continue;
1121: }
1122: *ret = c;
1123: }
1124: *ret = '\0';
1125:
1126: return ret-reallen;
1.37 heko 1127: }
1128:
1129: char
1.50 deraadt 1130: hextochar(const char *str)
1.37 heko 1131: {
1.53 deraadt 1132: char c, ret;
1.37 heko 1133:
1.53 deraadt 1134: c = str[0];
1135: ret = c;
1136: if (isalpha(c))
1137: ret -= isupper(c) ? 'A' - 10 : 'a' - 10;
1138: else
1139: ret -= '0';
1140: ret *= 16;
1141:
1142: c = str[1];
1143: ret += c;
1144: if (isalpha(c))
1145: ret -= isupper(c) ? 'A' - 10 : 'a' - 10;
1146: else
1147: ret -= '0';
1148: return ret;
1.25 itojun 1149: }
1150:
1151: int
1.50 deraadt 1152: isurl(const char *p)
1.25 itojun 1153: {
1.27 millert 1154:
1.26 deraadt 1155: if (strncasecmp(p, FTP_URL, sizeof(FTP_URL) - 1) == 0 ||
1156: strncasecmp(p, HTTP_URL, sizeof(HTTP_URL) - 1) == 0 ||
1.61 deraadt 1157: #ifndef SMALL
1158: strncasecmp(p, HTTPS_URL, sizeof(HTTPS_URL) - 1) == 0 ||
1.78 ! martynas 1159: #endif /* !SMALL */
1.27 millert 1160: strncasecmp(p, FILE_URL, sizeof(FILE_URL) - 1) == 0 ||
1161: strstr(p, ":/"))
1162: return (1);
1163: return (0);
1.1 millert 1164: }
1.61 deraadt 1165:
1166: char *
1167: ftp_readline(FILE *fp, SSL *ssl, size_t *lenp)
1168: {
1169: if (fp != NULL)
1170: return fparseln(fp, lenp, NULL, "\0\0\0", 0);
1171: #ifndef SMALL
1172: else if (ssl != NULL)
1173: return SSL_readline(ssl, lenp);
1.78 ! martynas 1174: #endif /* !SMALL */
1.61 deraadt 1175: else
1176: return NULL;
1177: }
1178:
1.65 ray 1179: size_t
1.61 deraadt 1180: ftp_read(FILE *fp, SSL *ssl, char *buf, size_t len)
1181: {
1.65 ray 1182: size_t ret;
1.61 deraadt 1183: if (fp != NULL)
1184: ret = fread(buf, sizeof(char), len, fp);
1185: #ifndef SMALL
1.65 ray 1186: else if (ssl != NULL) {
1187: int nr;
1188:
1189: if (len > INT_MAX)
1190: len = INT_MAX;
1191: if ((nr = SSL_read(ssl, buf, (int)len)) <= 0)
1192: ret = 0;
1193: else
1194: ret = nr;
1195: }
1.78 ! martynas 1196: #endif /* !SMALL */
1.61 deraadt 1197: else
1198: ret = 0;
1199: return (ret);
1200: }
1201:
1202: int
1203: ftp_printf(FILE *fp, SSL *ssl, const char *fmt, ...)
1204: {
1205: int ret;
1206: va_list ap;
1207:
1208: va_start(ap, fmt);
1209:
1210: if (fp != NULL)
1211: ret = vfprintf(fp, fmt, ap);
1212: #ifndef SMALL
1213: else if (ssl != NULL)
1214: ret = SSL_vprintf((SSL*)ssl, fmt, ap);
1.78 ! martynas 1215: #endif /* !SMALL */
1.61 deraadt 1216: else
1217: ret = NULL;
1218:
1219: va_end(ap);
1220: return (ret);
1221: }
1222:
1223: #ifndef SMALL
1224: int
1225: SSL_vprintf(SSL *ssl, const char *fmt, va_list ap)
1226: {
1227: int ret;
1228: char *string;
1229:
1230: if ((ret = vasprintf(&string, fmt, ap)) == -1)
1231: return ret;
1.64 ray 1232: ret = SSL_write(ssl, string, ret);
1.61 deraadt 1233: free(string);
1234: return ret;
1235: }
1236:
1237: char *
1238: SSL_readline(SSL *ssl, size_t *lenp)
1239: {
1.63 ray 1240: size_t i, len;
1.61 deraadt 1241: char *buf, *q, c;
1242:
1243: len = 128;
1244: if ((buf = malloc(len)) == NULL)
1245: errx(1, "Can't allocate memory for transfer buffer");
1246: for (i = 0; ; i++) {
1247: if (i >= len - 1) {
1248: if ((q = realloc(buf, 2 * len)) == NULL)
1249: errx(1, "Can't expand transfer buffer");
1250: buf = q;
1251: len *= 2;
1252: }
1253: if (SSL_read(ssl, &c, 1) <= 0)
1254: break;
1255: buf[i] = c;
1256: if (c == '\n')
1257: break;
1258: }
1259: *lenp = i;
1260: return (buf);
1261: }
1262:
1263: int
1264: proxy_connect(int socket, char *host)
1265: {
1.66 ray 1266: int l;
1.61 deraadt 1267: char buf[1024];
1268: char *connstr, *hosttail, *port;
1269:
1270: if (*host == '[' && (hosttail = strrchr(host, ']')) != NULL &&
1271: (hosttail[1] == '\0' || hosttail[1] == ':')) {
1272: host++;
1273: *hosttail++ = '\0';
1274: } else
1275: hosttail = host;
1276:
1277: port = strrchr(hosttail, ':'); /* find portnum */
1278: if (port != NULL)
1279: *port++ = '\0';
1280: if (!port)
1281: port = "443";
1282:
1.66 ray 1283: l = asprintf(&connstr, "CONNECT %s:%s HTTP/1.1\n\n", host, port);
1284: if (l == -1)
1.61 deraadt 1285: errx(1, "Could not allocate memory to assemble connect string!");
1.68 ray 1286: if (debug)
1287: printf("%s", connstr);
1.66 ray 1288: if (write(socket, connstr, l) != l)
1.68 ray 1289: err(1, "Could not send connect string");
1.61 deraadt 1290: read(socket, &buf, sizeof(buf)); /* only proxy header XXX: error handling? */
1.71 ray 1291: free(connstr);
1.61 deraadt 1292: return(200);
1293: }
1.78 ! martynas 1294: #endif /* !SMALL */