122499Sdist /* 236304Skarels * Copyright (c) 1985, 1988 Regents of the University of California. 333738Sbostic * All rights reserved. 433738Sbostic * 533738Sbostic * Redistribution and use in source and binary forms are permitted 634769Sbostic * provided that the above copyright notice and this paragraph are 734769Sbostic * duplicated in all such forms and that any documentation, 834769Sbostic * advertising materials, and other materials related to such 934769Sbostic * distribution and use acknowledge that the software was developed 1034769Sbostic * by the University of California, Berkeley. The name of the 1134769Sbostic * University may not be used to endorse or promote products derived 1234769Sbostic * from this software without specific prior written permission. 1334769Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1434769Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1536933Skarels * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1622499Sdist */ 1722499Sdist 1810275Ssam #ifndef lint 1922499Sdist char copyright[] = 2036304Skarels "@(#) Copyright (c) 1985, 1988 Regents of the University of California.\n\ 2122499Sdist All rights reserved.\n"; 2233738Sbostic #endif /* not lint */ 2310275Ssam 2422499Sdist #ifndef lint 25*40183Smckusick static char sccsid[] = "@(#)ftpd.c 5.32 (Berkeley) 02/20/90"; 2633738Sbostic #endif /* not lint */ 2722499Sdist 2810275Ssam /* 2910275Ssam * FTP server. 3010275Ssam */ 3110303Ssam #include <sys/param.h> 3210275Ssam #include <sys/stat.h> 3310275Ssam #include <sys/ioctl.h> 3410275Ssam #include <sys/socket.h> 3513247Ssam #include <sys/file.h> 3613595Ssam #include <sys/wait.h> 3736620Srick #include <sys/dir.h> 3810275Ssam 3910275Ssam #include <netinet/in.h> 4010275Ssam 4136933Skarels #define FTP_NAMES 4213034Ssam #include <arpa/ftp.h> 4313211Sroot #include <arpa/inet.h> 4426044Sminshall #include <arpa/telnet.h> 4513034Ssam 4636933Skarels #include <ctype.h> 4710275Ssam #include <stdio.h> 4810275Ssam #include <signal.h> 4910275Ssam #include <pwd.h> 5010275Ssam #include <setjmp.h> 5110275Ssam #include <netdb.h> 5210423Ssam #include <errno.h> 5326044Sminshall #include <strings.h> 5426493Sminshall #include <syslog.h> 5536435Sbostic #include <varargs.h> 5637459Skarels #include "pathnames.h" 5710275Ssam 5810695Ssam /* 5910695Ssam * File containing login names 6010695Ssam * NOT to be used on this machine. 6110695Ssam * Commonly used to disallow uucp. 6210695Ssam */ 6310275Ssam extern int errno; 6410275Ssam extern char *sys_errlist[]; 6536304Skarels extern int sys_nerr; 6610275Ssam extern char *crypt(); 6710275Ssam extern char version[]; 6810275Ssam extern char *home; /* pointer to home directory for glob */ 6936276Sbostic extern FILE *ftpd_popen(), *fopen(), *freopen(); 7036304Skarels extern int ftpd_pclose(), fclose(); 7126044Sminshall extern char *getline(); 7226044Sminshall extern char cbuf[]; 7337459Skarels extern off_t restart_point; 7410275Ssam 7510275Ssam struct sockaddr_in ctrl_addr; 7610275Ssam struct sockaddr_in data_source; 7710275Ssam struct sockaddr_in data_dest; 7810275Ssam struct sockaddr_in his_addr; 7936933Skarels struct sockaddr_in pasv_addr; 8010275Ssam 8110275Ssam int data; 8226044Sminshall jmp_buf errcatch, urgcatch; 8310275Ssam int logged_in; 8410275Ssam struct passwd *pw; 8510275Ssam int debug; 8626493Sminshall int timeout = 900; /* timeout after 15 minutes of inactivity */ 8736933Skarels int maxtimeout = 7200;/* don't allow idle time to be set beyond 2 hours */ 8811757Ssam int logging; 8910275Ssam int guest; 9010275Ssam int type; 9110275Ssam int form; 9210275Ssam int stru; /* avoid C keyword */ 9310275Ssam int mode; 9410321Ssam int usedefault = 1; /* for data transfers */ 9536304Skarels int pdata = -1; /* for passive mode */ 9626044Sminshall int transflag; 9736933Skarels off_t file_size; 9836933Skarels off_t byte_count; 9936933Skarels #if !defined(CMASK) || CMASK == 0 10036933Skarels #undef CMASK 10136933Skarels #define CMASK 027 10236933Skarels #endif 10336933Skarels int defumask = CMASK; /* default umask value */ 10426044Sminshall char tmpline[7]; 10536276Sbostic char hostname[MAXHOSTNAMELEN]; 10636276Sbostic char remotehost[MAXHOSTNAMELEN]; 10710275Ssam 10811653Ssam /* 10911653Ssam * Timeout intervals for retrying connections 11011653Ssam * to hosts that don't accept PORT cmds. This 11111653Ssam * is a kludge, but given the problems with TCP... 11211653Ssam */ 11311653Ssam #define SWAITMAX 90 /* wait at most 90 seconds */ 11411653Ssam #define SWAITINT 5 /* interval between retries */ 11511653Ssam 11611653Ssam int swaitmax = SWAITMAX; 11711653Ssam int swaitint = SWAITINT; 11811653Ssam 11910275Ssam int lostconn(); 12026044Sminshall int myoob(); 12110275Ssam FILE *getdatasock(), *dataconn(); 12210275Ssam 12336620Srick #ifdef SETPROCTITLE 12436620Srick char **Argv = NULL; /* pointer to argument vector */ 12536620Srick char *LastArgv = NULL; /* end of argv */ 12636933Skarels char proctitle[BUFSIZ]; /* initial part of title */ 12736620Srick #endif /* SETPROCTITLE */ 12836620Srick 12936620Srick main(argc, argv, envp) 13010275Ssam int argc; 13110275Ssam char *argv[]; 13236620Srick char **envp; 13310275Ssam { 13427750Sminshall int addrlen, on = 1; 13510275Ssam char *cp; 13610275Ssam 13716339Skarels addrlen = sizeof (his_addr); 13836304Skarels if (getpeername(0, (struct sockaddr *)&his_addr, &addrlen) < 0) { 13926493Sminshall syslog(LOG_ERR, "getpeername (%s): %m",argv[0]); 14010275Ssam exit(1); 14110275Ssam } 14216339Skarels addrlen = sizeof (ctrl_addr); 14336304Skarels if (getsockname(0, (struct sockaddr *)&ctrl_addr, &addrlen) < 0) { 14426493Sminshall syslog(LOG_ERR, "getsockname (%s): %m",argv[0]); 14516339Skarels exit(1); 14616339Skarels } 14716339Skarels data_source.sin_port = htons(ntohs(ctrl_addr.sin_port) - 1); 14810275Ssam debug = 0; 14926493Sminshall openlog("ftpd", LOG_PID, LOG_DAEMON); 15036620Srick #ifdef SETPROCTITLE 15136620Srick /* 15236620Srick * Save start and extent of argv for setproctitle. 15336620Srick */ 15436620Srick Argv = argv; 15536620Srick while (*envp) 15636620Srick envp++; 15736620Srick LastArgv = envp[-1] + strlen(envp[-1]); 15836620Srick #endif /* SETPROCTITLE */ 15936620Srick 16010275Ssam argc--, argv++; 16110275Ssam while (argc > 0 && *argv[0] == '-') { 16210275Ssam for (cp = &argv[0][1]; *cp; cp++) switch (*cp) { 16310275Ssam 16411653Ssam case 'v': 16511653Ssam debug = 1; 16611653Ssam break; 16711653Ssam 16810275Ssam case 'd': 16910275Ssam debug = 1; 17010275Ssam break; 17110275Ssam 17211757Ssam case 'l': 17311757Ssam logging = 1; 17411757Ssam break; 17511757Ssam 17611653Ssam case 't': 17711653Ssam timeout = atoi(++cp); 17836933Skarels if (maxtimeout < timeout) 17936933Skarels maxtimeout = timeout; 18011653Ssam goto nextopt; 18111653Ssam 18236933Skarels case 'T': 18336933Skarels maxtimeout = atoi(++cp); 18436933Skarels if (timeout > maxtimeout) 18536933Skarels timeout = maxtimeout; 18636933Skarels goto nextopt; 18736933Skarels 18836933Skarels case 'u': 18936933Skarels { 19036933Skarels int val = 0; 19136933Skarels 19236933Skarels while (*++cp && *cp >= '0' && *cp <= '9') 19336933Skarels val = val*8 + *cp - '0'; 19436933Skarels if (*cp) 19536933Skarels fprintf(stderr, "ftpd: Bad value for -u\n"); 19636933Skarels else 19736933Skarels defumask = val; 19836933Skarels goto nextopt; 19936933Skarels } 20036933Skarels 20110275Ssam default: 20216339Skarels fprintf(stderr, "ftpd: Unknown flag -%c ignored.\n", 20316339Skarels *cp); 20410275Ssam break; 20510275Ssam } 20611653Ssam nextopt: 20710275Ssam argc--, argv++; 20810275Ssam } 20937459Skarels (void) freopen(_PATH_DEVNULL, "w", stderr); 21026493Sminshall (void) signal(SIGPIPE, lostconn); 21126493Sminshall (void) signal(SIGCHLD, SIG_IGN); 21235691Sbostic if ((int)signal(SIGURG, myoob) < 0) 21326493Sminshall syslog(LOG_ERR, "signal: %m"); 21435691Sbostic 21538134Srick /* Try to handle urgent data inline */ 21627750Sminshall #ifdef SO_OOBINLINE 21736276Sbostic if (setsockopt(0, SOL_SOCKET, SO_OOBINLINE, (char *)&on, sizeof(on)) < 0) 21827750Sminshall syslog(LOG_ERR, "setsockopt: %m"); 21936276Sbostic #endif 22038134Srick 22136933Skarels #ifdef F_SETOWN 22236304Skarels if (fcntl(fileno(stdin), F_SETOWN, getpid()) == -1) 22336304Skarels syslog(LOG_ERR, "fcntl F_SETOWN: %m"); 22436933Skarels #endif 22516760Slepreau dolog(&his_addr); 22616339Skarels /* 22716339Skarels * Set up default state 22816339Skarels */ 22916339Skarels data = -1; 23016339Skarels type = TYPE_A; 23116339Skarels form = FORM_N; 23216339Skarels stru = STRU_F; 23316339Skarels mode = MODE_S; 23426044Sminshall tmpline[0] = '\0'; 23526493Sminshall (void) gethostname(hostname, sizeof (hostname)); 23636276Sbostic reply(220, "%s FTP server (%s) ready.", hostname, version); 23736304Skarels (void) setjmp(errcatch); 23836304Skarels for (;;) 23926493Sminshall (void) yyparse(); 24036620Srick /* NOTREACHED */ 24110275Ssam } 24210419Ssam 24310275Ssam lostconn() 24410275Ssam { 24510275Ssam 24614089Ssam if (debug) 24726493Sminshall syslog(LOG_DEBUG, "lost connection"); 24814089Ssam dologout(-1); 24910275Ssam } 25010275Ssam 25135672Sbostic static char ttyline[20]; 25235672Sbostic 25336185Sbostic /* 25436185Sbostic * Helper function for sgetpwnam(). 25536185Sbostic */ 25636185Sbostic char * 25736185Sbostic sgetsave(s) 25836185Sbostic char *s; 25936185Sbostic { 26036185Sbostic char *malloc(); 26136185Sbostic char *new = malloc((unsigned) strlen(s) + 1); 26236933Skarels 26336185Sbostic if (new == NULL) { 26436620Srick perror_reply(421, "Local resource failure: malloc"); 26536185Sbostic dologout(1); 26636620Srick /* NOTREACHED */ 26736185Sbostic } 26836185Sbostic (void) strcpy(new, s); 26936185Sbostic return (new); 27036185Sbostic } 27136185Sbostic 27236185Sbostic /* 27336185Sbostic * Save the result of a getpwnam. Used for USER command, since 27436185Sbostic * the data returned must not be clobbered by any other command 27536185Sbostic * (e.g., globbing). 27636185Sbostic */ 27736185Sbostic struct passwd * 27836185Sbostic sgetpwnam(name) 27936185Sbostic char *name; 28036185Sbostic { 28136185Sbostic static struct passwd save; 28236185Sbostic register struct passwd *p; 28336185Sbostic char *sgetsave(); 28436185Sbostic 28536185Sbostic if ((p = getpwnam(name)) == NULL) 28636185Sbostic return (p); 28736185Sbostic if (save.pw_name) { 28836185Sbostic free(save.pw_name); 28936185Sbostic free(save.pw_passwd); 29036185Sbostic free(save.pw_gecos); 29136185Sbostic free(save.pw_dir); 29236185Sbostic free(save.pw_shell); 29336185Sbostic } 29436185Sbostic save = *p; 29536185Sbostic save.pw_name = sgetsave(p->pw_name); 29636185Sbostic save.pw_passwd = sgetsave(p->pw_passwd); 29736185Sbostic save.pw_gecos = sgetsave(p->pw_gecos); 29836185Sbostic save.pw_dir = sgetsave(p->pw_dir); 29936185Sbostic save.pw_shell = sgetsave(p->pw_shell); 30036185Sbostic return (&save); 30136185Sbostic } 30236185Sbostic 30336304Skarels int login_attempts; /* number of failed login attempts */ 30436304Skarels int askpasswd; /* had user command, ask for passwd */ 30536304Skarels 30636304Skarels /* 30736304Skarels * USER command. 30836304Skarels * Sets global passwd pointer pw if named account exists 30936304Skarels * and is acceptable; sets askpasswd if a PASS command is 31036304Skarels * expected. If logged in previously, need to reset state. 31140155Smckusick * If name is "ftp" or "anonymous", the name is not in /etc/ftpusers, 31240155Smckusick * and ftp account exists, set guest and pw, then just return. 31336304Skarels * If account doesn't exist, ask for passwd anyway. 31436304Skarels * Otherwise, check user requesting login privileges. 31536304Skarels * Disallow anyone who does not have a standard 31637459Skarels * shell as returned by getusershell(). 31737459Skarels * Disallow anyone mentioned in the file _PATH_FTPUSERS 31836304Skarels * to allow people such as root and uucp to be avoided. 31936304Skarels */ 32036304Skarels user(name) 32136304Skarels char *name; 32236304Skarels { 32336304Skarels register char *cp; 32436304Skarels char *shell; 325*40183Smckusick char *getusershell(); 32636304Skarels 32736304Skarels if (logged_in) { 32836304Skarels if (guest) { 32936304Skarels reply(530, "Can't change user from guest login."); 33036304Skarels return; 33136304Skarels } 33236304Skarels end_login(); 33336304Skarels } 33436304Skarels 33536304Skarels guest = 0; 33636304Skarels if (strcmp(name, "ftp") == 0 || strcmp(name, "anonymous") == 0) { 337*40183Smckusick if (checkuser("ftp") || checkuser("anonymous")) 33840155Smckusick reply(530, "User %s access denied.", name); 33940155Smckusick else if ((pw = sgetpwnam("ftp")) != NULL) { 34036304Skarels guest = 1; 34136304Skarels askpasswd = 1; 34236304Skarels reply(331, "Guest login ok, send ident as password."); 34336933Skarels } else 34436304Skarels reply(530, "User %s unknown.", name); 34536304Skarels return; 34636304Skarels } 34736304Skarels if (pw = sgetpwnam(name)) { 34836304Skarels if ((shell = pw->pw_shell) == NULL || *shell == 0) 34937459Skarels shell = _PATH_BSHELL; 35036304Skarels while ((cp = getusershell()) != NULL) 35136304Skarels if (strcmp(cp, shell) == 0) 35236304Skarels break; 35336304Skarels endusershell(); 354*40183Smckusick if (cp == NULL || checkuser(name)) { 35536304Skarels reply(530, "User %s access denied.", name); 35636933Skarels if (logging) 35736933Skarels syslog(LOG_NOTICE, 35836933Skarels "FTP LOGIN REFUSED FROM %s, %s", 35936933Skarels remotehost, name); 36036304Skarels pw = (struct passwd *) NULL; 36136304Skarels return; 36236304Skarels } 36336304Skarels } 36436304Skarels reply(331, "Password required for %s.", name); 36536304Skarels askpasswd = 1; 36636304Skarels /* 36736304Skarels * Delay before reading passwd after first failed 36836304Skarels * attempt to slow down passwd-guessing programs. 36936304Skarels */ 37036304Skarels if (login_attempts) 37136304Skarels sleep((unsigned) login_attempts); 37236304Skarels } 37336304Skarels 37436304Skarels /* 375*40183Smckusick * Check if a user is in the file _PATH_FTPUSERS 376*40183Smckusick */ 377*40183Smckusick checkuser(name) 378*40183Smckusick char *name; 379*40183Smckusick { 380*40183Smckusick FILE *fd; 381*40183Smckusick char line[BUFSIZ], *cp; 382*40183Smckusick 383*40183Smckusick if ((fd = fopen(_PATH_FTPUSERS, "r")) != NULL) { 384*40183Smckusick while (fgets(line, sizeof (line), fd) != NULL) { 385*40183Smckusick if ((cp = index(line, '\n')) != NULL) 386*40183Smckusick *cp = '\0'; 387*40183Smckusick if (strcmp(line, name) == 0) 388*40183Smckusick return (1); 389*40183Smckusick } 390*40183Smckusick (void) fclose(fd); 391*40183Smckusick } 392*40183Smckusick return (0); 393*40183Smckusick } 394*40183Smckusick 395*40183Smckusick /* 39636304Skarels * Terminate login as previous user, if any, resetting state; 39736304Skarels * used when USER command is given or login fails. 39836304Skarels */ 39936304Skarels end_login() 40036304Skarels { 40136304Skarels 40236304Skarels (void) seteuid((uid_t)0); 40336304Skarels if (logged_in) 40436304Skarels logwtmp(ttyline, "", ""); 40536304Skarels pw = NULL; 40636304Skarels logged_in = 0; 40736304Skarels guest = 0; 40836304Skarels } 40936304Skarels 41010275Ssam pass(passwd) 41110275Ssam char *passwd; 41210275Ssam { 41336304Skarels char *xpasswd, *salt; 41410275Ssam 41536304Skarels if (logged_in || askpasswd == 0) { 41610275Ssam reply(503, "Login with USER first."); 41710275Ssam return; 41810275Ssam } 41936304Skarels askpasswd = 0; 42010275Ssam if (!guest) { /* "ftp" is only account allowed no password */ 42136304Skarels if (pw == NULL) 42236304Skarels salt = "xx"; 42336304Skarels else 42436304Skarels salt = pw->pw_passwd; 42536304Skarels xpasswd = crypt(passwd, salt); 42616760Slepreau /* The strcmp does not catch null passwords! */ 42736304Skarels if (pw == NULL || *pw->pw_passwd == '\0' || 42836304Skarels strcmp(xpasswd, pw->pw_passwd)) { 42910275Ssam reply(530, "Login incorrect."); 43010275Ssam pw = NULL; 43136304Skarels if (login_attempts++ >= 5) { 43236933Skarels syslog(LOG_NOTICE, 43336304Skarels "repeated login failures from %s", 43436304Skarels remotehost); 43536304Skarels exit(0); 43636304Skarels } 43710275Ssam return; 43810275Ssam } 43910275Ssam } 44036304Skarels login_attempts = 0; /* this time successful */ 44136304Skarels (void) setegid((gid_t)pw->pw_gid); 44236304Skarels (void) initgroups(pw->pw_name, pw->pw_gid); 44316033Sralph 44436192Sbostic /* open wtmp before chroot */ 44536192Sbostic (void)sprintf(ttyline, "ftp%d", getpid()); 44636192Sbostic logwtmp(ttyline, pw->pw_name, remotehost); 44736192Sbostic logged_in = 1; 44836192Sbostic 44936446Sbostic if (guest) { 45036620Srick /* 45136933Skarels * We MUST do a chdir() after the chroot. Otherwise 45236933Skarels * the old current directory will be accessible as "." 45336933Skarels * outside the new root! 45436620Srick */ 45536620Srick if (chroot(pw->pw_dir) < 0 || chdir("/") < 0) { 45636446Sbostic reply(550, "Can't set guest privileges."); 45736446Sbostic goto bad; 45836446Sbostic } 45936933Skarels } else if (chdir(pw->pw_dir) < 0) { 46036620Srick if (chdir("/") < 0) { 46136446Sbostic reply(530, "User %s: can't change directory to %s.", 46236446Sbostic pw->pw_name, pw->pw_dir); 46336446Sbostic goto bad; 46436933Skarels } else 46536446Sbostic lreply(230, "No directory! Logging in with home=/"); 46636620Srick } 46736304Skarels if (seteuid((uid_t)pw->pw_uid) < 0) { 46836304Skarels reply(550, "Can't set uid."); 46936304Skarels goto bad; 47036304Skarels } 47136550Sbostic if (guest) { 47236192Sbostic reply(230, "Guest login ok, access restrictions apply."); 47336933Skarels #ifdef SETPROCTITLE 47436933Skarels sprintf(proctitle, "%s: anonymous/%.*s", remotehost, 47536933Skarels sizeof(proctitle) - sizeof(remotehost) - 47636933Skarels sizeof(": anonymous/"), passwd); 47736933Skarels setproctitle(proctitle); 47836933Skarels #endif /* SETPROCTITLE */ 47936933Skarels if (logging) 48036933Skarels syslog(LOG_INFO, "ANONYMOUS FTP LOGIN FROM %s, %s", 48136933Skarels remotehost, passwd); 48236933Skarels } else { 48310275Ssam reply(230, "User %s logged in.", pw->pw_name); 48436933Skarels #ifdef SETPROCTITLE 48536933Skarels sprintf(proctitle, "%s: %s", remotehost, pw->pw_name); 48636933Skarels setproctitle(proctitle); 48736933Skarels #endif /* SETPROCTITLE */ 48836933Skarels if (logging) 48936933Skarels syslog(LOG_INFO, "FTP LOGIN FROM %s, %s", 49036933Skarels remotehost, pw->pw_name); 49136550Sbostic } 49210303Ssam home = pw->pw_dir; /* home dir for globbing */ 49336933Skarels (void) umask(defumask); 49410303Ssam return; 49510303Ssam bad: 49636304Skarels /* Forget all about it... */ 49736304Skarels end_login(); 49810275Ssam } 49910275Ssam 50010275Ssam retrieve(cmd, name) 50110275Ssam char *cmd, *name; 50210275Ssam { 50310275Ssam FILE *fin, *dout; 50410275Ssam struct stat st; 50536620Srick int (*closefunc)(); 50610275Ssam 50736557Sbostic if (cmd == 0) { 50836446Sbostic fin = fopen(name, "r"), closefunc = fclose; 50936557Sbostic st.st_size = 0; 51036557Sbostic } else { 51110275Ssam char line[BUFSIZ]; 51210275Ssam 51326493Sminshall (void) sprintf(line, cmd, name), name = line; 51436304Skarels fin = ftpd_popen(line, "r"), closefunc = ftpd_pclose; 51536557Sbostic st.st_size = -1; 51636933Skarels st.st_blksize = BUFSIZ; 51710275Ssam } 51810275Ssam if (fin == NULL) { 51913152Ssam if (errno != 0) 52036304Skarels perror_reply(550, name); 52110275Ssam return; 52210275Ssam } 52310275Ssam if (cmd == 0 && 52436933Skarels (fstat(fileno(fin), &st) < 0 || (st.st_mode&S_IFMT) != S_IFREG)) { 52510275Ssam reply(550, "%s: not a plain file.", name); 52610275Ssam goto done; 52710275Ssam } 52837459Skarels if (restart_point) { 52937459Skarels if (type == TYPE_A) { 53037459Skarels register int i, n, c; 53137459Skarels 53237459Skarels n = restart_point; 53337459Skarels i = 0; 53437459Skarels while (i++ < n) { 53537459Skarels if ((c=getc(fin)) == EOF) { 53637459Skarels perror_reply(550, name); 53737459Skarels goto done; 53837459Skarels } 53937459Skarels if (c == '\n') 54037459Skarels i++; 54137459Skarels } 54237459Skarels } else if (lseek(fileno(fin), restart_point, L_SET) < 0) { 54337459Skarels perror_reply(550, name); 54437459Skarels goto done; 54537459Skarels } 54637459Skarels } 54710275Ssam dout = dataconn(name, st.st_size, "w"); 54810275Ssam if (dout == NULL) 54910275Ssam goto done; 55036620Srick send_data(fin, dout, st.st_blksize); 55126493Sminshall (void) fclose(dout); 55226044Sminshall data = -1; 55326044Sminshall pdata = -1; 55410275Ssam done: 55510275Ssam (*closefunc)(fin); 55610275Ssam } 55710275Ssam 55836304Skarels store(name, mode, unique) 55910275Ssam char *name, *mode; 56036304Skarels int unique; 56110275Ssam { 56210275Ssam FILE *fout, *din; 56336446Sbostic struct stat st; 56436620Srick int (*closefunc)(); 56536304Skarels char *gunique(); 56610275Ssam 56736446Sbostic if (unique && stat(name, &st) == 0 && 56836446Sbostic (name = gunique(name)) == NULL) 56936446Sbostic return; 57010303Ssam 57137459Skarels if (restart_point) 57237459Skarels mode = "r+w"; 57336620Srick fout = fopen(name, mode); 57436620Srick closefunc = fclose; 57510275Ssam if (fout == NULL) { 57636304Skarels perror_reply(553, name); 57710275Ssam return; 57810275Ssam } 57937459Skarels if (restart_point) { 58037459Skarels if (type == TYPE_A) { 58137459Skarels register int i, n, c; 58237459Skarels 58337459Skarels n = restart_point; 58437459Skarels i = 0; 58537459Skarels while (i++ < n) { 58637459Skarels if ((c=getc(fout)) == EOF) { 58737459Skarels perror_reply(550, name); 58837459Skarels goto done; 58937459Skarels } 59037459Skarels if (c == '\n') 59137459Skarels i++; 59237459Skarels } 59337459Skarels /* 59437459Skarels * We must do this seek to "current" position 59537459Skarels * because we are changing from reading to 59637459Skarels * writing. 59737459Skarels */ 59837459Skarels if (fseek(fout, 0L, L_INCR) < 0) { 59937459Skarels perror_reply(550, name); 60037459Skarels goto done; 60137459Skarels } 60237459Skarels } else if (lseek(fileno(fout), restart_point, L_SET) < 0) { 60337459Skarels perror_reply(550, name); 60437459Skarels goto done; 60537459Skarels } 60637459Skarels } 60736304Skarels din = dataconn(name, (off_t)-1, "r"); 60810275Ssam if (din == NULL) 60910275Ssam goto done; 61036620Srick if (receive_data(din, fout) == 0) { 61136620Srick if (unique) 61236304Skarels reply(226, "Transfer complete (unique file name:%s).", 61336933Skarels name); 61436304Skarels else 61536304Skarels reply(226, "Transfer complete."); 61626044Sminshall } 61726493Sminshall (void) fclose(din); 61826044Sminshall data = -1; 61926044Sminshall pdata = -1; 62010275Ssam done: 62110275Ssam (*closefunc)(fout); 62210275Ssam } 62310275Ssam 62410275Ssam FILE * 62510275Ssam getdatasock(mode) 62610275Ssam char *mode; 62710275Ssam { 62837459Skarels int s, on = 1, tries; 62910275Ssam 63010275Ssam if (data >= 0) 63110275Ssam return (fdopen(data, mode)); 63213247Ssam s = socket(AF_INET, SOCK_STREAM, 0); 63310602Ssam if (s < 0) 63410275Ssam return (NULL); 63536304Skarels (void) seteuid((uid_t)0); 63637459Skarels if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, 63737459Skarels (char *) &on, sizeof (on)) < 0) 63810602Ssam goto bad; 63913152Ssam /* anchor socket to avoid multi-homing problems */ 64013152Ssam data_source.sin_family = AF_INET; 64113152Ssam data_source.sin_addr = ctrl_addr.sin_addr; 64237459Skarels for (tries = 1; ; tries++) { 64337459Skarels if (bind(s, (struct sockaddr *)&data_source, 64437459Skarels sizeof (data_source)) >= 0) 64537459Skarels break; 64637459Skarels if (errno != EADDRINUSE || tries > 10) 64737459Skarels goto bad; 64837459Skarels sleep(tries); 64937459Skarels } 65036304Skarels (void) seteuid((uid_t)pw->pw_uid); 65110275Ssam return (fdopen(s, mode)); 65210602Ssam bad: 65336304Skarels (void) seteuid((uid_t)pw->pw_uid); 65426493Sminshall (void) close(s); 65510602Ssam return (NULL); 65610275Ssam } 65710275Ssam 65810275Ssam FILE * 65910275Ssam dataconn(name, size, mode) 66010275Ssam char *name; 66111653Ssam off_t size; 66210275Ssam char *mode; 66310275Ssam { 66410275Ssam char sizebuf[32]; 66510275Ssam FILE *file; 66611653Ssam int retry = 0; 66710275Ssam 66836933Skarels file_size = size; 66936933Skarels byte_count = 0; 67036304Skarels if (size != (off_t) -1) 67126493Sminshall (void) sprintf (sizebuf, " (%ld bytes)", size); 67210275Ssam else 67310275Ssam (void) strcpy(sizebuf, ""); 67436304Skarels if (pdata >= 0) { 67526044Sminshall struct sockaddr_in from; 67626044Sminshall int s, fromlen = sizeof(from); 67726044Sminshall 67836304Skarels s = accept(pdata, (struct sockaddr *)&from, &fromlen); 67926044Sminshall if (s < 0) { 68026044Sminshall reply(425, "Can't open data connection."); 68126044Sminshall (void) close(pdata); 68226044Sminshall pdata = -1; 68326044Sminshall return(NULL); 68426044Sminshall } 68526044Sminshall (void) close(pdata); 68626044Sminshall pdata = s; 68736235Skarels reply(150, "Opening %s mode data connection for %s%s.", 68836235Skarels type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf); 68926044Sminshall return(fdopen(pdata, mode)); 69026044Sminshall } 69110275Ssam if (data >= 0) { 69210275Ssam reply(125, "Using existing data connection for %s%s.", 69310275Ssam name, sizebuf); 69410321Ssam usedefault = 1; 69510275Ssam return (fdopen(data, mode)); 69610275Ssam } 69710566Ssam if (usedefault) 69810422Ssam data_dest = his_addr; 69910422Ssam usedefault = 1; 70010275Ssam file = getdatasock(mode); 70110275Ssam if (file == NULL) { 70210275Ssam reply(425, "Can't create data socket (%s,%d): %s.", 70313247Ssam inet_ntoa(data_source.sin_addr), 70410275Ssam ntohs(data_source.sin_port), 70536304Skarels errno < sys_nerr ? sys_errlist[errno] : "unknown error"); 70610275Ssam return (NULL); 70710275Ssam } 70810275Ssam data = fileno(file); 70936304Skarels while (connect(data, (struct sockaddr *)&data_dest, 71036304Skarels sizeof (data_dest)) < 0) { 71111653Ssam if (errno == EADDRINUSE && retry < swaitmax) { 71226493Sminshall sleep((unsigned) swaitint); 71311653Ssam retry += swaitint; 71411653Ssam continue; 71511653Ssam } 71636304Skarels perror_reply(425, "Can't build data connection"); 71710275Ssam (void) fclose(file); 71810275Ssam data = -1; 71910275Ssam return (NULL); 72010275Ssam } 72136235Skarels reply(150, "Opening %s mode data connection for %s%s.", 72236235Skarels type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf); 72310275Ssam return (file); 72410275Ssam } 72510275Ssam 72610275Ssam /* 72710275Ssam * Tranfer the contents of "instr" to 72810275Ssam * "outstr" peer using the appropriate 72936933Skarels * encapsulation of the data subject 73010275Ssam * to Mode, Structure, and Type. 73110275Ssam * 73210275Ssam * NB: Form isn't handled. 73310275Ssam */ 73436446Sbostic send_data(instr, outstr, blksize) 73510275Ssam FILE *instr, *outstr; 73636446Sbostic off_t blksize; 73710275Ssam { 73836446Sbostic register int c, cnt; 73936446Sbostic register char *buf; 74036446Sbostic int netfd, filefd; 74110275Ssam 74226044Sminshall transflag++; 74326044Sminshall if (setjmp(urgcatch)) { 74426044Sminshall transflag = 0; 74536620Srick return; 74626044Sminshall } 74710275Ssam switch (type) { 74810275Ssam 74910275Ssam case TYPE_A: 75010275Ssam while ((c = getc(instr)) != EOF) { 75136933Skarels byte_count++; 75211220Ssam if (c == '\n') { 75336933Skarels if (ferror(outstr)) 75436620Srick goto data_err; 75527750Sminshall (void) putc('\r', outstr); 75611220Ssam } 75727750Sminshall (void) putc(c, outstr); 75810275Ssam } 75936933Skarels fflush(outstr); 76026044Sminshall transflag = 0; 76136933Skarels if (ferror(instr)) 76236933Skarels goto file_err; 76336933Skarels if (ferror(outstr)) 76436620Srick goto data_err; 76536620Srick reply(226, "Transfer complete."); 76636620Srick return; 76736446Sbostic 76810275Ssam case TYPE_I: 76910275Ssam case TYPE_L: 77036446Sbostic if ((buf = malloc((u_int)blksize)) == NULL) { 77136446Sbostic transflag = 0; 77236933Skarels perror_reply(451, "Local resource failure: malloc"); 77336933Skarels return; 77436446Sbostic } 77510275Ssam netfd = fileno(outstr); 77610275Ssam filefd = fileno(instr); 77736933Skarels while ((cnt = read(filefd, buf, (u_int)blksize)) > 0 && 77836620Srick write(netfd, buf, cnt) == cnt) 77936933Skarels byte_count += cnt; 78026044Sminshall transflag = 0; 78136446Sbostic (void)free(buf); 78236933Skarels if (cnt != 0) { 78336933Skarels if (cnt < 0) 78436933Skarels goto file_err; 78536620Srick goto data_err; 78636933Skarels } 78736620Srick reply(226, "Transfer complete."); 78836620Srick return; 78936620Srick default: 79036620Srick transflag = 0; 79136620Srick reply(550, "Unimplemented TYPE %d in send_data", type); 79236620Srick return; 79310275Ssam } 79436620Srick 79536620Srick data_err: 79626044Sminshall transflag = 0; 79736933Skarels perror_reply(426, "Data connection"); 79836933Skarels return; 79936933Skarels 80036933Skarels file_err: 80136933Skarels transflag = 0; 80236933Skarels perror_reply(551, "Error on input file"); 80310275Ssam } 80410275Ssam 80510275Ssam /* 80610275Ssam * Transfer data from peer to 80710275Ssam * "outstr" using the appropriate 80810275Ssam * encapulation of the data subject 80910275Ssam * to Mode, Structure, and Type. 81010275Ssam * 81110275Ssam * N.B.: Form isn't handled. 81210275Ssam */ 81310275Ssam receive_data(instr, outstr) 81410275Ssam FILE *instr, *outstr; 81510275Ssam { 81610275Ssam register int c; 81738134Srick int cnt, bare_lfs = 0; 81810275Ssam char buf[BUFSIZ]; 81910275Ssam 82026044Sminshall transflag++; 82126044Sminshall if (setjmp(urgcatch)) { 82226044Sminshall transflag = 0; 82336933Skarels return (-1); 82426044Sminshall } 82510275Ssam switch (type) { 82610275Ssam 82710275Ssam case TYPE_I: 82810275Ssam case TYPE_L: 82926044Sminshall while ((cnt = read(fileno(instr), buf, sizeof buf)) > 0) { 83036620Srick if (write(fileno(outstr), buf, cnt) != cnt) 83136933Skarels goto file_err; 83236933Skarels byte_count += cnt; 83326044Sminshall } 83436933Skarels if (cnt < 0) 83536620Srick goto data_err; 83626044Sminshall transflag = 0; 83736933Skarels return (0); 83810275Ssam 83910275Ssam case TYPE_E: 84027106Smckusick reply(553, "TYPE E not implemented."); 84126044Sminshall transflag = 0; 84227106Smckusick return (-1); 84310275Ssam 84410275Ssam case TYPE_A: 84510275Ssam while ((c = getc(instr)) != EOF) { 84636933Skarels byte_count++; 84738134Srick if (c == '\n') 84838134Srick bare_lfs++; 84927750Sminshall while (c == '\r') { 85036933Skarels if (ferror(outstr)) 85136620Srick goto data_err; 85236933Skarels if ((c = getc(instr)) != '\n') { 85327750Sminshall (void) putc ('\r', outstr); 85436933Skarels if (c == '\0' || c == EOF) 85536933Skarels goto contin2; 85636933Skarels } 85710275Ssam } 85836933Skarels (void) putc(c, outstr); 85936933Skarels contin2: ; 86010275Ssam } 86136620Srick fflush(outstr); 86236933Skarels if (ferror(instr)) 86336620Srick goto data_err; 86436933Skarels if (ferror(outstr)) 86536933Skarels goto file_err; 86626044Sminshall transflag = 0; 86738134Srick if (bare_lfs) { 86838134Srick lreply(230, "WARNING! %d bare linefeeds received in ASCII mode", bare_lfs); 86938134Srick printf(" File may not have transferred correctly.\r\n"); 87038134Srick } 87110275Ssam return (0); 87236620Srick default: 87336620Srick reply(550, "Unimplemented TYPE %d in receive_data", type); 87436620Srick transflag = 0; 87536933Skarels return (-1); 87610275Ssam } 87736620Srick 87836620Srick data_err: 87926044Sminshall transflag = 0; 88036933Skarels perror_reply(426, "Data Connection"); 88136933Skarels return (-1); 88236933Skarels 88336933Skarels file_err: 88436933Skarels transflag = 0; 88536933Skarels perror_reply(452, "Error writing file"); 88636933Skarels return (-1); 88710275Ssam } 88810275Ssam 88936933Skarels statfilecmd(filename) 89036933Skarels char *filename; 89136933Skarels { 89236933Skarels char line[BUFSIZ]; 89336933Skarels FILE *fin; 89436933Skarels int c; 89536933Skarels 89636933Skarels (void) sprintf(line, "/bin/ls -lgA %s", filename); 89736933Skarels fin = ftpd_popen(line, "r"); 89836933Skarels lreply(211, "status of %s:", filename); 89936933Skarels while ((c = getc(fin)) != EOF) { 90036933Skarels if (c == '\n') { 90136933Skarels if (ferror(stdout)){ 90236933Skarels perror_reply(421, "control connection"); 90336933Skarels (void) ftpd_pclose(fin); 90436933Skarels dologout(1); 90536933Skarels /* NOTREACHED */ 90636933Skarels } 90736933Skarels if (ferror(fin)) { 90836933Skarels perror_reply(551, filename); 90936933Skarels (void) ftpd_pclose(fin); 91036933Skarels return; 91136933Skarels } 91236933Skarels (void) putc('\r', stdout); 91336933Skarels } 91436933Skarels (void) putc(c, stdout); 91536933Skarels } 91636933Skarels (void) ftpd_pclose(fin); 91736933Skarels reply(211, "End of Status"); 91836933Skarels } 91936933Skarels 92036933Skarels statcmd() 92136933Skarels { 92236933Skarels struct sockaddr_in *sin; 92336933Skarels u_char *a, *p; 92436933Skarels 92536933Skarels lreply(211, "%s FTP server status:", hostname, version); 92636933Skarels printf(" %s\r\n", version); 92736933Skarels printf(" Connected to %s", remotehost); 92838134Srick if (!isdigit(remotehost[0])) 92936933Skarels printf(" (%s)", inet_ntoa(his_addr.sin_addr)); 93036933Skarels printf("\r\n"); 93136933Skarels if (logged_in) { 93236933Skarels if (guest) 93336933Skarels printf(" Logged in anonymously\r\n"); 93436933Skarels else 93536933Skarels printf(" Logged in as %s\r\n", pw->pw_name); 93636933Skarels } else if (askpasswd) 93736933Skarels printf(" Waiting for password\r\n"); 93836933Skarels else 93936933Skarels printf(" Waiting for user name\r\n"); 94036933Skarels printf(" TYPE: %s", typenames[type]); 94136933Skarels if (type == TYPE_A || type == TYPE_E) 94236933Skarels printf(", FORM: %s", formnames[form]); 94336933Skarels if (type == TYPE_L) 94436933Skarels #if NBBY == 8 94536933Skarels printf(" %d", NBBY); 94636933Skarels #else 94736933Skarels printf(" %d", bytesize); /* need definition! */ 94836933Skarels #endif 94936933Skarels printf("; STRUcture: %s; transfer MODE: %s\r\n", 95036933Skarels strunames[stru], modenames[mode]); 95136933Skarels if (data != -1) 95236933Skarels printf(" Data connection open\r\n"); 95336933Skarels else if (pdata != -1) { 95436933Skarels printf(" in Passive mode"); 95536933Skarels sin = &pasv_addr; 95636933Skarels goto printaddr; 95736933Skarels } else if (usedefault == 0) { 95836933Skarels printf(" PORT"); 95936933Skarels sin = &data_dest; 96036933Skarels printaddr: 96136933Skarels a = (u_char *) &sin->sin_addr; 96236933Skarels p = (u_char *) &sin->sin_port; 96336933Skarels #define UC(b) (((int) b) & 0xff) 96436933Skarels printf(" (%d,%d,%d,%d,%d,%d)\r\n", UC(a[0]), 96536933Skarels UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1])); 96636933Skarels #undef UC 96736933Skarels } else 96836933Skarels printf(" No data connection\r\n"); 96936933Skarels reply(211, "End of status"); 97036933Skarels } 97136933Skarels 97210275Ssam fatal(s) 97310275Ssam char *s; 97410275Ssam { 97510275Ssam reply(451, "Error in server: %s\n", s); 97610275Ssam reply(221, "Closing connection due to server error."); 97713247Ssam dologout(0); 97836620Srick /* NOTREACHED */ 97910275Ssam } 98010275Ssam 98136446Sbostic /* VARARGS2 */ 98236446Sbostic reply(n, fmt, p0, p1, p2, p3, p4, p5) 98310275Ssam int n; 98436446Sbostic char *fmt; 98510275Ssam { 98610275Ssam printf("%d ", n); 98736446Sbostic printf(fmt, p0, p1, p2, p3, p4, p5); 98810275Ssam printf("\r\n"); 98936435Sbostic (void)fflush(stdout); 99010275Ssam if (debug) { 99126493Sminshall syslog(LOG_DEBUG, "<--- %d ", n); 99236446Sbostic syslog(LOG_DEBUG, fmt, p0, p1, p2, p3, p4, p5); 99310275Ssam } 99436620Srick } 99510275Ssam 99636446Sbostic /* VARARGS2 */ 99736446Sbostic lreply(n, fmt, p0, p1, p2, p3, p4, p5) 99810275Ssam int n; 99936446Sbostic char *fmt; 100010275Ssam { 100136446Sbostic printf("%d- ", n); 100236446Sbostic printf(fmt, p0, p1, p2, p3, p4, p5); 100336446Sbostic printf("\r\n"); 100436435Sbostic (void)fflush(stdout); 100536446Sbostic if (debug) { 100636446Sbostic syslog(LOG_DEBUG, "<--- %d- ", n); 100736446Sbostic syslog(LOG_DEBUG, fmt, p0, p1, p2, p3, p4, p5); 100836446Sbostic } 100910275Ssam } 101010275Ssam 101110275Ssam ack(s) 101210275Ssam char *s; 101310275Ssam { 101427106Smckusick reply(250, "%s command successful.", s); 101510275Ssam } 101610275Ssam 101710275Ssam nack(s) 101810275Ssam char *s; 101910275Ssam { 102010275Ssam reply(502, "%s command not implemented.", s); 102110275Ssam } 102210275Ssam 102336304Skarels /* ARGSUSED */ 102426493Sminshall yyerror(s) 102526493Sminshall char *s; 102610275Ssam { 102726044Sminshall char *cp; 102826044Sminshall 102936551Sbostic if (cp = index(cbuf,'\n')) 103036551Sbostic *cp = '\0'; 103136933Skarels reply(500, "'%s': command not understood.", cbuf); 103210275Ssam } 103310275Ssam 103410275Ssam delete(name) 103510275Ssam char *name; 103610275Ssam { 103710275Ssam struct stat st; 103810275Ssam 103910275Ssam if (stat(name, &st) < 0) { 104036304Skarels perror_reply(550, name); 104110275Ssam return; 104210275Ssam } 104310275Ssam if ((st.st_mode&S_IFMT) == S_IFDIR) { 104410275Ssam if (rmdir(name) < 0) { 104536304Skarels perror_reply(550, name); 104610275Ssam return; 104710275Ssam } 104810275Ssam goto done; 104910275Ssam } 105010275Ssam if (unlink(name) < 0) { 105136304Skarels perror_reply(550, name); 105210275Ssam return; 105310275Ssam } 105410275Ssam done: 105510275Ssam ack("DELE"); 105610275Ssam } 105710275Ssam 105810275Ssam cwd(path) 105910275Ssam char *path; 106010275Ssam { 106136620Srick if (chdir(path) < 0) 106236304Skarels perror_reply(550, path); 106336620Srick else 106436620Srick ack("CWD"); 106510275Ssam } 106610275Ssam 106710303Ssam makedir(name) 106810275Ssam char *name; 106910275Ssam { 107036276Sbostic if (mkdir(name, 0777) < 0) 107136304Skarels perror_reply(550, name); 107236276Sbostic else 107336276Sbostic reply(257, "MKD command successful."); 107410275Ssam } 107510275Ssam 107610303Ssam removedir(name) 107710275Ssam char *name; 107810275Ssam { 107936620Srick if (rmdir(name) < 0) 108036304Skarels perror_reply(550, name); 108136620Srick else 108236620Srick ack("RMD"); 108310275Ssam } 108410275Ssam 108510303Ssam pwd() 108610275Ssam { 108710303Ssam char path[MAXPATHLEN + 1]; 108836304Skarels extern char *getwd(); 108910275Ssam 109036620Srick if (getwd(path) == (char *)NULL) 109127106Smckusick reply(550, "%s.", path); 109236620Srick else 109336620Srick reply(257, "\"%s\" is current directory.", path); 109410275Ssam } 109510275Ssam 109610275Ssam char * 109710275Ssam renamefrom(name) 109810275Ssam char *name; 109910275Ssam { 110010275Ssam struct stat st; 110110275Ssam 110210275Ssam if (stat(name, &st) < 0) { 110336304Skarels perror_reply(550, name); 110410275Ssam return ((char *)0); 110510275Ssam } 110610303Ssam reply(350, "File exists, ready for destination name"); 110710275Ssam return (name); 110810275Ssam } 110910275Ssam 111010275Ssam renamecmd(from, to) 111110275Ssam char *from, *to; 111210275Ssam { 111336620Srick if (rename(from, to) < 0) 111436304Skarels perror_reply(550, "rename"); 111536620Srick else 111636620Srick ack("RNTO"); 111710275Ssam } 111810275Ssam 111910275Ssam dolog(sin) 112010275Ssam struct sockaddr_in *sin; 112110275Ssam { 112236304Skarels struct hostent *hp = gethostbyaddr((char *)&sin->sin_addr, 112310275Ssam sizeof (struct in_addr), AF_INET); 112436304Skarels time_t t, time(); 112526493Sminshall extern char *ctime(); 112610275Ssam 112736304Skarels if (hp) 112826493Sminshall (void) strncpy(remotehost, hp->h_name, sizeof (remotehost)); 112936304Skarels else 113026493Sminshall (void) strncpy(remotehost, inet_ntoa(sin->sin_addr), 113113247Ssam sizeof (remotehost)); 113236620Srick #ifdef SETPROCTITLE 113336933Skarels sprintf(proctitle, "%s: connected", remotehost); 113436933Skarels setproctitle(proctitle); 113536620Srick #endif /* SETPROCTITLE */ 113636933Skarels 113736933Skarels if (logging) { 113836933Skarels t = time((time_t *) 0); 113936933Skarels syslog(LOG_INFO, "connection from %s at %s", 114036933Skarels remotehost, ctime(&t)); 114136933Skarels } 114210275Ssam } 114310695Ssam 114410695Ssam /* 114513247Ssam * Record logout in wtmp file 114613247Ssam * and exit with supplied status. 114713247Ssam */ 114813247Ssam dologout(status) 114913247Ssam int status; 115013247Ssam { 115117580Ssam if (logged_in) { 115236304Skarels (void) seteuid((uid_t)0); 115335672Sbostic logwtmp(ttyline, "", ""); 115413247Ssam } 115514436Ssam /* beware of flushing buffers after a SIGPIPE */ 115614436Ssam _exit(status); 115713247Ssam } 115813247Ssam 115926044Sminshall myoob() 116026044Sminshall { 116127750Sminshall char *cp; 116226044Sminshall 116327750Sminshall /* only process if transfer occurring */ 116436304Skarels if (!transflag) 116526044Sminshall return; 116627750Sminshall cp = tmpline; 116727750Sminshall if (getline(cp, 7, stdin) == NULL) { 116836304Skarels reply(221, "You could at least say goodbye."); 116927750Sminshall dologout(0); 117026044Sminshall } 117126044Sminshall upper(cp); 117236933Skarels if (strcmp(cp, "ABOR\r\n") == 0) { 117336933Skarels tmpline[0] = '\0'; 117436933Skarels reply(426, "Transfer aborted. Data connection closed."); 117536933Skarels reply(226, "Abort successful"); 117636933Skarels longjmp(urgcatch, 1); 117736933Skarels } 117836933Skarels if (strcmp(cp, "STAT\r\n") == 0) { 117936933Skarels if (file_size != (off_t) -1) 118036933Skarels reply(213, "Status: %lu of %lu bytes transferred", 118136933Skarels byte_count, file_size); 118236933Skarels else 118336933Skarels reply(213, "Status: %lu bytes transferred", byte_count); 118436933Skarels } 118526044Sminshall } 118626044Sminshall 118727106Smckusick /* 118836620Srick * Note: a response of 425 is not mentioned as a possible response to 118936620Srick * the PASV command in RFC959. However, it has been blessed as 119036620Srick * a legitimate response by Jon Postel in a telephone conversation 119136620Srick * with Rick Adams on 25 Jan 89. 119227106Smckusick */ 119326044Sminshall passive() 119426044Sminshall { 119526044Sminshall int len; 119626044Sminshall register char *p, *a; 119726044Sminshall 119826044Sminshall pdata = socket(AF_INET, SOCK_STREAM, 0); 119926044Sminshall if (pdata < 0) { 120036620Srick perror_reply(425, "Can't open passive connection"); 120126044Sminshall return; 120226044Sminshall } 120336933Skarels pasv_addr = ctrl_addr; 120436933Skarels pasv_addr.sin_port = 0; 120536304Skarels (void) seteuid((uid_t)0); 120636933Skarels if (bind(pdata, (struct sockaddr *)&pasv_addr, sizeof(pasv_addr)) < 0) { 120736304Skarels (void) seteuid((uid_t)pw->pw_uid); 120836620Srick goto pasv_error; 120926044Sminshall } 121036304Skarels (void) seteuid((uid_t)pw->pw_uid); 121136933Skarels len = sizeof(pasv_addr); 121236933Skarels if (getsockname(pdata, (struct sockaddr *) &pasv_addr, &len) < 0) 121336620Srick goto pasv_error; 121436620Srick if (listen(pdata, 1) < 0) 121536620Srick goto pasv_error; 121636933Skarels a = (char *) &pasv_addr.sin_addr; 121736933Skarels p = (char *) &pasv_addr.sin_port; 121826044Sminshall 121926044Sminshall #define UC(b) (((int) b) & 0xff) 122026044Sminshall 122126044Sminshall reply(227, "Entering Passive Mode (%d,%d,%d,%d,%d,%d)", UC(a[0]), 122226044Sminshall UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1])); 122336620Srick return; 122436620Srick 122536620Srick pasv_error: 122636620Srick (void) close(pdata); 122736620Srick pdata = -1; 122836620Srick perror_reply(425, "Can't open passive connection"); 122936620Srick return; 123026044Sminshall } 123126044Sminshall 123236304Skarels /* 123336304Skarels * Generate unique name for file with basename "local". 123436304Skarels * The file named "local" is already known to exist. 123536304Skarels * Generates failure reply on error. 123636304Skarels */ 123726044Sminshall char * 123826044Sminshall gunique(local) 123926044Sminshall char *local; 124026044Sminshall { 124126044Sminshall static char new[MAXPATHLEN]; 124236304Skarels struct stat st; 124326044Sminshall char *cp = rindex(local, '/'); 124436933Skarels int count = 0; 124526044Sminshall 124636304Skarels if (cp) 124726044Sminshall *cp = '\0'; 124836620Srick if (stat(cp ? local : ".", &st) < 0) { 124936933Skarels perror_reply(553, cp ? local : "."); 125026044Sminshall return((char *) 0); 125126044Sminshall } 125236620Srick if (cp) 125336620Srick *cp = '/'; 125426044Sminshall (void) strcpy(new, local); 125526044Sminshall cp = new + strlen(new); 125626044Sminshall *cp++ = '.'; 125736304Skarels for (count = 1; count < 100; count++) { 125836304Skarels (void) sprintf(cp, "%d", count); 125936304Skarels if (stat(new, &st) < 0) 126036304Skarels return(new); 126126044Sminshall } 126236304Skarels reply(452, "Unique file name cannot be created."); 126336304Skarels return((char *) 0); 126426044Sminshall } 126536304Skarels 126636304Skarels /* 126736304Skarels * Format and send reply containing system error number. 126836304Skarels */ 126936304Skarels perror_reply(code, string) 127036304Skarels int code; 127136304Skarels char *string; 127236304Skarels { 127336304Skarels if (errno < sys_nerr) 127436304Skarels reply(code, "%s: %s.", string, sys_errlist[errno]); 127536304Skarels else 127636304Skarels reply(code, "%s: unknown error %d.", string, errno); 127736304Skarels } 127836620Srick 127936620Srick static char *onefile[] = { 128036620Srick "", 128136620Srick 0 128236620Srick }; 128336620Srick 128436620Srick send_file_list(whichfiles) 128536620Srick char *whichfiles; 128636620Srick { 128736620Srick struct stat st; 128836620Srick DIR *dirp = NULL; 128936620Srick struct direct *dir; 129036620Srick FILE *dout = NULL; 129136620Srick register char **dirlist, *dirname; 129237459Skarels int simple = 0; 129336620Srick char *strpbrk(); 129436620Srick 129536620Srick if (strpbrk(whichfiles, "~{[*?") != NULL) { 129636620Srick extern char **glob(), *globerr; 129736933Skarels 129836620Srick globerr = NULL; 129936620Srick dirlist = glob(whichfiles); 130036620Srick if (globerr != NULL) { 130136620Srick reply(550, globerr); 130236620Srick return; 130336620Srick } else if (dirlist == NULL) { 130436620Srick errno = ENOENT; 130536620Srick perror_reply(550, whichfiles); 130636620Srick return; 130736620Srick } 130836620Srick } else { 130936620Srick onefile[0] = whichfiles; 131036620Srick dirlist = onefile; 131137459Skarels simple = 1; 131236620Srick } 131336933Skarels 131436933Skarels if (setjmp(urgcatch)) { 131536933Skarels transflag = 0; 131636933Skarels return; 131736933Skarels } 131836620Srick while (dirname = *dirlist++) { 131936620Srick if (stat(dirname, &st) < 0) { 132036933Skarels /* 132136933Skarels * If user typed "ls -l", etc, and the client 132236933Skarels * used NLST, do what the user meant. 132336933Skarels */ 132436933Skarels if (dirname[0] == '-' && *dirlist == NULL && 132536933Skarels transflag == 0) { 132636933Skarels retrieve("/bin/ls %s", dirname); 132736933Skarels return; 132836933Skarels } 132936620Srick perror_reply(550, whichfiles); 133036620Srick if (dout != NULL) { 133136620Srick (void) fclose(dout); 133236933Skarels transflag = 0; 133336620Srick data = -1; 133436620Srick pdata = -1; 133536620Srick } 133636620Srick return; 133736620Srick } 133836933Skarels 133936620Srick if ((st.st_mode&S_IFMT) == S_IFREG) { 134036620Srick if (dout == NULL) { 134137459Skarels dout = dataconn("file list", (off_t)-1, "w"); 134236620Srick if (dout == NULL) 134336620Srick return; 134436933Skarels transflag++; 134536620Srick } 134638158Srick fprintf(dout, "%s%s\n", dirname, 134738158Srick type == TYPE_A ? "\r" : ""); 134836933Skarels byte_count += strlen(dirname) + 1; 134936620Srick continue; 135036933Skarels } else if ((st.st_mode&S_IFMT) != S_IFDIR) 135136620Srick continue; 135236620Srick 135336620Srick if ((dirp = opendir(dirname)) == NULL) 135436620Srick continue; 135536620Srick 135636620Srick while ((dir = readdir(dirp)) != NULL) { 135736933Skarels char nbuf[MAXPATHLEN]; 135836620Srick 135936620Srick if (dir->d_name[0] == '.' && dir->d_namlen == 1) 136036620Srick continue; 136136933Skarels if (dir->d_name[0] == '.' && dir->d_name[1] == '.' && 136236933Skarels dir->d_namlen == 2) 136336620Srick continue; 136436620Srick 136536933Skarels sprintf(nbuf, "%s/%s", dirname, dir->d_name); 136636933Skarels 136736620Srick /* 136836933Skarels * We have to do a stat to insure it's 136936933Skarels * not a directory or special file. 137036620Srick */ 137137459Skarels if (simple || (stat(nbuf, &st) == 0 && 137237459Skarels (st.st_mode&S_IFMT) == S_IFREG)) { 137336620Srick if (dout == NULL) { 137437459Skarels dout = dataconn("file list", (off_t)-1, 137536620Srick "w"); 137636620Srick if (dout == NULL) 137736620Srick return; 137836933Skarels transflag++; 137936620Srick } 138036620Srick if (nbuf[0] == '.' && nbuf[1] == '/') 138138158Srick fprintf(dout, "%s%s\n", &nbuf[2], 138238158Srick type == TYPE_A ? "\r" : ""); 138336620Srick else 138438158Srick fprintf(dout, "%s%s\n", nbuf, 138538158Srick type == TYPE_A ? "\r" : ""); 138636933Skarels byte_count += strlen(nbuf) + 1; 138736620Srick } 138836620Srick } 138936620Srick (void) closedir(dirp); 139036620Srick } 139136620Srick 139236933Skarels if (dout == NULL) 139336933Skarels reply(550, "No files found."); 139436933Skarels else if (ferror(dout) != 0) 139536933Skarels perror_reply(550, "Data connection"); 139636933Skarels else 139736620Srick reply(226, "Transfer complete."); 139836620Srick 139936933Skarels transflag = 0; 140036933Skarels if (dout != NULL) 140136620Srick (void) fclose(dout); 140236620Srick data = -1; 140336620Srick pdata = -1; 140436620Srick } 140536620Srick 140636620Srick #ifdef SETPROCTITLE 140736620Srick /* 140836620Srick * clobber argv so ps will show what we're doing. 140936620Srick * (stolen from sendmail) 141036620Srick * warning, since this is usually started from inetd.conf, it 141136620Srick * often doesn't have much of an environment or arglist to overwrite. 141236620Srick */ 141336620Srick 141436620Srick /*VARARGS1*/ 141536620Srick setproctitle(fmt, a, b, c) 141636620Srick char *fmt; 141736620Srick { 141836620Srick register char *p, *bp, ch; 141936620Srick register int i; 142036620Srick char buf[BUFSIZ]; 142136620Srick 142236620Srick (void) sprintf(buf, fmt, a, b, c); 142336620Srick 142436620Srick /* make ps print our process name */ 142536620Srick p = Argv[0]; 142636620Srick *p++ = '-'; 142736620Srick 142836620Srick i = strlen(buf); 142936620Srick if (i > LastArgv - p - 2) { 143036620Srick i = LastArgv - p - 2; 143136620Srick buf[i] = '\0'; 143236620Srick } 143336620Srick bp = buf; 143436620Srick while (ch = *bp++) 143536620Srick if (ch != '\n' && ch != '\r') 143636620Srick *p++ = ch; 143736620Srick while (p < LastArgv) 143836620Srick *p++ = ' '; 143936620Srick } 144036620Srick #endif /* SETPROCTITLE */ 1441