110275Ssam #ifndef lint 2*10566Ssam static char sccsid[] = "@(#)ftpd.c 4.11 (Berkeley) 01/21/83"; 310275Ssam #endif 410275Ssam 510275Ssam /* 610275Ssam * FTP server. 710275Ssam */ 810303Ssam #include <sys/param.h> 910275Ssam #include <sys/stat.h> 1010275Ssam #include <sys/ioctl.h> 1110275Ssam #include <sys/socket.h> 1210275Ssam 1310275Ssam #include <netinet/in.h> 1410275Ssam 1510275Ssam #include <stdio.h> 1610275Ssam #include <signal.h> 1710275Ssam #include <wait.h> 1810275Ssam #include <pwd.h> 1910275Ssam #include <setjmp.h> 2010275Ssam #include <netdb.h> 2110423Ssam #include <errno.h> 2210275Ssam 2310275Ssam #include "ftp.h" 2410275Ssam 2510275Ssam extern int errno; 2610275Ssam extern char *sys_errlist[]; 2710275Ssam extern char *crypt(); 2810275Ssam extern char version[]; 2910275Ssam extern char *home; /* pointer to home directory for glob */ 3010275Ssam extern FILE *popen(), *fopen(); 3110275Ssam extern int pclose(), fclose(); 3210275Ssam 3310275Ssam struct sockaddr_in ctrl_addr; 3410275Ssam struct sockaddr_in data_source; 3510275Ssam struct sockaddr_in data_dest; 3610275Ssam struct sockaddr_in his_addr; 3710275Ssam 3810275Ssam struct hostent *hp; 3910275Ssam 4010275Ssam int data; 4110275Ssam jmp_buf errcatch; 4210275Ssam int logged_in; 4310275Ssam struct passwd *pw; 4410275Ssam int debug; 4510275Ssam int logging = 1; 4610275Ssam int guest; 4710275Ssam int type; 4810275Ssam int form; 4910275Ssam int stru; /* avoid C keyword */ 5010275Ssam int mode; 5110321Ssam int usedefault = 1; /* for data transfers */ 5210275Ssam char hostname[32]; 5310275Ssam char *remotehost; 5410321Ssam struct servent *sp; 5510275Ssam 5610275Ssam int lostconn(); 5710419Ssam int reapchild(); 5810275Ssam FILE *getdatasock(), *dataconn(); 5910275Ssam char *ntoa(); 6010275Ssam 6110275Ssam main(argc, argv) 6210275Ssam int argc; 6310275Ssam char *argv[]; 6410275Ssam { 6510275Ssam int ctrl, s, options = 0; 6610275Ssam char *cp; 6710275Ssam 6810275Ssam sp = getservbyname("ftp", "tcp"); 6910275Ssam if (sp == 0) { 7010275Ssam fprintf(stderr, "ftpd: fpt/tcp: unknown service\n"); 7110275Ssam exit(1); 7210275Ssam } 7310275Ssam ctrl_addr.sin_port = sp->s_port; 7410275Ssam data_source.sin_port = htons(ntohs(sp->s_port) - 1); 7510275Ssam signal(SIGPIPE, lostconn); 7610275Ssam debug = 0; 7710275Ssam argc--, argv++; 7810275Ssam while (argc > 0 && *argv[0] == '-') { 7910275Ssam for (cp = &argv[0][1]; *cp; cp++) switch (*cp) { 8010275Ssam 8110275Ssam case 'd': 8210275Ssam debug = 1; 8310275Ssam options |= SO_DEBUG; 8410275Ssam break; 8510275Ssam 8610275Ssam default: 8710275Ssam fprintf(stderr, "Unknown flag -%c ignored.\n", cp); 8810275Ssam break; 8910275Ssam } 9010275Ssam argc--, argv++; 9110275Ssam } 9210275Ssam #ifndef DEBUG 9310275Ssam if (fork()) 9410275Ssam exit(0); 9510275Ssam for (s = 0; s < 10; s++) 9610317Ssam (void) close(s); 9710275Ssam (void) open("/dev/null", 0); 9810275Ssam (void) dup2(0, 1); 9910275Ssam { int tt = open("/dev/tty", 2); 10010275Ssam if (tt > 0) { 10110275Ssam ioctl(tt, TIOCNOTTY, 0); 10210275Ssam close(tt); 10310275Ssam } 10410275Ssam } 10510275Ssam #endif 10610303Ssam while ((s = socket(AF_INET, SOCK_STREAM, 0, 0)) < 0) { 10710275Ssam perror("ftpd: socket"); 10810275Ssam sleep(5); 10910275Ssam } 11010419Ssam if (options & SO_DEBUG) 11110419Ssam if (setsockopt(s, SOL_SOCKET, SO_DEBUG, 0, 0) < 0) 11210419Ssam perror("ftpd: setsockopt (SO_DEBUG)"); 11310419Ssam #ifdef notdef 11410419Ssam if (setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, 0, 0) < 0) 11510419Ssam perror("ftpd: setsockopt (SO_KEEPALIVE)"); 11610419Ssam #endif 11710275Ssam while (bind(s, &ctrl_addr, sizeof (ctrl_addr), 0) < 0) { 11810275Ssam perror("ftpd: bind"); 11910275Ssam sleep(5); 12010275Ssam } 12110419Ssam signal(SIGCHLD, reapchild); 12210303Ssam listen(s, 10); 12310275Ssam for (;;) { 12410275Ssam int hisaddrlen = sizeof (his_addr); 12510275Ssam 12610275Ssam ctrl = accept(s, &his_addr, &hisaddrlen, 0); 12710275Ssam if (ctrl < 0) { 12810419Ssam if (errno == EINTR) 12910419Ssam continue; 13010275Ssam perror("ftpd: accept"); 13110275Ssam continue; 13210275Ssam } 13310275Ssam if (fork() == 0) { 13410275Ssam if (logging) 13510275Ssam dolog(&his_addr); 13610275Ssam close(s); 13710275Ssam dup2(ctrl, 0), close(ctrl), dup2(0, 1); 13810275Ssam /* do telnet option negotiation here */ 13910303Ssam /* 14010303Ssam * Set up default state 14110303Ssam */ 14210275Ssam logged_in = 0; 14310275Ssam data = -1; 14410303Ssam type = TYPE_A; 14510303Ssam form = FORM_N; 14610303Ssam stru = STRU_F; 14710303Ssam mode = MODE_S; 14810275Ssam gethostname(hostname, sizeof (hostname)); 14910275Ssam reply(220, "%s FTP server (%s) ready.", 15010275Ssam hostname, version); 15110275Ssam for (;;) { 15210275Ssam setjmp(errcatch); 15310275Ssam yyparse(); 15410275Ssam } 15510275Ssam } 15610275Ssam close(ctrl); 15710275Ssam } 15810275Ssam } 15910275Ssam 16010419Ssam reapchild() 16110419Ssam { 16210419Ssam union wait status; 16310419Ssam 16410419Ssam while (wait3(&status, WNOHANG, 0) > 0) 16510419Ssam ; 16610419Ssam } 16710419Ssam 16810275Ssam lostconn() 16910275Ssam { 17010275Ssam 17110275Ssam fatal("Connection closed."); 17210275Ssam } 17310275Ssam 17410275Ssam pass(passwd) 17510275Ssam char *passwd; 17610275Ssam { 17710303Ssam char *xpasswd, *savestr(); 17810303Ssam static struct passwd save; 17910275Ssam 18010275Ssam if (logged_in || pw == NULL) { 18110275Ssam reply(503, "Login with USER first."); 18210275Ssam return; 18310275Ssam } 18410275Ssam if (!guest) { /* "ftp" is only account allowed no password */ 18510275Ssam xpasswd = crypt(passwd, pw->pw_passwd); 18610275Ssam if (strcmp(xpasswd, pw->pw_passwd) != 0) { 18710275Ssam reply(530, "Login incorrect."); 18810275Ssam pw = NULL; 18910275Ssam return; 19010275Ssam } 19110275Ssam } 19210303Ssam setegid(pw->pw_gid); 19310275Ssam initgroups(pw->pw_name, pw->pw_gid); 19410275Ssam if (chdir(pw->pw_dir)) { 19510275Ssam reply(550, "User %s: can't change directory to $s.", 19610275Ssam pw->pw_name, pw->pw_dir); 19710303Ssam goto bad; 19810275Ssam } 19910303Ssam if (guest && chroot(pw->pw_dir) < 0) { 20010275Ssam reply(550, "Can't set guest privileges."); 20110303Ssam goto bad; 20210275Ssam } 20310275Ssam if (!guest) 20410275Ssam reply(230, "User %s logged in.", pw->pw_name); 20510275Ssam else 20610275Ssam reply(230, "Guest login ok, access restrictions apply."); 20710275Ssam logged_in = 1; 20810303Ssam seteuid(pw->pw_uid); 20910303Ssam /* 21010303Ssam * Save everything so globbing doesn't 21110303Ssam * clobber the fields. 21210303Ssam */ 21310303Ssam save = *pw; 21410303Ssam save.pw_name = savestr(pw->pw_name); 21510303Ssam save.pw_passwd = savestr(pw->pw_passwd); 21610303Ssam save.pw_comment = savestr(pw->pw_comment); 21710303Ssam save.pw_gecos = savestr(pw->pw_gecos, &save.pw_gecos); 21810303Ssam save.pw_dir = savestr(pw->pw_dir); 21910303Ssam save.pw_shell = savestr(pw->pw_shell); 22010303Ssam pw = &save; 22110303Ssam home = pw->pw_dir; /* home dir for globbing */ 22210303Ssam return; 22310303Ssam bad: 22410303Ssam seteuid(0); 22510303Ssam pw = NULL; 22610275Ssam } 22710275Ssam 22810303Ssam char * 22910303Ssam savestr(s) 23010303Ssam char *s; 23110303Ssam { 23210303Ssam char *malloc(); 23310303Ssam char *new = malloc(strlen(s) + 1); 23410303Ssam 23510303Ssam if (new != NULL) 23610303Ssam strcpy(new, s); 23710303Ssam return(new); 23810303Ssam } 23910303Ssam 24010275Ssam retrieve(cmd, name) 24110275Ssam char *cmd, *name; 24210275Ssam { 24310275Ssam FILE *fin, *dout; 24410275Ssam struct stat st; 24510275Ssam int (*closefunc)(); 24610275Ssam 24710275Ssam if (cmd == 0) { 24810317Ssam #ifdef notdef 24910317Ssam /* no remote command execution -- it's a security hole */ 25010275Ssam if (*name == '!') 25110275Ssam fin = popen(name + 1, "r"), closefunc = pclose; 25210275Ssam else 25310317Ssam #endif 25410275Ssam fin = fopen(name, "r"), closefunc = fclose; 25510275Ssam } else { 25610275Ssam char line[BUFSIZ]; 25710275Ssam 25810422Ssam sprintf(line, cmd, name), name = line; 25910275Ssam fin = popen(line, "r"), closefunc = pclose; 26010275Ssam } 26110275Ssam if (fin == NULL) { 26210275Ssam reply(550, "%s: %s.", name, sys_errlist[errno]); 26310275Ssam return; 26410275Ssam } 26510275Ssam st.st_size = 0; 26610275Ssam if (cmd == 0 && 26710275Ssam (stat(name, &st) < 0 || (st.st_mode&S_IFMT) != S_IFREG)) { 26810275Ssam reply(550, "%s: not a plain file.", name); 26910275Ssam goto done; 27010275Ssam } 27110275Ssam dout = dataconn(name, st.st_size, "w"); 27210275Ssam if (dout == NULL) 27310275Ssam goto done; 27410303Ssam if (send_data(fin, dout) || ferror(dout)) 27510275Ssam reply(550, "%s: %s.", name, sys_errlist[errno]); 27610275Ssam else 27710275Ssam reply(226, "Transfer complete."); 27810303Ssam fclose(dout), data = -1; 27910275Ssam done: 28010275Ssam (*closefunc)(fin); 28110275Ssam } 28210275Ssam 28310275Ssam store(name, mode) 28410275Ssam char *name, *mode; 28510275Ssam { 28610275Ssam FILE *fout, *din; 28710303Ssam int (*closefunc)(), dochown = 0; 28810275Ssam 28910317Ssam #ifdef notdef 29010317Ssam /* no remote command execution -- it's a security hole */ 29110275Ssam if (name[0] == '!') 29210275Ssam fout = popen(&name[1], "w"), closefunc = pclose; 29310317Ssam else 29410317Ssam #endif 29510317Ssam { 29610303Ssam struct stat st; 29710303Ssam 29810303Ssam if (stat(name, &st) < 0) 29910303Ssam dochown++; 30010275Ssam fout = fopen(name, mode), closefunc = fclose; 30110303Ssam } 30210275Ssam if (fout == NULL) { 30310275Ssam reply(550, "%s: %s.", name, sys_errlist[errno]); 30410275Ssam return; 30510275Ssam } 30610275Ssam din = dataconn(name, -1, "r"); 30710275Ssam if (din == NULL) 30810275Ssam goto done; 30910303Ssam if (receive_data(din, fout) || ferror(fout)) 31010275Ssam reply(550, "%s: %s.", name, sys_errlist[errno]); 31110275Ssam else 31210275Ssam reply(226, "Transfer complete."); 31310275Ssam fclose(din), data = -1; 31410275Ssam done: 31510303Ssam if (dochown) 31610303Ssam (void) chown(name, pw->pw_uid, -1); 31710275Ssam (*closefunc)(fout); 31810275Ssam } 31910275Ssam 32010275Ssam FILE * 32110275Ssam getdatasock(mode) 32210275Ssam char *mode; 32310275Ssam { 32410275Ssam int retrytime, s; 32510275Ssam 32610275Ssam if (data >= 0) 32710275Ssam return (fdopen(data, mode)); 32810275Ssam retrytime = 1; 32910275Ssam while ((s = socket(AF_INET, SOCK_STREAM, 0, 0)) < 0) { 33010275Ssam if (retrytime < 5) { 33110275Ssam sleep(retrytime); 33210275Ssam retrytime <<= 1; 33310275Ssam continue; 33410275Ssam } 33510275Ssam return (NULL); 33610275Ssam } 33710275Ssam retrytime = 1; 33810275Ssam seteuid(0); 33910275Ssam while (bind(s, &data_source, sizeof (data_source), 0) < 0) { 34010275Ssam if (retrytime < 5) { 34110275Ssam sleep(retrytime); 34210275Ssam retrytime <<= 1; 34310275Ssam continue; 34410275Ssam } 34510311Ssam seteuid(pw->pw_uid); 34610275Ssam close(s); 34710275Ssam return (NULL); 34810275Ssam } 34910311Ssam seteuid(pw->pw_uid); 35010275Ssam return (fdopen(s, mode)); 35110275Ssam } 35210275Ssam 35310275Ssam FILE * 35410275Ssam dataconn(name, size, mode) 35510275Ssam char *name; 35610275Ssam int size; 35710275Ssam char *mode; 35810275Ssam { 35910275Ssam char sizebuf[32]; 36010275Ssam FILE *file; 36110275Ssam 36210275Ssam if (size >= 0) 36310275Ssam sprintf(sizebuf, " (%d bytes)", size); 36410275Ssam else 36510275Ssam (void) strcpy(sizebuf, ""); 36610275Ssam if (data >= 0) { 36710275Ssam reply(125, "Using existing data connection for %s%s.", 36810275Ssam name, sizebuf); 36910321Ssam usedefault = 1; 37010275Ssam return (fdopen(data, mode)); 37110275Ssam } 372*10566Ssam if (usedefault) 37310422Ssam data_dest = his_addr; 37410422Ssam usedefault = 1; 37510275Ssam reply(150, "Opening data connection for %s (%s,%d)%s.", 37610275Ssam name, ntoa(data_dest.sin_addr.s_addr), 37710275Ssam ntohs(data_dest.sin_port), sizebuf); 37810275Ssam file = getdatasock(mode); 37910275Ssam if (file == NULL) { 38010275Ssam reply(425, "Can't create data socket (%s,%d): %s.", 38110275Ssam ntoa(data_source.sin_addr), 38210275Ssam ntohs(data_source.sin_port), 38310275Ssam sys_errlist[errno]); 38410275Ssam return (NULL); 38510275Ssam } 38610275Ssam data = fileno(file); 38710275Ssam if (connect(data, &data_dest, sizeof (data_dest), 0) < 0) { 38810275Ssam reply(425, "Can't build data connection: %s.", 38910275Ssam sys_errlist[errno]); 39010275Ssam (void) fclose(file); 39110275Ssam data = -1; 39210275Ssam return (NULL); 39310275Ssam } 39410275Ssam return (file); 39510275Ssam } 39610275Ssam 39710275Ssam /* 39810275Ssam * Tranfer the contents of "instr" to 39910275Ssam * "outstr" peer using the appropriate 40010275Ssam * encapulation of the date subject 40110275Ssam * to Mode, Structure, and Type. 40210275Ssam * 40310275Ssam * NB: Form isn't handled. 40410275Ssam */ 40510275Ssam send_data(instr, outstr) 40610275Ssam FILE *instr, *outstr; 40710275Ssam { 40810275Ssam register int c; 40910275Ssam int netfd, filefd, cnt; 41010275Ssam char buf[BUFSIZ]; 41110275Ssam 41210275Ssam switch (type) { 41310275Ssam 41410275Ssam case TYPE_A: 41510275Ssam while ((c = getc(instr)) != EOF) { 41610275Ssam if (c == '\n') 41710275Ssam putc('\r', outstr); 41810275Ssam if (putc(c, outstr) == EOF) 41910275Ssam return (1); 42010275Ssam } 42110275Ssam return (0); 42210275Ssam 42310275Ssam case TYPE_I: 42410275Ssam case TYPE_L: 42510275Ssam netfd = fileno(outstr); 42610275Ssam filefd = fileno(instr); 42710275Ssam 42810303Ssam while ((cnt = read(filefd, buf, sizeof (buf))) > 0) 42910275Ssam if (write(netfd, buf, cnt) < 0) 43010275Ssam return (1); 43110275Ssam return (cnt < 0); 43210275Ssam } 43310275Ssam reply(504,"Unimplemented TYPE %d in send_data", type); 43410275Ssam return (1); 43510275Ssam } 43610275Ssam 43710275Ssam /* 43810275Ssam * Transfer data from peer to 43910275Ssam * "outstr" using the appropriate 44010275Ssam * encapulation of the data subject 44110275Ssam * to Mode, Structure, and Type. 44210275Ssam * 44310275Ssam * N.B.: Form isn't handled. 44410275Ssam */ 44510275Ssam receive_data(instr, outstr) 44610275Ssam FILE *instr, *outstr; 44710275Ssam { 44810275Ssam register int c; 44910275Ssam int cr, escape, eof; 45010275Ssam int netfd, filefd, cnt; 45110275Ssam char buf[BUFSIZ]; 45210275Ssam 45310275Ssam 45410275Ssam switch (type) { 45510275Ssam 45610275Ssam case TYPE_I: 45710275Ssam case TYPE_L: 45810275Ssam netfd = fileno(instr); 45910275Ssam netfd = fileno(outstr); 46010275Ssam while ((cnt = read(netfd, buf, sizeof buf)) > 0) 46110275Ssam if (write(filefd, buf, cnt) < 0) 46210275Ssam return (1); 46310275Ssam return (cnt < 0); 46410275Ssam 46510275Ssam case TYPE_E: 46610275Ssam reply(504, "TYPE E not implemented."); 46710275Ssam return (1); 46810275Ssam 46910275Ssam case TYPE_A: 47010275Ssam cr = 0; 47110275Ssam while ((c = getc(instr)) != EOF) { 47210275Ssam if (cr) { 47310275Ssam if (c != '\r' && c != '\n') 47410275Ssam putc('\r', outstr); 47510275Ssam putc(c, outstr); 47610275Ssam cr = c == '\r'; 47710275Ssam continue; 47810275Ssam } 47910275Ssam if (c == '\r') { 48010275Ssam cr = 1; 48110275Ssam continue; 48210275Ssam } 48310275Ssam putc(c, outstr); 48410275Ssam } 48510275Ssam if (cr) 48610275Ssam putc('\r', outstr); 48710275Ssam return (0); 48810275Ssam } 48910275Ssam fatal("Unknown type in receive_data."); 49010275Ssam /*NOTREACHED*/ 49110275Ssam } 49210275Ssam 49310275Ssam fatal(s) 49410275Ssam char *s; 49510275Ssam { 49610275Ssam reply(451, "Error in server: %s\n", s); 49710275Ssam reply(221, "Closing connection due to server error."); 49810275Ssam exit(0); 49910275Ssam } 50010275Ssam 50110275Ssam reply(n, s, args) 50210275Ssam int n; 50310275Ssam char *s; 50410275Ssam { 50510275Ssam 50610275Ssam printf("%d ", n); 50710275Ssam _doprnt(s, &args, stdout); 50810275Ssam printf("\r\n"); 50910275Ssam fflush(stdout); 51010275Ssam if (debug) { 51110275Ssam fprintf(stderr, "<--- %d ", n); 51210275Ssam _doprnt(s, &args, stderr); 51310275Ssam fprintf(stderr, "\n"); 51410275Ssam fflush(stderr); 51510275Ssam } 51610275Ssam } 51710275Ssam 51810275Ssam lreply(n, s, args) 51910275Ssam int n; 52010275Ssam char *s; 52110275Ssam { 52210275Ssam printf("%d-", n); 52310275Ssam _doprnt(s, &args, stdout); 52410275Ssam printf("\r\n"); 52510275Ssam fflush(stdout); 52610275Ssam if (debug) { 52710275Ssam fprintf(stderr, "<--- %d-", n); 52810275Ssam _doprnt(s, &args, stderr); 52910275Ssam fprintf(stderr, "\n"); 53010275Ssam } 53110275Ssam } 53210275Ssam 53310275Ssam replystr(s) 53410275Ssam char *s; 53510275Ssam { 53610275Ssam printf("%s\r\n", s); 53710275Ssam fflush(stdout); 53810275Ssam if (debug) 53910275Ssam fprintf(stderr, "<--- %s\n", s); 54010275Ssam } 54110275Ssam 54210275Ssam ack(s) 54310275Ssam char *s; 54410275Ssam { 54510275Ssam reply(200, "%s command okay.", s); 54610275Ssam } 54710275Ssam 54810275Ssam nack(s) 54910275Ssam char *s; 55010275Ssam { 55110275Ssam reply(502, "%s command not implemented.", s); 55210275Ssam } 55310275Ssam 55410275Ssam yyerror() 55510275Ssam { 55610275Ssam reply(500, "Command not understood."); 55710275Ssam } 55810275Ssam 55910275Ssam delete(name) 56010275Ssam char *name; 56110275Ssam { 56210275Ssam struct stat st; 56310275Ssam 56410275Ssam if (stat(name, &st) < 0) { 56510275Ssam reply(550, "%s: %s.", name, sys_errlist[errno]); 56610275Ssam return; 56710275Ssam } 56810275Ssam if ((st.st_mode&S_IFMT) == S_IFDIR) { 56910275Ssam if (rmdir(name) < 0) { 57010275Ssam reply(550, "%s: %s.", name, sys_errlist[errno]); 57110275Ssam return; 57210275Ssam } 57310275Ssam goto done; 57410275Ssam } 57510275Ssam if (unlink(name) < 0) { 57610275Ssam reply(550, "%s: %s.", name, sys_errlist[errno]); 57710275Ssam return; 57810275Ssam } 57910275Ssam done: 58010275Ssam ack("DELE"); 58110275Ssam } 58210275Ssam 58310275Ssam cwd(path) 58410275Ssam char *path; 58510275Ssam { 58610275Ssam 58710275Ssam if (chdir(path) < 0) { 58810275Ssam reply(550, "%s: %s.", path, sys_errlist[errno]); 58910275Ssam return; 59010275Ssam } 59110275Ssam ack("CWD"); 59210275Ssam } 59310275Ssam 59410303Ssam makedir(name) 59510275Ssam char *name; 59610275Ssam { 59710303Ssam struct stat st; 59810303Ssam int dochown = stat(name, &st) < 0; 59910275Ssam 60010275Ssam if (mkdir(name, 0777) < 0) { 60110275Ssam reply(550, "%s: %s.", name, sys_errlist[errno]); 60210275Ssam return; 60310275Ssam } 60410303Ssam if (dochown) 60510303Ssam (void) chown(name, pw->pw_uid, -1); 60610275Ssam ack("MKDIR"); 60710275Ssam } 60810275Ssam 60910303Ssam removedir(name) 61010275Ssam char *name; 61110275Ssam { 61210275Ssam 61310275Ssam if (rmdir(name) < 0) { 61410275Ssam reply(550, "%s: %s.", name, sys_errlist[errno]); 61510275Ssam return; 61610275Ssam } 61710275Ssam ack("RMDIR"); 61810275Ssam } 61910275Ssam 62010303Ssam pwd() 62110275Ssam { 62210303Ssam char path[MAXPATHLEN + 1]; 62310275Ssam char *p; 62410275Ssam 62510275Ssam if (getwd(path) == NULL) { 62610275Ssam reply(451, "%s.", path); 62710275Ssam return; 62810275Ssam } 62910275Ssam reply(251, "\"%s\" is current directory.", path); 63010275Ssam } 63110275Ssam 63210275Ssam char * 63310275Ssam renamefrom(name) 63410275Ssam char *name; 63510275Ssam { 63610275Ssam struct stat st; 63710275Ssam 63810275Ssam if (stat(name, &st) < 0) { 63910275Ssam reply(550, "%s: %s.", name, sys_errlist[errno]); 64010275Ssam return ((char *)0); 64110275Ssam } 64210303Ssam reply(350, "File exists, ready for destination name"); 64310275Ssam return (name); 64410275Ssam } 64510275Ssam 64610275Ssam renamecmd(from, to) 64710275Ssam char *from, *to; 64810275Ssam { 64910275Ssam 65010275Ssam if (rename(from, to) < 0) { 65110275Ssam reply(550, "rename: %s.", sys_errlist[errno]); 65210275Ssam return; 65310275Ssam } 65410275Ssam ack("RNTO"); 65510275Ssam } 65610275Ssam 65710275Ssam int guest; 65810275Ssam /* 65910275Ssam * Test pathname for guest-user safety. 66010275Ssam */ 66110275Ssam inappropriate_request(name) 66210275Ssam char *name; 66310275Ssam { 66410275Ssam int bogus = 0, depth = 0, length = strlen(name); 66510275Ssam char *p, *s; 66610275Ssam 66710275Ssam if (!guest) 66810275Ssam return (0); 66910275Ssam if (name[0] == '/' || name[0] == '|') 67010275Ssam bogus = 1; 67110275Ssam for (p = name; p < name+length;) { 67210275Ssam s = p; /* start of token */ 67310275Ssam while ( *p && *p!= '/') 67410275Ssam p++; 67510275Ssam *p = 0; 67610275Ssam if (strcmp(s, "..") == 0) 67710275Ssam depth -= 1; /* backing up */ 67810275Ssam else if (strcmp(s, ".") == 0) 67910275Ssam depth += 0; /* no change */ 68010275Ssam else 68110275Ssam depth += 1; /* descending */ 68210275Ssam if (depth < 0) { 68310275Ssam bogus = 1; 68410275Ssam break; 68510275Ssam } 68610275Ssam } 68710275Ssam if (bogus) 68810275Ssam reply(553, "%s: pathname disallowed guest users", name); 68910275Ssam return (bogus); 69010275Ssam } 69110275Ssam 69210275Ssam /* 69310275Ssam * Convert network-format internet address 69410275Ssam * to base 256 d.d.d.d representation. 69510275Ssam */ 69610275Ssam char * 69710275Ssam ntoa(in) 69810275Ssam struct in_addr in; 69910275Ssam { 70010275Ssam static char b[18]; 70110275Ssam register char *p; 70210275Ssam 70310275Ssam in.s_addr = ntohl(in.s_addr); 70410275Ssam p = (char *)∈ 70510275Ssam #define UC(b) (((int)b)&0xff) 70610275Ssam sprintf(b, "%d.%d.%d.%d", UC(p[0]), UC(p[1]), UC(p[2]), UC(p[3])); 70710275Ssam return (b); 70810275Ssam } 70910275Ssam 71010275Ssam dolog(sin) 71110275Ssam struct sockaddr_in *sin; 71210275Ssam { 71310275Ssam struct hostent *hp = gethostbyaddr(&sin->sin_addr, 71410275Ssam sizeof (struct in_addr), AF_INET); 71510275Ssam char *remotehost; 71610275Ssam time_t t; 71710275Ssam 71810275Ssam if (hp) 71910275Ssam remotehost = hp->h_name; 72010275Ssam else 72110275Ssam remotehost = "UNKNOWNHOST"; 72210275Ssam t = time(0); 72310303Ssam fprintf(stderr,"FTP: connection from %s at %s", remotehost, ctime(&t)); 72410275Ssam fflush(stderr); 72510275Ssam } 726