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*40155Smckusick static char sccsid[] = "@(#)ftpd.c 5.31 (Berkeley) 02/19/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. 311*40155Smckusick * If name is "ftp" or "anonymous", the name is not in /etc/ftpusers, 312*40155Smckusick * 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 FILE *fd; 32536304Skarels char *shell; 32636551Sbostic char line[BUFSIZ], *getusershell(); 32736304Skarels 32836304Skarels if (logged_in) { 32936304Skarels if (guest) { 33036304Skarels reply(530, "Can't change user from guest login."); 33136304Skarels return; 33236304Skarels } 33336304Skarels end_login(); 33436304Skarels } 33536304Skarels 33636304Skarels guest = 0; 33736304Skarels if (strcmp(name, "ftp") == 0 || strcmp(name, "anonymous") == 0) { 338*40155Smckusick if (!checkuser("ftp") || !checkuser("anonymous")) 339*40155Smckusick reply(530, "User %s access denied.", name); 340*40155Smckusick else if ((pw = sgetpwnam("ftp")) != NULL) { 34136304Skarels guest = 1; 34236304Skarels askpasswd = 1; 34336304Skarels reply(331, "Guest login ok, send ident as password."); 34436933Skarels } else 34536304Skarels reply(530, "User %s unknown.", name); 34636304Skarels return; 34736304Skarels } 34836304Skarels if (pw = sgetpwnam(name)) { 34936304Skarels if ((shell = pw->pw_shell) == NULL || *shell == 0) 35037459Skarels shell = _PATH_BSHELL; 35136304Skarels while ((cp = getusershell()) != NULL) 35236304Skarels if (strcmp(cp, shell) == 0) 35336304Skarels break; 35436304Skarels endusershell(); 35536304Skarels if (cp == NULL) { 35636304Skarels reply(530, "User %s access denied.", name); 35736933Skarels if (logging) 35836933Skarels syslog(LOG_NOTICE, 35936933Skarels "FTP LOGIN REFUSED FROM %s, %s", 36036933Skarels remotehost, name); 36136304Skarels pw = (struct passwd *) NULL; 36236304Skarels return; 36336304Skarels } 36437459Skarels if ((fd = fopen(_PATH_FTPUSERS, "r")) != NULL) { 36536304Skarels while (fgets(line, sizeof (line), fd) != NULL) { 36636304Skarels if ((cp = index(line, '\n')) != NULL) 36736304Skarels *cp = '\0'; 36836304Skarels if (strcmp(line, name) == 0) { 36936304Skarels reply(530, "User %s access denied.", name); 37036933Skarels if (logging) 37136933Skarels syslog(LOG_NOTICE, 37236933Skarels "FTP LOGIN REFUSED FROM %s, %s", 37336933Skarels remotehost, name); 37436304Skarels pw = (struct passwd *) NULL; 37538134Srick (void) fclose(fd); 37636304Skarels return; 37736304Skarels } 37836304Skarels } 37938134Srick (void) fclose(fd); 38036304Skarels } 38136304Skarels } 38236304Skarels reply(331, "Password required for %s.", name); 38336304Skarels askpasswd = 1; 38436304Skarels /* 38536304Skarels * Delay before reading passwd after first failed 38636304Skarels * attempt to slow down passwd-guessing programs. 38736304Skarels */ 38836304Skarels if (login_attempts) 38936304Skarels sleep((unsigned) login_attempts); 39036304Skarels } 39136304Skarels 39236304Skarels /* 39336304Skarels * Terminate login as previous user, if any, resetting state; 39436304Skarels * used when USER command is given or login fails. 39536304Skarels */ 39636304Skarels end_login() 39736304Skarels { 39836304Skarels 39936304Skarels (void) seteuid((uid_t)0); 40036304Skarels if (logged_in) 40136304Skarels logwtmp(ttyline, "", ""); 40236304Skarels pw = NULL; 40336304Skarels logged_in = 0; 40436304Skarels guest = 0; 40536304Skarels } 40636304Skarels 40710275Ssam pass(passwd) 40810275Ssam char *passwd; 40910275Ssam { 41036304Skarels char *xpasswd, *salt; 41110275Ssam 41236304Skarels if (logged_in || askpasswd == 0) { 41310275Ssam reply(503, "Login with USER first."); 41410275Ssam return; 41510275Ssam } 41636304Skarels askpasswd = 0; 41710275Ssam if (!guest) { /* "ftp" is only account allowed no password */ 41836304Skarels if (pw == NULL) 41936304Skarels salt = "xx"; 42036304Skarels else 42136304Skarels salt = pw->pw_passwd; 42236304Skarels xpasswd = crypt(passwd, salt); 42316760Slepreau /* The strcmp does not catch null passwords! */ 42436304Skarels if (pw == NULL || *pw->pw_passwd == '\0' || 42536304Skarels strcmp(xpasswd, pw->pw_passwd)) { 42610275Ssam reply(530, "Login incorrect."); 42710275Ssam pw = NULL; 42836304Skarels if (login_attempts++ >= 5) { 42936933Skarels syslog(LOG_NOTICE, 43036304Skarels "repeated login failures from %s", 43136304Skarels remotehost); 43236304Skarels exit(0); 43336304Skarels } 43410275Ssam return; 43510275Ssam } 43610275Ssam } 43736304Skarels login_attempts = 0; /* this time successful */ 43836304Skarels (void) setegid((gid_t)pw->pw_gid); 43936304Skarels (void) initgroups(pw->pw_name, pw->pw_gid); 44016033Sralph 44136192Sbostic /* open wtmp before chroot */ 44236192Sbostic (void)sprintf(ttyline, "ftp%d", getpid()); 44336192Sbostic logwtmp(ttyline, pw->pw_name, remotehost); 44436192Sbostic logged_in = 1; 44536192Sbostic 44636446Sbostic if (guest) { 44736620Srick /* 44836933Skarels * We MUST do a chdir() after the chroot. Otherwise 44936933Skarels * the old current directory will be accessible as "." 45036933Skarels * outside the new root! 45136620Srick */ 45236620Srick if (chroot(pw->pw_dir) < 0 || chdir("/") < 0) { 45336446Sbostic reply(550, "Can't set guest privileges."); 45436446Sbostic goto bad; 45536446Sbostic } 45636933Skarels } else if (chdir(pw->pw_dir) < 0) { 45736620Srick if (chdir("/") < 0) { 45836446Sbostic reply(530, "User %s: can't change directory to %s.", 45936446Sbostic pw->pw_name, pw->pw_dir); 46036446Sbostic goto bad; 46136933Skarels } else 46236446Sbostic lreply(230, "No directory! Logging in with home=/"); 46336620Srick } 46436304Skarels if (seteuid((uid_t)pw->pw_uid) < 0) { 46536304Skarels reply(550, "Can't set uid."); 46636304Skarels goto bad; 46736304Skarels } 46836550Sbostic if (guest) { 46936192Sbostic reply(230, "Guest login ok, access restrictions apply."); 47036933Skarels #ifdef SETPROCTITLE 47136933Skarels sprintf(proctitle, "%s: anonymous/%.*s", remotehost, 47236933Skarels sizeof(proctitle) - sizeof(remotehost) - 47336933Skarels sizeof(": anonymous/"), passwd); 47436933Skarels setproctitle(proctitle); 47536933Skarels #endif /* SETPROCTITLE */ 47636933Skarels if (logging) 47736933Skarels syslog(LOG_INFO, "ANONYMOUS FTP LOGIN FROM %s, %s", 47836933Skarels remotehost, passwd); 47936933Skarels } else { 48010275Ssam reply(230, "User %s logged in.", pw->pw_name); 48136933Skarels #ifdef SETPROCTITLE 48236933Skarels sprintf(proctitle, "%s: %s", remotehost, pw->pw_name); 48336933Skarels setproctitle(proctitle); 48436933Skarels #endif /* SETPROCTITLE */ 48536933Skarels if (logging) 48636933Skarels syslog(LOG_INFO, "FTP LOGIN FROM %s, %s", 48736933Skarels remotehost, pw->pw_name); 48836550Sbostic } 48910303Ssam home = pw->pw_dir; /* home dir for globbing */ 49036933Skarels (void) umask(defumask); 49110303Ssam return; 49210303Ssam bad: 49336304Skarels /* Forget all about it... */ 49436304Skarels end_login(); 49510275Ssam } 49610275Ssam 49710275Ssam retrieve(cmd, name) 49810275Ssam char *cmd, *name; 49910275Ssam { 50010275Ssam FILE *fin, *dout; 50110275Ssam struct stat st; 50236620Srick int (*closefunc)(); 50310275Ssam 50436557Sbostic if (cmd == 0) { 50536446Sbostic fin = fopen(name, "r"), closefunc = fclose; 50636557Sbostic st.st_size = 0; 50736557Sbostic } else { 50810275Ssam char line[BUFSIZ]; 50910275Ssam 51026493Sminshall (void) sprintf(line, cmd, name), name = line; 51136304Skarels fin = ftpd_popen(line, "r"), closefunc = ftpd_pclose; 51236557Sbostic st.st_size = -1; 51336933Skarels st.st_blksize = BUFSIZ; 51410275Ssam } 51510275Ssam if (fin == NULL) { 51613152Ssam if (errno != 0) 51736304Skarels perror_reply(550, name); 51810275Ssam return; 51910275Ssam } 52010275Ssam if (cmd == 0 && 52136933Skarels (fstat(fileno(fin), &st) < 0 || (st.st_mode&S_IFMT) != S_IFREG)) { 52210275Ssam reply(550, "%s: not a plain file.", name); 52310275Ssam goto done; 52410275Ssam } 52537459Skarels if (restart_point) { 52637459Skarels if (type == TYPE_A) { 52737459Skarels register int i, n, c; 52837459Skarels 52937459Skarels n = restart_point; 53037459Skarels i = 0; 53137459Skarels while (i++ < n) { 53237459Skarels if ((c=getc(fin)) == EOF) { 53337459Skarels perror_reply(550, name); 53437459Skarels goto done; 53537459Skarels } 53637459Skarels if (c == '\n') 53737459Skarels i++; 53837459Skarels } 53937459Skarels } else if (lseek(fileno(fin), restart_point, L_SET) < 0) { 54037459Skarels perror_reply(550, name); 54137459Skarels goto done; 54237459Skarels } 54337459Skarels } 54410275Ssam dout = dataconn(name, st.st_size, "w"); 54510275Ssam if (dout == NULL) 54610275Ssam goto done; 54736620Srick send_data(fin, dout, st.st_blksize); 54826493Sminshall (void) fclose(dout); 54926044Sminshall data = -1; 55026044Sminshall pdata = -1; 55110275Ssam done: 55210275Ssam (*closefunc)(fin); 55310275Ssam } 55410275Ssam 55536304Skarels store(name, mode, unique) 55610275Ssam char *name, *mode; 55736304Skarels int unique; 55810275Ssam { 55910275Ssam FILE *fout, *din; 56036446Sbostic struct stat st; 56136620Srick int (*closefunc)(); 56236304Skarels char *gunique(); 56310275Ssam 56436446Sbostic if (unique && stat(name, &st) == 0 && 56536446Sbostic (name = gunique(name)) == NULL) 56636446Sbostic return; 56710303Ssam 56837459Skarels if (restart_point) 56937459Skarels mode = "r+w"; 57036620Srick fout = fopen(name, mode); 57136620Srick closefunc = fclose; 57210275Ssam if (fout == NULL) { 57336304Skarels perror_reply(553, name); 57410275Ssam return; 57510275Ssam } 57637459Skarels if (restart_point) { 57737459Skarels if (type == TYPE_A) { 57837459Skarels register int i, n, c; 57937459Skarels 58037459Skarels n = restart_point; 58137459Skarels i = 0; 58237459Skarels while (i++ < n) { 58337459Skarels if ((c=getc(fout)) == EOF) { 58437459Skarels perror_reply(550, name); 58537459Skarels goto done; 58637459Skarels } 58737459Skarels if (c == '\n') 58837459Skarels i++; 58937459Skarels } 59037459Skarels /* 59137459Skarels * We must do this seek to "current" position 59237459Skarels * because we are changing from reading to 59337459Skarels * writing. 59437459Skarels */ 59537459Skarels if (fseek(fout, 0L, L_INCR) < 0) { 59637459Skarels perror_reply(550, name); 59737459Skarels goto done; 59837459Skarels } 59937459Skarels } else if (lseek(fileno(fout), restart_point, L_SET) < 0) { 60037459Skarels perror_reply(550, name); 60137459Skarels goto done; 60237459Skarels } 60337459Skarels } 60436304Skarels din = dataconn(name, (off_t)-1, "r"); 60510275Ssam if (din == NULL) 60610275Ssam goto done; 60736620Srick if (receive_data(din, fout) == 0) { 60836620Srick if (unique) 60936304Skarels reply(226, "Transfer complete (unique file name:%s).", 61036933Skarels name); 61136304Skarels else 61236304Skarels reply(226, "Transfer complete."); 61326044Sminshall } 61426493Sminshall (void) fclose(din); 61526044Sminshall data = -1; 61626044Sminshall pdata = -1; 61710275Ssam done: 61810275Ssam (*closefunc)(fout); 61910275Ssam } 62010275Ssam 62110275Ssam FILE * 62210275Ssam getdatasock(mode) 62310275Ssam char *mode; 62410275Ssam { 62537459Skarels int s, on = 1, tries; 62610275Ssam 62710275Ssam if (data >= 0) 62810275Ssam return (fdopen(data, mode)); 62913247Ssam s = socket(AF_INET, SOCK_STREAM, 0); 63010602Ssam if (s < 0) 63110275Ssam return (NULL); 63236304Skarels (void) seteuid((uid_t)0); 63337459Skarels if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, 63437459Skarels (char *) &on, sizeof (on)) < 0) 63510602Ssam goto bad; 63613152Ssam /* anchor socket to avoid multi-homing problems */ 63713152Ssam data_source.sin_family = AF_INET; 63813152Ssam data_source.sin_addr = ctrl_addr.sin_addr; 63937459Skarels for (tries = 1; ; tries++) { 64037459Skarels if (bind(s, (struct sockaddr *)&data_source, 64137459Skarels sizeof (data_source)) >= 0) 64237459Skarels break; 64337459Skarels if (errno != EADDRINUSE || tries > 10) 64437459Skarels goto bad; 64537459Skarels sleep(tries); 64637459Skarels } 64736304Skarels (void) seteuid((uid_t)pw->pw_uid); 64810275Ssam return (fdopen(s, mode)); 64910602Ssam bad: 65036304Skarels (void) seteuid((uid_t)pw->pw_uid); 65126493Sminshall (void) close(s); 65210602Ssam return (NULL); 65310275Ssam } 65410275Ssam 65510275Ssam FILE * 65610275Ssam dataconn(name, size, mode) 65710275Ssam char *name; 65811653Ssam off_t size; 65910275Ssam char *mode; 66010275Ssam { 66110275Ssam char sizebuf[32]; 66210275Ssam FILE *file; 66311653Ssam int retry = 0; 66410275Ssam 66536933Skarels file_size = size; 66636933Skarels byte_count = 0; 66736304Skarels if (size != (off_t) -1) 66826493Sminshall (void) sprintf (sizebuf, " (%ld bytes)", size); 66910275Ssam else 67010275Ssam (void) strcpy(sizebuf, ""); 67136304Skarels if (pdata >= 0) { 67226044Sminshall struct sockaddr_in from; 67326044Sminshall int s, fromlen = sizeof(from); 67426044Sminshall 67536304Skarels s = accept(pdata, (struct sockaddr *)&from, &fromlen); 67626044Sminshall if (s < 0) { 67726044Sminshall reply(425, "Can't open data connection."); 67826044Sminshall (void) close(pdata); 67926044Sminshall pdata = -1; 68026044Sminshall return(NULL); 68126044Sminshall } 68226044Sminshall (void) close(pdata); 68326044Sminshall pdata = s; 68436235Skarels reply(150, "Opening %s mode data connection for %s%s.", 68536235Skarels type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf); 68626044Sminshall return(fdopen(pdata, mode)); 68726044Sminshall } 68810275Ssam if (data >= 0) { 68910275Ssam reply(125, "Using existing data connection for %s%s.", 69010275Ssam name, sizebuf); 69110321Ssam usedefault = 1; 69210275Ssam return (fdopen(data, mode)); 69310275Ssam } 69410566Ssam if (usedefault) 69510422Ssam data_dest = his_addr; 69610422Ssam usedefault = 1; 69710275Ssam file = getdatasock(mode); 69810275Ssam if (file == NULL) { 69910275Ssam reply(425, "Can't create data socket (%s,%d): %s.", 70013247Ssam inet_ntoa(data_source.sin_addr), 70110275Ssam ntohs(data_source.sin_port), 70236304Skarels errno < sys_nerr ? sys_errlist[errno] : "unknown error"); 70310275Ssam return (NULL); 70410275Ssam } 70510275Ssam data = fileno(file); 70636304Skarels while (connect(data, (struct sockaddr *)&data_dest, 70736304Skarels sizeof (data_dest)) < 0) { 70811653Ssam if (errno == EADDRINUSE && retry < swaitmax) { 70926493Sminshall sleep((unsigned) swaitint); 71011653Ssam retry += swaitint; 71111653Ssam continue; 71211653Ssam } 71336304Skarels perror_reply(425, "Can't build data connection"); 71410275Ssam (void) fclose(file); 71510275Ssam data = -1; 71610275Ssam return (NULL); 71710275Ssam } 71836235Skarels reply(150, "Opening %s mode data connection for %s%s.", 71936235Skarels type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf); 72010275Ssam return (file); 72110275Ssam } 72210275Ssam 72310275Ssam /* 72410275Ssam * Tranfer the contents of "instr" to 72510275Ssam * "outstr" peer using the appropriate 72636933Skarels * encapsulation of the data subject 72710275Ssam * to Mode, Structure, and Type. 72810275Ssam * 72910275Ssam * NB: Form isn't handled. 73010275Ssam */ 73136446Sbostic send_data(instr, outstr, blksize) 73210275Ssam FILE *instr, *outstr; 73336446Sbostic off_t blksize; 73410275Ssam { 73536446Sbostic register int c, cnt; 73636446Sbostic register char *buf; 73736446Sbostic int netfd, filefd; 73810275Ssam 73926044Sminshall transflag++; 74026044Sminshall if (setjmp(urgcatch)) { 74126044Sminshall transflag = 0; 74236620Srick return; 74326044Sminshall } 74410275Ssam switch (type) { 74510275Ssam 74610275Ssam case TYPE_A: 74710275Ssam while ((c = getc(instr)) != EOF) { 74836933Skarels byte_count++; 74911220Ssam if (c == '\n') { 75036933Skarels if (ferror(outstr)) 75136620Srick goto data_err; 75227750Sminshall (void) putc('\r', outstr); 75311220Ssam } 75427750Sminshall (void) putc(c, outstr); 75510275Ssam } 75636933Skarels fflush(outstr); 75726044Sminshall transflag = 0; 75836933Skarels if (ferror(instr)) 75936933Skarels goto file_err; 76036933Skarels if (ferror(outstr)) 76136620Srick goto data_err; 76236620Srick reply(226, "Transfer complete."); 76336620Srick return; 76436446Sbostic 76510275Ssam case TYPE_I: 76610275Ssam case TYPE_L: 76736446Sbostic if ((buf = malloc((u_int)blksize)) == NULL) { 76836446Sbostic transflag = 0; 76936933Skarels perror_reply(451, "Local resource failure: malloc"); 77036933Skarels return; 77136446Sbostic } 77210275Ssam netfd = fileno(outstr); 77310275Ssam filefd = fileno(instr); 77436933Skarels while ((cnt = read(filefd, buf, (u_int)blksize)) > 0 && 77536620Srick write(netfd, buf, cnt) == cnt) 77636933Skarels byte_count += cnt; 77726044Sminshall transflag = 0; 77836446Sbostic (void)free(buf); 77936933Skarels if (cnt != 0) { 78036933Skarels if (cnt < 0) 78136933Skarels goto file_err; 78236620Srick goto data_err; 78336933Skarels } 78436620Srick reply(226, "Transfer complete."); 78536620Srick return; 78636620Srick default: 78736620Srick transflag = 0; 78836620Srick reply(550, "Unimplemented TYPE %d in send_data", type); 78936620Srick return; 79010275Ssam } 79136620Srick 79236620Srick data_err: 79326044Sminshall transflag = 0; 79436933Skarels perror_reply(426, "Data connection"); 79536933Skarels return; 79636933Skarels 79736933Skarels file_err: 79836933Skarels transflag = 0; 79936933Skarels perror_reply(551, "Error on input file"); 80010275Ssam } 80110275Ssam 80210275Ssam /* 80310275Ssam * Transfer data from peer to 80410275Ssam * "outstr" using the appropriate 80510275Ssam * encapulation of the data subject 80610275Ssam * to Mode, Structure, and Type. 80710275Ssam * 80810275Ssam * N.B.: Form isn't handled. 80910275Ssam */ 81010275Ssam receive_data(instr, outstr) 81110275Ssam FILE *instr, *outstr; 81210275Ssam { 81310275Ssam register int c; 81438134Srick int cnt, bare_lfs = 0; 81510275Ssam char buf[BUFSIZ]; 81610275Ssam 81726044Sminshall transflag++; 81826044Sminshall if (setjmp(urgcatch)) { 81926044Sminshall transflag = 0; 82036933Skarels return (-1); 82126044Sminshall } 82210275Ssam switch (type) { 82310275Ssam 82410275Ssam case TYPE_I: 82510275Ssam case TYPE_L: 82626044Sminshall while ((cnt = read(fileno(instr), buf, sizeof buf)) > 0) { 82736620Srick if (write(fileno(outstr), buf, cnt) != cnt) 82836933Skarels goto file_err; 82936933Skarels byte_count += cnt; 83026044Sminshall } 83136933Skarels if (cnt < 0) 83236620Srick goto data_err; 83326044Sminshall transflag = 0; 83436933Skarels return (0); 83510275Ssam 83610275Ssam case TYPE_E: 83727106Smckusick reply(553, "TYPE E not implemented."); 83826044Sminshall transflag = 0; 83927106Smckusick return (-1); 84010275Ssam 84110275Ssam case TYPE_A: 84210275Ssam while ((c = getc(instr)) != EOF) { 84336933Skarels byte_count++; 84438134Srick if (c == '\n') 84538134Srick bare_lfs++; 84627750Sminshall while (c == '\r') { 84736933Skarels if (ferror(outstr)) 84836620Srick goto data_err; 84936933Skarels if ((c = getc(instr)) != '\n') { 85027750Sminshall (void) putc ('\r', outstr); 85136933Skarels if (c == '\0' || c == EOF) 85236933Skarels goto contin2; 85336933Skarels } 85410275Ssam } 85536933Skarels (void) putc(c, outstr); 85636933Skarels contin2: ; 85710275Ssam } 85836620Srick fflush(outstr); 85936933Skarels if (ferror(instr)) 86036620Srick goto data_err; 86136933Skarels if (ferror(outstr)) 86236933Skarels goto file_err; 86326044Sminshall transflag = 0; 86438134Srick if (bare_lfs) { 86538134Srick lreply(230, "WARNING! %d bare linefeeds received in ASCII mode", bare_lfs); 86638134Srick printf(" File may not have transferred correctly.\r\n"); 86738134Srick } 86810275Ssam return (0); 86936620Srick default: 87036620Srick reply(550, "Unimplemented TYPE %d in receive_data", type); 87136620Srick transflag = 0; 87236933Skarels return (-1); 87310275Ssam } 87436620Srick 87536620Srick data_err: 87626044Sminshall transflag = 0; 87736933Skarels perror_reply(426, "Data Connection"); 87836933Skarels return (-1); 87936933Skarels 88036933Skarels file_err: 88136933Skarels transflag = 0; 88236933Skarels perror_reply(452, "Error writing file"); 88336933Skarels return (-1); 88410275Ssam } 88510275Ssam 88636933Skarels statfilecmd(filename) 88736933Skarels char *filename; 88836933Skarels { 88936933Skarels char line[BUFSIZ]; 89036933Skarels FILE *fin; 89136933Skarels int c; 89236933Skarels 89336933Skarels (void) sprintf(line, "/bin/ls -lgA %s", filename); 89436933Skarels fin = ftpd_popen(line, "r"); 89536933Skarels lreply(211, "status of %s:", filename); 89636933Skarels while ((c = getc(fin)) != EOF) { 89736933Skarels if (c == '\n') { 89836933Skarels if (ferror(stdout)){ 89936933Skarels perror_reply(421, "control connection"); 90036933Skarels (void) ftpd_pclose(fin); 90136933Skarels dologout(1); 90236933Skarels /* NOTREACHED */ 90336933Skarels } 90436933Skarels if (ferror(fin)) { 90536933Skarels perror_reply(551, filename); 90636933Skarels (void) ftpd_pclose(fin); 90736933Skarels return; 90836933Skarels } 90936933Skarels (void) putc('\r', stdout); 91036933Skarels } 91136933Skarels (void) putc(c, stdout); 91236933Skarels } 91336933Skarels (void) ftpd_pclose(fin); 91436933Skarels reply(211, "End of Status"); 91536933Skarels } 91636933Skarels 91736933Skarels statcmd() 91836933Skarels { 91936933Skarels struct sockaddr_in *sin; 92036933Skarels u_char *a, *p; 92136933Skarels 92236933Skarels lreply(211, "%s FTP server status:", hostname, version); 92336933Skarels printf(" %s\r\n", version); 92436933Skarels printf(" Connected to %s", remotehost); 92538134Srick if (!isdigit(remotehost[0])) 92636933Skarels printf(" (%s)", inet_ntoa(his_addr.sin_addr)); 92736933Skarels printf("\r\n"); 92836933Skarels if (logged_in) { 92936933Skarels if (guest) 93036933Skarels printf(" Logged in anonymously\r\n"); 93136933Skarels else 93236933Skarels printf(" Logged in as %s\r\n", pw->pw_name); 93336933Skarels } else if (askpasswd) 93436933Skarels printf(" Waiting for password\r\n"); 93536933Skarels else 93636933Skarels printf(" Waiting for user name\r\n"); 93736933Skarels printf(" TYPE: %s", typenames[type]); 93836933Skarels if (type == TYPE_A || type == TYPE_E) 93936933Skarels printf(", FORM: %s", formnames[form]); 94036933Skarels if (type == TYPE_L) 94136933Skarels #if NBBY == 8 94236933Skarels printf(" %d", NBBY); 94336933Skarels #else 94436933Skarels printf(" %d", bytesize); /* need definition! */ 94536933Skarels #endif 94636933Skarels printf("; STRUcture: %s; transfer MODE: %s\r\n", 94736933Skarels strunames[stru], modenames[mode]); 94836933Skarels if (data != -1) 94936933Skarels printf(" Data connection open\r\n"); 95036933Skarels else if (pdata != -1) { 95136933Skarels printf(" in Passive mode"); 95236933Skarels sin = &pasv_addr; 95336933Skarels goto printaddr; 95436933Skarels } else if (usedefault == 0) { 95536933Skarels printf(" PORT"); 95636933Skarels sin = &data_dest; 95736933Skarels printaddr: 95836933Skarels a = (u_char *) &sin->sin_addr; 95936933Skarels p = (u_char *) &sin->sin_port; 96036933Skarels #define UC(b) (((int) b) & 0xff) 96136933Skarels printf(" (%d,%d,%d,%d,%d,%d)\r\n", UC(a[0]), 96236933Skarels UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1])); 96336933Skarels #undef UC 96436933Skarels } else 96536933Skarels printf(" No data connection\r\n"); 96636933Skarels reply(211, "End of status"); 96736933Skarels } 96836933Skarels 96910275Ssam fatal(s) 97010275Ssam char *s; 97110275Ssam { 97210275Ssam reply(451, "Error in server: %s\n", s); 97310275Ssam reply(221, "Closing connection due to server error."); 97413247Ssam dologout(0); 97536620Srick /* NOTREACHED */ 97610275Ssam } 97710275Ssam 97836446Sbostic /* VARARGS2 */ 97936446Sbostic reply(n, fmt, p0, p1, p2, p3, p4, p5) 98010275Ssam int n; 98136446Sbostic char *fmt; 98210275Ssam { 98310275Ssam printf("%d ", n); 98436446Sbostic printf(fmt, p0, p1, p2, p3, p4, p5); 98510275Ssam printf("\r\n"); 98636435Sbostic (void)fflush(stdout); 98710275Ssam if (debug) { 98826493Sminshall syslog(LOG_DEBUG, "<--- %d ", n); 98936446Sbostic syslog(LOG_DEBUG, fmt, p0, p1, p2, p3, p4, p5); 99010275Ssam } 99136620Srick } 99210275Ssam 99336446Sbostic /* VARARGS2 */ 99436446Sbostic lreply(n, fmt, p0, p1, p2, p3, p4, p5) 99510275Ssam int n; 99636446Sbostic char *fmt; 99710275Ssam { 99836446Sbostic printf("%d- ", n); 99936446Sbostic printf(fmt, p0, p1, p2, p3, p4, p5); 100036446Sbostic printf("\r\n"); 100136435Sbostic (void)fflush(stdout); 100236446Sbostic if (debug) { 100336446Sbostic syslog(LOG_DEBUG, "<--- %d- ", n); 100436446Sbostic syslog(LOG_DEBUG, fmt, p0, p1, p2, p3, p4, p5); 100536446Sbostic } 100610275Ssam } 100710275Ssam 100810275Ssam ack(s) 100910275Ssam char *s; 101010275Ssam { 101127106Smckusick reply(250, "%s command successful.", s); 101210275Ssam } 101310275Ssam 101410275Ssam nack(s) 101510275Ssam char *s; 101610275Ssam { 101710275Ssam reply(502, "%s command not implemented.", s); 101810275Ssam } 101910275Ssam 102036304Skarels /* ARGSUSED */ 102126493Sminshall yyerror(s) 102226493Sminshall char *s; 102310275Ssam { 102426044Sminshall char *cp; 102526044Sminshall 102636551Sbostic if (cp = index(cbuf,'\n')) 102736551Sbostic *cp = '\0'; 102836933Skarels reply(500, "'%s': command not understood.", cbuf); 102910275Ssam } 103010275Ssam 103110275Ssam delete(name) 103210275Ssam char *name; 103310275Ssam { 103410275Ssam struct stat st; 103510275Ssam 103610275Ssam if (stat(name, &st) < 0) { 103736304Skarels perror_reply(550, name); 103810275Ssam return; 103910275Ssam } 104010275Ssam if ((st.st_mode&S_IFMT) == S_IFDIR) { 104110275Ssam if (rmdir(name) < 0) { 104236304Skarels perror_reply(550, name); 104310275Ssam return; 104410275Ssam } 104510275Ssam goto done; 104610275Ssam } 104710275Ssam if (unlink(name) < 0) { 104836304Skarels perror_reply(550, name); 104910275Ssam return; 105010275Ssam } 105110275Ssam done: 105210275Ssam ack("DELE"); 105310275Ssam } 105410275Ssam 105510275Ssam cwd(path) 105610275Ssam char *path; 105710275Ssam { 105836620Srick if (chdir(path) < 0) 105936304Skarels perror_reply(550, path); 106036620Srick else 106136620Srick ack("CWD"); 106210275Ssam } 106310275Ssam 106410303Ssam makedir(name) 106510275Ssam char *name; 106610275Ssam { 106736276Sbostic if (mkdir(name, 0777) < 0) 106836304Skarels perror_reply(550, name); 106936276Sbostic else 107036276Sbostic reply(257, "MKD command successful."); 107110275Ssam } 107210275Ssam 107310303Ssam removedir(name) 107410275Ssam char *name; 107510275Ssam { 107636620Srick if (rmdir(name) < 0) 107736304Skarels perror_reply(550, name); 107836620Srick else 107936620Srick ack("RMD"); 108010275Ssam } 108110275Ssam 108210303Ssam pwd() 108310275Ssam { 108410303Ssam char path[MAXPATHLEN + 1]; 108536304Skarels extern char *getwd(); 108610275Ssam 108736620Srick if (getwd(path) == (char *)NULL) 108827106Smckusick reply(550, "%s.", path); 108936620Srick else 109036620Srick reply(257, "\"%s\" is current directory.", path); 109110275Ssam } 109210275Ssam 109310275Ssam char * 109410275Ssam renamefrom(name) 109510275Ssam char *name; 109610275Ssam { 109710275Ssam struct stat st; 109810275Ssam 109910275Ssam if (stat(name, &st) < 0) { 110036304Skarels perror_reply(550, name); 110110275Ssam return ((char *)0); 110210275Ssam } 110310303Ssam reply(350, "File exists, ready for destination name"); 110410275Ssam return (name); 110510275Ssam } 110610275Ssam 110710275Ssam renamecmd(from, to) 110810275Ssam char *from, *to; 110910275Ssam { 111036620Srick if (rename(from, to) < 0) 111136304Skarels perror_reply(550, "rename"); 111236620Srick else 111336620Srick ack("RNTO"); 111410275Ssam } 111510275Ssam 111610275Ssam dolog(sin) 111710275Ssam struct sockaddr_in *sin; 111810275Ssam { 111936304Skarels struct hostent *hp = gethostbyaddr((char *)&sin->sin_addr, 112010275Ssam sizeof (struct in_addr), AF_INET); 112136304Skarels time_t t, time(); 112226493Sminshall extern char *ctime(); 112310275Ssam 112436304Skarels if (hp) 112526493Sminshall (void) strncpy(remotehost, hp->h_name, sizeof (remotehost)); 112636304Skarels else 112726493Sminshall (void) strncpy(remotehost, inet_ntoa(sin->sin_addr), 112813247Ssam sizeof (remotehost)); 112936620Srick #ifdef SETPROCTITLE 113036933Skarels sprintf(proctitle, "%s: connected", remotehost); 113136933Skarels setproctitle(proctitle); 113236620Srick #endif /* SETPROCTITLE */ 113336933Skarels 113436933Skarels if (logging) { 113536933Skarels t = time((time_t *) 0); 113636933Skarels syslog(LOG_INFO, "connection from %s at %s", 113736933Skarels remotehost, ctime(&t)); 113836933Skarels } 113910275Ssam } 114010695Ssam 114110695Ssam /* 114213247Ssam * Record logout in wtmp file 114313247Ssam * and exit with supplied status. 114413247Ssam */ 114513247Ssam dologout(status) 114613247Ssam int status; 114713247Ssam { 114817580Ssam if (logged_in) { 114936304Skarels (void) seteuid((uid_t)0); 115035672Sbostic logwtmp(ttyline, "", ""); 115113247Ssam } 115214436Ssam /* beware of flushing buffers after a SIGPIPE */ 115314436Ssam _exit(status); 115413247Ssam } 115513247Ssam 115626044Sminshall myoob() 115726044Sminshall { 115827750Sminshall char *cp; 115926044Sminshall 116027750Sminshall /* only process if transfer occurring */ 116136304Skarels if (!transflag) 116226044Sminshall return; 116327750Sminshall cp = tmpline; 116427750Sminshall if (getline(cp, 7, stdin) == NULL) { 116536304Skarels reply(221, "You could at least say goodbye."); 116627750Sminshall dologout(0); 116726044Sminshall } 116826044Sminshall upper(cp); 116936933Skarels if (strcmp(cp, "ABOR\r\n") == 0) { 117036933Skarels tmpline[0] = '\0'; 117136933Skarels reply(426, "Transfer aborted. Data connection closed."); 117236933Skarels reply(226, "Abort successful"); 117336933Skarels longjmp(urgcatch, 1); 117436933Skarels } 117536933Skarels if (strcmp(cp, "STAT\r\n") == 0) { 117636933Skarels if (file_size != (off_t) -1) 117736933Skarels reply(213, "Status: %lu of %lu bytes transferred", 117836933Skarels byte_count, file_size); 117936933Skarels else 118036933Skarels reply(213, "Status: %lu bytes transferred", byte_count); 118136933Skarels } 118226044Sminshall } 118326044Sminshall 118427106Smckusick /* 118536620Srick * Note: a response of 425 is not mentioned as a possible response to 118636620Srick * the PASV command in RFC959. However, it has been blessed as 118736620Srick * a legitimate response by Jon Postel in a telephone conversation 118836620Srick * with Rick Adams on 25 Jan 89. 118927106Smckusick */ 119026044Sminshall passive() 119126044Sminshall { 119226044Sminshall int len; 119326044Sminshall register char *p, *a; 119426044Sminshall 119526044Sminshall pdata = socket(AF_INET, SOCK_STREAM, 0); 119626044Sminshall if (pdata < 0) { 119736620Srick perror_reply(425, "Can't open passive connection"); 119826044Sminshall return; 119926044Sminshall } 120036933Skarels pasv_addr = ctrl_addr; 120136933Skarels pasv_addr.sin_port = 0; 120236304Skarels (void) seteuid((uid_t)0); 120336933Skarels if (bind(pdata, (struct sockaddr *)&pasv_addr, sizeof(pasv_addr)) < 0) { 120436304Skarels (void) seteuid((uid_t)pw->pw_uid); 120536620Srick goto pasv_error; 120626044Sminshall } 120736304Skarels (void) seteuid((uid_t)pw->pw_uid); 120836933Skarels len = sizeof(pasv_addr); 120936933Skarels if (getsockname(pdata, (struct sockaddr *) &pasv_addr, &len) < 0) 121036620Srick goto pasv_error; 121136620Srick if (listen(pdata, 1) < 0) 121236620Srick goto pasv_error; 121336933Skarels a = (char *) &pasv_addr.sin_addr; 121436933Skarels p = (char *) &pasv_addr.sin_port; 121526044Sminshall 121626044Sminshall #define UC(b) (((int) b) & 0xff) 121726044Sminshall 121826044Sminshall reply(227, "Entering Passive Mode (%d,%d,%d,%d,%d,%d)", UC(a[0]), 121926044Sminshall UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1])); 122036620Srick return; 122136620Srick 122236620Srick pasv_error: 122336620Srick (void) close(pdata); 122436620Srick pdata = -1; 122536620Srick perror_reply(425, "Can't open passive connection"); 122636620Srick return; 122726044Sminshall } 122826044Sminshall 122936304Skarels /* 123036304Skarels * Generate unique name for file with basename "local". 123136304Skarels * The file named "local" is already known to exist. 123236304Skarels * Generates failure reply on error. 123336304Skarels */ 123426044Sminshall char * 123526044Sminshall gunique(local) 123626044Sminshall char *local; 123726044Sminshall { 123826044Sminshall static char new[MAXPATHLEN]; 123936304Skarels struct stat st; 124026044Sminshall char *cp = rindex(local, '/'); 124136933Skarels int count = 0; 124226044Sminshall 124336304Skarels if (cp) 124426044Sminshall *cp = '\0'; 124536620Srick if (stat(cp ? local : ".", &st) < 0) { 124636933Skarels perror_reply(553, cp ? local : "."); 124726044Sminshall return((char *) 0); 124826044Sminshall } 124936620Srick if (cp) 125036620Srick *cp = '/'; 125126044Sminshall (void) strcpy(new, local); 125226044Sminshall cp = new + strlen(new); 125326044Sminshall *cp++ = '.'; 125436304Skarels for (count = 1; count < 100; count++) { 125536304Skarels (void) sprintf(cp, "%d", count); 125636304Skarels if (stat(new, &st) < 0) 125736304Skarels return(new); 125826044Sminshall } 125936304Skarels reply(452, "Unique file name cannot be created."); 126036304Skarels return((char *) 0); 126126044Sminshall } 126236304Skarels 126336304Skarels /* 126436304Skarels * Format and send reply containing system error number. 126536304Skarels */ 126636304Skarels perror_reply(code, string) 126736304Skarels int code; 126836304Skarels char *string; 126936304Skarels { 127036304Skarels if (errno < sys_nerr) 127136304Skarels reply(code, "%s: %s.", string, sys_errlist[errno]); 127236304Skarels else 127336304Skarels reply(code, "%s: unknown error %d.", string, errno); 127436304Skarels } 127536620Srick 127636620Srick static char *onefile[] = { 127736620Srick "", 127836620Srick 0 127936620Srick }; 128036620Srick 128136620Srick send_file_list(whichfiles) 128236620Srick char *whichfiles; 128336620Srick { 128436620Srick struct stat st; 128536620Srick DIR *dirp = NULL; 128636620Srick struct direct *dir; 128736620Srick FILE *dout = NULL; 128836620Srick register char **dirlist, *dirname; 128937459Skarels int simple = 0; 129036620Srick char *strpbrk(); 129136620Srick 129236620Srick if (strpbrk(whichfiles, "~{[*?") != NULL) { 129336620Srick extern char **glob(), *globerr; 129436933Skarels 129536620Srick globerr = NULL; 129636620Srick dirlist = glob(whichfiles); 129736620Srick if (globerr != NULL) { 129836620Srick reply(550, globerr); 129936620Srick return; 130036620Srick } else if (dirlist == NULL) { 130136620Srick errno = ENOENT; 130236620Srick perror_reply(550, whichfiles); 130336620Srick return; 130436620Srick } 130536620Srick } else { 130636620Srick onefile[0] = whichfiles; 130736620Srick dirlist = onefile; 130837459Skarels simple = 1; 130936620Srick } 131036933Skarels 131136933Skarels if (setjmp(urgcatch)) { 131236933Skarels transflag = 0; 131336933Skarels return; 131436933Skarels } 131536620Srick while (dirname = *dirlist++) { 131636620Srick if (stat(dirname, &st) < 0) { 131736933Skarels /* 131836933Skarels * If user typed "ls -l", etc, and the client 131936933Skarels * used NLST, do what the user meant. 132036933Skarels */ 132136933Skarels if (dirname[0] == '-' && *dirlist == NULL && 132236933Skarels transflag == 0) { 132336933Skarels retrieve("/bin/ls %s", dirname); 132436933Skarels return; 132536933Skarels } 132636620Srick perror_reply(550, whichfiles); 132736620Srick if (dout != NULL) { 132836620Srick (void) fclose(dout); 132936933Skarels transflag = 0; 133036620Srick data = -1; 133136620Srick pdata = -1; 133236620Srick } 133336620Srick return; 133436620Srick } 133536933Skarels 133636620Srick if ((st.st_mode&S_IFMT) == S_IFREG) { 133736620Srick if (dout == NULL) { 133837459Skarels dout = dataconn("file list", (off_t)-1, "w"); 133936620Srick if (dout == NULL) 134036620Srick return; 134136933Skarels transflag++; 134236620Srick } 134338158Srick fprintf(dout, "%s%s\n", dirname, 134438158Srick type == TYPE_A ? "\r" : ""); 134536933Skarels byte_count += strlen(dirname) + 1; 134636620Srick continue; 134736933Skarels } else if ((st.st_mode&S_IFMT) != S_IFDIR) 134836620Srick continue; 134936620Srick 135036620Srick if ((dirp = opendir(dirname)) == NULL) 135136620Srick continue; 135236620Srick 135336620Srick while ((dir = readdir(dirp)) != NULL) { 135436933Skarels char nbuf[MAXPATHLEN]; 135536620Srick 135636620Srick if (dir->d_name[0] == '.' && dir->d_namlen == 1) 135736620Srick continue; 135836933Skarels if (dir->d_name[0] == '.' && dir->d_name[1] == '.' && 135936933Skarels dir->d_namlen == 2) 136036620Srick continue; 136136620Srick 136236933Skarels sprintf(nbuf, "%s/%s", dirname, dir->d_name); 136336933Skarels 136436620Srick /* 136536933Skarels * We have to do a stat to insure it's 136636933Skarels * not a directory or special file. 136736620Srick */ 136837459Skarels if (simple || (stat(nbuf, &st) == 0 && 136937459Skarels (st.st_mode&S_IFMT) == S_IFREG)) { 137036620Srick if (dout == NULL) { 137137459Skarels dout = dataconn("file list", (off_t)-1, 137236620Srick "w"); 137336620Srick if (dout == NULL) 137436620Srick return; 137536933Skarels transflag++; 137636620Srick } 137736620Srick if (nbuf[0] == '.' && nbuf[1] == '/') 137838158Srick fprintf(dout, "%s%s\n", &nbuf[2], 137938158Srick type == TYPE_A ? "\r" : ""); 138036620Srick else 138138158Srick fprintf(dout, "%s%s\n", nbuf, 138238158Srick type == TYPE_A ? "\r" : ""); 138336933Skarels byte_count += strlen(nbuf) + 1; 138436620Srick } 138536620Srick } 138636620Srick (void) closedir(dirp); 138736620Srick } 138836620Srick 138936933Skarels if (dout == NULL) 139036933Skarels reply(550, "No files found."); 139136933Skarels else if (ferror(dout) != 0) 139236933Skarels perror_reply(550, "Data connection"); 139336933Skarels else 139436620Srick reply(226, "Transfer complete."); 139536620Srick 139636933Skarels transflag = 0; 139736933Skarels if (dout != NULL) 139836620Srick (void) fclose(dout); 139936620Srick data = -1; 140036620Srick pdata = -1; 140136620Srick } 140236620Srick 140336620Srick #ifdef SETPROCTITLE 140436620Srick /* 140536620Srick * clobber argv so ps will show what we're doing. 140636620Srick * (stolen from sendmail) 140736620Srick * warning, since this is usually started from inetd.conf, it 140836620Srick * often doesn't have much of an environment or arglist to overwrite. 140936620Srick */ 141036620Srick 141136620Srick /*VARARGS1*/ 141236620Srick setproctitle(fmt, a, b, c) 141336620Srick char *fmt; 141436620Srick { 141536620Srick register char *p, *bp, ch; 141636620Srick register int i; 141736620Srick char buf[BUFSIZ]; 141836620Srick 141936620Srick (void) sprintf(buf, fmt, a, b, c); 142036620Srick 142136620Srick /* make ps print our process name */ 142236620Srick p = Argv[0]; 142336620Srick *p++ = '-'; 142436620Srick 142536620Srick i = strlen(buf); 142636620Srick if (i > LastArgv - p - 2) { 142736620Srick i = LastArgv - p - 2; 142836620Srick buf[i] = '\0'; 142936620Srick } 143036620Srick bp = buf; 143136620Srick while (ch = *bp++) 143236620Srick if (ch != '\n' && ch != '\r') 143336620Srick *p++ = ch; 143436620Srick while (p < LastArgv) 143536620Srick *p++ = ' '; 143636620Srick } 143736620Srick #endif /* SETPROCTITLE */ 1438