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