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