xref: /freebsd/libexec/ftpd/ftpd.c (revision 04683b2c3564b9adf076a9ee8b3f221fbfab6bd7)
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 #endif /* not lint */
47ea022d16SRodney W. Grimes 
480c4b401fSYaroslav Tykhiy #include <sys/cdefs.h>
490c4b401fSYaroslav Tykhiy __FBSDID("$FreeBSD$");
500c4b401fSYaroslav Tykhiy 
51ea022d16SRodney W. Grimes /*
52ea022d16SRodney W. Grimes  * FTP server.
53ea022d16SRodney W. Grimes  */
54ea022d16SRodney W. Grimes #include <sys/param.h>
55ea022d16SRodney W. Grimes #include <sys/ioctl.h>
569fc5823aSGarrett Wollman #include <sys/mman.h>
57eb2fc780SGarrett Wollman #include <sys/socket.h>
58eb2fc780SGarrett Wollman #include <sys/stat.h>
59eb2fc780SGarrett Wollman #include <sys/time.h>
60eb2fc780SGarrett Wollman #include <sys/wait.h>
61ea022d16SRodney W. Grimes 
62ea022d16SRodney W. Grimes #include <netinet/in.h>
63ea022d16SRodney W. Grimes #include <netinet/in_systm.h>
64ea022d16SRodney W. Grimes #include <netinet/ip.h>
659fc5823aSGarrett Wollman #include <netinet/tcp.h>
66ea022d16SRodney W. Grimes 
67ea022d16SRodney W. Grimes #define	FTP_NAMES
68ea022d16SRodney W. Grimes #include <arpa/ftp.h>
69ea022d16SRodney W. Grimes #include <arpa/inet.h>
70ea022d16SRodney W. Grimes #include <arpa/telnet.h>
71ea022d16SRodney W. Grimes 
72ea022d16SRodney W. Grimes #include <ctype.h>
73ea022d16SRodney W. Grimes #include <dirent.h>
74ea022d16SRodney W. Grimes #include <err.h>
75ea022d16SRodney W. Grimes #include <errno.h>
76ea022d16SRodney W. Grimes #include <fcntl.h>
77ea022d16SRodney W. Grimes #include <glob.h>
78ea022d16SRodney W. Grimes #include <limits.h>
79ea022d16SRodney W. Grimes #include <netdb.h>
80ea022d16SRodney W. Grimes #include <pwd.h>
8131fea7b8SDavid Nugent #include <grp.h>
8247499ecdSAndrey A. Chernov #include <opie.h>
83ea022d16SRodney W. Grimes #include <signal.h>
84a57e1ef0SYaroslav Tykhiy #include <stdint.h>
85ea022d16SRodney W. Grimes #include <stdio.h>
86ea022d16SRodney W. Grimes #include <stdlib.h>
87ea022d16SRodney W. Grimes #include <string.h>
88ea022d16SRodney W. Grimes #include <syslog.h>
89ea022d16SRodney W. Grimes #include <time.h>
90ea022d16SRodney W. Grimes #include <unistd.h>
91b63e1fe2SPeter Wemm #include <libutil.h>
92b071c689SDavid Nugent #ifdef	LOGIN_CAP
93b071c689SDavid Nugent #include <login_cap.h>
94b071c689SDavid Nugent #endif
95ea022d16SRodney W. Grimes 
965bc9d93dSMark Murray #ifdef USE_PAM
976c9134c0SMark Murray #include <security/pam_appl.h>
986c9134c0SMark Murray #endif
996c9134c0SMark Murray 
100ea022d16SRodney W. Grimes #include "pathnames.h"
101ea022d16SRodney W. Grimes #include "extern.h"
102ea022d16SRodney W. Grimes 
103ea022d16SRodney W. Grimes #include <stdarg.h>
104ea022d16SRodney W. Grimes 
105af85d782SDavid Nugent static char version[] = "Version 6.00LS";
106af85d782SDavid Nugent #undef main
107ea022d16SRodney W. Grimes 
108ea022d16SRodney W. Grimes extern	off_t restart_point;
109ea022d16SRodney W. Grimes extern	char cbuf[];
110ea022d16SRodney W. Grimes 
1114dd8b5abSYoshinobu Inoue union sockunion ctrl_addr;
1124dd8b5abSYoshinobu Inoue union sockunion data_source;
1134dd8b5abSYoshinobu Inoue union sockunion data_dest;
1144dd8b5abSYoshinobu Inoue union sockunion his_addr;
1154dd8b5abSYoshinobu Inoue union sockunion pasv_addr;
116ea022d16SRodney W. Grimes 
117cf09a206SDavid Greenman int	daemon_mode;
118ea022d16SRodney W. Grimes int	data;
11963591ba5SYaroslav Tykhiy int	dataport;
120c152df28SYaroslav Tykhiy int	hostinfo = 1;	/* print host-specific info in messages */
121ea022d16SRodney W. Grimes int	logged_in;
122ea022d16SRodney W. Grimes struct	passwd *pw;
123ce9287fcSYaroslav Tykhiy char	*homedir;
124618b0bbaSMark Murray int	ftpdebug;
125ea022d16SRodney W. Grimes int	timeout = 900;    /* timeout after 15 minutes of inactivity */
126ea022d16SRodney W. Grimes int	maxtimeout = 7200;/* don't allow idle time to be set beyond 2 hours */
127ea022d16SRodney W. Grimes int	logging;
1284c450ad7SPaul Traina int	restricted_data_ports = 1;
129a5a4544eSPaul Traina int	paranoid = 1;	  /* be extra careful about security */
1305a392aecSTorsten Blum int	anon_only = 0;    /* Only anonymous ftp allowed */
131ea022d16SRodney W. Grimes int	guest;
132a5a4544eSPaul Traina int	dochroot;
133215a9f9dSYaroslav Tykhiy char	*chrootdir;
1345d7e0128SYaroslav Tykhiy int	dowtmp = 1;
1353eb568f2SGuido van Rooij int	stats;
1363eb568f2SGuido van Rooij int	statfd = -1;
137ea022d16SRodney W. Grimes int	type;
138ea022d16SRodney W. Grimes int	form;
139ea022d16SRodney W. Grimes int	stru;			/* avoid C keyword */
140ea022d16SRodney W. Grimes int	mode;
141ea022d16SRodney W. Grimes int	usedefault = 1;		/* for data transfers */
142ea022d16SRodney W. Grimes int	pdata = -1;		/* for passive mode */
143a4b77a2aSPoul-Henning Kamp int	readonly = 0;		/* Server is in readonly mode.	*/
144a4b77a2aSPoul-Henning Kamp int	noepsv = 0;		/* EPSV command is disabled.	*/
14562513e76SNik Clayton int	noretr = 0;		/* RETR command is disabled.	*/
1461cc9f0bbSSheldon Hearn int	noguestretr = 0;	/* RETR command is disabled for anon users. */
147d186bb12SMatthew N. Dodd int	noguestmkd = 0;		/* MKD command is disabled for anon users. */
148a117c345SYaroslav Tykhiy int	noguestmod = 1;		/* anon users may not modify existing files. */
14962513e76SNik Clayton 
1504b82fc95SYaroslav Tykhiy static volatile sig_atomic_t recvurg;
151ea022d16SRodney W. Grimes sig_atomic_t transflag;
152ea022d16SRodney W. Grimes off_t	file_size;
153ea022d16SRodney W. Grimes off_t	byte_count;
154ea022d16SRodney W. Grimes #if !defined(CMASK) || CMASK == 0
155ea022d16SRodney W. Grimes #undef CMASK
156ea022d16SRodney W. Grimes #define CMASK 027
157ea022d16SRodney W. Grimes #endif
158ea022d16SRodney W. Grimes int	defumask = CMASK;		/* default umask value */
159ea022d16SRodney W. Grimes char	tmpline[7];
160ea4e54b9SDavid Nugent char	*hostname;
161ad442344SDima Dorfman int	epsvall = 0;
162ad442344SDima Dorfman 
1630512556aSDavid Nugent #ifdef VIRTUAL_HOSTING
164ea4e54b9SDavid Nugent char	*ftpuser;
165ea4e54b9SDavid Nugent 
166ea4e54b9SDavid Nugent static struct ftphost {
167ea4e54b9SDavid Nugent 	struct ftphost	*next;
168f38c6cadSYoshinobu Inoue 	struct addrinfo *hostinfo;
169ea4e54b9SDavid Nugent 	char		*hostname;
170ea4e54b9SDavid Nugent 	char		*anonuser;
171ea4e54b9SDavid Nugent 	char		*statfile;
172ea4e54b9SDavid Nugent 	char		*welcome;
173ea4e54b9SDavid Nugent 	char		*loginmsg;
174ea4e54b9SDavid Nugent } *thishost, *firsthost;
175ea4e54b9SDavid Nugent 
176ea4e54b9SDavid Nugent #endif
17704683b2cSYaroslav Tykhiy char	remotehost[NI_MAXHOST];
1783eb568f2SGuido van Rooij char	*ident = NULL;
179a5a4544eSPaul Traina 
180a5a4544eSPaul Traina static char	ttyline[20];
181a5a4544eSPaul Traina char		*tty = ttyline;		/* for klogin */
182a5a4544eSPaul Traina 
1835bc9d93dSMark Murray #ifdef USE_PAM
184e4bc453cSWarner Losh static int	auth_pam(struct passwd**, const char*);
1855bc9d93dSMark Murray pam_handle_t	*pamh = NULL;
18647499ecdSAndrey A. Chernov #endif
187fa1746c9SMark Murray 
188fa1746c9SMark Murray static struct opie	opiedata;
189fa1746c9SMark Murray static char		opieprompt[OPIE_CHALLENGE_MAX+1];
19047499ecdSAndrey A. Chernov static int		pwok;
1919aca17cbSMark Murray 
192105a3c98SJulian Elischer char	*pid_file = NULL;
193105a3c98SJulian Elischer 
194ea022d16SRodney W. Grimes /*
1956d10cb2fSJonathan Lemon  * Limit number of pathnames that glob can return.
1966d10cb2fSJonathan Lemon  * A limit of 0 indicates the number of pathnames is unlimited.
1976d10cb2fSJonathan Lemon  */
1986d10cb2fSJonathan Lemon #define MAXGLOBARGS	16384
1996d10cb2fSJonathan Lemon #
2006d10cb2fSJonathan Lemon 
2016d10cb2fSJonathan Lemon /*
202ea022d16SRodney W. Grimes  * Timeout intervals for retrying connections
203ea022d16SRodney W. Grimes  * to hosts that don't accept PORT cmds.  This
204ea022d16SRodney W. Grimes  * is a kludge, but given the problems with TCP...
205ea022d16SRodney W. Grimes  */
206ea022d16SRodney W. Grimes #define	SWAITMAX	90	/* wait at most 90 seconds */
207ea022d16SRodney W. Grimes #define	SWAITINT	5	/* interval between retries */
208ea022d16SRodney W. Grimes 
209ea022d16SRodney W. Grimes int	swaitmax = SWAITMAX;
210ea022d16SRodney W. Grimes int	swaitint = SWAITINT;
211ea022d16SRodney W. Grimes 
212ea022d16SRodney W. Grimes #ifdef SETPROCTITLE
213b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE
214ea022d16SRodney W. Grimes char	**Argv = NULL;		/* pointer to argument vector */
215ea022d16SRodney W. Grimes char	*LastArgv = NULL;	/* end of argv */
216b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */
217ea022d16SRodney W. Grimes char	proctitle[LINE_MAX];	/* initial part of title */
218ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */
219ea022d16SRodney W. Grimes 
2206b2dee6bSYaroslav Tykhiy #define LOGCMD(cmd, file)		logcmd((cmd), (file), NULL, -1)
2216b2dee6bSYaroslav Tykhiy #define LOGCMD2(cmd, file1, file2)	logcmd((cmd), (file1), (file2), -1)
2226b2dee6bSYaroslav Tykhiy #define LOGBYTES(cmd, file, cnt)	logcmd((cmd), (file), NULL, (cnt))
223ea022d16SRodney W. Grimes 
224ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
225e4bc453cSWarner Losh static void	 inithosts(void);
226e4bc453cSWarner Losh static void	 selecthost(union sockunion *);
227ea4e54b9SDavid Nugent #endif
228e4bc453cSWarner Losh static void	 ack(char *);
229e4bc453cSWarner Losh static void	 sigurg(int);
230e4bc453cSWarner Losh static void	 myoob(void);
2318657b576SYaroslav Tykhiy static int	 checkuser(char *, char *, int, char **);
232e4bc453cSWarner Losh static FILE	*dataconn(char *, off_t, char *);
233e4bc453cSWarner Losh static void	 dolog(struct sockaddr *);
234e4bc453cSWarner Losh static void	 end_login(void);
235e4bc453cSWarner Losh static FILE	*getdatasock(char *);
236a117c345SYaroslav Tykhiy static int	 guniquefd(char *, char **);
237e4bc453cSWarner Losh static void	 lostconn(int);
238e4bc453cSWarner Losh static void	 sigquit(int);
239e4bc453cSWarner Losh static int	 receive_data(FILE *, FILE *);
2406e4b0a55SYaroslav Tykhiy static int	 send_data(FILE *, FILE *, size_t, off_t, int);
241ea022d16SRodney W. Grimes static struct passwd *
242e4bc453cSWarner Losh 		 sgetpwnam(char *);
243e4bc453cSWarner Losh static char	*sgetsave(char *);
244e4bc453cSWarner Losh static void	 reapchild(int);
245eb5b2bb3SYaroslav Tykhiy static void	 appendf(char **, char *, ...) __printflike(2, 3);
2466b2dee6bSYaroslav Tykhiy static void	 logcmd(char *, char *, char *, off_t);
247e4bc453cSWarner Losh static void      logxfer(char *, off_t, time_t);
248e4648f05SYaroslav Tykhiy static char	*doublequote(char *);
249206fe568SHajimu UMEMOTO static int	*socksetup(int, char *, const char *);
250ea022d16SRodney W. Grimes 
251ea022d16SRodney W. Grimes int
252e4bc453cSWarner Losh main(int argc, char *argv[], char **envp)
253ea022d16SRodney W. Grimes {
254ea022d16SRodney W. Grimes 	int addrlen, ch, on = 1, tos;
255ea022d16SRodney W. Grimes 	char *cp, line[LINE_MAX];
256ea022d16SRodney W. Grimes 	FILE *fd;
2574dd8b5abSYoshinobu Inoue 	char	*bindname = NULL;
25863591ba5SYaroslav Tykhiy 	const char *bindport = "ftp";
2594dd8b5abSYoshinobu Inoue 	int	family = AF_UNSPEC;
2604b82fc95SYaroslav Tykhiy 	struct sigaction sa;
261ea022d16SRodney W. Grimes 
26234d1ba5cSAndrey A. Chernov 	tzset();		/* in case no timezone database in ~ftp */
2634b82fc95SYaroslav Tykhiy 	sigemptyset(&sa.sa_mask);
2644b82fc95SYaroslav Tykhiy 	sa.sa_flags = SA_RESTART;
26534d1ba5cSAndrey A. Chernov 
266b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE
267ea022d16SRodney W. Grimes 	/*
268ea022d16SRodney W. Grimes 	 *  Save start and extent of argv for setproctitle.
269ea022d16SRodney W. Grimes 	 */
270ea022d16SRodney W. Grimes 	Argv = argv;
271ea022d16SRodney W. Grimes 	while (*envp)
272ea022d16SRodney W. Grimes 		envp++;
273ea022d16SRodney W. Grimes 	LastArgv = envp[-1] + strlen(envp[-1]);
274b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */
275ea022d16SRodney W. Grimes 
2763eb568f2SGuido van Rooij 
277c152df28SYaroslav Tykhiy 	while ((ch = getopt(argc, argv,
278c152df28SYaroslav Tykhiy 	                    "46a:AdDEhlmMoOp:P:rRSt:T:u:UvW")) != -1) {
279ea022d16SRodney W. Grimes 		switch (ch) {
2800e063efeSYaroslav Tykhiy 		case '4':
281206fe568SHajimu UMEMOTO 			family = (family == AF_INET6) ? AF_UNSPEC : AF_INET;
2820e063efeSYaroslav Tykhiy 			break;
2830e063efeSYaroslav Tykhiy 
2840e063efeSYaroslav Tykhiy 		case '6':
285206fe568SHajimu UMEMOTO 			family = (family == AF_INET) ? AF_UNSPEC : AF_INET6;
2860e063efeSYaroslav Tykhiy 			break;
2870e063efeSYaroslav Tykhiy 
2880e063efeSYaroslav Tykhiy 		case 'a':
2890e063efeSYaroslav Tykhiy 			bindname = optarg;
2900e063efeSYaroslav Tykhiy 			break;
2910e063efeSYaroslav Tykhiy 
2920e063efeSYaroslav Tykhiy 		case 'A':
2930e063efeSYaroslav Tykhiy 			anon_only = 1;
294cf09a206SDavid Greenman 			break;
295cf09a206SDavid Greenman 
296ea022d16SRodney W. Grimes 		case 'd':
297618b0bbaSMark Murray 			ftpdebug++;
298ea022d16SRodney W. Grimes 			break;
299ea022d16SRodney W. Grimes 
3000e063efeSYaroslav Tykhiy 		case 'D':
3010e063efeSYaroslav Tykhiy 			daemon_mode++;
3020e063efeSYaroslav Tykhiy 			break;
3030e063efeSYaroslav Tykhiy 
304a4b77a2aSPoul-Henning Kamp 		case 'E':
305a4b77a2aSPoul-Henning Kamp 			noepsv = 1;
306a4b77a2aSPoul-Henning Kamp 			break;
307a4b77a2aSPoul-Henning Kamp 
308c152df28SYaroslav Tykhiy 		case 'h':
309c152df28SYaroslav Tykhiy 			hostinfo = 0;
310c152df28SYaroslav Tykhiy 			break;
311c152df28SYaroslav Tykhiy 
312ea022d16SRodney W. Grimes 		case 'l':
313ea022d16SRodney W. Grimes 			logging++;	/* > 1 == extra logging */
314ea022d16SRodney W. Grimes 			break;
315ea022d16SRodney W. Grimes 
316a117c345SYaroslav Tykhiy 		case 'm':
317a117c345SYaroslav Tykhiy 			noguestmod = 0;
318a117c345SYaroslav Tykhiy 			break;
319a117c345SYaroslav Tykhiy 
3200e063efeSYaroslav Tykhiy 		case 'M':
3210e063efeSYaroslav Tykhiy 			noguestmkd = 1;
3220e063efeSYaroslav Tykhiy 			break;
3230e063efeSYaroslav Tykhiy 
3240e063efeSYaroslav Tykhiy 		case 'o':
3250e063efeSYaroslav Tykhiy 			noretr = 1;
3260e063efeSYaroslav Tykhiy 			break;
3270e063efeSYaroslav Tykhiy 
3280e063efeSYaroslav Tykhiy 		case 'O':
3290e063efeSYaroslav Tykhiy 			noguestretr = 1;
3300e063efeSYaroslav Tykhiy 			break;
3310e063efeSYaroslav Tykhiy 
3320e063efeSYaroslav Tykhiy 		case 'p':
3330e063efeSYaroslav Tykhiy 			pid_file = optarg;
3340e063efeSYaroslav Tykhiy 			break;
3350e063efeSYaroslav Tykhiy 
33663591ba5SYaroslav Tykhiy 		case 'P':
33763591ba5SYaroslav Tykhiy 			bindport = optarg;
33863591ba5SYaroslav Tykhiy 			break;
33963591ba5SYaroslav Tykhiy 
340a4b77a2aSPoul-Henning Kamp 		case 'r':
341a4b77a2aSPoul-Henning Kamp 			readonly = 1;
342a4b77a2aSPoul-Henning Kamp 			break;
343a4b77a2aSPoul-Henning Kamp 
344a5a4544eSPaul Traina 		case 'R':
345a5a4544eSPaul Traina 			paranoid = 0;
346a5a4544eSPaul Traina 			break;
347a5a4544eSPaul Traina 
348a5a4544eSPaul Traina 		case 'S':
349a5a4544eSPaul Traina 			stats++;
350a5a4544eSPaul Traina 			break;
351a5a4544eSPaul Traina 
352ea022d16SRodney W. Grimes 		case 't':
353ea022d16SRodney W. Grimes 			timeout = atoi(optarg);
354ea022d16SRodney W. Grimes 			if (maxtimeout < timeout)
355ea022d16SRodney W. Grimes 				maxtimeout = timeout;
356ea022d16SRodney W. Grimes 			break;
357a5a4544eSPaul Traina 
3580e063efeSYaroslav Tykhiy 		case 'T':
3590e063efeSYaroslav Tykhiy 			maxtimeout = atoi(optarg);
3600e063efeSYaroslav Tykhiy 			if (timeout > maxtimeout)
3610e063efeSYaroslav Tykhiy 				timeout = maxtimeout;
362105a3c98SJulian Elischer 			break;
363105a3c98SJulian Elischer 
364ea022d16SRodney W. Grimes 		case 'u':
365ea022d16SRodney W. Grimes 		    {
366ea022d16SRodney W. Grimes 			long val = 0;
367ea022d16SRodney W. Grimes 
368ea022d16SRodney W. Grimes 			val = strtol(optarg, &optarg, 8);
369ea022d16SRodney W. Grimes 			if (*optarg != '\0' || val < 0)
370ea022d16SRodney W. Grimes 				warnx("bad value for -u");
371ea022d16SRodney W. Grimes 			else
372ea022d16SRodney W. Grimes 				defumask = val;
373ea022d16SRodney W. Grimes 			break;
374ea022d16SRodney W. Grimes 		    }
3750e063efeSYaroslav Tykhiy 		case 'U':
3760e063efeSYaroslav Tykhiy 			restricted_data_ports = 0;
3775a392aecSTorsten Blum 			break;
378ea022d16SRodney W. Grimes 
379ea022d16SRodney W. Grimes 		case 'v':
38093bd9dc5SYaroslav Tykhiy 			ftpdebug++;
381ea022d16SRodney W. Grimes 			break;
382ea022d16SRodney W. Grimes 
3835d7e0128SYaroslav Tykhiy 		case 'W':
3845d7e0128SYaroslav Tykhiy 			dowtmp = 0;
3855d7e0128SYaroslav Tykhiy 			break;
3865d7e0128SYaroslav Tykhiy 
387ea022d16SRodney W. Grimes 		default:
388ea022d16SRodney W. Grimes 			warnx("unknown flag -%c ignored", optopt);
389ea022d16SRodney W. Grimes 			break;
390ea022d16SRodney W. Grimes 		}
391ea022d16SRodney W. Grimes 	}
392cf09a206SDavid Greenman 
393ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
394ea4e54b9SDavid Nugent 	inithosts();
395ea4e54b9SDavid Nugent #endif
396ea022d16SRodney W. Grimes 	(void) freopen(_PATH_DEVNULL, "w", stderr);
397cf09a206SDavid Greenman 
398cf09a206SDavid Greenman 	/*
399cf09a206SDavid Greenman 	 * LOG_NDELAY sets up the logging connection immediately,
400cf09a206SDavid Greenman 	 * necessary for anonymous ftp's that chroot and can't do it later.
401cf09a206SDavid Greenman 	 */
402cf09a206SDavid Greenman 	openlog("ftpd", LOG_PID | LOG_NDELAY, LOG_FTP);
403cf09a206SDavid Greenman 
404cf09a206SDavid Greenman 	if (daemon_mode) {
405206fe568SHajimu UMEMOTO 		int *ctl_sock, fd, maxfd = -1, nfds, i;
406206fe568SHajimu UMEMOTO 		fd_set defreadfds, readfds;
407206fe568SHajimu UMEMOTO 		pid_t pid;
408cf09a206SDavid Greenman 
409cf09a206SDavid Greenman 		/*
410cf09a206SDavid Greenman 		 * Detach from parent.
411cf09a206SDavid Greenman 		 */
412cf09a206SDavid Greenman 		if (daemon(1, 1) < 0) {
413cf09a206SDavid Greenman 			syslog(LOG_ERR, "failed to become a daemon");
414cf09a206SDavid Greenman 			exit(1);
415cf09a206SDavid Greenman 		}
4164b82fc95SYaroslav Tykhiy 		sa.sa_handler = reapchild;
4174b82fc95SYaroslav Tykhiy 		(void)sigaction(SIGCHLD, &sa, NULL);
4184dd8b5abSYoshinobu Inoue 
419cf09a206SDavid Greenman 		/*
420cf09a206SDavid Greenman 		 * Open a socket, bind it to the FTP port, and start
421cf09a206SDavid Greenman 		 * listening.
422cf09a206SDavid Greenman 		 */
423206fe568SHajimu UMEMOTO 		ctl_sock = socksetup(family, bindname, bindport);
424206fe568SHajimu UMEMOTO 		if (ctl_sock == NULL)
425cf09a206SDavid Greenman 			exit(1);
426206fe568SHajimu UMEMOTO 
427206fe568SHajimu UMEMOTO 		FD_ZERO(&defreadfds);
428206fe568SHajimu UMEMOTO 		for (i = 1; i <= *ctl_sock; i++) {
429206fe568SHajimu UMEMOTO 			FD_SET(ctl_sock[i], &defreadfds);
430206fe568SHajimu UMEMOTO 			if (listen(ctl_sock[i], 32) < 0) {
431cf09a206SDavid Greenman 				syslog(LOG_ERR, "control listen: %m");
432cf09a206SDavid Greenman 				exit(1);
433cf09a206SDavid Greenman 			}
434206fe568SHajimu UMEMOTO 			if (maxfd < ctl_sock[i])
435206fe568SHajimu UMEMOTO 				maxfd = ctl_sock[i];
436206fe568SHajimu UMEMOTO 		}
437206fe568SHajimu UMEMOTO 
438cf09a206SDavid Greenman 		/*
439105a3c98SJulian Elischer 		 * Atomically write process ID
440105a3c98SJulian Elischer 		 */
441105a3c98SJulian Elischer 		if (pid_file)
442105a3c98SJulian Elischer 		{
443105a3c98SJulian Elischer 			int fd;
444105a3c98SJulian Elischer 			char buf[20];
445105a3c98SJulian Elischer 
446105a3c98SJulian Elischer 			fd = open(pid_file, O_CREAT | O_WRONLY | O_TRUNC
447105a3c98SJulian Elischer 				| O_NONBLOCK | O_EXLOCK, 0644);
44885966371SWarner Losh 			if (fd < 0) {
449105a3c98SJulian Elischer 				if (errno == EAGAIN)
450105a3c98SJulian Elischer 					errx(1, "%s: file locked", pid_file);
451105a3c98SJulian Elischer 				else
452105a3c98SJulian Elischer 					err(1, "%s", pid_file);
45385966371SWarner Losh 			}
454105a3c98SJulian Elischer 			snprintf(buf, sizeof(buf),
455105a3c98SJulian Elischer 				"%lu\n", (unsigned long) getpid());
456105a3c98SJulian Elischer 			if (write(fd, buf, strlen(buf)) < 0)
457105a3c98SJulian Elischer 				err(1, "%s: write", pid_file);
458105a3c98SJulian Elischer 			/* Leave the pid file open and locked */
459105a3c98SJulian Elischer 		}
460105a3c98SJulian Elischer 		/*
461cf09a206SDavid Greenman 		 * Loop forever accepting connection requests and forking off
462cf09a206SDavid Greenman 		 * children to handle them.
463cf09a206SDavid Greenman 		 */
464cf09a206SDavid Greenman 		while (1) {
465206fe568SHajimu UMEMOTO 			FD_COPY(&defreadfds, &readfds);
466206fe568SHajimu UMEMOTO 			nfds = select(maxfd + 1, &readfds, NULL, NULL, 0);
467206fe568SHajimu UMEMOTO 			if (nfds <= 0) {
468206fe568SHajimu UMEMOTO 				if (nfds < 0 && errno != EINTR)
469206fe568SHajimu UMEMOTO 					syslog(LOG_WARNING, "select: %m");
470206fe568SHajimu UMEMOTO 				continue;
471206fe568SHajimu UMEMOTO 			}
472206fe568SHajimu UMEMOTO 
473206fe568SHajimu UMEMOTO 			pid = -1;
474206fe568SHajimu UMEMOTO                         for (i = 1; i <= *ctl_sock; i++)
475206fe568SHajimu UMEMOTO 				if (FD_ISSET(ctl_sock[i], &readfds)) {
476206fe568SHajimu UMEMOTO 					addrlen = sizeof(his_addr);
477206fe568SHajimu UMEMOTO 					fd = accept(ctl_sock[i],
478206fe568SHajimu UMEMOTO 					    (struct sockaddr *)&his_addr,
479206fe568SHajimu UMEMOTO 					    &addrlen);
48040e67765SMaxim Konovalov 					if (fd >= 0) {
481206fe568SHajimu UMEMOTO 						if ((pid = fork()) == 0) {
482cf09a206SDavid Greenman 							/* child */
483cf09a206SDavid Greenman 							(void) dup2(fd, 0);
484cf09a206SDavid Greenman 							(void) dup2(fd, 1);
485206fe568SHajimu UMEMOTO 							close(ctl_sock[i]);
486206fe568SHajimu UMEMOTO 						} else
487cf09a206SDavid Greenman 							close(fd);
488cf09a206SDavid Greenman 					}
48940e67765SMaxim Konovalov 				}
490206fe568SHajimu UMEMOTO 			if (pid == 0)
491206fe568SHajimu UMEMOTO 				break;
492206fe568SHajimu UMEMOTO 		}
493cf09a206SDavid Greenman 	} else {
494cf09a206SDavid Greenman 		addrlen = sizeof(his_addr);
495cf09a206SDavid Greenman 		if (getpeername(0, (struct sockaddr *)&his_addr, &addrlen) < 0) {
496cf09a206SDavid Greenman 			syslog(LOG_ERR, "getpeername (%s): %m",argv[0]);
497cf09a206SDavid Greenman 			exit(1);
498cf09a206SDavid Greenman 		}
499cf09a206SDavid Greenman 	}
500cf09a206SDavid Greenman 
5014b82fc95SYaroslav Tykhiy 	sa.sa_handler = SIG_DFL;
5024b82fc95SYaroslav Tykhiy 	(void)sigaction(SIGCHLD, &sa, NULL);
5034b82fc95SYaroslav Tykhiy 
5044b82fc95SYaroslav Tykhiy 	sa.sa_handler = sigurg;
5054b82fc95SYaroslav Tykhiy 	sa.sa_flags = 0;		/* don't restart syscalls for SIGURG */
5064b82fc95SYaroslav Tykhiy 	(void)sigaction(SIGURG, &sa, NULL);
5074b82fc95SYaroslav Tykhiy 
5084b82fc95SYaroslav Tykhiy 	sigfillset(&sa.sa_mask);	/* block all signals in handler */
5094b82fc95SYaroslav Tykhiy 	sa.sa_flags = SA_RESTART;
5104b82fc95SYaroslav Tykhiy 	sa.sa_handler = sigquit;
5114b82fc95SYaroslav Tykhiy 	(void)sigaction(SIGHUP, &sa, NULL);
5124b82fc95SYaroslav Tykhiy 	(void)sigaction(SIGINT, &sa, NULL);
5134b82fc95SYaroslav Tykhiy 	(void)sigaction(SIGQUIT, &sa, NULL);
5144b82fc95SYaroslav Tykhiy 	(void)sigaction(SIGTERM, &sa, NULL);
5154b82fc95SYaroslav Tykhiy 
5164b82fc95SYaroslav Tykhiy 	sa.sa_handler = lostconn;
5174b82fc95SYaroslav Tykhiy 	(void)sigaction(SIGPIPE, &sa, NULL);
518ea022d16SRodney W. Grimes 
519cf09a206SDavid Greenman 	addrlen = sizeof(ctrl_addr);
520cf09a206SDavid Greenman 	if (getsockname(0, (struct sockaddr *)&ctrl_addr, &addrlen) < 0) {
521cf09a206SDavid Greenman 		syslog(LOG_ERR, "getsockname (%s): %m",argv[0]);
522cf09a206SDavid Greenman 		exit(1);
523cf09a206SDavid Greenman 	}
52463591ba5SYaroslav Tykhiy 	dataport = ntohs(ctrl_addr.su_port) - 1; /* as per RFC 959 */
525ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
526ea4e54b9SDavid Nugent 	/* select our identity from virtual host table */
5274dd8b5abSYoshinobu Inoue 	selecthost(&ctrl_addr);
528ea4e54b9SDavid Nugent #endif
529cf09a206SDavid Greenman #ifdef IP_TOS
5304dd8b5abSYoshinobu Inoue 	if (ctrl_addr.su_family == AF_INET)
5314dd8b5abSYoshinobu Inoue       {
532cf09a206SDavid Greenman 	tos = IPTOS_LOWDELAY;
53357d4ef07SYaroslav Tykhiy 	if (setsockopt(0, IPPROTO_IP, IP_TOS, &tos, sizeof(int)) < 0)
534406d1ae9SYaroslav Tykhiy 		syslog(LOG_WARNING, "control setsockopt (IP_TOS): %m");
5354dd8b5abSYoshinobu Inoue       }
536cf09a206SDavid Greenman #endif
537dadb9fb3SDavid Greenman 	/*
538dadb9fb3SDavid Greenman 	 * Disable Nagle on the control channel so that we don't have to wait
539dadb9fb3SDavid Greenman 	 * for peer's ACK before issuing our next reply.
540dadb9fb3SDavid Greenman 	 */
541dadb9fb3SDavid Greenman 	if (setsockopt(0, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) < 0)
542406d1ae9SYaroslav Tykhiy 		syslog(LOG_WARNING, "control setsockopt (TCP_NODELAY): %m");
543dadb9fb3SDavid Greenman 
5444dd8b5abSYoshinobu Inoue 	data_source.su_port = htons(ntohs(ctrl_addr.su_port) - 1);
545cf09a206SDavid Greenman 
546a5a4544eSPaul Traina 	/* set this here so klogin can use it... */
547e760ef2cSWarner Losh 	(void)snprintf(ttyline, sizeof(ttyline), "ftp%d", getpid());
548a5a4544eSPaul Traina 
549ea022d16SRodney W. Grimes 	/* Try to handle urgent data inline */
550ea022d16SRodney W. Grimes #ifdef SO_OOBINLINE
55157d4ef07SYaroslav Tykhiy 	if (setsockopt(0, SOL_SOCKET, SO_OOBINLINE, &on, sizeof(on)) < 0)
552406d1ae9SYaroslav Tykhiy 		syslog(LOG_WARNING, "control setsockopt (SO_OOBINLINE): %m");
553ea022d16SRodney W. Grimes #endif
554ea022d16SRodney W. Grimes 
555ea022d16SRodney W. Grimes #ifdef	F_SETOWN
556ea022d16SRodney W. Grimes 	if (fcntl(fileno(stdin), F_SETOWN, getpid()) == -1)
557ea022d16SRodney W. Grimes 		syslog(LOG_ERR, "fcntl F_SETOWN: %m");
558ea022d16SRodney W. Grimes #endif
5594dd8b5abSYoshinobu Inoue 	dolog((struct sockaddr *)&his_addr);
560ea022d16SRodney W. Grimes 	/*
561ea022d16SRodney W. Grimes 	 * Set up default state
562ea022d16SRodney W. Grimes 	 */
563ea022d16SRodney W. Grimes 	data = -1;
564ea022d16SRodney W. Grimes 	type = TYPE_A;
565ea022d16SRodney W. Grimes 	form = FORM_N;
566ea022d16SRodney W. Grimes 	stru = STRU_F;
567ea022d16SRodney W. Grimes 	mode = MODE_S;
568ea022d16SRodney W. Grimes 	tmpline[0] = '\0';
569ea022d16SRodney W. Grimes 
570ea022d16SRodney W. Grimes 	/* If logins are disabled, print out the message. */
571ea022d16SRodney W. Grimes 	if ((fd = fopen(_PATH_NOLOGIN,"r")) != NULL) {
572ea022d16SRodney W. Grimes 		while (fgets(line, sizeof(line), fd) != NULL) {
573ea022d16SRodney W. Grimes 			if ((cp = strchr(line, '\n')) != NULL)
574ea022d16SRodney W. Grimes 				*cp = '\0';
575ea022d16SRodney W. Grimes 			lreply(530, "%s", line);
576ea022d16SRodney W. Grimes 		}
577ea022d16SRodney W. Grimes 		(void) fflush(stdout);
578ea022d16SRodney W. Grimes 		(void) fclose(fd);
579ea022d16SRodney W. Grimes 		reply(530, "System not available.");
580ea022d16SRodney W. Grimes 		exit(0);
581ea022d16SRodney W. Grimes 	}
582ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
58363047c6fSDavid E. O'Brien 	fd = fopen(thishost->welcome, "r");
584ea4e54b9SDavid Nugent #else
58563047c6fSDavid E. O'Brien 	fd = fopen(_PATH_FTPWELCOME, "r");
586ea4e54b9SDavid Nugent #endif
58763047c6fSDavid E. O'Brien 	if (fd != NULL) {
588ea022d16SRodney W. Grimes 		while (fgets(line, sizeof(line), fd) != NULL) {
589ea022d16SRodney W. Grimes 			if ((cp = strchr(line, '\n')) != NULL)
590ea022d16SRodney W. Grimes 				*cp = '\0';
591ea022d16SRodney W. Grimes 			lreply(220, "%s", line);
592ea022d16SRodney W. Grimes 		}
593ea022d16SRodney W. Grimes 		(void) fflush(stdout);
594ea022d16SRodney W. Grimes 		(void) fclose(fd);
595ea022d16SRodney W. Grimes 		/* reply(220,) must follow */
596ea022d16SRodney W. Grimes 	}
597ea4e54b9SDavid Nugent #ifndef VIRTUAL_HOSTING
5980512556aSDavid Nugent 	if ((hostname = malloc(MAXHOSTNAMELEN)) == NULL)
599618b0bbaSMark Murray 		fatalerror("Ran out of memory.");
60004683b2cSYaroslav Tykhiy 	if (gethostname(hostname, MAXHOSTNAMELEN - 1) < 0)
60104683b2cSYaroslav Tykhiy 		hostname[0] = '\0';
6029e9a43bdSBrian Somers 	hostname[MAXHOSTNAMELEN - 1] = '\0';
603ea4e54b9SDavid Nugent #endif
604c152df28SYaroslav Tykhiy 	if (hostinfo)
605ea022d16SRodney W. Grimes 		reply(220, "%s FTP server (%s) ready.", hostname, version);
606c152df28SYaroslav Tykhiy 	else
607c152df28SYaroslav Tykhiy 		reply(220, "FTP server ready.");
608ea022d16SRodney W. Grimes 	for (;;)
609ea022d16SRodney W. Grimes 		(void) yyparse();
610ea022d16SRodney W. Grimes 	/* NOTREACHED */
611ea022d16SRodney W. Grimes }
612ea022d16SRodney W. Grimes 
613ea022d16SRodney W. Grimes static void
614e4bc453cSWarner Losh lostconn(int signo)
615ea022d16SRodney W. Grimes {
616ea022d16SRodney W. Grimes 
617618b0bbaSMark Murray 	if (ftpdebug)
618ea022d16SRodney W. Grimes 		syslog(LOG_DEBUG, "lost connection");
619e02897faSPhilippe Charnier 	dologout(1);
620ea022d16SRodney W. Grimes }
621ea022d16SRodney W. Grimes 
6224b82fc95SYaroslav Tykhiy static void
623e4bc453cSWarner Losh sigquit(int signo)
6244b82fc95SYaroslav Tykhiy {
6254b82fc95SYaroslav Tykhiy 
6264b82fc95SYaroslav Tykhiy 	syslog(LOG_ERR, "got signal %d", signo);
6274b82fc95SYaroslav Tykhiy 	dologout(1);
6284b82fc95SYaroslav Tykhiy }
6294b82fc95SYaroslav Tykhiy 
630ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
631ea4e54b9SDavid Nugent /*
632ea4e54b9SDavid Nugent  * read in virtual host tables (if they exist)
633ea4e54b9SDavid Nugent  */
634ea4e54b9SDavid Nugent 
635ea4e54b9SDavid Nugent static void
636e4bc453cSWarner Losh inithosts(void)
637ea4e54b9SDavid Nugent {
63855b54aa7SYaroslav Tykhiy 	int insert;
639737d08f3SYaroslav Tykhiy 	size_t len;
640ea4e54b9SDavid Nugent 	FILE *fp;
641737d08f3SYaroslav Tykhiy 	char *cp, *mp, *line;
642737d08f3SYaroslav Tykhiy 	char *hostname;
64355b54aa7SYaroslav Tykhiy 	char *vhost, *anonuser, *statfile, *welcome, *loginmsg;
644ea4e54b9SDavid Nugent 	struct ftphost *hrp, *lhrp;
6454dd8b5abSYoshinobu Inoue 	struct addrinfo hints, *res, *ai;
646ea4e54b9SDavid Nugent 
647ea4e54b9SDavid Nugent 	/*
648ea4e54b9SDavid Nugent 	 * Fill in the default host information
649ea4e54b9SDavid Nugent 	 */
650737d08f3SYaroslav Tykhiy 	if ((hostname = malloc(MAXHOSTNAMELEN)) == NULL)
651618b0bbaSMark Murray 		fatalerror("Ran out of memory.");
65204683b2cSYaroslav Tykhiy 	if (gethostname(hostname, MAXHOSTNAMELEN - 1) < 0)
653737d08f3SYaroslav Tykhiy 		hostname[0] = '\0';
654737d08f3SYaroslav Tykhiy 	hostname[MAXHOSTNAMELEN - 1] = '\0';
655737d08f3SYaroslav Tykhiy 	if ((hrp = malloc(sizeof(struct ftphost))) == NULL)
656737d08f3SYaroslav Tykhiy 		fatalerror("Ran out of memory.");
657737d08f3SYaroslav Tykhiy 	hrp->hostname = hostname;
658f38c6cadSYoshinobu Inoue 	hrp->hostinfo = NULL;
6594dd8b5abSYoshinobu Inoue 
6604dd8b5abSYoshinobu Inoue 	memset(&hints, 0, sizeof(hints));
6614dd8b5abSYoshinobu Inoue 	hints.ai_flags = AI_CANONNAME;
6624dd8b5abSYoshinobu Inoue 	hints.ai_family = AF_UNSPEC;
663b1d8d5cdSYaroslav Tykhiy 	if (getaddrinfo(hrp->hostname, NULL, &hints, &res) == 0)
664f38c6cadSYoshinobu Inoue 		hrp->hostinfo = res;
665ea4e54b9SDavid Nugent 	hrp->statfile = _PATH_FTPDSTATFILE;
666ea4e54b9SDavid Nugent 	hrp->welcome  = _PATH_FTPWELCOME;
667ea4e54b9SDavid Nugent 	hrp->loginmsg = _PATH_FTPLOGINMESG;
668ea4e54b9SDavid Nugent 	hrp->anonuser = "ftp";
669ea4e54b9SDavid Nugent 	hrp->next = NULL;
670ea4e54b9SDavid Nugent 	thishost = firsthost = lhrp = hrp;
671ea4e54b9SDavid Nugent 	if ((fp = fopen(_PATH_FTPHOSTS, "r")) != NULL) {
672ec009cf0SYaroslav Tykhiy 		int addrsize, gothost;
6734dd8b5abSYoshinobu Inoue 		void *addr;
6744dd8b5abSYoshinobu Inoue 		struct hostent *hp;
6754dd8b5abSYoshinobu Inoue 
676737d08f3SYaroslav Tykhiy 		while ((line = fgetln(fp, &len)) != NULL) {
6774dd8b5abSYoshinobu Inoue 			int	i, hp_error;
678ea4e54b9SDavid Nugent 
679737d08f3SYaroslav Tykhiy 			/* skip comments */
680737d08f3SYaroslav Tykhiy 			if (line[0] == '#')
681ea4e54b9SDavid Nugent 				continue;
682737d08f3SYaroslav Tykhiy 			if (line[len - 1] == '\n') {
683737d08f3SYaroslav Tykhiy 				line[len - 1] = '\0';
684737d08f3SYaroslav Tykhiy 				mp = NULL;
685737d08f3SYaroslav Tykhiy 			} else {
686737d08f3SYaroslav Tykhiy 				if ((mp = malloc(len + 1)) == NULL)
687737d08f3SYaroslav Tykhiy 					fatalerror("Ran out of memory.");
688737d08f3SYaroslav Tykhiy 				memcpy(mp, line, len);
689737d08f3SYaroslav Tykhiy 				mp[len] = '\0';
690737d08f3SYaroslav Tykhiy 				line = mp;
691ea4e54b9SDavid Nugent 			}
692ea4e54b9SDavid Nugent 			cp = strtok(line, " \t");
693737d08f3SYaroslav Tykhiy 			/* skip empty lines */
694737d08f3SYaroslav Tykhiy 			if (cp == NULL)
695737d08f3SYaroslav Tykhiy 				goto nextline;
69655b54aa7SYaroslav Tykhiy 			vhost = cp;
69755b54aa7SYaroslav Tykhiy 
69855b54aa7SYaroslav Tykhiy 			/* set defaults */
69955b54aa7SYaroslav Tykhiy 			anonuser = "ftp";
70055b54aa7SYaroslav Tykhiy 			statfile = _PATH_FTPDSTATFILE;
70155b54aa7SYaroslav Tykhiy 			welcome  = _PATH_FTPWELCOME;
70255b54aa7SYaroslav Tykhiy 			loginmsg = _PATH_FTPLOGINMESG;
70355b54aa7SYaroslav Tykhiy 
70455b54aa7SYaroslav Tykhiy 			/*
70555b54aa7SYaroslav Tykhiy 			 * Preparse the line so we can use its info
70655b54aa7SYaroslav Tykhiy 			 * for all the addresses associated with
70755b54aa7SYaroslav Tykhiy 			 * the virtual host name.
70855b54aa7SYaroslav Tykhiy 			 * Field 0, the virtual host name, is special:
70955b54aa7SYaroslav Tykhiy 			 * it's already parsed off and will be strdup'ed
71055b54aa7SYaroslav Tykhiy 			 * later, after we know its canonical form.
71155b54aa7SYaroslav Tykhiy 			 */
71255b54aa7SYaroslav Tykhiy 			for (i = 1; i < 5 && (cp = strtok(NULL, " \t")); i++)
71355b54aa7SYaroslav Tykhiy 				if (*cp != '-' && (cp = strdup(cp)))
71455b54aa7SYaroslav Tykhiy 					switch (i) {
71555b54aa7SYaroslav Tykhiy 					case 1:	/* anon user permissions */
71655b54aa7SYaroslav Tykhiy 						anonuser = cp;
71755b54aa7SYaroslav Tykhiy 						break;
71855b54aa7SYaroslav Tykhiy 					case 2: /* statistics file */
71955b54aa7SYaroslav Tykhiy 						statfile = cp;
72055b54aa7SYaroslav Tykhiy 						break;
72155b54aa7SYaroslav Tykhiy 					case 3: /* welcome message */
72255b54aa7SYaroslav Tykhiy 						welcome  = cp;
72355b54aa7SYaroslav Tykhiy 						break;
72455b54aa7SYaroslav Tykhiy 					case 4: /* login message */
72555b54aa7SYaroslav Tykhiy 						loginmsg = cp;
72655b54aa7SYaroslav Tykhiy 						break;
72755b54aa7SYaroslav Tykhiy 					default: /* programming error */
72855b54aa7SYaroslav Tykhiy 						abort();
72955b54aa7SYaroslav Tykhiy 						/* NOTREACHED */
73055b54aa7SYaroslav Tykhiy 					}
7314dd8b5abSYoshinobu Inoue 
7324dd8b5abSYoshinobu Inoue 			hints.ai_flags = 0;
7334dd8b5abSYoshinobu Inoue 			hints.ai_family = AF_UNSPEC;
7344dd8b5abSYoshinobu Inoue 			hints.ai_flags = AI_PASSIVE;
735b1d8d5cdSYaroslav Tykhiy 			if (getaddrinfo(vhost, NULL, &hints, &res) != 0)
736737d08f3SYaroslav Tykhiy 				goto nextline;
7374dd8b5abSYoshinobu Inoue 			for (ai = res; ai != NULL && ai->ai_addr != NULL;
738b535a9bfSDavid Nugent 			     ai = ai->ai_next) {
7394dd8b5abSYoshinobu Inoue 
740b535a9bfSDavid Nugent 			gothost = 0;
741ea4e54b9SDavid Nugent 			for (hrp = firsthost; hrp != NULL; hrp = hrp->next) {
742f38c6cadSYoshinobu Inoue 				struct addrinfo *hi;
743f38c6cadSYoshinobu Inoue 
744f38c6cadSYoshinobu Inoue 				for (hi = hrp->hostinfo; hi != NULL;
745f38c6cadSYoshinobu Inoue 				     hi = hi->ai_next)
746f38c6cadSYoshinobu Inoue 					if (hi->ai_addrlen == ai->ai_addrlen &&
747f38c6cadSYoshinobu Inoue 					    memcmp(hi->ai_addr,
7484dd8b5abSYoshinobu Inoue 						   ai->ai_addr,
749b535a9bfSDavid Nugent 						   ai->ai_addr->sa_len) == 0) {
750b535a9bfSDavid Nugent 						gothost++;
751b535a9bfSDavid Nugent 						break;
752b535a9bfSDavid Nugent 					}
753b535a9bfSDavid Nugent 				if (gothost)
754ea4e54b9SDavid Nugent 					break;
755ea4e54b9SDavid Nugent 			}
756ea4e54b9SDavid Nugent 			if (hrp == NULL) {
757ea4e54b9SDavid Nugent 				if ((hrp = malloc(sizeof(struct ftphost))) == NULL)
758737d08f3SYaroslav Tykhiy 					goto nextline;
759b1d8d5cdSYaroslav Tykhiy 				hrp->hostname = NULL;
76055b54aa7SYaroslav Tykhiy 				insert = 1;
761f2fe752dSYaroslav Tykhiy 			} else {
7621f75c13eSYaroslav Tykhiy 				if (hrp->hostinfo && hrp->hostinfo != res)
763b1d8d5cdSYaroslav Tykhiy 					freeaddrinfo(hrp->hostinfo);
764f2fe752dSYaroslav Tykhiy 				insert = 0; /* host already in the chain */
765f2fe752dSYaroslav Tykhiy 			}
766f38c6cadSYoshinobu Inoue 			hrp->hostinfo = res;
767f38c6cadSYoshinobu Inoue 
768ea4e54b9SDavid Nugent 			/*
769ea4e54b9SDavid Nugent 			 * determine hostname to use.
7704dd8b5abSYoshinobu Inoue 			 * force defined name if there is a valid alias
771ea4e54b9SDavid Nugent 			 * otherwise fallback to primary hostname
772ea4e54b9SDavid Nugent 			 */
7734dd8b5abSYoshinobu Inoue 			/* XXX: getaddrinfo() can't do alias check */
774f38c6cadSYoshinobu Inoue 			switch(hrp->hostinfo->ai_family) {
7754dd8b5abSYoshinobu Inoue 			case AF_INET:
7764b4cc4c6SYaroslav Tykhiy 				addr = &((struct sockaddr_in *)hrp->hostinfo->ai_addr)->sin_addr;
7774b4cc4c6SYaroslav Tykhiy 				addrsize = sizeof(struct in_addr);
7784dd8b5abSYoshinobu Inoue 				break;
7794dd8b5abSYoshinobu Inoue 			case AF_INET6:
7804b4cc4c6SYaroslav Tykhiy 				addr = &((struct sockaddr_in6 *)hrp->hostinfo->ai_addr)->sin6_addr;
7814b4cc4c6SYaroslav Tykhiy 				addrsize = sizeof(struct in6_addr);
7824dd8b5abSYoshinobu Inoue 				break;
7834dd8b5abSYoshinobu Inoue 			default:
7844dd8b5abSYoshinobu Inoue 				/* should not reach here */
785f38c6cadSYoshinobu Inoue 				freeaddrinfo(hrp->hostinfo);
786f2fe752dSYaroslav Tykhiy 				if (insert)
787f2fe752dSYaroslav Tykhiy 					free(hrp); /*not in chain, can free*/
788f2fe752dSYaroslav Tykhiy 				else
789f2fe752dSYaroslav Tykhiy 					hrp->hostinfo = NULL; /*mark as blank*/
790737d08f3SYaroslav Tykhiy 				goto nextline;
7914dd8b5abSYoshinobu Inoue 				/* NOTREACHED */
7924dd8b5abSYoshinobu Inoue 			}
79357d4ef07SYaroslav Tykhiy 			if ((hp = getipnodebyaddr(addr, addrsize,
794f38c6cadSYoshinobu Inoue 						  hrp->hostinfo->ai_family,
7954dd8b5abSYoshinobu Inoue 						  &hp_error)) != NULL) {
79655b54aa7SYaroslav Tykhiy 				if (strcmp(vhost, hp->h_name) != 0) {
797ea4e54b9SDavid Nugent 					if (hp->h_aliases == NULL)
79855b54aa7SYaroslav Tykhiy 						vhost = hp->h_name;
799ea4e54b9SDavid Nugent 					else {
800ea4e54b9SDavid Nugent 						i = 0;
801ea4e54b9SDavid Nugent 						while (hp->h_aliases[i] &&
80255b54aa7SYaroslav Tykhiy 						       strcmp(vhost, hp->h_aliases[i]) != 0)
803ea4e54b9SDavid Nugent 							++i;
804ea4e54b9SDavid Nugent 						if (hp->h_aliases[i] == NULL)
80555b54aa7SYaroslav Tykhiy 							vhost = hp->h_name;
806ea4e54b9SDavid Nugent 					}
807ea4e54b9SDavid Nugent 				}
808ea4e54b9SDavid Nugent 			}
809b1d8d5cdSYaroslav Tykhiy 			if (hrp->hostname &&
810b1d8d5cdSYaroslav Tykhiy 			    strcmp(hrp->hostname, vhost) != 0) {
811b1d8d5cdSYaroslav Tykhiy 				free(hrp->hostname);
812b1d8d5cdSYaroslav Tykhiy 				hrp->hostname = NULL;
813b1d8d5cdSYaroslav Tykhiy 			}
814b1d8d5cdSYaroslav Tykhiy 			if (hrp->hostname == NULL &&
815f2fe752dSYaroslav Tykhiy 			    (hrp->hostname = strdup(vhost)) == NULL) {
816f2fe752dSYaroslav Tykhiy 				freeaddrinfo(hrp->hostinfo);
817f2fe752dSYaroslav Tykhiy 				hrp->hostinfo = NULL; /* mark as blank */
818f2fe752dSYaroslav Tykhiy 				if (hp)
819f2fe752dSYaroslav Tykhiy 					freehostent(hp);
82055b54aa7SYaroslav Tykhiy 				goto nextline;
821f2fe752dSYaroslav Tykhiy 			}
82255b54aa7SYaroslav Tykhiy 			hrp->anonuser = anonuser;
82355b54aa7SYaroslav Tykhiy 			hrp->statfile = statfile;
82455b54aa7SYaroslav Tykhiy 			hrp->welcome  = welcome;
82555b54aa7SYaroslav Tykhiy 			hrp->loginmsg = loginmsg;
82655b54aa7SYaroslav Tykhiy 			if (insert) {
82755b54aa7SYaroslav Tykhiy 				hrp->next  = NULL;
82855b54aa7SYaroslav Tykhiy 				lhrp->next = hrp;
82955b54aa7SYaroslav Tykhiy 				lhrp = hrp;
83055b54aa7SYaroslav Tykhiy 			}
831233c0f66SYaroslav Tykhiy 			if (hp)
8324dd8b5abSYoshinobu Inoue 				freehostent(hp);
8334dd8b5abSYoshinobu Inoue 		      }
834737d08f3SYaroslav Tykhiy nextline:
835737d08f3SYaroslav Tykhiy 			if (mp)
836737d08f3SYaroslav Tykhiy 				free(mp);
837ea4e54b9SDavid Nugent 		}
838ea4e54b9SDavid Nugent 		(void) fclose(fp);
839ea4e54b9SDavid Nugent 	}
840ea4e54b9SDavid Nugent }
841ea4e54b9SDavid Nugent 
842ea4e54b9SDavid Nugent static void
843e4bc453cSWarner Losh selecthost(union sockunion *su)
844ea4e54b9SDavid Nugent {
845ea4e54b9SDavid Nugent 	struct ftphost	*hrp;
8464dd8b5abSYoshinobu Inoue 	u_int16_t port;
8474dd8b5abSYoshinobu Inoue #ifdef INET6
8484dd8b5abSYoshinobu Inoue 	struct in6_addr *mapped_in6 = NULL;
8494dd8b5abSYoshinobu Inoue #endif
850f38c6cadSYoshinobu Inoue 	struct addrinfo *hi;
8514dd8b5abSYoshinobu Inoue 
8524dd8b5abSYoshinobu Inoue #ifdef INET6
8534dd8b5abSYoshinobu Inoue 	/*
8544dd8b5abSYoshinobu Inoue 	 * XXX IPv4 mapped IPv6 addr consideraton,
8554dd8b5abSYoshinobu Inoue 	 * specified in rfc2373.
8564dd8b5abSYoshinobu Inoue 	 */
8574dd8b5abSYoshinobu Inoue 	if (su->su_family == AF_INET6 &&
8584dd8b5abSYoshinobu Inoue 	    IN6_IS_ADDR_V4MAPPED(&su->su_sin6.sin6_addr))
8594dd8b5abSYoshinobu Inoue 		mapped_in6 = &su->su_sin6.sin6_addr;
8604dd8b5abSYoshinobu Inoue #endif
861ea4e54b9SDavid Nugent 
862ea4e54b9SDavid Nugent 	hrp = thishost = firsthost;	/* default */
8634dd8b5abSYoshinobu Inoue 	port = su->su_port;
8644dd8b5abSYoshinobu Inoue 	su->su_port = 0;
865ea4e54b9SDavid Nugent 	while (hrp != NULL) {
866b535a9bfSDavid Nugent 	    for (hi = hrp->hostinfo; hi != NULL; hi = hi->ai_next) {
867f38c6cadSYoshinobu Inoue 		if (memcmp(su, hi->ai_addr, hi->ai_addrlen) == 0) {
868ea4e54b9SDavid Nugent 			thishost = hrp;
869ea4e54b9SDavid Nugent 			break;
870ea4e54b9SDavid Nugent 		}
8714dd8b5abSYoshinobu Inoue #ifdef INET6
8724dd8b5abSYoshinobu Inoue 		/* XXX IPv4 mapped IPv6 addr consideraton */
873f38c6cadSYoshinobu Inoue 		if (hi->ai_addr->sa_family == AF_INET && mapped_in6 != NULL &&
8744dd8b5abSYoshinobu Inoue 		    (memcmp(&mapped_in6->s6_addr[12],
875f38c6cadSYoshinobu Inoue 			    &((struct sockaddr_in *)hi->ai_addr)->sin_addr,
8764dd8b5abSYoshinobu Inoue 			    sizeof(struct in_addr)) == 0)) {
8774dd8b5abSYoshinobu Inoue 			thishost = hrp;
8784dd8b5abSYoshinobu Inoue 			break;
8794dd8b5abSYoshinobu Inoue 		}
8804dd8b5abSYoshinobu Inoue #endif
881f38c6cadSYoshinobu Inoue 	    }
882ea4e54b9SDavid Nugent 	    hrp = hrp->next;
883ea4e54b9SDavid Nugent 	}
8844dd8b5abSYoshinobu Inoue 	su->su_port = port;
885ea4e54b9SDavid Nugent 	/* setup static variables as appropriate */
886ea4e54b9SDavid Nugent 	hostname = thishost->hostname;
887ea4e54b9SDavid Nugent 	ftpuser = thishost->anonuser;
888ea4e54b9SDavid Nugent }
889ea4e54b9SDavid Nugent #endif
890ea4e54b9SDavid Nugent 
891ea022d16SRodney W. Grimes /*
892ea022d16SRodney W. Grimes  * Helper function for sgetpwnam().
893ea022d16SRodney W. Grimes  */
894ea022d16SRodney W. Grimes static char *
895e4bc453cSWarner Losh sgetsave(char *s)
896ea022d16SRodney W. Grimes {
897e3765043SYaroslav Tykhiy 	char *new = malloc(strlen(s) + 1);
898ea022d16SRodney W. Grimes 
899ea022d16SRodney W. Grimes 	if (new == NULL) {
90002c97492SYaroslav Tykhiy 		reply(421, "Ran out of memory.");
901ea022d16SRodney W. Grimes 		dologout(1);
902ea022d16SRodney W. Grimes 		/* NOTREACHED */
903ea022d16SRodney W. Grimes 	}
904ea022d16SRodney W. Grimes 	(void) strcpy(new, s);
905ea022d16SRodney W. Grimes 	return (new);
906ea022d16SRodney W. Grimes }
907ea022d16SRodney W. Grimes 
908ea022d16SRodney W. Grimes /*
909ea022d16SRodney W. Grimes  * Save the result of a getpwnam.  Used for USER command, since
910ea022d16SRodney W. Grimes  * the data returned must not be clobbered by any other command
911ea022d16SRodney W. Grimes  * (e.g., globbing).
912c29b9b47SYaroslav Tykhiy  * NB: The data returned by sgetpwnam() will remain valid until
913c29b9b47SYaroslav Tykhiy  * the next call to this function.  Its difference from getpwnam()
914c29b9b47SYaroslav Tykhiy  * is that sgetpwnam() is known to be called from ftpd code only.
915ea022d16SRodney W. Grimes  */
916ea022d16SRodney W. Grimes static struct passwd *
917e4bc453cSWarner Losh sgetpwnam(char *name)
918ea022d16SRodney W. Grimes {
919ea022d16SRodney W. Grimes 	static struct passwd save;
920ea022d16SRodney W. Grimes 	struct passwd *p;
921ea022d16SRodney W. Grimes 
922ea022d16SRodney W. Grimes 	if ((p = getpwnam(name)) == NULL)
923ea022d16SRodney W. Grimes 		return (p);
924ea022d16SRodney W. Grimes 	if (save.pw_name) {
925ea022d16SRodney W. Grimes 		free(save.pw_name);
926ea022d16SRodney W. Grimes 		free(save.pw_passwd);
927ea022d16SRodney W. Grimes 		free(save.pw_gecos);
928ea022d16SRodney W. Grimes 		free(save.pw_dir);
929ea022d16SRodney W. Grimes 		free(save.pw_shell);
930ea022d16SRodney W. Grimes 	}
931ea022d16SRodney W. Grimes 	save = *p;
932ea022d16SRodney W. Grimes 	save.pw_name = sgetsave(p->pw_name);
933ea022d16SRodney W. Grimes 	save.pw_passwd = sgetsave(p->pw_passwd);
934ea022d16SRodney W. Grimes 	save.pw_gecos = sgetsave(p->pw_gecos);
935ea022d16SRodney W. Grimes 	save.pw_dir = sgetsave(p->pw_dir);
936ea022d16SRodney W. Grimes 	save.pw_shell = sgetsave(p->pw_shell);
937ea022d16SRodney W. Grimes 	return (&save);
938ea022d16SRodney W. Grimes }
939ea022d16SRodney W. Grimes 
940ea022d16SRodney W. Grimes static int login_attempts;	/* number of failed login attempts */
941ea022d16SRodney W. Grimes static int askpasswd;		/* had user command, ask for passwd */
94290906a46SSheldon Hearn static char curname[MAXLOGNAME];	/* current USER name */
943ea022d16SRodney W. Grimes 
944ea022d16SRodney W. Grimes /*
945ea022d16SRodney W. Grimes  * USER command.
946ea022d16SRodney W. Grimes  * Sets global passwd pointer pw if named account exists and is acceptable;
947ea022d16SRodney W. Grimes  * sets askpasswd if a PASS command is expected.  If logged in previously,
948ea022d16SRodney W. Grimes  * need to reset state.  If name is "ftp" or "anonymous", the name is not in
949ea022d16SRodney W. Grimes  * _PATH_FTPUSERS, and ftp account exists, set guest and pw, then just return.
950ea022d16SRodney W. Grimes  * If account doesn't exist, ask for passwd anyway.  Otherwise, check user
951ea022d16SRodney W. Grimes  * requesting login privileges.  Disallow anyone who does not have a standard
952ea022d16SRodney W. Grimes  * shell as returned by getusershell().  Disallow anyone mentioned in the file
953ea022d16SRodney W. Grimes  * _PATH_FTPUSERS to allow people such as root and uucp to be avoided.
954ea022d16SRodney W. Grimes  */
955ea022d16SRodney W. Grimes void
956e4bc453cSWarner Losh user(char *name)
957ea022d16SRodney W. Grimes {
958ea022d16SRodney W. Grimes 	char *cp, *shell;
959ea022d16SRodney W. Grimes 
960ea022d16SRodney W. Grimes 	if (logged_in) {
961ea022d16SRodney W. Grimes 		if (guest) {
962ea022d16SRodney W. Grimes 			reply(530, "Can't change user from guest login.");
963ea022d16SRodney W. Grimes 			return;
964a5a4544eSPaul Traina 		} else if (dochroot) {
965a5a4544eSPaul Traina 			reply(530, "Can't change user from chroot user.");
966a5a4544eSPaul Traina 			return;
967ea022d16SRodney W. Grimes 		}
968ea022d16SRodney W. Grimes 		end_login();
969ea022d16SRodney W. Grimes 	}
970ea022d16SRodney W. Grimes 
971ea022d16SRodney W. Grimes 	guest = 0;
97263047c6fSDavid E. O'Brien #ifdef VIRTUAL_HOSTING
97363047c6fSDavid E. O'Brien 	pw = sgetpwnam(thishost->anonuser);
97463047c6fSDavid E. O'Brien #else
97563047c6fSDavid E. O'Brien 	pw = sgetpwnam("ftp");
97663047c6fSDavid E. O'Brien #endif
977ea022d16SRodney W. Grimes 	if (strcmp(name, "ftp") == 0 || strcmp(name, "anonymous") == 0) {
9788657b576SYaroslav Tykhiy 		if (checkuser(_PATH_FTPUSERS, "ftp", 0, NULL) ||
9798657b576SYaroslav Tykhiy 		    checkuser(_PATH_FTPUSERS, "anonymous", 0, NULL))
980ea022d16SRodney W. Grimes 			reply(530, "User %s access denied.", name);
98163047c6fSDavid E. O'Brien 		else if (pw != NULL) {
982ea022d16SRodney W. Grimes 			guest = 1;
983ea022d16SRodney W. Grimes 			askpasswd = 1;
984ea022d16SRodney W. Grimes 			reply(331,
9852c60c54cSPaul Traina 			"Guest login ok, send your email address as password.");
986ea022d16SRodney W. Grimes 		} else
987ea022d16SRodney W. Grimes 			reply(530, "User %s unknown.", name);
988ea022d16SRodney W. Grimes 		if (!askpasswd && logging)
989ea022d16SRodney W. Grimes 			syslog(LOG_NOTICE,
990ea022d16SRodney W. Grimes 			    "ANONYMOUS FTP LOGIN REFUSED FROM %s", remotehost);
991ea022d16SRodney W. Grimes 		return;
992ea022d16SRodney W. Grimes 	}
9935a392aecSTorsten Blum 	if (anon_only != 0) {
9945a392aecSTorsten Blum 		reply(530, "Sorry, only anonymous ftp allowed.");
9955a392aecSTorsten Blum 		return;
9965a392aecSTorsten Blum 	}
9975a392aecSTorsten Blum 
9989aca17cbSMark Murray 	if ((pw = sgetpwnam(name))) {
999ea022d16SRodney W. Grimes 		if ((shell = pw->pw_shell) == NULL || *shell == 0)
1000ea022d16SRodney W. Grimes 			shell = _PATH_BSHELL;
1001c433c9daSPhilippe Charnier 		setusershell();
1002ea022d16SRodney W. Grimes 		while ((cp = getusershell()) != NULL)
1003ea022d16SRodney W. Grimes 			if (strcmp(cp, shell) == 0)
1004ea022d16SRodney W. Grimes 				break;
1005ea022d16SRodney W. Grimes 		endusershell();
1006ea022d16SRodney W. Grimes 
10078657b576SYaroslav Tykhiy 		if (cp == NULL || checkuser(_PATH_FTPUSERS, name, 1, NULL)) {
1008ea022d16SRodney W. Grimes 			reply(530, "User %s access denied.", name);
1009ea022d16SRodney W. Grimes 			if (logging)
1010ea022d16SRodney W. Grimes 				syslog(LOG_NOTICE,
1011ea022d16SRodney W. Grimes 				    "FTP LOGIN REFUSED FROM %s, %s",
1012ea022d16SRodney W. Grimes 				    remotehost, name);
10137e295315SYaroslav Tykhiy 			pw = NULL;
1014ea022d16SRodney W. Grimes 			return;
1015ea022d16SRodney W. Grimes 		}
1016ea022d16SRodney W. Grimes 	}
1017ea022d16SRodney W. Grimes 	if (logging)
1018ea022d16SRodney W. Grimes 		strncpy(curname, name, sizeof(curname)-1);
101947499ecdSAndrey A. Chernov 
102047499ecdSAndrey A. Chernov 	pwok = 0;
1021fa1746c9SMark Murray #ifdef USE_PAM
1022fa1746c9SMark Murray 	/* XXX Kluge! The conversation mechanism needs to be fixed. */
1023726040deSGuido van Rooij #endif
102447499ecdSAndrey A. Chernov 	if (opiechallenge(&opiedata, name, opieprompt) == 0) {
102547499ecdSAndrey A. Chernov 		pwok = (pw != NULL) &&
102647499ecdSAndrey A. Chernov 		       opieaccessfile(remotehost) &&
102747499ecdSAndrey A. Chernov 		       opiealways(pw->pw_dir);
102847499ecdSAndrey A. Chernov 		reply(331, "Response to %s %s for %s.",
102947499ecdSAndrey A. Chernov 		      opieprompt, pwok ? "requested" : "required", name);
103047499ecdSAndrey A. Chernov 	} else {
103147499ecdSAndrey A. Chernov 		pwok = 1;
103247499ecdSAndrey A. Chernov 		reply(331, "Password required for %s.", name);
103347499ecdSAndrey A. Chernov 	}
1034ea022d16SRodney W. Grimes 	askpasswd = 1;
1035ea022d16SRodney W. Grimes 	/*
1036ea022d16SRodney W. Grimes 	 * Delay before reading passwd after first failed
1037ea022d16SRodney W. Grimes 	 * attempt to slow down passwd-guessing programs.
1038ea022d16SRodney W. Grimes 	 */
1039ea022d16SRodney W. Grimes 	if (login_attempts)
1040e3765043SYaroslav Tykhiy 		sleep(login_attempts);
1041ea022d16SRodney W. Grimes }
1042ea022d16SRodney W. Grimes 
1043ea022d16SRodney W. Grimes /*
10448657b576SYaroslav Tykhiy  * Check if a user is in the file "fname",
10458657b576SYaroslav Tykhiy  * return a pointer to a malloc'd string with the rest
10468657b576SYaroslav Tykhiy  * of the matching line in "residue" if not NULL.
1047ea022d16SRodney W. Grimes  */
1048ea022d16SRodney W. Grimes static int
10498657b576SYaroslav Tykhiy checkuser(char *fname, char *name, int pwset, char **residue)
1050ea022d16SRodney W. Grimes {
1051ea022d16SRodney W. Grimes 	FILE *fd;
1052ea022d16SRodney W. Grimes 	int found = 0;
1053737d08f3SYaroslav Tykhiy 	size_t len;
1054737d08f3SYaroslav Tykhiy 	char *line, *mp, *p;
1055ea022d16SRodney W. Grimes 
1056a5a4544eSPaul Traina 	if ((fd = fopen(fname, "r")) != NULL) {
1057737d08f3SYaroslav Tykhiy 		while (!found && (line = fgetln(fd, &len)) != NULL) {
1058737d08f3SYaroslav Tykhiy 			/* skip comments */
1059ea022d16SRodney W. Grimes 			if (line[0] == '#')
1060ea022d16SRodney W. Grimes 				continue;
1061737d08f3SYaroslav Tykhiy 			if (line[len - 1] == '\n') {
1062737d08f3SYaroslav Tykhiy 				line[len - 1] = '\0';
1063737d08f3SYaroslav Tykhiy 				mp = NULL;
1064737d08f3SYaroslav Tykhiy 			} else {
1065737d08f3SYaroslav Tykhiy 				if ((mp = malloc(len + 1)) == NULL)
1066737d08f3SYaroslav Tykhiy 					fatalerror("Ran out of memory.");
1067737d08f3SYaroslav Tykhiy 				memcpy(mp, line, len);
1068737d08f3SYaroslav Tykhiy 				mp[len] = '\0';
1069737d08f3SYaroslav Tykhiy 				line = mp;
1070737d08f3SYaroslav Tykhiy 			}
1071737d08f3SYaroslav Tykhiy 			/* avoid possible leading and trailing whitespace */
1072737d08f3SYaroslav Tykhiy 			p = strtok(line, " \t");
1073737d08f3SYaroslav Tykhiy 			/* skip empty lines */
1074737d08f3SYaroslav Tykhiy 			if (p == NULL)
1075737d08f3SYaroslav Tykhiy 				goto nextline;
107631fea7b8SDavid Nugent 			/*
107731fea7b8SDavid Nugent 			 * if first chr is '@', check group membership
107831fea7b8SDavid Nugent 			 */
1079737d08f3SYaroslav Tykhiy 			if (p[0] == '@') {
108031fea7b8SDavid Nugent 				int i = 0;
108131fea7b8SDavid Nugent 				struct group *grp;
108231fea7b8SDavid Nugent 
10838657b576SYaroslav Tykhiy 				if (p[1] == '\0') /* single @ matches anyone */
10848657b576SYaroslav Tykhiy 					found = 1;
10858657b576SYaroslav Tykhiy 				else {
1086737d08f3SYaroslav Tykhiy 					if ((grp = getgrnam(p+1)) == NULL)
1087737d08f3SYaroslav Tykhiy 						goto nextline;
10887edcb936SSteve Price 					/*
10897edcb936SSteve Price 					 * Check user's default group
10907edcb936SSteve Price 					 */
10917edcb936SSteve Price 					if (pwset && grp->gr_gid == pw->pw_gid)
10927edcb936SSteve Price 						found = 1;
10937edcb936SSteve Price 					/*
10947edcb936SSteve Price 					 * Check supplementary groups
10957edcb936SSteve Price 					 */
109631fea7b8SDavid Nugent 					while (!found && grp->gr_mem[i])
109731fea7b8SDavid Nugent 						found = strcmp(name,
109831fea7b8SDavid Nugent 							grp->gr_mem[i++])
109931fea7b8SDavid Nugent 							== 0;
1100ea022d16SRodney W. Grimes 				}
11018657b576SYaroslav Tykhiy 			}
110231fea7b8SDavid Nugent 			/*
110331fea7b8SDavid Nugent 			 * Otherwise, just check for username match
110431fea7b8SDavid Nugent 			 */
110531fea7b8SDavid Nugent 			else
1106737d08f3SYaroslav Tykhiy 				found = strcmp(p, name) == 0;
11078657b576SYaroslav Tykhiy 			/*
11088657b576SYaroslav Tykhiy 			 * Save the rest of line to "residue" if matched
11098657b576SYaroslav Tykhiy 			 */
11108657b576SYaroslav Tykhiy 			if (found && residue) {
11110ba71e24SYaroslav Tykhiy 				if ((p = strtok(NULL, "")) != NULL)
11120ba71e24SYaroslav Tykhiy 					p += strspn(p, " \t");
11130ba71e24SYaroslav Tykhiy 				if (p && *p) {
11148657b576SYaroslav Tykhiy 				 	if ((*residue = strdup(p)) == NULL)
11158657b576SYaroslav Tykhiy 						fatalerror("Ran out of memory.");
11168657b576SYaroslav Tykhiy 				} else
11178657b576SYaroslav Tykhiy 					*residue = NULL;
11188657b576SYaroslav Tykhiy 			}
1119737d08f3SYaroslav Tykhiy nextline:
1120737d08f3SYaroslav Tykhiy 			if (mp)
1121737d08f3SYaroslav Tykhiy 				free(mp);
1122ea022d16SRodney W. Grimes 		}
1123ea022d16SRodney W. Grimes 		(void) fclose(fd);
1124ea022d16SRodney W. Grimes 	}
1125ea022d16SRodney W. Grimes 	return (found);
1126ea022d16SRodney W. Grimes }
1127ea022d16SRodney W. Grimes 
1128ea022d16SRodney W. Grimes /*
1129ea022d16SRodney W. Grimes  * Terminate login as previous user, if any, resetting state;
1130ea022d16SRodney W. Grimes  * used when USER command is given or login fails.
1131ea022d16SRodney W. Grimes  */
1132ea022d16SRodney W. Grimes static void
1133e4bc453cSWarner Losh end_login(void)
1134ea022d16SRodney W. Grimes {
11355bc9d93dSMark Murray #ifdef USE_PAM
11365bc9d93dSMark Murray 	int e;
11375bc9d93dSMark Murray #endif
1138ea022d16SRodney W. Grimes 
113952e7ee74SYaroslav Tykhiy 	(void) seteuid(0);
11405d7e0128SYaroslav Tykhiy 	if (logged_in && dowtmp)
114146948173SHajimu UMEMOTO 		ftpd_logwtmp(ttyline, "", NULL);
1142ea022d16SRodney W. Grimes 	pw = NULL;
1143b071c689SDavid Nugent #ifdef	LOGIN_CAP
114452e7ee74SYaroslav Tykhiy 	setusercontext(NULL, getpwuid(0), 0,
1145d9e2c424SRobert Watson 		       LOGIN_SETPRIORITY|LOGIN_SETRESOURCES|LOGIN_SETUMASK|
1146d9e2c424SRobert Watson 		       LOGIN_SETMAC);
1147b071c689SDavid Nugent #endif
11485bc9d93dSMark Murray #ifdef USE_PAM
1149de45162dSYaroslav Tykhiy 	if (pamh) {
11505bc9d93dSMark Murray 		if ((e = pam_setcred(pamh, PAM_DELETE_CRED)) != PAM_SUCCESS)
11515bc9d93dSMark Murray 			syslog(LOG_ERR, "pam_setcred: %s", pam_strerror(pamh, e));
11525bc9d93dSMark Murray 		if ((e = pam_close_session(pamh,0)) != PAM_SUCCESS)
11535bc9d93dSMark Murray 			syslog(LOG_ERR, "pam_close_session: %s", pam_strerror(pamh, e));
11545bc9d93dSMark Murray 		if ((e = pam_end(pamh, e)) != PAM_SUCCESS)
11555bc9d93dSMark Murray 			syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e));
11565bc9d93dSMark Murray 		pamh = NULL;
1157de45162dSYaroslav Tykhiy 	}
11585bc9d93dSMark Murray #endif
1159ea022d16SRodney W. Grimes 	logged_in = 0;
1160ea022d16SRodney W. Grimes 	guest = 0;
1161a5a4544eSPaul Traina 	dochroot = 0;
1162ea022d16SRodney W. Grimes }
1163ea022d16SRodney W. Grimes 
11645bc9d93dSMark Murray #ifdef USE_PAM
11656c9134c0SMark Murray 
11666c9134c0SMark Murray /*
11676c9134c0SMark Murray  * the following code is stolen from imap-uw PAM authentication module and
11686c9134c0SMark Murray  * login.c
11696c9134c0SMark Murray  */
11706c9134c0SMark Murray #define COPY_STRING(s) (s ? strdup(s) : NULL)
11716c9134c0SMark Murray 
11726c9134c0SMark Murray struct cred_t {
11736c9134c0SMark Murray 	const char *uname;		/* user name */
11746c9134c0SMark Murray 	const char *pass;		/* password */
11756c9134c0SMark Murray };
11766c9134c0SMark Murray typedef struct cred_t cred_t;
11776c9134c0SMark Murray 
11786c9134c0SMark Murray static int
11796c9134c0SMark Murray auth_conv(int num_msg, const struct pam_message **msg,
11806c9134c0SMark Murray 	  struct pam_response **resp, void *appdata)
11816c9134c0SMark Murray {
11826c9134c0SMark Murray 	int i;
11836c9134c0SMark Murray 	cred_t *cred = (cred_t *) appdata;
118460769b19SDag-Erling Smørgrav 	struct pam_response *reply;
118560769b19SDag-Erling Smørgrav 
118660769b19SDag-Erling Smørgrav 	reply = calloc(num_msg, sizeof *reply);
118760769b19SDag-Erling Smørgrav 	if (reply == NULL)
118860769b19SDag-Erling Smørgrav 		return PAM_BUF_ERR;
11896c9134c0SMark Murray 
11906c9134c0SMark Murray 	for (i = 0; i < num_msg; i++) {
11916c9134c0SMark Murray 		switch (msg[i]->msg_style) {
11926c9134c0SMark Murray 		case PAM_PROMPT_ECHO_ON:	/* assume want user name */
11936c9134c0SMark Murray 			reply[i].resp_retcode = PAM_SUCCESS;
11946c9134c0SMark Murray 			reply[i].resp = COPY_STRING(cred->uname);
11956c9134c0SMark Murray 			/* PAM frees resp. */
11966c9134c0SMark Murray 			break;
11976c9134c0SMark Murray 		case PAM_PROMPT_ECHO_OFF:	/* assume want password */
11986c9134c0SMark Murray 			reply[i].resp_retcode = PAM_SUCCESS;
11996c9134c0SMark Murray 			reply[i].resp = COPY_STRING(cred->pass);
12006c9134c0SMark Murray 			/* PAM frees resp. */
12016c9134c0SMark Murray 			break;
12026c9134c0SMark Murray 		case PAM_TEXT_INFO:
12036c9134c0SMark Murray 		case PAM_ERROR_MSG:
12046c9134c0SMark Murray 			reply[i].resp_retcode = PAM_SUCCESS;
12056c9134c0SMark Murray 			reply[i].resp = NULL;
12066c9134c0SMark Murray 			break;
12076c9134c0SMark Murray 		default:			/* unknown message style */
12086c9134c0SMark Murray 			free(reply);
12096c9134c0SMark Murray 			return PAM_CONV_ERR;
12106c9134c0SMark Murray 		}
12116c9134c0SMark Murray 	}
12126c9134c0SMark Murray 
12136c9134c0SMark Murray 	*resp = reply;
12146c9134c0SMark Murray 	return PAM_SUCCESS;
12156c9134c0SMark Murray }
12166c9134c0SMark Murray 
12176c9134c0SMark Murray /*
12186c9134c0SMark Murray  * Attempt to authenticate the user using PAM.  Returns 0 if the user is
12196c9134c0SMark Murray  * authenticated, or 1 if not authenticated.  If some sort of PAM system
12206c9134c0SMark Murray  * error occurs (e.g., the "/etc/pam.conf" file is missing) then this
12216c9134c0SMark Murray  * function returns -1.  This can be used as an indication that we should
12226c9134c0SMark Murray  * fall back to a different authentication mechanism.
12236c9134c0SMark Murray  */
12246c9134c0SMark Murray static int
12256c9134c0SMark Murray auth_pam(struct passwd **ppw, const char *pass)
12266c9134c0SMark Murray {
12276c9134c0SMark Murray 	const char *tmpl_user;
12286c9134c0SMark Murray 	const void *item;
12296c9134c0SMark Murray 	int rval;
12306c9134c0SMark Murray 	int e;
12316c9134c0SMark Murray 	cred_t auth_cred = { (*ppw)->pw_name, pass };
12326c9134c0SMark Murray 	struct pam_conv conv = { &auth_conv, &auth_cred };
12336c9134c0SMark Murray 
12346c9134c0SMark Murray 	e = pam_start("ftpd", (*ppw)->pw_name, &conv, &pamh);
12356c9134c0SMark Murray 	if (e != PAM_SUCCESS) {
1236545ea864SYaroslav Tykhiy 		/*
1237545ea864SYaroslav Tykhiy 		 * In OpenPAM, it's OK to pass NULL to pam_strerror()
1238545ea864SYaroslav Tykhiy 		 * if context creation has failed in the first place.
1239545ea864SYaroslav Tykhiy 		 */
1240545ea864SYaroslav Tykhiy 		syslog(LOG_ERR, "pam_start: %s", pam_strerror(NULL, e));
12416c9134c0SMark Murray 		return -1;
12426c9134c0SMark Murray 	}
12436c9134c0SMark Murray 
1244ea413ab7SGuido van Rooij 	e = pam_set_item(pamh, PAM_RHOST, remotehost);
1245ea413ab7SGuido van Rooij 	if (e != PAM_SUCCESS) {
1246ea413ab7SGuido van Rooij 		syslog(LOG_ERR, "pam_set_item(PAM_RHOST): %s",
1247ea413ab7SGuido van Rooij 			pam_strerror(pamh, e));
1248de45162dSYaroslav Tykhiy 		if ((e = pam_end(pamh, e)) != PAM_SUCCESS) {
1249de45162dSYaroslav Tykhiy 			syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e));
1250de45162dSYaroslav Tykhiy 		}
1251de45162dSYaroslav Tykhiy 		pamh = NULL;
1252ea413ab7SGuido van Rooij 		return -1;
1253ea413ab7SGuido van Rooij 	}
1254ea413ab7SGuido van Rooij 
12556c9134c0SMark Murray 	e = pam_authenticate(pamh, 0);
12566c9134c0SMark Murray 	switch (e) {
12576c9134c0SMark Murray 	case PAM_SUCCESS:
12586c9134c0SMark Murray 		/*
12596c9134c0SMark Murray 		 * With PAM we support the concept of a "template"
12606c9134c0SMark Murray 		 * user.  The user enters a login name which is
12616c9134c0SMark Murray 		 * authenticated by PAM, usually via a remote service
12626c9134c0SMark Murray 		 * such as RADIUS or TACACS+.  If authentication
12636c9134c0SMark Murray 		 * succeeds, a different but related "template" name
12646c9134c0SMark Murray 		 * is used for setting the credentials, shell, and
12656c9134c0SMark Murray 		 * home directory.  The name the user enters need only
12666c9134c0SMark Murray 		 * exist on the remote authentication server, but the
12676c9134c0SMark Murray 		 * template name must be present in the local password
12686c9134c0SMark Murray 		 * database.
12696c9134c0SMark Murray 		 *
12706c9134c0SMark Murray 		 * This is supported by two various mechanisms in the
12716c9134c0SMark Murray 		 * individual modules.  However, from the application's
12726c9134c0SMark Murray 		 * point of view, the template user is always passed
12736c9134c0SMark Murray 		 * back as a changed value of the PAM_USER item.
12746c9134c0SMark Murray 		 */
12756c9134c0SMark Murray 		if ((e = pam_get_item(pamh, PAM_USER, &item)) ==
12766c9134c0SMark Murray 		    PAM_SUCCESS) {
12776c9134c0SMark Murray 			tmpl_user = (const char *) item;
12786c9134c0SMark Murray 			if (strcmp((*ppw)->pw_name, tmpl_user) != 0)
12796c9134c0SMark Murray 				*ppw = getpwnam(tmpl_user);
12806c9134c0SMark Murray 		} else
12816c9134c0SMark Murray 			syslog(LOG_ERR, "Couldn't get PAM_USER: %s",
12826c9134c0SMark Murray 			    pam_strerror(pamh, e));
12836c9134c0SMark Murray 		rval = 0;
12846c9134c0SMark Murray 		break;
12856c9134c0SMark Murray 
12866c9134c0SMark Murray 	case PAM_AUTH_ERR:
12876c9134c0SMark Murray 	case PAM_USER_UNKNOWN:
12886c9134c0SMark Murray 	case PAM_MAXTRIES:
12896c9134c0SMark Murray 		rval = 1;
12906c9134c0SMark Murray 		break;
12916c9134c0SMark Murray 
12926c9134c0SMark Murray 	default:
12935bc9d93dSMark Murray 		syslog(LOG_ERR, "pam_authenticate: %s", pam_strerror(pamh, e));
12946c9134c0SMark Murray 		rval = -1;
12956c9134c0SMark Murray 		break;
12966c9134c0SMark Murray 	}
12976c9134c0SMark Murray 
12985bc9d93dSMark Murray 	if (rval == 0) {
12995bc9d93dSMark Murray 		e = pam_acct_mgmt(pamh, 0);
13005bc9d93dSMark Murray 		if (e == PAM_NEW_AUTHTOK_REQD) {
13015bc9d93dSMark Murray 			e = pam_chauthtok(pamh, PAM_CHANGE_EXPIRED_AUTHTOK);
13025bc9d93dSMark Murray 			if (e != PAM_SUCCESS) {
13035bc9d93dSMark Murray 				syslog(LOG_ERR, "pam_chauthtok: %s", pam_strerror(pamh, e));
13045bc9d93dSMark Murray 				rval = 1;
13055bc9d93dSMark Murray 			}
13065bc9d93dSMark Murray 		} else if (e != PAM_SUCCESS) {
13075bc9d93dSMark Murray 			rval = 1;
13085bc9d93dSMark Murray 		}
13095bc9d93dSMark Murray 	}
13105bc9d93dSMark Murray 
13115bc9d93dSMark Murray 	if (rval != 0) {
13126c9134c0SMark Murray 		if ((e = pam_end(pamh, e)) != PAM_SUCCESS) {
13136c9134c0SMark Murray 			syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e));
13145bc9d93dSMark Murray 		}
13155bc9d93dSMark Murray 		pamh = NULL;
13166c9134c0SMark Murray 	}
13176c9134c0SMark Murray 	return rval;
13186c9134c0SMark Murray }
13196c9134c0SMark Murray 
13205bc9d93dSMark Murray #endif /* USE_PAM */
13216c9134c0SMark Murray 
1322ea022d16SRodney W. Grimes void
1323e4bc453cSWarner Losh pass(char *passwd)
1324ea022d16SRodney W. Grimes {
1325a5a4544eSPaul Traina 	int rval;
1326ea022d16SRodney W. Grimes 	FILE *fd;
1327b071c689SDavid Nugent #ifdef	LOGIN_CAP
1328b071c689SDavid Nugent 	login_cap_t *lc = NULL;
1329b071c689SDavid Nugent #endif
13305bc9d93dSMark Murray #ifdef USE_PAM
13315bc9d93dSMark Murray 	int e;
13325bc9d93dSMark Murray #endif
1333341e476eSYaroslav Tykhiy 	char *residue = NULL;
133447499ecdSAndrey A. Chernov 	char *xpasswd;
1335ea022d16SRodney W. Grimes 
1336ea022d16SRodney W. Grimes 	if (logged_in || askpasswd == 0) {
1337ea022d16SRodney W. Grimes 		reply(503, "Login with USER first.");
1338ea022d16SRodney W. Grimes 		return;
1339ea022d16SRodney W. Grimes 	}
1340ea022d16SRodney W. Grimes 	askpasswd = 0;
1341ea022d16SRodney W. Grimes 	if (!guest) {		/* "ftp" is only account allowed no password */
1342a5a4544eSPaul Traina 		if (pw == NULL) {
1343a5a4544eSPaul Traina 			rval = 1;	/* failure below */
1344a5a4544eSPaul Traina 			goto skip;
1345a5a4544eSPaul Traina 		}
13465bc9d93dSMark Murray #ifdef USE_PAM
13476c9134c0SMark Murray 		rval = auth_pam(&pw, passwd);
1348f650a124SAndrey A. Chernov 		if (rval >= 0) {
1349f650a124SAndrey A. Chernov 			opieunlock();
1350a5a4544eSPaul Traina 			goto skip;
1351f650a124SAndrey A. Chernov 		}
1352f650a124SAndrey A. Chernov #endif
135347499ecdSAndrey A. Chernov 		if (opieverify(&opiedata, passwd) == 0)
135447499ecdSAndrey A. Chernov 			xpasswd = pw->pw_passwd;
1355f650a124SAndrey A. Chernov 		else if (pwok) {
135647499ecdSAndrey A. Chernov 			xpasswd = crypt(passwd, pw->pw_passwd);
1357f650a124SAndrey A. Chernov 			if (passwd[0] == '\0' && pw->pw_passwd[0] != '\0')
1358f650a124SAndrey A. Chernov 				xpasswd = ":";
1359f650a124SAndrey A. Chernov 		} else {
136047499ecdSAndrey A. Chernov 			rval = 1;
136147499ecdSAndrey A. Chernov 			goto skip;
136247499ecdSAndrey A. Chernov 		}
136347499ecdSAndrey A. Chernov 		rval = strcmp(pw->pw_passwd, xpasswd);
1364f650a124SAndrey A. Chernov 		if (pw->pw_expire && time(NULL) >= pw->pw_expire)
1365a5a4544eSPaul Traina 			rval = 1;	/* failure */
1366a5a4544eSPaul Traina skip:
1367a5a4544eSPaul Traina 		/*
1368a5a4544eSPaul Traina 		 * If rval == 1, the user failed the authentication check
13696c9134c0SMark Murray 		 * above.  If rval == 0, either PAM or local authentication
1370a5a4544eSPaul Traina 		 * succeeded.
1371a5a4544eSPaul Traina 		 */
1372a5a4544eSPaul Traina 		if (rval) {
1373ea022d16SRodney W. Grimes 			reply(530, "Login incorrect.");
1374b8939f6fSYaroslav Tykhiy 			if (logging) {
1375ea022d16SRodney W. Grimes 				syslog(LOG_NOTICE,
1376b8939f6fSYaroslav Tykhiy 				    "FTP LOGIN FAILED FROM %s",
1377b8939f6fSYaroslav Tykhiy 				    remotehost);
1378b8939f6fSYaroslav Tykhiy 				syslog(LOG_AUTHPRIV | LOG_NOTICE,
1379ea022d16SRodney W. Grimes 				    "FTP LOGIN FAILED FROM %s, %s",
1380ea022d16SRodney W. Grimes 				    remotehost, curname);
1381b8939f6fSYaroslav Tykhiy 			}
1382ea022d16SRodney W. Grimes 			pw = NULL;
1383ea022d16SRodney W. Grimes 			if (login_attempts++ >= 5) {
1384ea022d16SRodney W. Grimes 				syslog(LOG_NOTICE,
1385ea022d16SRodney W. Grimes 				    "repeated login failures from %s",
1386ea022d16SRodney W. Grimes 				    remotehost);
1387ea022d16SRodney W. Grimes 				exit(0);
1388ea022d16SRodney W. Grimes 			}
1389ea022d16SRodney W. Grimes 			return;
1390ea022d16SRodney W. Grimes 		}
1391ea022d16SRodney W. Grimes 	}
1392ea022d16SRodney W. Grimes 	login_attempts = 0;		/* this time successful */
1393c4536e21SYaroslav Tykhiy 	if (setegid(pw->pw_gid) < 0) {
1394ea022d16SRodney W. Grimes 		reply(550, "Can't set gid.");
1395ea022d16SRodney W. Grimes 		return;
1396ea022d16SRodney W. Grimes 	}
1397b071c689SDavid Nugent 	/* May be overridden by login.conf */
1398b071c689SDavid Nugent 	(void) umask(defumask);
1399b071c689SDavid Nugent #ifdef	LOGIN_CAP
14005d0bfe39SDavid Nugent 	if ((lc = login_getpwclass(pw)) != NULL) {
140104683b2cSYaroslav Tykhiy 		char	remote_ip[NI_MAXHOST];
1402b071c689SDavid Nugent 
140304683b2cSYaroslav Tykhiy 		if (getnameinfo((struct sockaddr *)&his_addr, his_addr.su_len,
14044dd8b5abSYoshinobu Inoue 			remote_ip, sizeof(remote_ip) - 1, NULL, 0,
140504683b2cSYaroslav Tykhiy 			NI_NUMERICHOST))
140604683b2cSYaroslav Tykhiy 				*remote_ip = 0;
1407b071c689SDavid Nugent 		remote_ip[sizeof(remote_ip) - 1] = 0;
1408b071c689SDavid Nugent 		if (!auth_hostok(lc, remotehost, remote_ip)) {
1409b071c689SDavid Nugent 			syslog(LOG_INFO|LOG_AUTH,
1410b071c689SDavid Nugent 			    "FTP LOGIN FAILED (HOST) as %s: permission denied.",
1411b071c689SDavid Nugent 			    pw->pw_name);
14124a3e5acdSYaroslav Tykhiy 			reply(530, "Permission denied.");
1413b071c689SDavid Nugent 			pw = NULL;
1414b071c689SDavid Nugent 			return;
1415b071c689SDavid Nugent 		}
1416b071c689SDavid Nugent 		if (!auth_timeok(lc, time(NULL))) {
14174a3e5acdSYaroslav Tykhiy 			reply(530, "Login not available right now.");
1418b071c689SDavid Nugent 			pw = NULL;
1419b071c689SDavid Nugent 			return;
1420b071c689SDavid Nugent 		}
1421b071c689SDavid Nugent 	}
142252e7ee74SYaroslav Tykhiy 	setusercontext(lc, pw, 0,
1423e6fa0d43SDag-Erling Smørgrav 		LOGIN_SETLOGIN|LOGIN_SETGROUP|LOGIN_SETPRIORITY|
1424d9e2c424SRobert Watson 		LOGIN_SETRESOURCES|LOGIN_SETUMASK|LOGIN_SETMAC);
1425b071c689SDavid Nugent #else
1426e6fa0d43SDag-Erling Smørgrav 	setlogin(pw->pw_name);
1427ea022d16SRodney W. Grimes 	(void) initgroups(pw->pw_name, pw->pw_gid);
1428b071c689SDavid Nugent #endif
1429ea022d16SRodney W. Grimes 
14305bc9d93dSMark Murray #ifdef USE_PAM
14315bc9d93dSMark Murray 	if (pamh) {
14325bc9d93dSMark Murray 		if ((e = pam_open_session(pamh, 0)) != PAM_SUCCESS) {
14335bc9d93dSMark Murray 			syslog(LOG_ERR, "pam_open_session: %s", pam_strerror(pamh, e));
14345bc9d93dSMark Murray 		} else if ((e = pam_setcred(pamh, PAM_ESTABLISH_CRED)) != PAM_SUCCESS) {
14355bc9d93dSMark Murray 			syslog(LOG_ERR, "pam_setcred: %s", pam_strerror(pamh, e));
14365bc9d93dSMark Murray 		}
14375bc9d93dSMark Murray 	}
14385bc9d93dSMark Murray #endif
14395bc9d93dSMark Murray 
1440ea022d16SRodney W. Grimes 	/* open wtmp before chroot */
14415d7e0128SYaroslav Tykhiy 	if (dowtmp)
14425d7e0128SYaroslav Tykhiy 		ftpd_logwtmp(ttyline, pw->pw_name,
14435d7e0128SYaroslav Tykhiy 		    (struct sockaddr *)&his_addr);
1444ea022d16SRodney W. Grimes 	logged_in = 1;
1445ea022d16SRodney W. Grimes 
1446a5a4544eSPaul Traina 	if (guest && stats && statfd < 0)
1447ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
144863047c6fSDavid E. O'Brien 		statfd = open(thishost->statfile, O_WRONLY|O_APPEND);
1449ea4e54b9SDavid Nugent #else
145063047c6fSDavid E. O'Brien 		statfd = open(_PATH_FTPDSTATFILE, O_WRONLY|O_APPEND);
1451ea4e54b9SDavid Nugent #endif
145263047c6fSDavid E. O'Brien 		if (statfd < 0)
14533eb568f2SGuido van Rooij 			stats = 0;
14543eb568f2SGuido van Rooij 
1455b071c689SDavid Nugent 	dochroot =
1456341e476eSYaroslav Tykhiy 		checkuser(_PATH_FTPCHROOT, pw->pw_name, 1, &residue)
1457b071c689SDavid Nugent #ifdef	LOGIN_CAP	/* Allow login.conf configuration as well */
14588657b576SYaroslav Tykhiy 		|| login_getcapbool(lc, "ftp-chroot", 0)
1459b071c689SDavid Nugent #endif
14608657b576SYaroslav Tykhiy 	;
1461ce9287fcSYaroslav Tykhiy 	chrootdir = NULL;
1462ea022d16SRodney W. Grimes 	/*
1463ce9287fcSYaroslav Tykhiy 	 * For a chrooted local user,
1464ce9287fcSYaroslav Tykhiy 	 * a) see whether ftpchroot(5) specifies a chroot directory,
1465ce9287fcSYaroslav Tykhiy 	 * b) extract the directory pathname from the line,
1466ce9287fcSYaroslav Tykhiy 	 * c) expand it to the absolute pathname if necessary.
1467ea022d16SRodney W. Grimes 	 */
1468ce9287fcSYaroslav Tykhiy 	if (dochroot && residue &&
146939bce482SYaroslav Tykhiy 	    (chrootdir = strtok(residue, " \t")) != NULL) {
147039bce482SYaroslav Tykhiy 		if (chrootdir[0] != '/')
1471341e476eSYaroslav Tykhiy 			asprintf(&chrootdir, "%s/%s", pw->pw_dir, chrootdir);
147239bce482SYaroslav Tykhiy 		else
1473215a9f9dSYaroslav Tykhiy 			chrootdir = strdup(chrootdir); /* make it permanent */
1474341e476eSYaroslav Tykhiy 		if (chrootdir == NULL)
14758657b576SYaroslav Tykhiy 			fatalerror("Ran out of memory.");
14768657b576SYaroslav Tykhiy 	}
1477ce9287fcSYaroslav Tykhiy 	if (guest || dochroot) {
1478ce9287fcSYaroslav Tykhiy 		/*
1479ce9287fcSYaroslav Tykhiy 		 * If no chroot directory set yet, use the login directory.
1480ce9287fcSYaroslav Tykhiy 		 * Copy it so it can be modified while pw->pw_dir stays intact.
1481ce9287fcSYaroslav Tykhiy 		 */
1482ce9287fcSYaroslav Tykhiy 		if (chrootdir == NULL &&
1483ce9287fcSYaroslav Tykhiy 		    (chrootdir = strdup(pw->pw_dir)) == NULL)
1484ce9287fcSYaroslav Tykhiy 			fatalerror("Ran out of memory.");
1485ce9287fcSYaroslav Tykhiy 		/*
1486ce9287fcSYaroslav Tykhiy 		 * Check for the "/chroot/./home" syntax,
1487ce9287fcSYaroslav Tykhiy 		 * separate the chroot and home directory pathnames.
1488ce9287fcSYaroslav Tykhiy 		 */
1489ce9287fcSYaroslav Tykhiy 		if ((homedir = strstr(chrootdir, "/./")) != NULL) {
1490ce9287fcSYaroslav Tykhiy 			*(homedir++) = '\0';	/* wipe '/' */
1491ce9287fcSYaroslav Tykhiy 			homedir++;		/* skip '.' */
1492ce9287fcSYaroslav Tykhiy 		} else {
1493ce9287fcSYaroslav Tykhiy 			/*
1494ce9287fcSYaroslav Tykhiy 			 * We MUST do a chdir() after the chroot. Otherwise
1495ce9287fcSYaroslav Tykhiy 			 * the old current directory will be accessible as "."
1496ce9287fcSYaroslav Tykhiy 			 * outside the new root!
1497ce9287fcSYaroslav Tykhiy 			 */
1498ce9287fcSYaroslav Tykhiy 			homedir = "/";
1499ce9287fcSYaroslav Tykhiy 		}
1500ce9287fcSYaroslav Tykhiy 		/*
1501ce9287fcSYaroslav Tykhiy 		 * Finally, do chroot()
1502ce9287fcSYaroslav Tykhiy 		 */
1503ce9287fcSYaroslav Tykhiy 		if (chroot(chrootdir) < 0) {
1504a5a4544eSPaul Traina 			reply(550, "Can't change root.");
1505a5a4544eSPaul Traina 			goto bad;
1506a5a4544eSPaul Traina 		}
1507ce9287fcSYaroslav Tykhiy 	} else	/* real user w/o chroot */
1508ce9287fcSYaroslav Tykhiy 		homedir = pw->pw_dir;
1509ce9287fcSYaroslav Tykhiy 	/*
1510ce9287fcSYaroslav Tykhiy 	 * Set euid *before* doing chdir() so
1511ce9287fcSYaroslav Tykhiy 	 * a) the user won't be carried to a directory that he couldn't reach
1512ce9287fcSYaroslav Tykhiy 	 *    on his own due to no permission to upper path components,
1513ce9287fcSYaroslav Tykhiy 	 * b) NFS mounted homedirs w/restrictive permissions will be accessible
1514ce9287fcSYaroslav Tykhiy 	 *    (uid 0 has no root power over NFS if not mapped explicitly.)
1515ce9287fcSYaroslav Tykhiy 	 */
151652e7ee74SYaroslav Tykhiy 	if (seteuid(pw->pw_uid) < 0) {
1517ea022d16SRodney W. Grimes 		reply(550, "Can't set uid.");
1518ea022d16SRodney W. Grimes 		goto bad;
1519ea022d16SRodney W. Grimes 	}
1520ce9287fcSYaroslav Tykhiy 	if (chdir(homedir) < 0) {
1521ce9287fcSYaroslav Tykhiy 		if (guest || dochroot) {
1522ce9287fcSYaroslav Tykhiy 			reply(550, "Can't change to base directory.");
1523ce9287fcSYaroslav Tykhiy 			goto bad;
1524ce9287fcSYaroslav Tykhiy 		} else {
1525ce9287fcSYaroslav Tykhiy 			if (chdir("/") < 0) {
1526ce9287fcSYaroslav Tykhiy 				reply(550, "Root is inaccessible.");
1527ce9287fcSYaroslav Tykhiy 				goto bad;
1528ce9287fcSYaroslav Tykhiy 			}
152902c97492SYaroslav Tykhiy 			lreply(230, "No directory! Logging in with home=/.");
1530ce9287fcSYaroslav Tykhiy 		}
1531ce9287fcSYaroslav Tykhiy 	}
153282c76939SDavid Greenman 
153382c76939SDavid Greenman 	/*
1534ea022d16SRodney W. Grimes 	 * Display a login message, if it exists.
1535ea022d16SRodney W. Grimes 	 * N.B. reply(230,) must follow the message.
1536ea022d16SRodney W. Grimes 	 */
1537ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
153863047c6fSDavid E. O'Brien 	fd = fopen(thishost->loginmsg, "r");
1539ea4e54b9SDavid Nugent #else
154063047c6fSDavid E. O'Brien 	fd = fopen(_PATH_FTPLOGINMESG, "r");
1541ea4e54b9SDavid Nugent #endif
154263047c6fSDavid E. O'Brien 	if (fd != NULL) {
1543ea022d16SRodney W. Grimes 		char *cp, line[LINE_MAX];
1544ea022d16SRodney W. Grimes 
1545ea022d16SRodney W. Grimes 		while (fgets(line, sizeof(line), fd) != NULL) {
1546ea022d16SRodney W. Grimes 			if ((cp = strchr(line, '\n')) != NULL)
1547ea022d16SRodney W. Grimes 				*cp = '\0';
1548ea022d16SRodney W. Grimes 			lreply(230, "%s", line);
1549ea022d16SRodney W. Grimes 		}
1550ea022d16SRodney W. Grimes 		(void) fflush(stdout);
1551ea022d16SRodney W. Grimes 		(void) fclose(fd);
1552ea022d16SRodney W. Grimes 	}
1553ea022d16SRodney W. Grimes 	if (guest) {
15543eb568f2SGuido van Rooij 		if (ident != NULL)
15553eb568f2SGuido van Rooij 			free(ident);
155661f891a6SPaul Traina 		ident = strdup(passwd);
155761f891a6SPaul Traina 		if (ident == NULL)
1558618b0bbaSMark Murray 			fatalerror("Ran out of memory.");
155961f891a6SPaul Traina 
1560ea022d16SRodney W. Grimes 		reply(230, "Guest login ok, access restrictions apply.");
1561ea022d16SRodney W. Grimes #ifdef SETPROCTITLE
1562ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
1563ea4e54b9SDavid Nugent 		if (thishost != firsthost)
1564ea4e54b9SDavid Nugent 			snprintf(proctitle, sizeof(proctitle),
1565b3a0a7cdSMike Heffner 				 "%s: anonymous(%s)/%s", remotehost, hostname,
1566b3a0a7cdSMike Heffner 				 passwd);
1567ea4e54b9SDavid Nugent 		else
1568ea4e54b9SDavid Nugent #endif
1569ea022d16SRodney W. Grimes 			snprintf(proctitle, sizeof(proctitle),
1570b3a0a7cdSMike Heffner 				 "%s: anonymous/%s", remotehost, passwd);
1571b63e1fe2SPeter Wemm 		setproctitle("%s", proctitle);
1572ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */
1573ea022d16SRodney W. Grimes 		if (logging)
1574ea022d16SRodney W. Grimes 			syslog(LOG_INFO, "ANONYMOUS FTP LOGIN FROM %s, %s",
1575ea022d16SRodney W. Grimes 			    remotehost, passwd);
1576ea022d16SRodney W. Grimes 	} else {
15773401a71fSDaniel O'Callaghan 		if (dochroot)
157811342ab1SYaroslav Tykhiy 			reply(230, "User %s logged in, "
157911342ab1SYaroslav Tykhiy 				   "access restrictions apply.", pw->pw_name);
15803401a71fSDaniel O'Callaghan 		else
1581ea022d16SRodney W. Grimes 			reply(230, "User %s logged in.", pw->pw_name);
15823401a71fSDaniel O'Callaghan 
1583ea022d16SRodney W. Grimes #ifdef SETPROCTITLE
1584ea022d16SRodney W. Grimes 		snprintf(proctitle, sizeof(proctitle),
15857a29d7daSYaroslav Tykhiy 			 "%s: user/%s", remotehost, pw->pw_name);
1586b63e1fe2SPeter Wemm 		setproctitle("%s", proctitle);
1587ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */
1588ea022d16SRodney W. Grimes 		if (logging)
1589ea022d16SRodney W. Grimes 			syslog(LOG_INFO, "FTP LOGIN FROM %s as %s",
1590ea022d16SRodney W. Grimes 			    remotehost, pw->pw_name);
1591ea022d16SRodney W. Grimes 	}
1592b071c689SDavid Nugent #ifdef	LOGIN_CAP
1593b071c689SDavid Nugent 	login_close(lc);
1594b071c689SDavid Nugent #endif
1595ce9287fcSYaroslav Tykhiy 	if (residue)
1596ce9287fcSYaroslav Tykhiy 		free(residue);
1597ea022d16SRodney W. Grimes 	return;
1598ea022d16SRodney W. Grimes bad:
1599ea022d16SRodney W. Grimes 	/* Forget all about it... */
1600b071c689SDavid Nugent #ifdef	LOGIN_CAP
1601b071c689SDavid Nugent 	login_close(lc);
1602b071c689SDavid Nugent #endif
1603ce9287fcSYaroslav Tykhiy 	if (residue)
1604ce9287fcSYaroslav Tykhiy 		free(residue);
1605ea022d16SRodney W. Grimes 	end_login();
1606ea022d16SRodney W. Grimes }
1607ea022d16SRodney W. Grimes 
1608ea022d16SRodney W. Grimes void
1609e4bc453cSWarner Losh retrieve(char *cmd, char *name)
1610ea022d16SRodney W. Grimes {
1611ea022d16SRodney W. Grimes 	FILE *fin, *dout;
1612ea022d16SRodney W. Grimes 	struct stat st;
1613e4bc453cSWarner Losh 	int (*closefunc)(FILE *);
1614158a00b2SJohn Birrell 	time_t start;
1615ea022d16SRodney W. Grimes 
1616ea022d16SRodney W. Grimes 	if (cmd == 0) {
1617ea022d16SRodney W. Grimes 		fin = fopen(name, "r"), closefunc = fclose;
1618ea022d16SRodney W. Grimes 		st.st_size = 0;
1619ea022d16SRodney W. Grimes 	} else {
1620ea022d16SRodney W. Grimes 		char line[BUFSIZ];
1621ea022d16SRodney W. Grimes 
1622e760ef2cSWarner Losh 		(void) snprintf(line, sizeof(line), cmd, name), name = line;
1623ea022d16SRodney W. Grimes 		fin = ftpd_popen(line, "r"), closefunc = ftpd_pclose;
1624ea022d16SRodney W. Grimes 		st.st_size = -1;
1625ea022d16SRodney W. Grimes 		st.st_blksize = BUFSIZ;
1626ea022d16SRodney W. Grimes 	}
1627ea022d16SRodney W. Grimes 	if (fin == NULL) {
1628ea022d16SRodney W. Grimes 		if (errno != 0) {
1629ea022d16SRodney W. Grimes 			perror_reply(550, name);
1630ea022d16SRodney W. Grimes 			if (cmd == 0) {
1631ea022d16SRodney W. Grimes 				LOGCMD("get", name);
1632ea022d16SRodney W. Grimes 			}
1633ea022d16SRodney W. Grimes 		}
1634ea022d16SRodney W. Grimes 		return;
1635ea022d16SRodney W. Grimes 	}
1636ea022d16SRodney W. Grimes 	byte_count = -1;
1637ea701226SYaroslav Tykhiy 	if (cmd == 0) {
1638ea701226SYaroslav Tykhiy 		if (fstat(fileno(fin), &st) < 0) {
1639ea701226SYaroslav Tykhiy 			perror_reply(550, name);
1640ea701226SYaroslav Tykhiy 			goto done;
1641ea701226SYaroslav Tykhiy 		}
1642ea701226SYaroslav Tykhiy 		if (!S_ISREG(st.st_mode)) {
164310e89104SYaroslav Tykhiy 			/*
164410e89104SYaroslav Tykhiy 			 * Never sending a raw directory is a workaround
164510e89104SYaroslav Tykhiy 			 * for buggy clients that will attempt to RETR
164610e89104SYaroslav Tykhiy 			 * a directory before listing it, e.g., Mozilla.
164710e89104SYaroslav Tykhiy 			 * Preventing a guest from getting irregular files
164810e89104SYaroslav Tykhiy 			 * is a simple security measure.
164910e89104SYaroslav Tykhiy 			 */
165010e89104SYaroslav Tykhiy 			if (S_ISDIR(st.st_mode) || guest) {
1651ea022d16SRodney W. Grimes 				reply(550, "%s: not a plain file.", name);
1652ea022d16SRodney W. Grimes 				goto done;
1653ea022d16SRodney W. Grimes 			}
1654ea701226SYaroslav Tykhiy 			st.st_size = -1;
1655ea701226SYaroslav Tykhiy 			/* st.st_blksize is set for all descriptor types */
1656ea701226SYaroslav Tykhiy 		}
1657ea701226SYaroslav Tykhiy 	}
1658ea022d16SRodney W. Grimes 	if (restart_point) {
1659ea022d16SRodney W. Grimes 		if (type == TYPE_A) {
1660ea022d16SRodney W. Grimes 			off_t i, n;
1661ea022d16SRodney W. Grimes 			int c;
1662ea022d16SRodney W. Grimes 
1663ea022d16SRodney W. Grimes 			n = restart_point;
1664ea022d16SRodney W. Grimes 			i = 0;
1665ea022d16SRodney W. Grimes 			while (i++ < n) {
1666ea022d16SRodney W. Grimes 				if ((c=getc(fin)) == EOF) {
1667ea022d16SRodney W. Grimes 					perror_reply(550, name);
1668ea022d16SRodney W. Grimes 					goto done;
1669ea022d16SRodney W. Grimes 				}
1670ea022d16SRodney W. Grimes 				if (c == '\n')
1671ea022d16SRodney W. Grimes 					i++;
1672ea022d16SRodney W. Grimes 			}
1673ea022d16SRodney W. Grimes 		} else if (lseek(fileno(fin), restart_point, L_SET) < 0) {
1674ea022d16SRodney W. Grimes 			perror_reply(550, name);
1675ea022d16SRodney W. Grimes 			goto done;
1676ea022d16SRodney W. Grimes 		}
1677ea022d16SRodney W. Grimes 	}
1678ea022d16SRodney W. Grimes 	dout = dataconn(name, st.st_size, "w");
1679ea022d16SRodney W. Grimes 	if (dout == NULL)
1680ea022d16SRodney W. Grimes 		goto done;
16813eb568f2SGuido van Rooij 	time(&start);
16829fc5823aSGarrett Wollman 	send_data(fin, dout, st.st_blksize, st.st_size,
16839fc5823aSGarrett Wollman 		  restart_point == 0 && cmd == 0 && S_ISREG(st.st_mode));
1684c999732bSYaroslav Tykhiy 	if (cmd == 0 && guest && stats && byte_count > 0)
1685c999732bSYaroslav Tykhiy 		logxfer(name, byte_count, start);
1686ea022d16SRodney W. Grimes 	(void) fclose(dout);
1687ea022d16SRodney W. Grimes 	data = -1;
1688ea022d16SRodney W. Grimes 	pdata = -1;
1689ea022d16SRodney W. Grimes done:
1690ea022d16SRodney W. Grimes 	if (cmd == 0)
1691ea022d16SRodney W. Grimes 		LOGBYTES("get", name, byte_count);
1692ea022d16SRodney W. Grimes 	(*closefunc)(fin);
1693ea022d16SRodney W. Grimes }
1694ea022d16SRodney W. Grimes 
1695ea022d16SRodney W. Grimes void
1696e4bc453cSWarner Losh store(char *name, char *mode, int unique)
1697ea022d16SRodney W. Grimes {
1698a117c345SYaroslav Tykhiy 	int fd;
1699ea022d16SRodney W. Grimes 	FILE *fout, *din;
1700e4bc453cSWarner Losh 	int (*closefunc)(FILE *);
1701ea022d16SRodney W. Grimes 
1702a117c345SYaroslav Tykhiy 	if (*mode == 'a') {		/* APPE */
1703a117c345SYaroslav Tykhiy 		if (unique) {
1704a117c345SYaroslav Tykhiy 			/* Programming error */
1705a117c345SYaroslav Tykhiy 			syslog(LOG_ERR, "Internal: unique flag to APPE");
1706a117c345SYaroslav Tykhiy 			unique = 0;
1707a117c345SYaroslav Tykhiy 		}
1708a117c345SYaroslav Tykhiy 		if (guest && noguestmod) {
170902c97492SYaroslav Tykhiy 			reply(550, "Appending to existing file denied.");
1710a117c345SYaroslav Tykhiy 			goto err;
1711a117c345SYaroslav Tykhiy 		}
1712a117c345SYaroslav Tykhiy 		restart_point = 0;	/* not affected by preceding REST */
1713a117c345SYaroslav Tykhiy 	}
1714a117c345SYaroslav Tykhiy 	if (unique)			/* STOU overrides REST */
1715a117c345SYaroslav Tykhiy 		restart_point = 0;
1716a117c345SYaroslav Tykhiy 	if (guest && noguestmod) {
1717a117c345SYaroslav Tykhiy 		if (restart_point) {	/* guest STOR w/REST */
171802c97492SYaroslav Tykhiy 			reply(550, "Modifying existing file denied.");
1719a117c345SYaroslav Tykhiy 			goto err;
1720a117c345SYaroslav Tykhiy 		} else			/* treat guest STOR as STOU */
1721a117c345SYaroslav Tykhiy 			unique = 1;
1722ea022d16SRodney W. Grimes 	}
1723ea022d16SRodney W. Grimes 
1724ea022d16SRodney W. Grimes 	if (restart_point)
1725a117c345SYaroslav Tykhiy 		mode = "r+";	/* so ASCII manual seek can work */
1726a117c345SYaroslav Tykhiy 	if (unique) {
1727a117c345SYaroslav Tykhiy 		if ((fd = guniquefd(name, &name)) < 0)
1728a117c345SYaroslav Tykhiy 			goto err;
1729a117c345SYaroslav Tykhiy 		fout = fdopen(fd, mode);
1730a117c345SYaroslav Tykhiy 	} else
1731ea022d16SRodney W. Grimes 		fout = fopen(name, mode);
1732ea022d16SRodney W. Grimes 	closefunc = fclose;
1733ea022d16SRodney W. Grimes 	if (fout == NULL) {
1734ea022d16SRodney W. Grimes 		perror_reply(553, name);
1735a117c345SYaroslav Tykhiy 		goto err;
1736ea022d16SRodney W. Grimes 	}
1737ea022d16SRodney W. Grimes 	byte_count = -1;
1738ea022d16SRodney W. Grimes 	if (restart_point) {
1739ea022d16SRodney W. Grimes 		if (type == TYPE_A) {
1740ea022d16SRodney W. Grimes 			off_t i, n;
1741ea022d16SRodney W. Grimes 			int c;
1742ea022d16SRodney W. Grimes 
1743ea022d16SRodney W. Grimes 			n = restart_point;
1744ea022d16SRodney W. Grimes 			i = 0;
1745ea022d16SRodney W. Grimes 			while (i++ < n) {
1746ea022d16SRodney W. Grimes 				if ((c=getc(fout)) == EOF) {
1747ea022d16SRodney W. Grimes 					perror_reply(550, name);
1748ea022d16SRodney W. Grimes 					goto done;
1749ea022d16SRodney W. Grimes 				}
1750ea022d16SRodney W. Grimes 				if (c == '\n')
1751ea022d16SRodney W. Grimes 					i++;
1752ea022d16SRodney W. Grimes 			}
1753ea022d16SRodney W. Grimes 			/*
1754ea022d16SRodney W. Grimes 			 * We must do this seek to "current" position
1755ea022d16SRodney W. Grimes 			 * because we are changing from reading to
1756ea022d16SRodney W. Grimes 			 * writing.
1757ea022d16SRodney W. Grimes 			 */
17580e519c96SYaroslav Tykhiy 			if (fseeko(fout, 0, SEEK_CUR) < 0) {
1759ea022d16SRodney W. Grimes 				perror_reply(550, name);
1760ea022d16SRodney W. Grimes 				goto done;
1761ea022d16SRodney W. Grimes 			}
1762ea022d16SRodney W. Grimes 		} else if (lseek(fileno(fout), restart_point, L_SET) < 0) {
1763ea022d16SRodney W. Grimes 			perror_reply(550, name);
1764ea022d16SRodney W. Grimes 			goto done;
1765ea022d16SRodney W. Grimes 		}
1766ea022d16SRodney W. Grimes 	}
17670e519c96SYaroslav Tykhiy 	din = dataconn(name, -1, "r");
1768ea022d16SRodney W. Grimes 	if (din == NULL)
1769ea022d16SRodney W. Grimes 		goto done;
1770ea022d16SRodney W. Grimes 	if (receive_data(din, fout) == 0) {
1771ea022d16SRodney W. Grimes 		if (unique)
1772ea022d16SRodney W. Grimes 			reply(226, "Transfer complete (unique file name:%s).",
1773ea022d16SRodney W. Grimes 			    name);
1774ea022d16SRodney W. Grimes 		else
1775ea022d16SRodney W. Grimes 			reply(226, "Transfer complete.");
1776ea022d16SRodney W. Grimes 	}
1777ea022d16SRodney W. Grimes 	(void) fclose(din);
1778ea022d16SRodney W. Grimes 	data = -1;
1779ea022d16SRodney W. Grimes 	pdata = -1;
1780ea022d16SRodney W. Grimes done:
17817c20f337SYaroslav Tykhiy 	LOGBYTES(*mode == 'a' ? "append" : "put", name, byte_count);
1782ea022d16SRodney W. Grimes 	(*closefunc)(fout);
1783a117c345SYaroslav Tykhiy 	return;
1784a117c345SYaroslav Tykhiy err:
1785a117c345SYaroslav Tykhiy 	LOGCMD(*mode == 'a' ? "append" : "put" , name);
1786a117c345SYaroslav Tykhiy 	return;
1787ea022d16SRodney W. Grimes }
1788ea022d16SRodney W. Grimes 
1789ea022d16SRodney W. Grimes static FILE *
1790e4bc453cSWarner Losh getdatasock(char *mode)
1791ea022d16SRodney W. Grimes {
1792ea022d16SRodney W. Grimes 	int on = 1, s, t, tries;
1793ea022d16SRodney W. Grimes 
1794ea022d16SRodney W. Grimes 	if (data >= 0)
1795ea022d16SRodney W. Grimes 		return (fdopen(data, mode));
17964dd8b5abSYoshinobu Inoue 
17974dd8b5abSYoshinobu Inoue 	s = socket(data_dest.su_family, SOCK_STREAM, 0);
1798ea022d16SRodney W. Grimes 	if (s < 0)
1799ea022d16SRodney W. Grimes 		goto bad;
180057d4ef07SYaroslav Tykhiy 	if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
1801406d1ae9SYaroslav Tykhiy 		syslog(LOG_WARNING, "data setsockopt (SO_REUSEADDR): %m");
1802ea022d16SRodney W. Grimes 	/* anchor socket to avoid multi-homing problems */
18034dd8b5abSYoshinobu Inoue 	data_source = ctrl_addr;
180463591ba5SYaroslav Tykhiy 	data_source.su_port = htons(dataport);
180552e7ee74SYaroslav Tykhiy 	(void) seteuid(0);
1806ea022d16SRodney W. Grimes 	for (tries = 1; ; tries++) {
18079ec7612aSYaroslav Tykhiy 		/*
18089ec7612aSYaroslav Tykhiy 		 * We should loop here since it's possible that
18099ec7612aSYaroslav Tykhiy 		 * another ftpd instance has passed this point and is
18109ec7612aSYaroslav Tykhiy 		 * trying to open a data connection in active mode now.
18119ec7612aSYaroslav Tykhiy 		 * Until the other connection is opened, we'll be getting
18129ec7612aSYaroslav Tykhiy 		 * EADDRINUSE because no SOCK_STREAM sockets in the system
18139ec7612aSYaroslav Tykhiy 		 * can share both local and remote addresses, localIP:20
18149ec7612aSYaroslav Tykhiy 		 * and *:* in this case.
18159ec7612aSYaroslav Tykhiy 		 */
1816ea022d16SRodney W. Grimes 		if (bind(s, (struct sockaddr *)&data_source,
18174dd8b5abSYoshinobu Inoue 		    data_source.su_len) >= 0)
1818ea022d16SRodney W. Grimes 			break;
1819ea022d16SRodney W. Grimes 		if (errno != EADDRINUSE || tries > 10)
1820ea022d16SRodney W. Grimes 			goto bad;
1821ea022d16SRodney W. Grimes 		sleep(tries);
1822ea022d16SRodney W. Grimes 	}
182352e7ee74SYaroslav Tykhiy 	(void) seteuid(pw->pw_uid);
1824ea022d16SRodney W. Grimes #ifdef IP_TOS
18254dd8b5abSYoshinobu Inoue 	if (data_source.su_family == AF_INET)
18264dd8b5abSYoshinobu Inoue       {
1827ea022d16SRodney W. Grimes 	on = IPTOS_THROUGHPUT;
182857d4ef07SYaroslav Tykhiy 	if (setsockopt(s, IPPROTO_IP, IP_TOS, &on, sizeof(int)) < 0)
1829406d1ae9SYaroslav Tykhiy 		syslog(LOG_WARNING, "data setsockopt (IP_TOS): %m");
18304dd8b5abSYoshinobu Inoue       }
1831ea022d16SRodney W. Grimes #endif
18329fc5823aSGarrett Wollman #ifdef TCP_NOPUSH
18339fc5823aSGarrett Wollman 	/*
18349fc5823aSGarrett Wollman 	 * Turn off push flag to keep sender TCP from sending short packets
18359fc5823aSGarrett Wollman 	 * at the boundaries of each write().  Should probably do a SO_SNDBUF
18369fc5823aSGarrett Wollman 	 * to set the send buffer size as well, but that may not be desirable
18379fc5823aSGarrett Wollman 	 * in heavy-load situations.
18389fc5823aSGarrett Wollman 	 */
18399fc5823aSGarrett Wollman 	on = 1;
184057d4ef07SYaroslav Tykhiy 	if (setsockopt(s, IPPROTO_TCP, TCP_NOPUSH, &on, sizeof on) < 0)
1841406d1ae9SYaroslav Tykhiy 		syslog(LOG_WARNING, "data setsockopt (TCP_NOPUSH): %m");
18429fc5823aSGarrett Wollman #endif
18439fc5823aSGarrett Wollman #ifdef SO_SNDBUF
18449fc5823aSGarrett Wollman 	on = 65536;
184557d4ef07SYaroslav Tykhiy 	if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &on, sizeof on) < 0)
1846406d1ae9SYaroslav Tykhiy 		syslog(LOG_WARNING, "data setsockopt (SO_SNDBUF): %m");
18479fc5823aSGarrett Wollman #endif
18489fc5823aSGarrett Wollman 
1849ea022d16SRodney W. Grimes 	return (fdopen(s, mode));
1850ea022d16SRodney W. Grimes bad:
1851ea022d16SRodney W. Grimes 	/* Return the real value of errno (close may change it) */
1852ea022d16SRodney W. Grimes 	t = errno;
185352e7ee74SYaroslav Tykhiy 	(void) seteuid(pw->pw_uid);
1854ea022d16SRodney W. Grimes 	(void) close(s);
1855ea022d16SRodney W. Grimes 	errno = t;
1856ea022d16SRodney W. Grimes 	return (NULL);
1857ea022d16SRodney W. Grimes }
1858ea022d16SRodney W. Grimes 
1859ea022d16SRodney W. Grimes static FILE *
1860e4bc453cSWarner Losh dataconn(char *name, off_t size, char *mode)
1861ea022d16SRodney W. Grimes {
1862ea022d16SRodney W. Grimes 	char sizebuf[32];
1863ea022d16SRodney W. Grimes 	FILE *file;
1864e5094456SCrist J. Clark 	int retry = 0, tos, conerrno;
1865ea022d16SRodney W. Grimes 
1866ea022d16SRodney W. Grimes 	file_size = size;
1867ea022d16SRodney W. Grimes 	byte_count = 0;
18680e519c96SYaroslav Tykhiy 	if (size != -1)
1869a57e1ef0SYaroslav Tykhiy 		(void) snprintf(sizebuf, sizeof(sizebuf),
1870a57e1ef0SYaroslav Tykhiy 				" (%jd bytes)", (intmax_t)size);
1871ea022d16SRodney W. Grimes 	else
1872e760ef2cSWarner Losh 		*sizebuf = '\0';
1873ea022d16SRodney W. Grimes 	if (pdata >= 0) {
18744dd8b5abSYoshinobu Inoue 		union sockunion from;
18754cd48bacSYaroslav Tykhiy 		int flags;
18764dd8b5abSYoshinobu Inoue 		int s, fromlen = ctrl_addr.su_len;
1877d6ed3c37SGuido van Rooij 		struct timeval timeout;
1878d6ed3c37SGuido van Rooij 		fd_set set;
1879ea022d16SRodney W. Grimes 
1880d6ed3c37SGuido van Rooij 		FD_ZERO(&set);
1881d6ed3c37SGuido van Rooij 		FD_SET(pdata, &set);
1882d6ed3c37SGuido van Rooij 
1883d6ed3c37SGuido van Rooij 		timeout.tv_usec = 0;
1884d6ed3c37SGuido van Rooij 		timeout.tv_sec = 120;
1885d6ed3c37SGuido van Rooij 
18864cd48bacSYaroslav Tykhiy 		/*
18874cd48bacSYaroslav Tykhiy 		 * Granted a socket is in the blocking I/O mode,
18884cd48bacSYaroslav Tykhiy 		 * accept() will block after a successful select()
18894cd48bacSYaroslav Tykhiy 		 * if the selected connection dies in between.
18904cd48bacSYaroslav Tykhiy 		 * Therefore set the non-blocking I/O flag here.
18914cd48bacSYaroslav Tykhiy 		 */
18924cd48bacSYaroslav Tykhiy 		if ((flags = fcntl(pdata, F_GETFL, 0)) == -1 ||
18934cd48bacSYaroslav Tykhiy 		    fcntl(pdata, F_SETFL, flags | O_NONBLOCK) == -1)
18944cd48bacSYaroslav Tykhiy 			goto pdata_err;
1895aa5a9d3fSYaroslav Tykhiy 		if (select(pdata+1, &set, NULL, NULL, &timeout) <= 0 ||
18964cd48bacSYaroslav Tykhiy 		    (s = accept(pdata, (struct sockaddr *) &from, &fromlen)) < 0)
18974cd48bacSYaroslav Tykhiy 			goto pdata_err;
1898ea022d16SRodney W. Grimes 		(void) close(pdata);
1899ea022d16SRodney W. Grimes 		pdata = s;
19004cd48bacSYaroslav Tykhiy 		/*
1901f6daca0dSYaroslav Tykhiy 		 * Unset the inherited non-blocking I/O flag
1902f6daca0dSYaroslav Tykhiy 		 * on the child socket so stdio can work on it.
19034cd48bacSYaroslav Tykhiy 		 */
19044cd48bacSYaroslav Tykhiy 		if ((flags = fcntl(pdata, F_GETFL, 0)) == -1 ||
19054cd48bacSYaroslav Tykhiy 		    fcntl(pdata, F_SETFL, flags & ~O_NONBLOCK) == -1)
19064cd48bacSYaroslav Tykhiy 			goto pdata_err;
1907ea022d16SRodney W. Grimes #ifdef IP_TOS
19084dd8b5abSYoshinobu Inoue 		if (from.su_family == AF_INET)
19094dd8b5abSYoshinobu Inoue 	      {
1910a5a4544eSPaul Traina 		tos = IPTOS_THROUGHPUT;
191157d4ef07SYaroslav Tykhiy 		if (setsockopt(s, IPPROTO_IP, IP_TOS, &tos, sizeof(int)) < 0)
1912406d1ae9SYaroslav Tykhiy 			syslog(LOG_WARNING, "pdata setsockopt (IP_TOS): %m");
19134dd8b5abSYoshinobu Inoue 	      }
1914ea022d16SRodney W. Grimes #endif
1915ea022d16SRodney W. Grimes 		reply(150, "Opening %s mode data connection for '%s'%s.",
1916ea022d16SRodney W. Grimes 		     type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf);
1917ea022d16SRodney W. Grimes 		return (fdopen(pdata, mode));
19184cd48bacSYaroslav Tykhiy pdata_err:
19194cd48bacSYaroslav Tykhiy 		reply(425, "Can't open data connection.");
19204cd48bacSYaroslav Tykhiy 		(void) close(pdata);
19214cd48bacSYaroslav Tykhiy 		pdata = -1;
19224cd48bacSYaroslav Tykhiy 		return (NULL);
1923ea022d16SRodney W. Grimes 	}
1924ea022d16SRodney W. Grimes 	if (data >= 0) {
1925ea022d16SRodney W. Grimes 		reply(125, "Using existing data connection for '%s'%s.",
1926ea022d16SRodney W. Grimes 		    name, sizebuf);
1927ea022d16SRodney W. Grimes 		usedefault = 1;
1928ea022d16SRodney W. Grimes 		return (fdopen(data, mode));
1929ea022d16SRodney W. Grimes 	}
1930ea022d16SRodney W. Grimes 	if (usedefault)
1931ea022d16SRodney W. Grimes 		data_dest = his_addr;
1932ea022d16SRodney W. Grimes 	usedefault = 1;
1933e5094456SCrist J. Clark 	do {
1934ea022d16SRodney W. Grimes 		file = getdatasock(mode);
1935ea022d16SRodney W. Grimes 		if (file == NULL) {
193604683b2cSYaroslav Tykhiy 			char hostbuf[NI_MAXHOST], portbuf[NI_MAXSERV];
193704683b2cSYaroslav Tykhiy 
193804683b2cSYaroslav Tykhiy 			if (getnameinfo((struct sockaddr *)&data_source,
193904683b2cSYaroslav Tykhiy 				data_source.su_len,
194004683b2cSYaroslav Tykhiy 				hostbuf, sizeof(hostbuf) - 1,
194104683b2cSYaroslav Tykhiy 				portbuf, sizeof(portbuf) - 1,
194204683b2cSYaroslav Tykhiy 				NI_NUMERICHOST|NI_NUMERICSERV))
194304683b2cSYaroslav Tykhiy 					*hostbuf = *portbuf = 0;
194404683b2cSYaroslav Tykhiy 			hostbuf[sizeof(hostbuf) - 1] = 0;
194504683b2cSYaroslav Tykhiy 			portbuf[sizeof(portbuf) - 1] = 0;
19464dd8b5abSYoshinobu Inoue 			reply(425, "Can't create data socket (%s,%s): %s.",
19474dd8b5abSYoshinobu Inoue 				hostbuf, portbuf, strerror(errno));
1948ea022d16SRodney W. Grimes 			return (NULL);
1949ea022d16SRodney W. Grimes 		}
1950ea022d16SRodney W. Grimes 		data = fileno(file);
1951e5094456SCrist J. Clark 		conerrno = 0;
1952e5094456SCrist J. Clark 		if (connect(data, (struct sockaddr *)&data_dest,
1953e5094456SCrist J. Clark 		    data_dest.su_len) == 0)
1954e5094456SCrist J. Clark 			break;
1955e5094456SCrist J. Clark 		conerrno = errno;
1956ea022d16SRodney W. Grimes 		(void) fclose(file);
1957ea022d16SRodney W. Grimes 		data = -1;
1958e5094456SCrist J. Clark 		if (conerrno == EADDRINUSE) {
1959e3765043SYaroslav Tykhiy 			sleep(swaitint);
1960e5094456SCrist J. Clark 			retry += swaitint;
1961e5094456SCrist J. Clark 		} else {
1962e5094456SCrist J. Clark 			break;
1963e5094456SCrist J. Clark 		}
1964e5094456SCrist J. Clark 	} while (retry <= swaitmax);
1965e5094456SCrist J. Clark 	if (conerrno != 0) {
196682c03024SYaroslav Tykhiy 		reply(425, "Can't build data connection: %s.",
196782c03024SYaroslav Tykhiy 			   strerror(conerrno));
1968ea022d16SRodney W. Grimes 		return (NULL);
1969ea022d16SRodney W. Grimes 	}
1970ea022d16SRodney W. Grimes 	reply(150, "Opening %s mode data connection for '%s'%s.",
1971ea022d16SRodney W. Grimes 	     type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf);
1972ea022d16SRodney W. Grimes 	return (file);
1973ea022d16SRodney W. Grimes }
1974ea022d16SRodney W. Grimes 
1975ea022d16SRodney W. Grimes /*
1976ea022d16SRodney W. Grimes  * Tranfer the contents of "instr" to "outstr" peer using the appropriate
19779fc5823aSGarrett Wollman  * encapsulation of the data subject to Mode, Structure, and Type.
1978ea022d16SRodney W. Grimes  *
1979ea022d16SRodney W. Grimes  * NB: Form isn't handled.
1980ea022d16SRodney W. Grimes  */
19814b82fc95SYaroslav Tykhiy static int
19826e4b0a55SYaroslav Tykhiy send_data(FILE *instr, FILE *outstr, size_t blksize, off_t filesize, int isreg)
1983ea022d16SRodney W. Grimes {
1984db1c2da3SYaroslav Tykhiy 	int c, cp, filefd, netfd;
1985f6f0c4b9SDan Moschuk 	char *buf;
1986f6f0c4b9SDan Moschuk 	off_t cnt;
1987ea022d16SRodney W. Grimes 
1988ea022d16SRodney W. Grimes 	transflag++;
1989ea022d16SRodney W. Grimes 	switch (type) {
1990ea022d16SRodney W. Grimes 
1991ea022d16SRodney W. Grimes 	case TYPE_A:
1992db1c2da3SYaroslav Tykhiy 		cp = '\0';
1993ea022d16SRodney W. Grimes 		while ((c = getc(instr)) != EOF) {
19944b82fc95SYaroslav Tykhiy 			if (recvurg)
19954b82fc95SYaroslav Tykhiy 				goto got_oob;
1996ea022d16SRodney W. Grimes 			byte_count++;
1997db1c2da3SYaroslav Tykhiy 			if (c == '\n' && cp != '\r') {
1998ea022d16SRodney W. Grimes 				if (ferror(outstr))
1999ea022d16SRodney W. Grimes 					goto data_err;
2000ea022d16SRodney W. Grimes 				(void) putc('\r', outstr);
2001ea022d16SRodney W. Grimes 			}
2002ea022d16SRodney W. Grimes 			(void) putc(c, outstr);
2003db1c2da3SYaroslav Tykhiy 			cp = c;
2004ea022d16SRodney W. Grimes 		}
20054b82fc95SYaroslav Tykhiy 		if (recvurg)
20064b82fc95SYaroslav Tykhiy 			goto got_oob;
2007ea022d16SRodney W. Grimes 		fflush(outstr);
2008ea022d16SRodney W. Grimes 		transflag = 0;
2009ea022d16SRodney W. Grimes 		if (ferror(instr))
2010ea022d16SRodney W. Grimes 			goto file_err;
2011ea022d16SRodney W. Grimes 		if (ferror(outstr))
2012ea022d16SRodney W. Grimes 			goto data_err;
2013ea022d16SRodney W. Grimes 		reply(226, "Transfer complete.");
20144b82fc95SYaroslav Tykhiy 		return (0);
2015ea022d16SRodney W. Grimes 
2016ea022d16SRodney W. Grimes 	case TYPE_I:
2017ea022d16SRodney W. Grimes 	case TYPE_L:
20189fc5823aSGarrett Wollman 		/*
20199fc5823aSGarrett Wollman 		 * isreg is only set if we are not doing restart and we
20209fc5823aSGarrett Wollman 		 * are sending a regular file
20219fc5823aSGarrett Wollman 		 */
20229fc5823aSGarrett Wollman 		netfd = fileno(outstr);
20239fc5823aSGarrett Wollman 		filefd = fileno(instr);
20249fc5823aSGarrett Wollman 
2025f6f0c4b9SDan Moschuk 		if (isreg) {
20269fc5823aSGarrett Wollman 
20272e22b914SYaroslav Tykhiy 			char *msg = "Transfer complete.";
2028f6f0c4b9SDan Moschuk 			off_t offset;
2029f6f0c4b9SDan Moschuk 			int err;
2030f6f0c4b9SDan Moschuk 
20312f492fc8SYaroslav Tykhiy 			cnt = offset = 0;
2032f6f0c4b9SDan Moschuk 
20332f492fc8SYaroslav Tykhiy 			while (filesize > 0) {
2034492f1d9cSMaxim Konovalov 				err = sendfile(filefd, netfd, offset, 0,
20357e295315SYaroslav Tykhiy 					       NULL, &cnt, 0);
20363af48c42SMaxim Konovalov 				/*
20373af48c42SMaxim Konovalov 				 * Calculate byte_count before OOB processing.
20383af48c42SMaxim Konovalov 				 * It can be used in myoob() later.
20393af48c42SMaxim Konovalov 				 */
20403af48c42SMaxim Konovalov 				byte_count += cnt;
20414b82fc95SYaroslav Tykhiy 				if (recvurg)
20424b82fc95SYaroslav Tykhiy 					goto got_oob;
2043f6f0c4b9SDan Moschuk 				offset += cnt;
2044492f1d9cSMaxim Konovalov 				filesize -= cnt;
2045f6f0c4b9SDan Moschuk 
2046f6f0c4b9SDan Moschuk 				if (err == -1) {
2047b4585cc1SYaroslav Tykhiy 					if (cnt == 0 && offset == 0)
2048f6f0c4b9SDan Moschuk 						goto oldway;
2049f6f0c4b9SDan Moschuk 
20509fc5823aSGarrett Wollman 					goto data_err;
2051f6f0c4b9SDan Moschuk 				}
20522e22b914SYaroslav Tykhiy 
20532e22b914SYaroslav Tykhiy 				/*
20542e22b914SYaroslav Tykhiy 				 * We hit the EOF prematurely.
20552e22b914SYaroslav Tykhiy 				 * Perhaps the file was externally truncated.
20562e22b914SYaroslav Tykhiy 				 */
20572e22b914SYaroslav Tykhiy 				if (cnt == 0) {
20582e22b914SYaroslav Tykhiy 					msg = "Transfer finished due to "
20592e22b914SYaroslav Tykhiy 					      "premature end of file.";
20602e22b914SYaroslav Tykhiy 					break;
20612e22b914SYaroslav Tykhiy 				}
2062f6f0c4b9SDan Moschuk 			}
2063f6f0c4b9SDan Moschuk 
20640849c184SDan Moschuk 			transflag = 0;
20652e22b914SYaroslav Tykhiy 			reply(226, msg);
20664b82fc95SYaroslav Tykhiy 			return (0);
20679fc5823aSGarrett Wollman 		}
20689fc5823aSGarrett Wollman 
20699fc5823aSGarrett Wollman oldway:
2070e3765043SYaroslav Tykhiy 		if ((buf = malloc(blksize)) == NULL) {
2071ea022d16SRodney W. Grimes 			transflag = 0;
207202c97492SYaroslav Tykhiy 			reply(451, "Ran out of memory.");
20734b82fc95SYaroslav Tykhiy 			return (-1);
2074ea022d16SRodney W. Grimes 		}
20759fc5823aSGarrett Wollman 
2076e3765043SYaroslav Tykhiy 		while ((cnt = read(filefd, buf, blksize)) > 0 &&
2077ea022d16SRodney W. Grimes 		    write(netfd, buf, cnt) == cnt)
2078ea022d16SRodney W. Grimes 			byte_count += cnt;
2079ea022d16SRodney W. Grimes 		transflag = 0;
2080ea022d16SRodney W. Grimes 		(void)free(buf);
2081ea022d16SRodney W. Grimes 		if (cnt != 0) {
2082ea022d16SRodney W. Grimes 			if (cnt < 0)
2083ea022d16SRodney W. Grimes 				goto file_err;
2084ea022d16SRodney W. Grimes 			goto data_err;
2085ea022d16SRodney W. Grimes 		}
2086ea022d16SRodney W. Grimes 		reply(226, "Transfer complete.");
20874b82fc95SYaroslav Tykhiy 		return (0);
2088ea022d16SRodney W. Grimes 	default:
2089ea022d16SRodney W. Grimes 		transflag = 0;
209002c97492SYaroslav Tykhiy 		reply(550, "Unimplemented TYPE %d in send_data.", type);
20914b82fc95SYaroslav Tykhiy 		return (-1);
2092ea022d16SRodney W. Grimes 	}
2093ea022d16SRodney W. Grimes 
2094ea022d16SRodney W. Grimes data_err:
2095ea022d16SRodney W. Grimes 	transflag = 0;
2096ea022d16SRodney W. Grimes 	perror_reply(426, "Data connection");
20974b82fc95SYaroslav Tykhiy 	return (-1);
2098ea022d16SRodney W. Grimes 
2099ea022d16SRodney W. Grimes file_err:
2100ea022d16SRodney W. Grimes 	transflag = 0;
2101ea022d16SRodney W. Grimes 	perror_reply(551, "Error on input file");
21024b82fc95SYaroslav Tykhiy 	return (-1);
21034b82fc95SYaroslav Tykhiy 
21044b82fc95SYaroslav Tykhiy got_oob:
21054b82fc95SYaroslav Tykhiy 	myoob();
21064b82fc95SYaroslav Tykhiy 	recvurg = 0;
21074b82fc95SYaroslav Tykhiy 	transflag = 0;
21084b82fc95SYaroslav Tykhiy 	return (-1);
2109ea022d16SRodney W. Grimes }
2110ea022d16SRodney W. Grimes 
2111ea022d16SRodney W. Grimes /*
2112ea022d16SRodney W. Grimes  * Transfer data from peer to "outstr" using the appropriate encapulation of
2113ea022d16SRodney W. Grimes  * the data subject to Mode, Structure, and Type.
2114ea022d16SRodney W. Grimes  *
2115ea022d16SRodney W. Grimes  * N.B.: Form isn't handled.
2116ea022d16SRodney W. Grimes  */
2117ea022d16SRodney W. Grimes static int
2118e4bc453cSWarner Losh receive_data(FILE *instr, FILE *outstr)
2119ea022d16SRodney W. Grimes {
2120ea022d16SRodney W. Grimes 	int c;
212161f891a6SPaul Traina 	int cnt, bare_lfs;
2122ea022d16SRodney W. Grimes 	char buf[BUFSIZ];
2123ea022d16SRodney W. Grimes 
2124ea022d16SRodney W. Grimes 	transflag++;
212561f891a6SPaul Traina 	bare_lfs = 0;
212661f891a6SPaul Traina 
2127ea022d16SRodney W. Grimes 	switch (type) {
2128ea022d16SRodney W. Grimes 
2129ea022d16SRodney W. Grimes 	case TYPE_I:
2130ea022d16SRodney W. Grimes 	case TYPE_L:
2131ea022d16SRodney W. Grimes 		while ((cnt = read(fileno(instr), buf, sizeof(buf))) > 0) {
21324b82fc95SYaroslav Tykhiy 			if (recvurg)
21334b82fc95SYaroslav Tykhiy 				goto got_oob;
2134ea022d16SRodney W. Grimes 			if (write(fileno(outstr), buf, cnt) != cnt)
2135ea022d16SRodney W. Grimes 				goto file_err;
2136ea022d16SRodney W. Grimes 			byte_count += cnt;
2137ea022d16SRodney W. Grimes 		}
21384b82fc95SYaroslav Tykhiy 		if (recvurg)
21394b82fc95SYaroslav Tykhiy 			goto got_oob;
2140ea022d16SRodney W. Grimes 		if (cnt < 0)
2141ea022d16SRodney W. Grimes 			goto data_err;
2142ea022d16SRodney W. Grimes 		transflag = 0;
2143ea022d16SRodney W. Grimes 		return (0);
2144ea022d16SRodney W. Grimes 
2145ea022d16SRodney W. Grimes 	case TYPE_E:
2146ea022d16SRodney W. Grimes 		reply(553, "TYPE E not implemented.");
2147ea022d16SRodney W. Grimes 		transflag = 0;
2148ea022d16SRodney W. Grimes 		return (-1);
2149ea022d16SRodney W. Grimes 
2150ea022d16SRodney W. Grimes 	case TYPE_A:
2151ea022d16SRodney W. Grimes 		while ((c = getc(instr)) != EOF) {
21524b82fc95SYaroslav Tykhiy 			if (recvurg)
21534b82fc95SYaroslav Tykhiy 				goto got_oob;
2154ea022d16SRodney W. Grimes 			byte_count++;
2155ea022d16SRodney W. Grimes 			if (c == '\n')
2156ea022d16SRodney W. Grimes 				bare_lfs++;
2157ea022d16SRodney W. Grimes 			while (c == '\r') {
2158ea022d16SRodney W. Grimes 				if (ferror(outstr))
2159ea022d16SRodney W. Grimes 					goto data_err;
2160ea022d16SRodney W. Grimes 				if ((c = getc(instr)) != '\n') {
2161ea022d16SRodney W. Grimes 					(void) putc ('\r', outstr);
2162ea022d16SRodney W. Grimes 					if (c == '\0' || c == EOF)
2163ea022d16SRodney W. Grimes 						goto contin2;
2164ea022d16SRodney W. Grimes 				}
2165ea022d16SRodney W. Grimes 			}
2166ea022d16SRodney W. Grimes 			(void) putc(c, outstr);
2167ea022d16SRodney W. Grimes 	contin2:	;
2168ea022d16SRodney W. Grimes 		}
21694b82fc95SYaroslav Tykhiy 		if (recvurg)
21704b82fc95SYaroslav Tykhiy 			goto got_oob;
2171ea022d16SRodney W. Grimes 		fflush(outstr);
2172ea022d16SRodney W. Grimes 		if (ferror(instr))
2173ea022d16SRodney W. Grimes 			goto data_err;
2174ea022d16SRodney W. Grimes 		if (ferror(outstr))
2175ea022d16SRodney W. Grimes 			goto file_err;
2176ea022d16SRodney W. Grimes 		transflag = 0;
2177ea022d16SRodney W. Grimes 		if (bare_lfs) {
2178ea022d16SRodney W. Grimes 			lreply(226,
217902c97492SYaroslav Tykhiy 		"WARNING! %d bare linefeeds received in ASCII mode.",
2180ea022d16SRodney W. Grimes 			    bare_lfs);
2181ea022d16SRodney W. Grimes 		(void)printf("   File may not have transferred correctly.\r\n");
2182ea022d16SRodney W. Grimes 		}
2183ea022d16SRodney W. Grimes 		return (0);
2184ea022d16SRodney W. Grimes 	default:
218502c97492SYaroslav Tykhiy 		reply(550, "Unimplemented TYPE %d in receive_data.", type);
2186ea022d16SRodney W. Grimes 		transflag = 0;
2187ea022d16SRodney W. Grimes 		return (-1);
2188ea022d16SRodney W. Grimes 	}
2189ea022d16SRodney W. Grimes 
2190ea022d16SRodney W. Grimes data_err:
2191ea022d16SRodney W. Grimes 	transflag = 0;
219202c97492SYaroslav Tykhiy 	perror_reply(426, "Data connection");
2193ea022d16SRodney W. Grimes 	return (-1);
2194ea022d16SRodney W. Grimes 
2195ea022d16SRodney W. Grimes file_err:
2196ea022d16SRodney W. Grimes 	transflag = 0;
219702c97492SYaroslav Tykhiy 	perror_reply(452, "Error writing to file");
2198ea022d16SRodney W. Grimes 	return (-1);
21994b82fc95SYaroslav Tykhiy 
22004b82fc95SYaroslav Tykhiy got_oob:
22014b82fc95SYaroslav Tykhiy 	myoob();
22024b82fc95SYaroslav Tykhiy 	recvurg = 0;
22034b82fc95SYaroslav Tykhiy 	transflag = 0;
22044b82fc95SYaroslav Tykhiy 	return (-1);
2205ea022d16SRodney W. Grimes }
2206ea022d16SRodney W. Grimes 
2207ea022d16SRodney W. Grimes void
2208e4bc453cSWarner Losh statfilecmd(char *filename)
2209ea022d16SRodney W. Grimes {
2210ea022d16SRodney W. Grimes 	FILE *fin;
2211f8a581a0SYaroslav Tykhiy 	int atstart;
221241c57b48SYaroslav Tykhiy 	int c, code;
2213ea022d16SRodney W. Grimes 	char line[LINE_MAX];
221441c57b48SYaroslav Tykhiy 	struct stat st;
2215ea022d16SRodney W. Grimes 
221641c57b48SYaroslav Tykhiy 	code = lstat(filename, &st) == 0 && S_ISDIR(st.st_mode) ? 212 : 213;
2217af85d782SDavid Nugent 	(void)snprintf(line, sizeof(line), _PATH_LS " -lgA %s", filename);
2218ea022d16SRodney W. Grimes 	fin = ftpd_popen(line, "r");
22193b48b877SYaroslav Tykhiy 	lreply(code, "Status of %s:", filename);
2220f8a581a0SYaroslav Tykhiy 	atstart = 1;
2221ea022d16SRodney W. Grimes 	while ((c = getc(fin)) != EOF) {
2222ea022d16SRodney W. Grimes 		if (c == '\n') {
2223ea022d16SRodney W. Grimes 			if (ferror(stdout)){
222402c97492SYaroslav Tykhiy 				perror_reply(421, "Control connection");
2225ea022d16SRodney W. Grimes 				(void) ftpd_pclose(fin);
2226ea022d16SRodney W. Grimes 				dologout(1);
2227ea022d16SRodney W. Grimes 				/* NOTREACHED */
2228ea022d16SRodney W. Grimes 			}
2229ea022d16SRodney W. Grimes 			if (ferror(fin)) {
2230ea022d16SRodney W. Grimes 				perror_reply(551, filename);
2231ea022d16SRodney W. Grimes 				(void) ftpd_pclose(fin);
2232ea022d16SRodney W. Grimes 				return;
2233ea022d16SRodney W. Grimes 			}
2234ea022d16SRodney W. Grimes 			(void) putc('\r', stdout);
2235ea022d16SRodney W. Grimes 		}
2236f8a581a0SYaroslav Tykhiy 		/*
2237f8a581a0SYaroslav Tykhiy 		 * RFC 959 says neutral text should be prepended before
2238f8a581a0SYaroslav Tykhiy 		 * a leading 3-digit number followed by whitespace, but
2239f8a581a0SYaroslav Tykhiy 		 * many ftp clients can be confused by any leading digits,
2240f8a581a0SYaroslav Tykhiy 		 * as a matter of fact.
2241f8a581a0SYaroslav Tykhiy 		 */
2242f8a581a0SYaroslav Tykhiy 		if (atstart && isdigit(c))
2243f8a581a0SYaroslav Tykhiy 			(void) putc(' ', stdout);
2244ea022d16SRodney W. Grimes 		(void) putc(c, stdout);
2245f8a581a0SYaroslav Tykhiy 		atstart = (c == '\n');
2246ea022d16SRodney W. Grimes 	}
2247ea022d16SRodney W. Grimes 	(void) ftpd_pclose(fin);
224802c97492SYaroslav Tykhiy 	reply(code, "End of status.");
2249ea022d16SRodney W. Grimes }
2250ea022d16SRodney W. Grimes 
2251ea022d16SRodney W. Grimes void
2252e4bc453cSWarner Losh statcmd(void)
2253ea022d16SRodney W. Grimes {
22544dd8b5abSYoshinobu Inoue 	union sockunion *su;
2255ea022d16SRodney W. Grimes 	u_char *a, *p;
2256b0f06defSHajimu UMEMOTO 	char hname[NI_MAXHOST];
22574dd8b5abSYoshinobu Inoue 	int ispassive;
2258ea022d16SRodney W. Grimes 
2259c152df28SYaroslav Tykhiy 	if (hostinfo) {
2260a23f61bcSYaroslav Tykhiy 		lreply(211, "%s FTP server status:", hostname);
2261ea022d16SRodney W. Grimes 		printf("     %s\r\n", version);
2262c152df28SYaroslav Tykhiy 	} else
2263c152df28SYaroslav Tykhiy 		lreply(211, "FTP server status:");
2264ea022d16SRodney W. Grimes 	printf("     Connected to %s", remotehost);
22654dd8b5abSYoshinobu Inoue 	if (!getnameinfo((struct sockaddr *)&his_addr, his_addr.su_len,
2266b0f06defSHajimu UMEMOTO 			 hname, sizeof(hname) - 1, NULL, 0, NI_NUMERICHOST)) {
226704683b2cSYaroslav Tykhiy 		hname[sizeof(hname) - 1] = 0;
22684dd8b5abSYoshinobu Inoue 		if (strcmp(hname, remotehost) != 0)
22694dd8b5abSYoshinobu Inoue 			printf(" (%s)", hname);
22704dd8b5abSYoshinobu Inoue 	}
2271ea022d16SRodney W. Grimes 	printf("\r\n");
2272ea022d16SRodney W. Grimes 	if (logged_in) {
2273ea022d16SRodney W. Grimes 		if (guest)
2274ea022d16SRodney W. Grimes 			printf("     Logged in anonymously\r\n");
2275ea022d16SRodney W. Grimes 		else
2276ea022d16SRodney W. Grimes 			printf("     Logged in as %s\r\n", pw->pw_name);
2277ea022d16SRodney W. Grimes 	} else if (askpasswd)
2278ea022d16SRodney W. Grimes 		printf("     Waiting for password\r\n");
2279ea022d16SRodney W. Grimes 	else
2280ea022d16SRodney W. Grimes 		printf("     Waiting for user name\r\n");
2281ea022d16SRodney W. Grimes 	printf("     TYPE: %s", typenames[type]);
2282ea022d16SRodney W. Grimes 	if (type == TYPE_A || type == TYPE_E)
2283ea022d16SRodney W. Grimes 		printf(", FORM: %s", formnames[form]);
2284ea022d16SRodney W. Grimes 	if (type == TYPE_L)
228589fdc4e1SMike Barcroft #if CHAR_BIT == 8
228689fdc4e1SMike Barcroft 		printf(" %d", CHAR_BIT);
2287ea022d16SRodney W. Grimes #else
2288ea022d16SRodney W. Grimes 		printf(" %d", bytesize);	/* need definition! */
2289ea022d16SRodney W. Grimes #endif
2290ea022d16SRodney W. Grimes 	printf("; STRUcture: %s; transfer MODE: %s\r\n",
2291ea022d16SRodney W. Grimes 	    strunames[stru], modenames[mode]);
2292ea022d16SRodney W. Grimes 	if (data != -1)
2293ea022d16SRodney W. Grimes 		printf("     Data connection open\r\n");
2294ea022d16SRodney W. Grimes 	else if (pdata != -1) {
22954dd8b5abSYoshinobu Inoue 		ispassive = 1;
22964dd8b5abSYoshinobu Inoue 		su = &pasv_addr;
2297ea022d16SRodney W. Grimes 		goto printaddr;
2298ea022d16SRodney W. Grimes 	} else if (usedefault == 0) {
22994dd8b5abSYoshinobu Inoue 		ispassive = 0;
23004dd8b5abSYoshinobu Inoue 		su = &data_dest;
2301ea022d16SRodney W. Grimes printaddr:
2302ea022d16SRodney W. Grimes #define UC(b) (((int) b) & 0xff)
23034dd8b5abSYoshinobu Inoue 		if (epsvall) {
23044dd8b5abSYoshinobu Inoue 			printf("     EPSV only mode (EPSV ALL)\r\n");
23054dd8b5abSYoshinobu Inoue 			goto epsvonly;
23064dd8b5abSYoshinobu Inoue 		}
23074dd8b5abSYoshinobu Inoue 
23084dd8b5abSYoshinobu Inoue 		/* PORT/PASV */
23094dd8b5abSYoshinobu Inoue 		if (su->su_family == AF_INET) {
23104dd8b5abSYoshinobu Inoue 			a = (u_char *) &su->su_sin.sin_addr;
23114dd8b5abSYoshinobu Inoue 			p = (u_char *) &su->su_sin.sin_port;
23124dd8b5abSYoshinobu Inoue 			printf("     %s (%d,%d,%d,%d,%d,%d)\r\n",
23134dd8b5abSYoshinobu Inoue 				ispassive ? "PASV" : "PORT",
23144dd8b5abSYoshinobu Inoue 				UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
23154dd8b5abSYoshinobu Inoue 				UC(p[0]), UC(p[1]));
23164dd8b5abSYoshinobu Inoue 		}
23174dd8b5abSYoshinobu Inoue 
23184dd8b5abSYoshinobu Inoue 		/* LPRT/LPSV */
23194dd8b5abSYoshinobu Inoue 	    {
23204dd8b5abSYoshinobu Inoue 		int alen, af, i;
23214dd8b5abSYoshinobu Inoue 
23224dd8b5abSYoshinobu Inoue 		switch (su->su_family) {
23234dd8b5abSYoshinobu Inoue 		case AF_INET:
23244dd8b5abSYoshinobu Inoue 			a = (u_char *) &su->su_sin.sin_addr;
23254dd8b5abSYoshinobu Inoue 			p = (u_char *) &su->su_sin.sin_port;
23264dd8b5abSYoshinobu Inoue 			alen = sizeof(su->su_sin.sin_addr);
23274dd8b5abSYoshinobu Inoue 			af = 4;
23284dd8b5abSYoshinobu Inoue 			break;
23294dd8b5abSYoshinobu Inoue 		case AF_INET6:
23304dd8b5abSYoshinobu Inoue 			a = (u_char *) &su->su_sin6.sin6_addr;
23314dd8b5abSYoshinobu Inoue 			p = (u_char *) &su->su_sin6.sin6_port;
23324dd8b5abSYoshinobu Inoue 			alen = sizeof(su->su_sin6.sin6_addr);
23334dd8b5abSYoshinobu Inoue 			af = 6;
23344dd8b5abSYoshinobu Inoue 			break;
23354dd8b5abSYoshinobu Inoue 		default:
23364dd8b5abSYoshinobu Inoue 			af = 0;
23374dd8b5abSYoshinobu Inoue 			break;
23384dd8b5abSYoshinobu Inoue 		}
23394dd8b5abSYoshinobu Inoue 		if (af) {
23404dd8b5abSYoshinobu Inoue 			printf("     %s (%d,%d,", ispassive ? "LPSV" : "LPRT",
23414dd8b5abSYoshinobu Inoue 				af, alen);
23424dd8b5abSYoshinobu Inoue 			for (i = 0; i < alen; i++)
23434dd8b5abSYoshinobu Inoue 				printf("%d,", UC(a[i]));
23444dd8b5abSYoshinobu Inoue 			printf("%d,%d,%d)\r\n", 2, UC(p[0]), UC(p[1]));
23454dd8b5abSYoshinobu Inoue 		}
23464dd8b5abSYoshinobu Inoue 	    }
23474dd8b5abSYoshinobu Inoue 
23484dd8b5abSYoshinobu Inoue epsvonly:;
23494dd8b5abSYoshinobu Inoue 		/* EPRT/EPSV */
23504dd8b5abSYoshinobu Inoue 	    {
23514dd8b5abSYoshinobu Inoue 		int af;
23524dd8b5abSYoshinobu Inoue 
23534dd8b5abSYoshinobu Inoue 		switch (su->su_family) {
23544dd8b5abSYoshinobu Inoue 		case AF_INET:
23554dd8b5abSYoshinobu Inoue 			af = 1;
23564dd8b5abSYoshinobu Inoue 			break;
23574dd8b5abSYoshinobu Inoue 		case AF_INET6:
23584dd8b5abSYoshinobu Inoue 			af = 2;
23594dd8b5abSYoshinobu Inoue 			break;
23604dd8b5abSYoshinobu Inoue 		default:
23614dd8b5abSYoshinobu Inoue 			af = 0;
23624dd8b5abSYoshinobu Inoue 			break;
23634dd8b5abSYoshinobu Inoue 		}
23644dd8b5abSYoshinobu Inoue 		if (af) {
2365b0f06defSHajimu UMEMOTO 			union sockunion tmp;
2366b0f06defSHajimu UMEMOTO 
2367b0f06defSHajimu UMEMOTO 			tmp = *su;
2368b0f06defSHajimu UMEMOTO 			if (tmp.su_family == AF_INET6)
2369b0f06defSHajimu UMEMOTO 				tmp.su_sin6.sin6_scope_id = 0;
2370b0f06defSHajimu UMEMOTO 			if (!getnameinfo((struct sockaddr *)&tmp, tmp.su_len,
23714dd8b5abSYoshinobu Inoue 					hname, sizeof(hname) - 1, NULL, 0,
23724dd8b5abSYoshinobu Inoue 					NI_NUMERICHOST)) {
237304683b2cSYaroslav Tykhiy 				hname[sizeof(hname) - 1] = 0;
23744dd8b5abSYoshinobu Inoue 				printf("     %s |%d|%s|%d|\r\n",
23754dd8b5abSYoshinobu Inoue 					ispassive ? "EPSV" : "EPRT",
2376b0f06defSHajimu UMEMOTO 					af, hname, htons(tmp.su_port));
23774dd8b5abSYoshinobu Inoue 			}
23784dd8b5abSYoshinobu Inoue 		}
23794dd8b5abSYoshinobu Inoue 	    }
2380ea022d16SRodney W. Grimes #undef UC
2381ea022d16SRodney W. Grimes 	} else
2382ea022d16SRodney W. Grimes 		printf("     No data connection\r\n");
238302c97492SYaroslav Tykhiy 	reply(211, "End of status.");
2384ea022d16SRodney W. Grimes }
2385ea022d16SRodney W. Grimes 
2386ea022d16SRodney W. Grimes void
2387e4bc453cSWarner Losh fatalerror(char *s)
2388ea022d16SRodney W. Grimes {
2389ea022d16SRodney W. Grimes 
23904a3e5acdSYaroslav Tykhiy 	reply(451, "Error in server: %s", s);
2391ea022d16SRodney W. Grimes 	reply(221, "Closing connection due to server error.");
2392ea022d16SRodney W. Grimes 	dologout(0);
2393ea022d16SRodney W. Grimes 	/* NOTREACHED */
2394ea022d16SRodney W. Grimes }
2395ea022d16SRodney W. Grimes 
2396ea022d16SRodney W. Grimes void
2397ea022d16SRodney W. Grimes reply(int n, const char *fmt, ...)
2398ea022d16SRodney W. Grimes {
2399ea022d16SRodney W. Grimes 	va_list ap;
2400e4bc453cSWarner Losh 
2401ea022d16SRodney W. Grimes 	(void)printf("%d ", n);
24029cbb335cSTim J. Robbins 	va_start(ap, fmt);
2403ea022d16SRodney W. Grimes 	(void)vprintf(fmt, ap);
24049cbb335cSTim J. Robbins 	va_end(ap);
2405ea022d16SRodney W. Grimes 	(void)printf("\r\n");
2406ea022d16SRodney W. Grimes 	(void)fflush(stdout);
2407618b0bbaSMark Murray 	if (ftpdebug) {
2408ea022d16SRodney W. Grimes 		syslog(LOG_DEBUG, "<--- %d ", n);
24099cbb335cSTim J. Robbins 		va_start(ap, fmt);
2410ea022d16SRodney W. Grimes 		vsyslog(LOG_DEBUG, fmt, ap);
24119cbb335cSTim J. Robbins 		va_end(ap);
2412ea022d16SRodney W. Grimes 	}
2413ea022d16SRodney W. Grimes }
2414ea022d16SRodney W. Grimes 
2415ea022d16SRodney W. Grimes void
2416ea022d16SRodney W. Grimes lreply(int n, const char *fmt, ...)
2417ea022d16SRodney W. Grimes {
2418ea022d16SRodney W. Grimes 	va_list ap;
2419e4bc453cSWarner Losh 
2420ea022d16SRodney W. Grimes 	(void)printf("%d- ", n);
24219cbb335cSTim J. Robbins 	va_start(ap, fmt);
2422ea022d16SRodney W. Grimes 	(void)vprintf(fmt, ap);
24239cbb335cSTim J. Robbins 	va_end(ap);
2424ea022d16SRodney W. Grimes 	(void)printf("\r\n");
2425ea022d16SRodney W. Grimes 	(void)fflush(stdout);
2426618b0bbaSMark Murray 	if (ftpdebug) {
2427ea022d16SRodney W. Grimes 		syslog(LOG_DEBUG, "<--- %d- ", n);
24289cbb335cSTim J. Robbins 		va_start(ap, fmt);
2429ea022d16SRodney W. Grimes 		vsyslog(LOG_DEBUG, fmt, ap);
24309cbb335cSTim J. Robbins 		va_end(ap);
2431ea022d16SRodney W. Grimes 	}
2432ea022d16SRodney W. Grimes }
2433ea022d16SRodney W. Grimes 
2434ea022d16SRodney W. Grimes static void
2435e4bc453cSWarner Losh ack(char *s)
2436ea022d16SRodney W. Grimes {
2437ea022d16SRodney W. Grimes 
2438ea022d16SRodney W. Grimes 	reply(250, "%s command successful.", s);
2439ea022d16SRodney W. Grimes }
2440ea022d16SRodney W. Grimes 
2441ea022d16SRodney W. Grimes void
2442e4bc453cSWarner Losh nack(char *s)
2443ea022d16SRodney W. Grimes {
2444ea022d16SRodney W. Grimes 
2445ea022d16SRodney W. Grimes 	reply(502, "%s command not implemented.", s);
2446ea022d16SRodney W. Grimes }
2447ea022d16SRodney W. Grimes 
2448ea022d16SRodney W. Grimes /* ARGSUSED */
2449ea022d16SRodney W. Grimes void
2450e4bc453cSWarner Losh yyerror(char *s)
2451ea022d16SRodney W. Grimes {
2452ea022d16SRodney W. Grimes 	char *cp;
2453ea022d16SRodney W. Grimes 
24549aca17cbSMark Murray 	if ((cp = strchr(cbuf,'\n')))
2455ea022d16SRodney W. Grimes 		*cp = '\0';
245602c97492SYaroslav Tykhiy 	reply(500, "%s: command not understood.", cbuf);
2457ea022d16SRodney W. Grimes }
2458ea022d16SRodney W. Grimes 
2459ea022d16SRodney W. Grimes void
2460e4bc453cSWarner Losh delete(char *name)
2461ea022d16SRodney W. Grimes {
2462ea022d16SRodney W. Grimes 	struct stat st;
2463ea022d16SRodney W. Grimes 
2464ea022d16SRodney W. Grimes 	LOGCMD("delete", name);
24651b0e12d7SYaroslav Tykhiy 	if (lstat(name, &st) < 0) {
2466ea022d16SRodney W. Grimes 		perror_reply(550, name);
2467ea022d16SRodney W. Grimes 		return;
2468ea022d16SRodney W. Grimes 	}
2469ac4f2391SYaroslav Tykhiy 	if (S_ISDIR(st.st_mode)) {
2470ea022d16SRodney W. Grimes 		if (rmdir(name) < 0) {
2471ea022d16SRodney W. Grimes 			perror_reply(550, name);
2472ea022d16SRodney W. Grimes 			return;
2473ea022d16SRodney W. Grimes 		}
2474ea022d16SRodney W. Grimes 		goto done;
2475ea022d16SRodney W. Grimes 	}
24763f8b9cfeSYaroslav Tykhiy 	if (guest && noguestmod) {
247702c97492SYaroslav Tykhiy 		reply(550, "Operation not permitted.");
24783f8b9cfeSYaroslav Tykhiy 		return;
24793f8b9cfeSYaroslav Tykhiy 	}
24803f8b9cfeSYaroslav Tykhiy 	if (unlink(name) < 0) {
2481ea022d16SRodney W. Grimes 		perror_reply(550, name);
2482ea022d16SRodney W. Grimes 		return;
2483ea022d16SRodney W. Grimes 	}
2484ea022d16SRodney W. Grimes done:
2485ea022d16SRodney W. Grimes 	ack("DELE");
2486ea022d16SRodney W. Grimes }
2487ea022d16SRodney W. Grimes 
2488ea022d16SRodney W. Grimes void
2489e4bc453cSWarner Losh cwd(char *path)
2490ea022d16SRodney W. Grimes {
2491ea022d16SRodney W. Grimes 
2492ea022d16SRodney W. Grimes 	if (chdir(path) < 0)
2493ea022d16SRodney W. Grimes 		perror_reply(550, path);
2494ea022d16SRodney W. Grimes 	else
2495ea022d16SRodney W. Grimes 		ack("CWD");
2496ea022d16SRodney W. Grimes }
2497ea022d16SRodney W. Grimes 
2498ea022d16SRodney W. Grimes void
2499e4bc453cSWarner Losh makedir(char *name)
2500ea022d16SRodney W. Grimes {
25012b748987SYaroslav Tykhiy 	char *s;
2502ea022d16SRodney W. Grimes 
2503ea022d16SRodney W. Grimes 	LOGCMD("mkdir", name);
2504d186bb12SMatthew N. Dodd 	if (guest && noguestmkd)
2505405e2987SYaroslav Tykhiy 		reply(550, "Operation not permitted.");
2506d186bb12SMatthew N. Dodd 	else if (mkdir(name, 0777) < 0)
2507ea022d16SRodney W. Grimes 		perror_reply(550, name);
25082b748987SYaroslav Tykhiy 	else {
25092b748987SYaroslav Tykhiy 		if ((s = doublequote(name)) == NULL)
25102b748987SYaroslav Tykhiy 			fatalerror("Ran out of memory.");
25112b748987SYaroslav Tykhiy 		reply(257, "\"%s\" directory created.", s);
25122b748987SYaroslav Tykhiy 		free(s);
25132b748987SYaroslav Tykhiy 	}
2514ea022d16SRodney W. Grimes }
2515ea022d16SRodney W. Grimes 
2516ea022d16SRodney W. Grimes void
2517e4bc453cSWarner Losh removedir(char *name)
2518ea022d16SRodney W. Grimes {
2519ea022d16SRodney W. Grimes 
2520ea022d16SRodney W. Grimes 	LOGCMD("rmdir", name);
2521ea022d16SRodney W. Grimes 	if (rmdir(name) < 0)
2522ea022d16SRodney W. Grimes 		perror_reply(550, name);
2523ea022d16SRodney W. Grimes 	else
2524ea022d16SRodney W. Grimes 		ack("RMD");
2525ea022d16SRodney W. Grimes }
2526ea022d16SRodney W. Grimes 
2527ea022d16SRodney W. Grimes void
2528e4bc453cSWarner Losh pwd(void)
2529ea022d16SRodney W. Grimes {
2530e4648f05SYaroslav Tykhiy 	char *s, path[MAXPATHLEN + 1];
2531ea022d16SRodney W. Grimes 
2532de9b6c03SYaroslav Tykhiy 	if (getcwd(path, sizeof(path)) == NULL)
253375933089SYaroslav Tykhiy 		perror_reply(550, "Get current directory");
2534e4648f05SYaroslav Tykhiy 	else {
2535e4648f05SYaroslav Tykhiy 		if ((s = doublequote(path)) == NULL)
2536e4648f05SYaroslav Tykhiy 			fatalerror("Ran out of memory.");
2537e4648f05SYaroslav Tykhiy 		reply(257, "\"%s\" is current directory.", s);
2538e4648f05SYaroslav Tykhiy 		free(s);
2539e4648f05SYaroslav Tykhiy 	}
2540ea022d16SRodney W. Grimes }
2541ea022d16SRodney W. Grimes 
2542ea022d16SRodney W. Grimes char *
2543e4bc453cSWarner Losh renamefrom(char *name)
2544ea022d16SRodney W. Grimes {
2545ea022d16SRodney W. Grimes 	struct stat st;
2546ea022d16SRodney W. Grimes 
2547b943b3c4SYaroslav Tykhiy 	if (guest && noguestmod) {
254802c97492SYaroslav Tykhiy 		reply(550, "Operation not permitted.");
2549b943b3c4SYaroslav Tykhiy 		return (NULL);
2550b943b3c4SYaroslav Tykhiy 	}
25511b0e12d7SYaroslav Tykhiy 	if (lstat(name, &st) < 0) {
2552ea022d16SRodney W. Grimes 		perror_reply(550, name);
2553385f9bf0SYaroslav Tykhiy 		return (NULL);
2554ea022d16SRodney W. Grimes 	}
255502c97492SYaroslav Tykhiy 	reply(350, "File exists, ready for destination name.");
2556ea022d16SRodney W. Grimes 	return (name);
2557ea022d16SRodney W. Grimes }
2558ea022d16SRodney W. Grimes 
2559ea022d16SRodney W. Grimes void
2560e4bc453cSWarner Losh renamecmd(char *from, char *to)
2561ea022d16SRodney W. Grimes {
256261f891a6SPaul Traina 	struct stat st;
2563ea022d16SRodney W. Grimes 
2564ea022d16SRodney W. Grimes 	LOGCMD2("rename", from, to);
256561f891a6SPaul Traina 
256661f891a6SPaul Traina 	if (guest && (stat(to, &st) == 0)) {
256702c97492SYaroslav Tykhiy 		reply(550, "%s: permission denied.", to);
256861f891a6SPaul Traina 		return;
256961f891a6SPaul Traina 	}
257061f891a6SPaul Traina 
2571ea022d16SRodney W. Grimes 	if (rename(from, to) < 0)
2572ea022d16SRodney W. Grimes 		perror_reply(550, "rename");
2573ea022d16SRodney W. Grimes 	else
2574ea022d16SRodney W. Grimes 		ack("RNTO");
2575ea022d16SRodney W. Grimes }
2576ea022d16SRodney W. Grimes 
2577ea022d16SRodney W. Grimes static void
2578e4bc453cSWarner Losh dolog(struct sockaddr *who)
2579ea022d16SRodney W. Grimes {
25804dd8b5abSYoshinobu Inoue 	int error;
25814dd8b5abSYoshinobu Inoue 
25824dd8b5abSYoshinobu Inoue 	realhostname_sa(remotehost, sizeof(remotehost) - 1, who, who->sa_len);
258304683b2cSYaroslav Tykhiy 	remotehost[sizeof(remotehost) - 1] = 0;
2584ea022d16SRodney W. Grimes 
2585ea022d16SRodney W. Grimes #ifdef SETPROCTITLE
2586ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
2587ea4e54b9SDavid Nugent 	if (thishost != firsthost)
2588ea4e54b9SDavid Nugent 		snprintf(proctitle, sizeof(proctitle), "%s: connected (to %s)",
2589ea4e54b9SDavid Nugent 			 remotehost, hostname);
2590ea4e54b9SDavid Nugent 	else
2591ea4e54b9SDavid Nugent #endif
2592ea4e54b9SDavid Nugent 		snprintf(proctitle, sizeof(proctitle), "%s: connected",
2593ea4e54b9SDavid Nugent 			 remotehost);
2594b63e1fe2SPeter Wemm 	setproctitle("%s", proctitle);
2595ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */
2596ea022d16SRodney W. Grimes 
2597ea4e54b9SDavid Nugent 	if (logging) {
2598ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
2599ea4e54b9SDavid Nugent 		if (thishost != firsthost)
2600ea4e54b9SDavid Nugent 			syslog(LOG_INFO, "connection from %s (to %s)",
2601ea4e54b9SDavid Nugent 			       remotehost, hostname);
2602ea4e54b9SDavid Nugent 		else
2603ea4e54b9SDavid Nugent #endif
26044dd8b5abSYoshinobu Inoue 		{
260504683b2cSYaroslav Tykhiy 			char	who_name[NI_MAXHOST];
26064dd8b5abSYoshinobu Inoue 
26074dd8b5abSYoshinobu Inoue 			error = getnameinfo(who, who->sa_len,
26084dd8b5abSYoshinobu Inoue 					    who_name, sizeof(who_name) - 1,
2609b0f06defSHajimu UMEMOTO 					    NULL, 0, NI_NUMERICHOST);
261004683b2cSYaroslav Tykhiy 			who_name[sizeof(who_name) - 1] = 0;
2611f5c57d05SEivind Eklund 			syslog(LOG_INFO, "connection from %s (%s)", remotehost,
26124dd8b5abSYoshinobu Inoue 			       error == 0 ? who_name : "");
26134dd8b5abSYoshinobu Inoue 		}
2614ea022d16SRodney W. Grimes 	}
2615ea4e54b9SDavid Nugent }
2616ea022d16SRodney W. Grimes 
2617ea022d16SRodney W. Grimes /*
2618ea022d16SRodney W. Grimes  * Record logout in wtmp file
2619ea022d16SRodney W. Grimes  * and exit with supplied status.
2620ea022d16SRodney W. Grimes  */
2621ea022d16SRodney W. Grimes void
2622e4bc453cSWarner Losh dologout(int status)
2623ea022d16SRodney W. Grimes {
26240b4df2eeSDavid Greenman 	/*
26250b4df2eeSDavid Greenman 	 * Prevent reception of SIGURG from resulting in a resumption
26260b4df2eeSDavid Greenman 	 * back to the main program loop.
26270b4df2eeSDavid Greenman 	 */
26280b4df2eeSDavid Greenman 	transflag = 0;
2629ea022d16SRodney W. Grimes 
26305d7e0128SYaroslav Tykhiy 	if (logged_in && dowtmp) {
263152e7ee74SYaroslav Tykhiy 		(void) seteuid(0);
263246948173SHajimu UMEMOTO 		ftpd_logwtmp(ttyline, "", NULL);
2633ea022d16SRodney W. Grimes 	}
2634ea022d16SRodney W. Grimes 	/* beware of flushing buffers after a SIGPIPE */
2635ea022d16SRodney W. Grimes 	_exit(status);
2636ea022d16SRodney W. Grimes }
2637ea022d16SRodney W. Grimes 
2638ea022d16SRodney W. Grimes static void
2639e4bc453cSWarner Losh sigurg(int signo)
2640ea022d16SRodney W. Grimes {
26414b82fc95SYaroslav Tykhiy 
26424b82fc95SYaroslav Tykhiy 	recvurg = 1;
26434b82fc95SYaroslav Tykhiy }
26444b82fc95SYaroslav Tykhiy 
26454b82fc95SYaroslav Tykhiy static void
2646e4bc453cSWarner Losh myoob(void)
26474b82fc95SYaroslav Tykhiy {
2648ea022d16SRodney W. Grimes 	char *cp;
2649ea022d16SRodney W. Grimes 
2650ea022d16SRodney W. Grimes 	/* only process if transfer occurring */
2651ea022d16SRodney W. Grimes 	if (!transflag)
2652ea022d16SRodney W. Grimes 		return;
2653ea022d16SRodney W. Grimes 	cp = tmpline;
2654ea022d16SRodney W. Grimes 	if (getline(cp, 7, stdin) == NULL) {
2655ea022d16SRodney W. Grimes 		reply(221, "You could at least say goodbye.");
2656ea022d16SRodney W. Grimes 		dologout(0);
2657ea022d16SRodney W. Grimes 	}
2658ea022d16SRodney W. Grimes 	upper(cp);
2659ea022d16SRodney W. Grimes 	if (strcmp(cp, "ABOR\r\n") == 0) {
2660ea022d16SRodney W. Grimes 		tmpline[0] = '\0';
2661ea022d16SRodney W. Grimes 		reply(426, "Transfer aborted. Data connection closed.");
266202c97492SYaroslav Tykhiy 		reply(226, "Abort successful.");
2663ea022d16SRodney W. Grimes 	}
2664ea022d16SRodney W. Grimes 	if (strcmp(cp, "STAT\r\n") == 0) {
26659db4bbf3SMichael Haro 		tmpline[0] = '\0';
26660e519c96SYaroslav Tykhiy 		if (file_size != -1)
266702c97492SYaroslav Tykhiy 			reply(213, "Status: %jd of %jd bytes transferred.",
2668a57e1ef0SYaroslav Tykhiy 				   (intmax_t)byte_count, (intmax_t)file_size);
2669ea022d16SRodney W. Grimes 		else
267002c97492SYaroslav Tykhiy 			reply(213, "Status: %jd bytes transferred.",
2671a57e1ef0SYaroslav Tykhiy 				   (intmax_t)byte_count);
2672ea022d16SRodney W. Grimes 	}
2673ea022d16SRodney W. Grimes }
2674ea022d16SRodney W. Grimes 
2675ea022d16SRodney W. Grimes /*
2676ea022d16SRodney W. Grimes  * Note: a response of 425 is not mentioned as a possible response to
2677ea022d16SRodney W. Grimes  *	the PASV command in RFC959. However, it has been blessed as
2678ea022d16SRodney W. Grimes  *	a legitimate response by Jon Postel in a telephone conversation
2679ea022d16SRodney W. Grimes  *	with Rick Adams on 25 Jan 89.
2680ea022d16SRodney W. Grimes  */
2681ea022d16SRodney W. Grimes void
2682e4bc453cSWarner Losh passive(void)
2683ea022d16SRodney W. Grimes {
26848af7c9a3SYaroslav Tykhiy 	int len, on;
2685ea022d16SRodney W. Grimes 	char *p, *a;
2686ea022d16SRodney W. Grimes 
268761f891a6SPaul Traina 	if (pdata >= 0)		/* close old port if one set */
268861f891a6SPaul Traina 		close(pdata);
268961f891a6SPaul Traina 
26904dd8b5abSYoshinobu Inoue 	pdata = socket(ctrl_addr.su_family, SOCK_STREAM, 0);
2691ea022d16SRodney W. Grimes 	if (pdata < 0) {
2692ea022d16SRodney W. Grimes 		perror_reply(425, "Can't open passive connection");
2693ea022d16SRodney W. Grimes 		return;
2694ea022d16SRodney W. Grimes 	}
26958af7c9a3SYaroslav Tykhiy 	on = 1;
26968af7c9a3SYaroslav Tykhiy 	if (setsockopt(pdata, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
26978af7c9a3SYaroslav Tykhiy 		syslog(LOG_WARNING, "pdata setsockopt (SO_REUSEADDR): %m");
26984c450ad7SPaul Traina 
269952e7ee74SYaroslav Tykhiy 	(void) seteuid(0);
270061f891a6SPaul Traina 
2701dacc9752SPaul Traina #ifdef IP_PORTRANGE
27024dd8b5abSYoshinobu Inoue 	if (ctrl_addr.su_family == AF_INET) {
27038af7c9a3SYaroslav Tykhiy 	    on = restricted_data_ports ? IP_PORTRANGE_HIGH
2704dacc9752SPaul Traina 				       : IP_PORTRANGE_DEFAULT;
2705dacc9752SPaul Traina 
270640e9d39eSPeter Wemm 	    if (setsockopt(pdata, IPPROTO_IP, IP_PORTRANGE,
270757d4ef07SYaroslav Tykhiy 			    &on, sizeof(on)) < 0)
27084c450ad7SPaul Traina 		    goto pasv_error;
27094c450ad7SPaul Traina 	}
2710dacc9752SPaul Traina #endif
27112db39860SNick Sayer #ifdef IPV6_PORTRANGE
27122db39860SNick Sayer 	if (ctrl_addr.su_family == AF_INET6) {
27138af7c9a3SYaroslav Tykhiy 	    on = restricted_data_ports ? IPV6_PORTRANGE_HIGH
27142db39860SNick Sayer 				       : IPV6_PORTRANGE_DEFAULT;
27152db39860SNick Sayer 
27162db39860SNick Sayer 	    if (setsockopt(pdata, IPPROTO_IPV6, IPV6_PORTRANGE,
271757d4ef07SYaroslav Tykhiy 			    &on, sizeof(on)) < 0)
27182db39860SNick Sayer 		    goto pasv_error;
27192db39860SNick Sayer 	}
27202db39860SNick Sayer #endif
272140e9d39eSPeter Wemm 
2722ea022d16SRodney W. Grimes 	pasv_addr = ctrl_addr;
27234dd8b5abSYoshinobu Inoue 	pasv_addr.su_port = 0;
27244dd8b5abSYoshinobu Inoue 	if (bind(pdata, (struct sockaddr *)&pasv_addr, pasv_addr.su_len) < 0)
2725ea022d16SRodney W. Grimes 		goto pasv_error;
272661f891a6SPaul Traina 
272752e7ee74SYaroslav Tykhiy 	(void) seteuid(pw->pw_uid);
27284c450ad7SPaul Traina 
2729ea022d16SRodney W. Grimes 	len = sizeof(pasv_addr);
2730ea022d16SRodney W. Grimes 	if (getsockname(pdata, (struct sockaddr *) &pasv_addr, &len) < 0)
2731ea022d16SRodney W. Grimes 		goto pasv_error;
2732ea022d16SRodney W. Grimes 	if (listen(pdata, 1) < 0)
2733ea022d16SRodney W. Grimes 		goto pasv_error;
27344dd8b5abSYoshinobu Inoue 	if (pasv_addr.su_family == AF_INET)
27354dd8b5abSYoshinobu Inoue 		a = (char *) &pasv_addr.su_sin.sin_addr;
27364dd8b5abSYoshinobu Inoue 	else if (pasv_addr.su_family == AF_INET6 &&
27374dd8b5abSYoshinobu Inoue 		 IN6_IS_ADDR_V4MAPPED(&pasv_addr.su_sin6.sin6_addr))
27384dd8b5abSYoshinobu Inoue 		a = (char *) &pasv_addr.su_sin6.sin6_addr.s6_addr[12];
27394dd8b5abSYoshinobu Inoue 	else
27404dd8b5abSYoshinobu Inoue 		goto pasv_error;
27414dd8b5abSYoshinobu Inoue 
27424dd8b5abSYoshinobu Inoue 	p = (char *) &pasv_addr.su_port;
2743ea022d16SRodney W. Grimes 
2744ea022d16SRodney W. Grimes #define UC(b) (((int) b) & 0xff)
2745ea022d16SRodney W. Grimes 
2746ea022d16SRodney W. Grimes 	reply(227, "Entering Passive Mode (%d,%d,%d,%d,%d,%d)", UC(a[0]),
2747ea022d16SRodney W. Grimes 		UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1]));
2748ea022d16SRodney W. Grimes 	return;
2749ea022d16SRodney W. Grimes 
2750ea022d16SRodney W. Grimes pasv_error:
275152e7ee74SYaroslav Tykhiy 	(void) seteuid(pw->pw_uid);
2752ea022d16SRodney W. Grimes 	(void) close(pdata);
2753ea022d16SRodney W. Grimes 	pdata = -1;
2754ea022d16SRodney W. Grimes 	perror_reply(425, "Can't open passive connection");
2755ea022d16SRodney W. Grimes 	return;
2756ea022d16SRodney W. Grimes }
2757ea022d16SRodney W. Grimes 
2758ea022d16SRodney W. Grimes /*
27594dd8b5abSYoshinobu Inoue  * Long Passive defined in RFC 1639.
27604dd8b5abSYoshinobu Inoue  *     228 Entering Long Passive Mode
27614dd8b5abSYoshinobu Inoue  *         (af, hal, h1, h2, h3,..., pal, p1, p2...)
27624dd8b5abSYoshinobu Inoue  */
27634dd8b5abSYoshinobu Inoue 
27644dd8b5abSYoshinobu Inoue void
2765e4bc453cSWarner Losh long_passive(char *cmd, int pf)
27664dd8b5abSYoshinobu Inoue {
27678af7c9a3SYaroslav Tykhiy 	int len, on;
27684dd8b5abSYoshinobu Inoue 	char *p, *a;
27694dd8b5abSYoshinobu Inoue 
27704dd8b5abSYoshinobu Inoue 	if (pdata >= 0)		/* close old port if one set */
27714dd8b5abSYoshinobu Inoue 		close(pdata);
27724dd8b5abSYoshinobu Inoue 
27734dd8b5abSYoshinobu Inoue 	if (pf != PF_UNSPEC) {
27744dd8b5abSYoshinobu Inoue 		if (ctrl_addr.su_family != pf) {
27754dd8b5abSYoshinobu Inoue 			switch (ctrl_addr.su_family) {
27764dd8b5abSYoshinobu Inoue 			case AF_INET:
27774dd8b5abSYoshinobu Inoue 				pf = 1;
27784dd8b5abSYoshinobu Inoue 				break;
27794dd8b5abSYoshinobu Inoue 			case AF_INET6:
27804dd8b5abSYoshinobu Inoue 				pf = 2;
27814dd8b5abSYoshinobu Inoue 				break;
27824dd8b5abSYoshinobu Inoue 			default:
27834dd8b5abSYoshinobu Inoue 				pf = 0;
27844dd8b5abSYoshinobu Inoue 				break;
27854dd8b5abSYoshinobu Inoue 			}
27864dd8b5abSYoshinobu Inoue 			/*
27874dd8b5abSYoshinobu Inoue 			 * XXX
27884dd8b5abSYoshinobu Inoue 			 * only EPRT/EPSV ready clients will understand this
27894dd8b5abSYoshinobu Inoue 			 */
27904dd8b5abSYoshinobu Inoue 			if (strcmp(cmd, "EPSV") == 0 && pf) {
27914dd8b5abSYoshinobu Inoue 				reply(522, "Network protocol mismatch, "
27924dd8b5abSYoshinobu Inoue 					"use (%d)", pf);
27934dd8b5abSYoshinobu Inoue 			} else
279402c97492SYaroslav Tykhiy 				reply(501, "Network protocol mismatch."); /*XXX*/
27954dd8b5abSYoshinobu Inoue 
27964dd8b5abSYoshinobu Inoue 			return;
27974dd8b5abSYoshinobu Inoue 		}
27984dd8b5abSYoshinobu Inoue 	}
27994dd8b5abSYoshinobu Inoue 
28004dd8b5abSYoshinobu Inoue 	pdata = socket(ctrl_addr.su_family, SOCK_STREAM, 0);
28014dd8b5abSYoshinobu Inoue 	if (pdata < 0) {
28024dd8b5abSYoshinobu Inoue 		perror_reply(425, "Can't open passive connection");
28034dd8b5abSYoshinobu Inoue 		return;
28044dd8b5abSYoshinobu Inoue 	}
28058af7c9a3SYaroslav Tykhiy 	on = 1;
28068af7c9a3SYaroslav Tykhiy 	if (setsockopt(pdata, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
28078af7c9a3SYaroslav Tykhiy 		syslog(LOG_WARNING, "pdata setsockopt (SO_REUSEADDR): %m");
28084dd8b5abSYoshinobu Inoue 
280952e7ee74SYaroslav Tykhiy 	(void) seteuid(0);
28104dd8b5abSYoshinobu Inoue 
28114dd8b5abSYoshinobu Inoue 	pasv_addr = ctrl_addr;
28124dd8b5abSYoshinobu Inoue 	pasv_addr.su_port = 0;
28134dd8b5abSYoshinobu Inoue 	len = pasv_addr.su_len;
28144dd8b5abSYoshinobu Inoue 
28152db39860SNick Sayer #ifdef IP_PORTRANGE
28162db39860SNick Sayer 	if (ctrl_addr.su_family == AF_INET) {
28178af7c9a3SYaroslav Tykhiy 	    on = restricted_data_ports ? IP_PORTRANGE_HIGH
28182db39860SNick Sayer 				       : IP_PORTRANGE_DEFAULT;
28192db39860SNick Sayer 
28202db39860SNick Sayer 	    if (setsockopt(pdata, IPPROTO_IP, IP_PORTRANGE,
282157d4ef07SYaroslav Tykhiy 			    &on, sizeof(on)) < 0)
28222db39860SNick Sayer 		    goto pasv_error;
28232db39860SNick Sayer 	}
28242db39860SNick Sayer #endif
28252db39860SNick Sayer #ifdef IPV6_PORTRANGE
28262db39860SNick Sayer 	if (ctrl_addr.su_family == AF_INET6) {
28278af7c9a3SYaroslav Tykhiy 	    on = restricted_data_ports ? IPV6_PORTRANGE_HIGH
28282db39860SNick Sayer 				       : IPV6_PORTRANGE_DEFAULT;
28292db39860SNick Sayer 
28302db39860SNick Sayer 	    if (setsockopt(pdata, IPPROTO_IPV6, IPV6_PORTRANGE,
283157d4ef07SYaroslav Tykhiy 			    &on, sizeof(on)) < 0)
28322db39860SNick Sayer 		    goto pasv_error;
28332db39860SNick Sayer 	}
28342db39860SNick Sayer #endif
28352db39860SNick Sayer 
28364dd8b5abSYoshinobu Inoue 	if (bind(pdata, (struct sockaddr *)&pasv_addr, len) < 0)
28374dd8b5abSYoshinobu Inoue 		goto pasv_error;
28384dd8b5abSYoshinobu Inoue 
283952e7ee74SYaroslav Tykhiy 	(void) seteuid(pw->pw_uid);
28404dd8b5abSYoshinobu Inoue 
28414dd8b5abSYoshinobu Inoue 	if (getsockname(pdata, (struct sockaddr *) &pasv_addr, &len) < 0)
28424dd8b5abSYoshinobu Inoue 		goto pasv_error;
28434dd8b5abSYoshinobu Inoue 	if (listen(pdata, 1) < 0)
28444dd8b5abSYoshinobu Inoue 		goto pasv_error;
28454dd8b5abSYoshinobu Inoue 
28464dd8b5abSYoshinobu Inoue #define UC(b) (((int) b) & 0xff)
28474dd8b5abSYoshinobu Inoue 
28484dd8b5abSYoshinobu Inoue 	if (strcmp(cmd, "LPSV") == 0) {
28494dd8b5abSYoshinobu Inoue 		p = (char *)&pasv_addr.su_port;
28504dd8b5abSYoshinobu Inoue 		switch (pasv_addr.su_family) {
28514dd8b5abSYoshinobu Inoue 		case AF_INET:
28524dd8b5abSYoshinobu Inoue 			a = (char *) &pasv_addr.su_sin.sin_addr;
28534dd8b5abSYoshinobu Inoue 		v4_reply:
28544dd8b5abSYoshinobu Inoue 			reply(228,
28554dd8b5abSYoshinobu Inoue "Entering Long Passive Mode (%d,%d,%d,%d,%d,%d,%d,%d,%d)",
28564dd8b5abSYoshinobu Inoue 			      4, 4, UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
28574dd8b5abSYoshinobu Inoue 			      2, UC(p[0]), UC(p[1]));
28584dd8b5abSYoshinobu Inoue 			return;
28594dd8b5abSYoshinobu Inoue 		case AF_INET6:
28604dd8b5abSYoshinobu Inoue 			if (IN6_IS_ADDR_V4MAPPED(&pasv_addr.su_sin6.sin6_addr)) {
28614dd8b5abSYoshinobu Inoue 				a = (char *) &pasv_addr.su_sin6.sin6_addr.s6_addr[12];
28624dd8b5abSYoshinobu Inoue 				goto v4_reply;
28634dd8b5abSYoshinobu Inoue 			}
28644dd8b5abSYoshinobu Inoue 			a = (char *) &pasv_addr.su_sin6.sin6_addr;
28654dd8b5abSYoshinobu Inoue 			reply(228,
28664dd8b5abSYoshinobu Inoue "Entering Long Passive Mode "
28674dd8b5abSYoshinobu Inoue "(%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d)",
28684dd8b5abSYoshinobu Inoue 			      6, 16, UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
28694dd8b5abSYoshinobu Inoue 			      UC(a[4]), UC(a[5]), UC(a[6]), UC(a[7]),
28704dd8b5abSYoshinobu Inoue 			      UC(a[8]), UC(a[9]), UC(a[10]), UC(a[11]),
28714dd8b5abSYoshinobu Inoue 			      UC(a[12]), UC(a[13]), UC(a[14]), UC(a[15]),
28724dd8b5abSYoshinobu Inoue 			      2, UC(p[0]), UC(p[1]));
28734dd8b5abSYoshinobu Inoue 			return;
28744dd8b5abSYoshinobu Inoue 		}
28754dd8b5abSYoshinobu Inoue 	} else if (strcmp(cmd, "EPSV") == 0) {
28764dd8b5abSYoshinobu Inoue 		switch (pasv_addr.su_family) {
28774dd8b5abSYoshinobu Inoue 		case AF_INET:
28784dd8b5abSYoshinobu Inoue 		case AF_INET6:
28794dd8b5abSYoshinobu Inoue 			reply(229, "Entering Extended Passive Mode (|||%d|)",
28804dd8b5abSYoshinobu Inoue 				ntohs(pasv_addr.su_port));
28814dd8b5abSYoshinobu Inoue 			return;
28824dd8b5abSYoshinobu Inoue 		}
28834dd8b5abSYoshinobu Inoue 	} else {
28844dd8b5abSYoshinobu Inoue 		/* more proper error code? */
28854dd8b5abSYoshinobu Inoue 	}
28864dd8b5abSYoshinobu Inoue 
28874dd8b5abSYoshinobu Inoue pasv_error:
288852e7ee74SYaroslav Tykhiy 	(void) seteuid(pw->pw_uid);
28894dd8b5abSYoshinobu Inoue 	(void) close(pdata);
28904dd8b5abSYoshinobu Inoue 	pdata = -1;
28914dd8b5abSYoshinobu Inoue 	perror_reply(425, "Can't open passive connection");
28924dd8b5abSYoshinobu Inoue 	return;
28934dd8b5abSYoshinobu Inoue }
28944dd8b5abSYoshinobu Inoue 
28954dd8b5abSYoshinobu Inoue /*
2896a117c345SYaroslav Tykhiy  * Generate unique name for file with basename "local"
2897a117c345SYaroslav Tykhiy  * and open the file in order to avoid possible races.
2898a117c345SYaroslav Tykhiy  * Try "local" first, then "local.1", "local.2" etc, up to "local.99".
2899a117c345SYaroslav Tykhiy  * Return descriptor to the file, set "name" to its name.
2900a117c345SYaroslav Tykhiy  *
2901ea022d16SRodney W. Grimes  * Generates failure reply on error.
2902ea022d16SRodney W. Grimes  */
2903a117c345SYaroslav Tykhiy static int
2904a117c345SYaroslav Tykhiy guniquefd(char *local, char **name)
2905ea022d16SRodney W. Grimes {
2906ea022d16SRodney W. Grimes 	static char new[MAXPATHLEN];
2907ea022d16SRodney W. Grimes 	struct stat st;
2908ea022d16SRodney W. Grimes 	char *cp;
2909a117c345SYaroslav Tykhiy 	int count;
2910a117c345SYaroslav Tykhiy 	int fd;
2911ea022d16SRodney W. Grimes 
2912ea022d16SRodney W. Grimes 	cp = strrchr(local, '/');
2913ea022d16SRodney W. Grimes 	if (cp)
2914ea022d16SRodney W. Grimes 		*cp = '\0';
2915ea022d16SRodney W. Grimes 	if (stat(cp ? local : ".", &st) < 0) {
2916ea022d16SRodney W. Grimes 		perror_reply(553, cp ? local : ".");
2917a117c345SYaroslav Tykhiy 		return (-1);
2918ea022d16SRodney W. Grimes 	}
2919a117c345SYaroslav Tykhiy 	if (cp) {
2920a117c345SYaroslav Tykhiy 		/*
2921a117c345SYaroslav Tykhiy 		 * Let not overwrite dirname with counter suffix.
2922a117c345SYaroslav Tykhiy 		 * -4 is for /nn\0
2923a117c345SYaroslav Tykhiy 		 * In this extreme case dot won't be put in front of suffix.
2924a117c345SYaroslav Tykhiy 		 */
2925a117c345SYaroslav Tykhiy 		if (strlen(local) > sizeof(new) - 4) {
292602c97492SYaroslav Tykhiy 			reply(553, "Pathname too long.");
2927a117c345SYaroslav Tykhiy 			return (-1);
2928a117c345SYaroslav Tykhiy 		}
2929ea022d16SRodney W. Grimes 		*cp = '/';
2930a117c345SYaroslav Tykhiy 	}
2931e760ef2cSWarner Losh 	/* -4 is for the .nn<null> we put on the end below */
2932e760ef2cSWarner Losh 	(void) snprintf(new, sizeof(new) - 4, "%s", local);
2933ea022d16SRodney W. Grimes 	cp = new + strlen(new);
2934a117c345SYaroslav Tykhiy 	/*
2935a117c345SYaroslav Tykhiy 	 * Don't generate dotfile unless requested explicitly.
2936a117c345SYaroslav Tykhiy 	 * This covers the case when basename gets truncated off
2937a117c345SYaroslav Tykhiy 	 * by buffer size.
2938a117c345SYaroslav Tykhiy 	 */
2939a117c345SYaroslav Tykhiy 	if (cp > new && cp[-1] != '/')
2940ea022d16SRodney W. Grimes 		*cp++ = '.';
2941a117c345SYaroslav Tykhiy 	for (count = 0; count < 100; count++) {
2942a117c345SYaroslav Tykhiy 		/* At count 0 try unmodified name */
2943a117c345SYaroslav Tykhiy 		if (count)
2944ea022d16SRodney W. Grimes 			(void)sprintf(cp, "%d", count);
2945a117c345SYaroslav Tykhiy 		if ((fd = open(count ? new : local,
2946a117c345SYaroslav Tykhiy 		    O_RDWR | O_CREAT | O_EXCL, 0666)) >= 0) {
2947a117c345SYaroslav Tykhiy 			*name = count ? new : local;
2948a117c345SYaroslav Tykhiy 			return (fd);
2949a117c345SYaroslav Tykhiy 		}
295088b70721SYaroslav Tykhiy 		if (errno != EEXIST) {
295150618d61SYaroslav Tykhiy 			perror_reply(553, count ? new : local);
295288b70721SYaroslav Tykhiy 			return (-1);
295388b70721SYaroslav Tykhiy 		}
2954ea022d16SRodney W. Grimes 	}
2955ea022d16SRodney W. Grimes 	reply(452, "Unique file name cannot be created.");
2956a117c345SYaroslav Tykhiy 	return (-1);
2957ea022d16SRodney W. Grimes }
2958ea022d16SRodney W. Grimes 
2959ea022d16SRodney W. Grimes /*
2960ea022d16SRodney W. Grimes  * Format and send reply containing system error number.
2961ea022d16SRodney W. Grimes  */
2962ea022d16SRodney W. Grimes void
2963e4bc453cSWarner Losh perror_reply(int code, char *string)
2964ea022d16SRodney W. Grimes {
2965ea022d16SRodney W. Grimes 
2966ea022d16SRodney W. Grimes 	reply(code, "%s: %s.", string, strerror(errno));
2967ea022d16SRodney W. Grimes }
2968ea022d16SRodney W. Grimes 
2969ea022d16SRodney W. Grimes static char *onefile[] = {
2970ea022d16SRodney W. Grimes 	"",
2971ea022d16SRodney W. Grimes 	0
2972ea022d16SRodney W. Grimes };
2973ea022d16SRodney W. Grimes 
2974ea022d16SRodney W. Grimes void
2975e4bc453cSWarner Losh send_file_list(char *whichf)
2976ea022d16SRodney W. Grimes {
2977ea022d16SRodney W. Grimes 	struct stat st;
2978ea022d16SRodney W. Grimes 	DIR *dirp = NULL;
2979ea022d16SRodney W. Grimes 	struct dirent *dir;
2980ea022d16SRodney W. Grimes 	FILE *dout = NULL;
2981ea022d16SRodney W. Grimes 	char **dirlist, *dirname;
2982ea022d16SRodney W. Grimes 	int simple = 0;
2983ea022d16SRodney W. Grimes 	int freeglob = 0;
2984ea022d16SRodney W. Grimes 	glob_t gl;
2985ea022d16SRodney W. Grimes 
2986ea022d16SRodney W. Grimes 	if (strpbrk(whichf, "~{[*?") != NULL) {
298712da320bSMike Heffner 		int flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_TILDE;
2988ea022d16SRodney W. Grimes 
2989ea022d16SRodney W. Grimes 		memset(&gl, 0, sizeof(gl));
29906d10cb2fSJonathan Lemon 		gl.gl_matchc = MAXGLOBARGS;
299175dc5f1aSMike Heffner 		flags |= GLOB_LIMIT;
2992ea022d16SRodney W. Grimes 		freeglob = 1;
2993ea022d16SRodney W. Grimes 		if (glob(whichf, flags, 0, &gl)) {
299402c97492SYaroslav Tykhiy 			reply(550, "No matching files found.");
2995ea022d16SRodney W. Grimes 			goto out;
2996ea022d16SRodney W. Grimes 		} else if (gl.gl_pathc == 0) {
2997ea022d16SRodney W. Grimes 			errno = ENOENT;
2998ea022d16SRodney W. Grimes 			perror_reply(550, whichf);
2999ea022d16SRodney W. Grimes 			goto out;
3000ea022d16SRodney W. Grimes 		}
3001ea022d16SRodney W. Grimes 		dirlist = gl.gl_pathv;
3002ea022d16SRodney W. Grimes 	} else {
3003ea022d16SRodney W. Grimes 		onefile[0] = whichf;
3004ea022d16SRodney W. Grimes 		dirlist = onefile;
3005ea022d16SRodney W. Grimes 		simple = 1;
3006ea022d16SRodney W. Grimes 	}
3007ea022d16SRodney W. Grimes 
30089aca17cbSMark Murray 	while ((dirname = *dirlist++)) {
3009ea022d16SRodney W. Grimes 		if (stat(dirname, &st) < 0) {
3010ea022d16SRodney W. Grimes 			/*
3011ea022d16SRodney W. Grimes 			 * If user typed "ls -l", etc, and the client
3012ea022d16SRodney W. Grimes 			 * used NLST, do what the user meant.
3013ea022d16SRodney W. Grimes 			 */
3014ea022d16SRodney W. Grimes 			if (dirname[0] == '-' && *dirlist == NULL &&
3015ea022d16SRodney W. Grimes 			    transflag == 0) {
3016af85d782SDavid Nugent 				retrieve(_PATH_LS " %s", dirname);
3017ea022d16SRodney W. Grimes 				goto out;
3018ea022d16SRodney W. Grimes 			}
3019ea022d16SRodney W. Grimes 			perror_reply(550, whichf);
3020ea022d16SRodney W. Grimes 			if (dout != NULL) {
3021ea022d16SRodney W. Grimes 				(void) fclose(dout);
3022ea022d16SRodney W. Grimes 				transflag = 0;
3023ea022d16SRodney W. Grimes 				data = -1;
3024ea022d16SRodney W. Grimes 				pdata = -1;
3025ea022d16SRodney W. Grimes 			}
3026ea022d16SRodney W. Grimes 			goto out;
3027ea022d16SRodney W. Grimes 		}
3028ea022d16SRodney W. Grimes 
3029ea022d16SRodney W. Grimes 		if (S_ISREG(st.st_mode)) {
3030ea022d16SRodney W. Grimes 			if (dout == NULL) {
30310e519c96SYaroslav Tykhiy 				dout = dataconn("file list", -1, "w");
3032ea022d16SRodney W. Grimes 				if (dout == NULL)
3033ea022d16SRodney W. Grimes 					goto out;
3034ea022d16SRodney W. Grimes 				transflag++;
3035ea022d16SRodney W. Grimes 			}
3036ea022d16SRodney W. Grimes 			fprintf(dout, "%s%s\n", dirname,
3037ea022d16SRodney W. Grimes 				type == TYPE_A ? "\r" : "");
3038ea022d16SRodney W. Grimes 			byte_count += strlen(dirname) + 1;
3039ea022d16SRodney W. Grimes 			continue;
3040ea022d16SRodney W. Grimes 		} else if (!S_ISDIR(st.st_mode))
3041ea022d16SRodney W. Grimes 			continue;
3042ea022d16SRodney W. Grimes 
3043ea022d16SRodney W. Grimes 		if ((dirp = opendir(dirname)) == NULL)
3044ea022d16SRodney W. Grimes 			continue;
3045ea022d16SRodney W. Grimes 
3046ea022d16SRodney W. Grimes 		while ((dir = readdir(dirp)) != NULL) {
3047ea022d16SRodney W. Grimes 			char nbuf[MAXPATHLEN];
3048ea022d16SRodney W. Grimes 
30494b82fc95SYaroslav Tykhiy 			if (recvurg) {
30504b82fc95SYaroslav Tykhiy 				myoob();
30514b82fc95SYaroslav Tykhiy 				recvurg = 0;
30524b82fc95SYaroslav Tykhiy 				transflag = 0;
30534b82fc95SYaroslav Tykhiy 				goto out;
30544b82fc95SYaroslav Tykhiy 			}
30554b82fc95SYaroslav Tykhiy 
3056ea022d16SRodney W. Grimes 			if (dir->d_name[0] == '.' && dir->d_namlen == 1)
3057ea022d16SRodney W. Grimes 				continue;
3058ea022d16SRodney W. Grimes 			if (dir->d_name[0] == '.' && dir->d_name[1] == '.' &&
3059ea022d16SRodney W. Grimes 			    dir->d_namlen == 2)
3060ea022d16SRodney W. Grimes 				continue;
3061ea022d16SRodney W. Grimes 
3062e760ef2cSWarner Losh 			snprintf(nbuf, sizeof(nbuf),
3063e760ef2cSWarner Losh 				"%s/%s", dirname, dir->d_name);
3064ea022d16SRodney W. Grimes 
3065ea022d16SRodney W. Grimes 			/*
3066ea022d16SRodney W. Grimes 			 * We have to do a stat to insure it's
3067ea022d16SRodney W. Grimes 			 * not a directory or special file.
3068ea022d16SRodney W. Grimes 			 */
3069ea022d16SRodney W. Grimes 			if (simple || (stat(nbuf, &st) == 0 &&
3070ea022d16SRodney W. Grimes 			    S_ISREG(st.st_mode))) {
3071ea022d16SRodney W. Grimes 				if (dout == NULL) {
30720e519c96SYaroslav Tykhiy 					dout = dataconn("file list", -1, "w");
3073ea022d16SRodney W. Grimes 					if (dout == NULL)
3074ea022d16SRodney W. Grimes 						goto out;
3075ea022d16SRodney W. Grimes 					transflag++;
3076ea022d16SRodney W. Grimes 				}
3077ea022d16SRodney W. Grimes 				if (nbuf[0] == '.' && nbuf[1] == '/')
3078ea022d16SRodney W. Grimes 					fprintf(dout, "%s%s\n", &nbuf[2],
3079ea022d16SRodney W. Grimes 						type == TYPE_A ? "\r" : "");
3080ea022d16SRodney W. Grimes 				else
3081ea022d16SRodney W. Grimes 					fprintf(dout, "%s%s\n", nbuf,
3082ea022d16SRodney W. Grimes 						type == TYPE_A ? "\r" : "");
3083ea022d16SRodney W. Grimes 				byte_count += strlen(nbuf) + 1;
3084ea022d16SRodney W. Grimes 			}
3085ea022d16SRodney W. Grimes 		}
3086ea022d16SRodney W. Grimes 		(void) closedir(dirp);
3087ea022d16SRodney W. Grimes 	}
3088ea022d16SRodney W. Grimes 
3089ea022d16SRodney W. Grimes 	if (dout == NULL)
3090ea022d16SRodney W. Grimes 		reply(550, "No files found.");
3091ea022d16SRodney W. Grimes 	else if (ferror(dout) != 0)
3092ea022d16SRodney W. Grimes 		perror_reply(550, "Data connection");
3093ea022d16SRodney W. Grimes 	else
3094ea022d16SRodney W. Grimes 		reply(226, "Transfer complete.");
3095ea022d16SRodney W. Grimes 
3096ea022d16SRodney W. Grimes 	transflag = 0;
3097ea022d16SRodney W. Grimes 	if (dout != NULL)
3098ea022d16SRodney W. Grimes 		(void) fclose(dout);
3099ea022d16SRodney W. Grimes 	data = -1;
3100ea022d16SRodney W. Grimes 	pdata = -1;
3101ea022d16SRodney W. Grimes out:
3102ea022d16SRodney W. Grimes 	if (freeglob) {
3103ea022d16SRodney W. Grimes 		freeglob = 0;
3104ea022d16SRodney W. Grimes 		globfree(&gl);
3105ea022d16SRodney W. Grimes 	}
3106ea022d16SRodney W. Grimes }
3107ea022d16SRodney W. Grimes 
3108cf09a206SDavid Greenman void
3109e4bc453cSWarner Losh reapchild(int signo)
3110cf09a206SDavid Greenman {
3111de9b6c03SYaroslav Tykhiy 	while (waitpid(-1, NULL, WNOHANG) > 0);
3112cf09a206SDavid Greenman }
3113cf09a206SDavid Greenman 
3114b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE
3115ea022d16SRodney W. Grimes /*
3116ea022d16SRodney W. Grimes  * Clobber argv so ps will show what we're doing.  (Stolen from sendmail.)
3117ea022d16SRodney W. Grimes  * Warning, since this is usually started from inetd.conf, it often doesn't
3118ea022d16SRodney W. Grimes  * have much of an environment or arglist to overwrite.
3119ea022d16SRodney W. Grimes  */
3120ea022d16SRodney W. Grimes void
3121ea022d16SRodney W. Grimes setproctitle(const char *fmt, ...)
3122ea022d16SRodney W. Grimes {
3123ea022d16SRodney W. Grimes 	int i;
3124ea022d16SRodney W. Grimes 	va_list ap;
3125ea022d16SRodney W. Grimes 	char *p, *bp, ch;
3126ea022d16SRodney W. Grimes 	char buf[LINE_MAX];
3127ea022d16SRodney W. Grimes 
3128ea022d16SRodney W. Grimes 	va_start(ap, fmt);
3129ea022d16SRodney W. Grimes 	(void)vsnprintf(buf, sizeof(buf), fmt, ap);
3130ea022d16SRodney W. Grimes 
3131ea022d16SRodney W. Grimes 	/* make ps print our process name */
3132ea022d16SRodney W. Grimes 	p = Argv[0];
3133ea022d16SRodney W. Grimes 	*p++ = '-';
3134ea022d16SRodney W. Grimes 
3135ea022d16SRodney W. Grimes 	i = strlen(buf);
3136ea022d16SRodney W. Grimes 	if (i > LastArgv - p - 2) {
3137ea022d16SRodney W. Grimes 		i = LastArgv - p - 2;
3138ea022d16SRodney W. Grimes 		buf[i] = '\0';
3139ea022d16SRodney W. Grimes 	}
3140ea022d16SRodney W. Grimes 	bp = buf;
3141ea022d16SRodney W. Grimes 	while (ch = *bp++)
3142ea022d16SRodney W. Grimes 		if (ch != '\n' && ch != '\r')
3143ea022d16SRodney W. Grimes 			*p++ = ch;
3144ea022d16SRodney W. Grimes 	while (p < LastArgv)
3145ea022d16SRodney W. Grimes 		*p++ = ' ';
3146ea022d16SRodney W. Grimes }
3147b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */
31483eb568f2SGuido van Rooij 
314961f891a6SPaul Traina static void
31506b2dee6bSYaroslav Tykhiy appendf(char **strp, char *fmt, ...)
31516b2dee6bSYaroslav Tykhiy {
31526b2dee6bSYaroslav Tykhiy 	va_list ap;
31536b2dee6bSYaroslav Tykhiy 	char *ostr, *p;
31546b2dee6bSYaroslav Tykhiy 
31556b2dee6bSYaroslav Tykhiy 	va_start(ap, fmt);
31566b2dee6bSYaroslav Tykhiy 	vasprintf(&p, fmt, ap);
31576b2dee6bSYaroslav Tykhiy 	va_end(ap);
31586b2dee6bSYaroslav Tykhiy 	if (p == NULL)
31596b2dee6bSYaroslav Tykhiy 		fatalerror("Ran out of memory.");
31606b2dee6bSYaroslav Tykhiy 	if (*strp == NULL)
31616b2dee6bSYaroslav Tykhiy 		*strp = p;
31626b2dee6bSYaroslav Tykhiy 	else {
31636b2dee6bSYaroslav Tykhiy 		ostr = *strp;
31646b2dee6bSYaroslav Tykhiy 		asprintf(strp, "%s%s", ostr, p);
31656b2dee6bSYaroslav Tykhiy 		if (*strp == NULL)
31666b2dee6bSYaroslav Tykhiy 			fatalerror("Ran out of memory.");
31676b2dee6bSYaroslav Tykhiy 		free(ostr);
31686b2dee6bSYaroslav Tykhiy 	}
31696b2dee6bSYaroslav Tykhiy }
31706b2dee6bSYaroslav Tykhiy 
31716b2dee6bSYaroslav Tykhiy static void
31726b2dee6bSYaroslav Tykhiy logcmd(char *cmd, char *file1, char *file2, off_t cnt)
31736b2dee6bSYaroslav Tykhiy {
31746b2dee6bSYaroslav Tykhiy 	char *msg = NULL;
31756b2dee6bSYaroslav Tykhiy 	char wd[MAXPATHLEN + 1];
31766b2dee6bSYaroslav Tykhiy 
31776b2dee6bSYaroslav Tykhiy 	if (logging <= 1)
31786b2dee6bSYaroslav Tykhiy 		return;
31796b2dee6bSYaroslav Tykhiy 	/* If either filename isn't absolute, get current dir for log message. */
31806b2dee6bSYaroslav Tykhiy 	if ((file1 && file1[0] != '/') || (file2 && file2[0] != '/')) {
31816b2dee6bSYaroslav Tykhiy 		if (getcwd(wd, sizeof(wd) - 1) == NULL)
31826b2dee6bSYaroslav Tykhiy 			strcpy(wd, strerror(errno));
31836b2dee6bSYaroslav Tykhiy 	} else
31846b2dee6bSYaroslav Tykhiy 		wd[0] = '\0';
31856b2dee6bSYaroslav Tykhiy 
31866b2dee6bSYaroslav Tykhiy 	appendf(&msg, "%s", cmd);
31876b2dee6bSYaroslav Tykhiy 	if (file1)
31886b2dee6bSYaroslav Tykhiy 		appendf(&msg, " %s", file1);
31896b2dee6bSYaroslav Tykhiy 	if (file2)
31906b2dee6bSYaroslav Tykhiy 		appendf(&msg, " %s", file2);
31916b2dee6bSYaroslav Tykhiy 	if (cnt >= 0)
31926b2dee6bSYaroslav Tykhiy 		appendf(&msg, " = %jd bytes", (intmax_t)cnt);
31936b2dee6bSYaroslav Tykhiy 	if (wd[0])
31946b2dee6bSYaroslav Tykhiy 		appendf(&msg, " (wd: %s)", wd);
3195215a9f9dSYaroslav Tykhiy 	if (guest || dochroot)
3196215a9f9dSYaroslav Tykhiy 		appendf(&msg, " (chroot: %s)", chrootdir);
31976b2dee6bSYaroslav Tykhiy 	syslog(LOG_INFO, "%s", msg);
31986b2dee6bSYaroslav Tykhiy 	free(msg);
31996b2dee6bSYaroslav Tykhiy }
32006b2dee6bSYaroslav Tykhiy 
32016b2dee6bSYaroslav Tykhiy static void
3202e4bc453cSWarner Losh logxfer(char *name, off_t size, time_t start)
32033eb568f2SGuido van Rooij {
32048c1c21f2SYaroslav Tykhiy 	char buf[MAXPATHLEN + 1024];
32053eb568f2SGuido van Rooij 	char path[MAXPATHLEN + 1];
3206158a00b2SJohn Birrell 	time_t now;
32073eb568f2SGuido van Rooij 
32088c1c21f2SYaroslav Tykhiy 	if (statfd >= 0) {
32093eb568f2SGuido van Rooij 		time(&now);
32108c1c21f2SYaroslav Tykhiy 		if (realpath(name, path) == NULL) {
32118c1c21f2SYaroslav Tykhiy 			syslog(LOG_NOTICE, "realpath failed on %s: %m", path);
32128c1c21f2SYaroslav Tykhiy 			return;
32138c1c21f2SYaroslav Tykhiy 		}
32148c1c21f2SYaroslav Tykhiy 		snprintf(buf, sizeof(buf), "%.20s!%s!%s!%s!%jd!%ld\n",
32153eb568f2SGuido van Rooij 			ctime(&now)+4, ident, remotehost,
32168c1c21f2SYaroslav Tykhiy 			path, (intmax_t)size,
3217e4a71114SAndrey A. Chernov 			(long)(now - start + (now == start)));
32183eb568f2SGuido van Rooij 		write(statfd, buf, strlen(buf));
32193eb568f2SGuido van Rooij 	}
32203eb568f2SGuido van Rooij }
3221e4648f05SYaroslav Tykhiy 
3222e4648f05SYaroslav Tykhiy static char *
3223e4648f05SYaroslav Tykhiy doublequote(char *s)
3224e4648f05SYaroslav Tykhiy {
3225e4648f05SYaroslav Tykhiy 	int n;
3226e4648f05SYaroslav Tykhiy 	char *p, *s2;
3227e4648f05SYaroslav Tykhiy 
3228e4648f05SYaroslav Tykhiy 	for (p = s, n = 0; *p; p++)
3229e4648f05SYaroslav Tykhiy 		if (*p == '"')
3230e4648f05SYaroslav Tykhiy 			n++;
3231e4648f05SYaroslav Tykhiy 
3232e4648f05SYaroslav Tykhiy 	if ((s2 = malloc(p - s + n + 1)) == NULL)
3233e4648f05SYaroslav Tykhiy 		return (NULL);
3234e4648f05SYaroslav Tykhiy 
3235e4648f05SYaroslav Tykhiy 	for (p = s2; *s; s++, p++) {
3236e4648f05SYaroslav Tykhiy 		if ((*p = *s) == '"')
3237e4648f05SYaroslav Tykhiy 			*(++p) = '"';
3238e4648f05SYaroslav Tykhiy 	}
3239e4648f05SYaroslav Tykhiy 	*p = '\0';
3240e4648f05SYaroslav Tykhiy 
3241e4648f05SYaroslav Tykhiy 	return (s2);
3242e4648f05SYaroslav Tykhiy }
3243206fe568SHajimu UMEMOTO 
3244206fe568SHajimu UMEMOTO /* setup server socket for specified address family */
3245206fe568SHajimu UMEMOTO /* if af is PF_UNSPEC more than one socket may be returned */
3246206fe568SHajimu UMEMOTO /* the returned list is dynamically allocated, so caller needs to free it */
3247206fe568SHajimu UMEMOTO static int *
3248206fe568SHajimu UMEMOTO socksetup(int af, char *bindname, const char *bindport)
3249206fe568SHajimu UMEMOTO {
3250206fe568SHajimu UMEMOTO 	struct addrinfo hints, *res, *r;
3251206fe568SHajimu UMEMOTO 	int error, maxs, *s, *socks;
3252206fe568SHajimu UMEMOTO 	const int on = 1;
3253206fe568SHajimu UMEMOTO 
3254206fe568SHajimu UMEMOTO 	memset(&hints, 0, sizeof(hints));
3255206fe568SHajimu UMEMOTO 	hints.ai_flags = AI_PASSIVE;
3256206fe568SHajimu UMEMOTO 	hints.ai_family = af;
3257206fe568SHajimu UMEMOTO 	hints.ai_socktype = SOCK_STREAM;
3258206fe568SHajimu UMEMOTO 	error = getaddrinfo(bindname, bindport, &hints, &res);
3259206fe568SHajimu UMEMOTO 	if (error) {
3260206fe568SHajimu UMEMOTO 		syslog(LOG_ERR, "%s", gai_strerror(error));
3261206fe568SHajimu UMEMOTO 		if (error == EAI_SYSTEM)
3262206fe568SHajimu UMEMOTO 			syslog(LOG_ERR, "%s", strerror(errno));
3263206fe568SHajimu UMEMOTO 		return NULL;
3264206fe568SHajimu UMEMOTO 	}
3265206fe568SHajimu UMEMOTO 
3266206fe568SHajimu UMEMOTO 	/* Count max number of sockets we may open */
3267206fe568SHajimu UMEMOTO 	for (maxs = 0, r = res; r; r = r->ai_next, maxs++)
3268206fe568SHajimu UMEMOTO 		;
3269206fe568SHajimu UMEMOTO 	socks = malloc((maxs + 1) * sizeof(int));
3270206fe568SHajimu UMEMOTO 	if (!socks) {
3271206fe568SHajimu UMEMOTO 		freeaddrinfo(res);
3272206fe568SHajimu UMEMOTO 		syslog(LOG_ERR, "couldn't allocate memory for sockets");
3273206fe568SHajimu UMEMOTO 		return NULL;
3274206fe568SHajimu UMEMOTO 	}
3275206fe568SHajimu UMEMOTO 
3276206fe568SHajimu UMEMOTO 	*socks = 0;   /* num of sockets counter at start of array */
3277206fe568SHajimu UMEMOTO 	s = socks + 1;
3278206fe568SHajimu UMEMOTO 	for (r = res; r; r = r->ai_next) {
3279206fe568SHajimu UMEMOTO 		*s = socket(r->ai_family, r->ai_socktype, r->ai_protocol);
3280206fe568SHajimu UMEMOTO 		if (*s < 0) {
3281206fe568SHajimu UMEMOTO 			syslog(LOG_DEBUG, "control socket: %m");
3282206fe568SHajimu UMEMOTO 			continue;
3283206fe568SHajimu UMEMOTO 		}
3284206fe568SHajimu UMEMOTO 		if (setsockopt(*s, SOL_SOCKET, SO_REUSEADDR,
3285206fe568SHajimu UMEMOTO 		    &on, sizeof(on)) < 0)
3286206fe568SHajimu UMEMOTO 			syslog(LOG_WARNING,
3287206fe568SHajimu UMEMOTO 			    "control setsockopt (SO_REUSEADDR): %m");
3288206fe568SHajimu UMEMOTO 		if (r->ai_family == AF_INET6) {
3289206fe568SHajimu UMEMOTO 			if (setsockopt(*s, IPPROTO_IPV6, IPV6_V6ONLY,
3290206fe568SHajimu UMEMOTO 			    &on, sizeof(on)) < 0)
3291206fe568SHajimu UMEMOTO 				syslog(LOG_WARNING,
3292206fe568SHajimu UMEMOTO 				    "control setsockopt (IPV6_V6ONLY): %m");
3293206fe568SHajimu UMEMOTO 		}
3294206fe568SHajimu UMEMOTO 		if (bind(*s, r->ai_addr, r->ai_addrlen) < 0) {
3295206fe568SHajimu UMEMOTO 			syslog(LOG_DEBUG, "control bind: %m");
3296206fe568SHajimu UMEMOTO 			close(*s);
3297206fe568SHajimu UMEMOTO 			continue;
3298206fe568SHajimu UMEMOTO 		}
3299206fe568SHajimu UMEMOTO 		(*socks)++;
3300206fe568SHajimu UMEMOTO 		s++;
3301206fe568SHajimu UMEMOTO 	}
3302206fe568SHajimu UMEMOTO 
3303206fe568SHajimu UMEMOTO 	if (res)
3304206fe568SHajimu UMEMOTO 		freeaddrinfo(res);
3305206fe568SHajimu UMEMOTO 
3306206fe568SHajimu UMEMOTO 	if (*socks == 0) {
3307206fe568SHajimu UMEMOTO 		syslog(LOG_ERR, "control socket: Couldn't bind to any socket");
3308206fe568SHajimu UMEMOTO 		free(socks);
3309206fe568SHajimu UMEMOTO 		return NULL;
3310206fe568SHajimu UMEMOTO 	}
3311206fe568SHajimu UMEMOTO 	return(socks);
3312206fe568SHajimu UMEMOTO }
3313