1 /*- 2 * SPDX-License-Identifier: (BSD-3-Clause AND Beerware) 3 * 4 * Copyright (c) 1998-2011 Dag-Erling Smørgrav 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer 12 * in this position and unchanged. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. The name of the author may not be used to endorse or promote products 17 * derived from this software without specific prior written permission 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #include <sys/cdefs.h> 32 __FBSDID("$FreeBSD$"); 33 34 /* 35 * Portions of this code were taken from or based on ftpio.c: 36 * 37 * ---------------------------------------------------------------------------- 38 * "THE BEER-WARE LICENSE" (Revision 42): 39 * <phk@FreeBSD.org> wrote this file. As long as you retain this notice you 40 * can do whatever you want with this stuff. If we meet some day, and you think 41 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp 42 * ---------------------------------------------------------------------------- 43 * 44 * Major Changelog: 45 * 46 * Dag-Erling Smørgrav 47 * 9 Jun 1998 48 * 49 * Incorporated into libfetch 50 * 51 * Jordan K. Hubbard 52 * 17 Jan 1996 53 * 54 * Turned inside out. Now returns xfers as new file ids, not as a special 55 * `state' of FTP_t 56 * 57 * $ftpioId: ftpio.c,v 1.30 1998/04/11 07:28:53 phk Exp $ 58 * 59 */ 60 61 #include <sys/param.h> 62 #include <sys/socket.h> 63 #include <netinet/in.h> 64 65 #include <ctype.h> 66 #include <err.h> 67 #include <errno.h> 68 #include <fcntl.h> 69 #include <netdb.h> 70 #include <stdarg.h> 71 #include <stdint.h> 72 #include <stdio.h> 73 #include <stdlib.h> 74 #include <string.h> 75 #include <time.h> 76 #include <unistd.h> 77 78 #include "fetch.h" 79 #include "common.h" 80 #include "ftperr.h" 81 82 #define FTP_ANONYMOUS_USER "anonymous" 83 84 #define FTP_CONNECTION_ALREADY_OPEN 125 85 #define FTP_OPEN_DATA_CONNECTION 150 86 #define FTP_OK 200 87 #define FTP_FILE_STATUS 213 88 #define FTP_SERVICE_READY 220 89 #define FTP_TRANSFER_COMPLETE 226 90 #define FTP_PASSIVE_MODE 227 91 #define FTP_LPASSIVE_MODE 228 92 #define FTP_EPASSIVE_MODE 229 93 #define FTP_LOGGED_IN 230 94 #define FTP_FILE_ACTION_OK 250 95 #define FTP_DIRECTORY_CREATED 257 /* multiple meanings */ 96 #define FTP_FILE_CREATED 257 /* multiple meanings */ 97 #define FTP_WORKING_DIRECTORY 257 /* multiple meanings */ 98 #define FTP_NEED_PASSWORD 331 99 #define FTP_NEED_ACCOUNT 332 100 #define FTP_FILE_OK 350 101 #define FTP_SYNTAX_ERROR 500 102 #define FTP_PROTOCOL_ERROR 999 103 104 static struct url cached_host; 105 static conn_t *cached_connection; 106 107 #define isftpreply(foo) \ 108 (isdigit((unsigned char)foo[0]) && \ 109 isdigit((unsigned char)foo[1]) && \ 110 isdigit((unsigned char)foo[2]) && \ 111 (foo[3] == ' ' || foo[3] == '\0')) 112 #define isftpinfo(foo) \ 113 (isdigit((unsigned char)foo[0]) && \ 114 isdigit((unsigned char)foo[1]) && \ 115 isdigit((unsigned char)foo[2]) && \ 116 foo[3] == '-') 117 118 /* 119 * Translate IPv4 mapped IPv6 address to IPv4 address 120 */ 121 static void 122 unmappedaddr(struct sockaddr_in6 *sin6) 123 { 124 struct sockaddr_in *sin4; 125 u_int32_t addr; 126 int port; 127 128 if (sin6->sin6_family != AF_INET6 || 129 !IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) 130 return; 131 sin4 = (struct sockaddr_in *)sin6; 132 addr = *(u_int32_t *)(uintptr_t)&sin6->sin6_addr.s6_addr[12]; 133 port = sin6->sin6_port; 134 memset(sin4, 0, sizeof(struct sockaddr_in)); 135 sin4->sin_addr.s_addr = addr; 136 sin4->sin_port = port; 137 sin4->sin_family = AF_INET; 138 sin4->sin_len = sizeof(struct sockaddr_in); 139 } 140 141 /* 142 * Get server response 143 */ 144 static int 145 ftp_chkerr(conn_t *conn) 146 { 147 if (fetch_getln(conn) == -1) { 148 fetch_syserr(); 149 return (-1); 150 } 151 if (isftpinfo(conn->buf)) { 152 while (conn->buflen && !isftpreply(conn->buf)) { 153 if (fetch_getln(conn) == -1) { 154 fetch_syserr(); 155 return (-1); 156 } 157 } 158 } 159 160 while (conn->buflen && 161 isspace((unsigned char)conn->buf[conn->buflen - 1])) 162 conn->buflen--; 163 conn->buf[conn->buflen] = '\0'; 164 165 if (!isftpreply(conn->buf)) { 166 ftp_seterr(FTP_PROTOCOL_ERROR); 167 return (-1); 168 } 169 170 conn->err = (conn->buf[0] - '0') * 100 171 + (conn->buf[1] - '0') * 10 172 + (conn->buf[2] - '0'); 173 174 return (conn->err); 175 } 176 177 /* 178 * Send a command and check reply 179 */ 180 static int 181 ftp_cmd(conn_t *conn, const char *fmt, ...) 182 { 183 va_list ap; 184 size_t len; 185 char *msg; 186 int r; 187 188 va_start(ap, fmt); 189 len = vasprintf(&msg, fmt, ap); 190 va_end(ap); 191 192 if (msg == NULL) { 193 errno = ENOMEM; 194 fetch_syserr(); 195 return (-1); 196 } 197 198 r = fetch_putln(conn, msg, len); 199 free(msg); 200 201 if (r == -1) { 202 fetch_syserr(); 203 return (-1); 204 } 205 206 return (ftp_chkerr(conn)); 207 } 208 209 /* 210 * Return a pointer to the filename part of a path 211 */ 212 static const char * 213 ftp_filename(const char *file, int *len, int *type) 214 { 215 const char *s; 216 217 if ((s = strrchr(file, '/')) == NULL) 218 s = file; 219 else 220 s = s + 1; 221 *len = strlen(s); 222 if (*len > 7 && strncmp(s + *len - 7, ";type=", 6) == 0) { 223 *type = s[*len - 1]; 224 *len -= 7; 225 } else { 226 *type = '\0'; 227 } 228 return (s); 229 } 230 231 /* 232 * Get current working directory from the reply to a CWD, PWD or CDUP 233 * command. 234 */ 235 static int 236 ftp_pwd(conn_t *conn, char *pwd, size_t pwdlen) 237 { 238 char *src, *dst, *end; 239 int q; 240 241 if (conn->err != FTP_WORKING_DIRECTORY && 242 conn->err != FTP_FILE_ACTION_OK) 243 return (FTP_PROTOCOL_ERROR); 244 end = conn->buf + conn->buflen; 245 src = conn->buf + 4; 246 if (src >= end || *src++ != '"') 247 return (FTP_PROTOCOL_ERROR); 248 for (q = 0, dst = pwd; src < end && pwdlen--; ++src) { 249 if (!q && *src == '"') 250 q = 1; 251 else if (q && *src != '"') 252 break; 253 else if (q) 254 *dst++ = '"', q = 0; 255 else 256 *dst++ = *src; 257 } 258 if (!pwdlen) 259 return (FTP_PROTOCOL_ERROR); 260 *dst = '\0'; 261 #if 0 262 DEBUG(fprintf(stderr, "pwd: [%s]\n", pwd)); 263 #endif 264 return (FTP_OK); 265 } 266 267 /* 268 * Change working directory to the directory that contains the specified 269 * file. 270 */ 271 static int 272 ftp_cwd(conn_t *conn, const char *file) 273 { 274 const char *beg, *end; 275 char pwd[PATH_MAX]; 276 int e, i, len; 277 278 /* If no slashes in name, no need to change dirs. */ 279 if ((end = strrchr(file, '/')) == NULL) 280 return (0); 281 if ((e = ftp_cmd(conn, "PWD")) != FTP_WORKING_DIRECTORY || 282 (e = ftp_pwd(conn, pwd, sizeof(pwd))) != FTP_OK) { 283 ftp_seterr(e); 284 return (-1); 285 } 286 for (;;) { 287 len = strlen(pwd); 288 289 /* Look for a common prefix between PWD and dir to fetch. */ 290 for (i = 0; i <= len && i <= end - file; ++i) 291 if (pwd[i] != file[i]) 292 break; 293 #if 0 294 DEBUG(fprintf(stderr, "have: [%.*s|%s]\n", i, pwd, pwd + i)); 295 DEBUG(fprintf(stderr, "want: [%.*s|%s]\n", i, file, file + i)); 296 #endif 297 /* Keep going up a dir until we have a matching prefix. */ 298 if (pwd[i] == '\0' && (file[i - 1] == '/' || file[i] == '/')) 299 break; 300 if ((e = ftp_cmd(conn, "CDUP")) != FTP_FILE_ACTION_OK || 301 (e = ftp_cmd(conn, "PWD")) != FTP_WORKING_DIRECTORY || 302 (e = ftp_pwd(conn, pwd, sizeof(pwd))) != FTP_OK) { 303 ftp_seterr(e); 304 return (-1); 305 } 306 } 307 308 #ifdef FTP_COMBINE_CWDS 309 /* Skip leading slashes, even "////". */ 310 for (beg = file + i; beg < end && *beg == '/'; ++beg, ++i) 311 /* nothing */ ; 312 313 /* If there is no trailing dir, we're already there. */ 314 if (beg >= end) 315 return (0); 316 317 /* Change to the directory all in one chunk (e.g., foo/bar/baz). */ 318 e = ftp_cmd(conn, "CWD %.*s", (int)(end - beg), beg); 319 if (e == FTP_FILE_ACTION_OK) 320 return (0); 321 #endif /* FTP_COMBINE_CWDS */ 322 323 /* That didn't work so go back to legacy behavior (multiple CWDs). */ 324 for (beg = file + i; beg < end; beg = file + i + 1) { 325 while (*beg == '/') 326 ++beg, ++i; 327 for (++i; file + i < end && file[i] != '/'; ++i) 328 /* nothing */ ; 329 e = ftp_cmd(conn, "CWD %.*s", file + i - beg, beg); 330 if (e != FTP_FILE_ACTION_OK) { 331 ftp_seterr(e); 332 return (-1); 333 } 334 } 335 return (0); 336 } 337 338 /* 339 * Set transfer mode and data type 340 */ 341 static int 342 ftp_mode_type(conn_t *conn, int mode, int type) 343 { 344 int e; 345 346 switch (mode) { 347 case 0: 348 case 's': 349 mode = 'S'; 350 case 'S': 351 break; 352 default: 353 return (FTP_PROTOCOL_ERROR); 354 } 355 if ((e = ftp_cmd(conn, "MODE %c", mode)) != FTP_OK) { 356 if (mode == 'S') { 357 /* 358 * Stream mode is supposed to be the default - so 359 * much so that some servers not only do not 360 * support any other mode, but do not support the 361 * MODE command at all. 362 * 363 * If "MODE S" fails, it is unlikely that we 364 * previously succeeded in setting a different 365 * mode. Therefore, we simply hope that the 366 * server is already in the correct mode, and 367 * silently ignore the failure. 368 */ 369 } else { 370 return (e); 371 } 372 } 373 374 switch (type) { 375 case 0: 376 case 'i': 377 type = 'I'; 378 case 'I': 379 break; 380 case 'a': 381 type = 'A'; 382 case 'A': 383 break; 384 case 'd': 385 type = 'D'; 386 case 'D': 387 /* can't handle yet */ 388 default: 389 return (FTP_PROTOCOL_ERROR); 390 } 391 if ((e = ftp_cmd(conn, "TYPE %c", type)) != FTP_OK) 392 return (e); 393 394 return (FTP_OK); 395 } 396 397 /* 398 * Request and parse file stats 399 */ 400 static int 401 ftp_stat(conn_t *conn, const char *file, struct url_stat *us) 402 { 403 char *ln; 404 const char *filename; 405 int filenamelen, type; 406 struct tm tm; 407 time_t t; 408 int e; 409 410 us->size = -1; 411 us->atime = us->mtime = 0; 412 413 filename = ftp_filename(file, &filenamelen, &type); 414 415 if ((e = ftp_mode_type(conn, 0, type)) != FTP_OK) { 416 ftp_seterr(e); 417 return (-1); 418 } 419 420 e = ftp_cmd(conn, "SIZE %.*s", filenamelen, filename); 421 if (e != FTP_FILE_STATUS) { 422 ftp_seterr(e); 423 return (-1); 424 } 425 for (ln = conn->buf + 4; *ln && isspace((unsigned char)*ln); ln++) 426 /* nothing */ ; 427 for (us->size = 0; *ln && isdigit((unsigned char)*ln); ln++) 428 us->size = us->size * 10 + *ln - '0'; 429 if (*ln && !isspace((unsigned char)*ln)) { 430 ftp_seterr(FTP_PROTOCOL_ERROR); 431 us->size = -1; 432 return (-1); 433 } 434 if (us->size == 0) 435 us->size = -1; 436 DEBUG(fprintf(stderr, "size: [%lld]\n", (long long)us->size)); 437 438 e = ftp_cmd(conn, "MDTM %.*s", filenamelen, filename); 439 if (e != FTP_FILE_STATUS) { 440 ftp_seterr(e); 441 return (-1); 442 } 443 for (ln = conn->buf + 4; *ln && isspace((unsigned char)*ln); ln++) 444 /* nothing */ ; 445 switch (strspn(ln, "0123456789")) { 446 case 14: 447 break; 448 case 15: 449 ln++; 450 ln[0] = '2'; 451 ln[1] = '0'; 452 break; 453 default: 454 ftp_seterr(FTP_PROTOCOL_ERROR); 455 return (-1); 456 } 457 if (sscanf(ln, "%04d%02d%02d%02d%02d%02d", 458 &tm.tm_year, &tm.tm_mon, &tm.tm_mday, 459 &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) { 460 ftp_seterr(FTP_PROTOCOL_ERROR); 461 return (-1); 462 } 463 tm.tm_mon--; 464 tm.tm_year -= 1900; 465 tm.tm_isdst = -1; 466 t = timegm(&tm); 467 if (t == (time_t)-1) 468 t = time(NULL); 469 us->mtime = t; 470 us->atime = t; 471 DEBUG(fprintf(stderr, 472 "last modified: [%04d-%02d-%02d %02d:%02d:%02d]\n", 473 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, 474 tm.tm_hour, tm.tm_min, tm.tm_sec)); 475 return (0); 476 } 477 478 /* 479 * I/O functions for FTP 480 */ 481 struct ftpio { 482 conn_t *cconn; /* Control connection */ 483 conn_t *dconn; /* Data connection */ 484 int dir; /* Direction */ 485 int eof; /* EOF reached */ 486 int err; /* Error code */ 487 }; 488 489 static int ftp_readfn(void *, char *, int); 490 static int ftp_writefn(void *, const char *, int); 491 static fpos_t ftp_seekfn(void *, fpos_t, int); 492 static int ftp_closefn(void *); 493 494 static int 495 ftp_readfn(void *v, char *buf, int len) 496 { 497 struct ftpio *io; 498 int r; 499 500 io = (struct ftpio *)v; 501 if (io == NULL) { 502 errno = EBADF; 503 return (-1); 504 } 505 if (io->cconn == NULL || io->dconn == NULL || io->dir == O_WRONLY) { 506 errno = EBADF; 507 return (-1); 508 } 509 if (io->err) { 510 errno = io->err; 511 return (-1); 512 } 513 if (io->eof) 514 return (0); 515 r = fetch_read(io->dconn, buf, len); 516 if (r > 0) 517 return (r); 518 if (r == 0) { 519 io->eof = 1; 520 return (0); 521 } 522 if (errno != EINTR) 523 io->err = errno; 524 return (-1); 525 } 526 527 static int 528 ftp_writefn(void *v, const char *buf, int len) 529 { 530 struct ftpio *io; 531 int w; 532 533 io = (struct ftpio *)v; 534 if (io == NULL) { 535 errno = EBADF; 536 return (-1); 537 } 538 if (io->cconn == NULL || io->dconn == NULL || io->dir == O_RDONLY) { 539 errno = EBADF; 540 return (-1); 541 } 542 if (io->err) { 543 errno = io->err; 544 return (-1); 545 } 546 w = fetch_write(io->dconn, buf, len); 547 if (w >= 0) 548 return (w); 549 if (errno != EINTR) 550 io->err = errno; 551 return (-1); 552 } 553 554 static fpos_t 555 ftp_seekfn(void *v, fpos_t pos __unused, int whence __unused) 556 { 557 struct ftpio *io; 558 559 io = (struct ftpio *)v; 560 if (io == NULL) { 561 errno = EBADF; 562 return (-1); 563 } 564 errno = ESPIPE; 565 return (-1); 566 } 567 568 static int 569 ftp_closefn(void *v) 570 { 571 struct ftpio *io; 572 int r; 573 574 io = (struct ftpio *)v; 575 if (io == NULL) { 576 errno = EBADF; 577 return (-1); 578 } 579 if (io->dir == -1) 580 return (0); 581 if (io->cconn == NULL || io->dconn == NULL) { 582 errno = EBADF; 583 return (-1); 584 } 585 fetch_close(io->dconn); 586 io->dir = -1; 587 io->dconn = NULL; 588 DEBUG(fprintf(stderr, "Waiting for final status\n")); 589 r = ftp_chkerr(io->cconn); 590 if (io->cconn == cached_connection && io->cconn->ref == 1) 591 cached_connection = NULL; 592 fetch_close(io->cconn); 593 free(io); 594 return (r == FTP_TRANSFER_COMPLETE) ? 0 : -1; 595 } 596 597 static FILE * 598 ftp_setup(conn_t *cconn, conn_t *dconn, int mode) 599 { 600 struct ftpio *io; 601 FILE *f; 602 603 if (cconn == NULL || dconn == NULL) 604 return (NULL); 605 if ((io = malloc(sizeof(*io))) == NULL) 606 return (NULL); 607 io->cconn = cconn; 608 io->dconn = dconn; 609 io->dir = mode; 610 io->eof = io->err = 0; 611 f = funopen(io, ftp_readfn, ftp_writefn, ftp_seekfn, ftp_closefn); 612 if (f == NULL) 613 free(io); 614 return (f); 615 } 616 617 /* 618 * Transfer file 619 */ 620 static FILE * 621 ftp_transfer(conn_t *conn, const char *oper, const char *file, 622 int mode, off_t offset, const char *flags) 623 { 624 struct sockaddr_storage sa; 625 struct sockaddr_in6 *sin6; 626 struct sockaddr_in *sin4; 627 const char *bindaddr; 628 const char *filename; 629 int filenamelen, type; 630 int low, pasv, verbose; 631 int e, sd = -1; 632 socklen_t l; 633 char *s; 634 FILE *df; 635 636 /* check flags */ 637 low = CHECK_FLAG('l'); 638 pasv = CHECK_FLAG('p') || !CHECK_FLAG('P'); 639 verbose = CHECK_FLAG('v'); 640 641 /* passive mode */ 642 if ((s = getenv("FTP_PASSIVE_MODE")) != NULL) 643 pasv = (strncasecmp(s, "no", 2) != 0); 644 645 /* isolate filename */ 646 filename = ftp_filename(file, &filenamelen, &type); 647 648 /* set transfer mode and data type */ 649 if ((e = ftp_mode_type(conn, 0, type)) != FTP_OK) 650 goto ouch; 651 652 /* find our own address, bind, and listen */ 653 l = sizeof(sa); 654 if (getsockname(conn->sd, (struct sockaddr *)&sa, &l) == -1) 655 goto sysouch; 656 if (sa.ss_family == AF_INET6) 657 unmappedaddr((struct sockaddr_in6 *)&sa); 658 659 /* open data socket */ 660 if ((sd = socket(sa.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) { 661 fetch_syserr(); 662 return (NULL); 663 } 664 665 if (pasv) { 666 u_char addr[64]; 667 char *ln, *p; 668 unsigned int i; 669 int port; 670 671 /* send PASV command */ 672 if (verbose) 673 fetch_info("setting passive mode"); 674 switch (sa.ss_family) { 675 case AF_INET: 676 if ((e = ftp_cmd(conn, "PASV")) != FTP_PASSIVE_MODE) 677 goto ouch; 678 break; 679 case AF_INET6: 680 if ((e = ftp_cmd(conn, "EPSV")) != FTP_EPASSIVE_MODE) { 681 if (e == -1) 682 goto ouch; 683 if ((e = ftp_cmd(conn, "LPSV")) != 684 FTP_LPASSIVE_MODE) 685 goto ouch; 686 } 687 break; 688 default: 689 e = FTP_PROTOCOL_ERROR; /* XXX: error code should be prepared */ 690 goto ouch; 691 } 692 693 /* 694 * Find address and port number. The reply to the PASV command 695 * is IMHO the one and only weak point in the FTP protocol. 696 */ 697 ln = conn->buf; 698 switch (e) { 699 case FTP_PASSIVE_MODE: 700 case FTP_LPASSIVE_MODE: 701 for (p = ln + 3; *p && !isdigit((unsigned char)*p); p++) 702 /* nothing */ ; 703 if (!*p) { 704 e = FTP_PROTOCOL_ERROR; 705 goto ouch; 706 } 707 l = (e == FTP_PASSIVE_MODE ? 6 : 21); 708 for (i = 0; *p && i < l; i++, p++) 709 addr[i] = strtol(p, &p, 10); 710 if (i < l) { 711 e = FTP_PROTOCOL_ERROR; 712 goto ouch; 713 } 714 break; 715 case FTP_EPASSIVE_MODE: 716 for (p = ln + 3; *p && *p != '('; p++) 717 /* nothing */ ; 718 if (!*p) { 719 e = FTP_PROTOCOL_ERROR; 720 goto ouch; 721 } 722 ++p; 723 if (sscanf(p, "%c%c%c%d%c", &addr[0], &addr[1], &addr[2], 724 &port, &addr[3]) != 5 || 725 addr[0] != addr[1] || 726 addr[0] != addr[2] || addr[0] != addr[3]) { 727 e = FTP_PROTOCOL_ERROR; 728 goto ouch; 729 } 730 break; 731 } 732 733 /* seek to required offset */ 734 if (offset) 735 if (ftp_cmd(conn, "REST %lu", (u_long)offset) != FTP_FILE_OK) 736 goto sysouch; 737 738 /* construct sockaddr for data socket */ 739 l = sizeof(sa); 740 if (getpeername(conn->sd, (struct sockaddr *)&sa, &l) == -1) 741 goto sysouch; 742 if (sa.ss_family == AF_INET6) 743 unmappedaddr((struct sockaddr_in6 *)&sa); 744 switch (sa.ss_family) { 745 case AF_INET6: 746 sin6 = (struct sockaddr_in6 *)&sa; 747 if (e == FTP_EPASSIVE_MODE) 748 sin6->sin6_port = htons(port); 749 else { 750 memcpy(&sin6->sin6_addr, addr + 2, 16); 751 memcpy(&sin6->sin6_port, addr + 19, 2); 752 } 753 break; 754 case AF_INET: 755 sin4 = (struct sockaddr_in *)&sa; 756 if (e == FTP_EPASSIVE_MODE) 757 sin4->sin_port = htons(port); 758 else { 759 memcpy(&sin4->sin_addr, addr, 4); 760 memcpy(&sin4->sin_port, addr + 4, 2); 761 } 762 break; 763 default: 764 e = FTP_PROTOCOL_ERROR; /* XXX: error code should be prepared */ 765 break; 766 } 767 768 /* connect to data port */ 769 if (verbose) 770 fetch_info("opening data connection"); 771 bindaddr = getenv("FETCH_BIND_ADDRESS"); 772 if (bindaddr != NULL && *bindaddr != '\0' && 773 (e = fetch_bind(sd, sa.ss_family, bindaddr)) != 0) 774 goto ouch; 775 if (connect(sd, (struct sockaddr *)&sa, sa.ss_len) == -1) 776 goto sysouch; 777 778 /* make the server initiate the transfer */ 779 if (verbose) 780 fetch_info("initiating transfer"); 781 e = ftp_cmd(conn, "%s %.*s", oper, filenamelen, filename); 782 if (e != FTP_CONNECTION_ALREADY_OPEN && e != FTP_OPEN_DATA_CONNECTION) 783 goto ouch; 784 785 } else { 786 u_int32_t a; 787 u_short p; 788 int arg, d; 789 char *ap; 790 char hname[INET6_ADDRSTRLEN]; 791 792 switch (sa.ss_family) { 793 case AF_INET6: 794 ((struct sockaddr_in6 *)&sa)->sin6_port = 0; 795 #ifdef IPV6_PORTRANGE 796 arg = low ? IPV6_PORTRANGE_DEFAULT : IPV6_PORTRANGE_HIGH; 797 if (setsockopt(sd, IPPROTO_IPV6, IPV6_PORTRANGE, 798 (char *)&arg, sizeof(arg)) == -1) 799 goto sysouch; 800 #endif 801 break; 802 case AF_INET: 803 ((struct sockaddr_in *)&sa)->sin_port = 0; 804 arg = low ? IP_PORTRANGE_DEFAULT : IP_PORTRANGE_HIGH; 805 if (setsockopt(sd, IPPROTO_IP, IP_PORTRANGE, 806 (char *)&arg, sizeof(arg)) == -1) 807 goto sysouch; 808 break; 809 } 810 if (verbose) 811 fetch_info("binding data socket"); 812 if (bind(sd, (struct sockaddr *)&sa, sa.ss_len) == -1) 813 goto sysouch; 814 if (listen(sd, 1) == -1) 815 goto sysouch; 816 817 /* find what port we're on and tell the server */ 818 if (getsockname(sd, (struct sockaddr *)&sa, &l) == -1) 819 goto sysouch; 820 switch (sa.ss_family) { 821 case AF_INET: 822 sin4 = (struct sockaddr_in *)&sa; 823 a = ntohl(sin4->sin_addr.s_addr); 824 p = ntohs(sin4->sin_port); 825 e = ftp_cmd(conn, "PORT %d,%d,%d,%d,%d,%d", 826 (a >> 24) & 0xff, (a >> 16) & 0xff, 827 (a >> 8) & 0xff, a & 0xff, 828 (p >> 8) & 0xff, p & 0xff); 829 break; 830 case AF_INET6: 831 #define UC(b) (((int)b)&0xff) 832 e = -1; 833 sin6 = (struct sockaddr_in6 *)&sa; 834 sin6->sin6_scope_id = 0; 835 if (getnameinfo((struct sockaddr *)&sa, sa.ss_len, 836 hname, sizeof(hname), 837 NULL, 0, NI_NUMERICHOST) == 0) { 838 e = ftp_cmd(conn, "EPRT |%d|%s|%d|", 2, hname, 839 htons(sin6->sin6_port)); 840 if (e == -1) 841 goto ouch; 842 } 843 if (e != FTP_OK) { 844 ap = (char *)&sin6->sin6_addr; 845 e = ftp_cmd(conn, 846 "LPRT %d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d", 847 6, 16, 848 UC(ap[0]), UC(ap[1]), UC(ap[2]), UC(ap[3]), 849 UC(ap[4]), UC(ap[5]), UC(ap[6]), UC(ap[7]), 850 UC(ap[8]), UC(ap[9]), UC(ap[10]), UC(ap[11]), 851 UC(ap[12]), UC(ap[13]), UC(ap[14]), UC(ap[15]), 852 2, 853 (ntohs(sin6->sin6_port) >> 8) & 0xff, 854 ntohs(sin6->sin6_port) & 0xff); 855 } 856 break; 857 default: 858 e = FTP_PROTOCOL_ERROR; /* XXX: error code should be prepared */ 859 goto ouch; 860 } 861 if (e != FTP_OK) 862 goto ouch; 863 864 /* seek to required offset */ 865 if (offset) 866 if (ftp_cmd(conn, "REST %ju", (uintmax_t)offset) != FTP_FILE_OK) 867 goto sysouch; 868 869 /* make the server initiate the transfer */ 870 if (verbose) 871 fetch_info("initiating transfer"); 872 e = ftp_cmd(conn, "%s %.*s", oper, filenamelen, filename); 873 if (e != FTP_CONNECTION_ALREADY_OPEN && e != FTP_OPEN_DATA_CONNECTION) 874 goto ouch; 875 876 /* accept the incoming connection and go to town */ 877 if ((d = accept(sd, NULL, NULL)) == -1) 878 goto sysouch; 879 close(sd); 880 sd = d; 881 } 882 883 if ((df = ftp_setup(conn, fetch_reopen(sd), mode)) == NULL) 884 goto sysouch; 885 return (df); 886 887 sysouch: 888 fetch_syserr(); 889 if (sd >= 0) 890 close(sd); 891 return (NULL); 892 893 ouch: 894 if (e != -1) 895 ftp_seterr(e); 896 if (sd >= 0) 897 close(sd); 898 return (NULL); 899 } 900 901 /* 902 * Authenticate 903 */ 904 static int 905 ftp_authenticate(conn_t *conn, struct url *url, struct url *purl) 906 { 907 const char *user, *pwd, *logname; 908 char pbuf[MAXHOSTNAMELEN + MAXLOGNAME + 1]; 909 int e, len; 910 911 /* XXX FTP_AUTH, and maybe .netrc */ 912 913 /* send user name and password */ 914 if (url->user[0] == '\0') 915 fetch_netrc_auth(url); 916 user = url->user; 917 if (*user == '\0') 918 user = getenv("FTP_LOGIN"); 919 if (user == NULL || *user == '\0') 920 user = FTP_ANONYMOUS_USER; 921 if (purl && url->port == fetch_default_port(url->scheme)) 922 e = ftp_cmd(conn, "USER %s@%s", user, url->host); 923 else if (purl) 924 e = ftp_cmd(conn, "USER %s@%s@%d", user, url->host, url->port); 925 else 926 e = ftp_cmd(conn, "USER %s", user); 927 928 /* did the server request a password? */ 929 if (e == FTP_NEED_PASSWORD) { 930 pwd = url->pwd; 931 if (*pwd == '\0') 932 pwd = getenv("FTP_PASSWORD"); 933 if (pwd == NULL || *pwd == '\0') { 934 if ((logname = getlogin()) == NULL) 935 logname = FTP_ANONYMOUS_USER; 936 if ((len = snprintf(pbuf, MAXLOGNAME + 1, "%s@", logname)) < 0) 937 len = 0; 938 else if (len > MAXLOGNAME) 939 len = MAXLOGNAME; 940 gethostname(pbuf + len, sizeof(pbuf) - len); 941 pwd = pbuf; 942 } 943 e = ftp_cmd(conn, "PASS %s", pwd); 944 } 945 946 return (e); 947 } 948 949 /* 950 * Log on to FTP server 951 */ 952 static conn_t * 953 ftp_connect(struct url *url, struct url *purl, const char *flags) 954 { 955 conn_t *conn; 956 int e, direct, verbose; 957 #ifdef INET6 958 int af = AF_UNSPEC; 959 #else 960 int af = AF_INET; 961 #endif 962 963 direct = CHECK_FLAG('d'); 964 verbose = CHECK_FLAG('v'); 965 if (CHECK_FLAG('4')) 966 af = AF_INET; 967 else if (CHECK_FLAG('6')) 968 af = AF_INET6; 969 970 if (direct) 971 purl = NULL; 972 973 /* check for proxy */ 974 if (purl) { 975 /* XXX proxy authentication! */ 976 conn = fetch_connect(purl->host, purl->port, af, verbose); 977 } else { 978 /* no proxy, go straight to target */ 979 conn = fetch_connect(url->host, url->port, af, verbose); 980 purl = NULL; 981 } 982 983 /* check connection */ 984 if (conn == NULL) 985 /* fetch_connect() has already set an error code */ 986 return (NULL); 987 988 /* expect welcome message */ 989 if ((e = ftp_chkerr(conn)) != FTP_SERVICE_READY) 990 goto fouch; 991 992 /* authenticate */ 993 if ((e = ftp_authenticate(conn, url, purl)) != FTP_LOGGED_IN) 994 goto fouch; 995 996 /* TODO: Request extended features supported, if any (RFC 3659). */ 997 998 /* done */ 999 return (conn); 1000 1001 fouch: 1002 if (e != -1) 1003 ftp_seterr(e); 1004 fetch_close(conn); 1005 return (NULL); 1006 } 1007 1008 /* 1009 * Disconnect from server 1010 */ 1011 static void 1012 ftp_disconnect(conn_t *conn) 1013 { 1014 (void)ftp_cmd(conn, "QUIT"); 1015 if (conn == cached_connection && conn->ref == 1) 1016 cached_connection = NULL; 1017 fetch_close(conn); 1018 } 1019 1020 /* 1021 * Check if we're already connected 1022 */ 1023 static int 1024 ftp_isconnected(struct url *url) 1025 { 1026 return (cached_connection 1027 && (strcmp(url->host, cached_host.host) == 0) 1028 && (strcmp(url->user, cached_host.user) == 0) 1029 && (strcmp(url->pwd, cached_host.pwd) == 0) 1030 && (url->port == cached_host.port)); 1031 } 1032 1033 /* 1034 * Check the cache, reconnect if no luck 1035 */ 1036 static conn_t * 1037 ftp_cached_connect(struct url *url, struct url *purl, const char *flags) 1038 { 1039 conn_t *conn; 1040 int e; 1041 1042 /* set default port */ 1043 if (!url->port) 1044 url->port = fetch_default_port(url->scheme); 1045 1046 /* try to use previously cached connection */ 1047 if (ftp_isconnected(url)) { 1048 e = ftp_cmd(cached_connection, "NOOP"); 1049 if (e == FTP_OK || e == FTP_SYNTAX_ERROR) 1050 return (fetch_ref(cached_connection)); 1051 } 1052 1053 /* connect to server */ 1054 if ((conn = ftp_connect(url, purl, flags)) == NULL) 1055 return (NULL); 1056 if (cached_connection) 1057 ftp_disconnect(cached_connection); 1058 cached_connection = fetch_ref(conn); 1059 memcpy(&cached_host, url, sizeof(*url)); 1060 return (conn); 1061 } 1062 1063 /* 1064 * Check the proxy settings 1065 */ 1066 static struct url * 1067 ftp_get_proxy(struct url * url, const char *flags) 1068 { 1069 struct url *purl; 1070 char *p; 1071 1072 if (flags != NULL && strchr(flags, 'd') != NULL) 1073 return (NULL); 1074 if (fetch_no_proxy_match(url->host)) 1075 return (NULL); 1076 if (((p = getenv("FTP_PROXY")) || (p = getenv("ftp_proxy")) || 1077 (p = getenv("HTTP_PROXY")) || (p = getenv("http_proxy"))) && 1078 *p && (purl = fetchParseURL(p)) != NULL) { 1079 if (!*purl->scheme) { 1080 if (getenv("FTP_PROXY") || getenv("ftp_proxy")) 1081 strcpy(purl->scheme, SCHEME_FTP); 1082 else 1083 strcpy(purl->scheme, SCHEME_HTTP); 1084 } 1085 if (!purl->port) 1086 purl->port = fetch_default_proxy_port(purl->scheme); 1087 if (strcasecmp(purl->scheme, SCHEME_FTP) == 0 || 1088 strcasecmp(purl->scheme, SCHEME_HTTP) == 0) 1089 return (purl); 1090 fetchFreeURL(purl); 1091 } 1092 return (NULL); 1093 } 1094 1095 /* 1096 * Process an FTP request 1097 */ 1098 FILE * 1099 ftp_request(struct url *url, const char *op, struct url_stat *us, 1100 struct url *purl, const char *flags) 1101 { 1102 conn_t *conn; 1103 int oflag; 1104 1105 /* check if we should use HTTP instead */ 1106 if (purl && strcasecmp(purl->scheme, SCHEME_HTTP) == 0) { 1107 if (strcmp(op, "STAT") == 0) 1108 return (http_request(url, "HEAD", us, purl, flags)); 1109 else if (strcmp(op, "RETR") == 0) 1110 return (http_request(url, "GET", us, purl, flags)); 1111 /* 1112 * Our HTTP code doesn't support PUT requests yet, so try 1113 * a direct connection. 1114 */ 1115 } 1116 1117 /* connect to server */ 1118 conn = ftp_cached_connect(url, purl, flags); 1119 if (purl) 1120 fetchFreeURL(purl); 1121 if (conn == NULL) 1122 return (NULL); 1123 1124 /* change directory */ 1125 if (ftp_cwd(conn, url->doc) == -1) 1126 goto errsock; 1127 1128 /* stat file */ 1129 if (us && ftp_stat(conn, url->doc, us) == -1 1130 && fetchLastErrCode != FETCH_PROTO 1131 && fetchLastErrCode != FETCH_UNAVAIL) 1132 goto errsock; 1133 1134 /* just a stat */ 1135 if (strcmp(op, "STAT") == 0) { 1136 --conn->ref; 1137 ftp_disconnect(conn); 1138 return (FILE *)1; /* bogus return value */ 1139 } 1140 if (strcmp(op, "STOR") == 0 || strcmp(op, "APPE") == 0) 1141 oflag = O_WRONLY; 1142 else 1143 oflag = O_RDONLY; 1144 1145 /* initiate the transfer */ 1146 return (ftp_transfer(conn, op, url->doc, oflag, url->offset, flags)); 1147 1148 errsock: 1149 ftp_disconnect(conn); 1150 return (NULL); 1151 } 1152 1153 /* 1154 * Get and stat file 1155 */ 1156 FILE * 1157 fetchXGetFTP(struct url *url, struct url_stat *us, const char *flags) 1158 { 1159 return (ftp_request(url, "RETR", us, ftp_get_proxy(url, flags), flags)); 1160 } 1161 1162 /* 1163 * Get file 1164 */ 1165 FILE * 1166 fetchGetFTP(struct url *url, const char *flags) 1167 { 1168 return (fetchXGetFTP(url, NULL, flags)); 1169 } 1170 1171 /* 1172 * Put file 1173 */ 1174 FILE * 1175 fetchPutFTP(struct url *url, const char *flags) 1176 { 1177 return (ftp_request(url, CHECK_FLAG('a') ? "APPE" : "STOR", NULL, 1178 ftp_get_proxy(url, flags), flags)); 1179 } 1180 1181 /* 1182 * Get file stats 1183 */ 1184 int 1185 fetchStatFTP(struct url *url, struct url_stat *us, const char *flags) 1186 { 1187 FILE *f; 1188 1189 f = ftp_request(url, "STAT", us, ftp_get_proxy(url, flags), flags); 1190 if (f == NULL) 1191 return (-1); 1192 /* 1193 * When op is "STAT", ftp_request() will return either NULL or 1194 * (FILE *)1, never a valid FILE *, so we mustn't fclose(f) before 1195 * returning, as it would cause a segfault. 1196 */ 1197 return (0); 1198 } 1199 1200 /* 1201 * List a directory 1202 */ 1203 struct url_ent * 1204 fetchListFTP(struct url *url __unused, const char *flags __unused) 1205 { 1206 warnx("fetchListFTP(): not implemented"); 1207 return (NULL); 1208 } 1209