122499Sdist /* 2*44339Skarels * Copyright (c) 1985, 1988, 1990 Regents of the University of California. 333738Sbostic * All rights reserved. 433738Sbostic * 542666Sbostic * %sccs.include.redist.c% 622499Sdist */ 722499Sdist 810275Ssam #ifndef lint 922499Sdist char copyright[] = 10*44339Skarels "@(#) Copyright (c) 1985, 1988, 1990 Regents of the University of California.\n\ 1122499Sdist All rights reserved.\n"; 1233738Sbostic #endif /* not lint */ 1310275Ssam 1422499Sdist #ifndef lint 15*44339Skarels static char sccsid[] = "@(#)ftpd.c 5.37 (Berkeley) 06/27/90"; 1633738Sbostic #endif /* not lint */ 1722499Sdist 1810275Ssam /* 1910275Ssam * FTP server. 2010275Ssam */ 2110303Ssam #include <sys/param.h> 2210275Ssam #include <sys/stat.h> 2310275Ssam #include <sys/ioctl.h> 2410275Ssam #include <sys/socket.h> 2513247Ssam #include <sys/file.h> 2613595Ssam #include <sys/wait.h> 2736620Srick #include <sys/dir.h> 2810275Ssam 2910275Ssam #include <netinet/in.h> 30*44339Skarels #include <netinet/in_systm.h> 31*44339Skarels #include <netinet/ip.h> 3210275Ssam 3336933Skarels #define FTP_NAMES 3413034Ssam #include <arpa/ftp.h> 3513211Sroot #include <arpa/inet.h> 3626044Sminshall #include <arpa/telnet.h> 3713034Ssam 3836933Skarels #include <ctype.h> 3910275Ssam #include <stdio.h> 4010275Ssam #include <signal.h> 4110275Ssam #include <pwd.h> 4210275Ssam #include <setjmp.h> 4310275Ssam #include <netdb.h> 4410423Ssam #include <errno.h> 4542031Sbostic #include <string.h> 4626493Sminshall #include <syslog.h> 4736435Sbostic #include <varargs.h> 4837459Skarels #include "pathnames.h" 4910275Ssam 5010695Ssam /* 5110695Ssam * File containing login names 5210695Ssam * NOT to be used on this machine. 5310695Ssam * Commonly used to disallow uucp. 5410695Ssam */ 5510275Ssam extern int errno; 5610275Ssam extern char *crypt(); 5710275Ssam extern char version[]; 5810275Ssam extern char *home; /* pointer to home directory for glob */ 5936276Sbostic extern FILE *ftpd_popen(), *fopen(), *freopen(); 6036304Skarels extern int ftpd_pclose(), fclose(); 6126044Sminshall extern char *getline(); 6226044Sminshall extern char cbuf[]; 6337459Skarels extern off_t restart_point; 6410275Ssam 6510275Ssam struct sockaddr_in ctrl_addr; 6610275Ssam struct sockaddr_in data_source; 6710275Ssam struct sockaddr_in data_dest; 6810275Ssam struct sockaddr_in his_addr; 6936933Skarels struct sockaddr_in pasv_addr; 7010275Ssam 7110275Ssam int data; 7226044Sminshall jmp_buf errcatch, urgcatch; 7310275Ssam int logged_in; 7410275Ssam struct passwd *pw; 7510275Ssam int debug; 7626493Sminshall int timeout = 900; /* timeout after 15 minutes of inactivity */ 7736933Skarels int maxtimeout = 7200;/* don't allow idle time to be set beyond 2 hours */ 7811757Ssam int logging; 7910275Ssam int guest; 8010275Ssam int type; 8110275Ssam int form; 8210275Ssam int stru; /* avoid C keyword */ 8310275Ssam int mode; 8410321Ssam int usedefault = 1; /* for data transfers */ 8536304Skarels int pdata = -1; /* for passive mode */ 8626044Sminshall int transflag; 8736933Skarels off_t file_size; 8836933Skarels off_t byte_count; 8936933Skarels #if !defined(CMASK) || CMASK == 0 9036933Skarels #undef CMASK 9136933Skarels #define CMASK 027 9236933Skarels #endif 9336933Skarels int defumask = CMASK; /* default umask value */ 9426044Sminshall char tmpline[7]; 9536276Sbostic char hostname[MAXHOSTNAMELEN]; 9636276Sbostic char remotehost[MAXHOSTNAMELEN]; 9710275Ssam 9811653Ssam /* 9911653Ssam * Timeout intervals for retrying connections 10011653Ssam * to hosts that don't accept PORT cmds. This 10111653Ssam * is a kludge, but given the problems with TCP... 10211653Ssam */ 10311653Ssam #define SWAITMAX 90 /* wait at most 90 seconds */ 10411653Ssam #define SWAITINT 5 /* interval between retries */ 10511653Ssam 10611653Ssam int swaitmax = SWAITMAX; 10711653Ssam int swaitint = SWAITINT; 10811653Ssam 10910275Ssam int lostconn(); 11026044Sminshall int myoob(); 11110275Ssam FILE *getdatasock(), *dataconn(); 11210275Ssam 11336620Srick #ifdef SETPROCTITLE 11436620Srick char **Argv = NULL; /* pointer to argument vector */ 11536620Srick char *LastArgv = NULL; /* end of argv */ 11636933Skarels char proctitle[BUFSIZ]; /* initial part of title */ 11736620Srick #endif /* SETPROCTITLE */ 11836620Srick 11936620Srick main(argc, argv, envp) 12010275Ssam int argc; 12110275Ssam char *argv[]; 12236620Srick char **envp; 12310275Ssam { 124*44339Skarels int addrlen, on = 1, tos; 12510275Ssam char *cp; 12610275Ssam 12716339Skarels addrlen = sizeof (his_addr); 12836304Skarels if (getpeername(0, (struct sockaddr *)&his_addr, &addrlen) < 0) { 12926493Sminshall syslog(LOG_ERR, "getpeername (%s): %m",argv[0]); 13010275Ssam exit(1); 13110275Ssam } 13216339Skarels addrlen = sizeof (ctrl_addr); 13336304Skarels if (getsockname(0, (struct sockaddr *)&ctrl_addr, &addrlen) < 0) { 13426493Sminshall syslog(LOG_ERR, "getsockname (%s): %m",argv[0]); 13516339Skarels exit(1); 13616339Skarels } 137*44339Skarels #ifdef IP_TOS 138*44339Skarels tos = IPTOS_LOWDELAY; 139*44339Skarels if (setsockopt(0, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(int)) < 0) 140*44339Skarels syslog(LOG_WARNING, "setsockopt (IP_TOS): %m"); 141*44339Skarels #endif 14216339Skarels data_source.sin_port = htons(ntohs(ctrl_addr.sin_port) - 1); 14310275Ssam debug = 0; 14426493Sminshall openlog("ftpd", LOG_PID, LOG_DAEMON); 14536620Srick #ifdef SETPROCTITLE 14636620Srick /* 14736620Srick * Save start and extent of argv for setproctitle. 14836620Srick */ 14936620Srick Argv = argv; 15036620Srick while (*envp) 15136620Srick envp++; 15236620Srick LastArgv = envp[-1] + strlen(envp[-1]); 15336620Srick #endif /* SETPROCTITLE */ 15436620Srick 15510275Ssam argc--, argv++; 15610275Ssam while (argc > 0 && *argv[0] == '-') { 15710275Ssam for (cp = &argv[0][1]; *cp; cp++) switch (*cp) { 15810275Ssam 15911653Ssam case 'v': 16011653Ssam debug = 1; 16111653Ssam break; 16211653Ssam 16310275Ssam case 'd': 16410275Ssam debug = 1; 16510275Ssam break; 16610275Ssam 16711757Ssam case 'l': 16811757Ssam logging = 1; 16911757Ssam break; 17011757Ssam 17111653Ssam case 't': 17211653Ssam timeout = atoi(++cp); 17336933Skarels if (maxtimeout < timeout) 17436933Skarels maxtimeout = timeout; 17511653Ssam goto nextopt; 17611653Ssam 17736933Skarels case 'T': 17836933Skarels maxtimeout = atoi(++cp); 17936933Skarels if (timeout > maxtimeout) 18036933Skarels timeout = maxtimeout; 18136933Skarels goto nextopt; 18236933Skarels 18336933Skarels case 'u': 18436933Skarels { 18536933Skarels int val = 0; 18636933Skarels 18736933Skarels while (*++cp && *cp >= '0' && *cp <= '9') 18836933Skarels val = val*8 + *cp - '0'; 18936933Skarels if (*cp) 19036933Skarels fprintf(stderr, "ftpd: Bad value for -u\n"); 19136933Skarels else 19236933Skarels defumask = val; 19336933Skarels goto nextopt; 19436933Skarels } 19536933Skarels 19610275Ssam default: 19716339Skarels fprintf(stderr, "ftpd: Unknown flag -%c ignored.\n", 19816339Skarels *cp); 19910275Ssam break; 20010275Ssam } 20111653Ssam nextopt: 20210275Ssam argc--, argv++; 20310275Ssam } 20437459Skarels (void) freopen(_PATH_DEVNULL, "w", stderr); 20526493Sminshall (void) signal(SIGPIPE, lostconn); 20626493Sminshall (void) signal(SIGCHLD, SIG_IGN); 20735691Sbostic if ((int)signal(SIGURG, myoob) < 0) 20826493Sminshall syslog(LOG_ERR, "signal: %m"); 20935691Sbostic 21038134Srick /* Try to handle urgent data inline */ 21127750Sminshall #ifdef SO_OOBINLINE 21236276Sbostic if (setsockopt(0, SOL_SOCKET, SO_OOBINLINE, (char *)&on, sizeof(on)) < 0) 21327750Sminshall syslog(LOG_ERR, "setsockopt: %m"); 21436276Sbostic #endif 21538134Srick 21636933Skarels #ifdef F_SETOWN 21736304Skarels if (fcntl(fileno(stdin), F_SETOWN, getpid()) == -1) 21836304Skarels syslog(LOG_ERR, "fcntl F_SETOWN: %m"); 21936933Skarels #endif 22016760Slepreau dolog(&his_addr); 22116339Skarels /* 22216339Skarels * Set up default state 22316339Skarels */ 22416339Skarels data = -1; 22516339Skarels type = TYPE_A; 22616339Skarels form = FORM_N; 22716339Skarels stru = STRU_F; 22816339Skarels mode = MODE_S; 22926044Sminshall tmpline[0] = '\0'; 23026493Sminshall (void) gethostname(hostname, sizeof (hostname)); 23136276Sbostic reply(220, "%s FTP server (%s) ready.", hostname, version); 23236304Skarels (void) setjmp(errcatch); 23336304Skarels for (;;) 23426493Sminshall (void) yyparse(); 23536620Srick /* NOTREACHED */ 23610275Ssam } 23710419Ssam 23810275Ssam lostconn() 23910275Ssam { 24010275Ssam 24114089Ssam if (debug) 24226493Sminshall syslog(LOG_DEBUG, "lost connection"); 24314089Ssam dologout(-1); 24410275Ssam } 24510275Ssam 24635672Sbostic static char ttyline[20]; 24735672Sbostic 24836185Sbostic /* 24936185Sbostic * Helper function for sgetpwnam(). 25036185Sbostic */ 25136185Sbostic char * 25236185Sbostic sgetsave(s) 25336185Sbostic char *s; 25436185Sbostic { 25536185Sbostic char *malloc(); 25636185Sbostic char *new = malloc((unsigned) strlen(s) + 1); 25736933Skarels 25836185Sbostic if (new == NULL) { 25936620Srick perror_reply(421, "Local resource failure: malloc"); 26036185Sbostic dologout(1); 26136620Srick /* NOTREACHED */ 26236185Sbostic } 26336185Sbostic (void) strcpy(new, s); 26436185Sbostic return (new); 26536185Sbostic } 26636185Sbostic 26736185Sbostic /* 26836185Sbostic * Save the result of a getpwnam. Used for USER command, since 26936185Sbostic * the data returned must not be clobbered by any other command 27036185Sbostic * (e.g., globbing). 27136185Sbostic */ 27236185Sbostic struct passwd * 27336185Sbostic sgetpwnam(name) 27436185Sbostic char *name; 27536185Sbostic { 27636185Sbostic static struct passwd save; 27736185Sbostic register struct passwd *p; 27836185Sbostic char *sgetsave(); 27936185Sbostic 28036185Sbostic if ((p = getpwnam(name)) == NULL) 28136185Sbostic return (p); 28236185Sbostic if (save.pw_name) { 28336185Sbostic free(save.pw_name); 28436185Sbostic free(save.pw_passwd); 28536185Sbostic free(save.pw_gecos); 28636185Sbostic free(save.pw_dir); 28736185Sbostic free(save.pw_shell); 28836185Sbostic } 28936185Sbostic save = *p; 29036185Sbostic save.pw_name = sgetsave(p->pw_name); 29136185Sbostic save.pw_passwd = sgetsave(p->pw_passwd); 29236185Sbostic save.pw_gecos = sgetsave(p->pw_gecos); 29336185Sbostic save.pw_dir = sgetsave(p->pw_dir); 29436185Sbostic save.pw_shell = sgetsave(p->pw_shell); 29536185Sbostic return (&save); 29636185Sbostic } 29736185Sbostic 29836304Skarels int login_attempts; /* number of failed login attempts */ 29936304Skarels int askpasswd; /* had user command, ask for passwd */ 30036304Skarels 30136304Skarels /* 30236304Skarels * USER command. 30342327Sbostic * Sets global passwd pointer pw if named account exists and is acceptable; 30442327Sbostic * sets askpasswd if a PASS command is expected. If logged in previously, 30542327Sbostic * need to reset state. If name is "ftp" or "anonymous", the name is not in 30642327Sbostic * _PATH_FTPUSERS, and ftp account exists, set guest and pw, then just return. 30742327Sbostic * If account doesn't exist, ask for passwd anyway. Otherwise, check user 30842327Sbostic * requesting login privileges. Disallow anyone who does not have a standard 30942327Sbostic * shell as returned by getusershell(). Disallow anyone mentioned in the file 31042327Sbostic * _PATH_FTPUSERS to allow people such as root and uucp to be avoided. 31136304Skarels */ 31236304Skarels user(name) 31336304Skarels char *name; 31436304Skarels { 31536304Skarels register char *cp; 31636304Skarels char *shell; 31740183Smckusick char *getusershell(); 31836304Skarels 31936304Skarels if (logged_in) { 32036304Skarels if (guest) { 32136304Skarels reply(530, "Can't change user from guest login."); 32236304Skarels return; 32336304Skarels } 32436304Skarels end_login(); 32536304Skarels } 32636304Skarels 32736304Skarels guest = 0; 32836304Skarels if (strcmp(name, "ftp") == 0 || strcmp(name, "anonymous") == 0) { 32940183Smckusick if (checkuser("ftp") || checkuser("anonymous")) 33040155Smckusick reply(530, "User %s access denied.", name); 33140155Smckusick else if ((pw = sgetpwnam("ftp")) != NULL) { 33236304Skarels guest = 1; 33336304Skarels askpasswd = 1; 33436304Skarels reply(331, "Guest login ok, send ident as password."); 33536933Skarels } else 33636304Skarels reply(530, "User %s unknown.", name); 33736304Skarels return; 33836304Skarels } 33936304Skarels if (pw = sgetpwnam(name)) { 34036304Skarels if ((shell = pw->pw_shell) == NULL || *shell == 0) 34137459Skarels shell = _PATH_BSHELL; 34236304Skarels while ((cp = getusershell()) != NULL) 34336304Skarels if (strcmp(cp, shell) == 0) 34436304Skarels break; 34536304Skarels endusershell(); 34640183Smckusick if (cp == NULL || checkuser(name)) { 34736304Skarels reply(530, "User %s access denied.", name); 34836933Skarels if (logging) 34936933Skarels syslog(LOG_NOTICE, 35036933Skarels "FTP LOGIN REFUSED FROM %s, %s", 35136933Skarels remotehost, name); 35236304Skarels pw = (struct passwd *) NULL; 35336304Skarels return; 35436304Skarels } 35536304Skarels } 35636304Skarels reply(331, "Password required for %s.", name); 35736304Skarels askpasswd = 1; 35836304Skarels /* 35936304Skarels * Delay before reading passwd after first failed 36036304Skarels * attempt to slow down passwd-guessing programs. 36136304Skarels */ 36236304Skarels if (login_attempts) 36336304Skarels sleep((unsigned) login_attempts); 36436304Skarels } 36536304Skarels 36636304Skarels /* 36740183Smckusick * Check if a user is in the file _PATH_FTPUSERS 36840183Smckusick */ 36940183Smckusick checkuser(name) 37040183Smckusick char *name; 37140183Smckusick { 37242327Sbostic register FILE *fd; 37342327Sbostic register char *p; 37442327Sbostic char line[BUFSIZ]; 37540183Smckusick 37640183Smckusick if ((fd = fopen(_PATH_FTPUSERS, "r")) != NULL) { 37742327Sbostic while (fgets(line, sizeof(line), fd) != NULL) 37842327Sbostic if ((p = index(line, '\n')) != NULL) { 37942327Sbostic *p = '\0'; 38042327Sbostic if (line[0] == '#') 38142327Sbostic continue; 38242327Sbostic if (strcmp(line, name) == 0) 38342327Sbostic return (1); 38442327Sbostic } 38540183Smckusick (void) fclose(fd); 38640183Smckusick } 38740183Smckusick return (0); 38840183Smckusick } 38940183Smckusick 39040183Smckusick /* 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); 646*44339Skarels #ifdef IP_TOS 647*44339Skarels on = IPTOS_THROUGHPUT; 648*44339Skarels if (setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0) 649*44339Skarels syslog(LOG_WARNING, "setsockopt (IP_TOS): %m"); 650*44339Skarels #endif 65110275Ssam return (fdopen(s, mode)); 65210602Ssam bad: 65336304Skarels (void) seteuid((uid_t)pw->pw_uid); 65426493Sminshall (void) close(s); 65510602Ssam return (NULL); 65610275Ssam } 65710275Ssam 65810275Ssam FILE * 65910275Ssam dataconn(name, size, mode) 66010275Ssam char *name; 66111653Ssam off_t size; 66210275Ssam char *mode; 66310275Ssam { 66410275Ssam char sizebuf[32]; 66510275Ssam FILE *file; 666*44339Skarels int retry = 0, tos; 66710275Ssam 66836933Skarels file_size = size; 66936933Skarels byte_count = 0; 67036304Skarels if (size != (off_t) -1) 67126493Sminshall (void) sprintf (sizebuf, " (%ld bytes)", size); 67210275Ssam else 67310275Ssam (void) strcpy(sizebuf, ""); 67436304Skarels if (pdata >= 0) { 67526044Sminshall struct sockaddr_in from; 67626044Sminshall int s, fromlen = sizeof(from); 67726044Sminshall 67836304Skarels s = accept(pdata, (struct sockaddr *)&from, &fromlen); 67926044Sminshall if (s < 0) { 68026044Sminshall reply(425, "Can't open data connection."); 68126044Sminshall (void) close(pdata); 68226044Sminshall pdata = -1; 68326044Sminshall return(NULL); 68426044Sminshall } 68526044Sminshall (void) close(pdata); 68626044Sminshall pdata = s; 687*44339Skarels #ifdef IP_TOS 688*44339Skarels tos = IPTOS_LOWDELAY; 689*44339Skarels (void) setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&tos, 690*44339Skarels sizeof(int)); 691*44339Skarels #endif 69236235Skarels reply(150, "Opening %s mode data connection for %s%s.", 69336235Skarels type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf); 69426044Sminshall return(fdopen(pdata, mode)); 69526044Sminshall } 69610275Ssam if (data >= 0) { 69710275Ssam reply(125, "Using existing data connection for %s%s.", 69810275Ssam name, sizebuf); 69910321Ssam usedefault = 1; 70010275Ssam return (fdopen(data, mode)); 70110275Ssam } 70210566Ssam if (usedefault) 70310422Ssam data_dest = his_addr; 70410422Ssam usedefault = 1; 70510275Ssam file = getdatasock(mode); 70610275Ssam if (file == NULL) { 70710275Ssam reply(425, "Can't create data socket (%s,%d): %s.", 70813247Ssam inet_ntoa(data_source.sin_addr), 70942412Sbostic ntohs(data_source.sin_port), strerror(errno)); 71010275Ssam return (NULL); 71110275Ssam } 71210275Ssam data = fileno(file); 71336304Skarels while (connect(data, (struct sockaddr *)&data_dest, 71436304Skarels sizeof (data_dest)) < 0) { 71511653Ssam if (errno == EADDRINUSE && retry < swaitmax) { 71626493Sminshall sleep((unsigned) swaitint); 71711653Ssam retry += swaitint; 71811653Ssam continue; 71911653Ssam } 72036304Skarels perror_reply(425, "Can't build data connection"); 72110275Ssam (void) fclose(file); 72210275Ssam data = -1; 72310275Ssam return (NULL); 72410275Ssam } 72536235Skarels reply(150, "Opening %s mode data connection for %s%s.", 72636235Skarels type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf); 72710275Ssam return (file); 72810275Ssam } 72910275Ssam 73010275Ssam /* 73110275Ssam * Tranfer the contents of "instr" to 73210275Ssam * "outstr" peer using the appropriate 73336933Skarels * encapsulation of the data subject 73410275Ssam * to Mode, Structure, and Type. 73510275Ssam * 73610275Ssam * NB: Form isn't handled. 73710275Ssam */ 73836446Sbostic send_data(instr, outstr, blksize) 73910275Ssam FILE *instr, *outstr; 74036446Sbostic off_t blksize; 74110275Ssam { 74236446Sbostic register int c, cnt; 74336446Sbostic register char *buf; 74436446Sbostic int netfd, filefd; 74510275Ssam 74626044Sminshall transflag++; 74726044Sminshall if (setjmp(urgcatch)) { 74826044Sminshall transflag = 0; 74936620Srick return; 75026044Sminshall } 75110275Ssam switch (type) { 75210275Ssam 75310275Ssam case TYPE_A: 75410275Ssam while ((c = getc(instr)) != EOF) { 75536933Skarels byte_count++; 75611220Ssam if (c == '\n') { 75736933Skarels if (ferror(outstr)) 75836620Srick goto data_err; 75927750Sminshall (void) putc('\r', outstr); 76011220Ssam } 76127750Sminshall (void) putc(c, outstr); 76210275Ssam } 76336933Skarels fflush(outstr); 76426044Sminshall transflag = 0; 76536933Skarels if (ferror(instr)) 76636933Skarels goto file_err; 76736933Skarels if (ferror(outstr)) 76836620Srick goto data_err; 76936620Srick reply(226, "Transfer complete."); 77036620Srick return; 77136446Sbostic 77210275Ssam case TYPE_I: 77310275Ssam case TYPE_L: 77436446Sbostic if ((buf = malloc((u_int)blksize)) == NULL) { 77536446Sbostic transflag = 0; 77636933Skarels perror_reply(451, "Local resource failure: malloc"); 77736933Skarels return; 77836446Sbostic } 77910275Ssam netfd = fileno(outstr); 78010275Ssam filefd = fileno(instr); 78136933Skarels while ((cnt = read(filefd, buf, (u_int)blksize)) > 0 && 78236620Srick write(netfd, buf, cnt) == cnt) 78336933Skarels byte_count += cnt; 78426044Sminshall transflag = 0; 78536446Sbostic (void)free(buf); 78636933Skarels if (cnt != 0) { 78736933Skarels if (cnt < 0) 78836933Skarels goto file_err; 78936620Srick goto data_err; 79036933Skarels } 79136620Srick reply(226, "Transfer complete."); 79236620Srick return; 79336620Srick default: 79436620Srick transflag = 0; 79536620Srick reply(550, "Unimplemented TYPE %d in send_data", type); 79636620Srick return; 79710275Ssam } 79836620Srick 79936620Srick data_err: 80026044Sminshall transflag = 0; 80136933Skarels perror_reply(426, "Data connection"); 80236933Skarels return; 80336933Skarels 80436933Skarels file_err: 80536933Skarels transflag = 0; 80636933Skarels perror_reply(551, "Error on input file"); 80710275Ssam } 80810275Ssam 80910275Ssam /* 81010275Ssam * Transfer data from peer to 81110275Ssam * "outstr" using the appropriate 81210275Ssam * encapulation of the data subject 81310275Ssam * to Mode, Structure, and Type. 81410275Ssam * 81510275Ssam * N.B.: Form isn't handled. 81610275Ssam */ 81710275Ssam receive_data(instr, outstr) 81810275Ssam FILE *instr, *outstr; 81910275Ssam { 82010275Ssam register int c; 82138134Srick int cnt, bare_lfs = 0; 82210275Ssam char buf[BUFSIZ]; 82310275Ssam 82426044Sminshall transflag++; 82526044Sminshall if (setjmp(urgcatch)) { 82626044Sminshall transflag = 0; 82736933Skarels return (-1); 82826044Sminshall } 82910275Ssam switch (type) { 83010275Ssam 83110275Ssam case TYPE_I: 83210275Ssam case TYPE_L: 83326044Sminshall while ((cnt = read(fileno(instr), buf, sizeof buf)) > 0) { 83436620Srick if (write(fileno(outstr), buf, cnt) != cnt) 83536933Skarels goto file_err; 83636933Skarels byte_count += cnt; 83726044Sminshall } 83836933Skarels if (cnt < 0) 83936620Srick goto data_err; 84026044Sminshall transflag = 0; 84136933Skarels return (0); 84210275Ssam 84310275Ssam case TYPE_E: 84427106Smckusick reply(553, "TYPE E not implemented."); 84526044Sminshall transflag = 0; 84627106Smckusick return (-1); 84710275Ssam 84810275Ssam case TYPE_A: 84910275Ssam while ((c = getc(instr)) != EOF) { 85036933Skarels byte_count++; 85138134Srick if (c == '\n') 85238134Srick bare_lfs++; 85327750Sminshall while (c == '\r') { 85436933Skarels if (ferror(outstr)) 85536620Srick goto data_err; 85636933Skarels if ((c = getc(instr)) != '\n') { 85727750Sminshall (void) putc ('\r', outstr); 85836933Skarels if (c == '\0' || c == EOF) 85936933Skarels goto contin2; 86036933Skarels } 86110275Ssam } 86236933Skarels (void) putc(c, outstr); 86336933Skarels contin2: ; 86410275Ssam } 86536620Srick fflush(outstr); 86636933Skarels if (ferror(instr)) 86736620Srick goto data_err; 86836933Skarels if (ferror(outstr)) 86936933Skarels goto file_err; 87026044Sminshall transflag = 0; 87138134Srick if (bare_lfs) { 87238134Srick lreply(230, "WARNING! %d bare linefeeds received in ASCII mode", bare_lfs); 87338134Srick printf(" File may not have transferred correctly.\r\n"); 87438134Srick } 87510275Ssam return (0); 87636620Srick default: 87736620Srick reply(550, "Unimplemented TYPE %d in receive_data", type); 87836620Srick transflag = 0; 87936933Skarels return (-1); 88010275Ssam } 88136620Srick 88236620Srick data_err: 88326044Sminshall transflag = 0; 88436933Skarels perror_reply(426, "Data Connection"); 88536933Skarels return (-1); 88636933Skarels 88736933Skarels file_err: 88836933Skarels transflag = 0; 88936933Skarels perror_reply(452, "Error writing file"); 89036933Skarels return (-1); 89110275Ssam } 89210275Ssam 89336933Skarels statfilecmd(filename) 89436933Skarels char *filename; 89536933Skarels { 89636933Skarels char line[BUFSIZ]; 89736933Skarels FILE *fin; 89836933Skarels int c; 89936933Skarels 90036933Skarels (void) sprintf(line, "/bin/ls -lgA %s", filename); 90136933Skarels fin = ftpd_popen(line, "r"); 90236933Skarels lreply(211, "status of %s:", filename); 90336933Skarels while ((c = getc(fin)) != EOF) { 90436933Skarels if (c == '\n') { 90536933Skarels if (ferror(stdout)){ 90636933Skarels perror_reply(421, "control connection"); 90736933Skarels (void) ftpd_pclose(fin); 90836933Skarels dologout(1); 90936933Skarels /* NOTREACHED */ 91036933Skarels } 91136933Skarels if (ferror(fin)) { 91236933Skarels perror_reply(551, filename); 91336933Skarels (void) ftpd_pclose(fin); 91436933Skarels return; 91536933Skarels } 91636933Skarels (void) putc('\r', stdout); 91736933Skarels } 91836933Skarels (void) putc(c, stdout); 91936933Skarels } 92036933Skarels (void) ftpd_pclose(fin); 92136933Skarels reply(211, "End of Status"); 92236933Skarels } 92336933Skarels 92436933Skarels statcmd() 92536933Skarels { 92636933Skarels struct sockaddr_in *sin; 92736933Skarels u_char *a, *p; 92836933Skarels 92936933Skarels lreply(211, "%s FTP server status:", hostname, version); 93036933Skarels printf(" %s\r\n", version); 93136933Skarels printf(" Connected to %s", remotehost); 93238134Srick if (!isdigit(remotehost[0])) 93336933Skarels printf(" (%s)", inet_ntoa(his_addr.sin_addr)); 93436933Skarels printf("\r\n"); 93536933Skarels if (logged_in) { 93636933Skarels if (guest) 93736933Skarels printf(" Logged in anonymously\r\n"); 93836933Skarels else 93936933Skarels printf(" Logged in as %s\r\n", pw->pw_name); 94036933Skarels } else if (askpasswd) 94136933Skarels printf(" Waiting for password\r\n"); 94236933Skarels else 94336933Skarels printf(" Waiting for user name\r\n"); 94436933Skarels printf(" TYPE: %s", typenames[type]); 94536933Skarels if (type == TYPE_A || type == TYPE_E) 94636933Skarels printf(", FORM: %s", formnames[form]); 94736933Skarels if (type == TYPE_L) 94836933Skarels #if NBBY == 8 94936933Skarels printf(" %d", NBBY); 95036933Skarels #else 95136933Skarels printf(" %d", bytesize); /* need definition! */ 95236933Skarels #endif 95336933Skarels printf("; STRUcture: %s; transfer MODE: %s\r\n", 95436933Skarels strunames[stru], modenames[mode]); 95536933Skarels if (data != -1) 95636933Skarels printf(" Data connection open\r\n"); 95736933Skarels else if (pdata != -1) { 95836933Skarels printf(" in Passive mode"); 95936933Skarels sin = &pasv_addr; 96036933Skarels goto printaddr; 96136933Skarels } else if (usedefault == 0) { 96236933Skarels printf(" PORT"); 96336933Skarels sin = &data_dest; 96436933Skarels printaddr: 96536933Skarels a = (u_char *) &sin->sin_addr; 96636933Skarels p = (u_char *) &sin->sin_port; 96736933Skarels #define UC(b) (((int) b) & 0xff) 96836933Skarels printf(" (%d,%d,%d,%d,%d,%d)\r\n", UC(a[0]), 96936933Skarels UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1])); 97036933Skarels #undef UC 97136933Skarels } else 97236933Skarels printf(" No data connection\r\n"); 97336933Skarels reply(211, "End of status"); 97436933Skarels } 97536933Skarels 97610275Ssam fatal(s) 97710275Ssam char *s; 97810275Ssam { 97910275Ssam reply(451, "Error in server: %s\n", s); 98010275Ssam reply(221, "Closing connection due to server error."); 98113247Ssam dologout(0); 98236620Srick /* NOTREACHED */ 98310275Ssam } 98410275Ssam 98536446Sbostic /* VARARGS2 */ 98636446Sbostic reply(n, fmt, p0, p1, p2, p3, p4, p5) 98710275Ssam int n; 98836446Sbostic char *fmt; 98910275Ssam { 99010275Ssam printf("%d ", n); 99136446Sbostic printf(fmt, p0, p1, p2, p3, p4, p5); 99210275Ssam printf("\r\n"); 99336435Sbostic (void)fflush(stdout); 99410275Ssam if (debug) { 99526493Sminshall syslog(LOG_DEBUG, "<--- %d ", n); 99636446Sbostic syslog(LOG_DEBUG, fmt, p0, p1, p2, p3, p4, p5); 99710275Ssam } 99836620Srick } 99910275Ssam 100036446Sbostic /* VARARGS2 */ 100136446Sbostic lreply(n, fmt, p0, p1, p2, p3, p4, p5) 100210275Ssam int n; 100336446Sbostic char *fmt; 100410275Ssam { 100536446Sbostic printf("%d- ", n); 100636446Sbostic printf(fmt, p0, p1, p2, p3, p4, p5); 100736446Sbostic printf("\r\n"); 100836435Sbostic (void)fflush(stdout); 100936446Sbostic if (debug) { 101036446Sbostic syslog(LOG_DEBUG, "<--- %d- ", n); 101136446Sbostic syslog(LOG_DEBUG, fmt, p0, p1, p2, p3, p4, p5); 101236446Sbostic } 101310275Ssam } 101410275Ssam 101510275Ssam ack(s) 101610275Ssam char *s; 101710275Ssam { 101827106Smckusick reply(250, "%s command successful.", s); 101910275Ssam } 102010275Ssam 102110275Ssam nack(s) 102210275Ssam char *s; 102310275Ssam { 102410275Ssam reply(502, "%s command not implemented.", s); 102510275Ssam } 102610275Ssam 102736304Skarels /* ARGSUSED */ 102826493Sminshall yyerror(s) 102926493Sminshall char *s; 103010275Ssam { 103126044Sminshall char *cp; 103226044Sminshall 103336551Sbostic if (cp = index(cbuf,'\n')) 103436551Sbostic *cp = '\0'; 103536933Skarels reply(500, "'%s': command not understood.", cbuf); 103610275Ssam } 103710275Ssam 103810275Ssam delete(name) 103910275Ssam char *name; 104010275Ssam { 104110275Ssam struct stat st; 104210275Ssam 104310275Ssam if (stat(name, &st) < 0) { 104436304Skarels perror_reply(550, name); 104510275Ssam return; 104610275Ssam } 104710275Ssam if ((st.st_mode&S_IFMT) == S_IFDIR) { 104810275Ssam if (rmdir(name) < 0) { 104936304Skarels perror_reply(550, name); 105010275Ssam return; 105110275Ssam } 105210275Ssam goto done; 105310275Ssam } 105410275Ssam if (unlink(name) < 0) { 105536304Skarels perror_reply(550, name); 105610275Ssam return; 105710275Ssam } 105810275Ssam done: 105910275Ssam ack("DELE"); 106010275Ssam } 106110275Ssam 106210275Ssam cwd(path) 106310275Ssam char *path; 106410275Ssam { 106536620Srick if (chdir(path) < 0) 106636304Skarels perror_reply(550, path); 106736620Srick else 106836620Srick ack("CWD"); 106910275Ssam } 107010275Ssam 107110303Ssam makedir(name) 107210275Ssam char *name; 107310275Ssam { 107436276Sbostic if (mkdir(name, 0777) < 0) 107536304Skarels perror_reply(550, name); 107636276Sbostic else 107736276Sbostic reply(257, "MKD command successful."); 107810275Ssam } 107910275Ssam 108010303Ssam removedir(name) 108110275Ssam char *name; 108210275Ssam { 108336620Srick if (rmdir(name) < 0) 108436304Skarels perror_reply(550, name); 108536620Srick else 108636620Srick ack("RMD"); 108710275Ssam } 108810275Ssam 108910303Ssam pwd() 109010275Ssam { 109110303Ssam char path[MAXPATHLEN + 1]; 109236304Skarels extern char *getwd(); 109310275Ssam 109436620Srick if (getwd(path) == (char *)NULL) 109527106Smckusick reply(550, "%s.", path); 109636620Srick else 109736620Srick reply(257, "\"%s\" is current directory.", path); 109810275Ssam } 109910275Ssam 110010275Ssam char * 110110275Ssam renamefrom(name) 110210275Ssam char *name; 110310275Ssam { 110410275Ssam struct stat st; 110510275Ssam 110610275Ssam if (stat(name, &st) < 0) { 110736304Skarels perror_reply(550, name); 110810275Ssam return ((char *)0); 110910275Ssam } 111010303Ssam reply(350, "File exists, ready for destination name"); 111110275Ssam return (name); 111210275Ssam } 111310275Ssam 111410275Ssam renamecmd(from, to) 111510275Ssam char *from, *to; 111610275Ssam { 111736620Srick if (rename(from, to) < 0) 111836304Skarels perror_reply(550, "rename"); 111936620Srick else 112036620Srick ack("RNTO"); 112110275Ssam } 112210275Ssam 112310275Ssam dolog(sin) 112410275Ssam struct sockaddr_in *sin; 112510275Ssam { 112636304Skarels struct hostent *hp = gethostbyaddr((char *)&sin->sin_addr, 112710275Ssam sizeof (struct in_addr), AF_INET); 112836304Skarels time_t t, time(); 112926493Sminshall extern char *ctime(); 113010275Ssam 113136304Skarels if (hp) 113226493Sminshall (void) strncpy(remotehost, hp->h_name, sizeof (remotehost)); 113336304Skarels else 113426493Sminshall (void) strncpy(remotehost, inet_ntoa(sin->sin_addr), 113513247Ssam sizeof (remotehost)); 113636620Srick #ifdef SETPROCTITLE 113736933Skarels sprintf(proctitle, "%s: connected", remotehost); 113836933Skarels setproctitle(proctitle); 113936620Srick #endif /* SETPROCTITLE */ 114036933Skarels 114136933Skarels if (logging) { 114236933Skarels t = time((time_t *) 0); 114336933Skarels syslog(LOG_INFO, "connection from %s at %s", 114436933Skarels remotehost, ctime(&t)); 114536933Skarels } 114610275Ssam } 114710695Ssam 114810695Ssam /* 114913247Ssam * Record logout in wtmp file 115013247Ssam * and exit with supplied status. 115113247Ssam */ 115213247Ssam dologout(status) 115313247Ssam int status; 115413247Ssam { 115517580Ssam if (logged_in) { 115636304Skarels (void) seteuid((uid_t)0); 115735672Sbostic logwtmp(ttyline, "", ""); 115813247Ssam } 115914436Ssam /* beware of flushing buffers after a SIGPIPE */ 116014436Ssam _exit(status); 116113247Ssam } 116213247Ssam 116326044Sminshall myoob() 116426044Sminshall { 116527750Sminshall char *cp; 116626044Sminshall 116727750Sminshall /* only process if transfer occurring */ 116836304Skarels if (!transflag) 116926044Sminshall return; 117027750Sminshall cp = tmpline; 117127750Sminshall if (getline(cp, 7, stdin) == NULL) { 117236304Skarels reply(221, "You could at least say goodbye."); 117327750Sminshall dologout(0); 117426044Sminshall } 117526044Sminshall upper(cp); 117636933Skarels if (strcmp(cp, "ABOR\r\n") == 0) { 117736933Skarels tmpline[0] = '\0'; 117836933Skarels reply(426, "Transfer aborted. Data connection closed."); 117936933Skarels reply(226, "Abort successful"); 118036933Skarels longjmp(urgcatch, 1); 118136933Skarels } 118236933Skarels if (strcmp(cp, "STAT\r\n") == 0) { 118336933Skarels if (file_size != (off_t) -1) 118436933Skarels reply(213, "Status: %lu of %lu bytes transferred", 118536933Skarels byte_count, file_size); 118636933Skarels else 118736933Skarels reply(213, "Status: %lu bytes transferred", byte_count); 118836933Skarels } 118926044Sminshall } 119026044Sminshall 119127106Smckusick /* 119236620Srick * Note: a response of 425 is not mentioned as a possible response to 119336620Srick * the PASV command in RFC959. However, it has been blessed as 119436620Srick * a legitimate response by Jon Postel in a telephone conversation 119536620Srick * with Rick Adams on 25 Jan 89. 119627106Smckusick */ 119726044Sminshall passive() 119826044Sminshall { 119926044Sminshall int len; 120026044Sminshall register char *p, *a; 120126044Sminshall 120226044Sminshall pdata = socket(AF_INET, SOCK_STREAM, 0); 120326044Sminshall if (pdata < 0) { 120436620Srick perror_reply(425, "Can't open passive connection"); 120526044Sminshall return; 120626044Sminshall } 120736933Skarels pasv_addr = ctrl_addr; 120836933Skarels pasv_addr.sin_port = 0; 120936304Skarels (void) seteuid((uid_t)0); 121036933Skarels if (bind(pdata, (struct sockaddr *)&pasv_addr, sizeof(pasv_addr)) < 0) { 121136304Skarels (void) seteuid((uid_t)pw->pw_uid); 121236620Srick goto pasv_error; 121326044Sminshall } 121436304Skarels (void) seteuid((uid_t)pw->pw_uid); 121536933Skarels len = sizeof(pasv_addr); 121636933Skarels if (getsockname(pdata, (struct sockaddr *) &pasv_addr, &len) < 0) 121736620Srick goto pasv_error; 121836620Srick if (listen(pdata, 1) < 0) 121936620Srick goto pasv_error; 122036933Skarels a = (char *) &pasv_addr.sin_addr; 122136933Skarels p = (char *) &pasv_addr.sin_port; 122226044Sminshall 122326044Sminshall #define UC(b) (((int) b) & 0xff) 122426044Sminshall 122526044Sminshall reply(227, "Entering Passive Mode (%d,%d,%d,%d,%d,%d)", UC(a[0]), 122626044Sminshall UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1])); 122736620Srick return; 122836620Srick 122936620Srick pasv_error: 123036620Srick (void) close(pdata); 123136620Srick pdata = -1; 123236620Srick perror_reply(425, "Can't open passive connection"); 123336620Srick return; 123426044Sminshall } 123526044Sminshall 123636304Skarels /* 123736304Skarels * Generate unique name for file with basename "local". 123836304Skarels * The file named "local" is already known to exist. 123936304Skarels * Generates failure reply on error. 124036304Skarels */ 124126044Sminshall char * 124226044Sminshall gunique(local) 124326044Sminshall char *local; 124426044Sminshall { 124526044Sminshall static char new[MAXPATHLEN]; 124636304Skarels struct stat st; 124726044Sminshall char *cp = rindex(local, '/'); 124836933Skarels int count = 0; 124926044Sminshall 125036304Skarels if (cp) 125126044Sminshall *cp = '\0'; 125236620Srick if (stat(cp ? local : ".", &st) < 0) { 125336933Skarels perror_reply(553, cp ? local : "."); 125426044Sminshall return((char *) 0); 125526044Sminshall } 125636620Srick if (cp) 125736620Srick *cp = '/'; 125826044Sminshall (void) strcpy(new, local); 125926044Sminshall cp = new + strlen(new); 126026044Sminshall *cp++ = '.'; 126136304Skarels for (count = 1; count < 100; count++) { 126236304Skarels (void) sprintf(cp, "%d", count); 126336304Skarels if (stat(new, &st) < 0) 126436304Skarels return(new); 126526044Sminshall } 126636304Skarels reply(452, "Unique file name cannot be created."); 126736304Skarels return((char *) 0); 126826044Sminshall } 126936304Skarels 127036304Skarels /* 127136304Skarels * Format and send reply containing system error number. 127236304Skarels */ 127336304Skarels perror_reply(code, string) 127436304Skarels int code; 127536304Skarels char *string; 127636304Skarels { 127742412Sbostic reply(code, "%s: %s.", string, strerror(errno)); 127836304Skarels } 127936620Srick 128036620Srick static char *onefile[] = { 128136620Srick "", 128236620Srick 0 128336620Srick }; 128436620Srick 128536620Srick send_file_list(whichfiles) 128636620Srick char *whichfiles; 128736620Srick { 128836620Srick struct stat st; 128936620Srick DIR *dirp = NULL; 129036620Srick struct direct *dir; 129136620Srick FILE *dout = NULL; 129236620Srick register char **dirlist, *dirname; 129337459Skarels int simple = 0; 129436620Srick char *strpbrk(); 129536620Srick 129636620Srick if (strpbrk(whichfiles, "~{[*?") != NULL) { 129736620Srick extern char **glob(), *globerr; 129836933Skarels 129936620Srick globerr = NULL; 130036620Srick dirlist = glob(whichfiles); 130136620Srick if (globerr != NULL) { 130236620Srick reply(550, globerr); 130336620Srick return; 130436620Srick } else if (dirlist == NULL) { 130536620Srick errno = ENOENT; 130636620Srick perror_reply(550, whichfiles); 130736620Srick return; 130836620Srick } 130936620Srick } else { 131036620Srick onefile[0] = whichfiles; 131136620Srick dirlist = onefile; 131237459Skarels simple = 1; 131336620Srick } 131436933Skarels 131536933Skarels if (setjmp(urgcatch)) { 131636933Skarels transflag = 0; 131736933Skarels return; 131836933Skarels } 131936620Srick while (dirname = *dirlist++) { 132036620Srick if (stat(dirname, &st) < 0) { 132136933Skarels /* 132236933Skarels * If user typed "ls -l", etc, and the client 132336933Skarels * used NLST, do what the user meant. 132436933Skarels */ 132536933Skarels if (dirname[0] == '-' && *dirlist == NULL && 132636933Skarels transflag == 0) { 132736933Skarels retrieve("/bin/ls %s", dirname); 132836933Skarels return; 132936933Skarels } 133036620Srick perror_reply(550, whichfiles); 133136620Srick if (dout != NULL) { 133236620Srick (void) fclose(dout); 133336933Skarels transflag = 0; 133436620Srick data = -1; 133536620Srick pdata = -1; 133636620Srick } 133736620Srick return; 133836620Srick } 133936933Skarels 134036620Srick if ((st.st_mode&S_IFMT) == S_IFREG) { 134136620Srick if (dout == NULL) { 134237459Skarels dout = dataconn("file list", (off_t)-1, "w"); 134336620Srick if (dout == NULL) 134436620Srick return; 134536933Skarels transflag++; 134636620Srick } 134738158Srick fprintf(dout, "%s%s\n", dirname, 134838158Srick type == TYPE_A ? "\r" : ""); 134936933Skarels byte_count += strlen(dirname) + 1; 135036620Srick continue; 135136933Skarels } else if ((st.st_mode&S_IFMT) != S_IFDIR) 135236620Srick continue; 135336620Srick 135436620Srick if ((dirp = opendir(dirname)) == NULL) 135536620Srick continue; 135636620Srick 135736620Srick while ((dir = readdir(dirp)) != NULL) { 135836933Skarels char nbuf[MAXPATHLEN]; 135936620Srick 136036620Srick if (dir->d_name[0] == '.' && dir->d_namlen == 1) 136136620Srick continue; 136236933Skarels if (dir->d_name[0] == '.' && dir->d_name[1] == '.' && 136336933Skarels dir->d_namlen == 2) 136436620Srick continue; 136536620Srick 136636933Skarels sprintf(nbuf, "%s/%s", dirname, dir->d_name); 136736933Skarels 136836620Srick /* 136936933Skarels * We have to do a stat to insure it's 137036933Skarels * not a directory or special file. 137136620Srick */ 137237459Skarels if (simple || (stat(nbuf, &st) == 0 && 137337459Skarels (st.st_mode&S_IFMT) == S_IFREG)) { 137436620Srick if (dout == NULL) { 137537459Skarels dout = dataconn("file list", (off_t)-1, 137636620Srick "w"); 137736620Srick if (dout == NULL) 137836620Srick return; 137936933Skarels transflag++; 138036620Srick } 138136620Srick if (nbuf[0] == '.' && nbuf[1] == '/') 138238158Srick fprintf(dout, "%s%s\n", &nbuf[2], 138338158Srick type == TYPE_A ? "\r" : ""); 138436620Srick else 138538158Srick fprintf(dout, "%s%s\n", nbuf, 138638158Srick type == TYPE_A ? "\r" : ""); 138736933Skarels byte_count += strlen(nbuf) + 1; 138836620Srick } 138936620Srick } 139036620Srick (void) closedir(dirp); 139136620Srick } 139236620Srick 139336933Skarels if (dout == NULL) 139436933Skarels reply(550, "No files found."); 139536933Skarels else if (ferror(dout) != 0) 139636933Skarels perror_reply(550, "Data connection"); 139736933Skarels else 139836620Srick reply(226, "Transfer complete."); 139936620Srick 140036933Skarels transflag = 0; 140136933Skarels if (dout != NULL) 140236620Srick (void) fclose(dout); 140336620Srick data = -1; 140436620Srick pdata = -1; 140536620Srick } 140636620Srick 140736620Srick #ifdef SETPROCTITLE 140836620Srick /* 140936620Srick * clobber argv so ps will show what we're doing. 141036620Srick * (stolen from sendmail) 141136620Srick * warning, since this is usually started from inetd.conf, it 141236620Srick * often doesn't have much of an environment or arglist to overwrite. 141336620Srick */ 141436620Srick 141536620Srick /*VARARGS1*/ 141636620Srick setproctitle(fmt, a, b, c) 141736620Srick char *fmt; 141836620Srick { 141936620Srick register char *p, *bp, ch; 142036620Srick register int i; 142136620Srick char buf[BUFSIZ]; 142236620Srick 142336620Srick (void) sprintf(buf, fmt, a, b, c); 142436620Srick 142536620Srick /* make ps print our process name */ 142636620Srick p = Argv[0]; 142736620Srick *p++ = '-'; 142836620Srick 142936620Srick i = strlen(buf); 143036620Srick if (i > LastArgv - p - 2) { 143136620Srick i = LastArgv - p - 2; 143236620Srick buf[i] = '\0'; 143336620Srick } 143436620Srick bp = buf; 143536620Srick while (ch = *bp++) 143636620Srick if (ch != '\n' && ch != '\r') 143736620Srick *p++ = ch; 143836620Srick while (p < LastArgv) 143936620Srick *p++ = ' '; 144036620Srick } 144136620Srick #endif /* SETPROCTITLE */ 1442