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