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