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 1534769Sbostic * WARRANTIES OF MERCHANTIBILITY 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*36435Sbostic static char sccsid[] = "@(#)ftpd.c 5.21 (Berkeley) 12/16/88"; 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> 3710275Ssam 3810275Ssam #include <netinet/in.h> 3910275Ssam 4013034Ssam #include <arpa/ftp.h> 4113211Sroot #include <arpa/inet.h> 4226044Sminshall #include <arpa/telnet.h> 4313034Ssam 4410275Ssam #include <stdio.h> 4510275Ssam #include <signal.h> 4610275Ssam #include <pwd.h> 4710275Ssam #include <setjmp.h> 4810275Ssam #include <netdb.h> 4910423Ssam #include <errno.h> 5026044Sminshall #include <strings.h> 5126493Sminshall #include <syslog.h> 52*36435Sbostic #include <varargs.h> 5310275Ssam 5410695Ssam /* 5510695Ssam * File containing login names 5610695Ssam * NOT to be used on this machine. 5710695Ssam * Commonly used to disallow uucp. 5810695Ssam */ 5910695Ssam #define FTPUSERS "/etc/ftpusers" 6010695Ssam 6110275Ssam extern int errno; 6210275Ssam extern char *sys_errlist[]; 6336304Skarels extern int sys_nerr; 6410275Ssam extern char *crypt(); 6510275Ssam extern char version[]; 6610275Ssam extern char *home; /* pointer to home directory for glob */ 6736276Sbostic extern FILE *ftpd_popen(), *fopen(), *freopen(); 6836304Skarels extern int ftpd_pclose(), fclose(); 6926044Sminshall extern char *getline(); 7026044Sminshall extern char cbuf[]; 7110275Ssam 7210275Ssam struct sockaddr_in ctrl_addr; 7310275Ssam struct sockaddr_in data_source; 7410275Ssam struct sockaddr_in data_dest; 7510275Ssam struct sockaddr_in his_addr; 7610275Ssam 7710275Ssam int data; 7826044Sminshall jmp_buf errcatch, urgcatch; 7910275Ssam int logged_in; 8010275Ssam struct passwd *pw; 8110275Ssam int debug; 8226493Sminshall int timeout = 900; /* timeout after 15 minutes of inactivity */ 8311757Ssam int logging; 8410275Ssam int guest; 8510275Ssam int type; 8610275Ssam int form; 8710275Ssam int stru; /* avoid C keyword */ 8810275Ssam int mode; 8910321Ssam int usedefault = 1; /* for data transfers */ 9036304Skarels int pdata = -1; /* for passive mode */ 9126044Sminshall int transflag; 9226044Sminshall char tmpline[7]; 9336276Sbostic char hostname[MAXHOSTNAMELEN]; 9436276Sbostic char remotehost[MAXHOSTNAMELEN]; 9510275Ssam 9611653Ssam /* 9711653Ssam * Timeout intervals for retrying connections 9811653Ssam * to hosts that don't accept PORT cmds. This 9911653Ssam * is a kludge, but given the problems with TCP... 10011653Ssam */ 10111653Ssam #define SWAITMAX 90 /* wait at most 90 seconds */ 10211653Ssam #define SWAITINT 5 /* interval between retries */ 10311653Ssam 10411653Ssam int swaitmax = SWAITMAX; 10511653Ssam int swaitint = SWAITINT; 10611653Ssam 10710275Ssam int lostconn(); 10826044Sminshall int myoob(); 10910275Ssam FILE *getdatasock(), *dataconn(); 11010275Ssam 11110275Ssam main(argc, argv) 11210275Ssam int argc; 11310275Ssam char *argv[]; 11410275Ssam { 11527750Sminshall int addrlen, on = 1; 11610275Ssam char *cp; 11710275Ssam 11816339Skarels addrlen = sizeof (his_addr); 11936304Skarels if (getpeername(0, (struct sockaddr *)&his_addr, &addrlen) < 0) { 12026493Sminshall syslog(LOG_ERR, "getpeername (%s): %m",argv[0]); 12110275Ssam exit(1); 12210275Ssam } 12316339Skarels addrlen = sizeof (ctrl_addr); 12436304Skarels if (getsockname(0, (struct sockaddr *)&ctrl_addr, &addrlen) < 0) { 12526493Sminshall syslog(LOG_ERR, "getsockname (%s): %m",argv[0]); 12616339Skarels exit(1); 12716339Skarels } 12816339Skarels data_source.sin_port = htons(ntohs(ctrl_addr.sin_port) - 1); 12910275Ssam debug = 0; 13026493Sminshall openlog("ftpd", LOG_PID, LOG_DAEMON); 13110275Ssam argc--, argv++; 13210275Ssam while (argc > 0 && *argv[0] == '-') { 13310275Ssam for (cp = &argv[0][1]; *cp; cp++) switch (*cp) { 13410275Ssam 13511653Ssam case 'v': 13611653Ssam debug = 1; 13711653Ssam break; 13811653Ssam 13910275Ssam case 'd': 14010275Ssam debug = 1; 14110275Ssam break; 14210275Ssam 14311757Ssam case 'l': 14411757Ssam logging = 1; 14511757Ssam break; 14611757Ssam 14711653Ssam case 't': 14811653Ssam timeout = atoi(++cp); 14911653Ssam goto nextopt; 15011653Ssam break; 15111653Ssam 15210275Ssam default: 15316339Skarels fprintf(stderr, "ftpd: Unknown flag -%c ignored.\n", 15416339Skarels *cp); 15510275Ssam break; 15610275Ssam } 15711653Ssam nextopt: 15810275Ssam argc--, argv++; 15910275Ssam } 16030944Scsvsj (void) freopen("/dev/null", "w", stderr); 16126493Sminshall (void) signal(SIGPIPE, lostconn); 16226493Sminshall (void) signal(SIGCHLD, SIG_IGN); 16335691Sbostic if ((int)signal(SIGURG, myoob) < 0) 16426493Sminshall syslog(LOG_ERR, "signal: %m"); 16535691Sbostic 16627750Sminshall /* handle urgent data inline */ 16736276Sbostic /* Sequent defines this, but it doesn't work */ 16827750Sminshall #ifdef SO_OOBINLINE 16936276Sbostic if (setsockopt(0, SOL_SOCKET, SO_OOBINLINE, (char *)&on, sizeof(on)) < 0) 17027750Sminshall syslog(LOG_ERR, "setsockopt: %m"); 17136276Sbostic #endif 17236304Skarels if (fcntl(fileno(stdin), F_SETOWN, getpid()) == -1) 17336304Skarels syslog(LOG_ERR, "fcntl F_SETOWN: %m"); 17416760Slepreau dolog(&his_addr); 17516339Skarels /* do telnet option negotiation here */ 17616339Skarels /* 17716339Skarels * Set up default state 17816339Skarels */ 17916339Skarels data = -1; 18016339Skarels type = TYPE_A; 18116339Skarels form = FORM_N; 18216339Skarels stru = STRU_F; 18316339Skarels mode = MODE_S; 18426044Sminshall tmpline[0] = '\0'; 18526493Sminshall (void) gethostname(hostname, sizeof (hostname)); 18636276Sbostic reply(220, "%s FTP server (%s) ready.", hostname, version); 18736304Skarels (void) setjmp(errcatch); 18836304Skarels for (;;) 18926493Sminshall (void) yyparse(); 19010275Ssam } 19110419Ssam 19210275Ssam lostconn() 19310275Ssam { 19410275Ssam 19514089Ssam if (debug) 19626493Sminshall syslog(LOG_DEBUG, "lost connection"); 19714089Ssam dologout(-1); 19810275Ssam } 19910275Ssam 20035672Sbostic static char ttyline[20]; 20135672Sbostic 20236185Sbostic /* 20336185Sbostic * Helper function for sgetpwnam(). 20436185Sbostic */ 20536185Sbostic char * 20636185Sbostic sgetsave(s) 20736185Sbostic char *s; 20836185Sbostic { 20936185Sbostic char *malloc(); 21036185Sbostic char *new = malloc((unsigned) strlen(s) + 1); 21136185Sbostic 21236185Sbostic if (new == NULL) { 21336276Sbostic reply(553, "Local resource failure: malloc"); 21436185Sbostic dologout(1); 21536185Sbostic } 21636185Sbostic (void) strcpy(new, s); 21736185Sbostic return (new); 21836185Sbostic } 21936185Sbostic 22036185Sbostic /* 22136185Sbostic * Save the result of a getpwnam. Used for USER command, since 22236185Sbostic * the data returned must not be clobbered by any other command 22336185Sbostic * (e.g., globbing). 22436185Sbostic */ 22536185Sbostic struct passwd * 22636185Sbostic sgetpwnam(name) 22736185Sbostic char *name; 22836185Sbostic { 22936185Sbostic static struct passwd save; 23036185Sbostic register struct passwd *p; 23136185Sbostic char *sgetsave(); 23236185Sbostic 23336185Sbostic if ((p = getpwnam(name)) == NULL) 23436185Sbostic return (p); 23536185Sbostic if (save.pw_name) { 23636185Sbostic free(save.pw_name); 23736185Sbostic free(save.pw_passwd); 23836185Sbostic free(save.pw_comment); 23936185Sbostic free(save.pw_gecos); 24036185Sbostic free(save.pw_dir); 24136185Sbostic free(save.pw_shell); 24236185Sbostic } 24336185Sbostic save = *p; 24436185Sbostic save.pw_name = sgetsave(p->pw_name); 24536185Sbostic save.pw_passwd = sgetsave(p->pw_passwd); 24636185Sbostic save.pw_comment = sgetsave(p->pw_comment); 24736185Sbostic save.pw_gecos = sgetsave(p->pw_gecos); 24836185Sbostic save.pw_dir = sgetsave(p->pw_dir); 24936185Sbostic save.pw_shell = sgetsave(p->pw_shell); 25036185Sbostic return (&save); 25136185Sbostic } 25236185Sbostic 25336304Skarels int login_attempts; /* number of failed login attempts */ 25436304Skarels int askpasswd; /* had user command, ask for passwd */ 25536304Skarels 25636304Skarels /* 25736304Skarels * USER command. 25836304Skarels * Sets global passwd pointer pw if named account exists 25936304Skarels * and is acceptable; sets askpasswd if a PASS command is 26036304Skarels * expected. If logged in previously, need to reset state. 26136304Skarels * If name is "ftp" or "anonymous" and ftp account exists, 26236304Skarels * set guest and pw, then just return. 26336304Skarels * If account doesn't exist, ask for passwd anyway. 26436304Skarels * Otherwise, check user requesting login privileges. 26536304Skarels * Disallow anyone who does not have a standard 26636304Skarels * shell returned by getusershell() (/etc/shells). 26736304Skarels * Disallow anyone mentioned in the file FTPUSERS 26836304Skarels * to allow people such as root and uucp to be avoided. 26936304Skarels */ 27036304Skarels user(name) 27136304Skarels char *name; 27236304Skarels { 27336304Skarels register char *cp; 27436304Skarels FILE *fd; 27536304Skarels char *shell; 27636304Skarels char line[BUFSIZ], *index(), *getusershell(); 27736304Skarels 27836304Skarels if (logged_in) { 27936304Skarels if (guest) { 28036304Skarels reply(530, "Can't change user from guest login."); 28136304Skarels return; 28236304Skarels } 28336304Skarels end_login(); 28436304Skarels } 28536304Skarels 28636304Skarels guest = 0; 28736304Skarels if (strcmp(name, "ftp") == 0 || strcmp(name, "anonymous") == 0) { 28836304Skarels if ((pw = sgetpwnam("ftp")) != NULL) { 28936304Skarels guest = 1; 29036304Skarels askpasswd = 1; 29136304Skarels reply(331, "Guest login ok, send ident as password."); 29236304Skarels } else 29336304Skarels reply(530, "User %s unknown.", name); 29436304Skarels return; 29536304Skarels } 29636304Skarels if (pw = sgetpwnam(name)) { 29736304Skarels if ((shell = pw->pw_shell) == NULL || *shell == 0) 29836304Skarels shell = "/bin/sh"; 29936304Skarels while ((cp = getusershell()) != NULL) 30036304Skarels if (strcmp(cp, shell) == 0) 30136304Skarels break; 30236304Skarels endusershell(); 30336304Skarels if (cp == NULL) { 30436304Skarels reply(530, "User %s access denied.", name); 30536304Skarels pw = (struct passwd *) NULL; 30636304Skarels return; 30736304Skarels } 30836304Skarels if ((fd = fopen(FTPUSERS, "r")) != NULL) { 30936304Skarels while (fgets(line, sizeof (line), fd) != NULL) { 31036304Skarels if ((cp = index(line, '\n')) != NULL) 31136304Skarels *cp = '\0'; 31236304Skarels if (strcmp(line, name) == 0) { 31336304Skarels reply(530, "User %s access denied.", name); 31436304Skarels pw = (struct passwd *) NULL; 31536304Skarels return; 31636304Skarels } 31736304Skarels } 31836304Skarels } 31936304Skarels (void) fclose(fd); 32036304Skarels } 32136304Skarels reply(331, "Password required for %s.", name); 32236304Skarels askpasswd = 1; 32336304Skarels /* 32436304Skarels * Delay before reading passwd after first failed 32536304Skarels * attempt to slow down passwd-guessing programs. 32636304Skarels */ 32736304Skarels if (login_attempts) 32836304Skarels sleep((unsigned) login_attempts); 32936304Skarels } 33036304Skarels 33136304Skarels /* 33236304Skarels * Terminate login as previous user, if any, resetting state; 33336304Skarels * used when USER command is given or login fails. 33436304Skarels */ 33536304Skarels end_login() 33636304Skarels { 33736304Skarels 33836304Skarels (void) seteuid((uid_t)0); 33936304Skarels if (logged_in) 34036304Skarels logwtmp(ttyline, "", ""); 34136304Skarels pw = NULL; 34236304Skarels logged_in = 0; 34336304Skarels guest = 0; 34436304Skarels } 34536304Skarels 34610275Ssam pass(passwd) 34710275Ssam char *passwd; 34810275Ssam { 34936304Skarels char *xpasswd, *salt; 35010275Ssam 35136304Skarels if (logged_in || askpasswd == 0) { 35210275Ssam reply(503, "Login with USER first."); 35310275Ssam return; 35410275Ssam } 35536304Skarels askpasswd = 0; 35610275Ssam if (!guest) { /* "ftp" is only account allowed no password */ 35736304Skarels if (pw == NULL) 35836304Skarels salt = "xx"; 35936304Skarels else 36036304Skarels salt = pw->pw_passwd; 36136304Skarels xpasswd = crypt(passwd, salt); 36216760Slepreau /* The strcmp does not catch null passwords! */ 36336304Skarels if (pw == NULL || *pw->pw_passwd == '\0' || 36436304Skarels strcmp(xpasswd, pw->pw_passwd)) { 36510275Ssam reply(530, "Login incorrect."); 36610275Ssam pw = NULL; 36736304Skarels if (login_attempts++ >= 5) { 36836304Skarels syslog(LOG_ERR, 36936304Skarels "repeated login failures from %s", 37036304Skarels remotehost); 37136304Skarels exit(0); 37236304Skarels } 37310275Ssam return; 37410275Ssam } 37510275Ssam } 37636304Skarels login_attempts = 0; /* this time successful */ 37736304Skarels (void) setegid((gid_t)pw->pw_gid); 37836304Skarels (void) initgroups(pw->pw_name, pw->pw_gid); 37910275Ssam if (chdir(pw->pw_dir)) { 38027106Smckusick reply(530, "User %s: can't change directory to %s.", 38110275Ssam pw->pw_name, pw->pw_dir); 38210303Ssam goto bad; 38310275Ssam } 38416033Sralph 38536192Sbostic /* open wtmp before chroot */ 38636192Sbostic (void)sprintf(ttyline, "ftp%d", getpid()); 38736192Sbostic logwtmp(ttyline, pw->pw_name, remotehost); 38836192Sbostic logged_in = 1; 38936192Sbostic 39036304Skarels if (guest && chroot(pw->pw_dir) < 0) { 39136304Skarels reply(550, "Can't set guest privileges."); 39236304Skarels goto bad; 39336304Skarels } 39436304Skarels if (seteuid((uid_t)pw->pw_uid) < 0) { 39536304Skarels reply(550, "Can't set uid."); 39636304Skarels goto bad; 39736304Skarels } 39836304Skarels if (guest) 39936192Sbostic reply(230, "Guest login ok, access restrictions apply."); 40036304Skarels else 40110275Ssam reply(230, "User %s logged in.", pw->pw_name); 40210303Ssam home = pw->pw_dir; /* home dir for globbing */ 40310303Ssam return; 40410303Ssam bad: 40536304Skarels /* Forget all about it... */ 40636304Skarels end_login(); 40710275Ssam } 40810275Ssam 40910275Ssam retrieve(cmd, name) 41010275Ssam char *cmd, *name; 41110275Ssam { 41210275Ssam FILE *fin, *dout; 41310275Ssam struct stat st; 41426044Sminshall int (*closefunc)(), tmp; 41510275Ssam 41610275Ssam if (cmd == 0) { 41710317Ssam #ifdef notdef 41810317Ssam /* no remote command execution -- it's a security hole */ 41911653Ssam if (*name == '|') 42036304Skarels fin = ftpd_popen(name + 1, "r"), 42136304Skarels closefunc = ftpd_pclose; 42210275Ssam else 42310317Ssam #endif 42410275Ssam fin = fopen(name, "r"), closefunc = fclose; 42510275Ssam } else { 42610275Ssam char line[BUFSIZ]; 42710275Ssam 42826493Sminshall (void) sprintf(line, cmd, name), name = line; 42936304Skarels fin = ftpd_popen(line, "r"), closefunc = ftpd_pclose; 43010275Ssam } 43110275Ssam if (fin == NULL) { 43213152Ssam if (errno != 0) 43336304Skarels perror_reply(550, name); 43410275Ssam return; 43510275Ssam } 43610275Ssam st.st_size = 0; 43710275Ssam if (cmd == 0 && 43810275Ssam (stat(name, &st) < 0 || (st.st_mode&S_IFMT) != S_IFREG)) { 43910275Ssam reply(550, "%s: not a plain file.", name); 44010275Ssam goto done; 44110275Ssam } 44210275Ssam dout = dataconn(name, st.st_size, "w"); 44310275Ssam if (dout == NULL) 44410275Ssam goto done; 44526044Sminshall if ((tmp = send_data(fin, dout)) > 0 || ferror(dout) > 0) { 44636304Skarels perror_reply(550, name); 44726044Sminshall } 44826044Sminshall else if (tmp == 0) { 44910275Ssam reply(226, "Transfer complete."); 45026044Sminshall } 45126493Sminshall (void) fclose(dout); 45226044Sminshall data = -1; 45326044Sminshall pdata = -1; 45410275Ssam done: 45510275Ssam (*closefunc)(fin); 45610275Ssam } 45710275Ssam 45836304Skarels store(name, mode, unique) 45910275Ssam char *name, *mode; 46036304Skarels int unique; 46110275Ssam { 46210275Ssam FILE *fout, *din; 46336304Skarels int (*closefunc)(), tmp; 46436304Skarels char *gunique(); 46510275Ssam 46610317Ssam #ifdef notdef 46710317Ssam /* no remote command execution -- it's a security hole */ 46811653Ssam if (name[0] == '|') 46936304Skarels fout = ftpd_popen(&name[1], "w"), closefunc = ftpd_pclose; 47010317Ssam else 47110317Ssam #endif 47210317Ssam { 47310303Ssam struct stat st; 47410303Ssam 47536304Skarels if (unique && stat(name, &st) == 0 && 47636304Skarels (name = gunique(name)) == NULL) 47736304Skarels return; 47836304Skarels fout = fopen(name, mode), closefunc = fclose; 47910303Ssam } 48010275Ssam if (fout == NULL) { 48136304Skarels perror_reply(553, name); 48210275Ssam return; 48310275Ssam } 48436304Skarels din = dataconn(name, (off_t)-1, "r"); 48510275Ssam if (din == NULL) 48610275Ssam goto done; 48736304Skarels if ((tmp = receive_data(din, fout)) > 0) 48836304Skarels perror_reply(552, name); 48936304Skarels else if (tmp == 0) { 49036304Skarels if (ferror(fout) > 0) 49136304Skarels perror_reply(552, name); 49236304Skarels else if (unique) 49336304Skarels reply(226, "Transfer complete (unique file name:%s).", 49436304Skarels name); 49536304Skarels else 49636304Skarels reply(226, "Transfer complete."); 49726044Sminshall } 49826493Sminshall (void) fclose(din); 49926044Sminshall data = -1; 50026044Sminshall pdata = -1; 50110275Ssam done: 50210275Ssam (*closefunc)(fout); 50310275Ssam } 50410275Ssam 50510275Ssam FILE * 50610275Ssam getdatasock(mode) 50710275Ssam char *mode; 50810275Ssam { 50917157Ssam int s, on = 1; 51010275Ssam 51110275Ssam if (data >= 0) 51210275Ssam return (fdopen(data, mode)); 51313247Ssam s = socket(AF_INET, SOCK_STREAM, 0); 51410602Ssam if (s < 0) 51510275Ssam return (NULL); 51636304Skarels (void) seteuid((uid_t)0); 51726493Sminshall if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof (on)) < 0) 51810602Ssam goto bad; 51913152Ssam /* anchor socket to avoid multi-homing problems */ 52013152Ssam data_source.sin_family = AF_INET; 52113152Ssam data_source.sin_addr = ctrl_addr.sin_addr; 52236304Skarels if (bind(s, (struct sockaddr *)&data_source, sizeof (data_source)) < 0) 52310602Ssam goto bad; 52436304Skarels (void) seteuid((uid_t)pw->pw_uid); 52510275Ssam return (fdopen(s, mode)); 52610602Ssam bad: 52736304Skarels (void) seteuid((uid_t)pw->pw_uid); 52826493Sminshall (void) close(s); 52910602Ssam return (NULL); 53010275Ssam } 53110275Ssam 53210275Ssam FILE * 53310275Ssam dataconn(name, size, mode) 53410275Ssam char *name; 53511653Ssam off_t size; 53610275Ssam char *mode; 53710275Ssam { 53810275Ssam char sizebuf[32]; 53910275Ssam FILE *file; 54011653Ssam int retry = 0; 54110275Ssam 54236304Skarels if (size != (off_t) -1) 54326493Sminshall (void) sprintf (sizebuf, " (%ld bytes)", size); 54410275Ssam else 54510275Ssam (void) strcpy(sizebuf, ""); 54636304Skarels if (pdata >= 0) { 54726044Sminshall struct sockaddr_in from; 54826044Sminshall int s, fromlen = sizeof(from); 54926044Sminshall 55036304Skarels s = accept(pdata, (struct sockaddr *)&from, &fromlen); 55126044Sminshall if (s < 0) { 55226044Sminshall reply(425, "Can't open data connection."); 55326044Sminshall (void) close(pdata); 55426044Sminshall pdata = -1; 55526044Sminshall return(NULL); 55626044Sminshall } 55726044Sminshall (void) close(pdata); 55826044Sminshall pdata = s; 55936235Skarels reply(150, "Opening %s mode data connection for %s%s.", 56036235Skarels type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf); 56126044Sminshall return(fdopen(pdata, mode)); 56226044Sminshall } 56310275Ssam if (data >= 0) { 56410275Ssam reply(125, "Using existing data connection for %s%s.", 56510275Ssam name, sizebuf); 56610321Ssam usedefault = 1; 56710275Ssam return (fdopen(data, mode)); 56810275Ssam } 56910566Ssam if (usedefault) 57010422Ssam data_dest = his_addr; 57110422Ssam usedefault = 1; 57210275Ssam file = getdatasock(mode); 57310275Ssam if (file == NULL) { 57410275Ssam reply(425, "Can't create data socket (%s,%d): %s.", 57513247Ssam inet_ntoa(data_source.sin_addr), 57610275Ssam ntohs(data_source.sin_port), 57736304Skarels errno < sys_nerr ? sys_errlist[errno] : "unknown error"); 57810275Ssam return (NULL); 57910275Ssam } 58010275Ssam data = fileno(file); 58136304Skarels while (connect(data, (struct sockaddr *)&data_dest, 58236304Skarels sizeof (data_dest)) < 0) { 58311653Ssam if (errno == EADDRINUSE && retry < swaitmax) { 58426493Sminshall sleep((unsigned) swaitint); 58511653Ssam retry += swaitint; 58611653Ssam continue; 58711653Ssam } 58836304Skarels perror_reply(425, "Can't build data connection"); 58910275Ssam (void) fclose(file); 59010275Ssam data = -1; 59110275Ssam return (NULL); 59210275Ssam } 59336235Skarels reply(150, "Opening %s mode data connection for %s%s.", 59436235Skarels type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf); 59510275Ssam return (file); 59610275Ssam } 59710275Ssam 59810275Ssam /* 59910275Ssam * Tranfer the contents of "instr" to 60010275Ssam * "outstr" peer using the appropriate 60110275Ssam * encapulation of the date subject 60210275Ssam * to Mode, Structure, and Type. 60310275Ssam * 60410275Ssam * NB: Form isn't handled. 60510275Ssam */ 60610275Ssam send_data(instr, outstr) 60710275Ssam FILE *instr, *outstr; 60810275Ssam { 60910275Ssam register int c; 61010275Ssam int netfd, filefd, cnt; 61110275Ssam char buf[BUFSIZ]; 61210275Ssam 61326044Sminshall transflag++; 61426044Sminshall if (setjmp(urgcatch)) { 61526044Sminshall transflag = 0; 61626044Sminshall return(-1); 61726044Sminshall } 61810275Ssam switch (type) { 61910275Ssam 62010275Ssam case TYPE_A: 62110275Ssam while ((c = getc(instr)) != EOF) { 62211220Ssam if (c == '\n') { 62326044Sminshall if (ferror (outstr)) { 62426044Sminshall transflag = 0; 62511220Ssam return (1); 62626044Sminshall } 62727750Sminshall (void) putc('\r', outstr); 62811220Ssam } 62927750Sminshall (void) putc(c, outstr); 63026044Sminshall /* if (c == '\r') */ 63126044Sminshall /* putc ('\0', outstr); */ 63210275Ssam } 63326044Sminshall transflag = 0; 63426044Sminshall if (ferror (instr) || ferror (outstr)) { 63511220Ssam return (1); 63626044Sminshall } 63710275Ssam return (0); 63810275Ssam 63910275Ssam case TYPE_I: 64010275Ssam case TYPE_L: 64110275Ssam netfd = fileno(outstr); 64210275Ssam filefd = fileno(instr); 64310275Ssam 64426044Sminshall while ((cnt = read(filefd, buf, sizeof (buf))) > 0) { 64526044Sminshall if (write(netfd, buf, cnt) < 0) { 64626044Sminshall transflag = 0; 64710275Ssam return (1); 64826044Sminshall } 64926044Sminshall } 65026044Sminshall transflag = 0; 65110275Ssam return (cnt < 0); 65210275Ssam } 65327106Smckusick reply(550, "Unimplemented TYPE %d in send_data", type); 65426044Sminshall transflag = 0; 65527106Smckusick return (-1); 65610275Ssam } 65710275Ssam 65810275Ssam /* 65910275Ssam * Transfer data from peer to 66010275Ssam * "outstr" using the appropriate 66110275Ssam * encapulation of the data subject 66210275Ssam * to Mode, Structure, and Type. 66310275Ssam * 66410275Ssam * N.B.: Form isn't handled. 66510275Ssam */ 66610275Ssam receive_data(instr, outstr) 66710275Ssam FILE *instr, *outstr; 66810275Ssam { 66910275Ssam register int c; 67011220Ssam int cnt; 67110275Ssam char buf[BUFSIZ]; 67210275Ssam 67310275Ssam 67426044Sminshall transflag++; 67526044Sminshall if (setjmp(urgcatch)) { 67626044Sminshall transflag = 0; 67726044Sminshall return(-1); 67826044Sminshall } 67910275Ssam switch (type) { 68010275Ssam 68110275Ssam case TYPE_I: 68210275Ssam case TYPE_L: 68326044Sminshall while ((cnt = read(fileno(instr), buf, sizeof buf)) > 0) { 68426044Sminshall if (write(fileno(outstr), buf, cnt) < 0) { 68526044Sminshall transflag = 0; 68610275Ssam return (1); 68726044Sminshall } 68826044Sminshall } 68926044Sminshall transflag = 0; 69010275Ssam return (cnt < 0); 69110275Ssam 69210275Ssam case TYPE_E: 69327106Smckusick reply(553, "TYPE E not implemented."); 69426044Sminshall transflag = 0; 69527106Smckusick return (-1); 69610275Ssam 69710275Ssam case TYPE_A: 69810275Ssam while ((c = getc(instr)) != EOF) { 69927750Sminshall while (c == '\r') { 70026044Sminshall if (ferror (outstr)) { 70126044Sminshall transflag = 0; 70211220Ssam return (1); 70326044Sminshall } 70411220Ssam if ((c = getc(instr)) != '\n') 70527750Sminshall (void) putc ('\r', outstr); 70626044Sminshall /* if (c == '\0') */ 70726044Sminshall /* continue; */ 70810275Ssam } 70927750Sminshall (void) putc (c, outstr); 71010275Ssam } 71126044Sminshall transflag = 0; 71211220Ssam if (ferror (instr) || ferror (outstr)) 71311220Ssam return (1); 71410275Ssam return (0); 71510275Ssam } 71626044Sminshall transflag = 0; 71710275Ssam fatal("Unknown type in receive_data."); 71810275Ssam /*NOTREACHED*/ 71910275Ssam } 72010275Ssam 72110275Ssam fatal(s) 72210275Ssam char *s; 72310275Ssam { 72410275Ssam reply(451, "Error in server: %s\n", s); 72510275Ssam reply(221, "Closing connection due to server error."); 72613247Ssam dologout(0); 72710275Ssam } 72810275Ssam 729*36435Sbostic reply(n, s, argp) 73010275Ssam int n; 73110275Ssam char *s; 732*36435Sbostic va_list argp; 73310275Ssam { 73410275Ssam printf("%d ", n); 735*36435Sbostic printf(s, argp); 73610275Ssam printf("\r\n"); 737*36435Sbostic (void)fflush(stdout); 73810275Ssam if (debug) { 73926493Sminshall syslog(LOG_DEBUG, "<--- %d ", n); 740*36435Sbostic syslog(LOG_DEBUG, s, argp); 74110275Ssam } 74210275Ssam } 74310275Ssam 744*36435Sbostic lreply(n, s) 74510275Ssam int n; 74610275Ssam char *s; 74710275Ssam { 748*36435Sbostic printf("%d- %s\r\n", n, s); 749*36435Sbostic (void)fflush(stdout); 750*36435Sbostic if (debug) 751*36435Sbostic syslog(LOG_DEBUG, "<--- %d- %s", n, s); 75210275Ssam } 75310275Ssam 75410275Ssam ack(s) 75510275Ssam char *s; 75610275Ssam { 75727106Smckusick reply(250, "%s command successful.", s); 75810275Ssam } 75910275Ssam 76010275Ssam nack(s) 76110275Ssam char *s; 76210275Ssam { 76310275Ssam reply(502, "%s command not implemented.", s); 76410275Ssam } 76510275Ssam 76636304Skarels /* ARGSUSED */ 76726493Sminshall yyerror(s) 76826493Sminshall char *s; 76910275Ssam { 77026044Sminshall char *cp; 77126044Sminshall 77226044Sminshall cp = index(cbuf,'\n'); 77326044Sminshall *cp = '\0'; 77426044Sminshall reply(500, "'%s': command not understood.",cbuf); 77510275Ssam } 77610275Ssam 77710275Ssam delete(name) 77810275Ssam char *name; 77910275Ssam { 78010275Ssam struct stat st; 78110275Ssam 78210275Ssam if (stat(name, &st) < 0) { 78336304Skarels perror_reply(550, name); 78410275Ssam return; 78510275Ssam } 78610275Ssam if ((st.st_mode&S_IFMT) == S_IFDIR) { 78710275Ssam if (rmdir(name) < 0) { 78836304Skarels perror_reply(550, name); 78910275Ssam return; 79010275Ssam } 79110275Ssam goto done; 79210275Ssam } 79310275Ssam if (unlink(name) < 0) { 79436304Skarels perror_reply(550, name); 79510275Ssam return; 79610275Ssam } 79710275Ssam done: 79810275Ssam ack("DELE"); 79910275Ssam } 80010275Ssam 80110275Ssam cwd(path) 80210275Ssam char *path; 80310275Ssam { 80410275Ssam 80510275Ssam if (chdir(path) < 0) { 80636304Skarels perror_reply(550, path); 80710275Ssam return; 80810275Ssam } 80910275Ssam ack("CWD"); 81010275Ssam } 81110275Ssam 81210303Ssam makedir(name) 81310275Ssam char *name; 81410275Ssam { 81536276Sbostic if (mkdir(name, 0777) < 0) 81636304Skarels perror_reply(550, name); 81736276Sbostic else 81836276Sbostic reply(257, "MKD command successful."); 81910275Ssam } 82010275Ssam 82110303Ssam removedir(name) 82210275Ssam char *name; 82310275Ssam { 82410275Ssam 82510275Ssam if (rmdir(name) < 0) { 82636304Skarels perror_reply(550, name); 82710275Ssam return; 82810275Ssam } 82927106Smckusick ack("RMD"); 83010275Ssam } 83110275Ssam 83210303Ssam pwd() 83310275Ssam { 83410303Ssam char path[MAXPATHLEN + 1]; 83536304Skarels extern char *getwd(); 83610275Ssam 83736304Skarels if (getwd(path) == (char *)NULL) { 83827106Smckusick reply(550, "%s.", path); 83910275Ssam return; 84010275Ssam } 84127106Smckusick reply(257, "\"%s\" is current directory.", path); 84210275Ssam } 84310275Ssam 84410275Ssam char * 84510275Ssam renamefrom(name) 84610275Ssam char *name; 84710275Ssam { 84810275Ssam struct stat st; 84910275Ssam 85010275Ssam if (stat(name, &st) < 0) { 85136304Skarels perror_reply(550, name); 85210275Ssam return ((char *)0); 85310275Ssam } 85410303Ssam reply(350, "File exists, ready for destination name"); 85510275Ssam return (name); 85610275Ssam } 85710275Ssam 85810275Ssam renamecmd(from, to) 85910275Ssam char *from, *to; 86010275Ssam { 86110275Ssam 86210275Ssam if (rename(from, to) < 0) { 86336304Skarels perror_reply(550, "rename"); 86410275Ssam return; 86510275Ssam } 86610275Ssam ack("RNTO"); 86710275Ssam } 86810275Ssam 86910275Ssam dolog(sin) 87010275Ssam struct sockaddr_in *sin; 87110275Ssam { 87236304Skarels struct hostent *hp = gethostbyaddr((char *)&sin->sin_addr, 87310275Ssam sizeof (struct in_addr), AF_INET); 87436304Skarels time_t t, time(); 87526493Sminshall extern char *ctime(); 87610275Ssam 87736304Skarels if (hp) 87826493Sminshall (void) strncpy(remotehost, hp->h_name, sizeof (remotehost)); 87936304Skarels else 88026493Sminshall (void) strncpy(remotehost, inet_ntoa(sin->sin_addr), 88113247Ssam sizeof (remotehost)); 88213247Ssam if (!logging) 88313247Ssam return; 88426493Sminshall t = time((time_t *) 0); 88536304Skarels syslog(LOG_INFO, "connection from %s at %s", 88636304Skarels remotehost, ctime(&t)); 88710275Ssam } 88810695Ssam 88910695Ssam /* 89013247Ssam * Record logout in wtmp file 89113247Ssam * and exit with supplied status. 89213247Ssam */ 89313247Ssam dologout(status) 89413247Ssam int status; 89513247Ssam { 89617580Ssam if (logged_in) { 89736304Skarels (void) seteuid((uid_t)0); 89835672Sbostic logwtmp(ttyline, "", ""); 89913247Ssam } 90014436Ssam /* beware of flushing buffers after a SIGPIPE */ 90114436Ssam _exit(status); 90213247Ssam } 90313247Ssam 90426044Sminshall myoob() 90526044Sminshall { 90627750Sminshall char *cp; 90726044Sminshall 90827750Sminshall /* only process if transfer occurring */ 90936304Skarels if (!transflag) 91026044Sminshall return; 91127750Sminshall cp = tmpline; 91227750Sminshall if (getline(cp, 7, stdin) == NULL) { 91336304Skarels reply(221, "You could at least say goodbye."); 91427750Sminshall dologout(0); 91526044Sminshall } 91626044Sminshall upper(cp); 91726227Ssam if (strcmp(cp, "ABOR\r\n")) 91826044Sminshall return; 91926044Sminshall tmpline[0] = '\0'; 92026044Sminshall reply(426,"Transfer aborted. Data connection closed."); 92126044Sminshall reply(226,"Abort successful"); 92226044Sminshall longjmp(urgcatch, 1); 92326044Sminshall } 92426044Sminshall 92527106Smckusick /* 92627106Smckusick * Note: The 530 reply codes could be 4xx codes, except nothing is 92727106Smckusick * given in the state tables except 421 which implies an exit. (RFC959) 92827106Smckusick */ 92926044Sminshall passive() 93026044Sminshall { 93126044Sminshall int len; 93226044Sminshall struct sockaddr_in tmp; 93326044Sminshall register char *p, *a; 93426044Sminshall 93526044Sminshall pdata = socket(AF_INET, SOCK_STREAM, 0); 93626044Sminshall if (pdata < 0) { 93727106Smckusick reply(530, "Can't open passive connection"); 93826044Sminshall return; 93926044Sminshall } 94026044Sminshall tmp = ctrl_addr; 94126044Sminshall tmp.sin_port = 0; 94236304Skarels (void) seteuid((uid_t)0); 94326493Sminshall if (bind(pdata, (struct sockaddr *) &tmp, sizeof(tmp)) < 0) { 94436304Skarels (void) seteuid((uid_t)pw->pw_uid); 94526044Sminshall (void) close(pdata); 94626044Sminshall pdata = -1; 94727106Smckusick reply(530, "Can't open passive connection"); 94826044Sminshall return; 94926044Sminshall } 95036304Skarels (void) seteuid((uid_t)pw->pw_uid); 95126044Sminshall len = sizeof(tmp); 95236304Skarels if (getsockname(pdata, (struct sockaddr *) &tmp, &len) < 0) { 95326044Sminshall (void) close(pdata); 95426044Sminshall pdata = -1; 95527106Smckusick reply(530, "Can't open passive connection"); 95626044Sminshall return; 95726044Sminshall } 95826044Sminshall if (listen(pdata, 1) < 0) { 95926044Sminshall (void) close(pdata); 96026044Sminshall pdata = -1; 96127106Smckusick reply(530, "Can't open passive connection"); 96226044Sminshall return; 96326044Sminshall } 96426044Sminshall a = (char *) &tmp.sin_addr; 96526044Sminshall p = (char *) &tmp.sin_port; 96626044Sminshall 96726044Sminshall #define UC(b) (((int) b) & 0xff) 96826044Sminshall 96926044Sminshall reply(227, "Entering Passive Mode (%d,%d,%d,%d,%d,%d)", UC(a[0]), 97026044Sminshall UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1])); 97126044Sminshall } 97226044Sminshall 97336304Skarels /* 97436304Skarels * Generate unique name for file with basename "local". 97536304Skarels * The file named "local" is already known to exist. 97636304Skarels * Generates failure reply on error. 97736304Skarels */ 97826044Sminshall char * 97926044Sminshall gunique(local) 98026044Sminshall char *local; 98126044Sminshall { 98226044Sminshall static char new[MAXPATHLEN]; 98336304Skarels struct stat st; 98426044Sminshall char *cp = rindex(local, '/'); 98526044Sminshall int d, count=0; 98626044Sminshall 98736304Skarels if (cp) 98826044Sminshall *cp = '\0'; 98936304Skarels d = stat(cp ? local : ".", &st); 99036304Skarels if (cp) 99126044Sminshall *cp = '/'; 99226044Sminshall if (d < 0) { 99336304Skarels perror_reply(553, local); 99426044Sminshall return((char *) 0); 99526044Sminshall } 99626044Sminshall (void) strcpy(new, local); 99726044Sminshall cp = new + strlen(new); 99826044Sminshall *cp++ = '.'; 99936304Skarels for (count = 1; count < 100; count++) { 100036304Skarels (void) sprintf(cp, "%d", count); 100136304Skarels if (stat(new, &st) < 0) 100236304Skarels return(new); 100326044Sminshall } 100436304Skarels reply(452, "Unique file name cannot be created."); 100536304Skarels return((char *) 0); 100626044Sminshall } 100736304Skarels 100836304Skarels /* 100936304Skarels * Format and send reply containing system error number. 101036304Skarels */ 101136304Skarels perror_reply(code, string) 101236304Skarels int code; 101336304Skarels char *string; 101436304Skarels { 101536304Skarels 101636304Skarels if (errno < sys_nerr) 101736304Skarels reply(code, "%s: %s.", string, sys_errlist[errno]); 101836304Skarels else 101936304Skarels reply(code, "%s: unknown error %d.", string, errno); 102036304Skarels } 1021