1 /* 2 * Copyright (c) 1985, 1988, 1990 Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 char copyright[] = 10 "@(#) Copyright (c) 1985, 1988, 1990 Regents of the University of California.\n\ 11 All rights reserved.\n"; 12 #endif /* not lint */ 13 14 #ifndef lint 15 static char sccsid[] = "@(#)ftpd.c 5.39 (Berkeley) 02/25/91"; 16 #endif /* not lint */ 17 18 /* 19 * FTP server. 20 */ 21 #include <sys/param.h> 22 #include <sys/stat.h> 23 #include <sys/ioctl.h> 24 #include <sys/socket.h> 25 #include <sys/wait.h> 26 27 #include <netinet/in.h> 28 #include <netinet/in_systm.h> 29 #include <netinet/ip.h> 30 31 #define FTP_NAMES 32 #include <arpa/ftp.h> 33 #include <arpa/inet.h> 34 #include <arpa/telnet.h> 35 36 #include <signal.h> 37 #include <dirent.h> 38 #include <fcntl.h> 39 #include <time.h> 40 #include <pwd.h> 41 #include <setjmp.h> 42 #include <netdb.h> 43 #include <errno.h> 44 #include <syslog.h> 45 #include <varargs.h> 46 #include <unistd.h> 47 #include <stdio.h> 48 #include <ctype.h> 49 #include <stdlib.h> 50 #include <string.h> 51 #include "pathnames.h" 52 53 /* 54 * File containing login names 55 * NOT to be used on this machine. 56 * Commonly used to disallow uucp. 57 */ 58 extern int errno; 59 extern char *crypt(); 60 extern char version[]; 61 extern char *home; /* pointer to home directory for glob */ 62 extern FILE *ftpd_popen(), *fopen(), *freopen(); 63 extern int ftpd_pclose(), fclose(); 64 extern char *getline(); 65 extern char cbuf[]; 66 extern off_t restart_point; 67 68 struct sockaddr_in ctrl_addr; 69 struct sockaddr_in data_source; 70 struct sockaddr_in data_dest; 71 struct sockaddr_in his_addr; 72 struct sockaddr_in pasv_addr; 73 74 int data; 75 jmp_buf errcatch, urgcatch; 76 int logged_in; 77 struct passwd *pw; 78 int debug; 79 int timeout = 900; /* timeout after 15 minutes of inactivity */ 80 int maxtimeout = 7200;/* don't allow idle time to be set beyond 2 hours */ 81 int logging; 82 int guest; 83 int type; 84 int form; 85 int stru; /* avoid C keyword */ 86 int mode; 87 int usedefault = 1; /* for data transfers */ 88 int pdata = -1; /* for passive mode */ 89 int transflag; 90 off_t file_size; 91 off_t byte_count; 92 #if !defined(CMASK) || CMASK == 0 93 #undef CMASK 94 #define CMASK 027 95 #endif 96 int defumask = CMASK; /* default umask value */ 97 char tmpline[7]; 98 char hostname[MAXHOSTNAMELEN]; 99 char remotehost[MAXHOSTNAMELEN]; 100 101 /* 102 * Timeout intervals for retrying connections 103 * to hosts that don't accept PORT cmds. This 104 * is a kludge, but given the problems with TCP... 105 */ 106 #define SWAITMAX 90 /* wait at most 90 seconds */ 107 #define SWAITINT 5 /* interval between retries */ 108 109 int swaitmax = SWAITMAX; 110 int swaitint = SWAITINT; 111 112 void lostconn(), myoob(); 113 FILE *getdatasock(), *dataconn(); 114 115 #ifdef SETPROCTITLE 116 char **Argv = NULL; /* pointer to argument vector */ 117 char *LastArgv = NULL; /* end of argv */ 118 char proctitle[BUFSIZ]; /* initial part of title */ 119 #endif /* SETPROCTITLE */ 120 121 main(argc, argv, envp) 122 int argc; 123 char *argv[]; 124 char **envp; 125 { 126 int addrlen, on = 1, tos; 127 char *cp; 128 129 /* 130 * LOG_NDELAY sets up the logging connection immediately, 131 * necessary for anonymous ftp's that chroot and can't do it later. 132 */ 133 openlog("ftpd", LOG_PID | LOG_NDELAY, LOG_DAEMON); 134 addrlen = sizeof (his_addr); 135 if (getpeername(0, (struct sockaddr *)&his_addr, &addrlen) < 0) { 136 syslog(LOG_ERR, "getpeername (%s): %m",argv[0]); 137 exit(1); 138 } 139 addrlen = sizeof (ctrl_addr); 140 if (getsockname(0, (struct sockaddr *)&ctrl_addr, &addrlen) < 0) { 141 syslog(LOG_ERR, "getsockname (%s): %m",argv[0]); 142 exit(1); 143 } 144 #ifdef IP_TOS 145 tos = IPTOS_LOWDELAY; 146 if (setsockopt(0, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(int)) < 0) 147 syslog(LOG_WARNING, "setsockopt (IP_TOS): %m"); 148 #endif 149 data_source.sin_port = htons(ntohs(ctrl_addr.sin_port) - 1); 150 debug = 0; 151 #ifdef SETPROCTITLE 152 /* 153 * Save start and extent of argv for setproctitle. 154 */ 155 Argv = argv; 156 while (*envp) 157 envp++; 158 LastArgv = envp[-1] + strlen(envp[-1]); 159 #endif /* SETPROCTITLE */ 160 161 argc--, argv++; 162 while (argc > 0 && *argv[0] == '-') { 163 for (cp = &argv[0][1]; *cp; cp++) switch (*cp) { 164 165 case 'v': 166 debug = 1; 167 break; 168 169 case 'd': 170 debug = 1; 171 break; 172 173 case 'l': 174 logging = 1; 175 break; 176 177 case 't': 178 timeout = atoi(++cp); 179 if (maxtimeout < timeout) 180 maxtimeout = timeout; 181 goto nextopt; 182 183 case 'T': 184 maxtimeout = atoi(++cp); 185 if (timeout > maxtimeout) 186 timeout = maxtimeout; 187 goto nextopt; 188 189 case 'u': 190 { 191 int val = 0; 192 193 while (*++cp && *cp >= '0' && *cp <= '9') 194 val = val*8 + *cp - '0'; 195 if (*cp) 196 fprintf(stderr, "ftpd: Bad value for -u\n"); 197 else 198 defumask = val; 199 goto nextopt; 200 } 201 202 default: 203 fprintf(stderr, "ftpd: Unknown flag -%c ignored.\n", 204 *cp); 205 break; 206 } 207 nextopt: 208 argc--, argv++; 209 } 210 (void) freopen(_PATH_DEVNULL, "w", stderr); 211 (void) signal(SIGPIPE, lostconn); 212 (void) signal(SIGCHLD, SIG_IGN); 213 if ((int)signal(SIGURG, myoob) < 0) 214 syslog(LOG_ERR, "signal: %m"); 215 216 /* Try to handle urgent data inline */ 217 #ifdef SO_OOBINLINE 218 if (setsockopt(0, SOL_SOCKET, SO_OOBINLINE, (char *)&on, sizeof(on)) < 0) 219 syslog(LOG_ERR, "setsockopt: %m"); 220 #endif 221 222 #ifdef F_SETOWN 223 if (fcntl(fileno(stdin), F_SETOWN, getpid()) == -1) 224 syslog(LOG_ERR, "fcntl F_SETOWN: %m"); 225 #endif 226 dolog(&his_addr); 227 /* 228 * Set up default state 229 */ 230 data = -1; 231 type = TYPE_A; 232 form = FORM_N; 233 stru = STRU_F; 234 mode = MODE_S; 235 tmpline[0] = '\0'; 236 (void) gethostname(hostname, sizeof (hostname)); 237 reply(220, "%s FTP server (%s) ready.", hostname, version); 238 (void) setjmp(errcatch); 239 for (;;) 240 (void) yyparse(); 241 /* NOTREACHED */ 242 } 243 244 void 245 lostconn() 246 { 247 if (debug) 248 syslog(LOG_DEBUG, "lost connection"); 249 dologout(-1); 250 } 251 252 static char ttyline[20]; 253 254 /* 255 * Helper function for sgetpwnam(). 256 */ 257 char * 258 sgetsave(s) 259 char *s; 260 { 261 char *new = malloc((unsigned) strlen(s) + 1); 262 263 if (new == NULL) { 264 perror_reply(421, "Local resource failure: malloc"); 265 dologout(1); 266 /* NOTREACHED */ 267 } 268 (void) strcpy(new, s); 269 return (new); 270 } 271 272 /* 273 * Save the result of a getpwnam. Used for USER command, since 274 * the data returned must not be clobbered by any other command 275 * (e.g., globbing). 276 */ 277 struct passwd * 278 sgetpwnam(name) 279 char *name; 280 { 281 static struct passwd save; 282 register struct passwd *p; 283 char *sgetsave(); 284 285 if ((p = getpwnam(name)) == NULL) 286 return (p); 287 if (save.pw_name) { 288 free(save.pw_name); 289 free(save.pw_passwd); 290 free(save.pw_gecos); 291 free(save.pw_dir); 292 free(save.pw_shell); 293 } 294 save = *p; 295 save.pw_name = sgetsave(p->pw_name); 296 save.pw_passwd = sgetsave(p->pw_passwd); 297 save.pw_gecos = sgetsave(p->pw_gecos); 298 save.pw_dir = sgetsave(p->pw_dir); 299 save.pw_shell = sgetsave(p->pw_shell); 300 return (&save); 301 } 302 303 int login_attempts; /* number of failed login attempts */ 304 int askpasswd; /* had user command, ask for passwd */ 305 306 /* 307 * USER command. 308 * Sets global passwd pointer pw if named account exists and is acceptable; 309 * sets askpasswd if a PASS command is expected. If logged in previously, 310 * need to reset state. If name is "ftp" or "anonymous", the name is not in 311 * _PATH_FTPUSERS, and ftp account exists, set guest and pw, then just return. 312 * If account doesn't exist, ask for passwd anyway. Otherwise, check user 313 * requesting login privileges. Disallow anyone who does not have a standard 314 * shell as returned by getusershell(). Disallow anyone mentioned in the file 315 * _PATH_FTPUSERS to allow people such as root and uucp to be avoided. 316 */ 317 user(name) 318 char *name; 319 { 320 register char *cp; 321 char *shell; 322 char *getusershell(); 323 324 if (logged_in) { 325 if (guest) { 326 reply(530, "Can't change user from guest login."); 327 return; 328 } 329 end_login(); 330 } 331 332 guest = 0; 333 if (strcmp(name, "ftp") == 0 || strcmp(name, "anonymous") == 0) { 334 if (checkuser("ftp") || checkuser("anonymous")) 335 reply(530, "User %s access denied.", name); 336 else if ((pw = sgetpwnam("ftp")) != NULL) { 337 guest = 1; 338 askpasswd = 1; 339 reply(331, "Guest login ok, send ident as password."); 340 } else 341 reply(530, "User %s unknown.", name); 342 return; 343 } 344 if (pw = sgetpwnam(name)) { 345 if ((shell = pw->pw_shell) == NULL || *shell == 0) 346 shell = _PATH_BSHELL; 347 while ((cp = getusershell()) != NULL) 348 if (strcmp(cp, shell) == 0) 349 break; 350 endusershell(); 351 if (cp == NULL || checkuser(name)) { 352 reply(530, "User %s access denied.", name); 353 if (logging) 354 syslog(LOG_NOTICE, 355 "FTP LOGIN REFUSED FROM %s, %s", 356 remotehost, name); 357 pw = (struct passwd *) NULL; 358 return; 359 } 360 } 361 reply(331, "Password required for %s.", name); 362 askpasswd = 1; 363 /* 364 * Delay before reading passwd after first failed 365 * attempt to slow down passwd-guessing programs. 366 */ 367 if (login_attempts) 368 sleep((unsigned) login_attempts); 369 } 370 371 /* 372 * Check if a user is in the file _PATH_FTPUSERS 373 */ 374 checkuser(name) 375 char *name; 376 { 377 register FILE *fd; 378 register char *p; 379 char line[BUFSIZ]; 380 381 if ((fd = fopen(_PATH_FTPUSERS, "r")) != NULL) { 382 while (fgets(line, sizeof(line), fd) != NULL) 383 if ((p = index(line, '\n')) != NULL) { 384 *p = '\0'; 385 if (line[0] == '#') 386 continue; 387 if (strcmp(line, name) == 0) 388 return (1); 389 } 390 (void) fclose(fd); 391 } 392 return (0); 393 } 394 395 /* 396 * Terminate login as previous user, if any, resetting state; 397 * used when USER command is given or login fails. 398 */ 399 end_login() 400 { 401 402 (void) seteuid((uid_t)0); 403 if (logged_in) 404 logwtmp(ttyline, "", ""); 405 pw = NULL; 406 logged_in = 0; 407 guest = 0; 408 } 409 410 pass(passwd) 411 char *passwd; 412 { 413 char *xpasswd, *salt; 414 415 if (logged_in || askpasswd == 0) { 416 reply(503, "Login with USER first."); 417 return; 418 } 419 askpasswd = 0; 420 if (!guest) { /* "ftp" is only account allowed no password */ 421 if (pw == NULL) 422 salt = "xx"; 423 else 424 salt = pw->pw_passwd; 425 xpasswd = crypt(passwd, salt); 426 /* The strcmp does not catch null passwords! */ 427 if (pw == NULL || *pw->pw_passwd == '\0' || 428 strcmp(xpasswd, pw->pw_passwd)) { 429 reply(530, "Login incorrect."); 430 pw = NULL; 431 if (login_attempts++ >= 5) { 432 syslog(LOG_NOTICE, 433 "repeated login failures from %s", 434 remotehost); 435 exit(0); 436 } 437 return; 438 } 439 } 440 login_attempts = 0; /* this time successful */ 441 (void) setegid((gid_t)pw->pw_gid); 442 (void) initgroups(pw->pw_name, pw->pw_gid); 443 444 /* open wtmp before chroot */ 445 (void)sprintf(ttyline, "ftp%d", getpid()); 446 logwtmp(ttyline, pw->pw_name, remotehost); 447 logged_in = 1; 448 449 if (guest) { 450 /* 451 * We MUST do a chdir() after the chroot. Otherwise 452 * the old current directory will be accessible as "." 453 * outside the new root! 454 */ 455 if (chroot(pw->pw_dir) < 0 || chdir("/") < 0) { 456 reply(550, "Can't set guest privileges."); 457 goto bad; 458 } 459 } else if (chdir(pw->pw_dir) < 0) { 460 if (chdir("/") < 0) { 461 reply(530, "User %s: can't change directory to %s.", 462 pw->pw_name, pw->pw_dir); 463 goto bad; 464 } else 465 lreply(230, "No directory! Logging in with home=/"); 466 } 467 if (seteuid((uid_t)pw->pw_uid) < 0) { 468 reply(550, "Can't set uid."); 469 goto bad; 470 } 471 if (guest) { 472 reply(230, "Guest login ok, access restrictions apply."); 473 #ifdef SETPROCTITLE 474 sprintf(proctitle, "%s: anonymous/%.*s", remotehost, 475 sizeof(proctitle) - sizeof(remotehost) - 476 sizeof(": anonymous/"), passwd); 477 setproctitle(proctitle); 478 #endif /* SETPROCTITLE */ 479 if (logging) 480 syslog(LOG_INFO, "ANONYMOUS FTP LOGIN FROM %s, %s", 481 remotehost, passwd); 482 } else { 483 reply(230, "User %s logged in.", pw->pw_name); 484 #ifdef SETPROCTITLE 485 sprintf(proctitle, "%s: %s", remotehost, pw->pw_name); 486 setproctitle(proctitle); 487 #endif /* SETPROCTITLE */ 488 if (logging) 489 syslog(LOG_INFO, "FTP LOGIN FROM %s, %s", 490 remotehost, pw->pw_name); 491 } 492 home = pw->pw_dir; /* home dir for globbing */ 493 (void) umask(defumask); 494 return; 495 bad: 496 /* Forget all about it... */ 497 end_login(); 498 } 499 500 retrieve(cmd, name) 501 char *cmd, *name; 502 { 503 FILE *fin, *dout; 504 struct stat st; 505 int (*closefunc)(); 506 507 if (cmd == 0) { 508 fin = fopen(name, "r"), closefunc = fclose; 509 st.st_size = 0; 510 } else { 511 char line[BUFSIZ]; 512 513 (void) sprintf(line, cmd, name), name = line; 514 fin = ftpd_popen(line, "r"), closefunc = ftpd_pclose; 515 st.st_size = -1; 516 st.st_blksize = BUFSIZ; 517 } 518 if (fin == NULL) { 519 if (errno != 0) 520 perror_reply(550, name); 521 return; 522 } 523 if (cmd == 0 && 524 (fstat(fileno(fin), &st) < 0 || (st.st_mode&S_IFMT) != S_IFREG)) { 525 reply(550, "%s: not a plain file.", name); 526 goto done; 527 } 528 if (restart_point) { 529 if (type == TYPE_A) { 530 register int i, n, c; 531 532 n = restart_point; 533 i = 0; 534 while (i++ < n) { 535 if ((c=getc(fin)) == EOF) { 536 perror_reply(550, name); 537 goto done; 538 } 539 if (c == '\n') 540 i++; 541 } 542 } else if (lseek(fileno(fin), restart_point, L_SET) < 0) { 543 perror_reply(550, name); 544 goto done; 545 } 546 } 547 dout = dataconn(name, st.st_size, "w"); 548 if (dout == NULL) 549 goto done; 550 send_data(fin, dout, st.st_blksize); 551 (void) fclose(dout); 552 data = -1; 553 pdata = -1; 554 done: 555 (*closefunc)(fin); 556 } 557 558 store(name, mode, unique) 559 char *name, *mode; 560 int unique; 561 { 562 FILE *fout, *din; 563 struct stat st; 564 int (*closefunc)(); 565 char *gunique(); 566 567 if (unique && stat(name, &st) == 0 && 568 (name = gunique(name)) == NULL) 569 return; 570 571 if (restart_point) 572 mode = "r+w"; 573 fout = fopen(name, mode); 574 closefunc = fclose; 575 if (fout == NULL) { 576 perror_reply(553, name); 577 return; 578 } 579 if (restart_point) { 580 if (type == TYPE_A) { 581 register int i, n, c; 582 583 n = restart_point; 584 i = 0; 585 while (i++ < n) { 586 if ((c=getc(fout)) == EOF) { 587 perror_reply(550, name); 588 goto done; 589 } 590 if (c == '\n') 591 i++; 592 } 593 /* 594 * We must do this seek to "current" position 595 * because we are changing from reading to 596 * writing. 597 */ 598 if (fseek(fout, 0L, L_INCR) < 0) { 599 perror_reply(550, name); 600 goto done; 601 } 602 } else if (lseek(fileno(fout), restart_point, L_SET) < 0) { 603 perror_reply(550, name); 604 goto done; 605 } 606 } 607 din = dataconn(name, (off_t)-1, "r"); 608 if (din == NULL) 609 goto done; 610 if (receive_data(din, fout) == 0) { 611 if (unique) 612 reply(226, "Transfer complete (unique file name:%s).", 613 name); 614 else 615 reply(226, "Transfer complete."); 616 } 617 (void) fclose(din); 618 data = -1; 619 pdata = -1; 620 done: 621 (*closefunc)(fout); 622 } 623 624 FILE * 625 getdatasock(mode) 626 char *mode; 627 { 628 int s, on = 1, tries; 629 630 if (data >= 0) 631 return (fdopen(data, mode)); 632 s = socket(AF_INET, SOCK_STREAM, 0); 633 if (s < 0) 634 return (NULL); 635 (void) seteuid((uid_t)0); 636 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, 637 (char *) &on, sizeof (on)) < 0) 638 goto bad; 639 /* anchor socket to avoid multi-homing problems */ 640 data_source.sin_family = AF_INET; 641 data_source.sin_addr = ctrl_addr.sin_addr; 642 for (tries = 1; ; tries++) { 643 if (bind(s, (struct sockaddr *)&data_source, 644 sizeof (data_source)) >= 0) 645 break; 646 if (errno != EADDRINUSE || tries > 10) 647 goto bad; 648 sleep(tries); 649 } 650 (void) seteuid((uid_t)pw->pw_uid); 651 #ifdef IP_TOS 652 on = IPTOS_THROUGHPUT; 653 if (setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0) 654 syslog(LOG_WARNING, "setsockopt (IP_TOS): %m"); 655 #endif 656 return (fdopen(s, mode)); 657 bad: 658 (void) seteuid((uid_t)pw->pw_uid); 659 (void) close(s); 660 return (NULL); 661 } 662 663 FILE * 664 dataconn(name, size, mode) 665 char *name; 666 off_t size; 667 char *mode; 668 { 669 char sizebuf[32]; 670 FILE *file; 671 int retry = 0, tos; 672 673 file_size = size; 674 byte_count = 0; 675 if (size != (off_t) -1) 676 (void) sprintf (sizebuf, " (%ld bytes)", size); 677 else 678 (void) strcpy(sizebuf, ""); 679 if (pdata >= 0) { 680 struct sockaddr_in from; 681 int s, fromlen = sizeof(from); 682 683 s = accept(pdata, (struct sockaddr *)&from, &fromlen); 684 if (s < 0) { 685 reply(425, "Can't open data connection."); 686 (void) close(pdata); 687 pdata = -1; 688 return(NULL); 689 } 690 (void) close(pdata); 691 pdata = s; 692 #ifdef IP_TOS 693 tos = IPTOS_LOWDELAY; 694 (void) setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&tos, 695 sizeof(int)); 696 #endif 697 reply(150, "Opening %s mode data connection for %s%s.", 698 type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf); 699 return(fdopen(pdata, mode)); 700 } 701 if (data >= 0) { 702 reply(125, "Using existing data connection for %s%s.", 703 name, sizebuf); 704 usedefault = 1; 705 return (fdopen(data, mode)); 706 } 707 if (usedefault) 708 data_dest = his_addr; 709 usedefault = 1; 710 file = getdatasock(mode); 711 if (file == NULL) { 712 reply(425, "Can't create data socket (%s,%d): %s.", 713 inet_ntoa(data_source.sin_addr), 714 ntohs(data_source.sin_port), strerror(errno)); 715 return (NULL); 716 } 717 data = fileno(file); 718 while (connect(data, (struct sockaddr *)&data_dest, 719 sizeof (data_dest)) < 0) { 720 if (errno == EADDRINUSE && retry < swaitmax) { 721 sleep((unsigned) swaitint); 722 retry += swaitint; 723 continue; 724 } 725 perror_reply(425, "Can't build data connection"); 726 (void) fclose(file); 727 data = -1; 728 return (NULL); 729 } 730 reply(150, "Opening %s mode data connection for %s%s.", 731 type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf); 732 return (file); 733 } 734 735 /* 736 * Tranfer the contents of "instr" to 737 * "outstr" peer using the appropriate 738 * encapsulation of the data subject 739 * to Mode, Structure, and Type. 740 * 741 * NB: Form isn't handled. 742 */ 743 send_data(instr, outstr, blksize) 744 FILE *instr, *outstr; 745 off_t blksize; 746 { 747 register int c, cnt; 748 register char *buf; 749 int netfd, filefd; 750 751 transflag++; 752 if (setjmp(urgcatch)) { 753 transflag = 0; 754 return; 755 } 756 switch (type) { 757 758 case TYPE_A: 759 while ((c = getc(instr)) != EOF) { 760 byte_count++; 761 if (c == '\n') { 762 if (ferror(outstr)) 763 goto data_err; 764 (void) putc('\r', outstr); 765 } 766 (void) putc(c, outstr); 767 } 768 fflush(outstr); 769 transflag = 0; 770 if (ferror(instr)) 771 goto file_err; 772 if (ferror(outstr)) 773 goto data_err; 774 reply(226, "Transfer complete."); 775 return; 776 777 case TYPE_I: 778 case TYPE_L: 779 if ((buf = malloc((u_int)blksize)) == NULL) { 780 transflag = 0; 781 perror_reply(451, "Local resource failure: malloc"); 782 return; 783 } 784 netfd = fileno(outstr); 785 filefd = fileno(instr); 786 while ((cnt = read(filefd, buf, (u_int)blksize)) > 0 && 787 write(netfd, buf, cnt) == cnt) 788 byte_count += cnt; 789 transflag = 0; 790 (void)free(buf); 791 if (cnt != 0) { 792 if (cnt < 0) 793 goto file_err; 794 goto data_err; 795 } 796 reply(226, "Transfer complete."); 797 return; 798 default: 799 transflag = 0; 800 reply(550, "Unimplemented TYPE %d in send_data", type); 801 return; 802 } 803 804 data_err: 805 transflag = 0; 806 perror_reply(426, "Data connection"); 807 return; 808 809 file_err: 810 transflag = 0; 811 perror_reply(551, "Error on input file"); 812 } 813 814 /* 815 * Transfer data from peer to 816 * "outstr" using the appropriate 817 * encapulation of the data subject 818 * to Mode, Structure, and Type. 819 * 820 * N.B.: Form isn't handled. 821 */ 822 receive_data(instr, outstr) 823 FILE *instr, *outstr; 824 { 825 register int c; 826 int cnt, bare_lfs = 0; 827 char buf[BUFSIZ]; 828 829 transflag++; 830 if (setjmp(urgcatch)) { 831 transflag = 0; 832 return (-1); 833 } 834 switch (type) { 835 836 case TYPE_I: 837 case TYPE_L: 838 while ((cnt = read(fileno(instr), buf, sizeof buf)) > 0) { 839 if (write(fileno(outstr), buf, cnt) != cnt) 840 goto file_err; 841 byte_count += cnt; 842 } 843 if (cnt < 0) 844 goto data_err; 845 transflag = 0; 846 return (0); 847 848 case TYPE_E: 849 reply(553, "TYPE E not implemented."); 850 transflag = 0; 851 return (-1); 852 853 case TYPE_A: 854 while ((c = getc(instr)) != EOF) { 855 byte_count++; 856 if (c == '\n') 857 bare_lfs++; 858 while (c == '\r') { 859 if (ferror(outstr)) 860 goto data_err; 861 if ((c = getc(instr)) != '\n') { 862 (void) putc ('\r', outstr); 863 if (c == '\0' || c == EOF) 864 goto contin2; 865 } 866 } 867 (void) putc(c, outstr); 868 contin2: ; 869 } 870 fflush(outstr); 871 if (ferror(instr)) 872 goto data_err; 873 if (ferror(outstr)) 874 goto file_err; 875 transflag = 0; 876 if (bare_lfs) { 877 lreply(230, "WARNING! %d bare linefeeds received in ASCII mode", bare_lfs); 878 printf(" File may not have transferred correctly.\r\n"); 879 } 880 return (0); 881 default: 882 reply(550, "Unimplemented TYPE %d in receive_data", type); 883 transflag = 0; 884 return (-1); 885 } 886 887 data_err: 888 transflag = 0; 889 perror_reply(426, "Data Connection"); 890 return (-1); 891 892 file_err: 893 transflag = 0; 894 perror_reply(452, "Error writing file"); 895 return (-1); 896 } 897 898 statfilecmd(filename) 899 char *filename; 900 { 901 char line[BUFSIZ]; 902 FILE *fin; 903 int c; 904 905 (void) sprintf(line, "/bin/ls -lgA %s", filename); 906 fin = ftpd_popen(line, "r"); 907 lreply(211, "status of %s:", filename); 908 while ((c = getc(fin)) != EOF) { 909 if (c == '\n') { 910 if (ferror(stdout)){ 911 perror_reply(421, "control connection"); 912 (void) ftpd_pclose(fin); 913 dologout(1); 914 /* NOTREACHED */ 915 } 916 if (ferror(fin)) { 917 perror_reply(551, filename); 918 (void) ftpd_pclose(fin); 919 return; 920 } 921 (void) putc('\r', stdout); 922 } 923 (void) putc(c, stdout); 924 } 925 (void) ftpd_pclose(fin); 926 reply(211, "End of Status"); 927 } 928 929 statcmd() 930 { 931 struct sockaddr_in *sin; 932 u_char *a, *p; 933 934 lreply(211, "%s FTP server status:", hostname, version); 935 printf(" %s\r\n", version); 936 printf(" Connected to %s", remotehost); 937 if (!isdigit(remotehost[0])) 938 printf(" (%s)", inet_ntoa(his_addr.sin_addr)); 939 printf("\r\n"); 940 if (logged_in) { 941 if (guest) 942 printf(" Logged in anonymously\r\n"); 943 else 944 printf(" Logged in as %s\r\n", pw->pw_name); 945 } else if (askpasswd) 946 printf(" Waiting for password\r\n"); 947 else 948 printf(" Waiting for user name\r\n"); 949 printf(" TYPE: %s", typenames[type]); 950 if (type == TYPE_A || type == TYPE_E) 951 printf(", FORM: %s", formnames[form]); 952 if (type == TYPE_L) 953 #if NBBY == 8 954 printf(" %d", NBBY); 955 #else 956 printf(" %d", bytesize); /* need definition! */ 957 #endif 958 printf("; STRUcture: %s; transfer MODE: %s\r\n", 959 strunames[stru], modenames[mode]); 960 if (data != -1) 961 printf(" Data connection open\r\n"); 962 else if (pdata != -1) { 963 printf(" in Passive mode"); 964 sin = &pasv_addr; 965 goto printaddr; 966 } else if (usedefault == 0) { 967 printf(" PORT"); 968 sin = &data_dest; 969 printaddr: 970 a = (u_char *) &sin->sin_addr; 971 p = (u_char *) &sin->sin_port; 972 #define UC(b) (((int) b) & 0xff) 973 printf(" (%d,%d,%d,%d,%d,%d)\r\n", UC(a[0]), 974 UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1])); 975 #undef UC 976 } else 977 printf(" No data connection\r\n"); 978 reply(211, "End of status"); 979 } 980 981 fatal(s) 982 char *s; 983 { 984 reply(451, "Error in server: %s\n", s); 985 reply(221, "Closing connection due to server error."); 986 dologout(0); 987 /* NOTREACHED */ 988 } 989 990 /* VARARGS2 */ 991 reply(n, fmt, p0, p1, p2, p3, p4, p5) 992 int n; 993 char *fmt; 994 { 995 printf("%d ", n); 996 printf(fmt, p0, p1, p2, p3, p4, p5); 997 printf("\r\n"); 998 (void)fflush(stdout); 999 if (debug) { 1000 syslog(LOG_DEBUG, "<--- %d ", n); 1001 syslog(LOG_DEBUG, fmt, p0, p1, p2, p3, p4, p5); 1002 } 1003 } 1004 1005 /* VARARGS2 */ 1006 lreply(n, fmt, p0, p1, p2, p3, p4, p5) 1007 int n; 1008 char *fmt; 1009 { 1010 printf("%d- ", n); 1011 printf(fmt, p0, p1, p2, p3, p4, p5); 1012 printf("\r\n"); 1013 (void)fflush(stdout); 1014 if (debug) { 1015 syslog(LOG_DEBUG, "<--- %d- ", n); 1016 syslog(LOG_DEBUG, fmt, p0, p1, p2, p3, p4, p5); 1017 } 1018 } 1019 1020 ack(s) 1021 char *s; 1022 { 1023 reply(250, "%s command successful.", s); 1024 } 1025 1026 nack(s) 1027 char *s; 1028 { 1029 reply(502, "%s command not implemented.", s); 1030 } 1031 1032 /* ARGSUSED */ 1033 yyerror(s) 1034 char *s; 1035 { 1036 char *cp; 1037 1038 if (cp = index(cbuf,'\n')) 1039 *cp = '\0'; 1040 reply(500, "'%s': command not understood.", cbuf); 1041 } 1042 1043 delete(name) 1044 char *name; 1045 { 1046 struct stat st; 1047 1048 if (stat(name, &st) < 0) { 1049 perror_reply(550, name); 1050 return; 1051 } 1052 if ((st.st_mode&S_IFMT) == S_IFDIR) { 1053 if (rmdir(name) < 0) { 1054 perror_reply(550, name); 1055 return; 1056 } 1057 goto done; 1058 } 1059 if (unlink(name) < 0) { 1060 perror_reply(550, name); 1061 return; 1062 } 1063 done: 1064 ack("DELE"); 1065 } 1066 1067 cwd(path) 1068 char *path; 1069 { 1070 if (chdir(path) < 0) 1071 perror_reply(550, path); 1072 else 1073 ack("CWD"); 1074 } 1075 1076 makedir(name) 1077 char *name; 1078 { 1079 if (mkdir(name, 0777) < 0) 1080 perror_reply(550, name); 1081 else 1082 reply(257, "MKD command successful."); 1083 } 1084 1085 removedir(name) 1086 char *name; 1087 { 1088 if (rmdir(name) < 0) 1089 perror_reply(550, name); 1090 else 1091 ack("RMD"); 1092 } 1093 1094 pwd() 1095 { 1096 char path[MAXPATHLEN + 1]; 1097 extern char *getwd(); 1098 1099 if (getwd(path) == (char *)NULL) 1100 reply(550, "%s.", path); 1101 else 1102 reply(257, "\"%s\" is current directory.", path); 1103 } 1104 1105 char * 1106 renamefrom(name) 1107 char *name; 1108 { 1109 struct stat st; 1110 1111 if (stat(name, &st) < 0) { 1112 perror_reply(550, name); 1113 return ((char *)0); 1114 } 1115 reply(350, "File exists, ready for destination name"); 1116 return (name); 1117 } 1118 1119 renamecmd(from, to) 1120 char *from, *to; 1121 { 1122 if (rename(from, to) < 0) 1123 perror_reply(550, "rename"); 1124 else 1125 ack("RNTO"); 1126 } 1127 1128 dolog(sin) 1129 struct sockaddr_in *sin; 1130 { 1131 struct hostent *hp = gethostbyaddr((char *)&sin->sin_addr, 1132 sizeof (struct in_addr), AF_INET); 1133 time_t t, time(); 1134 extern char *ctime(); 1135 1136 if (hp) 1137 (void) strncpy(remotehost, hp->h_name, sizeof (remotehost)); 1138 else 1139 (void) strncpy(remotehost, inet_ntoa(sin->sin_addr), 1140 sizeof (remotehost)); 1141 #ifdef SETPROCTITLE 1142 sprintf(proctitle, "%s: connected", remotehost); 1143 setproctitle(proctitle); 1144 #endif /* SETPROCTITLE */ 1145 1146 if (logging) { 1147 t = time((time_t *) 0); 1148 syslog(LOG_INFO, "connection from %s at %s", 1149 remotehost, ctime(&t)); 1150 } 1151 } 1152 1153 /* 1154 * Record logout in wtmp file 1155 * and exit with supplied status. 1156 */ 1157 dologout(status) 1158 int status; 1159 { 1160 if (logged_in) { 1161 (void) seteuid((uid_t)0); 1162 logwtmp(ttyline, "", ""); 1163 } 1164 /* beware of flushing buffers after a SIGPIPE */ 1165 _exit(status); 1166 } 1167 1168 void 1169 myoob() 1170 { 1171 char *cp; 1172 1173 /* only process if transfer occurring */ 1174 if (!transflag) 1175 return; 1176 cp = tmpline; 1177 if (getline(cp, 7, stdin) == NULL) { 1178 reply(221, "You could at least say goodbye."); 1179 dologout(0); 1180 } 1181 upper(cp); 1182 if (strcmp(cp, "ABOR\r\n") == 0) { 1183 tmpline[0] = '\0'; 1184 reply(426, "Transfer aborted. Data connection closed."); 1185 reply(226, "Abort successful"); 1186 longjmp(urgcatch, 1); 1187 } 1188 if (strcmp(cp, "STAT\r\n") == 0) { 1189 if (file_size != (off_t) -1) 1190 reply(213, "Status: %lu of %lu bytes transferred", 1191 byte_count, file_size); 1192 else 1193 reply(213, "Status: %lu bytes transferred", byte_count); 1194 } 1195 } 1196 1197 /* 1198 * Note: a response of 425 is not mentioned as a possible response to 1199 * the PASV command in RFC959. However, it has been blessed as 1200 * a legitimate response by Jon Postel in a telephone conversation 1201 * with Rick Adams on 25 Jan 89. 1202 */ 1203 passive() 1204 { 1205 int len; 1206 register char *p, *a; 1207 1208 pdata = socket(AF_INET, SOCK_STREAM, 0); 1209 if (pdata < 0) { 1210 perror_reply(425, "Can't open passive connection"); 1211 return; 1212 } 1213 pasv_addr = ctrl_addr; 1214 pasv_addr.sin_port = 0; 1215 (void) seteuid((uid_t)0); 1216 if (bind(pdata, (struct sockaddr *)&pasv_addr, sizeof(pasv_addr)) < 0) { 1217 (void) seteuid((uid_t)pw->pw_uid); 1218 goto pasv_error; 1219 } 1220 (void) seteuid((uid_t)pw->pw_uid); 1221 len = sizeof(pasv_addr); 1222 if (getsockname(pdata, (struct sockaddr *) &pasv_addr, &len) < 0) 1223 goto pasv_error; 1224 if (listen(pdata, 1) < 0) 1225 goto pasv_error; 1226 a = (char *) &pasv_addr.sin_addr; 1227 p = (char *) &pasv_addr.sin_port; 1228 1229 #define UC(b) (((int) b) & 0xff) 1230 1231 reply(227, "Entering Passive Mode (%d,%d,%d,%d,%d,%d)", UC(a[0]), 1232 UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1])); 1233 return; 1234 1235 pasv_error: 1236 (void) close(pdata); 1237 pdata = -1; 1238 perror_reply(425, "Can't open passive connection"); 1239 return; 1240 } 1241 1242 /* 1243 * Generate unique name for file with basename "local". 1244 * The file named "local" is already known to exist. 1245 * Generates failure reply on error. 1246 */ 1247 char * 1248 gunique(local) 1249 char *local; 1250 { 1251 static char new[MAXPATHLEN]; 1252 struct stat st; 1253 char *cp = rindex(local, '/'); 1254 int count = 0; 1255 1256 if (cp) 1257 *cp = '\0'; 1258 if (stat(cp ? local : ".", &st) < 0) { 1259 perror_reply(553, cp ? local : "."); 1260 return((char *) 0); 1261 } 1262 if (cp) 1263 *cp = '/'; 1264 (void) strcpy(new, local); 1265 cp = new + strlen(new); 1266 *cp++ = '.'; 1267 for (count = 1; count < 100; count++) { 1268 (void) sprintf(cp, "%d", count); 1269 if (stat(new, &st) < 0) 1270 return(new); 1271 } 1272 reply(452, "Unique file name cannot be created."); 1273 return((char *) 0); 1274 } 1275 1276 /* 1277 * Format and send reply containing system error number. 1278 */ 1279 perror_reply(code, string) 1280 int code; 1281 char *string; 1282 { 1283 reply(code, "%s: %s.", string, strerror(errno)); 1284 } 1285 1286 static char *onefile[] = { 1287 "", 1288 0 1289 }; 1290 1291 send_file_list(whichfiles) 1292 char *whichfiles; 1293 { 1294 struct stat st; 1295 DIR *dirp = NULL; 1296 struct dirent *dir; 1297 FILE *dout = NULL; 1298 register char **dirlist, *dirname; 1299 int simple = 0; 1300 char *strpbrk(); 1301 1302 if (strpbrk(whichfiles, "~{[*?") != NULL) { 1303 extern char **ftpglob(), *globerr; 1304 1305 globerr = NULL; 1306 dirlist = ftpglob(whichfiles); 1307 if (globerr != NULL) { 1308 reply(550, globerr); 1309 return; 1310 } else if (dirlist == NULL) { 1311 errno = ENOENT; 1312 perror_reply(550, whichfiles); 1313 return; 1314 } 1315 } else { 1316 onefile[0] = whichfiles; 1317 dirlist = onefile; 1318 simple = 1; 1319 } 1320 1321 if (setjmp(urgcatch)) { 1322 transflag = 0; 1323 return; 1324 } 1325 while (dirname = *dirlist++) { 1326 if (stat(dirname, &st) < 0) { 1327 /* 1328 * If user typed "ls -l", etc, and the client 1329 * used NLST, do what the user meant. 1330 */ 1331 if (dirname[0] == '-' && *dirlist == NULL && 1332 transflag == 0) { 1333 retrieve("/bin/ls %s", dirname); 1334 return; 1335 } 1336 perror_reply(550, whichfiles); 1337 if (dout != NULL) { 1338 (void) fclose(dout); 1339 transflag = 0; 1340 data = -1; 1341 pdata = -1; 1342 } 1343 return; 1344 } 1345 1346 if ((st.st_mode&S_IFMT) == S_IFREG) { 1347 if (dout == NULL) { 1348 dout = dataconn("file list", (off_t)-1, "w"); 1349 if (dout == NULL) 1350 return; 1351 transflag++; 1352 } 1353 fprintf(dout, "%s%s\n", dirname, 1354 type == TYPE_A ? "\r" : ""); 1355 byte_count += strlen(dirname) + 1; 1356 continue; 1357 } else if ((st.st_mode&S_IFMT) != S_IFDIR) 1358 continue; 1359 1360 if ((dirp = opendir(dirname)) == NULL) 1361 continue; 1362 1363 while ((dir = readdir(dirp)) != NULL) { 1364 char nbuf[MAXPATHLEN]; 1365 1366 if (dir->d_name[0] == '.' && dir->d_namlen == 1) 1367 continue; 1368 if (dir->d_name[0] == '.' && dir->d_name[1] == '.' && 1369 dir->d_namlen == 2) 1370 continue; 1371 1372 sprintf(nbuf, "%s/%s", dirname, dir->d_name); 1373 1374 /* 1375 * We have to do a stat to insure it's 1376 * not a directory or special file. 1377 */ 1378 if (simple || (stat(nbuf, &st) == 0 && 1379 (st.st_mode&S_IFMT) == S_IFREG)) { 1380 if (dout == NULL) { 1381 dout = dataconn("file list", (off_t)-1, 1382 "w"); 1383 if (dout == NULL) 1384 return; 1385 transflag++; 1386 } 1387 if (nbuf[0] == '.' && nbuf[1] == '/') 1388 fprintf(dout, "%s%s\n", &nbuf[2], 1389 type == TYPE_A ? "\r" : ""); 1390 else 1391 fprintf(dout, "%s%s\n", nbuf, 1392 type == TYPE_A ? "\r" : ""); 1393 byte_count += strlen(nbuf) + 1; 1394 } 1395 } 1396 (void) closedir(dirp); 1397 } 1398 1399 if (dout == NULL) 1400 reply(550, "No files found."); 1401 else if (ferror(dout) != 0) 1402 perror_reply(550, "Data connection"); 1403 else 1404 reply(226, "Transfer complete."); 1405 1406 transflag = 0; 1407 if (dout != NULL) 1408 (void) fclose(dout); 1409 data = -1; 1410 pdata = -1; 1411 } 1412 1413 #ifdef SETPROCTITLE 1414 /* 1415 * clobber argv so ps will show what we're doing. 1416 * (stolen from sendmail) 1417 * warning, since this is usually started from inetd.conf, it 1418 * often doesn't have much of an environment or arglist to overwrite. 1419 */ 1420 1421 /*VARARGS1*/ 1422 setproctitle(fmt, a, b, c) 1423 char *fmt; 1424 { 1425 register char *p, *bp, ch; 1426 register int i; 1427 char buf[BUFSIZ]; 1428 1429 (void) sprintf(buf, fmt, a, b, c); 1430 1431 /* make ps print our process name */ 1432 p = Argv[0]; 1433 *p++ = '-'; 1434 1435 i = strlen(buf); 1436 if (i > LastArgv - p - 2) { 1437 i = LastArgv - p - 2; 1438 buf[i] = '\0'; 1439 } 1440 bp = buf; 1441 while (ch = *bp++) 1442 if (ch != '\n' && ch != '\r') 1443 *p++ = ch; 1444 while (p < LastArgv) 1445 *p++ = ' '; 1446 } 1447 #endif /* SETPROCTITLE */ 1448