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