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