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