xref: /freebsd/libexec/ftpd/ftpd.c (revision 4b82fc955f15e8f0aeeecab222072b8c1898f422)
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/ioctl.h>
559fc5823aSGarrett Wollman #include <sys/mman.h>
56eb2fc780SGarrett Wollman #include <sys/socket.h>
57eb2fc780SGarrett Wollman #include <sys/stat.h>
58eb2fc780SGarrett Wollman #include <sys/time.h>
59eb2fc780SGarrett Wollman #include <sys/wait.h>
60ea022d16SRodney W. Grimes 
61ea022d16SRodney W. Grimes #include <netinet/in.h>
62ea022d16SRodney W. Grimes #include <netinet/in_systm.h>
63ea022d16SRodney W. Grimes #include <netinet/ip.h>
649fc5823aSGarrett Wollman #include <netinet/tcp.h>
65ea022d16SRodney W. Grimes 
66ea022d16SRodney W. Grimes #define	FTP_NAMES
67ea022d16SRodney W. Grimes #include <arpa/ftp.h>
68ea022d16SRodney W. Grimes #include <arpa/inet.h>
69ea022d16SRodney W. Grimes #include <arpa/telnet.h>
70ea022d16SRodney W. Grimes 
71ea022d16SRodney W. Grimes #include <ctype.h>
72ea022d16SRodney W. Grimes #include <dirent.h>
73ea022d16SRodney W. Grimes #include <err.h>
74ea022d16SRodney W. Grimes #include <errno.h>
75ea022d16SRodney W. Grimes #include <fcntl.h>
76ea022d16SRodney W. Grimes #include <glob.h>
77ea022d16SRodney W. Grimes #include <limits.h>
78ea022d16SRodney W. Grimes #include <netdb.h>
79ea022d16SRodney W. Grimes #include <pwd.h>
8031fea7b8SDavid Nugent #include <grp.h>
8147499ecdSAndrey A. Chernov #include <opie.h>
82ea022d16SRodney W. Grimes #include <signal.h>
83ea022d16SRodney W. Grimes #include <stdio.h>
84ea022d16SRodney W. Grimes #include <stdlib.h>
85ea022d16SRodney W. Grimes #include <string.h>
86ea022d16SRodney W. Grimes #include <syslog.h>
87ea022d16SRodney W. Grimes #include <time.h>
88ea022d16SRodney W. Grimes #include <unistd.h>
89b63e1fe2SPeter Wemm #include <libutil.h>
90b071c689SDavid Nugent #ifdef	LOGIN_CAP
91b071c689SDavid Nugent #include <login_cap.h>
92b071c689SDavid Nugent #endif
93ea022d16SRodney W. Grimes 
945bc9d93dSMark Murray #ifdef USE_PAM
956c9134c0SMark Murray #include <security/pam_appl.h>
966c9134c0SMark Murray #endif
976c9134c0SMark Murray 
98ea022d16SRodney W. Grimes #include "pathnames.h"
99ea022d16SRodney W. Grimes #include "extern.h"
100ea022d16SRodney W. Grimes 
101ea022d16SRodney W. Grimes #if __STDC__
102ea022d16SRodney W. Grimes #include <stdarg.h>
103ea022d16SRodney W. Grimes #else
104ea022d16SRodney W. Grimes #include <varargs.h>
105ea022d16SRodney W. Grimes #endif
106ea022d16SRodney W. Grimes 
107af85d782SDavid Nugent static char version[] = "Version 6.00LS";
108af85d782SDavid Nugent #undef main
109ea022d16SRodney W. Grimes 
1104dd8b5abSYoshinobu Inoue /* wrapper for KAME-special getnameinfo() */
1114dd8b5abSYoshinobu Inoue #ifndef NI_WITHSCOPEID
1124dd8b5abSYoshinobu Inoue #define	NI_WITHSCOPEID	0
1134dd8b5abSYoshinobu Inoue #endif
1144dd8b5abSYoshinobu Inoue 
115ea022d16SRodney W. Grimes extern	off_t restart_point;
116ea022d16SRodney W. Grimes extern	char cbuf[];
117ea022d16SRodney W. Grimes 
1184dd8b5abSYoshinobu Inoue union sockunion server_addr;
1194dd8b5abSYoshinobu Inoue union sockunion ctrl_addr;
1204dd8b5abSYoshinobu Inoue union sockunion data_source;
1214dd8b5abSYoshinobu Inoue union sockunion data_dest;
1224dd8b5abSYoshinobu Inoue union sockunion his_addr;
1234dd8b5abSYoshinobu Inoue union sockunion pasv_addr;
124ea022d16SRodney W. Grimes 
125cf09a206SDavid Greenman int	daemon_mode;
126ea022d16SRodney W. Grimes int	data;
127ea022d16SRodney W. Grimes int	logged_in;
128ea022d16SRodney W. Grimes struct	passwd *pw;
129618b0bbaSMark Murray int	ftpdebug;
130ea022d16SRodney W. Grimes int	timeout = 900;    /* timeout after 15 minutes of inactivity */
131ea022d16SRodney W. Grimes int	maxtimeout = 7200;/* don't allow idle time to be set beyond 2 hours */
132ea022d16SRodney W. Grimes int	logging;
1334c450ad7SPaul Traina int	restricted_data_ports = 1;
134a5a4544eSPaul Traina int	paranoid = 1;	  /* be extra careful about security */
1355a392aecSTorsten Blum int	anon_only = 0;    /* Only anonymous ftp allowed */
136ea022d16SRodney W. Grimes int	guest;
137a5a4544eSPaul Traina int	dochroot;
1383eb568f2SGuido van Rooij int	stats;
1393eb568f2SGuido van Rooij int	statfd = -1;
140ea022d16SRodney W. Grimes int	type;
141ea022d16SRodney W. Grimes int	form;
142ea022d16SRodney W. Grimes int	stru;			/* avoid C keyword */
143ea022d16SRodney W. Grimes int	mode;
144ea022d16SRodney W. Grimes int	usedefault = 1;		/* for data transfers */
145ea022d16SRodney W. Grimes int	pdata = -1;		/* for passive mode */
146a4b77a2aSPoul-Henning Kamp int	readonly=0;		/* Server is in readonly mode.	*/
147a4b77a2aSPoul-Henning Kamp int	noepsv=0;		/* EPSV command is disabled.	*/
14862513e76SNik Clayton int	noretr=0;		/* RETR command is disabled.	*/
1491cc9f0bbSSheldon Hearn int	noguestretr=0;		/* RETR command is disabled for anon users. */
15062513e76SNik Clayton 
1514b82fc95SYaroslav Tykhiy static volatile sig_atomic_t recvurg;
152ea022d16SRodney W. Grimes sig_atomic_t transflag;
153ea022d16SRodney W. Grimes off_t	file_size;
154ea022d16SRodney W. Grimes off_t	byte_count;
155ea022d16SRodney W. Grimes #if !defined(CMASK) || CMASK == 0
156ea022d16SRodney W. Grimes #undef CMASK
157ea022d16SRodney W. Grimes #define CMASK 027
158ea022d16SRodney W. Grimes #endif
159ea022d16SRodney W. Grimes int	defumask = CMASK;		/* default umask value */
160ea022d16SRodney W. Grimes char	tmpline[7];
161ea4e54b9SDavid Nugent char	*hostname;
162ad442344SDima Dorfman int	epsvall = 0;
163ad442344SDima Dorfman 
1640512556aSDavid Nugent #ifdef VIRTUAL_HOSTING
165ea4e54b9SDavid Nugent char	*ftpuser;
166ea4e54b9SDavid Nugent 
167ea4e54b9SDavid Nugent static struct ftphost {
168ea4e54b9SDavid Nugent 	struct ftphost	*next;
169f38c6cadSYoshinobu Inoue 	struct addrinfo *hostinfo;
170ea4e54b9SDavid Nugent 	char		*hostname;
171ea4e54b9SDavid Nugent 	char		*anonuser;
172ea4e54b9SDavid Nugent 	char		*statfile;
173ea4e54b9SDavid Nugent 	char		*welcome;
174ea4e54b9SDavid Nugent 	char		*loginmsg;
175ea4e54b9SDavid Nugent } *thishost, *firsthost;
176ea4e54b9SDavid Nugent 
177ea4e54b9SDavid Nugent #endif
1789e9a43bdSBrian Somers char	remotehost[MAXHOSTNAMELEN];
1793eb568f2SGuido van Rooij char	*ident = NULL;
180a5a4544eSPaul Traina 
181a5a4544eSPaul Traina static char ttyline[20];
182a5a4544eSPaul Traina char	*tty = ttyline;		/* for klogin */
183a5a4544eSPaul Traina 
1845bc9d93dSMark Murray #ifdef USE_PAM
1856c9134c0SMark Murray static int	auth_pam __P((struct passwd**, const char*));
1865bc9d93dSMark Murray pam_handle_t *pamh = NULL;
18747499ecdSAndrey A. Chernov #endif
188fa1746c9SMark Murray 
189fa1746c9SMark Murray static struct opie opiedata;
190fa1746c9SMark Murray static char opieprompt[OPIE_CHALLENGE_MAX+1];
19147499ecdSAndrey A. Chernov static int pwok;
1929aca17cbSMark Murray 
193105a3c98SJulian Elischer char	*pid_file = NULL;
194105a3c98SJulian Elischer 
195ea022d16SRodney W. Grimes /*
1966d10cb2fSJonathan Lemon  * Limit number of pathnames that glob can return.
1976d10cb2fSJonathan Lemon  * A limit of 0 indicates the number of pathnames is unlimited.
1986d10cb2fSJonathan Lemon  */
1996d10cb2fSJonathan Lemon #define MAXGLOBARGS	16384
2006d10cb2fSJonathan Lemon #
2016d10cb2fSJonathan Lemon 
2026d10cb2fSJonathan Lemon /*
203ea022d16SRodney W. Grimes  * Timeout intervals for retrying connections
204ea022d16SRodney W. Grimes  * to hosts that don't accept PORT cmds.  This
205ea022d16SRodney W. Grimes  * is a kludge, but given the problems with TCP...
206ea022d16SRodney W. Grimes  */
207ea022d16SRodney W. Grimes #define	SWAITMAX	90	/* wait at most 90 seconds */
208ea022d16SRodney W. Grimes #define	SWAITINT	5	/* interval between retries */
209ea022d16SRodney W. Grimes 
210ea022d16SRodney W. Grimes int	swaitmax = SWAITMAX;
211ea022d16SRodney W. Grimes int	swaitint = SWAITINT;
212ea022d16SRodney W. Grimes 
213ea022d16SRodney W. Grimes #ifdef SETPROCTITLE
214b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE
215ea022d16SRodney W. Grimes char	**Argv = NULL;		/* pointer to argument vector */
216ea022d16SRodney W. Grimes char	*LastArgv = NULL;	/* end of argv */
217b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */
218ea022d16SRodney W. Grimes char	proctitle[LINE_MAX];	/* initial part of title */
219ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */
220ea022d16SRodney W. Grimes 
221ea022d16SRodney W. Grimes #define LOGCMD(cmd, file) \
222ea022d16SRodney W. Grimes 	if (logging > 1) \
223ea022d16SRodney W. Grimes 	    syslog(LOG_INFO,"%s %s%s", cmd, \
224ea022d16SRodney W. Grimes 		*(file) == '/' ? "" : curdir(), file);
225ea022d16SRodney W. Grimes #define LOGCMD2(cmd, file1, file2) \
226ea022d16SRodney W. Grimes 	 if (logging > 1) \
227ea022d16SRodney W. Grimes 	    syslog(LOG_INFO,"%s %s%s %s%s", cmd, \
228ea022d16SRodney W. Grimes 		*(file1) == '/' ? "" : curdir(), file1, \
229ea022d16SRodney W. Grimes 		*(file2) == '/' ? "" : curdir(), file2);
230ea022d16SRodney W. Grimes #define LOGBYTES(cmd, file, cnt) \
231ea022d16SRodney W. Grimes 	if (logging > 1) { \
232ea022d16SRodney W. Grimes 		if (cnt == (off_t)-1) \
233ea022d16SRodney W. Grimes 		    syslog(LOG_INFO,"%s %s%s", cmd, \
234ea022d16SRodney W. Grimes 			*(file) == '/' ? "" : curdir(), file); \
235ea022d16SRodney W. Grimes 		else \
236ea022d16SRodney W. Grimes 		    syslog(LOG_INFO, "%s %s%s = %qd bytes", \
237ea022d16SRodney W. Grimes 			cmd, (*(file) == '/') ? "" : curdir(), file, cnt); \
238ea022d16SRodney W. Grimes 	}
239ea022d16SRodney W. Grimes 
240ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
241ea4e54b9SDavid Nugent static void	 inithosts __P((void));
2424dd8b5abSYoshinobu Inoue static void	selecthost __P((union sockunion *));
243ea4e54b9SDavid Nugent #endif
244ea022d16SRodney W. Grimes static void	 ack __P((char *));
2454b82fc95SYaroslav Tykhiy static void	 sigurg __P((int));
2464b82fc95SYaroslav Tykhiy static void	 myoob __P((void));
2477edcb936SSteve Price static int	 checkuser __P((char *, char *, int));
248ea022d16SRodney W. Grimes static FILE	*dataconn __P((char *, off_t, char *));
2494dd8b5abSYoshinobu Inoue static void	 dolog __P((struct sockaddr *));
250ea022d16SRodney W. Grimes static char	*curdir __P((void));
251ea022d16SRodney W. Grimes static void	 end_login __P((void));
252ea022d16SRodney W. Grimes static FILE	*getdatasock __P((char *));
253ea022d16SRodney W. Grimes static char	*gunique __P((char *));
254ea022d16SRodney W. Grimes static void	 lostconn __P((int));
2554b82fc95SYaroslav Tykhiy static void	 sigquit __P((int));
256ea022d16SRodney W. Grimes static int	 receive_data __P((FILE *, FILE *));
2574b82fc95SYaroslav Tykhiy static int	 send_data __P((FILE *, FILE *, off_t, off_t, int));
258ea022d16SRodney W. Grimes static struct passwd *
259ea022d16SRodney W. Grimes 		 sgetpwnam __P((char *));
260ea022d16SRodney W. Grimes static char	*sgetsave __P((char *));
261cf09a206SDavid Greenman static void	 reapchild __P((int));
262e4a71114SAndrey A. Chernov static void      logxfer __P((char *, off_t, time_t));
263ea022d16SRodney W. Grimes 
264ea022d16SRodney W. Grimes static char *
265ea022d16SRodney W. Grimes curdir()
266ea022d16SRodney W. Grimes {
267ea022d16SRodney W. Grimes 	static char path[MAXPATHLEN+1+1];	/* path + '/' + '\0' */
268ea022d16SRodney W. Grimes 
269ea022d16SRodney W. Grimes 	if (getcwd(path, sizeof(path)-2) == NULL)
270ea022d16SRodney W. Grimes 		return ("");
271ea022d16SRodney W. Grimes 	if (path[1] != '\0')		/* special case for root dir. */
272ea022d16SRodney W. Grimes 		strcat(path, "/");
273ea022d16SRodney W. Grimes 	/* For guest account, skip / since it's chrooted */
274ea022d16SRodney W. Grimes 	return (guest ? path+1 : path);
275ea022d16SRodney W. Grimes }
276ea022d16SRodney W. Grimes 
277ea022d16SRodney W. Grimes int
278ea022d16SRodney W. Grimes main(argc, argv, envp)
279ea022d16SRodney W. Grimes 	int argc;
280ea022d16SRodney W. Grimes 	char *argv[];
281ea022d16SRodney W. Grimes 	char **envp;
282ea022d16SRodney W. Grimes {
283ea022d16SRodney W. Grimes 	int addrlen, ch, on = 1, tos;
284ea022d16SRodney W. Grimes 	char *cp, line[LINE_MAX];
285ea022d16SRodney W. Grimes 	FILE *fd;
2864dd8b5abSYoshinobu Inoue 	int error;
2874dd8b5abSYoshinobu Inoue 	char	*bindname = NULL;
2884dd8b5abSYoshinobu Inoue 	int	family = AF_UNSPEC;
2894dd8b5abSYoshinobu Inoue 	int	enable_v4 = 0;
2904b82fc95SYaroslav Tykhiy 	struct sigaction sa;
291ea022d16SRodney W. Grimes 
29234d1ba5cSAndrey A. Chernov 	tzset();		/* in case no timezone database in ~ftp */
2934b82fc95SYaroslav Tykhiy 	sigemptyset(&sa.sa_mask);
2944b82fc95SYaroslav Tykhiy 	sa.sa_flags = SA_RESTART;
29534d1ba5cSAndrey A. Chernov 
296b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE
297ea022d16SRodney W. Grimes 	/*
298ea022d16SRodney W. Grimes 	 *  Save start and extent of argv for setproctitle.
299ea022d16SRodney W. Grimes 	 */
300ea022d16SRodney W. Grimes 	Argv = argv;
301ea022d16SRodney W. Grimes 	while (*envp)
302ea022d16SRodney W. Grimes 		envp++;
303ea022d16SRodney W. Grimes 	LastArgv = envp[-1] + strlen(envp[-1]);
304b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */
305ea022d16SRodney W. Grimes 
3063eb568f2SGuido van Rooij 
3071cc9f0bbSSheldon Hearn 	while ((ch = getopt(argc, argv, "AdlDESURrt:T:u:vOoa:p:46")) != -1) {
308ea022d16SRodney W. Grimes 		switch (ch) {
309cf09a206SDavid Greenman 		case 'D':
310cf09a206SDavid Greenman 			daemon_mode++;
311cf09a206SDavid Greenman 			break;
312cf09a206SDavid Greenman 
313ea022d16SRodney W. Grimes 		case 'd':
314618b0bbaSMark Murray 			ftpdebug++;
315ea022d16SRodney W. Grimes 			break;
316ea022d16SRodney W. Grimes 
317a4b77a2aSPoul-Henning Kamp 		case 'E':
318a4b77a2aSPoul-Henning Kamp 			noepsv = 1;
319a4b77a2aSPoul-Henning Kamp 			break;
320a4b77a2aSPoul-Henning Kamp 
321ea022d16SRodney W. Grimes 		case 'l':
322ea022d16SRodney W. Grimes 			logging++;	/* > 1 == extra logging */
323ea022d16SRodney W. Grimes 			break;
324ea022d16SRodney W. Grimes 
325a4b77a2aSPoul-Henning Kamp 		case 'r':
326a4b77a2aSPoul-Henning Kamp 			readonly = 1;
327a4b77a2aSPoul-Henning Kamp 			break;
328a4b77a2aSPoul-Henning Kamp 
329a5a4544eSPaul Traina 		case 'R':
330a5a4544eSPaul Traina 			paranoid = 0;
331a5a4544eSPaul Traina 			break;
332a5a4544eSPaul Traina 
333a5a4544eSPaul Traina 		case 'S':
334a5a4544eSPaul Traina 			stats++;
335a5a4544eSPaul Traina 			break;
336a5a4544eSPaul Traina 
337a5a4544eSPaul Traina 		case 'T':
338a5a4544eSPaul Traina 			maxtimeout = atoi(optarg);
339a5a4544eSPaul Traina 			if (timeout > maxtimeout)
340a5a4544eSPaul Traina 				timeout = maxtimeout;
3414c450ad7SPaul Traina 			break;
3424c450ad7SPaul Traina 
343ea022d16SRodney W. Grimes 		case 't':
344ea022d16SRodney W. Grimes 			timeout = atoi(optarg);
345ea022d16SRodney W. Grimes 			if (maxtimeout < timeout)
346ea022d16SRodney W. Grimes 				maxtimeout = timeout;
347ea022d16SRodney W. Grimes 			break;
348a5a4544eSPaul Traina 
349a5a4544eSPaul Traina 		case 'U':
350a5a4544eSPaul Traina 			restricted_data_ports = 0;
351ea022d16SRodney W. Grimes 			break;
352ea022d16SRodney W. Grimes 
353105a3c98SJulian Elischer 		case 'a':
3544dd8b5abSYoshinobu Inoue 			bindname = optarg;
355105a3c98SJulian Elischer 			break;
356105a3c98SJulian Elischer 
357105a3c98SJulian Elischer 		case 'p':
358105a3c98SJulian Elischer 			pid_file = optarg;
359105a3c98SJulian Elischer 			break;
360105a3c98SJulian Elischer 
361ea022d16SRodney W. Grimes 		case 'u':
362ea022d16SRodney W. Grimes 		    {
363ea022d16SRodney W. Grimes 			long val = 0;
364ea022d16SRodney W. Grimes 
365ea022d16SRodney W. Grimes 			val = strtol(optarg, &optarg, 8);
366ea022d16SRodney W. Grimes 			if (*optarg != '\0' || val < 0)
367ea022d16SRodney W. Grimes 				warnx("bad value for -u");
368ea022d16SRodney W. Grimes 			else
369ea022d16SRodney W. Grimes 				defumask = val;
370ea022d16SRodney W. Grimes 			break;
371ea022d16SRodney W. Grimes 		    }
3725a392aecSTorsten Blum 		case 'A':
3735a392aecSTorsten Blum 			anon_only = 1;
3745a392aecSTorsten Blum 			break;
375ea022d16SRodney W. Grimes 
376ea022d16SRodney W. Grimes 		case 'v':
377618b0bbaSMark Murray 			ftpdebug = 1;
378ea022d16SRodney W. Grimes 			break;
379ea022d16SRodney W. Grimes 
3804dd8b5abSYoshinobu Inoue 		case '4':
3814dd8b5abSYoshinobu Inoue 			enable_v4 = 1;
3824dd8b5abSYoshinobu Inoue 			if (family == AF_UNSPEC)
3834dd8b5abSYoshinobu Inoue 				family = AF_INET;
3844dd8b5abSYoshinobu Inoue 			break;
3854dd8b5abSYoshinobu Inoue 
3864dd8b5abSYoshinobu Inoue 		case '6':
3874dd8b5abSYoshinobu Inoue 			family = AF_INET6;
3884dd8b5abSYoshinobu Inoue 			break;
3894dd8b5abSYoshinobu Inoue 
3901cc9f0bbSSheldon Hearn 		case 'O':
3911cc9f0bbSSheldon Hearn 			noguestretr = 1;
3921cc9f0bbSSheldon Hearn 			break;
3931cc9f0bbSSheldon Hearn 
39462513e76SNik Clayton 		case 'o':
39562513e76SNik Clayton 			noretr = 1;
39662513e76SNik Clayton 			break;
39762513e76SNik Clayton 
398ea022d16SRodney W. Grimes 		default:
399ea022d16SRodney W. Grimes 			warnx("unknown flag -%c ignored", optopt);
400ea022d16SRodney W. Grimes 			break;
401ea022d16SRodney W. Grimes 		}
402ea022d16SRodney W. Grimes 	}
403cf09a206SDavid Greenman 
404ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
405ea4e54b9SDavid Nugent 	inithosts();
406ea4e54b9SDavid Nugent #endif
407ea022d16SRodney W. Grimes 	(void) freopen(_PATH_DEVNULL, "w", stderr);
408cf09a206SDavid Greenman 
409cf09a206SDavid Greenman 	/*
410cf09a206SDavid Greenman 	 * LOG_NDELAY sets up the logging connection immediately,
411cf09a206SDavid Greenman 	 * necessary for anonymous ftp's that chroot and can't do it later.
412cf09a206SDavid Greenman 	 */
413cf09a206SDavid Greenman 	openlog("ftpd", LOG_PID | LOG_NDELAY, LOG_FTP);
414cf09a206SDavid Greenman 
415cf09a206SDavid Greenman 	if (daemon_mode) {
416cf09a206SDavid Greenman 		int ctl_sock, fd;
4174dd8b5abSYoshinobu Inoue 		struct addrinfo hints, *res;
418cf09a206SDavid Greenman 
419cf09a206SDavid Greenman 		/*
420cf09a206SDavid Greenman 		 * Detach from parent.
421cf09a206SDavid Greenman 		 */
422cf09a206SDavid Greenman 		if (daemon(1, 1) < 0) {
423cf09a206SDavid Greenman 			syslog(LOG_ERR, "failed to become a daemon");
424cf09a206SDavid Greenman 			exit(1);
425cf09a206SDavid Greenman 		}
4264b82fc95SYaroslav Tykhiy 		sa.sa_handler = reapchild;
4274b82fc95SYaroslav Tykhiy 		(void)sigaction(SIGCHLD, &sa, NULL);
4284dd8b5abSYoshinobu Inoue 		/* init bind_sa */
4294dd8b5abSYoshinobu Inoue 		memset(&hints, 0, sizeof(hints));
4304dd8b5abSYoshinobu Inoue 
4314dd8b5abSYoshinobu Inoue 		hints.ai_family = family == AF_UNSPEC ? AF_INET : family;
4324dd8b5abSYoshinobu Inoue 		hints.ai_socktype = SOCK_STREAM;
4334dd8b5abSYoshinobu Inoue 		hints.ai_protocol = 0;
4344dd8b5abSYoshinobu Inoue 		hints.ai_flags = AI_PASSIVE;
4354dd8b5abSYoshinobu Inoue 		error = getaddrinfo(bindname, "ftp", &hints, &res);
4364dd8b5abSYoshinobu Inoue 		if (error) {
4374dd8b5abSYoshinobu Inoue 			if (family == AF_UNSPEC) {
4384dd8b5abSYoshinobu Inoue 				hints.ai_family = AF_UNSPEC;
4394dd8b5abSYoshinobu Inoue 				error = getaddrinfo(bindname, "ftp", &hints,
4404dd8b5abSYoshinobu Inoue 						    &res);
4414dd8b5abSYoshinobu Inoue 			}
4424dd8b5abSYoshinobu Inoue 		}
4434dd8b5abSYoshinobu Inoue 		if (error) {
4443fb3b78fSKris Kennaway 			syslog(LOG_ERR, "%s", gai_strerror(error));
4454dd8b5abSYoshinobu Inoue 			if (error == EAI_SYSTEM)
4463fb3b78fSKris Kennaway 				syslog(LOG_ERR, "%s", strerror(errno));
4474dd8b5abSYoshinobu Inoue 			exit(1);
4484dd8b5abSYoshinobu Inoue 		}
4494dd8b5abSYoshinobu Inoue 		if (res->ai_addr == NULL) {
4504dd8b5abSYoshinobu Inoue 			syslog(LOG_ERR, "-a %s: getaddrinfo failed", hostname);
451cf09a206SDavid Greenman 			exit(1);
4522310b8c6SRuslan Ermilov 		} else
4532310b8c6SRuslan Ermilov 			family = res->ai_addr->sa_family;
454cf09a206SDavid Greenman 		/*
455cf09a206SDavid Greenman 		 * Open a socket, bind it to the FTP port, and start
456cf09a206SDavid Greenman 		 * listening.
457cf09a206SDavid Greenman 		 */
4584dd8b5abSYoshinobu Inoue 		ctl_sock = socket(family, SOCK_STREAM, 0);
459cf09a206SDavid Greenman 		if (ctl_sock < 0) {
460cf09a206SDavid Greenman 			syslog(LOG_ERR, "control socket: %m");
461cf09a206SDavid Greenman 			exit(1);
462cf09a206SDavid Greenman 		}
463cf09a206SDavid Greenman 		if (setsockopt(ctl_sock, SOL_SOCKET, SO_REUSEADDR,
464cf09a206SDavid Greenman 		    (char *)&on, sizeof(on)) < 0)
4654dd8b5abSYoshinobu Inoue 			syslog(LOG_ERR, "control setsockopt: %m");
4664dd8b5abSYoshinobu Inoue #ifdef IPV6_BINDV6ONLY
4674dd8b5abSYoshinobu Inoue 		if (family == AF_INET6 && enable_v4 == 0) {
4684dd8b5abSYoshinobu Inoue 			if (setsockopt(ctl_sock, IPPROTO_IPV6, IPV6_BINDV6ONLY,
4694dd8b5abSYoshinobu Inoue 				       (char *)&on, sizeof (on)) < 0)
4704dd8b5abSYoshinobu Inoue 				syslog(LOG_ERR,
4714dd8b5abSYoshinobu Inoue 				       "control setsockopt(IPV6_BINDV6ONLY): %m");
4724dd8b5abSYoshinobu Inoue 		}
4734dd8b5abSYoshinobu Inoue #endif /* IPV6_BINDV6ONLY */
4744dd8b5abSYoshinobu Inoue 		memcpy(&server_addr, res->ai_addr, res->ai_addr->sa_len);
4754dd8b5abSYoshinobu Inoue 		if (bind(ctl_sock, (struct sockaddr *)&server_addr,
4764dd8b5abSYoshinobu Inoue 			 server_addr.su_len) < 0) {
477cf09a206SDavid Greenman 			syslog(LOG_ERR, "control bind: %m");
478cf09a206SDavid Greenman 			exit(1);
479cf09a206SDavid Greenman 		}
480cf09a206SDavid Greenman 		if (listen(ctl_sock, 32) < 0) {
481cf09a206SDavid Greenman 			syslog(LOG_ERR, "control listen: %m");
482cf09a206SDavid Greenman 			exit(1);
483cf09a206SDavid Greenman 		}
484cf09a206SDavid Greenman 		/*
485105a3c98SJulian Elischer 		 * Atomically write process ID
486105a3c98SJulian Elischer 		 */
487105a3c98SJulian Elischer 		if (pid_file)
488105a3c98SJulian Elischer 		{
489105a3c98SJulian Elischer 			int fd;
490105a3c98SJulian Elischer 			char buf[20];
491105a3c98SJulian Elischer 
492105a3c98SJulian Elischer 			fd = open(pid_file, O_CREAT | O_WRONLY | O_TRUNC
493105a3c98SJulian Elischer 				| O_NONBLOCK | O_EXLOCK, 0644);
49485966371SWarner Losh 			if (fd < 0) {
495105a3c98SJulian Elischer 				if (errno == EAGAIN)
496105a3c98SJulian Elischer 					errx(1, "%s: file locked", pid_file);
497105a3c98SJulian Elischer 				else
498105a3c98SJulian Elischer 					err(1, "%s", pid_file);
49985966371SWarner Losh 			}
500105a3c98SJulian Elischer 			snprintf(buf, sizeof(buf),
501105a3c98SJulian Elischer 				"%lu\n", (unsigned long) getpid());
502105a3c98SJulian Elischer 			if (write(fd, buf, strlen(buf)) < 0)
503105a3c98SJulian Elischer 				err(1, "%s: write", pid_file);
504105a3c98SJulian Elischer 			/* Leave the pid file open and locked */
505105a3c98SJulian Elischer 		}
506105a3c98SJulian Elischer 		/*
507cf09a206SDavid Greenman 		 * Loop forever accepting connection requests and forking off
508cf09a206SDavid Greenman 		 * children to handle them.
509cf09a206SDavid Greenman 		 */
510cf09a206SDavid Greenman 		while (1) {
5114dd8b5abSYoshinobu Inoue 			addrlen = server_addr.su_len;
512cf09a206SDavid Greenman 			fd = accept(ctl_sock, (struct sockaddr *)&his_addr, &addrlen);
513cf09a206SDavid Greenman 			if (fork() == 0) {
514cf09a206SDavid Greenman 				/* child */
515cf09a206SDavid Greenman 				(void) dup2(fd, 0);
516cf09a206SDavid Greenman 				(void) dup2(fd, 1);
517cf09a206SDavid Greenman 				close(ctl_sock);
518cf09a206SDavid Greenman 				break;
519cf09a206SDavid Greenman 			}
520cf09a206SDavid Greenman 			close(fd);
521cf09a206SDavid Greenman 		}
522cf09a206SDavid Greenman 	} else {
523cf09a206SDavid Greenman 		addrlen = sizeof(his_addr);
524cf09a206SDavid Greenman 		if (getpeername(0, (struct sockaddr *)&his_addr, &addrlen) < 0) {
525cf09a206SDavid Greenman 			syslog(LOG_ERR, "getpeername (%s): %m",argv[0]);
526cf09a206SDavid Greenman 			exit(1);
527cf09a206SDavid Greenman 		}
528cf09a206SDavid Greenman 	}
529cf09a206SDavid Greenman 
5304b82fc95SYaroslav Tykhiy 	sa.sa_handler = SIG_DFL;
5314b82fc95SYaroslav Tykhiy 	(void)sigaction(SIGCHLD, &sa, NULL);
5324b82fc95SYaroslav Tykhiy 
5334b82fc95SYaroslav Tykhiy 	sa.sa_handler = sigurg;
5344b82fc95SYaroslav Tykhiy 	sa.sa_flags = 0;		/* don't restart syscalls for SIGURG */
5354b82fc95SYaroslav Tykhiy 	(void)sigaction(SIGURG, &sa, NULL);
5364b82fc95SYaroslav Tykhiy 
5374b82fc95SYaroslav Tykhiy 	sigfillset(&sa.sa_mask);	/* block all signals in handler */
5384b82fc95SYaroslav Tykhiy 	sa.sa_flags = SA_RESTART;
5394b82fc95SYaroslav Tykhiy 	sa.sa_handler = sigquit;
5404b82fc95SYaroslav Tykhiy 	(void)sigaction(SIGHUP, &sa, NULL);
5414b82fc95SYaroslav Tykhiy 	(void)sigaction(SIGINT, &sa, NULL);
5424b82fc95SYaroslav Tykhiy 	(void)sigaction(SIGQUIT, &sa, NULL);
5434b82fc95SYaroslav Tykhiy 	(void)sigaction(SIGTERM, &sa, NULL);
5444b82fc95SYaroslav Tykhiy 
5454b82fc95SYaroslav Tykhiy 	sa.sa_handler = lostconn;
5464b82fc95SYaroslav Tykhiy 	(void)sigaction(SIGPIPE, &sa, NULL);
547ea022d16SRodney W. Grimes 
548cf09a206SDavid Greenman 	addrlen = sizeof(ctrl_addr);
549cf09a206SDavid Greenman 	if (getsockname(0, (struct sockaddr *)&ctrl_addr, &addrlen) < 0) {
550cf09a206SDavid Greenman 		syslog(LOG_ERR, "getsockname (%s): %m",argv[0]);
551cf09a206SDavid Greenman 		exit(1);
552cf09a206SDavid Greenman 	}
553ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
554ea4e54b9SDavid Nugent 	/* select our identity from virtual host table */
5554dd8b5abSYoshinobu Inoue 	selecthost(&ctrl_addr);
556ea4e54b9SDavid Nugent #endif
557cf09a206SDavid Greenman #ifdef IP_TOS
5584dd8b5abSYoshinobu Inoue 	if (ctrl_addr.su_family == AF_INET)
5594dd8b5abSYoshinobu Inoue       {
560cf09a206SDavid Greenman 	tos = IPTOS_LOWDELAY;
561cf09a206SDavid Greenman 	if (setsockopt(0, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(int)) < 0)
562cf09a206SDavid Greenman 		syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
5634dd8b5abSYoshinobu Inoue       }
564cf09a206SDavid Greenman #endif
565dadb9fb3SDavid Greenman 	/*
566dadb9fb3SDavid Greenman 	 * Disable Nagle on the control channel so that we don't have to wait
567dadb9fb3SDavid Greenman 	 * for peer's ACK before issuing our next reply.
568dadb9fb3SDavid Greenman 	 */
569dadb9fb3SDavid Greenman 	if (setsockopt(0, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) < 0)
570dadb9fb3SDavid Greenman 		syslog(LOG_WARNING, "control setsockopt TCP_NODELAY: %m");
571dadb9fb3SDavid Greenman 
5724dd8b5abSYoshinobu Inoue 	data_source.su_port = htons(ntohs(ctrl_addr.su_port) - 1);
573cf09a206SDavid Greenman 
574a5a4544eSPaul Traina 	/* set this here so klogin can use it... */
575e760ef2cSWarner Losh 	(void)snprintf(ttyline, sizeof(ttyline), "ftp%d", getpid());
576a5a4544eSPaul Traina 
577ea022d16SRodney W. Grimes 	/* Try to handle urgent data inline */
578ea022d16SRodney W. Grimes #ifdef SO_OOBINLINE
579ea022d16SRodney W. Grimes 	if (setsockopt(0, SOL_SOCKET, SO_OOBINLINE, (char *)&on, sizeof(on)) < 0)
580ea022d16SRodney W. Grimes 		syslog(LOG_ERR, "setsockopt: %m");
581ea022d16SRodney W. Grimes #endif
582ea022d16SRodney W. Grimes 
583ea022d16SRodney W. Grimes #ifdef	F_SETOWN
584ea022d16SRodney W. Grimes 	if (fcntl(fileno(stdin), F_SETOWN, getpid()) == -1)
585ea022d16SRodney W. Grimes 		syslog(LOG_ERR, "fcntl F_SETOWN: %m");
586ea022d16SRodney W. Grimes #endif
5874dd8b5abSYoshinobu Inoue 	dolog((struct sockaddr *)&his_addr);
588ea022d16SRodney W. Grimes 	/*
589ea022d16SRodney W. Grimes 	 * Set up default state
590ea022d16SRodney W. Grimes 	 */
591ea022d16SRodney W. Grimes 	data = -1;
592ea022d16SRodney W. Grimes 	type = TYPE_A;
593ea022d16SRodney W. Grimes 	form = FORM_N;
594ea022d16SRodney W. Grimes 	stru = STRU_F;
595ea022d16SRodney W. Grimes 	mode = MODE_S;
596ea022d16SRodney W. Grimes 	tmpline[0] = '\0';
597ea022d16SRodney W. Grimes 
598ea022d16SRodney W. Grimes 	/* If logins are disabled, print out the message. */
599ea022d16SRodney W. Grimes 	if ((fd = fopen(_PATH_NOLOGIN,"r")) != NULL) {
600ea022d16SRodney W. Grimes 		while (fgets(line, sizeof(line), fd) != NULL) {
601ea022d16SRodney W. Grimes 			if ((cp = strchr(line, '\n')) != NULL)
602ea022d16SRodney W. Grimes 				*cp = '\0';
603ea022d16SRodney W. Grimes 			lreply(530, "%s", line);
604ea022d16SRodney W. Grimes 		}
605ea022d16SRodney W. Grimes 		(void) fflush(stdout);
606ea022d16SRodney W. Grimes 		(void) fclose(fd);
607ea022d16SRodney W. Grimes 		reply(530, "System not available.");
608ea022d16SRodney W. Grimes 		exit(0);
609ea022d16SRodney W. Grimes 	}
610ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
611ea4e54b9SDavid Nugent 	if ((fd = fopen(thishost->welcome, "r")) != NULL) {
612ea4e54b9SDavid Nugent #else
613ea022d16SRodney W. Grimes 	if ((fd = fopen(_PATH_FTPWELCOME, "r")) != NULL) {
614ea4e54b9SDavid Nugent #endif
615ea022d16SRodney W. Grimes 		while (fgets(line, sizeof(line), fd) != NULL) {
616ea022d16SRodney W. Grimes 			if ((cp = strchr(line, '\n')) != NULL)
617ea022d16SRodney W. Grimes 				*cp = '\0';
618ea022d16SRodney W. Grimes 			lreply(220, "%s", line);
619ea022d16SRodney W. Grimes 		}
620ea022d16SRodney W. Grimes 		(void) fflush(stdout);
621ea022d16SRodney W. Grimes 		(void) fclose(fd);
622ea022d16SRodney W. Grimes 		/* reply(220,) must follow */
623ea022d16SRodney W. Grimes 	}
624ea4e54b9SDavid Nugent #ifndef VIRTUAL_HOSTING
6250512556aSDavid Nugent 	if ((hostname = malloc(MAXHOSTNAMELEN)) == NULL)
626618b0bbaSMark Murray 		fatalerror("Ran out of memory.");
6279e9a43bdSBrian Somers 	(void) gethostname(hostname, MAXHOSTNAMELEN - 1);
6289e9a43bdSBrian Somers 	hostname[MAXHOSTNAMELEN - 1] = '\0';
629ea4e54b9SDavid Nugent #endif
630ea022d16SRodney W. Grimes 	reply(220, "%s FTP server (%s) ready.", hostname, version);
631ea022d16SRodney W. Grimes 	for (;;)
632ea022d16SRodney W. Grimes 		(void) yyparse();
633ea022d16SRodney W. Grimes 	/* NOTREACHED */
634ea022d16SRodney W. Grimes }
635ea022d16SRodney W. Grimes 
636ea022d16SRodney W. Grimes static void
637ea022d16SRodney W. Grimes lostconn(signo)
638ea022d16SRodney W. Grimes 	int signo;
639ea022d16SRodney W. Grimes {
640ea022d16SRodney W. Grimes 
641618b0bbaSMark Murray 	if (ftpdebug)
642ea022d16SRodney W. Grimes 		syslog(LOG_DEBUG, "lost connection");
643e02897faSPhilippe Charnier 	dologout(1);
644ea022d16SRodney W. Grimes }
645ea022d16SRodney W. Grimes 
6464b82fc95SYaroslav Tykhiy static void
6474b82fc95SYaroslav Tykhiy sigquit(signo)
6484b82fc95SYaroslav Tykhiy 	int signo;
6494b82fc95SYaroslav Tykhiy {
6504b82fc95SYaroslav Tykhiy 
6514b82fc95SYaroslav Tykhiy 	syslog(LOG_ERR, "got signal %d", signo);
6524b82fc95SYaroslav Tykhiy 	dologout(1);
6534b82fc95SYaroslav Tykhiy }
6544b82fc95SYaroslav Tykhiy 
655ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
656ea4e54b9SDavid Nugent /*
657ea4e54b9SDavid Nugent  * read in virtual host tables (if they exist)
658ea4e54b9SDavid Nugent  */
659ea4e54b9SDavid Nugent 
660ea4e54b9SDavid Nugent static void
661ea4e54b9SDavid Nugent inithosts()
662ea4e54b9SDavid Nugent {
663ea4e54b9SDavid Nugent 	FILE *fp;
664ea4e54b9SDavid Nugent 	char *cp;
665ea4e54b9SDavid Nugent 	struct ftphost *hrp, *lhrp;
666ea4e54b9SDavid Nugent 	char line[1024];
6674dd8b5abSYoshinobu Inoue 	struct addrinfo hints, *res, *ai;
668ea4e54b9SDavid Nugent 
669ea4e54b9SDavid Nugent 	/*
670ea4e54b9SDavid Nugent 	 * Fill in the default host information
671ea4e54b9SDavid Nugent 	 */
672ea4e54b9SDavid Nugent 	if (gethostname(line, sizeof(line)) < 0)
673ea4e54b9SDavid Nugent 		line[0] = '\0';
674ea4e54b9SDavid Nugent 	if ((hrp = malloc(sizeof(struct ftphost))) == NULL ||
675ea4e54b9SDavid Nugent 	    (hrp->hostname = strdup(line)) == NULL)
676618b0bbaSMark Murray 		fatalerror("Ran out of memory.");
677f38c6cadSYoshinobu Inoue 	hrp->hostinfo = NULL;
6784dd8b5abSYoshinobu Inoue 
6794dd8b5abSYoshinobu Inoue 	memset(&hints, 0, sizeof(hints));
6804dd8b5abSYoshinobu Inoue 	hints.ai_flags = AI_CANONNAME;
6814dd8b5abSYoshinobu Inoue 	hints.ai_family = AF_UNSPEC;
6824dd8b5abSYoshinobu Inoue 	getaddrinfo(hrp->hostname, NULL, &hints, &res);
6834dd8b5abSYoshinobu Inoue 	if (res)
684f38c6cadSYoshinobu Inoue 		hrp->hostinfo = res;
685ea4e54b9SDavid Nugent 	hrp->statfile = _PATH_FTPDSTATFILE;
686ea4e54b9SDavid Nugent 	hrp->welcome  = _PATH_FTPWELCOME;
687ea4e54b9SDavid Nugent 	hrp->loginmsg = _PATH_FTPLOGINMESG;
688ea4e54b9SDavid Nugent 	hrp->anonuser = "ftp";
689ea4e54b9SDavid Nugent 	hrp->next = NULL;
690ea4e54b9SDavid Nugent 	thishost = firsthost = lhrp = hrp;
691ea4e54b9SDavid Nugent 	if ((fp = fopen(_PATH_FTPHOSTS, "r")) != NULL) {
692b535a9bfSDavid Nugent 		int addrsize, error, gothost;
6934dd8b5abSYoshinobu Inoue 		void *addr;
6944dd8b5abSYoshinobu Inoue 		struct hostent *hp;
6954dd8b5abSYoshinobu Inoue 
696ea4e54b9SDavid Nugent 		while (fgets(line, sizeof(line), fp) != NULL) {
6974dd8b5abSYoshinobu Inoue 			int	i, hp_error;
698ea4e54b9SDavid Nugent 
699ea4e54b9SDavid Nugent 			if ((cp = strchr(line, '\n')) == NULL) {
700ea4e54b9SDavid Nugent 				/* ignore long lines */
701ea4e54b9SDavid Nugent 				while (fgets(line, sizeof(line), fp) != NULL &&
702ea4e54b9SDavid Nugent 					strchr(line, '\n') == NULL)
703ea4e54b9SDavid Nugent 					;
704ea4e54b9SDavid Nugent 				continue;
705ea4e54b9SDavid Nugent 			}
706ea4e54b9SDavid Nugent 			*cp = '\0';
707ea4e54b9SDavid Nugent 			cp = strtok(line, " \t");
708ea4e54b9SDavid Nugent 			/* skip comments and empty lines */
709ea4e54b9SDavid Nugent 			if (cp == NULL || line[0] == '#')
710ea4e54b9SDavid Nugent 				continue;
7114dd8b5abSYoshinobu Inoue 
7124dd8b5abSYoshinobu Inoue 			hints.ai_flags = 0;
7134dd8b5abSYoshinobu Inoue 			hints.ai_family = AF_UNSPEC;
7144dd8b5abSYoshinobu Inoue 			hints.ai_flags = AI_PASSIVE;
7154dd8b5abSYoshinobu Inoue 			error = getaddrinfo(cp, NULL, &hints, &res);
7164dd8b5abSYoshinobu Inoue 			if (error != NULL)
717ea4e54b9SDavid Nugent 				continue;
7184dd8b5abSYoshinobu Inoue 			for (ai = res; ai != NULL && ai->ai_addr != NULL;
719b535a9bfSDavid Nugent 			     ai = ai->ai_next) {
7204dd8b5abSYoshinobu Inoue 
721b535a9bfSDavid Nugent 			gothost = 0;
722ea4e54b9SDavid Nugent 			for (hrp = firsthost; hrp != NULL; hrp = hrp->next) {
723f38c6cadSYoshinobu Inoue 				struct addrinfo *hi;
724f38c6cadSYoshinobu Inoue 
725f38c6cadSYoshinobu Inoue 				for (hi = hrp->hostinfo; hi != NULL;
726f38c6cadSYoshinobu Inoue 				     hi = hi->ai_next)
727f38c6cadSYoshinobu Inoue 					if (hi->ai_addrlen == ai->ai_addrlen &&
728f38c6cadSYoshinobu Inoue 					    memcmp(hi->ai_addr,
7294dd8b5abSYoshinobu Inoue 						   ai->ai_addr,
730b535a9bfSDavid Nugent 						   ai->ai_addr->sa_len) == 0) {
731b535a9bfSDavid Nugent 						gothost++;
732b535a9bfSDavid Nugent 						break;
733b535a9bfSDavid Nugent 				}
734b535a9bfSDavid Nugent 				if (gothost)
735ea4e54b9SDavid Nugent 					break;
736ea4e54b9SDavid Nugent 			}
737ea4e54b9SDavid Nugent 			if (hrp == NULL) {
738ea4e54b9SDavid Nugent 				if ((hrp = malloc(sizeof(struct ftphost))) == NULL)
739ea4e54b9SDavid Nugent 					continue;
740ea4e54b9SDavid Nugent 				/* defaults */
741ea4e54b9SDavid Nugent 				hrp->statfile = _PATH_FTPDSTATFILE;
742ea4e54b9SDavid Nugent 				hrp->welcome  = _PATH_FTPWELCOME;
743ea4e54b9SDavid Nugent 				hrp->loginmsg = _PATH_FTPLOGINMESG;
744ea4e54b9SDavid Nugent 				hrp->anonuser = "ftp";
745ea4e54b9SDavid Nugent 				hrp->next     = NULL;
746ea4e54b9SDavid Nugent 				lhrp->next = hrp;
747ea4e54b9SDavid Nugent 				lhrp = hrp;
748ea4e54b9SDavid Nugent 			}
749f38c6cadSYoshinobu Inoue 			hrp->hostinfo = res;
750f38c6cadSYoshinobu Inoue 
751ea4e54b9SDavid Nugent 			/*
752ea4e54b9SDavid Nugent 			 * determine hostname to use.
7534dd8b5abSYoshinobu Inoue 			 * force defined name if there is a valid alias
754ea4e54b9SDavid Nugent 			 * otherwise fallback to primary hostname
755ea4e54b9SDavid Nugent 			 */
7564dd8b5abSYoshinobu Inoue 			/* XXX: getaddrinfo() can't do alias check */
757f38c6cadSYoshinobu Inoue 			switch(hrp->hostinfo->ai_family) {
7584dd8b5abSYoshinobu Inoue 			case AF_INET:
759f38c6cadSYoshinobu Inoue 				addr = &((struct sockaddr_in *)&hrp->hostinfo->ai_addr)->sin_addr;
7604dd8b5abSYoshinobu Inoue 				addrsize = sizeof(struct sockaddr_in);
7614dd8b5abSYoshinobu Inoue 				break;
7624dd8b5abSYoshinobu Inoue 			case AF_INET6:
763f38c6cadSYoshinobu Inoue 				addr = &((struct sockaddr_in6 *)&hrp->hostinfo->ai_addr)->sin6_addr;
7644dd8b5abSYoshinobu Inoue 				addrsize = sizeof(struct sockaddr_in6);
7654dd8b5abSYoshinobu Inoue 				break;
7664dd8b5abSYoshinobu Inoue 			default:
7674dd8b5abSYoshinobu Inoue 				/* should not reach here */
768f38c6cadSYoshinobu Inoue 				if (hrp->hostinfo != NULL)
769f38c6cadSYoshinobu Inoue 					freeaddrinfo(hrp->hostinfo);
7704dd8b5abSYoshinobu Inoue 				free(hrp);
7714dd8b5abSYoshinobu Inoue 				continue;
7724dd8b5abSYoshinobu Inoue 				/* NOTREACHED */
7734dd8b5abSYoshinobu Inoue 			}
7744dd8b5abSYoshinobu Inoue 			if ((hp = getipnodebyaddr((char*)addr, addrsize,
775f38c6cadSYoshinobu Inoue 						  hrp->hostinfo->ai_family,
7764dd8b5abSYoshinobu Inoue 						  &hp_error)) != NULL) {
777ea4e54b9SDavid Nugent 				if (strcmp(cp, hp->h_name) != 0) {
778ea4e54b9SDavid Nugent 					if (hp->h_aliases == NULL)
779ea4e54b9SDavid Nugent 						cp = hp->h_name;
780ea4e54b9SDavid Nugent 					else {
781ea4e54b9SDavid Nugent 						i = 0;
782ea4e54b9SDavid Nugent 						while (hp->h_aliases[i] &&
783ea4e54b9SDavid Nugent 						       strcmp(cp, hp->h_aliases[i]) != 0)
784ea4e54b9SDavid Nugent 							++i;
785ea4e54b9SDavid Nugent 						if (hp->h_aliases[i] == NULL)
786ea4e54b9SDavid Nugent 							cp = hp->h_name;
787ea4e54b9SDavid Nugent 					}
788ea4e54b9SDavid Nugent 				}
789ea4e54b9SDavid Nugent 			}
790ea4e54b9SDavid Nugent 			hrp->hostname = strdup(cp);
7914dd8b5abSYoshinobu Inoue 			freehostent(hp);
792ea4e54b9SDavid Nugent 			/* ok, now we now peel off the rest */
793ea4e54b9SDavid Nugent 			i = 0;
794ea4e54b9SDavid Nugent 			while (i < 4 && (cp = strtok(NULL, " \t")) != NULL) {
795ea4e54b9SDavid Nugent 				if (*cp != '-' && (cp = strdup(cp)) != NULL) {
796ea4e54b9SDavid Nugent 					switch (i) {
797ea4e54b9SDavid Nugent 					case 0:	/* anon user permissions */
798ea4e54b9SDavid Nugent 						hrp->anonuser = cp;
799ea4e54b9SDavid Nugent 						break;
800ea4e54b9SDavid Nugent 					case 1: /* statistics file */
801ea4e54b9SDavid Nugent 						hrp->statfile = cp;
802ea4e54b9SDavid Nugent 						break;
803ea4e54b9SDavid Nugent 					case 2: /* welcome message */
804ea4e54b9SDavid Nugent 						hrp->welcome  = cp;
805ea4e54b9SDavid Nugent 						break;
806ea4e54b9SDavid Nugent 					case 3: /* login message */
807ea4e54b9SDavid Nugent 						hrp->loginmsg = cp;
808ea4e54b9SDavid Nugent 						break;
809ea4e54b9SDavid Nugent 					}
810ea4e54b9SDavid Nugent 				}
811ea4e54b9SDavid Nugent 				++i;
812ea4e54b9SDavid Nugent 			}
8134dd8b5abSYoshinobu Inoue 			/* XXX: re-initialization for getaddrinfo() loop */
8144dd8b5abSYoshinobu Inoue 			cp = strtok(line, " \t");
8154dd8b5abSYoshinobu Inoue 		      }
816ea4e54b9SDavid Nugent 		}
817ea4e54b9SDavid Nugent 		(void) fclose(fp);
818ea4e54b9SDavid Nugent 	}
819ea4e54b9SDavid Nugent }
820ea4e54b9SDavid Nugent 
821ea4e54b9SDavid Nugent static void
8224dd8b5abSYoshinobu Inoue selecthost(su)
8234dd8b5abSYoshinobu Inoue 	union sockunion *su;
824ea4e54b9SDavid Nugent {
825ea4e54b9SDavid Nugent 	struct ftphost	*hrp;
8264dd8b5abSYoshinobu Inoue 	u_int16_t port;
8274dd8b5abSYoshinobu Inoue #ifdef INET6
8284dd8b5abSYoshinobu Inoue 	struct in6_addr *mapped_in6 = NULL;
8294dd8b5abSYoshinobu Inoue #endif
830f38c6cadSYoshinobu Inoue 	struct addrinfo *hi;
8314dd8b5abSYoshinobu Inoue 
8324dd8b5abSYoshinobu Inoue #ifdef INET6
8334dd8b5abSYoshinobu Inoue 	/*
8344dd8b5abSYoshinobu Inoue 	 * XXX IPv4 mapped IPv6 addr consideraton,
8354dd8b5abSYoshinobu Inoue 	 * specified in rfc2373.
8364dd8b5abSYoshinobu Inoue 	 */
8374dd8b5abSYoshinobu Inoue 	if (su->su_family == AF_INET6 &&
8384dd8b5abSYoshinobu Inoue 	    IN6_IS_ADDR_V4MAPPED(&su->su_sin6.sin6_addr))
8394dd8b5abSYoshinobu Inoue 		mapped_in6 = &su->su_sin6.sin6_addr;
8404dd8b5abSYoshinobu Inoue #endif
841ea4e54b9SDavid Nugent 
842ea4e54b9SDavid Nugent 	hrp = thishost = firsthost;	/* default */
8434dd8b5abSYoshinobu Inoue 	port = su->su_port;
8444dd8b5abSYoshinobu Inoue 	su->su_port = 0;
845ea4e54b9SDavid Nugent 	while (hrp != NULL) {
846b535a9bfSDavid Nugent 	    for (hi = hrp->hostinfo; hi != NULL; hi = hi->ai_next) {
847f38c6cadSYoshinobu Inoue 		if (memcmp(su, hi->ai_addr, hi->ai_addrlen) == 0) {
848ea4e54b9SDavid Nugent 			thishost = hrp;
849ea4e54b9SDavid Nugent 			break;
850ea4e54b9SDavid Nugent 		}
8514dd8b5abSYoshinobu Inoue #ifdef INET6
8524dd8b5abSYoshinobu Inoue 		/* XXX IPv4 mapped IPv6 addr consideraton */
853f38c6cadSYoshinobu Inoue 		if (hi->ai_addr->sa_family == AF_INET && mapped_in6 != NULL &&
8544dd8b5abSYoshinobu Inoue 		    (memcmp(&mapped_in6->s6_addr[12],
855f38c6cadSYoshinobu Inoue 			    &((struct sockaddr_in *)hi->ai_addr)->sin_addr,
8564dd8b5abSYoshinobu Inoue 			    sizeof(struct in_addr)) == 0)) {
8574dd8b5abSYoshinobu Inoue 			thishost = hrp;
8584dd8b5abSYoshinobu Inoue 			break;
8594dd8b5abSYoshinobu Inoue 		}
8604dd8b5abSYoshinobu Inoue #endif
861f38c6cadSYoshinobu Inoue 	    }
862ea4e54b9SDavid Nugent 	    hrp = hrp->next;
863ea4e54b9SDavid Nugent 	}
8644dd8b5abSYoshinobu Inoue 	su->su_port = port;
865ea4e54b9SDavid Nugent 	/* setup static variables as appropriate */
866ea4e54b9SDavid Nugent 	hostname = thishost->hostname;
867ea4e54b9SDavid Nugent 	ftpuser = thishost->anonuser;
868ea4e54b9SDavid Nugent }
869ea4e54b9SDavid Nugent #endif
870ea4e54b9SDavid Nugent 
871ea022d16SRodney W. Grimes /*
872ea022d16SRodney W. Grimes  * Helper function for sgetpwnam().
873ea022d16SRodney W. Grimes  */
874ea022d16SRodney W. Grimes static char *
875ea022d16SRodney W. Grimes sgetsave(s)
876ea022d16SRodney W. Grimes 	char *s;
877ea022d16SRodney W. Grimes {
878ea022d16SRodney W. Grimes 	char *new = malloc((unsigned) strlen(s) + 1);
879ea022d16SRodney W. Grimes 
880ea022d16SRodney W. Grimes 	if (new == NULL) {
881ea022d16SRodney W. Grimes 		perror_reply(421, "Local resource failure: malloc");
882ea022d16SRodney W. Grimes 		dologout(1);
883ea022d16SRodney W. Grimes 		/* NOTREACHED */
884ea022d16SRodney W. Grimes 	}
885ea022d16SRodney W. Grimes 	(void) strcpy(new, s);
886ea022d16SRodney W. Grimes 	return (new);
887ea022d16SRodney W. Grimes }
888ea022d16SRodney W. Grimes 
889ea022d16SRodney W. Grimes /*
890ea022d16SRodney W. Grimes  * Save the result of a getpwnam.  Used for USER command, since
891ea022d16SRodney W. Grimes  * the data returned must not be clobbered by any other command
892ea022d16SRodney W. Grimes  * (e.g., globbing).
893ea022d16SRodney W. Grimes  */
894ea022d16SRodney W. Grimes static struct passwd *
895ea022d16SRodney W. Grimes sgetpwnam(name)
896ea022d16SRodney W. Grimes 	char *name;
897ea022d16SRodney W. Grimes {
898ea022d16SRodney W. Grimes 	static struct passwd save;
899ea022d16SRodney W. Grimes 	struct passwd *p;
900ea022d16SRodney W. Grimes 
901ea022d16SRodney W. Grimes 	if ((p = getpwnam(name)) == NULL)
902ea022d16SRodney W. Grimes 		return (p);
903ea022d16SRodney W. Grimes 	if (save.pw_name) {
904ea022d16SRodney W. Grimes 		free(save.pw_name);
905ea022d16SRodney W. Grimes 		free(save.pw_passwd);
906ea022d16SRodney W. Grimes 		free(save.pw_gecos);
907ea022d16SRodney W. Grimes 		free(save.pw_dir);
908ea022d16SRodney W. Grimes 		free(save.pw_shell);
909ea022d16SRodney W. Grimes 	}
910ea022d16SRodney W. Grimes 	save = *p;
911ea022d16SRodney W. Grimes 	save.pw_name = sgetsave(p->pw_name);
912ea022d16SRodney W. Grimes 	save.pw_passwd = sgetsave(p->pw_passwd);
913ea022d16SRodney W. Grimes 	save.pw_gecos = sgetsave(p->pw_gecos);
914ea022d16SRodney W. Grimes 	save.pw_dir = sgetsave(p->pw_dir);
915ea022d16SRodney W. Grimes 	save.pw_shell = sgetsave(p->pw_shell);
916ea022d16SRodney W. Grimes 	return (&save);
917ea022d16SRodney W. Grimes }
918ea022d16SRodney W. Grimes 
919ea022d16SRodney W. Grimes static int login_attempts;	/* number of failed login attempts */
920ea022d16SRodney W. Grimes static int askpasswd;		/* had user command, ask for passwd */
92190906a46SSheldon Hearn static char curname[MAXLOGNAME];	/* current USER name */
922ea022d16SRodney W. Grimes 
923ea022d16SRodney W. Grimes /*
924ea022d16SRodney W. Grimes  * USER command.
925ea022d16SRodney W. Grimes  * Sets global passwd pointer pw if named account exists and is acceptable;
926ea022d16SRodney W. Grimes  * sets askpasswd if a PASS command is expected.  If logged in previously,
927ea022d16SRodney W. Grimes  * need to reset state.  If name is "ftp" or "anonymous", the name is not in
928ea022d16SRodney W. Grimes  * _PATH_FTPUSERS, and ftp account exists, set guest and pw, then just return.
929ea022d16SRodney W. Grimes  * If account doesn't exist, ask for passwd anyway.  Otherwise, check user
930ea022d16SRodney W. Grimes  * requesting login privileges.  Disallow anyone who does not have a standard
931ea022d16SRodney W. Grimes  * shell as returned by getusershell().  Disallow anyone mentioned in the file
932ea022d16SRodney W. Grimes  * _PATH_FTPUSERS to allow people such as root and uucp to be avoided.
933ea022d16SRodney W. Grimes  */
934ea022d16SRodney W. Grimes void
935ea022d16SRodney W. Grimes user(name)
936ea022d16SRodney W. Grimes 	char *name;
937ea022d16SRodney W. Grimes {
938ea022d16SRodney W. Grimes 	char *cp, *shell;
939ea022d16SRodney W. Grimes 
940ea022d16SRodney W. Grimes 	if (logged_in) {
941ea022d16SRodney W. Grimes 		if (guest) {
942ea022d16SRodney W. Grimes 			reply(530, "Can't change user from guest login.");
943ea022d16SRodney W. Grimes 			return;
944a5a4544eSPaul Traina 		} else if (dochroot) {
945a5a4544eSPaul Traina 			reply(530, "Can't change user from chroot user.");
946a5a4544eSPaul Traina 			return;
947ea022d16SRodney W. Grimes 		}
948ea022d16SRodney W. Grimes 		end_login();
949ea022d16SRodney W. Grimes 	}
950ea022d16SRodney W. Grimes 
951ea022d16SRodney W. Grimes 	guest = 0;
952ea022d16SRodney W. Grimes 	if (strcmp(name, "ftp") == 0 || strcmp(name, "anonymous") == 0) {
9537edcb936SSteve Price 		if (checkuser(_PATH_FTPUSERS, "ftp", 0) ||
9547edcb936SSteve Price 		    checkuser(_PATH_FTPUSERS, "anonymous", 0))
955ea022d16SRodney W. Grimes 			reply(530, "User %s access denied.", name);
956ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
957ea4e54b9SDavid Nugent 		else if ((pw = sgetpwnam(thishost->anonuser)) != NULL) {
958ea4e54b9SDavid Nugent #else
959ea022d16SRodney W. Grimes 		else if ((pw = sgetpwnam("ftp")) != NULL) {
960ea4e54b9SDavid Nugent #endif
961ea022d16SRodney W. Grimes 			guest = 1;
962ea022d16SRodney W. Grimes 			askpasswd = 1;
963ea022d16SRodney W. Grimes 			reply(331,
9642c60c54cSPaul Traina 			"Guest login ok, send your email address as password.");
965ea022d16SRodney W. Grimes 		} else
966ea022d16SRodney W. Grimes 			reply(530, "User %s unknown.", name);
967ea022d16SRodney W. Grimes 		if (!askpasswd && logging)
968ea022d16SRodney W. Grimes 			syslog(LOG_NOTICE,
969ea022d16SRodney W. Grimes 			    "ANONYMOUS FTP LOGIN REFUSED FROM %s", remotehost);
970ea022d16SRodney W. Grimes 		return;
971ea022d16SRodney W. Grimes 	}
9725a392aecSTorsten Blum 	if (anon_only != 0) {
9735a392aecSTorsten Blum 		reply(530, "Sorry, only anonymous ftp allowed.");
9745a392aecSTorsten Blum 		return;
9755a392aecSTorsten Blum 	}
9765a392aecSTorsten Blum 
9779aca17cbSMark Murray 	if ((pw = sgetpwnam(name))) {
978ea022d16SRodney W. Grimes 		if ((shell = pw->pw_shell) == NULL || *shell == 0)
979ea022d16SRodney W. Grimes 			shell = _PATH_BSHELL;
980ea022d16SRodney W. Grimes 		while ((cp = getusershell()) != NULL)
981ea022d16SRodney W. Grimes 			if (strcmp(cp, shell) == 0)
982ea022d16SRodney W. Grimes 				break;
983ea022d16SRodney W. Grimes 		endusershell();
984ea022d16SRodney W. Grimes 
9857edcb936SSteve Price 		if (cp == NULL || checkuser(_PATH_FTPUSERS, name, 1)) {
986ea022d16SRodney W. Grimes 			reply(530, "User %s access denied.", name);
987ea022d16SRodney W. Grimes 			if (logging)
988ea022d16SRodney W. Grimes 				syslog(LOG_NOTICE,
989ea022d16SRodney W. Grimes 				    "FTP LOGIN REFUSED FROM %s, %s",
990ea022d16SRodney W. Grimes 				    remotehost, name);
991ea022d16SRodney W. Grimes 			pw = (struct passwd *) NULL;
992ea022d16SRodney W. Grimes 			return;
993ea022d16SRodney W. Grimes 		}
994ea022d16SRodney W. Grimes 	}
995ea022d16SRodney W. Grimes 	if (logging)
996ea022d16SRodney W. Grimes 		strncpy(curname, name, sizeof(curname)-1);
99747499ecdSAndrey A. Chernov 
99847499ecdSAndrey A. Chernov 	pwok = 0;
999fa1746c9SMark Murray #ifdef USE_PAM
1000fa1746c9SMark Murray 	/* XXX Kluge! The conversation mechanism needs to be fixed. */
1001726040deSGuido van Rooij #endif
100247499ecdSAndrey A. Chernov 	if (opiechallenge(&opiedata, name, opieprompt) == 0) {
100347499ecdSAndrey A. Chernov 		pwok = (pw != NULL) &&
100447499ecdSAndrey A. Chernov 		       opieaccessfile(remotehost) &&
100547499ecdSAndrey A. Chernov 		       opiealways(pw->pw_dir);
100647499ecdSAndrey A. Chernov 		reply(331, "Response to %s %s for %s.",
100747499ecdSAndrey A. Chernov 		      opieprompt, pwok ? "requested" : "required", name);
100847499ecdSAndrey A. Chernov 	} else {
100947499ecdSAndrey A. Chernov 		pwok = 1;
101047499ecdSAndrey A. Chernov 		reply(331, "Password required for %s.", name);
101147499ecdSAndrey A. Chernov 	}
1012ea022d16SRodney W. Grimes 	askpasswd = 1;
1013ea022d16SRodney W. Grimes 	/*
1014ea022d16SRodney W. Grimes 	 * Delay before reading passwd after first failed
1015ea022d16SRodney W. Grimes 	 * attempt to slow down passwd-guessing programs.
1016ea022d16SRodney W. Grimes 	 */
1017ea022d16SRodney W. Grimes 	if (login_attempts)
1018ea022d16SRodney W. Grimes 		sleep((unsigned) login_attempts);
1019ea022d16SRodney W. Grimes }
1020ea022d16SRodney W. Grimes 
1021ea022d16SRodney W. Grimes /*
1022a5a4544eSPaul Traina  * Check if a user is in the file "fname"
1023ea022d16SRodney W. Grimes  */
1024ea022d16SRodney W. Grimes static int
10257edcb936SSteve Price checkuser(fname, name, pwset)
1026a5a4544eSPaul Traina 	char *fname;
1027ea022d16SRodney W. Grimes 	char *name;
10287edcb936SSteve Price 	int pwset;
1029ea022d16SRodney W. Grimes {
1030ea022d16SRodney W. Grimes 	FILE *fd;
1031ea022d16SRodney W. Grimes 	int found = 0;
1032ea022d16SRodney W. Grimes 	char *p, line[BUFSIZ];
1033ea022d16SRodney W. Grimes 
1034a5a4544eSPaul Traina 	if ((fd = fopen(fname, "r")) != NULL) {
103531fea7b8SDavid Nugent 		while (!found && fgets(line, sizeof(line), fd) != NULL)
1036ea022d16SRodney W. Grimes 			if ((p = strchr(line, '\n')) != NULL) {
1037ea022d16SRodney W. Grimes 				*p = '\0';
1038ea022d16SRodney W. Grimes 				if (line[0] == '#')
1039ea022d16SRodney W. Grimes 					continue;
104031fea7b8SDavid Nugent 				/*
104131fea7b8SDavid Nugent 				 * if first chr is '@', check group membership
104231fea7b8SDavid Nugent 				 */
104331fea7b8SDavid Nugent 				if (line[0] == '@') {
104431fea7b8SDavid Nugent 					int i = 0;
104531fea7b8SDavid Nugent 					struct group *grp;
104631fea7b8SDavid Nugent 
104731fea7b8SDavid Nugent 					if ((grp = getgrnam(line+1)) == NULL)
104831fea7b8SDavid Nugent 						continue;
10497edcb936SSteve Price 					/*
10507edcb936SSteve Price 					 * Check user's default group
10517edcb936SSteve Price 					 */
10527edcb936SSteve Price 					if (pwset && grp->gr_gid == pw->pw_gid)
10537edcb936SSteve Price 						found = 1;
10547edcb936SSteve Price 					/*
10557edcb936SSteve Price 					 * Check supplementary groups
10567edcb936SSteve Price 					 */
105731fea7b8SDavid Nugent 					while (!found && grp->gr_mem[i])
105831fea7b8SDavid Nugent 						found = strcmp(name,
105931fea7b8SDavid Nugent 							grp->gr_mem[i++])
106031fea7b8SDavid Nugent 							== 0;
1061ea022d16SRodney W. Grimes 				}
106231fea7b8SDavid Nugent 				/*
106331fea7b8SDavid Nugent 				 * Otherwise, just check for username match
106431fea7b8SDavid Nugent 				 */
106531fea7b8SDavid Nugent 				else
106631fea7b8SDavid Nugent 					found = strcmp(line, name) == 0;
1067ea022d16SRodney W. Grimes 			}
1068ea022d16SRodney W. Grimes 		(void) fclose(fd);
1069ea022d16SRodney W. Grimes 	}
1070ea022d16SRodney W. Grimes 	return (found);
1071ea022d16SRodney W. Grimes }
1072ea022d16SRodney W. Grimes 
1073ea022d16SRodney W. Grimes /*
1074ea022d16SRodney W. Grimes  * Terminate login as previous user, if any, resetting state;
1075ea022d16SRodney W. Grimes  * used when USER command is given or login fails.
1076ea022d16SRodney W. Grimes  */
1077ea022d16SRodney W. Grimes static void
1078ea022d16SRodney W. Grimes end_login()
1079ea022d16SRodney W. Grimes {
10805bc9d93dSMark Murray #ifdef USE_PAM
10815bc9d93dSMark Murray 	int e;
10825bc9d93dSMark Murray #endif
1083ea022d16SRodney W. Grimes 
1084ea022d16SRodney W. Grimes 	(void) seteuid((uid_t)0);
1085ea022d16SRodney W. Grimes 	if (logged_in)
108646948173SHajimu UMEMOTO 		ftpd_logwtmp(ttyline, "", NULL);
1087ea022d16SRodney W. Grimes 	pw = NULL;
1088b071c689SDavid Nugent #ifdef	LOGIN_CAP
1089b071c689SDavid Nugent 	setusercontext(NULL, getpwuid(0), (uid_t)0,
1090b071c689SDavid Nugent 		       LOGIN_SETPRIORITY|LOGIN_SETRESOURCES|LOGIN_SETUMASK);
1091b071c689SDavid Nugent #endif
10925bc9d93dSMark Murray #ifdef USE_PAM
10935bc9d93dSMark Murray 	if ((e = pam_setcred(pamh, PAM_DELETE_CRED)) != PAM_SUCCESS)
10945bc9d93dSMark Murray 		syslog(LOG_ERR, "pam_setcred: %s", pam_strerror(pamh, e));
10955bc9d93dSMark Murray 	if ((e = pam_close_session(pamh,0)) != PAM_SUCCESS)
10965bc9d93dSMark Murray 		syslog(LOG_ERR, "pam_close_session: %s", pam_strerror(pamh, e));
10975bc9d93dSMark Murray 	if ((e = pam_end(pamh, e)) != PAM_SUCCESS)
10985bc9d93dSMark Murray 		syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e));
10995bc9d93dSMark Murray 	pamh = NULL;
11005bc9d93dSMark Murray #endif
1101ea022d16SRodney W. Grimes 	logged_in = 0;
1102ea022d16SRodney W. Grimes 	guest = 0;
1103a5a4544eSPaul Traina 	dochroot = 0;
1104ea022d16SRodney W. Grimes }
1105ea022d16SRodney W. Grimes 
11065bc9d93dSMark Murray #ifdef USE_PAM
11076c9134c0SMark Murray 
11086c9134c0SMark Murray /*
11096c9134c0SMark Murray  * the following code is stolen from imap-uw PAM authentication module and
11106c9134c0SMark Murray  * login.c
11116c9134c0SMark Murray  */
11126c9134c0SMark Murray #define COPY_STRING(s) (s ? strdup(s) : NULL)
11136c9134c0SMark Murray 
11146c9134c0SMark Murray struct cred_t {
11156c9134c0SMark Murray 	const char *uname;		/* user name */
11166c9134c0SMark Murray 	const char *pass;		/* password */
11176c9134c0SMark Murray };
11186c9134c0SMark Murray typedef struct cred_t cred_t;
11196c9134c0SMark Murray 
11206c9134c0SMark Murray static int
11216c9134c0SMark Murray auth_conv(int num_msg, const struct pam_message **msg,
11226c9134c0SMark Murray 	  struct pam_response **resp, void *appdata)
11236c9134c0SMark Murray {
11246c9134c0SMark Murray 	int i;
11256c9134c0SMark Murray 	cred_t *cred = (cred_t *) appdata;
11266c9134c0SMark Murray 	struct pam_response *reply =
11276c9134c0SMark Murray 			malloc(sizeof(struct pam_response) * num_msg);
11286c9134c0SMark Murray 
11296c9134c0SMark Murray 	for (i = 0; i < num_msg; i++) {
11306c9134c0SMark Murray 		switch (msg[i]->msg_style) {
11316c9134c0SMark Murray 		case PAM_PROMPT_ECHO_ON:	/* assume want user name */
11326c9134c0SMark Murray 			reply[i].resp_retcode = PAM_SUCCESS;
11336c9134c0SMark Murray 			reply[i].resp = COPY_STRING(cred->uname);
11346c9134c0SMark Murray 			/* PAM frees resp. */
11356c9134c0SMark Murray 			break;
11366c9134c0SMark Murray 		case PAM_PROMPT_ECHO_OFF:	/* assume want password */
11376c9134c0SMark Murray 			reply[i].resp_retcode = PAM_SUCCESS;
11386c9134c0SMark Murray 			reply[i].resp = COPY_STRING(cred->pass);
11396c9134c0SMark Murray 			/* PAM frees resp. */
11406c9134c0SMark Murray 			break;
11416c9134c0SMark Murray 		case PAM_TEXT_INFO:
11426c9134c0SMark Murray 		case PAM_ERROR_MSG:
11436c9134c0SMark Murray 			reply[i].resp_retcode = PAM_SUCCESS;
11446c9134c0SMark Murray 			reply[i].resp = NULL;
11456c9134c0SMark Murray 			break;
11466c9134c0SMark Murray 		default:			/* unknown message style */
11476c9134c0SMark Murray 			free(reply);
11486c9134c0SMark Murray 			return PAM_CONV_ERR;
11496c9134c0SMark Murray 		}
11506c9134c0SMark Murray 	}
11516c9134c0SMark Murray 
11526c9134c0SMark Murray 	*resp = reply;
11536c9134c0SMark Murray 	return PAM_SUCCESS;
11546c9134c0SMark Murray }
11556c9134c0SMark Murray 
11566c9134c0SMark Murray /*
11576c9134c0SMark Murray  * Attempt to authenticate the user using PAM.  Returns 0 if the user is
11586c9134c0SMark Murray  * authenticated, or 1 if not authenticated.  If some sort of PAM system
11596c9134c0SMark Murray  * error occurs (e.g., the "/etc/pam.conf" file is missing) then this
11606c9134c0SMark Murray  * function returns -1.  This can be used as an indication that we should
11616c9134c0SMark Murray  * fall back to a different authentication mechanism.
11626c9134c0SMark Murray  */
11636c9134c0SMark Murray static int
11646c9134c0SMark Murray auth_pam(struct passwd **ppw, const char *pass)
11656c9134c0SMark Murray {
11666c9134c0SMark Murray 	pam_handle_t *pamh = NULL;
11676c9134c0SMark Murray 	const char *tmpl_user;
11686c9134c0SMark Murray 	const void *item;
11696c9134c0SMark Murray 	int rval;
11706c9134c0SMark Murray 	int e;
11716c9134c0SMark Murray 	cred_t auth_cred = { (*ppw)->pw_name, pass };
11726c9134c0SMark Murray 	struct pam_conv conv = { &auth_conv, &auth_cred };
11736c9134c0SMark Murray 
11746c9134c0SMark Murray 	e = pam_start("ftpd", (*ppw)->pw_name, &conv, &pamh);
11756c9134c0SMark Murray 	if (e != PAM_SUCCESS) {
11766c9134c0SMark Murray 		syslog(LOG_ERR, "pam_start: %s", pam_strerror(pamh, e));
11776c9134c0SMark Murray 		return -1;
11786c9134c0SMark Murray 	}
11796c9134c0SMark Murray 
1180ea413ab7SGuido van Rooij 	e = pam_set_item(pamh, PAM_RHOST, remotehost);
1181ea413ab7SGuido van Rooij 	if (e != PAM_SUCCESS) {
1182ea413ab7SGuido van Rooij 		syslog(LOG_ERR, "pam_set_item(PAM_RHOST): %s",
1183ea413ab7SGuido van Rooij 			pam_strerror(pamh, e));
1184ea413ab7SGuido van Rooij 		return -1;
1185ea413ab7SGuido van Rooij 	}
1186ea413ab7SGuido van Rooij 
11876c9134c0SMark Murray 	e = pam_authenticate(pamh, 0);
11886c9134c0SMark Murray 	switch (e) {
11896c9134c0SMark Murray 	case PAM_SUCCESS:
11906c9134c0SMark Murray 		/*
11916c9134c0SMark Murray 		 * With PAM we support the concept of a "template"
11926c9134c0SMark Murray 		 * user.  The user enters a login name which is
11936c9134c0SMark Murray 		 * authenticated by PAM, usually via a remote service
11946c9134c0SMark Murray 		 * such as RADIUS or TACACS+.  If authentication
11956c9134c0SMark Murray 		 * succeeds, a different but related "template" name
11966c9134c0SMark Murray 		 * is used for setting the credentials, shell, and
11976c9134c0SMark Murray 		 * home directory.  The name the user enters need only
11986c9134c0SMark Murray 		 * exist on the remote authentication server, but the
11996c9134c0SMark Murray 		 * template name must be present in the local password
12006c9134c0SMark Murray 		 * database.
12016c9134c0SMark Murray 		 *
12026c9134c0SMark Murray 		 * This is supported by two various mechanisms in the
12036c9134c0SMark Murray 		 * individual modules.  However, from the application's
12046c9134c0SMark Murray 		 * point of view, the template user is always passed
12056c9134c0SMark Murray 		 * back as a changed value of the PAM_USER item.
12066c9134c0SMark Murray 		 */
12076c9134c0SMark Murray 		if ((e = pam_get_item(pamh, PAM_USER, &item)) ==
12086c9134c0SMark Murray 		    PAM_SUCCESS) {
12096c9134c0SMark Murray 			tmpl_user = (const char *) item;
12106c9134c0SMark Murray 			if (strcmp((*ppw)->pw_name, tmpl_user) != 0)
12116c9134c0SMark Murray 				*ppw = getpwnam(tmpl_user);
12126c9134c0SMark Murray 		} else
12136c9134c0SMark Murray 			syslog(LOG_ERR, "Couldn't get PAM_USER: %s",
12146c9134c0SMark Murray 			    pam_strerror(pamh, e));
12156c9134c0SMark Murray 		rval = 0;
12166c9134c0SMark Murray 		break;
12176c9134c0SMark Murray 
12186c9134c0SMark Murray 	case PAM_AUTH_ERR:
12196c9134c0SMark Murray 	case PAM_USER_UNKNOWN:
12206c9134c0SMark Murray 	case PAM_MAXTRIES:
12216c9134c0SMark Murray 		rval = 1;
12226c9134c0SMark Murray 		break;
12236c9134c0SMark Murray 
12246c9134c0SMark Murray 	default:
12255bc9d93dSMark Murray 		syslog(LOG_ERR, "pam_authenticate: %s", pam_strerror(pamh, e));
12266c9134c0SMark Murray 		rval = -1;
12276c9134c0SMark Murray 		break;
12286c9134c0SMark Murray 	}
12296c9134c0SMark Murray 
12305bc9d93dSMark Murray 	if (rval == 0) {
12315bc9d93dSMark Murray 		e = pam_acct_mgmt(pamh, 0);
12325bc9d93dSMark Murray 		if (e == PAM_NEW_AUTHTOK_REQD) {
12335bc9d93dSMark Murray 			e = pam_chauthtok(pamh, PAM_CHANGE_EXPIRED_AUTHTOK);
12345bc9d93dSMark Murray 			if (e != PAM_SUCCESS) {
12355bc9d93dSMark Murray 				syslog(LOG_ERR, "pam_chauthtok: %s", pam_strerror(pamh, e));
12365bc9d93dSMark Murray 				rval = 1;
12375bc9d93dSMark Murray 			}
12385bc9d93dSMark Murray 		} else if (e != PAM_SUCCESS) {
12395bc9d93dSMark Murray 			rval = 1;
12405bc9d93dSMark Murray 		}
12415bc9d93dSMark Murray 	}
12425bc9d93dSMark Murray 
12435bc9d93dSMark Murray 	if (rval != 0) {
12446c9134c0SMark Murray 		if ((e = pam_end(pamh, e)) != PAM_SUCCESS) {
12456c9134c0SMark Murray 			syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e));
12465bc9d93dSMark Murray 		}
12475bc9d93dSMark Murray 		pamh = NULL;
12486c9134c0SMark Murray 	}
12496c9134c0SMark Murray 	return rval;
12506c9134c0SMark Murray }
12516c9134c0SMark Murray 
12525bc9d93dSMark Murray #endif /* USE_PAM */
12536c9134c0SMark Murray 
1254ea022d16SRodney W. Grimes void
1255ea022d16SRodney W. Grimes pass(passwd)
1256ea022d16SRodney W. Grimes 	char *passwd;
1257ea022d16SRodney W. Grimes {
1258a5a4544eSPaul Traina 	int rval;
1259ea022d16SRodney W. Grimes 	FILE *fd;
1260b071c689SDavid Nugent #ifdef	LOGIN_CAP
1261b071c689SDavid Nugent 	login_cap_t *lc = NULL;
1262b071c689SDavid Nugent #endif
12635bc9d93dSMark Murray #ifdef USE_PAM
12645bc9d93dSMark Murray 	int e;
12655bc9d93dSMark Murray #endif
126647499ecdSAndrey A. Chernov 	char *xpasswd;
1267ea022d16SRodney W. Grimes 
1268ea022d16SRodney W. Grimes 	if (logged_in || askpasswd == 0) {
1269ea022d16SRodney W. Grimes 		reply(503, "Login with USER first.");
1270ea022d16SRodney W. Grimes 		return;
1271ea022d16SRodney W. Grimes 	}
1272ea022d16SRodney W. Grimes 	askpasswd = 0;
1273ea022d16SRodney W. Grimes 	if (!guest) {		/* "ftp" is only account allowed no password */
1274a5a4544eSPaul Traina 		if (pw == NULL) {
1275a5a4544eSPaul Traina 			rval = 1;	/* failure below */
1276a5a4544eSPaul Traina 			goto skip;
1277a5a4544eSPaul Traina 		}
12785bc9d93dSMark Murray #ifdef USE_PAM
12796c9134c0SMark Murray 		rval = auth_pam(&pw, passwd);
1280f650a124SAndrey A. Chernov 		if (rval >= 0) {
1281f650a124SAndrey A. Chernov 			opieunlock();
1282a5a4544eSPaul Traina 			goto skip;
1283f650a124SAndrey A. Chernov 		}
1284f650a124SAndrey A. Chernov #endif
128547499ecdSAndrey A. Chernov 		if (opieverify(&opiedata, passwd) == 0)
128647499ecdSAndrey A. Chernov 			xpasswd = pw->pw_passwd;
1287f650a124SAndrey A. Chernov 		else if (pwok) {
128847499ecdSAndrey A. Chernov 			xpasswd = crypt(passwd, pw->pw_passwd);
1289f650a124SAndrey A. Chernov 			if (passwd[0] == '\0' && pw->pw_passwd[0] != '\0')
1290f650a124SAndrey A. Chernov 				xpasswd = ":";
1291f650a124SAndrey A. Chernov 		} else {
129247499ecdSAndrey A. Chernov 			rval = 1;
129347499ecdSAndrey A. Chernov 			goto skip;
129447499ecdSAndrey A. Chernov 		}
129547499ecdSAndrey A. Chernov 		rval = strcmp(pw->pw_passwd, xpasswd);
1296f650a124SAndrey A. Chernov 		if (pw->pw_expire && time(NULL) >= pw->pw_expire)
1297a5a4544eSPaul Traina 			rval = 1;	/* failure */
1298a5a4544eSPaul Traina skip:
1299a5a4544eSPaul Traina 		/*
1300a5a4544eSPaul Traina 		 * If rval == 1, the user failed the authentication check
13016c9134c0SMark Murray 		 * above.  If rval == 0, either PAM or local authentication
1302a5a4544eSPaul Traina 		 * succeeded.
1303a5a4544eSPaul Traina 		 */
1304a5a4544eSPaul Traina 		if (rval) {
1305ea022d16SRodney W. Grimes 			reply(530, "Login incorrect.");
1306ea022d16SRodney W. Grimes 			if (logging)
1307ea022d16SRodney W. Grimes 				syslog(LOG_NOTICE,
1308ea022d16SRodney W. Grimes 				    "FTP LOGIN FAILED FROM %s, %s",
1309ea022d16SRodney W. Grimes 				    remotehost, curname);
1310ea022d16SRodney W. Grimes 			pw = NULL;
1311ea022d16SRodney W. Grimes 			if (login_attempts++ >= 5) {
1312ea022d16SRodney W. Grimes 				syslog(LOG_NOTICE,
1313ea022d16SRodney W. Grimes 				    "repeated login failures from %s",
1314ea022d16SRodney W. Grimes 				    remotehost);
1315ea022d16SRodney W. Grimes 				exit(0);
1316ea022d16SRodney W. Grimes 			}
1317ea022d16SRodney W. Grimes 			return;
1318ea022d16SRodney W. Grimes 		}
1319ea022d16SRodney W. Grimes 	}
1320ea022d16SRodney W. Grimes 	login_attempts = 0;		/* this time successful */
1321ea022d16SRodney W. Grimes 	if (setegid((gid_t)pw->pw_gid) < 0) {
1322ea022d16SRodney W. Grimes 		reply(550, "Can't set gid.");
1323ea022d16SRodney W. Grimes 		return;
1324ea022d16SRodney W. Grimes 	}
1325b071c689SDavid Nugent 	/* May be overridden by login.conf */
1326b071c689SDavid Nugent 	(void) umask(defumask);
1327b071c689SDavid Nugent #ifdef	LOGIN_CAP
13285d0bfe39SDavid Nugent 	if ((lc = login_getpwclass(pw)) != NULL) {
1329b071c689SDavid Nugent 		char	remote_ip[MAXHOSTNAMELEN];
1330b071c689SDavid Nugent 
13314dd8b5abSYoshinobu Inoue 		getnameinfo((struct sockaddr *)&his_addr, his_addr.su_len,
13324dd8b5abSYoshinobu Inoue 			remote_ip, sizeof(remote_ip) - 1, NULL, 0,
13334dd8b5abSYoshinobu Inoue 			NI_NUMERICHOST|NI_WITHSCOPEID);
1334b071c689SDavid Nugent 		remote_ip[sizeof(remote_ip) - 1] = 0;
1335b071c689SDavid Nugent 		if (!auth_hostok(lc, remotehost, remote_ip)) {
1336b071c689SDavid Nugent 			syslog(LOG_INFO|LOG_AUTH,
1337b071c689SDavid Nugent 			    "FTP LOGIN FAILED (HOST) as %s: permission denied.",
1338b071c689SDavid Nugent 			    pw->pw_name);
1339b071c689SDavid Nugent 			reply(530, "Permission denied.\n");
1340b071c689SDavid Nugent 			pw = NULL;
1341b071c689SDavid Nugent 			return;
1342b071c689SDavid Nugent 		}
1343b071c689SDavid Nugent 		if (!auth_timeok(lc, time(NULL))) {
1344b071c689SDavid Nugent 			reply(530, "Login not available right now.\n");
1345b071c689SDavid Nugent 			pw = NULL;
1346b071c689SDavid Nugent 			return;
1347b071c689SDavid Nugent 		}
1348b071c689SDavid Nugent 	}
1349b071c689SDavid Nugent 	setusercontext(lc, pw, (uid_t)0,
1350e6fa0d43SDag-Erling Smørgrav 		LOGIN_SETLOGIN|LOGIN_SETGROUP|LOGIN_SETPRIORITY|
1351e6fa0d43SDag-Erling Smørgrav 		LOGIN_SETRESOURCES|LOGIN_SETUMASK);
1352b071c689SDavid Nugent #else
1353e6fa0d43SDag-Erling Smørgrav 	setlogin(pw->pw_name);
1354ea022d16SRodney W. Grimes 	(void) initgroups(pw->pw_name, pw->pw_gid);
1355b071c689SDavid Nugent #endif
1356ea022d16SRodney W. Grimes 
13575bc9d93dSMark Murray #ifdef USE_PAM
13585bc9d93dSMark Murray 	if (pamh) {
13595bc9d93dSMark Murray 		if ((e = pam_open_session(pamh, 0)) != PAM_SUCCESS) {
13605bc9d93dSMark Murray 			syslog(LOG_ERR, "pam_open_session: %s", pam_strerror(pamh, e));
13615bc9d93dSMark Murray 		} else if ((e = pam_setcred(pamh, PAM_ESTABLISH_CRED)) != PAM_SUCCESS) {
13625bc9d93dSMark Murray 			syslog(LOG_ERR, "pam_setcred: %s", pam_strerror(pamh, e));
13635bc9d93dSMark Murray 		}
13645bc9d93dSMark Murray 	}
13655bc9d93dSMark Murray #endif
13665bc9d93dSMark Murray 
1367ea022d16SRodney W. Grimes 	/* open wtmp before chroot */
136846948173SHajimu UMEMOTO 	ftpd_logwtmp(ttyline, pw->pw_name, (struct sockaddr *)&his_addr);
1369ea022d16SRodney W. Grimes 	logged_in = 1;
1370ea022d16SRodney W. Grimes 
1371a5a4544eSPaul Traina 	if (guest && stats && statfd < 0)
1372ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
1373ea4e54b9SDavid Nugent 		if ((statfd = open(thishost->statfile, O_WRONLY|O_APPEND)) < 0)
1374ea4e54b9SDavid Nugent #else
13753eb568f2SGuido van Rooij 		if ((statfd = open(_PATH_FTPDSTATFILE, O_WRONLY|O_APPEND)) < 0)
1376ea4e54b9SDavid Nugent #endif
13773eb568f2SGuido van Rooij 			stats = 0;
13783eb568f2SGuido van Rooij 
1379b071c689SDavid Nugent 	dochroot =
1380b071c689SDavid Nugent #ifdef	LOGIN_CAP	/* Allow login.conf configuration as well */
1381b071c689SDavid Nugent 		login_getcapbool(lc, "ftp-chroot", 0) ||
1382b071c689SDavid Nugent #endif
13837edcb936SSteve Price 		checkuser(_PATH_FTPCHROOT, pw->pw_name, 1);
1384ea022d16SRodney W. Grimes 	if (guest) {
1385ea022d16SRodney W. Grimes 		/*
1386ea022d16SRodney W. Grimes 		 * We MUST do a chdir() after the chroot. Otherwise
1387ea022d16SRodney W. Grimes 		 * the old current directory will be accessible as "."
1388ea022d16SRodney W. Grimes 		 * outside the new root!
1389ea022d16SRodney W. Grimes 		 */
1390ea022d16SRodney W. Grimes 		if (chroot(pw->pw_dir) < 0 || chdir("/") < 0) {
1391ea022d16SRodney W. Grimes 			reply(550, "Can't set guest privileges.");
1392ea022d16SRodney W. Grimes 			goto bad;
1393ea022d16SRodney W. Grimes 		}
1394a5a4544eSPaul Traina 	} else if (dochroot) {
1395a5a4544eSPaul Traina 		if (chroot(pw->pw_dir) < 0 || chdir("/") < 0) {
1396a5a4544eSPaul Traina 			reply(550, "Can't change root.");
1397a5a4544eSPaul Traina 			goto bad;
1398a5a4544eSPaul Traina 		}
1399ea022d16SRodney W. Grimes 	} else if (chdir(pw->pw_dir) < 0) {
1400ea022d16SRodney W. Grimes 		if (chdir("/") < 0) {
1401ea022d16SRodney W. Grimes 			reply(530, "User %s: can't change directory to %s.",
1402ea022d16SRodney W. Grimes 			    pw->pw_name, pw->pw_dir);
1403ea022d16SRodney W. Grimes 			goto bad;
1404ea022d16SRodney W. Grimes 		} else
1405ea022d16SRodney W. Grimes 			lreply(230, "No directory! Logging in with home=/");
1406ea022d16SRodney W. Grimes 	}
1407ea022d16SRodney W. Grimes 	if (seteuid((uid_t)pw->pw_uid) < 0) {
1408ea022d16SRodney W. Grimes 		reply(550, "Can't set uid.");
1409ea022d16SRodney W. Grimes 		goto bad;
1410ea022d16SRodney W. Grimes 	}
141182c76939SDavid Greenman 
141282c76939SDavid Greenman 	/*
1413ea022d16SRodney W. Grimes 	 * Display a login message, if it exists.
1414ea022d16SRodney W. Grimes 	 * N.B. reply(230,) must follow the message.
1415ea022d16SRodney W. Grimes 	 */
1416ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
1417ea4e54b9SDavid Nugent 	if ((fd = fopen(thishost->loginmsg, "r")) != NULL) {
1418ea4e54b9SDavid Nugent #else
1419ea022d16SRodney W. Grimes 	if ((fd = fopen(_PATH_FTPLOGINMESG, "r")) != NULL) {
1420ea4e54b9SDavid Nugent #endif
1421ea022d16SRodney W. Grimes 		char *cp, line[LINE_MAX];
1422ea022d16SRodney W. Grimes 
1423ea022d16SRodney W. Grimes 		while (fgets(line, sizeof(line), fd) != NULL) {
1424ea022d16SRodney W. Grimes 			if ((cp = strchr(line, '\n')) != NULL)
1425ea022d16SRodney W. Grimes 				*cp = '\0';
1426ea022d16SRodney W. Grimes 			lreply(230, "%s", line);
1427ea022d16SRodney W. Grimes 		}
1428ea022d16SRodney W. Grimes 		(void) fflush(stdout);
1429ea022d16SRodney W. Grimes 		(void) fclose(fd);
1430ea022d16SRodney W. Grimes 	}
1431ea022d16SRodney W. Grimes 	if (guest) {
14323eb568f2SGuido van Rooij 		if (ident != NULL)
14333eb568f2SGuido van Rooij 			free(ident);
143461f891a6SPaul Traina 		ident = strdup(passwd);
143561f891a6SPaul Traina 		if (ident == NULL)
1436618b0bbaSMark Murray 			fatalerror("Ran out of memory.");
143761f891a6SPaul Traina 
1438ea022d16SRodney W. Grimes 		reply(230, "Guest login ok, access restrictions apply.");
1439ea022d16SRodney W. Grimes #ifdef SETPROCTITLE
1440ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
1441ea4e54b9SDavid Nugent 		if (thishost != firsthost)
1442ea4e54b9SDavid Nugent 			snprintf(proctitle, sizeof(proctitle),
1443b3a0a7cdSMike Heffner 				 "%s: anonymous(%s)/%s", remotehost, hostname,
1444b3a0a7cdSMike Heffner 				 passwd);
1445ea4e54b9SDavid Nugent 		else
1446ea4e54b9SDavid Nugent #endif
1447ea022d16SRodney W. Grimes 			snprintf(proctitle, sizeof(proctitle),
1448b3a0a7cdSMike Heffner 				 "%s: anonymous/%s", remotehost, passwd);
1449b63e1fe2SPeter Wemm 		setproctitle("%s", proctitle);
1450ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */
1451ea022d16SRodney W. Grimes 		if (logging)
1452ea022d16SRodney W. Grimes 			syslog(LOG_INFO, "ANONYMOUS FTP LOGIN FROM %s, %s",
1453ea022d16SRodney W. Grimes 			    remotehost, passwd);
1454ea022d16SRodney W. Grimes 	} else {
14553401a71fSDaniel O'Callaghan 		if (dochroot)
145611342ab1SYaroslav Tykhiy 			reply(230, "User %s logged in, "
145711342ab1SYaroslav Tykhiy 				   "access restrictions apply.", pw->pw_name);
14583401a71fSDaniel O'Callaghan 		else
1459ea022d16SRodney W. Grimes 			reply(230, "User %s logged in.", pw->pw_name);
14603401a71fSDaniel O'Callaghan 
1461ea022d16SRodney W. Grimes #ifdef SETPROCTITLE
1462ea022d16SRodney W. Grimes 		snprintf(proctitle, sizeof(proctitle),
14637a29d7daSYaroslav Tykhiy 			 "%s: user/%s", remotehost, pw->pw_name);
1464b63e1fe2SPeter Wemm 		setproctitle("%s", proctitle);
1465ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */
1466ea022d16SRodney W. Grimes 		if (logging)
1467ea022d16SRodney W. Grimes 			syslog(LOG_INFO, "FTP LOGIN FROM %s as %s",
1468ea022d16SRodney W. Grimes 			    remotehost, pw->pw_name);
1469ea022d16SRodney W. Grimes 	}
1470b071c689SDavid Nugent #ifdef	LOGIN_CAP
1471b071c689SDavid Nugent 	login_close(lc);
1472b071c689SDavid Nugent #endif
1473ea022d16SRodney W. Grimes 	return;
1474ea022d16SRodney W. Grimes bad:
1475ea022d16SRodney W. Grimes 	/* Forget all about it... */
1476b071c689SDavid Nugent #ifdef	LOGIN_CAP
1477b071c689SDavid Nugent 	login_close(lc);
1478b071c689SDavid Nugent #endif
1479ea022d16SRodney W. Grimes 	end_login();
1480ea022d16SRodney W. Grimes }
1481ea022d16SRodney W. Grimes 
1482ea022d16SRodney W. Grimes void
1483ea022d16SRodney W. Grimes retrieve(cmd, name)
1484ea022d16SRodney W. Grimes 	char *cmd, *name;
1485ea022d16SRodney W. Grimes {
1486ea022d16SRodney W. Grimes 	FILE *fin, *dout;
1487ea022d16SRodney W. Grimes 	struct stat st;
1488ea022d16SRodney W. Grimes 	int (*closefunc) __P((FILE *));
1489158a00b2SJohn Birrell 	time_t start;
1490ea022d16SRodney W. Grimes 
1491ea022d16SRodney W. Grimes 	if (cmd == 0) {
1492ea022d16SRodney W. Grimes 		fin = fopen(name, "r"), closefunc = fclose;
1493ea022d16SRodney W. Grimes 		st.st_size = 0;
1494ea022d16SRodney W. Grimes 	} else {
1495ea022d16SRodney W. Grimes 		char line[BUFSIZ];
1496ea022d16SRodney W. Grimes 
1497e760ef2cSWarner Losh 		(void) snprintf(line, sizeof(line), cmd, name), name = line;
1498ea022d16SRodney W. Grimes 		fin = ftpd_popen(line, "r"), closefunc = ftpd_pclose;
1499ea022d16SRodney W. Grimes 		st.st_size = -1;
1500ea022d16SRodney W. Grimes 		st.st_blksize = BUFSIZ;
1501ea022d16SRodney W. Grimes 	}
1502ea022d16SRodney W. Grimes 	if (fin == NULL) {
1503ea022d16SRodney W. Grimes 		if (errno != 0) {
1504ea022d16SRodney W. Grimes 			perror_reply(550, name);
1505ea022d16SRodney W. Grimes 			if (cmd == 0) {
1506ea022d16SRodney W. Grimes 				LOGCMD("get", name);
1507ea022d16SRodney W. Grimes 			}
1508ea022d16SRodney W. Grimes 		}
1509ea022d16SRodney W. Grimes 		return;
1510ea022d16SRodney W. Grimes 	}
1511ea022d16SRodney W. Grimes 	byte_count = -1;
1512ea022d16SRodney W. Grimes 	if (cmd == 0 && (fstat(fileno(fin), &st) < 0 || !S_ISREG(st.st_mode))) {
1513ea022d16SRodney W. Grimes 		reply(550, "%s: not a plain file.", name);
1514ea022d16SRodney W. Grimes 		goto done;
1515ea022d16SRodney W. Grimes 	}
1516ea022d16SRodney W. Grimes 	if (restart_point) {
1517ea022d16SRodney W. Grimes 		if (type == TYPE_A) {
1518ea022d16SRodney W. Grimes 			off_t i, n;
1519ea022d16SRodney W. Grimes 			int c;
1520ea022d16SRodney W. Grimes 
1521ea022d16SRodney W. Grimes 			n = restart_point;
1522ea022d16SRodney W. Grimes 			i = 0;
1523ea022d16SRodney W. Grimes 			while (i++ < n) {
1524ea022d16SRodney W. Grimes 				if ((c=getc(fin)) == EOF) {
1525ea022d16SRodney W. Grimes 					perror_reply(550, name);
1526ea022d16SRodney W. Grimes 					goto done;
1527ea022d16SRodney W. Grimes 				}
1528ea022d16SRodney W. Grimes 				if (c == '\n')
1529ea022d16SRodney W. Grimes 					i++;
1530ea022d16SRodney W. Grimes 			}
1531ea022d16SRodney W. Grimes 		} else if (lseek(fileno(fin), restart_point, L_SET) < 0) {
1532ea022d16SRodney W. Grimes 			perror_reply(550, name);
1533ea022d16SRodney W. Grimes 			goto done;
1534ea022d16SRodney W. Grimes 		}
1535ea022d16SRodney W. Grimes 	}
1536ea022d16SRodney W. Grimes 	dout = dataconn(name, st.st_size, "w");
1537ea022d16SRodney W. Grimes 	if (dout == NULL)
1538ea022d16SRodney W. Grimes 		goto done;
15393eb568f2SGuido van Rooij 	time(&start);
15409fc5823aSGarrett Wollman 	send_data(fin, dout, st.st_blksize, st.st_size,
15419fc5823aSGarrett Wollman 		  restart_point == 0 && cmd == 0 && S_ISREG(st.st_mode));
15423eb568f2SGuido van Rooij 	if (cmd == 0 && guest && stats)
15433eb568f2SGuido van Rooij 		logxfer(name, st.st_size, start);
1544ea022d16SRodney W. Grimes 	(void) fclose(dout);
1545ea022d16SRodney W. Grimes 	data = -1;
1546ea022d16SRodney W. Grimes 	pdata = -1;
1547ea022d16SRodney W. Grimes done:
1548ea022d16SRodney W. Grimes 	if (cmd == 0)
1549ea022d16SRodney W. Grimes 		LOGBYTES("get", name, byte_count);
1550ea022d16SRodney W. Grimes 	(*closefunc)(fin);
1551ea022d16SRodney W. Grimes }
1552ea022d16SRodney W. Grimes 
1553ea022d16SRodney W. Grimes void
1554ea022d16SRodney W. Grimes store(name, mode, unique)
1555ea022d16SRodney W. Grimes 	char *name, *mode;
1556ea022d16SRodney W. Grimes 	int unique;
1557ea022d16SRodney W. Grimes {
1558ea022d16SRodney W. Grimes 	FILE *fout, *din;
1559ea022d16SRodney W. Grimes 	struct stat st;
1560ea022d16SRodney W. Grimes 	int (*closefunc) __P((FILE *));
1561ea022d16SRodney W. Grimes 
156261f891a6SPaul Traina 	if ((unique || guest) && stat(name, &st) == 0 &&
1563ea022d16SRodney W. Grimes 	    (name = gunique(name)) == NULL) {
1564ea022d16SRodney W. Grimes 		LOGCMD(*mode == 'w' ? "put" : "append", name);
1565ea022d16SRodney W. Grimes 		return;
1566ea022d16SRodney W. Grimes 	}
1567ea022d16SRodney W. Grimes 
1568ea022d16SRodney W. Grimes 	if (restart_point)
1569ea022d16SRodney W. Grimes 		mode = "r+";
1570ea022d16SRodney W. Grimes 	fout = fopen(name, mode);
1571ea022d16SRodney W. Grimes 	closefunc = fclose;
1572ea022d16SRodney W. Grimes 	if (fout == NULL) {
1573ea022d16SRodney W. Grimes 		perror_reply(553, name);
1574ea022d16SRodney W. Grimes 		LOGCMD(*mode == 'w' ? "put" : "append", name);
1575ea022d16SRodney W. Grimes 		return;
1576ea022d16SRodney W. Grimes 	}
1577ea022d16SRodney W. Grimes 	byte_count = -1;
1578ea022d16SRodney W. Grimes 	if (restart_point) {
1579ea022d16SRodney W. Grimes 		if (type == TYPE_A) {
1580ea022d16SRodney W. Grimes 			off_t i, n;
1581ea022d16SRodney W. Grimes 			int c;
1582ea022d16SRodney W. Grimes 
1583ea022d16SRodney W. Grimes 			n = restart_point;
1584ea022d16SRodney W. Grimes 			i = 0;
1585ea022d16SRodney W. Grimes 			while (i++ < n) {
1586ea022d16SRodney W. Grimes 				if ((c=getc(fout)) == EOF) {
1587ea022d16SRodney W. Grimes 					perror_reply(550, name);
1588ea022d16SRodney W. Grimes 					goto done;
1589ea022d16SRodney W. Grimes 				}
1590ea022d16SRodney W. Grimes 				if (c == '\n')
1591ea022d16SRodney W. Grimes 					i++;
1592ea022d16SRodney W. Grimes 			}
1593ea022d16SRodney W. Grimes 			/*
1594ea022d16SRodney W. Grimes 			 * We must do this seek to "current" position
1595ea022d16SRodney W. Grimes 			 * because we are changing from reading to
1596ea022d16SRodney W. Grimes 			 * writing.
1597ea022d16SRodney W. Grimes 			 */
1598e4a71114SAndrey A. Chernov 			if (fseeko(fout, (off_t)0, SEEK_CUR) < 0) {
1599ea022d16SRodney W. Grimes 				perror_reply(550, name);
1600ea022d16SRodney W. Grimes 				goto done;
1601ea022d16SRodney W. Grimes 			}
1602ea022d16SRodney W. Grimes 		} else if (lseek(fileno(fout), restart_point, L_SET) < 0) {
1603ea022d16SRodney W. Grimes 			perror_reply(550, name);
1604ea022d16SRodney W. Grimes 			goto done;
1605ea022d16SRodney W. Grimes 		}
1606ea022d16SRodney W. Grimes 	}
1607ea022d16SRodney W. Grimes 	din = dataconn(name, (off_t)-1, "r");
1608ea022d16SRodney W. Grimes 	if (din == NULL)
1609ea022d16SRodney W. Grimes 		goto done;
1610ea022d16SRodney W. Grimes 	if (receive_data(din, fout) == 0) {
1611ea022d16SRodney W. Grimes 		if (unique)
1612ea022d16SRodney W. Grimes 			reply(226, "Transfer complete (unique file name:%s).",
1613ea022d16SRodney W. Grimes 			    name);
1614ea022d16SRodney W. Grimes 		else
1615ea022d16SRodney W. Grimes 			reply(226, "Transfer complete.");
1616ea022d16SRodney W. Grimes 	}
1617ea022d16SRodney W. Grimes 	(void) fclose(din);
1618ea022d16SRodney W. Grimes 	data = -1;
1619ea022d16SRodney W. Grimes 	pdata = -1;
1620ea022d16SRodney W. Grimes done:
1621ea022d16SRodney W. Grimes 	LOGBYTES(*mode == 'w' ? "put" : "append", name, byte_count);
1622ea022d16SRodney W. Grimes 	(*closefunc)(fout);
1623ea022d16SRodney W. Grimes }
1624ea022d16SRodney W. Grimes 
1625ea022d16SRodney W. Grimes static FILE *
1626ea022d16SRodney W. Grimes getdatasock(mode)
1627ea022d16SRodney W. Grimes 	char *mode;
1628ea022d16SRodney W. Grimes {
1629ea022d16SRodney W. Grimes 	int on = 1, s, t, tries;
1630ea022d16SRodney W. Grimes 
1631ea022d16SRodney W. Grimes 	if (data >= 0)
1632ea022d16SRodney W. Grimes 		return (fdopen(data, mode));
1633ea022d16SRodney W. Grimes 	(void) seteuid((uid_t)0);
16344dd8b5abSYoshinobu Inoue 
16354dd8b5abSYoshinobu Inoue 	s = socket(data_dest.su_family, SOCK_STREAM, 0);
1636ea022d16SRodney W. Grimes 	if (s < 0)
1637ea022d16SRodney W. Grimes 		goto bad;
1638ea022d16SRodney W. Grimes 	if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
1639ea022d16SRodney W. Grimes 	    (char *) &on, sizeof(on)) < 0)
1640ea022d16SRodney W. Grimes 		goto bad;
1641ea022d16SRodney W. Grimes 	/* anchor socket to avoid multi-homing problems */
16424dd8b5abSYoshinobu Inoue 	data_source = ctrl_addr;
16434dd8b5abSYoshinobu Inoue 	data_source.su_port = htons(20); /* ftp-data port */
1644ea022d16SRodney W. Grimes 	for (tries = 1; ; tries++) {
1645ea022d16SRodney W. Grimes 		if (bind(s, (struct sockaddr *)&data_source,
16464dd8b5abSYoshinobu Inoue 		    data_source.su_len) >= 0)
1647ea022d16SRodney W. Grimes 			break;
1648ea022d16SRodney W. Grimes 		if (errno != EADDRINUSE || tries > 10)
1649ea022d16SRodney W. Grimes 			goto bad;
1650ea022d16SRodney W. Grimes 		sleep(tries);
1651ea022d16SRodney W. Grimes 	}
1652ea022d16SRodney W. Grimes 	(void) seteuid((uid_t)pw->pw_uid);
1653ea022d16SRodney W. Grimes #ifdef IP_TOS
16544dd8b5abSYoshinobu Inoue 	if (data_source.su_family == AF_INET)
16554dd8b5abSYoshinobu Inoue       {
1656ea022d16SRodney W. Grimes 	on = IPTOS_THROUGHPUT;
1657ea022d16SRodney W. Grimes 	if (setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0)
1658ea022d16SRodney W. Grimes 		syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
16594dd8b5abSYoshinobu Inoue       }
1660ea022d16SRodney W. Grimes #endif
16619fc5823aSGarrett Wollman #ifdef TCP_NOPUSH
16629fc5823aSGarrett Wollman 	/*
16639fc5823aSGarrett Wollman 	 * Turn off push flag to keep sender TCP from sending short packets
16649fc5823aSGarrett Wollman 	 * at the boundaries of each write().  Should probably do a SO_SNDBUF
16659fc5823aSGarrett Wollman 	 * to set the send buffer size as well, but that may not be desirable
16669fc5823aSGarrett Wollman 	 * in heavy-load situations.
16679fc5823aSGarrett Wollman 	 */
16689fc5823aSGarrett Wollman 	on = 1;
16699fc5823aSGarrett Wollman 	if (setsockopt(s, IPPROTO_TCP, TCP_NOPUSH, (char *)&on, sizeof on) < 0)
16709fc5823aSGarrett Wollman 		syslog(LOG_WARNING, "setsockopt (TCP_NOPUSH): %m");
16719fc5823aSGarrett Wollman #endif
16729fc5823aSGarrett Wollman #ifdef SO_SNDBUF
16739fc5823aSGarrett Wollman 	on = 65536;
16749fc5823aSGarrett Wollman 	if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, (char *)&on, sizeof on) < 0)
16759fc5823aSGarrett Wollman 		syslog(LOG_WARNING, "setsockopt (SO_SNDBUF): %m");
16769fc5823aSGarrett Wollman #endif
16779fc5823aSGarrett Wollman 
1678ea022d16SRodney W. Grimes 	return (fdopen(s, mode));
1679ea022d16SRodney W. Grimes bad:
1680ea022d16SRodney W. Grimes 	/* Return the real value of errno (close may change it) */
1681ea022d16SRodney W. Grimes 	t = errno;
1682ea022d16SRodney W. Grimes 	(void) seteuid((uid_t)pw->pw_uid);
1683ea022d16SRodney W. Grimes 	(void) close(s);
1684ea022d16SRodney W. Grimes 	errno = t;
1685ea022d16SRodney W. Grimes 	return (NULL);
1686ea022d16SRodney W. Grimes }
1687ea022d16SRodney W. Grimes 
1688ea022d16SRodney W. Grimes static FILE *
1689ea022d16SRodney W. Grimes dataconn(name, size, mode)
1690ea022d16SRodney W. Grimes 	char *name;
1691ea022d16SRodney W. Grimes 	off_t size;
1692ea022d16SRodney W. Grimes 	char *mode;
1693ea022d16SRodney W. Grimes {
1694ea022d16SRodney W. Grimes 	char sizebuf[32];
1695ea022d16SRodney W. Grimes 	FILE *file;
1696ea022d16SRodney W. Grimes 	int retry = 0, tos;
1697ea022d16SRodney W. Grimes 
1698ea022d16SRodney W. Grimes 	file_size = size;
1699ea022d16SRodney W. Grimes 	byte_count = 0;
1700ea022d16SRodney W. Grimes 	if (size != (off_t) -1)
1701e760ef2cSWarner Losh 		(void) snprintf(sizebuf, sizeof(sizebuf), " (%qd bytes)", size);
1702ea022d16SRodney W. Grimes 	else
1703e760ef2cSWarner Losh 		*sizebuf = '\0';
1704ea022d16SRodney W. Grimes 	if (pdata >= 0) {
17054dd8b5abSYoshinobu Inoue 		union sockunion from;
17064cd48bacSYaroslav Tykhiy 		int flags;
17074dd8b5abSYoshinobu Inoue 		int s, fromlen = ctrl_addr.su_len;
1708d6ed3c37SGuido van Rooij 		struct timeval timeout;
1709d6ed3c37SGuido van Rooij 		fd_set set;
1710ea022d16SRodney W. Grimes 
1711d6ed3c37SGuido van Rooij 		FD_ZERO(&set);
1712d6ed3c37SGuido van Rooij 		FD_SET(pdata, &set);
1713d6ed3c37SGuido van Rooij 
1714d6ed3c37SGuido van Rooij 		timeout.tv_usec = 0;
1715d6ed3c37SGuido van Rooij 		timeout.tv_sec = 120;
1716d6ed3c37SGuido van Rooij 
17174cd48bacSYaroslav Tykhiy 		/*
17184cd48bacSYaroslav Tykhiy 		 * Granted a socket is in the blocking I/O mode,
17194cd48bacSYaroslav Tykhiy 		 * accept() will block after a successful select()
17204cd48bacSYaroslav Tykhiy 		 * if the selected connection dies in between.
17214cd48bacSYaroslav Tykhiy 		 * Therefore set the non-blocking I/O flag here.
17224cd48bacSYaroslav Tykhiy 		 */
17234cd48bacSYaroslav Tykhiy 		if ((flags = fcntl(pdata, F_GETFL, 0)) == -1 ||
17244cd48bacSYaroslav Tykhiy 		    fcntl(pdata, F_SETFL, flags | O_NONBLOCK) == -1)
17254cd48bacSYaroslav Tykhiy 			goto pdata_err;
17264cd48bacSYaroslav Tykhiy 		if (select(pdata+1, &set, (fd_set *) 0, (fd_set *) 0, &timeout) <= 0 ||
17274cd48bacSYaroslav Tykhiy 		    (s = accept(pdata, (struct sockaddr *) &from, &fromlen)) < 0)
17284cd48bacSYaroslav Tykhiy 			goto pdata_err;
1729ea022d16SRodney W. Grimes 		(void) close(pdata);
1730ea022d16SRodney W. Grimes 		pdata = s;
17314cd48bacSYaroslav Tykhiy 		/*
17324cd48bacSYaroslav Tykhiy 		 * Unset the blocking I/O flag on the child socket
17334cd48bacSYaroslav Tykhiy 		 * again so stdio can work on it.
17344cd48bacSYaroslav Tykhiy 		 */
17354cd48bacSYaroslav Tykhiy 		if ((flags = fcntl(pdata, F_GETFL, 0)) == -1 ||
17364cd48bacSYaroslav Tykhiy 		    fcntl(pdata, F_SETFL, flags & ~O_NONBLOCK) == -1)
17374cd48bacSYaroslav Tykhiy 			goto pdata_err;
1738ea022d16SRodney W. Grimes #ifdef IP_TOS
17394dd8b5abSYoshinobu Inoue 		if (from.su_family == AF_INET)
17404dd8b5abSYoshinobu Inoue 	      {
1741a5a4544eSPaul Traina 		tos = IPTOS_THROUGHPUT;
1742ea022d16SRodney W. Grimes 		(void) setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&tos,
1743ea022d16SRodney W. Grimes 		    sizeof(int));
17444dd8b5abSYoshinobu Inoue 	      }
1745ea022d16SRodney W. Grimes #endif
1746ea022d16SRodney W. Grimes 		reply(150, "Opening %s mode data connection for '%s'%s.",
1747ea022d16SRodney W. Grimes 		     type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf);
1748ea022d16SRodney W. Grimes 		return (fdopen(pdata, mode));
17494cd48bacSYaroslav Tykhiy pdata_err:
17504cd48bacSYaroslav Tykhiy 		reply(425, "Can't open data connection.");
17514cd48bacSYaroslav Tykhiy 		(void) close(pdata);
17524cd48bacSYaroslav Tykhiy 		pdata = -1;
17534cd48bacSYaroslav Tykhiy 		return (NULL);
1754ea022d16SRodney W. Grimes 	}
1755ea022d16SRodney W. Grimes 	if (data >= 0) {
1756ea022d16SRodney W. Grimes 		reply(125, "Using existing data connection for '%s'%s.",
1757ea022d16SRodney W. Grimes 		    name, sizebuf);
1758ea022d16SRodney W. Grimes 		usedefault = 1;
1759ea022d16SRodney W. Grimes 		return (fdopen(data, mode));
1760ea022d16SRodney W. Grimes 	}
1761ea022d16SRodney W. Grimes 	if (usedefault)
1762ea022d16SRodney W. Grimes 		data_dest = his_addr;
1763ea022d16SRodney W. Grimes 	usedefault = 1;
1764ea022d16SRodney W. Grimes 	file = getdatasock(mode);
1765ea022d16SRodney W. Grimes 	if (file == NULL) {
17664dd8b5abSYoshinobu Inoue 		char hostbuf[BUFSIZ], portbuf[BUFSIZ];
17674dd8b5abSYoshinobu Inoue 		getnameinfo((struct sockaddr *)&data_source,
17684dd8b5abSYoshinobu Inoue 			data_source.su_len, hostbuf, sizeof(hostbuf) - 1,
17694dd8b5abSYoshinobu Inoue 			portbuf, sizeof(portbuf),
17704dd8b5abSYoshinobu Inoue 			NI_NUMERICHOST|NI_NUMERICSERV|NI_WITHSCOPEID);
17714dd8b5abSYoshinobu Inoue 		reply(425, "Can't create data socket (%s,%s): %s.",
17724dd8b5abSYoshinobu Inoue 			hostbuf, portbuf, strerror(errno));
1773ea022d16SRodney W. Grimes 		return (NULL);
1774ea022d16SRodney W. Grimes 	}
1775ea022d16SRodney W. Grimes 	data = fileno(file);
1776ea022d16SRodney W. Grimes 	while (connect(data, (struct sockaddr *)&data_dest,
17774dd8b5abSYoshinobu Inoue 	    data_dest.su_len) < 0) {
1778ea022d16SRodney W. Grimes 		if (errno == EADDRINUSE && retry < swaitmax) {
1779ea022d16SRodney W. Grimes 			sleep((unsigned) swaitint);
1780ea022d16SRodney W. Grimes 			retry += swaitint;
1781ea022d16SRodney W. Grimes 			continue;
1782ea022d16SRodney W. Grimes 		}
1783ea022d16SRodney W. Grimes 		perror_reply(425, "Can't build data connection");
1784ea022d16SRodney W. Grimes 		(void) fclose(file);
1785ea022d16SRodney W. Grimes 		data = -1;
1786ea022d16SRodney W. Grimes 		return (NULL);
1787ea022d16SRodney W. Grimes 	}
1788ea022d16SRodney W. Grimes 	reply(150, "Opening %s mode data connection for '%s'%s.",
1789ea022d16SRodney W. Grimes 	     type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf);
1790ea022d16SRodney W. Grimes 	return (file);
1791ea022d16SRodney W. Grimes }
1792ea022d16SRodney W. Grimes 
1793ea022d16SRodney W. Grimes /*
1794ea022d16SRodney W. Grimes  * Tranfer the contents of "instr" to "outstr" peer using the appropriate
17959fc5823aSGarrett Wollman  * encapsulation of the data subject to Mode, Structure, and Type.
1796ea022d16SRodney W. Grimes  *
1797ea022d16SRodney W. Grimes  * NB: Form isn't handled.
1798ea022d16SRodney W. Grimes  */
17994b82fc95SYaroslav Tykhiy static int
18009fc5823aSGarrett Wollman send_data(instr, outstr, blksize, filesize, isreg)
1801ea022d16SRodney W. Grimes 	FILE *instr, *outstr;
1802ea022d16SRodney W. Grimes 	off_t blksize;
18039fc5823aSGarrett Wollman 	off_t filesize;
18049fc5823aSGarrett Wollman 	int isreg;
1805ea022d16SRodney W. Grimes {
1806f6f0c4b9SDan Moschuk 	int c, filefd, netfd;
1807f6f0c4b9SDan Moschuk 	char *buf;
18089fc5823aSGarrett Wollman 	size_t len;
1809f6f0c4b9SDan Moschuk 	off_t cnt;
1810ea022d16SRodney W. Grimes 
1811ea022d16SRodney W. Grimes 	transflag++;
1812ea022d16SRodney W. Grimes 	switch (type) {
1813ea022d16SRodney W. Grimes 
1814ea022d16SRodney W. Grimes 	case TYPE_A:
1815ea022d16SRodney W. Grimes 		while ((c = getc(instr)) != EOF) {
18164b82fc95SYaroslav Tykhiy 			if (recvurg)
18174b82fc95SYaroslav Tykhiy 				goto got_oob;
1818ea022d16SRodney W. Grimes 			byte_count++;
1819ea022d16SRodney W. Grimes 			if (c == '\n') {
1820ea022d16SRodney W. Grimes 				if (ferror(outstr))
1821ea022d16SRodney W. Grimes 					goto data_err;
1822ea022d16SRodney W. Grimes 				(void) putc('\r', outstr);
1823ea022d16SRodney W. Grimes 			}
1824ea022d16SRodney W. Grimes 			(void) putc(c, outstr);
1825ea022d16SRodney W. Grimes 		}
18264b82fc95SYaroslav Tykhiy 		if (recvurg)
18274b82fc95SYaroslav Tykhiy 			goto got_oob;
1828ea022d16SRodney W. Grimes 		fflush(outstr);
1829ea022d16SRodney W. Grimes 		transflag = 0;
1830ea022d16SRodney W. Grimes 		if (ferror(instr))
1831ea022d16SRodney W. Grimes 			goto file_err;
1832ea022d16SRodney W. Grimes 		if (ferror(outstr))
1833ea022d16SRodney W. Grimes 			goto data_err;
1834ea022d16SRodney W. Grimes 		reply(226, "Transfer complete.");
18354b82fc95SYaroslav Tykhiy 		return (0);
1836ea022d16SRodney W. Grimes 
1837ea022d16SRodney W. Grimes 	case TYPE_I:
1838ea022d16SRodney W. Grimes 	case TYPE_L:
18399fc5823aSGarrett Wollman 		/*
18409fc5823aSGarrett Wollman 		 * isreg is only set if we are not doing restart and we
18419fc5823aSGarrett Wollman 		 * are sending a regular file
18429fc5823aSGarrett Wollman 		 */
18439fc5823aSGarrett Wollman 		netfd = fileno(outstr);
18449fc5823aSGarrett Wollman 		filefd = fileno(instr);
18459fc5823aSGarrett Wollman 
1846f6f0c4b9SDan Moschuk 		if (isreg) {
18479fc5823aSGarrett Wollman 
1848f6f0c4b9SDan Moschuk 			off_t offset;
1849f6f0c4b9SDan Moschuk 			int err;
1850f6f0c4b9SDan Moschuk 
1851f6f0c4b9SDan Moschuk 			len = filesize;
1852f6f0c4b9SDan Moschuk 			err = cnt = offset = 0;
1853f6f0c4b9SDan Moschuk 
1854f6f0c4b9SDan Moschuk 			while (err != -1 && cnt < filesize) {
1855f6f0c4b9SDan Moschuk 				err = sendfile(filefd, netfd, offset, len,
1856f6f0c4b9SDan Moschuk 					(struct sf_hdtr *) NULL, &cnt, 0);
18574b82fc95SYaroslav Tykhiy 				if (recvurg)
18584b82fc95SYaroslav Tykhiy 					goto got_oob;
18591f15c0d6SDag-Erling Smørgrav 				byte_count += cnt;
1860f6f0c4b9SDan Moschuk 				offset += cnt;
1861f6f0c4b9SDan Moschuk 				len -= cnt;
1862f6f0c4b9SDan Moschuk 
1863f6f0c4b9SDan Moschuk 				if (err == -1) {
1864f6f0c4b9SDan Moschuk 					if (!cnt)
1865f6f0c4b9SDan Moschuk 						goto oldway;
1866f6f0c4b9SDan Moschuk 
18679fc5823aSGarrett Wollman 					goto data_err;
1868f6f0c4b9SDan Moschuk 				}
1869f6f0c4b9SDan Moschuk 			}
1870f6f0c4b9SDan Moschuk 
18719fc5823aSGarrett Wollman 			reply(226, "Transfer complete.");
18724b82fc95SYaroslav Tykhiy 			return (0);
18739fc5823aSGarrett Wollman 		}
18749fc5823aSGarrett Wollman 
18759fc5823aSGarrett Wollman oldway:
1876ea022d16SRodney W. Grimes 		if ((buf = malloc((u_int)blksize)) == NULL) {
1877ea022d16SRodney W. Grimes 			transflag = 0;
1878ea022d16SRodney W. Grimes 			perror_reply(451, "Local resource failure: malloc");
18794b82fc95SYaroslav Tykhiy 			return (-1);
1880ea022d16SRodney W. Grimes 		}
18819fc5823aSGarrett Wollman 
1882ea022d16SRodney W. Grimes 		while ((cnt = read(filefd, buf, (u_int)blksize)) > 0 &&
1883ea022d16SRodney W. Grimes 		    write(netfd, buf, cnt) == cnt)
1884ea022d16SRodney W. Grimes 			byte_count += cnt;
1885ea022d16SRodney W. Grimes 		transflag = 0;
1886ea022d16SRodney W. Grimes 		(void)free(buf);
1887ea022d16SRodney W. Grimes 		if (cnt != 0) {
1888ea022d16SRodney W. Grimes 			if (cnt < 0)
1889ea022d16SRodney W. Grimes 				goto file_err;
1890ea022d16SRodney W. Grimes 			goto data_err;
1891ea022d16SRodney W. Grimes 		}
1892ea022d16SRodney W. Grimes 		reply(226, "Transfer complete.");
18934b82fc95SYaroslav Tykhiy 		return (0);
1894ea022d16SRodney W. Grimes 	default:
1895ea022d16SRodney W. Grimes 		transflag = 0;
1896ea022d16SRodney W. Grimes 		reply(550, "Unimplemented TYPE %d in send_data", type);
18974b82fc95SYaroslav Tykhiy 		return (-1);
1898ea022d16SRodney W. Grimes 	}
1899ea022d16SRodney W. Grimes 
1900ea022d16SRodney W. Grimes data_err:
1901ea022d16SRodney W. Grimes 	transflag = 0;
1902ea022d16SRodney W. Grimes 	perror_reply(426, "Data connection");
19034b82fc95SYaroslav Tykhiy 	return (-1);
1904ea022d16SRodney W. Grimes 
1905ea022d16SRodney W. Grimes file_err:
1906ea022d16SRodney W. Grimes 	transflag = 0;
1907ea022d16SRodney W. Grimes 	perror_reply(551, "Error on input file");
19084b82fc95SYaroslav Tykhiy 	return (-1);
19094b82fc95SYaroslav Tykhiy 
19104b82fc95SYaroslav Tykhiy got_oob:
19114b82fc95SYaroslav Tykhiy 	myoob();
19124b82fc95SYaroslav Tykhiy 	recvurg = 0;
19134b82fc95SYaroslav Tykhiy 	transflag = 0;
19144b82fc95SYaroslav Tykhiy 	return (-1);
1915ea022d16SRodney W. Grimes }
1916ea022d16SRodney W. Grimes 
1917ea022d16SRodney W. Grimes /*
1918ea022d16SRodney W. Grimes  * Transfer data from peer to "outstr" using the appropriate encapulation of
1919ea022d16SRodney W. Grimes  * the data subject to Mode, Structure, and Type.
1920ea022d16SRodney W. Grimes  *
1921ea022d16SRodney W. Grimes  * N.B.: Form isn't handled.
1922ea022d16SRodney W. Grimes  */
1923ea022d16SRodney W. Grimes static int
1924ea022d16SRodney W. Grimes receive_data(instr, outstr)
1925ea022d16SRodney W. Grimes 	FILE *instr, *outstr;
1926ea022d16SRodney W. Grimes {
1927ea022d16SRodney W. Grimes 	int c;
192861f891a6SPaul Traina 	int cnt, bare_lfs;
1929ea022d16SRodney W. Grimes 	char buf[BUFSIZ];
1930ea022d16SRodney W. Grimes 
1931ea022d16SRodney W. Grimes 	transflag++;
193261f891a6SPaul Traina 	bare_lfs = 0;
193361f891a6SPaul Traina 
1934ea022d16SRodney W. Grimes 	switch (type) {
1935ea022d16SRodney W. Grimes 
1936ea022d16SRodney W. Grimes 	case TYPE_I:
1937ea022d16SRodney W. Grimes 	case TYPE_L:
1938ea022d16SRodney W. Grimes 		while ((cnt = read(fileno(instr), buf, sizeof(buf))) > 0) {
19394b82fc95SYaroslav Tykhiy 			if (recvurg)
19404b82fc95SYaroslav Tykhiy 				goto got_oob;
1941ea022d16SRodney W. Grimes 			if (write(fileno(outstr), buf, cnt) != cnt)
1942ea022d16SRodney W. Grimes 				goto file_err;
1943ea022d16SRodney W. Grimes 			byte_count += cnt;
1944ea022d16SRodney W. Grimes 		}
19454b82fc95SYaroslav Tykhiy 		if (recvurg)
19464b82fc95SYaroslav Tykhiy 			goto got_oob;
1947ea022d16SRodney W. Grimes 		if (cnt < 0)
1948ea022d16SRodney W. Grimes 			goto data_err;
1949ea022d16SRodney W. Grimes 		transflag = 0;
1950ea022d16SRodney W. Grimes 		return (0);
1951ea022d16SRodney W. Grimes 
1952ea022d16SRodney W. Grimes 	case TYPE_E:
1953ea022d16SRodney W. Grimes 		reply(553, "TYPE E not implemented.");
1954ea022d16SRodney W. Grimes 		transflag = 0;
1955ea022d16SRodney W. Grimes 		return (-1);
1956ea022d16SRodney W. Grimes 
1957ea022d16SRodney W. Grimes 	case TYPE_A:
1958ea022d16SRodney W. Grimes 		while ((c = getc(instr)) != EOF) {
19594b82fc95SYaroslav Tykhiy 			if (recvurg)
19604b82fc95SYaroslav Tykhiy 				goto got_oob;
1961ea022d16SRodney W. Grimes 			byte_count++;
1962ea022d16SRodney W. Grimes 			if (c == '\n')
1963ea022d16SRodney W. Grimes 				bare_lfs++;
1964ea022d16SRodney W. Grimes 			while (c == '\r') {
1965ea022d16SRodney W. Grimes 				if (ferror(outstr))
1966ea022d16SRodney W. Grimes 					goto data_err;
1967ea022d16SRodney W. Grimes 				if ((c = getc(instr)) != '\n') {
1968ea022d16SRodney W. Grimes 					(void) putc ('\r', outstr);
1969ea022d16SRodney W. Grimes 					if (c == '\0' || c == EOF)
1970ea022d16SRodney W. Grimes 						goto contin2;
1971ea022d16SRodney W. Grimes 				}
1972ea022d16SRodney W. Grimes 			}
1973ea022d16SRodney W. Grimes 			(void) putc(c, outstr);
1974ea022d16SRodney W. Grimes 	contin2:	;
1975ea022d16SRodney W. Grimes 		}
19764b82fc95SYaroslav Tykhiy 		if (recvurg)
19774b82fc95SYaroslav Tykhiy 			goto got_oob;
1978ea022d16SRodney W. Grimes 		fflush(outstr);
1979ea022d16SRodney W. Grimes 		if (ferror(instr))
1980ea022d16SRodney W. Grimes 			goto data_err;
1981ea022d16SRodney W. Grimes 		if (ferror(outstr))
1982ea022d16SRodney W. Grimes 			goto file_err;
1983ea022d16SRodney W. Grimes 		transflag = 0;
1984ea022d16SRodney W. Grimes 		if (bare_lfs) {
1985ea022d16SRodney W. Grimes 			lreply(226,
1986ea022d16SRodney W. Grimes 		"WARNING! %d bare linefeeds received in ASCII mode",
1987ea022d16SRodney W. Grimes 			    bare_lfs);
1988ea022d16SRodney W. Grimes 		(void)printf("   File may not have transferred correctly.\r\n");
1989ea022d16SRodney W. Grimes 		}
1990ea022d16SRodney W. Grimes 		return (0);
1991ea022d16SRodney W. Grimes 	default:
1992ea022d16SRodney W. Grimes 		reply(550, "Unimplemented TYPE %d in receive_data", type);
1993ea022d16SRodney W. Grimes 		transflag = 0;
1994ea022d16SRodney W. Grimes 		return (-1);
1995ea022d16SRodney W. Grimes 	}
1996ea022d16SRodney W. Grimes 
1997ea022d16SRodney W. Grimes data_err:
1998ea022d16SRodney W. Grimes 	transflag = 0;
1999ea022d16SRodney W. Grimes 	perror_reply(426, "Data Connection");
2000ea022d16SRodney W. Grimes 	return (-1);
2001ea022d16SRodney W. Grimes 
2002ea022d16SRodney W. Grimes file_err:
2003ea022d16SRodney W. Grimes 	transflag = 0;
2004ea022d16SRodney W. Grimes 	perror_reply(452, "Error writing file");
2005ea022d16SRodney W. Grimes 	return (-1);
20064b82fc95SYaroslav Tykhiy 
20074b82fc95SYaroslav Tykhiy got_oob:
20084b82fc95SYaroslav Tykhiy 	myoob();
20094b82fc95SYaroslav Tykhiy 	recvurg = 0;
20104b82fc95SYaroslav Tykhiy 	transflag = 0;
20114b82fc95SYaroslav Tykhiy 	return (-1);
2012ea022d16SRodney W. Grimes }
2013ea022d16SRodney W. Grimes 
2014ea022d16SRodney W. Grimes void
2015ea022d16SRodney W. Grimes statfilecmd(filename)
2016ea022d16SRodney W. Grimes 	char *filename;
2017ea022d16SRodney W. Grimes {
2018ea022d16SRodney W. Grimes 	FILE *fin;
2019ea022d16SRodney W. Grimes 	int c;
2020ea022d16SRodney W. Grimes 	char line[LINE_MAX];
2021ea022d16SRodney W. Grimes 
2022af85d782SDavid Nugent 	(void)snprintf(line, sizeof(line), _PATH_LS " -lgA %s", filename);
2023ea022d16SRodney W. Grimes 	fin = ftpd_popen(line, "r");
2024ea022d16SRodney W. Grimes 	lreply(211, "status of %s:", filename);
2025ea022d16SRodney W. Grimes 	while ((c = getc(fin)) != EOF) {
2026ea022d16SRodney W. Grimes 		if (c == '\n') {
2027ea022d16SRodney W. Grimes 			if (ferror(stdout)){
2028ea022d16SRodney W. Grimes 				perror_reply(421, "control connection");
2029ea022d16SRodney W. Grimes 				(void) ftpd_pclose(fin);
2030ea022d16SRodney W. Grimes 				dologout(1);
2031ea022d16SRodney W. Grimes 				/* NOTREACHED */
2032ea022d16SRodney W. Grimes 			}
2033ea022d16SRodney W. Grimes 			if (ferror(fin)) {
2034ea022d16SRodney W. Grimes 				perror_reply(551, filename);
2035ea022d16SRodney W. Grimes 				(void) ftpd_pclose(fin);
2036ea022d16SRodney W. Grimes 				return;
2037ea022d16SRodney W. Grimes 			}
2038ea022d16SRodney W. Grimes 			(void) putc('\r', stdout);
2039ea022d16SRodney W. Grimes 		}
2040ea022d16SRodney W. Grimes 		(void) putc(c, stdout);
2041ea022d16SRodney W. Grimes 	}
2042ea022d16SRodney W. Grimes 	(void) ftpd_pclose(fin);
2043ea022d16SRodney W. Grimes 	reply(211, "End of Status");
2044ea022d16SRodney W. Grimes }
2045ea022d16SRodney W. Grimes 
2046ea022d16SRodney W. Grimes void
2047ea022d16SRodney W. Grimes statcmd()
2048ea022d16SRodney W. Grimes {
20494dd8b5abSYoshinobu Inoue 	union sockunion *su;
2050ea022d16SRodney W. Grimes 	u_char *a, *p;
20514dd8b5abSYoshinobu Inoue 	char hname[INET6_ADDRSTRLEN];
20524dd8b5abSYoshinobu Inoue 	int ispassive;
2053ea022d16SRodney W. Grimes 
2054ea022d16SRodney W. Grimes 	lreply(211, "%s FTP server status:", hostname, version);
2055ea022d16SRodney W. Grimes 	printf("     %s\r\n", version);
2056ea022d16SRodney W. Grimes 	printf("     Connected to %s", remotehost);
20574dd8b5abSYoshinobu Inoue 	if (!getnameinfo((struct sockaddr *)&his_addr, his_addr.su_len,
20584dd8b5abSYoshinobu Inoue 			 hname, sizeof(hname) - 1, NULL, 0,
20594dd8b5abSYoshinobu Inoue 			 NI_NUMERICHOST|NI_WITHSCOPEID)) {
20604dd8b5abSYoshinobu Inoue 		if (strcmp(hname, remotehost) != 0)
20614dd8b5abSYoshinobu Inoue 			printf(" (%s)", hname);
20624dd8b5abSYoshinobu Inoue 	}
2063ea022d16SRodney W. Grimes 	printf("\r\n");
2064ea022d16SRodney W. Grimes 	if (logged_in) {
2065ea022d16SRodney W. Grimes 		if (guest)
2066ea022d16SRodney W. Grimes 			printf("     Logged in anonymously\r\n");
2067ea022d16SRodney W. Grimes 		else
2068ea022d16SRodney W. Grimes 			printf("     Logged in as %s\r\n", pw->pw_name);
2069ea022d16SRodney W. Grimes 	} else if (askpasswd)
2070ea022d16SRodney W. Grimes 		printf("     Waiting for password\r\n");
2071ea022d16SRodney W. Grimes 	else
2072ea022d16SRodney W. Grimes 		printf("     Waiting for user name\r\n");
2073ea022d16SRodney W. Grimes 	printf("     TYPE: %s", typenames[type]);
2074ea022d16SRodney W. Grimes 	if (type == TYPE_A || type == TYPE_E)
2075ea022d16SRodney W. Grimes 		printf(", FORM: %s", formnames[form]);
2076ea022d16SRodney W. Grimes 	if (type == TYPE_L)
2077ea022d16SRodney W. Grimes #if NBBY == 8
2078ea022d16SRodney W. Grimes 		printf(" %d", NBBY);
2079ea022d16SRodney W. Grimes #else
2080ea022d16SRodney W. Grimes 		printf(" %d", bytesize);	/* need definition! */
2081ea022d16SRodney W. Grimes #endif
2082ea022d16SRodney W. Grimes 	printf("; STRUcture: %s; transfer MODE: %s\r\n",
2083ea022d16SRodney W. Grimes 	    strunames[stru], modenames[mode]);
2084ea022d16SRodney W. Grimes 	if (data != -1)
2085ea022d16SRodney W. Grimes 		printf("     Data connection open\r\n");
2086ea022d16SRodney W. Grimes 	else if (pdata != -1) {
20874dd8b5abSYoshinobu Inoue 		ispassive = 1;
20884dd8b5abSYoshinobu Inoue 		su = &pasv_addr;
2089ea022d16SRodney W. Grimes 		goto printaddr;
2090ea022d16SRodney W. Grimes 	} else if (usedefault == 0) {
20914dd8b5abSYoshinobu Inoue 		ispassive = 0;
20924dd8b5abSYoshinobu Inoue 		su = &data_dest;
2093ea022d16SRodney W. Grimes printaddr:
2094ea022d16SRodney W. Grimes #define UC(b) (((int) b) & 0xff)
20954dd8b5abSYoshinobu Inoue 		if (epsvall) {
20964dd8b5abSYoshinobu Inoue 			printf("     EPSV only mode (EPSV ALL)\r\n");
20974dd8b5abSYoshinobu Inoue 			goto epsvonly;
20984dd8b5abSYoshinobu Inoue 		}
20994dd8b5abSYoshinobu Inoue 
21004dd8b5abSYoshinobu Inoue 		/* PORT/PASV */
21014dd8b5abSYoshinobu Inoue 		if (su->su_family == AF_INET) {
21024dd8b5abSYoshinobu Inoue 			a = (u_char *) &su->su_sin.sin_addr;
21034dd8b5abSYoshinobu Inoue 			p = (u_char *) &su->su_sin.sin_port;
21044dd8b5abSYoshinobu Inoue 			printf("     %s (%d,%d,%d,%d,%d,%d)\r\n",
21054dd8b5abSYoshinobu Inoue 				ispassive ? "PASV" : "PORT",
21064dd8b5abSYoshinobu Inoue 				UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
21074dd8b5abSYoshinobu Inoue 				UC(p[0]), UC(p[1]));
21084dd8b5abSYoshinobu Inoue 		}
21094dd8b5abSYoshinobu Inoue 
21104dd8b5abSYoshinobu Inoue 		/* LPRT/LPSV */
21114dd8b5abSYoshinobu Inoue 	    {
21124dd8b5abSYoshinobu Inoue 		int alen, af, i;
21134dd8b5abSYoshinobu Inoue 
21144dd8b5abSYoshinobu Inoue 		switch (su->su_family) {
21154dd8b5abSYoshinobu Inoue 		case AF_INET:
21164dd8b5abSYoshinobu Inoue 			a = (u_char *) &su->su_sin.sin_addr;
21174dd8b5abSYoshinobu Inoue 			p = (u_char *) &su->su_sin.sin_port;
21184dd8b5abSYoshinobu Inoue 			alen = sizeof(su->su_sin.sin_addr);
21194dd8b5abSYoshinobu Inoue 			af = 4;
21204dd8b5abSYoshinobu Inoue 			break;
21214dd8b5abSYoshinobu Inoue 		case AF_INET6:
21224dd8b5abSYoshinobu Inoue 			a = (u_char *) &su->su_sin6.sin6_addr;
21234dd8b5abSYoshinobu Inoue 			p = (u_char *) &su->su_sin6.sin6_port;
21244dd8b5abSYoshinobu Inoue 			alen = sizeof(su->su_sin6.sin6_addr);
21254dd8b5abSYoshinobu Inoue 			af = 6;
21264dd8b5abSYoshinobu Inoue 			break;
21274dd8b5abSYoshinobu Inoue 		default:
21284dd8b5abSYoshinobu Inoue 			af = 0;
21294dd8b5abSYoshinobu Inoue 			break;
21304dd8b5abSYoshinobu Inoue 		}
21314dd8b5abSYoshinobu Inoue 		if (af) {
21324dd8b5abSYoshinobu Inoue 			printf("     %s (%d,%d,", ispassive ? "LPSV" : "LPRT",
21334dd8b5abSYoshinobu Inoue 				af, alen);
21344dd8b5abSYoshinobu Inoue 			for (i = 0; i < alen; i++)
21354dd8b5abSYoshinobu Inoue 				printf("%d,", UC(a[i]));
21364dd8b5abSYoshinobu Inoue 			printf("%d,%d,%d)\r\n", 2, UC(p[0]), UC(p[1]));
21374dd8b5abSYoshinobu Inoue 		}
21384dd8b5abSYoshinobu Inoue 	    }
21394dd8b5abSYoshinobu Inoue 
21404dd8b5abSYoshinobu Inoue epsvonly:;
21414dd8b5abSYoshinobu Inoue 		/* EPRT/EPSV */
21424dd8b5abSYoshinobu Inoue 	    {
21434dd8b5abSYoshinobu Inoue 		int af;
21444dd8b5abSYoshinobu Inoue 
21454dd8b5abSYoshinobu Inoue 		switch (su->su_family) {
21464dd8b5abSYoshinobu Inoue 		case AF_INET:
21474dd8b5abSYoshinobu Inoue 			af = 1;
21484dd8b5abSYoshinobu Inoue 			break;
21494dd8b5abSYoshinobu Inoue 		case AF_INET6:
21504dd8b5abSYoshinobu Inoue 			af = 2;
21514dd8b5abSYoshinobu Inoue 			break;
21524dd8b5abSYoshinobu Inoue 		default:
21534dd8b5abSYoshinobu Inoue 			af = 0;
21544dd8b5abSYoshinobu Inoue 			break;
21554dd8b5abSYoshinobu Inoue 		}
21564dd8b5abSYoshinobu Inoue 		if (af) {
21574dd8b5abSYoshinobu Inoue 			if (!getnameinfo((struct sockaddr *)su, su->su_len,
21584dd8b5abSYoshinobu Inoue 					hname, sizeof(hname) - 1, NULL, 0,
21594dd8b5abSYoshinobu Inoue 					NI_NUMERICHOST)) {
21604dd8b5abSYoshinobu Inoue 				printf("     %s |%d|%s|%d|\r\n",
21614dd8b5abSYoshinobu Inoue 					ispassive ? "EPSV" : "EPRT",
21624dd8b5abSYoshinobu Inoue 					af, hname, htons(su->su_port));
21634dd8b5abSYoshinobu Inoue 			}
21644dd8b5abSYoshinobu Inoue 		}
21654dd8b5abSYoshinobu Inoue 	    }
2166ea022d16SRodney W. Grimes #undef UC
2167ea022d16SRodney W. Grimes 	} else
2168ea022d16SRodney W. Grimes 		printf("     No data connection\r\n");
2169ea022d16SRodney W. Grimes 	reply(211, "End of status");
2170ea022d16SRodney W. Grimes }
2171ea022d16SRodney W. Grimes 
2172ea022d16SRodney W. Grimes void
2173618b0bbaSMark Murray fatalerror(s)
2174ea022d16SRodney W. Grimes 	char *s;
2175ea022d16SRodney W. Grimes {
2176ea022d16SRodney W. Grimes 
2177ea022d16SRodney W. Grimes 	reply(451, "Error in server: %s\n", s);
2178ea022d16SRodney W. Grimes 	reply(221, "Closing connection due to server error.");
2179ea022d16SRodney W. Grimes 	dologout(0);
2180ea022d16SRodney W. Grimes 	/* NOTREACHED */
2181ea022d16SRodney W. Grimes }
2182ea022d16SRodney W. Grimes 
2183ea022d16SRodney W. Grimes void
2184ea022d16SRodney W. Grimes #if __STDC__
2185ea022d16SRodney W. Grimes reply(int n, const char *fmt, ...)
2186ea022d16SRodney W. Grimes #else
2187ea022d16SRodney W. Grimes reply(n, fmt, va_alist)
2188ea022d16SRodney W. Grimes 	int n;
2189ea022d16SRodney W. Grimes 	char *fmt;
2190ea022d16SRodney W. Grimes         va_dcl
2191ea022d16SRodney W. Grimes #endif
2192ea022d16SRodney W. Grimes {
2193ea022d16SRodney W. Grimes 	va_list ap;
2194ea022d16SRodney W. Grimes #if __STDC__
2195ea022d16SRodney W. Grimes 	va_start(ap, fmt);
2196ea022d16SRodney W. Grimes #else
2197ea022d16SRodney W. Grimes 	va_start(ap);
2198ea022d16SRodney W. Grimes #endif
2199ea022d16SRodney W. Grimes 	(void)printf("%d ", n);
2200ea022d16SRodney W. Grimes 	(void)vprintf(fmt, ap);
2201ea022d16SRodney W. Grimes 	(void)printf("\r\n");
2202ea022d16SRodney W. Grimes 	(void)fflush(stdout);
2203618b0bbaSMark Murray 	if (ftpdebug) {
2204ea022d16SRodney W. Grimes 		syslog(LOG_DEBUG, "<--- %d ", n);
2205ea022d16SRodney W. Grimes 		vsyslog(LOG_DEBUG, fmt, ap);
2206ea022d16SRodney W. Grimes 	}
2207ea022d16SRodney W. Grimes }
2208ea022d16SRodney W. Grimes 
2209ea022d16SRodney W. Grimes void
2210ea022d16SRodney W. Grimes #if __STDC__
2211ea022d16SRodney W. Grimes lreply(int n, const char *fmt, ...)
2212ea022d16SRodney W. Grimes #else
2213ea022d16SRodney W. Grimes lreply(n, fmt, va_alist)
2214ea022d16SRodney W. Grimes 	int n;
2215ea022d16SRodney W. Grimes 	char *fmt;
2216ea022d16SRodney W. Grimes         va_dcl
2217ea022d16SRodney W. Grimes #endif
2218ea022d16SRodney W. Grimes {
2219ea022d16SRodney W. Grimes 	va_list ap;
2220ea022d16SRodney W. Grimes #if __STDC__
2221ea022d16SRodney W. Grimes 	va_start(ap, fmt);
2222ea022d16SRodney W. Grimes #else
2223ea022d16SRodney W. Grimes 	va_start(ap);
2224ea022d16SRodney W. Grimes #endif
2225ea022d16SRodney W. Grimes 	(void)printf("%d- ", n);
2226ea022d16SRodney W. Grimes 	(void)vprintf(fmt, ap);
2227ea022d16SRodney W. Grimes 	(void)printf("\r\n");
2228ea022d16SRodney W. Grimes 	(void)fflush(stdout);
2229618b0bbaSMark Murray 	if (ftpdebug) {
2230ea022d16SRodney W. Grimes 		syslog(LOG_DEBUG, "<--- %d- ", n);
2231ea022d16SRodney W. Grimes 		vsyslog(LOG_DEBUG, fmt, ap);
2232ea022d16SRodney W. Grimes 	}
2233ea022d16SRodney W. Grimes }
2234ea022d16SRodney W. Grimes 
2235ea022d16SRodney W. Grimes static void
2236ea022d16SRodney W. Grimes ack(s)
2237ea022d16SRodney W. Grimes 	char *s;
2238ea022d16SRodney W. Grimes {
2239ea022d16SRodney W. Grimes 
2240ea022d16SRodney W. Grimes 	reply(250, "%s command successful.", s);
2241ea022d16SRodney W. Grimes }
2242ea022d16SRodney W. Grimes 
2243ea022d16SRodney W. Grimes void
2244ea022d16SRodney W. Grimes nack(s)
2245ea022d16SRodney W. Grimes 	char *s;
2246ea022d16SRodney W. Grimes {
2247ea022d16SRodney W. Grimes 
2248ea022d16SRodney W. Grimes 	reply(502, "%s command not implemented.", s);
2249ea022d16SRodney W. Grimes }
2250ea022d16SRodney W. Grimes 
2251ea022d16SRodney W. Grimes /* ARGSUSED */
2252ea022d16SRodney W. Grimes void
2253ea022d16SRodney W. Grimes yyerror(s)
2254ea022d16SRodney W. Grimes 	char *s;
2255ea022d16SRodney W. Grimes {
2256ea022d16SRodney W. Grimes 	char *cp;
2257ea022d16SRodney W. Grimes 
22589aca17cbSMark Murray 	if ((cp = strchr(cbuf,'\n')))
2259ea022d16SRodney W. Grimes 		*cp = '\0';
2260ea022d16SRodney W. Grimes 	reply(500, "'%s': command not understood.", cbuf);
2261ea022d16SRodney W. Grimes }
2262ea022d16SRodney W. Grimes 
2263ea022d16SRodney W. Grimes void
2264ea022d16SRodney W. Grimes delete(name)
2265ea022d16SRodney W. Grimes 	char *name;
2266ea022d16SRodney W. Grimes {
2267ea022d16SRodney W. Grimes 	struct stat st;
2268ea022d16SRodney W. Grimes 
2269ea022d16SRodney W. Grimes 	LOGCMD("delete", name);
2270ea022d16SRodney W. Grimes 	if (stat(name, &st) < 0) {
2271ea022d16SRodney W. Grimes 		perror_reply(550, name);
2272ea022d16SRodney W. Grimes 		return;
2273ea022d16SRodney W. Grimes 	}
2274ea022d16SRodney W. Grimes 	if ((st.st_mode&S_IFMT) == S_IFDIR) {
2275ea022d16SRodney W. Grimes 		if (rmdir(name) < 0) {
2276ea022d16SRodney W. Grimes 			perror_reply(550, name);
2277ea022d16SRodney W. Grimes 			return;
2278ea022d16SRodney W. Grimes 		}
2279ea022d16SRodney W. Grimes 		goto done;
2280ea022d16SRodney W. Grimes 	}
2281ea022d16SRodney W. Grimes 	if (unlink(name) < 0) {
2282ea022d16SRodney W. Grimes 		perror_reply(550, name);
2283ea022d16SRodney W. Grimes 		return;
2284ea022d16SRodney W. Grimes 	}
2285ea022d16SRodney W. Grimes done:
2286ea022d16SRodney W. Grimes 	ack("DELE");
2287ea022d16SRodney W. Grimes }
2288ea022d16SRodney W. Grimes 
2289ea022d16SRodney W. Grimes void
2290ea022d16SRodney W. Grimes cwd(path)
2291ea022d16SRodney W. Grimes 	char *path;
2292ea022d16SRodney W. Grimes {
2293ea022d16SRodney W. Grimes 
2294ea022d16SRodney W. Grimes 	if (chdir(path) < 0)
2295ea022d16SRodney W. Grimes 		perror_reply(550, path);
2296ea022d16SRodney W. Grimes 	else
2297ea022d16SRodney W. Grimes 		ack("CWD");
2298ea022d16SRodney W. Grimes }
2299ea022d16SRodney W. Grimes 
2300ea022d16SRodney W. Grimes void
2301ea022d16SRodney W. Grimes makedir(name)
2302ea022d16SRodney W. Grimes 	char *name;
2303ea022d16SRodney W. Grimes {
2304ea022d16SRodney W. Grimes 
2305ea022d16SRodney W. Grimes 	LOGCMD("mkdir", name);
2306ea022d16SRodney W. Grimes 	if (mkdir(name, 0777) < 0)
2307ea022d16SRodney W. Grimes 		perror_reply(550, name);
2308ea022d16SRodney W. Grimes 	else
2309ea022d16SRodney W. Grimes 		reply(257, "MKD command successful.");
2310ea022d16SRodney W. Grimes }
2311ea022d16SRodney W. Grimes 
2312ea022d16SRodney W. Grimes void
2313ea022d16SRodney W. Grimes removedir(name)
2314ea022d16SRodney W. Grimes 	char *name;
2315ea022d16SRodney W. Grimes {
2316ea022d16SRodney W. Grimes 
2317ea022d16SRodney W. Grimes 	LOGCMD("rmdir", name);
2318ea022d16SRodney W. Grimes 	if (rmdir(name) < 0)
2319ea022d16SRodney W. Grimes 		perror_reply(550, name);
2320ea022d16SRodney W. Grimes 	else
2321ea022d16SRodney W. Grimes 		ack("RMD");
2322ea022d16SRodney W. Grimes }
2323ea022d16SRodney W. Grimes 
2324ea022d16SRodney W. Grimes void
2325ea022d16SRodney W. Grimes pwd()
2326ea022d16SRodney W. Grimes {
2327ea022d16SRodney W. Grimes 	char path[MAXPATHLEN + 1];
2328ea022d16SRodney W. Grimes 
2329ea022d16SRodney W. Grimes 	if (getwd(path) == (char *)NULL)
2330ea022d16SRodney W. Grimes 		reply(550, "%s.", path);
2331ea022d16SRodney W. Grimes 	else
2332ea022d16SRodney W. Grimes 		reply(257, "\"%s\" is current directory.", path);
2333ea022d16SRodney W. Grimes }
2334ea022d16SRodney W. Grimes 
2335ea022d16SRodney W. Grimes char *
2336ea022d16SRodney W. Grimes renamefrom(name)
2337ea022d16SRodney W. Grimes 	char *name;
2338ea022d16SRodney W. Grimes {
2339ea022d16SRodney W. Grimes 	struct stat st;
2340ea022d16SRodney W. Grimes 
2341ea022d16SRodney W. Grimes 	if (stat(name, &st) < 0) {
2342ea022d16SRodney W. Grimes 		perror_reply(550, name);
2343ea022d16SRodney W. Grimes 		return ((char *)0);
2344ea022d16SRodney W. Grimes 	}
2345ea022d16SRodney W. Grimes 	reply(350, "File exists, ready for destination name");
2346ea022d16SRodney W. Grimes 	return (name);
2347ea022d16SRodney W. Grimes }
2348ea022d16SRodney W. Grimes 
2349ea022d16SRodney W. Grimes void
2350ea022d16SRodney W. Grimes renamecmd(from, to)
2351ea022d16SRodney W. Grimes 	char *from, *to;
2352ea022d16SRodney W. Grimes {
235361f891a6SPaul Traina 	struct stat st;
2354ea022d16SRodney W. Grimes 
2355ea022d16SRodney W. Grimes 	LOGCMD2("rename", from, to);
235661f891a6SPaul Traina 
235761f891a6SPaul Traina 	if (guest && (stat(to, &st) == 0)) {
235861f891a6SPaul Traina 		reply(550, "%s: permission denied", to);
235961f891a6SPaul Traina 		return;
236061f891a6SPaul Traina 	}
236161f891a6SPaul Traina 
2362ea022d16SRodney W. Grimes 	if (rename(from, to) < 0)
2363ea022d16SRodney W. Grimes 		perror_reply(550, "rename");
2364ea022d16SRodney W. Grimes 	else
2365ea022d16SRodney W. Grimes 		ack("RNTO");
2366ea022d16SRodney W. Grimes }
2367ea022d16SRodney W. Grimes 
2368ea022d16SRodney W. Grimes static void
23694dd8b5abSYoshinobu Inoue dolog(who)
23704dd8b5abSYoshinobu Inoue 	struct sockaddr *who;
2371ea022d16SRodney W. Grimes {
23724dd8b5abSYoshinobu Inoue 	int error;
23734dd8b5abSYoshinobu Inoue 
23744dd8b5abSYoshinobu Inoue 	realhostname_sa(remotehost, sizeof(remotehost) - 1, who, who->sa_len);
2375ea022d16SRodney W. Grimes 
2376ea022d16SRodney W. Grimes #ifdef SETPROCTITLE
2377ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
2378ea4e54b9SDavid Nugent 	if (thishost != firsthost)
2379ea4e54b9SDavid Nugent 		snprintf(proctitle, sizeof(proctitle), "%s: connected (to %s)",
2380ea4e54b9SDavid Nugent 			 remotehost, hostname);
2381ea4e54b9SDavid Nugent 	else
2382ea4e54b9SDavid Nugent #endif
2383ea4e54b9SDavid Nugent 		snprintf(proctitle, sizeof(proctitle), "%s: connected",
2384ea4e54b9SDavid Nugent 			 remotehost);
2385b63e1fe2SPeter Wemm 	setproctitle("%s", proctitle);
2386ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */
2387ea022d16SRodney W. Grimes 
2388ea4e54b9SDavid Nugent 	if (logging) {
2389ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
2390ea4e54b9SDavid Nugent 		if (thishost != firsthost)
2391ea4e54b9SDavid Nugent 			syslog(LOG_INFO, "connection from %s (to %s)",
2392ea4e54b9SDavid Nugent 			       remotehost, hostname);
2393ea4e54b9SDavid Nugent 		else
2394ea4e54b9SDavid Nugent #endif
23954dd8b5abSYoshinobu Inoue 		{
23964dd8b5abSYoshinobu Inoue 			char	who_name[MAXHOSTNAMELEN];
23974dd8b5abSYoshinobu Inoue 
23984dd8b5abSYoshinobu Inoue 			error = getnameinfo(who, who->sa_len,
23994dd8b5abSYoshinobu Inoue 					    who_name, sizeof(who_name) - 1,
24004dd8b5abSYoshinobu Inoue 					    NULL, 0,
24014dd8b5abSYoshinobu Inoue 					    NI_NUMERICHOST|NI_WITHSCOPEID);
2402f5c57d05SEivind Eklund 			syslog(LOG_INFO, "connection from %s (%s)", remotehost,
24034dd8b5abSYoshinobu Inoue 			       error == 0 ? who_name : "");
24044dd8b5abSYoshinobu Inoue 		}
2405ea022d16SRodney W. Grimes 	}
2406ea4e54b9SDavid Nugent }
2407ea022d16SRodney W. Grimes 
2408ea022d16SRodney W. Grimes /*
2409ea022d16SRodney W. Grimes  * Record logout in wtmp file
2410ea022d16SRodney W. Grimes  * and exit with supplied status.
2411ea022d16SRodney W. Grimes  */
2412ea022d16SRodney W. Grimes void
2413ea022d16SRodney W. Grimes dologout(status)
2414ea022d16SRodney W. Grimes 	int status;
2415ea022d16SRodney W. Grimes {
24160b4df2eeSDavid Greenman 	/*
24170b4df2eeSDavid Greenman 	 * Prevent reception of SIGURG from resulting in a resumption
24180b4df2eeSDavid Greenman 	 * back to the main program loop.
24190b4df2eeSDavid Greenman 	 */
24200b4df2eeSDavid Greenman 	transflag = 0;
2421ea022d16SRodney W. Grimes 
2422ea022d16SRodney W. Grimes 	if (logged_in) {
2423ea022d16SRodney W. Grimes 		(void) seteuid((uid_t)0);
242446948173SHajimu UMEMOTO 		ftpd_logwtmp(ttyline, "", NULL);
2425ea022d16SRodney W. Grimes 	}
2426ea022d16SRodney W. Grimes 	/* beware of flushing buffers after a SIGPIPE */
2427ea022d16SRodney W. Grimes 	_exit(status);
2428ea022d16SRodney W. Grimes }
2429ea022d16SRodney W. Grimes 
2430ea022d16SRodney W. Grimes static void
24314b82fc95SYaroslav Tykhiy sigurg(signo)
2432ea022d16SRodney W. Grimes 	int signo;
2433ea022d16SRodney W. Grimes {
24344b82fc95SYaroslav Tykhiy 
24354b82fc95SYaroslav Tykhiy 	recvurg = 1;
24364b82fc95SYaroslav Tykhiy }
24374b82fc95SYaroslav Tykhiy 
24384b82fc95SYaroslav Tykhiy static void
24394b82fc95SYaroslav Tykhiy myoob()
24404b82fc95SYaroslav Tykhiy {
2441ea022d16SRodney W. Grimes 	char *cp;
2442ea022d16SRodney W. Grimes 
2443ea022d16SRodney W. Grimes 	/* only process if transfer occurring */
2444ea022d16SRodney W. Grimes 	if (!transflag)
2445ea022d16SRodney W. Grimes 		return;
2446ea022d16SRodney W. Grimes 	cp = tmpline;
2447ea022d16SRodney W. Grimes 	if (getline(cp, 7, stdin) == NULL) {
2448ea022d16SRodney W. Grimes 		reply(221, "You could at least say goodbye.");
2449ea022d16SRodney W. Grimes 		dologout(0);
2450ea022d16SRodney W. Grimes 	}
2451ea022d16SRodney W. Grimes 	upper(cp);
2452ea022d16SRodney W. Grimes 	if (strcmp(cp, "ABOR\r\n") == 0) {
2453ea022d16SRodney W. Grimes 		tmpline[0] = '\0';
2454ea022d16SRodney W. Grimes 		reply(426, "Transfer aborted. Data connection closed.");
2455ea022d16SRodney W. Grimes 		reply(226, "Abort successful");
2456ea022d16SRodney W. Grimes 	}
2457ea022d16SRodney W. Grimes 	if (strcmp(cp, "STAT\r\n") == 0) {
24589db4bbf3SMichael Haro 		tmpline[0] = '\0';
2459ea022d16SRodney W. Grimes 		if (file_size != (off_t) -1)
2460ea022d16SRodney W. Grimes 			reply(213, "Status: %qd of %qd bytes transferred",
2461ea022d16SRodney W. Grimes 			    byte_count, file_size);
2462ea022d16SRodney W. Grimes 		else
2463ea022d16SRodney W. Grimes 			reply(213, "Status: %qd bytes transferred", byte_count);
2464ea022d16SRodney W. Grimes 	}
2465ea022d16SRodney W. Grimes }
2466ea022d16SRodney W. Grimes 
2467ea022d16SRodney W. Grimes /*
2468ea022d16SRodney W. Grimes  * Note: a response of 425 is not mentioned as a possible response to
2469ea022d16SRodney W. Grimes  *	the PASV command in RFC959. However, it has been blessed as
2470ea022d16SRodney W. Grimes  *	a legitimate response by Jon Postel in a telephone conversation
2471ea022d16SRodney W. Grimes  *	with Rick Adams on 25 Jan 89.
2472ea022d16SRodney W. Grimes  */
2473ea022d16SRodney W. Grimes void
2474ea022d16SRodney W. Grimes passive()
2475ea022d16SRodney W. Grimes {
2476dacc9752SPaul Traina 	int len;
2477ea022d16SRodney W. Grimes 	char *p, *a;
2478ea022d16SRodney W. Grimes 
247961f891a6SPaul Traina 	if (pdata >= 0)		/* close old port if one set */
248061f891a6SPaul Traina 		close(pdata);
248161f891a6SPaul Traina 
24824dd8b5abSYoshinobu Inoue 	pdata = socket(ctrl_addr.su_family, SOCK_STREAM, 0);
2483ea022d16SRodney W. Grimes 	if (pdata < 0) {
2484ea022d16SRodney W. Grimes 		perror_reply(425, "Can't open passive connection");
2485ea022d16SRodney W. Grimes 		return;
2486ea022d16SRodney W. Grimes 	}
24874c450ad7SPaul Traina 
24884c450ad7SPaul Traina 	(void) seteuid((uid_t)0);
248961f891a6SPaul Traina 
2490dacc9752SPaul Traina #ifdef IP_PORTRANGE
24914dd8b5abSYoshinobu Inoue 	if (ctrl_addr.su_family == AF_INET) {
2492dacc9752SPaul Traina 	    int on = restricted_data_ports ? IP_PORTRANGE_HIGH
2493dacc9752SPaul Traina 					   : IP_PORTRANGE_DEFAULT;
2494dacc9752SPaul Traina 
249540e9d39eSPeter Wemm 	    if (setsockopt(pdata, IPPROTO_IP, IP_PORTRANGE,
2496dacc9752SPaul Traina 			    (char *)&on, sizeof(on)) < 0)
24974c450ad7SPaul Traina 		    goto pasv_error;
24984c450ad7SPaul Traina 	}
2499dacc9752SPaul Traina #endif
25002db39860SNick Sayer #ifdef IPV6_PORTRANGE
25012db39860SNick Sayer 	if (ctrl_addr.su_family == AF_INET6) {
25022db39860SNick Sayer 	    int on = restricted_data_ports ? IPV6_PORTRANGE_HIGH
25032db39860SNick Sayer 					   : IPV6_PORTRANGE_DEFAULT;
25042db39860SNick Sayer 
25052db39860SNick Sayer 	    if (setsockopt(pdata, IPPROTO_IPV6, IPV6_PORTRANGE,
25062db39860SNick Sayer 			    (char *)&on, sizeof(on)) < 0)
25072db39860SNick Sayer 		    goto pasv_error;
25082db39860SNick Sayer 	}
25092db39860SNick Sayer #endif
251040e9d39eSPeter Wemm 
2511ea022d16SRodney W. Grimes 	pasv_addr = ctrl_addr;
25124dd8b5abSYoshinobu Inoue 	pasv_addr.su_port = 0;
25134dd8b5abSYoshinobu Inoue 	if (bind(pdata, (struct sockaddr *)&pasv_addr, pasv_addr.su_len) < 0)
2514ea022d16SRodney W. Grimes 		goto pasv_error;
251561f891a6SPaul Traina 
2516ea022d16SRodney W. Grimes 	(void) seteuid((uid_t)pw->pw_uid);
25174c450ad7SPaul Traina 
2518ea022d16SRodney W. Grimes 	len = sizeof(pasv_addr);
2519ea022d16SRodney W. Grimes 	if (getsockname(pdata, (struct sockaddr *) &pasv_addr, &len) < 0)
2520ea022d16SRodney W. Grimes 		goto pasv_error;
2521ea022d16SRodney W. Grimes 	if (listen(pdata, 1) < 0)
2522ea022d16SRodney W. Grimes 		goto pasv_error;
25234dd8b5abSYoshinobu Inoue 	if (pasv_addr.su_family == AF_INET)
25244dd8b5abSYoshinobu Inoue 		a = (char *) &pasv_addr.su_sin.sin_addr;
25254dd8b5abSYoshinobu Inoue 	else if (pasv_addr.su_family == AF_INET6 &&
25264dd8b5abSYoshinobu Inoue 		 IN6_IS_ADDR_V4MAPPED(&pasv_addr.su_sin6.sin6_addr))
25274dd8b5abSYoshinobu Inoue 		a = (char *) &pasv_addr.su_sin6.sin6_addr.s6_addr[12];
25284dd8b5abSYoshinobu Inoue 	else
25294dd8b5abSYoshinobu Inoue 		goto pasv_error;
25304dd8b5abSYoshinobu Inoue 
25314dd8b5abSYoshinobu Inoue 	p = (char *) &pasv_addr.su_port;
2532ea022d16SRodney W. Grimes 
2533ea022d16SRodney W. Grimes #define UC(b) (((int) b) & 0xff)
2534ea022d16SRodney W. Grimes 
2535ea022d16SRodney W. Grimes 	reply(227, "Entering Passive Mode (%d,%d,%d,%d,%d,%d)", UC(a[0]),
2536ea022d16SRodney W. Grimes 		UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1]));
2537ea022d16SRodney W. Grimes 	return;
2538ea022d16SRodney W. Grimes 
2539ea022d16SRodney W. Grimes pasv_error:
254061f891a6SPaul Traina 	(void) seteuid((uid_t)pw->pw_uid);
2541ea022d16SRodney W. Grimes 	(void) close(pdata);
2542ea022d16SRodney W. Grimes 	pdata = -1;
2543ea022d16SRodney W. Grimes 	perror_reply(425, "Can't open passive connection");
2544ea022d16SRodney W. Grimes 	return;
2545ea022d16SRodney W. Grimes }
2546ea022d16SRodney W. Grimes 
2547ea022d16SRodney W. Grimes /*
25484dd8b5abSYoshinobu Inoue  * Long Passive defined in RFC 1639.
25494dd8b5abSYoshinobu Inoue  *     228 Entering Long Passive Mode
25504dd8b5abSYoshinobu Inoue  *         (af, hal, h1, h2, h3,..., pal, p1, p2...)
25514dd8b5abSYoshinobu Inoue  */
25524dd8b5abSYoshinobu Inoue 
25534dd8b5abSYoshinobu Inoue void
25544dd8b5abSYoshinobu Inoue long_passive(cmd, pf)
25554dd8b5abSYoshinobu Inoue 	char *cmd;
25564dd8b5abSYoshinobu Inoue 	int pf;
25574dd8b5abSYoshinobu Inoue {
25584dd8b5abSYoshinobu Inoue 	int len;
25594dd8b5abSYoshinobu Inoue 	char *p, *a;
25604dd8b5abSYoshinobu Inoue 
25614dd8b5abSYoshinobu Inoue 	if (pdata >= 0)		/* close old port if one set */
25624dd8b5abSYoshinobu Inoue 		close(pdata);
25634dd8b5abSYoshinobu Inoue 
25644dd8b5abSYoshinobu Inoue 	if (pf != PF_UNSPEC) {
25654dd8b5abSYoshinobu Inoue 		if (ctrl_addr.su_family != pf) {
25664dd8b5abSYoshinobu Inoue 			switch (ctrl_addr.su_family) {
25674dd8b5abSYoshinobu Inoue 			case AF_INET:
25684dd8b5abSYoshinobu Inoue 				pf = 1;
25694dd8b5abSYoshinobu Inoue 				break;
25704dd8b5abSYoshinobu Inoue 			case AF_INET6:
25714dd8b5abSYoshinobu Inoue 				pf = 2;
25724dd8b5abSYoshinobu Inoue 				break;
25734dd8b5abSYoshinobu Inoue 			default:
25744dd8b5abSYoshinobu Inoue 				pf = 0;
25754dd8b5abSYoshinobu Inoue 				break;
25764dd8b5abSYoshinobu Inoue 			}
25774dd8b5abSYoshinobu Inoue 			/*
25784dd8b5abSYoshinobu Inoue 			 * XXX
25794dd8b5abSYoshinobu Inoue 			 * only EPRT/EPSV ready clients will understand this
25804dd8b5abSYoshinobu Inoue 			 */
25814dd8b5abSYoshinobu Inoue 			if (strcmp(cmd, "EPSV") == 0 && pf) {
25824dd8b5abSYoshinobu Inoue 				reply(522, "Network protocol mismatch, "
25834dd8b5abSYoshinobu Inoue 					"use (%d)", pf);
25844dd8b5abSYoshinobu Inoue 			} else
25854dd8b5abSYoshinobu Inoue 				reply(501, "Network protocol mismatch"); /*XXX*/
25864dd8b5abSYoshinobu Inoue 
25874dd8b5abSYoshinobu Inoue 			return;
25884dd8b5abSYoshinobu Inoue 		}
25894dd8b5abSYoshinobu Inoue 	}
25904dd8b5abSYoshinobu Inoue 
25914dd8b5abSYoshinobu Inoue 	pdata = socket(ctrl_addr.su_family, SOCK_STREAM, 0);
25924dd8b5abSYoshinobu Inoue 	if (pdata < 0) {
25934dd8b5abSYoshinobu Inoue 		perror_reply(425, "Can't open passive connection");
25944dd8b5abSYoshinobu Inoue 		return;
25954dd8b5abSYoshinobu Inoue 	}
25964dd8b5abSYoshinobu Inoue 
25974dd8b5abSYoshinobu Inoue 	(void) seteuid((uid_t)0);
25984dd8b5abSYoshinobu Inoue 
25994dd8b5abSYoshinobu Inoue 	pasv_addr = ctrl_addr;
26004dd8b5abSYoshinobu Inoue 	pasv_addr.su_port = 0;
26014dd8b5abSYoshinobu Inoue 	len = pasv_addr.su_len;
26024dd8b5abSYoshinobu Inoue 
26032db39860SNick Sayer #ifdef IP_PORTRANGE
26042db39860SNick Sayer 	if (ctrl_addr.su_family == AF_INET) {
26052db39860SNick Sayer 	    int on = restricted_data_ports ? IP_PORTRANGE_HIGH
26062db39860SNick Sayer 					   : IP_PORTRANGE_DEFAULT;
26072db39860SNick Sayer 
26082db39860SNick Sayer 	    if (setsockopt(pdata, IPPROTO_IP, IP_PORTRANGE,
26092db39860SNick Sayer 			    (char *)&on, sizeof(on)) < 0)
26102db39860SNick Sayer 		    goto pasv_error;
26112db39860SNick Sayer 	}
26122db39860SNick Sayer #endif
26132db39860SNick Sayer #ifdef IPV6_PORTRANGE
26142db39860SNick Sayer 	if (ctrl_addr.su_family == AF_INET6) {
26152db39860SNick Sayer 	    int on = restricted_data_ports ? IPV6_PORTRANGE_HIGH
26162db39860SNick Sayer 					   : IPV6_PORTRANGE_DEFAULT;
26172db39860SNick Sayer 
26182db39860SNick Sayer 	    if (setsockopt(pdata, IPPROTO_IPV6, IPV6_PORTRANGE,
26192db39860SNick Sayer 			    (char *)&on, sizeof(on)) < 0)
26202db39860SNick Sayer 		    goto pasv_error;
26212db39860SNick Sayer 	}
26222db39860SNick Sayer #endif
26232db39860SNick Sayer 
26244dd8b5abSYoshinobu Inoue 	if (bind(pdata, (struct sockaddr *)&pasv_addr, len) < 0)
26254dd8b5abSYoshinobu Inoue 		goto pasv_error;
26264dd8b5abSYoshinobu Inoue 
26274dd8b5abSYoshinobu Inoue 	(void) seteuid((uid_t)pw->pw_uid);
26284dd8b5abSYoshinobu Inoue 
26294dd8b5abSYoshinobu Inoue 	if (getsockname(pdata, (struct sockaddr *) &pasv_addr, &len) < 0)
26304dd8b5abSYoshinobu Inoue 		goto pasv_error;
26314dd8b5abSYoshinobu Inoue 	if (listen(pdata, 1) < 0)
26324dd8b5abSYoshinobu Inoue 		goto pasv_error;
26334dd8b5abSYoshinobu Inoue 
26344dd8b5abSYoshinobu Inoue #define UC(b) (((int) b) & 0xff)
26354dd8b5abSYoshinobu Inoue 
26364dd8b5abSYoshinobu Inoue 	if (strcmp(cmd, "LPSV") == 0) {
26374dd8b5abSYoshinobu Inoue 		p = (char *)&pasv_addr.su_port;
26384dd8b5abSYoshinobu Inoue 		switch (pasv_addr.su_family) {
26394dd8b5abSYoshinobu Inoue 		case AF_INET:
26404dd8b5abSYoshinobu Inoue 			a = (char *) &pasv_addr.su_sin.sin_addr;
26414dd8b5abSYoshinobu Inoue 		v4_reply:
26424dd8b5abSYoshinobu Inoue 			reply(228,
26434dd8b5abSYoshinobu Inoue "Entering Long Passive Mode (%d,%d,%d,%d,%d,%d,%d,%d,%d)",
26444dd8b5abSYoshinobu Inoue 			      4, 4, UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
26454dd8b5abSYoshinobu Inoue 			      2, UC(p[0]), UC(p[1]));
26464dd8b5abSYoshinobu Inoue 			return;
26474dd8b5abSYoshinobu Inoue 		case AF_INET6:
26484dd8b5abSYoshinobu Inoue 			if (IN6_IS_ADDR_V4MAPPED(&pasv_addr.su_sin6.sin6_addr)) {
26494dd8b5abSYoshinobu Inoue 				a = (char *) &pasv_addr.su_sin6.sin6_addr.s6_addr[12];
26504dd8b5abSYoshinobu Inoue 				goto v4_reply;
26514dd8b5abSYoshinobu Inoue 			}
26524dd8b5abSYoshinobu Inoue 			a = (char *) &pasv_addr.su_sin6.sin6_addr;
26534dd8b5abSYoshinobu Inoue 			reply(228,
26544dd8b5abSYoshinobu Inoue "Entering Long Passive Mode "
26554dd8b5abSYoshinobu Inoue "(%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d)",
26564dd8b5abSYoshinobu Inoue 			      6, 16, UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
26574dd8b5abSYoshinobu Inoue 			      UC(a[4]), UC(a[5]), UC(a[6]), UC(a[7]),
26584dd8b5abSYoshinobu Inoue 			      UC(a[8]), UC(a[9]), UC(a[10]), UC(a[11]),
26594dd8b5abSYoshinobu Inoue 			      UC(a[12]), UC(a[13]), UC(a[14]), UC(a[15]),
26604dd8b5abSYoshinobu Inoue 			      2, UC(p[0]), UC(p[1]));
26614dd8b5abSYoshinobu Inoue 			return;
26624dd8b5abSYoshinobu Inoue 		}
26634dd8b5abSYoshinobu Inoue 	} else if (strcmp(cmd, "EPSV") == 0) {
26644dd8b5abSYoshinobu Inoue 		switch (pasv_addr.su_family) {
26654dd8b5abSYoshinobu Inoue 		case AF_INET:
26664dd8b5abSYoshinobu Inoue 		case AF_INET6:
26674dd8b5abSYoshinobu Inoue 			reply(229, "Entering Extended Passive Mode (|||%d|)",
26684dd8b5abSYoshinobu Inoue 				ntohs(pasv_addr.su_port));
26694dd8b5abSYoshinobu Inoue 			return;
26704dd8b5abSYoshinobu Inoue 		}
26714dd8b5abSYoshinobu Inoue 	} else {
26724dd8b5abSYoshinobu Inoue 		/* more proper error code? */
26734dd8b5abSYoshinobu Inoue 	}
26744dd8b5abSYoshinobu Inoue 
26754dd8b5abSYoshinobu Inoue pasv_error:
26764dd8b5abSYoshinobu Inoue 	(void) seteuid((uid_t)pw->pw_uid);
26774dd8b5abSYoshinobu Inoue 	(void) close(pdata);
26784dd8b5abSYoshinobu Inoue 	pdata = -1;
26794dd8b5abSYoshinobu Inoue 	perror_reply(425, "Can't open passive connection");
26804dd8b5abSYoshinobu Inoue 	return;
26814dd8b5abSYoshinobu Inoue }
26824dd8b5abSYoshinobu Inoue 
26834dd8b5abSYoshinobu Inoue /*
2684ea022d16SRodney W. Grimes  * Generate unique name for file with basename "local".
2685ea022d16SRodney W. Grimes  * The file named "local" is already known to exist.
2686ea022d16SRodney W. Grimes  * Generates failure reply on error.
2687ea022d16SRodney W. Grimes  */
2688ea022d16SRodney W. Grimes static char *
2689ea022d16SRodney W. Grimes gunique(local)
2690ea022d16SRodney W. Grimes 	char *local;
2691ea022d16SRodney W. Grimes {
2692ea022d16SRodney W. Grimes 	static char new[MAXPATHLEN];
2693ea022d16SRodney W. Grimes 	struct stat st;
2694ea022d16SRodney W. Grimes 	int count;
2695ea022d16SRodney W. Grimes 	char *cp;
2696ea022d16SRodney W. Grimes 
2697ea022d16SRodney W. Grimes 	cp = strrchr(local, '/');
2698ea022d16SRodney W. Grimes 	if (cp)
2699ea022d16SRodney W. Grimes 		*cp = '\0';
2700ea022d16SRodney W. Grimes 	if (stat(cp ? local : ".", &st) < 0) {
2701ea022d16SRodney W. Grimes 		perror_reply(553, cp ? local : ".");
2702ea022d16SRodney W. Grimes 		return ((char *) 0);
2703ea022d16SRodney W. Grimes 	}
2704ea022d16SRodney W. Grimes 	if (cp)
2705ea022d16SRodney W. Grimes 		*cp = '/';
2706e760ef2cSWarner Losh 	/* -4 is for the .nn<null> we put on the end below */
2707e760ef2cSWarner Losh 	(void) snprintf(new, sizeof(new) - 4, "%s", local);
2708ea022d16SRodney W. Grimes 	cp = new + strlen(new);
2709ea022d16SRodney W. Grimes 	*cp++ = '.';
2710ea022d16SRodney W. Grimes 	for (count = 1; count < 100; count++) {
2711ea022d16SRodney W. Grimes 		(void)sprintf(cp, "%d", count);
2712ea022d16SRodney W. Grimes 		if (stat(new, &st) < 0)
2713ea022d16SRodney W. Grimes 			return (new);
2714ea022d16SRodney W. Grimes 	}
2715ea022d16SRodney W. Grimes 	reply(452, "Unique file name cannot be created.");
2716ea022d16SRodney W. Grimes 	return (NULL);
2717ea022d16SRodney W. Grimes }
2718ea022d16SRodney W. Grimes 
2719ea022d16SRodney W. Grimes /*
2720ea022d16SRodney W. Grimes  * Format and send reply containing system error number.
2721ea022d16SRodney W. Grimes  */
2722ea022d16SRodney W. Grimes void
2723ea022d16SRodney W. Grimes perror_reply(code, string)
2724ea022d16SRodney W. Grimes 	int code;
2725ea022d16SRodney W. Grimes 	char *string;
2726ea022d16SRodney W. Grimes {
2727ea022d16SRodney W. Grimes 
2728ea022d16SRodney W. Grimes 	reply(code, "%s: %s.", string, strerror(errno));
2729ea022d16SRodney W. Grimes }
2730ea022d16SRodney W. Grimes 
2731ea022d16SRodney W. Grimes static char *onefile[] = {
2732ea022d16SRodney W. Grimes 	"",
2733ea022d16SRodney W. Grimes 	0
2734ea022d16SRodney W. Grimes };
2735ea022d16SRodney W. Grimes 
2736ea022d16SRodney W. Grimes void
2737ea022d16SRodney W. Grimes send_file_list(whichf)
2738ea022d16SRodney W. Grimes 	char *whichf;
2739ea022d16SRodney W. Grimes {
2740ea022d16SRodney W. Grimes 	struct stat st;
2741ea022d16SRodney W. Grimes 	DIR *dirp = NULL;
2742ea022d16SRodney W. Grimes 	struct dirent *dir;
2743ea022d16SRodney W. Grimes 	FILE *dout = NULL;
2744ea022d16SRodney W. Grimes 	char **dirlist, *dirname;
2745ea022d16SRodney W. Grimes 	int simple = 0;
2746ea022d16SRodney W. Grimes 	int freeglob = 0;
2747ea022d16SRodney W. Grimes 	glob_t gl;
2748ea022d16SRodney W. Grimes 
2749ea022d16SRodney W. Grimes 	if (strpbrk(whichf, "~{[*?") != NULL) {
2750ea022d16SRodney W. Grimes 		int flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_QUOTE|GLOB_TILDE;
2751ea022d16SRodney W. Grimes 
2752ea022d16SRodney W. Grimes 		memset(&gl, 0, sizeof(gl));
27536d10cb2fSJonathan Lemon 		gl.gl_matchc = MAXGLOBARGS;
275475dc5f1aSMike Heffner 		flags |= GLOB_LIMIT;
2755ea022d16SRodney W. Grimes 		freeglob = 1;
2756ea022d16SRodney W. Grimes 		if (glob(whichf, flags, 0, &gl)) {
2757ea022d16SRodney W. Grimes 			reply(550, "not found");
2758ea022d16SRodney W. Grimes 			goto out;
2759ea022d16SRodney W. Grimes 		} else if (gl.gl_pathc == 0) {
2760ea022d16SRodney W. Grimes 			errno = ENOENT;
2761ea022d16SRodney W. Grimes 			perror_reply(550, whichf);
2762ea022d16SRodney W. Grimes 			goto out;
2763ea022d16SRodney W. Grimes 		}
2764ea022d16SRodney W. Grimes 		dirlist = gl.gl_pathv;
2765ea022d16SRodney W. Grimes 	} else {
2766ea022d16SRodney W. Grimes 		onefile[0] = whichf;
2767ea022d16SRodney W. Grimes 		dirlist = onefile;
2768ea022d16SRodney W. Grimes 		simple = 1;
2769ea022d16SRodney W. Grimes 	}
2770ea022d16SRodney W. Grimes 
27719aca17cbSMark Murray 	while ((dirname = *dirlist++)) {
2772ea022d16SRodney W. Grimes 		if (stat(dirname, &st) < 0) {
2773ea022d16SRodney W. Grimes 			/*
2774ea022d16SRodney W. Grimes 			 * If user typed "ls -l", etc, and the client
2775ea022d16SRodney W. Grimes 			 * used NLST, do what the user meant.
2776ea022d16SRodney W. Grimes 			 */
2777ea022d16SRodney W. Grimes 			if (dirname[0] == '-' && *dirlist == NULL &&
2778ea022d16SRodney W. Grimes 			    transflag == 0) {
2779af85d782SDavid Nugent 				retrieve(_PATH_LS " %s", dirname);
2780ea022d16SRodney W. Grimes 				goto out;
2781ea022d16SRodney W. Grimes 			}
2782ea022d16SRodney W. Grimes 			perror_reply(550, whichf);
2783ea022d16SRodney W. Grimes 			if (dout != NULL) {
2784ea022d16SRodney W. Grimes 				(void) fclose(dout);
2785ea022d16SRodney W. Grimes 				transflag = 0;
2786ea022d16SRodney W. Grimes 				data = -1;
2787ea022d16SRodney W. Grimes 				pdata = -1;
2788ea022d16SRodney W. Grimes 			}
2789ea022d16SRodney W. Grimes 			goto out;
2790ea022d16SRodney W. Grimes 		}
2791ea022d16SRodney W. Grimes 
2792ea022d16SRodney W. Grimes 		if (S_ISREG(st.st_mode)) {
2793ea022d16SRodney W. Grimes 			if (dout == NULL) {
2794ea022d16SRodney W. Grimes 				dout = dataconn("file list", (off_t)-1, "w");
2795ea022d16SRodney W. Grimes 				if (dout == NULL)
2796ea022d16SRodney W. Grimes 					goto out;
2797ea022d16SRodney W. Grimes 				transflag++;
2798ea022d16SRodney W. Grimes 			}
2799ea022d16SRodney W. Grimes 			fprintf(dout, "%s%s\n", dirname,
2800ea022d16SRodney W. Grimes 				type == TYPE_A ? "\r" : "");
2801ea022d16SRodney W. Grimes 			byte_count += strlen(dirname) + 1;
2802ea022d16SRodney W. Grimes 			continue;
2803ea022d16SRodney W. Grimes 		} else if (!S_ISDIR(st.st_mode))
2804ea022d16SRodney W. Grimes 			continue;
2805ea022d16SRodney W. Grimes 
2806ea022d16SRodney W. Grimes 		if ((dirp = opendir(dirname)) == NULL)
2807ea022d16SRodney W. Grimes 			continue;
2808ea022d16SRodney W. Grimes 
2809ea022d16SRodney W. Grimes 		while ((dir = readdir(dirp)) != NULL) {
2810ea022d16SRodney W. Grimes 			char nbuf[MAXPATHLEN];
2811ea022d16SRodney W. Grimes 
28124b82fc95SYaroslav Tykhiy 			if (recvurg) {
28134b82fc95SYaroslav Tykhiy 				myoob();
28144b82fc95SYaroslav Tykhiy 				recvurg = 0;
28154b82fc95SYaroslav Tykhiy 				transflag = 0;
28164b82fc95SYaroslav Tykhiy 				goto out;
28174b82fc95SYaroslav Tykhiy 			}
28184b82fc95SYaroslav Tykhiy 
2819ea022d16SRodney W. Grimes 			if (dir->d_name[0] == '.' && dir->d_namlen == 1)
2820ea022d16SRodney W. Grimes 				continue;
2821ea022d16SRodney W. Grimes 			if (dir->d_name[0] == '.' && dir->d_name[1] == '.' &&
2822ea022d16SRodney W. Grimes 			    dir->d_namlen == 2)
2823ea022d16SRodney W. Grimes 				continue;
2824ea022d16SRodney W. Grimes 
2825e760ef2cSWarner Losh 			snprintf(nbuf, sizeof(nbuf),
2826e760ef2cSWarner Losh 				"%s/%s", dirname, dir->d_name);
2827ea022d16SRodney W. Grimes 
2828ea022d16SRodney W. Grimes 			/*
2829ea022d16SRodney W. Grimes 			 * We have to do a stat to insure it's
2830ea022d16SRodney W. Grimes 			 * not a directory or special file.
2831ea022d16SRodney W. Grimes 			 */
2832ea022d16SRodney W. Grimes 			if (simple || (stat(nbuf, &st) == 0 &&
2833ea022d16SRodney W. Grimes 			    S_ISREG(st.st_mode))) {
2834ea022d16SRodney W. Grimes 				if (dout == NULL) {
2835ea022d16SRodney W. Grimes 					dout = dataconn("file list", (off_t)-1,
2836ea022d16SRodney W. Grimes 						"w");
2837ea022d16SRodney W. Grimes 					if (dout == NULL)
2838ea022d16SRodney W. Grimes 						goto out;
2839ea022d16SRodney W. Grimes 					transflag++;
2840ea022d16SRodney W. Grimes 				}
2841ea022d16SRodney W. Grimes 				if (nbuf[0] == '.' && nbuf[1] == '/')
2842ea022d16SRodney W. Grimes 					fprintf(dout, "%s%s\n", &nbuf[2],
2843ea022d16SRodney W. Grimes 						type == TYPE_A ? "\r" : "");
2844ea022d16SRodney W. Grimes 				else
2845ea022d16SRodney W. Grimes 					fprintf(dout, "%s%s\n", nbuf,
2846ea022d16SRodney W. Grimes 						type == TYPE_A ? "\r" : "");
2847ea022d16SRodney W. Grimes 				byte_count += strlen(nbuf) + 1;
2848ea022d16SRodney W. Grimes 			}
2849ea022d16SRodney W. Grimes 		}
2850ea022d16SRodney W. Grimes 		(void) closedir(dirp);
2851ea022d16SRodney W. Grimes 	}
2852ea022d16SRodney W. Grimes 
2853ea022d16SRodney W. Grimes 	if (dout == NULL)
2854ea022d16SRodney W. Grimes 		reply(550, "No files found.");
2855ea022d16SRodney W. Grimes 	else if (ferror(dout) != 0)
2856ea022d16SRodney W. Grimes 		perror_reply(550, "Data connection");
2857ea022d16SRodney W. Grimes 	else
2858ea022d16SRodney W. Grimes 		reply(226, "Transfer complete.");
2859ea022d16SRodney W. Grimes 
2860ea022d16SRodney W. Grimes 	transflag = 0;
2861ea022d16SRodney W. Grimes 	if (dout != NULL)
2862ea022d16SRodney W. Grimes 		(void) fclose(dout);
2863ea022d16SRodney W. Grimes 	data = -1;
2864ea022d16SRodney W. Grimes 	pdata = -1;
2865ea022d16SRodney W. Grimes out:
2866ea022d16SRodney W. Grimes 	if (freeglob) {
2867ea022d16SRodney W. Grimes 		freeglob = 0;
2868ea022d16SRodney W. Grimes 		globfree(&gl);
2869ea022d16SRodney W. Grimes 	}
2870ea022d16SRodney W. Grimes }
2871ea022d16SRodney W. Grimes 
2872cf09a206SDavid Greenman void
2873cf09a206SDavid Greenman reapchild(signo)
2874cf09a206SDavid Greenman 	int signo;
2875cf09a206SDavid Greenman {
2876cf09a206SDavid Greenman 	while (wait3(NULL, WNOHANG, NULL) > 0);
2877cf09a206SDavid Greenman }
2878cf09a206SDavid Greenman 
2879b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE
2880ea022d16SRodney W. Grimes /*
2881ea022d16SRodney W. Grimes  * Clobber argv so ps will show what we're doing.  (Stolen from sendmail.)
2882ea022d16SRodney W. Grimes  * Warning, since this is usually started from inetd.conf, it often doesn't
2883ea022d16SRodney W. Grimes  * have much of an environment or arglist to overwrite.
2884ea022d16SRodney W. Grimes  */
2885ea022d16SRodney W. Grimes void
2886ea022d16SRodney W. Grimes #if __STDC__
2887ea022d16SRodney W. Grimes setproctitle(const char *fmt, ...)
2888ea022d16SRodney W. Grimes #else
2889ea022d16SRodney W. Grimes setproctitle(fmt, va_alist)
2890ea022d16SRodney W. Grimes 	char *fmt;
2891ea022d16SRodney W. Grimes         va_dcl
2892ea022d16SRodney W. Grimes #endif
2893ea022d16SRodney W. Grimes {
2894ea022d16SRodney W. Grimes 	int i;
2895ea022d16SRodney W. Grimes 	va_list ap;
2896ea022d16SRodney W. Grimes 	char *p, *bp, ch;
2897ea022d16SRodney W. Grimes 	char buf[LINE_MAX];
2898ea022d16SRodney W. Grimes 
2899ea022d16SRodney W. Grimes #if __STDC__
2900ea022d16SRodney W. Grimes 	va_start(ap, fmt);
2901ea022d16SRodney W. Grimes #else
2902ea022d16SRodney W. Grimes 	va_start(ap);
2903ea022d16SRodney W. Grimes #endif
2904ea022d16SRodney W. Grimes 	(void)vsnprintf(buf, sizeof(buf), fmt, ap);
2905ea022d16SRodney W. Grimes 
2906ea022d16SRodney W. Grimes 	/* make ps print our process name */
2907ea022d16SRodney W. Grimes 	p = Argv[0];
2908ea022d16SRodney W. Grimes 	*p++ = '-';
2909ea022d16SRodney W. Grimes 
2910ea022d16SRodney W. Grimes 	i = strlen(buf);
2911ea022d16SRodney W. Grimes 	if (i > LastArgv - p - 2) {
2912ea022d16SRodney W. Grimes 		i = LastArgv - p - 2;
2913ea022d16SRodney W. Grimes 		buf[i] = '\0';
2914ea022d16SRodney W. Grimes 	}
2915ea022d16SRodney W. Grimes 	bp = buf;
2916ea022d16SRodney W. Grimes 	while (ch = *bp++)
2917ea022d16SRodney W. Grimes 		if (ch != '\n' && ch != '\r')
2918ea022d16SRodney W. Grimes 			*p++ = ch;
2919ea022d16SRodney W. Grimes 	while (p < LastArgv)
2920ea022d16SRodney W. Grimes 		*p++ = ' ';
2921ea022d16SRodney W. Grimes }
2922b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */
29233eb568f2SGuido van Rooij 
292461f891a6SPaul Traina static void
29253eb568f2SGuido van Rooij logxfer(name, size, start)
29263eb568f2SGuido van Rooij 	char *name;
2927e4a71114SAndrey A. Chernov 	off_t size;
2928e4a71114SAndrey A. Chernov 	time_t start;
29293eb568f2SGuido van Rooij {
29303eb568f2SGuido van Rooij 	char buf[1024];
29313eb568f2SGuido van Rooij 	char path[MAXPATHLEN + 1];
2932158a00b2SJohn Birrell 	time_t now;
29333eb568f2SGuido van Rooij 
29343eb568f2SGuido van Rooij 	if (statfd >= 0 && getwd(path) != NULL) {
29353eb568f2SGuido van Rooij 		time(&now);
2936e4a71114SAndrey A. Chernov 		snprintf(buf, sizeof(buf), "%.20s!%s!%s!%s/%s!%qd!%ld\n",
29373eb568f2SGuido van Rooij 			ctime(&now)+4, ident, remotehost,
2938e4a71114SAndrey A. Chernov 			path, name, (long long)size,
2939e4a71114SAndrey A. Chernov 			(long)(now - start + (now == start)));
29403eb568f2SGuido van Rooij 		write(statfd, buf, strlen(buf));
29413eb568f2SGuido van Rooij 	}
29423eb568f2SGuido van Rooij }
2943