ftpd.c revision 18989
1/* 2 * Copyright (c) 1985, 1988, 1990, 1992, 1993, 1994 3 * The Regents of the University of California. 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 * $Id: ftpd.c,v 1.23 1996/08/09 22:22:30 julian Exp $ 34 */ 35 36#if 0 37#ifndef lint 38static char copyright[] = 39"@(#) Copyright (c) 1985, 1988, 1990, 1992, 1993, 1994\n\ 40 The Regents of the University of California. All rights reserved.\n"; 41#endif /* not lint */ 42#endif 43 44#if 0 45#ifndef lint 46static char sccsid[] = "@(#)ftpd.c 8.4 (Berkeley) 4/16/94"; 47#endif /* not lint */ 48#endif 49 50/* 51 * FTP server. 52 */ 53#include <sys/param.h> 54#include <sys/stat.h> 55#include <sys/ioctl.h> 56#include <sys/socket.h> 57#include <sys/wait.h> 58#include <sys/mman.h> 59 60#include <netinet/in.h> 61#include <netinet/in_systm.h> 62#include <netinet/ip.h> 63#include <netinet/tcp.h> 64 65#define FTP_NAMES 66#include <arpa/ftp.h> 67#include <arpa/inet.h> 68#include <arpa/telnet.h> 69 70#include <ctype.h> 71#include <dirent.h> 72#include <err.h> 73#include <errno.h> 74#include <fcntl.h> 75#include <glob.h> 76#include <limits.h> 77#include <netdb.h> 78#include <pwd.h> 79#include <setjmp.h> 80#include <signal.h> 81#include <stdio.h> 82#include <stdlib.h> 83#include <string.h> 84#include <syslog.h> 85#include <time.h> 86#include <unistd.h> 87#include <libutil.h> 88 89#ifdef SKEY 90#include <skey.h> 91#endif 92 93#include "pathnames.h" 94#include "extern.h" 95 96#if __STDC__ 97#include <stdarg.h> 98#else 99#include <varargs.h> 100#endif 101 102static char version[] = "Version 6.00"; 103 104extern off_t restart_point; 105extern char cbuf[]; 106 107struct sockaddr_in server_addr; 108struct sockaddr_in ctrl_addr; 109struct sockaddr_in data_source; 110struct sockaddr_in data_dest; 111struct sockaddr_in his_addr; 112struct sockaddr_in pasv_addr; 113 114int daemon_mode; 115int data; 116jmp_buf errcatch, urgcatch; 117int logged_in; 118struct passwd *pw; 119int debug; 120int timeout = 900; /* timeout after 15 minutes of inactivity */ 121int maxtimeout = 7200;/* don't allow idle time to be set beyond 2 hours */ 122int logging; 123int restricted_data_ports = 1; 124int paranoid = 1; /* be extra careful about security */ 125int guest; 126int dochroot; 127int stats; 128int statfd = -1; 129int type; 130int form; 131int stru; /* avoid C keyword */ 132int mode; 133int usedefault = 1; /* for data transfers */ 134int pdata = -1; /* for passive mode */ 135sig_atomic_t transflag; 136off_t file_size; 137off_t byte_count; 138#if !defined(CMASK) || CMASK == 0 139#undef CMASK 140#define CMASK 027 141#endif 142int defumask = CMASK; /* default umask value */ 143char tmpline[7]; 144char hostname[MAXHOSTNAMELEN]; 145char remotehost[MAXHOSTNAMELEN]; 146char *ident = NULL; 147 148static char ttyline[20]; 149char *tty = ttyline; /* for klogin */ 150 151#ifdef KERBEROS 152int klogin __P((struct passwd *, char *, char *, char *)); 153#endif 154 155struct in_addr bind_address; 156char *pid_file = NULL; 157 158#if defined(KERBEROS) 159int notickets = 1; 160int noticketsdontcomplain = 1; 161char *krbtkfile_env = NULL; 162#endif 163 164/* 165 * Timeout intervals for retrying connections 166 * to hosts that don't accept PORT cmds. This 167 * is a kludge, but given the problems with TCP... 168 */ 169#define SWAITMAX 90 /* wait at most 90 seconds */ 170#define SWAITINT 5 /* interval between retries */ 171 172int swaitmax = SWAITMAX; 173int swaitint = SWAITINT; 174 175#ifdef SETPROCTITLE 176#ifdef OLD_SETPROCTITLE 177char **Argv = NULL; /* pointer to argument vector */ 178char *LastArgv = NULL; /* end of argv */ 179#endif /* OLD_SETPROCTITLE */ 180char proctitle[LINE_MAX]; /* initial part of title */ 181#endif /* SETPROCTITLE */ 182 183#ifdef SKEY 184int pwok = 0; 185int sflag; 186char addr_string[20]; /* XXX */ 187#endif 188 189#define LOGCMD(cmd, file) \ 190 if (logging > 1) \ 191 syslog(LOG_INFO,"%s %s%s", cmd, \ 192 *(file) == '/' ? "" : curdir(), file); 193#define LOGCMD2(cmd, file1, file2) \ 194 if (logging > 1) \ 195 syslog(LOG_INFO,"%s %s%s %s%s", cmd, \ 196 *(file1) == '/' ? "" : curdir(), file1, \ 197 *(file2) == '/' ? "" : curdir(), file2); 198#define LOGBYTES(cmd, file, cnt) \ 199 if (logging > 1) { \ 200 if (cnt == (off_t)-1) \ 201 syslog(LOG_INFO,"%s %s%s", cmd, \ 202 *(file) == '/' ? "" : curdir(), file); \ 203 else \ 204 syslog(LOG_INFO, "%s %s%s = %qd bytes", \ 205 cmd, (*(file) == '/') ? "" : curdir(), file, cnt); \ 206 } 207 208static void ack __P((char *)); 209static void myoob __P((int)); 210static int checkuser __P((char *, char *)); 211static FILE *dataconn __P((char *, off_t, char *)); 212static void dolog __P((struct sockaddr_in *)); 213static char *curdir __P((void)); 214static void end_login __P((void)); 215static FILE *getdatasock __P((char *)); 216static char *gunique __P((char *)); 217static void lostconn __P((int)); 218static int receive_data __P((FILE *, FILE *)); 219static void send_data __P((FILE *, FILE *, off_t, off_t, int)); 220static struct passwd * 221 sgetpwnam __P((char *)); 222static char *sgetsave __P((char *)); 223static void reapchild __P((int)); 224static void logxfer __P((char *, long, long)); 225 226static char * 227curdir() 228{ 229 static char path[MAXPATHLEN+1+1]; /* path + '/' + '\0' */ 230 231 if (getcwd(path, sizeof(path)-2) == NULL) 232 return (""); 233 if (path[1] != '\0') /* special case for root dir. */ 234 strcat(path, "/"); 235 /* For guest account, skip / since it's chrooted */ 236 return (guest ? path+1 : path); 237} 238 239int 240main(argc, argv, envp) 241 int argc; 242 char *argv[]; 243 char **envp; 244{ 245 int addrlen, ch, on = 1, tos; 246 char *cp, line[LINE_MAX]; 247 FILE *fd; 248 249 tzset(); /* in case no timezone database in ~ftp */ 250 251#ifdef OLD_SETPROCTITLE 252 /* 253 * Save start and extent of argv for setproctitle. 254 */ 255 Argv = argv; 256 while (*envp) 257 envp++; 258 LastArgv = envp[-1] + strlen(envp[-1]); 259#endif /* OLD_SETPROCTITLE */ 260 261 262 bind_address.s_addr = htonl(INADDR_ANY); 263 while ((ch = getopt(argc, argv, "dlDSUt:T:u:va:p:")) != EOF) { 264 switch (ch) { 265 case 'D': 266 daemon_mode++; 267 break; 268 269 case 'd': 270 debug++; 271 break; 272 273 case 'l': 274 logging++; /* > 1 == extra logging */ 275 break; 276 277 case 'R': 278 paranoid = 0; 279 break; 280 281 case 'S': 282 stats++; 283 break; 284 285 case 'T': 286 maxtimeout = atoi(optarg); 287 if (timeout > maxtimeout) 288 timeout = maxtimeout; 289 break; 290 291 case 't': 292 timeout = atoi(optarg); 293 if (maxtimeout < timeout) 294 maxtimeout = timeout; 295 break; 296 297 case 'U': 298 restricted_data_ports = 0; 299 break; 300 301 case 'a': 302 if (!inet_aton(optarg, &bind_address)) 303 errx(1, "invalid address for -a"); 304 break; 305 306 case 'p': 307 pid_file = optarg; 308 break; 309 310 case 'u': 311 { 312 long val = 0; 313 314 val = strtol(optarg, &optarg, 8); 315 if (*optarg != '\0' || val < 0) 316 warnx("bad value for -u"); 317 else 318 defumask = val; 319 break; 320 } 321 322 case 'v': 323 debug = 1; 324 break; 325 326 default: 327 warnx("unknown flag -%c ignored", optopt); 328 break; 329 } 330 } 331 332 (void) freopen(_PATH_DEVNULL, "w", stderr); 333 334 /* 335 * LOG_NDELAY sets up the logging connection immediately, 336 * necessary for anonymous ftp's that chroot and can't do it later. 337 */ 338 openlog("ftpd", LOG_PID | LOG_NDELAY, LOG_FTP); 339 340 if (daemon_mode) { 341 int ctl_sock, fd; 342 struct servent *sv; 343 344 /* 345 * Detach from parent. 346 */ 347 if (daemon(1, 1) < 0) { 348 syslog(LOG_ERR, "failed to become a daemon"); 349 exit(1); 350 } 351 (void) signal(SIGCHLD, reapchild); 352 /* 353 * Get port number for ftp/tcp. 354 */ 355 sv = getservbyname("ftp", "tcp"); 356 if (sv == NULL) { 357 syslog(LOG_ERR, "getservbyname for ftp failed"); 358 exit(1); 359 } 360 /* 361 * Open a socket, bind it to the FTP port, and start 362 * listening. 363 */ 364 ctl_sock = socket(AF_INET, SOCK_STREAM, 0); 365 if (ctl_sock < 0) { 366 syslog(LOG_ERR, "control socket: %m"); 367 exit(1); 368 } 369 if (setsockopt(ctl_sock, SOL_SOCKET, SO_REUSEADDR, 370 (char *)&on, sizeof(on)) < 0) 371 syslog(LOG_ERR, "control setsockopt: %m");; 372 server_addr.sin_family = AF_INET; 373 server_addr.sin_addr = bind_address; 374 server_addr.sin_port = sv->s_port; 375 if (bind(ctl_sock, (struct sockaddr *)&server_addr, sizeof(server_addr))) { 376 syslog(LOG_ERR, "control bind: %m"); 377 exit(1); 378 } 379 if (listen(ctl_sock, 32) < 0) { 380 syslog(LOG_ERR, "control listen: %m"); 381 exit(1); 382 } 383 /* 384 * Atomically write process ID 385 */ 386 if (pid_file) 387 { 388 int fd; 389 char buf[20]; 390 391 fd = open(pid_file, O_CREAT | O_WRONLY | O_TRUNC 392 | O_NONBLOCK | O_EXLOCK, 0644); 393 if (fd < 0) 394 if (errno == EAGAIN) 395 errx(1, "%s: file locked", pid_file); 396 else 397 err(1, "%s", pid_file); 398 snprintf(buf, sizeof(buf), 399 "%lu\n", (unsigned long) getpid()); 400 if (write(fd, buf, strlen(buf)) < 0) 401 err(1, "%s: write", pid_file); 402 /* Leave the pid file open and locked */ 403 } 404 /* 405 * Loop forever accepting connection requests and forking off 406 * children to handle them. 407 */ 408 while (1) { 409 addrlen = sizeof(his_addr); 410 fd = accept(ctl_sock, (struct sockaddr *)&his_addr, &addrlen); 411 if (fork() == 0) { 412 /* child */ 413 (void) dup2(fd, 0); 414 (void) dup2(fd, 1); 415 close(ctl_sock); 416 break; 417 } 418 close(fd); 419 } 420 } else { 421 addrlen = sizeof(his_addr); 422 if (getpeername(0, (struct sockaddr *)&his_addr, &addrlen) < 0) { 423 syslog(LOG_ERR, "getpeername (%s): %m",argv[0]); 424 exit(1); 425 } 426 } 427 428 (void) signal(SIGCHLD, SIG_IGN); 429 (void) signal(SIGPIPE, lostconn); 430 if ((int)signal(SIGURG, myoob) < 0) 431 syslog(LOG_ERR, "signal: %m"); 432 433#ifdef SKEY 434 strncpy(addr_string, inet_ntoa(his_addr.sin_addr), sizeof(addr_string)); 435#endif 436 addrlen = sizeof(ctrl_addr); 437 if (getsockname(0, (struct sockaddr *)&ctrl_addr, &addrlen) < 0) { 438 syslog(LOG_ERR, "getsockname (%s): %m",argv[0]); 439 exit(1); 440 } 441#ifdef IP_TOS 442 tos = IPTOS_LOWDELAY; 443 if (setsockopt(0, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(int)) < 0) 444 syslog(LOG_WARNING, "setsockopt (IP_TOS): %m"); 445#endif 446 data_source.sin_port = htons(ntohs(ctrl_addr.sin_port) - 1); 447 448 /* set this here so klogin can use it... */ 449 (void)sprintf(ttyline, "ftp%d", getpid()); 450 451 /* Try to handle urgent data inline */ 452#ifdef SO_OOBINLINE 453 if (setsockopt(0, SOL_SOCKET, SO_OOBINLINE, (char *)&on, sizeof(on)) < 0) 454 syslog(LOG_ERR, "setsockopt: %m"); 455#endif 456 457#ifdef F_SETOWN 458 if (fcntl(fileno(stdin), F_SETOWN, getpid()) == -1) 459 syslog(LOG_ERR, "fcntl F_SETOWN: %m"); 460#endif 461 dolog(&his_addr); 462 /* 463 * Set up default state 464 */ 465 data = -1; 466 type = TYPE_A; 467 form = FORM_N; 468 stru = STRU_F; 469 mode = MODE_S; 470 tmpline[0] = '\0'; 471 472 /* If logins are disabled, print out the message. */ 473 if ((fd = fopen(_PATH_NOLOGIN,"r")) != NULL) { 474 while (fgets(line, sizeof(line), fd) != NULL) { 475 if ((cp = strchr(line, '\n')) != NULL) 476 *cp = '\0'; 477 lreply(530, "%s", line); 478 } 479 (void) fflush(stdout); 480 (void) fclose(fd); 481 reply(530, "System not available."); 482 exit(0); 483 } 484 if ((fd = fopen(_PATH_FTPWELCOME, "r")) != NULL) { 485 while (fgets(line, sizeof(line), fd) != NULL) { 486 if ((cp = strchr(line, '\n')) != NULL) 487 *cp = '\0'; 488 lreply(220, "%s", line); 489 } 490 (void) fflush(stdout); 491 (void) fclose(fd); 492 /* reply(220,) must follow */ 493 } 494 (void) gethostname(hostname, sizeof(hostname)); 495 reply(220, "%s FTP server (%s) ready.", hostname, version); 496 (void) setjmp(errcatch); 497 for (;;) 498 (void) yyparse(); 499 /* NOTREACHED */ 500} 501 502static void 503lostconn(signo) 504 int signo; 505{ 506 507 if (debug) 508 syslog(LOG_DEBUG, "lost connection"); 509 dologout(-1); 510} 511 512/* 513 * Helper function for sgetpwnam(). 514 */ 515static char * 516sgetsave(s) 517 char *s; 518{ 519 char *new = malloc((unsigned) strlen(s) + 1); 520 521 if (new == NULL) { 522 perror_reply(421, "Local resource failure: malloc"); 523 dologout(1); 524 /* NOTREACHED */ 525 } 526 (void) strcpy(new, s); 527 return (new); 528} 529 530/* 531 * Save the result of a getpwnam. Used for USER command, since 532 * the data returned must not be clobbered by any other command 533 * (e.g., globbing). 534 */ 535static struct passwd * 536sgetpwnam(name) 537 char *name; 538{ 539 static struct passwd save; 540 struct passwd *p; 541 542 if ((p = getpwnam(name)) == NULL) 543 return (p); 544 if (save.pw_name) { 545 free(save.pw_name); 546 free(save.pw_passwd); 547 free(save.pw_gecos); 548 free(save.pw_dir); 549 free(save.pw_shell); 550 } 551 save = *p; 552 save.pw_name = sgetsave(p->pw_name); 553 save.pw_passwd = sgetsave(p->pw_passwd); 554 save.pw_gecos = sgetsave(p->pw_gecos); 555 save.pw_dir = sgetsave(p->pw_dir); 556 save.pw_shell = sgetsave(p->pw_shell); 557 return (&save); 558} 559 560static int login_attempts; /* number of failed login attempts */ 561static int askpasswd; /* had user command, ask for passwd */ 562static char curname[10]; /* current USER name */ 563 564/* 565 * USER command. 566 * Sets global passwd pointer pw if named account exists and is acceptable; 567 * sets askpasswd if a PASS command is expected. If logged in previously, 568 * need to reset state. If name is "ftp" or "anonymous", the name is not in 569 * _PATH_FTPUSERS, and ftp account exists, set guest and pw, then just return. 570 * If account doesn't exist, ask for passwd anyway. Otherwise, check user 571 * requesting login privileges. Disallow anyone who does not have a standard 572 * shell as returned by getusershell(). Disallow anyone mentioned in the file 573 * _PATH_FTPUSERS to allow people such as root and uucp to be avoided. 574 */ 575void 576user(name) 577 char *name; 578{ 579 char *cp, *shell; 580 581 if (logged_in) { 582 if (guest) { 583 reply(530, "Can't change user from guest login."); 584 return; 585 } else if (dochroot) { 586 reply(530, "Can't change user from chroot user."); 587 return; 588 } 589 end_login(); 590 } 591 592 guest = 0; 593 if (strcmp(name, "ftp") == 0 || strcmp(name, "anonymous") == 0) { 594 if (checkuser(_PATH_FTPUSERS, "ftp") || 595 checkuser(_PATH_FTPUSERS, "anonymous")) 596 reply(530, "User %s access denied.", name); 597 else if ((pw = sgetpwnam("ftp")) != NULL) { 598 guest = 1; 599 askpasswd = 1; 600 reply(331, 601 "Guest login ok, send your email address as password."); 602 } else 603 reply(530, "User %s unknown.", name); 604 if (!askpasswd && logging) 605 syslog(LOG_NOTICE, 606 "ANONYMOUS FTP LOGIN REFUSED FROM %s", remotehost); 607 return; 608 } 609 if ((pw = sgetpwnam(name))) { 610 if ((shell = pw->pw_shell) == NULL || *shell == 0) 611 shell = _PATH_BSHELL; 612 while ((cp = getusershell()) != NULL) 613 if (strcmp(cp, shell) == 0) 614 break; 615 endusershell(); 616 617 if (cp == NULL || checkuser(_PATH_FTPUSERS, name)) { 618 reply(530, "User %s access denied.", name); 619 if (logging) 620 syslog(LOG_NOTICE, 621 "FTP LOGIN REFUSED FROM %s, %s", 622 remotehost, name); 623 pw = (struct passwd *) NULL; 624 return; 625 } 626 } 627 if (logging) 628 strncpy(curname, name, sizeof(curname)-1); 629#ifdef SKEY 630 pwok = skeyaccess(name, NULL, remotehost, addr_string); 631 cp = skey_challenge(name, pw, pwok, &sflag); 632 if (!pwok && sflag) { 633 reply(530, cp); 634 if (logging) 635 syslog(LOG_NOTICE, 636 "FTP LOGIN REFUSED FROM %s, %s", 637 remotehost, name); 638 pw = (struct passwd *) NULL; 639 return; 640 } 641 reply(331, cp); 642#else 643 reply(331, "Password required for %s.", name); 644#endif 645 askpasswd = 1; 646 /* 647 * Delay before reading passwd after first failed 648 * attempt to slow down passwd-guessing programs. 649 */ 650 if (login_attempts) 651 sleep((unsigned) login_attempts); 652} 653 654/* 655 * Check if a user is in the file "fname" 656 */ 657static int 658checkuser(fname, name) 659 char *fname; 660 char *name; 661{ 662 FILE *fd; 663 int found = 0; 664 char *p, line[BUFSIZ]; 665 666 if ((fd = fopen(fname, "r")) != NULL) { 667 while (fgets(line, sizeof(line), fd) != NULL) 668 if ((p = strchr(line, '\n')) != NULL) { 669 *p = '\0'; 670 if (line[0] == '#') 671 continue; 672 if (strcmp(line, name) == 0) { 673 found = 1; 674 break; 675 } 676 } 677 (void) fclose(fd); 678 } 679 return (found); 680} 681 682/* 683 * Terminate login as previous user, if any, resetting state; 684 * used when USER command is given or login fails. 685 */ 686static void 687end_login() 688{ 689 690 (void) seteuid((uid_t)0); 691 if (logged_in) 692 logwtmp(ttyline, "", ""); 693 pw = NULL; 694 logged_in = 0; 695 guest = 0; 696 dochroot = 0; 697} 698 699void 700pass(passwd) 701 char *passwd; 702{ 703 int rval; 704 FILE *fd; 705 static char homedir[MAXPATHLEN]; 706 707 if (logged_in || askpasswd == 0) { 708 reply(503, "Login with USER first."); 709 return; 710 } 711 askpasswd = 0; 712 if (!guest) { /* "ftp" is only account allowed no password */ 713 if (pw == NULL) { 714 rval = 1; /* failure below */ 715 goto skip; 716 } 717#if defined(KERBEROS) 718 rval = klogin(pw, "", hostname, passwd); 719 if (rval == 0) 720 goto skip; 721#endif 722#ifdef SKEY 723 rval = strcmp(skey_crypt(passwd, pw->pw_passwd, pw, pwok), 724 pw->pw_passwd); 725 pwok = 0; 726#else 727 rval = strcmp(crypt(passwd, pw->pw_passwd), pw->pw_passwd); 728#endif 729 /* The strcmp does not catch null passwords! */ 730 if (*pw->pw_passwd == '\0' || 731 (pw->pw_expire && time(NULL) >= pw->pw_expire)) 732 rval = 1; /* failure */ 733skip: 734 /* 735 * If rval == 1, the user failed the authentication check 736 * above. If rval == 0, either Kerberos or local authentication 737 * succeeded. 738 */ 739 if (rval) { 740 reply(530, "Login incorrect."); 741 if (logging) 742 syslog(LOG_NOTICE, 743 "FTP LOGIN FAILED FROM %s, %s", 744 remotehost, curname); 745 pw = NULL; 746 if (login_attempts++ >= 5) { 747 syslog(LOG_NOTICE, 748 "repeated login failures from %s", 749 remotehost); 750 exit(0); 751 } 752 return; 753 } 754 } 755 login_attempts = 0; /* this time successful */ 756 if (setegid((gid_t)pw->pw_gid) < 0) { 757 reply(550, "Can't set gid."); 758 return; 759 } 760 (void) initgroups(pw->pw_name, pw->pw_gid); 761 762 /* open wtmp before chroot */ 763 logwtmp(ttyline, pw->pw_name, remotehost); 764 logged_in = 1; 765 766 if (guest && stats && statfd < 0) 767 if ((statfd = open(_PATH_FTPDSTATFILE, O_WRONLY|O_APPEND)) < 0) 768 stats = 0; 769 770 dochroot = checkuser(_PATH_FTPCHROOT, pw->pw_name); 771 if (guest) { 772 /* 773 * We MUST do a chdir() after the chroot. Otherwise 774 * the old current directory will be accessible as "." 775 * outside the new root! 776 */ 777 if (chroot(pw->pw_dir) < 0 || chdir("/") < 0) { 778 reply(550, "Can't set guest privileges."); 779 goto bad; 780 } 781 } else if (dochroot) { 782 if (chroot(pw->pw_dir) < 0 || chdir("/") < 0) { 783 reply(550, "Can't change root."); 784 goto bad; 785 } 786 } else if (chdir(pw->pw_dir) < 0) { 787 if (chdir("/") < 0) { 788 reply(530, "User %s: can't change directory to %s.", 789 pw->pw_name, pw->pw_dir); 790 goto bad; 791 } else 792 lreply(230, "No directory! Logging in with home=/"); 793 } 794 if (seteuid((uid_t)pw->pw_uid) < 0) { 795 reply(550, "Can't set uid."); 796 goto bad; 797 } 798 799 /* 800 * Set home directory so that use of ~ (tilde) works correctly. 801 */ 802 if (getcwd(homedir, MAXPATHLEN) != NULL) 803 setenv("HOME", homedir, 1); 804 805 /* 806 * Display a login message, if it exists. 807 * N.B. reply(230,) must follow the message. 808 */ 809 if ((fd = fopen(_PATH_FTPLOGINMESG, "r")) != NULL) { 810 char *cp, line[LINE_MAX]; 811 812 while (fgets(line, sizeof(line), fd) != NULL) { 813 if ((cp = strchr(line, '\n')) != NULL) 814 *cp = '\0'; 815 lreply(230, "%s", line); 816 } 817 (void) fflush(stdout); 818 (void) fclose(fd); 819 } 820 if (guest) { 821 if (ident != NULL) 822 free(ident); 823 ident = strdup(passwd); 824 if (ident == NULL) 825 fatal("Ran out of memory."); 826 827 reply(230, "Guest login ok, access restrictions apply."); 828#ifdef SETPROCTITLE 829 snprintf(proctitle, sizeof(proctitle), 830 "%s: anonymous/%.*s", remotehost, 831 sizeof(proctitle) - sizeof(remotehost) - 832 sizeof(": anonymous/"), passwd); 833 setproctitle("%s", proctitle); 834#endif /* SETPROCTITLE */ 835 if (logging) 836 syslog(LOG_INFO, "ANONYMOUS FTP LOGIN FROM %s, %s", 837 remotehost, passwd); 838 } else { 839 reply(230, "User %s logged in.", pw->pw_name); 840#ifdef SETPROCTITLE 841 snprintf(proctitle, sizeof(proctitle), 842 "%s: %s", remotehost, pw->pw_name); 843 setproctitle("%s", proctitle); 844#endif /* SETPROCTITLE */ 845 if (logging) 846 syslog(LOG_INFO, "FTP LOGIN FROM %s as %s", 847 remotehost, pw->pw_name); 848 } 849 (void) umask(defumask); 850 return; 851bad: 852 /* Forget all about it... */ 853 end_login(); 854} 855 856void 857retrieve(cmd, name) 858 char *cmd, *name; 859{ 860 FILE *fin, *dout; 861 struct stat st; 862 int (*closefunc) __P((FILE *)); 863 long start; 864 865 if (cmd == 0) { 866 fin = fopen(name, "r"), closefunc = fclose; 867 st.st_size = 0; 868 } else { 869 char line[BUFSIZ]; 870 871 (void) sprintf(line, cmd, name), name = line; 872 fin = ftpd_popen(line, "r"), closefunc = ftpd_pclose; 873 st.st_size = -1; 874 st.st_blksize = BUFSIZ; 875 } 876 if (fin == NULL) { 877 if (errno != 0) { 878 perror_reply(550, name); 879 if (cmd == 0) { 880 LOGCMD("get", name); 881 } 882 } 883 return; 884 } 885 byte_count = -1; 886 if (cmd == 0 && (fstat(fileno(fin), &st) < 0 || !S_ISREG(st.st_mode))) { 887 reply(550, "%s: not a plain file.", name); 888 goto done; 889 } 890 if (restart_point) { 891 if (type == TYPE_A) { 892 off_t i, n; 893 int c; 894 895 n = restart_point; 896 i = 0; 897 while (i++ < n) { 898 if ((c=getc(fin)) == EOF) { 899 perror_reply(550, name); 900 goto done; 901 } 902 if (c == '\n') 903 i++; 904 } 905 } else if (lseek(fileno(fin), restart_point, L_SET) < 0) { 906 perror_reply(550, name); 907 goto done; 908 } 909 } 910 dout = dataconn(name, st.st_size, "w"); 911 if (dout == NULL) 912 goto done; 913 time(&start); 914 send_data(fin, dout, st.st_blksize, st.st_size, 915 restart_point == 0 && cmd == 0 && S_ISREG(st.st_mode)); 916 if (cmd == 0 && guest && stats) 917 logxfer(name, st.st_size, start); 918 (void) fclose(dout); 919 data = -1; 920 pdata = -1; 921done: 922 if (cmd == 0) 923 LOGBYTES("get", name, byte_count); 924 (*closefunc)(fin); 925} 926 927void 928store(name, mode, unique) 929 char *name, *mode; 930 int unique; 931{ 932 FILE *fout, *din; 933 struct stat st; 934 int (*closefunc) __P((FILE *)); 935 936 if ((unique || guest) && stat(name, &st) == 0 && 937 (name = gunique(name)) == NULL) { 938 LOGCMD(*mode == 'w' ? "put" : "append", name); 939 return; 940 } 941 942 if (restart_point) 943 mode = "r+"; 944 fout = fopen(name, mode); 945 closefunc = fclose; 946 if (fout == NULL) { 947 perror_reply(553, name); 948 LOGCMD(*mode == 'w' ? "put" : "append", name); 949 return; 950 } 951 byte_count = -1; 952 if (restart_point) { 953 if (type == TYPE_A) { 954 off_t i, n; 955 int c; 956 957 n = restart_point; 958 i = 0; 959 while (i++ < n) { 960 if ((c=getc(fout)) == EOF) { 961 perror_reply(550, name); 962 goto done; 963 } 964 if (c == '\n') 965 i++; 966 } 967 /* 968 * We must do this seek to "current" position 969 * because we are changing from reading to 970 * writing. 971 */ 972 if (fseek(fout, 0L, L_INCR) < 0) { 973 perror_reply(550, name); 974 goto done; 975 } 976 } else if (lseek(fileno(fout), restart_point, L_SET) < 0) { 977 perror_reply(550, name); 978 goto done; 979 } 980 } 981 din = dataconn(name, (off_t)-1, "r"); 982 if (din == NULL) 983 goto done; 984 if (receive_data(din, fout) == 0) { 985 if (unique) 986 reply(226, "Transfer complete (unique file name:%s).", 987 name); 988 else 989 reply(226, "Transfer complete."); 990 } 991 (void) fclose(din); 992 data = -1; 993 pdata = -1; 994done: 995 LOGBYTES(*mode == 'w' ? "put" : "append", name, byte_count); 996 (*closefunc)(fout); 997} 998 999static FILE * 1000getdatasock(mode) 1001 char *mode; 1002{ 1003 int on = 1, s, t, tries; 1004 1005 if (data >= 0) 1006 return (fdopen(data, mode)); 1007 (void) seteuid((uid_t)0); 1008 s = socket(AF_INET, SOCK_STREAM, 0); 1009 if (s < 0) 1010 goto bad; 1011 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, 1012 (char *) &on, sizeof(on)) < 0) 1013 goto bad; 1014 /* anchor socket to avoid multi-homing problems */ 1015 data_source.sin_len = sizeof(struct sockaddr_in); 1016 data_source.sin_family = AF_INET; 1017 data_source.sin_addr = ctrl_addr.sin_addr; 1018 for (tries = 1; ; tries++) { 1019 if (bind(s, (struct sockaddr *)&data_source, 1020 sizeof(data_source)) >= 0) 1021 break; 1022 if (errno != EADDRINUSE || tries > 10) 1023 goto bad; 1024 sleep(tries); 1025 } 1026 (void) seteuid((uid_t)pw->pw_uid); 1027#ifdef IP_TOS 1028 on = IPTOS_THROUGHPUT; 1029 if (setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0) 1030 syslog(LOG_WARNING, "setsockopt (IP_TOS): %m"); 1031#endif 1032#ifdef TCP_NOPUSH 1033 /* 1034 * Turn off push flag to keep sender TCP from sending short packets 1035 * at the boundaries of each write(). Should probably do a SO_SNDBUF 1036 * to set the send buffer size as well, but that may not be desirable 1037 * in heavy-load situations. 1038 */ 1039 on = 1; 1040 if (setsockopt(s, IPPROTO_TCP, TCP_NOPUSH, (char *)&on, sizeof on) < 0) 1041 syslog(LOG_WARNING, "setsockopt (TCP_NOPUSH): %m"); 1042#endif 1043#ifdef SO_SNDBUF 1044 on = 65536; 1045 if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, (char *)&on, sizeof on) < 0) 1046 syslog(LOG_WARNING, "setsockopt (SO_SNDBUF): %m"); 1047#endif 1048 1049 return (fdopen(s, mode)); 1050bad: 1051 /* Return the real value of errno (close may change it) */ 1052 t = errno; 1053 (void) seteuid((uid_t)pw->pw_uid); 1054 (void) close(s); 1055 errno = t; 1056 return (NULL); 1057} 1058 1059static FILE * 1060dataconn(name, size, mode) 1061 char *name; 1062 off_t size; 1063 char *mode; 1064{ 1065 char sizebuf[32]; 1066 FILE *file; 1067 int retry = 0, tos; 1068 1069 file_size = size; 1070 byte_count = 0; 1071 if (size != (off_t) -1) 1072 (void) sprintf(sizebuf, " (%qd bytes)", size); 1073 else 1074 (void) strcpy(sizebuf, ""); 1075 if (pdata >= 0) { 1076 struct sockaddr_in from; 1077 int s, fromlen = sizeof(from); 1078 struct timeval timeout; 1079 fd_set set; 1080 1081 FD_ZERO(&set); 1082 FD_SET(pdata, &set); 1083 1084 timeout.tv_usec = 0; 1085 timeout.tv_sec = 120; 1086 1087 if (select(pdata+1, &set, (fd_set *) 0, (fd_set *) 0, &timeout) == 0 || 1088 (s = accept(pdata, (struct sockaddr *) &from, &fromlen)) < 0) { 1089 reply(425, "Can't open data connection."); 1090 (void) close(pdata); 1091 pdata = -1; 1092 return (NULL); 1093 } 1094 (void) close(pdata); 1095 pdata = s; 1096#ifdef IP_TOS 1097 tos = IPTOS_THROUGHPUT; 1098 (void) setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&tos, 1099 sizeof(int)); 1100#endif 1101 reply(150, "Opening %s mode data connection for '%s'%s.", 1102 type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf); 1103 return (fdopen(pdata, mode)); 1104 } 1105 if (data >= 0) { 1106 reply(125, "Using existing data connection for '%s'%s.", 1107 name, sizebuf); 1108 usedefault = 1; 1109 return (fdopen(data, mode)); 1110 } 1111 if (usedefault) 1112 data_dest = his_addr; 1113 usedefault = 1; 1114 file = getdatasock(mode); 1115 if (file == NULL) { 1116 reply(425, "Can't create data socket (%s,%d): %s.", 1117 inet_ntoa(data_source.sin_addr), 1118 ntohs(data_source.sin_port), strerror(errno)); 1119 return (NULL); 1120 } 1121 data = fileno(file); 1122 while (connect(data, (struct sockaddr *)&data_dest, 1123 sizeof(data_dest)) < 0) { 1124 if (errno == EADDRINUSE && retry < swaitmax) { 1125 sleep((unsigned) swaitint); 1126 retry += swaitint; 1127 continue; 1128 } 1129 perror_reply(425, "Can't build data connection"); 1130 (void) fclose(file); 1131 data = -1; 1132 return (NULL); 1133 } 1134 reply(150, "Opening %s mode data connection for '%s'%s.", 1135 type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf); 1136 return (file); 1137} 1138 1139/* 1140 * Tranfer the contents of "instr" to "outstr" peer using the appropriate 1141 * encapsulation of the data subject to Mode, Structure, and Type. 1142 * 1143 * NB: Form isn't handled. 1144 */ 1145static void 1146send_data(instr, outstr, blksize, filesize, isreg) 1147 FILE *instr, *outstr; 1148 off_t blksize; 1149 off_t filesize; 1150 int isreg; 1151{ 1152 int c, cnt, filefd, netfd; 1153 char *buf, *bp; 1154 size_t len; 1155 1156 transflag++; 1157 if (setjmp(urgcatch)) { 1158 transflag = 0; 1159 return; 1160 } 1161 switch (type) { 1162 1163 case TYPE_A: 1164 while ((c = getc(instr)) != EOF) { 1165 byte_count++; 1166 if (c == '\n') { 1167 if (ferror(outstr)) 1168 goto data_err; 1169 (void) putc('\r', outstr); 1170 } 1171 (void) putc(c, outstr); 1172 } 1173 fflush(outstr); 1174 transflag = 0; 1175 if (ferror(instr)) 1176 goto file_err; 1177 if (ferror(outstr)) 1178 goto data_err; 1179 reply(226, "Transfer complete."); 1180 return; 1181 1182 case TYPE_I: 1183 case TYPE_L: 1184 /* 1185 * isreg is only set if we are not doing restart and we 1186 * are sending a regular file 1187 */ 1188 netfd = fileno(outstr); 1189 filefd = fileno(instr); 1190 1191 if (isreg && filesize < (off_t)16 * 1024 * 1024) { 1192 buf = mmap(0, filesize, PROT_READ, MAP_SHARED, filefd, 1193 (off_t)0); 1194 if (!buf) { 1195 syslog(LOG_WARNING, "mmap(%lu): %m", 1196 (unsigned long)filesize); 1197 goto oldway; 1198 } 1199 bp = buf; 1200 len = filesize; 1201 do { 1202 cnt = write(netfd, bp, len); 1203 len -= cnt; 1204 bp += cnt; 1205 if (cnt > 0) byte_count += cnt; 1206 } while(cnt > 0 && len > 0); 1207 1208 transflag = 0; 1209 munmap(buf, (size_t)filesize); 1210 if (cnt < 0) 1211 goto data_err; 1212 reply(226, "Transfer complete."); 1213 return; 1214 } 1215 1216oldway: 1217 if ((buf = malloc((u_int)blksize)) == NULL) { 1218 transflag = 0; 1219 perror_reply(451, "Local resource failure: malloc"); 1220 return; 1221 } 1222 1223 while ((cnt = read(filefd, buf, (u_int)blksize)) > 0 && 1224 write(netfd, buf, cnt) == cnt) 1225 byte_count += cnt; 1226 transflag = 0; 1227 (void)free(buf); 1228 if (cnt != 0) { 1229 if (cnt < 0) 1230 goto file_err; 1231 goto data_err; 1232 } 1233 reply(226, "Transfer complete."); 1234 return; 1235 default: 1236 transflag = 0; 1237 reply(550, "Unimplemented TYPE %d in send_data", type); 1238 return; 1239 } 1240 1241data_err: 1242 transflag = 0; 1243 perror_reply(426, "Data connection"); 1244 return; 1245 1246file_err: 1247 transflag = 0; 1248 perror_reply(551, "Error on input file"); 1249} 1250 1251/* 1252 * Transfer data from peer to "outstr" using the appropriate encapulation of 1253 * the data subject to Mode, Structure, and Type. 1254 * 1255 * N.B.: Form isn't handled. 1256 */ 1257static int 1258receive_data(instr, outstr) 1259 FILE *instr, *outstr; 1260{ 1261 int c; 1262 int cnt, bare_lfs; 1263 char buf[BUFSIZ]; 1264 1265 transflag++; 1266 if (setjmp(urgcatch)) { 1267 transflag = 0; 1268 return (-1); 1269 } 1270 1271 bare_lfs = 0; 1272 1273 switch (type) { 1274 1275 case TYPE_I: 1276 case TYPE_L: 1277 while ((cnt = read(fileno(instr), buf, sizeof(buf))) > 0) { 1278 if (write(fileno(outstr), buf, cnt) != cnt) 1279 goto file_err; 1280 byte_count += cnt; 1281 } 1282 if (cnt < 0) 1283 goto data_err; 1284 transflag = 0; 1285 return (0); 1286 1287 case TYPE_E: 1288 reply(553, "TYPE E not implemented."); 1289 transflag = 0; 1290 return (-1); 1291 1292 case TYPE_A: 1293 while ((c = getc(instr)) != EOF) { 1294 byte_count++; 1295 if (c == '\n') 1296 bare_lfs++; 1297 while (c == '\r') { 1298 if (ferror(outstr)) 1299 goto data_err; 1300 if ((c = getc(instr)) != '\n') { 1301 (void) putc ('\r', outstr); 1302 if (c == '\0' || c == EOF) 1303 goto contin2; 1304 } 1305 } 1306 (void) putc(c, outstr); 1307 contin2: ; 1308 } 1309 fflush(outstr); 1310 if (ferror(instr)) 1311 goto data_err; 1312 if (ferror(outstr)) 1313 goto file_err; 1314 transflag = 0; 1315 if (bare_lfs) { 1316 lreply(226, 1317 "WARNING! %d bare linefeeds received in ASCII mode", 1318 bare_lfs); 1319 (void)printf(" File may not have transferred correctly.\r\n"); 1320 } 1321 return (0); 1322 default: 1323 reply(550, "Unimplemented TYPE %d in receive_data", type); 1324 transflag = 0; 1325 return (-1); 1326 } 1327 1328data_err: 1329 transflag = 0; 1330 perror_reply(426, "Data Connection"); 1331 return (-1); 1332 1333file_err: 1334 transflag = 0; 1335 perror_reply(452, "Error writing file"); 1336 return (-1); 1337} 1338 1339void 1340statfilecmd(filename) 1341 char *filename; 1342{ 1343 FILE *fin; 1344 int c; 1345 char line[LINE_MAX]; 1346 1347 (void)snprintf(line, sizeof(line), "/bin/ls -lgA %s", filename); 1348 fin = ftpd_popen(line, "r"); 1349 lreply(211, "status of %s:", filename); 1350 while ((c = getc(fin)) != EOF) { 1351 if (c == '\n') { 1352 if (ferror(stdout)){ 1353 perror_reply(421, "control connection"); 1354 (void) ftpd_pclose(fin); 1355 dologout(1); 1356 /* NOTREACHED */ 1357 } 1358 if (ferror(fin)) { 1359 perror_reply(551, filename); 1360 (void) ftpd_pclose(fin); 1361 return; 1362 } 1363 (void) putc('\r', stdout); 1364 } 1365 (void) putc(c, stdout); 1366 } 1367 (void) ftpd_pclose(fin); 1368 reply(211, "End of Status"); 1369} 1370 1371void 1372statcmd() 1373{ 1374 struct sockaddr_in *sin; 1375 u_char *a, *p; 1376 1377 lreply(211, "%s FTP server status:", hostname, version); 1378 printf(" %s\r\n", version); 1379 printf(" Connected to %s", remotehost); 1380 if (!isdigit(remotehost[0])) 1381 printf(" (%s)", inet_ntoa(his_addr.sin_addr)); 1382 printf("\r\n"); 1383 if (logged_in) { 1384 if (guest) 1385 printf(" Logged in anonymously\r\n"); 1386 else 1387 printf(" Logged in as %s\r\n", pw->pw_name); 1388 } else if (askpasswd) 1389 printf(" Waiting for password\r\n"); 1390 else 1391 printf(" Waiting for user name\r\n"); 1392 printf(" TYPE: %s", typenames[type]); 1393 if (type == TYPE_A || type == TYPE_E) 1394 printf(", FORM: %s", formnames[form]); 1395 if (type == TYPE_L) 1396#if NBBY == 8 1397 printf(" %d", NBBY); 1398#else 1399 printf(" %d", bytesize); /* need definition! */ 1400#endif 1401 printf("; STRUcture: %s; transfer MODE: %s\r\n", 1402 strunames[stru], modenames[mode]); 1403 if (data != -1) 1404 printf(" Data connection open\r\n"); 1405 else if (pdata != -1) { 1406 printf(" in Passive mode"); 1407 sin = &pasv_addr; 1408 goto printaddr; 1409 } else if (usedefault == 0) { 1410 printf(" PORT"); 1411 sin = &data_dest; 1412printaddr: 1413 a = (u_char *) &sin->sin_addr; 1414 p = (u_char *) &sin->sin_port; 1415#define UC(b) (((int) b) & 0xff) 1416 printf(" (%d,%d,%d,%d,%d,%d)\r\n", UC(a[0]), 1417 UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1])); 1418#undef UC 1419 } else 1420 printf(" No data connection\r\n"); 1421 reply(211, "End of status"); 1422} 1423 1424void 1425fatal(s) 1426 char *s; 1427{ 1428 1429 reply(451, "Error in server: %s\n", s); 1430 reply(221, "Closing connection due to server error."); 1431 dologout(0); 1432 /* NOTREACHED */ 1433} 1434 1435void 1436#if __STDC__ 1437reply(int n, const char *fmt, ...) 1438#else 1439reply(n, fmt, va_alist) 1440 int n; 1441 char *fmt; 1442 va_dcl 1443#endif 1444{ 1445 va_list ap; 1446#if __STDC__ 1447 va_start(ap, fmt); 1448#else 1449 va_start(ap); 1450#endif 1451 (void)printf("%d ", n); 1452 (void)vprintf(fmt, ap); 1453 (void)printf("\r\n"); 1454 (void)fflush(stdout); 1455 if (debug) { 1456 syslog(LOG_DEBUG, "<--- %d ", n); 1457 vsyslog(LOG_DEBUG, fmt, ap); 1458 } 1459} 1460 1461void 1462#if __STDC__ 1463lreply(int n, const char *fmt, ...) 1464#else 1465lreply(n, fmt, va_alist) 1466 int n; 1467 char *fmt; 1468 va_dcl 1469#endif 1470{ 1471 va_list ap; 1472#if __STDC__ 1473 va_start(ap, fmt); 1474#else 1475 va_start(ap); 1476#endif 1477 (void)printf("%d- ", n); 1478 (void)vprintf(fmt, ap); 1479 (void)printf("\r\n"); 1480 (void)fflush(stdout); 1481 if (debug) { 1482 syslog(LOG_DEBUG, "<--- %d- ", n); 1483 vsyslog(LOG_DEBUG, fmt, ap); 1484 } 1485} 1486 1487static void 1488ack(s) 1489 char *s; 1490{ 1491 1492 reply(250, "%s command successful.", s); 1493} 1494 1495void 1496nack(s) 1497 char *s; 1498{ 1499 1500 reply(502, "%s command not implemented.", s); 1501} 1502 1503/* ARGSUSED */ 1504void 1505yyerror(s) 1506 char *s; 1507{ 1508 char *cp; 1509 1510 if ((cp = strchr(cbuf,'\n'))) 1511 *cp = '\0'; 1512 reply(500, "'%s': command not understood.", cbuf); 1513} 1514 1515void 1516delete(name) 1517 char *name; 1518{ 1519 struct stat st; 1520 1521 LOGCMD("delete", name); 1522 if (stat(name, &st) < 0) { 1523 perror_reply(550, name); 1524 return; 1525 } 1526 if ((st.st_mode&S_IFMT) == S_IFDIR) { 1527 if (rmdir(name) < 0) { 1528 perror_reply(550, name); 1529 return; 1530 } 1531 goto done; 1532 } 1533 if (unlink(name) < 0) { 1534 perror_reply(550, name); 1535 return; 1536 } 1537done: 1538 ack("DELE"); 1539} 1540 1541void 1542cwd(path) 1543 char *path; 1544{ 1545 1546 if (chdir(path) < 0) 1547 perror_reply(550, path); 1548 else 1549 ack("CWD"); 1550} 1551 1552void 1553makedir(name) 1554 char *name; 1555{ 1556 1557 LOGCMD("mkdir", name); 1558 if (mkdir(name, 0777) < 0) 1559 perror_reply(550, name); 1560 else 1561 reply(257, "MKD command successful."); 1562} 1563 1564void 1565removedir(name) 1566 char *name; 1567{ 1568 1569 LOGCMD("rmdir", name); 1570 if (rmdir(name) < 0) 1571 perror_reply(550, name); 1572 else 1573 ack("RMD"); 1574} 1575 1576void 1577pwd() 1578{ 1579 char path[MAXPATHLEN + 1]; 1580 1581 if (getwd(path) == (char *)NULL) 1582 reply(550, "%s.", path); 1583 else 1584 reply(257, "\"%s\" is current directory.", path); 1585} 1586 1587char * 1588renamefrom(name) 1589 char *name; 1590{ 1591 struct stat st; 1592 1593 if (stat(name, &st) < 0) { 1594 perror_reply(550, name); 1595 return ((char *)0); 1596 } 1597 reply(350, "File exists, ready for destination name"); 1598 return (name); 1599} 1600 1601void 1602renamecmd(from, to) 1603 char *from, *to; 1604{ 1605 struct stat st; 1606 1607 LOGCMD2("rename", from, to); 1608 1609 if (guest && (stat(to, &st) == 0)) { 1610 reply(550, "%s: permission denied", to); 1611 return; 1612 } 1613 1614 if (rename(from, to) < 0) 1615 perror_reply(550, "rename"); 1616 else 1617 ack("RNTO"); 1618} 1619 1620static void 1621dolog(sin) 1622 struct sockaddr_in *sin; 1623{ 1624 struct hostent *hp = gethostbyaddr((char *)&sin->sin_addr, 1625 sizeof(struct in_addr), AF_INET); 1626 1627 if (hp) 1628 (void) strncpy(remotehost, hp->h_name, sizeof(remotehost)); 1629 else 1630 (void) strncpy(remotehost, inet_ntoa(sin->sin_addr), 1631 sizeof(remotehost)); 1632#ifdef SETPROCTITLE 1633 snprintf(proctitle, sizeof(proctitle), "%s: connected", remotehost); 1634 setproctitle("%s", proctitle); 1635#endif /* SETPROCTITLE */ 1636 1637 if (logging) 1638 syslog(LOG_INFO, "connection from %s", remotehost); 1639} 1640 1641/* 1642 * Record logout in wtmp file 1643 * and exit with supplied status. 1644 */ 1645void 1646dologout(status) 1647 int status; 1648{ 1649 1650 if (logged_in) { 1651 (void) seteuid((uid_t)0); 1652 logwtmp(ttyline, "", ""); 1653#if defined(KERBEROS) 1654 if (!notickets && krbtkfile_env) 1655 unlink(krbtkfile_env); 1656#endif 1657 } 1658 /* beware of flushing buffers after a SIGPIPE */ 1659 _exit(status); 1660} 1661 1662static void 1663myoob(signo) 1664 int signo; 1665{ 1666 char *cp; 1667 1668 /* only process if transfer occurring */ 1669 if (!transflag) 1670 return; 1671 cp = tmpline; 1672 if (getline(cp, 7, stdin) == NULL) { 1673 reply(221, "You could at least say goodbye."); 1674 dologout(0); 1675 } 1676 upper(cp); 1677 if (strcmp(cp, "ABOR\r\n") == 0) { 1678 tmpline[0] = '\0'; 1679 reply(426, "Transfer aborted. Data connection closed."); 1680 reply(226, "Abort successful"); 1681 longjmp(urgcatch, 1); 1682 } 1683 if (strcmp(cp, "STAT\r\n") == 0) { 1684 if (file_size != (off_t) -1) 1685 reply(213, "Status: %qd of %qd bytes transferred", 1686 byte_count, file_size); 1687 else 1688 reply(213, "Status: %qd bytes transferred", byte_count); 1689 } 1690} 1691 1692/* 1693 * Note: a response of 425 is not mentioned as a possible response to 1694 * the PASV command in RFC959. However, it has been blessed as 1695 * a legitimate response by Jon Postel in a telephone conversation 1696 * with Rick Adams on 25 Jan 89. 1697 */ 1698void 1699passive() 1700{ 1701 int len, on; 1702 char *p, *a; 1703 1704 if (pdata >= 0) /* close old port if one set */ 1705 close(pdata); 1706 1707 pdata = socket(AF_INET, SOCK_STREAM, 0); 1708 if (pdata < 0) { 1709 perror_reply(425, "Can't open passive connection"); 1710 return; 1711 } 1712 1713 (void) seteuid((uid_t)0); 1714 1715 on = restricted_data_ports ? IP_PORTRANGE_HIGH : IP_PORTRANGE_DEFAULT; 1716 if (setsockopt(pdata, IPPROTO_IP, IP_PORTRANGE, 1717 (char *)&on, sizeof(on)) < 0) { 1718 goto pasv_error; 1719 } 1720 1721 pasv_addr = ctrl_addr; 1722 pasv_addr.sin_port = 0; 1723 if (bind(pdata, (struct sockaddr *)&pasv_addr, 1724 sizeof(pasv_addr)) < 0) 1725 goto pasv_error; 1726 1727 (void) seteuid((uid_t)pw->pw_uid); 1728 1729 len = sizeof(pasv_addr); 1730 if (getsockname(pdata, (struct sockaddr *) &pasv_addr, &len) < 0) 1731 goto pasv_error; 1732 if (listen(pdata, 1) < 0) 1733 goto pasv_error; 1734 a = (char *) &pasv_addr.sin_addr; 1735 p = (char *) &pasv_addr.sin_port; 1736 1737#define UC(b) (((int) b) & 0xff) 1738 1739 reply(227, "Entering Passive Mode (%d,%d,%d,%d,%d,%d)", UC(a[0]), 1740 UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1])); 1741 return; 1742 1743pasv_error: 1744 (void) seteuid((uid_t)pw->pw_uid); 1745 (void) close(pdata); 1746 pdata = -1; 1747 perror_reply(425, "Can't open passive connection"); 1748 return; 1749} 1750 1751/* 1752 * Generate unique name for file with basename "local". 1753 * The file named "local" is already known to exist. 1754 * Generates failure reply on error. 1755 */ 1756static char * 1757gunique(local) 1758 char *local; 1759{ 1760 static char new[MAXPATHLEN]; 1761 struct stat st; 1762 int count; 1763 char *cp; 1764 1765 cp = strrchr(local, '/'); 1766 if (cp) 1767 *cp = '\0'; 1768 if (stat(cp ? local : ".", &st) < 0) { 1769 perror_reply(553, cp ? local : "."); 1770 return ((char *) 0); 1771 } 1772 if (cp) 1773 *cp = '/'; 1774 (void) snprintf(new, sizeof(new), "%s", local); 1775 cp = new + strlen(new); 1776 *cp++ = '.'; 1777 for (count = 1; count < 100; count++) { 1778 (void)sprintf(cp, "%d", count); 1779 if (stat(new, &st) < 0) 1780 return (new); 1781 } 1782 reply(452, "Unique file name cannot be created."); 1783 return (NULL); 1784} 1785 1786/* 1787 * Format and send reply containing system error number. 1788 */ 1789void 1790perror_reply(code, string) 1791 int code; 1792 char *string; 1793{ 1794 1795 reply(code, "%s: %s.", string, strerror(errno)); 1796} 1797 1798static char *onefile[] = { 1799 "", 1800 0 1801}; 1802 1803void 1804send_file_list(whichf) 1805 char *whichf; 1806{ 1807 struct stat st; 1808 DIR *dirp = NULL; 1809 struct dirent *dir; 1810 FILE *dout = NULL; 1811 char **dirlist, *dirname; 1812 int simple = 0; 1813 int freeglob = 0; 1814 glob_t gl; 1815 1816 if (strpbrk(whichf, "~{[*?") != NULL) { 1817 int flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_QUOTE|GLOB_TILDE; 1818 1819 memset(&gl, 0, sizeof(gl)); 1820 freeglob = 1; 1821 if (glob(whichf, flags, 0, &gl)) { 1822 reply(550, "not found"); 1823 goto out; 1824 } else if (gl.gl_pathc == 0) { 1825 errno = ENOENT; 1826 perror_reply(550, whichf); 1827 goto out; 1828 } 1829 dirlist = gl.gl_pathv; 1830 } else { 1831 onefile[0] = whichf; 1832 dirlist = onefile; 1833 simple = 1; 1834 } 1835 1836 if (setjmp(urgcatch)) { 1837 transflag = 0; 1838 goto out; 1839 } 1840 while ((dirname = *dirlist++)) { 1841 if (stat(dirname, &st) < 0) { 1842 /* 1843 * If user typed "ls -l", etc, and the client 1844 * used NLST, do what the user meant. 1845 */ 1846 if (dirname[0] == '-' && *dirlist == NULL && 1847 transflag == 0) { 1848 retrieve("/bin/ls %s", dirname); 1849 goto out; 1850 } 1851 perror_reply(550, whichf); 1852 if (dout != NULL) { 1853 (void) fclose(dout); 1854 transflag = 0; 1855 data = -1; 1856 pdata = -1; 1857 } 1858 goto out; 1859 } 1860 1861 if (S_ISREG(st.st_mode)) { 1862 if (dout == NULL) { 1863 dout = dataconn("file list", (off_t)-1, "w"); 1864 if (dout == NULL) 1865 goto out; 1866 transflag++; 1867 } 1868 fprintf(dout, "%s%s\n", dirname, 1869 type == TYPE_A ? "\r" : ""); 1870 byte_count += strlen(dirname) + 1; 1871 continue; 1872 } else if (!S_ISDIR(st.st_mode)) 1873 continue; 1874 1875 if ((dirp = opendir(dirname)) == NULL) 1876 continue; 1877 1878 while ((dir = readdir(dirp)) != NULL) { 1879 char nbuf[MAXPATHLEN]; 1880 1881 if (dir->d_name[0] == '.' && dir->d_namlen == 1) 1882 continue; 1883 if (dir->d_name[0] == '.' && dir->d_name[1] == '.' && 1884 dir->d_namlen == 2) 1885 continue; 1886 1887 sprintf(nbuf, "%s/%s", dirname, dir->d_name); 1888 1889 /* 1890 * We have to do a stat to insure it's 1891 * not a directory or special file. 1892 */ 1893 if (simple || (stat(nbuf, &st) == 0 && 1894 S_ISREG(st.st_mode))) { 1895 if (dout == NULL) { 1896 dout = dataconn("file list", (off_t)-1, 1897 "w"); 1898 if (dout == NULL) 1899 goto out; 1900 transflag++; 1901 } 1902 if (nbuf[0] == '.' && nbuf[1] == '/') 1903 fprintf(dout, "%s%s\n", &nbuf[2], 1904 type == TYPE_A ? "\r" : ""); 1905 else 1906 fprintf(dout, "%s%s\n", nbuf, 1907 type == TYPE_A ? "\r" : ""); 1908 byte_count += strlen(nbuf) + 1; 1909 } 1910 } 1911 (void) closedir(dirp); 1912 } 1913 1914 if (dout == NULL) 1915 reply(550, "No files found."); 1916 else if (ferror(dout) != 0) 1917 perror_reply(550, "Data connection"); 1918 else 1919 reply(226, "Transfer complete."); 1920 1921 transflag = 0; 1922 if (dout != NULL) 1923 (void) fclose(dout); 1924 data = -1; 1925 pdata = -1; 1926out: 1927 if (freeglob) { 1928 freeglob = 0; 1929 globfree(&gl); 1930 } 1931} 1932 1933void 1934reapchild(signo) 1935 int signo; 1936{ 1937 while (wait3(NULL, WNOHANG, NULL) > 0); 1938} 1939 1940#ifdef OLD_SETPROCTITLE 1941/* 1942 * Clobber argv so ps will show what we're doing. (Stolen from sendmail.) 1943 * Warning, since this is usually started from inetd.conf, it often doesn't 1944 * have much of an environment or arglist to overwrite. 1945 */ 1946void 1947#if __STDC__ 1948setproctitle(const char *fmt, ...) 1949#else 1950setproctitle(fmt, va_alist) 1951 char *fmt; 1952 va_dcl 1953#endif 1954{ 1955 int i; 1956 va_list ap; 1957 char *p, *bp, ch; 1958 char buf[LINE_MAX]; 1959 1960#if __STDC__ 1961 va_start(ap, fmt); 1962#else 1963 va_start(ap); 1964#endif 1965 (void)vsnprintf(buf, sizeof(buf), fmt, ap); 1966 1967 /* make ps print our process name */ 1968 p = Argv[0]; 1969 *p++ = '-'; 1970 1971 i = strlen(buf); 1972 if (i > LastArgv - p - 2) { 1973 i = LastArgv - p - 2; 1974 buf[i] = '\0'; 1975 } 1976 bp = buf; 1977 while (ch = *bp++) 1978 if (ch != '\n' && ch != '\r') 1979 *p++ = ch; 1980 while (p < LastArgv) 1981 *p++ = ' '; 1982} 1983#endif /* OLD_SETPROCTITLE */ 1984 1985static void 1986logxfer(name, size, start) 1987 char *name; 1988 long size; 1989 long start; 1990{ 1991 char buf[1024]; 1992 char path[MAXPATHLEN + 1]; 1993 long now; 1994 1995 if (statfd >= 0 && getwd(path) != NULL) { 1996 time(&now); 1997 snprintf(buf, sizeof(buf), "%.20s!%s!%s!%s/%s!%ld!%ld\n", 1998 ctime(&now)+4, ident, remotehost, 1999 path, name, size, now - start + (now == start)); 2000 write(statfd, buf, strlen(buf)); 2001 } 2002} 2003