Home | History | Annotate | Line # | Download | only in ftpd
ftpd.c revision 1.4
      1 /*
      2  * Copyright (c) 1985, 1988, 1990 Regents of the University of California.
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Berkeley and its contributors.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  */
     33 
     34 #ifndef lint
     35 char copyright[] =
     36 "@(#) Copyright (c) 1985, 1988, 1990 Regents of the University of California.\n\
     37  All rights reserved.\n";
     38 #endif /* not lint */
     39 
     40 #ifndef lint
     41 /*static char sccsid[] = "from: @(#)ftpd.c	5.40 (Berkeley) 7/2/91";*/
     42 static char rcsid[] = "$Id: ftpd.c,v 1.4 1994/03/30 02:50:29 cgd Exp $";
     43 #endif /* not lint */
     44 
     45 /*
     46  * FTP server.
     47  */
     48 #include <sys/param.h>
     49 #include <sys/stat.h>
     50 #include <sys/ioctl.h>
     51 #include <sys/socket.h>
     52 #include <sys/wait.h>
     53 
     54 #include <netinet/in.h>
     55 #include <netinet/in_systm.h>
     56 #include <netinet/ip.h>
     57 
     58 #define	FTP_NAMES
     59 #include <arpa/ftp.h>
     60 #include <arpa/inet.h>
     61 #include <arpa/telnet.h>
     62 
     63 #include <signal.h>
     64 #include <dirent.h>
     65 #include <fcntl.h>
     66 #include <time.h>
     67 #include <pwd.h>
     68 #include <setjmp.h>
     69 #include <netdb.h>
     70 #include <errno.h>
     71 #include <syslog.h>
     72 #include <varargs.h>
     73 #include <unistd.h>
     74 #include <stdio.h>
     75 #include <ctype.h>
     76 #include <stdlib.h>
     77 #include <string.h>
     78 #include "pathnames.h"
     79 
     80 /*
     81  * File containing login names
     82  * NOT to be used on this machine.
     83  * Commonly used to disallow uucp.
     84  */
     85 extern	int errno;
     86 extern	char *crypt();
     87 extern	char version[];
     88 extern	char *home;		/* pointer to home directory for glob */
     89 extern	FILE *ftpd_popen(), *fopen(), *freopen();
     90 extern	int  ftpd_pclose(), fclose();
     91 extern	char *getline();
     92 extern	char cbuf[];
     93 extern	off_t restart_point;
     94 
     95 struct	sockaddr_in ctrl_addr;
     96 struct	sockaddr_in data_source;
     97 struct	sockaddr_in data_dest;
     98 struct	sockaddr_in his_addr;
     99 struct	sockaddr_in pasv_addr;
    100 
    101 int	data;
    102 jmp_buf	errcatch, urgcatch;
    103 int	logged_in;
    104 struct	passwd *pw;
    105 int	debug;
    106 int	timeout = 900;    /* timeout after 15 minutes of inactivity */
    107 int	maxtimeout = 7200;/* don't allow idle time to be set beyond 2 hours */
    108 int	logging;
    109 int	guest;
    110 int	type;
    111 int	form;
    112 int	stru;			/* avoid C keyword */
    113 int	mode;
    114 int	usedefault = 1;		/* for data transfers */
    115 int	pdata = -1;		/* for passive mode */
    116 int	transflag;
    117 off_t	file_size;
    118 off_t	byte_count;
    119 #if !defined(CMASK) || CMASK == 0
    120 #undef CMASK
    121 #define CMASK 027
    122 #endif
    123 int	defumask = CMASK;		/* default umask value */
    124 char	tmpline[7];
    125 char	hostname[MAXHOSTNAMELEN];
    126 char	remotehost[MAXHOSTNAMELEN];
    127 
    128 #if defined(KERBEROS)
    129 int notickets = 1;
    130 char *krbtkfile_env = NULL;
    131 #endif
    132 
    133 /*
    134  * Timeout intervals for retrying connections
    135  * to hosts that don't accept PORT cmds.  This
    136  * is a kludge, but given the problems with TCP...
    137  */
    138 #define	SWAITMAX	90	/* wait at most 90 seconds */
    139 #define	SWAITINT	5	/* interval between retries */
    140 
    141 int	swaitmax = SWAITMAX;
    142 int	swaitint = SWAITINT;
    143 
    144 void	lostconn(), myoob();
    145 FILE	*getdatasock(), *dataconn();
    146 
    147 #ifdef SETPROCTITLE
    148 char	**Argv = NULL;		/* pointer to argument vector */
    149 char	*LastArgv = NULL;	/* end of argv */
    150 char	proctitle[BUFSIZ];	/* initial part of title */
    151 #endif /* SETPROCTITLE */
    152 
    153 main(argc, argv, envp)
    154 	int argc;
    155 	char *argv[];
    156 	char **envp;
    157 {
    158 	int addrlen, on = 1, tos;
    159 	char *cp;
    160 
    161 	/*
    162 	 * LOG_NDELAY sets up the logging connection immediately,
    163 	 * necessary for anonymous ftp's that chroot and can't do it later.
    164 	 */
    165 	openlog("ftpd", LOG_PID | LOG_NDELAY, LOG_DAEMON);
    166 	addrlen = sizeof (his_addr);
    167 	if (getpeername(0, (struct sockaddr *)&his_addr, &addrlen) < 0) {
    168 		syslog(LOG_ERR, "getpeername (%s): %m",argv[0]);
    169 		exit(1);
    170 	}
    171 	addrlen = sizeof (ctrl_addr);
    172 	if (getsockname(0, (struct sockaddr *)&ctrl_addr, &addrlen) < 0) {
    173 		syslog(LOG_ERR, "getsockname (%s): %m",argv[0]);
    174 		exit(1);
    175 	}
    176 #ifdef IP_TOS
    177 	tos = IPTOS_LOWDELAY;
    178 	if (setsockopt(0, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(int)) < 0)
    179 		syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
    180 #endif
    181 	data_source.sin_port = htons(ntohs(ctrl_addr.sin_port) - 1);
    182 	debug = 0;
    183 #ifdef SETPROCTITLE
    184 	/*
    185 	 *  Save start and extent of argv for setproctitle.
    186 	 */
    187 	Argv = argv;
    188 	while (*envp)
    189 		envp++;
    190 	LastArgv = envp[-1] + strlen(envp[-1]);
    191 #endif /* SETPROCTITLE */
    192 
    193 	argc--, argv++;
    194 	while (argc > 0 && *argv[0] == '-') {
    195 		for (cp = &argv[0][1]; *cp; cp++) switch (*cp) {
    196 
    197 		case 'v':
    198 			debug = 1;
    199 			break;
    200 
    201 		case 'd':
    202 			debug = 1;
    203 			break;
    204 
    205 		case 'l':
    206 			logging = 1;
    207 			break;
    208 
    209 		case 't':
    210 			timeout = atoi(++cp);
    211 			if (maxtimeout < timeout)
    212 				maxtimeout = timeout;
    213 			goto nextopt;
    214 
    215 		case 'T':
    216 			maxtimeout = atoi(++cp);
    217 			if (timeout > maxtimeout)
    218 				timeout = maxtimeout;
    219 			goto nextopt;
    220 
    221 		case 'u':
    222 		    {
    223 			int val = 0;
    224 
    225 			while (*++cp && *cp >= '0' && *cp <= '9')
    226 				val = val*8 + *cp - '0';
    227 			if (*cp)
    228 				fprintf(stderr, "ftpd: Bad value for -u\n");
    229 			else
    230 				defumask = val;
    231 			goto nextopt;
    232 		    }
    233 
    234 		default:
    235 			fprintf(stderr, "ftpd: Unknown flag -%c ignored.\n",
    236 			     *cp);
    237 			break;
    238 		}
    239 nextopt:
    240 		argc--, argv++;
    241 	}
    242 	(void) freopen(_PATH_DEVNULL, "w", stderr);
    243 	(void) signal(SIGPIPE, lostconn);
    244 	(void) signal(SIGCHLD, SIG_IGN);
    245 	if ((int)signal(SIGURG, myoob) < 0)
    246 		syslog(LOG_ERR, "signal: %m");
    247 
    248 	/* Try to handle urgent data inline */
    249 #ifdef SO_OOBINLINE
    250 	if (setsockopt(0, SOL_SOCKET, SO_OOBINLINE, (char *)&on, sizeof(on)) < 0)
    251 		syslog(LOG_ERR, "setsockopt: %m");
    252 #endif
    253 
    254 #ifdef	F_SETOWN
    255 	if (fcntl(fileno(stdin), F_SETOWN, getpid()) == -1)
    256 		syslog(LOG_ERR, "fcntl F_SETOWN: %m");
    257 #endif
    258 	dolog(&his_addr);
    259 	/*
    260 	 * Set up default state
    261 	 */
    262 	data = -1;
    263 	type = TYPE_A;
    264 	form = FORM_N;
    265 	stru = STRU_F;
    266 	mode = MODE_S;
    267 	tmpline[0] = '\0';
    268 	(void) gethostname(hostname, sizeof (hostname));
    269 	reply(220, "%s FTP server (%s) ready.", hostname, version);
    270 	(void) setjmp(errcatch);
    271 	for (;;)
    272 		(void) yyparse();
    273 	/* NOTREACHED */
    274 }
    275 
    276 void
    277 lostconn()
    278 {
    279 	if (debug)
    280 		syslog(LOG_DEBUG, "lost connection");
    281 	dologout(-1);
    282 }
    283 
    284 static char ttyline[20];
    285 
    286 /*
    287  * Helper function for sgetpwnam().
    288  */
    289 char *
    290 sgetsave(s)
    291 	char *s;
    292 {
    293 	char *new = malloc((unsigned) strlen(s) + 1);
    294 
    295 	if (new == NULL) {
    296 		perror_reply(421, "Local resource failure: malloc");
    297 		dologout(1);
    298 		/* NOTREACHED */
    299 	}
    300 	(void) strcpy(new, s);
    301 	return (new);
    302 }
    303 
    304 /*
    305  * Save the result of a getpwnam.  Used for USER command, since
    306  * the data returned must not be clobbered by any other command
    307  * (e.g., globbing).
    308  */
    309 struct passwd *
    310 sgetpwnam(name)
    311 	char *name;
    312 {
    313 	static struct passwd save;
    314 	register struct passwd *p;
    315 	char *sgetsave();
    316 
    317 	if ((p = getpwnam(name)) == NULL)
    318 		return (p);
    319 	if (save.pw_name) {
    320 		free(save.pw_name);
    321 		free(save.pw_passwd);
    322 		free(save.pw_gecos);
    323 		free(save.pw_dir);
    324 		free(save.pw_shell);
    325 	}
    326 	save = *p;
    327 	save.pw_name = sgetsave(p->pw_name);
    328 	save.pw_passwd = sgetsave(p->pw_passwd);
    329 	save.pw_gecos = sgetsave(p->pw_gecos);
    330 	save.pw_dir = sgetsave(p->pw_dir);
    331 	save.pw_shell = sgetsave(p->pw_shell);
    332 	return (&save);
    333 }
    334 
    335 int login_attempts;		/* number of failed login attempts */
    336 int askpasswd;			/* had user command, ask for passwd */
    337 
    338 /*
    339  * USER command.
    340  * Sets global passwd pointer pw if named account exists and is acceptable;
    341  * sets askpasswd if a PASS command is expected.  If logged in previously,
    342  * need to reset state.  If name is "ftp" or "anonymous", the name is not in
    343  * _PATH_FTPUSERS, and ftp account exists, set guest and pw, then just return.
    344  * If account doesn't exist, ask for passwd anyway.  Otherwise, check user
    345  * requesting login privileges.  Disallow anyone who does not have a standard
    346  * shell as returned by getusershell().  Disallow anyone mentioned in the file
    347  * _PATH_FTPUSERS to allow people such as root and uucp to be avoided.
    348  */
    349 user(name)
    350 	char *name;
    351 {
    352 	register char *cp;
    353 	char *shell;
    354 	char *getusershell();
    355 
    356 	if (logged_in) {
    357 		if (guest) {
    358 			reply(530, "Can't change user from guest login.");
    359 			return;
    360 		}
    361 		end_login();
    362 	}
    363 
    364 	guest = 0;
    365 	if (strcmp(name, "ftp") == 0 || strcmp(name, "anonymous") == 0) {
    366 		if (checkuser("ftp") || checkuser("anonymous"))
    367 			reply(530, "User %s access denied.", name);
    368 		else if ((pw = sgetpwnam("ftp")) != NULL) {
    369 			guest = 1;
    370 			askpasswd = 1;
    371 			reply(331, "Guest login ok, send ident as password.");
    372 		} else
    373 			reply(530, "User %s unknown.", name);
    374 		return;
    375 	}
    376 	if (pw = sgetpwnam(name)) {
    377 		if ((shell = pw->pw_shell) == NULL || *shell == 0)
    378 			shell = _PATH_BSHELL;
    379 		while ((cp = getusershell()) != NULL)
    380 			if (strcmp(cp, shell) == 0)
    381 				break;
    382 		endusershell();
    383 		if (cp == NULL || checkuser(name)) {
    384 			reply(530, "User %s access denied.", name);
    385 			if (logging)
    386 				syslog(LOG_NOTICE,
    387 				    "FTP LOGIN REFUSED FROM %s, %s",
    388 				    remotehost, name);
    389 			pw = (struct passwd *) NULL;
    390 			return;
    391 		}
    392 	}
    393 	reply(331, "Password required for %s.", name);
    394 	askpasswd = 1;
    395 	/*
    396 	 * Delay before reading passwd after first failed
    397 	 * attempt to slow down passwd-guessing programs.
    398 	 */
    399 	if (login_attempts)
    400 		sleep((unsigned) login_attempts);
    401 }
    402 
    403 /*
    404  * Check if a user is in the file _PATH_FTPUSERS
    405  */
    406 checkuser(name)
    407 	char *name;
    408 {
    409 	register FILE *fd;
    410 	register char *p;
    411 	char line[BUFSIZ];
    412 
    413 	if ((fd = fopen(_PATH_FTPUSERS, "r")) != NULL) {
    414 		while (fgets(line, sizeof(line), fd) != NULL)
    415 			if ((p = index(line, '\n')) != NULL) {
    416 				*p = '\0';
    417 				if (line[0] == '#')
    418 					continue;
    419 				if (strcmp(line, name) == 0)
    420 					return (1);
    421 			}
    422 		(void) fclose(fd);
    423 	}
    424 	return (0);
    425 }
    426 
    427 /*
    428  * Terminate login as previous user, if any, resetting state;
    429  * used when USER command is given or login fails.
    430  */
    431 end_login()
    432 {
    433 
    434 	(void) seteuid((uid_t)0);
    435 	if (logged_in)
    436 		logwtmp(ttyline, "", "");
    437 	pw = NULL;
    438 	logged_in = 0;
    439 	guest = 0;
    440 }
    441 
    442 pass(passwd)
    443 	char *passwd;
    444 {
    445 	int rval;
    446 
    447 	if (logged_in || askpasswd == 0) {
    448 		reply(503, "Login with USER first.");
    449 		return;
    450 	}
    451 	askpasswd = 0;
    452 	if (!guest) {		/* "ftp" is only account allowed no password */
    453 		if (pw == NULL)
    454 			rval = 1;  /* failure below */
    455 		else
    456 #if defined(KERBEROS)
    457 			rval = klogin(pw, "", hostname, passwd);
    458 			if (rval == 1)
    459 #endif
    460 				/* the strcmp does not catch null passwords! */
    461 				if (pw == NULL || *pw->pw_passwd == '\0')
    462 					rval = 1;   /* failure */
    463 		else
    464 					rval = strcmp(crypt(passwd, (pw ? pw->pw_passwd : "xx")),
    465 						      pw->pw_passwd);
    466 
    467 		/*
    468 		 * If rval == 1, the user failed the authentication check
    469 		 * above.  If rval == 0, either Kerberos or local authentication
    470 		 * succeeded.
    471 		 */
    472 		if (rval) {
    473 			reply(530, "Login incorrect.");
    474 			pw = NULL;
    475 			if (login_attempts++ >= 5) {
    476 				syslog(LOG_NOTICE,
    477 				    "repeated login failures from %s",
    478 				    remotehost);
    479 				exit(0);
    480 			}
    481 			return;
    482 		}
    483 	}
    484 	login_attempts = 0;		/* this time successful */
    485 	(void) setegid((gid_t)pw->pw_gid);
    486 	(void) initgroups(pw->pw_name, pw->pw_gid);
    487 
    488 	/* open wtmp before chroot */
    489 	(void)sprintf(ttyline, "ftp%d", getpid());
    490 	logwtmp(ttyline, pw->pw_name, remotehost);
    491 	logged_in = 1;
    492 
    493 	if (guest) {
    494 		/*
    495 		 * We MUST do a chdir() after the chroot. Otherwise
    496 		 * the old current directory will be accessible as "."
    497 		 * outside the new root!
    498 		 */
    499 		if (chroot(pw->pw_dir) < 0 || chdir("/") < 0) {
    500 			reply(550, "Can't set guest privileges.");
    501 			goto bad;
    502 		}
    503 	} else if (chdir(pw->pw_dir) < 0) {
    504 		if (chdir("/") < 0) {
    505 			reply(530, "User %s: can't change directory to %s.",
    506 			    pw->pw_name, pw->pw_dir);
    507 			goto bad;
    508 		} else
    509 			lreply(230, "No directory! Logging in with home=/");
    510 	}
    511 	if (seteuid((uid_t)pw->pw_uid) < 0) {
    512 		reply(550, "Can't set uid.");
    513 		goto bad;
    514 	}
    515 	if (guest) {
    516 		reply(230, "Guest login ok, access restrictions apply.");
    517 #ifdef SETPROCTITLE
    518 		sprintf(proctitle, "%s: anonymous/%.*s", remotehost,
    519 		    sizeof(proctitle) - sizeof(remotehost) -
    520 		    sizeof(": anonymous/"), passwd);
    521 		setproctitle(proctitle);
    522 #endif /* SETPROCTITLE */
    523 		if (logging)
    524 			syslog(LOG_INFO, "ANONYMOUS FTP LOGIN FROM %s, %s",
    525 			    remotehost, passwd);
    526 	} else {
    527 		reply(230, "User %s logged in.", pw->pw_name);
    528 #ifdef SETPROCTITLE
    529 		sprintf(proctitle, "%s: %s", remotehost, pw->pw_name);
    530 		setproctitle(proctitle);
    531 #endif /* SETPROCTITLE */
    532 		if (logging)
    533 			syslog(LOG_INFO, "FTP LOGIN FROM %s, %s",
    534 			    remotehost, pw->pw_name);
    535 	}
    536 	home = pw->pw_dir;		/* home dir for globbing */
    537 	(void) umask(defumask);
    538 	return;
    539 bad:
    540 	/* Forget all about it... */
    541 	end_login();
    542 }
    543 
    544 retrieve(cmd, name)
    545 	char *cmd, *name;
    546 {
    547 	FILE *fin, *dout;
    548 	struct stat st;
    549 	int (*closefunc)();
    550 
    551 	if (cmd == 0) {
    552 		fin = fopen(name, "r"), closefunc = fclose;
    553 		st.st_size = 0;
    554 	} else {
    555 		char line[BUFSIZ];
    556 
    557 		(void) sprintf(line, cmd, name), name = line;
    558 		fin = ftpd_popen(line, "r"), closefunc = ftpd_pclose;
    559 		st.st_size = -1;
    560 		st.st_blksize = BUFSIZ;
    561 	}
    562 	if (fin == NULL) {
    563 		if (errno != 0)
    564 			perror_reply(550, name);
    565 		return;
    566 	}
    567 	if (cmd == 0 &&
    568 	    (fstat(fileno(fin), &st) < 0 || (st.st_mode&S_IFMT) != S_IFREG)) {
    569 		reply(550, "%s: not a plain file.", name);
    570 		goto done;
    571 	}
    572 	if (restart_point) {
    573 		if (type == TYPE_A) {
    574 			register int i, n, c;
    575 
    576 			n = restart_point;
    577 			i = 0;
    578 			while (i++ < n) {
    579 				if ((c=getc(fin)) == EOF) {
    580 					perror_reply(550, name);
    581 					goto done;
    582 				}
    583 				if (c == '\n')
    584 					i++;
    585 			}
    586 		} else if (lseek(fileno(fin), restart_point, L_SET) < 0) {
    587 			perror_reply(550, name);
    588 			goto done;
    589 		}
    590 	}
    591 	dout = dataconn(name, st.st_size, "w");
    592 	if (dout == NULL)
    593 		goto done;
    594 	send_data(fin, dout, st.st_blksize);
    595 	(void) fclose(dout);
    596 	data = -1;
    597 	pdata = -1;
    598 done:
    599 	(*closefunc)(fin);
    600 }
    601 
    602 store(name, mode, unique)
    603 	char *name, *mode;
    604 	int unique;
    605 {
    606 	FILE *fout, *din;
    607 	struct stat st;
    608 	int (*closefunc)();
    609 	char *gunique();
    610 
    611 	if (unique && stat(name, &st) == 0 &&
    612 	    (name = gunique(name)) == NULL)
    613 		return;
    614 
    615 	if (restart_point)
    616 		mode = "r+w";
    617 	fout = fopen(name, mode);
    618 	closefunc = fclose;
    619 	if (fout == NULL) {
    620 		perror_reply(553, name);
    621 		return;
    622 	}
    623 	if (restart_point) {
    624 		if (type == TYPE_A) {
    625 			register int i, n, c;
    626 
    627 			n = restart_point;
    628 			i = 0;
    629 			while (i++ < n) {
    630 				if ((c=getc(fout)) == EOF) {
    631 					perror_reply(550, name);
    632 					goto done;
    633 				}
    634 				if (c == '\n')
    635 					i++;
    636 			}
    637 			/*
    638 			 * We must do this seek to "current" position
    639 			 * because we are changing from reading to
    640 			 * writing.
    641 			 */
    642 			if (fseek(fout, 0L, L_INCR) < 0) {
    643 				perror_reply(550, name);
    644 				goto done;
    645 			}
    646 		} else if (lseek(fileno(fout), restart_point, L_SET) < 0) {
    647 			perror_reply(550, name);
    648 			goto done;
    649 		}
    650 	}
    651 	din = dataconn(name, (off_t)-1, "r");
    652 	if (din == NULL)
    653 		goto done;
    654 	if (receive_data(din, fout) == 0) {
    655 		if (unique)
    656 			reply(226, "Transfer complete (unique file name:%s).",
    657 			    name);
    658 		else
    659 			reply(226, "Transfer complete.");
    660 	}
    661 	(void) fclose(din);
    662 	data = -1;
    663 	pdata = -1;
    664 done:
    665 	(*closefunc)(fout);
    666 }
    667 
    668 FILE *
    669 getdatasock(mode)
    670 	char *mode;
    671 {
    672 	int s, on = 1, tries;
    673 
    674 	if (data >= 0)
    675 		return (fdopen(data, mode));
    676 	(void) seteuid((uid_t)0);
    677 	s = socket(AF_INET, SOCK_STREAM, 0);
    678 	if (s < 0)
    679 		goto bad;
    680 	if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
    681 	    (char *) &on, sizeof (on)) < 0)
    682 		goto bad;
    683 	/* anchor socket to avoid multi-homing problems */
    684 	data_source.sin_family = AF_INET;
    685 	data_source.sin_addr = ctrl_addr.sin_addr;
    686 	for (tries = 1; ; tries++) {
    687 		if (bind(s, (struct sockaddr *)&data_source,
    688 		    sizeof (data_source)) >= 0)
    689 			break;
    690 		if (errno != EADDRINUSE || tries > 10)
    691 			goto bad;
    692 		sleep(tries);
    693 	}
    694 	(void) seteuid((uid_t)pw->pw_uid);
    695 #ifdef IP_TOS
    696 	on = IPTOS_THROUGHPUT;
    697 	if (setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0)
    698 		syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
    699 #endif
    700 	return (fdopen(s, mode));
    701 bad:
    702 	(void) seteuid((uid_t)pw->pw_uid);
    703 	(void) close(s);
    704 	return (NULL);
    705 }
    706 
    707 FILE *
    708 dataconn(name, size, mode)
    709 	char *name;
    710 	off_t size;
    711 	char *mode;
    712 {
    713 	char sizebuf[32];
    714 	FILE *file;
    715 	int retry = 0, tos;
    716 
    717 	file_size = size;
    718 	byte_count = 0;
    719 	if (size != (off_t) -1)
    720 		(void) sprintf (sizebuf, " (%ld bytes)", size);
    721 	else
    722 		(void) strcpy(sizebuf, "");
    723 	if (pdata >= 0) {
    724 		struct sockaddr_in from;
    725 		int s, fromlen = sizeof(from);
    726 
    727 		s = accept(pdata, (struct sockaddr *)&from, &fromlen);
    728 		if (s < 0) {
    729 			reply(425, "Can't open data connection.");
    730 			(void) close(pdata);
    731 			pdata = -1;
    732 			return(NULL);
    733 		}
    734 		(void) close(pdata);
    735 		pdata = s;
    736 #ifdef IP_TOS
    737 		tos = IPTOS_LOWDELAY;
    738 		(void) setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&tos,
    739 		    sizeof(int));
    740 #endif
    741 		reply(150, "Opening %s mode data connection for %s%s.",
    742 		     type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf);
    743 		return(fdopen(pdata, mode));
    744 	}
    745 	if (data >= 0) {
    746 		reply(125, "Using existing data connection for %s%s.",
    747 		    name, sizebuf);
    748 		usedefault = 1;
    749 		return (fdopen(data, mode));
    750 	}
    751 	if (usedefault)
    752 		data_dest = his_addr;
    753 	usedefault = 1;
    754 	file = getdatasock(mode);
    755 	if (file == NULL) {
    756 		reply(425, "Can't create data socket (%s,%d): %s.",
    757 		    inet_ntoa(data_source.sin_addr),
    758 		    ntohs(data_source.sin_port), strerror(errno));
    759 		return (NULL);
    760 	}
    761 	data = fileno(file);
    762 	while (connect(data, (struct sockaddr *)&data_dest,
    763 	    sizeof (data_dest)) < 0) {
    764 		if (errno == EADDRINUSE && retry < swaitmax) {
    765 			sleep((unsigned) swaitint);
    766 			retry += swaitint;
    767 			continue;
    768 		}
    769 		perror_reply(425, "Can't build data connection");
    770 		(void) fclose(file);
    771 		data = -1;
    772 		return (NULL);
    773 	}
    774 	reply(150, "Opening %s mode data connection for %s%s.",
    775 	     type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf);
    776 	return (file);
    777 }
    778 
    779 /*
    780  * Tranfer the contents of "instr" to
    781  * "outstr" peer using the appropriate
    782  * encapsulation of the data subject
    783  * to Mode, Structure, and Type.
    784  *
    785  * NB: Form isn't handled.
    786  */
    787 send_data(instr, outstr, blksize)
    788 	FILE *instr, *outstr;
    789 	off_t blksize;
    790 {
    791 	register int c, cnt;
    792 	register char *buf;
    793 	int netfd, filefd;
    794 
    795 	transflag++;
    796 	if (setjmp(urgcatch)) {
    797 		transflag = 0;
    798 		return;
    799 	}
    800 	switch (type) {
    801 
    802 	case TYPE_A:
    803 		while ((c = getc(instr)) != EOF) {
    804 			byte_count++;
    805 			if (c == '\n') {
    806 				if (ferror(outstr))
    807 					goto data_err;
    808 				(void) putc('\r', outstr);
    809 			}
    810 			(void) putc(c, outstr);
    811 		}
    812 		fflush(outstr);
    813 		transflag = 0;
    814 		if (ferror(instr))
    815 			goto file_err;
    816 		if (ferror(outstr))
    817 			goto data_err;
    818 		reply(226, "Transfer complete.");
    819 		return;
    820 
    821 	case TYPE_I:
    822 	case TYPE_L:
    823 		if ((buf = malloc((u_int)blksize)) == NULL) {
    824 			transflag = 0;
    825 			perror_reply(451, "Local resource failure: malloc");
    826 			return;
    827 		}
    828 		netfd = fileno(outstr);
    829 		filefd = fileno(instr);
    830 		while ((cnt = read(filefd, buf, (u_int)blksize)) > 0 &&
    831 		    write(netfd, buf, cnt) == cnt)
    832 			byte_count += cnt;
    833 		transflag = 0;
    834 		(void)free(buf);
    835 		if (cnt != 0) {
    836 			if (cnt < 0)
    837 				goto file_err;
    838 			goto data_err;
    839 		}
    840 		reply(226, "Transfer complete.");
    841 		return;
    842 	default:
    843 		transflag = 0;
    844 		reply(550, "Unimplemented TYPE %d in send_data", type);
    845 		return;
    846 	}
    847 
    848 data_err:
    849 	transflag = 0;
    850 	perror_reply(426, "Data connection");
    851 	return;
    852 
    853 file_err:
    854 	transflag = 0;
    855 	perror_reply(551, "Error on input file");
    856 }
    857 
    858 /*
    859  * Transfer data from peer to
    860  * "outstr" using the appropriate
    861  * encapulation of the data subject
    862  * to Mode, Structure, and Type.
    863  *
    864  * N.B.: Form isn't handled.
    865  */
    866 receive_data(instr, outstr)
    867 	FILE *instr, *outstr;
    868 {
    869 	register int c;
    870 	int cnt, bare_lfs = 0;
    871 	char buf[BUFSIZ];
    872 
    873 	transflag++;
    874 	if (setjmp(urgcatch)) {
    875 		transflag = 0;
    876 		return (-1);
    877 	}
    878 	switch (type) {
    879 
    880 	case TYPE_I:
    881 	case TYPE_L:
    882 		while ((cnt = read(fileno(instr), buf, sizeof buf)) > 0) {
    883 			if (write(fileno(outstr), buf, cnt) != cnt)
    884 				goto file_err;
    885 			byte_count += cnt;
    886 		}
    887 		if (cnt < 0)
    888 			goto data_err;
    889 		transflag = 0;
    890 		return (0);
    891 
    892 	case TYPE_E:
    893 		reply(553, "TYPE E not implemented.");
    894 		transflag = 0;
    895 		return (-1);
    896 
    897 	case TYPE_A:
    898 		while ((c = getc(instr)) != EOF) {
    899 			byte_count++;
    900 			if (c == '\n')
    901 				bare_lfs++;
    902 			while (c == '\r') {
    903 				if (ferror(outstr))
    904 					goto data_err;
    905 				if ((c = getc(instr)) != '\n') {
    906 					(void) putc ('\r', outstr);
    907 					if (c == '\0' || c == EOF)
    908 						goto contin2;
    909 				}
    910 			}
    911 			(void) putc(c, outstr);
    912 	contin2:	;
    913 		}
    914 		fflush(outstr);
    915 		if (ferror(instr))
    916 			goto data_err;
    917 		if (ferror(outstr))
    918 			goto file_err;
    919 		transflag = 0;
    920 		if (bare_lfs) {
    921 			lreply(230, "WARNING! %d bare linefeeds received in ASCII mode", bare_lfs);
    922 			printf("   File may not have transferred correctly.\r\n");
    923 		}
    924 		return (0);
    925 	default:
    926 		reply(550, "Unimplemented TYPE %d in receive_data", type);
    927 		transflag = 0;
    928 		return (-1);
    929 	}
    930 
    931 data_err:
    932 	transflag = 0;
    933 	perror_reply(426, "Data Connection");
    934 	return (-1);
    935 
    936 file_err:
    937 	transflag = 0;
    938 	perror_reply(452, "Error writing file");
    939 	return (-1);
    940 }
    941 
    942 statfilecmd(filename)
    943 	char *filename;
    944 {
    945 	char line[BUFSIZ];
    946 	FILE *fin;
    947 	int c;
    948 
    949 	(void) sprintf(line, "/bin/ls -lgA %s", filename);
    950 	fin = ftpd_popen(line, "r");
    951 	lreply(211, "status of %s:", filename);
    952 	while ((c = getc(fin)) != EOF) {
    953 		if (c == '\n') {
    954 			if (ferror(stdout)){
    955 				perror_reply(421, "control connection");
    956 				(void) ftpd_pclose(fin);
    957 				dologout(1);
    958 				/* NOTREACHED */
    959 			}
    960 			if (ferror(fin)) {
    961 				perror_reply(551, filename);
    962 				(void) ftpd_pclose(fin);
    963 				return;
    964 			}
    965 			(void) putc('\r', stdout);
    966 		}
    967 		(void) putc(c, stdout);
    968 	}
    969 	(void) ftpd_pclose(fin);
    970 	reply(211, "End of Status");
    971 }
    972 
    973 statcmd()
    974 {
    975 	struct sockaddr_in *sin;
    976 	u_char *a, *p;
    977 
    978 	lreply(211, "%s FTP server status:", hostname, version);
    979 	printf("     %s\r\n", version);
    980 	printf("     Connected to %s", remotehost);
    981 	if (!isdigit(remotehost[0]))
    982 		printf(" (%s)", inet_ntoa(his_addr.sin_addr));
    983 	printf("\r\n");
    984 	if (logged_in) {
    985 		if (guest)
    986 			printf("     Logged in anonymously\r\n");
    987 		else
    988 			printf("     Logged in as %s\r\n", pw->pw_name);
    989 	} else if (askpasswd)
    990 		printf("     Waiting for password\r\n");
    991 	else
    992 		printf("     Waiting for user name\r\n");
    993 	printf("     TYPE: %s", typenames[type]);
    994 	if (type == TYPE_A || type == TYPE_E)
    995 		printf(", FORM: %s", formnames[form]);
    996 	if (type == TYPE_L)
    997 #if NBBY == 8
    998 		printf(" %d", NBBY);
    999 #else
   1000 		printf(" %d", bytesize);	/* need definition! */
   1001 #endif
   1002 	printf("; STRUcture: %s; transfer MODE: %s\r\n",
   1003 	    strunames[stru], modenames[mode]);
   1004 	if (data != -1)
   1005 		printf("     Data connection open\r\n");
   1006 	else if (pdata != -1) {
   1007 		printf("     in Passive mode");
   1008 		sin = &pasv_addr;
   1009 		goto printaddr;
   1010 	} else if (usedefault == 0) {
   1011 		printf("     PORT");
   1012 		sin = &data_dest;
   1013 printaddr:
   1014 		a = (u_char *) &sin->sin_addr;
   1015 		p = (u_char *) &sin->sin_port;
   1016 #define UC(b) (((int) b) & 0xff)
   1017 		printf(" (%d,%d,%d,%d,%d,%d)\r\n", UC(a[0]),
   1018 			UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1]));
   1019 #undef UC
   1020 	} else
   1021 		printf("     No data connection\r\n");
   1022 	reply(211, "End of status");
   1023 }
   1024 
   1025 fatal(s)
   1026 	char *s;
   1027 {
   1028 	reply(451, "Error in server: %s\n", s);
   1029 	reply(221, "Closing connection due to server error.");
   1030 	dologout(0);
   1031 	/* NOTREACHED */
   1032 }
   1033 
   1034 /* VARARGS2 */
   1035 reply(n, fmt, p0, p1, p2, p3, p4, p5)
   1036 	int n;
   1037 	char *fmt;
   1038 {
   1039 	printf("%d ", n);
   1040 	printf(fmt, p0, p1, p2, p3, p4, p5);
   1041 	printf("\r\n");
   1042 	(void)fflush(stdout);
   1043 	if (debug) {
   1044 		syslog(LOG_DEBUG, "<--- %d ", n);
   1045 		syslog(LOG_DEBUG, fmt, p0, p1, p2, p3, p4, p5);
   1046 }
   1047 }
   1048 
   1049 /* VARARGS2 */
   1050 lreply(n, fmt, p0, p1, p2, p3, p4, p5)
   1051 	int n;
   1052 	char *fmt;
   1053 {
   1054 	printf("%d- ", n);
   1055 	printf(fmt, p0, p1, p2, p3, p4, p5);
   1056 	printf("\r\n");
   1057 	(void)fflush(stdout);
   1058 	if (debug) {
   1059 		syslog(LOG_DEBUG, "<--- %d- ", n);
   1060 		syslog(LOG_DEBUG, fmt, p0, p1, p2, p3, p4, p5);
   1061 	}
   1062 }
   1063 
   1064 ack(s)
   1065 	char *s;
   1066 {
   1067 	reply(250, "%s command successful.", s);
   1068 }
   1069 
   1070 nack(s)
   1071 	char *s;
   1072 {
   1073 	reply(502, "%s command not implemented.", s);
   1074 }
   1075 
   1076 /* ARGSUSED */
   1077 yyerror(s)
   1078 	char *s;
   1079 {
   1080 	char *cp;
   1081 
   1082 	if (cp = index(cbuf,'\n'))
   1083 		*cp = '\0';
   1084 	reply(500, "'%s': command not understood.", cbuf);
   1085 }
   1086 
   1087 delete(name)
   1088 	char *name;
   1089 {
   1090 	struct stat st;
   1091 
   1092 	if (stat(name, &st) < 0) {
   1093 		perror_reply(550, name);
   1094 		return;
   1095 	}
   1096 	if ((st.st_mode&S_IFMT) == S_IFDIR) {
   1097 		if (rmdir(name) < 0) {
   1098 			perror_reply(550, name);
   1099 			return;
   1100 		}
   1101 		goto done;
   1102 	}
   1103 	if (unlink(name) < 0) {
   1104 		perror_reply(550, name);
   1105 		return;
   1106 	}
   1107 done:
   1108 	ack("DELE");
   1109 }
   1110 
   1111 cwd(path)
   1112 	char *path;
   1113 {
   1114 	if (chdir(path) < 0)
   1115 		perror_reply(550, path);
   1116 	else
   1117 		ack("CWD");
   1118 }
   1119 
   1120 makedir(name)
   1121 	char *name;
   1122 {
   1123 	if (mkdir(name, 0777) < 0)
   1124 		perror_reply(550, name);
   1125 	else
   1126 		reply(257, "MKD command successful.");
   1127 }
   1128 
   1129 removedir(name)
   1130 	char *name;
   1131 {
   1132 	if (rmdir(name) < 0)
   1133 		perror_reply(550, name);
   1134 	else
   1135 		ack("RMD");
   1136 }
   1137 
   1138 pwd()
   1139 {
   1140 	char path[MAXPATHLEN + 1];
   1141 	extern char *getwd();
   1142 
   1143 	if (getwd(path) == (char *)NULL)
   1144 		reply(550, "%s.", path);
   1145 	else
   1146 		reply(257, "\"%s\" is current directory.", path);
   1147 }
   1148 
   1149 char *
   1150 renamefrom(name)
   1151 	char *name;
   1152 {
   1153 	struct stat st;
   1154 
   1155 	if (stat(name, &st) < 0) {
   1156 		perror_reply(550, name);
   1157 		return ((char *)0);
   1158 	}
   1159 	reply(350, "File exists, ready for destination name");
   1160 	return (name);
   1161 }
   1162 
   1163 renamecmd(from, to)
   1164 	char *from, *to;
   1165 {
   1166 	if (rename(from, to) < 0)
   1167 		perror_reply(550, "rename");
   1168 	else
   1169 		ack("RNTO");
   1170 }
   1171 
   1172 dolog(sin)
   1173 	struct sockaddr_in *sin;
   1174 {
   1175 	struct hostent *hp = gethostbyaddr((char *)&sin->sin_addr,
   1176 		sizeof (struct in_addr), AF_INET);
   1177 	time_t t, time();
   1178 	extern char *ctime();
   1179 
   1180 	if (hp)
   1181 		(void) strncpy(remotehost, hp->h_name, sizeof (remotehost));
   1182 	else
   1183 		(void) strncpy(remotehost, inet_ntoa(sin->sin_addr),
   1184 		    sizeof (remotehost));
   1185 #ifdef SETPROCTITLE
   1186 	sprintf(proctitle, "%s: connected", remotehost);
   1187 	setproctitle(proctitle);
   1188 #endif /* SETPROCTITLE */
   1189 
   1190 	if (logging) {
   1191 		t = time((time_t *) 0);
   1192 		syslog(LOG_INFO, "connection from %s at %s",
   1193 		    remotehost, ctime(&t));
   1194 	}
   1195 }
   1196 
   1197 /*
   1198  * Record logout in wtmp file
   1199  * and exit with supplied status.
   1200  */
   1201 dologout(status)
   1202 	int status;
   1203 {
   1204 	if (logged_in) {
   1205 		(void) seteuid((uid_t)0);
   1206 		logwtmp(ttyline, "", "");
   1207 #if defined(KERBEROS)
   1208 		if (!notickets && krbtkfile_env)
   1209 			unlink(krbtkfile_env);
   1210 #endif
   1211 	}
   1212 	/* beware of flushing buffers after a SIGPIPE */
   1213 	_exit(status);
   1214 }
   1215 
   1216 void
   1217 myoob()
   1218 {
   1219 	char *cp;
   1220 
   1221 	/* only process if transfer occurring */
   1222 	if (!transflag)
   1223 		return;
   1224 	cp = tmpline;
   1225 	if (getline(cp, 7, stdin) == NULL) {
   1226 		reply(221, "You could at least say goodbye.");
   1227 		dologout(0);
   1228 	}
   1229 	upper(cp);
   1230 	if (strcmp(cp, "ABOR\r\n") == 0) {
   1231 		tmpline[0] = '\0';
   1232 		reply(426, "Transfer aborted. Data connection closed.");
   1233 		reply(226, "Abort successful");
   1234 		longjmp(urgcatch, 1);
   1235 	}
   1236 	if (strcmp(cp, "STAT\r\n") == 0) {
   1237 		if (file_size != (off_t) -1)
   1238 			reply(213, "Status: %lu of %lu bytes transferred",
   1239 			    byte_count, file_size);
   1240 		else
   1241 			reply(213, "Status: %lu bytes transferred", byte_count);
   1242 	}
   1243 }
   1244 
   1245 /*
   1246  * Note: a response of 425 is not mentioned as a possible response to
   1247  * 	the PASV command in RFC959. However, it has been blessed as
   1248  * 	a legitimate response by Jon Postel in a telephone conversation
   1249  *	with Rick Adams on 25 Jan 89.
   1250  */
   1251 passive()
   1252 {
   1253 	int len;
   1254 	register char *p, *a;
   1255 
   1256 	pdata = socket(AF_INET, SOCK_STREAM, 0);
   1257 	if (pdata < 0) {
   1258 		perror_reply(425, "Can't open passive connection");
   1259 		return;
   1260 	}
   1261 	pasv_addr = ctrl_addr;
   1262 	pasv_addr.sin_port = 0;
   1263 	(void) seteuid((uid_t)0);
   1264 	if (bind(pdata, (struct sockaddr *)&pasv_addr, sizeof(pasv_addr)) < 0) {
   1265 		(void) seteuid((uid_t)pw->pw_uid);
   1266 		goto pasv_error;
   1267 	}
   1268 	(void) seteuid((uid_t)pw->pw_uid);
   1269 	len = sizeof(pasv_addr);
   1270 	if (getsockname(pdata, (struct sockaddr *) &pasv_addr, &len) < 0)
   1271 		goto pasv_error;
   1272 	if (listen(pdata, 1) < 0)
   1273 		goto pasv_error;
   1274 	a = (char *) &pasv_addr.sin_addr;
   1275 	p = (char *) &pasv_addr.sin_port;
   1276 
   1277 #define UC(b) (((int) b) & 0xff)
   1278 
   1279 	reply(227, "Entering Passive Mode (%d,%d,%d,%d,%d,%d)", UC(a[0]),
   1280 		UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1]));
   1281 	return;
   1282 
   1283 pasv_error:
   1284 	(void) close(pdata);
   1285 	pdata = -1;
   1286 	perror_reply(425, "Can't open passive connection");
   1287 	return;
   1288 }
   1289 
   1290 /*
   1291  * Generate unique name for file with basename "local".
   1292  * The file named "local" is already known to exist.
   1293  * Generates failure reply on error.
   1294  */
   1295 char *
   1296 gunique(local)
   1297 	char *local;
   1298 {
   1299 	static char new[MAXPATHLEN];
   1300 	struct stat st;
   1301 	char *cp = rindex(local, '/');
   1302 	int count = 0;
   1303 
   1304 	if (cp)
   1305 		*cp = '\0';
   1306 	if (stat(cp ? local : ".", &st) < 0) {
   1307 		perror_reply(553, cp ? local : ".");
   1308 		return((char *) 0);
   1309 	}
   1310 	if (cp)
   1311 		*cp = '/';
   1312 	(void) strcpy(new, local);
   1313 	cp = new + strlen(new);
   1314 	*cp++ = '.';
   1315 	for (count = 1; count < 100; count++) {
   1316 		(void) sprintf(cp, "%d", count);
   1317 		if (stat(new, &st) < 0)
   1318 			return(new);
   1319 	}
   1320 	reply(452, "Unique file name cannot be created.");
   1321 	return((char *) 0);
   1322 }
   1323 
   1324 /*
   1325  * Format and send reply containing system error number.
   1326  */
   1327 perror_reply(code, string)
   1328 	int code;
   1329 	char *string;
   1330 {
   1331 	reply(code, "%s: %s.", string, strerror(errno));
   1332 }
   1333 
   1334 static char *onefile[] = {
   1335 	"",
   1336 	0
   1337 };
   1338 
   1339 send_file_list(whichfiles)
   1340 	char *whichfiles;
   1341 {
   1342 	struct stat st;
   1343 	DIR *dirp = NULL;
   1344 	struct dirent *dir;
   1345 	FILE *dout = NULL;
   1346 	register char **dirlist, *dirname;
   1347 	int simple = 0;
   1348 	char *strpbrk();
   1349 
   1350 	if (strpbrk(whichfiles, "~{[*?") != NULL) {
   1351 		extern char **ftpglob(), *globerr;
   1352 
   1353 		globerr = NULL;
   1354 		dirlist = ftpglob(whichfiles);
   1355 		if (globerr != NULL) {
   1356 			reply(550, globerr);
   1357 			return;
   1358 		} else if (dirlist == NULL) {
   1359 			errno = ENOENT;
   1360 			perror_reply(550, whichfiles);
   1361 			return;
   1362 		}
   1363 	} else {
   1364 		onefile[0] = whichfiles;
   1365 		dirlist = onefile;
   1366 		simple = 1;
   1367 	}
   1368 
   1369 	if (setjmp(urgcatch)) {
   1370 		transflag = 0;
   1371 		return;
   1372 	}
   1373 	while (dirname = *dirlist++) {
   1374 		if (stat(dirname, &st) < 0) {
   1375 			/*
   1376 			 * If user typed "ls -l", etc, and the client
   1377 			 * used NLST, do what the user meant.
   1378 			 */
   1379 			if (dirname[0] == '-' && *dirlist == NULL &&
   1380 			    transflag == 0) {
   1381 				retrieve("/bin/ls %s", dirname);
   1382 				return;
   1383 			}
   1384 			perror_reply(550, whichfiles);
   1385 			if (dout != NULL) {
   1386 				(void) fclose(dout);
   1387 				transflag = 0;
   1388 				data = -1;
   1389 				pdata = -1;
   1390 			}
   1391 			return;
   1392 		}
   1393 
   1394 		if ((st.st_mode&S_IFMT) == S_IFREG) {
   1395 			if (dout == NULL) {
   1396 				dout = dataconn("file list", (off_t)-1, "w");
   1397 				if (dout == NULL)
   1398 					return;
   1399 				transflag++;
   1400 			}
   1401 			fprintf(dout, "%s%s\n", dirname,
   1402 				type == TYPE_A ? "\r" : "");
   1403 			byte_count += strlen(dirname) + 1;
   1404 			continue;
   1405 		} else if ((st.st_mode&S_IFMT) != S_IFDIR)
   1406 			continue;
   1407 
   1408 		if ((dirp = opendir(dirname)) == NULL)
   1409 			continue;
   1410 
   1411 		while ((dir = readdir(dirp)) != NULL) {
   1412 			char nbuf[MAXPATHLEN];
   1413 
   1414 			if (dir->d_name[0] == '.' && dir->d_namlen == 1)
   1415 				continue;
   1416 			if (dir->d_name[0] == '.' && dir->d_name[1] == '.' &&
   1417 			    dir->d_namlen == 2)
   1418 				continue;
   1419 
   1420 			sprintf(nbuf, "%s/%s", dirname, dir->d_name);
   1421 
   1422 			/*
   1423 			 * We have to do a stat to insure it's
   1424 			 * not a directory or special file.
   1425 			 */
   1426 			if (simple || (stat(nbuf, &st) == 0 &&
   1427 			    (st.st_mode&S_IFMT) == S_IFREG)) {
   1428 				if (dout == NULL) {
   1429 					dout = dataconn("file list", (off_t)-1,
   1430 						"w");
   1431 					if (dout == NULL)
   1432 						return;
   1433 					transflag++;
   1434 				}
   1435 				if (nbuf[0] == '.' && nbuf[1] == '/')
   1436 					fprintf(dout, "%s%s\n", &nbuf[2],
   1437 						type == TYPE_A ? "\r" : "");
   1438 				else
   1439 					fprintf(dout, "%s%s\n", nbuf,
   1440 						type == TYPE_A ? "\r" : "");
   1441 				byte_count += strlen(nbuf) + 1;
   1442 			}
   1443 		}
   1444 		(void) closedir(dirp);
   1445 	}
   1446 
   1447 	if (dout == NULL)
   1448 		reply(550, "No files found.");
   1449 	else if (ferror(dout) != 0)
   1450 		perror_reply(550, "Data connection");
   1451 	else
   1452 		reply(226, "Transfer complete.");
   1453 
   1454 	transflag = 0;
   1455 	if (dout != NULL)
   1456 		(void) fclose(dout);
   1457 	data = -1;
   1458 	pdata = -1;
   1459 }
   1460 
   1461 #ifdef SETPROCTITLE
   1462 /*
   1463  * clobber argv so ps will show what we're doing.
   1464  * (stolen from sendmail)
   1465  * warning, since this is usually started from inetd.conf, it
   1466  * often doesn't have much of an environment or arglist to overwrite.
   1467  */
   1468 
   1469 /*VARARGS1*/
   1470 setproctitle(fmt, a, b, c)
   1471 char *fmt;
   1472 {
   1473 	register char *p, *bp, ch;
   1474 	register int i;
   1475 	char buf[BUFSIZ];
   1476 
   1477 	(void) sprintf(buf, fmt, a, b, c);
   1478 
   1479 	/* make ps print our process name */
   1480 	p = Argv[0];
   1481 	*p++ = '-';
   1482 
   1483 	i = strlen(buf);
   1484 	if (i > LastArgv - p - 2) {
   1485 		i = LastArgv - p - 2;
   1486 		buf[i] = '\0';
   1487 	}
   1488 	bp = buf;
   1489 	while (ch = *bp++)
   1490 		if (ch != '\n' && ch != '\r')
   1491 			*p++ = ch;
   1492 	while (p < LastArgv)
   1493 		*p++ = ' ';
   1494 }
   1495 #endif /* SETPROCTITLE */
   1496