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