Annotation of src/usr.bin/ftp/fetch.c, Revision 1.109
1.109 ! sthen 1: /* $OpenBSD: fetch.c,v 1.108 2013/03/30 10:11:35 tobias Exp $ */
1.15 millert 2: /* $NetBSD: fetch.c,v 1.14 1997/08/18 10:20:20 lukem Exp $ */
1.1 millert 3:
4: /*-
5: * Copyright (c) 1997 The NetBSD Foundation, Inc.
6: * All rights reserved.
7: *
8: * This code is derived from software contributed to The NetBSD Foundation
9: * by Jason Thorpe and Luke Mewburn.
10: *
11: * Redistribution and use in source and binary forms, with or without
12: * modification, are permitted provided that the following conditions
13: * are met:
14: * 1. Redistributions of source code must retain the above copyright
15: * notice, this list of conditions and the following disclaimer.
16: * 2. Redistributions in binary form must reproduce the above copyright
17: * notice, this list of conditions and the following disclaimer in the
18: * documentation and/or other materials provided with the distribution.
19: *
20: * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22: * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1.15 millert 23: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
1.1 millert 25: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30: * POSSIBILITY OF SUCH DAMAGE.
31: */
32:
33: /*
34: * FTP User Program -- Command line file retrieval
35: */
36:
37: #include <sys/types.h>
38: #include <sys/param.h>
39: #include <sys/socket.h>
1.22 deraadt 40: #include <sys/stat.h>
1.1 millert 41:
42: #include <netinet/in.h>
43:
44: #include <arpa/ftp.h>
45: #include <arpa/inet.h>
46:
47: #include <ctype.h>
48: #include <err.h>
1.17 millert 49: #include <libgen.h>
1.58 grunk 50: #include <limits.h>
1.1 millert 51: #include <netdb.h>
52: #include <fcntl.h>
1.3 millert 53: #include <signal.h>
1.1 millert 54: #include <stdio.h>
1.61 deraadt 55: #include <stdarg.h>
1.19 deraadt 56: #include <errno.h>
1.1 millert 57: #include <stdlib.h>
58: #include <string.h>
59: #include <unistd.h>
1.40 fgsch 60: #include <util.h>
1.73 drahn 61: #include <resolv.h>
1.1 millert 62:
1.61 deraadt 63: #ifndef SMALL
64: #include <openssl/ssl.h>
65: #include <openssl/err.h>
1.78 martynas 66: #else /* !SMALL */
1.61 deraadt 67: #define SSL void
1.78 martynas 68: #endif /* !SMALL */
1.61 deraadt 69:
1.1 millert 70: #include "ftp_var.h"
1.86 martynas 71: #include "cmds.h"
1.1 millert 72:
1.38 millert 73: static int url_get(const char *, const char *, const char *);
74: void aborthttp(int);
75: void abortfile(int);
1.42 deraadt 76: char hextochar(const char *);
77: char *urldecode(const char *);
1.61 deraadt 78: int ftp_printf(FILE *, SSL *, const char *, ...) __attribute__((format(printf, 3, 4)));
79: char *ftp_readline(FILE *, SSL *, size_t *);
1.65 ray 80: size_t ftp_read(FILE *, SSL *, char *, size_t);
1.61 deraadt 81: #ifndef SMALL
1.81 espie 82: int proxy_connect(int, char *, char *);
1.61 deraadt 83: int SSL_vprintf(SSL *, const char *, va_list);
84: char *SSL_readline(SSL *, size_t *);
1.78 martynas 85: #endif /* !SMALL */
1.14 millert 86:
1.1 millert 87: #define FTP_URL "ftp://" /* ftp URL prefix */
88: #define HTTP_URL "http://" /* http URL prefix */
1.61 deraadt 89: #define HTTPS_URL "https://" /* https URL prefix */
1.22 deraadt 90: #define FILE_URL "file:" /* file URL prefix */
1.6 millert 91: #define FTP_PROXY "ftp_proxy" /* env var with ftp proxy location */
1.1 millert 92: #define HTTP_PROXY "http_proxy" /* env var with http proxy location */
93:
1.73 drahn 94: #define COOKIE_MAX_LEN 42
1.1 millert 95:
96: #define EMPTYSTRING(x) ((x) == NULL || (*(x) == '\0'))
97:
1.37 heko 98: static const char *at_encoding_warning =
1.53 deraadt 99: "Extra `@' characters in usernames and passwords should be encoded as %%40";
1.37 heko 100:
1.1 millert 101: jmp_buf httpabort;
102:
1.54 fgsch 103: static int redirect_loop;
104:
1.1 millert 105: /*
1.95 martynas 106: * Determine whether the character needs encoding, per RFC1738:
107: * - No corresponding graphic US-ASCII.
108: * - Unsafe characters.
109: */
110: static int
111: unsafe_char(const char *c)
112: {
113: const char *unsafe_chars = " <>\"#{}|\\^~[]`";
114:
115: /*
116: * No corresponding graphic US-ASCII.
117: * Control characters and octets not used in US-ASCII.
118: */
119: return (iscntrl(*c) || !isascii(*c) ||
120:
121: /*
122: * Unsafe characters.
123: * '%' is also unsafe, if is not followed by two
124: * hexadecimal digits.
125: */
126: strchr(unsafe_chars, *c) != NULL ||
127: (*c == '%' && (!isxdigit(*++c) || !isxdigit(*++c))));
128: }
129:
130: /*
131: * Encode given URL, per RFC1738.
132: * Allocate and return string to the caller.
133: */
134: static char *
135: url_encode(const char *path)
136: {
137: size_t i, length, new_length;
138: char *epath, *epathp;
139:
140: length = new_length = strlen(path);
141:
142: /*
143: * First pass:
144: * Count unsafe characters, and determine length of the
145: * final URL.
146: */
147: for (i = 0; i < length; i++)
148: if (unsafe_char(path + i))
149: new_length += 2;
150:
151: epath = epathp = malloc(new_length + 1); /* One more for '\0'. */
152: if (epath == NULL)
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());
609: ssl = SSL_new(ssl_ctx);
610: if (ssl == NULL || ssl_ctx == NULL) {
611: ERR_print_errors_fp(ttyout);
612: goto cleanup_url_get;
613: }
614: if (SSL_set_fd(ssl, s) == 0) {
615: ERR_print_errors_fp(ttyout);
616: goto cleanup_url_get;
617: }
618: if (SSL_connect(ssl) <= 0) {
619: ERR_print_errors_fp(ttyout);
1.72 ray 620: goto cleanup_url_get;
1.61 deraadt 621: }
622: } else {
623: fin = fdopen(s, "r+");
624: }
1.78 martynas 625: #else /* !SMALL */
1.40 fgsch 626: fin = fdopen(s, "r+");
1.78 martynas 627: #endif /* !SMALL */
1.40 fgsch 628:
1.55 fgsch 629: if (verbose)
630: fprintf(ttyout, "Requesting %s", origline);
1.95 martynas 631:
1.1 millert 632: /*
1.40 fgsch 633: * Construct and send the request. Proxy requests don't want leading /.
1.1 millert 634: */
1.74 pyr 635: #ifndef SMALL
636: cookie_get(host, path, ishttpsurl, &buf);
1.78 martynas 637: #endif /* !SMALL */
1.95 martynas 638:
639: epath = url_encode(path);
1.62 ray 640: if (proxyurl) {
1.55 fgsch 641: if (verbose)
1.81 espie 642: fprintf(ttyout, " (via %s)\n", proxyurl);
1.32 itojun 643: /*
644: * Host: directive must use the destination host address for
645: * the original URI (path). We do not attach it at this moment.
646: */
1.73 drahn 647: if (cookie)
648: ftp_printf(fin, ssl, "GET %s HTTP/1.0\r\n"
1.74 pyr 649: "Proxy-Authorization: Basic %s%s\r\n%s\r\n\r\n",
1.95 martynas 650: epath, cookie, buf ? buf : "", HTTP_USER_AGENT);
1.73 drahn 651: else
1.74 pyr 652: ftp_printf(fin, ssl, "GET %s HTTP/1.0\r\n%s%s\r\n\r\n",
1.95 martynas 653: epath, buf ? buf : "", HTTP_USER_AGENT);
1.73 drahn 654:
1.28 itojun 655: } else {
1.90 martynas 656: #ifndef SMALL
657: if (resume) {
658: struct stat stbuf;
659:
660: if (stat(savefile, &stbuf) == 0)
661: restart_point = stbuf.st_size;
662: else
663: restart_point = 0;
664: }
1.106 haesbaer 665: if (auth) {
666: ftp_printf(fin, ssl,
667: "GET /%s %s\r\nAuthorization: Basic %s\r\nHost: ",
668: epath, restart_point ?
669: "HTTP/1.1\r\nConnection: close" : "HTTP/1.0",
670: auth);
671: free(auth);
672: auth = NULL;
673: } else
674: #endif /* SMALL */
1.107 haesbaer 675: ftp_printf(fin, ssl, "GET /%s %s\r\nHost: ", epath,
676: #ifndef SMALL
677: restart_point ? "HTTP/1.1\r\nConnection: close" :
678: #endif /* !SMALL */
679: "HTTP/1.0");
1.32 itojun 680: if (strchr(host, ':')) {
1.55 fgsch 681: /*
682: * strip off scoped address portion, since it's
683: * local to node
684: */
1.32 itojun 685: h = strdup(host);
686: if (h == NULL)
687: errx(1, "Can't allocate memory.");
688: if ((p = strchr(h, '%')) != NULL)
689: *p = '\0';
1.61 deraadt 690: ftp_printf(fin, ssl, "[%s]", h);
1.32 itojun 691: free(h);
1.55 fgsch 692: } else
1.61 deraadt 693: ftp_printf(fin, ssl, "%s", host);
1.55 fgsch 694:
695: /*
696: * Send port number only if it's specified and does not equal
697: * 80. Some broken HTTP servers get confused if you explicitly
698: * send them the port number.
699: */
1.61 deraadt 700: #ifndef SMALL
701: if (port && strcmp(port, (ishttpsurl ? "443" : "80")) != 0)
702: ftp_printf(fin, ssl, ":%s", port);
1.90 martynas 703: if (restart_point)
1.75 martynas 704: ftp_printf(fin, ssl, "\r\nRange: bytes=%lld-",
705: (long long)restart_point);
1.78 martynas 706: #else /* !SMALL */
1.55 fgsch 707: if (port && strcmp(port, "80") != 0)
1.61 deraadt 708: ftp_printf(fin, ssl, ":%s", port);
1.78 martynas 709: #endif /* !SMALL */
1.74 pyr 710: ftp_printf(fin, ssl, "\r\n%s%s\r\n\r\n",
711: buf ? buf : "", HTTP_USER_AGENT);
1.55 fgsch 712: if (verbose)
713: fprintf(ttyout, "\n");
1.28 itojun 714: }
1.95 martynas 715: free(epath);
1.74 pyr 716:
717: #ifndef SMALL
718: free(buf);
1.78 martynas 719: #endif /* !SMALL */
1.74 pyr 720: buf = NULL;
721:
1.61 deraadt 722: if (fin != NULL && fflush(fin) == EOF) {
1.14 millert 723: warn("Writing HTTP request");
1.6 millert 724: goto cleanup_url_get;
1.1 millert 725: }
1.61 deraadt 726: if ((buf = ftp_readline(fin, ssl, &len)) == NULL) {
1.40 fgsch 727: warn("Receiving HTTP reply");
728: goto cleanup_url_get;
1.1 millert 729: }
1.40 fgsch 730:
731: while (len > 0 && (buf[len-1] == '\r' || buf[len-1] == '\n'))
732: buf[--len] = '\0';
1.80 martynas 733: #ifndef SMALL
1.40 fgsch 734: if (debug)
735: fprintf(ttyout, "received '%s'\n", buf);
1.80 martynas 736: #endif /* !SMALL */
1.40 fgsch 737:
1.1 millert 738: cp = strchr(buf, ' ');
739: if (cp == NULL)
740: goto improper;
741: else
742: cp++;
1.69 jsg 743:
744: strlcpy(ststr, cp, sizeof(ststr));
1.75 martynas 745: status = strtonum(ststr, 200, 416, &errstr);
1.69 jsg 746: if (errstr) {
747: warnx("Error retrieving file: %s", cp);
748: goto cleanup_url_get;
749: }
750:
751: switch (status) {
752: case 200: /* OK */
1.75 martynas 753: #ifndef SMALL
1.109 ! sthen 754: /*
1.98 phessler 755: * When we request a partial file, and we receive an HTTP 200
756: * it is a good indication that the server doesn't support
757: * range requests, and is about to send us the entire file.
758: * If the restart_point == 0, then we are not actually
759: * requesting a partial file, and an HTTP 200 is appropriate.
760: */
761: if (resume && restart_point != 0) {
762: warnx("Server does not support resume.");
763: restart_point = resume = 0;
764: }
1.101 halex 765: /* FALLTHROUGH */
1.75 martynas 766: case 206: /* Partial Content */
1.91 halex 767: #endif /* !SMALL */
1.69 jsg 768: break;
769: case 301: /* Moved Permanently */
770: case 302: /* Found */
771: case 303: /* See Other */
772: case 307: /* Temporary Redirect */
1.40 fgsch 773: isredirect++;
1.54 fgsch 774: if (redirect_loop++ > 10) {
775: warnx("Too many redirections requested");
776: goto cleanup_url_get;
777: }
1.69 jsg 778: break;
1.75 martynas 779: #ifndef SMALL
780: case 416: /* Requested Range Not Satisfiable */
781: warnx("File is already fully retrieved.");
782: goto cleanup_url_get;
1.78 martynas 783: #endif /* !SMALL */
1.69 jsg 784: default:
1.1 millert 785: warnx("Error retrieving file: %s", cp);
1.6 millert 786: goto cleanup_url_get;
1.1 millert 787: }
788:
789: /*
790: * Read the rest of the header.
791: */
1.40 fgsch 792: free(buf);
793: filesize = -1;
794:
1.62 ray 795: for (;;) {
1.61 deraadt 796: if ((buf = ftp_readline(fin, ssl, &len)) == NULL) {
1.40 fgsch 797: warn("Receiving HTTP reply");
798: goto cleanup_url_get;
799: }
1.61 deraadt 800:
1.40 fgsch 801: while (len > 0 && (buf[len-1] == '\r' || buf[len-1] == '\n'))
802: buf[--len] = '\0';
803: if (len == 0)
1.1 millert 804: break;
1.80 martynas 805: #ifndef SMALL
1.40 fgsch 806: if (debug)
807: fprintf(ttyout, "received '%s'\n", buf);
1.80 martynas 808: #endif /* !SMALL */
1.1 millert 809:
1.40 fgsch 810: /* Look for some headers */
811: cp = buf;
1.1 millert 812: #define CONTENTLEN "Content-Length: "
1.40 fgsch 813: if (strncasecmp(cp, CONTENTLEN, sizeof(CONTENTLEN) - 1) == 0) {
1.104 sthen 814: size_t s;
1.40 fgsch 815: cp += sizeof(CONTENTLEN) - 1;
1.104 sthen 816: if (s=strcspn(cp, " \t"))
817: *(cp+s) = 0;
1.58 grunk 818: filesize = strtonum(cp, 0, LLONG_MAX, &errstr);
819: if (errstr != NULL)
1.40 fgsch 820: goto improper;
1.75 martynas 821: #ifndef SMALL
1.90 martynas 822: if (restart_point)
1.75 martynas 823: filesize += restart_point;
1.78 martynas 824: #endif /* !SMALL */
1.40 fgsch 825: #define LOCATION "Location: "
826: } else if (isredirect &&
827: strncasecmp(cp, LOCATION, sizeof(LOCATION) - 1) == 0) {
828: cp += sizeof(LOCATION) - 1;
1.102 halex 829: if (strstr(cp, "://") == NULL) {
830: #ifdef SMALL
831: errx(1, "Relative redirect not supported");
832: #else /* SMALL */
833: if (*cp == '/') {
834: locbase = NULL;
835: cp++;
836: } else {
837: locbase = strdup(path);
838: if (locbase == NULL)
839: errx(1, "Can't allocate memory"
840: " for location base");
841: loctail = strchr(locbase, '#');
842: if (loctail != NULL)
843: *loctail = '\0';
844: loctail = strchr(locbase, '?');
845: if (loctail != NULL)
846: *loctail = '\0';
847: loctail = strrchr(locbase, '/');
848: if (loctail == NULL) {
849: free(locbase);
850: locbase = NULL;
851: } else
852: loctail[1] = '\0';
853: }
854: /* Contruct URL from relative redirect */
855: if (asprintf(&redirurl, "%s%s%s%s/%s%s",
856: scheme, full_host,
857: portnum ? ":" : "",
858: portnum ? portnum : "",
859: locbase ? locbase : "",
860: cp) == -1)
861: errx(1, "Cannot build "
862: "redirect URL");
863: free(locbase);
864: #endif /* SMALL */
865: } else if ((redirurl = strdup(cp)) == NULL)
866: errx(1, "Cannot allocate memory for URL");
867: loctail = strchr(redirurl, '#');
868: if (loctail != NULL)
869: *loctail = '\0';
1.40 fgsch 870: if (verbose)
1.102 halex 871: fprintf(ttyout, "Redirected to %s\n", redirurl);
1.40 fgsch 872: if (fin != NULL)
873: fclose(fin);
874: else if (s != -1)
875: close(s);
1.102 halex 876: rval = url_get(redirurl, proxyenv, savefile);
877: free(redirurl);
878: goto cleanup_url_get;
1.40 fgsch 879: }
1.108 tobias 880: free(buf);
1.1 millert 881: }
882:
1.17 millert 883: /* Open the output file. */
1.100 halex 884: if (!pipeout) {
1.75 martynas 885: #ifndef SMALL
886: if (resume)
1.83 martynas 887: out = open(savefile, O_CREAT | O_WRONLY | O_APPEND,
888: 0666);
1.75 martynas 889: else
1.78 martynas 890: #endif /* !SMALL */
1.75 martynas 891: out = open(savefile, O_CREAT | O_WRONLY | O_TRUNC,
892: 0666);
1.10 deraadt 893: if (out < 0) {
894: warn("Can't open %s", savefile);
895: goto cleanup_url_get;
896: }
897: } else
1.17 millert 898: out = fileno(stdout);
1.1 millert 899:
900: /* Trap signals */
901: oldintr = NULL;
1.97 martynas 902: oldinti = NULL;
1.1 millert 903: if (setjmp(httpabort)) {
904: if (oldintr)
1.2 millert 905: (void)signal(SIGINT, oldintr);
1.97 martynas 906: if (oldinti)
907: (void)signal(SIGINFO, oldinti);
1.6 millert 908: goto cleanup_url_get;
1.1 millert 909: }
910: oldintr = signal(SIGINT, aborthttp);
911:
912: bytes = 0;
913: hashbytes = mark;
1.84 martynas 914: progressmeter(-1, path);
1.43 millert 915:
916: free(buf);
1.1 millert 917:
918: /* Finally, suck down the file. */
1.40 fgsch 919: if ((buf = malloc(4096)) == NULL)
1.47 deraadt 920: errx(1, "Can't allocate memory for transfer buffer");
1.1 millert 921: i = 0;
1.61 deraadt 922: len = 1;
1.97 martynas 923: oldinti = signal(SIGINFO, psummary);
1.61 deraadt 924: while (len > 0) {
925: len = ftp_read(fin, ssl, buf, 4096);
1.1 millert 926: bytes += len;
1.61 deraadt 927: for (cp = buf, wlen = len; wlen > 0; wlen -= i, cp += i) {
928: if ((i = write(out, cp, wlen)) == -1) {
1.1 millert 929: warn("Writing %s", savefile);
1.97 martynas 930: signal(SIGINFO, oldinti);
1.6 millert 931: goto cleanup_url_get;
1.1 millert 932: }
933: else if (i == 0)
934: break;
935: }
936: if (hash && !progress) {
937: while (bytes >= hashbytes) {
1.10 deraadt 938: (void)putc('#', ttyout);
1.1 millert 939: hashbytes += mark;
940: }
1.10 deraadt 941: (void)fflush(ttyout);
1.1 millert 942: }
943: }
1.97 martynas 944: signal(SIGINFO, oldinti);
1.1 millert 945: if (hash && !progress && bytes > 0) {
946: if (bytes < mark)
1.10 deraadt 947: (void)putc('#', ttyout);
948: (void)putc('\n', ttyout);
949: (void)fflush(ttyout);
1.1 millert 950: }
951: if (len != 0) {
952: warn("Reading from socket");
1.6 millert 953: goto cleanup_url_get;
1.1 millert 954: }
1.84 martynas 955: progressmeter(1, NULL);
1.75 martynas 956: if (
957: #ifndef SMALL
958: !resume &&
1.78 martynas 959: #endif /* !SMALL */
1.75 martynas 960: filesize != -1 && len == 0 && bytes != filesize) {
1.24 deraadt 961: if (verbose)
962: fputs("Read short file.\n", ttyout);
963: goto cleanup_url_get;
964: }
965:
1.1 millert 966: if (verbose)
1.97 martynas 967: ptransfer(0);
1.2 millert 968: (void)signal(SIGINT, oldintr);
1.1 millert 969:
1.40 fgsch 970: rval = 0;
971: goto cleanup_url_get;
1.1 millert 972:
1.14 millert 973: noftpautologin:
974: warnx(
975: "Auto-login using ftp URLs isn't supported when using $ftp_proxy");
976: goto cleanup_url_get;
977:
1.1 millert 978: improper:
1.8 millert 979: warnx("Improper response from %s", host);
1.14 millert 980:
1.6 millert 981: cleanup_url_get:
1.61 deraadt 982: #ifndef SMALL
983: if (ssl) {
984: SSL_shutdown(ssl);
985: SSL_free(ssl);
986: }
1.102 halex 987: free(full_host);
1.106 haesbaer 988: free(auth);
1.78 martynas 989: #endif /* !SMALL */
1.40 fgsch 990: if (fin != NULL)
991: fclose(fin);
992: else if (s != -1)
1.1 millert 993: close(s);
1.67 steven 994: free(buf);
995: free(proxyurl);
1.62 ray 996: free(newline);
1.81 espie 997: free(cookie);
1.40 fgsch 998: return (rval);
1.1 millert 999: }
1000:
1001: /*
1002: * Abort a http retrieval
1003: */
1.51 deraadt 1004: /* ARGSUSED */
1.1 millert 1005: void
1.51 deraadt 1006: aborthttp(int signo)
1.1 millert 1007: {
1008:
1009: alarmtimer(0);
1.10 deraadt 1010: fputs("\nhttp fetch aborted.\n", ttyout);
1011: (void)fflush(ttyout);
1.1 millert 1012: longjmp(httpabort, 1);
1013: }
1014:
1015: /*
1.22 deraadt 1016: * Abort a http retrieval
1017: */
1.51 deraadt 1018: /* ARGSUSED */
1.22 deraadt 1019: void
1.51 deraadt 1020: abortfile(int signo)
1.22 deraadt 1021: {
1022:
1023: alarmtimer(0);
1024: fputs("\nfile fetch aborted.\n", ttyout);
1025: (void)fflush(ttyout);
1026: longjmp(httpabort, 1);
1027: }
1028:
1029: /*
1.1 millert 1030: * Retrieve multiple files from the command line, transferring
1031: * files of the form "host:path", "ftp://host/path" using the
1032: * ftp protocol, and files of the form "http://host/path" using
1033: * the http protocol.
1.2 millert 1034: * If path has a trailing "/", then return (-1);
1.1 millert 1035: * the path will be cd-ed into and the connection remains open,
1036: * and the function will return -1 (to indicate the connection
1037: * is alive).
1038: * If an error occurs the return value will be the offset+1 in
1039: * argv[] of the file that caused a problem (i.e, argv[x]
1040: * returns x+1)
1041: * Otherwise, 0 is returned if all files retrieved successfully.
1042: */
1043: int
1.50 deraadt 1044: auto_fetch(int argc, char *argv[], char *outfile)
1.1 millert 1045: {
1046: char *xargv[5];
1.62 ray 1047: char *cp, *url, *host, *dir, *file, *portnum;
1048: char *username, *pass, *pathstart;
1.6 millert 1049: char *ftpproxy, *httpproxy;
1.14 millert 1050: int rval, xargc;
1051: volatile int argpos;
1.49 krw 1052: int dirhasglob, filehasglob, oautologin;
1.14 millert 1053: char rempath[MAXPATHLEN];
1.1 millert 1054:
1055: argpos = 0;
1056:
1057: if (setjmp(toplevel)) {
1058: if (connected)
1059: disconnect(0, NULL);
1.2 millert 1060: return (argpos + 1);
1.1 millert 1061: }
1.3 millert 1062: (void)signal(SIGINT, (sig_t)intr);
1063: (void)signal(SIGPIPE, (sig_t)lostpeer);
1.1 millert 1064:
1.45 millert 1065: if ((ftpproxy = getenv(FTP_PROXY)) != NULL && *ftpproxy == '\0')
1066: ftpproxy = NULL;
1067: if ((httpproxy = getenv(HTTP_PROXY)) != NULL && *httpproxy == '\0')
1068: httpproxy = NULL;
1.6 millert 1069:
1.1 millert 1070: /*
1071: * Loop through as long as there's files to fetch.
1072: */
1.62 ray 1073: for (rval = 0; (rval == 0) && (argpos < argc); free(url), argpos++) {
1.1 millert 1074: if (strchr(argv[argpos], ':') == NULL)
1075: break;
1.62 ray 1076: host = dir = file = portnum = username = pass = NULL;
1.1 millert 1077:
1078: /*
1079: * We muck with the string, so we make a copy.
1080: */
1.62 ray 1081: url = strdup(argv[argpos]);
1082: if (url == NULL)
1.1 millert 1083: errx(1, "Can't allocate memory for auto-fetch.");
1084:
1085: /*
1086: * Try HTTP URL-style arguments first.
1087: */
1.62 ray 1088: if (strncasecmp(url, HTTP_URL, sizeof(HTTP_URL) - 1) == 0 ||
1.61 deraadt 1089: #ifndef SMALL
1090: /* even if we compiled without SSL, url_get will check */
1.62 ray 1091: strncasecmp(url, HTTPS_URL, sizeof(HTTPS_URL) -1) == 0 ||
1.78 martynas 1092: #endif /* !SMALL */
1.62 ray 1093: strncasecmp(url, FILE_URL, sizeof(FILE_URL) - 1) == 0) {
1.54 fgsch 1094: redirect_loop = 0;
1.62 ray 1095: if (url_get(url, httpproxy, outfile) == -1)
1.1 millert 1096: rval = argpos + 1;
1097: continue;
1098: }
1099:
1100: /*
1.6 millert 1101: * Try FTP URL-style arguments next. If ftpproxy is
1102: * set, use url_get() instead of standard ftp.
1103: * Finally, try host:file.
1.1 millert 1104: */
1.62 ray 1105: host = url;
1106: if (strncasecmp(url, FTP_URL, sizeof(FTP_URL) - 1) == 0) {
1.37 heko 1107: char *passend, *passagain, *userend;
1.31 itojun 1108:
1.6 millert 1109: if (ftpproxy) {
1.62 ray 1110: if (url_get(url, ftpproxy, outfile) == -1)
1.6 millert 1111: rval = argpos + 1;
1112: continue;
1113: }
1.1 millert 1114: host += sizeof(FTP_URL) - 1;
1.8 millert 1115: dir = strchr(host, '/');
1.1 millert 1116:
1.8 millert 1117: /* Look for [user:pass@]host[:port] */
1.31 itojun 1118:
1119: /* check if we have "user:pass@" */
1.37 heko 1120: userend = strchr(host, ':');
1.31 itojun 1121: passend = strchr(host, '@');
1122: if (passend && userend && userend < passend &&
1123: (!dir || passend < dir)) {
1.62 ray 1124: username = host;
1.31 itojun 1125: pass = userend + 1;
1126: host = passend + 1;
1127: *userend = *passend = '\0';
1.37 heko 1128: passagain = strchr(host, '@');
1.42 deraadt 1129: if (strchr(pass, '@') != NULL ||
1.37 heko 1130: (passagain != NULL && passagain < dir)) {
1131: warnx(at_encoding_warning);
1132: goto bad_ftp_url;
1.42 deraadt 1133: }
1.31 itojun 1134:
1.77 martynas 1135: if (EMPTYSTRING(username)) {
1.11 millert 1136: bad_ftp_url:
1.31 itojun 1137: warnx("Invalid URL: %s", argv[argpos]);
1138: rval = argpos + 1;
1139: continue;
1140: }
1.62 ray 1141: username = urldecode(username);
1.37 heko 1142: pass = urldecode(pass);
1.8 millert 1143: }
1.31 itojun 1144:
1145: #ifdef INET6
1146: /* check [host]:port, or [host] */
1147: if (host[0] == '[') {
1148: cp = strchr(host, ']');
1149: if (cp && (!dir || cp < dir)) {
1150: if (cp + 1 == dir || cp[1] == ':') {
1151: host++;
1152: *cp++ = '\0';
1153: } else
1154: cp = NULL;
1155: } else
1156: cp = host;
1.25 itojun 1157: } else
1158: cp = host;
1.31 itojun 1159: #else
1160: cp = host;
1.25 itojun 1161: #endif
1.31 itojun 1162:
1163: /* split off host[:port] if there is */
1164: if (cp) {
1165: portnum = strchr(cp, ':');
1.52 henning 1166: pathstart = strchr(cp, '/');
1167: /* : in path is not a port # indicator */
1168: if (portnum && pathstart &&
1169: pathstart < portnum)
1170: portnum = NULL;
1171:
1.31 itojun 1172: if (!portnum)
1173: ;
1174: else {
1175: if (!dir)
1176: ;
1177: else if (portnum + 1 < dir) {
1178: *portnum++ = '\0';
1179: /*
1180: * XXX should check if portnum
1181: * is decimal number
1182: */
1183: } else {
1184: /* empty portnum */
1185: goto bad_ftp_url;
1186: }
1187: }
1188: } else
1189: portnum = NULL;
1.8 millert 1190: } else { /* classic style `host:file' */
1191: dir = strchr(host, ':');
1192: }
1.1 millert 1193: if (EMPTYSTRING(host)) {
1194: rval = argpos + 1;
1195: continue;
1196: }
1197:
1198: /*
1.9 millert 1199: * If dir is NULL, the file wasn't specified
1.1 millert 1200: * (URL looked something like ftp://host)
1201: */
1.8 millert 1202: if (dir != NULL)
1203: *dir++ = '\0';
1.1 millert 1204:
1205: /*
1206: * Extract the file and (if present) directory name.
1207: */
1.42 deraadt 1208: if (!EMPTYSTRING(dir)) {
1.8 millert 1209: cp = strrchr(dir, '/');
1.1 millert 1210: if (cp != NULL) {
1211: *cp++ = '\0';
1212: file = cp;
1213: } else {
1214: file = dir;
1215: dir = NULL;
1216: }
1217: }
1.80 martynas 1218: #ifndef SMALL
1.1 millert 1219: if (debug)
1.42 deraadt 1220: fprintf(ttyout,
1221: "user %s:%s host %s port %s dir %s file %s\n",
1.76 martynas 1222: username, pass ? "XXXX" : NULL, host, portnum,
1223: dir, file);
1.80 martynas 1224: #endif /* !SMALL */
1.1 millert 1225:
1226: /*
1.49 krw 1227: * Set up the connection.
1.1 millert 1228: */
1.49 krw 1229: if (connected)
1230: disconnect(0, NULL);
1231: xargv[0] = __progname;
1232: xargv[1] = host;
1.8 millert 1233: xargv[2] = NULL;
1.49 krw 1234: xargc = 2;
1235: if (!EMPTYSTRING(portnum)) {
1236: xargv[2] = portnum;
1237: xargv[3] = NULL;
1238: xargc = 3;
1239: }
1240: oautologin = autologin;
1.88 martynas 1241: if (username == NULL)
1.89 halex 1242: anonftp = 1;
1243: else {
1244: anonftp = 0;
1.49 krw 1245: autologin = 0;
1.89 halex 1246: }
1.49 krw 1247: setpeer(xargc, xargv);
1248: autologin = oautologin;
1.87 martynas 1249: if (connected == 0 ||
1250: (connected == 1 && autologin && (username == NULL ||
1251: !ftp_login(host, username, pass)))) {
1.49 krw 1252: warnx("Can't connect or login to host `%s'", host);
1.8 millert 1253: rval = argpos + 1;
1254: continue;
1.1 millert 1255: }
1.49 krw 1256:
1257: /* Always use binary transfers. */
1258: setbinary(0, NULL);
1.1 millert 1259:
1.4 millert 1260: dirhasglob = filehasglob = 0;
1261: if (doglob) {
1.42 deraadt 1262: if (!EMPTYSTRING(dir) &&
1.4 millert 1263: strpbrk(dir, "*?[]{}") != NULL)
1264: dirhasglob = 1;
1.42 deraadt 1265: if (!EMPTYSTRING(file) &&
1.4 millert 1266: strpbrk(file, "*?[]{}") != NULL)
1267: filehasglob = 1;
1268: }
1269:
1.1 millert 1270: /* Change directories, if necessary. */
1.42 deraadt 1271: if (!EMPTYSTRING(dir) && !dirhasglob) {
1.1 millert 1272: xargv[0] = "cd";
1273: xargv[1] = dir;
1274: xargv[2] = NULL;
1275: cd(2, xargv);
1.42 deraadt 1276: if (!dirchange) {
1.1 millert 1277: rval = argpos + 1;
1278: continue;
1279: }
1280: }
1281:
1282: if (EMPTYSTRING(file)) {
1.86 martynas 1283: #ifndef SMALL
1.1 millert 1284: rval = -1;
1.86 martynas 1285: #else /* !SMALL */
1286: recvrequest("NLST", "-", NULL, "w", 0, 0);
1287: rval = 0;
1288: #endif /* !SMALL */
1.1 millert 1289: continue;
1290: }
1291:
1.21 marc 1292: if (verbose)
1.10 deraadt 1293: fprintf(ttyout, "Retrieving %s/%s\n", dir ? dir : "", file);
1.1 millert 1294:
1.4 millert 1295: if (dirhasglob) {
1296: snprintf(rempath, sizeof(rempath), "%s/%s", dir, file);
1297: file = rempath;
1298: }
1299:
1300: /* Fetch the file(s). */
1.10 deraadt 1301: xargc = 2;
1.1 millert 1302: xargv[0] = "get";
1303: xargv[1] = file;
1304: xargv[2] = NULL;
1.4 millert 1305: if (dirhasglob || filehasglob) {
1306: int ointeractive;
1307:
1308: ointeractive = interactive;
1309: interactive = 0;
1310: xargv[0] = "mget";
1.78 martynas 1311: #ifndef SMALL
1312: if (resume) {
1313: xargc = 3;
1314: xargv[1] = "-c";
1315: xargv[2] = file;
1316: xargv[3] = NULL;
1317: }
1318: #endif /* !SMALL */
1.10 deraadt 1319: mget(xargc, xargv);
1.5 millert 1320: interactive = ointeractive;
1.10 deraadt 1321: } else {
1.17 millert 1322: if (outfile != NULL) {
1323: xargv[2] = outfile;
1324: xargv[3] = NULL;
1.10 deraadt 1325: xargc++;
1326: }
1.75 martynas 1327: #ifndef SMALL
1328: if (resume)
1329: reget(xargc, xargv);
1330: else
1.78 martynas 1331: #endif /* !SMALL */
1.75 martynas 1332: get(xargc, xargv);
1.10 deraadt 1333: }
1.1 millert 1334:
1.4 millert 1335: if ((code / 100) != COMPLETE)
1.1 millert 1336: rval = argpos + 1;
1337: }
1338: if (connected && rval != -1)
1339: disconnect(0, NULL);
1340: return (rval);
1.37 heko 1341: }
1342:
1343: char *
1.50 deraadt 1344: urldecode(const char *str)
1.37 heko 1345: {
1.53 deraadt 1346: char *ret, c;
1347: int i, reallen;
1.37 heko 1348:
1.53 deraadt 1349: if (str == NULL)
1350: return NULL;
1351: if ((ret = malloc(strlen(str)+1)) == NULL)
1352: err(1, "Can't allocate memory for URL decoding");
1353: for (i = 0, reallen = 0; str[i] != '\0'; i++, reallen++, ret++) {
1354: c = str[i];
1355: if (c == '+') {
1356: *ret = ' ';
1357: continue;
1358: }
1.61 deraadt 1359:
1360: /* Cannot use strtol here because next char
1361: * after %xx may be a digit.
1362: */
1.53 deraadt 1363: if (c == '%' && isxdigit(str[i+1]) && isxdigit(str[i+2])) {
1364: *ret = hextochar(&str[i+1]);
1365: i+=2;
1366: continue;
1367: }
1368: *ret = c;
1369: }
1370: *ret = '\0';
1371:
1372: return ret-reallen;
1.37 heko 1373: }
1374:
1375: char
1.50 deraadt 1376: hextochar(const char *str)
1.37 heko 1377: {
1.53 deraadt 1378: char c, ret;
1.37 heko 1379:
1.53 deraadt 1380: c = str[0];
1381: ret = c;
1382: if (isalpha(c))
1383: ret -= isupper(c) ? 'A' - 10 : 'a' - 10;
1384: else
1385: ret -= '0';
1386: ret *= 16;
1387:
1388: c = str[1];
1389: ret += c;
1390: if (isalpha(c))
1391: ret -= isupper(c) ? 'A' - 10 : 'a' - 10;
1392: else
1393: ret -= '0';
1394: return ret;
1.25 itojun 1395: }
1396:
1397: int
1.50 deraadt 1398: isurl(const char *p)
1.25 itojun 1399: {
1.27 millert 1400:
1.26 deraadt 1401: if (strncasecmp(p, FTP_URL, sizeof(FTP_URL) - 1) == 0 ||
1402: strncasecmp(p, HTTP_URL, sizeof(HTTP_URL) - 1) == 0 ||
1.61 deraadt 1403: #ifndef SMALL
1404: strncasecmp(p, HTTPS_URL, sizeof(HTTPS_URL) - 1) == 0 ||
1.78 martynas 1405: #endif /* !SMALL */
1.27 millert 1406: strncasecmp(p, FILE_URL, sizeof(FILE_URL) - 1) == 0 ||
1407: strstr(p, ":/"))
1408: return (1);
1409: return (0);
1.1 millert 1410: }
1.61 deraadt 1411:
1412: char *
1413: ftp_readline(FILE *fp, SSL *ssl, size_t *lenp)
1414: {
1415: if (fp != NULL)
1416: return fparseln(fp, lenp, NULL, "\0\0\0", 0);
1417: #ifndef SMALL
1418: else if (ssl != NULL)
1419: return SSL_readline(ssl, lenp);
1.78 martynas 1420: #endif /* !SMALL */
1.61 deraadt 1421: else
1422: return NULL;
1423: }
1424:
1.65 ray 1425: size_t
1.61 deraadt 1426: ftp_read(FILE *fp, SSL *ssl, char *buf, size_t len)
1427: {
1.65 ray 1428: size_t ret;
1.61 deraadt 1429: if (fp != NULL)
1430: ret = fread(buf, sizeof(char), len, fp);
1431: #ifndef SMALL
1.65 ray 1432: else if (ssl != NULL) {
1433: int nr;
1434:
1435: if (len > INT_MAX)
1436: len = INT_MAX;
1437: if ((nr = SSL_read(ssl, buf, (int)len)) <= 0)
1438: ret = 0;
1439: else
1440: ret = nr;
1441: }
1.78 martynas 1442: #endif /* !SMALL */
1.61 deraadt 1443: else
1444: ret = 0;
1445: return (ret);
1446: }
1447:
1448: int
1449: ftp_printf(FILE *fp, SSL *ssl, const char *fmt, ...)
1450: {
1451: int ret;
1452: va_list ap;
1453:
1454: va_start(ap, fmt);
1455:
1456: if (fp != NULL)
1457: ret = vfprintf(fp, fmt, ap);
1458: #ifndef SMALL
1459: else if (ssl != NULL)
1460: ret = SSL_vprintf((SSL*)ssl, fmt, ap);
1.78 martynas 1461: #endif /* !SMALL */
1.61 deraadt 1462: else
1.92 jsg 1463: ret = 0;
1.61 deraadt 1464:
1465: va_end(ap);
1466: return (ret);
1467: }
1468:
1469: #ifndef SMALL
1470: int
1471: SSL_vprintf(SSL *ssl, const char *fmt, va_list ap)
1472: {
1473: int ret;
1474: char *string;
1475:
1476: if ((ret = vasprintf(&string, fmt, ap)) == -1)
1477: return ret;
1.64 ray 1478: ret = SSL_write(ssl, string, ret);
1.61 deraadt 1479: free(string);
1480: return ret;
1481: }
1482:
1483: char *
1484: SSL_readline(SSL *ssl, size_t *lenp)
1485: {
1.63 ray 1486: size_t i, len;
1.61 deraadt 1487: char *buf, *q, c;
1.109 ! sthen 1488: int ret;
1.61 deraadt 1489:
1490: len = 128;
1491: if ((buf = malloc(len)) == NULL)
1492: errx(1, "Can't allocate memory for transfer buffer");
1493: for (i = 0; ; i++) {
1494: if (i >= len - 1) {
1495: if ((q = realloc(buf, 2 * len)) == NULL)
1496: errx(1, "Can't expand transfer buffer");
1497: buf = q;
1498: len *= 2;
1499: }
1.109 ! sthen 1500: again:
! 1501: ret = SSL_read(ssl, &c, 1);
! 1502: if (ret <= 0) {
! 1503: if (SSL_get_error(ssl, ret) == SSL_ERROR_WANT_READ)
! 1504: goto again;
! 1505: else
! 1506: errx(1, "SSL_read error: %u",
! 1507: SSL_get_error(ssl, ret));
! 1508: }
1.61 deraadt 1509: buf[i] = c;
1510: if (c == '\n')
1511: break;
1512: }
1513: *lenp = i;
1514: return (buf);
1515: }
1516:
1517: int
1.81 espie 1518: proxy_connect(int socket, char *host, char *cookie)
1.61 deraadt 1519: {
1.66 ray 1520: int l;
1.61 deraadt 1521: char buf[1024];
1522: char *connstr, *hosttail, *port;
1523:
1524: if (*host == '[' && (hosttail = strrchr(host, ']')) != NULL &&
1525: (hosttail[1] == '\0' || hosttail[1] == ':')) {
1526: host++;
1527: *hosttail++ = '\0';
1528: } else
1529: hosttail = host;
1530:
1531: port = strrchr(hosttail, ':'); /* find portnum */
1532: if (port != NULL)
1533: *port++ = '\0';
1534: if (!port)
1535: port = "443";
1536:
1.81 espie 1537: if (cookie) {
1538: l = asprintf(&connstr, "CONNECT %s:%s HTTP/1.1\r\n"
1539: "Proxy-Authorization: Basic %s\r\n%s\r\n\r\n",
1540: host, port, cookie, HTTP_USER_AGENT);
1541: } else {
1542: l = asprintf(&connstr, "CONNECT %s:%s HTTP/1.1\r\n%s\r\n\r\n",
1543: host, port, HTTP_USER_AGENT);
1544: }
1545:
1.66 ray 1546: if (l == -1)
1.61 deraadt 1547: errx(1, "Could not allocate memory to assemble connect string!");
1.80 martynas 1548: #ifndef SMALL
1.68 ray 1549: if (debug)
1550: printf("%s", connstr);
1.80 martynas 1551: #endif /* !SMALL */
1.66 ray 1552: if (write(socket, connstr, l) != l)
1.68 ray 1553: err(1, "Could not send connect string");
1.61 deraadt 1554: read(socket, &buf, sizeof(buf)); /* only proxy header XXX: error handling? */
1.71 ray 1555: free(connstr);
1.61 deraadt 1556: return(200);
1557: }
1.78 martynas 1558: #endif /* !SMALL */