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