xref: /freebsd/libexec/ftpd/ftpd.c (revision f38c6cadf95c64f3eebc866af2bd9cbe851edc46)
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.
32ea022d16SRodney W. Grimes  */
33ea022d16SRodney W. Grimes 
349aca17cbSMark Murray #if 0
35ea022d16SRodney W. Grimes #ifndef lint
36ea022d16SRodney W. Grimes static char copyright[] =
37ea022d16SRodney W. Grimes "@(#) Copyright (c) 1985, 1988, 1990, 1992, 1993, 1994\n\
38ea022d16SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
39ea022d16SRodney W. Grimes #endif /* not lint */
409aca17cbSMark Murray #endif
41ea022d16SRodney W. Grimes 
42ea022d16SRodney W. Grimes #ifndef lint
43e02897faSPhilippe Charnier #if 0
44ea022d16SRodney W. Grimes static char sccsid[] = "@(#)ftpd.c	8.4 (Berkeley) 4/16/94";
459aca17cbSMark Murray #endif
46e02897faSPhilippe Charnier static const char rcsid[] =
477f3dea24SPeter Wemm   "$FreeBSD$";
48e02897faSPhilippe Charnier #endif /* not lint */
49ea022d16SRodney W. Grimes 
50ea022d16SRodney W. Grimes /*
51ea022d16SRodney W. Grimes  * FTP server.
52ea022d16SRodney W. Grimes  */
53ea022d16SRodney W. Grimes #include <sys/param.h>
54ea022d16SRodney W. Grimes #include <sys/stat.h>
55ea022d16SRodney W. Grimes #include <sys/ioctl.h>
56ea022d16SRodney W. Grimes #include <sys/socket.h>
57ea022d16SRodney W. Grimes #include <sys/wait.h>
589fc5823aSGarrett Wollman #include <sys/mman.h>
59ea022d16SRodney W. Grimes 
60ea022d16SRodney W. Grimes #include <netinet/in.h>
61ea022d16SRodney W. Grimes #include <netinet/in_systm.h>
62ea022d16SRodney W. Grimes #include <netinet/ip.h>
639fc5823aSGarrett Wollman #include <netinet/tcp.h>
64ea022d16SRodney W. Grimes 
65ea022d16SRodney W. Grimes #define	FTP_NAMES
66ea022d16SRodney W. Grimes #include <arpa/ftp.h>
67ea022d16SRodney W. Grimes #include <arpa/inet.h>
68ea022d16SRodney W. Grimes #include <arpa/telnet.h>
69ea022d16SRodney W. Grimes 
70ea022d16SRodney W. Grimes #include <ctype.h>
71ea022d16SRodney W. Grimes #include <dirent.h>
72ea022d16SRodney W. Grimes #include <err.h>
73ea022d16SRodney W. Grimes #include <errno.h>
74ea022d16SRodney W. Grimes #include <fcntl.h>
75ea022d16SRodney W. Grimes #include <glob.h>
76ea022d16SRodney W. Grimes #include <limits.h>
77ea022d16SRodney W. Grimes #include <netdb.h>
78ea022d16SRodney W. Grimes #include <pwd.h>
7931fea7b8SDavid Nugent #include <grp.h>
80ea022d16SRodney W. Grimes #include <setjmp.h>
81ea022d16SRodney W. Grimes #include <signal.h>
82ea022d16SRodney W. Grimes #include <stdio.h>
83ea022d16SRodney W. Grimes #include <stdlib.h>
84ea022d16SRodney W. Grimes #include <string.h>
85ea022d16SRodney W. Grimes #include <syslog.h>
86ea022d16SRodney W. Grimes #include <time.h>
87ea022d16SRodney W. Grimes #include <unistd.h>
88b63e1fe2SPeter Wemm #include <libutil.h>
89b071c689SDavid Nugent #ifdef	LOGIN_CAP
90b071c689SDavid Nugent #include <login_cap.h>
91b071c689SDavid Nugent #endif
92ea022d16SRodney W. Grimes 
932c60c54cSPaul Traina #ifdef	SKEY
942c60c54cSPaul Traina #include <skey.h>
952c60c54cSPaul Traina #endif
962c60c54cSPaul Traina 
976c9134c0SMark Murray #if !defined(NOPAM)
986c9134c0SMark Murray #include <security/pam_appl.h>
996c9134c0SMark Murray #endif
1006c9134c0SMark Murray 
101ea022d16SRodney W. Grimes #include "pathnames.h"
102ea022d16SRodney W. Grimes #include "extern.h"
103ea022d16SRodney W. Grimes 
104ea022d16SRodney W. Grimes #if __STDC__
105ea022d16SRodney W. Grimes #include <stdarg.h>
106ea022d16SRodney W. Grimes #else
107ea022d16SRodney W. Grimes #include <varargs.h>
108ea022d16SRodney W. Grimes #endif
109ea022d16SRodney W. Grimes 
110af85d782SDavid Nugent static char version[] = "Version 6.00LS";
111af85d782SDavid Nugent #undef main
112ea022d16SRodney W. Grimes 
1134dd8b5abSYoshinobu Inoue /* wrapper for KAME-special getnameinfo() */
1144dd8b5abSYoshinobu Inoue #ifndef NI_WITHSCOPEID
1154dd8b5abSYoshinobu Inoue #define	NI_WITHSCOPEID	0
1164dd8b5abSYoshinobu Inoue #endif
1174dd8b5abSYoshinobu Inoue 
118ea022d16SRodney W. Grimes extern	off_t restart_point;
119ea022d16SRodney W. Grimes extern	char cbuf[];
120ea022d16SRodney W. Grimes 
1214dd8b5abSYoshinobu Inoue union sockunion server_addr;
1224dd8b5abSYoshinobu Inoue union sockunion ctrl_addr;
1234dd8b5abSYoshinobu Inoue union sockunion data_source;
1244dd8b5abSYoshinobu Inoue union sockunion data_dest;
1254dd8b5abSYoshinobu Inoue union sockunion his_addr;
1264dd8b5abSYoshinobu Inoue union sockunion pasv_addr;
127ea022d16SRodney W. Grimes 
128cf09a206SDavid Greenman int	daemon_mode;
129ea022d16SRodney W. Grimes int	data;
130ea022d16SRodney W. Grimes jmp_buf	errcatch, urgcatch;
131ea022d16SRodney W. Grimes int	logged_in;
132ea022d16SRodney W. Grimes struct	passwd *pw;
133ea022d16SRodney W. Grimes int	debug;
134ea022d16SRodney W. Grimes int	timeout = 900;    /* timeout after 15 minutes of inactivity */
135ea022d16SRodney W. Grimes int	maxtimeout = 7200;/* don't allow idle time to be set beyond 2 hours */
136ea022d16SRodney W. Grimes int	logging;
1374c450ad7SPaul Traina int	restricted_data_ports = 1;
138a5a4544eSPaul Traina int	paranoid = 1;	  /* be extra careful about security */
1395a392aecSTorsten Blum int	anon_only = 0;    /* Only anonymous ftp allowed */
140ea022d16SRodney W. Grimes int	guest;
141a5a4544eSPaul Traina int	dochroot;
1423eb568f2SGuido van Rooij int	stats;
1433eb568f2SGuido van Rooij int	statfd = -1;
144ea022d16SRodney W. Grimes int	type;
145ea022d16SRodney W. Grimes int	form;
146ea022d16SRodney W. Grimes int	stru;			/* avoid C keyword */
147ea022d16SRodney W. Grimes int	mode;
148ea022d16SRodney W. Grimes int	usedefault = 1;		/* for data transfers */
149ea022d16SRodney W. Grimes int	pdata = -1;		/* for passive mode */
150ea022d16SRodney W. Grimes sig_atomic_t transflag;
151ea022d16SRodney W. Grimes off_t	file_size;
152ea022d16SRodney W. Grimes off_t	byte_count;
153ea022d16SRodney W. Grimes #if !defined(CMASK) || CMASK == 0
154ea022d16SRodney W. Grimes #undef CMASK
155ea022d16SRodney W. Grimes #define CMASK 027
156ea022d16SRodney W. Grimes #endif
157ea022d16SRodney W. Grimes int	defumask = CMASK;		/* default umask value */
158ea022d16SRodney W. Grimes char	tmpline[7];
159ea4e54b9SDavid Nugent char	*hostname;
1600512556aSDavid Nugent #ifdef VIRTUAL_HOSTING
161ea4e54b9SDavid Nugent char	*ftpuser;
162ea4e54b9SDavid Nugent 
1634dd8b5abSYoshinobu Inoue int	epsvall = 0;
1644dd8b5abSYoshinobu Inoue 
165ea4e54b9SDavid Nugent static struct ftphost {
166ea4e54b9SDavid Nugent 	struct ftphost	*next;
167f38c6cadSYoshinobu Inoue 	struct addrinfo *hostinfo;
168ea4e54b9SDavid Nugent 	char		*hostname;
169ea4e54b9SDavid Nugent 	char		*anonuser;
170ea4e54b9SDavid Nugent 	char		*statfile;
171ea4e54b9SDavid Nugent 	char		*welcome;
172ea4e54b9SDavid Nugent 	char		*loginmsg;
173ea4e54b9SDavid Nugent } *thishost, *firsthost;
174ea4e54b9SDavid Nugent 
175ea4e54b9SDavid Nugent #endif
1769e9a43bdSBrian Somers char	remotehost[MAXHOSTNAMELEN];
1773eb568f2SGuido van Rooij char	*ident = NULL;
178a5a4544eSPaul Traina 
179a5a4544eSPaul Traina static char ttyline[20];
180a5a4544eSPaul Traina char	*tty = ttyline;		/* for klogin */
181a5a4544eSPaul Traina 
1826c9134c0SMark Murray #if !defined(NOPAM)
1836c9134c0SMark Murray static int	auth_pam __P((struct passwd**, const char*));
1849aca17cbSMark Murray #endif
1859aca17cbSMark Murray 
186105a3c98SJulian Elischer char	*pid_file = NULL;
187105a3c98SJulian Elischer 
188ea022d16SRodney W. Grimes /*
189ea022d16SRodney W. Grimes  * Timeout intervals for retrying connections
190ea022d16SRodney W. Grimes  * to hosts that don't accept PORT cmds.  This
191ea022d16SRodney W. Grimes  * is a kludge, but given the problems with TCP...
192ea022d16SRodney W. Grimes  */
193ea022d16SRodney W. Grimes #define	SWAITMAX	90	/* wait at most 90 seconds */
194ea022d16SRodney W. Grimes #define	SWAITINT	5	/* interval between retries */
195ea022d16SRodney W. Grimes 
196ea022d16SRodney W. Grimes int	swaitmax = SWAITMAX;
197ea022d16SRodney W. Grimes int	swaitint = SWAITINT;
198ea022d16SRodney W. Grimes 
199ea022d16SRodney W. Grimes #ifdef SETPROCTITLE
200b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE
201ea022d16SRodney W. Grimes char	**Argv = NULL;		/* pointer to argument vector */
202ea022d16SRodney W. Grimes char	*LastArgv = NULL;	/* end of argv */
203b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */
204ea022d16SRodney W. Grimes char	proctitle[LINE_MAX];	/* initial part of title */
205ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */
206ea022d16SRodney W. Grimes 
207726040deSGuido van Rooij #ifdef SKEY
208726040deSGuido van Rooij int	pwok = 0;
2094dd8b5abSYoshinobu Inoue char	addr_string[INET6_ADDRSTRLEN];	/* XXX */
210726040deSGuido van Rooij #endif
211726040deSGuido van Rooij 
212ea022d16SRodney W. Grimes #define LOGCMD(cmd, file) \
213ea022d16SRodney W. Grimes 	if (logging > 1) \
214ea022d16SRodney W. Grimes 	    syslog(LOG_INFO,"%s %s%s", cmd, \
215ea022d16SRodney W. Grimes 		*(file) == '/' ? "" : curdir(), file);
216ea022d16SRodney W. Grimes #define LOGCMD2(cmd, file1, file2) \
217ea022d16SRodney W. Grimes 	 if (logging > 1) \
218ea022d16SRodney W. Grimes 	    syslog(LOG_INFO,"%s %s%s %s%s", cmd, \
219ea022d16SRodney W. Grimes 		*(file1) == '/' ? "" : curdir(), file1, \
220ea022d16SRodney W. Grimes 		*(file2) == '/' ? "" : curdir(), file2);
221ea022d16SRodney W. Grimes #define LOGBYTES(cmd, file, cnt) \
222ea022d16SRodney W. Grimes 	if (logging > 1) { \
223ea022d16SRodney W. Grimes 		if (cnt == (off_t)-1) \
224ea022d16SRodney W. Grimes 		    syslog(LOG_INFO,"%s %s%s", cmd, \
225ea022d16SRodney W. Grimes 			*(file) == '/' ? "" : curdir(), file); \
226ea022d16SRodney W. Grimes 		else \
227ea022d16SRodney W. Grimes 		    syslog(LOG_INFO, "%s %s%s = %qd bytes", \
228ea022d16SRodney W. Grimes 			cmd, (*(file) == '/') ? "" : curdir(), file, cnt); \
229ea022d16SRodney W. Grimes 	}
230ea022d16SRodney W. Grimes 
231ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
232ea4e54b9SDavid Nugent static void	 inithosts __P((void));
2334dd8b5abSYoshinobu Inoue static void	selecthost __P((union sockunion *));
234ea4e54b9SDavid Nugent #endif
235ea022d16SRodney W. Grimes static void	 ack __P((char *));
236ea022d16SRodney W. Grimes static void	 myoob __P((int));
2377edcb936SSteve Price static int	 checkuser __P((char *, char *, int));
238ea022d16SRodney W. Grimes static FILE	*dataconn __P((char *, off_t, char *));
2394dd8b5abSYoshinobu Inoue static void	 dolog __P((struct sockaddr *));
240ea022d16SRodney W. Grimes static char	*curdir __P((void));
241ea022d16SRodney W. Grimes static void	 end_login __P((void));
242ea022d16SRodney W. Grimes static FILE	*getdatasock __P((char *));
243ea022d16SRodney W. Grimes static char	*gunique __P((char *));
244ea022d16SRodney W. Grimes static void	 lostconn __P((int));
245ea022d16SRodney W. Grimes static int	 receive_data __P((FILE *, FILE *));
2469fc5823aSGarrett Wollman static void	 send_data __P((FILE *, FILE *, off_t, off_t, int));
247ea022d16SRodney W. Grimes static struct passwd *
248ea022d16SRodney W. Grimes 		 sgetpwnam __P((char *));
249ea022d16SRodney W. Grimes static char	*sgetsave __P((char *));
250cf09a206SDavid Greenman static void	 reapchild __P((int));
25161f891a6SPaul Traina static void      logxfer __P((char *, long, long));
252ea022d16SRodney W. Grimes 
253ea022d16SRodney W. Grimes static char *
254ea022d16SRodney W. Grimes curdir()
255ea022d16SRodney W. Grimes {
256ea022d16SRodney W. Grimes 	static char path[MAXPATHLEN+1+1];	/* path + '/' + '\0' */
257ea022d16SRodney W. Grimes 
258ea022d16SRodney W. Grimes 	if (getcwd(path, sizeof(path)-2) == NULL)
259ea022d16SRodney W. Grimes 		return ("");
260ea022d16SRodney W. Grimes 	if (path[1] != '\0')		/* special case for root dir. */
261ea022d16SRodney W. Grimes 		strcat(path, "/");
262ea022d16SRodney W. Grimes 	/* For guest account, skip / since it's chrooted */
263ea022d16SRodney W. Grimes 	return (guest ? path+1 : path);
264ea022d16SRodney W. Grimes }
265ea022d16SRodney W. Grimes 
266ea022d16SRodney W. Grimes int
267ea022d16SRodney W. Grimes main(argc, argv, envp)
268ea022d16SRodney W. Grimes 	int argc;
269ea022d16SRodney W. Grimes 	char *argv[];
270ea022d16SRodney W. Grimes 	char **envp;
271ea022d16SRodney W. Grimes {
272ea022d16SRodney W. Grimes 	int addrlen, ch, on = 1, tos;
273ea022d16SRodney W. Grimes 	char *cp, line[LINE_MAX];
274ea022d16SRodney W. Grimes 	FILE *fd;
2754dd8b5abSYoshinobu Inoue 	int error;
2764dd8b5abSYoshinobu Inoue 	char	*bindname = NULL;
2774dd8b5abSYoshinobu Inoue 	int	family = AF_UNSPEC;
2784dd8b5abSYoshinobu Inoue 	int	enable_v4 = 0;
279ea022d16SRodney W. Grimes 
28034d1ba5cSAndrey A. Chernov 	tzset();		/* in case no timezone database in ~ftp */
28134d1ba5cSAndrey A. Chernov 
282b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE
283ea022d16SRodney W. Grimes 	/*
284ea022d16SRodney W. Grimes 	 *  Save start and extent of argv for setproctitle.
285ea022d16SRodney W. Grimes 	 */
286ea022d16SRodney W. Grimes 	Argv = argv;
287ea022d16SRodney W. Grimes 	while (*envp)
288ea022d16SRodney W. Grimes 		envp++;
289ea022d16SRodney W. Grimes 	LastArgv = envp[-1] + strlen(envp[-1]);
290b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */
291ea022d16SRodney W. Grimes 
2923eb568f2SGuido van Rooij 
2934dd8b5abSYoshinobu Inoue 	while ((ch = getopt(argc, argv, "AdlDSURt:T:u:va:p:46")) != -1) {
294ea022d16SRodney W. Grimes 		switch (ch) {
295cf09a206SDavid Greenman 		case 'D':
296cf09a206SDavid Greenman 			daemon_mode++;
297cf09a206SDavid Greenman 			break;
298cf09a206SDavid Greenman 
299ea022d16SRodney W. Grimes 		case 'd':
300a5a4544eSPaul Traina 			debug++;
301ea022d16SRodney W. Grimes 			break;
302ea022d16SRodney W. Grimes 
303ea022d16SRodney W. Grimes 		case 'l':
304ea022d16SRodney W. Grimes 			logging++;	/* > 1 == extra logging */
305ea022d16SRodney W. Grimes 			break;
306ea022d16SRodney W. Grimes 
307a5a4544eSPaul Traina 		case 'R':
308a5a4544eSPaul Traina 			paranoid = 0;
309a5a4544eSPaul Traina 			break;
310a5a4544eSPaul Traina 
311a5a4544eSPaul Traina 		case 'S':
312a5a4544eSPaul Traina 			stats++;
313a5a4544eSPaul Traina 			break;
314a5a4544eSPaul Traina 
315a5a4544eSPaul Traina 		case 'T':
316a5a4544eSPaul Traina 			maxtimeout = atoi(optarg);
317a5a4544eSPaul Traina 			if (timeout > maxtimeout)
318a5a4544eSPaul Traina 				timeout = maxtimeout;
3194c450ad7SPaul Traina 			break;
3204c450ad7SPaul Traina 
321ea022d16SRodney W. Grimes 		case 't':
322ea022d16SRodney W. Grimes 			timeout = atoi(optarg);
323ea022d16SRodney W. Grimes 			if (maxtimeout < timeout)
324ea022d16SRodney W. Grimes 				maxtimeout = timeout;
325ea022d16SRodney W. Grimes 			break;
326a5a4544eSPaul Traina 
327a5a4544eSPaul Traina 		case 'U':
328a5a4544eSPaul Traina 			restricted_data_ports = 0;
329ea022d16SRodney W. Grimes 			break;
330ea022d16SRodney W. Grimes 
331105a3c98SJulian Elischer 		case 'a':
3324dd8b5abSYoshinobu Inoue 			bindname = optarg;
333105a3c98SJulian Elischer 			break;
334105a3c98SJulian Elischer 
335105a3c98SJulian Elischer 		case 'p':
336105a3c98SJulian Elischer 			pid_file = optarg;
337105a3c98SJulian Elischer 			break;
338105a3c98SJulian Elischer 
339ea022d16SRodney W. Grimes 		case 'u':
340ea022d16SRodney W. Grimes 		    {
341ea022d16SRodney W. Grimes 			long val = 0;
342ea022d16SRodney W. Grimes 
343ea022d16SRodney W. Grimes 			val = strtol(optarg, &optarg, 8);
344ea022d16SRodney W. Grimes 			if (*optarg != '\0' || val < 0)
345ea022d16SRodney W. Grimes 				warnx("bad value for -u");
346ea022d16SRodney W. Grimes 			else
347ea022d16SRodney W. Grimes 				defumask = val;
348ea022d16SRodney W. Grimes 			break;
349ea022d16SRodney W. Grimes 		    }
3505a392aecSTorsten Blum 		case 'A':
3515a392aecSTorsten Blum 			anon_only = 1;
3525a392aecSTorsten Blum 			break;
353ea022d16SRodney W. Grimes 
354ea022d16SRodney W. Grimes 		case 'v':
355ea022d16SRodney W. Grimes 			debug = 1;
356ea022d16SRodney W. Grimes 			break;
357ea022d16SRodney W. Grimes 
3584dd8b5abSYoshinobu Inoue 		case '4':
3594dd8b5abSYoshinobu Inoue 			enable_v4 = 1;
3604dd8b5abSYoshinobu Inoue 			if (family == AF_UNSPEC)
3614dd8b5abSYoshinobu Inoue 				family = AF_INET;
3624dd8b5abSYoshinobu Inoue 			break;
3634dd8b5abSYoshinobu Inoue 
3644dd8b5abSYoshinobu Inoue 		case '6':
3654dd8b5abSYoshinobu Inoue 			family = AF_INET6;
3664dd8b5abSYoshinobu Inoue 			break;
3674dd8b5abSYoshinobu Inoue 
368ea022d16SRodney W. Grimes 		default:
369ea022d16SRodney W. Grimes 			warnx("unknown flag -%c ignored", optopt);
370ea022d16SRodney W. Grimes 			break;
371ea022d16SRodney W. Grimes 		}
372ea022d16SRodney W. Grimes 	}
373cf09a206SDavid Greenman 
374ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
375ea4e54b9SDavid Nugent 	inithosts();
376ea4e54b9SDavid Nugent #endif
377ea022d16SRodney W. Grimes 	(void) freopen(_PATH_DEVNULL, "w", stderr);
378cf09a206SDavid Greenman 
379cf09a206SDavid Greenman 	/*
380cf09a206SDavid Greenman 	 * LOG_NDELAY sets up the logging connection immediately,
381cf09a206SDavid Greenman 	 * necessary for anonymous ftp's that chroot and can't do it later.
382cf09a206SDavid Greenman 	 */
383cf09a206SDavid Greenman 	openlog("ftpd", LOG_PID | LOG_NDELAY, LOG_FTP);
384cf09a206SDavid Greenman 
385cf09a206SDavid Greenman 	if (daemon_mode) {
386cf09a206SDavid Greenman 		int ctl_sock, fd;
3874dd8b5abSYoshinobu Inoue 		struct addrinfo hints, *res;
388cf09a206SDavid Greenman 
389cf09a206SDavid Greenman 		/*
390cf09a206SDavid Greenman 		 * Detach from parent.
391cf09a206SDavid Greenman 		 */
392cf09a206SDavid Greenman 		if (daemon(1, 1) < 0) {
393cf09a206SDavid Greenman 			syslog(LOG_ERR, "failed to become a daemon");
394cf09a206SDavid Greenman 			exit(1);
395cf09a206SDavid Greenman 		}
396cf09a206SDavid Greenman 		(void) signal(SIGCHLD, reapchild);
3974dd8b5abSYoshinobu Inoue 		/* init bind_sa */
3984dd8b5abSYoshinobu Inoue 		memset(&hints, 0, sizeof(hints));
3994dd8b5abSYoshinobu Inoue 
4004dd8b5abSYoshinobu Inoue 		hints.ai_family = family == AF_UNSPEC ? AF_INET : family;
4014dd8b5abSYoshinobu Inoue 		hints.ai_socktype = SOCK_STREAM;
4024dd8b5abSYoshinobu Inoue 		hints.ai_protocol = 0;
4034dd8b5abSYoshinobu Inoue 		hints.ai_flags = AI_PASSIVE;
4044dd8b5abSYoshinobu Inoue 		error = getaddrinfo(bindname, "ftp", &hints, &res);
4054dd8b5abSYoshinobu Inoue 		if (error) {
4064dd8b5abSYoshinobu Inoue 			if (family == AF_UNSPEC) {
4074dd8b5abSYoshinobu Inoue 				hints.ai_family = AF_UNSPEC;
4084dd8b5abSYoshinobu Inoue 				error = getaddrinfo(bindname, "ftp", &hints,
4094dd8b5abSYoshinobu Inoue 						    &res);
4104dd8b5abSYoshinobu Inoue 			}
4114dd8b5abSYoshinobu Inoue 			if (error == 0 && res->ai_addr != NULL)
4124dd8b5abSYoshinobu Inoue 				family = res->ai_addr->sa_family;
4134dd8b5abSYoshinobu Inoue 		}
4144dd8b5abSYoshinobu Inoue 		if (error) {
4154dd8b5abSYoshinobu Inoue 			syslog(LOG_ERR, gai_strerror(error));
4164dd8b5abSYoshinobu Inoue 			if (error == EAI_SYSTEM)
4174dd8b5abSYoshinobu Inoue 				syslog(LOG_ERR, strerror(errno));
4184dd8b5abSYoshinobu Inoue 			exit(1);
4194dd8b5abSYoshinobu Inoue 		}
4204dd8b5abSYoshinobu Inoue 		if (res->ai_addr == NULL) {
4214dd8b5abSYoshinobu Inoue 			syslog(LOG_ERR, "-a %s: getaddrinfo failed", hostname);
422cf09a206SDavid Greenman 			exit(1);
423cf09a206SDavid Greenman 		}
424cf09a206SDavid Greenman 		/*
425cf09a206SDavid Greenman 		 * Open a socket, bind it to the FTP port, and start
426cf09a206SDavid Greenman 		 * listening.
427cf09a206SDavid Greenman 		 */
4284dd8b5abSYoshinobu Inoue 		ctl_sock = socket(family, SOCK_STREAM, 0);
429cf09a206SDavid Greenman 		if (ctl_sock < 0) {
430cf09a206SDavid Greenman 			syslog(LOG_ERR, "control socket: %m");
431cf09a206SDavid Greenman 			exit(1);
432cf09a206SDavid Greenman 		}
433cf09a206SDavid Greenman 		if (setsockopt(ctl_sock, SOL_SOCKET, SO_REUSEADDR,
434cf09a206SDavid Greenman 		    (char *)&on, sizeof(on)) < 0)
4354dd8b5abSYoshinobu Inoue 			syslog(LOG_ERR, "control setsockopt: %m");
4364dd8b5abSYoshinobu Inoue #ifdef IPV6_BINDV6ONLY
4374dd8b5abSYoshinobu Inoue 		if (family == AF_INET6 && enable_v4 == 0) {
4384dd8b5abSYoshinobu Inoue 			if (setsockopt(ctl_sock, IPPROTO_IPV6, IPV6_BINDV6ONLY,
4394dd8b5abSYoshinobu Inoue 				       (char *)&on, sizeof (on)) < 0)
4404dd8b5abSYoshinobu Inoue 				syslog(LOG_ERR,
4414dd8b5abSYoshinobu Inoue 				       "control setsockopt(IPV6_BINDV6ONLY): %m");
4424dd8b5abSYoshinobu Inoue 		}
4434dd8b5abSYoshinobu Inoue #endif /* IPV6_BINDV6ONLY */
4444dd8b5abSYoshinobu Inoue 		memcpy(&server_addr, res->ai_addr, res->ai_addr->sa_len);
4454dd8b5abSYoshinobu Inoue 		if (bind(ctl_sock, (struct sockaddr *)&server_addr,
4464dd8b5abSYoshinobu Inoue 			 server_addr.su_len) < 0) {
447cf09a206SDavid Greenman 			syslog(LOG_ERR, "control bind: %m");
448cf09a206SDavid Greenman 			exit(1);
449cf09a206SDavid Greenman 		}
450cf09a206SDavid Greenman 		if (listen(ctl_sock, 32) < 0) {
451cf09a206SDavid Greenman 			syslog(LOG_ERR, "control listen: %m");
452cf09a206SDavid Greenman 			exit(1);
453cf09a206SDavid Greenman 		}
454cf09a206SDavid Greenman 		/*
455105a3c98SJulian Elischer 		 * Atomically write process ID
456105a3c98SJulian Elischer 		 */
457105a3c98SJulian Elischer 		if (pid_file)
458105a3c98SJulian Elischer 		{
459105a3c98SJulian Elischer 			int fd;
460105a3c98SJulian Elischer 			char buf[20];
461105a3c98SJulian Elischer 
462105a3c98SJulian Elischer 			fd = open(pid_file, O_CREAT | O_WRONLY | O_TRUNC
463105a3c98SJulian Elischer 				| O_NONBLOCK | O_EXLOCK, 0644);
46485966371SWarner Losh 			if (fd < 0) {
465105a3c98SJulian Elischer 				if (errno == EAGAIN)
466105a3c98SJulian Elischer 					errx(1, "%s: file locked", pid_file);
467105a3c98SJulian Elischer 				else
468105a3c98SJulian Elischer 					err(1, "%s", pid_file);
46985966371SWarner Losh 			}
470105a3c98SJulian Elischer 			snprintf(buf, sizeof(buf),
471105a3c98SJulian Elischer 				"%lu\n", (unsigned long) getpid());
472105a3c98SJulian Elischer 			if (write(fd, buf, strlen(buf)) < 0)
473105a3c98SJulian Elischer 				err(1, "%s: write", pid_file);
474105a3c98SJulian Elischer 			/* Leave the pid file open and locked */
475105a3c98SJulian Elischer 		}
476105a3c98SJulian Elischer 		/*
477cf09a206SDavid Greenman 		 * Loop forever accepting connection requests and forking off
478cf09a206SDavid Greenman 		 * children to handle them.
479cf09a206SDavid Greenman 		 */
480cf09a206SDavid Greenman 		while (1) {
4814dd8b5abSYoshinobu Inoue 			addrlen = server_addr.su_len;
482cf09a206SDavid Greenman 			fd = accept(ctl_sock, (struct sockaddr *)&his_addr, &addrlen);
483cf09a206SDavid Greenman 			if (fork() == 0) {
484cf09a206SDavid Greenman 				/* child */
485cf09a206SDavid Greenman 				(void) dup2(fd, 0);
486cf09a206SDavid Greenman 				(void) dup2(fd, 1);
487cf09a206SDavid Greenman 				close(ctl_sock);
488cf09a206SDavid Greenman 				break;
489cf09a206SDavid Greenman 			}
490cf09a206SDavid Greenman 			close(fd);
491cf09a206SDavid Greenman 		}
492cf09a206SDavid Greenman 	} else {
493cf09a206SDavid Greenman 		addrlen = sizeof(his_addr);
494cf09a206SDavid Greenman 		if (getpeername(0, (struct sockaddr *)&his_addr, &addrlen) < 0) {
495cf09a206SDavid Greenman 			syslog(LOG_ERR, "getpeername (%s): %m",argv[0]);
496cf09a206SDavid Greenman 			exit(1);
497cf09a206SDavid Greenman 		}
498cf09a206SDavid Greenman 	}
499cf09a206SDavid Greenman 
500ea022d16SRodney W. Grimes 	(void) signal(SIGCHLD, SIG_IGN);
501cf09a206SDavid Greenman 	(void) signal(SIGPIPE, lostconn);
502158a00b2SJohn Birrell 	if (signal(SIGURG, myoob) == SIG_ERR)
503ea022d16SRodney W. Grimes 		syslog(LOG_ERR, "signal: %m");
504ea022d16SRodney W. Grimes 
505cf09a206SDavid Greenman #ifdef SKEY
5064dd8b5abSYoshinobu Inoue 	getnameinfo((struct sockaddr *)&his_addr, his_addr.su_len,
5074dd8b5abSYoshinobu Inoue 		    addr_string, sizeof(addr_string) - 1, NULL, 0,
5084dd8b5abSYoshinobu Inoue 		    NI_NUMERICHOST|NI_WITHSCOPEID);
509cf09a206SDavid Greenman #endif
510cf09a206SDavid Greenman 	addrlen = sizeof(ctrl_addr);
511cf09a206SDavid Greenman 	if (getsockname(0, (struct sockaddr *)&ctrl_addr, &addrlen) < 0) {
512cf09a206SDavid Greenman 		syslog(LOG_ERR, "getsockname (%s): %m",argv[0]);
513cf09a206SDavid Greenman 		exit(1);
514cf09a206SDavid Greenman 	}
515ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
516ea4e54b9SDavid Nugent 	/* select our identity from virtual host table */
5174dd8b5abSYoshinobu Inoue 	selecthost(&ctrl_addr);
518ea4e54b9SDavid Nugent #endif
519cf09a206SDavid Greenman #ifdef IP_TOS
5204dd8b5abSYoshinobu Inoue 	if (ctrl_addr.su_family == AF_INET)
5214dd8b5abSYoshinobu Inoue       {
522cf09a206SDavid Greenman 	tos = IPTOS_LOWDELAY;
523cf09a206SDavid Greenman 	if (setsockopt(0, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(int)) < 0)
524cf09a206SDavid Greenman 		syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
5254dd8b5abSYoshinobu Inoue       }
526cf09a206SDavid Greenman #endif
527dadb9fb3SDavid Greenman 	/*
528dadb9fb3SDavid Greenman 	 * Disable Nagle on the control channel so that we don't have to wait
529dadb9fb3SDavid Greenman 	 * for peer's ACK before issuing our next reply.
530dadb9fb3SDavid Greenman 	 */
531dadb9fb3SDavid Greenman 	if (setsockopt(0, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) < 0)
532dadb9fb3SDavid Greenman 		syslog(LOG_WARNING, "control setsockopt TCP_NODELAY: %m");
533dadb9fb3SDavid Greenman 
5344dd8b5abSYoshinobu Inoue 	data_source.su_port = htons(ntohs(ctrl_addr.su_port) - 1);
535cf09a206SDavid Greenman 
536a5a4544eSPaul Traina 	/* set this here so klogin can use it... */
537e760ef2cSWarner Losh 	(void)snprintf(ttyline, sizeof(ttyline), "ftp%d", getpid());
538a5a4544eSPaul Traina 
539ea022d16SRodney W. Grimes 	/* Try to handle urgent data inline */
540ea022d16SRodney W. Grimes #ifdef SO_OOBINLINE
541ea022d16SRodney W. Grimes 	if (setsockopt(0, SOL_SOCKET, SO_OOBINLINE, (char *)&on, sizeof(on)) < 0)
542ea022d16SRodney W. Grimes 		syslog(LOG_ERR, "setsockopt: %m");
543ea022d16SRodney W. Grimes #endif
544ea022d16SRodney W. Grimes 
545ea022d16SRodney W. Grimes #ifdef	F_SETOWN
546ea022d16SRodney W. Grimes 	if (fcntl(fileno(stdin), F_SETOWN, getpid()) == -1)
547ea022d16SRodney W. Grimes 		syslog(LOG_ERR, "fcntl F_SETOWN: %m");
548ea022d16SRodney W. Grimes #endif
5494dd8b5abSYoshinobu Inoue 	dolog((struct sockaddr *)&his_addr);
550ea022d16SRodney W. Grimes 	/*
551ea022d16SRodney W. Grimes 	 * Set up default state
552ea022d16SRodney W. Grimes 	 */
553ea022d16SRodney W. Grimes 	data = -1;
554ea022d16SRodney W. Grimes 	type = TYPE_A;
555ea022d16SRodney W. Grimes 	form = FORM_N;
556ea022d16SRodney W. Grimes 	stru = STRU_F;
557ea022d16SRodney W. Grimes 	mode = MODE_S;
558ea022d16SRodney W. Grimes 	tmpline[0] = '\0';
559ea022d16SRodney W. Grimes 
560ea022d16SRodney W. Grimes 	/* If logins are disabled, print out the message. */
561ea022d16SRodney W. Grimes 	if ((fd = fopen(_PATH_NOLOGIN,"r")) != NULL) {
562ea022d16SRodney W. Grimes 		while (fgets(line, sizeof(line), fd) != NULL) {
563ea022d16SRodney W. Grimes 			if ((cp = strchr(line, '\n')) != NULL)
564ea022d16SRodney W. Grimes 				*cp = '\0';
565ea022d16SRodney W. Grimes 			lreply(530, "%s", line);
566ea022d16SRodney W. Grimes 		}
567ea022d16SRodney W. Grimes 		(void) fflush(stdout);
568ea022d16SRodney W. Grimes 		(void) fclose(fd);
569ea022d16SRodney W. Grimes 		reply(530, "System not available.");
570ea022d16SRodney W. Grimes 		exit(0);
571ea022d16SRodney W. Grimes 	}
572ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
573ea4e54b9SDavid Nugent 	if ((fd = fopen(thishost->welcome, "r")) != NULL) {
574ea4e54b9SDavid Nugent #else
575ea022d16SRodney W. Grimes 	if ((fd = fopen(_PATH_FTPWELCOME, "r")) != NULL) {
576ea4e54b9SDavid Nugent #endif
577ea022d16SRodney W. Grimes 		while (fgets(line, sizeof(line), fd) != NULL) {
578ea022d16SRodney W. Grimes 			if ((cp = strchr(line, '\n')) != NULL)
579ea022d16SRodney W. Grimes 				*cp = '\0';
580ea022d16SRodney W. Grimes 			lreply(220, "%s", line);
581ea022d16SRodney W. Grimes 		}
582ea022d16SRodney W. Grimes 		(void) fflush(stdout);
583ea022d16SRodney W. Grimes 		(void) fclose(fd);
584ea022d16SRodney W. Grimes 		/* reply(220,) must follow */
585ea022d16SRodney W. Grimes 	}
586ea4e54b9SDavid Nugent #ifndef VIRTUAL_HOSTING
5870512556aSDavid Nugent 	if ((hostname = malloc(MAXHOSTNAMELEN)) == NULL)
5880512556aSDavid Nugent 		fatal("Ran out of memory.");
5899e9a43bdSBrian Somers 	(void) gethostname(hostname, MAXHOSTNAMELEN - 1);
5909e9a43bdSBrian Somers 	hostname[MAXHOSTNAMELEN - 1] = '\0';
591ea4e54b9SDavid Nugent #endif
592ea022d16SRodney W. Grimes 	reply(220, "%s FTP server (%s) ready.", hostname, version);
593ea022d16SRodney W. Grimes 	(void) setjmp(errcatch);
594ea022d16SRodney W. Grimes 	for (;;)
595ea022d16SRodney W. Grimes 		(void) yyparse();
596ea022d16SRodney W. Grimes 	/* NOTREACHED */
597ea022d16SRodney W. Grimes }
598ea022d16SRodney W. Grimes 
599ea022d16SRodney W. Grimes static void
600ea022d16SRodney W. Grimes lostconn(signo)
601ea022d16SRodney W. Grimes 	int signo;
602ea022d16SRodney W. Grimes {
603ea022d16SRodney W. Grimes 
604ea022d16SRodney W. Grimes 	if (debug)
605ea022d16SRodney W. Grimes 		syslog(LOG_DEBUG, "lost connection");
606e02897faSPhilippe Charnier 	dologout(1);
607ea022d16SRodney W. Grimes }
608ea022d16SRodney W. Grimes 
609ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
610ea4e54b9SDavid Nugent /*
611ea4e54b9SDavid Nugent  * read in virtual host tables (if they exist)
612ea4e54b9SDavid Nugent  */
613ea4e54b9SDavid Nugent 
614ea4e54b9SDavid Nugent static void
615ea4e54b9SDavid Nugent inithosts()
616ea4e54b9SDavid Nugent {
617ea4e54b9SDavid Nugent 	FILE *fp;
618ea4e54b9SDavid Nugent 	char *cp;
619ea4e54b9SDavid Nugent 	struct ftphost *hrp, *lhrp;
620ea4e54b9SDavid Nugent 	char line[1024];
6214dd8b5abSYoshinobu Inoue 	struct addrinfo hints, *res, *ai;
622ea4e54b9SDavid Nugent 
623ea4e54b9SDavid Nugent 	/*
624ea4e54b9SDavid Nugent 	 * Fill in the default host information
625ea4e54b9SDavid Nugent 	 */
626ea4e54b9SDavid Nugent 	if (gethostname(line, sizeof(line)) < 0)
627ea4e54b9SDavid Nugent 		line[0] = '\0';
628ea4e54b9SDavid Nugent 	if ((hrp = malloc(sizeof(struct ftphost))) == NULL ||
629ea4e54b9SDavid Nugent 	    (hrp->hostname = strdup(line)) == NULL)
630ea4e54b9SDavid Nugent 		fatal("Ran out of memory.");
631f38c6cadSYoshinobu Inoue 	hrp->hostinfo = NULL;
6324dd8b5abSYoshinobu Inoue 
6334dd8b5abSYoshinobu Inoue 	memset(&hints, 0, sizeof(hints));
6344dd8b5abSYoshinobu Inoue 	hints.ai_flags = AI_CANONNAME;
6354dd8b5abSYoshinobu Inoue 	hints.ai_family = AF_UNSPEC;
6364dd8b5abSYoshinobu Inoue 	getaddrinfo(hrp->hostname, NULL, &hints, &res);
6374dd8b5abSYoshinobu Inoue 	if (res)
638f38c6cadSYoshinobu Inoue 		hrp->hostinfo = res;
639ea4e54b9SDavid Nugent 	hrp->statfile = _PATH_FTPDSTATFILE;
640ea4e54b9SDavid Nugent 	hrp->welcome  = _PATH_FTPWELCOME;
641ea4e54b9SDavid Nugent 	hrp->loginmsg = _PATH_FTPLOGINMESG;
642ea4e54b9SDavid Nugent 	hrp->anonuser = "ftp";
643ea4e54b9SDavid Nugent 	hrp->next = NULL;
644ea4e54b9SDavid Nugent 	thishost = firsthost = lhrp = hrp;
645ea4e54b9SDavid Nugent 	if ((fp = fopen(_PATH_FTPHOSTS, "r")) != NULL) {
6464dd8b5abSYoshinobu Inoue 		int addrsize, error;
6474dd8b5abSYoshinobu Inoue 		void *addr;
6484dd8b5abSYoshinobu Inoue 		struct hostent *hp;
6494dd8b5abSYoshinobu Inoue 
650ea4e54b9SDavid Nugent 		while (fgets(line, sizeof(line), fp) != NULL) {
6514dd8b5abSYoshinobu Inoue 			int	i, hp_error;
652ea4e54b9SDavid Nugent 
653ea4e54b9SDavid Nugent 			if ((cp = strchr(line, '\n')) == NULL) {
654ea4e54b9SDavid Nugent 				/* ignore long lines */
655ea4e54b9SDavid Nugent 				while (fgets(line, sizeof(line), fp) != NULL &&
656ea4e54b9SDavid Nugent 					strchr(line, '\n') == NULL)
657ea4e54b9SDavid Nugent 					;
658ea4e54b9SDavid Nugent 				continue;
659ea4e54b9SDavid Nugent 			}
660ea4e54b9SDavid Nugent 			*cp = '\0';
661ea4e54b9SDavid Nugent 			cp = strtok(line, " \t");
662ea4e54b9SDavid Nugent 			/* skip comments and empty lines */
663ea4e54b9SDavid Nugent 			if (cp == NULL || line[0] == '#')
664ea4e54b9SDavid Nugent 				continue;
6654dd8b5abSYoshinobu Inoue 
6664dd8b5abSYoshinobu Inoue 			hints.ai_flags = 0;
6674dd8b5abSYoshinobu Inoue 			hints.ai_family = AF_UNSPEC;
6684dd8b5abSYoshinobu Inoue 			hints.ai_flags = AI_PASSIVE;
6694dd8b5abSYoshinobu Inoue 			error = getaddrinfo(cp, NULL, &hints, &res);
6704dd8b5abSYoshinobu Inoue 			if (error != NULL)
671ea4e54b9SDavid Nugent 				continue;
6724dd8b5abSYoshinobu Inoue 			for (ai = res; ai != NULL && ai->ai_addr != NULL;
6734dd8b5abSYoshinobu Inoue 			     ai = ai->ai_next)
6744dd8b5abSYoshinobu Inoue 		      {
6754dd8b5abSYoshinobu Inoue 
676ea4e54b9SDavid Nugent 			for (hrp = firsthost; hrp != NULL; hrp = hrp->next) {
677f38c6cadSYoshinobu Inoue 				struct addrinfo *hi;
678f38c6cadSYoshinobu Inoue 
679f38c6cadSYoshinobu Inoue 				for (hi = hrp->hostinfo; hi != NULL;
680f38c6cadSYoshinobu Inoue 				     hi = hi->ai_next)
681f38c6cadSYoshinobu Inoue 					if (hi->ai_addrlen == ai->ai_addrlen &&
682f38c6cadSYoshinobu Inoue 					    memcmp(hi->ai_addr,
6834dd8b5abSYoshinobu Inoue 						   ai->ai_addr,
6844dd8b5abSYoshinobu Inoue 						   ai->ai_addr->sa_len) == 0)
685ea4e54b9SDavid Nugent 						break;
686ea4e54b9SDavid Nugent 			}
687ea4e54b9SDavid Nugent 			if (hrp == NULL) {
688ea4e54b9SDavid Nugent 				if ((hrp = malloc(sizeof(struct ftphost))) == NULL)
689ea4e54b9SDavid Nugent 					continue;
690ea4e54b9SDavid Nugent 				/* defaults */
691ea4e54b9SDavid Nugent 				hrp->statfile = _PATH_FTPDSTATFILE;
692ea4e54b9SDavid Nugent 				hrp->welcome  = _PATH_FTPWELCOME;
693ea4e54b9SDavid Nugent 				hrp->loginmsg = _PATH_FTPLOGINMESG;
694ea4e54b9SDavid Nugent 				hrp->anonuser = "ftp";
695ea4e54b9SDavid Nugent 				hrp->next     = NULL;
696ea4e54b9SDavid Nugent 				lhrp->next = hrp;
697ea4e54b9SDavid Nugent 				lhrp = hrp;
698ea4e54b9SDavid Nugent 			}
699f38c6cadSYoshinobu Inoue 			hrp->hostinfo = res;
700f38c6cadSYoshinobu Inoue 
701ea4e54b9SDavid Nugent 			/*
702ea4e54b9SDavid Nugent 			 * determine hostname to use.
7034dd8b5abSYoshinobu Inoue 			 * force defined name if there is a valid alias
704ea4e54b9SDavid Nugent 			 * otherwise fallback to primary hostname
705ea4e54b9SDavid Nugent 			 */
7064dd8b5abSYoshinobu Inoue 			/* XXX: getaddrinfo() can't do alias check */
707f38c6cadSYoshinobu Inoue 			switch(hrp->hostinfo->ai_family) {
7084dd8b5abSYoshinobu Inoue 			case AF_INET:
709f38c6cadSYoshinobu Inoue 				addr = &((struct sockaddr_in *)&hrp->hostinfo->ai_addr)->sin_addr;
7104dd8b5abSYoshinobu Inoue 				addrsize = sizeof(struct sockaddr_in);
7114dd8b5abSYoshinobu Inoue 				break;
7124dd8b5abSYoshinobu Inoue 			case AF_INET6:
713f38c6cadSYoshinobu Inoue 				addr = &((struct sockaddr_in6 *)&hrp->hostinfo->ai_addr)->sin6_addr;
7144dd8b5abSYoshinobu Inoue 				addrsize = sizeof(struct sockaddr_in6);
7154dd8b5abSYoshinobu Inoue 				break;
7164dd8b5abSYoshinobu Inoue 			default:
7174dd8b5abSYoshinobu Inoue 				/* should not reach here */
718f38c6cadSYoshinobu Inoue 				if (hrp->hostinfo != NULL)
719f38c6cadSYoshinobu Inoue 					freeaddrinfo(hrp->hostinfo);
7204dd8b5abSYoshinobu Inoue 				free(hrp);
7214dd8b5abSYoshinobu Inoue 				continue;
7224dd8b5abSYoshinobu Inoue 				/* NOTREACHED */
7234dd8b5abSYoshinobu Inoue 			}
7244dd8b5abSYoshinobu Inoue 			if ((hp = getipnodebyaddr((char*)addr, addrsize,
725f38c6cadSYoshinobu Inoue 						  hrp->hostinfo->ai_family,
7264dd8b5abSYoshinobu Inoue 						  &hp_error)) != NULL) {
727ea4e54b9SDavid Nugent 				if (strcmp(cp, hp->h_name) != 0) {
728ea4e54b9SDavid Nugent 					if (hp->h_aliases == NULL)
729ea4e54b9SDavid Nugent 						cp = hp->h_name;
730ea4e54b9SDavid Nugent 					else {
731ea4e54b9SDavid Nugent 						i = 0;
732ea4e54b9SDavid Nugent 						while (hp->h_aliases[i] &&
733ea4e54b9SDavid Nugent 						       strcmp(cp, hp->h_aliases[i]) != 0)
734ea4e54b9SDavid Nugent 							++i;
735ea4e54b9SDavid Nugent 						if (hp->h_aliases[i] == NULL)
736ea4e54b9SDavid Nugent 							cp = hp->h_name;
737ea4e54b9SDavid Nugent 					}
738ea4e54b9SDavid Nugent 				}
739ea4e54b9SDavid Nugent 			}
740ea4e54b9SDavid Nugent 			hrp->hostname = strdup(cp);
7414dd8b5abSYoshinobu Inoue 			freehostent(hp);
742ea4e54b9SDavid Nugent 			/* ok, now we now peel off the rest */
743ea4e54b9SDavid Nugent 			i = 0;
744ea4e54b9SDavid Nugent 			while (i < 4 && (cp = strtok(NULL, " \t")) != NULL) {
745ea4e54b9SDavid Nugent 				if (*cp != '-' && (cp = strdup(cp)) != NULL) {
746ea4e54b9SDavid Nugent 					switch (i) {
747ea4e54b9SDavid Nugent 					case 0:	/* anon user permissions */
748ea4e54b9SDavid Nugent 						hrp->anonuser = cp;
749ea4e54b9SDavid Nugent 						break;
750ea4e54b9SDavid Nugent 					case 1: /* statistics file */
751ea4e54b9SDavid Nugent 						hrp->statfile = cp;
752ea4e54b9SDavid Nugent 						break;
753ea4e54b9SDavid Nugent 					case 2: /* welcome message */
754ea4e54b9SDavid Nugent 						hrp->welcome  = cp;
755ea4e54b9SDavid Nugent 						break;
756ea4e54b9SDavid Nugent 					case 3: /* login message */
757ea4e54b9SDavid Nugent 						hrp->loginmsg = cp;
758ea4e54b9SDavid Nugent 						break;
759ea4e54b9SDavid Nugent 					}
760ea4e54b9SDavid Nugent 				}
761ea4e54b9SDavid Nugent 				++i;
762ea4e54b9SDavid Nugent 			}
7634dd8b5abSYoshinobu Inoue 			/* XXX: re-initialization for getaddrinfo() loop */
7644dd8b5abSYoshinobu Inoue 			cp = strtok(line, " \t");
7654dd8b5abSYoshinobu Inoue 		      }
766ea4e54b9SDavid Nugent 		}
767ea4e54b9SDavid Nugent 		(void) fclose(fp);
768ea4e54b9SDavid Nugent 	}
769ea4e54b9SDavid Nugent }
770ea4e54b9SDavid Nugent 
771ea4e54b9SDavid Nugent static void
7724dd8b5abSYoshinobu Inoue selecthost(su)
7734dd8b5abSYoshinobu Inoue 	union sockunion *su;
774ea4e54b9SDavid Nugent {
775ea4e54b9SDavid Nugent 	struct ftphost	*hrp;
7764dd8b5abSYoshinobu Inoue 	u_int16_t port;
7774dd8b5abSYoshinobu Inoue #ifdef INET6
7784dd8b5abSYoshinobu Inoue 	struct in6_addr *mapped_in6 = NULL;
7794dd8b5abSYoshinobu Inoue #endif
780f38c6cadSYoshinobu Inoue 	struct addrinfo *hi;
7814dd8b5abSYoshinobu Inoue 
7824dd8b5abSYoshinobu Inoue #ifdef INET6
7834dd8b5abSYoshinobu Inoue 	/*
7844dd8b5abSYoshinobu Inoue 	 * XXX IPv4 mapped IPv6 addr consideraton,
7854dd8b5abSYoshinobu Inoue 	 * specified in rfc2373.
7864dd8b5abSYoshinobu Inoue 	 */
7874dd8b5abSYoshinobu Inoue 	if (su->su_family == AF_INET6 &&
7884dd8b5abSYoshinobu Inoue 	    IN6_IS_ADDR_V4MAPPED(&su->su_sin6.sin6_addr))
7894dd8b5abSYoshinobu Inoue 		mapped_in6 = &su->su_sin6.sin6_addr;
7904dd8b5abSYoshinobu Inoue #endif
791ea4e54b9SDavid Nugent 
792ea4e54b9SDavid Nugent 	hrp = thishost = firsthost;	/* default */
7934dd8b5abSYoshinobu Inoue 	port = su->su_port;
7944dd8b5abSYoshinobu Inoue 	su->su_port = 0;
795ea4e54b9SDavid Nugent 	while (hrp != NULL) {
796f38c6cadSYoshinobu Inoue 		for (hi = hrp->hostinfo; hi != NULL; hi = hi->ai_next)
797f38c6cadSYoshinobu Inoue 	      {
798f38c6cadSYoshinobu Inoue 		if (memcmp(su, hi->ai_addr, hi->ai_addrlen) == 0) {
799ea4e54b9SDavid Nugent 			thishost = hrp;
800ea4e54b9SDavid Nugent 			break;
801ea4e54b9SDavid Nugent 		}
8024dd8b5abSYoshinobu Inoue #ifdef INET6
8034dd8b5abSYoshinobu Inoue 		/* XXX IPv4 mapped IPv6 addr consideraton */
804f38c6cadSYoshinobu Inoue 		if (hi->ai_addr->sa_family == AF_INET && mapped_in6 != NULL &&
8054dd8b5abSYoshinobu Inoue 		    (memcmp(&mapped_in6->s6_addr[12],
806f38c6cadSYoshinobu Inoue 			    &((struct sockaddr_in *)hi->ai_addr)->sin_addr,
8074dd8b5abSYoshinobu Inoue 			    sizeof(struct in_addr)) == 0)) {
8084dd8b5abSYoshinobu Inoue 			thishost = hrp;
8094dd8b5abSYoshinobu Inoue 			break;
8104dd8b5abSYoshinobu Inoue 		}
8114dd8b5abSYoshinobu Inoue #endif
812f38c6cadSYoshinobu Inoue 	      }
813ea4e54b9SDavid Nugent 		hrp = hrp->next;
814ea4e54b9SDavid Nugent 	}
8154dd8b5abSYoshinobu Inoue 	su->su_port = port;
816ea4e54b9SDavid Nugent 	/* setup static variables as appropriate */
817ea4e54b9SDavid Nugent 	hostname = thishost->hostname;
818ea4e54b9SDavid Nugent 	ftpuser = thishost->anonuser;
819ea4e54b9SDavid Nugent }
820ea4e54b9SDavid Nugent #endif
821ea4e54b9SDavid Nugent 
822ea022d16SRodney W. Grimes /*
823ea022d16SRodney W. Grimes  * Helper function for sgetpwnam().
824ea022d16SRodney W. Grimes  */
825ea022d16SRodney W. Grimes static char *
826ea022d16SRodney W. Grimes sgetsave(s)
827ea022d16SRodney W. Grimes 	char *s;
828ea022d16SRodney W. Grimes {
829ea022d16SRodney W. Grimes 	char *new = malloc((unsigned) strlen(s) + 1);
830ea022d16SRodney W. Grimes 
831ea022d16SRodney W. Grimes 	if (new == NULL) {
832ea022d16SRodney W. Grimes 		perror_reply(421, "Local resource failure: malloc");
833ea022d16SRodney W. Grimes 		dologout(1);
834ea022d16SRodney W. Grimes 		/* NOTREACHED */
835ea022d16SRodney W. Grimes 	}
836ea022d16SRodney W. Grimes 	(void) strcpy(new, s);
837ea022d16SRodney W. Grimes 	return (new);
838ea022d16SRodney W. Grimes }
839ea022d16SRodney W. Grimes 
840ea022d16SRodney W. Grimes /*
841ea022d16SRodney W. Grimes  * Save the result of a getpwnam.  Used for USER command, since
842ea022d16SRodney W. Grimes  * the data returned must not be clobbered by any other command
843ea022d16SRodney W. Grimes  * (e.g., globbing).
844ea022d16SRodney W. Grimes  */
845ea022d16SRodney W. Grimes static struct passwd *
846ea022d16SRodney W. Grimes sgetpwnam(name)
847ea022d16SRodney W. Grimes 	char *name;
848ea022d16SRodney W. Grimes {
849ea022d16SRodney W. Grimes 	static struct passwd save;
850ea022d16SRodney W. Grimes 	struct passwd *p;
851ea022d16SRodney W. Grimes 
852ea022d16SRodney W. Grimes 	if ((p = getpwnam(name)) == NULL)
853ea022d16SRodney W. Grimes 		return (p);
854ea022d16SRodney W. Grimes 	if (save.pw_name) {
855ea022d16SRodney W. Grimes 		free(save.pw_name);
856ea022d16SRodney W. Grimes 		free(save.pw_passwd);
857ea022d16SRodney W. Grimes 		free(save.pw_gecos);
858ea022d16SRodney W. Grimes 		free(save.pw_dir);
859ea022d16SRodney W. Grimes 		free(save.pw_shell);
860ea022d16SRodney W. Grimes 	}
861ea022d16SRodney W. Grimes 	save = *p;
862ea022d16SRodney W. Grimes 	save.pw_name = sgetsave(p->pw_name);
863ea022d16SRodney W. Grimes 	save.pw_passwd = sgetsave(p->pw_passwd);
864ea022d16SRodney W. Grimes 	save.pw_gecos = sgetsave(p->pw_gecos);
865ea022d16SRodney W. Grimes 	save.pw_dir = sgetsave(p->pw_dir);
866ea022d16SRodney W. Grimes 	save.pw_shell = sgetsave(p->pw_shell);
867ea022d16SRodney W. Grimes 	return (&save);
868ea022d16SRodney W. Grimes }
869ea022d16SRodney W. Grimes 
870ea022d16SRodney W. Grimes static int login_attempts;	/* number of failed login attempts */
871ea022d16SRodney W. Grimes static int askpasswd;		/* had user command, ask for passwd */
872ea022d16SRodney W. Grimes static char curname[10];	/* current USER name */
873ea022d16SRodney W. Grimes 
874ea022d16SRodney W. Grimes /*
875ea022d16SRodney W. Grimes  * USER command.
876ea022d16SRodney W. Grimes  * Sets global passwd pointer pw if named account exists and is acceptable;
877ea022d16SRodney W. Grimes  * sets askpasswd if a PASS command is expected.  If logged in previously,
878ea022d16SRodney W. Grimes  * need to reset state.  If name is "ftp" or "anonymous", the name is not in
879ea022d16SRodney W. Grimes  * _PATH_FTPUSERS, and ftp account exists, set guest and pw, then just return.
880ea022d16SRodney W. Grimes  * If account doesn't exist, ask for passwd anyway.  Otherwise, check user
881ea022d16SRodney W. Grimes  * requesting login privileges.  Disallow anyone who does not have a standard
882ea022d16SRodney W. Grimes  * shell as returned by getusershell().  Disallow anyone mentioned in the file
883ea022d16SRodney W. Grimes  * _PATH_FTPUSERS to allow people such as root and uucp to be avoided.
884ea022d16SRodney W. Grimes  */
885ea022d16SRodney W. Grimes void
886ea022d16SRodney W. Grimes user(name)
887ea022d16SRodney W. Grimes 	char *name;
888ea022d16SRodney W. Grimes {
889ea022d16SRodney W. Grimes 	char *cp, *shell;
890ea022d16SRodney W. Grimes 
891ea022d16SRodney W. Grimes 	if (logged_in) {
892ea022d16SRodney W. Grimes 		if (guest) {
893ea022d16SRodney W. Grimes 			reply(530, "Can't change user from guest login.");
894ea022d16SRodney W. Grimes 			return;
895a5a4544eSPaul Traina 		} else if (dochroot) {
896a5a4544eSPaul Traina 			reply(530, "Can't change user from chroot user.");
897a5a4544eSPaul Traina 			return;
898ea022d16SRodney W. Grimes 		}
899ea022d16SRodney W. Grimes 		end_login();
900ea022d16SRodney W. Grimes 	}
901ea022d16SRodney W. Grimes 
902ea022d16SRodney W. Grimes 	guest = 0;
903ea022d16SRodney W. Grimes 	if (strcmp(name, "ftp") == 0 || strcmp(name, "anonymous") == 0) {
9047edcb936SSteve Price 		if (checkuser(_PATH_FTPUSERS, "ftp", 0) ||
9057edcb936SSteve Price 		    checkuser(_PATH_FTPUSERS, "anonymous", 0))
906ea022d16SRodney W. Grimes 			reply(530, "User %s access denied.", name);
907ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
908ea4e54b9SDavid Nugent 		else if ((pw = sgetpwnam(thishost->anonuser)) != NULL) {
909ea4e54b9SDavid Nugent #else
910ea022d16SRodney W. Grimes 		else if ((pw = sgetpwnam("ftp")) != NULL) {
911ea4e54b9SDavid Nugent #endif
912ea022d16SRodney W. Grimes 			guest = 1;
913ea022d16SRodney W. Grimes 			askpasswd = 1;
914ea022d16SRodney W. Grimes 			reply(331,
9152c60c54cSPaul Traina 			"Guest login ok, send your email address as password.");
916ea022d16SRodney W. Grimes 		} else
917ea022d16SRodney W. Grimes 			reply(530, "User %s unknown.", name);
918ea022d16SRodney W. Grimes 		if (!askpasswd && logging)
919ea022d16SRodney W. Grimes 			syslog(LOG_NOTICE,
920ea022d16SRodney W. Grimes 			    "ANONYMOUS FTP LOGIN REFUSED FROM %s", remotehost);
921ea022d16SRodney W. Grimes 		return;
922ea022d16SRodney W. Grimes 	}
9235a392aecSTorsten Blum 	if (anon_only != 0) {
9245a392aecSTorsten Blum 		reply(530, "Sorry, only anonymous ftp allowed.");
9255a392aecSTorsten Blum 		return;
9265a392aecSTorsten Blum 	}
9275a392aecSTorsten Blum 
9289aca17cbSMark Murray 	if ((pw = sgetpwnam(name))) {
929ea022d16SRodney W. Grimes 		if ((shell = pw->pw_shell) == NULL || *shell == 0)
930ea022d16SRodney W. Grimes 			shell = _PATH_BSHELL;
931ea022d16SRodney W. Grimes 		while ((cp = getusershell()) != NULL)
932ea022d16SRodney W. Grimes 			if (strcmp(cp, shell) == 0)
933ea022d16SRodney W. Grimes 				break;
934ea022d16SRodney W. Grimes 		endusershell();
935ea022d16SRodney W. Grimes 
9367edcb936SSteve Price 		if (cp == NULL || checkuser(_PATH_FTPUSERS, name, 1)) {
937ea022d16SRodney W. Grimes 			reply(530, "User %s access denied.", name);
938ea022d16SRodney W. Grimes 			if (logging)
939ea022d16SRodney W. Grimes 				syslog(LOG_NOTICE,
940ea022d16SRodney W. Grimes 				    "FTP LOGIN REFUSED FROM %s, %s",
941ea022d16SRodney W. Grimes 				    remotehost, name);
942ea022d16SRodney W. Grimes 			pw = (struct passwd *) NULL;
943ea022d16SRodney W. Grimes 			return;
944ea022d16SRodney W. Grimes 		}
945ea022d16SRodney W. Grimes 	}
946ea022d16SRodney W. Grimes 	if (logging)
947ea022d16SRodney W. Grimes 		strncpy(curname, name, sizeof(curname)-1);
948726040deSGuido van Rooij #ifdef SKEY
9492c60c54cSPaul Traina 	pwok = skeyaccess(name, NULL, remotehost, addr_string);
95043658eacSAndrey A. Chernov 	reply(331, "%s", skey_challenge(name, pw, pwok));
951726040deSGuido van Rooij #else
952ea022d16SRodney W. Grimes 	reply(331, "Password required for %s.", name);
953726040deSGuido van Rooij #endif
954ea022d16SRodney W. Grimes 	askpasswd = 1;
955ea022d16SRodney W. Grimes 	/*
956ea022d16SRodney W. Grimes 	 * Delay before reading passwd after first failed
957ea022d16SRodney W. Grimes 	 * attempt to slow down passwd-guessing programs.
958ea022d16SRodney W. Grimes 	 */
959ea022d16SRodney W. Grimes 	if (login_attempts)
960ea022d16SRodney W. Grimes 		sleep((unsigned) login_attempts);
961ea022d16SRodney W. Grimes }
962ea022d16SRodney W. Grimes 
963ea022d16SRodney W. Grimes /*
964a5a4544eSPaul Traina  * Check if a user is in the file "fname"
965ea022d16SRodney W. Grimes  */
966ea022d16SRodney W. Grimes static int
9677edcb936SSteve Price checkuser(fname, name, pwset)
968a5a4544eSPaul Traina 	char *fname;
969ea022d16SRodney W. Grimes 	char *name;
9707edcb936SSteve Price 	int pwset;
971ea022d16SRodney W. Grimes {
972ea022d16SRodney W. Grimes 	FILE *fd;
973ea022d16SRodney W. Grimes 	int found = 0;
974ea022d16SRodney W. Grimes 	char *p, line[BUFSIZ];
975ea022d16SRodney W. Grimes 
976a5a4544eSPaul Traina 	if ((fd = fopen(fname, "r")) != NULL) {
97731fea7b8SDavid Nugent 		while (!found && fgets(line, sizeof(line), fd) != NULL)
978ea022d16SRodney W. Grimes 			if ((p = strchr(line, '\n')) != NULL) {
979ea022d16SRodney W. Grimes 				*p = '\0';
980ea022d16SRodney W. Grimes 				if (line[0] == '#')
981ea022d16SRodney W. Grimes 					continue;
98231fea7b8SDavid Nugent 				/*
98331fea7b8SDavid Nugent 				 * if first chr is '@', check group membership
98431fea7b8SDavid Nugent 				 */
98531fea7b8SDavid Nugent 				if (line[0] == '@') {
98631fea7b8SDavid Nugent 					int i = 0;
98731fea7b8SDavid Nugent 					struct group *grp;
98831fea7b8SDavid Nugent 
98931fea7b8SDavid Nugent 					if ((grp = getgrnam(line+1)) == NULL)
99031fea7b8SDavid Nugent 						continue;
9917edcb936SSteve Price 					/*
9927edcb936SSteve Price 					 * Check user's default group
9937edcb936SSteve Price 					 */
9947edcb936SSteve Price 					if (pwset && grp->gr_gid == pw->pw_gid)
9957edcb936SSteve Price 						found = 1;
9967edcb936SSteve Price 					/*
9977edcb936SSteve Price 					 * Check supplementary groups
9987edcb936SSteve Price 					 */
99931fea7b8SDavid Nugent 					while (!found && grp->gr_mem[i])
100031fea7b8SDavid Nugent 						found = strcmp(name,
100131fea7b8SDavid Nugent 							grp->gr_mem[i++])
100231fea7b8SDavid Nugent 							== 0;
1003ea022d16SRodney W. Grimes 				}
100431fea7b8SDavid Nugent 				/*
100531fea7b8SDavid Nugent 				 * Otherwise, just check for username match
100631fea7b8SDavid Nugent 				 */
100731fea7b8SDavid Nugent 				else
100831fea7b8SDavid Nugent 					found = strcmp(line, name) == 0;
1009ea022d16SRodney W. Grimes 			}
1010ea022d16SRodney W. Grimes 		(void) fclose(fd);
1011ea022d16SRodney W. Grimes 	}
1012ea022d16SRodney W. Grimes 	return (found);
1013ea022d16SRodney W. Grimes }
1014ea022d16SRodney W. Grimes 
1015ea022d16SRodney W. Grimes /*
1016ea022d16SRodney W. Grimes  * Terminate login as previous user, if any, resetting state;
1017ea022d16SRodney W. Grimes  * used when USER command is given or login fails.
1018ea022d16SRodney W. Grimes  */
1019ea022d16SRodney W. Grimes static void
1020ea022d16SRodney W. Grimes end_login()
1021ea022d16SRodney W. Grimes {
1022ea022d16SRodney W. Grimes 
1023ea022d16SRodney W. Grimes 	(void) seteuid((uid_t)0);
1024ea022d16SRodney W. Grimes 	if (logged_in)
1025986a1172SThomas Gellekum 		ftpd_logwtmp(ttyline, "", "");
1026ea022d16SRodney W. Grimes 	pw = NULL;
1027b071c689SDavid Nugent #ifdef	LOGIN_CAP
1028b071c689SDavid Nugent 	setusercontext(NULL, getpwuid(0), (uid_t)0,
1029b071c689SDavid Nugent 		       LOGIN_SETPRIORITY|LOGIN_SETRESOURCES|LOGIN_SETUMASK);
1030b071c689SDavid Nugent #endif
1031ea022d16SRodney W. Grimes 	logged_in = 0;
1032ea022d16SRodney W. Grimes 	guest = 0;
1033a5a4544eSPaul Traina 	dochroot = 0;
1034ea022d16SRodney W. Grimes }
1035ea022d16SRodney W. Grimes 
10366c9134c0SMark Murray #if !defined(NOPAM)
10376c9134c0SMark Murray 
10386c9134c0SMark Murray /*
10396c9134c0SMark Murray  * the following code is stolen from imap-uw PAM authentication module and
10406c9134c0SMark Murray  * login.c
10416c9134c0SMark Murray  */
10426c9134c0SMark Murray #define COPY_STRING(s) (s ? strdup(s) : NULL)
10436c9134c0SMark Murray 
10446c9134c0SMark Murray struct cred_t {
10456c9134c0SMark Murray 	const char *uname;		/* user name */
10466c9134c0SMark Murray 	const char *pass;		/* password */
10476c9134c0SMark Murray };
10486c9134c0SMark Murray typedef struct cred_t cred_t;
10496c9134c0SMark Murray 
10506c9134c0SMark Murray static int
10516c9134c0SMark Murray auth_conv(int num_msg, const struct pam_message **msg,
10526c9134c0SMark Murray 	  struct pam_response **resp, void *appdata)
10536c9134c0SMark Murray {
10546c9134c0SMark Murray 	int i;
10556c9134c0SMark Murray 	cred_t *cred = (cred_t *) appdata;
10566c9134c0SMark Murray 	struct pam_response *reply =
10576c9134c0SMark Murray 			malloc(sizeof(struct pam_response) * num_msg);
10586c9134c0SMark Murray 
10596c9134c0SMark Murray 	for (i = 0; i < num_msg; i++) {
10606c9134c0SMark Murray 		switch (msg[i]->msg_style) {
10616c9134c0SMark Murray 		case PAM_PROMPT_ECHO_ON:	/* assume want user name */
10626c9134c0SMark Murray 			reply[i].resp_retcode = PAM_SUCCESS;
10636c9134c0SMark Murray 			reply[i].resp = COPY_STRING(cred->uname);
10646c9134c0SMark Murray 			/* PAM frees resp. */
10656c9134c0SMark Murray 			break;
10666c9134c0SMark Murray 		case PAM_PROMPT_ECHO_OFF:	/* assume want password */
10676c9134c0SMark Murray 			reply[i].resp_retcode = PAM_SUCCESS;
10686c9134c0SMark Murray 			reply[i].resp = COPY_STRING(cred->pass);
10696c9134c0SMark Murray 			/* PAM frees resp. */
10706c9134c0SMark Murray 			break;
10716c9134c0SMark Murray 		case PAM_TEXT_INFO:
10726c9134c0SMark Murray 		case PAM_ERROR_MSG:
10736c9134c0SMark Murray 			reply[i].resp_retcode = PAM_SUCCESS;
10746c9134c0SMark Murray 			reply[i].resp = NULL;
10756c9134c0SMark Murray 			break;
10766c9134c0SMark Murray 		default:			/* unknown message style */
10776c9134c0SMark Murray 			free(reply);
10786c9134c0SMark Murray 			return PAM_CONV_ERR;
10796c9134c0SMark Murray 		}
10806c9134c0SMark Murray 	}
10816c9134c0SMark Murray 
10826c9134c0SMark Murray 	*resp = reply;
10836c9134c0SMark Murray 	return PAM_SUCCESS;
10846c9134c0SMark Murray }
10856c9134c0SMark Murray 
10866c9134c0SMark Murray /*
10876c9134c0SMark Murray  * Attempt to authenticate the user using PAM.  Returns 0 if the user is
10886c9134c0SMark Murray  * authenticated, or 1 if not authenticated.  If some sort of PAM system
10896c9134c0SMark Murray  * error occurs (e.g., the "/etc/pam.conf" file is missing) then this
10906c9134c0SMark Murray  * function returns -1.  This can be used as an indication that we should
10916c9134c0SMark Murray  * fall back to a different authentication mechanism.
10926c9134c0SMark Murray  */
10936c9134c0SMark Murray static int
10946c9134c0SMark Murray auth_pam(struct passwd **ppw, const char *pass)
10956c9134c0SMark Murray {
10966c9134c0SMark Murray 	pam_handle_t *pamh = NULL;
10976c9134c0SMark Murray 	const char *tmpl_user;
10986c9134c0SMark Murray 	const void *item;
10996c9134c0SMark Murray 	int rval;
11006c9134c0SMark Murray 	int e;
11016c9134c0SMark Murray 	cred_t auth_cred = { (*ppw)->pw_name, pass };
11026c9134c0SMark Murray 	struct pam_conv conv = { &auth_conv, &auth_cred };
11036c9134c0SMark Murray 
11046c9134c0SMark Murray 	e = pam_start("ftpd", (*ppw)->pw_name, &conv, &pamh);
11056c9134c0SMark Murray 	if (e != PAM_SUCCESS) {
11066c9134c0SMark Murray 		syslog(LOG_ERR, "pam_start: %s", pam_strerror(pamh, e));
11076c9134c0SMark Murray 		return -1;
11086c9134c0SMark Murray 	}
11096c9134c0SMark Murray 
11106c9134c0SMark Murray 	e = pam_authenticate(pamh, 0);
11116c9134c0SMark Murray 	switch (e) {
11126c9134c0SMark Murray 	case PAM_SUCCESS:
11136c9134c0SMark Murray 		/*
11146c9134c0SMark Murray 		 * With PAM we support the concept of a "template"
11156c9134c0SMark Murray 		 * user.  The user enters a login name which is
11166c9134c0SMark Murray 		 * authenticated by PAM, usually via a remote service
11176c9134c0SMark Murray 		 * such as RADIUS or TACACS+.  If authentication
11186c9134c0SMark Murray 		 * succeeds, a different but related "template" name
11196c9134c0SMark Murray 		 * is used for setting the credentials, shell, and
11206c9134c0SMark Murray 		 * home directory.  The name the user enters need only
11216c9134c0SMark Murray 		 * exist on the remote authentication server, but the
11226c9134c0SMark Murray 		 * template name must be present in the local password
11236c9134c0SMark Murray 		 * database.
11246c9134c0SMark Murray 		 *
11256c9134c0SMark Murray 		 * This is supported by two various mechanisms in the
11266c9134c0SMark Murray 		 * individual modules.  However, from the application's
11276c9134c0SMark Murray 		 * point of view, the template user is always passed
11286c9134c0SMark Murray 		 * back as a changed value of the PAM_USER item.
11296c9134c0SMark Murray 		 */
11306c9134c0SMark Murray 		if ((e = pam_get_item(pamh, PAM_USER, &item)) ==
11316c9134c0SMark Murray 		    PAM_SUCCESS) {
11326c9134c0SMark Murray 			tmpl_user = (const char *) item;
11336c9134c0SMark Murray 			if (strcmp((*ppw)->pw_name, tmpl_user) != 0)
11346c9134c0SMark Murray 				*ppw = getpwnam(tmpl_user);
11356c9134c0SMark Murray 		} else
11366c9134c0SMark Murray 			syslog(LOG_ERR, "Couldn't get PAM_USER: %s",
11376c9134c0SMark Murray 			    pam_strerror(pamh, e));
11386c9134c0SMark Murray 		rval = 0;
11396c9134c0SMark Murray 		break;
11406c9134c0SMark Murray 
11416c9134c0SMark Murray 	case PAM_AUTH_ERR:
11426c9134c0SMark Murray 	case PAM_USER_UNKNOWN:
11436c9134c0SMark Murray 	case PAM_MAXTRIES:
11446c9134c0SMark Murray 		rval = 1;
11456c9134c0SMark Murray 		break;
11466c9134c0SMark Murray 
11476c9134c0SMark Murray 	default:
11486c9134c0SMark Murray 		syslog(LOG_ERR, "auth_pam: %s", pam_strerror(pamh, e));
11496c9134c0SMark Murray 		rval = -1;
11506c9134c0SMark Murray 		break;
11516c9134c0SMark Murray 	}
11526c9134c0SMark Murray 
11536c9134c0SMark Murray 	if ((e = pam_end(pamh, e)) != PAM_SUCCESS) {
11546c9134c0SMark Murray 		syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e));
11556c9134c0SMark Murray 		rval = -1;
11566c9134c0SMark Murray 	}
11576c9134c0SMark Murray 	return rval;
11586c9134c0SMark Murray }
11596c9134c0SMark Murray 
11606c9134c0SMark Murray #endif /* !defined(NOPAM) */
11616c9134c0SMark Murray 
1162ea022d16SRodney W. Grimes void
1163ea022d16SRodney W. Grimes pass(passwd)
1164ea022d16SRodney W. Grimes 	char *passwd;
1165ea022d16SRodney W. Grimes {
1166a5a4544eSPaul Traina 	int rval;
1167ea022d16SRodney W. Grimes 	FILE *fd;
1168b071c689SDavid Nugent #ifdef	LOGIN_CAP
1169b071c689SDavid Nugent 	login_cap_t *lc = NULL;
1170b071c689SDavid Nugent #endif
1171ea022d16SRodney W. Grimes 
1172ea022d16SRodney W. Grimes 	if (logged_in || askpasswd == 0) {
1173ea022d16SRodney W. Grimes 		reply(503, "Login with USER first.");
1174ea022d16SRodney W. Grimes 		return;
1175ea022d16SRodney W. Grimes 	}
1176ea022d16SRodney W. Grimes 	askpasswd = 0;
1177ea022d16SRodney W. Grimes 	if (!guest) {		/* "ftp" is only account allowed no password */
1178a5a4544eSPaul Traina 		if (pw == NULL) {
1179a5a4544eSPaul Traina 			rval = 1;	/* failure below */
1180a5a4544eSPaul Traina 			goto skip;
1181a5a4544eSPaul Traina 		}
11826c9134c0SMark Murray #if !defined(NOPAM)
11836c9134c0SMark Murray 		rval = auth_pam(&pw, passwd);
11846c9134c0SMark Murray 		if (rval >= 0)
1185a5a4544eSPaul Traina 			goto skip;
1186a5a4544eSPaul Traina #endif
1187726040deSGuido van Rooij #ifdef SKEY
1188a5a4544eSPaul Traina 		rval = strcmp(skey_crypt(passwd, pw->pw_passwd, pw, pwok),
11890bb6e9edSPoul-Henning Kamp 			      pw->pw_passwd);
1190bb56d435SPaul Traina 		pwok = 0;
1191726040deSGuido van Rooij #else
11923cde2031SPoul-Henning Kamp 		rval = strcmp(crypt(passwd, pw->pw_passwd), pw->pw_passwd);
1193726040deSGuido van Rooij #endif
1194ea022d16SRodney W. Grimes 		/* The strcmp does not catch null passwords! */
1195a5a4544eSPaul Traina 		if (*pw->pw_passwd == '\0' ||
1196a5a4544eSPaul Traina 		    (pw->pw_expire && time(NULL) >= pw->pw_expire))
1197a5a4544eSPaul Traina 			rval = 1;	/* failure */
1198a5a4544eSPaul Traina skip:
1199a5a4544eSPaul Traina 		/*
1200a5a4544eSPaul Traina 		 * If rval == 1, the user failed the authentication check
12016c9134c0SMark Murray 		 * above.  If rval == 0, either PAM or local authentication
1202a5a4544eSPaul Traina 		 * succeeded.
1203a5a4544eSPaul Traina 		 */
1204a5a4544eSPaul Traina 		if (rval) {
1205ea022d16SRodney W. Grimes 			reply(530, "Login incorrect.");
1206ea022d16SRodney W. Grimes 			if (logging)
1207ea022d16SRodney W. Grimes 				syslog(LOG_NOTICE,
1208ea022d16SRodney W. Grimes 				    "FTP LOGIN FAILED FROM %s, %s",
1209ea022d16SRodney W. Grimes 				    remotehost, curname);
1210ea022d16SRodney W. Grimes 			pw = NULL;
1211ea022d16SRodney W. Grimes 			if (login_attempts++ >= 5) {
1212ea022d16SRodney W. Grimes 				syslog(LOG_NOTICE,
1213ea022d16SRodney W. Grimes 				    "repeated login failures from %s",
1214ea022d16SRodney W. Grimes 				    remotehost);
1215ea022d16SRodney W. Grimes 				exit(0);
1216ea022d16SRodney W. Grimes 			}
1217ea022d16SRodney W. Grimes 			return;
1218ea022d16SRodney W. Grimes 		}
1219ea022d16SRodney W. Grimes 	}
1220ea022d16SRodney W. Grimes 	login_attempts = 0;		/* this time successful */
1221ea022d16SRodney W. Grimes 	if (setegid((gid_t)pw->pw_gid) < 0) {
1222ea022d16SRodney W. Grimes 		reply(550, "Can't set gid.");
1223ea022d16SRodney W. Grimes 		return;
1224ea022d16SRodney W. Grimes 	}
1225b071c689SDavid Nugent 	/* May be overridden by login.conf */
1226b071c689SDavid Nugent 	(void) umask(defumask);
1227b071c689SDavid Nugent #ifdef	LOGIN_CAP
12285d0bfe39SDavid Nugent 	if ((lc = login_getpwclass(pw)) != NULL) {
1229b071c689SDavid Nugent 		char	remote_ip[MAXHOSTNAMELEN];
1230b071c689SDavid Nugent 
12314dd8b5abSYoshinobu Inoue 		getnameinfo((struct sockaddr *)&his_addr, his_addr.su_len,
12324dd8b5abSYoshinobu Inoue 			remote_ip, sizeof(remote_ip) - 1, NULL, 0,
12334dd8b5abSYoshinobu Inoue 			NI_NUMERICHOST|NI_WITHSCOPEID);
1234b071c689SDavid Nugent 		remote_ip[sizeof(remote_ip) - 1] = 0;
1235b071c689SDavid Nugent 		if (!auth_hostok(lc, remotehost, remote_ip)) {
1236b071c689SDavid Nugent 			syslog(LOG_INFO|LOG_AUTH,
1237b071c689SDavid Nugent 			    "FTP LOGIN FAILED (HOST) as %s: permission denied.",
1238b071c689SDavid Nugent 			    pw->pw_name);
1239b071c689SDavid Nugent 			reply(530, "Permission denied.\n");
1240b071c689SDavid Nugent 			pw = NULL;
1241b071c689SDavid Nugent 			return;
1242b071c689SDavid Nugent 		}
1243b071c689SDavid Nugent 		if (!auth_timeok(lc, time(NULL))) {
1244b071c689SDavid Nugent 			reply(530, "Login not available right now.\n");
1245b071c689SDavid Nugent 			pw = NULL;
1246b071c689SDavid Nugent 			return;
1247b071c689SDavid Nugent 		}
1248b071c689SDavid Nugent 	}
1249b071c689SDavid Nugent 	setusercontext(lc, pw, (uid_t)0,
1250e6fa0d43SDag-Erling Smørgrav 		LOGIN_SETLOGIN|LOGIN_SETGROUP|LOGIN_SETPRIORITY|
1251e6fa0d43SDag-Erling Smørgrav 		LOGIN_SETRESOURCES|LOGIN_SETUMASK);
1252b071c689SDavid Nugent #else
1253e6fa0d43SDag-Erling Smørgrav 	setlogin(pw->pw_name);
1254ea022d16SRodney W. Grimes 	(void) initgroups(pw->pw_name, pw->pw_gid);
1255b071c689SDavid Nugent #endif
1256ea022d16SRodney W. Grimes 
1257ea022d16SRodney W. Grimes 	/* open wtmp before chroot */
1258986a1172SThomas Gellekum 	ftpd_logwtmp(ttyline, pw->pw_name, remotehost);
1259ea022d16SRodney W. Grimes 	logged_in = 1;
1260ea022d16SRodney W. Grimes 
1261a5a4544eSPaul Traina 	if (guest && stats && statfd < 0)
1262ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
1263ea4e54b9SDavid Nugent 		if ((statfd = open(thishost->statfile, O_WRONLY|O_APPEND)) < 0)
1264ea4e54b9SDavid Nugent #else
12653eb568f2SGuido van Rooij 		if ((statfd = open(_PATH_FTPDSTATFILE, O_WRONLY|O_APPEND)) < 0)
1266ea4e54b9SDavid Nugent #endif
12673eb568f2SGuido van Rooij 			stats = 0;
12683eb568f2SGuido van Rooij 
1269b071c689SDavid Nugent 	dochroot =
1270b071c689SDavid Nugent #ifdef	LOGIN_CAP	/* Allow login.conf configuration as well */
1271b071c689SDavid Nugent 		login_getcapbool(lc, "ftp-chroot", 0) ||
1272b071c689SDavid Nugent #endif
12737edcb936SSteve Price 		checkuser(_PATH_FTPCHROOT, pw->pw_name, 1);
1274ea022d16SRodney W. Grimes 	if (guest) {
1275ea022d16SRodney W. Grimes 		/*
1276ea022d16SRodney W. Grimes 		 * We MUST do a chdir() after the chroot. Otherwise
1277ea022d16SRodney W. Grimes 		 * the old current directory will be accessible as "."
1278ea022d16SRodney W. Grimes 		 * outside the new root!
1279ea022d16SRodney W. Grimes 		 */
1280ea022d16SRodney W. Grimes 		if (chroot(pw->pw_dir) < 0 || chdir("/") < 0) {
1281ea022d16SRodney W. Grimes 			reply(550, "Can't set guest privileges.");
1282ea022d16SRodney W. Grimes 			goto bad;
1283ea022d16SRodney W. Grimes 		}
1284a5a4544eSPaul Traina 	} else if (dochroot) {
1285a5a4544eSPaul Traina 		if (chroot(pw->pw_dir) < 0 || chdir("/") < 0) {
1286a5a4544eSPaul Traina 			reply(550, "Can't change root.");
1287a5a4544eSPaul Traina 			goto bad;
1288a5a4544eSPaul Traina 		}
1289ea022d16SRodney W. Grimes 	} else if (chdir(pw->pw_dir) < 0) {
1290ea022d16SRodney W. Grimes 		if (chdir("/") < 0) {
1291ea022d16SRodney W. Grimes 			reply(530, "User %s: can't change directory to %s.",
1292ea022d16SRodney W. Grimes 			    pw->pw_name, pw->pw_dir);
1293ea022d16SRodney W. Grimes 			goto bad;
1294ea022d16SRodney W. Grimes 		} else
1295ea022d16SRodney W. Grimes 			lreply(230, "No directory! Logging in with home=/");
1296ea022d16SRodney W. Grimes 	}
1297ea022d16SRodney W. Grimes 	if (seteuid((uid_t)pw->pw_uid) < 0) {
1298ea022d16SRodney W. Grimes 		reply(550, "Can't set uid.");
1299ea022d16SRodney W. Grimes 		goto bad;
1300ea022d16SRodney W. Grimes 	}
130182c76939SDavid Greenman 
130282c76939SDavid Greenman 	/*
1303ea022d16SRodney W. Grimes 	 * Display a login message, if it exists.
1304ea022d16SRodney W. Grimes 	 * N.B. reply(230,) must follow the message.
1305ea022d16SRodney W. Grimes 	 */
1306ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
1307ea4e54b9SDavid Nugent 	if ((fd = fopen(thishost->loginmsg, "r")) != NULL) {
1308ea4e54b9SDavid Nugent #else
1309ea022d16SRodney W. Grimes 	if ((fd = fopen(_PATH_FTPLOGINMESG, "r")) != NULL) {
1310ea4e54b9SDavid Nugent #endif
1311ea022d16SRodney W. Grimes 		char *cp, line[LINE_MAX];
1312ea022d16SRodney W. Grimes 
1313ea022d16SRodney W. Grimes 		while (fgets(line, sizeof(line), fd) != NULL) {
1314ea022d16SRodney W. Grimes 			if ((cp = strchr(line, '\n')) != NULL)
1315ea022d16SRodney W. Grimes 				*cp = '\0';
1316ea022d16SRodney W. Grimes 			lreply(230, "%s", line);
1317ea022d16SRodney W. Grimes 		}
1318ea022d16SRodney W. Grimes 		(void) fflush(stdout);
1319ea022d16SRodney W. Grimes 		(void) fclose(fd);
1320ea022d16SRodney W. Grimes 	}
1321ea022d16SRodney W. Grimes 	if (guest) {
13223eb568f2SGuido van Rooij 		if (ident != NULL)
13233eb568f2SGuido van Rooij 			free(ident);
132461f891a6SPaul Traina 		ident = strdup(passwd);
132561f891a6SPaul Traina 		if (ident == NULL)
132661f891a6SPaul Traina 			fatal("Ran out of memory.");
132761f891a6SPaul Traina 
1328ea022d16SRodney W. Grimes 		reply(230, "Guest login ok, access restrictions apply.");
1329ea022d16SRodney W. Grimes #ifdef SETPROCTITLE
1330ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
1331ea4e54b9SDavid Nugent 		if (thishost != firsthost)
1332ea4e54b9SDavid Nugent 			snprintf(proctitle, sizeof(proctitle),
1333ea4e54b9SDavid Nugent 				 "%s: anonymous(%s)/%.*s", remotehost, hostname,
13346c9134c0SMark Murray 				 (int)(sizeof(proctitle) - sizeof(remotehost) -
13356c9134c0SMark Murray 				 sizeof(": anonymous/")), passwd);
1336ea4e54b9SDavid Nugent 		else
1337ea4e54b9SDavid Nugent #endif
1338ea022d16SRodney W. Grimes 			snprintf(proctitle, sizeof(proctitle),
1339ea022d16SRodney W. Grimes 				 "%s: anonymous/%.*s", remotehost,
13406c9134c0SMark Murray 				 (int)(sizeof(proctitle) - sizeof(remotehost) -
13416c9134c0SMark Murray 				 sizeof(": anonymous/")), passwd);
1342b63e1fe2SPeter Wemm 		setproctitle("%s", proctitle);
1343ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */
1344ea022d16SRodney W. Grimes 		if (logging)
1345ea022d16SRodney W. Grimes 			syslog(LOG_INFO, "ANONYMOUS FTP LOGIN FROM %s, %s",
1346ea022d16SRodney W. Grimes 			    remotehost, passwd);
1347ea022d16SRodney W. Grimes 	} else {
13483401a71fSDaniel O'Callaghan 	    if (dochroot)
13493401a71fSDaniel O'Callaghan 		reply(230, "User %s logged in, access restrictions apply.",
13503401a71fSDaniel O'Callaghan 			pw->pw_name);
13513401a71fSDaniel O'Callaghan 	    else
1352ea022d16SRodney W. Grimes 		reply(230, "User %s logged in.", pw->pw_name);
13533401a71fSDaniel O'Callaghan 
1354ea022d16SRodney W. Grimes #ifdef SETPROCTITLE
1355ea022d16SRodney W. Grimes 		snprintf(proctitle, sizeof(proctitle),
1356ea022d16SRodney W. Grimes 			 "%s: %s", remotehost, pw->pw_name);
1357b63e1fe2SPeter Wemm 		setproctitle("%s", proctitle);
1358ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */
1359ea022d16SRodney W. Grimes 		if (logging)
1360ea022d16SRodney W. Grimes 			syslog(LOG_INFO, "FTP LOGIN FROM %s as %s",
1361ea022d16SRodney W. Grimes 			    remotehost, pw->pw_name);
1362ea022d16SRodney W. Grimes 	}
1363b071c689SDavid Nugent #ifdef	LOGIN_CAP
1364b071c689SDavid Nugent 	login_close(lc);
1365b071c689SDavid Nugent #endif
1366ea022d16SRodney W. Grimes 	return;
1367ea022d16SRodney W. Grimes bad:
1368ea022d16SRodney W. Grimes 	/* Forget all about it... */
1369b071c689SDavid Nugent #ifdef	LOGIN_CAP
1370b071c689SDavid Nugent 	login_close(lc);
1371b071c689SDavid Nugent #endif
1372ea022d16SRodney W. Grimes 	end_login();
1373ea022d16SRodney W. Grimes }
1374ea022d16SRodney W. Grimes 
1375ea022d16SRodney W. Grimes void
1376ea022d16SRodney W. Grimes retrieve(cmd, name)
1377ea022d16SRodney W. Grimes 	char *cmd, *name;
1378ea022d16SRodney W. Grimes {
1379ea022d16SRodney W. Grimes 	FILE *fin, *dout;
1380ea022d16SRodney W. Grimes 	struct stat st;
1381ea022d16SRodney W. Grimes 	int (*closefunc) __P((FILE *));
1382158a00b2SJohn Birrell 	time_t start;
1383ea022d16SRodney W. Grimes 
1384ea022d16SRodney W. Grimes 	if (cmd == 0) {
1385ea022d16SRodney W. Grimes 		fin = fopen(name, "r"), closefunc = fclose;
1386ea022d16SRodney W. Grimes 		st.st_size = 0;
1387ea022d16SRodney W. Grimes 	} else {
1388ea022d16SRodney W. Grimes 		char line[BUFSIZ];
1389ea022d16SRodney W. Grimes 
1390e760ef2cSWarner Losh 		(void) snprintf(line, sizeof(line), cmd, name), name = line;
1391ea022d16SRodney W. Grimes 		fin = ftpd_popen(line, "r"), closefunc = ftpd_pclose;
1392ea022d16SRodney W. Grimes 		st.st_size = -1;
1393ea022d16SRodney W. Grimes 		st.st_blksize = BUFSIZ;
1394ea022d16SRodney W. Grimes 	}
1395ea022d16SRodney W. Grimes 	if (fin == NULL) {
1396ea022d16SRodney W. Grimes 		if (errno != 0) {
1397ea022d16SRodney W. Grimes 			perror_reply(550, name);
1398ea022d16SRodney W. Grimes 			if (cmd == 0) {
1399ea022d16SRodney W. Grimes 				LOGCMD("get", name);
1400ea022d16SRodney W. Grimes 			}
1401ea022d16SRodney W. Grimes 		}
1402ea022d16SRodney W. Grimes 		return;
1403ea022d16SRodney W. Grimes 	}
1404ea022d16SRodney W. Grimes 	byte_count = -1;
1405ea022d16SRodney W. Grimes 	if (cmd == 0 && (fstat(fileno(fin), &st) < 0 || !S_ISREG(st.st_mode))) {
1406ea022d16SRodney W. Grimes 		reply(550, "%s: not a plain file.", name);
1407ea022d16SRodney W. Grimes 		goto done;
1408ea022d16SRodney W. Grimes 	}
1409ea022d16SRodney W. Grimes 	if (restart_point) {
1410ea022d16SRodney W. Grimes 		if (type == TYPE_A) {
1411ea022d16SRodney W. Grimes 			off_t i, n;
1412ea022d16SRodney W. Grimes 			int c;
1413ea022d16SRodney W. Grimes 
1414ea022d16SRodney W. Grimes 			n = restart_point;
1415ea022d16SRodney W. Grimes 			i = 0;
1416ea022d16SRodney W. Grimes 			while (i++ < n) {
1417ea022d16SRodney W. Grimes 				if ((c=getc(fin)) == EOF) {
1418ea022d16SRodney W. Grimes 					perror_reply(550, name);
1419ea022d16SRodney W. Grimes 					goto done;
1420ea022d16SRodney W. Grimes 				}
1421ea022d16SRodney W. Grimes 				if (c == '\n')
1422ea022d16SRodney W. Grimes 					i++;
1423ea022d16SRodney W. Grimes 			}
1424ea022d16SRodney W. Grimes 		} else if (lseek(fileno(fin), restart_point, L_SET) < 0) {
1425ea022d16SRodney W. Grimes 			perror_reply(550, name);
1426ea022d16SRodney W. Grimes 			goto done;
1427ea022d16SRodney W. Grimes 		}
1428ea022d16SRodney W. Grimes 	}
1429ea022d16SRodney W. Grimes 	dout = dataconn(name, st.st_size, "w");
1430ea022d16SRodney W. Grimes 	if (dout == NULL)
1431ea022d16SRodney W. Grimes 		goto done;
14323eb568f2SGuido van Rooij 	time(&start);
14339fc5823aSGarrett Wollman 	send_data(fin, dout, st.st_blksize, st.st_size,
14349fc5823aSGarrett Wollman 		  restart_point == 0 && cmd == 0 && S_ISREG(st.st_mode));
14353eb568f2SGuido van Rooij 	if (cmd == 0 && guest && stats)
14363eb568f2SGuido van Rooij 		logxfer(name, st.st_size, start);
1437ea022d16SRodney W. Grimes 	(void) fclose(dout);
1438ea022d16SRodney W. Grimes 	data = -1;
1439ea022d16SRodney W. Grimes 	pdata = -1;
1440ea022d16SRodney W. Grimes done:
1441ea022d16SRodney W. Grimes 	if (cmd == 0)
1442ea022d16SRodney W. Grimes 		LOGBYTES("get", name, byte_count);
1443ea022d16SRodney W. Grimes 	(*closefunc)(fin);
1444ea022d16SRodney W. Grimes }
1445ea022d16SRodney W. Grimes 
1446ea022d16SRodney W. Grimes void
1447ea022d16SRodney W. Grimes store(name, mode, unique)
1448ea022d16SRodney W. Grimes 	char *name, *mode;
1449ea022d16SRodney W. Grimes 	int unique;
1450ea022d16SRodney W. Grimes {
1451ea022d16SRodney W. Grimes 	FILE *fout, *din;
1452ea022d16SRodney W. Grimes 	struct stat st;
1453ea022d16SRodney W. Grimes 	int (*closefunc) __P((FILE *));
1454ea022d16SRodney W. Grimes 
145561f891a6SPaul Traina 	if ((unique || guest) && stat(name, &st) == 0 &&
1456ea022d16SRodney W. Grimes 	    (name = gunique(name)) == NULL) {
1457ea022d16SRodney W. Grimes 		LOGCMD(*mode == 'w' ? "put" : "append", name);
1458ea022d16SRodney W. Grimes 		return;
1459ea022d16SRodney W. Grimes 	}
1460ea022d16SRodney W. Grimes 
1461ea022d16SRodney W. Grimes 	if (restart_point)
1462ea022d16SRodney W. Grimes 		mode = "r+";
1463ea022d16SRodney W. Grimes 	fout = fopen(name, mode);
1464ea022d16SRodney W. Grimes 	closefunc = fclose;
1465ea022d16SRodney W. Grimes 	if (fout == NULL) {
1466ea022d16SRodney W. Grimes 		perror_reply(553, name);
1467ea022d16SRodney W. Grimes 		LOGCMD(*mode == 'w' ? "put" : "append", name);
1468ea022d16SRodney W. Grimes 		return;
1469ea022d16SRodney W. Grimes 	}
1470ea022d16SRodney W. Grimes 	byte_count = -1;
1471ea022d16SRodney W. Grimes 	if (restart_point) {
1472ea022d16SRodney W. Grimes 		if (type == TYPE_A) {
1473ea022d16SRodney W. Grimes 			off_t i, n;
1474ea022d16SRodney W. Grimes 			int c;
1475ea022d16SRodney W. Grimes 
1476ea022d16SRodney W. Grimes 			n = restart_point;
1477ea022d16SRodney W. Grimes 			i = 0;
1478ea022d16SRodney W. Grimes 			while (i++ < n) {
1479ea022d16SRodney W. Grimes 				if ((c=getc(fout)) == EOF) {
1480ea022d16SRodney W. Grimes 					perror_reply(550, name);
1481ea022d16SRodney W. Grimes 					goto done;
1482ea022d16SRodney W. Grimes 				}
1483ea022d16SRodney W. Grimes 				if (c == '\n')
1484ea022d16SRodney W. Grimes 					i++;
1485ea022d16SRodney W. Grimes 			}
1486ea022d16SRodney W. Grimes 			/*
1487ea022d16SRodney W. Grimes 			 * We must do this seek to "current" position
1488ea022d16SRodney W. Grimes 			 * because we are changing from reading to
1489ea022d16SRodney W. Grimes 			 * writing.
1490ea022d16SRodney W. Grimes 			 */
1491ea022d16SRodney W. Grimes 			if (fseek(fout, 0L, L_INCR) < 0) {
1492ea022d16SRodney W. Grimes 				perror_reply(550, name);
1493ea022d16SRodney W. Grimes 				goto done;
1494ea022d16SRodney W. Grimes 			}
1495ea022d16SRodney W. Grimes 		} else if (lseek(fileno(fout), restart_point, L_SET) < 0) {
1496ea022d16SRodney W. Grimes 			perror_reply(550, name);
1497ea022d16SRodney W. Grimes 			goto done;
1498ea022d16SRodney W. Grimes 		}
1499ea022d16SRodney W. Grimes 	}
1500ea022d16SRodney W. Grimes 	din = dataconn(name, (off_t)-1, "r");
1501ea022d16SRodney W. Grimes 	if (din == NULL)
1502ea022d16SRodney W. Grimes 		goto done;
1503ea022d16SRodney W. Grimes 	if (receive_data(din, fout) == 0) {
1504ea022d16SRodney W. Grimes 		if (unique)
1505ea022d16SRodney W. Grimes 			reply(226, "Transfer complete (unique file name:%s).",
1506ea022d16SRodney W. Grimes 			    name);
1507ea022d16SRodney W. Grimes 		else
1508ea022d16SRodney W. Grimes 			reply(226, "Transfer complete.");
1509ea022d16SRodney W. Grimes 	}
1510ea022d16SRodney W. Grimes 	(void) fclose(din);
1511ea022d16SRodney W. Grimes 	data = -1;
1512ea022d16SRodney W. Grimes 	pdata = -1;
1513ea022d16SRodney W. Grimes done:
1514ea022d16SRodney W. Grimes 	LOGBYTES(*mode == 'w' ? "put" : "append", name, byte_count);
1515ea022d16SRodney W. Grimes 	(*closefunc)(fout);
1516ea022d16SRodney W. Grimes }
1517ea022d16SRodney W. Grimes 
1518ea022d16SRodney W. Grimes static FILE *
1519ea022d16SRodney W. Grimes getdatasock(mode)
1520ea022d16SRodney W. Grimes 	char *mode;
1521ea022d16SRodney W. Grimes {
1522ea022d16SRodney W. Grimes 	int on = 1, s, t, tries;
1523ea022d16SRodney W. Grimes 
1524ea022d16SRodney W. Grimes 	if (data >= 0)
1525ea022d16SRodney W. Grimes 		return (fdopen(data, mode));
1526ea022d16SRodney W. Grimes 	(void) seteuid((uid_t)0);
15274dd8b5abSYoshinobu Inoue 
15284dd8b5abSYoshinobu Inoue 	s = socket(data_dest.su_family, SOCK_STREAM, 0);
1529ea022d16SRodney W. Grimes 	if (s < 0)
1530ea022d16SRodney W. Grimes 		goto bad;
1531ea022d16SRodney W. Grimes 	if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
1532ea022d16SRodney W. Grimes 	    (char *) &on, sizeof(on)) < 0)
1533ea022d16SRodney W. Grimes 		goto bad;
1534ea022d16SRodney W. Grimes 	/* anchor socket to avoid multi-homing problems */
15354dd8b5abSYoshinobu Inoue 	data_source = ctrl_addr;
15364dd8b5abSYoshinobu Inoue 	data_source.su_port = htons(20); /* ftp-data port */
1537ea022d16SRodney W. Grimes 	for (tries = 1; ; tries++) {
1538ea022d16SRodney W. Grimes 		if (bind(s, (struct sockaddr *)&data_source,
15394dd8b5abSYoshinobu Inoue 		    data_source.su_len) >= 0)
1540ea022d16SRodney W. Grimes 			break;
1541ea022d16SRodney W. Grimes 		if (errno != EADDRINUSE || tries > 10)
1542ea022d16SRodney W. Grimes 			goto bad;
1543ea022d16SRodney W. Grimes 		sleep(tries);
1544ea022d16SRodney W. Grimes 	}
1545ea022d16SRodney W. Grimes 	(void) seteuid((uid_t)pw->pw_uid);
1546ea022d16SRodney W. Grimes #ifdef IP_TOS
15474dd8b5abSYoshinobu Inoue 	if (data_source.su_family == AF_INET)
15484dd8b5abSYoshinobu Inoue       {
1549ea022d16SRodney W. Grimes 	on = IPTOS_THROUGHPUT;
1550ea022d16SRodney W. Grimes 	if (setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0)
1551ea022d16SRodney W. Grimes 		syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
15524dd8b5abSYoshinobu Inoue       }
1553ea022d16SRodney W. Grimes #endif
15549fc5823aSGarrett Wollman #ifdef TCP_NOPUSH
15559fc5823aSGarrett Wollman 	/*
15569fc5823aSGarrett Wollman 	 * Turn off push flag to keep sender TCP from sending short packets
15579fc5823aSGarrett Wollman 	 * at the boundaries of each write().  Should probably do a SO_SNDBUF
15589fc5823aSGarrett Wollman 	 * to set the send buffer size as well, but that may not be desirable
15599fc5823aSGarrett Wollman 	 * in heavy-load situations.
15609fc5823aSGarrett Wollman 	 */
15619fc5823aSGarrett Wollman 	on = 1;
15629fc5823aSGarrett Wollman 	if (setsockopt(s, IPPROTO_TCP, TCP_NOPUSH, (char *)&on, sizeof on) < 0)
15639fc5823aSGarrett Wollman 		syslog(LOG_WARNING, "setsockopt (TCP_NOPUSH): %m");
15649fc5823aSGarrett Wollman #endif
15659fc5823aSGarrett Wollman #ifdef SO_SNDBUF
15669fc5823aSGarrett Wollman 	on = 65536;
15679fc5823aSGarrett Wollman 	if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, (char *)&on, sizeof on) < 0)
15689fc5823aSGarrett Wollman 		syslog(LOG_WARNING, "setsockopt (SO_SNDBUF): %m");
15699fc5823aSGarrett Wollman #endif
15709fc5823aSGarrett Wollman 
1571ea022d16SRodney W. Grimes 	return (fdopen(s, mode));
1572ea022d16SRodney W. Grimes bad:
1573ea022d16SRodney W. Grimes 	/* Return the real value of errno (close may change it) */
1574ea022d16SRodney W. Grimes 	t = errno;
1575ea022d16SRodney W. Grimes 	(void) seteuid((uid_t)pw->pw_uid);
1576ea022d16SRodney W. Grimes 	(void) close(s);
1577ea022d16SRodney W. Grimes 	errno = t;
1578ea022d16SRodney W. Grimes 	return (NULL);
1579ea022d16SRodney W. Grimes }
1580ea022d16SRodney W. Grimes 
1581ea022d16SRodney W. Grimes static FILE *
1582ea022d16SRodney W. Grimes dataconn(name, size, mode)
1583ea022d16SRodney W. Grimes 	char *name;
1584ea022d16SRodney W. Grimes 	off_t size;
1585ea022d16SRodney W. Grimes 	char *mode;
1586ea022d16SRodney W. Grimes {
1587ea022d16SRodney W. Grimes 	char sizebuf[32];
1588ea022d16SRodney W. Grimes 	FILE *file;
1589ea022d16SRodney W. Grimes 	int retry = 0, tos;
1590ea022d16SRodney W. Grimes 
1591ea022d16SRodney W. Grimes 	file_size = size;
1592ea022d16SRodney W. Grimes 	byte_count = 0;
1593ea022d16SRodney W. Grimes 	if (size != (off_t) -1)
1594e760ef2cSWarner Losh 		(void) snprintf(sizebuf, sizeof(sizebuf), " (%qd bytes)", size);
1595ea022d16SRodney W. Grimes 	else
1596e760ef2cSWarner Losh 		*sizebuf = '\0';
1597ea022d16SRodney W. Grimes 	if (pdata >= 0) {
15984dd8b5abSYoshinobu Inoue 		union sockunion from;
15994dd8b5abSYoshinobu Inoue 		int s, fromlen = ctrl_addr.su_len;
1600d6ed3c37SGuido van Rooij 		struct timeval timeout;
1601d6ed3c37SGuido van Rooij 		fd_set set;
1602ea022d16SRodney W. Grimes 
1603d6ed3c37SGuido van Rooij 		FD_ZERO(&set);
1604d6ed3c37SGuido van Rooij 		FD_SET(pdata, &set);
1605d6ed3c37SGuido van Rooij 
1606d6ed3c37SGuido van Rooij 		timeout.tv_usec = 0;
1607d6ed3c37SGuido van Rooij 		timeout.tv_sec = 120;
1608d6ed3c37SGuido van Rooij 
1609d6ed3c37SGuido van Rooij 		if (select(pdata+1, &set, (fd_set *) 0, (fd_set *) 0, &timeout) == 0 ||
1610d6ed3c37SGuido van Rooij 		    (s = accept(pdata, (struct sockaddr *) &from, &fromlen)) < 0) {
1611ea022d16SRodney W. Grimes 			reply(425, "Can't open data connection.");
1612ea022d16SRodney W. Grimes 			(void) close(pdata);
1613ea022d16SRodney W. Grimes 			pdata = -1;
1614ea022d16SRodney W. Grimes 			return (NULL);
1615ea022d16SRodney W. Grimes 		}
1616ea022d16SRodney W. Grimes 		(void) close(pdata);
1617ea022d16SRodney W. Grimes 		pdata = s;
1618ea022d16SRodney W. Grimes #ifdef IP_TOS
16194dd8b5abSYoshinobu Inoue 		if (from.su_family == AF_INET)
16204dd8b5abSYoshinobu Inoue 	      {
1621a5a4544eSPaul Traina 		tos = IPTOS_THROUGHPUT;
1622ea022d16SRodney W. Grimes 		(void) setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&tos,
1623ea022d16SRodney W. Grimes 		    sizeof(int));
16244dd8b5abSYoshinobu Inoue 	      }
1625ea022d16SRodney W. Grimes #endif
1626ea022d16SRodney W. Grimes 		reply(150, "Opening %s mode data connection for '%s'%s.",
1627ea022d16SRodney W. Grimes 		     type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf);
1628ea022d16SRodney W. Grimes 		return (fdopen(pdata, mode));
1629ea022d16SRodney W. Grimes 	}
1630ea022d16SRodney W. Grimes 	if (data >= 0) {
1631ea022d16SRodney W. Grimes 		reply(125, "Using existing data connection for '%s'%s.",
1632ea022d16SRodney W. Grimes 		    name, sizebuf);
1633ea022d16SRodney W. Grimes 		usedefault = 1;
1634ea022d16SRodney W. Grimes 		return (fdopen(data, mode));
1635ea022d16SRodney W. Grimes 	}
1636ea022d16SRodney W. Grimes 	if (usedefault)
1637ea022d16SRodney W. Grimes 		data_dest = his_addr;
1638ea022d16SRodney W. Grimes 	usedefault = 1;
1639ea022d16SRodney W. Grimes 	file = getdatasock(mode);
1640ea022d16SRodney W. Grimes 	if (file == NULL) {
16414dd8b5abSYoshinobu Inoue 		char hostbuf[BUFSIZ], portbuf[BUFSIZ];
16424dd8b5abSYoshinobu Inoue 		getnameinfo((struct sockaddr *)&data_source,
16434dd8b5abSYoshinobu Inoue 			data_source.su_len, hostbuf, sizeof(hostbuf) - 1,
16444dd8b5abSYoshinobu Inoue 			portbuf, sizeof(portbuf),
16454dd8b5abSYoshinobu Inoue 			NI_NUMERICHOST|NI_NUMERICSERV|NI_WITHSCOPEID);
16464dd8b5abSYoshinobu Inoue 		reply(425, "Can't create data socket (%s,%s): %s.",
16474dd8b5abSYoshinobu Inoue 			hostbuf, portbuf, strerror(errno));
1648ea022d16SRodney W. Grimes 		return (NULL);
1649ea022d16SRodney W. Grimes 	}
1650ea022d16SRodney W. Grimes 	data = fileno(file);
1651ea022d16SRodney W. Grimes 	while (connect(data, (struct sockaddr *)&data_dest,
16524dd8b5abSYoshinobu Inoue 	    data_dest.su_len) < 0) {
1653ea022d16SRodney W. Grimes 		if (errno == EADDRINUSE && retry < swaitmax) {
1654ea022d16SRodney W. Grimes 			sleep((unsigned) swaitint);
1655ea022d16SRodney W. Grimes 			retry += swaitint;
1656ea022d16SRodney W. Grimes 			continue;
1657ea022d16SRodney W. Grimes 		}
1658ea022d16SRodney W. Grimes 		perror_reply(425, "Can't build data connection");
1659ea022d16SRodney W. Grimes 		(void) fclose(file);
1660ea022d16SRodney W. Grimes 		data = -1;
1661ea022d16SRodney W. Grimes 		return (NULL);
1662ea022d16SRodney W. Grimes 	}
1663ea022d16SRodney W. Grimes 	reply(150, "Opening %s mode data connection for '%s'%s.",
1664ea022d16SRodney W. Grimes 	     type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf);
1665ea022d16SRodney W. Grimes 	return (file);
1666ea022d16SRodney W. Grimes }
1667ea022d16SRodney W. Grimes 
1668ea022d16SRodney W. Grimes /*
1669ea022d16SRodney W. Grimes  * Tranfer the contents of "instr" to "outstr" peer using the appropriate
16709fc5823aSGarrett Wollman  * encapsulation of the data subject to Mode, Structure, and Type.
1671ea022d16SRodney W. Grimes  *
1672ea022d16SRodney W. Grimes  * NB: Form isn't handled.
1673ea022d16SRodney W. Grimes  */
1674ea022d16SRodney W. Grimes static void
16759fc5823aSGarrett Wollman send_data(instr, outstr, blksize, filesize, isreg)
1676ea022d16SRodney W. Grimes 	FILE *instr, *outstr;
1677ea022d16SRodney W. Grimes 	off_t blksize;
16789fc5823aSGarrett Wollman 	off_t filesize;
16799fc5823aSGarrett Wollman 	int isreg;
1680ea022d16SRodney W. Grimes {
1681ea022d16SRodney W. Grimes 	int c, cnt, filefd, netfd;
16829fc5823aSGarrett Wollman 	char *buf, *bp;
16839fc5823aSGarrett Wollman 	size_t len;
1684ea022d16SRodney W. Grimes 
1685ea022d16SRodney W. Grimes 	transflag++;
1686ea022d16SRodney W. Grimes 	if (setjmp(urgcatch)) {
1687ea022d16SRodney W. Grimes 		transflag = 0;
1688ea022d16SRodney W. Grimes 		return;
1689ea022d16SRodney W. Grimes 	}
1690ea022d16SRodney W. Grimes 	switch (type) {
1691ea022d16SRodney W. Grimes 
1692ea022d16SRodney W. Grimes 	case TYPE_A:
1693ea022d16SRodney W. Grimes 		while ((c = getc(instr)) != EOF) {
1694ea022d16SRodney W. Grimes 			byte_count++;
1695ea022d16SRodney W. Grimes 			if (c == '\n') {
1696ea022d16SRodney W. Grimes 				if (ferror(outstr))
1697ea022d16SRodney W. Grimes 					goto data_err;
1698ea022d16SRodney W. Grimes 				(void) putc('\r', outstr);
1699ea022d16SRodney W. Grimes 			}
1700ea022d16SRodney W. Grimes 			(void) putc(c, outstr);
1701ea022d16SRodney W. Grimes 		}
1702ea022d16SRodney W. Grimes 		fflush(outstr);
1703ea022d16SRodney W. Grimes 		transflag = 0;
1704ea022d16SRodney W. Grimes 		if (ferror(instr))
1705ea022d16SRodney W. Grimes 			goto file_err;
1706ea022d16SRodney W. Grimes 		if (ferror(outstr))
1707ea022d16SRodney W. Grimes 			goto data_err;
1708ea022d16SRodney W. Grimes 		reply(226, "Transfer complete.");
1709ea022d16SRodney W. Grimes 		return;
1710ea022d16SRodney W. Grimes 
1711ea022d16SRodney W. Grimes 	case TYPE_I:
1712ea022d16SRodney W. Grimes 	case TYPE_L:
17139fc5823aSGarrett Wollman 		/*
17149fc5823aSGarrett Wollman 		 * isreg is only set if we are not doing restart and we
17159fc5823aSGarrett Wollman 		 * are sending a regular file
17169fc5823aSGarrett Wollman 		 */
17179fc5823aSGarrett Wollman 		netfd = fileno(outstr);
17189fc5823aSGarrett Wollman 		filefd = fileno(instr);
17199fc5823aSGarrett Wollman 
17209fc5823aSGarrett Wollman 		if (isreg && filesize < (off_t)16 * 1024 * 1024) {
17219fc5823aSGarrett Wollman 			buf = mmap(0, filesize, PROT_READ, MAP_SHARED, filefd,
17229fc5823aSGarrett Wollman 				   (off_t)0);
17238abdc2ebSAlexander Langer 			if (buf == MAP_FAILED) {
17249fc5823aSGarrett Wollman 				syslog(LOG_WARNING, "mmap(%lu): %m",
17259fc5823aSGarrett Wollman 				       (unsigned long)filesize);
17269fc5823aSGarrett Wollman 				goto oldway;
17279fc5823aSGarrett Wollman 			}
17289fc5823aSGarrett Wollman 			bp = buf;
17299fc5823aSGarrett Wollman 			len = filesize;
17309fc5823aSGarrett Wollman 			do {
17319fc5823aSGarrett Wollman 				cnt = write(netfd, bp, len);
17329fc5823aSGarrett Wollman 				len -= cnt;
17339fc5823aSGarrett Wollman 				bp += cnt;
17349fc5823aSGarrett Wollman 				if (cnt > 0) byte_count += cnt;
17359fc5823aSGarrett Wollman 			} while(cnt > 0 && len > 0);
17369fc5823aSGarrett Wollman 
17379fc5823aSGarrett Wollman 			transflag = 0;
17389fc5823aSGarrett Wollman 			munmap(buf, (size_t)filesize);
17399fc5823aSGarrett Wollman 			if (cnt < 0)
17409fc5823aSGarrett Wollman 				goto data_err;
17419fc5823aSGarrett Wollman 			reply(226, "Transfer complete.");
17429fc5823aSGarrett Wollman 			return;
17439fc5823aSGarrett Wollman 		}
17449fc5823aSGarrett Wollman 
17459fc5823aSGarrett Wollman oldway:
1746ea022d16SRodney W. Grimes 		if ((buf = malloc((u_int)blksize)) == NULL) {
1747ea022d16SRodney W. Grimes 			transflag = 0;
1748ea022d16SRodney W. Grimes 			perror_reply(451, "Local resource failure: malloc");
1749ea022d16SRodney W. Grimes 			return;
1750ea022d16SRodney W. Grimes 		}
17519fc5823aSGarrett Wollman 
1752ea022d16SRodney W. Grimes 		while ((cnt = read(filefd, buf, (u_int)blksize)) > 0 &&
1753ea022d16SRodney W. Grimes 		    write(netfd, buf, cnt) == cnt)
1754ea022d16SRodney W. Grimes 			byte_count += cnt;
1755ea022d16SRodney W. Grimes 		transflag = 0;
1756ea022d16SRodney W. Grimes 		(void)free(buf);
1757ea022d16SRodney W. Grimes 		if (cnt != 0) {
1758ea022d16SRodney W. Grimes 			if (cnt < 0)
1759ea022d16SRodney W. Grimes 				goto file_err;
1760ea022d16SRodney W. Grimes 			goto data_err;
1761ea022d16SRodney W. Grimes 		}
1762ea022d16SRodney W. Grimes 		reply(226, "Transfer complete.");
1763ea022d16SRodney W. Grimes 		return;
1764ea022d16SRodney W. Grimes 	default:
1765ea022d16SRodney W. Grimes 		transflag = 0;
1766ea022d16SRodney W. Grimes 		reply(550, "Unimplemented TYPE %d in send_data", type);
1767ea022d16SRodney W. Grimes 		return;
1768ea022d16SRodney W. Grimes 	}
1769ea022d16SRodney W. Grimes 
1770ea022d16SRodney W. Grimes data_err:
1771ea022d16SRodney W. Grimes 	transflag = 0;
1772ea022d16SRodney W. Grimes 	perror_reply(426, "Data connection");
1773ea022d16SRodney W. Grimes 	return;
1774ea022d16SRodney W. Grimes 
1775ea022d16SRodney W. Grimes file_err:
1776ea022d16SRodney W. Grimes 	transflag = 0;
1777ea022d16SRodney W. Grimes 	perror_reply(551, "Error on input file");
1778ea022d16SRodney W. Grimes }
1779ea022d16SRodney W. Grimes 
1780ea022d16SRodney W. Grimes /*
1781ea022d16SRodney W. Grimes  * Transfer data from peer to "outstr" using the appropriate encapulation of
1782ea022d16SRodney W. Grimes  * the data subject to Mode, Structure, and Type.
1783ea022d16SRodney W. Grimes  *
1784ea022d16SRodney W. Grimes  * N.B.: Form isn't handled.
1785ea022d16SRodney W. Grimes  */
1786ea022d16SRodney W. Grimes static int
1787ea022d16SRodney W. Grimes receive_data(instr, outstr)
1788ea022d16SRodney W. Grimes 	FILE *instr, *outstr;
1789ea022d16SRodney W. Grimes {
1790ea022d16SRodney W. Grimes 	int c;
179161f891a6SPaul Traina 	int cnt, bare_lfs;
1792ea022d16SRodney W. Grimes 	char buf[BUFSIZ];
1793ea022d16SRodney W. Grimes 
1794ea022d16SRodney W. Grimes 	transflag++;
1795ea022d16SRodney W. Grimes 	if (setjmp(urgcatch)) {
1796ea022d16SRodney W. Grimes 		transflag = 0;
1797ea022d16SRodney W. Grimes 		return (-1);
1798ea022d16SRodney W. Grimes 	}
179961f891a6SPaul Traina 
180061f891a6SPaul Traina 	bare_lfs = 0;
180161f891a6SPaul Traina 
1802ea022d16SRodney W. Grimes 	switch (type) {
1803ea022d16SRodney W. Grimes 
1804ea022d16SRodney W. Grimes 	case TYPE_I:
1805ea022d16SRodney W. Grimes 	case TYPE_L:
1806ea022d16SRodney W. Grimes 		while ((cnt = read(fileno(instr), buf, sizeof(buf))) > 0) {
1807ea022d16SRodney W. Grimes 			if (write(fileno(outstr), buf, cnt) != cnt)
1808ea022d16SRodney W. Grimes 				goto file_err;
1809ea022d16SRodney W. Grimes 			byte_count += cnt;
1810ea022d16SRodney W. Grimes 		}
1811ea022d16SRodney W. Grimes 		if (cnt < 0)
1812ea022d16SRodney W. Grimes 			goto data_err;
1813ea022d16SRodney W. Grimes 		transflag = 0;
1814ea022d16SRodney W. Grimes 		return (0);
1815ea022d16SRodney W. Grimes 
1816ea022d16SRodney W. Grimes 	case TYPE_E:
1817ea022d16SRodney W. Grimes 		reply(553, "TYPE E not implemented.");
1818ea022d16SRodney W. Grimes 		transflag = 0;
1819ea022d16SRodney W. Grimes 		return (-1);
1820ea022d16SRodney W. Grimes 
1821ea022d16SRodney W. Grimes 	case TYPE_A:
1822ea022d16SRodney W. Grimes 		while ((c = getc(instr)) != EOF) {
1823ea022d16SRodney W. Grimes 			byte_count++;
1824ea022d16SRodney W. Grimes 			if (c == '\n')
1825ea022d16SRodney W. Grimes 				bare_lfs++;
1826ea022d16SRodney W. Grimes 			while (c == '\r') {
1827ea022d16SRodney W. Grimes 				if (ferror(outstr))
1828ea022d16SRodney W. Grimes 					goto data_err;
1829ea022d16SRodney W. Grimes 				if ((c = getc(instr)) != '\n') {
1830ea022d16SRodney W. Grimes 					(void) putc ('\r', outstr);
1831ea022d16SRodney W. Grimes 					if (c == '\0' || c == EOF)
1832ea022d16SRodney W. Grimes 						goto contin2;
1833ea022d16SRodney W. Grimes 				}
1834ea022d16SRodney W. Grimes 			}
1835ea022d16SRodney W. Grimes 			(void) putc(c, outstr);
1836ea022d16SRodney W. Grimes 	contin2:	;
1837ea022d16SRodney W. Grimes 		}
1838ea022d16SRodney W. Grimes 		fflush(outstr);
1839ea022d16SRodney W. Grimes 		if (ferror(instr))
1840ea022d16SRodney W. Grimes 			goto data_err;
1841ea022d16SRodney W. Grimes 		if (ferror(outstr))
1842ea022d16SRodney W. Grimes 			goto file_err;
1843ea022d16SRodney W. Grimes 		transflag = 0;
1844ea022d16SRodney W. Grimes 		if (bare_lfs) {
1845ea022d16SRodney W. Grimes 			lreply(226,
1846ea022d16SRodney W. Grimes 		"WARNING! %d bare linefeeds received in ASCII mode",
1847ea022d16SRodney W. Grimes 			    bare_lfs);
1848ea022d16SRodney W. Grimes 		(void)printf("   File may not have transferred correctly.\r\n");
1849ea022d16SRodney W. Grimes 		}
1850ea022d16SRodney W. Grimes 		return (0);
1851ea022d16SRodney W. Grimes 	default:
1852ea022d16SRodney W. Grimes 		reply(550, "Unimplemented TYPE %d in receive_data", type);
1853ea022d16SRodney W. Grimes 		transflag = 0;
1854ea022d16SRodney W. Grimes 		return (-1);
1855ea022d16SRodney W. Grimes 	}
1856ea022d16SRodney W. Grimes 
1857ea022d16SRodney W. Grimes data_err:
1858ea022d16SRodney W. Grimes 	transflag = 0;
1859ea022d16SRodney W. Grimes 	perror_reply(426, "Data Connection");
1860ea022d16SRodney W. Grimes 	return (-1);
1861ea022d16SRodney W. Grimes 
1862ea022d16SRodney W. Grimes file_err:
1863ea022d16SRodney W. Grimes 	transflag = 0;
1864ea022d16SRodney W. Grimes 	perror_reply(452, "Error writing file");
1865ea022d16SRodney W. Grimes 	return (-1);
1866ea022d16SRodney W. Grimes }
1867ea022d16SRodney W. Grimes 
1868ea022d16SRodney W. Grimes void
1869ea022d16SRodney W. Grimes statfilecmd(filename)
1870ea022d16SRodney W. Grimes 	char *filename;
1871ea022d16SRodney W. Grimes {
1872ea022d16SRodney W. Grimes 	FILE *fin;
1873ea022d16SRodney W. Grimes 	int c;
1874ea022d16SRodney W. Grimes 	char line[LINE_MAX];
1875ea022d16SRodney W. Grimes 
1876af85d782SDavid Nugent 	(void)snprintf(line, sizeof(line), _PATH_LS " -lgA %s", filename);
1877ea022d16SRodney W. Grimes 	fin = ftpd_popen(line, "r");
1878ea022d16SRodney W. Grimes 	lreply(211, "status of %s:", filename);
1879ea022d16SRodney W. Grimes 	while ((c = getc(fin)) != EOF) {
1880ea022d16SRodney W. Grimes 		if (c == '\n') {
1881ea022d16SRodney W. Grimes 			if (ferror(stdout)){
1882ea022d16SRodney W. Grimes 				perror_reply(421, "control connection");
1883ea022d16SRodney W. Grimes 				(void) ftpd_pclose(fin);
1884ea022d16SRodney W. Grimes 				dologout(1);
1885ea022d16SRodney W. Grimes 				/* NOTREACHED */
1886ea022d16SRodney W. Grimes 			}
1887ea022d16SRodney W. Grimes 			if (ferror(fin)) {
1888ea022d16SRodney W. Grimes 				perror_reply(551, filename);
1889ea022d16SRodney W. Grimes 				(void) ftpd_pclose(fin);
1890ea022d16SRodney W. Grimes 				return;
1891ea022d16SRodney W. Grimes 			}
1892ea022d16SRodney W. Grimes 			(void) putc('\r', stdout);
1893ea022d16SRodney W. Grimes 		}
1894ea022d16SRodney W. Grimes 		(void) putc(c, stdout);
1895ea022d16SRodney W. Grimes 	}
1896ea022d16SRodney W. Grimes 	(void) ftpd_pclose(fin);
1897ea022d16SRodney W. Grimes 	reply(211, "End of Status");
1898ea022d16SRodney W. Grimes }
1899ea022d16SRodney W. Grimes 
1900ea022d16SRodney W. Grimes void
1901ea022d16SRodney W. Grimes statcmd()
1902ea022d16SRodney W. Grimes {
19034dd8b5abSYoshinobu Inoue 	union sockunion *su;
1904ea022d16SRodney W. Grimes 	u_char *a, *p;
19054dd8b5abSYoshinobu Inoue 	char hname[INET6_ADDRSTRLEN];
19064dd8b5abSYoshinobu Inoue 	int ispassive;
1907ea022d16SRodney W. Grimes 
1908ea022d16SRodney W. Grimes 	lreply(211, "%s FTP server status:", hostname, version);
1909ea022d16SRodney W. Grimes 	printf("     %s\r\n", version);
1910ea022d16SRodney W. Grimes 	printf("     Connected to %s", remotehost);
19114dd8b5abSYoshinobu Inoue 	if (!getnameinfo((struct sockaddr *)&his_addr, his_addr.su_len,
19124dd8b5abSYoshinobu Inoue 			 hname, sizeof(hname) - 1, NULL, 0,
19134dd8b5abSYoshinobu Inoue 			 NI_NUMERICHOST|NI_WITHSCOPEID)) {
19144dd8b5abSYoshinobu Inoue 		if (strcmp(hname, remotehost) != 0)
19154dd8b5abSYoshinobu Inoue 			printf(" (%s)", hname);
19164dd8b5abSYoshinobu Inoue 	}
1917ea022d16SRodney W. Grimes 	printf("\r\n");
1918ea022d16SRodney W. Grimes 	if (logged_in) {
1919ea022d16SRodney W. Grimes 		if (guest)
1920ea022d16SRodney W. Grimes 			printf("     Logged in anonymously\r\n");
1921ea022d16SRodney W. Grimes 		else
1922ea022d16SRodney W. Grimes 			printf("     Logged in as %s\r\n", pw->pw_name);
1923ea022d16SRodney W. Grimes 	} else if (askpasswd)
1924ea022d16SRodney W. Grimes 		printf("     Waiting for password\r\n");
1925ea022d16SRodney W. Grimes 	else
1926ea022d16SRodney W. Grimes 		printf("     Waiting for user name\r\n");
1927ea022d16SRodney W. Grimes 	printf("     TYPE: %s", typenames[type]);
1928ea022d16SRodney W. Grimes 	if (type == TYPE_A || type == TYPE_E)
1929ea022d16SRodney W. Grimes 		printf(", FORM: %s", formnames[form]);
1930ea022d16SRodney W. Grimes 	if (type == TYPE_L)
1931ea022d16SRodney W. Grimes #if NBBY == 8
1932ea022d16SRodney W. Grimes 		printf(" %d", NBBY);
1933ea022d16SRodney W. Grimes #else
1934ea022d16SRodney W. Grimes 		printf(" %d", bytesize);	/* need definition! */
1935ea022d16SRodney W. Grimes #endif
1936ea022d16SRodney W. Grimes 	printf("; STRUcture: %s; transfer MODE: %s\r\n",
1937ea022d16SRodney W. Grimes 	    strunames[stru], modenames[mode]);
1938ea022d16SRodney W. Grimes 	if (data != -1)
1939ea022d16SRodney W. Grimes 		printf("     Data connection open\r\n");
1940ea022d16SRodney W. Grimes 	else if (pdata != -1) {
19414dd8b5abSYoshinobu Inoue 		ispassive = 1;
19424dd8b5abSYoshinobu Inoue 		su = &pasv_addr;
1943ea022d16SRodney W. Grimes 		goto printaddr;
1944ea022d16SRodney W. Grimes 	} else if (usedefault == 0) {
19454dd8b5abSYoshinobu Inoue 		ispassive = 0;
19464dd8b5abSYoshinobu Inoue 		su = &data_dest;
1947ea022d16SRodney W. Grimes printaddr:
1948ea022d16SRodney W. Grimes #define UC(b) (((int) b) & 0xff)
19494dd8b5abSYoshinobu Inoue 		if (epsvall) {
19504dd8b5abSYoshinobu Inoue 			printf("     EPSV only mode (EPSV ALL)\r\n");
19514dd8b5abSYoshinobu Inoue 			goto epsvonly;
19524dd8b5abSYoshinobu Inoue 		}
19534dd8b5abSYoshinobu Inoue 
19544dd8b5abSYoshinobu Inoue 		/* PORT/PASV */
19554dd8b5abSYoshinobu Inoue 		if (su->su_family == AF_INET) {
19564dd8b5abSYoshinobu Inoue 			a = (u_char *) &su->su_sin.sin_addr;
19574dd8b5abSYoshinobu Inoue 			p = (u_char *) &su->su_sin.sin_port;
19584dd8b5abSYoshinobu Inoue 			printf("     %s (%d,%d,%d,%d,%d,%d)\r\n",
19594dd8b5abSYoshinobu Inoue 				ispassive ? "PASV" : "PORT",
19604dd8b5abSYoshinobu Inoue 				UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
19614dd8b5abSYoshinobu Inoue 				UC(p[0]), UC(p[1]));
19624dd8b5abSYoshinobu Inoue 		}
19634dd8b5abSYoshinobu Inoue 
19644dd8b5abSYoshinobu Inoue 		/* LPRT/LPSV */
19654dd8b5abSYoshinobu Inoue 	    {
19664dd8b5abSYoshinobu Inoue 		int alen, af, i;
19674dd8b5abSYoshinobu Inoue 
19684dd8b5abSYoshinobu Inoue 		switch (su->su_family) {
19694dd8b5abSYoshinobu Inoue 		case AF_INET:
19704dd8b5abSYoshinobu Inoue 			a = (u_char *) &su->su_sin.sin_addr;
19714dd8b5abSYoshinobu Inoue 			p = (u_char *) &su->su_sin.sin_port;
19724dd8b5abSYoshinobu Inoue 			alen = sizeof(su->su_sin.sin_addr);
19734dd8b5abSYoshinobu Inoue 			af = 4;
19744dd8b5abSYoshinobu Inoue 			break;
19754dd8b5abSYoshinobu Inoue 		case AF_INET6:
19764dd8b5abSYoshinobu Inoue 			a = (u_char *) &su->su_sin6.sin6_addr;
19774dd8b5abSYoshinobu Inoue 			p = (u_char *) &su->su_sin6.sin6_port;
19784dd8b5abSYoshinobu Inoue 			alen = sizeof(su->su_sin6.sin6_addr);
19794dd8b5abSYoshinobu Inoue 			af = 6;
19804dd8b5abSYoshinobu Inoue 			break;
19814dd8b5abSYoshinobu Inoue 		default:
19824dd8b5abSYoshinobu Inoue 			af = 0;
19834dd8b5abSYoshinobu Inoue 			break;
19844dd8b5abSYoshinobu Inoue 		}
19854dd8b5abSYoshinobu Inoue 		if (af) {
19864dd8b5abSYoshinobu Inoue 			printf("     %s (%d,%d,", ispassive ? "LPSV" : "LPRT",
19874dd8b5abSYoshinobu Inoue 				af, alen);
19884dd8b5abSYoshinobu Inoue 			for (i = 0; i < alen; i++)
19894dd8b5abSYoshinobu Inoue 				printf("%d,", UC(a[i]));
19904dd8b5abSYoshinobu Inoue 			printf("%d,%d,%d)\r\n", 2, UC(p[0]), UC(p[1]));
19914dd8b5abSYoshinobu Inoue 		}
19924dd8b5abSYoshinobu Inoue 	    }
19934dd8b5abSYoshinobu Inoue 
19944dd8b5abSYoshinobu Inoue epsvonly:;
19954dd8b5abSYoshinobu Inoue 		/* EPRT/EPSV */
19964dd8b5abSYoshinobu Inoue 	    {
19974dd8b5abSYoshinobu Inoue 		int af;
19984dd8b5abSYoshinobu Inoue 
19994dd8b5abSYoshinobu Inoue 		switch (su->su_family) {
20004dd8b5abSYoshinobu Inoue 		case AF_INET:
20014dd8b5abSYoshinobu Inoue 			af = 1;
20024dd8b5abSYoshinobu Inoue 			break;
20034dd8b5abSYoshinobu Inoue 		case AF_INET6:
20044dd8b5abSYoshinobu Inoue 			af = 2;
20054dd8b5abSYoshinobu Inoue 			break;
20064dd8b5abSYoshinobu Inoue 		default:
20074dd8b5abSYoshinobu Inoue 			af = 0;
20084dd8b5abSYoshinobu Inoue 			break;
20094dd8b5abSYoshinobu Inoue 		}
20104dd8b5abSYoshinobu Inoue 		if (af) {
20114dd8b5abSYoshinobu Inoue 			if (!getnameinfo((struct sockaddr *)su, su->su_len,
20124dd8b5abSYoshinobu Inoue 					hname, sizeof(hname) - 1, NULL, 0,
20134dd8b5abSYoshinobu Inoue 					NI_NUMERICHOST)) {
20144dd8b5abSYoshinobu Inoue 				printf("     %s |%d|%s|%d|\r\n",
20154dd8b5abSYoshinobu Inoue 					ispassive ? "EPSV" : "EPRT",
20164dd8b5abSYoshinobu Inoue 					af, hname, htons(su->su_port));
20174dd8b5abSYoshinobu Inoue 			}
20184dd8b5abSYoshinobu Inoue 		}
20194dd8b5abSYoshinobu Inoue 	    }
2020ea022d16SRodney W. Grimes #undef UC
2021ea022d16SRodney W. Grimes 	} else
2022ea022d16SRodney W. Grimes 		printf("     No data connection\r\n");
2023ea022d16SRodney W. Grimes 	reply(211, "End of status");
2024ea022d16SRodney W. Grimes }
2025ea022d16SRodney W. Grimes 
2026ea022d16SRodney W. Grimes void
2027ea022d16SRodney W. Grimes fatal(s)
2028ea022d16SRodney W. Grimes 	char *s;
2029ea022d16SRodney W. Grimes {
2030ea022d16SRodney W. Grimes 
2031ea022d16SRodney W. Grimes 	reply(451, "Error in server: %s\n", s);
2032ea022d16SRodney W. Grimes 	reply(221, "Closing connection due to server error.");
2033ea022d16SRodney W. Grimes 	dologout(0);
2034ea022d16SRodney W. Grimes 	/* NOTREACHED */
2035ea022d16SRodney W. Grimes }
2036ea022d16SRodney W. Grimes 
2037ea022d16SRodney W. Grimes void
2038ea022d16SRodney W. Grimes #if __STDC__
2039ea022d16SRodney W. Grimes reply(int n, const char *fmt, ...)
2040ea022d16SRodney W. Grimes #else
2041ea022d16SRodney W. Grimes reply(n, fmt, va_alist)
2042ea022d16SRodney W. Grimes 	int n;
2043ea022d16SRodney W. Grimes 	char *fmt;
2044ea022d16SRodney W. Grimes         va_dcl
2045ea022d16SRodney W. Grimes #endif
2046ea022d16SRodney W. Grimes {
2047ea022d16SRodney W. Grimes 	va_list ap;
2048ea022d16SRodney W. Grimes #if __STDC__
2049ea022d16SRodney W. Grimes 	va_start(ap, fmt);
2050ea022d16SRodney W. Grimes #else
2051ea022d16SRodney W. Grimes 	va_start(ap);
2052ea022d16SRodney W. Grimes #endif
2053ea022d16SRodney W. Grimes 	(void)printf("%d ", n);
2054ea022d16SRodney W. Grimes 	(void)vprintf(fmt, ap);
2055ea022d16SRodney W. Grimes 	(void)printf("\r\n");
2056ea022d16SRodney W. Grimes 	(void)fflush(stdout);
2057ea022d16SRodney W. Grimes 	if (debug) {
2058ea022d16SRodney W. Grimes 		syslog(LOG_DEBUG, "<--- %d ", n);
2059ea022d16SRodney W. Grimes 		vsyslog(LOG_DEBUG, fmt, ap);
2060ea022d16SRodney W. Grimes 	}
2061ea022d16SRodney W. Grimes }
2062ea022d16SRodney W. Grimes 
2063ea022d16SRodney W. Grimes void
2064ea022d16SRodney W. Grimes #if __STDC__
2065ea022d16SRodney W. Grimes lreply(int n, const char *fmt, ...)
2066ea022d16SRodney W. Grimes #else
2067ea022d16SRodney W. Grimes lreply(n, fmt, va_alist)
2068ea022d16SRodney W. Grimes 	int n;
2069ea022d16SRodney W. Grimes 	char *fmt;
2070ea022d16SRodney W. Grimes         va_dcl
2071ea022d16SRodney W. Grimes #endif
2072ea022d16SRodney W. Grimes {
2073ea022d16SRodney W. Grimes 	va_list ap;
2074ea022d16SRodney W. Grimes #if __STDC__
2075ea022d16SRodney W. Grimes 	va_start(ap, fmt);
2076ea022d16SRodney W. Grimes #else
2077ea022d16SRodney W. Grimes 	va_start(ap);
2078ea022d16SRodney W. Grimes #endif
2079ea022d16SRodney W. Grimes 	(void)printf("%d- ", n);
2080ea022d16SRodney W. Grimes 	(void)vprintf(fmt, ap);
2081ea022d16SRodney W. Grimes 	(void)printf("\r\n");
2082ea022d16SRodney W. Grimes 	(void)fflush(stdout);
2083ea022d16SRodney W. Grimes 	if (debug) {
2084ea022d16SRodney W. Grimes 		syslog(LOG_DEBUG, "<--- %d- ", n);
2085ea022d16SRodney W. Grimes 		vsyslog(LOG_DEBUG, fmt, ap);
2086ea022d16SRodney W. Grimes 	}
2087ea022d16SRodney W. Grimes }
2088ea022d16SRodney W. Grimes 
2089ea022d16SRodney W. Grimes static void
2090ea022d16SRodney W. Grimes ack(s)
2091ea022d16SRodney W. Grimes 	char *s;
2092ea022d16SRodney W. Grimes {
2093ea022d16SRodney W. Grimes 
2094ea022d16SRodney W. Grimes 	reply(250, "%s command successful.", s);
2095ea022d16SRodney W. Grimes }
2096ea022d16SRodney W. Grimes 
2097ea022d16SRodney W. Grimes void
2098ea022d16SRodney W. Grimes nack(s)
2099ea022d16SRodney W. Grimes 	char *s;
2100ea022d16SRodney W. Grimes {
2101ea022d16SRodney W. Grimes 
2102ea022d16SRodney W. Grimes 	reply(502, "%s command not implemented.", s);
2103ea022d16SRodney W. Grimes }
2104ea022d16SRodney W. Grimes 
2105ea022d16SRodney W. Grimes /* ARGSUSED */
2106ea022d16SRodney W. Grimes void
2107ea022d16SRodney W. Grimes yyerror(s)
2108ea022d16SRodney W. Grimes 	char *s;
2109ea022d16SRodney W. Grimes {
2110ea022d16SRodney W. Grimes 	char *cp;
2111ea022d16SRodney W. Grimes 
21129aca17cbSMark Murray 	if ((cp = strchr(cbuf,'\n')))
2113ea022d16SRodney W. Grimes 		*cp = '\0';
2114ea022d16SRodney W. Grimes 	reply(500, "'%s': command not understood.", cbuf);
2115ea022d16SRodney W. Grimes }
2116ea022d16SRodney W. Grimes 
2117ea022d16SRodney W. Grimes void
2118ea022d16SRodney W. Grimes delete(name)
2119ea022d16SRodney W. Grimes 	char *name;
2120ea022d16SRodney W. Grimes {
2121ea022d16SRodney W. Grimes 	struct stat st;
2122ea022d16SRodney W. Grimes 
2123ea022d16SRodney W. Grimes 	LOGCMD("delete", name);
2124ea022d16SRodney W. Grimes 	if (stat(name, &st) < 0) {
2125ea022d16SRodney W. Grimes 		perror_reply(550, name);
2126ea022d16SRodney W. Grimes 		return;
2127ea022d16SRodney W. Grimes 	}
2128ea022d16SRodney W. Grimes 	if ((st.st_mode&S_IFMT) == S_IFDIR) {
2129ea022d16SRodney W. Grimes 		if (rmdir(name) < 0) {
2130ea022d16SRodney W. Grimes 			perror_reply(550, name);
2131ea022d16SRodney W. Grimes 			return;
2132ea022d16SRodney W. Grimes 		}
2133ea022d16SRodney W. Grimes 		goto done;
2134ea022d16SRodney W. Grimes 	}
2135ea022d16SRodney W. Grimes 	if (unlink(name) < 0) {
2136ea022d16SRodney W. Grimes 		perror_reply(550, name);
2137ea022d16SRodney W. Grimes 		return;
2138ea022d16SRodney W. Grimes 	}
2139ea022d16SRodney W. Grimes done:
2140ea022d16SRodney W. Grimes 	ack("DELE");
2141ea022d16SRodney W. Grimes }
2142ea022d16SRodney W. Grimes 
2143ea022d16SRodney W. Grimes void
2144ea022d16SRodney W. Grimes cwd(path)
2145ea022d16SRodney W. Grimes 	char *path;
2146ea022d16SRodney W. Grimes {
2147ea022d16SRodney W. Grimes 
2148ea022d16SRodney W. Grimes 	if (chdir(path) < 0)
2149ea022d16SRodney W. Grimes 		perror_reply(550, path);
2150ea022d16SRodney W. Grimes 	else
2151ea022d16SRodney W. Grimes 		ack("CWD");
2152ea022d16SRodney W. Grimes }
2153ea022d16SRodney W. Grimes 
2154ea022d16SRodney W. Grimes void
2155ea022d16SRodney W. Grimes makedir(name)
2156ea022d16SRodney W. Grimes 	char *name;
2157ea022d16SRodney W. Grimes {
2158ea022d16SRodney W. Grimes 
2159ea022d16SRodney W. Grimes 	LOGCMD("mkdir", name);
2160ea022d16SRodney W. Grimes 	if (mkdir(name, 0777) < 0)
2161ea022d16SRodney W. Grimes 		perror_reply(550, name);
2162ea022d16SRodney W. Grimes 	else
2163ea022d16SRodney W. Grimes 		reply(257, "MKD command successful.");
2164ea022d16SRodney W. Grimes }
2165ea022d16SRodney W. Grimes 
2166ea022d16SRodney W. Grimes void
2167ea022d16SRodney W. Grimes removedir(name)
2168ea022d16SRodney W. Grimes 	char *name;
2169ea022d16SRodney W. Grimes {
2170ea022d16SRodney W. Grimes 
2171ea022d16SRodney W. Grimes 	LOGCMD("rmdir", name);
2172ea022d16SRodney W. Grimes 	if (rmdir(name) < 0)
2173ea022d16SRodney W. Grimes 		perror_reply(550, name);
2174ea022d16SRodney W. Grimes 	else
2175ea022d16SRodney W. Grimes 		ack("RMD");
2176ea022d16SRodney W. Grimes }
2177ea022d16SRodney W. Grimes 
2178ea022d16SRodney W. Grimes void
2179ea022d16SRodney W. Grimes pwd()
2180ea022d16SRodney W. Grimes {
2181ea022d16SRodney W. Grimes 	char path[MAXPATHLEN + 1];
2182ea022d16SRodney W. Grimes 
2183ea022d16SRodney W. Grimes 	if (getwd(path) == (char *)NULL)
2184ea022d16SRodney W. Grimes 		reply(550, "%s.", path);
2185ea022d16SRodney W. Grimes 	else
2186ea022d16SRodney W. Grimes 		reply(257, "\"%s\" is current directory.", path);
2187ea022d16SRodney W. Grimes }
2188ea022d16SRodney W. Grimes 
2189ea022d16SRodney W. Grimes char *
2190ea022d16SRodney W. Grimes renamefrom(name)
2191ea022d16SRodney W. Grimes 	char *name;
2192ea022d16SRodney W. Grimes {
2193ea022d16SRodney W. Grimes 	struct stat st;
2194ea022d16SRodney W. Grimes 
2195ea022d16SRodney W. Grimes 	if (stat(name, &st) < 0) {
2196ea022d16SRodney W. Grimes 		perror_reply(550, name);
2197ea022d16SRodney W. Grimes 		return ((char *)0);
2198ea022d16SRodney W. Grimes 	}
2199ea022d16SRodney W. Grimes 	reply(350, "File exists, ready for destination name");
2200ea022d16SRodney W. Grimes 	return (name);
2201ea022d16SRodney W. Grimes }
2202ea022d16SRodney W. Grimes 
2203ea022d16SRodney W. Grimes void
2204ea022d16SRodney W. Grimes renamecmd(from, to)
2205ea022d16SRodney W. Grimes 	char *from, *to;
2206ea022d16SRodney W. Grimes {
220761f891a6SPaul Traina 	struct stat st;
2208ea022d16SRodney W. Grimes 
2209ea022d16SRodney W. Grimes 	LOGCMD2("rename", from, to);
221061f891a6SPaul Traina 
221161f891a6SPaul Traina 	if (guest && (stat(to, &st) == 0)) {
221261f891a6SPaul Traina 		reply(550, "%s: permission denied", to);
221361f891a6SPaul Traina 		return;
221461f891a6SPaul Traina 	}
221561f891a6SPaul Traina 
2216ea022d16SRodney W. Grimes 	if (rename(from, to) < 0)
2217ea022d16SRodney W. Grimes 		perror_reply(550, "rename");
2218ea022d16SRodney W. Grimes 	else
2219ea022d16SRodney W. Grimes 		ack("RNTO");
2220ea022d16SRodney W. Grimes }
2221ea022d16SRodney W. Grimes 
2222ea022d16SRodney W. Grimes static void
22234dd8b5abSYoshinobu Inoue dolog(who)
22244dd8b5abSYoshinobu Inoue 	struct sockaddr *who;
2225ea022d16SRodney W. Grimes {
22264dd8b5abSYoshinobu Inoue 	int error;
22274dd8b5abSYoshinobu Inoue 
22284dd8b5abSYoshinobu Inoue 	realhostname_sa(remotehost, sizeof(remotehost) - 1, who, who->sa_len);
2229ea022d16SRodney W. Grimes 
2230ea022d16SRodney W. Grimes #ifdef SETPROCTITLE
2231ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
2232ea4e54b9SDavid Nugent 	if (thishost != firsthost)
2233ea4e54b9SDavid Nugent 		snprintf(proctitle, sizeof(proctitle), "%s: connected (to %s)",
2234ea4e54b9SDavid Nugent 			 remotehost, hostname);
2235ea4e54b9SDavid Nugent 	else
2236ea4e54b9SDavid Nugent #endif
2237ea4e54b9SDavid Nugent 		snprintf(proctitle, sizeof(proctitle), "%s: connected",
2238ea4e54b9SDavid Nugent 			 remotehost);
2239b63e1fe2SPeter Wemm 	setproctitle("%s", proctitle);
2240ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */
2241ea022d16SRodney W. Grimes 
2242ea4e54b9SDavid Nugent 	if (logging) {
2243ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
2244ea4e54b9SDavid Nugent 		if (thishost != firsthost)
2245ea4e54b9SDavid Nugent 			syslog(LOG_INFO, "connection from %s (to %s)",
2246ea4e54b9SDavid Nugent 			       remotehost, hostname);
2247ea4e54b9SDavid Nugent 		else
2248ea4e54b9SDavid Nugent #endif
22494dd8b5abSYoshinobu Inoue 		{
22504dd8b5abSYoshinobu Inoue 			char	who_name[MAXHOSTNAMELEN];
22514dd8b5abSYoshinobu Inoue 
22524dd8b5abSYoshinobu Inoue 			error = getnameinfo(who, who->sa_len,
22534dd8b5abSYoshinobu Inoue 					    who_name, sizeof(who_name) - 1,
22544dd8b5abSYoshinobu Inoue 					    NULL, 0,
22554dd8b5abSYoshinobu Inoue 					    NI_NUMERICHOST|NI_WITHSCOPEID);
2256f5c57d05SEivind Eklund 			syslog(LOG_INFO, "connection from %s (%s)", remotehost,
22574dd8b5abSYoshinobu Inoue 			       error == 0 ? who_name : "");
22584dd8b5abSYoshinobu Inoue 		}
2259ea022d16SRodney W. Grimes 	}
2260ea4e54b9SDavid Nugent }
2261ea022d16SRodney W. Grimes 
2262ea022d16SRodney W. Grimes /*
2263ea022d16SRodney W. Grimes  * Record logout in wtmp file
2264ea022d16SRodney W. Grimes  * and exit with supplied status.
2265ea022d16SRodney W. Grimes  */
2266ea022d16SRodney W. Grimes void
2267ea022d16SRodney W. Grimes dologout(status)
2268ea022d16SRodney W. Grimes 	int status;
2269ea022d16SRodney W. Grimes {
22700b4df2eeSDavid Greenman 	/*
22710b4df2eeSDavid Greenman 	 * Prevent reception of SIGURG from resulting in a resumption
22720b4df2eeSDavid Greenman 	 * back to the main program loop.
22730b4df2eeSDavid Greenman 	 */
22740b4df2eeSDavid Greenman 	transflag = 0;
2275ea022d16SRodney W. Grimes 
2276ea022d16SRodney W. Grimes 	if (logged_in) {
2277ea022d16SRodney W. Grimes 		(void) seteuid((uid_t)0);
2278986a1172SThomas Gellekum 		ftpd_logwtmp(ttyline, "", "");
2279ea022d16SRodney W. Grimes 	}
2280ea022d16SRodney W. Grimes 	/* beware of flushing buffers after a SIGPIPE */
2281ea022d16SRodney W. Grimes 	_exit(status);
2282ea022d16SRodney W. Grimes }
2283ea022d16SRodney W. Grimes 
2284ea022d16SRodney W. Grimes static void
2285ea022d16SRodney W. Grimes myoob(signo)
2286ea022d16SRodney W. Grimes 	int signo;
2287ea022d16SRodney W. Grimes {
2288ea022d16SRodney W. Grimes 	char *cp;
2289ea022d16SRodney W. Grimes 
2290ea022d16SRodney W. Grimes 	/* only process if transfer occurring */
2291ea022d16SRodney W. Grimes 	if (!transflag)
2292ea022d16SRodney W. Grimes 		return;
2293ea022d16SRodney W. Grimes 	cp = tmpline;
2294ea022d16SRodney W. Grimes 	if (getline(cp, 7, stdin) == NULL) {
2295ea022d16SRodney W. Grimes 		reply(221, "You could at least say goodbye.");
2296ea022d16SRodney W. Grimes 		dologout(0);
2297ea022d16SRodney W. Grimes 	}
2298ea022d16SRodney W. Grimes 	upper(cp);
2299ea022d16SRodney W. Grimes 	if (strcmp(cp, "ABOR\r\n") == 0) {
2300ea022d16SRodney W. Grimes 		tmpline[0] = '\0';
2301ea022d16SRodney W. Grimes 		reply(426, "Transfer aborted. Data connection closed.");
2302ea022d16SRodney W. Grimes 		reply(226, "Abort successful");
2303ea022d16SRodney W. Grimes 		longjmp(urgcatch, 1);
2304ea022d16SRodney W. Grimes 	}
2305ea022d16SRodney W. Grimes 	if (strcmp(cp, "STAT\r\n") == 0) {
23069db4bbf3SMichael Haro 		tmpline[0] = '\0';
2307ea022d16SRodney W. Grimes 		if (file_size != (off_t) -1)
2308ea022d16SRodney W. Grimes 			reply(213, "Status: %qd of %qd bytes transferred",
2309ea022d16SRodney W. Grimes 			    byte_count, file_size);
2310ea022d16SRodney W. Grimes 		else
2311ea022d16SRodney W. Grimes 			reply(213, "Status: %qd bytes transferred", byte_count);
2312ea022d16SRodney W. Grimes 	}
2313ea022d16SRodney W. Grimes }
2314ea022d16SRodney W. Grimes 
2315ea022d16SRodney W. Grimes /*
2316ea022d16SRodney W. Grimes  * Note: a response of 425 is not mentioned as a possible response to
2317ea022d16SRodney W. Grimes  *	the PASV command in RFC959. However, it has been blessed as
2318ea022d16SRodney W. Grimes  *	a legitimate response by Jon Postel in a telephone conversation
2319ea022d16SRodney W. Grimes  *	with Rick Adams on 25 Jan 89.
2320ea022d16SRodney W. Grimes  */
2321ea022d16SRodney W. Grimes void
2322ea022d16SRodney W. Grimes passive()
2323ea022d16SRodney W. Grimes {
2324dacc9752SPaul Traina 	int len;
2325ea022d16SRodney W. Grimes 	char *p, *a;
2326ea022d16SRodney W. Grimes 
232761f891a6SPaul Traina 	if (pdata >= 0)		/* close old port if one set */
232861f891a6SPaul Traina 		close(pdata);
232961f891a6SPaul Traina 
23304dd8b5abSYoshinobu Inoue 	pdata = socket(ctrl_addr.su_family, SOCK_STREAM, 0);
2331ea022d16SRodney W. Grimes 	if (pdata < 0) {
2332ea022d16SRodney W. Grimes 		perror_reply(425, "Can't open passive connection");
2333ea022d16SRodney W. Grimes 		return;
2334ea022d16SRodney W. Grimes 	}
23354c450ad7SPaul Traina 
23364c450ad7SPaul Traina 	(void) seteuid((uid_t)0);
233761f891a6SPaul Traina 
2338dacc9752SPaul Traina #ifdef IP_PORTRANGE
23394dd8b5abSYoshinobu Inoue 	if (ctrl_addr.su_family == AF_INET) {
2340dacc9752SPaul Traina 	    int on = restricted_data_ports ? IP_PORTRANGE_HIGH
2341dacc9752SPaul Traina 					   : IP_PORTRANGE_DEFAULT;
2342dacc9752SPaul Traina 
234340e9d39eSPeter Wemm 	    if (setsockopt(pdata, IPPROTO_IP, IP_PORTRANGE,
2344dacc9752SPaul Traina 			    (char *)&on, sizeof(on)) < 0)
23454c450ad7SPaul Traina 		    goto pasv_error;
23464c450ad7SPaul Traina 	}
2347dacc9752SPaul Traina #endif
234840e9d39eSPeter Wemm 
2349ea022d16SRodney W. Grimes 	pasv_addr = ctrl_addr;
23504dd8b5abSYoshinobu Inoue 	pasv_addr.su_port = 0;
23514dd8b5abSYoshinobu Inoue 	if (bind(pdata, (struct sockaddr *)&pasv_addr, pasv_addr.su_len) < 0)
2352ea022d16SRodney W. Grimes 		goto pasv_error;
235361f891a6SPaul Traina 
2354ea022d16SRodney W. Grimes 	(void) seteuid((uid_t)pw->pw_uid);
23554c450ad7SPaul Traina 
2356ea022d16SRodney W. Grimes 	len = sizeof(pasv_addr);
2357ea022d16SRodney W. Grimes 	if (getsockname(pdata, (struct sockaddr *) &pasv_addr, &len) < 0)
2358ea022d16SRodney W. Grimes 		goto pasv_error;
2359ea022d16SRodney W. Grimes 	if (listen(pdata, 1) < 0)
2360ea022d16SRodney W. Grimes 		goto pasv_error;
23614dd8b5abSYoshinobu Inoue 	if (pasv_addr.su_family == AF_INET)
23624dd8b5abSYoshinobu Inoue 		a = (char *) &pasv_addr.su_sin.sin_addr;
23634dd8b5abSYoshinobu Inoue 	else if (pasv_addr.su_family == AF_INET6 &&
23644dd8b5abSYoshinobu Inoue 		 IN6_IS_ADDR_V4MAPPED(&pasv_addr.su_sin6.sin6_addr))
23654dd8b5abSYoshinobu Inoue 		a = (char *) &pasv_addr.su_sin6.sin6_addr.s6_addr[12];
23664dd8b5abSYoshinobu Inoue 	else
23674dd8b5abSYoshinobu Inoue 		goto pasv_error;
23684dd8b5abSYoshinobu Inoue 
23694dd8b5abSYoshinobu Inoue 	p = (char *) &pasv_addr.su_port;
2370ea022d16SRodney W. Grimes 
2371ea022d16SRodney W. Grimes #define UC(b) (((int) b) & 0xff)
2372ea022d16SRodney W. Grimes 
2373ea022d16SRodney W. Grimes 	reply(227, "Entering Passive Mode (%d,%d,%d,%d,%d,%d)", UC(a[0]),
2374ea022d16SRodney W. Grimes 		UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1]));
2375ea022d16SRodney W. Grimes 	return;
2376ea022d16SRodney W. Grimes 
2377ea022d16SRodney W. Grimes pasv_error:
237861f891a6SPaul Traina 	(void) seteuid((uid_t)pw->pw_uid);
2379ea022d16SRodney W. Grimes 	(void) close(pdata);
2380ea022d16SRodney W. Grimes 	pdata = -1;
2381ea022d16SRodney W. Grimes 	perror_reply(425, "Can't open passive connection");
2382ea022d16SRodney W. Grimes 	return;
2383ea022d16SRodney W. Grimes }
2384ea022d16SRodney W. Grimes 
2385ea022d16SRodney W. Grimes /*
23864dd8b5abSYoshinobu Inoue  * Long Passive defined in RFC 1639.
23874dd8b5abSYoshinobu Inoue  *     228 Entering Long Passive Mode
23884dd8b5abSYoshinobu Inoue  *         (af, hal, h1, h2, h3,..., pal, p1, p2...)
23894dd8b5abSYoshinobu Inoue  */
23904dd8b5abSYoshinobu Inoue 
23914dd8b5abSYoshinobu Inoue void
23924dd8b5abSYoshinobu Inoue long_passive(cmd, pf)
23934dd8b5abSYoshinobu Inoue 	char *cmd;
23944dd8b5abSYoshinobu Inoue 	int pf;
23954dd8b5abSYoshinobu Inoue {
23964dd8b5abSYoshinobu Inoue 	int len;
23974dd8b5abSYoshinobu Inoue 	char *p, *a;
23984dd8b5abSYoshinobu Inoue 
23994dd8b5abSYoshinobu Inoue 	if (pdata >= 0)		/* close old port if one set */
24004dd8b5abSYoshinobu Inoue 		close(pdata);
24014dd8b5abSYoshinobu Inoue 
24024dd8b5abSYoshinobu Inoue 	if (pf != PF_UNSPEC) {
24034dd8b5abSYoshinobu Inoue 		if (ctrl_addr.su_family != pf) {
24044dd8b5abSYoshinobu Inoue 			switch (ctrl_addr.su_family) {
24054dd8b5abSYoshinobu Inoue 			case AF_INET:
24064dd8b5abSYoshinobu Inoue 				pf = 1;
24074dd8b5abSYoshinobu Inoue 				break;
24084dd8b5abSYoshinobu Inoue 			case AF_INET6:
24094dd8b5abSYoshinobu Inoue 				pf = 2;
24104dd8b5abSYoshinobu Inoue 				break;
24114dd8b5abSYoshinobu Inoue 			default:
24124dd8b5abSYoshinobu Inoue 				pf = 0;
24134dd8b5abSYoshinobu Inoue 				break;
24144dd8b5abSYoshinobu Inoue 			}
24154dd8b5abSYoshinobu Inoue 			/*
24164dd8b5abSYoshinobu Inoue 			 * XXX
24174dd8b5abSYoshinobu Inoue 			 * only EPRT/EPSV ready clients will understand this
24184dd8b5abSYoshinobu Inoue 			 */
24194dd8b5abSYoshinobu Inoue 			if (strcmp(cmd, "EPSV") == 0 && pf) {
24204dd8b5abSYoshinobu Inoue 				reply(522, "Network protocol mismatch, "
24214dd8b5abSYoshinobu Inoue 					"use (%d)", pf);
24224dd8b5abSYoshinobu Inoue 			} else
24234dd8b5abSYoshinobu Inoue 				reply(501, "Network protocol mismatch"); /*XXX*/
24244dd8b5abSYoshinobu Inoue 
24254dd8b5abSYoshinobu Inoue 			return;
24264dd8b5abSYoshinobu Inoue 		}
24274dd8b5abSYoshinobu Inoue 	}
24284dd8b5abSYoshinobu Inoue 
24294dd8b5abSYoshinobu Inoue 	pdata = socket(ctrl_addr.su_family, SOCK_STREAM, 0);
24304dd8b5abSYoshinobu Inoue 	if (pdata < 0) {
24314dd8b5abSYoshinobu Inoue 		perror_reply(425, "Can't open passive connection");
24324dd8b5abSYoshinobu Inoue 		return;
24334dd8b5abSYoshinobu Inoue 	}
24344dd8b5abSYoshinobu Inoue 
24354dd8b5abSYoshinobu Inoue 	(void) seteuid((uid_t)0);
24364dd8b5abSYoshinobu Inoue 
24374dd8b5abSYoshinobu Inoue 	pasv_addr = ctrl_addr;
24384dd8b5abSYoshinobu Inoue 	pasv_addr.su_port = 0;
24394dd8b5abSYoshinobu Inoue 	len = pasv_addr.su_len;
24404dd8b5abSYoshinobu Inoue 
24414dd8b5abSYoshinobu Inoue 	if (bind(pdata, (struct sockaddr *)&pasv_addr, len) < 0)
24424dd8b5abSYoshinobu Inoue 		goto pasv_error;
24434dd8b5abSYoshinobu Inoue 
24444dd8b5abSYoshinobu Inoue 	(void) seteuid((uid_t)pw->pw_uid);
24454dd8b5abSYoshinobu Inoue 
24464dd8b5abSYoshinobu Inoue 	if (getsockname(pdata, (struct sockaddr *) &pasv_addr, &len) < 0)
24474dd8b5abSYoshinobu Inoue 		goto pasv_error;
24484dd8b5abSYoshinobu Inoue 	if (listen(pdata, 1) < 0)
24494dd8b5abSYoshinobu Inoue 		goto pasv_error;
24504dd8b5abSYoshinobu Inoue 
24514dd8b5abSYoshinobu Inoue #define UC(b) (((int) b) & 0xff)
24524dd8b5abSYoshinobu Inoue 
24534dd8b5abSYoshinobu Inoue 	if (strcmp(cmd, "LPSV") == 0) {
24544dd8b5abSYoshinobu Inoue 		p = (char *)&pasv_addr.su_port;
24554dd8b5abSYoshinobu Inoue 		switch (pasv_addr.su_family) {
24564dd8b5abSYoshinobu Inoue 		case AF_INET:
24574dd8b5abSYoshinobu Inoue 			a = (char *) &pasv_addr.su_sin.sin_addr;
24584dd8b5abSYoshinobu Inoue 		v4_reply:
24594dd8b5abSYoshinobu Inoue 			reply(228,
24604dd8b5abSYoshinobu Inoue "Entering Long Passive Mode (%d,%d,%d,%d,%d,%d,%d,%d,%d)",
24614dd8b5abSYoshinobu Inoue 			      4, 4, UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
24624dd8b5abSYoshinobu Inoue 			      2, UC(p[0]), UC(p[1]));
24634dd8b5abSYoshinobu Inoue 			return;
24644dd8b5abSYoshinobu Inoue 		case AF_INET6:
24654dd8b5abSYoshinobu Inoue 			if (IN6_IS_ADDR_V4MAPPED(&pasv_addr.su_sin6.sin6_addr)) {
24664dd8b5abSYoshinobu Inoue 				a = (char *) &pasv_addr.su_sin6.sin6_addr.s6_addr[12];
24674dd8b5abSYoshinobu Inoue 				goto v4_reply;
24684dd8b5abSYoshinobu Inoue 			}
24694dd8b5abSYoshinobu Inoue 			a = (char *) &pasv_addr.su_sin6.sin6_addr;
24704dd8b5abSYoshinobu Inoue 			reply(228,
24714dd8b5abSYoshinobu Inoue "Entering Long Passive Mode "
24724dd8b5abSYoshinobu Inoue "(%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d)",
24734dd8b5abSYoshinobu Inoue 			      6, 16, UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
24744dd8b5abSYoshinobu Inoue 			      UC(a[4]), UC(a[5]), UC(a[6]), UC(a[7]),
24754dd8b5abSYoshinobu Inoue 			      UC(a[8]), UC(a[9]), UC(a[10]), UC(a[11]),
24764dd8b5abSYoshinobu Inoue 			      UC(a[12]), UC(a[13]), UC(a[14]), UC(a[15]),
24774dd8b5abSYoshinobu Inoue 			      2, UC(p[0]), UC(p[1]));
24784dd8b5abSYoshinobu Inoue 			return;
24794dd8b5abSYoshinobu Inoue 		}
24804dd8b5abSYoshinobu Inoue 	} else if (strcmp(cmd, "EPSV") == 0) {
24814dd8b5abSYoshinobu Inoue 		switch (pasv_addr.su_family) {
24824dd8b5abSYoshinobu Inoue 		case AF_INET:
24834dd8b5abSYoshinobu Inoue 		case AF_INET6:
24844dd8b5abSYoshinobu Inoue 			reply(229, "Entering Extended Passive Mode (|||%d|)",
24854dd8b5abSYoshinobu Inoue 				ntohs(pasv_addr.su_port));
24864dd8b5abSYoshinobu Inoue 			return;
24874dd8b5abSYoshinobu Inoue 		}
24884dd8b5abSYoshinobu Inoue 	} else {
24894dd8b5abSYoshinobu Inoue 		/* more proper error code? */
24904dd8b5abSYoshinobu Inoue 	}
24914dd8b5abSYoshinobu Inoue 
24924dd8b5abSYoshinobu Inoue pasv_error:
24934dd8b5abSYoshinobu Inoue 	(void) seteuid((uid_t)pw->pw_uid);
24944dd8b5abSYoshinobu Inoue 	(void) close(pdata);
24954dd8b5abSYoshinobu Inoue 	pdata = -1;
24964dd8b5abSYoshinobu Inoue 	perror_reply(425, "Can't open passive connection");
24974dd8b5abSYoshinobu Inoue 	return;
24984dd8b5abSYoshinobu Inoue }
24994dd8b5abSYoshinobu Inoue 
25004dd8b5abSYoshinobu Inoue /*
2501ea022d16SRodney W. Grimes  * Generate unique name for file with basename "local".
2502ea022d16SRodney W. Grimes  * The file named "local" is already known to exist.
2503ea022d16SRodney W. Grimes  * Generates failure reply on error.
2504ea022d16SRodney W. Grimes  */
2505ea022d16SRodney W. Grimes static char *
2506ea022d16SRodney W. Grimes gunique(local)
2507ea022d16SRodney W. Grimes 	char *local;
2508ea022d16SRodney W. Grimes {
2509ea022d16SRodney W. Grimes 	static char new[MAXPATHLEN];
2510ea022d16SRodney W. Grimes 	struct stat st;
2511ea022d16SRodney W. Grimes 	int count;
2512ea022d16SRodney W. Grimes 	char *cp;
2513ea022d16SRodney W. Grimes 
2514ea022d16SRodney W. Grimes 	cp = strrchr(local, '/');
2515ea022d16SRodney W. Grimes 	if (cp)
2516ea022d16SRodney W. Grimes 		*cp = '\0';
2517ea022d16SRodney W. Grimes 	if (stat(cp ? local : ".", &st) < 0) {
2518ea022d16SRodney W. Grimes 		perror_reply(553, cp ? local : ".");
2519ea022d16SRodney W. Grimes 		return ((char *) 0);
2520ea022d16SRodney W. Grimes 	}
2521ea022d16SRodney W. Grimes 	if (cp)
2522ea022d16SRodney W. Grimes 		*cp = '/';
2523e760ef2cSWarner Losh 	/* -4 is for the .nn<null> we put on the end below */
2524e760ef2cSWarner Losh 	(void) snprintf(new, sizeof(new) - 4, "%s", local);
2525ea022d16SRodney W. Grimes 	cp = new + strlen(new);
2526ea022d16SRodney W. Grimes 	*cp++ = '.';
2527ea022d16SRodney W. Grimes 	for (count = 1; count < 100; count++) {
2528ea022d16SRodney W. Grimes 		(void)sprintf(cp, "%d", count);
2529ea022d16SRodney W. Grimes 		if (stat(new, &st) < 0)
2530ea022d16SRodney W. Grimes 			return (new);
2531ea022d16SRodney W. Grimes 	}
2532ea022d16SRodney W. Grimes 	reply(452, "Unique file name cannot be created.");
2533ea022d16SRodney W. Grimes 	return (NULL);
2534ea022d16SRodney W. Grimes }
2535ea022d16SRodney W. Grimes 
2536ea022d16SRodney W. Grimes /*
2537ea022d16SRodney W. Grimes  * Format and send reply containing system error number.
2538ea022d16SRodney W. Grimes  */
2539ea022d16SRodney W. Grimes void
2540ea022d16SRodney W. Grimes perror_reply(code, string)
2541ea022d16SRodney W. Grimes 	int code;
2542ea022d16SRodney W. Grimes 	char *string;
2543ea022d16SRodney W. Grimes {
2544ea022d16SRodney W. Grimes 
2545ea022d16SRodney W. Grimes 	reply(code, "%s: %s.", string, strerror(errno));
2546ea022d16SRodney W. Grimes }
2547ea022d16SRodney W. Grimes 
2548ea022d16SRodney W. Grimes static char *onefile[] = {
2549ea022d16SRodney W. Grimes 	"",
2550ea022d16SRodney W. Grimes 	0
2551ea022d16SRodney W. Grimes };
2552ea022d16SRodney W. Grimes 
2553ea022d16SRodney W. Grimes void
2554ea022d16SRodney W. Grimes send_file_list(whichf)
2555ea022d16SRodney W. Grimes 	char *whichf;
2556ea022d16SRodney W. Grimes {
2557ea022d16SRodney W. Grimes 	struct stat st;
2558ea022d16SRodney W. Grimes 	DIR *dirp = NULL;
2559ea022d16SRodney W. Grimes 	struct dirent *dir;
2560ea022d16SRodney W. Grimes 	FILE *dout = NULL;
2561ea022d16SRodney W. Grimes 	char **dirlist, *dirname;
2562ea022d16SRodney W. Grimes 	int simple = 0;
2563ea022d16SRodney W. Grimes 	int freeglob = 0;
2564ea022d16SRodney W. Grimes 	glob_t gl;
2565ea022d16SRodney W. Grimes 
2566ea022d16SRodney W. Grimes 	if (strpbrk(whichf, "~{[*?") != NULL) {
2567ea022d16SRodney W. Grimes 		int flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_QUOTE|GLOB_TILDE;
2568ea022d16SRodney W. Grimes 
2569ea022d16SRodney W. Grimes 		memset(&gl, 0, sizeof(gl));
2570ea022d16SRodney W. Grimes 		freeglob = 1;
2571ea022d16SRodney W. Grimes 		if (glob(whichf, flags, 0, &gl)) {
2572ea022d16SRodney W. Grimes 			reply(550, "not found");
2573ea022d16SRodney W. Grimes 			goto out;
2574ea022d16SRodney W. Grimes 		} else if (gl.gl_pathc == 0) {
2575ea022d16SRodney W. Grimes 			errno = ENOENT;
2576ea022d16SRodney W. Grimes 			perror_reply(550, whichf);
2577ea022d16SRodney W. Grimes 			goto out;
2578ea022d16SRodney W. Grimes 		}
2579ea022d16SRodney W. Grimes 		dirlist = gl.gl_pathv;
2580ea022d16SRodney W. Grimes 	} else {
2581ea022d16SRodney W. Grimes 		onefile[0] = whichf;
2582ea022d16SRodney W. Grimes 		dirlist = onefile;
2583ea022d16SRodney W. Grimes 		simple = 1;
2584ea022d16SRodney W. Grimes 	}
2585ea022d16SRodney W. Grimes 
2586ea022d16SRodney W. Grimes 	if (setjmp(urgcatch)) {
2587ea022d16SRodney W. Grimes 		transflag = 0;
2588ea022d16SRodney W. Grimes 		goto out;
2589ea022d16SRodney W. Grimes 	}
25909aca17cbSMark Murray 	while ((dirname = *dirlist++)) {
2591ea022d16SRodney W. Grimes 		if (stat(dirname, &st) < 0) {
2592ea022d16SRodney W. Grimes 			/*
2593ea022d16SRodney W. Grimes 			 * If user typed "ls -l", etc, and the client
2594ea022d16SRodney W. Grimes 			 * used NLST, do what the user meant.
2595ea022d16SRodney W. Grimes 			 */
2596ea022d16SRodney W. Grimes 			if (dirname[0] == '-' && *dirlist == NULL &&
2597ea022d16SRodney W. Grimes 			    transflag == 0) {
2598af85d782SDavid Nugent 				retrieve(_PATH_LS " %s", dirname);
2599ea022d16SRodney W. Grimes 				goto out;
2600ea022d16SRodney W. Grimes 			}
2601ea022d16SRodney W. Grimes 			perror_reply(550, whichf);
2602ea022d16SRodney W. Grimes 			if (dout != NULL) {
2603ea022d16SRodney W. Grimes 				(void) fclose(dout);
2604ea022d16SRodney W. Grimes 				transflag = 0;
2605ea022d16SRodney W. Grimes 				data = -1;
2606ea022d16SRodney W. Grimes 				pdata = -1;
2607ea022d16SRodney W. Grimes 			}
2608ea022d16SRodney W. Grimes 			goto out;
2609ea022d16SRodney W. Grimes 		}
2610ea022d16SRodney W. Grimes 
2611ea022d16SRodney W. Grimes 		if (S_ISREG(st.st_mode)) {
2612ea022d16SRodney W. Grimes 			if (dout == NULL) {
2613ea022d16SRodney W. Grimes 				dout = dataconn("file list", (off_t)-1, "w");
2614ea022d16SRodney W. Grimes 				if (dout == NULL)
2615ea022d16SRodney W. Grimes 					goto out;
2616ea022d16SRodney W. Grimes 				transflag++;
2617ea022d16SRodney W. Grimes 			}
2618ea022d16SRodney W. Grimes 			fprintf(dout, "%s%s\n", dirname,
2619ea022d16SRodney W. Grimes 				type == TYPE_A ? "\r" : "");
2620ea022d16SRodney W. Grimes 			byte_count += strlen(dirname) + 1;
2621ea022d16SRodney W. Grimes 			continue;
2622ea022d16SRodney W. Grimes 		} else if (!S_ISDIR(st.st_mode))
2623ea022d16SRodney W. Grimes 			continue;
2624ea022d16SRodney W. Grimes 
2625ea022d16SRodney W. Grimes 		if ((dirp = opendir(dirname)) == NULL)
2626ea022d16SRodney W. Grimes 			continue;
2627ea022d16SRodney W. Grimes 
2628ea022d16SRodney W. Grimes 		while ((dir = readdir(dirp)) != NULL) {
2629ea022d16SRodney W. Grimes 			char nbuf[MAXPATHLEN];
2630ea022d16SRodney W. Grimes 
2631ea022d16SRodney W. Grimes 			if (dir->d_name[0] == '.' && dir->d_namlen == 1)
2632ea022d16SRodney W. Grimes 				continue;
2633ea022d16SRodney W. Grimes 			if (dir->d_name[0] == '.' && dir->d_name[1] == '.' &&
2634ea022d16SRodney W. Grimes 			    dir->d_namlen == 2)
2635ea022d16SRodney W. Grimes 				continue;
2636ea022d16SRodney W. Grimes 
2637e760ef2cSWarner Losh 			snprintf(nbuf, sizeof(nbuf),
2638e760ef2cSWarner Losh 				"%s/%s", dirname, dir->d_name);
2639ea022d16SRodney W. Grimes 
2640ea022d16SRodney W. Grimes 			/*
2641ea022d16SRodney W. Grimes 			 * We have to do a stat to insure it's
2642ea022d16SRodney W. Grimes 			 * not a directory or special file.
2643ea022d16SRodney W. Grimes 			 */
2644ea022d16SRodney W. Grimes 			if (simple || (stat(nbuf, &st) == 0 &&
2645ea022d16SRodney W. Grimes 			    S_ISREG(st.st_mode))) {
2646ea022d16SRodney W. Grimes 				if (dout == NULL) {
2647ea022d16SRodney W. Grimes 					dout = dataconn("file list", (off_t)-1,
2648ea022d16SRodney W. Grimes 						"w");
2649ea022d16SRodney W. Grimes 					if (dout == NULL)
2650ea022d16SRodney W. Grimes 						goto out;
2651ea022d16SRodney W. Grimes 					transflag++;
2652ea022d16SRodney W. Grimes 				}
2653ea022d16SRodney W. Grimes 				if (nbuf[0] == '.' && nbuf[1] == '/')
2654ea022d16SRodney W. Grimes 					fprintf(dout, "%s%s\n", &nbuf[2],
2655ea022d16SRodney W. Grimes 						type == TYPE_A ? "\r" : "");
2656ea022d16SRodney W. Grimes 				else
2657ea022d16SRodney W. Grimes 					fprintf(dout, "%s%s\n", nbuf,
2658ea022d16SRodney W. Grimes 						type == TYPE_A ? "\r" : "");
2659ea022d16SRodney W. Grimes 				byte_count += strlen(nbuf) + 1;
2660ea022d16SRodney W. Grimes 			}
2661ea022d16SRodney W. Grimes 		}
2662ea022d16SRodney W. Grimes 		(void) closedir(dirp);
2663ea022d16SRodney W. Grimes 	}
2664ea022d16SRodney W. Grimes 
2665ea022d16SRodney W. Grimes 	if (dout == NULL)
2666ea022d16SRodney W. Grimes 		reply(550, "No files found.");
2667ea022d16SRodney W. Grimes 	else if (ferror(dout) != 0)
2668ea022d16SRodney W. Grimes 		perror_reply(550, "Data connection");
2669ea022d16SRodney W. Grimes 	else
2670ea022d16SRodney W. Grimes 		reply(226, "Transfer complete.");
2671ea022d16SRodney W. Grimes 
2672ea022d16SRodney W. Grimes 	transflag = 0;
2673ea022d16SRodney W. Grimes 	if (dout != NULL)
2674ea022d16SRodney W. Grimes 		(void) fclose(dout);
2675ea022d16SRodney W. Grimes 	data = -1;
2676ea022d16SRodney W. Grimes 	pdata = -1;
2677ea022d16SRodney W. Grimes out:
2678ea022d16SRodney W. Grimes 	if (freeglob) {
2679ea022d16SRodney W. Grimes 		freeglob = 0;
2680ea022d16SRodney W. Grimes 		globfree(&gl);
2681ea022d16SRodney W. Grimes 	}
2682ea022d16SRodney W. Grimes }
2683ea022d16SRodney W. Grimes 
2684cf09a206SDavid Greenman void
2685cf09a206SDavid Greenman reapchild(signo)
2686cf09a206SDavid Greenman 	int signo;
2687cf09a206SDavid Greenman {
2688cf09a206SDavid Greenman 	while (wait3(NULL, WNOHANG, NULL) > 0);
2689cf09a206SDavid Greenman }
2690cf09a206SDavid Greenman 
2691b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE
2692ea022d16SRodney W. Grimes /*
2693ea022d16SRodney W. Grimes  * Clobber argv so ps will show what we're doing.  (Stolen from sendmail.)
2694ea022d16SRodney W. Grimes  * Warning, since this is usually started from inetd.conf, it often doesn't
2695ea022d16SRodney W. Grimes  * have much of an environment or arglist to overwrite.
2696ea022d16SRodney W. Grimes  */
2697ea022d16SRodney W. Grimes void
2698ea022d16SRodney W. Grimes #if __STDC__
2699ea022d16SRodney W. Grimes setproctitle(const char *fmt, ...)
2700ea022d16SRodney W. Grimes #else
2701ea022d16SRodney W. Grimes setproctitle(fmt, va_alist)
2702ea022d16SRodney W. Grimes 	char *fmt;
2703ea022d16SRodney W. Grimes         va_dcl
2704ea022d16SRodney W. Grimes #endif
2705ea022d16SRodney W. Grimes {
2706ea022d16SRodney W. Grimes 	int i;
2707ea022d16SRodney W. Grimes 	va_list ap;
2708ea022d16SRodney W. Grimes 	char *p, *bp, ch;
2709ea022d16SRodney W. Grimes 	char buf[LINE_MAX];
2710ea022d16SRodney W. Grimes 
2711ea022d16SRodney W. Grimes #if __STDC__
2712ea022d16SRodney W. Grimes 	va_start(ap, fmt);
2713ea022d16SRodney W. Grimes #else
2714ea022d16SRodney W. Grimes 	va_start(ap);
2715ea022d16SRodney W. Grimes #endif
2716ea022d16SRodney W. Grimes 	(void)vsnprintf(buf, sizeof(buf), fmt, ap);
2717ea022d16SRodney W. Grimes 
2718ea022d16SRodney W. Grimes 	/* make ps print our process name */
2719ea022d16SRodney W. Grimes 	p = Argv[0];
2720ea022d16SRodney W. Grimes 	*p++ = '-';
2721ea022d16SRodney W. Grimes 
2722ea022d16SRodney W. Grimes 	i = strlen(buf);
2723ea022d16SRodney W. Grimes 	if (i > LastArgv - p - 2) {
2724ea022d16SRodney W. Grimes 		i = LastArgv - p - 2;
2725ea022d16SRodney W. Grimes 		buf[i] = '\0';
2726ea022d16SRodney W. Grimes 	}
2727ea022d16SRodney W. Grimes 	bp = buf;
2728ea022d16SRodney W. Grimes 	while (ch = *bp++)
2729ea022d16SRodney W. Grimes 		if (ch != '\n' && ch != '\r')
2730ea022d16SRodney W. Grimes 			*p++ = ch;
2731ea022d16SRodney W. Grimes 	while (p < LastArgv)
2732ea022d16SRodney W. Grimes 		*p++ = ' ';
2733ea022d16SRodney W. Grimes }
2734b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */
27353eb568f2SGuido van Rooij 
273661f891a6SPaul Traina static void
27373eb568f2SGuido van Rooij logxfer(name, size, start)
27383eb568f2SGuido van Rooij 	char *name;
27393eb568f2SGuido van Rooij 	long size;
27403eb568f2SGuido van Rooij 	long start;
27413eb568f2SGuido van Rooij {
27423eb568f2SGuido van Rooij 	char buf[1024];
27433eb568f2SGuido van Rooij 	char path[MAXPATHLEN + 1];
2744158a00b2SJohn Birrell 	time_t now;
27453eb568f2SGuido van Rooij 
27463eb568f2SGuido van Rooij 	if (statfd >= 0 && getwd(path) != NULL) {
27473eb568f2SGuido van Rooij 		time(&now);
274861f891a6SPaul Traina 		snprintf(buf, sizeof(buf), "%.20s!%s!%s!%s/%s!%ld!%ld\n",
27493eb568f2SGuido van Rooij 			ctime(&now)+4, ident, remotehost,
27503eb568f2SGuido van Rooij 			path, name, size, now - start + (now == start));
27513eb568f2SGuido van Rooij 		write(statfd, buf, strlen(buf));
27523eb568f2SGuido van Rooij 	}
27533eb568f2SGuido van Rooij }
2754