xref: /freebsd/libexec/ftpd/ftpd.c (revision b63e1fe2c44533d69bee3a2174b1d512048bfdac)
1ea022d16SRodney W. Grimes /*
2ea022d16SRodney W. Grimes  * Copyright (c) 1985, 1988, 1990, 1992, 1993, 1994
3ea022d16SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4ea022d16SRodney W. Grimes  *
5ea022d16SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6ea022d16SRodney W. Grimes  * modification, are permitted provided that the following conditions
7ea022d16SRodney W. Grimes  * are met:
8ea022d16SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9ea022d16SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10ea022d16SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11ea022d16SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12ea022d16SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13ea022d16SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
14ea022d16SRodney W. Grimes  *    must display the following acknowledgement:
15ea022d16SRodney W. Grimes  *	This product includes software developed by the University of
16ea022d16SRodney W. Grimes  *	California, Berkeley and its contributors.
17ea022d16SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
18ea022d16SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
19ea022d16SRodney W. Grimes  *    without specific prior written permission.
20ea022d16SRodney W. Grimes  *
21ea022d16SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22ea022d16SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23ea022d16SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24ea022d16SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25ea022d16SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26ea022d16SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27ea022d16SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28ea022d16SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29ea022d16SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30ea022d16SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31ea022d16SRodney W. Grimes  * SUCH DAMAGE.
3295645563SDavid Greenman  *
33b63e1fe2SPeter Wemm  *	$Id: ftpd.c,v 1.13 1995/11/29 19:52:30 guido Exp $
34ea022d16SRodney W. Grimes  */
35ea022d16SRodney W. Grimes 
36ea022d16SRodney W. Grimes #ifndef lint
37ea022d16SRodney W. Grimes static char copyright[] =
38ea022d16SRodney W. Grimes "@(#) Copyright (c) 1985, 1988, 1990, 1992, 1993, 1994\n\
39ea022d16SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
40ea022d16SRodney W. Grimes #endif /* not lint */
41ea022d16SRodney W. Grimes 
42ea022d16SRodney W. Grimes #ifndef lint
43ea022d16SRodney W. Grimes static char sccsid[] = "@(#)ftpd.c	8.4 (Berkeley) 4/16/94";
44ea022d16SRodney W. Grimes #endif /* not lint */
45ea022d16SRodney W. Grimes 
46ea022d16SRodney W. Grimes /*
47ea022d16SRodney W. Grimes  * FTP server.
48ea022d16SRodney W. Grimes  */
49ea022d16SRodney W. Grimes #include <sys/param.h>
50ea022d16SRodney W. Grimes #include <sys/stat.h>
51ea022d16SRodney W. Grimes #include <sys/ioctl.h>
52ea022d16SRodney W. Grimes #include <sys/socket.h>
53ea022d16SRodney W. Grimes #include <sys/wait.h>
549fc5823aSGarrett Wollman #include <sys/mman.h>
55ea022d16SRodney W. Grimes 
56ea022d16SRodney W. Grimes #include <netinet/in.h>
57ea022d16SRodney W. Grimes #include <netinet/in_systm.h>
58ea022d16SRodney W. Grimes #include <netinet/ip.h>
599fc5823aSGarrett Wollman #include <netinet/tcp.h>
60ea022d16SRodney W. Grimes 
61ea022d16SRodney W. Grimes #define	FTP_NAMES
62ea022d16SRodney W. Grimes #include <arpa/ftp.h>
63ea022d16SRodney W. Grimes #include <arpa/inet.h>
64ea022d16SRodney W. Grimes #include <arpa/telnet.h>
65ea022d16SRodney W. Grimes 
66ea022d16SRodney W. Grimes #include <ctype.h>
67ea022d16SRodney W. Grimes #include <dirent.h>
68ea022d16SRodney W. Grimes #include <err.h>
69ea022d16SRodney W. Grimes #include <errno.h>
70ea022d16SRodney W. Grimes #include <fcntl.h>
71ea022d16SRodney W. Grimes #include <glob.h>
72ea022d16SRodney W. Grimes #include <limits.h>
73ea022d16SRodney W. Grimes #include <netdb.h>
74ea022d16SRodney W. Grimes #include <pwd.h>
75ea022d16SRodney W. Grimes #include <setjmp.h>
76ea022d16SRodney W. Grimes #include <signal.h>
77ea022d16SRodney W. Grimes #include <stdio.h>
78ea022d16SRodney W. Grimes #include <stdlib.h>
79ea022d16SRodney W. Grimes #include <string.h>
80ea022d16SRodney W. Grimes #include <syslog.h>
81ea022d16SRodney W. Grimes #include <time.h>
82ea022d16SRodney W. Grimes #include <unistd.h>
83b63e1fe2SPeter Wemm #include <libutil.h>
84ea022d16SRodney W. Grimes 
852c60c54cSPaul Traina #ifdef	SKEY
862c60c54cSPaul Traina #include <skey.h>
872c60c54cSPaul Traina #endif
882c60c54cSPaul Traina 
89ea022d16SRodney W. Grimes #include "pathnames.h"
90ea022d16SRodney W. Grimes #include "extern.h"
91ea022d16SRodney W. Grimes 
92ea022d16SRodney W. Grimes #if __STDC__
93ea022d16SRodney W. Grimes #include <stdarg.h>
94ea022d16SRodney W. Grimes #else
95ea022d16SRodney W. Grimes #include <varargs.h>
96ea022d16SRodney W. Grimes #endif
97ea022d16SRodney W. Grimes 
98ea022d16SRodney W. Grimes static char version[] = "Version 6.00";
99ea022d16SRodney W. Grimes 
100ea022d16SRodney W. Grimes extern	off_t restart_point;
101ea022d16SRodney W. Grimes extern	char cbuf[];
102ea022d16SRodney W. Grimes 
103ea022d16SRodney W. Grimes struct	sockaddr_in ctrl_addr;
104ea022d16SRodney W. Grimes struct	sockaddr_in data_source;
105ea022d16SRodney W. Grimes struct	sockaddr_in data_dest;
106ea022d16SRodney W. Grimes struct	sockaddr_in his_addr;
107ea022d16SRodney W. Grimes struct	sockaddr_in pasv_addr;
108ea022d16SRodney W. Grimes 
109ea022d16SRodney W. Grimes int	data;
110ea022d16SRodney W. Grimes jmp_buf	errcatch, urgcatch;
111ea022d16SRodney W. Grimes int	logged_in;
112ea022d16SRodney W. Grimes struct	passwd *pw;
113ea022d16SRodney W. Grimes int	debug;
114ea022d16SRodney W. Grimes int	timeout = 900;    /* timeout after 15 minutes of inactivity */
115ea022d16SRodney W. Grimes int	maxtimeout = 7200;/* don't allow idle time to be set beyond 2 hours */
116ea022d16SRodney W. Grimes int	logging;
1174c450ad7SPaul Traina int	restricted_data_ports = 1;
118ea022d16SRodney W. Grimes int	guest;
1193eb568f2SGuido van Rooij #ifdef STATS
1203eb568f2SGuido van Rooij int	stats;
1213eb568f2SGuido van Rooij int	statfd = -1;
1223eb568f2SGuido van Rooij #endif
123ea022d16SRodney W. Grimes int	type;
124ea022d16SRodney W. Grimes int	form;
125ea022d16SRodney W. Grimes int	stru;			/* avoid C keyword */
126ea022d16SRodney W. Grimes int	mode;
127ea022d16SRodney W. Grimes int	usedefault = 1;		/* for data transfers */
128ea022d16SRodney W. Grimes int	pdata = -1;		/* for passive mode */
129ea022d16SRodney W. Grimes sig_atomic_t transflag;
130ea022d16SRodney W. Grimes off_t	file_size;
131ea022d16SRodney W. Grimes off_t	byte_count;
132ea022d16SRodney W. Grimes #if !defined(CMASK) || CMASK == 0
133ea022d16SRodney W. Grimes #undef CMASK
134ea022d16SRodney W. Grimes #define CMASK 027
135ea022d16SRodney W. Grimes #endif
136ea022d16SRodney W. Grimes int	defumask = CMASK;		/* default umask value */
137ea022d16SRodney W. Grimes char	tmpline[7];
138ea022d16SRodney W. Grimes char	hostname[MAXHOSTNAMELEN];
139ea022d16SRodney W. Grimes char	remotehost[MAXHOSTNAMELEN];
1403eb568f2SGuido van Rooij #ifdef STATS
1413eb568f2SGuido van Rooij char	*ident = NULL;
1423eb568f2SGuido van Rooij #endif
143ea022d16SRodney W. Grimes 
144ea022d16SRodney W. Grimes /*
145ea022d16SRodney W. Grimes  * Timeout intervals for retrying connections
146ea022d16SRodney W. Grimes  * to hosts that don't accept PORT cmds.  This
147ea022d16SRodney W. Grimes  * is a kludge, but given the problems with TCP...
148ea022d16SRodney W. Grimes  */
149ea022d16SRodney W. Grimes #define	SWAITMAX	90	/* wait at most 90 seconds */
150ea022d16SRodney W. Grimes #define	SWAITINT	5	/* interval between retries */
151ea022d16SRodney W. Grimes 
152ea022d16SRodney W. Grimes int	swaitmax = SWAITMAX;
153ea022d16SRodney W. Grimes int	swaitint = SWAITINT;
154ea022d16SRodney W. Grimes 
155ea022d16SRodney W. Grimes #ifdef SETPROCTITLE
156b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE
157ea022d16SRodney W. Grimes char	**Argv = NULL;		/* pointer to argument vector */
158ea022d16SRodney W. Grimes char	*LastArgv = NULL;	/* end of argv */
159b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */
160ea022d16SRodney W. Grimes char	proctitle[LINE_MAX];	/* initial part of title */
161ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */
162ea022d16SRodney W. Grimes 
163726040deSGuido van Rooij #ifdef SKEY
164726040deSGuido van Rooij int	pwok = 0;
1652c60c54cSPaul Traina char	addr_string[20];	/* XXX */
166726040deSGuido van Rooij #endif
167726040deSGuido van Rooij 
168ea022d16SRodney W. Grimes #define LOGCMD(cmd, file) \
169ea022d16SRodney W. Grimes 	if (logging > 1) \
170ea022d16SRodney W. Grimes 	    syslog(LOG_INFO,"%s %s%s", cmd, \
171ea022d16SRodney W. Grimes 		*(file) == '/' ? "" : curdir(), file);
172ea022d16SRodney W. Grimes #define LOGCMD2(cmd, file1, file2) \
173ea022d16SRodney W. Grimes 	 if (logging > 1) \
174ea022d16SRodney W. Grimes 	    syslog(LOG_INFO,"%s %s%s %s%s", cmd, \
175ea022d16SRodney W. Grimes 		*(file1) == '/' ? "" : curdir(), file1, \
176ea022d16SRodney W. Grimes 		*(file2) == '/' ? "" : curdir(), file2);
177ea022d16SRodney W. Grimes #define LOGBYTES(cmd, file, cnt) \
178ea022d16SRodney W. Grimes 	if (logging > 1) { \
179ea022d16SRodney W. Grimes 		if (cnt == (off_t)-1) \
180ea022d16SRodney W. Grimes 		    syslog(LOG_INFO,"%s %s%s", cmd, \
181ea022d16SRodney W. Grimes 			*(file) == '/' ? "" : curdir(), file); \
182ea022d16SRodney W. Grimes 		else \
183ea022d16SRodney W. Grimes 		    syslog(LOG_INFO, "%s %s%s = %qd bytes", \
184ea022d16SRodney W. Grimes 			cmd, (*(file) == '/') ? "" : curdir(), file, cnt); \
185ea022d16SRodney W. Grimes 	}
186ea022d16SRodney W. Grimes 
187ea022d16SRodney W. Grimes static void	 ack __P((char *));
188ea022d16SRodney W. Grimes static void	 myoob __P((int));
189ea022d16SRodney W. Grimes static int	 checkuser __P((char *));
190ea022d16SRodney W. Grimes static FILE	*dataconn __P((char *, off_t, char *));
191ea022d16SRodney W. Grimes static void	 dolog __P((struct sockaddr_in *));
192ea022d16SRodney W. Grimes static char	*curdir __P((void));
193ea022d16SRodney W. Grimes static void	 end_login __P((void));
194ea022d16SRodney W. Grimes static FILE	*getdatasock __P((char *));
195ea022d16SRodney W. Grimes static char	*gunique __P((char *));
196ea022d16SRodney W. Grimes static void	 lostconn __P((int));
197ea022d16SRodney W. Grimes static int	 receive_data __P((FILE *, FILE *));
1989fc5823aSGarrett Wollman static void	 send_data __P((FILE *, FILE *, off_t, off_t, int));
199ea022d16SRodney W. Grimes static struct passwd *
200ea022d16SRodney W. Grimes 		 sgetpwnam __P((char *));
201ea022d16SRodney W. Grimes static char	*sgetsave __P((char *));
202ea022d16SRodney W. Grimes 
203ea022d16SRodney W. Grimes static char *
204ea022d16SRodney W. Grimes curdir()
205ea022d16SRodney W. Grimes {
206ea022d16SRodney W. Grimes 	static char path[MAXPATHLEN+1+1];	/* path + '/' + '\0' */
207ea022d16SRodney W. Grimes 
208ea022d16SRodney W. Grimes 	if (getcwd(path, sizeof(path)-2) == NULL)
209ea022d16SRodney W. Grimes 		return ("");
210ea022d16SRodney W. Grimes 	if (path[1] != '\0')		/* special case for root dir. */
211ea022d16SRodney W. Grimes 		strcat(path, "/");
212ea022d16SRodney W. Grimes 	/* For guest account, skip / since it's chrooted */
213ea022d16SRodney W. Grimes 	return (guest ? path+1 : path);
214ea022d16SRodney W. Grimes }
215ea022d16SRodney W. Grimes 
216ea022d16SRodney W. Grimes int
217ea022d16SRodney W. Grimes main(argc, argv, envp)
218ea022d16SRodney W. Grimes 	int argc;
219ea022d16SRodney W. Grimes 	char *argv[];
220ea022d16SRodney W. Grimes 	char **envp;
221ea022d16SRodney W. Grimes {
222ea022d16SRodney W. Grimes 	int addrlen, ch, on = 1, tos;
223ea022d16SRodney W. Grimes 	char *cp, line[LINE_MAX];
224ea022d16SRodney W. Grimes 	FILE *fd;
225ea022d16SRodney W. Grimes 
2262c60c54cSPaul Traina 	tzset();		/* in case no timezone database in ~ftp */
2272c60c54cSPaul Traina 
228ea022d16SRodney W. Grimes 	/*
229ea022d16SRodney W. Grimes 	 * LOG_NDELAY sets up the logging connection immediately,
230ea022d16SRodney W. Grimes 	 * necessary for anonymous ftp's that chroot and can't do it later.
231ea022d16SRodney W. Grimes 	 */
232ea022d16SRodney W. Grimes 	openlog("ftpd", LOG_PID | LOG_NDELAY, LOG_FTP);
233ea022d16SRodney W. Grimes 	addrlen = sizeof(his_addr);
234ea022d16SRodney W. Grimes 	if (getpeername(0, (struct sockaddr *)&his_addr, &addrlen) < 0) {
235ea022d16SRodney W. Grimes 		syslog(LOG_ERR, "getpeername (%s): %m",argv[0]);
236ea022d16SRodney W. Grimes 		exit(1);
237ea022d16SRodney W. Grimes 	}
2382c60c54cSPaul Traina #ifdef SKEY
2392c60c54cSPaul Traina 	strcpy(addr_string, inet_ntoa(his_addr.sin_addr));
2402c60c54cSPaul Traina #endif
241ea022d16SRodney W. Grimes 	addrlen = sizeof(ctrl_addr);
242ea022d16SRodney W. Grimes 	if (getsockname(0, (struct sockaddr *)&ctrl_addr, &addrlen) < 0) {
243ea022d16SRodney W. Grimes 		syslog(LOG_ERR, "getsockname (%s): %m",argv[0]);
244ea022d16SRodney W. Grimes 		exit(1);
245ea022d16SRodney W. Grimes 	}
246ea022d16SRodney W. Grimes #ifdef IP_TOS
247ea022d16SRodney W. Grimes 	tos = IPTOS_LOWDELAY;
248ea022d16SRodney W. Grimes 	if (setsockopt(0, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(int)) < 0)
249ea022d16SRodney W. Grimes 		syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
250ea022d16SRodney W. Grimes #endif
251ea022d16SRodney W. Grimes 	data_source.sin_port = htons(ntohs(ctrl_addr.sin_port) - 1);
252ea022d16SRodney W. Grimes 	debug = 0;
253b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE
254ea022d16SRodney W. Grimes 	/*
255ea022d16SRodney W. Grimes 	 *  Save start and extent of argv for setproctitle.
256ea022d16SRodney W. Grimes 	 */
257ea022d16SRodney W. Grimes 	Argv = argv;
258ea022d16SRodney W. Grimes 	while (*envp)
259ea022d16SRodney W. Grimes 		envp++;
260ea022d16SRodney W. Grimes 	LastArgv = envp[-1] + strlen(envp[-1]);
261b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */
262ea022d16SRodney W. Grimes 
2633eb568f2SGuido van Rooij 
2643eb568f2SGuido van Rooij #ifdef STATS
2653eb568f2SGuido van Rooij 	while ((ch = getopt(argc, argv, "dlSt:T:u:v")) != EOF) {
2663eb568f2SGuido van Rooij #else
2674c450ad7SPaul Traina 	while ((ch = getopt(argc, argv, "dlUt:T:u:v")) != EOF) {
2683eb568f2SGuido van Rooij #endif
269ea022d16SRodney W. Grimes 		switch (ch) {
270ea022d16SRodney W. Grimes 		case 'd':
271ea022d16SRodney W. Grimes 			debug = 1;
272ea022d16SRodney W. Grimes 			break;
273ea022d16SRodney W. Grimes 
274ea022d16SRodney W. Grimes 		case 'l':
275ea022d16SRodney W. Grimes 			logging++;	/* > 1 == extra logging */
276ea022d16SRodney W. Grimes 			break;
277ea022d16SRodney W. Grimes 
2784c450ad7SPaul Traina 		case 'U':
2794c450ad7SPaul Traina 			restricted_data_ports = 0;
2804c450ad7SPaul Traina 			break;
2814c450ad7SPaul Traina 
282ea022d16SRodney W. Grimes 		case 't':
283ea022d16SRodney W. Grimes 			timeout = atoi(optarg);
284ea022d16SRodney W. Grimes 			if (maxtimeout < timeout)
285ea022d16SRodney W. Grimes 				maxtimeout = timeout;
286ea022d16SRodney W. Grimes 			break;
2873eb568f2SGuido van Rooij #ifdef STATS
2883eb568f2SGuido van Rooij 		case 'S':
2893eb568f2SGuido van Rooij 			stats =  1;
2903eb568f2SGuido van Rooij 			break;
2913eb568f2SGuido van Rooij #endif
292ea022d16SRodney W. Grimes 		case 'T':
293ea022d16SRodney W. Grimes 			maxtimeout = atoi(optarg);
294ea022d16SRodney W. Grimes 			if (timeout > maxtimeout)
295ea022d16SRodney W. Grimes 				timeout = maxtimeout;
296ea022d16SRodney W. Grimes 			break;
297ea022d16SRodney W. Grimes 
298ea022d16SRodney W. Grimes 		case 'u':
299ea022d16SRodney W. Grimes 		    {
300ea022d16SRodney W. Grimes 			long val = 0;
301ea022d16SRodney W. Grimes 
302ea022d16SRodney W. Grimes 			val = strtol(optarg, &optarg, 8);
303ea022d16SRodney W. Grimes 			if (*optarg != '\0' || val < 0)
304ea022d16SRodney W. Grimes 				warnx("bad value for -u");
305ea022d16SRodney W. Grimes 			else
306ea022d16SRodney W. Grimes 				defumask = val;
307ea022d16SRodney W. Grimes 			break;
308ea022d16SRodney W. Grimes 		    }
309ea022d16SRodney W. Grimes 
310ea022d16SRodney W. Grimes 		case 'v':
311ea022d16SRodney W. Grimes 			debug = 1;
312ea022d16SRodney W. Grimes 			break;
313ea022d16SRodney W. Grimes 
314ea022d16SRodney W. Grimes 		default:
315ea022d16SRodney W. Grimes 			warnx("unknown flag -%c ignored", optopt);
316ea022d16SRodney W. Grimes 			break;
317ea022d16SRodney W. Grimes 		}
318ea022d16SRodney W. Grimes 	}
319ea022d16SRodney W. Grimes 	(void) freopen(_PATH_DEVNULL, "w", stderr);
320ea022d16SRodney W. Grimes 	(void) signal(SIGPIPE, lostconn);
321ea022d16SRodney W. Grimes 	(void) signal(SIGCHLD, SIG_IGN);
322ea022d16SRodney W. Grimes 	if ((int)signal(SIGURG, myoob) < 0)
323ea022d16SRodney W. Grimes 		syslog(LOG_ERR, "signal: %m");
324ea022d16SRodney W. Grimes 
325ea022d16SRodney W. Grimes 	/* Try to handle urgent data inline */
326ea022d16SRodney W. Grimes #ifdef SO_OOBINLINE
327ea022d16SRodney W. Grimes 	if (setsockopt(0, SOL_SOCKET, SO_OOBINLINE, (char *)&on, sizeof(on)) < 0)
328ea022d16SRodney W. Grimes 		syslog(LOG_ERR, "setsockopt: %m");
329ea022d16SRodney W. Grimes #endif
330ea022d16SRodney W. Grimes 
331ea022d16SRodney W. Grimes #ifdef	F_SETOWN
332ea022d16SRodney W. Grimes 	if (fcntl(fileno(stdin), F_SETOWN, getpid()) == -1)
333ea022d16SRodney W. Grimes 		syslog(LOG_ERR, "fcntl F_SETOWN: %m");
334ea022d16SRodney W. Grimes #endif
335ea022d16SRodney W. Grimes 	dolog(&his_addr);
336ea022d16SRodney W. Grimes 	/*
337ea022d16SRodney W. Grimes 	 * Set up default state
338ea022d16SRodney W. Grimes 	 */
339ea022d16SRodney W. Grimes 	data = -1;
340ea022d16SRodney W. Grimes 	type = TYPE_A;
341ea022d16SRodney W. Grimes 	form = FORM_N;
342ea022d16SRodney W. Grimes 	stru = STRU_F;
343ea022d16SRodney W. Grimes 	mode = MODE_S;
344ea022d16SRodney W. Grimes 	tmpline[0] = '\0';
345ea022d16SRodney W. Grimes 
346ea022d16SRodney W. Grimes 	/* If logins are disabled, print out the message. */
347ea022d16SRodney W. Grimes 	if ((fd = fopen(_PATH_NOLOGIN,"r")) != NULL) {
348ea022d16SRodney W. Grimes 		while (fgets(line, sizeof(line), fd) != NULL) {
349ea022d16SRodney W. Grimes 			if ((cp = strchr(line, '\n')) != NULL)
350ea022d16SRodney W. Grimes 				*cp = '\0';
351ea022d16SRodney W. Grimes 			lreply(530, "%s", line);
352ea022d16SRodney W. Grimes 		}
353ea022d16SRodney W. Grimes 		(void) fflush(stdout);
354ea022d16SRodney W. Grimes 		(void) fclose(fd);
355ea022d16SRodney W. Grimes 		reply(530, "System not available.");
356ea022d16SRodney W. Grimes 		exit(0);
357ea022d16SRodney W. Grimes 	}
358ea022d16SRodney W. Grimes 	if ((fd = fopen(_PATH_FTPWELCOME, "r")) != NULL) {
359ea022d16SRodney W. Grimes 		while (fgets(line, sizeof(line), fd) != NULL) {
360ea022d16SRodney W. Grimes 			if ((cp = strchr(line, '\n')) != NULL)
361ea022d16SRodney W. Grimes 				*cp = '\0';
362ea022d16SRodney W. Grimes 			lreply(220, "%s", line);
363ea022d16SRodney W. Grimes 		}
364ea022d16SRodney W. Grimes 		(void) fflush(stdout);
365ea022d16SRodney W. Grimes 		(void) fclose(fd);
366ea022d16SRodney W. Grimes 		/* reply(220,) must follow */
367ea022d16SRodney W. Grimes 	}
368ea022d16SRodney W. Grimes 	(void) gethostname(hostname, sizeof(hostname));
369ea022d16SRodney W. Grimes 	reply(220, "%s FTP server (%s) ready.", hostname, version);
370ea022d16SRodney W. Grimes 	(void) setjmp(errcatch);
371ea022d16SRodney W. Grimes 	for (;;)
372ea022d16SRodney W. Grimes 		(void) yyparse();
373ea022d16SRodney W. Grimes 	/* NOTREACHED */
374ea022d16SRodney W. Grimes }
375ea022d16SRodney W. Grimes 
376ea022d16SRodney W. Grimes static void
377ea022d16SRodney W. Grimes lostconn(signo)
378ea022d16SRodney W. Grimes 	int signo;
379ea022d16SRodney W. Grimes {
380ea022d16SRodney W. Grimes 
381ea022d16SRodney W. Grimes 	if (debug)
382ea022d16SRodney W. Grimes 		syslog(LOG_DEBUG, "lost connection");
383ea022d16SRodney W. Grimes 	dologout(-1);
384ea022d16SRodney W. Grimes }
385ea022d16SRodney W. Grimes 
386ea022d16SRodney W. Grimes static char ttyline[20];
387ea022d16SRodney W. Grimes 
388ea022d16SRodney W. Grimes /*
389ea022d16SRodney W. Grimes  * Helper function for sgetpwnam().
390ea022d16SRodney W. Grimes  */
391ea022d16SRodney W. Grimes static char *
392ea022d16SRodney W. Grimes sgetsave(s)
393ea022d16SRodney W. Grimes 	char *s;
394ea022d16SRodney W. Grimes {
395ea022d16SRodney W. Grimes 	char *new = malloc((unsigned) strlen(s) + 1);
396ea022d16SRodney W. Grimes 
397ea022d16SRodney W. Grimes 	if (new == NULL) {
398ea022d16SRodney W. Grimes 		perror_reply(421, "Local resource failure: malloc");
399ea022d16SRodney W. Grimes 		dologout(1);
400ea022d16SRodney W. Grimes 		/* NOTREACHED */
401ea022d16SRodney W. Grimes 	}
402ea022d16SRodney W. Grimes 	(void) strcpy(new, s);
403ea022d16SRodney W. Grimes 	return (new);
404ea022d16SRodney W. Grimes }
405ea022d16SRodney W. Grimes 
406ea022d16SRodney W. Grimes /*
407ea022d16SRodney W. Grimes  * Save the result of a getpwnam.  Used for USER command, since
408ea022d16SRodney W. Grimes  * the data returned must not be clobbered by any other command
409ea022d16SRodney W. Grimes  * (e.g., globbing).
410ea022d16SRodney W. Grimes  */
411ea022d16SRodney W. Grimes static struct passwd *
412ea022d16SRodney W. Grimes sgetpwnam(name)
413ea022d16SRodney W. Grimes 	char *name;
414ea022d16SRodney W. Grimes {
415ea022d16SRodney W. Grimes 	static struct passwd save;
416ea022d16SRodney W. Grimes 	struct passwd *p;
417ea022d16SRodney W. Grimes 
418ea022d16SRodney W. Grimes 	if ((p = getpwnam(name)) == NULL)
419ea022d16SRodney W. Grimes 		return (p);
420ea022d16SRodney W. Grimes 	if (save.pw_name) {
421ea022d16SRodney W. Grimes 		free(save.pw_name);
422ea022d16SRodney W. Grimes 		free(save.pw_passwd);
423ea022d16SRodney W. Grimes 		free(save.pw_gecos);
424ea022d16SRodney W. Grimes 		free(save.pw_dir);
425ea022d16SRodney W. Grimes 		free(save.pw_shell);
426ea022d16SRodney W. Grimes 	}
427ea022d16SRodney W. Grimes 	save = *p;
428ea022d16SRodney W. Grimes 	save.pw_name = sgetsave(p->pw_name);
429ea022d16SRodney W. Grimes 	save.pw_passwd = sgetsave(p->pw_passwd);
430ea022d16SRodney W. Grimes 	save.pw_gecos = sgetsave(p->pw_gecos);
431ea022d16SRodney W. Grimes 	save.pw_dir = sgetsave(p->pw_dir);
432ea022d16SRodney W. Grimes 	save.pw_shell = sgetsave(p->pw_shell);
433ea022d16SRodney W. Grimes 	return (&save);
434ea022d16SRodney W. Grimes }
435ea022d16SRodney W. Grimes 
436ea022d16SRodney W. Grimes static int login_attempts;	/* number of failed login attempts */
437ea022d16SRodney W. Grimes static int askpasswd;		/* had user command, ask for passwd */
438ea022d16SRodney W. Grimes static char curname[10];	/* current USER name */
439ea022d16SRodney W. Grimes 
440ea022d16SRodney W. Grimes /*
441ea022d16SRodney W. Grimes  * USER command.
442ea022d16SRodney W. Grimes  * Sets global passwd pointer pw if named account exists and is acceptable;
443ea022d16SRodney W. Grimes  * sets askpasswd if a PASS command is expected.  If logged in previously,
444ea022d16SRodney W. Grimes  * need to reset state.  If name is "ftp" or "anonymous", the name is not in
445ea022d16SRodney W. Grimes  * _PATH_FTPUSERS, and ftp account exists, set guest and pw, then just return.
446ea022d16SRodney W. Grimes  * If account doesn't exist, ask for passwd anyway.  Otherwise, check user
447ea022d16SRodney W. Grimes  * requesting login privileges.  Disallow anyone who does not have a standard
448ea022d16SRodney W. Grimes  * shell as returned by getusershell().  Disallow anyone mentioned in the file
449ea022d16SRodney W. Grimes  * _PATH_FTPUSERS to allow people such as root and uucp to be avoided.
450ea022d16SRodney W. Grimes  */
451ea022d16SRodney W. Grimes void
452ea022d16SRodney W. Grimes user(name)
453ea022d16SRodney W. Grimes 	char *name;
454ea022d16SRodney W. Grimes {
455ea022d16SRodney W. Grimes 	char *cp, *shell;
456ea022d16SRodney W. Grimes 
457ea022d16SRodney W. Grimes 	if (logged_in) {
458ea022d16SRodney W. Grimes 		if (guest) {
459ea022d16SRodney W. Grimes 			reply(530, "Can't change user from guest login.");
460ea022d16SRodney W. Grimes 			return;
461ea022d16SRodney W. Grimes 		}
462ea022d16SRodney W. Grimes 		end_login();
463ea022d16SRodney W. Grimes 	}
464ea022d16SRodney W. Grimes 
465ea022d16SRodney W. Grimes 	guest = 0;
466ea022d16SRodney W. Grimes 	if (strcmp(name, "ftp") == 0 || strcmp(name, "anonymous") == 0) {
467ea022d16SRodney W. Grimes 		if (checkuser("ftp") || checkuser("anonymous"))
468ea022d16SRodney W. Grimes 			reply(530, "User %s access denied.", name);
469ea022d16SRodney W. Grimes 		else if ((pw = sgetpwnam("ftp")) != NULL) {
470ea022d16SRodney W. Grimes 			guest = 1;
471ea022d16SRodney W. Grimes 			askpasswd = 1;
472ea022d16SRodney W. Grimes 			reply(331,
4732c60c54cSPaul Traina 			"Guest login ok, send your email address as password.");
474ea022d16SRodney W. Grimes 		} else
475ea022d16SRodney W. Grimes 			reply(530, "User %s unknown.", name);
476ea022d16SRodney W. Grimes 		if (!askpasswd && logging)
477ea022d16SRodney W. Grimes 			syslog(LOG_NOTICE,
478ea022d16SRodney W. Grimes 			    "ANONYMOUS FTP LOGIN REFUSED FROM %s", remotehost);
479ea022d16SRodney W. Grimes 		return;
480ea022d16SRodney W. Grimes 	}
481ea022d16SRodney W. Grimes 	if (pw = sgetpwnam(name)) {
482ea022d16SRodney W. Grimes 		if ((shell = pw->pw_shell) == NULL || *shell == 0)
483ea022d16SRodney W. Grimes 			shell = _PATH_BSHELL;
484ea022d16SRodney W. Grimes 		while ((cp = getusershell()) != NULL)
485ea022d16SRodney W. Grimes 			if (strcmp(cp, shell) == 0)
486ea022d16SRodney W. Grimes 				break;
487ea022d16SRodney W. Grimes 		endusershell();
488ea022d16SRodney W. Grimes 
489ea022d16SRodney W. Grimes 		if (cp == NULL || checkuser(name)) {
490ea022d16SRodney W. Grimes 			reply(530, "User %s access denied.", name);
491ea022d16SRodney W. Grimes 			if (logging)
492ea022d16SRodney W. Grimes 				syslog(LOG_NOTICE,
493ea022d16SRodney W. Grimes 				    "FTP LOGIN REFUSED FROM %s, %s",
494ea022d16SRodney W. Grimes 				    remotehost, name);
495ea022d16SRodney W. Grimes 			pw = (struct passwd *) NULL;
496ea022d16SRodney W. Grimes 			return;
497ea022d16SRodney W. Grimes 		}
498ea022d16SRodney W. Grimes 	}
499ea022d16SRodney W. Grimes 	if (logging)
500ea022d16SRodney W. Grimes 		strncpy(curname, name, sizeof(curname)-1);
501726040deSGuido van Rooij #ifdef SKEY
5022c60c54cSPaul Traina 	pwok = skeyaccess(name, NULL, remotehost, addr_string);
503726040deSGuido van Rooij 	reply(331, "%s", skey_challenge(name, pw, pwok));
504726040deSGuido van Rooij #else
505ea022d16SRodney W. Grimes 	reply(331, "Password required for %s.", name);
506726040deSGuido van Rooij #endif
507ea022d16SRodney W. Grimes 	askpasswd = 1;
508ea022d16SRodney W. Grimes 	/*
509ea022d16SRodney W. Grimes 	 * Delay before reading passwd after first failed
510ea022d16SRodney W. Grimes 	 * attempt to slow down passwd-guessing programs.
511ea022d16SRodney W. Grimes 	 */
512ea022d16SRodney W. Grimes 	if (login_attempts)
513ea022d16SRodney W. Grimes 		sleep((unsigned) login_attempts);
514ea022d16SRodney W. Grimes }
515ea022d16SRodney W. Grimes 
516ea022d16SRodney W. Grimes /*
517ea022d16SRodney W. Grimes  * Check if a user is in the file _PATH_FTPUSERS
518ea022d16SRodney W. Grimes  */
519ea022d16SRodney W. Grimes static int
520ea022d16SRodney W. Grimes checkuser(name)
521ea022d16SRodney W. Grimes 	char *name;
522ea022d16SRodney W. Grimes {
523ea022d16SRodney W. Grimes 	FILE *fd;
524ea022d16SRodney W. Grimes 	int found = 0;
525ea022d16SRodney W. Grimes 	char *p, line[BUFSIZ];
526ea022d16SRodney W. Grimes 
527ea022d16SRodney W. Grimes 	if ((fd = fopen(_PATH_FTPUSERS, "r")) != NULL) {
528ea022d16SRodney W. Grimes 		while (fgets(line, sizeof(line), fd) != NULL)
529ea022d16SRodney W. Grimes 			if ((p = strchr(line, '\n')) != NULL) {
530ea022d16SRodney W. Grimes 				*p = '\0';
531ea022d16SRodney W. Grimes 				if (line[0] == '#')
532ea022d16SRodney W. Grimes 					continue;
533348c7a12SDavid Greenman 				if (strcmp(line, name) == 0) {
534ea022d16SRodney W. Grimes 					found = 1;
535ea022d16SRodney W. Grimes 					break;
536ea022d16SRodney W. Grimes 				}
537ea022d16SRodney W. Grimes 			}
538ea022d16SRodney W. Grimes 		(void) fclose(fd);
539ea022d16SRodney W. Grimes 	}
540ea022d16SRodney W. Grimes 	return (found);
541ea022d16SRodney W. Grimes }
542ea022d16SRodney W. Grimes 
543ea022d16SRodney W. Grimes /*
544ea022d16SRodney W. Grimes  * Terminate login as previous user, if any, resetting state;
545ea022d16SRodney W. Grimes  * used when USER command is given or login fails.
546ea022d16SRodney W. Grimes  */
547ea022d16SRodney W. Grimes static void
548ea022d16SRodney W. Grimes end_login()
549ea022d16SRodney W. Grimes {
550ea022d16SRodney W. Grimes 
551ea022d16SRodney W. Grimes 	(void) seteuid((uid_t)0);
552ea022d16SRodney W. Grimes 	if (logged_in)
553ea022d16SRodney W. Grimes 		logwtmp(ttyline, "", "");
554ea022d16SRodney W. Grimes 	pw = NULL;
555ea022d16SRodney W. Grimes 	logged_in = 0;
556ea022d16SRodney W. Grimes 	guest = 0;
557ea022d16SRodney W. Grimes }
558ea022d16SRodney W. Grimes 
559ea022d16SRodney W. Grimes void
560ea022d16SRodney W. Grimes pass(passwd)
561ea022d16SRodney W. Grimes 	char *passwd;
562ea022d16SRodney W. Grimes {
563ea022d16SRodney W. Grimes 	char *salt, *xpasswd;
564ea022d16SRodney W. Grimes 	FILE *fd;
56582c76939SDavid Greenman 	static char homedir[MAXPATHLEN];
566ea022d16SRodney W. Grimes 
567ea022d16SRodney W. Grimes 	if (logged_in || askpasswd == 0) {
568ea022d16SRodney W. Grimes 		reply(503, "Login with USER first.");
569ea022d16SRodney W. Grimes 		return;
570ea022d16SRodney W. Grimes 	}
571ea022d16SRodney W. Grimes 	askpasswd = 0;
572ea022d16SRodney W. Grimes 	if (!guest) {		/* "ftp" is only account allowed no password */
573ea022d16SRodney W. Grimes 		if (pw == NULL)
574ea022d16SRodney W. Grimes 			salt = "xx";
575ea022d16SRodney W. Grimes 		else
576ea022d16SRodney W. Grimes 			salt = pw->pw_passwd;
577726040deSGuido van Rooij #ifdef SKEY
578726040deSGuido van Rooij 		xpasswd = skey_crypt(passwd, salt, pw, pwok);
579bb56d435SPaul Traina 		pwok = 0;
580726040deSGuido van Rooij #else
581ea022d16SRodney W. Grimes 		xpasswd = crypt(passwd, salt);
582726040deSGuido van Rooij #endif
583ea022d16SRodney W. Grimes 		/* The strcmp does not catch null passwords! */
584ea022d16SRodney W. Grimes 		if (pw == NULL || *pw->pw_passwd == '\0' ||
585ae532ecbSMike Pritchard 		    (pw->pw_expire && time(NULL) >= pw->pw_expire) ||
586ea022d16SRodney W. Grimes 		    strcmp(xpasswd, pw->pw_passwd)) {
587ea022d16SRodney W. Grimes 			reply(530, "Login incorrect.");
588ea022d16SRodney W. Grimes 			if (logging)
589ea022d16SRodney W. Grimes 				syslog(LOG_NOTICE,
590ea022d16SRodney W. Grimes 				    "FTP LOGIN FAILED FROM %s, %s",
591ea022d16SRodney W. Grimes 				    remotehost, curname);
592ea022d16SRodney W. Grimes 			pw = NULL;
593ea022d16SRodney W. Grimes 			if (login_attempts++ >= 5) {
594ea022d16SRodney W. Grimes 				syslog(LOG_NOTICE,
595ea022d16SRodney W. Grimes 				    "repeated login failures from %s",
596ea022d16SRodney W. Grimes 				    remotehost);
597ea022d16SRodney W. Grimes 				exit(0);
598ea022d16SRodney W. Grimes 			}
599ea022d16SRodney W. Grimes 			return;
600ea022d16SRodney W. Grimes 		}
601ea022d16SRodney W. Grimes 	}
602ea022d16SRodney W. Grimes 	login_attempts = 0;		/* this time successful */
603ea022d16SRodney W. Grimes 	if (setegid((gid_t)pw->pw_gid) < 0) {
604ea022d16SRodney W. Grimes 		reply(550, "Can't set gid.");
605ea022d16SRodney W. Grimes 		return;
606ea022d16SRodney W. Grimes 	}
607ea022d16SRodney W. Grimes 	(void) initgroups(pw->pw_name, pw->pw_gid);
608ea022d16SRodney W. Grimes 
609ea022d16SRodney W. Grimes 	/* open wtmp before chroot */
610ea022d16SRodney W. Grimes 	(void)sprintf(ttyline, "ftp%d", getpid());
611ea022d16SRodney W. Grimes 	logwtmp(ttyline, pw->pw_name, remotehost);
612ea022d16SRodney W. Grimes 	logged_in = 1;
613ea022d16SRodney W. Grimes 
6143eb568f2SGuido van Rooij #ifdef STATS
6153eb568f2SGuido van Rooij 	if (guest && stats == 1 && statfd < 0)
6163eb568f2SGuido van Rooij 		if ((statfd = open(_PATH_FTPDSTATFILE, O_WRONLY|O_APPEND)) < 0)
6173eb568f2SGuido van Rooij 			stats = 0;
6183eb568f2SGuido van Rooij #endif
6193eb568f2SGuido van Rooij 
620ea022d16SRodney W. Grimes 	if (guest) {
621ea022d16SRodney W. Grimes 		/*
622ea022d16SRodney W. Grimes 		 * We MUST do a chdir() after the chroot. Otherwise
623ea022d16SRodney W. Grimes 		 * the old current directory will be accessible as "."
624ea022d16SRodney W. Grimes 		 * outside the new root!
625ea022d16SRodney W. Grimes 		 */
626ea022d16SRodney W. Grimes 		if (chroot(pw->pw_dir) < 0 || chdir("/") < 0) {
627ea022d16SRodney W. Grimes 			reply(550, "Can't set guest privileges.");
628ea022d16SRodney W. Grimes 			goto bad;
629ea022d16SRodney W. Grimes 		}
630ea022d16SRodney W. Grimes 	} else if (chdir(pw->pw_dir) < 0) {
631ea022d16SRodney W. Grimes 		if (chdir("/") < 0) {
632ea022d16SRodney W. Grimes 			reply(530, "User %s: can't change directory to %s.",
633ea022d16SRodney W. Grimes 			    pw->pw_name, pw->pw_dir);
634ea022d16SRodney W. Grimes 			goto bad;
635ea022d16SRodney W. Grimes 		} else
636ea022d16SRodney W. Grimes 			lreply(230, "No directory! Logging in with home=/");
637ea022d16SRodney W. Grimes 	}
638ea022d16SRodney W. Grimes 	if (seteuid((uid_t)pw->pw_uid) < 0) {
639ea022d16SRodney W. Grimes 		reply(550, "Can't set uid.");
640ea022d16SRodney W. Grimes 		goto bad;
641ea022d16SRodney W. Grimes 	}
64282c76939SDavid Greenman 
64382c76939SDavid Greenman 	/*
64482c76939SDavid Greenman 	 * Set home directory so that use of ~ (tilde) works correctly.
64582c76939SDavid Greenman 	 */
64695645563SDavid Greenman 	if (getcwd(homedir, MAXPATHLEN) != NULL)
64795645563SDavid Greenman 		setenv("HOME", homedir, 1);
64882c76939SDavid Greenman 
649ea022d16SRodney W. Grimes 	/*
650ea022d16SRodney W. Grimes 	 * Display a login message, if it exists.
651ea022d16SRodney W. Grimes 	 * N.B. reply(230,) must follow the message.
652ea022d16SRodney W. Grimes 	 */
653ea022d16SRodney W. Grimes 	if ((fd = fopen(_PATH_FTPLOGINMESG, "r")) != NULL) {
654ea022d16SRodney W. Grimes 		char *cp, line[LINE_MAX];
655ea022d16SRodney W. Grimes 
656ea022d16SRodney W. Grimes 		while (fgets(line, sizeof(line), fd) != NULL) {
657ea022d16SRodney W. Grimes 			if ((cp = strchr(line, '\n')) != NULL)
658ea022d16SRodney W. Grimes 				*cp = '\0';
659ea022d16SRodney W. Grimes 			lreply(230, "%s", line);
660ea022d16SRodney W. Grimes 		}
661ea022d16SRodney W. Grimes 		(void) fflush(stdout);
662ea022d16SRodney W. Grimes 		(void) fclose(fd);
663ea022d16SRodney W. Grimes 	}
664ea022d16SRodney W. Grimes 	if (guest) {
6653eb568f2SGuido van Rooij #ifdef STATS
6663eb568f2SGuido van Rooij 		char * copy();
6673eb568f2SGuido van Rooij 
6683eb568f2SGuido van Rooij 		if (ident != NULL)
6693eb568f2SGuido van Rooij 			free(ident);
6703eb568f2SGuido van Rooij 		ident = (char *) copy(passwd);
6713eb568f2SGuido van Rooij #endif
672ea022d16SRodney W. Grimes 		reply(230, "Guest login ok, access restrictions apply.");
673ea022d16SRodney W. Grimes #ifdef SETPROCTITLE
674ea022d16SRodney W. Grimes 		snprintf(proctitle, sizeof(proctitle),
675ea022d16SRodney W. Grimes 		    "%s: anonymous/%.*s", remotehost,
676ea022d16SRodney W. Grimes 		    sizeof(proctitle) - sizeof(remotehost) -
677ea022d16SRodney W. Grimes 		    sizeof(": anonymous/"), passwd);
678b63e1fe2SPeter Wemm 		setproctitle("%s", proctitle);
679ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */
680ea022d16SRodney W. Grimes 		if (logging)
681ea022d16SRodney W. Grimes 			syslog(LOG_INFO, "ANONYMOUS FTP LOGIN FROM %s, %s",
682ea022d16SRodney W. Grimes 			    remotehost, passwd);
683ea022d16SRodney W. Grimes 	} else {
684ea022d16SRodney W. Grimes 		reply(230, "User %s logged in.", pw->pw_name);
685ea022d16SRodney W. Grimes #ifdef SETPROCTITLE
686ea022d16SRodney W. Grimes 		snprintf(proctitle, sizeof(proctitle),
687ea022d16SRodney W. Grimes 		    "%s: %s", remotehost, pw->pw_name);
688b63e1fe2SPeter Wemm 		setproctitle("%s", proctitle);
689ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */
690ea022d16SRodney W. Grimes 		if (logging)
691ea022d16SRodney W. Grimes 			syslog(LOG_INFO, "FTP LOGIN FROM %s as %s",
692ea022d16SRodney W. Grimes 			    remotehost, pw->pw_name);
693ea022d16SRodney W. Grimes 	}
694ea022d16SRodney W. Grimes 	(void) umask(defumask);
695ea022d16SRodney W. Grimes 	return;
696ea022d16SRodney W. Grimes bad:
697ea022d16SRodney W. Grimes 	/* Forget all about it... */
698ea022d16SRodney W. Grimes 	end_login();
699ea022d16SRodney W. Grimes }
700ea022d16SRodney W. Grimes 
701ea022d16SRodney W. Grimes void
702ea022d16SRodney W. Grimes retrieve(cmd, name)
703ea022d16SRodney W. Grimes 	char *cmd, *name;
704ea022d16SRodney W. Grimes {
705ea022d16SRodney W. Grimes 	FILE *fin, *dout;
706ea022d16SRodney W. Grimes 	struct stat st;
707ea022d16SRodney W. Grimes 	int (*closefunc) __P((FILE *));
7083eb568f2SGuido van Rooij #ifdef STATS
7093eb568f2SGuido van Rooij 	long start;
7103eb568f2SGuido van Rooij #endif
711ea022d16SRodney W. Grimes 
712ea022d16SRodney W. Grimes 	if (cmd == 0) {
713ea022d16SRodney W. Grimes 		fin = fopen(name, "r"), closefunc = fclose;
714ea022d16SRodney W. Grimes 		st.st_size = 0;
715ea022d16SRodney W. Grimes 	} else {
716ea022d16SRodney W. Grimes 		char line[BUFSIZ];
717ea022d16SRodney W. Grimes 
718ea022d16SRodney W. Grimes 		(void) sprintf(line, cmd, name), name = line;
719ea022d16SRodney W. Grimes 		fin = ftpd_popen(line, "r"), closefunc = ftpd_pclose;
720ea022d16SRodney W. Grimes 		st.st_size = -1;
721ea022d16SRodney W. Grimes 		st.st_blksize = BUFSIZ;
722ea022d16SRodney W. Grimes 	}
723ea022d16SRodney W. Grimes 	if (fin == NULL) {
724ea022d16SRodney W. Grimes 		if (errno != 0) {
725ea022d16SRodney W. Grimes 			perror_reply(550, name);
726ea022d16SRodney W. Grimes 			if (cmd == 0) {
727ea022d16SRodney W. Grimes 				LOGCMD("get", name);
728ea022d16SRodney W. Grimes 			}
729ea022d16SRodney W. Grimes 		}
730ea022d16SRodney W. Grimes 		return;
731ea022d16SRodney W. Grimes 	}
732ea022d16SRodney W. Grimes 	byte_count = -1;
733ea022d16SRodney W. Grimes 	if (cmd == 0 && (fstat(fileno(fin), &st) < 0 || !S_ISREG(st.st_mode))) {
734ea022d16SRodney W. Grimes 		reply(550, "%s: not a plain file.", name);
735ea022d16SRodney W. Grimes 		goto done;
736ea022d16SRodney W. Grimes 	}
737ea022d16SRodney W. Grimes 	if (restart_point) {
738ea022d16SRodney W. Grimes 		if (type == TYPE_A) {
739ea022d16SRodney W. Grimes 			off_t i, n;
740ea022d16SRodney W. Grimes 			int c;
741ea022d16SRodney W. Grimes 
742ea022d16SRodney W. Grimes 			n = restart_point;
743ea022d16SRodney W. Grimes 			i = 0;
744ea022d16SRodney W. Grimes 			while (i++ < n) {
745ea022d16SRodney W. Grimes 				if ((c=getc(fin)) == EOF) {
746ea022d16SRodney W. Grimes 					perror_reply(550, name);
747ea022d16SRodney W. Grimes 					goto done;
748ea022d16SRodney W. Grimes 				}
749ea022d16SRodney W. Grimes 				if (c == '\n')
750ea022d16SRodney W. Grimes 					i++;
751ea022d16SRodney W. Grimes 			}
752ea022d16SRodney W. Grimes 		} else if (lseek(fileno(fin), restart_point, L_SET) < 0) {
753ea022d16SRodney W. Grimes 			perror_reply(550, name);
754ea022d16SRodney W. Grimes 			goto done;
755ea022d16SRodney W. Grimes 		}
756ea022d16SRodney W. Grimes 	}
757ea022d16SRodney W. Grimes 	dout = dataconn(name, st.st_size, "w");
758ea022d16SRodney W. Grimes 	if (dout == NULL)
759ea022d16SRodney W. Grimes 		goto done;
7603eb568f2SGuido van Rooij #ifdef STATS
7613eb568f2SGuido van Rooij 	time(&start);
7623eb568f2SGuido van Rooij #endif
7639fc5823aSGarrett Wollman 	send_data(fin, dout, st.st_blksize, st.st_size,
7649fc5823aSGarrett Wollman 		  restart_point == 0 && cmd == 0 && S_ISREG(st.st_mode));
7653eb568f2SGuido van Rooij #ifdef STATS
7663eb568f2SGuido van Rooij 	if (cmd == 0 && guest && stats)
7673eb568f2SGuido van Rooij 		logxfer( name, st.st_size, start);
7683eb568f2SGuido van Rooij #endif
769ea022d16SRodney W. Grimes 	(void) fclose(dout);
770ea022d16SRodney W. Grimes 	data = -1;
771ea022d16SRodney W. Grimes 	pdata = -1;
772ea022d16SRodney W. Grimes done:
773ea022d16SRodney W. Grimes 	if (cmd == 0)
774ea022d16SRodney W. Grimes 		LOGBYTES("get", name, byte_count);
775ea022d16SRodney W. Grimes 	(*closefunc)(fin);
776ea022d16SRodney W. Grimes }
777ea022d16SRodney W. Grimes 
778ea022d16SRodney W. Grimes void
779ea022d16SRodney W. Grimes store(name, mode, unique)
780ea022d16SRodney W. Grimes 	char *name, *mode;
781ea022d16SRodney W. Grimes 	int unique;
782ea022d16SRodney W. Grimes {
783ea022d16SRodney W. Grimes 	FILE *fout, *din;
784ea022d16SRodney W. Grimes 	struct stat st;
785ea022d16SRodney W. Grimes 	int (*closefunc) __P((FILE *));
786ea022d16SRodney W. Grimes 
787ea022d16SRodney W. Grimes 	if (unique && stat(name, &st) == 0 &&
788ea022d16SRodney W. Grimes 	    (name = gunique(name)) == NULL) {
789ea022d16SRodney W. Grimes 		LOGCMD(*mode == 'w' ? "put" : "append", name);
790ea022d16SRodney W. Grimes 		return;
791ea022d16SRodney W. Grimes 	}
792ea022d16SRodney W. Grimes 
793ea022d16SRodney W. Grimes 	if (restart_point)
794ea022d16SRodney W. Grimes 		mode = "r+";
795ea022d16SRodney W. Grimes 	fout = fopen(name, mode);
796ea022d16SRodney W. Grimes 	closefunc = fclose;
797ea022d16SRodney W. Grimes 	if (fout == NULL) {
798ea022d16SRodney W. Grimes 		perror_reply(553, name);
799ea022d16SRodney W. Grimes 		LOGCMD(*mode == 'w' ? "put" : "append", name);
800ea022d16SRodney W. Grimes 		return;
801ea022d16SRodney W. Grimes 	}
802ea022d16SRodney W. Grimes 	byte_count = -1;
803ea022d16SRodney W. Grimes 	if (restart_point) {
804ea022d16SRodney W. Grimes 		if (type == TYPE_A) {
805ea022d16SRodney W. Grimes 			off_t i, n;
806ea022d16SRodney W. Grimes 			int c;
807ea022d16SRodney W. Grimes 
808ea022d16SRodney W. Grimes 			n = restart_point;
809ea022d16SRodney W. Grimes 			i = 0;
810ea022d16SRodney W. Grimes 			while (i++ < n) {
811ea022d16SRodney W. Grimes 				if ((c=getc(fout)) == EOF) {
812ea022d16SRodney W. Grimes 					perror_reply(550, name);
813ea022d16SRodney W. Grimes 					goto done;
814ea022d16SRodney W. Grimes 				}
815ea022d16SRodney W. Grimes 				if (c == '\n')
816ea022d16SRodney W. Grimes 					i++;
817ea022d16SRodney W. Grimes 			}
818ea022d16SRodney W. Grimes 			/*
819ea022d16SRodney W. Grimes 			 * We must do this seek to "current" position
820ea022d16SRodney W. Grimes 			 * because we are changing from reading to
821ea022d16SRodney W. Grimes 			 * writing.
822ea022d16SRodney W. Grimes 			 */
823ea022d16SRodney W. Grimes 			if (fseek(fout, 0L, L_INCR) < 0) {
824ea022d16SRodney W. Grimes 				perror_reply(550, name);
825ea022d16SRodney W. Grimes 				goto done;
826ea022d16SRodney W. Grimes 			}
827ea022d16SRodney W. Grimes 		} else if (lseek(fileno(fout), restart_point, L_SET) < 0) {
828ea022d16SRodney W. Grimes 			perror_reply(550, name);
829ea022d16SRodney W. Grimes 			goto done;
830ea022d16SRodney W. Grimes 		}
831ea022d16SRodney W. Grimes 	}
832ea022d16SRodney W. Grimes 	din = dataconn(name, (off_t)-1, "r");
833ea022d16SRodney W. Grimes 	if (din == NULL)
834ea022d16SRodney W. Grimes 		goto done;
835ea022d16SRodney W. Grimes 	if (receive_data(din, fout) == 0) {
836ea022d16SRodney W. Grimes 		if (unique)
837ea022d16SRodney W. Grimes 			reply(226, "Transfer complete (unique file name:%s).",
838ea022d16SRodney W. Grimes 			    name);
839ea022d16SRodney W. Grimes 		else
840ea022d16SRodney W. Grimes 			reply(226, "Transfer complete.");
841ea022d16SRodney W. Grimes 	}
842ea022d16SRodney W. Grimes 	(void) fclose(din);
843ea022d16SRodney W. Grimes 	data = -1;
844ea022d16SRodney W. Grimes 	pdata = -1;
845ea022d16SRodney W. Grimes done:
846ea022d16SRodney W. Grimes 	LOGBYTES(*mode == 'w' ? "put" : "append", name, byte_count);
847ea022d16SRodney W. Grimes 	(*closefunc)(fout);
848ea022d16SRodney W. Grimes }
849ea022d16SRodney W. Grimes 
850ea022d16SRodney W. Grimes static FILE *
851ea022d16SRodney W. Grimes getdatasock(mode)
852ea022d16SRodney W. Grimes 	char *mode;
853ea022d16SRodney W. Grimes {
854ea022d16SRodney W. Grimes 	int on = 1, s, t, tries;
855ea022d16SRodney W. Grimes 
856ea022d16SRodney W. Grimes 	if (data >= 0)
857ea022d16SRodney W. Grimes 		return (fdopen(data, mode));
858ea022d16SRodney W. Grimes 	(void) seteuid((uid_t)0);
859ea022d16SRodney W. Grimes 	s = socket(AF_INET, SOCK_STREAM, 0);
860ea022d16SRodney W. Grimes 	if (s < 0)
861ea022d16SRodney W. Grimes 		goto bad;
862ea022d16SRodney W. Grimes 	if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
863ea022d16SRodney W. Grimes 	    (char *) &on, sizeof(on)) < 0)
864ea022d16SRodney W. Grimes 		goto bad;
865ea022d16SRodney W. Grimes 	/* anchor socket to avoid multi-homing problems */
866ea022d16SRodney W. Grimes 	data_source.sin_family = AF_INET;
867ea022d16SRodney W. Grimes 	data_source.sin_addr = ctrl_addr.sin_addr;
868ea022d16SRodney W. Grimes 	for (tries = 1; ; tries++) {
869ea022d16SRodney W. Grimes 		if (bind(s, (struct sockaddr *)&data_source,
870ea022d16SRodney W. Grimes 		    sizeof(data_source)) >= 0)
871ea022d16SRodney W. Grimes 			break;
872ea022d16SRodney W. Grimes 		if (errno != EADDRINUSE || tries > 10)
873ea022d16SRodney W. Grimes 			goto bad;
874ea022d16SRodney W. Grimes 		sleep(tries);
875ea022d16SRodney W. Grimes 	}
876ea022d16SRodney W. Grimes 	(void) seteuid((uid_t)pw->pw_uid);
877ea022d16SRodney W. Grimes #ifdef IP_TOS
878ea022d16SRodney W. Grimes 	on = IPTOS_THROUGHPUT;
879ea022d16SRodney W. Grimes 	if (setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0)
880ea022d16SRodney W. Grimes 		syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
881ea022d16SRodney W. Grimes #endif
8829fc5823aSGarrett Wollman #ifdef TCP_NOPUSH
8839fc5823aSGarrett Wollman 	/*
8849fc5823aSGarrett Wollman 	 * Turn off push flag to keep sender TCP from sending short packets
8859fc5823aSGarrett Wollman 	 * at the boundaries of each write().  Should probably do a SO_SNDBUF
8869fc5823aSGarrett Wollman 	 * to set the send buffer size as well, but that may not be desirable
8879fc5823aSGarrett Wollman 	 * in heavy-load situations.
8889fc5823aSGarrett Wollman 	 */
8899fc5823aSGarrett Wollman 	on = 1;
8909fc5823aSGarrett Wollman 	if (setsockopt(s, IPPROTO_TCP, TCP_NOPUSH, (char *)&on, sizeof on) < 0)
8919fc5823aSGarrett Wollman 		syslog(LOG_WARNING, "setsockopt (TCP_NOPUSH): %m");
8929fc5823aSGarrett Wollman #endif
8939fc5823aSGarrett Wollman #ifdef SO_SNDBUF
8949fc5823aSGarrett Wollman 	on = 65536;
8959fc5823aSGarrett Wollman 	if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, (char *)&on, sizeof on) < 0)
8969fc5823aSGarrett Wollman 		syslog(LOG_WARNING, "setsockopt (SO_SNDBUF): %m");
8979fc5823aSGarrett Wollman #endif
8989fc5823aSGarrett Wollman 
899ea022d16SRodney W. Grimes 	return (fdopen(s, mode));
900ea022d16SRodney W. Grimes bad:
901ea022d16SRodney W. Grimes 	/* Return the real value of errno (close may change it) */
902ea022d16SRodney W. Grimes 	t = errno;
903ea022d16SRodney W. Grimes 	(void) seteuid((uid_t)pw->pw_uid);
904ea022d16SRodney W. Grimes 	(void) close(s);
905ea022d16SRodney W. Grimes 	errno = t;
906ea022d16SRodney W. Grimes 	return (NULL);
907ea022d16SRodney W. Grimes }
908ea022d16SRodney W. Grimes 
909ea022d16SRodney W. Grimes static FILE *
910ea022d16SRodney W. Grimes dataconn(name, size, mode)
911ea022d16SRodney W. Grimes 	char *name;
912ea022d16SRodney W. Grimes 	off_t size;
913ea022d16SRodney W. Grimes 	char *mode;
914ea022d16SRodney W. Grimes {
915ea022d16SRodney W. Grimes 	char sizebuf[32];
916ea022d16SRodney W. Grimes 	FILE *file;
917ea022d16SRodney W. Grimes 	int retry = 0, tos;
918ea022d16SRodney W. Grimes 
919ea022d16SRodney W. Grimes 	file_size = size;
920ea022d16SRodney W. Grimes 	byte_count = 0;
921ea022d16SRodney W. Grimes 	if (size != (off_t) -1)
922ea022d16SRodney W. Grimes 		(void) sprintf(sizebuf, " (%qd bytes)", size);
923ea022d16SRodney W. Grimes 	else
924ea022d16SRodney W. Grimes 		(void) strcpy(sizebuf, "");
925ea022d16SRodney W. Grimes 	if (pdata >= 0) {
926ea022d16SRodney W. Grimes 		struct sockaddr_in from;
927ea022d16SRodney W. Grimes 		int s, fromlen = sizeof(from);
928d6ed3c37SGuido van Rooij 		struct timeval timeout;
929d6ed3c37SGuido van Rooij 		fd_set set;
930ea022d16SRodney W. Grimes 
931d6ed3c37SGuido van Rooij 		FD_ZERO(&set);
932d6ed3c37SGuido van Rooij 		FD_SET(pdata, &set);
933d6ed3c37SGuido van Rooij 
934d6ed3c37SGuido van Rooij 		timeout.tv_usec = 0;
935d6ed3c37SGuido van Rooij 		timeout.tv_sec = 120;
936d6ed3c37SGuido van Rooij 
937d6ed3c37SGuido van Rooij 		if (select(pdata+1, &set, (fd_set *) 0, (fd_set *) 0, &timeout) == 0 ||
938d6ed3c37SGuido van Rooij 		    (s = accept(pdata, (struct sockaddr *) &from, &fromlen)) < 0) {
939ea022d16SRodney W. Grimes 			reply(425, "Can't open data connection.");
940ea022d16SRodney W. Grimes 			(void) close(pdata);
941ea022d16SRodney W. Grimes 			pdata = -1;
942ea022d16SRodney W. Grimes 			return (NULL);
943ea022d16SRodney W. Grimes 		}
944ea022d16SRodney W. Grimes 		(void) close(pdata);
945ea022d16SRodney W. Grimes 		pdata = s;
946ea022d16SRodney W. Grimes #ifdef IP_TOS
947ea022d16SRodney W. Grimes 		tos = IPTOS_LOWDELAY;
948ea022d16SRodney W. Grimes 		(void) setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&tos,
949ea022d16SRodney W. Grimes 		    sizeof(int));
950ea022d16SRodney W. Grimes #endif
951ea022d16SRodney W. Grimes 		reply(150, "Opening %s mode data connection for '%s'%s.",
952ea022d16SRodney W. Grimes 		     type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf);
953ea022d16SRodney W. Grimes 		return (fdopen(pdata, mode));
954ea022d16SRodney W. Grimes 	}
955ea022d16SRodney W. Grimes 	if (data >= 0) {
956ea022d16SRodney W. Grimes 		reply(125, "Using existing data connection for '%s'%s.",
957ea022d16SRodney W. Grimes 		    name, sizebuf);
958ea022d16SRodney W. Grimes 		usedefault = 1;
959ea022d16SRodney W. Grimes 		return (fdopen(data, mode));
960ea022d16SRodney W. Grimes 	}
961ea022d16SRodney W. Grimes 	if (usedefault)
962ea022d16SRodney W. Grimes 		data_dest = his_addr;
963ea022d16SRodney W. Grimes 	usedefault = 1;
964ea022d16SRodney W. Grimes 	file = getdatasock(mode);
965ea022d16SRodney W. Grimes 	if (file == NULL) {
966ea022d16SRodney W. Grimes 		reply(425, "Can't create data socket (%s,%d): %s.",
967ea022d16SRodney W. Grimes 		    inet_ntoa(data_source.sin_addr),
968ea022d16SRodney W. Grimes 		    ntohs(data_source.sin_port), strerror(errno));
969ea022d16SRodney W. Grimes 		return (NULL);
970ea022d16SRodney W. Grimes 	}
971ea022d16SRodney W. Grimes 	data = fileno(file);
972ea022d16SRodney W. Grimes 	while (connect(data, (struct sockaddr *)&data_dest,
973ea022d16SRodney W. Grimes 	    sizeof(data_dest)) < 0) {
974ea022d16SRodney W. Grimes 		if (errno == EADDRINUSE && retry < swaitmax) {
975ea022d16SRodney W. Grimes 			sleep((unsigned) swaitint);
976ea022d16SRodney W. Grimes 			retry += swaitint;
977ea022d16SRodney W. Grimes 			continue;
978ea022d16SRodney W. Grimes 		}
979ea022d16SRodney W. Grimes 		perror_reply(425, "Can't build data connection");
980ea022d16SRodney W. Grimes 		(void) fclose(file);
981ea022d16SRodney W. Grimes 		data = -1;
982ea022d16SRodney W. Grimes 		return (NULL);
983ea022d16SRodney W. Grimes 	}
984ea022d16SRodney W. Grimes 	reply(150, "Opening %s mode data connection for '%s'%s.",
985ea022d16SRodney W. Grimes 	     type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf);
986ea022d16SRodney W. Grimes 	return (file);
987ea022d16SRodney W. Grimes }
988ea022d16SRodney W. Grimes 
989ea022d16SRodney W. Grimes /*
990ea022d16SRodney W. Grimes  * Tranfer the contents of "instr" to "outstr" peer using the appropriate
9919fc5823aSGarrett Wollman  * encapsulation of the data subject to Mode, Structure, and Type.
992ea022d16SRodney W. Grimes  *
993ea022d16SRodney W. Grimes  * NB: Form isn't handled.
994ea022d16SRodney W. Grimes  */
995ea022d16SRodney W. Grimes static void
9969fc5823aSGarrett Wollman send_data(instr, outstr, blksize, filesize, isreg)
997ea022d16SRodney W. Grimes 	FILE *instr, *outstr;
998ea022d16SRodney W. Grimes 	off_t blksize;
9999fc5823aSGarrett Wollman 	off_t filesize;
10009fc5823aSGarrett Wollman 	int isreg;
1001ea022d16SRodney W. Grimes {
1002ea022d16SRodney W. Grimes 	int c, cnt, filefd, netfd;
10039fc5823aSGarrett Wollman 	char *buf, *bp;
10049fc5823aSGarrett Wollman 	size_t len;
1005ea022d16SRodney W. Grimes 
1006ea022d16SRodney W. Grimes 	transflag++;
1007ea022d16SRodney W. Grimes 	if (setjmp(urgcatch)) {
1008ea022d16SRodney W. Grimes 		transflag = 0;
1009ea022d16SRodney W. Grimes 		return;
1010ea022d16SRodney W. Grimes 	}
1011ea022d16SRodney W. Grimes 	switch (type) {
1012ea022d16SRodney W. Grimes 
1013ea022d16SRodney W. Grimes 	case TYPE_A:
1014ea022d16SRodney W. Grimes 		while ((c = getc(instr)) != EOF) {
1015ea022d16SRodney W. Grimes 			byte_count++;
1016ea022d16SRodney W. Grimes 			if (c == '\n') {
1017ea022d16SRodney W. Grimes 				if (ferror(outstr))
1018ea022d16SRodney W. Grimes 					goto data_err;
1019ea022d16SRodney W. Grimes 				(void) putc('\r', outstr);
1020ea022d16SRodney W. Grimes 			}
1021ea022d16SRodney W. Grimes 			(void) putc(c, outstr);
1022ea022d16SRodney W. Grimes 		}
1023ea022d16SRodney W. Grimes 		fflush(outstr);
1024ea022d16SRodney W. Grimes 		transflag = 0;
1025ea022d16SRodney W. Grimes 		if (ferror(instr))
1026ea022d16SRodney W. Grimes 			goto file_err;
1027ea022d16SRodney W. Grimes 		if (ferror(outstr))
1028ea022d16SRodney W. Grimes 			goto data_err;
1029ea022d16SRodney W. Grimes 		reply(226, "Transfer complete.");
1030ea022d16SRodney W. Grimes 		return;
1031ea022d16SRodney W. Grimes 
1032ea022d16SRodney W. Grimes 	case TYPE_I:
1033ea022d16SRodney W. Grimes 	case TYPE_L:
10349fc5823aSGarrett Wollman 		/*
10359fc5823aSGarrett Wollman 		 * isreg is only set if we are not doing restart and we
10369fc5823aSGarrett Wollman 		 * are sending a regular file
10379fc5823aSGarrett Wollman 		 */
10389fc5823aSGarrett Wollman 		netfd = fileno(outstr);
10399fc5823aSGarrett Wollman 		filefd = fileno(instr);
10409fc5823aSGarrett Wollman 
10419fc5823aSGarrett Wollman 		if (isreg && filesize < (off_t)16 * 1024 * 1024) {
10429fc5823aSGarrett Wollman 			buf = mmap(0, filesize, PROT_READ, MAP_SHARED, filefd,
10439fc5823aSGarrett Wollman 				   (off_t)0);
10449fc5823aSGarrett Wollman 			if (!buf) {
10459fc5823aSGarrett Wollman 				syslog(LOG_WARNING, "mmap(%lu): %m",
10469fc5823aSGarrett Wollman 				       (unsigned long)filesize);
10479fc5823aSGarrett Wollman 				goto oldway;
10489fc5823aSGarrett Wollman 			}
10499fc5823aSGarrett Wollman 			bp = buf;
10509fc5823aSGarrett Wollman 			len = filesize;
10519fc5823aSGarrett Wollman 			do {
10529fc5823aSGarrett Wollman 				cnt = write(netfd, bp, len);
10539fc5823aSGarrett Wollman 				len -= cnt;
10549fc5823aSGarrett Wollman 				bp += cnt;
10559fc5823aSGarrett Wollman 				if (cnt > 0) byte_count += cnt;
10569fc5823aSGarrett Wollman 			} while(cnt > 0 && len > 0);
10579fc5823aSGarrett Wollman 
10589fc5823aSGarrett Wollman 			transflag = 0;
10599fc5823aSGarrett Wollman 			munmap(buf, (size_t)filesize);
10609fc5823aSGarrett Wollman 			if (cnt < 0)
10619fc5823aSGarrett Wollman 				goto data_err;
10629fc5823aSGarrett Wollman 			reply(226, "Transfer complete.");
10639fc5823aSGarrett Wollman 			return;
10649fc5823aSGarrett Wollman 		}
10659fc5823aSGarrett Wollman 
10669fc5823aSGarrett Wollman oldway:
1067ea022d16SRodney W. Grimes 		if ((buf = malloc((u_int)blksize)) == NULL) {
1068ea022d16SRodney W. Grimes 			transflag = 0;
1069ea022d16SRodney W. Grimes 			perror_reply(451, "Local resource failure: malloc");
1070ea022d16SRodney W. Grimes 			return;
1071ea022d16SRodney W. Grimes 		}
10729fc5823aSGarrett Wollman 
1073ea022d16SRodney W. Grimes 		while ((cnt = read(filefd, buf, (u_int)blksize)) > 0 &&
1074ea022d16SRodney W. Grimes 		    write(netfd, buf, cnt) == cnt)
1075ea022d16SRodney W. Grimes 			byte_count += cnt;
1076ea022d16SRodney W. Grimes 		transflag = 0;
1077ea022d16SRodney W. Grimes 		(void)free(buf);
1078ea022d16SRodney W. Grimes 		if (cnt != 0) {
1079ea022d16SRodney W. Grimes 			if (cnt < 0)
1080ea022d16SRodney W. Grimes 				goto file_err;
1081ea022d16SRodney W. Grimes 			goto data_err;
1082ea022d16SRodney W. Grimes 		}
1083ea022d16SRodney W. Grimes 		reply(226, "Transfer complete.");
1084ea022d16SRodney W. Grimes 		return;
1085ea022d16SRodney W. Grimes 	default:
1086ea022d16SRodney W. Grimes 		transflag = 0;
1087ea022d16SRodney W. Grimes 		reply(550, "Unimplemented TYPE %d in send_data", type);
1088ea022d16SRodney W. Grimes 		return;
1089ea022d16SRodney W. Grimes 	}
1090ea022d16SRodney W. Grimes 
1091ea022d16SRodney W. Grimes data_err:
1092ea022d16SRodney W. Grimes 	transflag = 0;
1093ea022d16SRodney W. Grimes 	perror_reply(426, "Data connection");
1094ea022d16SRodney W. Grimes 	return;
1095ea022d16SRodney W. Grimes 
1096ea022d16SRodney W. Grimes file_err:
1097ea022d16SRodney W. Grimes 	transflag = 0;
1098ea022d16SRodney W. Grimes 	perror_reply(551, "Error on input file");
1099ea022d16SRodney W. Grimes }
1100ea022d16SRodney W. Grimes 
1101ea022d16SRodney W. Grimes /*
1102ea022d16SRodney W. Grimes  * Transfer data from peer to "outstr" using the appropriate encapulation of
1103ea022d16SRodney W. Grimes  * the data subject to Mode, Structure, and Type.
1104ea022d16SRodney W. Grimes  *
1105ea022d16SRodney W. Grimes  * N.B.: Form isn't handled.
1106ea022d16SRodney W. Grimes  */
1107ea022d16SRodney W. Grimes static int
1108ea022d16SRodney W. Grimes receive_data(instr, outstr)
1109ea022d16SRodney W. Grimes 	FILE *instr, *outstr;
1110ea022d16SRodney W. Grimes {
1111ea022d16SRodney W. Grimes 	int c;
1112ea022d16SRodney W. Grimes 	int cnt, bare_lfs = 0;
1113ea022d16SRodney W. Grimes 	char buf[BUFSIZ];
1114ea022d16SRodney W. Grimes 
1115ea022d16SRodney W. Grimes 	transflag++;
1116ea022d16SRodney W. Grimes 	if (setjmp(urgcatch)) {
1117ea022d16SRodney W. Grimes 		transflag = 0;
1118ea022d16SRodney W. Grimes 		return (-1);
1119ea022d16SRodney W. Grimes 	}
1120ea022d16SRodney W. Grimes 	switch (type) {
1121ea022d16SRodney W. Grimes 
1122ea022d16SRodney W. Grimes 	case TYPE_I:
1123ea022d16SRodney W. Grimes 	case TYPE_L:
1124ea022d16SRodney W. Grimes 		while ((cnt = read(fileno(instr), buf, sizeof(buf))) > 0) {
1125ea022d16SRodney W. Grimes 			if (write(fileno(outstr), buf, cnt) != cnt)
1126ea022d16SRodney W. Grimes 				goto file_err;
1127ea022d16SRodney W. Grimes 			byte_count += cnt;
1128ea022d16SRodney W. Grimes 		}
1129ea022d16SRodney W. Grimes 		if (cnt < 0)
1130ea022d16SRodney W. Grimes 			goto data_err;
1131ea022d16SRodney W. Grimes 		transflag = 0;
1132ea022d16SRodney W. Grimes 		return (0);
1133ea022d16SRodney W. Grimes 
1134ea022d16SRodney W. Grimes 	case TYPE_E:
1135ea022d16SRodney W. Grimes 		reply(553, "TYPE E not implemented.");
1136ea022d16SRodney W. Grimes 		transflag = 0;
1137ea022d16SRodney W. Grimes 		return (-1);
1138ea022d16SRodney W. Grimes 
1139ea022d16SRodney W. Grimes 	case TYPE_A:
1140ea022d16SRodney W. Grimes 		while ((c = getc(instr)) != EOF) {
1141ea022d16SRodney W. Grimes 			byte_count++;
1142ea022d16SRodney W. Grimes 			if (c == '\n')
1143ea022d16SRodney W. Grimes 				bare_lfs++;
1144ea022d16SRodney W. Grimes 			while (c == '\r') {
1145ea022d16SRodney W. Grimes 				if (ferror(outstr))
1146ea022d16SRodney W. Grimes 					goto data_err;
1147ea022d16SRodney W. Grimes 				if ((c = getc(instr)) != '\n') {
1148ea022d16SRodney W. Grimes 					(void) putc ('\r', outstr);
1149ea022d16SRodney W. Grimes 					if (c == '\0' || c == EOF)
1150ea022d16SRodney W. Grimes 						goto contin2;
1151ea022d16SRodney W. Grimes 				}
1152ea022d16SRodney W. Grimes 			}
1153ea022d16SRodney W. Grimes 			(void) putc(c, outstr);
1154ea022d16SRodney W. Grimes 	contin2:	;
1155ea022d16SRodney W. Grimes 		}
1156ea022d16SRodney W. Grimes 		fflush(outstr);
1157ea022d16SRodney W. Grimes 		if (ferror(instr))
1158ea022d16SRodney W. Grimes 			goto data_err;
1159ea022d16SRodney W. Grimes 		if (ferror(outstr))
1160ea022d16SRodney W. Grimes 			goto file_err;
1161ea022d16SRodney W. Grimes 		transflag = 0;
1162ea022d16SRodney W. Grimes 		if (bare_lfs) {
1163ea022d16SRodney W. Grimes 			lreply(226,
1164ea022d16SRodney W. Grimes 		"WARNING! %d bare linefeeds received in ASCII mode",
1165ea022d16SRodney W. Grimes 			    bare_lfs);
1166ea022d16SRodney W. Grimes 		(void)printf("   File may not have transferred correctly.\r\n");
1167ea022d16SRodney W. Grimes 		}
1168ea022d16SRodney W. Grimes 		return (0);
1169ea022d16SRodney W. Grimes 	default:
1170ea022d16SRodney W. Grimes 		reply(550, "Unimplemented TYPE %d in receive_data", type);
1171ea022d16SRodney W. Grimes 		transflag = 0;
1172ea022d16SRodney W. Grimes 		return (-1);
1173ea022d16SRodney W. Grimes 	}
1174ea022d16SRodney W. Grimes 
1175ea022d16SRodney W. Grimes data_err:
1176ea022d16SRodney W. Grimes 	transflag = 0;
1177ea022d16SRodney W. Grimes 	perror_reply(426, "Data Connection");
1178ea022d16SRodney W. Grimes 	return (-1);
1179ea022d16SRodney W. Grimes 
1180ea022d16SRodney W. Grimes file_err:
1181ea022d16SRodney W. Grimes 	transflag = 0;
1182ea022d16SRodney W. Grimes 	perror_reply(452, "Error writing file");
1183ea022d16SRodney W. Grimes 	return (-1);
1184ea022d16SRodney W. Grimes }
1185ea022d16SRodney W. Grimes 
1186ea022d16SRodney W. Grimes void
1187ea022d16SRodney W. Grimes statfilecmd(filename)
1188ea022d16SRodney W. Grimes 	char *filename;
1189ea022d16SRodney W. Grimes {
1190ea022d16SRodney W. Grimes 	FILE *fin;
1191ea022d16SRodney W. Grimes 	int c;
1192ea022d16SRodney W. Grimes 	char line[LINE_MAX];
1193ea022d16SRodney W. Grimes 
1194ea022d16SRodney W. Grimes 	(void)snprintf(line, sizeof(line), "/bin/ls -lgA %s", filename);
1195ea022d16SRodney W. Grimes 	fin = ftpd_popen(line, "r");
1196ea022d16SRodney W. Grimes 	lreply(211, "status of %s:", filename);
1197ea022d16SRodney W. Grimes 	while ((c = getc(fin)) != EOF) {
1198ea022d16SRodney W. Grimes 		if (c == '\n') {
1199ea022d16SRodney W. Grimes 			if (ferror(stdout)){
1200ea022d16SRodney W. Grimes 				perror_reply(421, "control connection");
1201ea022d16SRodney W. Grimes 				(void) ftpd_pclose(fin);
1202ea022d16SRodney W. Grimes 				dologout(1);
1203ea022d16SRodney W. Grimes 				/* NOTREACHED */
1204ea022d16SRodney W. Grimes 			}
1205ea022d16SRodney W. Grimes 			if (ferror(fin)) {
1206ea022d16SRodney W. Grimes 				perror_reply(551, filename);
1207ea022d16SRodney W. Grimes 				(void) ftpd_pclose(fin);
1208ea022d16SRodney W. Grimes 				return;
1209ea022d16SRodney W. Grimes 			}
1210ea022d16SRodney W. Grimes 			(void) putc('\r', stdout);
1211ea022d16SRodney W. Grimes 		}
1212ea022d16SRodney W. Grimes 		(void) putc(c, stdout);
1213ea022d16SRodney W. Grimes 	}
1214ea022d16SRodney W. Grimes 	(void) ftpd_pclose(fin);
1215ea022d16SRodney W. Grimes 	reply(211, "End of Status");
1216ea022d16SRodney W. Grimes }
1217ea022d16SRodney W. Grimes 
1218ea022d16SRodney W. Grimes void
1219ea022d16SRodney W. Grimes statcmd()
1220ea022d16SRodney W. Grimes {
1221ea022d16SRodney W. Grimes 	struct sockaddr_in *sin;
1222ea022d16SRodney W. Grimes 	u_char *a, *p;
1223ea022d16SRodney W. Grimes 
1224ea022d16SRodney W. Grimes 	lreply(211, "%s FTP server status:", hostname, version);
1225ea022d16SRodney W. Grimes 	printf("     %s\r\n", version);
1226ea022d16SRodney W. Grimes 	printf("     Connected to %s", remotehost);
1227ea022d16SRodney W. Grimes 	if (!isdigit(remotehost[0]))
1228ea022d16SRodney W. Grimes 		printf(" (%s)", inet_ntoa(his_addr.sin_addr));
1229ea022d16SRodney W. Grimes 	printf("\r\n");
1230ea022d16SRodney W. Grimes 	if (logged_in) {
1231ea022d16SRodney W. Grimes 		if (guest)
1232ea022d16SRodney W. Grimes 			printf("     Logged in anonymously\r\n");
1233ea022d16SRodney W. Grimes 		else
1234ea022d16SRodney W. Grimes 			printf("     Logged in as %s\r\n", pw->pw_name);
1235ea022d16SRodney W. Grimes 	} else if (askpasswd)
1236ea022d16SRodney W. Grimes 		printf("     Waiting for password\r\n");
1237ea022d16SRodney W. Grimes 	else
1238ea022d16SRodney W. Grimes 		printf("     Waiting for user name\r\n");
1239ea022d16SRodney W. Grimes 	printf("     TYPE: %s", typenames[type]);
1240ea022d16SRodney W. Grimes 	if (type == TYPE_A || type == TYPE_E)
1241ea022d16SRodney W. Grimes 		printf(", FORM: %s", formnames[form]);
1242ea022d16SRodney W. Grimes 	if (type == TYPE_L)
1243ea022d16SRodney W. Grimes #if NBBY == 8
1244ea022d16SRodney W. Grimes 		printf(" %d", NBBY);
1245ea022d16SRodney W. Grimes #else
1246ea022d16SRodney W. Grimes 		printf(" %d", bytesize);	/* need definition! */
1247ea022d16SRodney W. Grimes #endif
1248ea022d16SRodney W. Grimes 	printf("; STRUcture: %s; transfer MODE: %s\r\n",
1249ea022d16SRodney W. Grimes 	    strunames[stru], modenames[mode]);
1250ea022d16SRodney W. Grimes 	if (data != -1)
1251ea022d16SRodney W. Grimes 		printf("     Data connection open\r\n");
1252ea022d16SRodney W. Grimes 	else if (pdata != -1) {
1253ea022d16SRodney W. Grimes 		printf("     in Passive mode");
1254ea022d16SRodney W. Grimes 		sin = &pasv_addr;
1255ea022d16SRodney W. Grimes 		goto printaddr;
1256ea022d16SRodney W. Grimes 	} else if (usedefault == 0) {
1257ea022d16SRodney W. Grimes 		printf("     PORT");
1258ea022d16SRodney W. Grimes 		sin = &data_dest;
1259ea022d16SRodney W. Grimes printaddr:
1260ea022d16SRodney W. Grimes 		a = (u_char *) &sin->sin_addr;
1261ea022d16SRodney W. Grimes 		p = (u_char *) &sin->sin_port;
1262ea022d16SRodney W. Grimes #define UC(b) (((int) b) & 0xff)
1263ea022d16SRodney W. Grimes 		printf(" (%d,%d,%d,%d,%d,%d)\r\n", UC(a[0]),
1264ea022d16SRodney W. Grimes 			UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1]));
1265ea022d16SRodney W. Grimes #undef UC
1266ea022d16SRodney W. Grimes 	} else
1267ea022d16SRodney W. Grimes 		printf("     No data connection\r\n");
1268ea022d16SRodney W. Grimes 	reply(211, "End of status");
1269ea022d16SRodney W. Grimes }
1270ea022d16SRodney W. Grimes 
1271ea022d16SRodney W. Grimes void
1272ea022d16SRodney W. Grimes fatal(s)
1273ea022d16SRodney W. Grimes 	char *s;
1274ea022d16SRodney W. Grimes {
1275ea022d16SRodney W. Grimes 
1276ea022d16SRodney W. Grimes 	reply(451, "Error in server: %s\n", s);
1277ea022d16SRodney W. Grimes 	reply(221, "Closing connection due to server error.");
1278ea022d16SRodney W. Grimes 	dologout(0);
1279ea022d16SRodney W. Grimes 	/* NOTREACHED */
1280ea022d16SRodney W. Grimes }
1281ea022d16SRodney W. Grimes 
1282ea022d16SRodney W. Grimes void
1283ea022d16SRodney W. Grimes #if __STDC__
1284ea022d16SRodney W. Grimes reply(int n, const char *fmt, ...)
1285ea022d16SRodney W. Grimes #else
1286ea022d16SRodney W. Grimes reply(n, fmt, va_alist)
1287ea022d16SRodney W. Grimes 	int n;
1288ea022d16SRodney W. Grimes 	char *fmt;
1289ea022d16SRodney W. Grimes         va_dcl
1290ea022d16SRodney W. Grimes #endif
1291ea022d16SRodney W. Grimes {
1292ea022d16SRodney W. Grimes 	va_list ap;
1293ea022d16SRodney W. Grimes #if __STDC__
1294ea022d16SRodney W. Grimes 	va_start(ap, fmt);
1295ea022d16SRodney W. Grimes #else
1296ea022d16SRodney W. Grimes 	va_start(ap);
1297ea022d16SRodney W. Grimes #endif
1298ea022d16SRodney W. Grimes 	(void)printf("%d ", n);
1299ea022d16SRodney W. Grimes 	(void)vprintf(fmt, ap);
1300ea022d16SRodney W. Grimes 	(void)printf("\r\n");
1301ea022d16SRodney W. Grimes 	(void)fflush(stdout);
1302ea022d16SRodney W. Grimes 	if (debug) {
1303ea022d16SRodney W. Grimes 		syslog(LOG_DEBUG, "<--- %d ", n);
1304ea022d16SRodney W. Grimes 		vsyslog(LOG_DEBUG, fmt, ap);
1305ea022d16SRodney W. Grimes 	}
1306ea022d16SRodney W. Grimes }
1307ea022d16SRodney W. Grimes 
1308ea022d16SRodney W. Grimes void
1309ea022d16SRodney W. Grimes #if __STDC__
1310ea022d16SRodney W. Grimes lreply(int n, const char *fmt, ...)
1311ea022d16SRodney W. Grimes #else
1312ea022d16SRodney W. Grimes lreply(n, fmt, va_alist)
1313ea022d16SRodney W. Grimes 	int n;
1314ea022d16SRodney W. Grimes 	char *fmt;
1315ea022d16SRodney W. Grimes         va_dcl
1316ea022d16SRodney W. Grimes #endif
1317ea022d16SRodney W. Grimes {
1318ea022d16SRodney W. Grimes 	va_list ap;
1319ea022d16SRodney W. Grimes #if __STDC__
1320ea022d16SRodney W. Grimes 	va_start(ap, fmt);
1321ea022d16SRodney W. Grimes #else
1322ea022d16SRodney W. Grimes 	va_start(ap);
1323ea022d16SRodney W. Grimes #endif
1324ea022d16SRodney W. Grimes 	(void)printf("%d- ", n);
1325ea022d16SRodney W. Grimes 	(void)vprintf(fmt, ap);
1326ea022d16SRodney W. Grimes 	(void)printf("\r\n");
1327ea022d16SRodney W. Grimes 	(void)fflush(stdout);
1328ea022d16SRodney W. Grimes 	if (debug) {
1329ea022d16SRodney W. Grimes 		syslog(LOG_DEBUG, "<--- %d- ", n);
1330ea022d16SRodney W. Grimes 		vsyslog(LOG_DEBUG, fmt, ap);
1331ea022d16SRodney W. Grimes 	}
1332ea022d16SRodney W. Grimes }
1333ea022d16SRodney W. Grimes 
1334ea022d16SRodney W. Grimes static void
1335ea022d16SRodney W. Grimes ack(s)
1336ea022d16SRodney W. Grimes 	char *s;
1337ea022d16SRodney W. Grimes {
1338ea022d16SRodney W. Grimes 
1339ea022d16SRodney W. Grimes 	reply(250, "%s command successful.", s);
1340ea022d16SRodney W. Grimes }
1341ea022d16SRodney W. Grimes 
1342ea022d16SRodney W. Grimes void
1343ea022d16SRodney W. Grimes nack(s)
1344ea022d16SRodney W. Grimes 	char *s;
1345ea022d16SRodney W. Grimes {
1346ea022d16SRodney W. Grimes 
1347ea022d16SRodney W. Grimes 	reply(502, "%s command not implemented.", s);
1348ea022d16SRodney W. Grimes }
1349ea022d16SRodney W. Grimes 
1350ea022d16SRodney W. Grimes /* ARGSUSED */
1351ea022d16SRodney W. Grimes void
1352ea022d16SRodney W. Grimes yyerror(s)
1353ea022d16SRodney W. Grimes 	char *s;
1354ea022d16SRodney W. Grimes {
1355ea022d16SRodney W. Grimes 	char *cp;
1356ea022d16SRodney W. Grimes 
1357ea022d16SRodney W. Grimes 	if (cp = strchr(cbuf,'\n'))
1358ea022d16SRodney W. Grimes 		*cp = '\0';
1359ea022d16SRodney W. Grimes 	reply(500, "'%s': command not understood.", cbuf);
1360ea022d16SRodney W. Grimes }
1361ea022d16SRodney W. Grimes 
1362ea022d16SRodney W. Grimes void
1363ea022d16SRodney W. Grimes delete(name)
1364ea022d16SRodney W. Grimes 	char *name;
1365ea022d16SRodney W. Grimes {
1366ea022d16SRodney W. Grimes 	struct stat st;
1367ea022d16SRodney W. Grimes 
1368ea022d16SRodney W. Grimes 	LOGCMD("delete", name);
1369ea022d16SRodney W. Grimes 	if (stat(name, &st) < 0) {
1370ea022d16SRodney W. Grimes 		perror_reply(550, name);
1371ea022d16SRodney W. Grimes 		return;
1372ea022d16SRodney W. Grimes 	}
1373ea022d16SRodney W. Grimes 	if ((st.st_mode&S_IFMT) == S_IFDIR) {
1374ea022d16SRodney W. Grimes 		if (rmdir(name) < 0) {
1375ea022d16SRodney W. Grimes 			perror_reply(550, name);
1376ea022d16SRodney W. Grimes 			return;
1377ea022d16SRodney W. Grimes 		}
1378ea022d16SRodney W. Grimes 		goto done;
1379ea022d16SRodney W. Grimes 	}
1380ea022d16SRodney W. Grimes 	if (unlink(name) < 0) {
1381ea022d16SRodney W. Grimes 		perror_reply(550, name);
1382ea022d16SRodney W. Grimes 		return;
1383ea022d16SRodney W. Grimes 	}
1384ea022d16SRodney W. Grimes done:
1385ea022d16SRodney W. Grimes 	ack("DELE");
1386ea022d16SRodney W. Grimes }
1387ea022d16SRodney W. Grimes 
1388ea022d16SRodney W. Grimes void
1389ea022d16SRodney W. Grimes cwd(path)
1390ea022d16SRodney W. Grimes 	char *path;
1391ea022d16SRodney W. Grimes {
1392ea022d16SRodney W. Grimes 
1393ea022d16SRodney W. Grimes 	if (chdir(path) < 0)
1394ea022d16SRodney W. Grimes 		perror_reply(550, path);
1395ea022d16SRodney W. Grimes 	else
1396ea022d16SRodney W. Grimes 		ack("CWD");
1397ea022d16SRodney W. Grimes }
1398ea022d16SRodney W. Grimes 
1399ea022d16SRodney W. Grimes void
1400ea022d16SRodney W. Grimes makedir(name)
1401ea022d16SRodney W. Grimes 	char *name;
1402ea022d16SRodney W. Grimes {
1403ea022d16SRodney W. Grimes 
1404ea022d16SRodney W. Grimes 	LOGCMD("mkdir", name);
1405ea022d16SRodney W. Grimes 	if (mkdir(name, 0777) < 0)
1406ea022d16SRodney W. Grimes 		perror_reply(550, name);
1407ea022d16SRodney W. Grimes 	else
1408ea022d16SRodney W. Grimes 		reply(257, "MKD command successful.");
1409ea022d16SRodney W. Grimes }
1410ea022d16SRodney W. Grimes 
1411ea022d16SRodney W. Grimes void
1412ea022d16SRodney W. Grimes removedir(name)
1413ea022d16SRodney W. Grimes 	char *name;
1414ea022d16SRodney W. Grimes {
1415ea022d16SRodney W. Grimes 
1416ea022d16SRodney W. Grimes 	LOGCMD("rmdir", name);
1417ea022d16SRodney W. Grimes 	if (rmdir(name) < 0)
1418ea022d16SRodney W. Grimes 		perror_reply(550, name);
1419ea022d16SRodney W. Grimes 	else
1420ea022d16SRodney W. Grimes 		ack("RMD");
1421ea022d16SRodney W. Grimes }
1422ea022d16SRodney W. Grimes 
1423ea022d16SRodney W. Grimes void
1424ea022d16SRodney W. Grimes pwd()
1425ea022d16SRodney W. Grimes {
1426ea022d16SRodney W. Grimes 	char path[MAXPATHLEN + 1];
1427ea022d16SRodney W. Grimes 
1428ea022d16SRodney W. Grimes 	if (getwd(path) == (char *)NULL)
1429ea022d16SRodney W. Grimes 		reply(550, "%s.", path);
1430ea022d16SRodney W. Grimes 	else
1431ea022d16SRodney W. Grimes 		reply(257, "\"%s\" is current directory.", path);
1432ea022d16SRodney W. Grimes }
1433ea022d16SRodney W. Grimes 
1434ea022d16SRodney W. Grimes char *
1435ea022d16SRodney W. Grimes renamefrom(name)
1436ea022d16SRodney W. Grimes 	char *name;
1437ea022d16SRodney W. Grimes {
1438ea022d16SRodney W. Grimes 	struct stat st;
1439ea022d16SRodney W. Grimes 
1440ea022d16SRodney W. Grimes 	if (stat(name, &st) < 0) {
1441ea022d16SRodney W. Grimes 		perror_reply(550, name);
1442ea022d16SRodney W. Grimes 		return ((char *)0);
1443ea022d16SRodney W. Grimes 	}
1444ea022d16SRodney W. Grimes 	reply(350, "File exists, ready for destination name");
1445ea022d16SRodney W. Grimes 	return (name);
1446ea022d16SRodney W. Grimes }
1447ea022d16SRodney W. Grimes 
1448ea022d16SRodney W. Grimes void
1449ea022d16SRodney W. Grimes renamecmd(from, to)
1450ea022d16SRodney W. Grimes 	char *from, *to;
1451ea022d16SRodney W. Grimes {
1452ea022d16SRodney W. Grimes 
1453ea022d16SRodney W. Grimes 	LOGCMD2("rename", from, to);
1454ea022d16SRodney W. Grimes 	if (rename(from, to) < 0)
1455ea022d16SRodney W. Grimes 		perror_reply(550, "rename");
1456ea022d16SRodney W. Grimes 	else
1457ea022d16SRodney W. Grimes 		ack("RNTO");
1458ea022d16SRodney W. Grimes }
1459ea022d16SRodney W. Grimes 
1460ea022d16SRodney W. Grimes static void
1461ea022d16SRodney W. Grimes dolog(sin)
1462ea022d16SRodney W. Grimes 	struct sockaddr_in *sin;
1463ea022d16SRodney W. Grimes {
1464ea022d16SRodney W. Grimes 	struct hostent *hp = gethostbyaddr((char *)&sin->sin_addr,
1465ea022d16SRodney W. Grimes 		sizeof(struct in_addr), AF_INET);
1466ea022d16SRodney W. Grimes 
1467ea022d16SRodney W. Grimes 	if (hp)
1468ea022d16SRodney W. Grimes 		(void) strncpy(remotehost, hp->h_name, sizeof(remotehost));
1469ea022d16SRodney W. Grimes 	else
1470ea022d16SRodney W. Grimes 		(void) strncpy(remotehost, inet_ntoa(sin->sin_addr),
1471ea022d16SRodney W. Grimes 		    sizeof(remotehost));
1472ea022d16SRodney W. Grimes #ifdef SETPROCTITLE
1473ea022d16SRodney W. Grimes 	snprintf(proctitle, sizeof(proctitle), "%s: connected", remotehost);
1474b63e1fe2SPeter Wemm 	setproctitle("%s", proctitle);
1475ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */
1476ea022d16SRodney W. Grimes 
1477ea022d16SRodney W. Grimes 	if (logging)
1478ea022d16SRodney W. Grimes 		syslog(LOG_INFO, "connection from %s", remotehost);
1479ea022d16SRodney W. Grimes }
1480ea022d16SRodney W. Grimes 
1481ea022d16SRodney W. Grimes /*
1482ea022d16SRodney W. Grimes  * Record logout in wtmp file
1483ea022d16SRodney W. Grimes  * and exit with supplied status.
1484ea022d16SRodney W. Grimes  */
1485ea022d16SRodney W. Grimes void
1486ea022d16SRodney W. Grimes dologout(status)
1487ea022d16SRodney W. Grimes 	int status;
1488ea022d16SRodney W. Grimes {
1489ea022d16SRodney W. Grimes 
1490ea022d16SRodney W. Grimes 	if (logged_in) {
1491ea022d16SRodney W. Grimes 		(void) seteuid((uid_t)0);
1492ea022d16SRodney W. Grimes 		logwtmp(ttyline, "", "");
1493ea022d16SRodney W. Grimes 	}
1494ea022d16SRodney W. Grimes 	/* beware of flushing buffers after a SIGPIPE */
1495ea022d16SRodney W. Grimes 	_exit(status);
1496ea022d16SRodney W. Grimes }
1497ea022d16SRodney W. Grimes 
1498ea022d16SRodney W. Grimes static void
1499ea022d16SRodney W. Grimes myoob(signo)
1500ea022d16SRodney W. Grimes 	int signo;
1501ea022d16SRodney W. Grimes {
1502ea022d16SRodney W. Grimes 	char *cp;
1503ea022d16SRodney W. Grimes 
1504ea022d16SRodney W. Grimes 	/* only process if transfer occurring */
1505ea022d16SRodney W. Grimes 	if (!transflag)
1506ea022d16SRodney W. Grimes 		return;
1507ea022d16SRodney W. Grimes 	cp = tmpline;
1508ea022d16SRodney W. Grimes 	if (getline(cp, 7, stdin) == NULL) {
1509ea022d16SRodney W. Grimes 		reply(221, "You could at least say goodbye.");
1510ea022d16SRodney W. Grimes 		dologout(0);
1511ea022d16SRodney W. Grimes 	}
1512ea022d16SRodney W. Grimes 	upper(cp);
1513ea022d16SRodney W. Grimes 	if (strcmp(cp, "ABOR\r\n") == 0) {
1514ea022d16SRodney W. Grimes 		tmpline[0] = '\0';
1515ea022d16SRodney W. Grimes 		reply(426, "Transfer aborted. Data connection closed.");
1516ea022d16SRodney W. Grimes 		reply(226, "Abort successful");
1517ea022d16SRodney W. Grimes 		longjmp(urgcatch, 1);
1518ea022d16SRodney W. Grimes 	}
1519ea022d16SRodney W. Grimes 	if (strcmp(cp, "STAT\r\n") == 0) {
1520ea022d16SRodney W. Grimes 		if (file_size != (off_t) -1)
1521ea022d16SRodney W. Grimes 			reply(213, "Status: %qd of %qd bytes transferred",
1522ea022d16SRodney W. Grimes 			    byte_count, file_size);
1523ea022d16SRodney W. Grimes 		else
1524ea022d16SRodney W. Grimes 			reply(213, "Status: %qd bytes transferred", byte_count);
1525ea022d16SRodney W. Grimes 	}
1526ea022d16SRodney W. Grimes }
1527ea022d16SRodney W. Grimes 
1528ea022d16SRodney W. Grimes /*
1529ea022d16SRodney W. Grimes  * Note: a response of 425 is not mentioned as a possible response to
1530ea022d16SRodney W. Grimes  *	the PASV command in RFC959. However, it has been blessed as
1531ea022d16SRodney W. Grimes  *	a legitimate response by Jon Postel in a telephone conversation
1532ea022d16SRodney W. Grimes  *	with Rick Adams on 25 Jan 89.
1533ea022d16SRodney W. Grimes  */
1534ea022d16SRodney W. Grimes void
1535ea022d16SRodney W. Grimes passive()
1536ea022d16SRodney W. Grimes {
1537ea022d16SRodney W. Grimes 	int len;
15384c450ad7SPaul Traina 	u_short port;
1539ea022d16SRodney W. Grimes 	char *p, *a;
1540ea022d16SRodney W. Grimes 
1541ea022d16SRodney W. Grimes 	pdata = socket(AF_INET, SOCK_STREAM, 0);
1542ea022d16SRodney W. Grimes 	if (pdata < 0) {
1543ea022d16SRodney W. Grimes 		perror_reply(425, "Can't open passive connection");
1544ea022d16SRodney W. Grimes 		return;
1545ea022d16SRodney W. Grimes 	}
15464c450ad7SPaul Traina 
15474c450ad7SPaul Traina 	if (restricted_data_ports) {
15484c450ad7SPaul Traina 		for (port = FTP_DATA_BOTTOM; port <= FTP_DATA_TOP; port++) {
15494c450ad7SPaul Traina 			pasv_addr = ctrl_addr;
15504c450ad7SPaul Traina 			pasv_addr.sin_port = htons(port);
15514c450ad7SPaul Traina 			(void) seteuid((uid_t)0);
15524c450ad7SPaul Traina 			if (bind(pdata, (struct sockaddr *)&pasv_addr,
15534c450ad7SPaul Traina 				 sizeof(pasv_addr)) < 0) {
15544c450ad7SPaul Traina 				(void) seteuid((uid_t)pw->pw_uid);
15554c450ad7SPaul Traina 				if (errno == EADDRINUSE)
15564c450ad7SPaul Traina 					continue;
15574c450ad7SPaul Traina 				else
15584c450ad7SPaul Traina 					goto pasv_error;
15594c450ad7SPaul Traina 			}
15604c450ad7SPaul Traina 			(void) seteuid((uid_t)pw->pw_uid);
15614c450ad7SPaul Traina 			break;
15624c450ad7SPaul Traina 		}
15634c450ad7SPaul Traina 		if (port > FTP_DATA_TOP)
15644c450ad7SPaul Traina 			goto pasv_error;
15654c450ad7SPaul Traina 	} else {
1566ea022d16SRodney W. Grimes 		pasv_addr = ctrl_addr;
1567ea022d16SRodney W. Grimes 		pasv_addr.sin_port = 0;
1568ea022d16SRodney W. Grimes 		(void) seteuid((uid_t)0);
15694c450ad7SPaul Traina 		if (bind(pdata, (struct sockaddr *)&pasv_addr,
15704c450ad7SPaul Traina 			 sizeof(pasv_addr)) < 0) {
1571ea022d16SRodney W. Grimes 			(void) seteuid((uid_t)pw->pw_uid);
1572ea022d16SRodney W. Grimes 			goto pasv_error;
1573ea022d16SRodney W. Grimes 		}
1574ea022d16SRodney W. Grimes 		(void) seteuid((uid_t)pw->pw_uid);
15754c450ad7SPaul Traina 	}
15764c450ad7SPaul Traina 
1577ea022d16SRodney W. Grimes 	len = sizeof(pasv_addr);
1578ea022d16SRodney W. Grimes 	if (getsockname(pdata, (struct sockaddr *) &pasv_addr, &len) < 0)
1579ea022d16SRodney W. Grimes 		goto pasv_error;
1580ea022d16SRodney W. Grimes 	if (listen(pdata, 1) < 0)
1581ea022d16SRodney W. Grimes 		goto pasv_error;
1582ea022d16SRodney W. Grimes 	a = (char *) &pasv_addr.sin_addr;
1583ea022d16SRodney W. Grimes 	p = (char *) &pasv_addr.sin_port;
1584ea022d16SRodney W. Grimes 
1585ea022d16SRodney W. Grimes #define UC(b) (((int) b) & 0xff)
1586ea022d16SRodney W. Grimes 
1587ea022d16SRodney W. Grimes 	reply(227, "Entering Passive Mode (%d,%d,%d,%d,%d,%d)", UC(a[0]),
1588ea022d16SRodney W. Grimes 		UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1]));
1589ea022d16SRodney W. Grimes 	return;
1590ea022d16SRodney W. Grimes 
1591ea022d16SRodney W. Grimes pasv_error:
1592ea022d16SRodney W. Grimes 	(void) close(pdata);
1593ea022d16SRodney W. Grimes 	pdata = -1;
1594ea022d16SRodney W. Grimes 	perror_reply(425, "Can't open passive connection");
1595ea022d16SRodney W. Grimes 	return;
1596ea022d16SRodney W. Grimes }
1597ea022d16SRodney W. Grimes 
1598ea022d16SRodney W. Grimes /*
1599ea022d16SRodney W. Grimes  * Generate unique name for file with basename "local".
1600ea022d16SRodney W. Grimes  * The file named "local" is already known to exist.
1601ea022d16SRodney W. Grimes  * Generates failure reply on error.
1602ea022d16SRodney W. Grimes  */
1603ea022d16SRodney W. Grimes static char *
1604ea022d16SRodney W. Grimes gunique(local)
1605ea022d16SRodney W. Grimes 	char *local;
1606ea022d16SRodney W. Grimes {
1607ea022d16SRodney W. Grimes 	static char new[MAXPATHLEN];
1608ea022d16SRodney W. Grimes 	struct stat st;
1609ea022d16SRodney W. Grimes 	int count;
1610ea022d16SRodney W. Grimes 	char *cp;
1611ea022d16SRodney W. Grimes 
1612ea022d16SRodney W. Grimes 	cp = strrchr(local, '/');
1613ea022d16SRodney W. Grimes 	if (cp)
1614ea022d16SRodney W. Grimes 		*cp = '\0';
1615ea022d16SRodney W. Grimes 	if (stat(cp ? local : ".", &st) < 0) {
1616ea022d16SRodney W. Grimes 		perror_reply(553, cp ? local : ".");
1617ea022d16SRodney W. Grimes 		return ((char *) 0);
1618ea022d16SRodney W. Grimes 	}
1619ea022d16SRodney W. Grimes 	if (cp)
1620ea022d16SRodney W. Grimes 		*cp = '/';
1621ea022d16SRodney W. Grimes 	(void) strcpy(new, local);
1622ea022d16SRodney W. Grimes 	cp = new + strlen(new);
1623ea022d16SRodney W. Grimes 	*cp++ = '.';
1624ea022d16SRodney W. Grimes 	for (count = 1; count < 100; count++) {
1625ea022d16SRodney W. Grimes 		(void)sprintf(cp, "%d", count);
1626ea022d16SRodney W. Grimes 		if (stat(new, &st) < 0)
1627ea022d16SRodney W. Grimes 			return (new);
1628ea022d16SRodney W. Grimes 	}
1629ea022d16SRodney W. Grimes 	reply(452, "Unique file name cannot be created.");
1630ea022d16SRodney W. Grimes 	return (NULL);
1631ea022d16SRodney W. Grimes }
1632ea022d16SRodney W. Grimes 
1633ea022d16SRodney W. Grimes /*
1634ea022d16SRodney W. Grimes  * Format and send reply containing system error number.
1635ea022d16SRodney W. Grimes  */
1636ea022d16SRodney W. Grimes void
1637ea022d16SRodney W. Grimes perror_reply(code, string)
1638ea022d16SRodney W. Grimes 	int code;
1639ea022d16SRodney W. Grimes 	char *string;
1640ea022d16SRodney W. Grimes {
1641ea022d16SRodney W. Grimes 
1642ea022d16SRodney W. Grimes 	reply(code, "%s: %s.", string, strerror(errno));
1643ea022d16SRodney W. Grimes }
1644ea022d16SRodney W. Grimes 
1645ea022d16SRodney W. Grimes static char *onefile[] = {
1646ea022d16SRodney W. Grimes 	"",
1647ea022d16SRodney W. Grimes 	0
1648ea022d16SRodney W. Grimes };
1649ea022d16SRodney W. Grimes 
1650ea022d16SRodney W. Grimes void
1651ea022d16SRodney W. Grimes send_file_list(whichf)
1652ea022d16SRodney W. Grimes 	char *whichf;
1653ea022d16SRodney W. Grimes {
1654ea022d16SRodney W. Grimes 	struct stat st;
1655ea022d16SRodney W. Grimes 	DIR *dirp = NULL;
1656ea022d16SRodney W. Grimes 	struct dirent *dir;
1657ea022d16SRodney W. Grimes 	FILE *dout = NULL;
1658ea022d16SRodney W. Grimes 	char **dirlist, *dirname;
1659ea022d16SRodney W. Grimes 	int simple = 0;
1660ea022d16SRodney W. Grimes 	int freeglob = 0;
1661ea022d16SRodney W. Grimes 	glob_t gl;
1662ea022d16SRodney W. Grimes 
1663ea022d16SRodney W. Grimes 	if (strpbrk(whichf, "~{[*?") != NULL) {
1664ea022d16SRodney W. Grimes 		int flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_QUOTE|GLOB_TILDE;
1665ea022d16SRodney W. Grimes 
1666ea022d16SRodney W. Grimes 		memset(&gl, 0, sizeof(gl));
1667ea022d16SRodney W. Grimes 		freeglob = 1;
1668ea022d16SRodney W. Grimes 		if (glob(whichf, flags, 0, &gl)) {
1669ea022d16SRodney W. Grimes 			reply(550, "not found");
1670ea022d16SRodney W. Grimes 			goto out;
1671ea022d16SRodney W. Grimes 		} else if (gl.gl_pathc == 0) {
1672ea022d16SRodney W. Grimes 			errno = ENOENT;
1673ea022d16SRodney W. Grimes 			perror_reply(550, whichf);
1674ea022d16SRodney W. Grimes 			goto out;
1675ea022d16SRodney W. Grimes 		}
1676ea022d16SRodney W. Grimes 		dirlist = gl.gl_pathv;
1677ea022d16SRodney W. Grimes 	} else {
1678ea022d16SRodney W. Grimes 		onefile[0] = whichf;
1679ea022d16SRodney W. Grimes 		dirlist = onefile;
1680ea022d16SRodney W. Grimes 		simple = 1;
1681ea022d16SRodney W. Grimes 	}
1682ea022d16SRodney W. Grimes 
1683ea022d16SRodney W. Grimes 	if (setjmp(urgcatch)) {
1684ea022d16SRodney W. Grimes 		transflag = 0;
1685ea022d16SRodney W. Grimes 		goto out;
1686ea022d16SRodney W. Grimes 	}
1687ea022d16SRodney W. Grimes 	while (dirname = *dirlist++) {
1688ea022d16SRodney W. Grimes 		if (stat(dirname, &st) < 0) {
1689ea022d16SRodney W. Grimes 			/*
1690ea022d16SRodney W. Grimes 			 * If user typed "ls -l", etc, and the client
1691ea022d16SRodney W. Grimes 			 * used NLST, do what the user meant.
1692ea022d16SRodney W. Grimes 			 */
1693ea022d16SRodney W. Grimes 			if (dirname[0] == '-' && *dirlist == NULL &&
1694ea022d16SRodney W. Grimes 			    transflag == 0) {
1695ea022d16SRodney W. Grimes 				retrieve("/bin/ls %s", dirname);
1696ea022d16SRodney W. Grimes 				goto out;
1697ea022d16SRodney W. Grimes 			}
1698ea022d16SRodney W. Grimes 			perror_reply(550, whichf);
1699ea022d16SRodney W. Grimes 			if (dout != NULL) {
1700ea022d16SRodney W. Grimes 				(void) fclose(dout);
1701ea022d16SRodney W. Grimes 				transflag = 0;
1702ea022d16SRodney W. Grimes 				data = -1;
1703ea022d16SRodney W. Grimes 				pdata = -1;
1704ea022d16SRodney W. Grimes 			}
1705ea022d16SRodney W. Grimes 			goto out;
1706ea022d16SRodney W. Grimes 		}
1707ea022d16SRodney W. Grimes 
1708ea022d16SRodney W. Grimes 		if (S_ISREG(st.st_mode)) {
1709ea022d16SRodney W. Grimes 			if (dout == NULL) {
1710ea022d16SRodney W. Grimes 				dout = dataconn("file list", (off_t)-1, "w");
1711ea022d16SRodney W. Grimes 				if (dout == NULL)
1712ea022d16SRodney W. Grimes 					goto out;
1713ea022d16SRodney W. Grimes 				transflag++;
1714ea022d16SRodney W. Grimes 			}
1715ea022d16SRodney W. Grimes 			fprintf(dout, "%s%s\n", dirname,
1716ea022d16SRodney W. Grimes 				type == TYPE_A ? "\r" : "");
1717ea022d16SRodney W. Grimes 			byte_count += strlen(dirname) + 1;
1718ea022d16SRodney W. Grimes 			continue;
1719ea022d16SRodney W. Grimes 		} else if (!S_ISDIR(st.st_mode))
1720ea022d16SRodney W. Grimes 			continue;
1721ea022d16SRodney W. Grimes 
1722ea022d16SRodney W. Grimes 		if ((dirp = opendir(dirname)) == NULL)
1723ea022d16SRodney W. Grimes 			continue;
1724ea022d16SRodney W. Grimes 
1725ea022d16SRodney W. Grimes 		while ((dir = readdir(dirp)) != NULL) {
1726ea022d16SRodney W. Grimes 			char nbuf[MAXPATHLEN];
1727ea022d16SRodney W. Grimes 
1728ea022d16SRodney W. Grimes 			if (dir->d_name[0] == '.' && dir->d_namlen == 1)
1729ea022d16SRodney W. Grimes 				continue;
1730ea022d16SRodney W. Grimes 			if (dir->d_name[0] == '.' && dir->d_name[1] == '.' &&
1731ea022d16SRodney W. Grimes 			    dir->d_namlen == 2)
1732ea022d16SRodney W. Grimes 				continue;
1733ea022d16SRodney W. Grimes 
1734ea022d16SRodney W. Grimes 			sprintf(nbuf, "%s/%s", dirname, dir->d_name);
1735ea022d16SRodney W. Grimes 
1736ea022d16SRodney W. Grimes 			/*
1737ea022d16SRodney W. Grimes 			 * We have to do a stat to insure it's
1738ea022d16SRodney W. Grimes 			 * not a directory or special file.
1739ea022d16SRodney W. Grimes 			 */
1740ea022d16SRodney W. Grimes 			if (simple || (stat(nbuf, &st) == 0 &&
1741ea022d16SRodney W. Grimes 			    S_ISREG(st.st_mode))) {
1742ea022d16SRodney W. Grimes 				if (dout == NULL) {
1743ea022d16SRodney W. Grimes 					dout = dataconn("file list", (off_t)-1,
1744ea022d16SRodney W. Grimes 						"w");
1745ea022d16SRodney W. Grimes 					if (dout == NULL)
1746ea022d16SRodney W. Grimes 						goto out;
1747ea022d16SRodney W. Grimes 					transflag++;
1748ea022d16SRodney W. Grimes 				}
1749ea022d16SRodney W. Grimes 				if (nbuf[0] == '.' && nbuf[1] == '/')
1750ea022d16SRodney W. Grimes 					fprintf(dout, "%s%s\n", &nbuf[2],
1751ea022d16SRodney W. Grimes 						type == TYPE_A ? "\r" : "");
1752ea022d16SRodney W. Grimes 				else
1753ea022d16SRodney W. Grimes 					fprintf(dout, "%s%s\n", nbuf,
1754ea022d16SRodney W. Grimes 						type == TYPE_A ? "\r" : "");
1755ea022d16SRodney W. Grimes 				byte_count += strlen(nbuf) + 1;
1756ea022d16SRodney W. Grimes 			}
1757ea022d16SRodney W. Grimes 		}
1758ea022d16SRodney W. Grimes 		(void) closedir(dirp);
1759ea022d16SRodney W. Grimes 	}
1760ea022d16SRodney W. Grimes 
1761ea022d16SRodney W. Grimes 	if (dout == NULL)
1762ea022d16SRodney W. Grimes 		reply(550, "No files found.");
1763ea022d16SRodney W. Grimes 	else if (ferror(dout) != 0)
1764ea022d16SRodney W. Grimes 		perror_reply(550, "Data connection");
1765ea022d16SRodney W. Grimes 	else
1766ea022d16SRodney W. Grimes 		reply(226, "Transfer complete.");
1767ea022d16SRodney W. Grimes 
1768ea022d16SRodney W. Grimes 	transflag = 0;
1769ea022d16SRodney W. Grimes 	if (dout != NULL)
1770ea022d16SRodney W. Grimes 		(void) fclose(dout);
1771ea022d16SRodney W. Grimes 	data = -1;
1772ea022d16SRodney W. Grimes 	pdata = -1;
1773ea022d16SRodney W. Grimes out:
1774ea022d16SRodney W. Grimes 	if (freeglob) {
1775ea022d16SRodney W. Grimes 		freeglob = 0;
1776ea022d16SRodney W. Grimes 		globfree(&gl);
1777ea022d16SRodney W. Grimes 	}
1778ea022d16SRodney W. Grimes }
1779ea022d16SRodney W. Grimes 
1780b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE
1781ea022d16SRodney W. Grimes /*
1782ea022d16SRodney W. Grimes  * Clobber argv so ps will show what we're doing.  (Stolen from sendmail.)
1783ea022d16SRodney W. Grimes  * Warning, since this is usually started from inetd.conf, it often doesn't
1784ea022d16SRodney W. Grimes  * have much of an environment or arglist to overwrite.
1785ea022d16SRodney W. Grimes  */
1786ea022d16SRodney W. Grimes void
1787ea022d16SRodney W. Grimes #if __STDC__
1788ea022d16SRodney W. Grimes setproctitle(const char *fmt, ...)
1789ea022d16SRodney W. Grimes #else
1790ea022d16SRodney W. Grimes setproctitle(fmt, va_alist)
1791ea022d16SRodney W. Grimes 	char *fmt;
1792ea022d16SRodney W. Grimes         va_dcl
1793ea022d16SRodney W. Grimes #endif
1794ea022d16SRodney W. Grimes {
1795ea022d16SRodney W. Grimes 	int i;
1796ea022d16SRodney W. Grimes 	va_list ap;
1797ea022d16SRodney W. Grimes 	char *p, *bp, ch;
1798ea022d16SRodney W. Grimes 	char buf[LINE_MAX];
1799ea022d16SRodney W. Grimes 
1800ea022d16SRodney W. Grimes #if __STDC__
1801ea022d16SRodney W. Grimes 	va_start(ap, fmt);
1802ea022d16SRodney W. Grimes #else
1803ea022d16SRodney W. Grimes 	va_start(ap);
1804ea022d16SRodney W. Grimes #endif
1805ea022d16SRodney W. Grimes 	(void)vsnprintf(buf, sizeof(buf), fmt, ap);
1806ea022d16SRodney W. Grimes 
1807ea022d16SRodney W. Grimes 	/* make ps print our process name */
1808ea022d16SRodney W. Grimes 	p = Argv[0];
1809ea022d16SRodney W. Grimes 	*p++ = '-';
1810ea022d16SRodney W. Grimes 
1811ea022d16SRodney W. Grimes 	i = strlen(buf);
1812ea022d16SRodney W. Grimes 	if (i > LastArgv - p - 2) {
1813ea022d16SRodney W. Grimes 		i = LastArgv - p - 2;
1814ea022d16SRodney W. Grimes 		buf[i] = '\0';
1815ea022d16SRodney W. Grimes 	}
1816ea022d16SRodney W. Grimes 	bp = buf;
1817ea022d16SRodney W. Grimes 	while (ch = *bp++)
1818ea022d16SRodney W. Grimes 		if (ch != '\n' && ch != '\r')
1819ea022d16SRodney W. Grimes 			*p++ = ch;
1820ea022d16SRodney W. Grimes 	while (p < LastArgv)
1821ea022d16SRodney W. Grimes 		*p++ = ' ';
1822ea022d16SRodney W. Grimes }
1823b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */
18243eb568f2SGuido van Rooij 
18253eb568f2SGuido van Rooij #ifdef STATS
18263eb568f2SGuido van Rooij logxfer(name, size, start)
18273eb568f2SGuido van Rooij 	char *name;
18283eb568f2SGuido van Rooij 	long size;
18293eb568f2SGuido van Rooij 	long start;
18303eb568f2SGuido van Rooij {
18313eb568f2SGuido van Rooij 	char buf[1024];
18323eb568f2SGuido van Rooij 	char path[MAXPATHLEN + 1];
18333eb568f2SGuido van Rooij 	long now;
18343eb568f2SGuido van Rooij 
18353eb568f2SGuido van Rooij 	if (statfd >= 0 && getwd(path) != NULL) {
18363eb568f2SGuido van Rooij 		time(&now);
18373eb568f2SGuido van Rooij 		sprintf(buf, "%.20s!%s!%s!%s/%s!%ld!%ld\n",
18383eb568f2SGuido van Rooij 			ctime(&now)+4, ident, remotehost,
18393eb568f2SGuido van Rooij 			path, name, size, now - start + (now == start));
18403eb568f2SGuido van Rooij 		write(statfd, buf, strlen(buf));
18413eb568f2SGuido van Rooij 	}
18423eb568f2SGuido van Rooij }
18433eb568f2SGuido van Rooij 
18443eb568f2SGuido van Rooij char *
18453eb568f2SGuido van Rooij copy(s)
18463eb568f2SGuido van Rooij 	char *s;
18473eb568f2SGuido van Rooij {
18483eb568f2SGuido van Rooij 	char *p;
18493eb568f2SGuido van Rooij 
18503eb568f2SGuido van Rooij 	p = malloc((unsigned) strlen(s) + 1);
18513eb568f2SGuido van Rooij 	if (p == NULL)
18523eb568f2SGuido van Rooij 		fatal("Ran out of memory.");
18533eb568f2SGuido van Rooij 	(void) strcpy(p, s);
18543eb568f2SGuido van Rooij 	return (p);
18553eb568f2SGuido van Rooij }
18563eb568f2SGuido van Rooij #endif
1857