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