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*38158Srick static char sccsid[] = "@(#)ftpd.c 5.30 (Berkeley) 05/28/89"; 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. 31136304Skarels * If name is "ftp" or "anonymous" and ftp account exists, 31236304Skarels * 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) { 33836304Skarels if ((pw = sgetpwnam("ftp")) != NULL) { 33936304Skarels guest = 1; 34036304Skarels askpasswd = 1; 34136304Skarels reply(331, "Guest login ok, send ident as password."); 34236933Skarels } else 34336304Skarels reply(530, "User %s unknown.", name); 34436304Skarels return; 34536304Skarels } 34636304Skarels if (pw = sgetpwnam(name)) { 34736304Skarels if ((shell = pw->pw_shell) == NULL || *shell == 0) 34837459Skarels shell = _PATH_BSHELL; 34936304Skarels while ((cp = getusershell()) != NULL) 35036304Skarels if (strcmp(cp, shell) == 0) 35136304Skarels break; 35236304Skarels endusershell(); 35336304Skarels if (cp == NULL) { 35436304Skarels reply(530, "User %s access denied.", name); 35536933Skarels if (logging) 35636933Skarels syslog(LOG_NOTICE, 35736933Skarels "FTP LOGIN REFUSED FROM %s, %s", 35836933Skarels remotehost, name); 35936304Skarels pw = (struct passwd *) NULL; 36036304Skarels return; 36136304Skarels } 36237459Skarels if ((fd = fopen(_PATH_FTPUSERS, "r")) != NULL) { 36336304Skarels while (fgets(line, sizeof (line), fd) != NULL) { 36436304Skarels if ((cp = index(line, '\n')) != NULL) 36536304Skarels *cp = '\0'; 36636304Skarels if (strcmp(line, name) == 0) { 36736304Skarels reply(530, "User %s access denied.", name); 36836933Skarels if (logging) 36936933Skarels syslog(LOG_NOTICE, 37036933Skarels "FTP LOGIN REFUSED FROM %s, %s", 37136933Skarels remotehost, name); 37236304Skarels pw = (struct passwd *) NULL; 37338134Srick (void) fclose(fd); 37436304Skarels return; 37536304Skarels } 37636304Skarels } 37738134Srick (void) fclose(fd); 37836304Skarels } 37936304Skarels } 38036304Skarels reply(331, "Password required for %s.", name); 38136304Skarels askpasswd = 1; 38236304Skarels /* 38336304Skarels * Delay before reading passwd after first failed 38436304Skarels * attempt to slow down passwd-guessing programs. 38536304Skarels */ 38636304Skarels if (login_attempts) 38736304Skarels sleep((unsigned) login_attempts); 38836304Skarels } 38936304Skarels 39036304Skarels /* 39136304Skarels * Terminate login as previous user, if any, resetting state; 39236304Skarels * used when USER command is given or login fails. 39336304Skarels */ 39436304Skarels end_login() 39536304Skarels { 39636304Skarels 39736304Skarels (void) seteuid((uid_t)0); 39836304Skarels if (logged_in) 39936304Skarels logwtmp(ttyline, "", ""); 40036304Skarels pw = NULL; 40136304Skarels logged_in = 0; 40236304Skarels guest = 0; 40336304Skarels } 40436304Skarels 40510275Ssam pass(passwd) 40610275Ssam char *passwd; 40710275Ssam { 40836304Skarels char *xpasswd, *salt; 40910275Ssam 41036304Skarels if (logged_in || askpasswd == 0) { 41110275Ssam reply(503, "Login with USER first."); 41210275Ssam return; 41310275Ssam } 41436304Skarels askpasswd = 0; 41510275Ssam if (!guest) { /* "ftp" is only account allowed no password */ 41636304Skarels if (pw == NULL) 41736304Skarels salt = "xx"; 41836304Skarels else 41936304Skarels salt = pw->pw_passwd; 42036304Skarels xpasswd = crypt(passwd, salt); 42116760Slepreau /* The strcmp does not catch null passwords! */ 42236304Skarels if (pw == NULL || *pw->pw_passwd == '\0' || 42336304Skarels strcmp(xpasswd, pw->pw_passwd)) { 42410275Ssam reply(530, "Login incorrect."); 42510275Ssam pw = NULL; 42636304Skarels if (login_attempts++ >= 5) { 42736933Skarels syslog(LOG_NOTICE, 42836304Skarels "repeated login failures from %s", 42936304Skarels remotehost); 43036304Skarels exit(0); 43136304Skarels } 43210275Ssam return; 43310275Ssam } 43410275Ssam } 43536304Skarels login_attempts = 0; /* this time successful */ 43636304Skarels (void) setegid((gid_t)pw->pw_gid); 43736304Skarels (void) initgroups(pw->pw_name, pw->pw_gid); 43816033Sralph 43936192Sbostic /* open wtmp before chroot */ 44036192Sbostic (void)sprintf(ttyline, "ftp%d", getpid()); 44136192Sbostic logwtmp(ttyline, pw->pw_name, remotehost); 44236192Sbostic logged_in = 1; 44336192Sbostic 44436446Sbostic if (guest) { 44536620Srick /* 44636933Skarels * We MUST do a chdir() after the chroot. Otherwise 44736933Skarels * the old current directory will be accessible as "." 44836933Skarels * outside the new root! 44936620Srick */ 45036620Srick if (chroot(pw->pw_dir) < 0 || chdir("/") < 0) { 45136446Sbostic reply(550, "Can't set guest privileges."); 45236446Sbostic goto bad; 45336446Sbostic } 45436933Skarels } else if (chdir(pw->pw_dir) < 0) { 45536620Srick if (chdir("/") < 0) { 45636446Sbostic reply(530, "User %s: can't change directory to %s.", 45736446Sbostic pw->pw_name, pw->pw_dir); 45836446Sbostic goto bad; 45936933Skarels } else 46036446Sbostic lreply(230, "No directory! Logging in with home=/"); 46136620Srick } 46236304Skarels if (seteuid((uid_t)pw->pw_uid) < 0) { 46336304Skarels reply(550, "Can't set uid."); 46436304Skarels goto bad; 46536304Skarels } 46636550Sbostic if (guest) { 46736192Sbostic reply(230, "Guest login ok, access restrictions apply."); 46836933Skarels #ifdef SETPROCTITLE 46936933Skarels sprintf(proctitle, "%s: anonymous/%.*s", remotehost, 47036933Skarels sizeof(proctitle) - sizeof(remotehost) - 47136933Skarels sizeof(": anonymous/"), passwd); 47236933Skarels setproctitle(proctitle); 47336933Skarels #endif /* SETPROCTITLE */ 47436933Skarels if (logging) 47536933Skarels syslog(LOG_INFO, "ANONYMOUS FTP LOGIN FROM %s, %s", 47636933Skarels remotehost, passwd); 47736933Skarels } else { 47810275Ssam reply(230, "User %s logged in.", pw->pw_name); 47936933Skarels #ifdef SETPROCTITLE 48036933Skarels sprintf(proctitle, "%s: %s", remotehost, pw->pw_name); 48136933Skarels setproctitle(proctitle); 48236933Skarels #endif /* SETPROCTITLE */ 48336933Skarels if (logging) 48436933Skarels syslog(LOG_INFO, "FTP LOGIN FROM %s, %s", 48536933Skarels remotehost, pw->pw_name); 48636550Sbostic } 48710303Ssam home = pw->pw_dir; /* home dir for globbing */ 48836933Skarels (void) umask(defumask); 48910303Ssam return; 49010303Ssam bad: 49136304Skarels /* Forget all about it... */ 49236304Skarels end_login(); 49310275Ssam } 49410275Ssam 49510275Ssam retrieve(cmd, name) 49610275Ssam char *cmd, *name; 49710275Ssam { 49810275Ssam FILE *fin, *dout; 49910275Ssam struct stat st; 50036620Srick int (*closefunc)(); 50110275Ssam 50236557Sbostic if (cmd == 0) { 50336446Sbostic fin = fopen(name, "r"), closefunc = fclose; 50436557Sbostic st.st_size = 0; 50536557Sbostic } else { 50610275Ssam char line[BUFSIZ]; 50710275Ssam 50826493Sminshall (void) sprintf(line, cmd, name), name = line; 50936304Skarels fin = ftpd_popen(line, "r"), closefunc = ftpd_pclose; 51036557Sbostic st.st_size = -1; 51136933Skarels st.st_blksize = BUFSIZ; 51210275Ssam } 51310275Ssam if (fin == NULL) { 51413152Ssam if (errno != 0) 51536304Skarels perror_reply(550, name); 51610275Ssam return; 51710275Ssam } 51810275Ssam if (cmd == 0 && 51936933Skarels (fstat(fileno(fin), &st) < 0 || (st.st_mode&S_IFMT) != S_IFREG)) { 52010275Ssam reply(550, "%s: not a plain file.", name); 52110275Ssam goto done; 52210275Ssam } 52337459Skarels if (restart_point) { 52437459Skarels if (type == TYPE_A) { 52537459Skarels register int i, n, c; 52637459Skarels 52737459Skarels n = restart_point; 52837459Skarels i = 0; 52937459Skarels while (i++ < n) { 53037459Skarels if ((c=getc(fin)) == EOF) { 53137459Skarels perror_reply(550, name); 53237459Skarels goto done; 53337459Skarels } 53437459Skarels if (c == '\n') 53537459Skarels i++; 53637459Skarels } 53737459Skarels } else if (lseek(fileno(fin), restart_point, L_SET) < 0) { 53837459Skarels perror_reply(550, name); 53937459Skarels goto done; 54037459Skarels } 54137459Skarels } 54210275Ssam dout = dataconn(name, st.st_size, "w"); 54310275Ssam if (dout == NULL) 54410275Ssam goto done; 54536620Srick send_data(fin, dout, st.st_blksize); 54626493Sminshall (void) fclose(dout); 54726044Sminshall data = -1; 54826044Sminshall pdata = -1; 54910275Ssam done: 55010275Ssam (*closefunc)(fin); 55110275Ssam } 55210275Ssam 55336304Skarels store(name, mode, unique) 55410275Ssam char *name, *mode; 55536304Skarels int unique; 55610275Ssam { 55710275Ssam FILE *fout, *din; 55836446Sbostic struct stat st; 55936620Srick int (*closefunc)(); 56036304Skarels char *gunique(); 56110275Ssam 56236446Sbostic if (unique && stat(name, &st) == 0 && 56336446Sbostic (name = gunique(name)) == NULL) 56436446Sbostic return; 56510303Ssam 56637459Skarels if (restart_point) 56737459Skarels mode = "r+w"; 56836620Srick fout = fopen(name, mode); 56936620Srick closefunc = fclose; 57010275Ssam if (fout == NULL) { 57136304Skarels perror_reply(553, name); 57210275Ssam return; 57310275Ssam } 57437459Skarels if (restart_point) { 57537459Skarels if (type == TYPE_A) { 57637459Skarels register int i, n, c; 57737459Skarels 57837459Skarels n = restart_point; 57937459Skarels i = 0; 58037459Skarels while (i++ < n) { 58137459Skarels if ((c=getc(fout)) == EOF) { 58237459Skarels perror_reply(550, name); 58337459Skarels goto done; 58437459Skarels } 58537459Skarels if (c == '\n') 58637459Skarels i++; 58737459Skarels } 58837459Skarels /* 58937459Skarels * We must do this seek to "current" position 59037459Skarels * because we are changing from reading to 59137459Skarels * writing. 59237459Skarels */ 59337459Skarels if (fseek(fout, 0L, L_INCR) < 0) { 59437459Skarels perror_reply(550, name); 59537459Skarels goto done; 59637459Skarels } 59737459Skarels } else if (lseek(fileno(fout), restart_point, L_SET) < 0) { 59837459Skarels perror_reply(550, name); 59937459Skarels goto done; 60037459Skarels } 60137459Skarels } 60236304Skarels din = dataconn(name, (off_t)-1, "r"); 60310275Ssam if (din == NULL) 60410275Ssam goto done; 60536620Srick if (receive_data(din, fout) == 0) { 60636620Srick if (unique) 60736304Skarels reply(226, "Transfer complete (unique file name:%s).", 60836933Skarels name); 60936304Skarels else 61036304Skarels reply(226, "Transfer complete."); 61126044Sminshall } 61226493Sminshall (void) fclose(din); 61326044Sminshall data = -1; 61426044Sminshall pdata = -1; 61510275Ssam done: 61610275Ssam (*closefunc)(fout); 61710275Ssam } 61810275Ssam 61910275Ssam FILE * 62010275Ssam getdatasock(mode) 62110275Ssam char *mode; 62210275Ssam { 62337459Skarels int s, on = 1, tries; 62410275Ssam 62510275Ssam if (data >= 0) 62610275Ssam return (fdopen(data, mode)); 62713247Ssam s = socket(AF_INET, SOCK_STREAM, 0); 62810602Ssam if (s < 0) 62910275Ssam return (NULL); 63036304Skarels (void) seteuid((uid_t)0); 63137459Skarels if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, 63237459Skarels (char *) &on, sizeof (on)) < 0) 63310602Ssam goto bad; 63413152Ssam /* anchor socket to avoid multi-homing problems */ 63513152Ssam data_source.sin_family = AF_INET; 63613152Ssam data_source.sin_addr = ctrl_addr.sin_addr; 63737459Skarels for (tries = 1; ; tries++) { 63837459Skarels if (bind(s, (struct sockaddr *)&data_source, 63937459Skarels sizeof (data_source)) >= 0) 64037459Skarels break; 64137459Skarels if (errno != EADDRINUSE || tries > 10) 64237459Skarels goto bad; 64337459Skarels sleep(tries); 64437459Skarels } 64536304Skarels (void) seteuid((uid_t)pw->pw_uid); 64610275Ssam return (fdopen(s, mode)); 64710602Ssam bad: 64836304Skarels (void) seteuid((uid_t)pw->pw_uid); 64926493Sminshall (void) close(s); 65010602Ssam return (NULL); 65110275Ssam } 65210275Ssam 65310275Ssam FILE * 65410275Ssam dataconn(name, size, mode) 65510275Ssam char *name; 65611653Ssam off_t size; 65710275Ssam char *mode; 65810275Ssam { 65910275Ssam char sizebuf[32]; 66010275Ssam FILE *file; 66111653Ssam int retry = 0; 66210275Ssam 66336933Skarels file_size = size; 66436933Skarels byte_count = 0; 66536304Skarels if (size != (off_t) -1) 66626493Sminshall (void) sprintf (sizebuf, " (%ld bytes)", size); 66710275Ssam else 66810275Ssam (void) strcpy(sizebuf, ""); 66936304Skarels if (pdata >= 0) { 67026044Sminshall struct sockaddr_in from; 67126044Sminshall int s, fromlen = sizeof(from); 67226044Sminshall 67336304Skarels s = accept(pdata, (struct sockaddr *)&from, &fromlen); 67426044Sminshall if (s < 0) { 67526044Sminshall reply(425, "Can't open data connection."); 67626044Sminshall (void) close(pdata); 67726044Sminshall pdata = -1; 67826044Sminshall return(NULL); 67926044Sminshall } 68026044Sminshall (void) close(pdata); 68126044Sminshall pdata = s; 68236235Skarels reply(150, "Opening %s mode data connection for %s%s.", 68336235Skarels type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf); 68426044Sminshall return(fdopen(pdata, mode)); 68526044Sminshall } 68610275Ssam if (data >= 0) { 68710275Ssam reply(125, "Using existing data connection for %s%s.", 68810275Ssam name, sizebuf); 68910321Ssam usedefault = 1; 69010275Ssam return (fdopen(data, mode)); 69110275Ssam } 69210566Ssam if (usedefault) 69310422Ssam data_dest = his_addr; 69410422Ssam usedefault = 1; 69510275Ssam file = getdatasock(mode); 69610275Ssam if (file == NULL) { 69710275Ssam reply(425, "Can't create data socket (%s,%d): %s.", 69813247Ssam inet_ntoa(data_source.sin_addr), 69910275Ssam ntohs(data_source.sin_port), 70036304Skarels errno < sys_nerr ? sys_errlist[errno] : "unknown error"); 70110275Ssam return (NULL); 70210275Ssam } 70310275Ssam data = fileno(file); 70436304Skarels while (connect(data, (struct sockaddr *)&data_dest, 70536304Skarels sizeof (data_dest)) < 0) { 70611653Ssam if (errno == EADDRINUSE && retry < swaitmax) { 70726493Sminshall sleep((unsigned) swaitint); 70811653Ssam retry += swaitint; 70911653Ssam continue; 71011653Ssam } 71136304Skarels perror_reply(425, "Can't build data connection"); 71210275Ssam (void) fclose(file); 71310275Ssam data = -1; 71410275Ssam return (NULL); 71510275Ssam } 71636235Skarels reply(150, "Opening %s mode data connection for %s%s.", 71736235Skarels type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf); 71810275Ssam return (file); 71910275Ssam } 72010275Ssam 72110275Ssam /* 72210275Ssam * Tranfer the contents of "instr" to 72310275Ssam * "outstr" peer using the appropriate 72436933Skarels * encapsulation of the data subject 72510275Ssam * to Mode, Structure, and Type. 72610275Ssam * 72710275Ssam * NB: Form isn't handled. 72810275Ssam */ 72936446Sbostic send_data(instr, outstr, blksize) 73010275Ssam FILE *instr, *outstr; 73136446Sbostic off_t blksize; 73210275Ssam { 73336446Sbostic register int c, cnt; 73436446Sbostic register char *buf; 73536446Sbostic int netfd, filefd; 73610275Ssam 73726044Sminshall transflag++; 73826044Sminshall if (setjmp(urgcatch)) { 73926044Sminshall transflag = 0; 74036620Srick return; 74126044Sminshall } 74210275Ssam switch (type) { 74310275Ssam 74410275Ssam case TYPE_A: 74510275Ssam while ((c = getc(instr)) != EOF) { 74636933Skarels byte_count++; 74711220Ssam if (c == '\n') { 74836933Skarels if (ferror(outstr)) 74936620Srick goto data_err; 75027750Sminshall (void) putc('\r', outstr); 75111220Ssam } 75227750Sminshall (void) putc(c, outstr); 75310275Ssam } 75436933Skarels fflush(outstr); 75526044Sminshall transflag = 0; 75636933Skarels if (ferror(instr)) 75736933Skarels goto file_err; 75836933Skarels if (ferror(outstr)) 75936620Srick goto data_err; 76036620Srick reply(226, "Transfer complete."); 76136620Srick return; 76236446Sbostic 76310275Ssam case TYPE_I: 76410275Ssam case TYPE_L: 76536446Sbostic if ((buf = malloc((u_int)blksize)) == NULL) { 76636446Sbostic transflag = 0; 76736933Skarels perror_reply(451, "Local resource failure: malloc"); 76836933Skarels return; 76936446Sbostic } 77010275Ssam netfd = fileno(outstr); 77110275Ssam filefd = fileno(instr); 77236933Skarels while ((cnt = read(filefd, buf, (u_int)blksize)) > 0 && 77336620Srick write(netfd, buf, cnt) == cnt) 77436933Skarels byte_count += cnt; 77526044Sminshall transflag = 0; 77636446Sbostic (void)free(buf); 77736933Skarels if (cnt != 0) { 77836933Skarels if (cnt < 0) 77936933Skarels goto file_err; 78036620Srick goto data_err; 78136933Skarels } 78236620Srick reply(226, "Transfer complete."); 78336620Srick return; 78436620Srick default: 78536620Srick transflag = 0; 78636620Srick reply(550, "Unimplemented TYPE %d in send_data", type); 78736620Srick return; 78810275Ssam } 78936620Srick 79036620Srick data_err: 79126044Sminshall transflag = 0; 79236933Skarels perror_reply(426, "Data connection"); 79336933Skarels return; 79436933Skarels 79536933Skarels file_err: 79636933Skarels transflag = 0; 79736933Skarels perror_reply(551, "Error on input file"); 79810275Ssam } 79910275Ssam 80010275Ssam /* 80110275Ssam * Transfer data from peer to 80210275Ssam * "outstr" using the appropriate 80310275Ssam * encapulation of the data subject 80410275Ssam * to Mode, Structure, and Type. 80510275Ssam * 80610275Ssam * N.B.: Form isn't handled. 80710275Ssam */ 80810275Ssam receive_data(instr, outstr) 80910275Ssam FILE *instr, *outstr; 81010275Ssam { 81110275Ssam register int c; 81238134Srick int cnt, bare_lfs = 0; 81310275Ssam char buf[BUFSIZ]; 81410275Ssam 81526044Sminshall transflag++; 81626044Sminshall if (setjmp(urgcatch)) { 81726044Sminshall transflag = 0; 81836933Skarels return (-1); 81926044Sminshall } 82010275Ssam switch (type) { 82110275Ssam 82210275Ssam case TYPE_I: 82310275Ssam case TYPE_L: 82426044Sminshall while ((cnt = read(fileno(instr), buf, sizeof buf)) > 0) { 82536620Srick if (write(fileno(outstr), buf, cnt) != cnt) 82636933Skarels goto file_err; 82736933Skarels byte_count += cnt; 82826044Sminshall } 82936933Skarels if (cnt < 0) 83036620Srick goto data_err; 83126044Sminshall transflag = 0; 83236933Skarels return (0); 83310275Ssam 83410275Ssam case TYPE_E: 83527106Smckusick reply(553, "TYPE E not implemented."); 83626044Sminshall transflag = 0; 83727106Smckusick return (-1); 83810275Ssam 83910275Ssam case TYPE_A: 84010275Ssam while ((c = getc(instr)) != EOF) { 84136933Skarels byte_count++; 84238134Srick if (c == '\n') 84338134Srick bare_lfs++; 84427750Sminshall while (c == '\r') { 84536933Skarels if (ferror(outstr)) 84636620Srick goto data_err; 84736933Skarels if ((c = getc(instr)) != '\n') { 84827750Sminshall (void) putc ('\r', outstr); 84936933Skarels if (c == '\0' || c == EOF) 85036933Skarels goto contin2; 85136933Skarels } 85210275Ssam } 85336933Skarels (void) putc(c, outstr); 85436933Skarels contin2: ; 85510275Ssam } 85636620Srick fflush(outstr); 85736933Skarels if (ferror(instr)) 85836620Srick goto data_err; 85936933Skarels if (ferror(outstr)) 86036933Skarels goto file_err; 86126044Sminshall transflag = 0; 86238134Srick if (bare_lfs) { 86338134Srick lreply(230, "WARNING! %d bare linefeeds received in ASCII mode", bare_lfs); 86438134Srick printf(" File may not have transferred correctly.\r\n"); 86538134Srick } 86610275Ssam return (0); 86736620Srick default: 86836620Srick reply(550, "Unimplemented TYPE %d in receive_data", type); 86936620Srick transflag = 0; 87036933Skarels return (-1); 87110275Ssam } 87236620Srick 87336620Srick data_err: 87426044Sminshall transflag = 0; 87536933Skarels perror_reply(426, "Data Connection"); 87636933Skarels return (-1); 87736933Skarels 87836933Skarels file_err: 87936933Skarels transflag = 0; 88036933Skarels perror_reply(452, "Error writing file"); 88136933Skarels return (-1); 88210275Ssam } 88310275Ssam 88436933Skarels statfilecmd(filename) 88536933Skarels char *filename; 88636933Skarels { 88736933Skarels char line[BUFSIZ]; 88836933Skarels FILE *fin; 88936933Skarels int c; 89036933Skarels 89136933Skarels (void) sprintf(line, "/bin/ls -lgA %s", filename); 89236933Skarels fin = ftpd_popen(line, "r"); 89336933Skarels lreply(211, "status of %s:", filename); 89436933Skarels while ((c = getc(fin)) != EOF) { 89536933Skarels if (c == '\n') { 89636933Skarels if (ferror(stdout)){ 89736933Skarels perror_reply(421, "control connection"); 89836933Skarels (void) ftpd_pclose(fin); 89936933Skarels dologout(1); 90036933Skarels /* NOTREACHED */ 90136933Skarels } 90236933Skarels if (ferror(fin)) { 90336933Skarels perror_reply(551, filename); 90436933Skarels (void) ftpd_pclose(fin); 90536933Skarels return; 90636933Skarels } 90736933Skarels (void) putc('\r', stdout); 90836933Skarels } 90936933Skarels (void) putc(c, stdout); 91036933Skarels } 91136933Skarels (void) ftpd_pclose(fin); 91236933Skarels reply(211, "End of Status"); 91336933Skarels } 91436933Skarels 91536933Skarels statcmd() 91636933Skarels { 91736933Skarels struct sockaddr_in *sin; 91836933Skarels u_char *a, *p; 91936933Skarels 92036933Skarels lreply(211, "%s FTP server status:", hostname, version); 92136933Skarels printf(" %s\r\n", version); 92236933Skarels printf(" Connected to %s", remotehost); 92338134Srick if (!isdigit(remotehost[0])) 92436933Skarels printf(" (%s)", inet_ntoa(his_addr.sin_addr)); 92536933Skarels printf("\r\n"); 92636933Skarels if (logged_in) { 92736933Skarels if (guest) 92836933Skarels printf(" Logged in anonymously\r\n"); 92936933Skarels else 93036933Skarels printf(" Logged in as %s\r\n", pw->pw_name); 93136933Skarels } else if (askpasswd) 93236933Skarels printf(" Waiting for password\r\n"); 93336933Skarels else 93436933Skarels printf(" Waiting for user name\r\n"); 93536933Skarels printf(" TYPE: %s", typenames[type]); 93636933Skarels if (type == TYPE_A || type == TYPE_E) 93736933Skarels printf(", FORM: %s", formnames[form]); 93836933Skarels if (type == TYPE_L) 93936933Skarels #if NBBY == 8 94036933Skarels printf(" %d", NBBY); 94136933Skarels #else 94236933Skarels printf(" %d", bytesize); /* need definition! */ 94336933Skarels #endif 94436933Skarels printf("; STRUcture: %s; transfer MODE: %s\r\n", 94536933Skarels strunames[stru], modenames[mode]); 94636933Skarels if (data != -1) 94736933Skarels printf(" Data connection open\r\n"); 94836933Skarels else if (pdata != -1) { 94936933Skarels printf(" in Passive mode"); 95036933Skarels sin = &pasv_addr; 95136933Skarels goto printaddr; 95236933Skarels } else if (usedefault == 0) { 95336933Skarels printf(" PORT"); 95436933Skarels sin = &data_dest; 95536933Skarels printaddr: 95636933Skarels a = (u_char *) &sin->sin_addr; 95736933Skarels p = (u_char *) &sin->sin_port; 95836933Skarels #define UC(b) (((int) b) & 0xff) 95936933Skarels printf(" (%d,%d,%d,%d,%d,%d)\r\n", UC(a[0]), 96036933Skarels UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1])); 96136933Skarels #undef UC 96236933Skarels } else 96336933Skarels printf(" No data connection\r\n"); 96436933Skarels reply(211, "End of status"); 96536933Skarels } 96636933Skarels 96710275Ssam fatal(s) 96810275Ssam char *s; 96910275Ssam { 97010275Ssam reply(451, "Error in server: %s\n", s); 97110275Ssam reply(221, "Closing connection due to server error."); 97213247Ssam dologout(0); 97336620Srick /* NOTREACHED */ 97410275Ssam } 97510275Ssam 97636446Sbostic /* VARARGS2 */ 97736446Sbostic reply(n, fmt, p0, p1, p2, p3, p4, p5) 97810275Ssam int n; 97936446Sbostic char *fmt; 98010275Ssam { 98110275Ssam printf("%d ", n); 98236446Sbostic printf(fmt, p0, p1, p2, p3, p4, p5); 98310275Ssam printf("\r\n"); 98436435Sbostic (void)fflush(stdout); 98510275Ssam if (debug) { 98626493Sminshall syslog(LOG_DEBUG, "<--- %d ", n); 98736446Sbostic syslog(LOG_DEBUG, fmt, p0, p1, p2, p3, p4, p5); 98810275Ssam } 98936620Srick } 99010275Ssam 99136446Sbostic /* VARARGS2 */ 99236446Sbostic lreply(n, fmt, p0, p1, p2, p3, p4, p5) 99310275Ssam int n; 99436446Sbostic char *fmt; 99510275Ssam { 99636446Sbostic printf("%d- ", n); 99736446Sbostic printf(fmt, p0, p1, p2, p3, p4, p5); 99836446Sbostic printf("\r\n"); 99936435Sbostic (void)fflush(stdout); 100036446Sbostic if (debug) { 100136446Sbostic syslog(LOG_DEBUG, "<--- %d- ", n); 100236446Sbostic syslog(LOG_DEBUG, fmt, p0, p1, p2, p3, p4, p5); 100336446Sbostic } 100410275Ssam } 100510275Ssam 100610275Ssam ack(s) 100710275Ssam char *s; 100810275Ssam { 100927106Smckusick reply(250, "%s command successful.", s); 101010275Ssam } 101110275Ssam 101210275Ssam nack(s) 101310275Ssam char *s; 101410275Ssam { 101510275Ssam reply(502, "%s command not implemented.", s); 101610275Ssam } 101710275Ssam 101836304Skarels /* ARGSUSED */ 101926493Sminshall yyerror(s) 102026493Sminshall char *s; 102110275Ssam { 102226044Sminshall char *cp; 102326044Sminshall 102436551Sbostic if (cp = index(cbuf,'\n')) 102536551Sbostic *cp = '\0'; 102636933Skarels reply(500, "'%s': command not understood.", cbuf); 102710275Ssam } 102810275Ssam 102910275Ssam delete(name) 103010275Ssam char *name; 103110275Ssam { 103210275Ssam struct stat st; 103310275Ssam 103410275Ssam if (stat(name, &st) < 0) { 103536304Skarels perror_reply(550, name); 103610275Ssam return; 103710275Ssam } 103810275Ssam if ((st.st_mode&S_IFMT) == S_IFDIR) { 103910275Ssam if (rmdir(name) < 0) { 104036304Skarels perror_reply(550, name); 104110275Ssam return; 104210275Ssam } 104310275Ssam goto done; 104410275Ssam } 104510275Ssam if (unlink(name) < 0) { 104636304Skarels perror_reply(550, name); 104710275Ssam return; 104810275Ssam } 104910275Ssam done: 105010275Ssam ack("DELE"); 105110275Ssam } 105210275Ssam 105310275Ssam cwd(path) 105410275Ssam char *path; 105510275Ssam { 105636620Srick if (chdir(path) < 0) 105736304Skarels perror_reply(550, path); 105836620Srick else 105936620Srick ack("CWD"); 106010275Ssam } 106110275Ssam 106210303Ssam makedir(name) 106310275Ssam char *name; 106410275Ssam { 106536276Sbostic if (mkdir(name, 0777) < 0) 106636304Skarels perror_reply(550, name); 106736276Sbostic else 106836276Sbostic reply(257, "MKD command successful."); 106910275Ssam } 107010275Ssam 107110303Ssam removedir(name) 107210275Ssam char *name; 107310275Ssam { 107436620Srick if (rmdir(name) < 0) 107536304Skarels perror_reply(550, name); 107636620Srick else 107736620Srick ack("RMD"); 107810275Ssam } 107910275Ssam 108010303Ssam pwd() 108110275Ssam { 108210303Ssam char path[MAXPATHLEN + 1]; 108336304Skarels extern char *getwd(); 108410275Ssam 108536620Srick if (getwd(path) == (char *)NULL) 108627106Smckusick reply(550, "%s.", path); 108736620Srick else 108836620Srick reply(257, "\"%s\" is current directory.", path); 108910275Ssam } 109010275Ssam 109110275Ssam char * 109210275Ssam renamefrom(name) 109310275Ssam char *name; 109410275Ssam { 109510275Ssam struct stat st; 109610275Ssam 109710275Ssam if (stat(name, &st) < 0) { 109836304Skarels perror_reply(550, name); 109910275Ssam return ((char *)0); 110010275Ssam } 110110303Ssam reply(350, "File exists, ready for destination name"); 110210275Ssam return (name); 110310275Ssam } 110410275Ssam 110510275Ssam renamecmd(from, to) 110610275Ssam char *from, *to; 110710275Ssam { 110836620Srick if (rename(from, to) < 0) 110936304Skarels perror_reply(550, "rename"); 111036620Srick else 111136620Srick ack("RNTO"); 111210275Ssam } 111310275Ssam 111410275Ssam dolog(sin) 111510275Ssam struct sockaddr_in *sin; 111610275Ssam { 111736304Skarels struct hostent *hp = gethostbyaddr((char *)&sin->sin_addr, 111810275Ssam sizeof (struct in_addr), AF_INET); 111936304Skarels time_t t, time(); 112026493Sminshall extern char *ctime(); 112110275Ssam 112236304Skarels if (hp) 112326493Sminshall (void) strncpy(remotehost, hp->h_name, sizeof (remotehost)); 112436304Skarels else 112526493Sminshall (void) strncpy(remotehost, inet_ntoa(sin->sin_addr), 112613247Ssam sizeof (remotehost)); 112736620Srick #ifdef SETPROCTITLE 112836933Skarels sprintf(proctitle, "%s: connected", remotehost); 112936933Skarels setproctitle(proctitle); 113036620Srick #endif /* SETPROCTITLE */ 113136933Skarels 113236933Skarels if (logging) { 113336933Skarels t = time((time_t *) 0); 113436933Skarels syslog(LOG_INFO, "connection from %s at %s", 113536933Skarels remotehost, ctime(&t)); 113636933Skarels } 113710275Ssam } 113810695Ssam 113910695Ssam /* 114013247Ssam * Record logout in wtmp file 114113247Ssam * and exit with supplied status. 114213247Ssam */ 114313247Ssam dologout(status) 114413247Ssam int status; 114513247Ssam { 114617580Ssam if (logged_in) { 114736304Skarels (void) seteuid((uid_t)0); 114835672Sbostic logwtmp(ttyline, "", ""); 114913247Ssam } 115014436Ssam /* beware of flushing buffers after a SIGPIPE */ 115114436Ssam _exit(status); 115213247Ssam } 115313247Ssam 115426044Sminshall myoob() 115526044Sminshall { 115627750Sminshall char *cp; 115726044Sminshall 115827750Sminshall /* only process if transfer occurring */ 115936304Skarels if (!transflag) 116026044Sminshall return; 116127750Sminshall cp = tmpline; 116227750Sminshall if (getline(cp, 7, stdin) == NULL) { 116336304Skarels reply(221, "You could at least say goodbye."); 116427750Sminshall dologout(0); 116526044Sminshall } 116626044Sminshall upper(cp); 116736933Skarels if (strcmp(cp, "ABOR\r\n") == 0) { 116836933Skarels tmpline[0] = '\0'; 116936933Skarels reply(426, "Transfer aborted. Data connection closed."); 117036933Skarels reply(226, "Abort successful"); 117136933Skarels longjmp(urgcatch, 1); 117236933Skarels } 117336933Skarels if (strcmp(cp, "STAT\r\n") == 0) { 117436933Skarels if (file_size != (off_t) -1) 117536933Skarels reply(213, "Status: %lu of %lu bytes transferred", 117636933Skarels byte_count, file_size); 117736933Skarels else 117836933Skarels reply(213, "Status: %lu bytes transferred", byte_count); 117936933Skarels } 118026044Sminshall } 118126044Sminshall 118227106Smckusick /* 118336620Srick * Note: a response of 425 is not mentioned as a possible response to 118436620Srick * the PASV command in RFC959. However, it has been blessed as 118536620Srick * a legitimate response by Jon Postel in a telephone conversation 118636620Srick * with Rick Adams on 25 Jan 89. 118727106Smckusick */ 118826044Sminshall passive() 118926044Sminshall { 119026044Sminshall int len; 119126044Sminshall register char *p, *a; 119226044Sminshall 119326044Sminshall pdata = socket(AF_INET, SOCK_STREAM, 0); 119426044Sminshall if (pdata < 0) { 119536620Srick perror_reply(425, "Can't open passive connection"); 119626044Sminshall return; 119726044Sminshall } 119836933Skarels pasv_addr = ctrl_addr; 119936933Skarels pasv_addr.sin_port = 0; 120036304Skarels (void) seteuid((uid_t)0); 120136933Skarels if (bind(pdata, (struct sockaddr *)&pasv_addr, sizeof(pasv_addr)) < 0) { 120236304Skarels (void) seteuid((uid_t)pw->pw_uid); 120336620Srick goto pasv_error; 120426044Sminshall } 120536304Skarels (void) seteuid((uid_t)pw->pw_uid); 120636933Skarels len = sizeof(pasv_addr); 120736933Skarels if (getsockname(pdata, (struct sockaddr *) &pasv_addr, &len) < 0) 120836620Srick goto pasv_error; 120936620Srick if (listen(pdata, 1) < 0) 121036620Srick goto pasv_error; 121136933Skarels a = (char *) &pasv_addr.sin_addr; 121236933Skarels p = (char *) &pasv_addr.sin_port; 121326044Sminshall 121426044Sminshall #define UC(b) (((int) b) & 0xff) 121526044Sminshall 121626044Sminshall reply(227, "Entering Passive Mode (%d,%d,%d,%d,%d,%d)", UC(a[0]), 121726044Sminshall UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1])); 121836620Srick return; 121936620Srick 122036620Srick pasv_error: 122136620Srick (void) close(pdata); 122236620Srick pdata = -1; 122336620Srick perror_reply(425, "Can't open passive connection"); 122436620Srick return; 122526044Sminshall } 122626044Sminshall 122736304Skarels /* 122836304Skarels * Generate unique name for file with basename "local". 122936304Skarels * The file named "local" is already known to exist. 123036304Skarels * Generates failure reply on error. 123136304Skarels */ 123226044Sminshall char * 123326044Sminshall gunique(local) 123426044Sminshall char *local; 123526044Sminshall { 123626044Sminshall static char new[MAXPATHLEN]; 123736304Skarels struct stat st; 123826044Sminshall char *cp = rindex(local, '/'); 123936933Skarels int count = 0; 124026044Sminshall 124136304Skarels if (cp) 124226044Sminshall *cp = '\0'; 124336620Srick if (stat(cp ? local : ".", &st) < 0) { 124436933Skarels perror_reply(553, cp ? local : "."); 124526044Sminshall return((char *) 0); 124626044Sminshall } 124736620Srick if (cp) 124836620Srick *cp = '/'; 124926044Sminshall (void) strcpy(new, local); 125026044Sminshall cp = new + strlen(new); 125126044Sminshall *cp++ = '.'; 125236304Skarels for (count = 1; count < 100; count++) { 125336304Skarels (void) sprintf(cp, "%d", count); 125436304Skarels if (stat(new, &st) < 0) 125536304Skarels return(new); 125626044Sminshall } 125736304Skarels reply(452, "Unique file name cannot be created."); 125836304Skarels return((char *) 0); 125926044Sminshall } 126036304Skarels 126136304Skarels /* 126236304Skarels * Format and send reply containing system error number. 126336304Skarels */ 126436304Skarels perror_reply(code, string) 126536304Skarels int code; 126636304Skarels char *string; 126736304Skarels { 126836304Skarels if (errno < sys_nerr) 126936304Skarels reply(code, "%s: %s.", string, sys_errlist[errno]); 127036304Skarels else 127136304Skarels reply(code, "%s: unknown error %d.", string, errno); 127236304Skarels } 127336620Srick 127436620Srick static char *onefile[] = { 127536620Srick "", 127636620Srick 0 127736620Srick }; 127836620Srick 127936620Srick send_file_list(whichfiles) 128036620Srick char *whichfiles; 128136620Srick { 128236620Srick struct stat st; 128336620Srick DIR *dirp = NULL; 128436620Srick struct direct *dir; 128536620Srick FILE *dout = NULL; 128636620Srick register char **dirlist, *dirname; 128737459Skarels int simple = 0; 128836620Srick char *strpbrk(); 128936620Srick 129036620Srick if (strpbrk(whichfiles, "~{[*?") != NULL) { 129136620Srick extern char **glob(), *globerr; 129236933Skarels 129336620Srick globerr = NULL; 129436620Srick dirlist = glob(whichfiles); 129536620Srick if (globerr != NULL) { 129636620Srick reply(550, globerr); 129736620Srick return; 129836620Srick } else if (dirlist == NULL) { 129936620Srick errno = ENOENT; 130036620Srick perror_reply(550, whichfiles); 130136620Srick return; 130236620Srick } 130336620Srick } else { 130436620Srick onefile[0] = whichfiles; 130536620Srick dirlist = onefile; 130637459Skarels simple = 1; 130736620Srick } 130836933Skarels 130936933Skarels if (setjmp(urgcatch)) { 131036933Skarels transflag = 0; 131136933Skarels return; 131236933Skarels } 131336620Srick while (dirname = *dirlist++) { 131436620Srick if (stat(dirname, &st) < 0) { 131536933Skarels /* 131636933Skarels * If user typed "ls -l", etc, and the client 131736933Skarels * used NLST, do what the user meant. 131836933Skarels */ 131936933Skarels if (dirname[0] == '-' && *dirlist == NULL && 132036933Skarels transflag == 0) { 132136933Skarels retrieve("/bin/ls %s", dirname); 132236933Skarels return; 132336933Skarels } 132436620Srick perror_reply(550, whichfiles); 132536620Srick if (dout != NULL) { 132636620Srick (void) fclose(dout); 132736933Skarels transflag = 0; 132836620Srick data = -1; 132936620Srick pdata = -1; 133036620Srick } 133136620Srick return; 133236620Srick } 133336933Skarels 133436620Srick if ((st.st_mode&S_IFMT) == S_IFREG) { 133536620Srick if (dout == NULL) { 133637459Skarels dout = dataconn("file list", (off_t)-1, "w"); 133736620Srick if (dout == NULL) 133836620Srick return; 133936933Skarels transflag++; 134036620Srick } 1341*38158Srick fprintf(dout, "%s%s\n", dirname, 1342*38158Srick type == TYPE_A ? "\r" : ""); 134336933Skarels byte_count += strlen(dirname) + 1; 134436620Srick continue; 134536933Skarels } else if ((st.st_mode&S_IFMT) != S_IFDIR) 134636620Srick continue; 134736620Srick 134836620Srick if ((dirp = opendir(dirname)) == NULL) 134936620Srick continue; 135036620Srick 135136620Srick while ((dir = readdir(dirp)) != NULL) { 135236933Skarels char nbuf[MAXPATHLEN]; 135336620Srick 135436620Srick if (dir->d_name[0] == '.' && dir->d_namlen == 1) 135536620Srick continue; 135636933Skarels if (dir->d_name[0] == '.' && dir->d_name[1] == '.' && 135736933Skarels dir->d_namlen == 2) 135836620Srick continue; 135936620Srick 136036933Skarels sprintf(nbuf, "%s/%s", dirname, dir->d_name); 136136933Skarels 136236620Srick /* 136336933Skarels * We have to do a stat to insure it's 136436933Skarels * not a directory or special file. 136536620Srick */ 136637459Skarels if (simple || (stat(nbuf, &st) == 0 && 136737459Skarels (st.st_mode&S_IFMT) == S_IFREG)) { 136836620Srick if (dout == NULL) { 136937459Skarels dout = dataconn("file list", (off_t)-1, 137036620Srick "w"); 137136620Srick if (dout == NULL) 137236620Srick return; 137336933Skarels transflag++; 137436620Srick } 137536620Srick if (nbuf[0] == '.' && nbuf[1] == '/') 1376*38158Srick fprintf(dout, "%s%s\n", &nbuf[2], 1377*38158Srick type == TYPE_A ? "\r" : ""); 137836620Srick else 1379*38158Srick fprintf(dout, "%s%s\n", nbuf, 1380*38158Srick type == TYPE_A ? "\r" : ""); 138136933Skarels byte_count += strlen(nbuf) + 1; 138236620Srick } 138336620Srick } 138436620Srick (void) closedir(dirp); 138536620Srick } 138636620Srick 138736933Skarels if (dout == NULL) 138836933Skarels reply(550, "No files found."); 138936933Skarels else if (ferror(dout) != 0) 139036933Skarels perror_reply(550, "Data connection"); 139136933Skarels else 139236620Srick reply(226, "Transfer complete."); 139336620Srick 139436933Skarels transflag = 0; 139536933Skarels if (dout != NULL) 139636620Srick (void) fclose(dout); 139736620Srick data = -1; 139836620Srick pdata = -1; 139936620Srick } 140036620Srick 140136620Srick #ifdef SETPROCTITLE 140236620Srick /* 140336620Srick * clobber argv so ps will show what we're doing. 140436620Srick * (stolen from sendmail) 140536620Srick * warning, since this is usually started from inetd.conf, it 140636620Srick * often doesn't have much of an environment or arglist to overwrite. 140736620Srick */ 140836620Srick 140936620Srick /*VARARGS1*/ 141036620Srick setproctitle(fmt, a, b, c) 141136620Srick char *fmt; 141236620Srick { 141336620Srick register char *p, *bp, ch; 141436620Srick register int i; 141536620Srick char buf[BUFSIZ]; 141636620Srick 141736620Srick (void) sprintf(buf, fmt, a, b, c); 141836620Srick 141936620Srick /* make ps print our process name */ 142036620Srick p = Argv[0]; 142136620Srick *p++ = '-'; 142236620Srick 142336620Srick i = strlen(buf); 142436620Srick if (i > LastArgv - p - 2) { 142536620Srick i = LastArgv - p - 2; 142636620Srick buf[i] = '\0'; 142736620Srick } 142836620Srick bp = buf; 142936620Srick while (ch = *bp++) 143036620Srick if (ch != '\n' && ch != '\r') 143136620Srick *p++ = ch; 143236620Srick while (p < LastArgv) 143336620Srick *p++ = ' '; 143436620Srick } 143536620Srick #endif /* SETPROCTITLE */ 1436