1 /* 2 * Copyright (c) 1985, 1989, 1993, 1994 3 * The Regents of the University of California. All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 static char sccsid[] = "@(#)ftp.c 8.4 (Berkeley) 04/06/94"; 10 #endif /* not lint */ 11 12 #include <sys/param.h> 13 #include <sys/stat.h> 14 #include <sys/ioctl.h> 15 #include <sys/socket.h> 16 #include <sys/time.h> 17 #include <sys/file.h> 18 19 #include <netinet/in.h> 20 #include <netinet/in_systm.h> 21 #include <netinet/ip.h> 22 #include <arpa/inet.h> 23 #include <arpa/ftp.h> 24 #include <arpa/telnet.h> 25 26 #include <ctype.h> 27 #include <err.h> 28 #include <errno.h> 29 #include <fcntl.h> 30 #include <netdb.h> 31 #include <pwd.h> 32 #include <signal.h> 33 #include <stdio.h> 34 #include <stdlib.h> 35 #include <string.h> 36 #include <unistd.h> 37 #include <varargs.h> 38 39 #include "ftp_var.h" 40 41 extern int h_errno; 42 43 struct sockaddr_in hisctladdr; 44 struct sockaddr_in data_addr; 45 int data = -1; 46 int abrtflag = 0; 47 jmp_buf ptabort; 48 int ptabflg; 49 int ptflag = 0; 50 struct sockaddr_in myctladdr; 51 off_t restart_point = 0; 52 53 54 FILE *cin, *cout; 55 56 char * 57 hookup(host, port) 58 char *host; 59 int port; 60 { 61 struct hostent *hp = 0; 62 int s, len, tos; 63 static char hostnamebuf[80]; 64 65 memset((char *)&hisctladdr, 0, sizeof (hisctladdr)); 66 hisctladdr.sin_addr.s_addr = inet_addr(host); 67 if (hisctladdr.sin_addr.s_addr != -1) { 68 hisctladdr.sin_family = AF_INET; 69 (void) strncpy(hostnamebuf, host, sizeof(hostnamebuf)); 70 } else { 71 hp = gethostbyname(host); 72 if (hp == NULL) { 73 warnx("%s: %s", host, hstrerror(h_errno)); 74 code = -1; 75 return ((char *) 0); 76 } 77 hisctladdr.sin_family = hp->h_addrtype; 78 memmove((caddr_t)&hisctladdr.sin_addr, 79 hp->h_addr_list[0], hp->h_length); 80 (void) strncpy(hostnamebuf, hp->h_name, sizeof(hostnamebuf)); 81 } 82 hostname = hostnamebuf; 83 s = socket(hisctladdr.sin_family, SOCK_STREAM, 0); 84 if (s < 0) { 85 warn("socket"); 86 code = -1; 87 return (0); 88 } 89 hisctladdr.sin_port = port; 90 while (connect(s, (struct sockaddr *)&hisctladdr, sizeof (hisctladdr)) < 0) { 91 if (hp && hp->h_addr_list[1]) { 92 int oerrno = errno; 93 char *ia; 94 95 ia = inet_ntoa(hisctladdr.sin_addr); 96 errno = oerrno; 97 warn("connect to address %s", ia); 98 hp->h_addr_list++; 99 memmove((caddr_t)&hisctladdr.sin_addr, 100 hp->h_addr_list[0], hp->h_length); 101 fprintf(stdout, "Trying %s...\n", 102 inet_ntoa(hisctladdr.sin_addr)); 103 (void) close(s); 104 s = socket(hisctladdr.sin_family, SOCK_STREAM, 0); 105 if (s < 0) { 106 warn("socket"); 107 code = -1; 108 return (0); 109 } 110 continue; 111 } 112 warn("connect"); 113 code = -1; 114 goto bad; 115 } 116 len = sizeof (myctladdr); 117 if (getsockname(s, (struct sockaddr *)&myctladdr, &len) < 0) { 118 warn("getsockname"); 119 code = -1; 120 goto bad; 121 } 122 #ifdef IP_TOS 123 tos = IPTOS_LOWDELAY; 124 if (setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(int)) < 0) 125 warn("setsockopt TOS (ignored)"); 126 #endif 127 cin = fdopen(s, "r"); 128 cout = fdopen(s, "w"); 129 if (cin == NULL || cout == NULL) { 130 warnx("fdopen failed."); 131 if (cin) 132 (void) fclose(cin); 133 if (cout) 134 (void) fclose(cout); 135 code = -1; 136 goto bad; 137 } 138 if (verbose) 139 printf("Connected to %s.\n", hostname); 140 if (getreply(0) > 2) { /* read startup message from server */ 141 if (cin) 142 (void) fclose(cin); 143 if (cout) 144 (void) fclose(cout); 145 code = -1; 146 goto bad; 147 } 148 #ifdef SO_OOBINLINE 149 { 150 int on = 1; 151 152 if (setsockopt(s, SOL_SOCKET, SO_OOBINLINE, (char *)&on, sizeof(on)) 153 < 0 && debug) { 154 warn("setsockopt"); 155 } 156 } 157 #endif /* SO_OOBINLINE */ 158 159 return (hostname); 160 bad: 161 (void) close(s); 162 return ((char *)0); 163 } 164 165 int 166 login(host) 167 char *host; 168 { 169 char tmp[80]; 170 char *user, *pass, *acct; 171 int n, aflag = 0; 172 173 user = pass = acct = 0; 174 if (ruserpass(host, &user, &pass, &acct) < 0) { 175 code = -1; 176 return (0); 177 } 178 while (user == NULL) { 179 char *myname = getlogin(); 180 181 if (myname == NULL) { 182 struct passwd *pp = getpwuid(getuid()); 183 184 if (pp != NULL) 185 myname = pp->pw_name; 186 } 187 if (myname) 188 printf("Name (%s:%s): ", host, myname); 189 else 190 printf("Name (%s): ", host); 191 (void) fgets(tmp, sizeof(tmp) - 1, stdin); 192 tmp[strlen(tmp) - 1] = '\0'; 193 if (*tmp == '\0') 194 user = myname; 195 else 196 user = tmp; 197 } 198 n = command("USER %s", user); 199 if (n == CONTINUE) { 200 if (pass == NULL) 201 pass = getpass("Password:"); 202 n = command("PASS %s", pass); 203 } 204 if (n == CONTINUE) { 205 aflag++; 206 acct = getpass("Account:"); 207 n = command("ACCT %s", acct); 208 } 209 if (n != COMPLETE) { 210 warnx("Login failed."); 211 return (0); 212 } 213 if (!aflag && acct != NULL) 214 (void) command("ACCT %s", acct); 215 if (proxy) 216 return (1); 217 for (n = 0; n < macnum; ++n) { 218 if (!strcmp("init", macros[n].mac_name)) { 219 (void) strcpy(line, "$init"); 220 makeargv(); 221 domacro(margc, margv); 222 break; 223 } 224 } 225 return (1); 226 } 227 228 void 229 cmdabort() 230 { 231 232 printf("\n"); 233 (void) fflush(stdout); 234 abrtflag++; 235 if (ptflag) 236 longjmp(ptabort,1); 237 } 238 239 /*VARARGS*/ 240 int 241 command(va_alist) 242 va_dcl 243 { 244 va_list ap; 245 char *fmt; 246 int r; 247 sig_t oldintr; 248 249 abrtflag = 0; 250 if (debug) { 251 printf("---> "); 252 va_start(ap); 253 fmt = va_arg(ap, char *); 254 if (strncmp("PASS ", fmt, 5) == 0) 255 printf("PASS XXXX"); 256 else 257 vfprintf(stdout, fmt, ap); 258 va_end(ap); 259 printf("\n"); 260 (void) fflush(stdout); 261 } 262 if (cout == NULL) { 263 warn("No control connection for command"); 264 code = -1; 265 return (0); 266 } 267 oldintr = signal(SIGINT, cmdabort); 268 va_start(ap); 269 fmt = va_arg(ap, char *); 270 vfprintf(cout, fmt, ap); 271 va_end(ap); 272 fprintf(cout, "\r\n"); 273 (void) fflush(cout); 274 cpend = 1; 275 r = getreply(!strcmp(fmt, "QUIT")); 276 if (abrtflag && oldintr != SIG_IGN) 277 (*oldintr)(SIGINT); 278 (void) signal(SIGINT, oldintr); 279 return (r); 280 } 281 282 char reply_string[BUFSIZ]; /* last line of previous reply */ 283 284 int 285 getreply(expecteof) 286 int expecteof; 287 { 288 int c, n; 289 int dig; 290 int originalcode = 0, continuation = 0; 291 sig_t oldintr; 292 int pflag = 0; 293 char *cp, *pt = pasv; 294 295 oldintr = signal(SIGINT, cmdabort); 296 for (;;) { 297 dig = n = code = 0; 298 cp = reply_string; 299 while ((c = getc(cin)) != '\n') { 300 if (c == IAC) { /* handle telnet commands */ 301 switch (c = getc(cin)) { 302 case WILL: 303 case WONT: 304 c = getc(cin); 305 fprintf(cout, "%c%c%c", IAC, DONT, c); 306 (void) fflush(cout); 307 break; 308 case DO: 309 case DONT: 310 c = getc(cin); 311 fprintf(cout, "%c%c%c", IAC, WONT, c); 312 (void) fflush(cout); 313 break; 314 default: 315 break; 316 } 317 continue; 318 } 319 dig++; 320 if (c == EOF) { 321 if (expecteof) { 322 (void) signal(SIGINT,oldintr); 323 code = 221; 324 return (0); 325 } 326 lostpeer(); 327 if (verbose) { 328 printf("421 Service not available, remote server has closed connection\n"); 329 (void) fflush(stdout); 330 } 331 code = 421; 332 return (4); 333 } 334 if (c != '\r' && (verbose > 0 || 335 (verbose > -1 && n == '5' && dig > 4))) { 336 if (proxflag && 337 (dig == 1 || dig == 5 && verbose == 0)) 338 printf("%s:",hostname); 339 (void) putchar(c); 340 } 341 if (dig < 4 && isdigit(c)) 342 code = code * 10 + (c - '0'); 343 if (!pflag && code == 227) 344 pflag = 1; 345 if (dig > 4 && pflag == 1 && isdigit(c)) 346 pflag = 2; 347 if (pflag == 2) { 348 if (c != '\r' && c != ')') 349 *pt++ = c; 350 else { 351 *pt = '\0'; 352 pflag = 3; 353 } 354 } 355 if (dig == 4 && c == '-') { 356 if (continuation) 357 code = 0; 358 continuation++; 359 } 360 if (n == 0) 361 n = c; 362 if (cp < &reply_string[sizeof(reply_string) - 1]) 363 *cp++ = c; 364 } 365 if (verbose > 0 || verbose > -1 && n == '5') { 366 (void) putchar(c); 367 (void) fflush (stdout); 368 } 369 if (continuation && code != originalcode) { 370 if (originalcode == 0) 371 originalcode = code; 372 continue; 373 } 374 *cp = '\0'; 375 if (n != '1') 376 cpend = 0; 377 (void) signal(SIGINT,oldintr); 378 if (code == 421 || originalcode == 421) 379 lostpeer(); 380 if (abrtflag && oldintr != cmdabort && oldintr != SIG_IGN) 381 (*oldintr)(SIGINT); 382 return (n - '0'); 383 } 384 } 385 386 int 387 empty(mask, sec) 388 struct fd_set *mask; 389 int sec; 390 { 391 struct timeval t; 392 393 t.tv_sec = (long) sec; 394 t.tv_usec = 0; 395 return (select(32, mask, (struct fd_set *) 0, (struct fd_set *) 0, &t)); 396 } 397 398 jmp_buf sendabort; 399 400 void 401 abortsend() 402 { 403 404 mflag = 0; 405 abrtflag = 0; 406 printf("\nsend aborted\nwaiting for remote to finish abort\n"); 407 (void) fflush(stdout); 408 longjmp(sendabort, 1); 409 } 410 411 #define HASHBYTES 1024 412 413 void 414 sendrequest(cmd, local, remote, printnames) 415 char *cmd, *local, *remote; 416 int printnames; 417 { 418 struct stat st; 419 struct timeval start, stop; 420 int c, d; 421 FILE *fin, *dout = 0, *popen(); 422 int (*closefunc) __P((FILE *)); 423 sig_t oldintr, oldintp; 424 long bytes = 0, hashbytes = HASHBYTES; 425 char *lmode, buf[BUFSIZ], *bufp; 426 427 if (verbose && printnames) { 428 if (local && *local != '-') 429 printf("local: %s ", local); 430 if (remote) 431 printf("remote: %s\n", remote); 432 } 433 if (proxy) { 434 proxtrans(cmd, local, remote); 435 return; 436 } 437 if (curtype != type) 438 changetype(type, 0); 439 closefunc = NULL; 440 oldintr = NULL; 441 oldintp = NULL; 442 lmode = "w"; 443 if (setjmp(sendabort)) { 444 while (cpend) { 445 (void) getreply(0); 446 } 447 if (data >= 0) { 448 (void) close(data); 449 data = -1; 450 } 451 if (oldintr) 452 (void) signal(SIGINT,oldintr); 453 if (oldintp) 454 (void) signal(SIGPIPE,oldintp); 455 code = -1; 456 return; 457 } 458 oldintr = signal(SIGINT, abortsend); 459 if (strcmp(local, "-") == 0) 460 fin = stdin; 461 else if (*local == '|') { 462 oldintp = signal(SIGPIPE,SIG_IGN); 463 fin = popen(local + 1, "r"); 464 if (fin == NULL) { 465 warn("%s", local + 1); 466 (void) signal(SIGINT, oldintr); 467 (void) signal(SIGPIPE, oldintp); 468 code = -1; 469 return; 470 } 471 closefunc = pclose; 472 } else { 473 fin = fopen(local, "r"); 474 if (fin == NULL) { 475 warn("local: %s", local); 476 (void) signal(SIGINT, oldintr); 477 code = -1; 478 return; 479 } 480 closefunc = fclose; 481 if (fstat(fileno(fin), &st) < 0 || 482 (st.st_mode&S_IFMT) != S_IFREG) { 483 fprintf(stdout, "%s: not a plain file.\n", local); 484 (void) signal(SIGINT, oldintr); 485 fclose(fin); 486 code = -1; 487 return; 488 } 489 } 490 if (initconn()) { 491 (void) signal(SIGINT, oldintr); 492 if (oldintp) 493 (void) signal(SIGPIPE, oldintp); 494 code = -1; 495 if (closefunc != NULL) 496 (*closefunc)(fin); 497 return; 498 } 499 if (setjmp(sendabort)) 500 goto abort; 501 502 if (restart_point && 503 (strcmp(cmd, "STOR") == 0 || strcmp(cmd, "APPE") == 0)) { 504 int rc; 505 506 switch (curtype) { 507 case TYPE_A: 508 rc = fseek(fin, (long) restart_point, SEEK_SET); 509 break; 510 case TYPE_I: 511 case TYPE_L: 512 rc = lseek(fileno(fin), restart_point, SEEK_SET); 513 break; 514 } 515 if (rc < 0) { 516 warn("local: %s", local); 517 restart_point = 0; 518 if (closefunc != NULL) 519 (*closefunc)(fin); 520 return; 521 } 522 if (command("REST %ld", (long) restart_point) 523 != CONTINUE) { 524 restart_point = 0; 525 if (closefunc != NULL) 526 (*closefunc)(fin); 527 return; 528 } 529 restart_point = 0; 530 lmode = "r+w"; 531 } 532 if (remote) { 533 if (command("%s %s", cmd, remote) != PRELIM) { 534 (void) signal(SIGINT, oldintr); 535 if (oldintp) 536 (void) signal(SIGPIPE, oldintp); 537 if (closefunc != NULL) 538 (*closefunc)(fin); 539 return; 540 } 541 } else 542 if (command("%s", cmd) != PRELIM) { 543 (void) signal(SIGINT, oldintr); 544 if (oldintp) 545 (void) signal(SIGPIPE, oldintp); 546 if (closefunc != NULL) 547 (*closefunc)(fin); 548 return; 549 } 550 dout = dataconn(lmode); 551 if (dout == NULL) 552 goto abort; 553 (void) gettimeofday(&start, (struct timezone *)0); 554 oldintp = signal(SIGPIPE, SIG_IGN); 555 switch (curtype) { 556 557 case TYPE_I: 558 case TYPE_L: 559 errno = d = 0; 560 while ((c = read(fileno(fin), buf, sizeof (buf))) > 0) { 561 bytes += c; 562 for (bufp = buf; c > 0; c -= d, bufp += d) 563 if ((d = write(fileno(dout), bufp, c)) <= 0) 564 break; 565 if (hash) { 566 while (bytes >= hashbytes) { 567 (void) putchar('#'); 568 hashbytes += HASHBYTES; 569 } 570 (void) fflush(stdout); 571 } 572 } 573 if (hash && bytes > 0) { 574 if (bytes < HASHBYTES) 575 (void) putchar('#'); 576 (void) putchar('\n'); 577 (void) fflush(stdout); 578 } 579 if (c < 0) 580 warn("local: %s", local); 581 if (d < 0) { 582 if (errno != EPIPE) 583 warn("netout"); 584 bytes = -1; 585 } 586 break; 587 588 case TYPE_A: 589 while ((c = getc(fin)) != EOF) { 590 if (c == '\n') { 591 while (hash && (bytes >= hashbytes)) { 592 (void) putchar('#'); 593 (void) fflush(stdout); 594 hashbytes += HASHBYTES; 595 } 596 if (ferror(dout)) 597 break; 598 (void) putc('\r', dout); 599 bytes++; 600 } 601 (void) putc(c, dout); 602 bytes++; 603 /* if (c == '\r') { */ 604 /* (void) putc('\0', dout); // this violates rfc */ 605 /* bytes++; */ 606 /* } */ 607 } 608 if (hash) { 609 if (bytes < hashbytes) 610 (void) putchar('#'); 611 (void) putchar('\n'); 612 (void) fflush(stdout); 613 } 614 if (ferror(fin)) 615 warn("local: %s", local); 616 if (ferror(dout)) { 617 if (errno != EPIPE) 618 warn("netout"); 619 bytes = -1; 620 } 621 break; 622 } 623 (void) gettimeofday(&stop, (struct timezone *)0); 624 if (closefunc != NULL) 625 (*closefunc)(fin); 626 (void) fclose(dout); 627 (void) getreply(0); 628 (void) signal(SIGINT, oldintr); 629 if (oldintp) 630 (void) signal(SIGPIPE, oldintp); 631 if (bytes > 0) 632 ptransfer("sent", bytes, &start, &stop); 633 return; 634 abort: 635 (void) gettimeofday(&stop, (struct timezone *)0); 636 (void) signal(SIGINT, oldintr); 637 if (oldintp) 638 (void) signal(SIGPIPE, oldintp); 639 if (!cpend) { 640 code = -1; 641 return; 642 } 643 if (data >= 0) { 644 (void) close(data); 645 data = -1; 646 } 647 if (dout) 648 (void) fclose(dout); 649 (void) getreply(0); 650 code = -1; 651 if (closefunc != NULL && fin != NULL) 652 (*closefunc)(fin); 653 if (bytes > 0) 654 ptransfer("sent", bytes, &start, &stop); 655 } 656 657 jmp_buf recvabort; 658 659 void 660 abortrecv() 661 { 662 663 mflag = 0; 664 abrtflag = 0; 665 printf("\nreceive aborted\nwaiting for remote to finish abort\n"); 666 (void) fflush(stdout); 667 longjmp(recvabort, 1); 668 } 669 670 void 671 recvrequest(cmd, local, remote, lmode, printnames) 672 char *cmd, *local, *remote, *lmode; 673 int printnames; 674 { 675 FILE *fout, *din = 0; 676 int (*closefunc) __P((FILE *)); 677 sig_t oldintr, oldintp; 678 int c, d, is_retr, tcrflag, bare_lfs = 0; 679 static int bufsize; 680 static char *buf; 681 long bytes = 0, hashbytes = HASHBYTES; 682 struct timeval start, stop; 683 struct stat st; 684 685 is_retr = strcmp(cmd, "RETR") == 0; 686 if (is_retr && verbose && printnames) { 687 if (local && *local != '-') 688 printf("local: %s ", local); 689 if (remote) 690 printf("remote: %s\n", remote); 691 } 692 if (proxy && is_retr) { 693 proxtrans(cmd, local, remote); 694 return; 695 } 696 closefunc = NULL; 697 oldintr = NULL; 698 oldintp = NULL; 699 tcrflag = !crflag && is_retr; 700 if (setjmp(recvabort)) { 701 while (cpend) { 702 (void) getreply(0); 703 } 704 if (data >= 0) { 705 (void) close(data); 706 data = -1; 707 } 708 if (oldintr) 709 (void) signal(SIGINT, oldintr); 710 code = -1; 711 return; 712 } 713 oldintr = signal(SIGINT, abortrecv); 714 if (strcmp(local, "-") && *local != '|') { 715 if (access(local, 2) < 0) { 716 char *dir = strrchr(local, '/'); 717 718 if (errno != ENOENT && errno != EACCES) { 719 warn("local: %s", local); 720 (void) signal(SIGINT, oldintr); 721 code = -1; 722 return; 723 } 724 if (dir != NULL) 725 *dir = 0; 726 d = access(dir ? local : ".", 2); 727 if (dir != NULL) 728 *dir = '/'; 729 if (d < 0) { 730 warn("local: %s", local); 731 (void) signal(SIGINT, oldintr); 732 code = -1; 733 return; 734 } 735 if (!runique && errno == EACCES && 736 chmod(local, 0600) < 0) { 737 warn("local: %s", local); 738 (void) signal(SIGINT, oldintr); 739 (void) signal(SIGINT, oldintr); 740 code = -1; 741 return; 742 } 743 if (runique && errno == EACCES && 744 (local = gunique(local)) == NULL) { 745 (void) signal(SIGINT, oldintr); 746 code = -1; 747 return; 748 } 749 } 750 else if (runique && (local = gunique(local)) == NULL) { 751 (void) signal(SIGINT, oldintr); 752 code = -1; 753 return; 754 } 755 } 756 if (!is_retr) { 757 if (curtype != TYPE_A) 758 changetype(TYPE_A, 0); 759 } else if (curtype != type) 760 changetype(type, 0); 761 if (initconn()) { 762 (void) signal(SIGINT, oldintr); 763 code = -1; 764 return; 765 } 766 if (setjmp(recvabort)) 767 goto abort; 768 if (is_retr && restart_point && 769 command("REST %ld", (long) restart_point) != CONTINUE) 770 return; 771 if (remote) { 772 if (command("%s %s", cmd, remote) != PRELIM) { 773 (void) signal(SIGINT, oldintr); 774 return; 775 } 776 } else { 777 if (command("%s", cmd) != PRELIM) { 778 (void) signal(SIGINT, oldintr); 779 return; 780 } 781 } 782 din = dataconn("r"); 783 if (din == NULL) 784 goto abort; 785 if (strcmp(local, "-") == 0) 786 fout = stdout; 787 else if (*local == '|') { 788 oldintp = signal(SIGPIPE, SIG_IGN); 789 fout = popen(local + 1, "w"); 790 if (fout == NULL) { 791 warn("%s", local+1); 792 goto abort; 793 } 794 closefunc = pclose; 795 } else { 796 fout = fopen(local, lmode); 797 if (fout == NULL) { 798 warn("local: %s", local); 799 goto abort; 800 } 801 closefunc = fclose; 802 } 803 if (fstat(fileno(fout), &st) < 0 || st.st_blksize == 0) 804 st.st_blksize = BUFSIZ; 805 if (st.st_blksize > bufsize) { 806 if (buf) 807 (void) free(buf); 808 buf = malloc((unsigned)st.st_blksize); 809 if (buf == NULL) { 810 warn("malloc"); 811 bufsize = 0; 812 goto abort; 813 } 814 bufsize = st.st_blksize; 815 } 816 (void) gettimeofday(&start, (struct timezone *)0); 817 switch (curtype) { 818 819 case TYPE_I: 820 case TYPE_L: 821 if (restart_point && 822 lseek(fileno(fout), restart_point, SEEK_SET) < 0) { 823 warn("local: %s", local); 824 if (closefunc != NULL) 825 (*closefunc)(fout); 826 return; 827 } 828 errno = d = 0; 829 while ((c = read(fileno(din), buf, bufsize)) > 0) { 830 if ((d = write(fileno(fout), buf, c)) != c) 831 break; 832 bytes += c; 833 if (hash) { 834 while (bytes >= hashbytes) { 835 (void) putchar('#'); 836 hashbytes += HASHBYTES; 837 } 838 (void) fflush(stdout); 839 } 840 } 841 if (hash && bytes > 0) { 842 if (bytes < HASHBYTES) 843 (void) putchar('#'); 844 (void) putchar('\n'); 845 (void) fflush(stdout); 846 } 847 if (c < 0) { 848 if (errno != EPIPE) 849 warn("netin"); 850 bytes = -1; 851 } 852 if (d < c) { 853 if (d < 0) 854 warn("local: %s", local); 855 else 856 warnx("%s: short write", local); 857 } 858 break; 859 860 case TYPE_A: 861 if (restart_point) { 862 int i, n, ch; 863 864 if (fseek(fout, 0L, SEEK_SET) < 0) 865 goto done; 866 n = restart_point; 867 for (i = 0; i++ < n;) { 868 if ((ch = getc(fout)) == EOF) 869 goto done; 870 if (ch == '\n') 871 i++; 872 } 873 if (fseek(fout, 0L, SEEK_CUR) < 0) { 874 done: 875 warn("local: %s", local); 876 if (closefunc != NULL) 877 (*closefunc)(fout); 878 return; 879 } 880 } 881 while ((c = getc(din)) != EOF) { 882 if (c == '\n') 883 bare_lfs++; 884 while (c == '\r') { 885 while (hash && (bytes >= hashbytes)) { 886 (void) putchar('#'); 887 (void) fflush(stdout); 888 hashbytes += HASHBYTES; 889 } 890 bytes++; 891 if ((c = getc(din)) != '\n' || tcrflag) { 892 if (ferror(fout)) 893 goto break2; 894 (void) putc('\r', fout); 895 if (c == '\0') { 896 bytes++; 897 goto contin2; 898 } 899 if (c == EOF) 900 goto contin2; 901 } 902 } 903 (void) putc(c, fout); 904 bytes++; 905 contin2: ; 906 } 907 break2: 908 if (bare_lfs) { 909 printf("WARNING! %d bare linefeeds received in ASCII mode\n", bare_lfs); 910 printf("File may not have transferred correctly.\n"); 911 } 912 if (hash) { 913 if (bytes < hashbytes) 914 (void) putchar('#'); 915 (void) putchar('\n'); 916 (void) fflush(stdout); 917 } 918 if (ferror(din)) { 919 if (errno != EPIPE) 920 warn("netin"); 921 bytes = -1; 922 } 923 if (ferror(fout)) 924 warn("local: %s", local); 925 break; 926 } 927 if (closefunc != NULL) 928 (*closefunc)(fout); 929 (void) signal(SIGINT, oldintr); 930 if (oldintp) 931 (void) signal(SIGPIPE, oldintp); 932 (void) gettimeofday(&stop, (struct timezone *)0); 933 (void) fclose(din); 934 (void) getreply(0); 935 if (bytes > 0 && is_retr) 936 ptransfer("received", bytes, &start, &stop); 937 return; 938 abort: 939 940 /* abort using RFC959 recommended IP,SYNC sequence */ 941 942 (void) gettimeofday(&stop, (struct timezone *)0); 943 if (oldintp) 944 (void) signal(SIGPIPE, oldintr); 945 (void) signal(SIGINT, SIG_IGN); 946 if (!cpend) { 947 code = -1; 948 (void) signal(SIGINT, oldintr); 949 return; 950 } 951 952 abort_remote(din); 953 code = -1; 954 if (data >= 0) { 955 (void) close(data); 956 data = -1; 957 } 958 if (closefunc != NULL && fout != NULL) 959 (*closefunc)(fout); 960 if (din) 961 (void) fclose(din); 962 if (bytes > 0) 963 ptransfer("received", bytes, &start, &stop); 964 (void) signal(SIGINT, oldintr); 965 } 966 967 /* 968 * Need to start a listen on the data channel before we send the command, 969 * otherwise the server's connect may fail. 970 */ 971 int 972 initconn() 973 { 974 char *p, *a; 975 int result, len, tmpno = 0; 976 int on = 1; 977 978 noport: 979 data_addr = myctladdr; 980 if (sendport) 981 data_addr.sin_port = 0; /* let system pick one */ 982 if (data != -1) 983 (void) close(data); 984 data = socket(AF_INET, SOCK_STREAM, 0); 985 if (data < 0) { 986 warn("socket"); 987 if (tmpno) 988 sendport = 1; 989 return (1); 990 } 991 if (!sendport) 992 if (setsockopt(data, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof (on)) < 0) { 993 warn("setsockopt (reuse address)"); 994 goto bad; 995 } 996 if (bind(data, (struct sockaddr *)&data_addr, sizeof (data_addr)) < 0) { 997 warn("bind"); 998 goto bad; 999 } 1000 if (options & SO_DEBUG && 1001 setsockopt(data, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof (on)) < 0) 1002 warn("setsockopt (ignored)"); 1003 len = sizeof (data_addr); 1004 if (getsockname(data, (struct sockaddr *)&data_addr, &len) < 0) { 1005 warn("getsockname"); 1006 goto bad; 1007 } 1008 if (listen(data, 1) < 0) 1009 warn("listen"); 1010 if (sendport) { 1011 a = (char *)&data_addr.sin_addr; 1012 p = (char *)&data_addr.sin_port; 1013 #define UC(b) (((int)b)&0xff) 1014 result = 1015 command("PORT %d,%d,%d,%d,%d,%d", 1016 UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]), 1017 UC(p[0]), UC(p[1])); 1018 if (result == ERROR && sendport == -1) { 1019 sendport = 0; 1020 tmpno = 1; 1021 goto noport; 1022 } 1023 return (result != COMPLETE); 1024 } 1025 if (tmpno) 1026 sendport = 1; 1027 #ifdef IP_TOS 1028 on = IPTOS_THROUGHPUT; 1029 if (setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0) 1030 warn("setsockopt TOS (ignored)"); 1031 #endif 1032 return (0); 1033 bad: 1034 (void) close(data), data = -1; 1035 if (tmpno) 1036 sendport = 1; 1037 return (1); 1038 } 1039 1040 FILE * 1041 dataconn(lmode) 1042 char *lmode; 1043 { 1044 struct sockaddr_in from; 1045 int s, fromlen = sizeof (from), tos; 1046 1047 s = accept(data, (struct sockaddr *) &from, &fromlen); 1048 if (s < 0) { 1049 warn("accept"); 1050 (void) close(data), data = -1; 1051 return (NULL); 1052 } 1053 (void) close(data); 1054 data = s; 1055 #ifdef IP_TOS 1056 tos = IPTOS_THROUGHPUT; 1057 if (setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(int)) < 0) 1058 warn("setsockopt TOS (ignored)"); 1059 #endif 1060 return (fdopen(data, lmode)); 1061 } 1062 1063 void 1064 ptransfer(direction, bytes, t0, t1) 1065 char *direction; 1066 long bytes; 1067 struct timeval *t0, *t1; 1068 { 1069 struct timeval td; 1070 float s, bs; 1071 1072 if (verbose) { 1073 tvsub(&td, t1, t0); 1074 s = td.tv_sec + (td.tv_usec / 1000000.); 1075 #define nz(x) ((x) == 0 ? 1 : (x)) 1076 bs = bytes / nz(s); 1077 printf("%ld bytes %s in %.2g seconds (%.2g Kbytes/s)\n", 1078 bytes, direction, s, bs / 1024.); 1079 } 1080 } 1081 1082 /* 1083 void 1084 tvadd(tsum, t0) 1085 struct timeval *tsum, *t0; 1086 { 1087 1088 tsum->tv_sec += t0->tv_sec; 1089 tsum->tv_usec += t0->tv_usec; 1090 if (tsum->tv_usec > 1000000) 1091 tsum->tv_sec++, tsum->tv_usec -= 1000000; 1092 } 1093 */ 1094 1095 void 1096 tvsub(tdiff, t1, t0) 1097 struct timeval *tdiff, *t1, *t0; 1098 { 1099 1100 tdiff->tv_sec = t1->tv_sec - t0->tv_sec; 1101 tdiff->tv_usec = t1->tv_usec - t0->tv_usec; 1102 if (tdiff->tv_usec < 0) 1103 tdiff->tv_sec--, tdiff->tv_usec += 1000000; 1104 } 1105 1106 void 1107 psabort() 1108 { 1109 1110 abrtflag++; 1111 } 1112 1113 void 1114 pswitch(flag) 1115 int flag; 1116 { 1117 sig_t oldintr; 1118 static struct comvars { 1119 int connect; 1120 char name[MAXHOSTNAMELEN]; 1121 struct sockaddr_in mctl; 1122 struct sockaddr_in hctl; 1123 FILE *in; 1124 FILE *out; 1125 int tpe; 1126 int curtpe; 1127 int cpnd; 1128 int sunqe; 1129 int runqe; 1130 int mcse; 1131 int ntflg; 1132 char nti[17]; 1133 char nto[17]; 1134 int mapflg; 1135 char mi[MAXPATHLEN]; 1136 char mo[MAXPATHLEN]; 1137 } proxstruct, tmpstruct; 1138 struct comvars *ip, *op; 1139 1140 abrtflag = 0; 1141 oldintr = signal(SIGINT, psabort); 1142 if (flag) { 1143 if (proxy) 1144 return; 1145 ip = &tmpstruct; 1146 op = &proxstruct; 1147 proxy++; 1148 } else { 1149 if (!proxy) 1150 return; 1151 ip = &proxstruct; 1152 op = &tmpstruct; 1153 proxy = 0; 1154 } 1155 ip->connect = connected; 1156 connected = op->connect; 1157 if (hostname) { 1158 (void) strncpy(ip->name, hostname, sizeof(ip->name) - 1); 1159 ip->name[strlen(ip->name)] = '\0'; 1160 } else 1161 ip->name[0] = 0; 1162 hostname = op->name; 1163 ip->hctl = hisctladdr; 1164 hisctladdr = op->hctl; 1165 ip->mctl = myctladdr; 1166 myctladdr = op->mctl; 1167 ip->in = cin; 1168 cin = op->in; 1169 ip->out = cout; 1170 cout = op->out; 1171 ip->tpe = type; 1172 type = op->tpe; 1173 ip->curtpe = curtype; 1174 curtype = op->curtpe; 1175 ip->cpnd = cpend; 1176 cpend = op->cpnd; 1177 ip->sunqe = sunique; 1178 sunique = op->sunqe; 1179 ip->runqe = runique; 1180 runique = op->runqe; 1181 ip->mcse = mcase; 1182 mcase = op->mcse; 1183 ip->ntflg = ntflag; 1184 ntflag = op->ntflg; 1185 (void) strncpy(ip->nti, ntin, 16); 1186 (ip->nti)[strlen(ip->nti)] = '\0'; 1187 (void) strcpy(ntin, op->nti); 1188 (void) strncpy(ip->nto, ntout, 16); 1189 (ip->nto)[strlen(ip->nto)] = '\0'; 1190 (void) strcpy(ntout, op->nto); 1191 ip->mapflg = mapflag; 1192 mapflag = op->mapflg; 1193 (void) strncpy(ip->mi, mapin, MAXPATHLEN - 1); 1194 (ip->mi)[strlen(ip->mi)] = '\0'; 1195 (void) strcpy(mapin, op->mi); 1196 (void) strncpy(ip->mo, mapout, MAXPATHLEN - 1); 1197 (ip->mo)[strlen(ip->mo)] = '\0'; 1198 (void) strcpy(mapout, op->mo); 1199 (void) signal(SIGINT, oldintr); 1200 if (abrtflag) { 1201 abrtflag = 0; 1202 (*oldintr)(SIGINT); 1203 } 1204 } 1205 1206 void 1207 abortpt() 1208 { 1209 1210 printf("\n"); 1211 (void) fflush(stdout); 1212 ptabflg++; 1213 mflag = 0; 1214 abrtflag = 0; 1215 longjmp(ptabort, 1); 1216 } 1217 1218 void 1219 proxtrans(cmd, local, remote) 1220 char *cmd, *local, *remote; 1221 { 1222 sig_t oldintr; 1223 int secndflag = 0, prox_type, nfnd; 1224 char *cmd2; 1225 struct fd_set mask; 1226 1227 if (strcmp(cmd, "RETR")) 1228 cmd2 = "RETR"; 1229 else 1230 cmd2 = runique ? "STOU" : "STOR"; 1231 if ((prox_type = type) == 0) { 1232 if (unix_server && unix_proxy) 1233 prox_type = TYPE_I; 1234 else 1235 prox_type = TYPE_A; 1236 } 1237 if (curtype != prox_type) 1238 changetype(prox_type, 1); 1239 if (command("PASV") != COMPLETE) { 1240 printf("proxy server does not support third party transfers.\n"); 1241 return; 1242 } 1243 pswitch(0); 1244 if (!connected) { 1245 printf("No primary connection\n"); 1246 pswitch(1); 1247 code = -1; 1248 return; 1249 } 1250 if (curtype != prox_type) 1251 changetype(prox_type, 1); 1252 if (command("PORT %s", pasv) != COMPLETE) { 1253 pswitch(1); 1254 return; 1255 } 1256 if (setjmp(ptabort)) 1257 goto abort; 1258 oldintr = signal(SIGINT, abortpt); 1259 if (command("%s %s", cmd, remote) != PRELIM) { 1260 (void) signal(SIGINT, oldintr); 1261 pswitch(1); 1262 return; 1263 } 1264 sleep(2); 1265 pswitch(1); 1266 secndflag++; 1267 if (command("%s %s", cmd2, local) != PRELIM) 1268 goto abort; 1269 ptflag++; 1270 (void) getreply(0); 1271 pswitch(0); 1272 (void) getreply(0); 1273 (void) signal(SIGINT, oldintr); 1274 pswitch(1); 1275 ptflag = 0; 1276 printf("local: %s remote: %s\n", local, remote); 1277 return; 1278 abort: 1279 (void) signal(SIGINT, SIG_IGN); 1280 ptflag = 0; 1281 if (strcmp(cmd, "RETR") && !proxy) 1282 pswitch(1); 1283 else if (!strcmp(cmd, "RETR") && proxy) 1284 pswitch(0); 1285 if (!cpend && !secndflag) { /* only here if cmd = "STOR" (proxy=1) */ 1286 if (command("%s %s", cmd2, local) != PRELIM) { 1287 pswitch(0); 1288 if (cpend) 1289 abort_remote((FILE *) NULL); 1290 } 1291 pswitch(1); 1292 if (ptabflg) 1293 code = -1; 1294 (void) signal(SIGINT, oldintr); 1295 return; 1296 } 1297 if (cpend) 1298 abort_remote((FILE *) NULL); 1299 pswitch(!proxy); 1300 if (!cpend && !secndflag) { /* only if cmd = "RETR" (proxy=1) */ 1301 if (command("%s %s", cmd2, local) != PRELIM) { 1302 pswitch(0); 1303 if (cpend) 1304 abort_remote((FILE *) NULL); 1305 pswitch(1); 1306 if (ptabflg) 1307 code = -1; 1308 (void) signal(SIGINT, oldintr); 1309 return; 1310 } 1311 } 1312 if (cpend) 1313 abort_remote((FILE *) NULL); 1314 pswitch(!proxy); 1315 if (cpend) { 1316 FD_ZERO(&mask); 1317 FD_SET(fileno(cin), &mask); 1318 if ((nfnd = empty(&mask, 10)) <= 0) { 1319 if (nfnd < 0) { 1320 warn("abort"); 1321 } 1322 if (ptabflg) 1323 code = -1; 1324 lostpeer(); 1325 } 1326 (void) getreply(0); 1327 (void) getreply(0); 1328 } 1329 if (proxy) 1330 pswitch(0); 1331 pswitch(1); 1332 if (ptabflg) 1333 code = -1; 1334 (void) signal(SIGINT, oldintr); 1335 } 1336 1337 void 1338 reset(argc, argv) 1339 int argc; 1340 char *argv[]; 1341 { 1342 struct fd_set mask; 1343 int nfnd = 1; 1344 1345 FD_ZERO(&mask); 1346 while (nfnd > 0) { 1347 FD_SET(fileno(cin), &mask); 1348 if ((nfnd = empty(&mask,0)) < 0) { 1349 warn("reset"); 1350 code = -1; 1351 lostpeer(); 1352 } 1353 else if (nfnd) { 1354 (void) getreply(0); 1355 } 1356 } 1357 } 1358 1359 char * 1360 gunique(local) 1361 char *local; 1362 { 1363 static char new[MAXPATHLEN]; 1364 char *cp = strrchr(local, '/'); 1365 int d, count=0; 1366 char ext = '1'; 1367 1368 if (cp) 1369 *cp = '\0'; 1370 d = access(cp ? local : ".", 2); 1371 if (cp) 1372 *cp = '/'; 1373 if (d < 0) { 1374 warn("local: %s", local); 1375 return ((char *) 0); 1376 } 1377 (void) strcpy(new, local); 1378 cp = new + strlen(new); 1379 *cp++ = '.'; 1380 while (!d) { 1381 if (++count == 100) { 1382 printf("runique: can't find unique file name.\n"); 1383 return ((char *) 0); 1384 } 1385 *cp++ = ext; 1386 *cp = '\0'; 1387 if (ext == '9') 1388 ext = '0'; 1389 else 1390 ext++; 1391 if ((d = access(new, 0)) < 0) 1392 break; 1393 if (ext != '0') 1394 cp--; 1395 else if (*(cp - 2) == '.') 1396 *(cp - 1) = '1'; 1397 else { 1398 *(cp - 2) = *(cp - 2) + 1; 1399 cp--; 1400 } 1401 } 1402 return (new); 1403 } 1404 1405 void 1406 abort_remote(din) 1407 FILE *din; 1408 { 1409 char buf[BUFSIZ]; 1410 int nfnd; 1411 struct fd_set mask; 1412 1413 /* 1414 * send IAC in urgent mode instead of DM because 4.3BSD places oob mark 1415 * after urgent byte rather than before as is protocol now 1416 */ 1417 sprintf(buf, "%c%c%c", IAC, IP, IAC); 1418 if (send(fileno(cout), buf, 3, MSG_OOB) != 3) 1419 warn("abort"); 1420 fprintf(cout,"%cABOR\r\n", DM); 1421 (void) fflush(cout); 1422 FD_ZERO(&mask); 1423 FD_SET(fileno(cin), &mask); 1424 if (din) { 1425 FD_SET(fileno(din), &mask); 1426 } 1427 if ((nfnd = empty(&mask, 10)) <= 0) { 1428 if (nfnd < 0) { 1429 warn("abort"); 1430 } 1431 if (ptabflg) 1432 code = -1; 1433 lostpeer(); 1434 } 1435 if (din && FD_ISSET(fileno(din), &mask)) { 1436 while (read(fileno(din), buf, BUFSIZ) > 0) 1437 /* LOOP */; 1438 } 1439 if (getreply(0) == ERROR && code == 552) { 1440 /* 552 needed for nic style abort */ 1441 (void) getreply(0); 1442 } 1443 (void) getreply(0); 1444 } 1445