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