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*36551Sbostic static char sccsid[] = "@(#)ftpd.c 5.24 (Berkeley) 01/15/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> 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> 5236435Sbostic #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[]; 71*36551Sbostic extern off_t restart_point; 7210275Ssam 7310275Ssam struct sockaddr_in ctrl_addr; 7410275Ssam struct sockaddr_in data_source; 7510275Ssam struct sockaddr_in data_dest; 7610275Ssam struct sockaddr_in his_addr; 7710275Ssam 7810275Ssam int data; 7926044Sminshall jmp_buf errcatch, urgcatch; 8010275Ssam int logged_in; 8110275Ssam struct passwd *pw; 8210275Ssam int debug; 8326493Sminshall int timeout = 900; /* timeout after 15 minutes of inactivity */ 8411757Ssam int logging; 8510275Ssam int guest; 8610275Ssam int type; 8710275Ssam int form; 8810275Ssam int stru; /* avoid C keyword */ 8910275Ssam int mode; 9010321Ssam int usedefault = 1; /* for data transfers */ 9136304Skarels int pdata = -1; /* for passive mode */ 9226044Sminshall int transflag; 9326044Sminshall char tmpline[7]; 9436276Sbostic char hostname[MAXHOSTNAMELEN]; 9536276Sbostic char remotehost[MAXHOSTNAMELEN]; 9610275Ssam 9711653Ssam /* 9811653Ssam * Timeout intervals for retrying connections 9911653Ssam * to hosts that don't accept PORT cmds. This 10011653Ssam * is a kludge, but given the problems with TCP... 10111653Ssam */ 10211653Ssam #define SWAITMAX 90 /* wait at most 90 seconds */ 10311653Ssam #define SWAITINT 5 /* interval between retries */ 10411653Ssam 10511653Ssam int swaitmax = SWAITMAX; 10611653Ssam int swaitint = SWAITINT; 10711653Ssam 10810275Ssam int lostconn(); 10926044Sminshall int myoob(); 11010275Ssam FILE *getdatasock(), *dataconn(); 11110275Ssam 11210275Ssam main(argc, argv) 11310275Ssam int argc; 11410275Ssam char *argv[]; 11510275Ssam { 11627750Sminshall int addrlen, on = 1; 11710275Ssam char *cp; 11810275Ssam 11916339Skarels addrlen = sizeof (his_addr); 12036304Skarels if (getpeername(0, (struct sockaddr *)&his_addr, &addrlen) < 0) { 12126493Sminshall syslog(LOG_ERR, "getpeername (%s): %m",argv[0]); 12210275Ssam exit(1); 12310275Ssam } 12416339Skarels addrlen = sizeof (ctrl_addr); 12536304Skarels if (getsockname(0, (struct sockaddr *)&ctrl_addr, &addrlen) < 0) { 12626493Sminshall syslog(LOG_ERR, "getsockname (%s): %m",argv[0]); 12716339Skarels exit(1); 12816339Skarels } 12916339Skarels data_source.sin_port = htons(ntohs(ctrl_addr.sin_port) - 1); 13010275Ssam debug = 0; 13126493Sminshall openlog("ftpd", LOG_PID, LOG_DAEMON); 13210275Ssam argc--, argv++; 13310275Ssam while (argc > 0 && *argv[0] == '-') { 13410275Ssam for (cp = &argv[0][1]; *cp; cp++) switch (*cp) { 13510275Ssam 13611653Ssam case 'v': 13711653Ssam debug = 1; 13811653Ssam break; 13911653Ssam 14010275Ssam case 'd': 14110275Ssam debug = 1; 14210275Ssam break; 14310275Ssam 14411757Ssam case 'l': 14511757Ssam logging = 1; 14611757Ssam break; 14711757Ssam 14811653Ssam case 't': 14911653Ssam timeout = atoi(++cp); 15011653Ssam goto nextopt; 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; 276*36551Sbostic char line[BUFSIZ], *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."); 292*36551Sbostic } 293*36551Sbostic else 29436304Skarels reply(530, "User %s unknown.", name); 29536304Skarels return; 29636304Skarels } 29736304Skarels if (pw = sgetpwnam(name)) { 29836304Skarels if ((shell = pw->pw_shell) == NULL || *shell == 0) 29936304Skarels shell = "/bin/sh"; 30036304Skarels while ((cp = getusershell()) != NULL) 30136304Skarels if (strcmp(cp, shell) == 0) 30236304Skarels break; 30336304Skarels endusershell(); 30436304Skarels if (cp == NULL) { 30536304Skarels reply(530, "User %s access denied.", name); 30636550Sbostic syslog(LOG_ERR, "FTP LOGIN REFUSED FROM %s, %s", 30736550Sbostic remotehost, name); 30836304Skarels pw = (struct passwd *) NULL; 30936304Skarels return; 31036304Skarels } 31136304Skarels if ((fd = fopen(FTPUSERS, "r")) != NULL) { 31236304Skarels while (fgets(line, sizeof (line), fd) != NULL) { 31336304Skarels if ((cp = index(line, '\n')) != NULL) 31436304Skarels *cp = '\0'; 31536304Skarels if (strcmp(line, name) == 0) { 31636304Skarels reply(530, "User %s access denied.", name); 31736550Sbostic syslog(LOG_ERR, "FTP LOGIN REFUSED FROM %s, %s", 31836550Sbostic remotehost, name); 31936304Skarels pw = (struct passwd *) NULL; 32036304Skarels return; 32136304Skarels } 32236304Skarels } 32336304Skarels } 32436304Skarels (void) fclose(fd); 32536304Skarels } 32636304Skarels reply(331, "Password required for %s.", name); 32736304Skarels askpasswd = 1; 32836304Skarels /* 32936304Skarels * Delay before reading passwd after first failed 33036304Skarels * attempt to slow down passwd-guessing programs. 33136304Skarels */ 33236304Skarels if (login_attempts) 33336304Skarels sleep((unsigned) login_attempts); 33436304Skarels } 33536304Skarels 33636304Skarels /* 33736304Skarels * Terminate login as previous user, if any, resetting state; 33836304Skarels * used when USER command is given or login fails. 33936304Skarels */ 34036304Skarels end_login() 34136304Skarels { 34236304Skarels 34336304Skarels (void) seteuid((uid_t)0); 34436304Skarels if (logged_in) 34536304Skarels logwtmp(ttyline, "", ""); 34636304Skarels pw = NULL; 34736304Skarels logged_in = 0; 34836304Skarels guest = 0; 34936304Skarels } 35036304Skarels 35110275Ssam pass(passwd) 35210275Ssam char *passwd; 35310275Ssam { 35436304Skarels char *xpasswd, *salt; 35510275Ssam 35636304Skarels if (logged_in || askpasswd == 0) { 35710275Ssam reply(503, "Login with USER first."); 35810275Ssam return; 35910275Ssam } 36036304Skarels askpasswd = 0; 36110275Ssam if (!guest) { /* "ftp" is only account allowed no password */ 36236304Skarels if (pw == NULL) 36336304Skarels salt = "xx"; 36436304Skarels else 36536304Skarels salt = pw->pw_passwd; 36636304Skarels xpasswd = crypt(passwd, salt); 36716760Slepreau /* The strcmp does not catch null passwords! */ 36836304Skarels if (pw == NULL || *pw->pw_passwd == '\0' || 36936304Skarels strcmp(xpasswd, pw->pw_passwd)) { 37010275Ssam reply(530, "Login incorrect."); 37110275Ssam pw = NULL; 37236304Skarels if (login_attempts++ >= 5) { 37336304Skarels syslog(LOG_ERR, 37436304Skarels "repeated login failures from %s", 37536304Skarels remotehost); 37636304Skarels exit(0); 37736304Skarels } 37810275Ssam return; 37910275Ssam } 38010275Ssam } 38136304Skarels login_attempts = 0; /* this time successful */ 38236304Skarels (void) setegid((gid_t)pw->pw_gid); 38336304Skarels (void) initgroups(pw->pw_name, pw->pw_gid); 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 39036446Sbostic if (guest) { 39136446Sbostic if (chroot(pw->pw_dir) < 0) { 39236446Sbostic reply(550, "Can't set guest privileges."); 39336446Sbostic goto bad; 39436446Sbostic } 39536304Skarels } 39636446Sbostic else if (chdir(pw->pw_dir)) 39736446Sbostic if (chdir("/")) { 39836446Sbostic reply(530, "User %s: can't change directory to %s.", 39936446Sbostic pw->pw_name, pw->pw_dir); 40036446Sbostic goto bad; 40136446Sbostic } 40236446Sbostic else 40336446Sbostic lreply(230, "No directory! Logging in with home=/"); 40436304Skarels if (seteuid((uid_t)pw->pw_uid) < 0) { 40536304Skarels reply(550, "Can't set uid."); 40636304Skarels goto bad; 40736304Skarels } 40836550Sbostic if (guest) { 40936192Sbostic reply(230, "Guest login ok, access restrictions apply."); 41036550Sbostic syslog(LOG_INFO, "ANONYMOUS FTP LOGIN FROM %s, %s", 41136550Sbostic remotehost, passwd); 412*36551Sbostic } 413*36551Sbostic else { 41410275Ssam reply(230, "User %s logged in.", pw->pw_name); 41536550Sbostic syslog(LOG_INFO, "FTP LOGIN FROM %s, %s", 41636550Sbostic remotehost, pw->pw_name); 41736550Sbostic } 41810303Ssam home = pw->pw_dir; /* home dir for globbing */ 41910303Ssam return; 42010303Ssam bad: 42136304Skarels /* Forget all about it... */ 42236304Skarels end_login(); 42310275Ssam } 42410275Ssam 42510275Ssam retrieve(cmd, name) 42610275Ssam char *cmd, *name; 42710275Ssam { 42810275Ssam FILE *fin, *dout; 42910275Ssam struct stat st; 43026044Sminshall int (*closefunc)(), tmp; 43110275Ssam 43236446Sbostic if (cmd == 0) 43336446Sbostic fin = fopen(name, "r"), closefunc = fclose; 43436446Sbostic else { 43510275Ssam char line[BUFSIZ]; 43610275Ssam 43726493Sminshall (void) sprintf(line, cmd, name), name = line; 43836304Skarels fin = ftpd_popen(line, "r"), closefunc = ftpd_pclose; 43910275Ssam } 44010275Ssam if (fin == NULL) { 44113152Ssam if (errno != 0) 44236304Skarels perror_reply(550, name); 44310275Ssam return; 44410275Ssam } 44510275Ssam st.st_size = 0; 44610275Ssam if (cmd == 0 && 44710275Ssam (stat(name, &st) < 0 || (st.st_mode&S_IFMT) != S_IFREG)) { 44810275Ssam reply(550, "%s: not a plain file.", name); 44910275Ssam goto done; 45010275Ssam } 451*36551Sbostic if (restart_point) 452*36551Sbostic if (type == TYPE_A) { 453*36551Sbostic if (fseek(fin, restart_point, L_SET) < 0) 454*36551Sbostic perror_reply(550, name); 455*36551Sbostic } 456*36551Sbostic else if (lseek(fileno(fin), restart_point, L_SET) < 0) 457*36551Sbostic perror_reply(550, name); 45810275Ssam dout = dataconn(name, st.st_size, "w"); 45910275Ssam if (dout == NULL) 46010275Ssam goto done; 46136446Sbostic if ((tmp = send_data(fin, dout, st.st_blksize)) > 0 || 462*36551Sbostic ferror(dout) > 0) 46336304Skarels perror_reply(550, name); 464*36551Sbostic else if (tmp == 0) 46510275Ssam reply(226, "Transfer complete."); 46626493Sminshall (void) fclose(dout); 46726044Sminshall data = -1; 46826044Sminshall pdata = -1; 46910275Ssam done: 47010275Ssam (*closefunc)(fin); 47110275Ssam } 47210275Ssam 47336304Skarels store(name, mode, unique) 47410275Ssam char *name, *mode; 47536304Skarels int unique; 47610275Ssam { 47710275Ssam FILE *fout, *din; 47836446Sbostic struct stat st; 47936304Skarels int (*closefunc)(), tmp; 48036304Skarels char *gunique(); 48110275Ssam 48236446Sbostic if (unique && stat(name, &st) == 0 && 48336446Sbostic (name = gunique(name)) == NULL) 48436446Sbostic return; 48510303Ssam 48636446Sbostic fout = fopen(name, mode), closefunc = fclose; 48710275Ssam if (fout == NULL) { 48836304Skarels perror_reply(553, name); 48910275Ssam return; 49010275Ssam } 491*36551Sbostic if (restart_point) 492*36551Sbostic if (type == TYPE_A) { 493*36551Sbostic if (fseek(fout, restart_point, L_SET) < 0) 494*36551Sbostic perror_reply(550, name); 495*36551Sbostic } 496*36551Sbostic else if (lseek(fileno(fout), restart_point, L_SET) < 0) 497*36551Sbostic perror_reply(550, name); 49836304Skarels din = dataconn(name, (off_t)-1, "r"); 49910275Ssam if (din == NULL) 50010275Ssam goto done; 50136304Skarels if ((tmp = receive_data(din, fout)) > 0) 50236304Skarels perror_reply(552, name); 50336304Skarels else if (tmp == 0) { 50436304Skarels if (ferror(fout) > 0) 50536304Skarels perror_reply(552, name); 50636304Skarels else if (unique) 50736304Skarels reply(226, "Transfer complete (unique file name:%s).", 50836304Skarels name); 50936304Skarels else 51036304Skarels reply(226, "Transfer complete."); 51126044Sminshall } 51226493Sminshall (void) fclose(din); 51326044Sminshall data = -1; 51426044Sminshall pdata = -1; 51510275Ssam done: 51610275Ssam (*closefunc)(fout); 51710275Ssam } 51810275Ssam 51910275Ssam FILE * 52010275Ssam getdatasock(mode) 52110275Ssam char *mode; 52210275Ssam { 52317157Ssam int s, on = 1; 52410275Ssam 52510275Ssam if (data >= 0) 52610275Ssam return (fdopen(data, mode)); 52713247Ssam s = socket(AF_INET, SOCK_STREAM, 0); 52810602Ssam if (s < 0) 52910275Ssam return (NULL); 53036304Skarels (void) seteuid((uid_t)0); 53126493Sminshall if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof (on)) < 0) 53210602Ssam goto bad; 53313152Ssam /* anchor socket to avoid multi-homing problems */ 53413152Ssam data_source.sin_family = AF_INET; 53513152Ssam data_source.sin_addr = ctrl_addr.sin_addr; 53636304Skarels if (bind(s, (struct sockaddr *)&data_source, sizeof (data_source)) < 0) 53710602Ssam goto bad; 53836304Skarels (void) seteuid((uid_t)pw->pw_uid); 53910275Ssam return (fdopen(s, mode)); 54010602Ssam bad: 54136304Skarels (void) seteuid((uid_t)pw->pw_uid); 54226493Sminshall (void) close(s); 54310602Ssam return (NULL); 54410275Ssam } 54510275Ssam 54610275Ssam FILE * 54710275Ssam dataconn(name, size, mode) 54810275Ssam char *name; 54911653Ssam off_t size; 55010275Ssam char *mode; 55110275Ssam { 55210275Ssam char sizebuf[32]; 55310275Ssam FILE *file; 55411653Ssam int retry = 0; 55510275Ssam 55636304Skarels if (size != (off_t) -1) 55726493Sminshall (void) sprintf (sizebuf, " (%ld bytes)", size); 55810275Ssam else 55910275Ssam (void) strcpy(sizebuf, ""); 56036304Skarels if (pdata >= 0) { 56126044Sminshall struct sockaddr_in from; 56226044Sminshall int s, fromlen = sizeof(from); 56326044Sminshall 56436304Skarels s = accept(pdata, (struct sockaddr *)&from, &fromlen); 56526044Sminshall if (s < 0) { 56626044Sminshall reply(425, "Can't open data connection."); 56726044Sminshall (void) close(pdata); 56826044Sminshall pdata = -1; 56926044Sminshall return(NULL); 57026044Sminshall } 57126044Sminshall (void) close(pdata); 57226044Sminshall pdata = s; 57336235Skarels reply(150, "Opening %s mode data connection for %s%s.", 57436235Skarels type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf); 57526044Sminshall return(fdopen(pdata, mode)); 57626044Sminshall } 57710275Ssam if (data >= 0) { 57810275Ssam reply(125, "Using existing data connection for %s%s.", 57910275Ssam name, sizebuf); 58010321Ssam usedefault = 1; 58110275Ssam return (fdopen(data, mode)); 58210275Ssam } 58310566Ssam if (usedefault) 58410422Ssam data_dest = his_addr; 58510422Ssam usedefault = 1; 58610275Ssam file = getdatasock(mode); 58710275Ssam if (file == NULL) { 58810275Ssam reply(425, "Can't create data socket (%s,%d): %s.", 58913247Ssam inet_ntoa(data_source.sin_addr), 59010275Ssam ntohs(data_source.sin_port), 59136304Skarels errno < sys_nerr ? sys_errlist[errno] : "unknown error"); 59210275Ssam return (NULL); 59310275Ssam } 59410275Ssam data = fileno(file); 59536304Skarels while (connect(data, (struct sockaddr *)&data_dest, 59636304Skarels sizeof (data_dest)) < 0) { 59711653Ssam if (errno == EADDRINUSE && retry < swaitmax) { 59826493Sminshall sleep((unsigned) swaitint); 59911653Ssam retry += swaitint; 60011653Ssam continue; 60111653Ssam } 60236304Skarels perror_reply(425, "Can't build data connection"); 60310275Ssam (void) fclose(file); 60410275Ssam data = -1; 60510275Ssam return (NULL); 60610275Ssam } 60736235Skarels reply(150, "Opening %s mode data connection for %s%s.", 60836235Skarels type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf); 60910275Ssam return (file); 61010275Ssam } 61110275Ssam 61210275Ssam /* 61310275Ssam * Tranfer the contents of "instr" to 61410275Ssam * "outstr" peer using the appropriate 61510275Ssam * encapulation of the date subject 61610275Ssam * to Mode, Structure, and Type. 61710275Ssam * 61810275Ssam * NB: Form isn't handled. 61910275Ssam */ 62036446Sbostic send_data(instr, outstr, blksize) 62110275Ssam FILE *instr, *outstr; 62236446Sbostic off_t blksize; 62310275Ssam { 62436446Sbostic register int c, cnt; 62536446Sbostic register char *buf; 62636446Sbostic int netfd, filefd; 62710275Ssam 62826044Sminshall transflag++; 62926044Sminshall if (setjmp(urgcatch)) { 63026044Sminshall transflag = 0; 63126044Sminshall return(-1); 63226044Sminshall } 63310275Ssam switch (type) { 63410275Ssam 63510275Ssam case TYPE_A: 63610275Ssam while ((c = getc(instr)) != EOF) { 63711220Ssam if (c == '\n') { 63826044Sminshall if (ferror (outstr)) { 63926044Sminshall transflag = 0; 64011220Ssam return (1); 64126044Sminshall } 64227750Sminshall (void) putc('\r', outstr); 64311220Ssam } 64427750Sminshall (void) putc(c, outstr); 64510275Ssam } 64626044Sminshall transflag = 0; 64726044Sminshall if (ferror (instr) || ferror (outstr)) { 64811220Ssam return (1); 64926044Sminshall } 65010275Ssam return (0); 65136446Sbostic 65210275Ssam case TYPE_I: 65310275Ssam case TYPE_L: 65436446Sbostic if ((buf = malloc((u_int)blksize)) == NULL) { 65536446Sbostic transflag = 0; 65636446Sbostic return (1); 65736446Sbostic } 65810275Ssam netfd = fileno(outstr); 65910275Ssam filefd = fileno(instr); 66036446Sbostic while ((cnt = read(filefd, buf, sizeof(buf))) > 0 && 66136446Sbostic write(netfd, buf, cnt) == cnt); 66226044Sminshall transflag = 0; 66336446Sbostic (void)free(buf); 66436446Sbostic return (cnt != 0); 66510275Ssam } 66627106Smckusick reply(550, "Unimplemented TYPE %d in send_data", type); 66726044Sminshall transflag = 0; 66827106Smckusick return (-1); 66910275Ssam } 67010275Ssam 67110275Ssam /* 67210275Ssam * Transfer data from peer to 67310275Ssam * "outstr" using the appropriate 67410275Ssam * encapulation of the data subject 67510275Ssam * to Mode, Structure, and Type. 67610275Ssam * 67710275Ssam * N.B.: Form isn't handled. 67810275Ssam */ 67910275Ssam receive_data(instr, outstr) 68010275Ssam FILE *instr, *outstr; 68110275Ssam { 68210275Ssam register int c; 68311220Ssam int cnt; 68410275Ssam char buf[BUFSIZ]; 68510275Ssam 68610275Ssam 68726044Sminshall transflag++; 68826044Sminshall if (setjmp(urgcatch)) { 68926044Sminshall transflag = 0; 69026044Sminshall return(-1); 69126044Sminshall } 69210275Ssam switch (type) { 69310275Ssam 69410275Ssam case TYPE_I: 69510275Ssam case TYPE_L: 69626044Sminshall while ((cnt = read(fileno(instr), buf, sizeof buf)) > 0) { 69726044Sminshall if (write(fileno(outstr), buf, cnt) < 0) { 69826044Sminshall transflag = 0; 69910275Ssam return (1); 70026044Sminshall } 70126044Sminshall } 70226044Sminshall transflag = 0; 70310275Ssam return (cnt < 0); 70410275Ssam 70510275Ssam case TYPE_E: 70627106Smckusick reply(553, "TYPE E not implemented."); 70726044Sminshall transflag = 0; 70827106Smckusick return (-1); 70910275Ssam 71010275Ssam case TYPE_A: 71110275Ssam while ((c = getc(instr)) != EOF) { 71227750Sminshall while (c == '\r') { 71326044Sminshall if (ferror (outstr)) { 71426044Sminshall transflag = 0; 71511220Ssam return (1); 71626044Sminshall } 71711220Ssam if ((c = getc(instr)) != '\n') 71827750Sminshall (void) putc ('\r', outstr); 71926044Sminshall /* if (c == '\0') */ 72026044Sminshall /* continue; */ 72110275Ssam } 72227750Sminshall (void) putc (c, outstr); 72310275Ssam } 72426044Sminshall transflag = 0; 72511220Ssam if (ferror (instr) || ferror (outstr)) 72611220Ssam return (1); 72710275Ssam return (0); 72810275Ssam } 72926044Sminshall transflag = 0; 73010275Ssam fatal("Unknown type in receive_data."); 73110275Ssam /*NOTREACHED*/ 73210275Ssam } 73310275Ssam 73410275Ssam fatal(s) 73510275Ssam char *s; 73610275Ssam { 73710275Ssam reply(451, "Error in server: %s\n", s); 73810275Ssam reply(221, "Closing connection due to server error."); 73913247Ssam dologout(0); 74010275Ssam } 74110275Ssam 74236446Sbostic /* VARARGS2 */ 74336446Sbostic reply(n, fmt, p0, p1, p2, p3, p4, p5) 74410275Ssam int n; 74536446Sbostic char *fmt; 74610275Ssam { 74710275Ssam printf("%d ", n); 74836446Sbostic printf(fmt, p0, p1, p2, p3, p4, p5); 74910275Ssam printf("\r\n"); 75036435Sbostic (void)fflush(stdout); 75110275Ssam if (debug) { 75226493Sminshall syslog(LOG_DEBUG, "<--- %d ", n); 75336446Sbostic syslog(LOG_DEBUG, fmt, p0, p1, p2, p3, p4, p5); 75410275Ssam } 75510275Ssam } 75610275Ssam 75736446Sbostic /* VARARGS2 */ 75836446Sbostic lreply(n, fmt, p0, p1, p2, p3, p4, p5) 75910275Ssam int n; 76036446Sbostic char *fmt; 76110275Ssam { 76236446Sbostic printf("%d- ", n); 76336446Sbostic printf(fmt, p0, p1, p2, p3, p4, p5); 76436446Sbostic printf("\r\n"); 76536435Sbostic (void)fflush(stdout); 76636446Sbostic if (debug) { 76736446Sbostic syslog(LOG_DEBUG, "<--- %d- ", n); 76836446Sbostic syslog(LOG_DEBUG, fmt, p0, p1, p2, p3, p4, p5); 76936446Sbostic } 77010275Ssam } 77110275Ssam 77210275Ssam ack(s) 77310275Ssam char *s; 77410275Ssam { 77527106Smckusick reply(250, "%s command successful.", s); 77610275Ssam } 77710275Ssam 77810275Ssam nack(s) 77910275Ssam char *s; 78010275Ssam { 78110275Ssam reply(502, "%s command not implemented.", s); 78210275Ssam } 78310275Ssam 78436304Skarels /* ARGSUSED */ 78526493Sminshall yyerror(s) 78626493Sminshall char *s; 78710275Ssam { 78826044Sminshall char *cp; 78926044Sminshall 790*36551Sbostic if (cp = index(cbuf,'\n')) 791*36551Sbostic *cp = '\0'; 79226044Sminshall reply(500, "'%s': command not understood.",cbuf); 79310275Ssam } 79410275Ssam 79510275Ssam delete(name) 79610275Ssam char *name; 79710275Ssam { 79810275Ssam struct stat st; 79910275Ssam 80010275Ssam if (stat(name, &st) < 0) { 80136304Skarels perror_reply(550, name); 80210275Ssam return; 80310275Ssam } 80410275Ssam if ((st.st_mode&S_IFMT) == S_IFDIR) { 80510275Ssam if (rmdir(name) < 0) { 80636304Skarels perror_reply(550, name); 80710275Ssam return; 80810275Ssam } 80910275Ssam goto done; 81010275Ssam } 81110275Ssam if (unlink(name) < 0) { 81236304Skarels perror_reply(550, name); 81310275Ssam return; 81410275Ssam } 81510275Ssam done: 81610275Ssam ack("DELE"); 81710275Ssam } 81810275Ssam 81910275Ssam cwd(path) 82010275Ssam char *path; 82110275Ssam { 82210275Ssam 82310275Ssam if (chdir(path) < 0) { 82436304Skarels perror_reply(550, path); 82510275Ssam return; 82610275Ssam } 82710275Ssam ack("CWD"); 82810275Ssam } 82910275Ssam 83010303Ssam makedir(name) 83110275Ssam char *name; 83210275Ssam { 83336276Sbostic if (mkdir(name, 0777) < 0) 83436304Skarels perror_reply(550, name); 83536276Sbostic else 83636276Sbostic reply(257, "MKD command successful."); 83710275Ssam } 83810275Ssam 83910303Ssam removedir(name) 84010275Ssam char *name; 84110275Ssam { 84210275Ssam 84310275Ssam if (rmdir(name) < 0) { 84436304Skarels perror_reply(550, name); 84510275Ssam return; 84610275Ssam } 84727106Smckusick ack("RMD"); 84810275Ssam } 84910275Ssam 85010303Ssam pwd() 85110275Ssam { 85210303Ssam char path[MAXPATHLEN + 1]; 85336304Skarels extern char *getwd(); 85410275Ssam 85536304Skarels if (getwd(path) == (char *)NULL) { 85627106Smckusick reply(550, "%s.", path); 85710275Ssam return; 85810275Ssam } 85927106Smckusick reply(257, "\"%s\" is current directory.", path); 86010275Ssam } 86110275Ssam 86210275Ssam char * 86310275Ssam renamefrom(name) 86410275Ssam char *name; 86510275Ssam { 86610275Ssam struct stat st; 86710275Ssam 86810275Ssam if (stat(name, &st) < 0) { 86936304Skarels perror_reply(550, name); 87010275Ssam return ((char *)0); 87110275Ssam } 87210303Ssam reply(350, "File exists, ready for destination name"); 87310275Ssam return (name); 87410275Ssam } 87510275Ssam 87610275Ssam renamecmd(from, to) 87710275Ssam char *from, *to; 87810275Ssam { 87910275Ssam 88010275Ssam if (rename(from, to) < 0) { 88136304Skarels perror_reply(550, "rename"); 88210275Ssam return; 88310275Ssam } 88410275Ssam ack("RNTO"); 88510275Ssam } 88610275Ssam 88710275Ssam dolog(sin) 88810275Ssam struct sockaddr_in *sin; 88910275Ssam { 89036304Skarels struct hostent *hp = gethostbyaddr((char *)&sin->sin_addr, 89110275Ssam sizeof (struct in_addr), AF_INET); 89236304Skarels time_t t, time(); 89326493Sminshall extern char *ctime(); 89410275Ssam 89536304Skarels if (hp) 89626493Sminshall (void) strncpy(remotehost, hp->h_name, sizeof (remotehost)); 89736304Skarels else 89826493Sminshall (void) strncpy(remotehost, inet_ntoa(sin->sin_addr), 89913247Ssam sizeof (remotehost)); 90013247Ssam if (!logging) 90113247Ssam return; 90226493Sminshall t = time((time_t *) 0); 90336304Skarels syslog(LOG_INFO, "connection from %s at %s", 90436304Skarels remotehost, ctime(&t)); 90510275Ssam } 90610695Ssam 90710695Ssam /* 90813247Ssam * Record logout in wtmp file 90913247Ssam * and exit with supplied status. 91013247Ssam */ 91113247Ssam dologout(status) 91213247Ssam int status; 91313247Ssam { 91417580Ssam if (logged_in) { 91536304Skarels (void) seteuid((uid_t)0); 91635672Sbostic logwtmp(ttyline, "", ""); 91713247Ssam } 91814436Ssam /* beware of flushing buffers after a SIGPIPE */ 91914436Ssam _exit(status); 92013247Ssam } 92113247Ssam 92226044Sminshall myoob() 92326044Sminshall { 92427750Sminshall char *cp; 92526044Sminshall 92627750Sminshall /* only process if transfer occurring */ 92736304Skarels if (!transflag) 92826044Sminshall return; 92927750Sminshall cp = tmpline; 93027750Sminshall if (getline(cp, 7, stdin) == NULL) { 93136304Skarels reply(221, "You could at least say goodbye."); 93227750Sminshall dologout(0); 93326044Sminshall } 93426044Sminshall upper(cp); 93526227Ssam if (strcmp(cp, "ABOR\r\n")) 93626044Sminshall return; 93726044Sminshall tmpline[0] = '\0'; 93826044Sminshall reply(426,"Transfer aborted. Data connection closed."); 93926044Sminshall reply(226,"Abort successful"); 94026044Sminshall longjmp(urgcatch, 1); 94126044Sminshall } 94226044Sminshall 94327106Smckusick /* 94427106Smckusick * Note: The 530 reply codes could be 4xx codes, except nothing is 94527106Smckusick * given in the state tables except 421 which implies an exit. (RFC959) 94627106Smckusick */ 94726044Sminshall passive() 94826044Sminshall { 94926044Sminshall int len; 95026044Sminshall struct sockaddr_in tmp; 95126044Sminshall register char *p, *a; 95226044Sminshall 95326044Sminshall pdata = socket(AF_INET, SOCK_STREAM, 0); 95426044Sminshall if (pdata < 0) { 95527106Smckusick reply(530, "Can't open passive connection"); 95626044Sminshall return; 95726044Sminshall } 95826044Sminshall tmp = ctrl_addr; 95926044Sminshall tmp.sin_port = 0; 96036304Skarels (void) seteuid((uid_t)0); 96126493Sminshall if (bind(pdata, (struct sockaddr *) &tmp, sizeof(tmp)) < 0) { 96236304Skarels (void) seteuid((uid_t)pw->pw_uid); 96326044Sminshall (void) close(pdata); 96426044Sminshall pdata = -1; 96527106Smckusick reply(530, "Can't open passive connection"); 96626044Sminshall return; 96726044Sminshall } 96836304Skarels (void) seteuid((uid_t)pw->pw_uid); 96926044Sminshall len = sizeof(tmp); 97036304Skarels if (getsockname(pdata, (struct sockaddr *) &tmp, &len) < 0) { 97126044Sminshall (void) close(pdata); 97226044Sminshall pdata = -1; 97327106Smckusick reply(530, "Can't open passive connection"); 97426044Sminshall return; 97526044Sminshall } 97626044Sminshall if (listen(pdata, 1) < 0) { 97726044Sminshall (void) close(pdata); 97826044Sminshall pdata = -1; 97927106Smckusick reply(530, "Can't open passive connection"); 98026044Sminshall return; 98126044Sminshall } 98226044Sminshall a = (char *) &tmp.sin_addr; 98326044Sminshall p = (char *) &tmp.sin_port; 98426044Sminshall 98526044Sminshall #define UC(b) (((int) b) & 0xff) 98626044Sminshall 98726044Sminshall reply(227, "Entering Passive Mode (%d,%d,%d,%d,%d,%d)", UC(a[0]), 98826044Sminshall UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1])); 98926044Sminshall } 99026044Sminshall 99136304Skarels /* 99236304Skarels * Generate unique name for file with basename "local". 99336304Skarels * The file named "local" is already known to exist. 99436304Skarels * Generates failure reply on error. 99536304Skarels */ 99626044Sminshall char * 99726044Sminshall gunique(local) 99826044Sminshall char *local; 99926044Sminshall { 100026044Sminshall static char new[MAXPATHLEN]; 100136304Skarels struct stat st; 100226044Sminshall char *cp = rindex(local, '/'); 100326044Sminshall int d, count=0; 100426044Sminshall 100536304Skarels if (cp) 100626044Sminshall *cp = '\0'; 100736304Skarels d = stat(cp ? local : ".", &st); 100836304Skarels if (cp) 100926044Sminshall *cp = '/'; 101026044Sminshall if (d < 0) { 101136304Skarels perror_reply(553, local); 101226044Sminshall return((char *) 0); 101326044Sminshall } 101426044Sminshall (void) strcpy(new, local); 101526044Sminshall cp = new + strlen(new); 101626044Sminshall *cp++ = '.'; 101736304Skarels for (count = 1; count < 100; count++) { 101836304Skarels (void) sprintf(cp, "%d", count); 101936304Skarels if (stat(new, &st) < 0) 102036304Skarels return(new); 102126044Sminshall } 102236304Skarels reply(452, "Unique file name cannot be created."); 102336304Skarels return((char *) 0); 102426044Sminshall } 102536304Skarels 102636304Skarels /* 102736304Skarels * Format and send reply containing system error number. 102836304Skarels */ 102936304Skarels perror_reply(code, string) 103036304Skarels int code; 103136304Skarels char *string; 103236304Skarels { 103336304Skarels 103436304Skarels if (errno < sys_nerr) 103536304Skarels reply(code, "%s: %s.", string, sys_errlist[errno]); 103636304Skarels else 103736304Skarels reply(code, "%s: unknown error %d.", string, errno); 103836304Skarels } 1039