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