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.3 (Berkeley) 04/02/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 if (fseek(fin, (long) restart_point, 0) < 0) { 505 warn("local: %s", local); 506 restart_point = 0; 507 if (closefunc != NULL) 508 (*closefunc)(fin); 509 return; 510 } 511 if (command("REST %ld", (long) restart_point) 512 != CONTINUE) { 513 restart_point = 0; 514 if (closefunc != NULL) 515 (*closefunc)(fin); 516 return; 517 } 518 restart_point = 0; 519 lmode = "r+w"; 520 } 521 if (remote) { 522 if (command("%s %s", cmd, remote) != PRELIM) { 523 (void) signal(SIGINT, oldintr); 524 if (oldintp) 525 (void) signal(SIGPIPE, oldintp); 526 if (closefunc != NULL) 527 (*closefunc)(fin); 528 return; 529 } 530 } else 531 if (command("%s", cmd) != PRELIM) { 532 (void) signal(SIGINT, oldintr); 533 if (oldintp) 534 (void) signal(SIGPIPE, oldintp); 535 if (closefunc != NULL) 536 (*closefunc)(fin); 537 return; 538 } 539 dout = dataconn(lmode); 540 if (dout == NULL) 541 goto abort; 542 (void) gettimeofday(&start, (struct timezone *)0); 543 oldintp = signal(SIGPIPE, SIG_IGN); 544 switch (curtype) { 545 546 case TYPE_I: 547 case TYPE_L: 548 errno = d = 0; 549 while ((c = read(fileno(fin), buf, sizeof (buf))) > 0) { 550 bytes += c; 551 for (bufp = buf; c > 0; c -= d, bufp += d) 552 if ((d = write(fileno(dout), bufp, c)) <= 0) 553 break; 554 if (hash) { 555 while (bytes >= hashbytes) { 556 (void) putchar('#'); 557 hashbytes += HASHBYTES; 558 } 559 (void) fflush(stdout); 560 } 561 } 562 if (hash && bytes > 0) { 563 if (bytes < HASHBYTES) 564 (void) putchar('#'); 565 (void) putchar('\n'); 566 (void) fflush(stdout); 567 } 568 if (c < 0) 569 warn("local: %s", local); 570 if (d < 0) { 571 if (errno != EPIPE) 572 warn("netout"); 573 bytes = -1; 574 } 575 break; 576 577 case TYPE_A: 578 while ((c = getc(fin)) != EOF) { 579 if (c == '\n') { 580 while (hash && (bytes >= hashbytes)) { 581 (void) putchar('#'); 582 (void) fflush(stdout); 583 hashbytes += HASHBYTES; 584 } 585 if (ferror(dout)) 586 break; 587 (void) putc('\r', dout); 588 bytes++; 589 } 590 (void) putc(c, dout); 591 bytes++; 592 /* if (c == '\r') { */ 593 /* (void) putc('\0', dout); // this violates rfc */ 594 /* bytes++; */ 595 /* } */ 596 } 597 if (hash) { 598 if (bytes < hashbytes) 599 (void) putchar('#'); 600 (void) putchar('\n'); 601 (void) fflush(stdout); 602 } 603 if (ferror(fin)) 604 warn("local: %s", local); 605 if (ferror(dout)) { 606 if (errno != EPIPE) 607 warn("netout"); 608 bytes = -1; 609 } 610 break; 611 } 612 (void) gettimeofday(&stop, (struct timezone *)0); 613 if (closefunc != NULL) 614 (*closefunc)(fin); 615 (void) fclose(dout); 616 (void) getreply(0); 617 (void) signal(SIGINT, oldintr); 618 if (oldintp) 619 (void) signal(SIGPIPE, oldintp); 620 if (bytes > 0) 621 ptransfer("sent", bytes, &start, &stop); 622 return; 623 abort: 624 (void) gettimeofday(&stop, (struct timezone *)0); 625 (void) signal(SIGINT, oldintr); 626 if (oldintp) 627 (void) signal(SIGPIPE, oldintp); 628 if (!cpend) { 629 code = -1; 630 return; 631 } 632 if (data >= 0) { 633 (void) close(data); 634 data = -1; 635 } 636 if (dout) 637 (void) fclose(dout); 638 (void) getreply(0); 639 code = -1; 640 if (closefunc != NULL && fin != NULL) 641 (*closefunc)(fin); 642 if (bytes > 0) 643 ptransfer("sent", bytes, &start, &stop); 644 } 645 646 jmp_buf recvabort; 647 648 void 649 abortrecv() 650 { 651 652 mflag = 0; 653 abrtflag = 0; 654 printf("\nreceive aborted\nwaiting for remote to finish abort\n"); 655 (void) fflush(stdout); 656 longjmp(recvabort, 1); 657 } 658 659 void 660 recvrequest(cmd, local, remote, lmode, printnames) 661 char *cmd, *local, *remote, *lmode; 662 int printnames; 663 { 664 FILE *fout, *din = 0; 665 int (*closefunc) __P((FILE *)); 666 sig_t oldintr, oldintp; 667 int c, d, is_retr, tcrflag, bare_lfs = 0; 668 static int bufsize; 669 static char *buf; 670 long bytes = 0, hashbytes = HASHBYTES; 671 struct timeval start, stop; 672 struct stat st; 673 674 is_retr = strcmp(cmd, "RETR") == 0; 675 if (is_retr && verbose && printnames) { 676 if (local && *local != '-') 677 printf("local: %s ", local); 678 if (remote) 679 printf("remote: %s\n", remote); 680 } 681 if (proxy && is_retr) { 682 proxtrans(cmd, local, remote); 683 return; 684 } 685 closefunc = NULL; 686 oldintr = NULL; 687 oldintp = NULL; 688 tcrflag = !crflag && is_retr; 689 if (setjmp(recvabort)) { 690 while (cpend) { 691 (void) getreply(0); 692 } 693 if (data >= 0) { 694 (void) close(data); 695 data = -1; 696 } 697 if (oldintr) 698 (void) signal(SIGINT, oldintr); 699 code = -1; 700 return; 701 } 702 oldintr = signal(SIGINT, abortrecv); 703 if (strcmp(local, "-") && *local != '|') { 704 if (access(local, 2) < 0) { 705 char *dir = strrchr(local, '/'); 706 707 if (errno != ENOENT && errno != EACCES) { 708 warn("local: %s", local); 709 (void) signal(SIGINT, oldintr); 710 code = -1; 711 return; 712 } 713 if (dir != NULL) 714 *dir = 0; 715 d = access(dir ? local : ".", 2); 716 if (dir != NULL) 717 *dir = '/'; 718 if (d < 0) { 719 warn("local: %s", local); 720 (void) signal(SIGINT, oldintr); 721 code = -1; 722 return; 723 } 724 if (!runique && errno == EACCES && 725 chmod(local, 0600) < 0) { 726 warn("local: %s", local); 727 (void) signal(SIGINT, oldintr); 728 (void) signal(SIGINT, oldintr); 729 code = -1; 730 return; 731 } 732 if (runique && errno == EACCES && 733 (local = gunique(local)) == NULL) { 734 (void) signal(SIGINT, oldintr); 735 code = -1; 736 return; 737 } 738 } 739 else if (runique && (local = gunique(local)) == NULL) { 740 (void) signal(SIGINT, oldintr); 741 code = -1; 742 return; 743 } 744 } 745 if (!is_retr) { 746 if (curtype != TYPE_A) 747 changetype(TYPE_A, 0); 748 } else if (curtype != type) 749 changetype(type, 0); 750 if (initconn()) { 751 (void) signal(SIGINT, oldintr); 752 code = -1; 753 return; 754 } 755 if (setjmp(recvabort)) 756 goto abort; 757 if (is_retr && restart_point && 758 command("REST %ld", (long) restart_point) != CONTINUE) 759 return; 760 if (remote) { 761 if (command("%s %s", cmd, remote) != PRELIM) { 762 (void) signal(SIGINT, oldintr); 763 return; 764 } 765 } else { 766 if (command("%s", cmd) != PRELIM) { 767 (void) signal(SIGINT, oldintr); 768 return; 769 } 770 } 771 din = dataconn("r"); 772 if (din == NULL) 773 goto abort; 774 if (strcmp(local, "-") == 0) 775 fout = stdout; 776 else if (*local == '|') { 777 oldintp = signal(SIGPIPE, SIG_IGN); 778 fout = popen(local + 1, "w"); 779 if (fout == NULL) { 780 warn("%s", local+1); 781 goto abort; 782 } 783 closefunc = pclose; 784 } else { 785 fout = fopen(local, lmode); 786 if (fout == NULL) { 787 warn("local: %s", local); 788 goto abort; 789 } 790 closefunc = fclose; 791 } 792 if (fstat(fileno(fout), &st) < 0 || st.st_blksize == 0) 793 st.st_blksize = BUFSIZ; 794 if (st.st_blksize > bufsize) { 795 if (buf) 796 (void) free(buf); 797 buf = malloc((unsigned)st.st_blksize); 798 if (buf == NULL) { 799 warn("malloc"); 800 bufsize = 0; 801 goto abort; 802 } 803 bufsize = st.st_blksize; 804 } 805 (void) gettimeofday(&start, (struct timezone *)0); 806 switch (curtype) { 807 808 case TYPE_I: 809 case TYPE_L: 810 if (restart_point && 811 lseek(fileno(fout), restart_point, L_SET) < 0) { 812 warn("local: %s", local); 813 if (closefunc != NULL) 814 (*closefunc)(fout); 815 return; 816 } 817 errno = d = 0; 818 while ((c = read(fileno(din), buf, bufsize)) > 0) { 819 if ((d = write(fileno(fout), buf, c)) != c) 820 break; 821 bytes += c; 822 if (hash) { 823 while (bytes >= hashbytes) { 824 (void) putchar('#'); 825 hashbytes += HASHBYTES; 826 } 827 (void) fflush(stdout); 828 } 829 } 830 if (hash && bytes > 0) { 831 if (bytes < HASHBYTES) 832 (void) putchar('#'); 833 (void) putchar('\n'); 834 (void) fflush(stdout); 835 } 836 if (c < 0) { 837 if (errno != EPIPE) 838 warn("netin"); 839 bytes = -1; 840 } 841 if (d < c) { 842 if (d < 0) 843 warn("local: %s", local); 844 else 845 warnx("%s: short write", local); 846 } 847 break; 848 849 case TYPE_A: 850 if (restart_point) { 851 int i, n, ch; 852 853 if (fseek(fout, 0L, L_SET) < 0) 854 goto done; 855 n = restart_point; 856 for (i = 0; i++ < n;) { 857 if ((ch = getc(fout)) == EOF) 858 goto done; 859 if (ch == '\n') 860 i++; 861 } 862 if (fseek(fout, 0L, L_INCR) < 0) { 863 done: 864 warn("local: %s", local); 865 if (closefunc != NULL) 866 (*closefunc)(fout); 867 return; 868 } 869 } 870 while ((c = getc(din)) != EOF) { 871 if (c == '\n') 872 bare_lfs++; 873 while (c == '\r') { 874 while (hash && (bytes >= hashbytes)) { 875 (void) putchar('#'); 876 (void) fflush(stdout); 877 hashbytes += HASHBYTES; 878 } 879 bytes++; 880 if ((c = getc(din)) != '\n' || tcrflag) { 881 if (ferror(fout)) 882 goto break2; 883 (void) putc('\r', fout); 884 if (c == '\0') { 885 bytes++; 886 goto contin2; 887 } 888 if (c == EOF) 889 goto contin2; 890 } 891 } 892 (void) putc(c, fout); 893 bytes++; 894 contin2: ; 895 } 896 break2: 897 if (bare_lfs) { 898 printf("WARNING! %d bare linefeeds received in ASCII mode\n", bare_lfs); 899 printf("File may not have transferred correctly.\n"); 900 } 901 if (hash) { 902 if (bytes < hashbytes) 903 (void) putchar('#'); 904 (void) putchar('\n'); 905 (void) fflush(stdout); 906 } 907 if (ferror(din)) { 908 if (errno != EPIPE) 909 warn("netin"); 910 bytes = -1; 911 } 912 if (ferror(fout)) 913 warn("local: %s", local); 914 break; 915 } 916 if (closefunc != NULL) 917 (*closefunc)(fout); 918 (void) signal(SIGINT, oldintr); 919 if (oldintp) 920 (void) signal(SIGPIPE, oldintp); 921 (void) gettimeofday(&stop, (struct timezone *)0); 922 (void) fclose(din); 923 (void) getreply(0); 924 if (bytes > 0 && is_retr) 925 ptransfer("received", bytes, &start, &stop); 926 return; 927 abort: 928 929 /* abort using RFC959 recommended IP,SYNC sequence */ 930 931 (void) gettimeofday(&stop, (struct timezone *)0); 932 if (oldintp) 933 (void) signal(SIGPIPE, oldintr); 934 (void) signal(SIGINT, SIG_IGN); 935 if (!cpend) { 936 code = -1; 937 (void) signal(SIGINT, oldintr); 938 return; 939 } 940 941 abort_remote(din); 942 code = -1; 943 if (data >= 0) { 944 (void) close(data); 945 data = -1; 946 } 947 if (closefunc != NULL && fout != NULL) 948 (*closefunc)(fout); 949 if (din) 950 (void) fclose(din); 951 if (bytes > 0) 952 ptransfer("received", bytes, &start, &stop); 953 (void) signal(SIGINT, oldintr); 954 } 955 956 /* 957 * Need to start a listen on the data channel before we send the command, 958 * otherwise the server's connect may fail. 959 */ 960 int 961 initconn() 962 { 963 char *p, *a; 964 int result, len, tmpno = 0; 965 int on = 1; 966 967 noport: 968 data_addr = myctladdr; 969 if (sendport) 970 data_addr.sin_port = 0; /* let system pick one */ 971 if (data != -1) 972 (void) close(data); 973 data = socket(AF_INET, SOCK_STREAM, 0); 974 if (data < 0) { 975 warn("socket"); 976 if (tmpno) 977 sendport = 1; 978 return (1); 979 } 980 if (!sendport) 981 if (setsockopt(data, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof (on)) < 0) { 982 warn("setsockopt (reuse address)"); 983 goto bad; 984 } 985 if (bind(data, (struct sockaddr *)&data_addr, sizeof (data_addr)) < 0) { 986 warn("bind"); 987 goto bad; 988 } 989 if (options & SO_DEBUG && 990 setsockopt(data, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof (on)) < 0) 991 warn("setsockopt (ignored)"); 992 len = sizeof (data_addr); 993 if (getsockname(data, (struct sockaddr *)&data_addr, &len) < 0) { 994 warn("getsockname"); 995 goto bad; 996 } 997 if (listen(data, 1) < 0) 998 warn("listen"); 999 if (sendport) { 1000 a = (char *)&data_addr.sin_addr; 1001 p = (char *)&data_addr.sin_port; 1002 #define UC(b) (((int)b)&0xff) 1003 result = 1004 command("PORT %d,%d,%d,%d,%d,%d", 1005 UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]), 1006 UC(p[0]), UC(p[1])); 1007 if (result == ERROR && sendport == -1) { 1008 sendport = 0; 1009 tmpno = 1; 1010 goto noport; 1011 } 1012 return (result != COMPLETE); 1013 } 1014 if (tmpno) 1015 sendport = 1; 1016 #ifdef IP_TOS 1017 on = IPTOS_THROUGHPUT; 1018 if (setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0) 1019 warn("setsockopt TOS (ignored)"); 1020 #endif 1021 return (0); 1022 bad: 1023 (void) close(data), data = -1; 1024 if (tmpno) 1025 sendport = 1; 1026 return (1); 1027 } 1028 1029 FILE * 1030 dataconn(lmode) 1031 char *lmode; 1032 { 1033 struct sockaddr_in from; 1034 int s, fromlen = sizeof (from), tos; 1035 1036 s = accept(data, (struct sockaddr *) &from, &fromlen); 1037 if (s < 0) { 1038 warn("accept"); 1039 (void) close(data), data = -1; 1040 return (NULL); 1041 } 1042 (void) close(data); 1043 data = s; 1044 #ifdef IP_TOS 1045 tos = IPTOS_THROUGHPUT; 1046 if (setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(int)) < 0) 1047 warn("setsockopt TOS (ignored)"); 1048 #endif 1049 return (fdopen(data, lmode)); 1050 } 1051 1052 void 1053 ptransfer(direction, bytes, t0, t1) 1054 char *direction; 1055 long bytes; 1056 struct timeval *t0, *t1; 1057 { 1058 struct timeval td; 1059 float s, bs; 1060 1061 if (verbose) { 1062 tvsub(&td, t1, t0); 1063 s = td.tv_sec + (td.tv_usec / 1000000.); 1064 #define nz(x) ((x) == 0 ? 1 : (x)) 1065 bs = bytes / nz(s); 1066 printf("%ld bytes %s in %.2g seconds (%.2g Kbytes/s)\n", 1067 bytes, direction, s, bs / 1024.); 1068 } 1069 } 1070 1071 /* 1072 void 1073 tvadd(tsum, t0) 1074 struct timeval *tsum, *t0; 1075 { 1076 1077 tsum->tv_sec += t0->tv_sec; 1078 tsum->tv_usec += t0->tv_usec; 1079 if (tsum->tv_usec > 1000000) 1080 tsum->tv_sec++, tsum->tv_usec -= 1000000; 1081 } 1082 */ 1083 1084 void 1085 tvsub(tdiff, t1, t0) 1086 struct timeval *tdiff, *t1, *t0; 1087 { 1088 1089 tdiff->tv_sec = t1->tv_sec - t0->tv_sec; 1090 tdiff->tv_usec = t1->tv_usec - t0->tv_usec; 1091 if (tdiff->tv_usec < 0) 1092 tdiff->tv_sec--, tdiff->tv_usec += 1000000; 1093 } 1094 1095 void 1096 psabort() 1097 { 1098 1099 abrtflag++; 1100 } 1101 1102 void 1103 pswitch(flag) 1104 int flag; 1105 { 1106 sig_t oldintr; 1107 static struct comvars { 1108 int connect; 1109 char name[MAXHOSTNAMELEN]; 1110 struct sockaddr_in mctl; 1111 struct sockaddr_in hctl; 1112 FILE *in; 1113 FILE *out; 1114 int tpe; 1115 int curtpe; 1116 int cpnd; 1117 int sunqe; 1118 int runqe; 1119 int mcse; 1120 int ntflg; 1121 char nti[17]; 1122 char nto[17]; 1123 int mapflg; 1124 char mi[MAXPATHLEN]; 1125 char mo[MAXPATHLEN]; 1126 } proxstruct, tmpstruct; 1127 struct comvars *ip, *op; 1128 1129 abrtflag = 0; 1130 oldintr = signal(SIGINT, psabort); 1131 if (flag) { 1132 if (proxy) 1133 return; 1134 ip = &tmpstruct; 1135 op = &proxstruct; 1136 proxy++; 1137 } else { 1138 if (!proxy) 1139 return; 1140 ip = &proxstruct; 1141 op = &tmpstruct; 1142 proxy = 0; 1143 } 1144 ip->connect = connected; 1145 connected = op->connect; 1146 if (hostname) { 1147 (void) strncpy(ip->name, hostname, sizeof(ip->name) - 1); 1148 ip->name[strlen(ip->name)] = '\0'; 1149 } else 1150 ip->name[0] = 0; 1151 hostname = op->name; 1152 ip->hctl = hisctladdr; 1153 hisctladdr = op->hctl; 1154 ip->mctl = myctladdr; 1155 myctladdr = op->mctl; 1156 ip->in = cin; 1157 cin = op->in; 1158 ip->out = cout; 1159 cout = op->out; 1160 ip->tpe = type; 1161 type = op->tpe; 1162 ip->curtpe = curtype; 1163 curtype = op->curtpe; 1164 ip->cpnd = cpend; 1165 cpend = op->cpnd; 1166 ip->sunqe = sunique; 1167 sunique = op->sunqe; 1168 ip->runqe = runique; 1169 runique = op->runqe; 1170 ip->mcse = mcase; 1171 mcase = op->mcse; 1172 ip->ntflg = ntflag; 1173 ntflag = op->ntflg; 1174 (void) strncpy(ip->nti, ntin, 16); 1175 (ip->nti)[strlen(ip->nti)] = '\0'; 1176 (void) strcpy(ntin, op->nti); 1177 (void) strncpy(ip->nto, ntout, 16); 1178 (ip->nto)[strlen(ip->nto)] = '\0'; 1179 (void) strcpy(ntout, op->nto); 1180 ip->mapflg = mapflag; 1181 mapflag = op->mapflg; 1182 (void) strncpy(ip->mi, mapin, MAXPATHLEN - 1); 1183 (ip->mi)[strlen(ip->mi)] = '\0'; 1184 (void) strcpy(mapin, op->mi); 1185 (void) strncpy(ip->mo, mapout, MAXPATHLEN - 1); 1186 (ip->mo)[strlen(ip->mo)] = '\0'; 1187 (void) strcpy(mapout, op->mo); 1188 (void) signal(SIGINT, oldintr); 1189 if (abrtflag) { 1190 abrtflag = 0; 1191 (*oldintr)(SIGINT); 1192 } 1193 } 1194 1195 void 1196 abortpt() 1197 { 1198 1199 printf("\n"); 1200 (void) fflush(stdout); 1201 ptabflg++; 1202 mflag = 0; 1203 abrtflag = 0; 1204 longjmp(ptabort, 1); 1205 } 1206 1207 void 1208 proxtrans(cmd, local, remote) 1209 char *cmd, *local, *remote; 1210 { 1211 sig_t oldintr; 1212 int secndflag = 0, prox_type, nfnd; 1213 char *cmd2; 1214 struct fd_set mask; 1215 1216 if (strcmp(cmd, "RETR")) 1217 cmd2 = "RETR"; 1218 else 1219 cmd2 = runique ? "STOU" : "STOR"; 1220 if ((prox_type = type) == 0) { 1221 if (unix_server && unix_proxy) 1222 prox_type = TYPE_I; 1223 else 1224 prox_type = TYPE_A; 1225 } 1226 if (curtype != prox_type) 1227 changetype(prox_type, 1); 1228 if (command("PASV") != COMPLETE) { 1229 printf("proxy server does not support third party transfers.\n"); 1230 return; 1231 } 1232 pswitch(0); 1233 if (!connected) { 1234 printf("No primary connection\n"); 1235 pswitch(1); 1236 code = -1; 1237 return; 1238 } 1239 if (curtype != prox_type) 1240 changetype(prox_type, 1); 1241 if (command("PORT %s", pasv) != COMPLETE) { 1242 pswitch(1); 1243 return; 1244 } 1245 if (setjmp(ptabort)) 1246 goto abort; 1247 oldintr = signal(SIGINT, abortpt); 1248 if (command("%s %s", cmd, remote) != PRELIM) { 1249 (void) signal(SIGINT, oldintr); 1250 pswitch(1); 1251 return; 1252 } 1253 sleep(2); 1254 pswitch(1); 1255 secndflag++; 1256 if (command("%s %s", cmd2, local) != PRELIM) 1257 goto abort; 1258 ptflag++; 1259 (void) getreply(0); 1260 pswitch(0); 1261 (void) getreply(0); 1262 (void) signal(SIGINT, oldintr); 1263 pswitch(1); 1264 ptflag = 0; 1265 printf("local: %s remote: %s\n", local, remote); 1266 return; 1267 abort: 1268 (void) signal(SIGINT, SIG_IGN); 1269 ptflag = 0; 1270 if (strcmp(cmd, "RETR") && !proxy) 1271 pswitch(1); 1272 else if (!strcmp(cmd, "RETR") && proxy) 1273 pswitch(0); 1274 if (!cpend && !secndflag) { /* only here if cmd = "STOR" (proxy=1) */ 1275 if (command("%s %s", cmd2, local) != PRELIM) { 1276 pswitch(0); 1277 if (cpend) 1278 abort_remote((FILE *) NULL); 1279 } 1280 pswitch(1); 1281 if (ptabflg) 1282 code = -1; 1283 (void) signal(SIGINT, oldintr); 1284 return; 1285 } 1286 if (cpend) 1287 abort_remote((FILE *) NULL); 1288 pswitch(!proxy); 1289 if (!cpend && !secndflag) { /* only if cmd = "RETR" (proxy=1) */ 1290 if (command("%s %s", cmd2, local) != PRELIM) { 1291 pswitch(0); 1292 if (cpend) 1293 abort_remote((FILE *) NULL); 1294 pswitch(1); 1295 if (ptabflg) 1296 code = -1; 1297 (void) signal(SIGINT, oldintr); 1298 return; 1299 } 1300 } 1301 if (cpend) 1302 abort_remote((FILE *) NULL); 1303 pswitch(!proxy); 1304 if (cpend) { 1305 FD_ZERO(&mask); 1306 FD_SET(fileno(cin), &mask); 1307 if ((nfnd = empty(&mask, 10)) <= 0) { 1308 if (nfnd < 0) { 1309 warn("abort"); 1310 } 1311 if (ptabflg) 1312 code = -1; 1313 lostpeer(); 1314 } 1315 (void) getreply(0); 1316 (void) getreply(0); 1317 } 1318 if (proxy) 1319 pswitch(0); 1320 pswitch(1); 1321 if (ptabflg) 1322 code = -1; 1323 (void) signal(SIGINT, oldintr); 1324 } 1325 1326 void 1327 reset(argc, argv) 1328 int argc; 1329 char *argv[]; 1330 { 1331 struct fd_set mask; 1332 int nfnd = 1; 1333 1334 FD_ZERO(&mask); 1335 while (nfnd > 0) { 1336 FD_SET(fileno(cin), &mask); 1337 if ((nfnd = empty(&mask,0)) < 0) { 1338 warn("reset"); 1339 code = -1; 1340 lostpeer(); 1341 } 1342 else if (nfnd) { 1343 (void) getreply(0); 1344 } 1345 } 1346 } 1347 1348 char * 1349 gunique(local) 1350 char *local; 1351 { 1352 static char new[MAXPATHLEN]; 1353 char *cp = strrchr(local, '/'); 1354 int d, count=0; 1355 char ext = '1'; 1356 1357 if (cp) 1358 *cp = '\0'; 1359 d = access(cp ? local : ".", 2); 1360 if (cp) 1361 *cp = '/'; 1362 if (d < 0) { 1363 warn("local: %s", local); 1364 return ((char *) 0); 1365 } 1366 (void) strcpy(new, local); 1367 cp = new + strlen(new); 1368 *cp++ = '.'; 1369 while (!d) { 1370 if (++count == 100) { 1371 printf("runique: can't find unique file name.\n"); 1372 return ((char *) 0); 1373 } 1374 *cp++ = ext; 1375 *cp = '\0'; 1376 if (ext == '9') 1377 ext = '0'; 1378 else 1379 ext++; 1380 if ((d = access(new, 0)) < 0) 1381 break; 1382 if (ext != '0') 1383 cp--; 1384 else if (*(cp - 2) == '.') 1385 *(cp - 1) = '1'; 1386 else { 1387 *(cp - 2) = *(cp - 2) + 1; 1388 cp--; 1389 } 1390 } 1391 return (new); 1392 } 1393 1394 void 1395 abort_remote(din) 1396 FILE *din; 1397 { 1398 char buf[BUFSIZ]; 1399 int nfnd; 1400 struct fd_set mask; 1401 1402 /* 1403 * send IAC in urgent mode instead of DM because 4.3BSD places oob mark 1404 * after urgent byte rather than before as is protocol now 1405 */ 1406 sprintf(buf, "%c%c%c", IAC, IP, IAC); 1407 if (send(fileno(cout), buf, 3, MSG_OOB) != 3) 1408 warn("abort"); 1409 fprintf(cout,"%cABOR\r\n", DM); 1410 (void) fflush(cout); 1411 FD_ZERO(&mask); 1412 FD_SET(fileno(cin), &mask); 1413 if (din) { 1414 FD_SET(fileno(din), &mask); 1415 } 1416 if ((nfnd = empty(&mask, 10)) <= 0) { 1417 if (nfnd < 0) { 1418 warn("abort"); 1419 } 1420 if (ptabflg) 1421 code = -1; 1422 lostpeer(); 1423 } 1424 if (din && FD_ISSET(fileno(din), &mask)) { 1425 while (read(fileno(din), buf, BUFSIZ) > 0) 1426 /* LOOP */; 1427 } 1428 if (getreply(0) == ERROR && code == 552) { 1429 /* 552 needed for nic style abort */ 1430 (void) getreply(0); 1431 } 1432 (void) getreply(0); 1433 } 1434