xref: /csrg-svn/libexec/ftpd/ftpd.c (revision 36550)
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*36550Sbostic static char sccsid[] = "@(#)ftpd.c	5.23 (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[];
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 
15110275Ssam 		default:
15216339Skarels 			fprintf(stderr, "ftpd: Unknown flag -%c ignored.\n",
15316339Skarels 			     *cp);
15410275Ssam 			break;
15510275Ssam 		}
15611653Ssam nextopt:
15710275Ssam 		argc--, argv++;
15810275Ssam 	}
15930944Scsvsj 	(void) freopen("/dev/null", "w", stderr);
16026493Sminshall 	(void) signal(SIGPIPE, lostconn);
16126493Sminshall 	(void) signal(SIGCHLD, SIG_IGN);
16235691Sbostic 	if ((int)signal(SIGURG, myoob) < 0)
16326493Sminshall 		syslog(LOG_ERR, "signal: %m");
16435691Sbostic 
16527750Sminshall 	/* handle urgent data inline */
16636276Sbostic 	/* Sequent defines this, but it doesn't work */
16727750Sminshall #ifdef SO_OOBINLINE
16836276Sbostic 	if (setsockopt(0, SOL_SOCKET, SO_OOBINLINE, (char *)&on, sizeof(on)) < 0)
16927750Sminshall 		syslog(LOG_ERR, "setsockopt: %m");
17036276Sbostic #endif
17136304Skarels 	if (fcntl(fileno(stdin), F_SETOWN, getpid()) == -1)
17236304Skarels 		syslog(LOG_ERR, "fcntl F_SETOWN: %m");
17316760Slepreau 	dolog(&his_addr);
17416339Skarels 	/* do telnet option negotiation here */
17516339Skarels 	/*
17616339Skarels 	 * Set up default state
17716339Skarels 	 */
17816339Skarels 	data = -1;
17916339Skarels 	type = TYPE_A;
18016339Skarels 	form = FORM_N;
18116339Skarels 	stru = STRU_F;
18216339Skarels 	mode = MODE_S;
18326044Sminshall 	tmpline[0] = '\0';
18426493Sminshall 	(void) gethostname(hostname, sizeof (hostname));
18536276Sbostic 	reply(220, "%s FTP server (%s) ready.", hostname, version);
18636304Skarels 	(void) setjmp(errcatch);
18736304Skarels 	for (;;)
18826493Sminshall 		(void) yyparse();
18910275Ssam }
19010419Ssam 
19110275Ssam lostconn()
19210275Ssam {
19310275Ssam 
19414089Ssam 	if (debug)
19526493Sminshall 		syslog(LOG_DEBUG, "lost connection");
19614089Ssam 	dologout(-1);
19710275Ssam }
19810275Ssam 
19935672Sbostic static char ttyline[20];
20035672Sbostic 
20136185Sbostic /*
20236185Sbostic  * Helper function for sgetpwnam().
20336185Sbostic  */
20436185Sbostic char *
20536185Sbostic sgetsave(s)
20636185Sbostic 	char *s;
20736185Sbostic {
20836185Sbostic 	char *malloc();
20936185Sbostic 	char *new = malloc((unsigned) strlen(s) + 1);
21036185Sbostic 
21136185Sbostic 	if (new == NULL) {
21236276Sbostic 		reply(553, "Local resource failure: malloc");
21336185Sbostic 		dologout(1);
21436185Sbostic 	}
21536185Sbostic 	(void) strcpy(new, s);
21636185Sbostic 	return (new);
21736185Sbostic }
21836185Sbostic 
21936185Sbostic /*
22036185Sbostic  * Save the result of a getpwnam.  Used for USER command, since
22136185Sbostic  * the data returned must not be clobbered by any other command
22236185Sbostic  * (e.g., globbing).
22336185Sbostic  */
22436185Sbostic struct passwd *
22536185Sbostic sgetpwnam(name)
22636185Sbostic 	char *name;
22736185Sbostic {
22836185Sbostic 	static struct passwd save;
22936185Sbostic 	register struct passwd *p;
23036185Sbostic 	char *sgetsave();
23136185Sbostic 
23236185Sbostic 	if ((p = getpwnam(name)) == NULL)
23336185Sbostic 		return (p);
23436185Sbostic 	if (save.pw_name) {
23536185Sbostic 		free(save.pw_name);
23636185Sbostic 		free(save.pw_passwd);
23736185Sbostic 		free(save.pw_comment);
23836185Sbostic 		free(save.pw_gecos);
23936185Sbostic 		free(save.pw_dir);
24036185Sbostic 		free(save.pw_shell);
24136185Sbostic 	}
24236185Sbostic 	save = *p;
24336185Sbostic 	save.pw_name = sgetsave(p->pw_name);
24436185Sbostic 	save.pw_passwd = sgetsave(p->pw_passwd);
24536185Sbostic 	save.pw_comment = sgetsave(p->pw_comment);
24636185Sbostic 	save.pw_gecos = sgetsave(p->pw_gecos);
24736185Sbostic 	save.pw_dir = sgetsave(p->pw_dir);
24836185Sbostic 	save.pw_shell = sgetsave(p->pw_shell);
24936185Sbostic 	return (&save);
25036185Sbostic }
25136185Sbostic 
25236304Skarels int login_attempts;		/* number of failed login attempts */
25336304Skarels int askpasswd;			/* had user command, ask for passwd */
25436304Skarels 
25536304Skarels /*
25636304Skarels  * USER command.
25736304Skarels  * Sets global passwd pointer pw if named account exists
25836304Skarels  * and is acceptable; sets askpasswd if a PASS command is
25936304Skarels  * expected. If logged in previously, need to reset state.
26036304Skarels  * If name is "ftp" or "anonymous" and ftp account exists,
26136304Skarels  * set guest and pw, then just return.
26236304Skarels  * If account doesn't exist, ask for passwd anyway.
26336304Skarels  * Otherwise, check user requesting login privileges.
26436304Skarels  * Disallow anyone who does not have a standard
26536304Skarels  * shell returned by getusershell() (/etc/shells).
26636304Skarels  * Disallow anyone mentioned in the file FTPUSERS
26736304Skarels  * to allow people such as root and uucp to be avoided.
26836304Skarels  */
26936304Skarels user(name)
27036304Skarels 	char *name;
27136304Skarels {
27236304Skarels 	register char *cp;
27336304Skarels 	FILE *fd;
27436304Skarels 	char *shell;
27536304Skarels 	char line[BUFSIZ], *index(), *getusershell();
27636304Skarels 
27736304Skarels 	if (logged_in) {
27836304Skarels 		if (guest) {
27936304Skarels 			reply(530, "Can't change user from guest login.");
28036304Skarels 			return;
28136304Skarels 		}
28236304Skarels 		end_login();
28336304Skarels 	}
28436304Skarels 
28536304Skarels 	guest = 0;
28636304Skarels 	if (strcmp(name, "ftp") == 0 || strcmp(name, "anonymous") == 0) {
28736304Skarels 		if ((pw = sgetpwnam("ftp")) != NULL) {
28836304Skarels 			guest = 1;
28936304Skarels 			askpasswd = 1;
29036304Skarels 			reply(331, "Guest login ok, send ident as password.");
29136304Skarels 		} else
29236304Skarels 			reply(530, "User %s unknown.", name);
29336304Skarels 		return;
29436304Skarels 	}
29536304Skarels 	if (pw = sgetpwnam(name)) {
29636304Skarels 		if ((shell = pw->pw_shell) == NULL || *shell == 0)
29736304Skarels 			shell = "/bin/sh";
29836304Skarels 		while ((cp = getusershell()) != NULL)
29936304Skarels 			if (strcmp(cp, shell) == 0)
30036304Skarels 				break;
30136304Skarels 		endusershell();
30236304Skarels 		if (cp == NULL) {
30336304Skarels 			reply(530, "User %s access denied.", name);
304*36550Sbostic 			syslog(LOG_ERR, "FTP LOGIN REFUSED FROM %s, %s",
305*36550Sbostic 			    remotehost, name);
30636304Skarels 			pw = (struct passwd *) NULL;
30736304Skarels 			return;
30836304Skarels 		}
30936304Skarels 		if ((fd = fopen(FTPUSERS, "r")) != NULL) {
31036304Skarels 		    while (fgets(line, sizeof (line), fd) != NULL) {
31136304Skarels 			if ((cp = index(line, '\n')) != NULL)
31236304Skarels 				*cp = '\0';
31336304Skarels 			if (strcmp(line, name) == 0) {
31436304Skarels 				reply(530, "User %s access denied.", name);
315*36550Sbostic 				syslog(LOG_ERR, "FTP LOGIN REFUSED FROM %s, %s",
316*36550Sbostic 				    remotehost, name);
31736304Skarels 				pw = (struct passwd *) NULL;
31836304Skarels 				return;
31936304Skarels 			}
32036304Skarels 		    }
32136304Skarels 		}
32236304Skarels 		(void) fclose(fd);
32336304Skarels 	}
32436304Skarels 	reply(331, "Password required for %s.", name);
32536304Skarels 	askpasswd = 1;
32636304Skarels 	/*
32736304Skarels 	 * Delay before reading passwd after first failed
32836304Skarels 	 * attempt to slow down passwd-guessing programs.
32936304Skarels 	 */
33036304Skarels 	if (login_attempts)
33136304Skarels 		sleep((unsigned) login_attempts);
33236304Skarels }
33336304Skarels 
33436304Skarels /*
33536304Skarels  * Terminate login as previous user, if any, resetting state;
33636304Skarels  * used when USER command is given or login fails.
33736304Skarels  */
33836304Skarels end_login()
33936304Skarels {
34036304Skarels 
34136304Skarels 	(void) seteuid((uid_t)0);
34236304Skarels 	if (logged_in)
34336304Skarels 		logwtmp(ttyline, "", "");
34436304Skarels 	pw = NULL;
34536304Skarels 	logged_in = 0;
34636304Skarels 	guest = 0;
34736304Skarels }
34836304Skarels 
34910275Ssam pass(passwd)
35010275Ssam 	char *passwd;
35110275Ssam {
35236304Skarels 	char *xpasswd, *salt;
35310275Ssam 
35436304Skarels 	if (logged_in || askpasswd == 0) {
35510275Ssam 		reply(503, "Login with USER first.");
35610275Ssam 		return;
35710275Ssam 	}
35836304Skarels 	askpasswd = 0;
35910275Ssam 	if (!guest) {		/* "ftp" is only account allowed no password */
36036304Skarels 		if (pw == NULL)
36136304Skarels 			salt = "xx";
36236304Skarels 		else
36336304Skarels 			salt = pw->pw_passwd;
36436304Skarels 		xpasswd = crypt(passwd, salt);
36516760Slepreau 		/* The strcmp does not catch null passwords! */
36636304Skarels 		if (pw == NULL || *pw->pw_passwd == '\0' ||
36736304Skarels 		    strcmp(xpasswd, pw->pw_passwd)) {
36810275Ssam 			reply(530, "Login incorrect.");
36910275Ssam 			pw = NULL;
37036304Skarels 			if (login_attempts++ >= 5) {
37136304Skarels 				syslog(LOG_ERR,
37236304Skarels 				    "repeated login failures from %s",
37336304Skarels 				    remotehost);
37436304Skarels 				exit(0);
37536304Skarels 			}
37610275Ssam 			return;
37710275Ssam 		}
37810275Ssam 	}
37936304Skarels 	login_attempts = 0;		/* this time successful */
38036304Skarels 	(void) setegid((gid_t)pw->pw_gid);
38136304Skarels 	(void) initgroups(pw->pw_name, pw->pw_gid);
38216033Sralph 
38336192Sbostic 	/* open wtmp before chroot */
38436192Sbostic 	(void)sprintf(ttyline, "ftp%d", getpid());
38536192Sbostic 	logwtmp(ttyline, pw->pw_name, remotehost);
38636192Sbostic 	logged_in = 1;
38736192Sbostic 
38836446Sbostic 	if (guest) {
38936446Sbostic 		if (chroot(pw->pw_dir) < 0) {
39036446Sbostic 			reply(550, "Can't set guest privileges.");
39136446Sbostic 			goto bad;
39236446Sbostic 		}
39336304Skarels 	}
39436446Sbostic 	else if (chdir(pw->pw_dir))
39536446Sbostic 		if (chdir("/")) {
39636446Sbostic 			reply(530, "User %s: can't change directory to %s.",
39736446Sbostic 			    pw->pw_name, pw->pw_dir);
39836446Sbostic 			goto bad;
39936446Sbostic 		}
40036446Sbostic 		else
40136446Sbostic 			lreply(230, "No directory! Logging in with home=/");
40236304Skarels 	if (seteuid((uid_t)pw->pw_uid) < 0) {
40336304Skarels 		reply(550, "Can't set uid.");
40436304Skarels 		goto bad;
40536304Skarels 	}
406*36550Sbostic 	if (guest) {
40736192Sbostic 		reply(230, "Guest login ok, access restrictions apply.");
408*36550Sbostic 		syslog(LOG_INFO, "ANONYMOUS FTP LOGIN FROM %s, %s",
409*36550Sbostic 		    remotehost, passwd);
410*36550Sbostic 	} else {
41110275Ssam 		reply(230, "User %s logged in.", pw->pw_name);
412*36550Sbostic 		syslog(LOG_INFO, "FTP LOGIN FROM %s, %s",
413*36550Sbostic 		    remotehost, pw->pw_name);
414*36550Sbostic 	}
41510303Ssam 	home = pw->pw_dir;		/* home dir for globbing */
41610303Ssam 	return;
41710303Ssam bad:
41836304Skarels 	/* Forget all about it... */
41936304Skarels 	end_login();
42010275Ssam }
42110275Ssam 
42210275Ssam retrieve(cmd, name)
42310275Ssam 	char *cmd, *name;
42410275Ssam {
42510275Ssam 	FILE *fin, *dout;
42610275Ssam 	struct stat st;
42726044Sminshall 	int (*closefunc)(), tmp;
42810275Ssam 
42936446Sbostic 	if (cmd == 0)
43036446Sbostic 		fin = fopen(name, "r"), closefunc = fclose;
43136446Sbostic 	else {
43210275Ssam 		char line[BUFSIZ];
43310275Ssam 
43426493Sminshall 		(void) sprintf(line, cmd, name), name = line;
43536304Skarels 		fin = ftpd_popen(line, "r"), closefunc = ftpd_pclose;
43610275Ssam 	}
43710275Ssam 	if (fin == NULL) {
43813152Ssam 		if (errno != 0)
43936304Skarels 			perror_reply(550, name);
44010275Ssam 		return;
44110275Ssam 	}
44210275Ssam 	st.st_size = 0;
44310275Ssam 	if (cmd == 0 &&
44410275Ssam 	    (stat(name, &st) < 0 || (st.st_mode&S_IFMT) != S_IFREG)) {
44510275Ssam 		reply(550, "%s: not a plain file.", name);
44610275Ssam 		goto done;
44710275Ssam 	}
44810275Ssam 	dout = dataconn(name, st.st_size, "w");
44910275Ssam 	if (dout == NULL)
45010275Ssam 		goto done;
45136446Sbostic 	if ((tmp = send_data(fin, dout, st.st_blksize)) > 0 ||
45236446Sbostic 	    ferror(dout) > 0) {
45336304Skarels 		perror_reply(550, name);
45426044Sminshall 	}
45526044Sminshall 	else if (tmp == 0) {
45610275Ssam 		reply(226, "Transfer complete.");
45726044Sminshall 	}
45826493Sminshall 	(void) fclose(dout);
45926044Sminshall 	data = -1;
46026044Sminshall 	pdata = -1;
46110275Ssam done:
46210275Ssam 	(*closefunc)(fin);
46310275Ssam }
46410275Ssam 
46536304Skarels store(name, mode, unique)
46610275Ssam 	char *name, *mode;
46736304Skarels 	int unique;
46810275Ssam {
46910275Ssam 	FILE *fout, *din;
47036446Sbostic 	struct stat st;
47136304Skarels 	int (*closefunc)(), tmp;
47236304Skarels 	char *gunique();
47310275Ssam 
47436446Sbostic 	if (unique && stat(name, &st) == 0 &&
47536446Sbostic 	    (name = gunique(name)) == NULL)
47636446Sbostic 		return;
47710303Ssam 
47836446Sbostic 	fout = fopen(name, mode), closefunc = fclose;
47910275Ssam 	if (fout == NULL) {
48036304Skarels 		perror_reply(553, name);
48110275Ssam 		return;
48210275Ssam 	}
48336304Skarels 	din = dataconn(name, (off_t)-1, "r");
48410275Ssam 	if (din == NULL)
48510275Ssam 		goto done;
48636304Skarels 	if ((tmp = receive_data(din, fout)) > 0)
48736304Skarels 		perror_reply(552, name);
48836304Skarels 	else if (tmp == 0) {
48936304Skarels 		if (ferror(fout) > 0)
49036304Skarels 			perror_reply(552, name);
49136304Skarels 		else if (unique)
49236304Skarels 			reply(226, "Transfer complete (unique file name:%s).",
49336304Skarels 			    name);
49436304Skarels 		else
49536304Skarels 			reply(226, "Transfer complete.");
49626044Sminshall 	}
49726493Sminshall 	(void) fclose(din);
49826044Sminshall 	data = -1;
49926044Sminshall 	pdata = -1;
50010275Ssam done:
50110275Ssam 	(*closefunc)(fout);
50210275Ssam }
50310275Ssam 
50410275Ssam FILE *
50510275Ssam getdatasock(mode)
50610275Ssam 	char *mode;
50710275Ssam {
50817157Ssam 	int s, on = 1;
50910275Ssam 
51010275Ssam 	if (data >= 0)
51110275Ssam 		return (fdopen(data, mode));
51213247Ssam 	s = socket(AF_INET, SOCK_STREAM, 0);
51310602Ssam 	if (s < 0)
51410275Ssam 		return (NULL);
51536304Skarels 	(void) seteuid((uid_t)0);
51626493Sminshall 	if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof (on)) < 0)
51710602Ssam 		goto bad;
51813152Ssam 	/* anchor socket to avoid multi-homing problems */
51913152Ssam 	data_source.sin_family = AF_INET;
52013152Ssam 	data_source.sin_addr = ctrl_addr.sin_addr;
52136304Skarels 	if (bind(s, (struct sockaddr *)&data_source, sizeof (data_source)) < 0)
52210602Ssam 		goto bad;
52336304Skarels 	(void) seteuid((uid_t)pw->pw_uid);
52410275Ssam 	return (fdopen(s, mode));
52510602Ssam bad:
52636304Skarels 	(void) seteuid((uid_t)pw->pw_uid);
52726493Sminshall 	(void) close(s);
52810602Ssam 	return (NULL);
52910275Ssam }
53010275Ssam 
53110275Ssam FILE *
53210275Ssam dataconn(name, size, mode)
53310275Ssam 	char *name;
53411653Ssam 	off_t size;
53510275Ssam 	char *mode;
53610275Ssam {
53710275Ssam 	char sizebuf[32];
53810275Ssam 	FILE *file;
53911653Ssam 	int retry = 0;
54010275Ssam 
54136304Skarels 	if (size != (off_t) -1)
54226493Sminshall 		(void) sprintf (sizebuf, " (%ld bytes)", size);
54310275Ssam 	else
54410275Ssam 		(void) strcpy(sizebuf, "");
54536304Skarels 	if (pdata >= 0) {
54626044Sminshall 		struct sockaddr_in from;
54726044Sminshall 		int s, fromlen = sizeof(from);
54826044Sminshall 
54936304Skarels 		s = accept(pdata, (struct sockaddr *)&from, &fromlen);
55026044Sminshall 		if (s < 0) {
55126044Sminshall 			reply(425, "Can't open data connection.");
55226044Sminshall 			(void) close(pdata);
55326044Sminshall 			pdata = -1;
55426044Sminshall 			return(NULL);
55526044Sminshall 		}
55626044Sminshall 		(void) close(pdata);
55726044Sminshall 		pdata = s;
55836235Skarels 		reply(150, "Opening %s mode data connection for %s%s.",
55936235Skarels 		     type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf);
56026044Sminshall 		return(fdopen(pdata, mode));
56126044Sminshall 	}
56210275Ssam 	if (data >= 0) {
56310275Ssam 		reply(125, "Using existing data connection for %s%s.",
56410275Ssam 		    name, sizebuf);
56510321Ssam 		usedefault = 1;
56610275Ssam 		return (fdopen(data, mode));
56710275Ssam 	}
56810566Ssam 	if (usedefault)
56910422Ssam 		data_dest = his_addr;
57010422Ssam 	usedefault = 1;
57110275Ssam 	file = getdatasock(mode);
57210275Ssam 	if (file == NULL) {
57310275Ssam 		reply(425, "Can't create data socket (%s,%d): %s.",
57413247Ssam 		    inet_ntoa(data_source.sin_addr),
57510275Ssam 		    ntohs(data_source.sin_port),
57636304Skarels 		    errno < sys_nerr ? sys_errlist[errno] : "unknown error");
57710275Ssam 		return (NULL);
57810275Ssam 	}
57910275Ssam 	data = fileno(file);
58036304Skarels 	while (connect(data, (struct sockaddr *)&data_dest,
58136304Skarels 	    sizeof (data_dest)) < 0) {
58211653Ssam 		if (errno == EADDRINUSE && retry < swaitmax) {
58326493Sminshall 			sleep((unsigned) swaitint);
58411653Ssam 			retry += swaitint;
58511653Ssam 			continue;
58611653Ssam 		}
58736304Skarels 		perror_reply(425, "Can't build data connection");
58810275Ssam 		(void) fclose(file);
58910275Ssam 		data = -1;
59010275Ssam 		return (NULL);
59110275Ssam 	}
59236235Skarels 	reply(150, "Opening %s mode data connection for %s%s.",
59336235Skarels 	     type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf);
59410275Ssam 	return (file);
59510275Ssam }
59610275Ssam 
59710275Ssam /*
59810275Ssam  * Tranfer the contents of "instr" to
59910275Ssam  * "outstr" peer using the appropriate
60010275Ssam  * encapulation of the date subject
60110275Ssam  * to Mode, Structure, and Type.
60210275Ssam  *
60310275Ssam  * NB: Form isn't handled.
60410275Ssam  */
60536446Sbostic send_data(instr, outstr, blksize)
60610275Ssam 	FILE *instr, *outstr;
60736446Sbostic 	off_t blksize;
60810275Ssam {
60936446Sbostic 	register int c, cnt;
61036446Sbostic 	register char *buf;
61136446Sbostic 	int netfd, filefd;
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);
63010275Ssam 		}
63126044Sminshall 		transflag = 0;
63226044Sminshall 		if (ferror (instr) || ferror (outstr)) {
63311220Ssam 			return (1);
63426044Sminshall 		}
63510275Ssam 		return (0);
63636446Sbostic 
63710275Ssam 	case TYPE_I:
63810275Ssam 	case TYPE_L:
63936446Sbostic 		if ((buf = malloc((u_int)blksize)) == NULL) {
64036446Sbostic 			transflag = 0;
64136446Sbostic 			return (1);
64236446Sbostic 		}
64310275Ssam 		netfd = fileno(outstr);
64410275Ssam 		filefd = fileno(instr);
64536446Sbostic 		while ((cnt = read(filefd, buf, sizeof(buf))) > 0 &&
64636446Sbostic 		    write(netfd, buf, cnt) == cnt);
64726044Sminshall 		transflag = 0;
64836446Sbostic 		(void)free(buf);
64936446Sbostic 		return (cnt != 0);
65010275Ssam 	}
65127106Smckusick 	reply(550, "Unimplemented TYPE %d in send_data", type);
65226044Sminshall 	transflag = 0;
65327106Smckusick 	return (-1);
65410275Ssam }
65510275Ssam 
65610275Ssam /*
65710275Ssam  * Transfer data from peer to
65810275Ssam  * "outstr" using the appropriate
65910275Ssam  * encapulation of the data subject
66010275Ssam  * to Mode, Structure, and Type.
66110275Ssam  *
66210275Ssam  * N.B.: Form isn't handled.
66310275Ssam  */
66410275Ssam receive_data(instr, outstr)
66510275Ssam 	FILE *instr, *outstr;
66610275Ssam {
66710275Ssam 	register int c;
66811220Ssam 	int cnt;
66910275Ssam 	char buf[BUFSIZ];
67010275Ssam 
67110275Ssam 
67226044Sminshall 	transflag++;
67326044Sminshall 	if (setjmp(urgcatch)) {
67426044Sminshall 		transflag = 0;
67526044Sminshall 		return(-1);
67626044Sminshall 	}
67710275Ssam 	switch (type) {
67810275Ssam 
67910275Ssam 	case TYPE_I:
68010275Ssam 	case TYPE_L:
68126044Sminshall 		while ((cnt = read(fileno(instr), buf, sizeof buf)) > 0) {
68226044Sminshall 			if (write(fileno(outstr), buf, cnt) < 0) {
68326044Sminshall 				transflag = 0;
68410275Ssam 				return (1);
68526044Sminshall 			}
68626044Sminshall 		}
68726044Sminshall 		transflag = 0;
68810275Ssam 		return (cnt < 0);
68910275Ssam 
69010275Ssam 	case TYPE_E:
69127106Smckusick 		reply(553, "TYPE E not implemented.");
69226044Sminshall 		transflag = 0;
69327106Smckusick 		return (-1);
69410275Ssam 
69510275Ssam 	case TYPE_A:
69610275Ssam 		while ((c = getc(instr)) != EOF) {
69727750Sminshall 			while (c == '\r') {
69826044Sminshall 				if (ferror (outstr)) {
69926044Sminshall 					transflag = 0;
70011220Ssam 					return (1);
70126044Sminshall 				}
70211220Ssam 				if ((c = getc(instr)) != '\n')
70327750Sminshall 					(void) putc ('\r', outstr);
70426044Sminshall 			/*	if (c == '\0')			*/
70526044Sminshall 			/*		continue;		*/
70610275Ssam 			}
70727750Sminshall 			(void) putc (c, outstr);
70810275Ssam 		}
70926044Sminshall 		transflag = 0;
71011220Ssam 		if (ferror (instr) || ferror (outstr))
71111220Ssam 			return (1);
71210275Ssam 		return (0);
71310275Ssam 	}
71426044Sminshall 	transflag = 0;
71510275Ssam 	fatal("Unknown type in receive_data.");
71610275Ssam 	/*NOTREACHED*/
71710275Ssam }
71810275Ssam 
71910275Ssam fatal(s)
72010275Ssam 	char *s;
72110275Ssam {
72210275Ssam 	reply(451, "Error in server: %s\n", s);
72310275Ssam 	reply(221, "Closing connection due to server error.");
72413247Ssam 	dologout(0);
72510275Ssam }
72610275Ssam 
72736446Sbostic /* VARARGS2 */
72836446Sbostic reply(n, fmt, p0, p1, p2, p3, p4, p5)
72910275Ssam 	int n;
73036446Sbostic 	char *fmt;
73110275Ssam {
73210275Ssam 	printf("%d ", n);
73336446Sbostic 	printf(fmt, p0, p1, p2, p3, p4, p5);
73410275Ssam 	printf("\r\n");
73536435Sbostic 	(void)fflush(stdout);
73610275Ssam 	if (debug) {
73726493Sminshall 		syslog(LOG_DEBUG, "<--- %d ", n);
73836446Sbostic 		syslog(LOG_DEBUG, fmt, p0, p1, p2, p3, p4, p5);
73910275Ssam 	}
74010275Ssam }
74110275Ssam 
74236446Sbostic /* VARARGS2 */
74336446Sbostic lreply(n, fmt, p0, p1, p2, p3, p4, p5)
74410275Ssam 	int n;
74536446Sbostic 	char *fmt;
74610275Ssam {
74736446Sbostic 	printf("%d- ", n);
74836446Sbostic 	printf(fmt, p0, p1, p2, p3, p4, p5);
74936446Sbostic 	printf("\r\n");
75036435Sbostic 	(void)fflush(stdout);
75136446Sbostic 	if (debug) {
75236446Sbostic 		syslog(LOG_DEBUG, "<--- %d- ", n);
75336446Sbostic 		syslog(LOG_DEBUG, fmt, p0, p1, p2, p3, p4, p5);
75436446Sbostic 	}
75510275Ssam }
75610275Ssam 
75710275Ssam ack(s)
75810275Ssam 	char *s;
75910275Ssam {
76027106Smckusick 	reply(250, "%s command successful.", s);
76110275Ssam }
76210275Ssam 
76310275Ssam nack(s)
76410275Ssam 	char *s;
76510275Ssam {
76610275Ssam 	reply(502, "%s command not implemented.", s);
76710275Ssam }
76810275Ssam 
76936304Skarels /* ARGSUSED */
77026493Sminshall yyerror(s)
77126493Sminshall 	char *s;
77210275Ssam {
77326044Sminshall 	char *cp;
77426044Sminshall 
77526044Sminshall 	cp = index(cbuf,'\n');
77626044Sminshall 	*cp = '\0';
77726044Sminshall 	reply(500, "'%s': command not understood.",cbuf);
77810275Ssam }
77910275Ssam 
78010275Ssam delete(name)
78110275Ssam 	char *name;
78210275Ssam {
78310275Ssam 	struct stat st;
78410275Ssam 
78510275Ssam 	if (stat(name, &st) < 0) {
78636304Skarels 		perror_reply(550, name);
78710275Ssam 		return;
78810275Ssam 	}
78910275Ssam 	if ((st.st_mode&S_IFMT) == S_IFDIR) {
79010275Ssam 		if (rmdir(name) < 0) {
79136304Skarels 			perror_reply(550, name);
79210275Ssam 			return;
79310275Ssam 		}
79410275Ssam 		goto done;
79510275Ssam 	}
79610275Ssam 	if (unlink(name) < 0) {
79736304Skarels 		perror_reply(550, name);
79810275Ssam 		return;
79910275Ssam 	}
80010275Ssam done:
80110275Ssam 	ack("DELE");
80210275Ssam }
80310275Ssam 
80410275Ssam cwd(path)
80510275Ssam 	char *path;
80610275Ssam {
80710275Ssam 
80810275Ssam 	if (chdir(path) < 0) {
80936304Skarels 		perror_reply(550, path);
81010275Ssam 		return;
81110275Ssam 	}
81210275Ssam 	ack("CWD");
81310275Ssam }
81410275Ssam 
81510303Ssam makedir(name)
81610275Ssam 	char *name;
81710275Ssam {
81836276Sbostic 	if (mkdir(name, 0777) < 0)
81936304Skarels 		perror_reply(550, name);
82036276Sbostic 	else
82136276Sbostic 		reply(257, "MKD command successful.");
82210275Ssam }
82310275Ssam 
82410303Ssam removedir(name)
82510275Ssam 	char *name;
82610275Ssam {
82710275Ssam 
82810275Ssam 	if (rmdir(name) < 0) {
82936304Skarels 		perror_reply(550, name);
83010275Ssam 		return;
83110275Ssam 	}
83227106Smckusick 	ack("RMD");
83310275Ssam }
83410275Ssam 
83510303Ssam pwd()
83610275Ssam {
83710303Ssam 	char path[MAXPATHLEN + 1];
83836304Skarels 	extern char *getwd();
83910275Ssam 
84036304Skarels 	if (getwd(path) == (char *)NULL) {
84127106Smckusick 		reply(550, "%s.", path);
84210275Ssam 		return;
84310275Ssam 	}
84427106Smckusick 	reply(257, "\"%s\" is current directory.", path);
84510275Ssam }
84610275Ssam 
84710275Ssam char *
84810275Ssam renamefrom(name)
84910275Ssam 	char *name;
85010275Ssam {
85110275Ssam 	struct stat st;
85210275Ssam 
85310275Ssam 	if (stat(name, &st) < 0) {
85436304Skarels 		perror_reply(550, name);
85510275Ssam 		return ((char *)0);
85610275Ssam 	}
85710303Ssam 	reply(350, "File exists, ready for destination name");
85810275Ssam 	return (name);
85910275Ssam }
86010275Ssam 
86110275Ssam renamecmd(from, to)
86210275Ssam 	char *from, *to;
86310275Ssam {
86410275Ssam 
86510275Ssam 	if (rename(from, to) < 0) {
86636304Skarels 		perror_reply(550, "rename");
86710275Ssam 		return;
86810275Ssam 	}
86910275Ssam 	ack("RNTO");
87010275Ssam }
87110275Ssam 
87210275Ssam dolog(sin)
87310275Ssam 	struct sockaddr_in *sin;
87410275Ssam {
87536304Skarels 	struct hostent *hp = gethostbyaddr((char *)&sin->sin_addr,
87610275Ssam 		sizeof (struct in_addr), AF_INET);
87736304Skarels 	time_t t, time();
87826493Sminshall 	extern char *ctime();
87910275Ssam 
88036304Skarels 	if (hp)
88126493Sminshall 		(void) strncpy(remotehost, hp->h_name, sizeof (remotehost));
88236304Skarels 	else
88326493Sminshall 		(void) strncpy(remotehost, inet_ntoa(sin->sin_addr),
88413247Ssam 		    sizeof (remotehost));
88513247Ssam 	if (!logging)
88613247Ssam 		return;
88726493Sminshall 	t = time((time_t *) 0);
88836304Skarels 	syslog(LOG_INFO, "connection from %s at %s",
88936304Skarels 	    remotehost, ctime(&t));
89010275Ssam }
89110695Ssam 
89210695Ssam /*
89313247Ssam  * Record logout in wtmp file
89413247Ssam  * and exit with supplied status.
89513247Ssam  */
89613247Ssam dologout(status)
89713247Ssam 	int status;
89813247Ssam {
89917580Ssam 	if (logged_in) {
90036304Skarels 		(void) seteuid((uid_t)0);
90135672Sbostic 		logwtmp(ttyline, "", "");
90213247Ssam 	}
90314436Ssam 	/* beware of flushing buffers after a SIGPIPE */
90414436Ssam 	_exit(status);
90513247Ssam }
90613247Ssam 
90726044Sminshall myoob()
90826044Sminshall {
90927750Sminshall 	char *cp;
91026044Sminshall 
91127750Sminshall 	/* only process if transfer occurring */
91236304Skarels 	if (!transflag)
91326044Sminshall 		return;
91427750Sminshall 	cp = tmpline;
91527750Sminshall 	if (getline(cp, 7, stdin) == NULL) {
91636304Skarels 		reply(221, "You could at least say goodbye.");
91727750Sminshall 		dologout(0);
91826044Sminshall 	}
91926044Sminshall 	upper(cp);
92026227Ssam 	if (strcmp(cp, "ABOR\r\n"))
92126044Sminshall 		return;
92226044Sminshall 	tmpline[0] = '\0';
92326044Sminshall 	reply(426,"Transfer aborted. Data connection closed.");
92426044Sminshall 	reply(226,"Abort successful");
92526044Sminshall 	longjmp(urgcatch, 1);
92626044Sminshall }
92726044Sminshall 
92827106Smckusick /*
92927106Smckusick  * Note: The 530 reply codes could be 4xx codes, except nothing is
93027106Smckusick  * given in the state tables except 421 which implies an exit.  (RFC959)
93127106Smckusick  */
93226044Sminshall passive()
93326044Sminshall {
93426044Sminshall 	int len;
93526044Sminshall 	struct sockaddr_in tmp;
93626044Sminshall 	register char *p, *a;
93726044Sminshall 
93826044Sminshall 	pdata = socket(AF_INET, SOCK_STREAM, 0);
93926044Sminshall 	if (pdata < 0) {
94027106Smckusick 		reply(530, "Can't open passive connection");
94126044Sminshall 		return;
94226044Sminshall 	}
94326044Sminshall 	tmp = ctrl_addr;
94426044Sminshall 	tmp.sin_port = 0;
94536304Skarels 	(void) seteuid((uid_t)0);
94626493Sminshall 	if (bind(pdata, (struct sockaddr *) &tmp, sizeof(tmp)) < 0) {
94736304Skarels 		(void) seteuid((uid_t)pw->pw_uid);
94826044Sminshall 		(void) close(pdata);
94926044Sminshall 		pdata = -1;
95027106Smckusick 		reply(530, "Can't open passive connection");
95126044Sminshall 		return;
95226044Sminshall 	}
95336304Skarels 	(void) seteuid((uid_t)pw->pw_uid);
95426044Sminshall 	len = sizeof(tmp);
95536304Skarels 	if (getsockname(pdata, (struct sockaddr *) &tmp, &len) < 0) {
95626044Sminshall 		(void) close(pdata);
95726044Sminshall 		pdata = -1;
95827106Smckusick 		reply(530, "Can't open passive connection");
95926044Sminshall 		return;
96026044Sminshall 	}
96126044Sminshall 	if (listen(pdata, 1) < 0) {
96226044Sminshall 		(void) close(pdata);
96326044Sminshall 		pdata = -1;
96427106Smckusick 		reply(530, "Can't open passive connection");
96526044Sminshall 		return;
96626044Sminshall 	}
96726044Sminshall 	a = (char *) &tmp.sin_addr;
96826044Sminshall 	p = (char *) &tmp.sin_port;
96926044Sminshall 
97026044Sminshall #define UC(b) (((int) b) & 0xff)
97126044Sminshall 
97226044Sminshall 	reply(227, "Entering Passive Mode (%d,%d,%d,%d,%d,%d)", UC(a[0]),
97326044Sminshall 		UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1]));
97426044Sminshall }
97526044Sminshall 
97636304Skarels /*
97736304Skarels  * Generate unique name for file with basename "local".
97836304Skarels  * The file named "local" is already known to exist.
97936304Skarels  * Generates failure reply on error.
98036304Skarels  */
98126044Sminshall char *
98226044Sminshall gunique(local)
98326044Sminshall 	char *local;
98426044Sminshall {
98526044Sminshall 	static char new[MAXPATHLEN];
98636304Skarels 	struct stat st;
98726044Sminshall 	char *cp = rindex(local, '/');
98826044Sminshall 	int d, count=0;
98926044Sminshall 
99036304Skarels 	if (cp)
99126044Sminshall 		*cp = '\0';
99236304Skarels 	d = stat(cp ? local : ".", &st);
99336304Skarels 	if (cp)
99426044Sminshall 		*cp = '/';
99526044Sminshall 	if (d < 0) {
99636304Skarels 		perror_reply(553, local);
99726044Sminshall 		return((char *) 0);
99826044Sminshall 	}
99926044Sminshall 	(void) strcpy(new, local);
100026044Sminshall 	cp = new + strlen(new);
100126044Sminshall 	*cp++ = '.';
100236304Skarels 	for (count = 1; count < 100; count++) {
100336304Skarels 		(void) sprintf(cp, "%d", count);
100436304Skarels 		if (stat(new, &st) < 0)
100536304Skarels 			return(new);
100626044Sminshall 	}
100736304Skarels 	reply(452, "Unique file name cannot be created.");
100836304Skarels 	return((char *) 0);
100926044Sminshall }
101036304Skarels 
101136304Skarels /*
101236304Skarels  * Format and send reply containing system error number.
101336304Skarels  */
101436304Skarels perror_reply(code, string)
101536304Skarels 	int code;
101636304Skarels 	char *string;
101736304Skarels {
101836304Skarels 
101936304Skarels 	if (errno < sys_nerr)
102036304Skarels 		reply(code, "%s: %s.", string, sys_errlist[errno]);
102136304Skarels 	else
102236304Skarels 		reply(code, "%s: unknown error %d.", string, errno);
102336304Skarels }
1024