1 /* $NetBSD: ftp.c,v 1.181 2026/02/07 03:11:20 lukem Exp $ */ 2 3 /*- 4 * Copyright (c) 1996-2026 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Luke Mewburn. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * Copyright (c) 1985, 1989, 1993, 1994 34 * The Regents of the University of California. All rights reserved. 35 * 36 * Redistribution and use in source and binary forms, with or without 37 * modification, are permitted provided that the following conditions 38 * are met: 39 * 1. Redistributions of source code must retain the above copyright 40 * notice, this list of conditions and the following disclaimer. 41 * 2. Redistributions in binary form must reproduce the above copyright 42 * notice, this list of conditions and the following disclaimer in the 43 * documentation and/or other materials provided with the distribution. 44 * 3. Neither the name of the University nor the names of its contributors 45 * may be used to endorse or promote products derived from this software 46 * without specific prior written permission. 47 * 48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 58 * SUCH DAMAGE. 59 */ 60 61 /* 62 * Copyright (C) 1997 and 1998 WIDE Project. 63 * All rights reserved. 64 * 65 * Redistribution and use in source and binary forms, with or without 66 * modification, are permitted provided that the following conditions 67 * are met: 68 * 1. Redistributions of source code must retain the above copyright 69 * notice, this list of conditions and the following disclaimer. 70 * 2. Redistributions in binary form must reproduce the above copyright 71 * notice, this list of conditions and the following disclaimer in the 72 * documentation and/or other materials provided with the distribution. 73 * 3. Neither the name of the project nor the names of its contributors 74 * may be used to endorse or promote products derived from this software 75 * without specific prior written permission. 76 * 77 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 78 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 79 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 80 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 81 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 82 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 83 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 84 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 85 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 86 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 87 * SUCH DAMAGE. 88 */ 89 90 #include <sys/cdefs.h> 91 #ifndef lint 92 #if 0 93 static char sccsid[] = "@(#)ftp.c 8.6 (Berkeley) 10/27/94"; 94 #else 95 __RCSID("$NetBSD: ftp.c,v 1.181 2026/02/07 03:11:20 lukem Exp $"); 96 #endif 97 #endif /* not lint */ 98 99 #include <sys/types.h> 100 #include <sys/stat.h> 101 #include <sys/socket.h> 102 #include <sys/time.h> 103 104 #include <netinet/in.h> 105 #include <netinet/in_systm.h> 106 #include <netinet/ip.h> 107 #include <arpa/inet.h> 108 #include <arpa/ftp.h> 109 #include <arpa/telnet.h> 110 111 #include <ctype.h> 112 #include <err.h> 113 #include <errno.h> 114 #include <fcntl.h> 115 #include <netdb.h> 116 #include <stdio.h> 117 #include <stdlib.h> 118 #include <string.h> 119 #include <time.h> 120 #include <unistd.h> 121 #include <stdarg.h> 122 123 #include "ftp_var.h" 124 125 static volatile sig_atomic_t abrtflag; 126 static volatile sig_atomic_t timeoutflag; 127 128 static sigjmp_buf ptabort; 129 static int ptabflg; 130 static int ptflag = 0; 131 static char pasv[BUFSIZ]; /* passive port for proxy data connection */ 132 size_t ftp_buflen = FTPBUFLEN; 133 134 static int empty(FILE *, FILE *, int); 135 __dead static void abort_squared(int); 136 137 struct sockinet { 138 union sockunion { 139 struct sockaddr_in su_sin; 140 #ifdef INET6 141 struct sockaddr_in6 su_sin6; 142 #endif 143 } si_su; 144 #if !defined(HAVE_STRUCT_SOCKADDR_IN_SIN_LEN) 145 int si_len; 146 #endif 147 }; 148 149 #if !defined(HAVE_STRUCT_SOCKADDR_IN_SIN_LEN) 150 # define su_len si_len 151 #else 152 # define su_len si_su.su_sin.sin_len 153 #endif 154 #define su_family si_su.su_sin.sin_family 155 #define su_port si_su.su_sin.sin_port 156 157 static struct sockinet myctladdr, hisctladdr, data_addr; 158 159 char * 160 hookup(const char *host, const char *port) 161 { 162 int s = -1, error; 163 struct addrinfo hints, *res, *res0; 164 static char hostnamebuf[MAXHOSTNAMELEN]; 165 socklen_t len; 166 int on = 1; 167 168 memset((char *)&hisctladdr, 0, sizeof (hisctladdr)); 169 memset((char *)&myctladdr, 0, sizeof (myctladdr)); 170 memset(&hints, 0, sizeof(hints)); 171 hints.ai_flags = AI_CANONNAME; 172 hints.ai_family = family; 173 hints.ai_socktype = SOCK_STREAM; 174 hints.ai_protocol = 0; 175 error = getaddrinfo(host, port, &hints, &res0); 176 if (error) { 177 warnx("Can't lookup `%s:%s': %s", host, port, 178 (error == EAI_SYSTEM) ? strerror(errno) 179 : gai_strerror(error)); 180 code = -1; 181 return (0); 182 } 183 184 if (res0->ai_canonname) 185 (void)strlcpy(hostnamebuf, res0->ai_canonname, 186 sizeof(hostnamebuf)); 187 else 188 (void)strlcpy(hostnamebuf, host, sizeof(hostnamebuf)); 189 hostname = hostnamebuf; 190 191 for (res = res0; res; res = res->ai_next) { 192 char hname[NI_MAXHOST], sname[NI_MAXSERV]; 193 194 ai_unmapped(res); 195 if (getnameinfo(res->ai_addr, res->ai_addrlen, 196 hname, sizeof(hname), sname, sizeof(sname), 197 NI_NUMERICHOST | NI_NUMERICSERV) != 0) { 198 strlcpy(hname, "?", sizeof(hname)); 199 strlcpy(sname, "?", sizeof(sname)); 200 } 201 if (verbose && res0->ai_next) { 202 /* if we have multiple possibilities */ 203 #ifdef INET6 204 if(res->ai_family == AF_INET6) { 205 fprintf(ttyout, "Trying [%s]:%s ...\n", hname, 206 sname); 207 } else { 208 #endif 209 fprintf(ttyout, "Trying %s:%s ...\n", hname, 210 sname); 211 #ifdef INET6 212 } 213 #endif 214 } 215 s = socket(res->ai_family, SOCK_STREAM, res->ai_protocol); 216 if (s < 0) { 217 warn("Can't create socket for connection to `%s:%s'", 218 hname, sname); 219 continue; 220 } 221 if (ftp_connect(s, res->ai_addr, res->ai_addrlen, 222 verbose || !res->ai_next) < 0) { 223 close(s); 224 s = -1; 225 continue; 226 } 227 228 /* finally we got one */ 229 break; 230 } 231 if (s < 0) { 232 warnx("Can't connect to `%s:%s'", host, port); 233 code = -1; 234 freeaddrinfo(res0); 235 return 0; 236 } 237 memcpy(&hisctladdr.si_su, res->ai_addr, res->ai_addrlen); 238 hisctladdr.su_len = res->ai_addrlen; 239 freeaddrinfo(res0); 240 res0 = res = NULL; 241 242 len = hisctladdr.su_len; 243 if (getsockname(s, (struct sockaddr *)(void *)&myctladdr.si_su, &len) == -1) { 244 warn("Can't determine my address of connection to `%s:%s'", 245 host, port); 246 code = -1; 247 goto bad; 248 } 249 myctladdr.su_len = len; 250 251 #ifdef IPTOS_LOWDELAY 252 if (hisctladdr.su_family == AF_INET) { 253 int tos = IPTOS_LOWDELAY; 254 if (setsockopt(s, IPPROTO_IP, IP_TOS, 255 (void *)&tos, sizeof(tos)) == -1) { 256 DWARN("setsockopt %s (ignored)", 257 "IPTOS_LOWDELAY"); 258 } 259 } 260 #endif 261 cin = fdopen(s, "r"); 262 cout = fdopen(s, "w"); 263 if (cin == NULL || cout == NULL) { 264 warnx("Can't fdopen socket"); 265 if (cin) 266 (void)fclose(cin); 267 if (cout) 268 (void)fclose(cout); 269 code = -1; 270 goto bad; 271 } 272 if (verbose) 273 fprintf(ttyout, "Connected to %s.\n", hostname); 274 if (getreply(0) > 2) { /* read startup message from server */ 275 if (cin) 276 (void)fclose(cin); 277 if (cout) 278 (void)fclose(cout); 279 code = -1; 280 goto bad; 281 } 282 283 if (setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, 284 (void *)&on, sizeof(on)) == -1) { 285 DWARN("setsockopt %s (ignored)", "SO_KEEPALIVE"); 286 } 287 288 if (setsockopt(s, SOL_SOCKET, SO_OOBINLINE, 289 (void *)&on, sizeof(on)) == -1) { 290 DWARN("setsockopt %s (ignored)", "SO_OOBINLINE"); 291 } 292 293 return (hostname); 294 bad: 295 (void)close(s); 296 return (NULL); 297 } 298 299 void 300 cmdabort(int notused __unused) 301 { 302 int oerrno = errno; 303 304 sigint_raised = 1; 305 alarmtimer(0); 306 if (fromatty) 307 write(fileno(ttyout), "\n", 1); 308 abrtflag++; 309 if (ptflag) 310 siglongjmp(ptabort, 1); 311 errno = oerrno; 312 } 313 314 void 315 cmdtimeout(int notused __unused) 316 { 317 int oerrno = errno; 318 319 alarmtimer(0); 320 if (fromatty) 321 write(fileno(ttyout), "\n", 1); 322 timeoutflag++; 323 if (ptflag) 324 siglongjmp(ptabort, 1); 325 errno = oerrno; 326 } 327 328 static int 329 issighandler(sigfunc func) 330 { 331 return (func != SIG_IGN && 332 func != SIG_DFL && 333 #ifdef SIG_HOLD 334 func != SIG_HOLD && 335 #endif 336 func != SIG_ERR); 337 } 338 339 /*VARARGS*/ 340 int 341 command(const char *fmt, ...) 342 { 343 va_list ap; 344 int r; 345 sigfunc oldsigint; 346 347 #ifndef NO_DEBUG 348 if (ftp_debug) { 349 fputs("---> ", ttyout); 350 va_start(ap, fmt); 351 if (strncmp("PASS ", fmt, 5) == 0) 352 fputs("PASS XXXX", ttyout); 353 else if (strncmp("ACCT ", fmt, 5) == 0) 354 fputs("ACCT XXXX", ttyout); 355 else 356 vfprintf(ttyout, fmt, ap); 357 va_end(ap); 358 putc('\n', ttyout); 359 } 360 #endif 361 if (cout == NULL) { 362 warnx("No control connection for command"); 363 code = -1; 364 return (0); 365 } 366 367 abrtflag = 0; 368 369 oldsigint = xsignal(SIGINT, cmdabort); 370 371 va_start(ap, fmt); 372 vfprintf(cout, fmt, ap); /* TODO: handle EINTR? */ 373 va_end(ap); 374 fputs("\r\n", cout); /* TODO: handle EINTR? */ 375 (void)fflush(cout); 376 cpend = 1; 377 r = getreply(!strcmp(fmt, "QUIT")); 378 if (abrtflag && issighandler(oldsigint)) { 379 (*oldsigint)(SIGINT); 380 } 381 (void)xsignal(SIGINT, oldsigint); 382 return (r); 383 } 384 385 static const char *m421[] = { 386 "remote server timed out. Connection closed", 387 "user interrupt. Connection closed", 388 "remote server has closed connection", 389 }; 390 391 int 392 getreply(int expecteof) 393 { 394 char current_line[BUFSIZ]; /* last line of previous reply */ 395 int c, n, lineno; 396 int dig; 397 int originalcode = 0, continuation = 0; 398 sigfunc oldsigint, oldsigalrm; 399 int pflag = 0; 400 char *cp, *pt = pasv; 401 int cin_errno = 0; 402 403 abrtflag = 0; 404 timeoutflag = 0; 405 406 oldsigint = xsignal(SIGINT, cmdabort); 407 oldsigalrm = xsignal(SIGALRM, cmdtimeout); 408 409 for (lineno = 0 ;; lineno++) { 410 dig = n = code = 0; 411 cp = current_line; 412 while (alarmtimer(quit_time ? quit_time : 60), 413 ((c = ftp_getc(cin, &cin_errno)) != '\n')) { 414 if (c == IAC) { /* handle telnet commands */ 415 switch (c = ftp_getc(cin, &cin_errno)) { 416 case WILL: 417 case WONT: 418 c = ftp_getc(cin, &cin_errno); 419 if (c != EOF) { 420 fprintf(cout, "%c%c%c", 421 IAC, DONT, c); 422 /* TODO: handle EINTR? */ 423 (void)fflush(cout); 424 } 425 break; 426 case DO: 427 case DONT: 428 c = ftp_getc(cin, &cin_errno); 429 if (c != EOF) { 430 fprintf(cout, "%c%c%c", 431 IAC, WONT, c); 432 /* TODO: handle EINTR? */ 433 (void)fflush(cout); 434 } 435 break; 436 default: 437 break; 438 } 439 continue; 440 } 441 dig++; 442 if (c == EOF) { 443 /* 444 * these will get trashed by pswitch() 445 * in lostpeer() 446 */ 447 int reply_timeoutflag = timeoutflag; 448 int reply_abrtflag = abrtflag; 449 450 alarmtimer(0); 451 if (expecteof && feof(cin)) { 452 (void)xsignal(SIGINT, oldsigint); 453 (void)xsignal(SIGALRM, oldsigalrm); 454 code = 221; 455 return (0); 456 } 457 cpend = 0; 458 lostpeer(0); 459 if (verbose) { 460 size_t midx; 461 if (reply_timeoutflag) 462 midx = 0; 463 else if (reply_abrtflag) 464 midx = 1; 465 else 466 midx = 2; 467 (void)fprintf(ttyout, 468 "421 Service not available, %s.\n", m421[midx]); 469 (void)fflush(ttyout); 470 } 471 code = 421; 472 (void)xsignal(SIGINT, oldsigint); 473 (void)xsignal(SIGALRM, oldsigalrm); 474 return (4); 475 } 476 if (c != '\r' && (verbose > 0 || 477 ((verbose > -1 && n == '5' && dig > 4) && 478 (((!n && c < '5') || (n && n < '5')) 479 || !retry_connect)))) { 480 if (proxflag && 481 (dig == 1 || (dig == 5 && verbose == 0))) 482 fprintf(ttyout, "%s:", hostname); 483 (void)putc(c, ttyout); 484 } 485 if (dig < 4 && isdigit(c)) 486 code = code * 10 + (c - '0'); 487 if (!pflag && (code == 227 || code == 228)) 488 pflag = 1; 489 else if (!pflag && code == 229) 490 pflag = 100; 491 if (dig > 4 && pflag == 1 && isdigit(c)) 492 pflag = 2; 493 if (pflag == 2) { 494 if (c != '\r' && c != ')') { 495 if (pt < &pasv[sizeof(pasv) - 1]) 496 *pt++ = c; 497 } else { 498 *pt = '\0'; 499 pflag = 3; 500 } 501 } 502 if (pflag == 100 && c == '(') 503 pflag = 2; 504 if (dig == 4 && c == '-') { 505 if (continuation) 506 code = 0; 507 continuation++; 508 } 509 if (n == 0) 510 n = c; 511 if (cp < ¤t_line[sizeof(current_line) - 1]) 512 *cp++ = c; 513 } 514 if (verbose > 0 || ((verbose > -1 && n == '5') && 515 (n < '5' || !retry_connect))) { 516 (void)putc(c, ttyout); 517 (void)fflush(ttyout); 518 } 519 if (cp[-1] == '\r') 520 cp[-1] = '\0'; 521 *cp = '\0'; 522 if (lineno == 0) 523 (void)strlcpy(reply_string, current_line, 524 sizeof(reply_string)); 525 if (lineno > 0 && code == 0 && reply_callback != NULL) 526 (*reply_callback)(current_line); 527 if (continuation && code != originalcode) { 528 if (originalcode == 0) 529 originalcode = code; 530 continue; 531 } 532 if (n != '1') 533 cpend = 0; 534 alarmtimer(0); 535 (void)xsignal(SIGINT, oldsigint); 536 (void)xsignal(SIGALRM, oldsigalrm); 537 if (code == 421 || originalcode == 421) 538 lostpeer(0); 539 if (abrtflag && oldsigint != cmdabort && 540 issighandler(oldsigint)) { 541 (*oldsigint)(SIGINT); 542 } 543 if (timeoutflag && oldsigalrm != cmdtimeout && 544 issighandler(oldsigalrm)) { 545 (*oldsigalrm)(SIGINT); 546 } 547 return (n - '0'); 548 } 549 } 550 551 static int 552 empty(FILE *ecin, FILE *din, int sec) 553 { 554 int nr, nfd; 555 struct pollfd pfd[2]; 556 557 nfd = 0; 558 if (ecin) { 559 pfd[nfd].fd = fileno(ecin); 560 pfd[nfd++].events = POLLIN; 561 } 562 563 if (din) { 564 pfd[nfd].fd = fileno(din); 565 pfd[nfd++].events = POLLIN; 566 } 567 568 if ((nr = ftp_poll(pfd, nfd, sec * 1000)) <= 0) 569 return nr; 570 571 nr = 0; 572 nfd = 0; 573 if (ecin) 574 nr |= (pfd[nfd++].revents & POLLIN) ? 1 : 0; 575 if (din) 576 nr |= (pfd[nfd++].revents & POLLIN) ? 2 : 0; 577 return nr; 578 } 579 580 static sigjmp_buf xferabort; 581 582 __dead static void 583 abortxfer(int notused __unused) 584 { 585 char msgbuf[100]; 586 size_t len; 587 588 sigint_raised = 1; 589 alarmtimer(0); 590 mflag = 0; 591 abrtflag = 0; 592 switch (direction[0]) { 593 case 'r': 594 strlcpy(msgbuf, "\nreceive", sizeof(msgbuf)); 595 break; 596 case 's': 597 strlcpy(msgbuf, "\nsend", sizeof(msgbuf)); 598 break; 599 default: 600 errx(1, "abortxfer: unknown direction `%s'", direction); 601 } 602 len = strlcat(msgbuf, " aborted. Waiting for remote to finish abort.\n", 603 sizeof(msgbuf)); 604 write(fileno(ttyout), msgbuf, len); 605 siglongjmp(xferabort, 1); 606 } 607 608 /* 609 * Read data from infd & write to outfd, using buf/bufsize as the temporary 610 * buffer, dealing with short reads or writes. 611 * If rate_limit != 0, rate-limit the transfer. 612 * If hash_interval != 0, putc('c', ttyout) every hash_interval bytes. 613 * Updates global variables: bytes. 614 * Returns 0 if ok, 1 if there was a read error, 2 if there was a write error. 615 * In the case of error, errno contains the appropriate error code. 616 */ 617 static int 618 copy_bytes(int infd, int outfd, char *buf, size_t bufsize, 619 int rate_limit, int hash_interval) 620 { 621 volatile off_t hashc; 622 ssize_t inc, outc; 623 char *bufp; 624 struct timeval tvthen, tvnow, tvdiff; 625 off_t bufrem, bufchunk; 626 int serr; 627 628 hashc = hash_interval; 629 if (rate_limit) 630 bufchunk = rate_limit; 631 else 632 bufchunk = bufsize; 633 634 for (;;) { 635 if (rate_limit) { 636 (void)gettimeofday(&tvthen, NULL); 637 } 638 errno = 0; 639 inc = outc = 0; 640 /* copy bufchunk at a time */ 641 bufrem = bufchunk; 642 while (bufrem > 0) { 643 inc = read(infd, buf, MIN((off_t)bufsize, bufrem)); 644 if (inc < 0) { 645 if (errno == EINTR || errno == EAGAIN) { 646 continue; 647 } 648 goto copy_done; 649 } else if (inc == 0) { 650 goto copy_done; 651 } 652 bytes += inc; 653 bufrem -= inc; 654 bufp = buf; 655 while (inc > 0) { 656 outc = write(outfd, bufp, inc); 657 if (outc < 0) { 658 if (errno == EINTR || errno == EAGAIN) { 659 continue; 660 } 661 goto copy_done; 662 } 663 inc -= outc; 664 bufp += outc; 665 } 666 if (hash_interval) { 667 while (bytes >= hashc) { 668 (void)putc('#', ttyout); 669 hashc += hash_interval; 670 } 671 (void)fflush(ttyout); 672 } 673 } 674 if (rate_limit) { /* rate limited; wait if necessary */ 675 for (;;) { 676 (void)gettimeofday(&tvnow, NULL); 677 timersub(&tvnow, &tvthen, &tvdiff); 678 if (tvdiff.tv_sec > 0) 679 break; 680 usleep(1000000 - tvdiff.tv_usec); 681 } 682 } 683 } 684 685 copy_done: 686 serr = errno; 687 if (hash_interval && bytes > 0) { 688 if (bytes < hash_interval) 689 (void)putc('#', ttyout); 690 (void)putc('\n', ttyout); 691 (void)fflush(ttyout); 692 } 693 errno = serr; 694 if (inc == -1) 695 return 1; 696 if (outc == -1) 697 return 2; 698 699 return 0; 700 } 701 702 void 703 sendrequest(const char *cmd, const char *local, const char *remote, 704 int printnames) 705 { 706 struct stat st; 707 int c; 708 FILE *volatile fin; 709 int fin_errno = 0; 710 FILE *volatile dout; 711 int dout_errno = 0; 712 int (*volatile closefunc)(FILE *); 713 sigfunc volatile oldintr; 714 sigfunc volatile oldpipe; 715 off_t volatile hashbytes; 716 int hash_interval; 717 const char *lmode; 718 static size_t bufsize; 719 static char *buf; 720 int oprogress; 721 722 hashbytes = mark; 723 direction = "sent"; 724 dout = NULL; 725 bytes = 0; 726 filesize = -1; 727 oprogress = progress; 728 if (verbose && printnames) { 729 if (*local != '-') 730 fprintf(ttyout, "local: %s ", local); 731 if (remote) 732 fprintf(ttyout, "remote: %s\n", remote); 733 } 734 if (proxy) { 735 proxtrans(cmd, local, remote); 736 return; 737 } 738 if (curtype != type) 739 changetype(type, 0); 740 closefunc = NULL; 741 oldintr = SIG_ERR; 742 oldpipe = SIG_ERR; 743 lmode = "w"; 744 if (sigsetjmp(xferabort, 1)) { 745 while (cpend) 746 (void)getreply(0); 747 code = -1; 748 goto cleanupsend; 749 } 750 (void)xsignal(SIGQUIT, psummary); 751 oldintr = xsignal(SIGINT, abortxfer); 752 if (strcmp(local, "-") == 0) { 753 fin = stdin; 754 progress = 0; 755 } else if (*local == '|') { 756 oldpipe = xsignal(SIGPIPE, SIG_IGN); 757 fin = popen(local + 1, "r"); 758 if (fin == NULL) { 759 warn("Can't execute `%s'", local + 1); 760 code = -1; 761 goto cleanupsend; 762 } 763 progress = 0; 764 closefunc = pclose; 765 } else { 766 fin = fopen(local, "r"); 767 if (fin == NULL) { 768 warn("Can't open `%s'", local); 769 code = -1; 770 goto cleanupsend; 771 } 772 closefunc = fclose; 773 if (fstat(fileno(fin), &st) < 0 || !S_ISREG(st.st_mode)) { 774 fprintf(ttyout, "%s: not a plain file.\n", local); 775 code = -1; 776 goto cleanupsend; 777 } 778 filesize = st.st_size; 779 } 780 if (initconn()) { 781 code = -1; 782 goto cleanupsend; 783 } 784 if (sigsetjmp(xferabort, 1)) 785 goto abort; 786 787 if (restart_point && 788 (strcmp(cmd, "STOR") == 0 || strcmp(cmd, "APPE") == 0)) { 789 off_t rc; 790 791 rc = -1; 792 switch (curtype) { 793 case TYPE_A: 794 rc = fseeko(fin, restart_point, SEEK_SET); 795 break; 796 case TYPE_I: 797 case TYPE_L: 798 rc = lseek(fileno(fin), restart_point, SEEK_SET); 799 break; 800 } 801 if (rc < 0) { 802 warn("Can't seek to restart `%s'", local); 803 goto cleanupsend; 804 } 805 if (command("REST " LLF, (LLT)restart_point) != CONTINUE) 806 goto cleanupsend; 807 lmode = "r+"; 808 } 809 if (remote) { 810 if (command("%s %s", cmd, remote) != PRELIM) 811 goto cleanupsend; 812 } else { 813 if (command("%s", cmd) != PRELIM) 814 goto cleanupsend; 815 } 816 dirchange = 1; 817 dout = dataconn(lmode); 818 if (dout == NULL) 819 goto abort; 820 821 /* Resize buf to clamped sndbuf_size */ 822 if (bufsize == 0 || (size_t)sndbuf_size != bufsize) { 823 if (buf) 824 (void)free(buf); 825 if (sndbuf_size == 0) 826 bufsize = XFERBUFMAX; 827 else 828 bufsize = MAX(XFERBUFMIN, MIN(sndbuf_size, XFERBUFMAX)); 829 buf = ftp_malloc(bufsize); 830 DPRINTF("resized buf to bufsize %zu using sndbuf_size %d\n", 831 bufsize, sndbuf_size); 832 } 833 834 progressmeter(-1); 835 if (oldpipe == SIG_ERR) { 836 oldpipe = xsignal(SIGPIPE, SIG_IGN); 837 } 838 hash_interval = (hash && (!progress || filesize < 0)) ? mark : 0; 839 840 switch (curtype) { 841 842 case TYPE_I: 843 case TYPE_L: 844 c = copy_bytes(fileno(fin), fileno(dout), buf, bufsize, 845 rate_put, hash_interval); 846 if (c == 1) { 847 warn("Reading `%s'", local); 848 } else if (c == 2) { 849 if (errno != EPIPE) 850 warn("Writing to network"); 851 bytes = -1; 852 } 853 break; 854 855 case TYPE_A: 856 while ((c = ftp_getc(fin, &fin_errno)) != EOF) { 857 if (c == '\n') { 858 while (hash_interval && bytes >= hashbytes) { 859 (void)putc('#', ttyout); 860 (void)fflush(ttyout); 861 hashbytes += mark; 862 } 863 if (ftp_putc('\r', dout, &dout_errno) == EOF 864 || ferror(dout)) 865 break; 866 bytes++; 867 } 868 if (ftp_putc(c, dout, &dout_errno) == EOF 869 || ferror(dout)) 870 break; 871 bytes++; 872 } 873 if (hash_interval) { 874 if (bytes < hashbytes) 875 (void)putc('#', ttyout); 876 (void)putc('\n', ttyout); 877 } 878 if (ferror(fin)) { 879 errno = fin_errno; 880 warn("Reading `%s'", local); 881 } 882 if (ferror(dout)) { 883 errno = dout_errno; 884 if (errno != EPIPE) 885 warn("Writing to network"); 886 bytes = -1; 887 } 888 break; 889 } 890 891 progressmeter(1); 892 if (closefunc != NULL) { 893 (*closefunc)(fin); 894 fin = NULL; 895 } 896 (void)fclose(dout); 897 dout = NULL; 898 (void)getreply(0); 899 if (bytes > 0) 900 ptransfer(0); 901 goto cleanupsend; 902 903 abort: 904 (void)xsignal(SIGINT, oldintr); 905 oldintr = SIG_ERR; 906 if (!cpend) { 907 code = -1; 908 goto cleanupsend; 909 } 910 if (data >= 0) { 911 (void)close(data); 912 data = -1; 913 } 914 if (dout) { 915 (void)fclose(dout); 916 dout = NULL; 917 } 918 (void)getreply(0); 919 code = -1; 920 if (bytes > 0) 921 ptransfer(0); 922 923 cleanupsend: 924 if (oldintr != SIG_ERR) 925 (void)xsignal(SIGINT, oldintr); 926 if (oldpipe != SIG_ERR) 927 (void)xsignal(SIGPIPE, oldpipe); 928 if (data >= 0) { 929 (void)close(data); 930 data = -1; 931 } 932 if (closefunc != NULL && fin != NULL) 933 (*closefunc)(fin); 934 if (dout) 935 (void)fclose(dout); 936 progress = oprogress; 937 restart_point = 0; 938 bytes = 0; 939 } 940 941 void 942 recvrequest(const char *cmd, char *volatile local, const char *remote, 943 const char *lmode, int printnames, int ignorespecial) 944 { 945 FILE *volatile fout; 946 int fout_errno = 0; 947 FILE *volatile din; 948 int din_errno = 0; 949 int (*volatile closefunc)(FILE *); 950 sigfunc volatile oldintr; 951 sigfunc volatile oldpipe; 952 int c, d; 953 int volatile is_retr; 954 int volatile tcrflag; 955 int volatile bare_lfs; 956 static size_t bufsize; 957 static char *buf; 958 off_t volatile hashbytes; 959 int hash_interval; 960 struct stat st; 961 time_t mtime; 962 struct timeval tval[2]; 963 int oprogress; 964 int opreserve; 965 966 fout = NULL; 967 din = NULL; 968 hashbytes = mark; 969 direction = "received"; 970 bytes = 0; 971 bare_lfs = 0; 972 filesize = -1; 973 oprogress = progress; 974 opreserve = preserve; 975 is_retr = (strcmp(cmd, "RETR") == 0); 976 if (is_retr && verbose && printnames) { 977 if (ignorespecial || *local != '-') 978 fprintf(ttyout, "local: %s ", local); 979 if (remote) 980 fprintf(ttyout, "remote: %s\n", remote); 981 } 982 if (proxy && is_retr) { 983 proxtrans(cmd, local, remote); 984 return; 985 } 986 closefunc = NULL; 987 oldintr = SIG_ERR; 988 oldpipe = SIG_ERR; 989 tcrflag = !crflag && is_retr; 990 if (sigsetjmp(xferabort, 1)) { 991 while (cpend) 992 (void)getreply(0); 993 code = -1; 994 goto cleanuprecv; 995 } 996 (void)xsignal(SIGQUIT, psummary); 997 oldintr = xsignal(SIGINT, abortxfer); 998 if (ignorespecial || (strcmp(local, "-") && *local != '|')) { 999 if (access(local, W_OK) < 0) { 1000 char *dir = strrchr(local, '/'); 1001 1002 if (errno != ENOENT && errno != EACCES) { 1003 warn("Can't access `%s'", local); 1004 code = -1; 1005 goto cleanuprecv; 1006 } 1007 if (dir != NULL) 1008 *dir = 0; 1009 d = access(dir == local ? "/" : 1010 dir ? local : ".", W_OK); 1011 if (dir != NULL) 1012 *dir = '/'; 1013 if (d < 0) { 1014 warn("Can't access `%s'", local); 1015 code = -1; 1016 goto cleanuprecv; 1017 } 1018 if (!runique && errno == EACCES && 1019 chmod(local, (S_IRUSR|S_IWUSR)) < 0) { 1020 warn("Can't chmod `%s'", local); 1021 code = -1; 1022 goto cleanuprecv; 1023 } 1024 if (runique && errno == EACCES && 1025 (local = gunique(local)) == NULL) { 1026 code = -1; 1027 goto cleanuprecv; 1028 } 1029 } 1030 else if (runique && (local = gunique(local)) == NULL) { 1031 code = -1; 1032 goto cleanuprecv; 1033 } 1034 } 1035 if (!is_retr) { 1036 if (curtype != TYPE_A) 1037 changetype(TYPE_A, 0); 1038 } else { 1039 if (curtype != type) 1040 changetype(type, 0); 1041 filesize = remotesize(remote, 0); 1042 if (code == 421 || code == -1) 1043 goto cleanuprecv; 1044 } 1045 if (initconn()) { 1046 code = -1; 1047 goto cleanuprecv; 1048 } 1049 if (sigsetjmp(xferabort, 1)) 1050 goto abort; 1051 if (is_retr && restart_point && 1052 command("REST " LLF, (LLT) restart_point) != CONTINUE) 1053 goto cleanuprecv; 1054 if (! EMPTYSTRING(remote)) { 1055 if (command("%s %s", cmd, remote) != PRELIM) 1056 goto cleanuprecv; 1057 } else { 1058 if (command("%s", cmd) != PRELIM) 1059 goto cleanuprecv; 1060 } 1061 din = dataconn("r"); 1062 if (din == NULL) 1063 goto abort; 1064 if (!ignorespecial && strcmp(local, "-") == 0) { 1065 fout = stdout; 1066 progress = 0; 1067 preserve = 0; 1068 } else if (!ignorespecial && *local == '|') { 1069 oldpipe = xsignal(SIGPIPE, SIG_IGN); 1070 fout = popen(local + 1, "w"); 1071 if (fout == NULL) { 1072 warn("Can't execute `%s'", local+1); 1073 goto abort; 1074 } 1075 progress = 0; 1076 preserve = 0; 1077 closefunc = pclose; 1078 } else { 1079 fout = fopen(local, lmode); 1080 if (fout == NULL) { 1081 warn("Can't open `%s'", local); 1082 goto abort; 1083 } 1084 closefunc = fclose; 1085 } 1086 1087 if (fstat(fileno(fout), &st) != -1 && !S_ISREG(st.st_mode)) { 1088 progress = 0; 1089 preserve = 0; 1090 } 1091 1092 /* Resize buf to clamped rcvbuf_size */ 1093 if (bufsize == 0 || (size_t)rcvbuf_size != bufsize) { 1094 if (buf) 1095 (void)free(buf); 1096 if (rcvbuf_size == 0) 1097 bufsize = XFERBUFMAX; 1098 else 1099 bufsize = MAX(XFERBUFMIN, MIN(rcvbuf_size, XFERBUFMAX)); 1100 buf = ftp_malloc(bufsize); 1101 DPRINTF("resized buf to bufsize %zu using rcvbuf_size %d\n", 1102 bufsize, rcvbuf_size); 1103 } 1104 1105 progressmeter(-1); 1106 hash_interval = (hash && (!progress || filesize < 0)) ? mark : 0; 1107 1108 switch (curtype) { 1109 1110 case TYPE_I: 1111 case TYPE_L: 1112 if (is_retr && restart_point && 1113 lseek(fileno(fout), restart_point, SEEK_SET) < 0) { 1114 warn("Can't seek to restart `%s'", local); 1115 goto cleanuprecv; 1116 } 1117 c = copy_bytes(fileno(din), fileno(fout), buf, bufsize, 1118 rate_get, hash_interval); 1119 if (c == 1) { 1120 if (errno != EPIPE) 1121 warn("Reading from network"); 1122 bytes = -1; 1123 } else if (c == 2) { 1124 warn("Writing `%s'", local); 1125 } 1126 break; 1127 1128 case TYPE_A: 1129 if (is_retr && restart_point) { 1130 int ch; 1131 off_t i; 1132 1133 if (fseeko(fout, (off_t)0, SEEK_SET) < 0) 1134 goto done; 1135 for (i = 0; i++ < restart_point;) { 1136 if ((ch = ftp_getc(fout, &fout_errno)) == EOF) 1137 goto done; 1138 if (ch == '\n') 1139 i++; 1140 } 1141 if (fseeko(fout, (off_t)0, SEEK_CUR) < 0) { 1142 done: 1143 warn("Can't seek to restart `%s'", local); 1144 goto cleanuprecv; 1145 } 1146 } 1147 while ((c = ftp_getc(din, &din_errno)) != EOF) { 1148 if (c == '\n') 1149 bare_lfs++; 1150 while (c == '\r') { 1151 while (hash_interval && bytes >= hashbytes) { 1152 (void)putc('#', ttyout); 1153 (void)fflush(ttyout); 1154 hashbytes += mark; 1155 } 1156 bytes++; 1157 if ((c = ftp_getc(din, &din_errno)) != '\n' 1158 || tcrflag) { 1159 if (ftp_putc('\r', fout, &fout_errno) == EOF 1160 || ferror(fout)) { 1161 goto break2; 1162 } 1163 if (c == '\0') { 1164 bytes++; 1165 goto contin2; 1166 } 1167 if (c == EOF) 1168 goto contin2; 1169 } 1170 } 1171 if (ftp_putc(c, fout, &fout_errno) == EOF) 1172 break; 1173 bytes++; 1174 contin2: ; 1175 } 1176 break2: 1177 if (hash_interval) { 1178 if (bytes < hashbytes) 1179 (void)putc('#', ttyout); 1180 (void)putc('\n', ttyout); 1181 } 1182 if (ferror(din)) { 1183 errno = din_errno; 1184 if (errno != EPIPE) 1185 warn("Reading from network"); 1186 bytes = -1; 1187 } 1188 if (ferror(fout)) { 1189 errno = fout_errno; 1190 warn("Writing `%s'", local); 1191 } 1192 break; 1193 } 1194 1195 progressmeter(1); 1196 if (closefunc != NULL) { 1197 (*closefunc)(fout); 1198 fout = NULL; 1199 } 1200 (void)fclose(din); 1201 din = NULL; 1202 (void)getreply(0); 1203 if (bare_lfs) { 1204 fprintf(ttyout, 1205 "WARNING! %d bare linefeeds received in ASCII mode.\n", 1206 bare_lfs); 1207 fputs("File may not have transferred correctly.\n", ttyout); 1208 } 1209 if (bytes >= 0 && is_retr) { 1210 if (bytes > 0) 1211 ptransfer(0); 1212 if (preserve && (closefunc == fclose)) { 1213 mtime = remotemodtime(remote, 0); 1214 if (mtime != -1) { 1215 (void)gettimeofday(&tval[0], NULL); 1216 tval[1].tv_sec = mtime; 1217 tval[1].tv_usec = 0; 1218 if (utimes(local, tval) == -1) { 1219 fprintf(ttyout, 1220 "Can't change modification time on %s to %s", 1221 local, 1222 rfc2822time(localtime(&mtime))); 1223 } 1224 } 1225 } 1226 } 1227 goto cleanuprecv; 1228 1229 abort: 1230 /* 1231 * abort using RFC 959 recommended IP,SYNC sequence 1232 */ 1233 if (! sigsetjmp(xferabort, 1)) { 1234 /* this is the first call */ 1235 (void)xsignal(SIGINT, abort_squared); 1236 if (!cpend) { 1237 code = -1; 1238 goto cleanuprecv; 1239 } 1240 abort_remote(din); 1241 } 1242 code = -1; 1243 if (bytes > 0) 1244 ptransfer(0); 1245 1246 cleanuprecv: 1247 if (oldintr != SIG_ERR) 1248 (void)xsignal(SIGINT, oldintr); 1249 if (oldpipe != SIG_ERR) 1250 (void)xsignal(SIGPIPE, oldpipe); 1251 if (data >= 0) { 1252 (void)close(data); 1253 data = -1; 1254 } 1255 if (closefunc != NULL && fout != NULL) 1256 (*closefunc)(fout); 1257 if (din) 1258 (void)fclose(din); 1259 progress = oprogress; 1260 preserve = opreserve; 1261 bytes = 0; 1262 } 1263 1264 /* 1265 * Need to start a listen on the data channel before we send the command, 1266 * otherwise the server's connect may fail. 1267 */ 1268 int 1269 initconn(void) 1270 { 1271 char *p, *a; 1272 int result, tmpno = 0; 1273 int on = 1; 1274 int error; 1275 unsigned int addr[16], port[2]; 1276 unsigned int af, hal, pal; 1277 socklen_t len; 1278 const char *pasvcmd = NULL; 1279 int overbose; 1280 1281 #ifdef INET6 1282 #ifndef NO_DEBUG 1283 if (myctladdr.su_family == AF_INET6 && ftp_debug && 1284 (IN6_IS_ADDR_LINKLOCAL(&myctladdr.si_su.su_sin6.sin6_addr) || 1285 IN6_IS_ADDR_SITELOCAL(&myctladdr.si_su.su_sin6.sin6_addr))) { 1286 warnx("Use of scoped addresses can be troublesome"); 1287 } 1288 #endif 1289 #endif 1290 1291 reinit: 1292 if (passivemode) { 1293 data_addr = myctladdr; 1294 data = socket(data_addr.su_family, SOCK_STREAM, 0); 1295 if (data < 0) { 1296 warn("Can't create socket for data connection"); 1297 return (1); 1298 } 1299 if ((options & SO_DEBUG) && 1300 setsockopt(data, SOL_SOCKET, SO_DEBUG, 1301 (void *)&on, sizeof(on)) == -1) { 1302 DWARN("setsockopt %s (ignored)", "SO_DEBUG"); 1303 } 1304 result = COMPLETE + 1; 1305 switch (data_addr.su_family) { 1306 case AF_INET: 1307 if (epsv4 && !epsv4bad) { 1308 pasvcmd = "EPSV"; 1309 overbose = verbose; 1310 if (ftp_debug == 0) 1311 verbose = -1; 1312 result = command("EPSV"); 1313 verbose = overbose; 1314 if (verbose > 0 && 1315 (result == COMPLETE || !connected)) 1316 fprintf(ttyout, "%s\n", reply_string); 1317 if (!connected) 1318 return (1); 1319 /* 1320 * this code is to be friendly with broken 1321 * BSDI ftpd 1322 */ 1323 if (code / 10 == 22 && code != 229) { 1324 fputs( 1325 "wrong server: return code must be 229\n", 1326 ttyout); 1327 result = COMPLETE + 1; 1328 } 1329 if (result != COMPLETE) { 1330 epsv4bad = 1; 1331 DPRINTF("disabling epsv4 for this " 1332 "connection\n"); 1333 } 1334 } 1335 if (result != COMPLETE) { 1336 pasvcmd = "PASV"; 1337 result = command("PASV"); 1338 if (!connected) 1339 return (1); 1340 } 1341 break; 1342 #ifdef INET6 1343 case AF_INET6: 1344 if (epsv6 && !epsv6bad) { 1345 pasvcmd = "EPSV"; 1346 overbose = verbose; 1347 if (ftp_debug == 0) 1348 verbose = -1; 1349 result = command("EPSV"); 1350 verbose = overbose; 1351 if (verbose > 0 && 1352 (result == COMPLETE || !connected)) 1353 fprintf(ttyout, "%s\n", reply_string); 1354 if (!connected) 1355 return (1); 1356 /* 1357 * this code is to be friendly with 1358 * broken BSDI ftpd 1359 */ 1360 if (code / 10 == 22 && code != 229) { 1361 fputs( 1362 "wrong server: return code must be 229\n", 1363 ttyout); 1364 result = COMPLETE + 1; 1365 } 1366 if (result != COMPLETE) { 1367 epsv6bad = 1; 1368 DPRINTF("disabling epsv6 for this " 1369 "connection\n"); 1370 } 1371 } 1372 if (result != COMPLETE) { 1373 pasvcmd = "LPSV"; 1374 result = command("LPSV"); 1375 } 1376 if (!connected) 1377 return (1); 1378 break; 1379 #endif 1380 default: 1381 result = COMPLETE + 1; 1382 break; 1383 } 1384 if (result != COMPLETE) { 1385 if (activefallback) { 1386 (void)close(data); 1387 data = -1; 1388 passivemode = 0; 1389 #if 0 1390 activefallback = 0; 1391 #endif 1392 goto reinit; 1393 } 1394 fputs("Passive mode refused.\n", ttyout); 1395 goto bad; 1396 } 1397 1398 #define pack2(var, off) \ 1399 (((var[(off) + 0] & 0xff) << 8) | ((var[(off) + 1] & 0xff) << 0)) 1400 #define pack4(var, off) \ 1401 (((var[(off) + 0] & 0xff) << 24) | ((var[(off) + 1] & 0xff) << 16) | \ 1402 ((var[(off) + 2] & 0xff) << 8) | ((var[(off) + 3] & 0xff) << 0)) 1403 #define UC(b) (((int)b)&0xff) 1404 1405 /* 1406 * What we've got at this point is a string of comma separated 1407 * one-byte unsigned integer values, separated by commas. 1408 */ 1409 if (strcmp(pasvcmd, "PASV") == 0) { 1410 if (data_addr.su_family != AF_INET) { 1411 fputs( 1412 "Passive mode AF mismatch. Shouldn't happen!\n", ttyout); 1413 goto bad; 1414 } 1415 if (code / 10 == 22 && code != 227) { 1416 fputs("wrong server: return code must be 227\n", 1417 ttyout); 1418 goto bad; 1419 } 1420 error = sscanf(pasv, "%u,%u,%u,%u,%u,%u", 1421 &addr[0], &addr[1], &addr[2], &addr[3], 1422 &port[0], &port[1]); 1423 if (error != 6) { 1424 fputs( 1425 "Passive mode address scan failure. Shouldn't happen!\n", ttyout); 1426 goto bad; 1427 } 1428 memset(&data_addr, 0, sizeof(data_addr)); 1429 data_addr.su_family = AF_INET; 1430 data_addr.su_len = sizeof(struct sockaddr_in); 1431 data_addr.si_su.su_sin.sin_addr.s_addr = 1432 htonl(pack4(addr, 0)); 1433 data_addr.su_port = htons(pack2(port, 0)); 1434 if (data_addr.si_su.su_sin.sin_addr.s_addr != 1435 hisctladdr.si_su.su_sin.sin_addr.s_addr) { 1436 fputs("Passive mode address mismatch.\n", 1437 ttyout); 1438 goto bad; 1439 } 1440 } else if (strcmp(pasvcmd, "LPSV") == 0) { 1441 if (code / 10 == 22 && code != 228) { 1442 fputs("wrong server: return code must be 228\n", 1443 ttyout); 1444 goto bad; 1445 } 1446 switch (data_addr.su_family) { 1447 case AF_INET: 1448 error = sscanf(pasv, 1449 "%u,%u,%u,%u,%u,%u,%u,%u,%u", 1450 &af, &hal, 1451 &addr[0], &addr[1], &addr[2], &addr[3], 1452 &pal, &port[0], &port[1]); 1453 if (error != 9) { 1454 fputs( 1455 "Passive mode address scan failure. Shouldn't happen!\n", ttyout); 1456 goto bad; 1457 } 1458 if (af != 4 || hal != 4 || pal != 2) { 1459 fputs( 1460 "Passive mode AF mismatch. Shouldn't happen!\n", ttyout); 1461 goto bad; 1462 } 1463 1464 memset(&data_addr, 0, sizeof(data_addr)); 1465 data_addr.su_family = AF_INET; 1466 data_addr.su_len = sizeof(struct sockaddr_in); 1467 data_addr.si_su.su_sin.sin_addr.s_addr = 1468 htonl(pack4(addr, 0)); 1469 data_addr.su_port = htons(pack2(port, 0)); 1470 if (data_addr.si_su.su_sin.sin_addr.s_addr != 1471 hisctladdr.si_su.su_sin.sin_addr.s_addr) { 1472 fputs("Passive mode address mismatch.\n", 1473 ttyout); 1474 goto bad; 1475 } 1476 break; 1477 #ifdef INET6 1478 case AF_INET6: 1479 error = sscanf(pasv, 1480 "%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u", 1481 &af, &hal, 1482 &addr[0], &addr[1], &addr[2], &addr[3], 1483 &addr[4], &addr[5], &addr[6], &addr[7], 1484 &addr[8], &addr[9], &addr[10], 1485 &addr[11], &addr[12], &addr[13], 1486 &addr[14], &addr[15], 1487 &pal, &port[0], &port[1]); 1488 if (error != 21) { 1489 fputs( 1490 "Passive mode address scan failure. Shouldn't happen!\n", ttyout); 1491 goto bad; 1492 } 1493 if (af != 6 || hal != 16 || pal != 2) { 1494 fputs( 1495 "Passive mode AF mismatch. Shouldn't happen!\n", ttyout); 1496 goto bad; 1497 } 1498 1499 memset(&data_addr, 0, sizeof(data_addr)); 1500 data_addr.su_family = AF_INET6; 1501 data_addr.su_len = sizeof(struct sockaddr_in6); 1502 { 1503 size_t i; 1504 for (i = 0; i < sizeof(struct in6_addr); i++) { 1505 data_addr.si_su.su_sin6.sin6_addr.s6_addr[i] = 1506 UC(addr[i]); 1507 } 1508 } 1509 data_addr.su_port = htons(pack2(port, 0)); 1510 if (memcmp( 1511 &data_addr.si_su.su_sin6.sin6_addr, 1512 &hisctladdr.si_su.su_sin6.sin6_addr, 1513 sizeof(data_addr.si_su.su_sin6.sin6_addr))) { 1514 fputs("Passive mode address mismatch.\n", 1515 ttyout); 1516 goto bad; 1517 } 1518 break; 1519 #endif 1520 default: 1521 fputs("Unknown passive mode AF.\n", ttyout); 1522 goto bad; 1523 } 1524 } else if (strcmp(pasvcmd, "EPSV") == 0) { 1525 char delim[4]; 1526 1527 port[0] = 0; 1528 if (code / 10 == 22 && code != 229) { 1529 fputs("wrong server: return code must be 229\n", 1530 ttyout); 1531 goto bad; 1532 } 1533 if (sscanf(pasv, "%c%c%c%d%c", &delim[0], 1534 &delim[1], &delim[2], &port[1], 1535 &delim[3]) != 5) { 1536 fputs("parse error!\n", ttyout); 1537 goto bad; 1538 } 1539 if (delim[0] != delim[1] || delim[0] != delim[2] 1540 || delim[0] != delim[3]) { 1541 fputs("parse error!\n", ttyout); 1542 goto bad; 1543 } 1544 data_addr = hisctladdr; 1545 data_addr.su_port = htons(port[1]); 1546 } else 1547 goto bad; 1548 1549 if (ftp_connect(data, (struct sockaddr *)(void *)&data_addr.si_su, 1550 data_addr.su_len, 1) < 0) { 1551 if (activefallback) { 1552 (void)close(data); 1553 data = -1; 1554 passivemode = 0; 1555 #if 0 1556 activefallback = 0; 1557 #endif 1558 goto reinit; 1559 } 1560 goto bad; 1561 } 1562 #ifdef IPTOS_THROUGHPUT 1563 if (data_addr.su_family == AF_INET) { 1564 on = IPTOS_THROUGHPUT; 1565 if (setsockopt(data, IPPROTO_IP, IP_TOS, 1566 (void *)&on, sizeof(on)) == -1) { 1567 DWARN("setsockopt %s (ignored)", 1568 "IPTOS_THROUGHPUT"); 1569 } 1570 } 1571 #endif 1572 return (0); 1573 } 1574 1575 noport: 1576 data_addr = myctladdr; 1577 if (sendport) 1578 data_addr.su_port = 0; /* let system pick one */ 1579 if (data != -1) 1580 (void)close(data); 1581 data = socket(data_addr.su_family, SOCK_STREAM, 0); 1582 if (data < 0) { 1583 warn("Can't create socket for data connection"); 1584 if (tmpno) 1585 sendport = 1; 1586 return (1); 1587 } 1588 if (!sendport) 1589 if (setsockopt(data, SOL_SOCKET, SO_REUSEADDR, 1590 (void *)&on, sizeof(on)) == -1) { 1591 warn("Can't set SO_REUSEADDR on data connection"); 1592 goto bad; 1593 } 1594 if (bind(data, (struct sockaddr *)(void *)&data_addr.si_su, 1595 data_addr.su_len) < 0) { 1596 warn("Can't bind for data connection"); 1597 goto bad; 1598 } 1599 if ((options & SO_DEBUG) && 1600 setsockopt(data, SOL_SOCKET, SO_DEBUG, 1601 (void *)&on, sizeof(on)) == -1) { 1602 DWARN("setsockopt %s (ignored)", "SO_DEBUG"); 1603 } 1604 len = sizeof(data_addr.si_su); 1605 memset((char *)&data_addr, 0, sizeof (data_addr)); 1606 if (getsockname(data, (struct sockaddr *)(void *)&data_addr.si_su, &len) == -1) { 1607 warn("Can't determine my address of data connection"); 1608 goto bad; 1609 } 1610 data_addr.su_len = len; 1611 if (ftp_listen(data, 1) < 0) 1612 warn("Can't listen to data connection"); 1613 1614 if (sendport) { 1615 char hname[NI_MAXHOST], sname[NI_MAXSERV]; 1616 struct sockinet tmp; 1617 1618 switch (data_addr.su_family) { 1619 case AF_INET: 1620 if (!epsv4 || epsv4bad) { 1621 result = COMPLETE + 1; 1622 break; 1623 } 1624 #ifdef INET6 1625 /* FALLTHROUGH */ 1626 case AF_INET6: 1627 if (!epsv6 || epsv6bad) { 1628 result = COMPLETE + 1; 1629 break; 1630 } 1631 #endif 1632 af = (data_addr.su_family == AF_INET) ? 1 : 2; 1633 tmp = data_addr; 1634 #ifdef INET6 1635 if (tmp.su_family == AF_INET6) 1636 tmp.si_su.su_sin6.sin6_scope_id = 0; 1637 #endif 1638 if (getnameinfo((struct sockaddr *)(void *)&tmp.si_su, 1639 tmp.su_len, hname, sizeof(hname), sname, 1640 sizeof(sname), NI_NUMERICHOST | NI_NUMERICSERV)) { 1641 result = ERROR; 1642 } else { 1643 overbose = verbose; 1644 if (ftp_debug == 0) 1645 verbose = -1; 1646 result = command("EPRT |%u|%s|%s|", af, hname, 1647 sname); 1648 verbose = overbose; 1649 if (verbose > 0 && 1650 (result == COMPLETE || !connected)) 1651 fprintf(ttyout, "%s\n", reply_string); 1652 if (!connected) 1653 return (1); 1654 if (result != COMPLETE) { 1655 epsv4bad = 1; 1656 DPRINTF("disabling epsv4 for this " 1657 "connection\n"); 1658 } 1659 } 1660 break; 1661 default: 1662 result = COMPLETE + 1; 1663 break; 1664 } 1665 if (result == COMPLETE) 1666 goto skip_port; 1667 1668 switch (data_addr.su_family) { 1669 case AF_INET: 1670 a = (char *)&data_addr.si_su.su_sin.sin_addr; 1671 p = (char *)&data_addr.su_port; 1672 result = command("PORT %d,%d,%d,%d,%d,%d", 1673 UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]), 1674 UC(p[0]), UC(p[1])); 1675 break; 1676 #ifdef INET6 1677 case AF_INET6: { 1678 uint8_t ua[sizeof(data_addr.si_su.su_sin6.sin6_addr)]; 1679 uint8_t up[sizeof(data_addr.su_port)]; 1680 1681 memcpy(ua, &data_addr.si_su.su_sin6.sin6_addr, 1682 sizeof(ua)); 1683 memcpy(up, &data_addr.su_port, sizeof(up)); 1684 1685 result = command( 1686 "LPRT %d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d", 1687 6, 16, 1688 ua[0], ua[1], ua[2], ua[3], 1689 ua[4], ua[5], ua[6], ua[7], 1690 ua[8], ua[9], ua[10], ua[11], 1691 ua[12], ua[13], ua[14], ua[15], 1692 2, 1693 up[0], up[1]); 1694 break; 1695 } 1696 #endif 1697 default: 1698 result = COMPLETE + 1; /* xxx */ 1699 } 1700 if (!connected) 1701 return (1); 1702 skip_port: 1703 1704 if (result == ERROR && sendport == -1) { 1705 sendport = 0; 1706 tmpno = 1; 1707 goto noport; 1708 } 1709 return (result != COMPLETE); 1710 } 1711 if (tmpno) 1712 sendport = 1; 1713 #ifdef IPTOS_THROUGHPUT 1714 if (data_addr.su_family == AF_INET) { 1715 on = IPTOS_THROUGHPUT; 1716 if (setsockopt(data, IPPROTO_IP, IP_TOS, 1717 (void *)&on, sizeof(on)) == -1) { 1718 DWARN("setsockopt %s (ignored)", "IPTOS_THROUGHPUT"); 1719 } 1720 } 1721 #endif 1722 return (0); 1723 bad: 1724 (void)close(data); 1725 data = -1; 1726 if (tmpno) 1727 sendport = 1; 1728 return (1); 1729 } 1730 1731 FILE * 1732 dataconn(const char *lmode) 1733 { 1734 struct sockinet from; 1735 int s, flags, rv, timeout; 1736 struct timeval endtime, now, td; 1737 struct pollfd pfd[1]; 1738 socklen_t fromlen; 1739 1740 if (passivemode) /* passive data connection */ 1741 return (fdopen(data, lmode)); 1742 1743 /* active mode data connection */ 1744 1745 if ((flags = fcntl(data, F_GETFL, 0)) == -1) 1746 goto dataconn_failed; /* get current socket flags */ 1747 if (fcntl(data, F_SETFL, flags | O_NONBLOCK) == -1) 1748 goto dataconn_failed; /* set non-blocking connect */ 1749 1750 /* NOTE: we now must restore socket flags on successful exit */ 1751 1752 /* limit time waiting on listening socket */ 1753 pfd[0].fd = data; 1754 pfd[0].events = POLLIN; 1755 (void)gettimeofday(&endtime, NULL); /* determine end time */ 1756 endtime.tv_sec += (quit_time > 0) ? quit_time: 60; 1757 /* without -q, default to 60s */ 1758 do { 1759 (void)gettimeofday(&now, NULL); 1760 timersub(&endtime, &now, &td); 1761 timeout = (int)(td.tv_sec * 1000 + td.tv_usec / 1000); 1762 if (timeout < 0) 1763 timeout = 0; 1764 rv = ftp_poll(pfd, 1, timeout); 1765 /* loop until poll !EINTR && !EAGAIN */ 1766 } while (rv == -1 && (errno == EINTR || errno == EAGAIN)); 1767 if (rv == -1) { 1768 warn("Can't poll waiting before accept"); 1769 goto dataconn_failed; 1770 } 1771 if (rv == 0) { 1772 warnx("Poll timeout waiting before accept"); 1773 goto dataconn_failed; 1774 } 1775 1776 /* (non-blocking) accept the connection */ 1777 fromlen = myctladdr.su_len; 1778 do { 1779 s = accept(data, (struct sockaddr *)(void *)&from.si_su, &fromlen); 1780 /* loop until accept !EINTR && !EAGAIN */ 1781 } while (s == -1 && (errno == EINTR || errno == EAGAIN)); 1782 if (s == -1) { 1783 warn("Can't accept data connection"); 1784 goto dataconn_failed; 1785 } 1786 1787 (void)close(data); 1788 data = s; 1789 if (fcntl(data, F_SETFL, flags) == -1) /* restore socket flags */ 1790 goto dataconn_failed; 1791 1792 #ifdef IPTOS_THROUGHPUT 1793 if (from.su_family == AF_INET) { 1794 int tos = IPTOS_THROUGHPUT; 1795 if (setsockopt(s, IPPROTO_IP, IP_TOS, 1796 (void *)&tos, sizeof(tos)) == -1) { 1797 DWARN("setsockopt %s (ignored)", "IPTOS_THROUGHPUT"); 1798 } 1799 } 1800 #endif 1801 return (fdopen(data, lmode)); 1802 1803 dataconn_failed: 1804 (void)close(data); 1805 data = -1; 1806 return (NULL); 1807 } 1808 1809 void 1810 psabort(int notused __unused) 1811 { 1812 int oerrno = errno; 1813 1814 sigint_raised = 1; 1815 alarmtimer(0); 1816 abrtflag++; 1817 errno = oerrno; 1818 } 1819 1820 void 1821 pswitch(int flag) 1822 { 1823 sigfunc oldintr; 1824 static struct comvars { 1825 int connect; 1826 char name[MAXHOSTNAMELEN]; 1827 struct sockinet mctl; 1828 struct sockinet hctl; 1829 FILE *in; 1830 FILE *out; 1831 int tpe; 1832 int curtpe; 1833 int cpnd; 1834 int sunqe; 1835 int runqe; 1836 int mcse; 1837 int ntflg; 1838 char nti[17]; 1839 char nto[17]; 1840 int mapflg; 1841 char mi[MAXPATHLEN]; 1842 char mo[MAXPATHLEN]; 1843 } proxstruct, tmpstruct; 1844 struct comvars *ip, *op; 1845 1846 abrtflag = 0; 1847 oldintr = xsignal(SIGINT, psabort); 1848 if (flag) { 1849 if (proxy) 1850 return; 1851 ip = &tmpstruct; 1852 op = &proxstruct; 1853 proxy++; 1854 } else { 1855 if (!proxy) 1856 return; 1857 ip = &proxstruct; 1858 op = &tmpstruct; 1859 proxy = 0; 1860 } 1861 ip->connect = connected; 1862 connected = op->connect; 1863 if (hostname) 1864 (void)strlcpy(ip->name, hostname, sizeof(ip->name)); 1865 else 1866 ip->name[0] = '\0'; 1867 hostname = op->name; 1868 ip->hctl = hisctladdr; 1869 hisctladdr = op->hctl; 1870 ip->mctl = myctladdr; 1871 myctladdr = op->mctl; 1872 ip->in = cin; 1873 cin = op->in; 1874 ip->out = cout; 1875 cout = op->out; 1876 ip->tpe = type; 1877 type = op->tpe; 1878 ip->curtpe = curtype; 1879 curtype = op->curtpe; 1880 ip->cpnd = cpend; 1881 cpend = op->cpnd; 1882 ip->sunqe = sunique; 1883 sunique = op->sunqe; 1884 ip->runqe = runique; 1885 runique = op->runqe; 1886 ip->mcse = mcase; 1887 mcase = op->mcse; 1888 ip->ntflg = ntflag; 1889 ntflag = op->ntflg; 1890 (void)strlcpy(ip->nti, ntin, sizeof(ip->nti)); 1891 (void)strlcpy(ntin, op->nti, sizeof(ntin)); 1892 (void)strlcpy(ip->nto, ntout, sizeof(ip->nto)); 1893 (void)strlcpy(ntout, op->nto, sizeof(ntout)); 1894 ip->mapflg = mapflag; 1895 mapflag = op->mapflg; 1896 (void)strlcpy(ip->mi, mapin, sizeof(ip->mi)); 1897 (void)strlcpy(mapin, op->mi, sizeof(mapin)); 1898 (void)strlcpy(ip->mo, mapout, sizeof(ip->mo)); 1899 (void)strlcpy(mapout, op->mo, sizeof(mapout)); 1900 (void)xsignal(SIGINT, oldintr); 1901 if (abrtflag) { 1902 abrtflag = 0; 1903 (*oldintr)(SIGINT); 1904 } 1905 } 1906 1907 __dead static void 1908 abortpt(int notused __unused) 1909 { 1910 1911 sigint_raised = 1; 1912 alarmtimer(0); 1913 if (fromatty) 1914 write(fileno(ttyout), "\n", 1); 1915 ptabflg++; 1916 mflag = 0; 1917 abrtflag = 0; 1918 siglongjmp(ptabort, 1); 1919 } 1920 1921 void 1922 proxtrans(const char *cmd, const char *local, const char *remote) 1923 { 1924 sigfunc volatile oldintr; 1925 int prox_type, nfnd; 1926 int volatile secndflag; 1927 const char *volatile cmd2; 1928 1929 oldintr = SIG_ERR; 1930 secndflag = 0; 1931 if (strcmp(cmd, "RETR")) 1932 cmd2 = "RETR"; 1933 else 1934 cmd2 = runique ? "STOU" : "STOR"; 1935 if ((prox_type = type) == 0) { 1936 if (unix_server && unix_proxy) 1937 prox_type = TYPE_I; 1938 else 1939 prox_type = TYPE_A; 1940 } 1941 if (curtype != prox_type) 1942 changetype(prox_type, 1); 1943 if (command("PASV") != COMPLETE) { 1944 fputs("proxy server does not support third party transfers.\n", 1945 ttyout); 1946 return; 1947 } 1948 pswitch(0); 1949 if (!connected) { 1950 fputs("No primary connection.\n", ttyout); 1951 pswitch(1); 1952 code = -1; 1953 return; 1954 } 1955 if (curtype != prox_type) 1956 changetype(prox_type, 1); 1957 if (command("PORT %s", pasv) != COMPLETE) { 1958 pswitch(1); 1959 return; 1960 } 1961 if (sigsetjmp(ptabort, 1)) 1962 goto abort; 1963 oldintr = xsignal(SIGINT, abortpt); 1964 if ((restart_point && 1965 (command("REST " LLF, (LLT) restart_point) != CONTINUE)) 1966 || (command("%s %s", cmd, remote) != PRELIM)) { 1967 (void)xsignal(SIGINT, oldintr); 1968 pswitch(1); 1969 return; 1970 } 1971 sleep(2); 1972 pswitch(1); 1973 secndflag++; 1974 if ((restart_point && 1975 (command("REST " LLF, (LLT) restart_point) != CONTINUE)) 1976 || (command("%s %s", cmd2, local) != PRELIM)) 1977 goto abort; 1978 ptflag++; 1979 (void)getreply(0); 1980 pswitch(0); 1981 (void)getreply(0); 1982 (void)xsignal(SIGINT, oldintr); 1983 pswitch(1); 1984 ptflag = 0; 1985 fprintf(ttyout, "local: %s remote: %s\n", local, remote); 1986 return; 1987 abort: 1988 if (sigsetjmp(xferabort, 1)) { 1989 (void)xsignal(SIGINT, oldintr); 1990 return; 1991 } 1992 (void)xsignal(SIGINT, abort_squared); 1993 ptflag = 0; 1994 if (strcmp(cmd, "RETR") && !proxy) 1995 pswitch(1); 1996 else if (!strcmp(cmd, "RETR") && proxy) 1997 pswitch(0); 1998 if (!cpend && !secndflag) { /* only here if cmd = "STOR" (proxy=1) */ 1999 if (command("%s %s", cmd2, local) != PRELIM) { 2000 pswitch(0); 2001 if (cpend) 2002 abort_remote(NULL); 2003 } 2004 pswitch(1); 2005 if (ptabflg) 2006 code = -1; 2007 (void)xsignal(SIGINT, oldintr); 2008 return; 2009 } 2010 if (cpend) 2011 abort_remote(NULL); 2012 pswitch(!proxy); 2013 if (!cpend && !secndflag) { /* only if cmd = "RETR" (proxy=1) */ 2014 if (command("%s %s", cmd2, local) != PRELIM) { 2015 pswitch(0); 2016 if (cpend) 2017 abort_remote(NULL); 2018 pswitch(1); 2019 if (ptabflg) 2020 code = -1; 2021 (void)xsignal(SIGINT, oldintr); 2022 return; 2023 } 2024 } 2025 if (cpend) 2026 abort_remote(NULL); 2027 pswitch(!proxy); 2028 if (cpend) { 2029 if ((nfnd = empty(cin, NULL, 10)) <= 0) { 2030 if (nfnd < 0) 2031 warn("Error aborting proxy command"); 2032 if (ptabflg) 2033 code = -1; 2034 lostpeer(0); 2035 } 2036 (void)getreply(0); 2037 (void)getreply(0); 2038 } 2039 if (proxy) 2040 pswitch(0); 2041 pswitch(1); 2042 if (ptabflg) 2043 code = -1; 2044 (void)xsignal(SIGINT, oldintr); 2045 } 2046 2047 void 2048 reset(int argc, char *argv[]) 2049 { 2050 int nfnd = 1; 2051 2052 if (argc == 0 && argv != NULL) { 2053 UPRINTF("usage: %s\n", argv[0]); 2054 code = -1; 2055 return; 2056 } 2057 while (nfnd > 0) { 2058 if ((nfnd = empty(cin, NULL, 0)) < 0) { 2059 warn("Error resetting connection"); 2060 code = -1; 2061 lostpeer(0); 2062 } else if (nfnd) 2063 (void)getreply(0); 2064 } 2065 } 2066 2067 char * 2068 gunique(char *local) 2069 { 2070 static char new[MAXPATHLEN]; 2071 char *cp = strrchr(local, '/'); 2072 int d, count = 0; 2073 size_t len; 2074 char ext = '1'; 2075 2076 if (cp) 2077 *cp = '\0'; 2078 d = access(cp == local ? "/" : cp ? local : ".", W_OK); 2079 if (cp) 2080 *cp = '/'; 2081 if (d < 0) { 2082 warn("Can't access `%s'", local); 2083 return (NULL); 2084 } 2085 len = strlcpy(new, local, sizeof(new)); 2086 cp = &new[len]; 2087 *cp++ = '.'; 2088 while (!d) { 2089 if (++count == 100) { 2090 fputs("runique: can't find unique file name.\n", 2091 ttyout); 2092 return (NULL); 2093 } 2094 *cp++ = ext; 2095 *cp = '\0'; 2096 if (ext == '9') 2097 ext = '0'; 2098 else 2099 ext++; 2100 if ((d = access(new, F_OK)) < 0) 2101 break; 2102 if (ext != '0') 2103 cp--; 2104 else if (*(cp - 2) == '.') 2105 *(cp - 1) = '1'; 2106 else { 2107 *(cp - 2) = *(cp - 2) + 1; 2108 cp--; 2109 } 2110 } 2111 return (new); 2112 } 2113 2114 /* 2115 * abort_squared -- 2116 * aborts abort_remote(). lostpeer() is called because if the user is 2117 * too impatient to wait or there's another problem then ftp really 2118 * needs to get back to a known state. 2119 */ 2120 static void 2121 abort_squared(int signo) 2122 { 2123 char msgbuf[100]; 2124 size_t len; 2125 2126 sigint_raised = 1; 2127 alarmtimer(0); 2128 len = strlcpy(msgbuf, "\nremote abort aborted; closing connection.\n", 2129 sizeof(msgbuf)); 2130 write(fileno(ttyout), msgbuf, len); 2131 lostpeer(signo); 2132 siglongjmp(xferabort, 1); 2133 } 2134 2135 void 2136 abort_remote(FILE *din) 2137 { 2138 unsigned char buf[BUFSIZ]; 2139 int nfnd; 2140 2141 if (cout == NULL) { 2142 warnx("Lost control connection for abort"); 2143 if (ptabflg) 2144 code = -1; 2145 lostpeer(0); 2146 return; 2147 } 2148 /* 2149 * send IAC in urgent mode instead of DM because 4.3BSD places oob mark 2150 * after urgent byte rather than before as is protocol now 2151 */ 2152 buf[0] = IAC; 2153 buf[1] = IP; 2154 buf[2] = IAC; 2155 if (send(fileno(cout), buf, 3, MSG_OOB) != 3) 2156 warn("Can't send abort message"); 2157 fprintf(cout, "%cABOR\r\n", DM); /* TODO: handle EINTR? */ 2158 (void)fflush(cout); 2159 if ((nfnd = empty(cin, din, 10)) <= 0) { 2160 if (nfnd < 0) 2161 warn("Can't send abort message"); 2162 if (ptabflg) 2163 code = -1; 2164 lostpeer(0); 2165 } 2166 if (din && (nfnd & 2)) { 2167 while (read(fileno(din), buf, BUFSIZ) > 0) 2168 continue; 2169 } 2170 if (getreply(0) == ERROR && code == 552) { 2171 /* 552 needed for nic style abort */ 2172 (void)getreply(0); 2173 } 2174 (void)getreply(0); 2175 } 2176 2177 /* 2178 * Ensure that ai->ai_addr is NOT an IPv4 mapped address. 2179 * IPv4 mapped address complicates too many things in FTP 2180 * protocol handling, as FTP protocol is defined differently 2181 * between IPv4 and IPv6. 2182 * 2183 * This may not be the best way to handle this situation, 2184 * since the semantics of IPv4 mapped address is defined in 2185 * the kernel. There are configurations where we should use 2186 * IPv4 mapped address as native IPv6 address, not as 2187 * "an IPv6 address that embeds IPv4 address" (namely, SIIT). 2188 * 2189 * More complete solution would be to have an additional 2190 * getsockopt to grab "real" peername/sockname. "real" 2191 * peername/sockname will be AF_INET if IPv4 mapped address 2192 * is used to embed IPv4 address, and will be AF_INET6 if 2193 * we use it as native. What a mess! 2194 */ 2195 void 2196 ai_unmapped(struct addrinfo *ai) 2197 { 2198 #ifdef INET6 2199 struct sockaddr_in6 *sin6; 2200 struct sockaddr_in sin; 2201 socklen_t len; 2202 2203 if (ai->ai_family != AF_INET6) 2204 return; 2205 if (ai->ai_addrlen != sizeof(struct sockaddr_in6) || 2206 sizeof(sin) > ai->ai_addrlen) 2207 return; 2208 sin6 = (struct sockaddr_in6 *)(void *)ai->ai_addr; 2209 if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) 2210 return; 2211 2212 memset(&sin, 0, sizeof(sin)); 2213 sin.sin_family = AF_INET; 2214 len = sizeof(struct sockaddr_in); 2215 memcpy(&sin.sin_addr, &sin6->sin6_addr.s6_addr[12], 2216 sizeof(sin.sin_addr)); 2217 sin.sin_port = sin6->sin6_port; 2218 2219 ai->ai_family = AF_INET; 2220 #if defined(HAVE_STRUCT_SOCKADDR_IN_SIN_LEN) 2221 sin.sin_len = len; 2222 #endif 2223 memcpy(ai->ai_addr, &sin, len); 2224 ai->ai_addrlen = len; 2225 #endif 2226 } 2227 2228 #ifdef NO_USAGE 2229 void 2230 xusage(void) 2231 { 2232 fputs("Usage error\n", ttyout); 2233 } 2234 #endif 2235