xref: /freebsd/libexec/ftpd/ftpd.c (revision de9b6c03438a7531f55d122324c04815d13fabe8)
1ea022d16SRodney W. Grimes /*
2ea022d16SRodney W. Grimes  * Copyright (c) 1985, 1988, 1990, 1992, 1993, 1994
3ea022d16SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4ea022d16SRodney W. Grimes  *
5ea022d16SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6ea022d16SRodney W. Grimes  * modification, are permitted provided that the following conditions
7ea022d16SRodney W. Grimes  * are met:
8ea022d16SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9ea022d16SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10ea022d16SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11ea022d16SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12ea022d16SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13ea022d16SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
14ea022d16SRodney W. Grimes  *    must display the following acknowledgement:
15ea022d16SRodney W. Grimes  *	This product includes software developed by the University of
16ea022d16SRodney W. Grimes  *	California, Berkeley and its contributors.
17ea022d16SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
18ea022d16SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
19ea022d16SRodney W. Grimes  *    without specific prior written permission.
20ea022d16SRodney W. Grimes  *
21ea022d16SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22ea022d16SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23ea022d16SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24ea022d16SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25ea022d16SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26ea022d16SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27ea022d16SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28ea022d16SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29ea022d16SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30ea022d16SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31ea022d16SRodney W. Grimes  * SUCH DAMAGE.
32ea022d16SRodney W. Grimes  */
33ea022d16SRodney W. Grimes 
349aca17cbSMark Murray #if 0
35ea022d16SRodney W. Grimes #ifndef lint
36ea022d16SRodney W. Grimes static char copyright[] =
37ea022d16SRodney W. Grimes "@(#) Copyright (c) 1985, 1988, 1990, 1992, 1993, 1994\n\
38ea022d16SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
39ea022d16SRodney W. Grimes #endif /* not lint */
409aca17cbSMark Murray #endif
41ea022d16SRodney W. Grimes 
42ea022d16SRodney W. Grimes #ifndef lint
43e02897faSPhilippe Charnier #if 0
44ea022d16SRodney W. Grimes static char sccsid[] = "@(#)ftpd.c	8.4 (Berkeley) 4/16/94";
459aca17cbSMark Murray #endif
46e02897faSPhilippe Charnier static const char rcsid[] =
477f3dea24SPeter Wemm   "$FreeBSD$";
48e02897faSPhilippe Charnier #endif /* not lint */
49ea022d16SRodney W. Grimes 
50ea022d16SRodney W. Grimes /*
51ea022d16SRodney W. Grimes  * FTP server.
52ea022d16SRodney W. Grimes  */
53ea022d16SRodney W. Grimes #include <sys/param.h>
54ea022d16SRodney W. Grimes #include <sys/ioctl.h>
559fc5823aSGarrett Wollman #include <sys/mman.h>
56eb2fc780SGarrett Wollman #include <sys/socket.h>
57eb2fc780SGarrett Wollman #include <sys/stat.h>
58eb2fc780SGarrett Wollman #include <sys/time.h>
59eb2fc780SGarrett Wollman #include <sys/wait.h>
60ea022d16SRodney W. Grimes 
61ea022d16SRodney W. Grimes #include <netinet/in.h>
62ea022d16SRodney W. Grimes #include <netinet/in_systm.h>
63ea022d16SRodney W. Grimes #include <netinet/ip.h>
649fc5823aSGarrett Wollman #include <netinet/tcp.h>
65ea022d16SRodney W. Grimes 
66ea022d16SRodney W. Grimes #define	FTP_NAMES
67ea022d16SRodney W. Grimes #include <arpa/ftp.h>
68ea022d16SRodney W. Grimes #include <arpa/inet.h>
69ea022d16SRodney W. Grimes #include <arpa/telnet.h>
70ea022d16SRodney W. Grimes 
71ea022d16SRodney W. Grimes #include <ctype.h>
72ea022d16SRodney W. Grimes #include <dirent.h>
73ea022d16SRodney W. Grimes #include <err.h>
74ea022d16SRodney W. Grimes #include <errno.h>
75ea022d16SRodney W. Grimes #include <fcntl.h>
76ea022d16SRodney W. Grimes #include <glob.h>
77ea022d16SRodney W. Grimes #include <limits.h>
78ea022d16SRodney W. Grimes #include <netdb.h>
79ea022d16SRodney W. Grimes #include <pwd.h>
8031fea7b8SDavid Nugent #include <grp.h>
8147499ecdSAndrey A. Chernov #include <opie.h>
82ea022d16SRodney W. Grimes #include <signal.h>
83a57e1ef0SYaroslav Tykhiy #include <stdint.h>
84ea022d16SRodney W. Grimes #include <stdio.h>
85ea022d16SRodney W. Grimes #include <stdlib.h>
86ea022d16SRodney W. Grimes #include <string.h>
87ea022d16SRodney W. Grimes #include <syslog.h>
88ea022d16SRodney W. Grimes #include <time.h>
89ea022d16SRodney W. Grimes #include <unistd.h>
90b63e1fe2SPeter Wemm #include <libutil.h>
91b071c689SDavid Nugent #ifdef	LOGIN_CAP
92b071c689SDavid Nugent #include <login_cap.h>
93b071c689SDavid Nugent #endif
94ea022d16SRodney W. Grimes 
955bc9d93dSMark Murray #ifdef USE_PAM
966c9134c0SMark Murray #include <security/pam_appl.h>
976c9134c0SMark Murray #endif
986c9134c0SMark Murray 
99ea022d16SRodney W. Grimes #include "pathnames.h"
100ea022d16SRodney W. Grimes #include "extern.h"
101ea022d16SRodney W. Grimes 
102ea022d16SRodney W. Grimes #include <stdarg.h>
103ea022d16SRodney W. Grimes 
104af85d782SDavid Nugent static char version[] = "Version 6.00LS";
105af85d782SDavid Nugent #undef main
106ea022d16SRodney W. Grimes 
107ea022d16SRodney W. Grimes extern	off_t restart_point;
108ea022d16SRodney W. Grimes extern	char cbuf[];
109ea022d16SRodney W. Grimes 
1104dd8b5abSYoshinobu Inoue union sockunion ctrl_addr;
1114dd8b5abSYoshinobu Inoue union sockunion data_source;
1124dd8b5abSYoshinobu Inoue union sockunion data_dest;
1134dd8b5abSYoshinobu Inoue union sockunion his_addr;
1144dd8b5abSYoshinobu Inoue union sockunion pasv_addr;
115ea022d16SRodney W. Grimes 
116cf09a206SDavid Greenman int	daemon_mode;
117ea022d16SRodney W. Grimes int	data;
11863591ba5SYaroslav Tykhiy int	dataport;
119c152df28SYaroslav Tykhiy int	hostinfo = 1;	/* print host-specific info in messages */
120ea022d16SRodney W. Grimes int	logged_in;
121ea022d16SRodney W. Grimes struct	passwd *pw;
122ce9287fcSYaroslav Tykhiy char	*homedir;
123618b0bbaSMark Murray int	ftpdebug;
124ea022d16SRodney W. Grimes int	timeout = 900;    /* timeout after 15 minutes of inactivity */
125ea022d16SRodney W. Grimes int	maxtimeout = 7200;/* don't allow idle time to be set beyond 2 hours */
126ea022d16SRodney W. Grimes int	logging;
1274c450ad7SPaul Traina int	restricted_data_ports = 1;
128a5a4544eSPaul Traina int	paranoid = 1;	  /* be extra careful about security */
1295a392aecSTorsten Blum int	anon_only = 0;    /* Only anonymous ftp allowed */
130ea022d16SRodney W. Grimes int	guest;
131a5a4544eSPaul Traina int	dochroot;
1325d7e0128SYaroslav Tykhiy int	dowtmp = 1;
1333eb568f2SGuido van Rooij int	stats;
1343eb568f2SGuido van Rooij int	statfd = -1;
135ea022d16SRodney W. Grimes int	type;
136ea022d16SRodney W. Grimes int	form;
137ea022d16SRodney W. Grimes int	stru;			/* avoid C keyword */
138ea022d16SRodney W. Grimes int	mode;
139ea022d16SRodney W. Grimes int	usedefault = 1;		/* for data transfers */
140ea022d16SRodney W. Grimes int	pdata = -1;		/* for passive mode */
141a4b77a2aSPoul-Henning Kamp int	readonly=0;		/* Server is in readonly mode.	*/
142a4b77a2aSPoul-Henning Kamp int	noepsv=0;		/* EPSV command is disabled.	*/
14362513e76SNik Clayton int	noretr=0;		/* RETR command is disabled.	*/
1441cc9f0bbSSheldon Hearn int	noguestretr=0;		/* RETR command is disabled for anon users. */
145d186bb12SMatthew N. Dodd int	noguestmkd=0;		/* MKD command is disabled for anon users. */
146a117c345SYaroslav Tykhiy int	noguestmod=1;		/* anon users may not modify existing files. */
14762513e76SNik Clayton 
1484b82fc95SYaroslav Tykhiy static volatile sig_atomic_t recvurg;
149ea022d16SRodney W. Grimes sig_atomic_t transflag;
150ea022d16SRodney W. Grimes off_t	file_size;
151ea022d16SRodney W. Grimes off_t	byte_count;
152ea022d16SRodney W. Grimes #if !defined(CMASK) || CMASK == 0
153ea022d16SRodney W. Grimes #undef CMASK
154ea022d16SRodney W. Grimes #define CMASK 027
155ea022d16SRodney W. Grimes #endif
156ea022d16SRodney W. Grimes int	defumask = CMASK;		/* default umask value */
157ea022d16SRodney W. Grimes char	tmpline[7];
158ea4e54b9SDavid Nugent char	*hostname;
159ad442344SDima Dorfman int	epsvall = 0;
160ad442344SDima Dorfman 
1610512556aSDavid Nugent #ifdef VIRTUAL_HOSTING
162ea4e54b9SDavid Nugent char	*ftpuser;
163ea4e54b9SDavid Nugent 
164ea4e54b9SDavid Nugent static struct ftphost {
165ea4e54b9SDavid Nugent 	struct ftphost	*next;
166f38c6cadSYoshinobu Inoue 	struct addrinfo *hostinfo;
167ea4e54b9SDavid Nugent 	char		*hostname;
168ea4e54b9SDavid Nugent 	char		*anonuser;
169ea4e54b9SDavid Nugent 	char		*statfile;
170ea4e54b9SDavid Nugent 	char		*welcome;
171ea4e54b9SDavid Nugent 	char		*loginmsg;
172ea4e54b9SDavid Nugent } *thishost, *firsthost;
173ea4e54b9SDavid Nugent 
174ea4e54b9SDavid Nugent #endif
1759e9a43bdSBrian Somers char	remotehost[MAXHOSTNAMELEN];
1763eb568f2SGuido van Rooij char	*ident = NULL;
177a5a4544eSPaul Traina 
178a5a4544eSPaul Traina static char ttyline[20];
179a5a4544eSPaul Traina char	*tty = ttyline;		/* for klogin */
180a5a4544eSPaul Traina 
1815bc9d93dSMark Murray #ifdef USE_PAM
182e4bc453cSWarner Losh static int	auth_pam(struct passwd**, const char*);
1835bc9d93dSMark Murray pam_handle_t *pamh = NULL;
18447499ecdSAndrey A. Chernov #endif
185fa1746c9SMark Murray 
186fa1746c9SMark Murray static struct opie opiedata;
187fa1746c9SMark Murray static char opieprompt[OPIE_CHALLENGE_MAX+1];
18847499ecdSAndrey A. Chernov static int pwok;
1899aca17cbSMark Murray 
190105a3c98SJulian Elischer char	*pid_file = NULL;
191105a3c98SJulian Elischer 
192ea022d16SRodney W. Grimes /*
1936d10cb2fSJonathan Lemon  * Limit number of pathnames that glob can return.
1946d10cb2fSJonathan Lemon  * A limit of 0 indicates the number of pathnames is unlimited.
1956d10cb2fSJonathan Lemon  */
1966d10cb2fSJonathan Lemon #define MAXGLOBARGS	16384
1976d10cb2fSJonathan Lemon #
1986d10cb2fSJonathan Lemon 
1996d10cb2fSJonathan Lemon /*
200ea022d16SRodney W. Grimes  * Timeout intervals for retrying connections
201ea022d16SRodney W. Grimes  * to hosts that don't accept PORT cmds.  This
202ea022d16SRodney W. Grimes  * is a kludge, but given the problems with TCP...
203ea022d16SRodney W. Grimes  */
204ea022d16SRodney W. Grimes #define	SWAITMAX	90	/* wait at most 90 seconds */
205ea022d16SRodney W. Grimes #define	SWAITINT	5	/* interval between retries */
206ea022d16SRodney W. Grimes 
207ea022d16SRodney W. Grimes int	swaitmax = SWAITMAX;
208ea022d16SRodney W. Grimes int	swaitint = SWAITINT;
209ea022d16SRodney W. Grimes 
210ea022d16SRodney W. Grimes #ifdef SETPROCTITLE
211b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE
212ea022d16SRodney W. Grimes char	**Argv = NULL;		/* pointer to argument vector */
213ea022d16SRodney W. Grimes char	*LastArgv = NULL;	/* end of argv */
214b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */
215ea022d16SRodney W. Grimes char	proctitle[LINE_MAX];	/* initial part of title */
216ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */
217ea022d16SRodney W. Grimes 
218ea022d16SRodney W. Grimes #define LOGCMD(cmd, file) \
219ea022d16SRodney W. Grimes 	if (logging > 1) \
220ea022d16SRodney W. Grimes 	    syslog(LOG_INFO,"%s %s%s", cmd, \
221ea022d16SRodney W. Grimes 		*(file) == '/' ? "" : curdir(), file);
222ea022d16SRodney W. Grimes #define LOGCMD2(cmd, file1, file2) \
223ea022d16SRodney W. Grimes 	 if (logging > 1) \
224ea022d16SRodney W. Grimes 	    syslog(LOG_INFO,"%s %s%s %s%s", cmd, \
225ea022d16SRodney W. Grimes 		*(file1) == '/' ? "" : curdir(), file1, \
226ea022d16SRodney W. Grimes 		*(file2) == '/' ? "" : curdir(), file2);
227ea022d16SRodney W. Grimes #define LOGBYTES(cmd, file, cnt) \
228ea022d16SRodney W. Grimes 	if (logging > 1) { \
2290e519c96SYaroslav Tykhiy 		if (cnt == -1) \
230ea022d16SRodney W. Grimes 		    syslog(LOG_INFO,"%s %s%s", cmd, \
231ea022d16SRodney W. Grimes 			*(file) == '/' ? "" : curdir(), file); \
232ea022d16SRodney W. Grimes 		else \
233a57e1ef0SYaroslav Tykhiy 		    syslog(LOG_INFO, "%s %s%s = %jd bytes", \
234a57e1ef0SYaroslav Tykhiy 			cmd, (*(file) == '/') ? "" : curdir(), file, \
235a57e1ef0SYaroslav Tykhiy 			(intmax_t)cnt); \
236ea022d16SRodney W. Grimes 	}
237ea022d16SRodney W. Grimes 
238ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
239e4bc453cSWarner Losh static void	 inithosts(void);
240e4bc453cSWarner Losh static void	selecthost(union sockunion *);
241ea4e54b9SDavid Nugent #endif
242e4bc453cSWarner Losh static void	 ack(char *);
243e4bc453cSWarner Losh static void	 sigurg(int);
244e4bc453cSWarner Losh static void	 myoob(void);
2458657b576SYaroslav Tykhiy static int	 checkuser(char *, char *, int, char **);
246e4bc453cSWarner Losh static FILE	*dataconn(char *, off_t, char *);
247e4bc453cSWarner Losh static void	 dolog(struct sockaddr *);
248e4bc453cSWarner Losh static char	*curdir(void);
249e4bc453cSWarner Losh static void	 end_login(void);
250e4bc453cSWarner Losh static FILE	*getdatasock(char *);
251a117c345SYaroslav Tykhiy static int	 guniquefd(char *, char **);
252e4bc453cSWarner Losh static void	 lostconn(int);
253e4bc453cSWarner Losh static void	 sigquit(int);
254e4bc453cSWarner Losh static int	 receive_data(FILE *, FILE *);
2556e4b0a55SYaroslav Tykhiy static int	 send_data(FILE *, FILE *, size_t, off_t, int);
256ea022d16SRodney W. Grimes static struct passwd *
257e4bc453cSWarner Losh 		 sgetpwnam(char *);
258e4bc453cSWarner Losh static char	*sgetsave(char *);
259e4bc453cSWarner Losh static void	 reapchild(int);
260e4bc453cSWarner Losh static void      logxfer(char *, off_t, time_t);
261e4648f05SYaroslav Tykhiy static char	*doublequote(char *);
262206fe568SHajimu UMEMOTO static int	*socksetup(int, char *, const char *);
263ea022d16SRodney W. Grimes 
264ea022d16SRodney W. Grimes static char *
265e4bc453cSWarner Losh curdir(void)
266ea022d16SRodney W. Grimes {
267ea022d16SRodney W. Grimes 	static char path[MAXPATHLEN+1+1];	/* path + '/' + '\0' */
268ea022d16SRodney W. Grimes 
269ea022d16SRodney W. Grimes 	if (getcwd(path, sizeof(path)-2) == NULL)
270ea022d16SRodney W. Grimes 		return ("");
271ea022d16SRodney W. Grimes 	if (path[1] != '\0')		/* special case for root dir. */
272ea022d16SRodney W. Grimes 		strcat(path, "/");
273ea022d16SRodney W. Grimes 	/* For guest account, skip / since it's chrooted */
274ea022d16SRodney W. Grimes 	return (guest ? path+1 : path);
275ea022d16SRodney W. Grimes }
276ea022d16SRodney W. Grimes 
277ea022d16SRodney W. Grimes int
278e4bc453cSWarner Losh main(int argc, char *argv[], char **envp)
279ea022d16SRodney W. Grimes {
280ea022d16SRodney W. Grimes 	int addrlen, ch, on = 1, tos;
281ea022d16SRodney W. Grimes 	char *cp, line[LINE_MAX];
282ea022d16SRodney W. Grimes 	FILE *fd;
2834dd8b5abSYoshinobu Inoue 	char	*bindname = NULL;
28463591ba5SYaroslav Tykhiy 	const char *bindport = "ftp";
2854dd8b5abSYoshinobu Inoue 	int	family = AF_UNSPEC;
2864b82fc95SYaroslav Tykhiy 	struct sigaction sa;
287ea022d16SRodney W. Grimes 
28834d1ba5cSAndrey A. Chernov 	tzset();		/* in case no timezone database in ~ftp */
2894b82fc95SYaroslav Tykhiy 	sigemptyset(&sa.sa_mask);
2904b82fc95SYaroslav Tykhiy 	sa.sa_flags = SA_RESTART;
29134d1ba5cSAndrey A. Chernov 
292b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE
293ea022d16SRodney W. Grimes 	/*
294ea022d16SRodney W. Grimes 	 *  Save start and extent of argv for setproctitle.
295ea022d16SRodney W. Grimes 	 */
296ea022d16SRodney W. Grimes 	Argv = argv;
297ea022d16SRodney W. Grimes 	while (*envp)
298ea022d16SRodney W. Grimes 		envp++;
299ea022d16SRodney W. Grimes 	LastArgv = envp[-1] + strlen(envp[-1]);
300b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */
301ea022d16SRodney W. Grimes 
3023eb568f2SGuido van Rooij 
303c152df28SYaroslav Tykhiy 	while ((ch = getopt(argc, argv,
304c152df28SYaroslav Tykhiy 	                    "46a:AdDEhlmMoOp:P:rRSt:T:u:UvW")) != -1) {
305ea022d16SRodney W. Grimes 		switch (ch) {
3060e063efeSYaroslav Tykhiy 		case '4':
307206fe568SHajimu UMEMOTO 			family = (family == AF_INET6) ? AF_UNSPEC : AF_INET;
3080e063efeSYaroslav Tykhiy 			break;
3090e063efeSYaroslav Tykhiy 
3100e063efeSYaroslav Tykhiy 		case '6':
311206fe568SHajimu UMEMOTO 			family = (family == AF_INET) ? AF_UNSPEC : AF_INET6;
3120e063efeSYaroslav Tykhiy 			break;
3130e063efeSYaroslav Tykhiy 
3140e063efeSYaroslav Tykhiy 		case 'a':
3150e063efeSYaroslav Tykhiy 			bindname = optarg;
3160e063efeSYaroslav Tykhiy 			break;
3170e063efeSYaroslav Tykhiy 
3180e063efeSYaroslav Tykhiy 		case 'A':
3190e063efeSYaroslav Tykhiy 			anon_only = 1;
320cf09a206SDavid Greenman 			break;
321cf09a206SDavid Greenman 
322ea022d16SRodney W. Grimes 		case 'd':
323618b0bbaSMark Murray 			ftpdebug++;
324ea022d16SRodney W. Grimes 			break;
325ea022d16SRodney W. Grimes 
3260e063efeSYaroslav Tykhiy 		case 'D':
3270e063efeSYaroslav Tykhiy 			daemon_mode++;
3280e063efeSYaroslav Tykhiy 			break;
3290e063efeSYaroslav Tykhiy 
330a4b77a2aSPoul-Henning Kamp 		case 'E':
331a4b77a2aSPoul-Henning Kamp 			noepsv = 1;
332a4b77a2aSPoul-Henning Kamp 			break;
333a4b77a2aSPoul-Henning Kamp 
334c152df28SYaroslav Tykhiy 		case 'h':
335c152df28SYaroslav Tykhiy 			hostinfo = 0;
336c152df28SYaroslav Tykhiy 			break;
337c152df28SYaroslav Tykhiy 
338ea022d16SRodney W. Grimes 		case 'l':
339ea022d16SRodney W. Grimes 			logging++;	/* > 1 == extra logging */
340ea022d16SRodney W. Grimes 			break;
341ea022d16SRodney W. Grimes 
342a117c345SYaroslav Tykhiy 		case 'm':
343a117c345SYaroslav Tykhiy 			noguestmod = 0;
344a117c345SYaroslav Tykhiy 			break;
345a117c345SYaroslav Tykhiy 
3460e063efeSYaroslav Tykhiy 		case 'M':
3470e063efeSYaroslav Tykhiy 			noguestmkd = 1;
3480e063efeSYaroslav Tykhiy 			break;
3490e063efeSYaroslav Tykhiy 
3500e063efeSYaroslav Tykhiy 		case 'o':
3510e063efeSYaroslav Tykhiy 			noretr = 1;
3520e063efeSYaroslav Tykhiy 			break;
3530e063efeSYaroslav Tykhiy 
3540e063efeSYaroslav Tykhiy 		case 'O':
3550e063efeSYaroslav Tykhiy 			noguestretr = 1;
3560e063efeSYaroslav Tykhiy 			break;
3570e063efeSYaroslav Tykhiy 
3580e063efeSYaroslav Tykhiy 		case 'p':
3590e063efeSYaroslav Tykhiy 			pid_file = optarg;
3600e063efeSYaroslav Tykhiy 			break;
3610e063efeSYaroslav Tykhiy 
36263591ba5SYaroslav Tykhiy 		case 'P':
36363591ba5SYaroslav Tykhiy 			bindport = optarg;
36463591ba5SYaroslav Tykhiy 			break;
36563591ba5SYaroslav Tykhiy 
366a4b77a2aSPoul-Henning Kamp 		case 'r':
367a4b77a2aSPoul-Henning Kamp 			readonly = 1;
368a4b77a2aSPoul-Henning Kamp 			break;
369a4b77a2aSPoul-Henning Kamp 
370a5a4544eSPaul Traina 		case 'R':
371a5a4544eSPaul Traina 			paranoid = 0;
372a5a4544eSPaul Traina 			break;
373a5a4544eSPaul Traina 
374a5a4544eSPaul Traina 		case 'S':
375a5a4544eSPaul Traina 			stats++;
376a5a4544eSPaul Traina 			break;
377a5a4544eSPaul Traina 
378ea022d16SRodney W. Grimes 		case 't':
379ea022d16SRodney W. Grimes 			timeout = atoi(optarg);
380ea022d16SRodney W. Grimes 			if (maxtimeout < timeout)
381ea022d16SRodney W. Grimes 				maxtimeout = timeout;
382ea022d16SRodney W. Grimes 			break;
383a5a4544eSPaul Traina 
3840e063efeSYaroslav Tykhiy 		case 'T':
3850e063efeSYaroslav Tykhiy 			maxtimeout = atoi(optarg);
3860e063efeSYaroslav Tykhiy 			if (timeout > maxtimeout)
3870e063efeSYaroslav Tykhiy 				timeout = maxtimeout;
388105a3c98SJulian Elischer 			break;
389105a3c98SJulian Elischer 
390ea022d16SRodney W. Grimes 		case 'u':
391ea022d16SRodney W. Grimes 		    {
392ea022d16SRodney W. Grimes 			long val = 0;
393ea022d16SRodney W. Grimes 
394ea022d16SRodney W. Grimes 			val = strtol(optarg, &optarg, 8);
395ea022d16SRodney W. Grimes 			if (*optarg != '\0' || val < 0)
396ea022d16SRodney W. Grimes 				warnx("bad value for -u");
397ea022d16SRodney W. Grimes 			else
398ea022d16SRodney W. Grimes 				defumask = val;
399ea022d16SRodney W. Grimes 			break;
400ea022d16SRodney W. Grimes 		    }
4010e063efeSYaroslav Tykhiy 		case 'U':
4020e063efeSYaroslav Tykhiy 			restricted_data_ports = 0;
4035a392aecSTorsten Blum 			break;
404ea022d16SRodney W. Grimes 
405ea022d16SRodney W. Grimes 		case 'v':
40693bd9dc5SYaroslav Tykhiy 			ftpdebug++;
407ea022d16SRodney W. Grimes 			break;
408ea022d16SRodney W. Grimes 
4095d7e0128SYaroslav Tykhiy 		case 'W':
4105d7e0128SYaroslav Tykhiy 			dowtmp = 0;
4115d7e0128SYaroslav Tykhiy 			break;
4125d7e0128SYaroslav Tykhiy 
413ea022d16SRodney W. Grimes 		default:
414ea022d16SRodney W. Grimes 			warnx("unknown flag -%c ignored", optopt);
415ea022d16SRodney W. Grimes 			break;
416ea022d16SRodney W. Grimes 		}
417ea022d16SRodney W. Grimes 	}
418cf09a206SDavid Greenman 
419ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
420ea4e54b9SDavid Nugent 	inithosts();
421ea4e54b9SDavid Nugent #endif
422ea022d16SRodney W. Grimes 	(void) freopen(_PATH_DEVNULL, "w", stderr);
423cf09a206SDavid Greenman 
424cf09a206SDavid Greenman 	/*
425cf09a206SDavid Greenman 	 * LOG_NDELAY sets up the logging connection immediately,
426cf09a206SDavid Greenman 	 * necessary for anonymous ftp's that chroot and can't do it later.
427cf09a206SDavid Greenman 	 */
428cf09a206SDavid Greenman 	openlog("ftpd", LOG_PID | LOG_NDELAY, LOG_FTP);
429cf09a206SDavid Greenman 
430cf09a206SDavid Greenman 	if (daemon_mode) {
431206fe568SHajimu UMEMOTO 		int *ctl_sock, fd, maxfd = -1, nfds, i;
432206fe568SHajimu UMEMOTO 		fd_set defreadfds, readfds;
433206fe568SHajimu UMEMOTO 		pid_t pid;
434cf09a206SDavid Greenman 
435cf09a206SDavid Greenman 		/*
436cf09a206SDavid Greenman 		 * Detach from parent.
437cf09a206SDavid Greenman 		 */
438cf09a206SDavid Greenman 		if (daemon(1, 1) < 0) {
439cf09a206SDavid Greenman 			syslog(LOG_ERR, "failed to become a daemon");
440cf09a206SDavid Greenman 			exit(1);
441cf09a206SDavid Greenman 		}
4424b82fc95SYaroslav Tykhiy 		sa.sa_handler = reapchild;
4434b82fc95SYaroslav Tykhiy 		(void)sigaction(SIGCHLD, &sa, NULL);
4444dd8b5abSYoshinobu Inoue 
445cf09a206SDavid Greenman 		/*
446cf09a206SDavid Greenman 		 * Open a socket, bind it to the FTP port, and start
447cf09a206SDavid Greenman 		 * listening.
448cf09a206SDavid Greenman 		 */
449206fe568SHajimu UMEMOTO 		ctl_sock = socksetup(family, bindname, bindport);
450206fe568SHajimu UMEMOTO 		if (ctl_sock == NULL)
451cf09a206SDavid Greenman 			exit(1);
452206fe568SHajimu UMEMOTO 
453206fe568SHajimu UMEMOTO 		FD_ZERO(&defreadfds);
454206fe568SHajimu UMEMOTO 		for (i = 1; i <= *ctl_sock; i++) {
455206fe568SHajimu UMEMOTO 			FD_SET(ctl_sock[i], &defreadfds);
456206fe568SHajimu UMEMOTO 			if (listen(ctl_sock[i], 32) < 0) {
457cf09a206SDavid Greenman 				syslog(LOG_ERR, "control listen: %m");
458cf09a206SDavid Greenman 				exit(1);
459cf09a206SDavid Greenman 			}
460206fe568SHajimu UMEMOTO 			if (maxfd < ctl_sock[i])
461206fe568SHajimu UMEMOTO 				maxfd = ctl_sock[i];
462206fe568SHajimu UMEMOTO 		}
463206fe568SHajimu UMEMOTO 
464cf09a206SDavid Greenman 		/*
465105a3c98SJulian Elischer 		 * Atomically write process ID
466105a3c98SJulian Elischer 		 */
467105a3c98SJulian Elischer 		if (pid_file)
468105a3c98SJulian Elischer 		{
469105a3c98SJulian Elischer 			int fd;
470105a3c98SJulian Elischer 			char buf[20];
471105a3c98SJulian Elischer 
472105a3c98SJulian Elischer 			fd = open(pid_file, O_CREAT | O_WRONLY | O_TRUNC
473105a3c98SJulian Elischer 				| O_NONBLOCK | O_EXLOCK, 0644);
47485966371SWarner Losh 			if (fd < 0) {
475105a3c98SJulian Elischer 				if (errno == EAGAIN)
476105a3c98SJulian Elischer 					errx(1, "%s: file locked", pid_file);
477105a3c98SJulian Elischer 				else
478105a3c98SJulian Elischer 					err(1, "%s", pid_file);
47985966371SWarner Losh 			}
480105a3c98SJulian Elischer 			snprintf(buf, sizeof(buf),
481105a3c98SJulian Elischer 				"%lu\n", (unsigned long) getpid());
482105a3c98SJulian Elischer 			if (write(fd, buf, strlen(buf)) < 0)
483105a3c98SJulian Elischer 				err(1, "%s: write", pid_file);
484105a3c98SJulian Elischer 			/* Leave the pid file open and locked */
485105a3c98SJulian Elischer 		}
486105a3c98SJulian Elischer 		/*
487cf09a206SDavid Greenman 		 * Loop forever accepting connection requests and forking off
488cf09a206SDavid Greenman 		 * children to handle them.
489cf09a206SDavid Greenman 		 */
490cf09a206SDavid Greenman 		while (1) {
491206fe568SHajimu UMEMOTO 			FD_COPY(&defreadfds, &readfds);
492206fe568SHajimu UMEMOTO 			nfds = select(maxfd + 1, &readfds, NULL, NULL, 0);
493206fe568SHajimu UMEMOTO 			if (nfds <= 0) {
494206fe568SHajimu UMEMOTO 				if (nfds < 0 && errno != EINTR)
495206fe568SHajimu UMEMOTO 					syslog(LOG_WARNING, "select: %m");
496206fe568SHajimu UMEMOTO 				continue;
497206fe568SHajimu UMEMOTO 			}
498206fe568SHajimu UMEMOTO 
499206fe568SHajimu UMEMOTO 			pid = -1;
500206fe568SHajimu UMEMOTO                         for (i = 1; i <= *ctl_sock; i++)
501206fe568SHajimu UMEMOTO 				if (FD_ISSET(ctl_sock[i], &readfds)) {
502206fe568SHajimu UMEMOTO 					addrlen = sizeof(his_addr);
503206fe568SHajimu UMEMOTO 					fd = accept(ctl_sock[i],
504206fe568SHajimu UMEMOTO 					    (struct sockaddr *)&his_addr,
505206fe568SHajimu UMEMOTO 					    &addrlen);
50640e67765SMaxim Konovalov 					if (fd >= 0) {
507206fe568SHajimu UMEMOTO 						if ((pid = fork()) == 0) {
508cf09a206SDavid Greenman 							/* child */
509cf09a206SDavid Greenman 							(void) dup2(fd, 0);
510cf09a206SDavid Greenman 							(void) dup2(fd, 1);
511206fe568SHajimu UMEMOTO 							close(ctl_sock[i]);
512206fe568SHajimu UMEMOTO 						} else
513cf09a206SDavid Greenman 							close(fd);
514cf09a206SDavid Greenman 					}
51540e67765SMaxim Konovalov 				}
516206fe568SHajimu UMEMOTO 			if (pid == 0)
517206fe568SHajimu UMEMOTO 				break;
518206fe568SHajimu UMEMOTO 		}
519cf09a206SDavid Greenman 	} else {
520cf09a206SDavid Greenman 		addrlen = sizeof(his_addr);
521cf09a206SDavid Greenman 		if (getpeername(0, (struct sockaddr *)&his_addr, &addrlen) < 0) {
522cf09a206SDavid Greenman 			syslog(LOG_ERR, "getpeername (%s): %m",argv[0]);
523cf09a206SDavid Greenman 			exit(1);
524cf09a206SDavid Greenman 		}
525cf09a206SDavid Greenman 	}
526cf09a206SDavid Greenman 
5274b82fc95SYaroslav Tykhiy 	sa.sa_handler = SIG_DFL;
5284b82fc95SYaroslav Tykhiy 	(void)sigaction(SIGCHLD, &sa, NULL);
5294b82fc95SYaroslav Tykhiy 
5304b82fc95SYaroslav Tykhiy 	sa.sa_handler = sigurg;
5314b82fc95SYaroslav Tykhiy 	sa.sa_flags = 0;		/* don't restart syscalls for SIGURG */
5324b82fc95SYaroslav Tykhiy 	(void)sigaction(SIGURG, &sa, NULL);
5334b82fc95SYaroslav Tykhiy 
5344b82fc95SYaroslav Tykhiy 	sigfillset(&sa.sa_mask);	/* block all signals in handler */
5354b82fc95SYaroslav Tykhiy 	sa.sa_flags = SA_RESTART;
5364b82fc95SYaroslav Tykhiy 	sa.sa_handler = sigquit;
5374b82fc95SYaroslav Tykhiy 	(void)sigaction(SIGHUP, &sa, NULL);
5384b82fc95SYaroslav Tykhiy 	(void)sigaction(SIGINT, &sa, NULL);
5394b82fc95SYaroslav Tykhiy 	(void)sigaction(SIGQUIT, &sa, NULL);
5404b82fc95SYaroslav Tykhiy 	(void)sigaction(SIGTERM, &sa, NULL);
5414b82fc95SYaroslav Tykhiy 
5424b82fc95SYaroslav Tykhiy 	sa.sa_handler = lostconn;
5434b82fc95SYaroslav Tykhiy 	(void)sigaction(SIGPIPE, &sa, NULL);
544ea022d16SRodney W. Grimes 
545cf09a206SDavid Greenman 	addrlen = sizeof(ctrl_addr);
546cf09a206SDavid Greenman 	if (getsockname(0, (struct sockaddr *)&ctrl_addr, &addrlen) < 0) {
547cf09a206SDavid Greenman 		syslog(LOG_ERR, "getsockname (%s): %m",argv[0]);
548cf09a206SDavid Greenman 		exit(1);
549cf09a206SDavid Greenman 	}
55063591ba5SYaroslav Tykhiy 	dataport = ntohs(ctrl_addr.su_port) - 1; /* as per RFC 959 */
551ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
552ea4e54b9SDavid Nugent 	/* select our identity from virtual host table */
5534dd8b5abSYoshinobu Inoue 	selecthost(&ctrl_addr);
554ea4e54b9SDavid Nugent #endif
555cf09a206SDavid Greenman #ifdef IP_TOS
5564dd8b5abSYoshinobu Inoue 	if (ctrl_addr.su_family == AF_INET)
5574dd8b5abSYoshinobu Inoue       {
558cf09a206SDavid Greenman 	tos = IPTOS_LOWDELAY;
55957d4ef07SYaroslav Tykhiy 	if (setsockopt(0, IPPROTO_IP, IP_TOS, &tos, sizeof(int)) < 0)
560406d1ae9SYaroslav Tykhiy 		syslog(LOG_WARNING, "control setsockopt (IP_TOS): %m");
5614dd8b5abSYoshinobu Inoue       }
562cf09a206SDavid Greenman #endif
563dadb9fb3SDavid Greenman 	/*
564dadb9fb3SDavid Greenman 	 * Disable Nagle on the control channel so that we don't have to wait
565dadb9fb3SDavid Greenman 	 * for peer's ACK before issuing our next reply.
566dadb9fb3SDavid Greenman 	 */
567dadb9fb3SDavid Greenman 	if (setsockopt(0, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) < 0)
568406d1ae9SYaroslav Tykhiy 		syslog(LOG_WARNING, "control setsockopt (TCP_NODELAY): %m");
569dadb9fb3SDavid Greenman 
5704dd8b5abSYoshinobu Inoue 	data_source.su_port = htons(ntohs(ctrl_addr.su_port) - 1);
571cf09a206SDavid Greenman 
572a5a4544eSPaul Traina 	/* set this here so klogin can use it... */
573e760ef2cSWarner Losh 	(void)snprintf(ttyline, sizeof(ttyline), "ftp%d", getpid());
574a5a4544eSPaul Traina 
575ea022d16SRodney W. Grimes 	/* Try to handle urgent data inline */
576ea022d16SRodney W. Grimes #ifdef SO_OOBINLINE
57757d4ef07SYaroslav Tykhiy 	if (setsockopt(0, SOL_SOCKET, SO_OOBINLINE, &on, sizeof(on)) < 0)
578406d1ae9SYaroslav Tykhiy 		syslog(LOG_WARNING, "control setsockopt (SO_OOBINLINE): %m");
579ea022d16SRodney W. Grimes #endif
580ea022d16SRodney W. Grimes 
581ea022d16SRodney W. Grimes #ifdef	F_SETOWN
582ea022d16SRodney W. Grimes 	if (fcntl(fileno(stdin), F_SETOWN, getpid()) == -1)
583ea022d16SRodney W. Grimes 		syslog(LOG_ERR, "fcntl F_SETOWN: %m");
584ea022d16SRodney W. Grimes #endif
5854dd8b5abSYoshinobu Inoue 	dolog((struct sockaddr *)&his_addr);
586ea022d16SRodney W. Grimes 	/*
587ea022d16SRodney W. Grimes 	 * Set up default state
588ea022d16SRodney W. Grimes 	 */
589ea022d16SRodney W. Grimes 	data = -1;
590ea022d16SRodney W. Grimes 	type = TYPE_A;
591ea022d16SRodney W. Grimes 	form = FORM_N;
592ea022d16SRodney W. Grimes 	stru = STRU_F;
593ea022d16SRodney W. Grimes 	mode = MODE_S;
594ea022d16SRodney W. Grimes 	tmpline[0] = '\0';
595ea022d16SRodney W. Grimes 
596ea022d16SRodney W. Grimes 	/* If logins are disabled, print out the message. */
597ea022d16SRodney W. Grimes 	if ((fd = fopen(_PATH_NOLOGIN,"r")) != NULL) {
598ea022d16SRodney W. Grimes 		while (fgets(line, sizeof(line), fd) != NULL) {
599ea022d16SRodney W. Grimes 			if ((cp = strchr(line, '\n')) != NULL)
600ea022d16SRodney W. Grimes 				*cp = '\0';
601ea022d16SRodney W. Grimes 			lreply(530, "%s", line);
602ea022d16SRodney W. Grimes 		}
603ea022d16SRodney W. Grimes 		(void) fflush(stdout);
604ea022d16SRodney W. Grimes 		(void) fclose(fd);
605ea022d16SRodney W. Grimes 		reply(530, "System not available.");
606ea022d16SRodney W. Grimes 		exit(0);
607ea022d16SRodney W. Grimes 	}
608ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
60963047c6fSDavid E. O'Brien 	fd = fopen(thishost->welcome, "r");
610ea4e54b9SDavid Nugent #else
61163047c6fSDavid E. O'Brien 	fd = fopen(_PATH_FTPWELCOME, "r");
612ea4e54b9SDavid Nugent #endif
61363047c6fSDavid E. O'Brien 	if (fd != NULL) {
614ea022d16SRodney W. Grimes 		while (fgets(line, sizeof(line), fd) != NULL) {
615ea022d16SRodney W. Grimes 			if ((cp = strchr(line, '\n')) != NULL)
616ea022d16SRodney W. Grimes 				*cp = '\0';
617ea022d16SRodney W. Grimes 			lreply(220, "%s", line);
618ea022d16SRodney W. Grimes 		}
619ea022d16SRodney W. Grimes 		(void) fflush(stdout);
620ea022d16SRodney W. Grimes 		(void) fclose(fd);
621ea022d16SRodney W. Grimes 		/* reply(220,) must follow */
622ea022d16SRodney W. Grimes 	}
623ea4e54b9SDavid Nugent #ifndef VIRTUAL_HOSTING
6240512556aSDavid Nugent 	if ((hostname = malloc(MAXHOSTNAMELEN)) == NULL)
625618b0bbaSMark Murray 		fatalerror("Ran out of memory.");
6269e9a43bdSBrian Somers 	(void) gethostname(hostname, MAXHOSTNAMELEN - 1);
6279e9a43bdSBrian Somers 	hostname[MAXHOSTNAMELEN - 1] = '\0';
628ea4e54b9SDavid Nugent #endif
629c152df28SYaroslav Tykhiy 	if (hostinfo)
630ea022d16SRodney W. Grimes 		reply(220, "%s FTP server (%s) ready.", hostname, version);
631c152df28SYaroslav Tykhiy 	else
632c152df28SYaroslav Tykhiy 		reply(220, "FTP server ready.");
633ea022d16SRodney W. Grimes 	for (;;)
634ea022d16SRodney W. Grimes 		(void) yyparse();
635ea022d16SRodney W. Grimes 	/* NOTREACHED */
636ea022d16SRodney W. Grimes }
637ea022d16SRodney W. Grimes 
638ea022d16SRodney W. Grimes static void
639e4bc453cSWarner Losh lostconn(int signo)
640ea022d16SRodney W. Grimes {
641ea022d16SRodney W. Grimes 
642618b0bbaSMark Murray 	if (ftpdebug)
643ea022d16SRodney W. Grimes 		syslog(LOG_DEBUG, "lost connection");
644e02897faSPhilippe Charnier 	dologout(1);
645ea022d16SRodney W. Grimes }
646ea022d16SRodney W. Grimes 
6474b82fc95SYaroslav Tykhiy static void
648e4bc453cSWarner Losh sigquit(int signo)
6494b82fc95SYaroslav Tykhiy {
6504b82fc95SYaroslav Tykhiy 
6514b82fc95SYaroslav Tykhiy 	syslog(LOG_ERR, "got signal %d", signo);
6524b82fc95SYaroslav Tykhiy 	dologout(1);
6534b82fc95SYaroslav Tykhiy }
6544b82fc95SYaroslav Tykhiy 
655ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
656ea4e54b9SDavid Nugent /*
657ea4e54b9SDavid Nugent  * read in virtual host tables (if they exist)
658ea4e54b9SDavid Nugent  */
659ea4e54b9SDavid Nugent 
660ea4e54b9SDavid Nugent static void
661e4bc453cSWarner Losh inithosts(void)
662ea4e54b9SDavid Nugent {
66355b54aa7SYaroslav Tykhiy 	int insert;
664737d08f3SYaroslav Tykhiy 	size_t len;
665ea4e54b9SDavid Nugent 	FILE *fp;
666737d08f3SYaroslav Tykhiy 	char *cp, *mp, *line;
667737d08f3SYaroslav Tykhiy 	char *hostname;
66855b54aa7SYaroslav Tykhiy 	char *vhost, *anonuser, *statfile, *welcome, *loginmsg;
669ea4e54b9SDavid Nugent 	struct ftphost *hrp, *lhrp;
6704dd8b5abSYoshinobu Inoue 	struct addrinfo hints, *res, *ai;
671ea4e54b9SDavid Nugent 
672ea4e54b9SDavid Nugent 	/*
673ea4e54b9SDavid Nugent 	 * Fill in the default host information
674ea4e54b9SDavid Nugent 	 */
675737d08f3SYaroslav Tykhiy 	if ((hostname = malloc(MAXHOSTNAMELEN)) == NULL)
676618b0bbaSMark Murray 		fatalerror("Ran out of memory.");
677737d08f3SYaroslav Tykhiy 	if (gethostname(hostname, MAXHOSTNAMELEN) < 0)
678737d08f3SYaroslav Tykhiy 		hostname[0] = '\0';
679737d08f3SYaroslav Tykhiy 	hostname[MAXHOSTNAMELEN - 1] = '\0';
680737d08f3SYaroslav Tykhiy 	if ((hrp = malloc(sizeof(struct ftphost))) == NULL)
681737d08f3SYaroslav Tykhiy 		fatalerror("Ran out of memory.");
682737d08f3SYaroslav Tykhiy 	hrp->hostname = hostname;
683f38c6cadSYoshinobu Inoue 	hrp->hostinfo = NULL;
6844dd8b5abSYoshinobu Inoue 
6854dd8b5abSYoshinobu Inoue 	memset(&hints, 0, sizeof(hints));
6864dd8b5abSYoshinobu Inoue 	hints.ai_flags = AI_CANONNAME;
6874dd8b5abSYoshinobu Inoue 	hints.ai_family = AF_UNSPEC;
688b1d8d5cdSYaroslav Tykhiy 	if (getaddrinfo(hrp->hostname, NULL, &hints, &res) == 0)
689f38c6cadSYoshinobu Inoue 		hrp->hostinfo = res;
690ea4e54b9SDavid Nugent 	hrp->statfile = _PATH_FTPDSTATFILE;
691ea4e54b9SDavid Nugent 	hrp->welcome  = _PATH_FTPWELCOME;
692ea4e54b9SDavid Nugent 	hrp->loginmsg = _PATH_FTPLOGINMESG;
693ea4e54b9SDavid Nugent 	hrp->anonuser = "ftp";
694ea4e54b9SDavid Nugent 	hrp->next = NULL;
695ea4e54b9SDavid Nugent 	thishost = firsthost = lhrp = hrp;
696ea4e54b9SDavid Nugent 	if ((fp = fopen(_PATH_FTPHOSTS, "r")) != NULL) {
697ec009cf0SYaroslav Tykhiy 		int addrsize, gothost;
6984dd8b5abSYoshinobu Inoue 		void *addr;
6994dd8b5abSYoshinobu Inoue 		struct hostent *hp;
7004dd8b5abSYoshinobu Inoue 
701737d08f3SYaroslav Tykhiy 		while ((line = fgetln(fp, &len)) != NULL) {
7024dd8b5abSYoshinobu Inoue 			int	i, hp_error;
703ea4e54b9SDavid Nugent 
704737d08f3SYaroslav Tykhiy 			/* skip comments */
705737d08f3SYaroslav Tykhiy 			if (line[0] == '#')
706ea4e54b9SDavid Nugent 				continue;
707737d08f3SYaroslav Tykhiy 			if (line[len - 1] == '\n') {
708737d08f3SYaroslav Tykhiy 				line[len - 1] = '\0';
709737d08f3SYaroslav Tykhiy 				mp = NULL;
710737d08f3SYaroslav Tykhiy 			} else {
711737d08f3SYaroslav Tykhiy 				if ((mp = malloc(len + 1)) == NULL)
712737d08f3SYaroslav Tykhiy 					fatalerror("Ran out of memory.");
713737d08f3SYaroslav Tykhiy 				memcpy(mp, line, len);
714737d08f3SYaroslav Tykhiy 				mp[len] = '\0';
715737d08f3SYaroslav Tykhiy 				line = mp;
716ea4e54b9SDavid Nugent 			}
717ea4e54b9SDavid Nugent 			cp = strtok(line, " \t");
718737d08f3SYaroslav Tykhiy 			/* skip empty lines */
719737d08f3SYaroslav Tykhiy 			if (cp == NULL)
720737d08f3SYaroslav Tykhiy 				goto nextline;
72155b54aa7SYaroslav Tykhiy 			vhost = cp;
72255b54aa7SYaroslav Tykhiy 
72355b54aa7SYaroslav Tykhiy 			/* set defaults */
72455b54aa7SYaroslav Tykhiy 			anonuser = "ftp";
72555b54aa7SYaroslav Tykhiy 			statfile = _PATH_FTPDSTATFILE;
72655b54aa7SYaroslav Tykhiy 			welcome  = _PATH_FTPWELCOME;
72755b54aa7SYaroslav Tykhiy 			loginmsg = _PATH_FTPLOGINMESG;
72855b54aa7SYaroslav Tykhiy 
72955b54aa7SYaroslav Tykhiy 			/*
73055b54aa7SYaroslav Tykhiy 			 * Preparse the line so we can use its info
73155b54aa7SYaroslav Tykhiy 			 * for all the addresses associated with
73255b54aa7SYaroslav Tykhiy 			 * the virtual host name.
73355b54aa7SYaroslav Tykhiy 			 * Field 0, the virtual host name, is special:
73455b54aa7SYaroslav Tykhiy 			 * it's already parsed off and will be strdup'ed
73555b54aa7SYaroslav Tykhiy 			 * later, after we know its canonical form.
73655b54aa7SYaroslav Tykhiy 			 */
73755b54aa7SYaroslav Tykhiy 			for (i = 1; i < 5 && (cp = strtok(NULL, " \t")); i++)
73855b54aa7SYaroslav Tykhiy 				if (*cp != '-' && (cp = strdup(cp)))
73955b54aa7SYaroslav Tykhiy 					switch (i) {
74055b54aa7SYaroslav Tykhiy 					case 1:	/* anon user permissions */
74155b54aa7SYaroslav Tykhiy 						anonuser = cp;
74255b54aa7SYaroslav Tykhiy 						break;
74355b54aa7SYaroslav Tykhiy 					case 2: /* statistics file */
74455b54aa7SYaroslav Tykhiy 						statfile = cp;
74555b54aa7SYaroslav Tykhiy 						break;
74655b54aa7SYaroslav Tykhiy 					case 3: /* welcome message */
74755b54aa7SYaroslav Tykhiy 						welcome  = cp;
74855b54aa7SYaroslav Tykhiy 						break;
74955b54aa7SYaroslav Tykhiy 					case 4: /* login message */
75055b54aa7SYaroslav Tykhiy 						loginmsg = cp;
75155b54aa7SYaroslav Tykhiy 						break;
75255b54aa7SYaroslav Tykhiy 					default: /* programming error */
75355b54aa7SYaroslav Tykhiy 						abort();
75455b54aa7SYaroslav Tykhiy 						/* NOTREACHED */
75555b54aa7SYaroslav Tykhiy 					}
7564dd8b5abSYoshinobu Inoue 
7574dd8b5abSYoshinobu Inoue 			hints.ai_flags = 0;
7584dd8b5abSYoshinobu Inoue 			hints.ai_family = AF_UNSPEC;
7594dd8b5abSYoshinobu Inoue 			hints.ai_flags = AI_PASSIVE;
760b1d8d5cdSYaroslav Tykhiy 			if (getaddrinfo(vhost, NULL, &hints, &res) != 0)
761737d08f3SYaroslav Tykhiy 				goto nextline;
7624dd8b5abSYoshinobu Inoue 			for (ai = res; ai != NULL && ai->ai_addr != NULL;
763b535a9bfSDavid Nugent 			     ai = ai->ai_next) {
7644dd8b5abSYoshinobu Inoue 
765b535a9bfSDavid Nugent 			gothost = 0;
766ea4e54b9SDavid Nugent 			for (hrp = firsthost; hrp != NULL; hrp = hrp->next) {
767f38c6cadSYoshinobu Inoue 				struct addrinfo *hi;
768f38c6cadSYoshinobu Inoue 
769f38c6cadSYoshinobu Inoue 				for (hi = hrp->hostinfo; hi != NULL;
770f38c6cadSYoshinobu Inoue 				     hi = hi->ai_next)
771f38c6cadSYoshinobu Inoue 					if (hi->ai_addrlen == ai->ai_addrlen &&
772f38c6cadSYoshinobu Inoue 					    memcmp(hi->ai_addr,
7734dd8b5abSYoshinobu Inoue 						   ai->ai_addr,
774b535a9bfSDavid Nugent 						   ai->ai_addr->sa_len) == 0) {
775b535a9bfSDavid Nugent 						gothost++;
776b535a9bfSDavid Nugent 						break;
777b535a9bfSDavid Nugent 					}
778b535a9bfSDavid Nugent 				if (gothost)
779ea4e54b9SDavid Nugent 					break;
780ea4e54b9SDavid Nugent 			}
781ea4e54b9SDavid Nugent 			if (hrp == NULL) {
782ea4e54b9SDavid Nugent 				if ((hrp = malloc(sizeof(struct ftphost))) == NULL)
783737d08f3SYaroslav Tykhiy 					goto nextline;
784b1d8d5cdSYaroslav Tykhiy 				hrp->hostname = NULL;
78555b54aa7SYaroslav Tykhiy 				insert = 1;
786f2fe752dSYaroslav Tykhiy 			} else {
7871f75c13eSYaroslav Tykhiy 				if (hrp->hostinfo && hrp->hostinfo != res)
788b1d8d5cdSYaroslav Tykhiy 					freeaddrinfo(hrp->hostinfo);
789f2fe752dSYaroslav Tykhiy 				insert = 0; /* host already in the chain */
790f2fe752dSYaroslav Tykhiy 			}
791f38c6cadSYoshinobu Inoue 			hrp->hostinfo = res;
792f38c6cadSYoshinobu Inoue 
793ea4e54b9SDavid Nugent 			/*
794ea4e54b9SDavid Nugent 			 * determine hostname to use.
7954dd8b5abSYoshinobu Inoue 			 * force defined name if there is a valid alias
796ea4e54b9SDavid Nugent 			 * otherwise fallback to primary hostname
797ea4e54b9SDavid Nugent 			 */
7984dd8b5abSYoshinobu Inoue 			/* XXX: getaddrinfo() can't do alias check */
799f38c6cadSYoshinobu Inoue 			switch(hrp->hostinfo->ai_family) {
8004dd8b5abSYoshinobu Inoue 			case AF_INET:
8014b4cc4c6SYaroslav Tykhiy 				addr = &((struct sockaddr_in *)hrp->hostinfo->ai_addr)->sin_addr;
8024b4cc4c6SYaroslav Tykhiy 				addrsize = sizeof(struct in_addr);
8034dd8b5abSYoshinobu Inoue 				break;
8044dd8b5abSYoshinobu Inoue 			case AF_INET6:
8054b4cc4c6SYaroslav Tykhiy 				addr = &((struct sockaddr_in6 *)hrp->hostinfo->ai_addr)->sin6_addr;
8064b4cc4c6SYaroslav Tykhiy 				addrsize = sizeof(struct in6_addr);
8074dd8b5abSYoshinobu Inoue 				break;
8084dd8b5abSYoshinobu Inoue 			default:
8094dd8b5abSYoshinobu Inoue 				/* should not reach here */
810f38c6cadSYoshinobu Inoue 				freeaddrinfo(hrp->hostinfo);
811f2fe752dSYaroslav Tykhiy 				if (insert)
812f2fe752dSYaroslav Tykhiy 					free(hrp); /*not in chain, can free*/
813f2fe752dSYaroslav Tykhiy 				else
814f2fe752dSYaroslav Tykhiy 					hrp->hostinfo = NULL; /*mark as blank*/
815737d08f3SYaroslav Tykhiy 				goto nextline;
8164dd8b5abSYoshinobu Inoue 				/* NOTREACHED */
8174dd8b5abSYoshinobu Inoue 			}
81857d4ef07SYaroslav Tykhiy 			if ((hp = getipnodebyaddr(addr, addrsize,
819f38c6cadSYoshinobu Inoue 						  hrp->hostinfo->ai_family,
8204dd8b5abSYoshinobu Inoue 						  &hp_error)) != NULL) {
82155b54aa7SYaroslav Tykhiy 				if (strcmp(vhost, hp->h_name) != 0) {
822ea4e54b9SDavid Nugent 					if (hp->h_aliases == NULL)
82355b54aa7SYaroslav Tykhiy 						vhost = hp->h_name;
824ea4e54b9SDavid Nugent 					else {
825ea4e54b9SDavid Nugent 						i = 0;
826ea4e54b9SDavid Nugent 						while (hp->h_aliases[i] &&
82755b54aa7SYaroslav Tykhiy 						       strcmp(vhost, hp->h_aliases[i]) != 0)
828ea4e54b9SDavid Nugent 							++i;
829ea4e54b9SDavid Nugent 						if (hp->h_aliases[i] == NULL)
83055b54aa7SYaroslav Tykhiy 							vhost = hp->h_name;
831ea4e54b9SDavid Nugent 					}
832ea4e54b9SDavid Nugent 				}
833ea4e54b9SDavid Nugent 			}
834b1d8d5cdSYaroslav Tykhiy 			if (hrp->hostname &&
835b1d8d5cdSYaroslav Tykhiy 			    strcmp(hrp->hostname, vhost) != 0) {
836b1d8d5cdSYaroslav Tykhiy 				free(hrp->hostname);
837b1d8d5cdSYaroslav Tykhiy 				hrp->hostname = NULL;
838b1d8d5cdSYaroslav Tykhiy 			}
839b1d8d5cdSYaroslav Tykhiy 			if (hrp->hostname == NULL &&
840f2fe752dSYaroslav Tykhiy 			    (hrp->hostname = strdup(vhost)) == NULL) {
841f2fe752dSYaroslav Tykhiy 				freeaddrinfo(hrp->hostinfo);
842f2fe752dSYaroslav Tykhiy 				hrp->hostinfo = NULL; /* mark as blank */
843f2fe752dSYaroslav Tykhiy 				if (hp)
844f2fe752dSYaroslav Tykhiy 					freehostent(hp);
84555b54aa7SYaroslav Tykhiy 				goto nextline;
846f2fe752dSYaroslav Tykhiy 			}
84755b54aa7SYaroslav Tykhiy 			hrp->anonuser = anonuser;
84855b54aa7SYaroslav Tykhiy 			hrp->statfile = statfile;
84955b54aa7SYaroslav Tykhiy 			hrp->welcome  = welcome;
85055b54aa7SYaroslav Tykhiy 			hrp->loginmsg = loginmsg;
85155b54aa7SYaroslav Tykhiy 			if (insert) {
85255b54aa7SYaroslav Tykhiy 				hrp->next  = NULL;
85355b54aa7SYaroslav Tykhiy 				lhrp->next = hrp;
85455b54aa7SYaroslav Tykhiy 				lhrp = hrp;
85555b54aa7SYaroslav Tykhiy 			}
856233c0f66SYaroslav Tykhiy 			if (hp)
8574dd8b5abSYoshinobu Inoue 				freehostent(hp);
8584dd8b5abSYoshinobu Inoue 		      }
859737d08f3SYaroslav Tykhiy nextline:
860737d08f3SYaroslav Tykhiy 			if (mp)
861737d08f3SYaroslav Tykhiy 				free(mp);
862ea4e54b9SDavid Nugent 		}
863ea4e54b9SDavid Nugent 		(void) fclose(fp);
864ea4e54b9SDavid Nugent 	}
865ea4e54b9SDavid Nugent }
866ea4e54b9SDavid Nugent 
867ea4e54b9SDavid Nugent static void
868e4bc453cSWarner Losh selecthost(union sockunion *su)
869ea4e54b9SDavid Nugent {
870ea4e54b9SDavid Nugent 	struct ftphost	*hrp;
8714dd8b5abSYoshinobu Inoue 	u_int16_t port;
8724dd8b5abSYoshinobu Inoue #ifdef INET6
8734dd8b5abSYoshinobu Inoue 	struct in6_addr *mapped_in6 = NULL;
8744dd8b5abSYoshinobu Inoue #endif
875f38c6cadSYoshinobu Inoue 	struct addrinfo *hi;
8764dd8b5abSYoshinobu Inoue 
8774dd8b5abSYoshinobu Inoue #ifdef INET6
8784dd8b5abSYoshinobu Inoue 	/*
8794dd8b5abSYoshinobu Inoue 	 * XXX IPv4 mapped IPv6 addr consideraton,
8804dd8b5abSYoshinobu Inoue 	 * specified in rfc2373.
8814dd8b5abSYoshinobu Inoue 	 */
8824dd8b5abSYoshinobu Inoue 	if (su->su_family == AF_INET6 &&
8834dd8b5abSYoshinobu Inoue 	    IN6_IS_ADDR_V4MAPPED(&su->su_sin6.sin6_addr))
8844dd8b5abSYoshinobu Inoue 		mapped_in6 = &su->su_sin6.sin6_addr;
8854dd8b5abSYoshinobu Inoue #endif
886ea4e54b9SDavid Nugent 
887ea4e54b9SDavid Nugent 	hrp = thishost = firsthost;	/* default */
8884dd8b5abSYoshinobu Inoue 	port = su->su_port;
8894dd8b5abSYoshinobu Inoue 	su->su_port = 0;
890ea4e54b9SDavid Nugent 	while (hrp != NULL) {
891b535a9bfSDavid Nugent 	    for (hi = hrp->hostinfo; hi != NULL; hi = hi->ai_next) {
892f38c6cadSYoshinobu Inoue 		if (memcmp(su, hi->ai_addr, hi->ai_addrlen) == 0) {
893ea4e54b9SDavid Nugent 			thishost = hrp;
894ea4e54b9SDavid Nugent 			break;
895ea4e54b9SDavid Nugent 		}
8964dd8b5abSYoshinobu Inoue #ifdef INET6
8974dd8b5abSYoshinobu Inoue 		/* XXX IPv4 mapped IPv6 addr consideraton */
898f38c6cadSYoshinobu Inoue 		if (hi->ai_addr->sa_family == AF_INET && mapped_in6 != NULL &&
8994dd8b5abSYoshinobu Inoue 		    (memcmp(&mapped_in6->s6_addr[12],
900f38c6cadSYoshinobu Inoue 			    &((struct sockaddr_in *)hi->ai_addr)->sin_addr,
9014dd8b5abSYoshinobu Inoue 			    sizeof(struct in_addr)) == 0)) {
9024dd8b5abSYoshinobu Inoue 			thishost = hrp;
9034dd8b5abSYoshinobu Inoue 			break;
9044dd8b5abSYoshinobu Inoue 		}
9054dd8b5abSYoshinobu Inoue #endif
906f38c6cadSYoshinobu Inoue 	    }
907ea4e54b9SDavid Nugent 	    hrp = hrp->next;
908ea4e54b9SDavid Nugent 	}
9094dd8b5abSYoshinobu Inoue 	su->su_port = port;
910ea4e54b9SDavid Nugent 	/* setup static variables as appropriate */
911ea4e54b9SDavid Nugent 	hostname = thishost->hostname;
912ea4e54b9SDavid Nugent 	ftpuser = thishost->anonuser;
913ea4e54b9SDavid Nugent }
914ea4e54b9SDavid Nugent #endif
915ea4e54b9SDavid Nugent 
916ea022d16SRodney W. Grimes /*
917ea022d16SRodney W. Grimes  * Helper function for sgetpwnam().
918ea022d16SRodney W. Grimes  */
919ea022d16SRodney W. Grimes static char *
920e4bc453cSWarner Losh sgetsave(char *s)
921ea022d16SRodney W. Grimes {
922e3765043SYaroslav Tykhiy 	char *new = malloc(strlen(s) + 1);
923ea022d16SRodney W. Grimes 
924ea022d16SRodney W. Grimes 	if (new == NULL) {
925ea022d16SRodney W. Grimes 		perror_reply(421, "Local resource failure: malloc");
926ea022d16SRodney W. Grimes 		dologout(1);
927ea022d16SRodney W. Grimes 		/* NOTREACHED */
928ea022d16SRodney W. Grimes 	}
929ea022d16SRodney W. Grimes 	(void) strcpy(new, s);
930ea022d16SRodney W. Grimes 	return (new);
931ea022d16SRodney W. Grimes }
932ea022d16SRodney W. Grimes 
933ea022d16SRodney W. Grimes /*
934ea022d16SRodney W. Grimes  * Save the result of a getpwnam.  Used for USER command, since
935ea022d16SRodney W. Grimes  * the data returned must not be clobbered by any other command
936ea022d16SRodney W. Grimes  * (e.g., globbing).
937c29b9b47SYaroslav Tykhiy  * NB: The data returned by sgetpwnam() will remain valid until
938c29b9b47SYaroslav Tykhiy  * the next call to this function.  Its difference from getpwnam()
939c29b9b47SYaroslav Tykhiy  * is that sgetpwnam() is known to be called from ftpd code only.
940ea022d16SRodney W. Grimes  */
941ea022d16SRodney W. Grimes static struct passwd *
942e4bc453cSWarner Losh sgetpwnam(char *name)
943ea022d16SRodney W. Grimes {
944ea022d16SRodney W. Grimes 	static struct passwd save;
945ea022d16SRodney W. Grimes 	struct passwd *p;
946ea022d16SRodney W. Grimes 
947ea022d16SRodney W. Grimes 	if ((p = getpwnam(name)) == NULL)
948ea022d16SRodney W. Grimes 		return (p);
949ea022d16SRodney W. Grimes 	if (save.pw_name) {
950ea022d16SRodney W. Grimes 		free(save.pw_name);
951ea022d16SRodney W. Grimes 		free(save.pw_passwd);
952ea022d16SRodney W. Grimes 		free(save.pw_gecos);
953ea022d16SRodney W. Grimes 		free(save.pw_dir);
954ea022d16SRodney W. Grimes 		free(save.pw_shell);
955ea022d16SRodney W. Grimes 	}
956ea022d16SRodney W. Grimes 	save = *p;
957ea022d16SRodney W. Grimes 	save.pw_name = sgetsave(p->pw_name);
958ea022d16SRodney W. Grimes 	save.pw_passwd = sgetsave(p->pw_passwd);
959ea022d16SRodney W. Grimes 	save.pw_gecos = sgetsave(p->pw_gecos);
960ea022d16SRodney W. Grimes 	save.pw_dir = sgetsave(p->pw_dir);
961ea022d16SRodney W. Grimes 	save.pw_shell = sgetsave(p->pw_shell);
962ea022d16SRodney W. Grimes 	return (&save);
963ea022d16SRodney W. Grimes }
964ea022d16SRodney W. Grimes 
965ea022d16SRodney W. Grimes static int login_attempts;	/* number of failed login attempts */
966ea022d16SRodney W. Grimes static int askpasswd;		/* had user command, ask for passwd */
96790906a46SSheldon Hearn static char curname[MAXLOGNAME];	/* current USER name */
968ea022d16SRodney W. Grimes 
969ea022d16SRodney W. Grimes /*
970ea022d16SRodney W. Grimes  * USER command.
971ea022d16SRodney W. Grimes  * Sets global passwd pointer pw if named account exists and is acceptable;
972ea022d16SRodney W. Grimes  * sets askpasswd if a PASS command is expected.  If logged in previously,
973ea022d16SRodney W. Grimes  * need to reset state.  If name is "ftp" or "anonymous", the name is not in
974ea022d16SRodney W. Grimes  * _PATH_FTPUSERS, and ftp account exists, set guest and pw, then just return.
975ea022d16SRodney W. Grimes  * If account doesn't exist, ask for passwd anyway.  Otherwise, check user
976ea022d16SRodney W. Grimes  * requesting login privileges.  Disallow anyone who does not have a standard
977ea022d16SRodney W. Grimes  * shell as returned by getusershell().  Disallow anyone mentioned in the file
978ea022d16SRodney W. Grimes  * _PATH_FTPUSERS to allow people such as root and uucp to be avoided.
979ea022d16SRodney W. Grimes  */
980ea022d16SRodney W. Grimes void
981e4bc453cSWarner Losh user(char *name)
982ea022d16SRodney W. Grimes {
983ea022d16SRodney W. Grimes 	char *cp, *shell;
984ea022d16SRodney W. Grimes 
985ea022d16SRodney W. Grimes 	if (logged_in) {
986ea022d16SRodney W. Grimes 		if (guest) {
987ea022d16SRodney W. Grimes 			reply(530, "Can't change user from guest login.");
988ea022d16SRodney W. Grimes 			return;
989a5a4544eSPaul Traina 		} else if (dochroot) {
990a5a4544eSPaul Traina 			reply(530, "Can't change user from chroot user.");
991a5a4544eSPaul Traina 			return;
992ea022d16SRodney W. Grimes 		}
993ea022d16SRodney W. Grimes 		end_login();
994ea022d16SRodney W. Grimes 	}
995ea022d16SRodney W. Grimes 
996ea022d16SRodney W. Grimes 	guest = 0;
99763047c6fSDavid E. O'Brien #ifdef VIRTUAL_HOSTING
99863047c6fSDavid E. O'Brien 	pw = sgetpwnam(thishost->anonuser);
99963047c6fSDavid E. O'Brien #else
100063047c6fSDavid E. O'Brien 	pw = sgetpwnam("ftp");
100163047c6fSDavid E. O'Brien #endif
1002ea022d16SRodney W. Grimes 	if (strcmp(name, "ftp") == 0 || strcmp(name, "anonymous") == 0) {
10038657b576SYaroslav Tykhiy 		if (checkuser(_PATH_FTPUSERS, "ftp", 0, NULL) ||
10048657b576SYaroslav Tykhiy 		    checkuser(_PATH_FTPUSERS, "anonymous", 0, NULL))
1005ea022d16SRodney W. Grimes 			reply(530, "User %s access denied.", name);
100663047c6fSDavid E. O'Brien 		else if (pw != NULL) {
1007ea022d16SRodney W. Grimes 			guest = 1;
1008ea022d16SRodney W. Grimes 			askpasswd = 1;
1009ea022d16SRodney W. Grimes 			reply(331,
10102c60c54cSPaul Traina 			"Guest login ok, send your email address as password.");
1011ea022d16SRodney W. Grimes 		} else
1012ea022d16SRodney W. Grimes 			reply(530, "User %s unknown.", name);
1013ea022d16SRodney W. Grimes 		if (!askpasswd && logging)
1014ea022d16SRodney W. Grimes 			syslog(LOG_NOTICE,
1015ea022d16SRodney W. Grimes 			    "ANONYMOUS FTP LOGIN REFUSED FROM %s", remotehost);
1016ea022d16SRodney W. Grimes 		return;
1017ea022d16SRodney W. Grimes 	}
10185a392aecSTorsten Blum 	if (anon_only != 0) {
10195a392aecSTorsten Blum 		reply(530, "Sorry, only anonymous ftp allowed.");
10205a392aecSTorsten Blum 		return;
10215a392aecSTorsten Blum 	}
10225a392aecSTorsten Blum 
10239aca17cbSMark Murray 	if ((pw = sgetpwnam(name))) {
1024ea022d16SRodney W. Grimes 		if ((shell = pw->pw_shell) == NULL || *shell == 0)
1025ea022d16SRodney W. Grimes 			shell = _PATH_BSHELL;
1026c433c9daSPhilippe Charnier 		setusershell();
1027ea022d16SRodney W. Grimes 		while ((cp = getusershell()) != NULL)
1028ea022d16SRodney W. Grimes 			if (strcmp(cp, shell) == 0)
1029ea022d16SRodney W. Grimes 				break;
1030ea022d16SRodney W. Grimes 		endusershell();
1031ea022d16SRodney W. Grimes 
10328657b576SYaroslav Tykhiy 		if (cp == NULL || checkuser(_PATH_FTPUSERS, name, 1, NULL)) {
1033ea022d16SRodney W. Grimes 			reply(530, "User %s access denied.", name);
1034ea022d16SRodney W. Grimes 			if (logging)
1035ea022d16SRodney W. Grimes 				syslog(LOG_NOTICE,
1036ea022d16SRodney W. Grimes 				    "FTP LOGIN REFUSED FROM %s, %s",
1037ea022d16SRodney W. Grimes 				    remotehost, name);
10387e295315SYaroslav Tykhiy 			pw = NULL;
1039ea022d16SRodney W. Grimes 			return;
1040ea022d16SRodney W. Grimes 		}
1041ea022d16SRodney W. Grimes 	}
1042ea022d16SRodney W. Grimes 	if (logging)
1043ea022d16SRodney W. Grimes 		strncpy(curname, name, sizeof(curname)-1);
104447499ecdSAndrey A. Chernov 
104547499ecdSAndrey A. Chernov 	pwok = 0;
1046fa1746c9SMark Murray #ifdef USE_PAM
1047fa1746c9SMark Murray 	/* XXX Kluge! The conversation mechanism needs to be fixed. */
1048726040deSGuido van Rooij #endif
104947499ecdSAndrey A. Chernov 	if (opiechallenge(&opiedata, name, opieprompt) == 0) {
105047499ecdSAndrey A. Chernov 		pwok = (pw != NULL) &&
105147499ecdSAndrey A. Chernov 		       opieaccessfile(remotehost) &&
105247499ecdSAndrey A. Chernov 		       opiealways(pw->pw_dir);
105347499ecdSAndrey A. Chernov 		reply(331, "Response to %s %s for %s.",
105447499ecdSAndrey A. Chernov 		      opieprompt, pwok ? "requested" : "required", name);
105547499ecdSAndrey A. Chernov 	} else {
105647499ecdSAndrey A. Chernov 		pwok = 1;
105747499ecdSAndrey A. Chernov 		reply(331, "Password required for %s.", name);
105847499ecdSAndrey A. Chernov 	}
1059ea022d16SRodney W. Grimes 	askpasswd = 1;
1060ea022d16SRodney W. Grimes 	/*
1061ea022d16SRodney W. Grimes 	 * Delay before reading passwd after first failed
1062ea022d16SRodney W. Grimes 	 * attempt to slow down passwd-guessing programs.
1063ea022d16SRodney W. Grimes 	 */
1064ea022d16SRodney W. Grimes 	if (login_attempts)
1065e3765043SYaroslav Tykhiy 		sleep(login_attempts);
1066ea022d16SRodney W. Grimes }
1067ea022d16SRodney W. Grimes 
1068ea022d16SRodney W. Grimes /*
10698657b576SYaroslav Tykhiy  * Check if a user is in the file "fname",
10708657b576SYaroslav Tykhiy  * return a pointer to a malloc'd string with the rest
10718657b576SYaroslav Tykhiy  * of the matching line in "residue" if not NULL.
1072ea022d16SRodney W. Grimes  */
1073ea022d16SRodney W. Grimes static int
10748657b576SYaroslav Tykhiy checkuser(char *fname, char *name, int pwset, char **residue)
1075ea022d16SRodney W. Grimes {
1076ea022d16SRodney W. Grimes 	FILE *fd;
1077ea022d16SRodney W. Grimes 	int found = 0;
1078737d08f3SYaroslav Tykhiy 	size_t len;
1079737d08f3SYaroslav Tykhiy 	char *line, *mp, *p;
1080ea022d16SRodney W. Grimes 
1081a5a4544eSPaul Traina 	if ((fd = fopen(fname, "r")) != NULL) {
1082737d08f3SYaroslav Tykhiy 		while (!found && (line = fgetln(fd, &len)) != NULL) {
1083737d08f3SYaroslav Tykhiy 			/* skip comments */
1084ea022d16SRodney W. Grimes 			if (line[0] == '#')
1085ea022d16SRodney W. Grimes 				continue;
1086737d08f3SYaroslav Tykhiy 			if (line[len - 1] == '\n') {
1087737d08f3SYaroslav Tykhiy 				line[len - 1] = '\0';
1088737d08f3SYaroslav Tykhiy 				mp = NULL;
1089737d08f3SYaroslav Tykhiy 			} else {
1090737d08f3SYaroslav Tykhiy 				if ((mp = malloc(len + 1)) == NULL)
1091737d08f3SYaroslav Tykhiy 					fatalerror("Ran out of memory.");
1092737d08f3SYaroslav Tykhiy 				memcpy(mp, line, len);
1093737d08f3SYaroslav Tykhiy 				mp[len] = '\0';
1094737d08f3SYaroslav Tykhiy 				line = mp;
1095737d08f3SYaroslav Tykhiy 			}
1096737d08f3SYaroslav Tykhiy 			/* avoid possible leading and trailing whitespace */
1097737d08f3SYaroslav Tykhiy 			p = strtok(line, " \t");
1098737d08f3SYaroslav Tykhiy 			/* skip empty lines */
1099737d08f3SYaroslav Tykhiy 			if (p == NULL)
1100737d08f3SYaroslav Tykhiy 				goto nextline;
110131fea7b8SDavid Nugent 			/*
110231fea7b8SDavid Nugent 			 * if first chr is '@', check group membership
110331fea7b8SDavid Nugent 			 */
1104737d08f3SYaroslav Tykhiy 			if (p[0] == '@') {
110531fea7b8SDavid Nugent 				int i = 0;
110631fea7b8SDavid Nugent 				struct group *grp;
110731fea7b8SDavid Nugent 
11088657b576SYaroslav Tykhiy 				if (p[1] == '\0') /* single @ matches anyone */
11098657b576SYaroslav Tykhiy 					found = 1;
11108657b576SYaroslav Tykhiy 				else {
1111737d08f3SYaroslav Tykhiy 					if ((grp = getgrnam(p+1)) == NULL)
1112737d08f3SYaroslav Tykhiy 						goto nextline;
11137edcb936SSteve Price 					/*
11147edcb936SSteve Price 					 * Check user's default group
11157edcb936SSteve Price 					 */
11167edcb936SSteve Price 					if (pwset && grp->gr_gid == pw->pw_gid)
11177edcb936SSteve Price 						found = 1;
11187edcb936SSteve Price 					/*
11197edcb936SSteve Price 					 * Check supplementary groups
11207edcb936SSteve Price 					 */
112131fea7b8SDavid Nugent 					while (!found && grp->gr_mem[i])
112231fea7b8SDavid Nugent 						found = strcmp(name,
112331fea7b8SDavid Nugent 							grp->gr_mem[i++])
112431fea7b8SDavid Nugent 							== 0;
1125ea022d16SRodney W. Grimes 				}
11268657b576SYaroslav Tykhiy 			}
112731fea7b8SDavid Nugent 			/*
112831fea7b8SDavid Nugent 			 * Otherwise, just check for username match
112931fea7b8SDavid Nugent 			 */
113031fea7b8SDavid Nugent 			else
1131737d08f3SYaroslav Tykhiy 				found = strcmp(p, name) == 0;
11328657b576SYaroslav Tykhiy 			/*
11338657b576SYaroslav Tykhiy 			 * Save the rest of line to "residue" if matched
11348657b576SYaroslav Tykhiy 			 */
11358657b576SYaroslav Tykhiy 			if (found && residue) {
11360ba71e24SYaroslav Tykhiy 				if ((p = strtok(NULL, "")) != NULL)
11370ba71e24SYaroslav Tykhiy 					p += strspn(p, " \t");
11380ba71e24SYaroslav Tykhiy 				if (p && *p) {
11398657b576SYaroslav Tykhiy 				 	if ((*residue = strdup(p)) == NULL)
11408657b576SYaroslav Tykhiy 						fatalerror("Ran out of memory.");
11418657b576SYaroslav Tykhiy 				} else
11428657b576SYaroslav Tykhiy 					*residue = NULL;
11438657b576SYaroslav Tykhiy 			}
1144737d08f3SYaroslav Tykhiy nextline:
1145737d08f3SYaroslav Tykhiy 			if (mp)
1146737d08f3SYaroslav Tykhiy 				free(mp);
1147ea022d16SRodney W. Grimes 		}
1148ea022d16SRodney W. Grimes 		(void) fclose(fd);
1149ea022d16SRodney W. Grimes 	}
1150ea022d16SRodney W. Grimes 	return (found);
1151ea022d16SRodney W. Grimes }
1152ea022d16SRodney W. Grimes 
1153ea022d16SRodney W. Grimes /*
1154ea022d16SRodney W. Grimes  * Terminate login as previous user, if any, resetting state;
1155ea022d16SRodney W. Grimes  * used when USER command is given or login fails.
1156ea022d16SRodney W. Grimes  */
1157ea022d16SRodney W. Grimes static void
1158e4bc453cSWarner Losh end_login(void)
1159ea022d16SRodney W. Grimes {
11605bc9d93dSMark Murray #ifdef USE_PAM
11615bc9d93dSMark Murray 	int e;
11625bc9d93dSMark Murray #endif
1163ea022d16SRodney W. Grimes 
116452e7ee74SYaroslav Tykhiy 	(void) seteuid(0);
11655d7e0128SYaroslav Tykhiy 	if (logged_in && dowtmp)
116646948173SHajimu UMEMOTO 		ftpd_logwtmp(ttyline, "", NULL);
1167ea022d16SRodney W. Grimes 	pw = NULL;
1168b071c689SDavid Nugent #ifdef	LOGIN_CAP
116952e7ee74SYaroslav Tykhiy 	setusercontext(NULL, getpwuid(0), 0,
1170d9e2c424SRobert Watson 		       LOGIN_SETPRIORITY|LOGIN_SETRESOURCES|LOGIN_SETUMASK|
1171d9e2c424SRobert Watson 		       LOGIN_SETMAC);
1172b071c689SDavid Nugent #endif
11735bc9d93dSMark Murray #ifdef USE_PAM
1174de45162dSYaroslav Tykhiy 	if (pamh) {
11755bc9d93dSMark Murray 		if ((e = pam_setcred(pamh, PAM_DELETE_CRED)) != PAM_SUCCESS)
11765bc9d93dSMark Murray 			syslog(LOG_ERR, "pam_setcred: %s", pam_strerror(pamh, e));
11775bc9d93dSMark Murray 		if ((e = pam_close_session(pamh,0)) != PAM_SUCCESS)
11785bc9d93dSMark Murray 			syslog(LOG_ERR, "pam_close_session: %s", pam_strerror(pamh, e));
11795bc9d93dSMark Murray 		if ((e = pam_end(pamh, e)) != PAM_SUCCESS)
11805bc9d93dSMark Murray 			syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e));
11815bc9d93dSMark Murray 		pamh = NULL;
1182de45162dSYaroslav Tykhiy 	}
11835bc9d93dSMark Murray #endif
1184ea022d16SRodney W. Grimes 	logged_in = 0;
1185ea022d16SRodney W. Grimes 	guest = 0;
1186a5a4544eSPaul Traina 	dochroot = 0;
1187ea022d16SRodney W. Grimes }
1188ea022d16SRodney W. Grimes 
11895bc9d93dSMark Murray #ifdef USE_PAM
11906c9134c0SMark Murray 
11916c9134c0SMark Murray /*
11926c9134c0SMark Murray  * the following code is stolen from imap-uw PAM authentication module and
11936c9134c0SMark Murray  * login.c
11946c9134c0SMark Murray  */
11956c9134c0SMark Murray #define COPY_STRING(s) (s ? strdup(s) : NULL)
11966c9134c0SMark Murray 
11976c9134c0SMark Murray struct cred_t {
11986c9134c0SMark Murray 	const char *uname;		/* user name */
11996c9134c0SMark Murray 	const char *pass;		/* password */
12006c9134c0SMark Murray };
12016c9134c0SMark Murray typedef struct cred_t cred_t;
12026c9134c0SMark Murray 
12036c9134c0SMark Murray static int
12046c9134c0SMark Murray auth_conv(int num_msg, const struct pam_message **msg,
12056c9134c0SMark Murray 	  struct pam_response **resp, void *appdata)
12066c9134c0SMark Murray {
12076c9134c0SMark Murray 	int i;
12086c9134c0SMark Murray 	cred_t *cred = (cred_t *) appdata;
120960769b19SDag-Erling Smørgrav 	struct pam_response *reply;
121060769b19SDag-Erling Smørgrav 
121160769b19SDag-Erling Smørgrav 	reply = calloc(num_msg, sizeof *reply);
121260769b19SDag-Erling Smørgrav 	if (reply == NULL)
121360769b19SDag-Erling Smørgrav 		return PAM_BUF_ERR;
12146c9134c0SMark Murray 
12156c9134c0SMark Murray 	for (i = 0; i < num_msg; i++) {
12166c9134c0SMark Murray 		switch (msg[i]->msg_style) {
12176c9134c0SMark Murray 		case PAM_PROMPT_ECHO_ON:	/* assume want user name */
12186c9134c0SMark Murray 			reply[i].resp_retcode = PAM_SUCCESS;
12196c9134c0SMark Murray 			reply[i].resp = COPY_STRING(cred->uname);
12206c9134c0SMark Murray 			/* PAM frees resp. */
12216c9134c0SMark Murray 			break;
12226c9134c0SMark Murray 		case PAM_PROMPT_ECHO_OFF:	/* assume want password */
12236c9134c0SMark Murray 			reply[i].resp_retcode = PAM_SUCCESS;
12246c9134c0SMark Murray 			reply[i].resp = COPY_STRING(cred->pass);
12256c9134c0SMark Murray 			/* PAM frees resp. */
12266c9134c0SMark Murray 			break;
12276c9134c0SMark Murray 		case PAM_TEXT_INFO:
12286c9134c0SMark Murray 		case PAM_ERROR_MSG:
12296c9134c0SMark Murray 			reply[i].resp_retcode = PAM_SUCCESS;
12306c9134c0SMark Murray 			reply[i].resp = NULL;
12316c9134c0SMark Murray 			break;
12326c9134c0SMark Murray 		default:			/* unknown message style */
12336c9134c0SMark Murray 			free(reply);
12346c9134c0SMark Murray 			return PAM_CONV_ERR;
12356c9134c0SMark Murray 		}
12366c9134c0SMark Murray 	}
12376c9134c0SMark Murray 
12386c9134c0SMark Murray 	*resp = reply;
12396c9134c0SMark Murray 	return PAM_SUCCESS;
12406c9134c0SMark Murray }
12416c9134c0SMark Murray 
12426c9134c0SMark Murray /*
12436c9134c0SMark Murray  * Attempt to authenticate the user using PAM.  Returns 0 if the user is
12446c9134c0SMark Murray  * authenticated, or 1 if not authenticated.  If some sort of PAM system
12456c9134c0SMark Murray  * error occurs (e.g., the "/etc/pam.conf" file is missing) then this
12466c9134c0SMark Murray  * function returns -1.  This can be used as an indication that we should
12476c9134c0SMark Murray  * fall back to a different authentication mechanism.
12486c9134c0SMark Murray  */
12496c9134c0SMark Murray static int
12506c9134c0SMark Murray auth_pam(struct passwd **ppw, const char *pass)
12516c9134c0SMark Murray {
12526c9134c0SMark Murray 	const char *tmpl_user;
12536c9134c0SMark Murray 	const void *item;
12546c9134c0SMark Murray 	int rval;
12556c9134c0SMark Murray 	int e;
12566c9134c0SMark Murray 	cred_t auth_cred = { (*ppw)->pw_name, pass };
12576c9134c0SMark Murray 	struct pam_conv conv = { &auth_conv, &auth_cred };
12586c9134c0SMark Murray 
12596c9134c0SMark Murray 	e = pam_start("ftpd", (*ppw)->pw_name, &conv, &pamh);
12606c9134c0SMark Murray 	if (e != PAM_SUCCESS) {
1261545ea864SYaroslav Tykhiy 		/*
1262545ea864SYaroslav Tykhiy 		 * In OpenPAM, it's OK to pass NULL to pam_strerror()
1263545ea864SYaroslav Tykhiy 		 * if context creation has failed in the first place.
1264545ea864SYaroslav Tykhiy 		 */
1265545ea864SYaroslav Tykhiy 		syslog(LOG_ERR, "pam_start: %s", pam_strerror(NULL, e));
12666c9134c0SMark Murray 		return -1;
12676c9134c0SMark Murray 	}
12686c9134c0SMark Murray 
1269ea413ab7SGuido van Rooij 	e = pam_set_item(pamh, PAM_RHOST, remotehost);
1270ea413ab7SGuido van Rooij 	if (e != PAM_SUCCESS) {
1271ea413ab7SGuido van Rooij 		syslog(LOG_ERR, "pam_set_item(PAM_RHOST): %s",
1272ea413ab7SGuido van Rooij 			pam_strerror(pamh, e));
1273de45162dSYaroslav Tykhiy 		if ((e = pam_end(pamh, e)) != PAM_SUCCESS) {
1274de45162dSYaroslav Tykhiy 			syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e));
1275de45162dSYaroslav Tykhiy 		}
1276de45162dSYaroslav Tykhiy 		pamh = NULL;
1277ea413ab7SGuido van Rooij 		return -1;
1278ea413ab7SGuido van Rooij 	}
1279ea413ab7SGuido van Rooij 
12806c9134c0SMark Murray 	e = pam_authenticate(pamh, 0);
12816c9134c0SMark Murray 	switch (e) {
12826c9134c0SMark Murray 	case PAM_SUCCESS:
12836c9134c0SMark Murray 		/*
12846c9134c0SMark Murray 		 * With PAM we support the concept of a "template"
12856c9134c0SMark Murray 		 * user.  The user enters a login name which is
12866c9134c0SMark Murray 		 * authenticated by PAM, usually via a remote service
12876c9134c0SMark Murray 		 * such as RADIUS or TACACS+.  If authentication
12886c9134c0SMark Murray 		 * succeeds, a different but related "template" name
12896c9134c0SMark Murray 		 * is used for setting the credentials, shell, and
12906c9134c0SMark Murray 		 * home directory.  The name the user enters need only
12916c9134c0SMark Murray 		 * exist on the remote authentication server, but the
12926c9134c0SMark Murray 		 * template name must be present in the local password
12936c9134c0SMark Murray 		 * database.
12946c9134c0SMark Murray 		 *
12956c9134c0SMark Murray 		 * This is supported by two various mechanisms in the
12966c9134c0SMark Murray 		 * individual modules.  However, from the application's
12976c9134c0SMark Murray 		 * point of view, the template user is always passed
12986c9134c0SMark Murray 		 * back as a changed value of the PAM_USER item.
12996c9134c0SMark Murray 		 */
13006c9134c0SMark Murray 		if ((e = pam_get_item(pamh, PAM_USER, &item)) ==
13016c9134c0SMark Murray 		    PAM_SUCCESS) {
13026c9134c0SMark Murray 			tmpl_user = (const char *) item;
13036c9134c0SMark Murray 			if (strcmp((*ppw)->pw_name, tmpl_user) != 0)
13046c9134c0SMark Murray 				*ppw = getpwnam(tmpl_user);
13056c9134c0SMark Murray 		} else
13066c9134c0SMark Murray 			syslog(LOG_ERR, "Couldn't get PAM_USER: %s",
13076c9134c0SMark Murray 			    pam_strerror(pamh, e));
13086c9134c0SMark Murray 		rval = 0;
13096c9134c0SMark Murray 		break;
13106c9134c0SMark Murray 
13116c9134c0SMark Murray 	case PAM_AUTH_ERR:
13126c9134c0SMark Murray 	case PAM_USER_UNKNOWN:
13136c9134c0SMark Murray 	case PAM_MAXTRIES:
13146c9134c0SMark Murray 		rval = 1;
13156c9134c0SMark Murray 		break;
13166c9134c0SMark Murray 
13176c9134c0SMark Murray 	default:
13185bc9d93dSMark Murray 		syslog(LOG_ERR, "pam_authenticate: %s", pam_strerror(pamh, e));
13196c9134c0SMark Murray 		rval = -1;
13206c9134c0SMark Murray 		break;
13216c9134c0SMark Murray 	}
13226c9134c0SMark Murray 
13235bc9d93dSMark Murray 	if (rval == 0) {
13245bc9d93dSMark Murray 		e = pam_acct_mgmt(pamh, 0);
13255bc9d93dSMark Murray 		if (e == PAM_NEW_AUTHTOK_REQD) {
13265bc9d93dSMark Murray 			e = pam_chauthtok(pamh, PAM_CHANGE_EXPIRED_AUTHTOK);
13275bc9d93dSMark Murray 			if (e != PAM_SUCCESS) {
13285bc9d93dSMark Murray 				syslog(LOG_ERR, "pam_chauthtok: %s", pam_strerror(pamh, e));
13295bc9d93dSMark Murray 				rval = 1;
13305bc9d93dSMark Murray 			}
13315bc9d93dSMark Murray 		} else if (e != PAM_SUCCESS) {
13325bc9d93dSMark Murray 			rval = 1;
13335bc9d93dSMark Murray 		}
13345bc9d93dSMark Murray 	}
13355bc9d93dSMark Murray 
13365bc9d93dSMark Murray 	if (rval != 0) {
13376c9134c0SMark Murray 		if ((e = pam_end(pamh, e)) != PAM_SUCCESS) {
13386c9134c0SMark Murray 			syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e));
13395bc9d93dSMark Murray 		}
13405bc9d93dSMark Murray 		pamh = NULL;
13416c9134c0SMark Murray 	}
13426c9134c0SMark Murray 	return rval;
13436c9134c0SMark Murray }
13446c9134c0SMark Murray 
13455bc9d93dSMark Murray #endif /* USE_PAM */
13466c9134c0SMark Murray 
1347ea022d16SRodney W. Grimes void
1348e4bc453cSWarner Losh pass(char *passwd)
1349ea022d16SRodney W. Grimes {
1350a5a4544eSPaul Traina 	int rval;
1351ea022d16SRodney W. Grimes 	FILE *fd;
1352b071c689SDavid Nugent #ifdef	LOGIN_CAP
1353b071c689SDavid Nugent 	login_cap_t *lc = NULL;
1354b071c689SDavid Nugent #endif
13555bc9d93dSMark Murray #ifdef USE_PAM
13565bc9d93dSMark Murray 	int e;
13575bc9d93dSMark Murray #endif
1358ce9287fcSYaroslav Tykhiy 	char *chrootdir;
1359341e476eSYaroslav Tykhiy 	char *residue = NULL;
136047499ecdSAndrey A. Chernov 	char *xpasswd;
1361ea022d16SRodney W. Grimes 
1362ea022d16SRodney W. Grimes 	if (logged_in || askpasswd == 0) {
1363ea022d16SRodney W. Grimes 		reply(503, "Login with USER first.");
1364ea022d16SRodney W. Grimes 		return;
1365ea022d16SRodney W. Grimes 	}
1366ea022d16SRodney W. Grimes 	askpasswd = 0;
1367ea022d16SRodney W. Grimes 	if (!guest) {		/* "ftp" is only account allowed no password */
1368a5a4544eSPaul Traina 		if (pw == NULL) {
1369a5a4544eSPaul Traina 			rval = 1;	/* failure below */
1370a5a4544eSPaul Traina 			goto skip;
1371a5a4544eSPaul Traina 		}
13725bc9d93dSMark Murray #ifdef USE_PAM
13736c9134c0SMark Murray 		rval = auth_pam(&pw, passwd);
1374f650a124SAndrey A. Chernov 		if (rval >= 0) {
1375f650a124SAndrey A. Chernov 			opieunlock();
1376a5a4544eSPaul Traina 			goto skip;
1377f650a124SAndrey A. Chernov 		}
1378f650a124SAndrey A. Chernov #endif
137947499ecdSAndrey A. Chernov 		if (opieverify(&opiedata, passwd) == 0)
138047499ecdSAndrey A. Chernov 			xpasswd = pw->pw_passwd;
1381f650a124SAndrey A. Chernov 		else if (pwok) {
138247499ecdSAndrey A. Chernov 			xpasswd = crypt(passwd, pw->pw_passwd);
1383f650a124SAndrey A. Chernov 			if (passwd[0] == '\0' && pw->pw_passwd[0] != '\0')
1384f650a124SAndrey A. Chernov 				xpasswd = ":";
1385f650a124SAndrey A. Chernov 		} else {
138647499ecdSAndrey A. Chernov 			rval = 1;
138747499ecdSAndrey A. Chernov 			goto skip;
138847499ecdSAndrey A. Chernov 		}
138947499ecdSAndrey A. Chernov 		rval = strcmp(pw->pw_passwd, xpasswd);
1390f650a124SAndrey A. Chernov 		if (pw->pw_expire && time(NULL) >= pw->pw_expire)
1391a5a4544eSPaul Traina 			rval = 1;	/* failure */
1392a5a4544eSPaul Traina skip:
1393a5a4544eSPaul Traina 		/*
1394a5a4544eSPaul Traina 		 * If rval == 1, the user failed the authentication check
13956c9134c0SMark Murray 		 * above.  If rval == 0, either PAM or local authentication
1396a5a4544eSPaul Traina 		 * succeeded.
1397a5a4544eSPaul Traina 		 */
1398a5a4544eSPaul Traina 		if (rval) {
1399ea022d16SRodney W. Grimes 			reply(530, "Login incorrect.");
1400b8939f6fSYaroslav Tykhiy 			if (logging) {
1401ea022d16SRodney W. Grimes 				syslog(LOG_NOTICE,
1402b8939f6fSYaroslav Tykhiy 				    "FTP LOGIN FAILED FROM %s",
1403b8939f6fSYaroslav Tykhiy 				    remotehost);
1404b8939f6fSYaroslav Tykhiy 				syslog(LOG_AUTHPRIV | LOG_NOTICE,
1405ea022d16SRodney W. Grimes 				    "FTP LOGIN FAILED FROM %s, %s",
1406ea022d16SRodney W. Grimes 				    remotehost, curname);
1407b8939f6fSYaroslav Tykhiy 			}
1408ea022d16SRodney W. Grimes 			pw = NULL;
1409ea022d16SRodney W. Grimes 			if (login_attempts++ >= 5) {
1410ea022d16SRodney W. Grimes 				syslog(LOG_NOTICE,
1411ea022d16SRodney W. Grimes 				    "repeated login failures from %s",
1412ea022d16SRodney W. Grimes 				    remotehost);
1413ea022d16SRodney W. Grimes 				exit(0);
1414ea022d16SRodney W. Grimes 			}
1415ea022d16SRodney W. Grimes 			return;
1416ea022d16SRodney W. Grimes 		}
1417ea022d16SRodney W. Grimes 	}
1418ea022d16SRodney W. Grimes 	login_attempts = 0;		/* this time successful */
1419c4536e21SYaroslav Tykhiy 	if (setegid(pw->pw_gid) < 0) {
1420ea022d16SRodney W. Grimes 		reply(550, "Can't set gid.");
1421ea022d16SRodney W. Grimes 		return;
1422ea022d16SRodney W. Grimes 	}
1423b071c689SDavid Nugent 	/* May be overridden by login.conf */
1424b071c689SDavid Nugent 	(void) umask(defumask);
1425b071c689SDavid Nugent #ifdef	LOGIN_CAP
14265d0bfe39SDavid Nugent 	if ((lc = login_getpwclass(pw)) != NULL) {
1427b071c689SDavid Nugent 		char	remote_ip[MAXHOSTNAMELEN];
1428b071c689SDavid Nugent 
14294dd8b5abSYoshinobu Inoue 		getnameinfo((struct sockaddr *)&his_addr, his_addr.su_len,
14304dd8b5abSYoshinobu Inoue 			remote_ip, sizeof(remote_ip) - 1, NULL, 0,
1431b0f06defSHajimu UMEMOTO 			NI_NUMERICHOST);
1432b071c689SDavid Nugent 		remote_ip[sizeof(remote_ip) - 1] = 0;
1433b071c689SDavid Nugent 		if (!auth_hostok(lc, remotehost, remote_ip)) {
1434b071c689SDavid Nugent 			syslog(LOG_INFO|LOG_AUTH,
1435b071c689SDavid Nugent 			    "FTP LOGIN FAILED (HOST) as %s: permission denied.",
1436b071c689SDavid Nugent 			    pw->pw_name);
1437b071c689SDavid Nugent 			reply(530, "Permission denied.\n");
1438b071c689SDavid Nugent 			pw = NULL;
1439b071c689SDavid Nugent 			return;
1440b071c689SDavid Nugent 		}
1441b071c689SDavid Nugent 		if (!auth_timeok(lc, time(NULL))) {
1442b071c689SDavid Nugent 			reply(530, "Login not available right now.\n");
1443b071c689SDavid Nugent 			pw = NULL;
1444b071c689SDavid Nugent 			return;
1445b071c689SDavid Nugent 		}
1446b071c689SDavid Nugent 	}
144752e7ee74SYaroslav Tykhiy 	setusercontext(lc, pw, 0,
1448e6fa0d43SDag-Erling Smørgrav 		LOGIN_SETLOGIN|LOGIN_SETGROUP|LOGIN_SETPRIORITY|
1449d9e2c424SRobert Watson 		LOGIN_SETRESOURCES|LOGIN_SETUMASK|LOGIN_SETMAC);
1450b071c689SDavid Nugent #else
1451e6fa0d43SDag-Erling Smørgrav 	setlogin(pw->pw_name);
1452ea022d16SRodney W. Grimes 	(void) initgroups(pw->pw_name, pw->pw_gid);
1453b071c689SDavid Nugent #endif
1454ea022d16SRodney W. Grimes 
14555bc9d93dSMark Murray #ifdef USE_PAM
14565bc9d93dSMark Murray 	if (pamh) {
14575bc9d93dSMark Murray 		if ((e = pam_open_session(pamh, 0)) != PAM_SUCCESS) {
14585bc9d93dSMark Murray 			syslog(LOG_ERR, "pam_open_session: %s", pam_strerror(pamh, e));
14595bc9d93dSMark Murray 		} else if ((e = pam_setcred(pamh, PAM_ESTABLISH_CRED)) != PAM_SUCCESS) {
14605bc9d93dSMark Murray 			syslog(LOG_ERR, "pam_setcred: %s", pam_strerror(pamh, e));
14615bc9d93dSMark Murray 		}
14625bc9d93dSMark Murray 	}
14635bc9d93dSMark Murray #endif
14645bc9d93dSMark Murray 
1465ea022d16SRodney W. Grimes 	/* open wtmp before chroot */
14665d7e0128SYaroslav Tykhiy 	if (dowtmp)
14675d7e0128SYaroslav Tykhiy 		ftpd_logwtmp(ttyline, pw->pw_name,
14685d7e0128SYaroslav Tykhiy 		    (struct sockaddr *)&his_addr);
1469ea022d16SRodney W. Grimes 	logged_in = 1;
1470ea022d16SRodney W. Grimes 
1471a5a4544eSPaul Traina 	if (guest && stats && statfd < 0)
1472ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
147363047c6fSDavid E. O'Brien 		statfd = open(thishost->statfile, O_WRONLY|O_APPEND);
1474ea4e54b9SDavid Nugent #else
147563047c6fSDavid E. O'Brien 		statfd = open(_PATH_FTPDSTATFILE, O_WRONLY|O_APPEND);
1476ea4e54b9SDavid Nugent #endif
147763047c6fSDavid E. O'Brien 		if (statfd < 0)
14783eb568f2SGuido van Rooij 			stats = 0;
14793eb568f2SGuido van Rooij 
1480b071c689SDavid Nugent 	dochroot =
1481341e476eSYaroslav Tykhiy 		checkuser(_PATH_FTPCHROOT, pw->pw_name, 1, &residue)
1482b071c689SDavid Nugent #ifdef	LOGIN_CAP	/* Allow login.conf configuration as well */
14838657b576SYaroslav Tykhiy 		|| login_getcapbool(lc, "ftp-chroot", 0)
1484b071c689SDavid Nugent #endif
14858657b576SYaroslav Tykhiy 	;
1486ce9287fcSYaroslav Tykhiy 	chrootdir = NULL;
1487ea022d16SRodney W. Grimes 	/*
1488ce9287fcSYaroslav Tykhiy 	 * For a chrooted local user,
1489ce9287fcSYaroslav Tykhiy 	 * a) see whether ftpchroot(5) specifies a chroot directory,
1490ce9287fcSYaroslav Tykhiy 	 * b) extract the directory pathname from the line,
1491ce9287fcSYaroslav Tykhiy 	 * c) expand it to the absolute pathname if necessary.
1492ea022d16SRodney W. Grimes 	 */
1493ce9287fcSYaroslav Tykhiy 	if (dochroot && residue &&
149439bce482SYaroslav Tykhiy 	    (chrootdir = strtok(residue, " \t")) != NULL) {
149539bce482SYaroslav Tykhiy 		if (chrootdir[0] != '/')
1496341e476eSYaroslav Tykhiy 			asprintf(&chrootdir, "%s/%s", pw->pw_dir, chrootdir);
149739bce482SYaroslav Tykhiy 		else
149839bce482SYaroslav Tykhiy 			chrootdir = strdup(chrootdir); /* so it can be freed */
1499341e476eSYaroslav Tykhiy 		if (chrootdir == NULL)
15008657b576SYaroslav Tykhiy 			fatalerror("Ran out of memory.");
15018657b576SYaroslav Tykhiy 	}
1502ce9287fcSYaroslav Tykhiy 	if (guest || dochroot) {
1503ce9287fcSYaroslav Tykhiy 		/*
1504ce9287fcSYaroslav Tykhiy 		 * If no chroot directory set yet, use the login directory.
1505ce9287fcSYaroslav Tykhiy 		 * Copy it so it can be modified while pw->pw_dir stays intact.
1506ce9287fcSYaroslav Tykhiy 		 */
1507ce9287fcSYaroslav Tykhiy 		if (chrootdir == NULL &&
1508ce9287fcSYaroslav Tykhiy 		    (chrootdir = strdup(pw->pw_dir)) == NULL)
1509ce9287fcSYaroslav Tykhiy 			fatalerror("Ran out of memory.");
1510ce9287fcSYaroslav Tykhiy 		/*
1511ce9287fcSYaroslav Tykhiy 		 * Check for the "/chroot/./home" syntax,
1512ce9287fcSYaroslav Tykhiy 		 * separate the chroot and home directory pathnames.
1513ce9287fcSYaroslav Tykhiy 		 */
1514ce9287fcSYaroslav Tykhiy 		if ((homedir = strstr(chrootdir, "/./")) != NULL) {
1515ce9287fcSYaroslav Tykhiy 			*(homedir++) = '\0';	/* wipe '/' */
1516ce9287fcSYaroslav Tykhiy 			homedir++;		/* skip '.' */
1517ce9287fcSYaroslav Tykhiy 			/* so chrootdir can be freed later */
1518ce9287fcSYaroslav Tykhiy 			if ((homedir = strdup(homedir)) == NULL)
1519ce9287fcSYaroslav Tykhiy 				fatalerror("Ran out of memory.");
1520ce9287fcSYaroslav Tykhiy 		} else {
1521ce9287fcSYaroslav Tykhiy 			/*
1522ce9287fcSYaroslav Tykhiy 			 * We MUST do a chdir() after the chroot. Otherwise
1523ce9287fcSYaroslav Tykhiy 			 * the old current directory will be accessible as "."
1524ce9287fcSYaroslav Tykhiy 			 * outside the new root!
1525ce9287fcSYaroslav Tykhiy 			 */
1526ce9287fcSYaroslav Tykhiy 			homedir = "/";
1527ce9287fcSYaroslav Tykhiy 		}
1528ce9287fcSYaroslav Tykhiy 		/*
1529ce9287fcSYaroslav Tykhiy 		 * Finally, do chroot()
1530ce9287fcSYaroslav Tykhiy 		 */
1531ce9287fcSYaroslav Tykhiy 		if (chroot(chrootdir) < 0) {
1532a5a4544eSPaul Traina 			reply(550, "Can't change root.");
1533a5a4544eSPaul Traina 			goto bad;
1534a5a4544eSPaul Traina 		}
1535ce9287fcSYaroslav Tykhiy 	} else	/* real user w/o chroot */
1536ce9287fcSYaroslav Tykhiy 		homedir = pw->pw_dir;
1537ce9287fcSYaroslav Tykhiy 	/*
1538ce9287fcSYaroslav Tykhiy 	 * Set euid *before* doing chdir() so
1539ce9287fcSYaroslav Tykhiy 	 * a) the user won't be carried to a directory that he couldn't reach
1540ce9287fcSYaroslav Tykhiy 	 *    on his own due to no permission to upper path components,
1541ce9287fcSYaroslav Tykhiy 	 * b) NFS mounted homedirs w/restrictive permissions will be accessible
1542ce9287fcSYaroslav Tykhiy 	 *    (uid 0 has no root power over NFS if not mapped explicitly.)
1543ce9287fcSYaroslav Tykhiy 	 */
154452e7ee74SYaroslav Tykhiy 	if (seteuid(pw->pw_uid) < 0) {
1545ea022d16SRodney W. Grimes 		reply(550, "Can't set uid.");
1546ea022d16SRodney W. Grimes 		goto bad;
1547ea022d16SRodney W. Grimes 	}
1548ce9287fcSYaroslav Tykhiy 	if (chdir(homedir) < 0) {
1549ce9287fcSYaroslav Tykhiy 		if (guest || dochroot) {
1550ce9287fcSYaroslav Tykhiy 			reply(550, "Can't change to base directory.");
1551ce9287fcSYaroslav Tykhiy 			goto bad;
1552ce9287fcSYaroslav Tykhiy 		} else {
1553ce9287fcSYaroslav Tykhiy 			if (chdir("/") < 0) {
1554ce9287fcSYaroslav Tykhiy 				reply(550, "Root is inaccessible.");
1555ce9287fcSYaroslav Tykhiy 				goto bad;
1556ce9287fcSYaroslav Tykhiy 			}
1557ce9287fcSYaroslav Tykhiy 			lreply(230, "No directory! Logging in with home=/");
1558ce9287fcSYaroslav Tykhiy 		}
1559ce9287fcSYaroslav Tykhiy 	}
156082c76939SDavid Greenman 
156182c76939SDavid Greenman 	/*
1562ea022d16SRodney W. Grimes 	 * Display a login message, if it exists.
1563ea022d16SRodney W. Grimes 	 * N.B. reply(230,) must follow the message.
1564ea022d16SRodney W. Grimes 	 */
1565ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
156663047c6fSDavid E. O'Brien 	fd = fopen(thishost->loginmsg, "r");
1567ea4e54b9SDavid Nugent #else
156863047c6fSDavid E. O'Brien 	fd = fopen(_PATH_FTPLOGINMESG, "r");
1569ea4e54b9SDavid Nugent #endif
157063047c6fSDavid E. O'Brien 	if (fd != NULL) {
1571ea022d16SRodney W. Grimes 		char *cp, line[LINE_MAX];
1572ea022d16SRodney W. Grimes 
1573ea022d16SRodney W. Grimes 		while (fgets(line, sizeof(line), fd) != NULL) {
1574ea022d16SRodney W. Grimes 			if ((cp = strchr(line, '\n')) != NULL)
1575ea022d16SRodney W. Grimes 				*cp = '\0';
1576ea022d16SRodney W. Grimes 			lreply(230, "%s", line);
1577ea022d16SRodney W. Grimes 		}
1578ea022d16SRodney W. Grimes 		(void) fflush(stdout);
1579ea022d16SRodney W. Grimes 		(void) fclose(fd);
1580ea022d16SRodney W. Grimes 	}
1581ea022d16SRodney W. Grimes 	if (guest) {
15823eb568f2SGuido van Rooij 		if (ident != NULL)
15833eb568f2SGuido van Rooij 			free(ident);
158461f891a6SPaul Traina 		ident = strdup(passwd);
158561f891a6SPaul Traina 		if (ident == NULL)
1586618b0bbaSMark Murray 			fatalerror("Ran out of memory.");
158761f891a6SPaul Traina 
1588ea022d16SRodney W. Grimes 		reply(230, "Guest login ok, access restrictions apply.");
1589ea022d16SRodney W. Grimes #ifdef SETPROCTITLE
1590ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
1591ea4e54b9SDavid Nugent 		if (thishost != firsthost)
1592ea4e54b9SDavid Nugent 			snprintf(proctitle, sizeof(proctitle),
1593b3a0a7cdSMike Heffner 				 "%s: anonymous(%s)/%s", remotehost, hostname,
1594b3a0a7cdSMike Heffner 				 passwd);
1595ea4e54b9SDavid Nugent 		else
1596ea4e54b9SDavid Nugent #endif
1597ea022d16SRodney W. Grimes 			snprintf(proctitle, sizeof(proctitle),
1598b3a0a7cdSMike Heffner 				 "%s: anonymous/%s", remotehost, passwd);
1599b63e1fe2SPeter Wemm 		setproctitle("%s", proctitle);
1600ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */
1601ea022d16SRodney W. Grimes 		if (logging)
1602ea022d16SRodney W. Grimes 			syslog(LOG_INFO, "ANONYMOUS FTP LOGIN FROM %s, %s",
1603ea022d16SRodney W. Grimes 			    remotehost, passwd);
1604ea022d16SRodney W. Grimes 	} else {
16053401a71fSDaniel O'Callaghan 		if (dochroot)
160611342ab1SYaroslav Tykhiy 			reply(230, "User %s logged in, "
160711342ab1SYaroslav Tykhiy 				   "access restrictions apply.", pw->pw_name);
16083401a71fSDaniel O'Callaghan 		else
1609ea022d16SRodney W. Grimes 			reply(230, "User %s logged in.", pw->pw_name);
16103401a71fSDaniel O'Callaghan 
1611ea022d16SRodney W. Grimes #ifdef SETPROCTITLE
1612ea022d16SRodney W. Grimes 		snprintf(proctitle, sizeof(proctitle),
16137a29d7daSYaroslav Tykhiy 			 "%s: user/%s", remotehost, pw->pw_name);
1614b63e1fe2SPeter Wemm 		setproctitle("%s", proctitle);
1615ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */
1616ea022d16SRodney W. Grimes 		if (logging)
1617ea022d16SRodney W. Grimes 			syslog(LOG_INFO, "FTP LOGIN FROM %s as %s",
1618ea022d16SRodney W. Grimes 			    remotehost, pw->pw_name);
1619ea022d16SRodney W. Grimes 	}
1620b071c689SDavid Nugent #ifdef	LOGIN_CAP
1621b071c689SDavid Nugent 	login_close(lc);
1622b071c689SDavid Nugent #endif
1623ce9287fcSYaroslav Tykhiy 	if (chrootdir)
1624ce9287fcSYaroslav Tykhiy 		free(chrootdir);
1625ce9287fcSYaroslav Tykhiy 	if (residue)
1626ce9287fcSYaroslav Tykhiy 		free(residue);
1627ea022d16SRodney W. Grimes 	return;
1628ea022d16SRodney W. Grimes bad:
1629ea022d16SRodney W. Grimes 	/* Forget all about it... */
1630b071c689SDavid Nugent #ifdef	LOGIN_CAP
1631b071c689SDavid Nugent 	login_close(lc);
1632b071c689SDavid Nugent #endif
1633ce9287fcSYaroslav Tykhiy 	if (chrootdir)
1634ce9287fcSYaroslav Tykhiy 		free(chrootdir);
1635ce9287fcSYaroslav Tykhiy 	if (residue)
1636ce9287fcSYaroslav Tykhiy 		free(residue);
1637ea022d16SRodney W. Grimes 	end_login();
1638ea022d16SRodney W. Grimes }
1639ea022d16SRodney W. Grimes 
1640ea022d16SRodney W. Grimes void
1641e4bc453cSWarner Losh retrieve(char *cmd, char *name)
1642ea022d16SRodney W. Grimes {
1643ea022d16SRodney W. Grimes 	FILE *fin, *dout;
1644ea022d16SRodney W. Grimes 	struct stat st;
1645e4bc453cSWarner Losh 	int (*closefunc)(FILE *);
1646158a00b2SJohn Birrell 	time_t start;
1647ea022d16SRodney W. Grimes 
1648ea022d16SRodney W. Grimes 	if (cmd == 0) {
1649ea022d16SRodney W. Grimes 		fin = fopen(name, "r"), closefunc = fclose;
1650ea022d16SRodney W. Grimes 		st.st_size = 0;
1651ea022d16SRodney W. Grimes 	} else {
1652ea022d16SRodney W. Grimes 		char line[BUFSIZ];
1653ea022d16SRodney W. Grimes 
1654e760ef2cSWarner Losh 		(void) snprintf(line, sizeof(line), cmd, name), name = line;
1655ea022d16SRodney W. Grimes 		fin = ftpd_popen(line, "r"), closefunc = ftpd_pclose;
1656ea022d16SRodney W. Grimes 		st.st_size = -1;
1657ea022d16SRodney W. Grimes 		st.st_blksize = BUFSIZ;
1658ea022d16SRodney W. Grimes 	}
1659ea022d16SRodney W. Grimes 	if (fin == NULL) {
1660ea022d16SRodney W. Grimes 		if (errno != 0) {
1661ea022d16SRodney W. Grimes 			perror_reply(550, name);
1662ea022d16SRodney W. Grimes 			if (cmd == 0) {
1663ea022d16SRodney W. Grimes 				LOGCMD("get", name);
1664ea022d16SRodney W. Grimes 			}
1665ea022d16SRodney W. Grimes 		}
1666ea022d16SRodney W. Grimes 		return;
1667ea022d16SRodney W. Grimes 	}
1668ea022d16SRodney W. Grimes 	byte_count = -1;
1669ea701226SYaroslav Tykhiy 	if (cmd == 0) {
1670ea701226SYaroslav Tykhiy 		if (fstat(fileno(fin), &st) < 0) {
1671ea701226SYaroslav Tykhiy 			perror_reply(550, name);
1672ea701226SYaroslav Tykhiy 			goto done;
1673ea701226SYaroslav Tykhiy 		}
1674ea701226SYaroslav Tykhiy 		if (!S_ISREG(st.st_mode)) {
167510e89104SYaroslav Tykhiy 			/*
167610e89104SYaroslav Tykhiy 			 * Never sending a raw directory is a workaround
167710e89104SYaroslav Tykhiy 			 * for buggy clients that will attempt to RETR
167810e89104SYaroslav Tykhiy 			 * a directory before listing it, e.g., Mozilla.
167910e89104SYaroslav Tykhiy 			 * Preventing a guest from getting irregular files
168010e89104SYaroslav Tykhiy 			 * is a simple security measure.
168110e89104SYaroslav Tykhiy 			 */
168210e89104SYaroslav Tykhiy 			if (S_ISDIR(st.st_mode) || guest) {
1683ea022d16SRodney W. Grimes 				reply(550, "%s: not a plain file.", name);
1684ea022d16SRodney W. Grimes 				goto done;
1685ea022d16SRodney W. Grimes 			}
1686ea701226SYaroslav Tykhiy 			st.st_size = -1;
1687ea701226SYaroslav Tykhiy 			/* st.st_blksize is set for all descriptor types */
1688ea701226SYaroslav Tykhiy 		}
1689ea701226SYaroslav Tykhiy 	}
1690ea022d16SRodney W. Grimes 	if (restart_point) {
1691ea022d16SRodney W. Grimes 		if (type == TYPE_A) {
1692ea022d16SRodney W. Grimes 			off_t i, n;
1693ea022d16SRodney W. Grimes 			int c;
1694ea022d16SRodney W. Grimes 
1695ea022d16SRodney W. Grimes 			n = restart_point;
1696ea022d16SRodney W. Grimes 			i = 0;
1697ea022d16SRodney W. Grimes 			while (i++ < n) {
1698ea022d16SRodney W. Grimes 				if ((c=getc(fin)) == EOF) {
1699ea022d16SRodney W. Grimes 					perror_reply(550, name);
1700ea022d16SRodney W. Grimes 					goto done;
1701ea022d16SRodney W. Grimes 				}
1702ea022d16SRodney W. Grimes 				if (c == '\n')
1703ea022d16SRodney W. Grimes 					i++;
1704ea022d16SRodney W. Grimes 			}
1705ea022d16SRodney W. Grimes 		} else if (lseek(fileno(fin), restart_point, L_SET) < 0) {
1706ea022d16SRodney W. Grimes 			perror_reply(550, name);
1707ea022d16SRodney W. Grimes 			goto done;
1708ea022d16SRodney W. Grimes 		}
1709ea022d16SRodney W. Grimes 	}
1710ea022d16SRodney W. Grimes 	dout = dataconn(name, st.st_size, "w");
1711ea022d16SRodney W. Grimes 	if (dout == NULL)
1712ea022d16SRodney W. Grimes 		goto done;
17133eb568f2SGuido van Rooij 	time(&start);
17149fc5823aSGarrett Wollman 	send_data(fin, dout, st.st_blksize, st.st_size,
17159fc5823aSGarrett Wollman 		  restart_point == 0 && cmd == 0 && S_ISREG(st.st_mode));
1716c999732bSYaroslav Tykhiy 	if (cmd == 0 && guest && stats && byte_count > 0)
1717c999732bSYaroslav Tykhiy 		logxfer(name, byte_count, start);
1718ea022d16SRodney W. Grimes 	(void) fclose(dout);
1719ea022d16SRodney W. Grimes 	data = -1;
1720ea022d16SRodney W. Grimes 	pdata = -1;
1721ea022d16SRodney W. Grimes done:
1722ea022d16SRodney W. Grimes 	if (cmd == 0)
1723ea022d16SRodney W. Grimes 		LOGBYTES("get", name, byte_count);
1724ea022d16SRodney W. Grimes 	(*closefunc)(fin);
1725ea022d16SRodney W. Grimes }
1726ea022d16SRodney W. Grimes 
1727ea022d16SRodney W. Grimes void
1728e4bc453cSWarner Losh store(char *name, char *mode, int unique)
1729ea022d16SRodney W. Grimes {
1730a117c345SYaroslav Tykhiy 	int fd;
1731ea022d16SRodney W. Grimes 	FILE *fout, *din;
1732e4bc453cSWarner Losh 	int (*closefunc)(FILE *);
1733ea022d16SRodney W. Grimes 
1734a117c345SYaroslav Tykhiy 	if (*mode == 'a') {		/* APPE */
1735a117c345SYaroslav Tykhiy 		if (unique) {
1736a117c345SYaroslav Tykhiy 			/* Programming error */
1737a117c345SYaroslav Tykhiy 			syslog(LOG_ERR, "Internal: unique flag to APPE");
1738a117c345SYaroslav Tykhiy 			unique = 0;
1739a117c345SYaroslav Tykhiy 		}
1740a117c345SYaroslav Tykhiy 		if (guest && noguestmod) {
1741a117c345SYaroslav Tykhiy 			reply(550, "Appending to existing file denied");
1742a117c345SYaroslav Tykhiy 			goto err;
1743a117c345SYaroslav Tykhiy 		}
1744a117c345SYaroslav Tykhiy 		restart_point = 0;	/* not affected by preceding REST */
1745a117c345SYaroslav Tykhiy 	}
1746a117c345SYaroslav Tykhiy 	if (unique)			/* STOU overrides REST */
1747a117c345SYaroslav Tykhiy 		restart_point = 0;
1748a117c345SYaroslav Tykhiy 	if (guest && noguestmod) {
1749a117c345SYaroslav Tykhiy 		if (restart_point) {	/* guest STOR w/REST */
1750a117c345SYaroslav Tykhiy 			reply(550, "Modifying existing file denied");
1751a117c345SYaroslav Tykhiy 			goto err;
1752a117c345SYaroslav Tykhiy 		} else			/* treat guest STOR as STOU */
1753a117c345SYaroslav Tykhiy 			unique = 1;
1754ea022d16SRodney W. Grimes 	}
1755ea022d16SRodney W. Grimes 
1756ea022d16SRodney W. Grimes 	if (restart_point)
1757a117c345SYaroslav Tykhiy 		mode = "r+";	/* so ASCII manual seek can work */
1758a117c345SYaroslav Tykhiy 	if (unique) {
1759a117c345SYaroslav Tykhiy 		if ((fd = guniquefd(name, &name)) < 0)
1760a117c345SYaroslav Tykhiy 			goto err;
1761a117c345SYaroslav Tykhiy 		fout = fdopen(fd, mode);
1762a117c345SYaroslav Tykhiy 	} else
1763ea022d16SRodney W. Grimes 		fout = fopen(name, mode);
1764ea022d16SRodney W. Grimes 	closefunc = fclose;
1765ea022d16SRodney W. Grimes 	if (fout == NULL) {
1766ea022d16SRodney W. Grimes 		perror_reply(553, name);
1767a117c345SYaroslav Tykhiy 		goto err;
1768ea022d16SRodney W. Grimes 	}
1769ea022d16SRodney W. Grimes 	byte_count = -1;
1770ea022d16SRodney W. Grimes 	if (restart_point) {
1771ea022d16SRodney W. Grimes 		if (type == TYPE_A) {
1772ea022d16SRodney W. Grimes 			off_t i, n;
1773ea022d16SRodney W. Grimes 			int c;
1774ea022d16SRodney W. Grimes 
1775ea022d16SRodney W. Grimes 			n = restart_point;
1776ea022d16SRodney W. Grimes 			i = 0;
1777ea022d16SRodney W. Grimes 			while (i++ < n) {
1778ea022d16SRodney W. Grimes 				if ((c=getc(fout)) == EOF) {
1779ea022d16SRodney W. Grimes 					perror_reply(550, name);
1780ea022d16SRodney W. Grimes 					goto done;
1781ea022d16SRodney W. Grimes 				}
1782ea022d16SRodney W. Grimes 				if (c == '\n')
1783ea022d16SRodney W. Grimes 					i++;
1784ea022d16SRodney W. Grimes 			}
1785ea022d16SRodney W. Grimes 			/*
1786ea022d16SRodney W. Grimes 			 * We must do this seek to "current" position
1787ea022d16SRodney W. Grimes 			 * because we are changing from reading to
1788ea022d16SRodney W. Grimes 			 * writing.
1789ea022d16SRodney W. Grimes 			 */
17900e519c96SYaroslav Tykhiy 			if (fseeko(fout, 0, SEEK_CUR) < 0) {
1791ea022d16SRodney W. Grimes 				perror_reply(550, name);
1792ea022d16SRodney W. Grimes 				goto done;
1793ea022d16SRodney W. Grimes 			}
1794ea022d16SRodney W. Grimes 		} else if (lseek(fileno(fout), restart_point, L_SET) < 0) {
1795ea022d16SRodney W. Grimes 			perror_reply(550, name);
1796ea022d16SRodney W. Grimes 			goto done;
1797ea022d16SRodney W. Grimes 		}
1798ea022d16SRodney W. Grimes 	}
17990e519c96SYaroslav Tykhiy 	din = dataconn(name, -1, "r");
1800ea022d16SRodney W. Grimes 	if (din == NULL)
1801ea022d16SRodney W. Grimes 		goto done;
1802ea022d16SRodney W. Grimes 	if (receive_data(din, fout) == 0) {
1803ea022d16SRodney W. Grimes 		if (unique)
1804ea022d16SRodney W. Grimes 			reply(226, "Transfer complete (unique file name:%s).",
1805ea022d16SRodney W. Grimes 			    name);
1806ea022d16SRodney W. Grimes 		else
1807ea022d16SRodney W. Grimes 			reply(226, "Transfer complete.");
1808ea022d16SRodney W. Grimes 	}
1809ea022d16SRodney W. Grimes 	(void) fclose(din);
1810ea022d16SRodney W. Grimes 	data = -1;
1811ea022d16SRodney W. Grimes 	pdata = -1;
1812ea022d16SRodney W. Grimes done:
18137c20f337SYaroslav Tykhiy 	LOGBYTES(*mode == 'a' ? "append" : "put", name, byte_count);
1814ea022d16SRodney W. Grimes 	(*closefunc)(fout);
1815a117c345SYaroslav Tykhiy 	return;
1816a117c345SYaroslav Tykhiy err:
1817a117c345SYaroslav Tykhiy 	LOGCMD(*mode == 'a' ? "append" : "put" , name);
1818a117c345SYaroslav Tykhiy 	return;
1819ea022d16SRodney W. Grimes }
1820ea022d16SRodney W. Grimes 
1821ea022d16SRodney W. Grimes static FILE *
1822e4bc453cSWarner Losh getdatasock(char *mode)
1823ea022d16SRodney W. Grimes {
1824ea022d16SRodney W. Grimes 	int on = 1, s, t, tries;
1825ea022d16SRodney W. Grimes 
1826ea022d16SRodney W. Grimes 	if (data >= 0)
1827ea022d16SRodney W. Grimes 		return (fdopen(data, mode));
18284dd8b5abSYoshinobu Inoue 
18294dd8b5abSYoshinobu Inoue 	s = socket(data_dest.su_family, SOCK_STREAM, 0);
1830ea022d16SRodney W. Grimes 	if (s < 0)
1831ea022d16SRodney W. Grimes 		goto bad;
183257d4ef07SYaroslav Tykhiy 	if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
1833406d1ae9SYaroslav Tykhiy 		syslog(LOG_WARNING, "data setsockopt (SO_REUSEADDR): %m");
1834ea022d16SRodney W. Grimes 	/* anchor socket to avoid multi-homing problems */
18354dd8b5abSYoshinobu Inoue 	data_source = ctrl_addr;
183663591ba5SYaroslav Tykhiy 	data_source.su_port = htons(dataport);
183752e7ee74SYaroslav Tykhiy 	(void) seteuid(0);
1838ea022d16SRodney W. Grimes 	for (tries = 1; ; tries++) {
18399ec7612aSYaroslav Tykhiy 		/*
18409ec7612aSYaroslav Tykhiy 		 * We should loop here since it's possible that
18419ec7612aSYaroslav Tykhiy 		 * another ftpd instance has passed this point and is
18429ec7612aSYaroslav Tykhiy 		 * trying to open a data connection in active mode now.
18439ec7612aSYaroslav Tykhiy 		 * Until the other connection is opened, we'll be getting
18449ec7612aSYaroslav Tykhiy 		 * EADDRINUSE because no SOCK_STREAM sockets in the system
18459ec7612aSYaroslav Tykhiy 		 * can share both local and remote addresses, localIP:20
18469ec7612aSYaroslav Tykhiy 		 * and *:* in this case.
18479ec7612aSYaroslav Tykhiy 		 */
1848ea022d16SRodney W. Grimes 		if (bind(s, (struct sockaddr *)&data_source,
18494dd8b5abSYoshinobu Inoue 		    data_source.su_len) >= 0)
1850ea022d16SRodney W. Grimes 			break;
1851ea022d16SRodney W. Grimes 		if (errno != EADDRINUSE || tries > 10)
1852ea022d16SRodney W. Grimes 			goto bad;
1853ea022d16SRodney W. Grimes 		sleep(tries);
1854ea022d16SRodney W. Grimes 	}
185552e7ee74SYaroslav Tykhiy 	(void) seteuid(pw->pw_uid);
1856ea022d16SRodney W. Grimes #ifdef IP_TOS
18574dd8b5abSYoshinobu Inoue 	if (data_source.su_family == AF_INET)
18584dd8b5abSYoshinobu Inoue       {
1859ea022d16SRodney W. Grimes 	on = IPTOS_THROUGHPUT;
186057d4ef07SYaroslav Tykhiy 	if (setsockopt(s, IPPROTO_IP, IP_TOS, &on, sizeof(int)) < 0)
1861406d1ae9SYaroslav Tykhiy 		syslog(LOG_WARNING, "data setsockopt (IP_TOS): %m");
18624dd8b5abSYoshinobu Inoue       }
1863ea022d16SRodney W. Grimes #endif
18649fc5823aSGarrett Wollman #ifdef TCP_NOPUSH
18659fc5823aSGarrett Wollman 	/*
18669fc5823aSGarrett Wollman 	 * Turn off push flag to keep sender TCP from sending short packets
18679fc5823aSGarrett Wollman 	 * at the boundaries of each write().  Should probably do a SO_SNDBUF
18689fc5823aSGarrett Wollman 	 * to set the send buffer size as well, but that may not be desirable
18699fc5823aSGarrett Wollman 	 * in heavy-load situations.
18709fc5823aSGarrett Wollman 	 */
18719fc5823aSGarrett Wollman 	on = 1;
187257d4ef07SYaroslav Tykhiy 	if (setsockopt(s, IPPROTO_TCP, TCP_NOPUSH, &on, sizeof on) < 0)
1873406d1ae9SYaroslav Tykhiy 		syslog(LOG_WARNING, "data setsockopt (TCP_NOPUSH): %m");
18749fc5823aSGarrett Wollman #endif
18759fc5823aSGarrett Wollman #ifdef SO_SNDBUF
18769fc5823aSGarrett Wollman 	on = 65536;
187757d4ef07SYaroslav Tykhiy 	if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &on, sizeof on) < 0)
1878406d1ae9SYaroslav Tykhiy 		syslog(LOG_WARNING, "data setsockopt (SO_SNDBUF): %m");
18799fc5823aSGarrett Wollman #endif
18809fc5823aSGarrett Wollman 
1881ea022d16SRodney W. Grimes 	return (fdopen(s, mode));
1882ea022d16SRodney W. Grimes bad:
1883ea022d16SRodney W. Grimes 	/* Return the real value of errno (close may change it) */
1884ea022d16SRodney W. Grimes 	t = errno;
188552e7ee74SYaroslav Tykhiy 	(void) seteuid(pw->pw_uid);
1886ea022d16SRodney W. Grimes 	(void) close(s);
1887ea022d16SRodney W. Grimes 	errno = t;
1888ea022d16SRodney W. Grimes 	return (NULL);
1889ea022d16SRodney W. Grimes }
1890ea022d16SRodney W. Grimes 
1891ea022d16SRodney W. Grimes static FILE *
1892e4bc453cSWarner Losh dataconn(char *name, off_t size, char *mode)
1893ea022d16SRodney W. Grimes {
1894ea022d16SRodney W. Grimes 	char sizebuf[32];
1895ea022d16SRodney W. Grimes 	FILE *file;
1896e5094456SCrist J. Clark 	int retry = 0, tos, conerrno;
1897ea022d16SRodney W. Grimes 
1898ea022d16SRodney W. Grimes 	file_size = size;
1899ea022d16SRodney W. Grimes 	byte_count = 0;
19000e519c96SYaroslav Tykhiy 	if (size != -1)
1901a57e1ef0SYaroslav Tykhiy 		(void) snprintf(sizebuf, sizeof(sizebuf),
1902a57e1ef0SYaroslav Tykhiy 				" (%jd bytes)", (intmax_t)size);
1903ea022d16SRodney W. Grimes 	else
1904e760ef2cSWarner Losh 		*sizebuf = '\0';
1905ea022d16SRodney W. Grimes 	if (pdata >= 0) {
19064dd8b5abSYoshinobu Inoue 		union sockunion from;
19074cd48bacSYaroslav Tykhiy 		int flags;
19084dd8b5abSYoshinobu Inoue 		int s, fromlen = ctrl_addr.su_len;
1909d6ed3c37SGuido van Rooij 		struct timeval timeout;
1910d6ed3c37SGuido van Rooij 		fd_set set;
1911ea022d16SRodney W. Grimes 
1912d6ed3c37SGuido van Rooij 		FD_ZERO(&set);
1913d6ed3c37SGuido van Rooij 		FD_SET(pdata, &set);
1914d6ed3c37SGuido van Rooij 
1915d6ed3c37SGuido van Rooij 		timeout.tv_usec = 0;
1916d6ed3c37SGuido van Rooij 		timeout.tv_sec = 120;
1917d6ed3c37SGuido van Rooij 
19184cd48bacSYaroslav Tykhiy 		/*
19194cd48bacSYaroslav Tykhiy 		 * Granted a socket is in the blocking I/O mode,
19204cd48bacSYaroslav Tykhiy 		 * accept() will block after a successful select()
19214cd48bacSYaroslav Tykhiy 		 * if the selected connection dies in between.
19224cd48bacSYaroslav Tykhiy 		 * Therefore set the non-blocking I/O flag here.
19234cd48bacSYaroslav Tykhiy 		 */
19244cd48bacSYaroslav Tykhiy 		if ((flags = fcntl(pdata, F_GETFL, 0)) == -1 ||
19254cd48bacSYaroslav Tykhiy 		    fcntl(pdata, F_SETFL, flags | O_NONBLOCK) == -1)
19264cd48bacSYaroslav Tykhiy 			goto pdata_err;
1927aa5a9d3fSYaroslav Tykhiy 		if (select(pdata+1, &set, NULL, NULL, &timeout) <= 0 ||
19284cd48bacSYaroslav Tykhiy 		    (s = accept(pdata, (struct sockaddr *) &from, &fromlen)) < 0)
19294cd48bacSYaroslav Tykhiy 			goto pdata_err;
1930ea022d16SRodney W. Grimes 		(void) close(pdata);
1931ea022d16SRodney W. Grimes 		pdata = s;
19324cd48bacSYaroslav Tykhiy 		/*
1933f6daca0dSYaroslav Tykhiy 		 * Unset the inherited non-blocking I/O flag
1934f6daca0dSYaroslav Tykhiy 		 * on the child socket so stdio can work on it.
19354cd48bacSYaroslav Tykhiy 		 */
19364cd48bacSYaroslav Tykhiy 		if ((flags = fcntl(pdata, F_GETFL, 0)) == -1 ||
19374cd48bacSYaroslav Tykhiy 		    fcntl(pdata, F_SETFL, flags & ~O_NONBLOCK) == -1)
19384cd48bacSYaroslav Tykhiy 			goto pdata_err;
1939ea022d16SRodney W. Grimes #ifdef IP_TOS
19404dd8b5abSYoshinobu Inoue 		if (from.su_family == AF_INET)
19414dd8b5abSYoshinobu Inoue 	      {
1942a5a4544eSPaul Traina 		tos = IPTOS_THROUGHPUT;
194357d4ef07SYaroslav Tykhiy 		if (setsockopt(s, IPPROTO_IP, IP_TOS, &tos, sizeof(int)) < 0)
1944406d1ae9SYaroslav Tykhiy 			syslog(LOG_WARNING, "pdata setsockopt (IP_TOS): %m");
19454dd8b5abSYoshinobu Inoue 	      }
1946ea022d16SRodney W. Grimes #endif
1947ea022d16SRodney W. Grimes 		reply(150, "Opening %s mode data connection for '%s'%s.",
1948ea022d16SRodney W. Grimes 		     type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf);
1949ea022d16SRodney W. Grimes 		return (fdopen(pdata, mode));
19504cd48bacSYaroslav Tykhiy pdata_err:
19514cd48bacSYaroslav Tykhiy 		reply(425, "Can't open data connection.");
19524cd48bacSYaroslav Tykhiy 		(void) close(pdata);
19534cd48bacSYaroslav Tykhiy 		pdata = -1;
19544cd48bacSYaroslav Tykhiy 		return (NULL);
1955ea022d16SRodney W. Grimes 	}
1956ea022d16SRodney W. Grimes 	if (data >= 0) {
1957ea022d16SRodney W. Grimes 		reply(125, "Using existing data connection for '%s'%s.",
1958ea022d16SRodney W. Grimes 		    name, sizebuf);
1959ea022d16SRodney W. Grimes 		usedefault = 1;
1960ea022d16SRodney W. Grimes 		return (fdopen(data, mode));
1961ea022d16SRodney W. Grimes 	}
1962ea022d16SRodney W. Grimes 	if (usedefault)
1963ea022d16SRodney W. Grimes 		data_dest = his_addr;
1964ea022d16SRodney W. Grimes 	usedefault = 1;
1965e5094456SCrist J. Clark 	do {
1966ea022d16SRodney W. Grimes 		file = getdatasock(mode);
1967ea022d16SRodney W. Grimes 		if (file == NULL) {
19684dd8b5abSYoshinobu Inoue 			char hostbuf[BUFSIZ], portbuf[BUFSIZ];
19694dd8b5abSYoshinobu Inoue 			getnameinfo((struct sockaddr *)&data_source,
19704dd8b5abSYoshinobu Inoue 				data_source.su_len, hostbuf, sizeof(hostbuf) - 1,
19714dd8b5abSYoshinobu Inoue 				portbuf, sizeof(portbuf),
1972b0f06defSHajimu UMEMOTO 				NI_NUMERICHOST|NI_NUMERICSERV);
19734dd8b5abSYoshinobu Inoue 			reply(425, "Can't create data socket (%s,%s): %s.",
19744dd8b5abSYoshinobu Inoue 				hostbuf, portbuf, strerror(errno));
1975ea022d16SRodney W. Grimes 			return (NULL);
1976ea022d16SRodney W. Grimes 		}
1977ea022d16SRodney W. Grimes 		data = fileno(file);
1978e5094456SCrist J. Clark 		conerrno = 0;
1979e5094456SCrist J. Clark 		if (connect(data, (struct sockaddr *)&data_dest,
1980e5094456SCrist J. Clark 		    data_dest.su_len) == 0)
1981e5094456SCrist J. Clark 			break;
1982e5094456SCrist J. Clark 		conerrno = errno;
1983ea022d16SRodney W. Grimes 		(void) fclose(file);
1984ea022d16SRodney W. Grimes 		data = -1;
1985e5094456SCrist J. Clark 		if (conerrno == EADDRINUSE) {
1986e3765043SYaroslav Tykhiy 			sleep(swaitint);
1987e5094456SCrist J. Clark 			retry += swaitint;
1988e5094456SCrist J. Clark 		} else {
1989e5094456SCrist J. Clark 			break;
1990e5094456SCrist J. Clark 		}
1991e5094456SCrist J. Clark 	} while (retry <= swaitmax);
1992e5094456SCrist J. Clark 	if (conerrno != 0) {
1993e5094456SCrist J. Clark 		perror_reply(425, "Can't build data connection");
1994ea022d16SRodney W. Grimes 		return (NULL);
1995ea022d16SRodney W. Grimes 	}
1996ea022d16SRodney W. Grimes 	reply(150, "Opening %s mode data connection for '%s'%s.",
1997ea022d16SRodney W. Grimes 	     type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf);
1998ea022d16SRodney W. Grimes 	return (file);
1999ea022d16SRodney W. Grimes }
2000ea022d16SRodney W. Grimes 
2001ea022d16SRodney W. Grimes /*
2002ea022d16SRodney W. Grimes  * Tranfer the contents of "instr" to "outstr" peer using the appropriate
20039fc5823aSGarrett Wollman  * encapsulation of the data subject to Mode, Structure, and Type.
2004ea022d16SRodney W. Grimes  *
2005ea022d16SRodney W. Grimes  * NB: Form isn't handled.
2006ea022d16SRodney W. Grimes  */
20074b82fc95SYaroslav Tykhiy static int
20086e4b0a55SYaroslav Tykhiy send_data(FILE *instr, FILE *outstr, size_t blksize, off_t filesize, int isreg)
2009ea022d16SRodney W. Grimes {
2010db1c2da3SYaroslav Tykhiy 	int c, cp, filefd, netfd;
2011f6f0c4b9SDan Moschuk 	char *buf;
2012f6f0c4b9SDan Moschuk 	off_t cnt;
2013ea022d16SRodney W. Grimes 
2014ea022d16SRodney W. Grimes 	transflag++;
2015ea022d16SRodney W. Grimes 	switch (type) {
2016ea022d16SRodney W. Grimes 
2017ea022d16SRodney W. Grimes 	case TYPE_A:
2018db1c2da3SYaroslav Tykhiy 		cp = '\0';
2019ea022d16SRodney W. Grimes 		while ((c = getc(instr)) != EOF) {
20204b82fc95SYaroslav Tykhiy 			if (recvurg)
20214b82fc95SYaroslav Tykhiy 				goto got_oob;
2022ea022d16SRodney W. Grimes 			byte_count++;
2023db1c2da3SYaroslav Tykhiy 			if (c == '\n' && cp != '\r') {
2024ea022d16SRodney W. Grimes 				if (ferror(outstr))
2025ea022d16SRodney W. Grimes 					goto data_err;
2026ea022d16SRodney W. Grimes 				(void) putc('\r', outstr);
2027ea022d16SRodney W. Grimes 			}
2028ea022d16SRodney W. Grimes 			(void) putc(c, outstr);
2029db1c2da3SYaroslav Tykhiy 			cp = c;
2030ea022d16SRodney W. Grimes 		}
20314b82fc95SYaroslav Tykhiy 		if (recvurg)
20324b82fc95SYaroslav Tykhiy 			goto got_oob;
2033ea022d16SRodney W. Grimes 		fflush(outstr);
2034ea022d16SRodney W. Grimes 		transflag = 0;
2035ea022d16SRodney W. Grimes 		if (ferror(instr))
2036ea022d16SRodney W. Grimes 			goto file_err;
2037ea022d16SRodney W. Grimes 		if (ferror(outstr))
2038ea022d16SRodney W. Grimes 			goto data_err;
2039ea022d16SRodney W. Grimes 		reply(226, "Transfer complete.");
20404b82fc95SYaroslav Tykhiy 		return (0);
2041ea022d16SRodney W. Grimes 
2042ea022d16SRodney W. Grimes 	case TYPE_I:
2043ea022d16SRodney W. Grimes 	case TYPE_L:
20449fc5823aSGarrett Wollman 		/*
20459fc5823aSGarrett Wollman 		 * isreg is only set if we are not doing restart and we
20469fc5823aSGarrett Wollman 		 * are sending a regular file
20479fc5823aSGarrett Wollman 		 */
20489fc5823aSGarrett Wollman 		netfd = fileno(outstr);
20499fc5823aSGarrett Wollman 		filefd = fileno(instr);
20509fc5823aSGarrett Wollman 
2051f6f0c4b9SDan Moschuk 		if (isreg) {
20529fc5823aSGarrett Wollman 
20532e22b914SYaroslav Tykhiy 			char *msg = "Transfer complete.";
2054f6f0c4b9SDan Moschuk 			off_t offset;
2055f6f0c4b9SDan Moschuk 			int err;
2056f6f0c4b9SDan Moschuk 
20572f492fc8SYaroslav Tykhiy 			cnt = offset = 0;
2058f6f0c4b9SDan Moschuk 
20592f492fc8SYaroslav Tykhiy 			while (filesize > 0) {
2060492f1d9cSMaxim Konovalov 				err = sendfile(filefd, netfd, offset, 0,
20617e295315SYaroslav Tykhiy 					       NULL, &cnt, 0);
20623af48c42SMaxim Konovalov 				/*
20633af48c42SMaxim Konovalov 				 * Calculate byte_count before OOB processing.
20643af48c42SMaxim Konovalov 				 * It can be used in myoob() later.
20653af48c42SMaxim Konovalov 				 */
20663af48c42SMaxim Konovalov 				byte_count += cnt;
20674b82fc95SYaroslav Tykhiy 				if (recvurg)
20684b82fc95SYaroslav Tykhiy 					goto got_oob;
2069f6f0c4b9SDan Moschuk 				offset += cnt;
2070492f1d9cSMaxim Konovalov 				filesize -= cnt;
2071f6f0c4b9SDan Moschuk 
2072f6f0c4b9SDan Moschuk 				if (err == -1) {
2073b4585cc1SYaroslav Tykhiy 					if (cnt == 0 && offset == 0)
2074f6f0c4b9SDan Moschuk 						goto oldway;
2075f6f0c4b9SDan Moschuk 
20769fc5823aSGarrett Wollman 					goto data_err;
2077f6f0c4b9SDan Moschuk 				}
20782e22b914SYaroslav Tykhiy 
20792e22b914SYaroslav Tykhiy 				/*
20802e22b914SYaroslav Tykhiy 				 * We hit the EOF prematurely.
20812e22b914SYaroslav Tykhiy 				 * Perhaps the file was externally truncated.
20822e22b914SYaroslav Tykhiy 				 */
20832e22b914SYaroslav Tykhiy 				if (cnt == 0) {
20842e22b914SYaroslav Tykhiy 					msg = "Transfer finished due to "
20852e22b914SYaroslav Tykhiy 					      "premature end of file.";
20862e22b914SYaroslav Tykhiy 					break;
20872e22b914SYaroslav Tykhiy 				}
2088f6f0c4b9SDan Moschuk 			}
2089f6f0c4b9SDan Moschuk 
20900849c184SDan Moschuk 			transflag = 0;
20912e22b914SYaroslav Tykhiy 			reply(226, msg);
20924b82fc95SYaroslav Tykhiy 			return (0);
20939fc5823aSGarrett Wollman 		}
20949fc5823aSGarrett Wollman 
20959fc5823aSGarrett Wollman oldway:
2096e3765043SYaroslav Tykhiy 		if ((buf = malloc(blksize)) == NULL) {
2097ea022d16SRodney W. Grimes 			transflag = 0;
2098ea022d16SRodney W. Grimes 			perror_reply(451, "Local resource failure: malloc");
20994b82fc95SYaroslav Tykhiy 			return (-1);
2100ea022d16SRodney W. Grimes 		}
21019fc5823aSGarrett Wollman 
2102e3765043SYaroslav Tykhiy 		while ((cnt = read(filefd, buf, blksize)) > 0 &&
2103ea022d16SRodney W. Grimes 		    write(netfd, buf, cnt) == cnt)
2104ea022d16SRodney W. Grimes 			byte_count += cnt;
2105ea022d16SRodney W. Grimes 		transflag = 0;
2106ea022d16SRodney W. Grimes 		(void)free(buf);
2107ea022d16SRodney W. Grimes 		if (cnt != 0) {
2108ea022d16SRodney W. Grimes 			if (cnt < 0)
2109ea022d16SRodney W. Grimes 				goto file_err;
2110ea022d16SRodney W. Grimes 			goto data_err;
2111ea022d16SRodney W. Grimes 		}
2112ea022d16SRodney W. Grimes 		reply(226, "Transfer complete.");
21134b82fc95SYaroslav Tykhiy 		return (0);
2114ea022d16SRodney W. Grimes 	default:
2115ea022d16SRodney W. Grimes 		transflag = 0;
2116ea022d16SRodney W. Grimes 		reply(550, "Unimplemented TYPE %d in send_data", type);
21174b82fc95SYaroslav Tykhiy 		return (-1);
2118ea022d16SRodney W. Grimes 	}
2119ea022d16SRodney W. Grimes 
2120ea022d16SRodney W. Grimes data_err:
2121ea022d16SRodney W. Grimes 	transflag = 0;
2122ea022d16SRodney W. Grimes 	perror_reply(426, "Data connection");
21234b82fc95SYaroslav Tykhiy 	return (-1);
2124ea022d16SRodney W. Grimes 
2125ea022d16SRodney W. Grimes file_err:
2126ea022d16SRodney W. Grimes 	transflag = 0;
2127ea022d16SRodney W. Grimes 	perror_reply(551, "Error on input file");
21284b82fc95SYaroslav Tykhiy 	return (-1);
21294b82fc95SYaroslav Tykhiy 
21304b82fc95SYaroslav Tykhiy got_oob:
21314b82fc95SYaroslav Tykhiy 	myoob();
21324b82fc95SYaroslav Tykhiy 	recvurg = 0;
21334b82fc95SYaroslav Tykhiy 	transflag = 0;
21344b82fc95SYaroslav Tykhiy 	return (-1);
2135ea022d16SRodney W. Grimes }
2136ea022d16SRodney W. Grimes 
2137ea022d16SRodney W. Grimes /*
2138ea022d16SRodney W. Grimes  * Transfer data from peer to "outstr" using the appropriate encapulation of
2139ea022d16SRodney W. Grimes  * the data subject to Mode, Structure, and Type.
2140ea022d16SRodney W. Grimes  *
2141ea022d16SRodney W. Grimes  * N.B.: Form isn't handled.
2142ea022d16SRodney W. Grimes  */
2143ea022d16SRodney W. Grimes static int
2144e4bc453cSWarner Losh receive_data(FILE *instr, FILE *outstr)
2145ea022d16SRodney W. Grimes {
2146ea022d16SRodney W. Grimes 	int c;
214761f891a6SPaul Traina 	int cnt, bare_lfs;
2148ea022d16SRodney W. Grimes 	char buf[BUFSIZ];
2149ea022d16SRodney W. Grimes 
2150ea022d16SRodney W. Grimes 	transflag++;
215161f891a6SPaul Traina 	bare_lfs = 0;
215261f891a6SPaul Traina 
2153ea022d16SRodney W. Grimes 	switch (type) {
2154ea022d16SRodney W. Grimes 
2155ea022d16SRodney W. Grimes 	case TYPE_I:
2156ea022d16SRodney W. Grimes 	case TYPE_L:
2157ea022d16SRodney W. Grimes 		while ((cnt = read(fileno(instr), buf, sizeof(buf))) > 0) {
21584b82fc95SYaroslav Tykhiy 			if (recvurg)
21594b82fc95SYaroslav Tykhiy 				goto got_oob;
2160ea022d16SRodney W. Grimes 			if (write(fileno(outstr), buf, cnt) != cnt)
2161ea022d16SRodney W. Grimes 				goto file_err;
2162ea022d16SRodney W. Grimes 			byte_count += cnt;
2163ea022d16SRodney W. Grimes 		}
21644b82fc95SYaroslav Tykhiy 		if (recvurg)
21654b82fc95SYaroslav Tykhiy 			goto got_oob;
2166ea022d16SRodney W. Grimes 		if (cnt < 0)
2167ea022d16SRodney W. Grimes 			goto data_err;
2168ea022d16SRodney W. Grimes 		transflag = 0;
2169ea022d16SRodney W. Grimes 		return (0);
2170ea022d16SRodney W. Grimes 
2171ea022d16SRodney W. Grimes 	case TYPE_E:
2172ea022d16SRodney W. Grimes 		reply(553, "TYPE E not implemented.");
2173ea022d16SRodney W. Grimes 		transflag = 0;
2174ea022d16SRodney W. Grimes 		return (-1);
2175ea022d16SRodney W. Grimes 
2176ea022d16SRodney W. Grimes 	case TYPE_A:
2177ea022d16SRodney W. Grimes 		while ((c = getc(instr)) != EOF) {
21784b82fc95SYaroslav Tykhiy 			if (recvurg)
21794b82fc95SYaroslav Tykhiy 				goto got_oob;
2180ea022d16SRodney W. Grimes 			byte_count++;
2181ea022d16SRodney W. Grimes 			if (c == '\n')
2182ea022d16SRodney W. Grimes 				bare_lfs++;
2183ea022d16SRodney W. Grimes 			while (c == '\r') {
2184ea022d16SRodney W. Grimes 				if (ferror(outstr))
2185ea022d16SRodney W. Grimes 					goto data_err;
2186ea022d16SRodney W. Grimes 				if ((c = getc(instr)) != '\n') {
2187ea022d16SRodney W. Grimes 					(void) putc ('\r', outstr);
2188ea022d16SRodney W. Grimes 					if (c == '\0' || c == EOF)
2189ea022d16SRodney W. Grimes 						goto contin2;
2190ea022d16SRodney W. Grimes 				}
2191ea022d16SRodney W. Grimes 			}
2192ea022d16SRodney W. Grimes 			(void) putc(c, outstr);
2193ea022d16SRodney W. Grimes 	contin2:	;
2194ea022d16SRodney W. Grimes 		}
21954b82fc95SYaroslav Tykhiy 		if (recvurg)
21964b82fc95SYaroslav Tykhiy 			goto got_oob;
2197ea022d16SRodney W. Grimes 		fflush(outstr);
2198ea022d16SRodney W. Grimes 		if (ferror(instr))
2199ea022d16SRodney W. Grimes 			goto data_err;
2200ea022d16SRodney W. Grimes 		if (ferror(outstr))
2201ea022d16SRodney W. Grimes 			goto file_err;
2202ea022d16SRodney W. Grimes 		transflag = 0;
2203ea022d16SRodney W. Grimes 		if (bare_lfs) {
2204ea022d16SRodney W. Grimes 			lreply(226,
2205ea022d16SRodney W. Grimes 		"WARNING! %d bare linefeeds received in ASCII mode",
2206ea022d16SRodney W. Grimes 			    bare_lfs);
2207ea022d16SRodney W. Grimes 		(void)printf("   File may not have transferred correctly.\r\n");
2208ea022d16SRodney W. Grimes 		}
2209ea022d16SRodney W. Grimes 		return (0);
2210ea022d16SRodney W. Grimes 	default:
2211ea022d16SRodney W. Grimes 		reply(550, "Unimplemented TYPE %d in receive_data", type);
2212ea022d16SRodney W. Grimes 		transflag = 0;
2213ea022d16SRodney W. Grimes 		return (-1);
2214ea022d16SRodney W. Grimes 	}
2215ea022d16SRodney W. Grimes 
2216ea022d16SRodney W. Grimes data_err:
2217ea022d16SRodney W. Grimes 	transflag = 0;
2218ea022d16SRodney W. Grimes 	perror_reply(426, "Data Connection");
2219ea022d16SRodney W. Grimes 	return (-1);
2220ea022d16SRodney W. Grimes 
2221ea022d16SRodney W. Grimes file_err:
2222ea022d16SRodney W. Grimes 	transflag = 0;
2223ea022d16SRodney W. Grimes 	perror_reply(452, "Error writing file");
2224ea022d16SRodney W. Grimes 	return (-1);
22254b82fc95SYaroslav Tykhiy 
22264b82fc95SYaroslav Tykhiy got_oob:
22274b82fc95SYaroslav Tykhiy 	myoob();
22284b82fc95SYaroslav Tykhiy 	recvurg = 0;
22294b82fc95SYaroslav Tykhiy 	transflag = 0;
22304b82fc95SYaroslav Tykhiy 	return (-1);
2231ea022d16SRodney W. Grimes }
2232ea022d16SRodney W. Grimes 
2233ea022d16SRodney W. Grimes void
2234e4bc453cSWarner Losh statfilecmd(char *filename)
2235ea022d16SRodney W. Grimes {
2236ea022d16SRodney W. Grimes 	FILE *fin;
2237f8a581a0SYaroslav Tykhiy 	int atstart;
223841c57b48SYaroslav Tykhiy 	int c, code;
2239ea022d16SRodney W. Grimes 	char line[LINE_MAX];
224041c57b48SYaroslav Tykhiy 	struct stat st;
2241ea022d16SRodney W. Grimes 
224241c57b48SYaroslav Tykhiy 	code = lstat(filename, &st) == 0 && S_ISDIR(st.st_mode) ? 212 : 213;
2243af85d782SDavid Nugent 	(void)snprintf(line, sizeof(line), _PATH_LS " -lgA %s", filename);
2244ea022d16SRodney W. Grimes 	fin = ftpd_popen(line, "r");
22453b48b877SYaroslav Tykhiy 	lreply(code, "Status of %s:", filename);
2246f8a581a0SYaroslav Tykhiy 	atstart = 1;
2247ea022d16SRodney W. Grimes 	while ((c = getc(fin)) != EOF) {
2248ea022d16SRodney W. Grimes 		if (c == '\n') {
2249ea022d16SRodney W. Grimes 			if (ferror(stdout)){
2250ea022d16SRodney W. Grimes 				perror_reply(421, "control connection");
2251ea022d16SRodney W. Grimes 				(void) ftpd_pclose(fin);
2252ea022d16SRodney W. Grimes 				dologout(1);
2253ea022d16SRodney W. Grimes 				/* NOTREACHED */
2254ea022d16SRodney W. Grimes 			}
2255ea022d16SRodney W. Grimes 			if (ferror(fin)) {
2256ea022d16SRodney W. Grimes 				perror_reply(551, filename);
2257ea022d16SRodney W. Grimes 				(void) ftpd_pclose(fin);
2258ea022d16SRodney W. Grimes 				return;
2259ea022d16SRodney W. Grimes 			}
2260ea022d16SRodney W. Grimes 			(void) putc('\r', stdout);
2261ea022d16SRodney W. Grimes 		}
2262f8a581a0SYaroslav Tykhiy 		/*
2263f8a581a0SYaroslav Tykhiy 		 * RFC 959 says neutral text should be prepended before
2264f8a581a0SYaroslav Tykhiy 		 * a leading 3-digit number followed by whitespace, but
2265f8a581a0SYaroslav Tykhiy 		 * many ftp clients can be confused by any leading digits,
2266f8a581a0SYaroslav Tykhiy 		 * as a matter of fact.
2267f8a581a0SYaroslav Tykhiy 		 */
2268f8a581a0SYaroslav Tykhiy 		if (atstart && isdigit(c))
2269f8a581a0SYaroslav Tykhiy 			(void) putc(' ', stdout);
2270ea022d16SRodney W. Grimes 		(void) putc(c, stdout);
2271f8a581a0SYaroslav Tykhiy 		atstart = (c == '\n');
2272ea022d16SRodney W. Grimes 	}
2273ea022d16SRodney W. Grimes 	(void) ftpd_pclose(fin);
22743b48b877SYaroslav Tykhiy 	reply(code, "End of status");
2275ea022d16SRodney W. Grimes }
2276ea022d16SRodney W. Grimes 
2277ea022d16SRodney W. Grimes void
2278e4bc453cSWarner Losh statcmd(void)
2279ea022d16SRodney W. Grimes {
22804dd8b5abSYoshinobu Inoue 	union sockunion *su;
2281ea022d16SRodney W. Grimes 	u_char *a, *p;
2282b0f06defSHajimu UMEMOTO 	char hname[NI_MAXHOST];
22834dd8b5abSYoshinobu Inoue 	int ispassive;
2284ea022d16SRodney W. Grimes 
2285c152df28SYaroslav Tykhiy 	if (hostinfo) {
2286a23f61bcSYaroslav Tykhiy 		lreply(211, "%s FTP server status:", hostname);
2287ea022d16SRodney W. Grimes 		printf("     %s\r\n", version);
2288c152df28SYaroslav Tykhiy 	} else
2289c152df28SYaroslav Tykhiy 		lreply(211, "FTP server status:");
2290ea022d16SRodney W. Grimes 	printf("     Connected to %s", remotehost);
22914dd8b5abSYoshinobu Inoue 	if (!getnameinfo((struct sockaddr *)&his_addr, his_addr.su_len,
2292b0f06defSHajimu UMEMOTO 			 hname, sizeof(hname) - 1, NULL, 0, NI_NUMERICHOST)) {
22934dd8b5abSYoshinobu Inoue 		if (strcmp(hname, remotehost) != 0)
22944dd8b5abSYoshinobu Inoue 			printf(" (%s)", hname);
22954dd8b5abSYoshinobu Inoue 	}
2296ea022d16SRodney W. Grimes 	printf("\r\n");
2297ea022d16SRodney W. Grimes 	if (logged_in) {
2298ea022d16SRodney W. Grimes 		if (guest)
2299ea022d16SRodney W. Grimes 			printf("     Logged in anonymously\r\n");
2300ea022d16SRodney W. Grimes 		else
2301ea022d16SRodney W. Grimes 			printf("     Logged in as %s\r\n", pw->pw_name);
2302ea022d16SRodney W. Grimes 	} else if (askpasswd)
2303ea022d16SRodney W. Grimes 		printf("     Waiting for password\r\n");
2304ea022d16SRodney W. Grimes 	else
2305ea022d16SRodney W. Grimes 		printf("     Waiting for user name\r\n");
2306ea022d16SRodney W. Grimes 	printf("     TYPE: %s", typenames[type]);
2307ea022d16SRodney W. Grimes 	if (type == TYPE_A || type == TYPE_E)
2308ea022d16SRodney W. Grimes 		printf(", FORM: %s", formnames[form]);
2309ea022d16SRodney W. Grimes 	if (type == TYPE_L)
231089fdc4e1SMike Barcroft #if CHAR_BIT == 8
231189fdc4e1SMike Barcroft 		printf(" %d", CHAR_BIT);
2312ea022d16SRodney W. Grimes #else
2313ea022d16SRodney W. Grimes 		printf(" %d", bytesize);	/* need definition! */
2314ea022d16SRodney W. Grimes #endif
2315ea022d16SRodney W. Grimes 	printf("; STRUcture: %s; transfer MODE: %s\r\n",
2316ea022d16SRodney W. Grimes 	    strunames[stru], modenames[mode]);
2317ea022d16SRodney W. Grimes 	if (data != -1)
2318ea022d16SRodney W. Grimes 		printf("     Data connection open\r\n");
2319ea022d16SRodney W. Grimes 	else if (pdata != -1) {
23204dd8b5abSYoshinobu Inoue 		ispassive = 1;
23214dd8b5abSYoshinobu Inoue 		su = &pasv_addr;
2322ea022d16SRodney W. Grimes 		goto printaddr;
2323ea022d16SRodney W. Grimes 	} else if (usedefault == 0) {
23244dd8b5abSYoshinobu Inoue 		ispassive = 0;
23254dd8b5abSYoshinobu Inoue 		su = &data_dest;
2326ea022d16SRodney W. Grimes printaddr:
2327ea022d16SRodney W. Grimes #define UC(b) (((int) b) & 0xff)
23284dd8b5abSYoshinobu Inoue 		if (epsvall) {
23294dd8b5abSYoshinobu Inoue 			printf("     EPSV only mode (EPSV ALL)\r\n");
23304dd8b5abSYoshinobu Inoue 			goto epsvonly;
23314dd8b5abSYoshinobu Inoue 		}
23324dd8b5abSYoshinobu Inoue 
23334dd8b5abSYoshinobu Inoue 		/* PORT/PASV */
23344dd8b5abSYoshinobu Inoue 		if (su->su_family == AF_INET) {
23354dd8b5abSYoshinobu Inoue 			a = (u_char *) &su->su_sin.sin_addr;
23364dd8b5abSYoshinobu Inoue 			p = (u_char *) &su->su_sin.sin_port;
23374dd8b5abSYoshinobu Inoue 			printf("     %s (%d,%d,%d,%d,%d,%d)\r\n",
23384dd8b5abSYoshinobu Inoue 				ispassive ? "PASV" : "PORT",
23394dd8b5abSYoshinobu Inoue 				UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
23404dd8b5abSYoshinobu Inoue 				UC(p[0]), UC(p[1]));
23414dd8b5abSYoshinobu Inoue 		}
23424dd8b5abSYoshinobu Inoue 
23434dd8b5abSYoshinobu Inoue 		/* LPRT/LPSV */
23444dd8b5abSYoshinobu Inoue 	    {
23454dd8b5abSYoshinobu Inoue 		int alen, af, i;
23464dd8b5abSYoshinobu Inoue 
23474dd8b5abSYoshinobu Inoue 		switch (su->su_family) {
23484dd8b5abSYoshinobu Inoue 		case AF_INET:
23494dd8b5abSYoshinobu Inoue 			a = (u_char *) &su->su_sin.sin_addr;
23504dd8b5abSYoshinobu Inoue 			p = (u_char *) &su->su_sin.sin_port;
23514dd8b5abSYoshinobu Inoue 			alen = sizeof(su->su_sin.sin_addr);
23524dd8b5abSYoshinobu Inoue 			af = 4;
23534dd8b5abSYoshinobu Inoue 			break;
23544dd8b5abSYoshinobu Inoue 		case AF_INET6:
23554dd8b5abSYoshinobu Inoue 			a = (u_char *) &su->su_sin6.sin6_addr;
23564dd8b5abSYoshinobu Inoue 			p = (u_char *) &su->su_sin6.sin6_port;
23574dd8b5abSYoshinobu Inoue 			alen = sizeof(su->su_sin6.sin6_addr);
23584dd8b5abSYoshinobu Inoue 			af = 6;
23594dd8b5abSYoshinobu Inoue 			break;
23604dd8b5abSYoshinobu Inoue 		default:
23614dd8b5abSYoshinobu Inoue 			af = 0;
23624dd8b5abSYoshinobu Inoue 			break;
23634dd8b5abSYoshinobu Inoue 		}
23644dd8b5abSYoshinobu Inoue 		if (af) {
23654dd8b5abSYoshinobu Inoue 			printf("     %s (%d,%d,", ispassive ? "LPSV" : "LPRT",
23664dd8b5abSYoshinobu Inoue 				af, alen);
23674dd8b5abSYoshinobu Inoue 			for (i = 0; i < alen; i++)
23684dd8b5abSYoshinobu Inoue 				printf("%d,", UC(a[i]));
23694dd8b5abSYoshinobu Inoue 			printf("%d,%d,%d)\r\n", 2, UC(p[0]), UC(p[1]));
23704dd8b5abSYoshinobu Inoue 		}
23714dd8b5abSYoshinobu Inoue 	    }
23724dd8b5abSYoshinobu Inoue 
23734dd8b5abSYoshinobu Inoue epsvonly:;
23744dd8b5abSYoshinobu Inoue 		/* EPRT/EPSV */
23754dd8b5abSYoshinobu Inoue 	    {
23764dd8b5abSYoshinobu Inoue 		int af;
23774dd8b5abSYoshinobu Inoue 
23784dd8b5abSYoshinobu Inoue 		switch (su->su_family) {
23794dd8b5abSYoshinobu Inoue 		case AF_INET:
23804dd8b5abSYoshinobu Inoue 			af = 1;
23814dd8b5abSYoshinobu Inoue 			break;
23824dd8b5abSYoshinobu Inoue 		case AF_INET6:
23834dd8b5abSYoshinobu Inoue 			af = 2;
23844dd8b5abSYoshinobu Inoue 			break;
23854dd8b5abSYoshinobu Inoue 		default:
23864dd8b5abSYoshinobu Inoue 			af = 0;
23874dd8b5abSYoshinobu Inoue 			break;
23884dd8b5abSYoshinobu Inoue 		}
23894dd8b5abSYoshinobu Inoue 		if (af) {
2390b0f06defSHajimu UMEMOTO 			union sockunion tmp;
2391b0f06defSHajimu UMEMOTO 
2392b0f06defSHajimu UMEMOTO 			tmp = *su;
2393b0f06defSHajimu UMEMOTO 			if (tmp.su_family == AF_INET6)
2394b0f06defSHajimu UMEMOTO 				tmp.su_sin6.sin6_scope_id = 0;
2395b0f06defSHajimu UMEMOTO 			if (!getnameinfo((struct sockaddr *)&tmp, tmp.su_len,
23964dd8b5abSYoshinobu Inoue 					hname, sizeof(hname) - 1, NULL, 0,
23974dd8b5abSYoshinobu Inoue 					NI_NUMERICHOST)) {
23984dd8b5abSYoshinobu Inoue 				printf("     %s |%d|%s|%d|\r\n",
23994dd8b5abSYoshinobu Inoue 					ispassive ? "EPSV" : "EPRT",
2400b0f06defSHajimu UMEMOTO 					af, hname, htons(tmp.su_port));
24014dd8b5abSYoshinobu Inoue 			}
24024dd8b5abSYoshinobu Inoue 		}
24034dd8b5abSYoshinobu Inoue 	    }
2404ea022d16SRodney W. Grimes #undef UC
2405ea022d16SRodney W. Grimes 	} else
2406ea022d16SRodney W. Grimes 		printf("     No data connection\r\n");
2407ea022d16SRodney W. Grimes 	reply(211, "End of status");
2408ea022d16SRodney W. Grimes }
2409ea022d16SRodney W. Grimes 
2410ea022d16SRodney W. Grimes void
2411e4bc453cSWarner Losh fatalerror(char *s)
2412ea022d16SRodney W. Grimes {
2413ea022d16SRodney W. Grimes 
2414ea022d16SRodney W. Grimes 	reply(451, "Error in server: %s\n", s);
2415ea022d16SRodney W. Grimes 	reply(221, "Closing connection due to server error.");
2416ea022d16SRodney W. Grimes 	dologout(0);
2417ea022d16SRodney W. Grimes 	/* NOTREACHED */
2418ea022d16SRodney W. Grimes }
2419ea022d16SRodney W. Grimes 
2420ea022d16SRodney W. Grimes void
2421ea022d16SRodney W. Grimes reply(int n, const char *fmt, ...)
2422ea022d16SRodney W. Grimes {
2423ea022d16SRodney W. Grimes 	va_list ap;
2424e4bc453cSWarner Losh 
2425ea022d16SRodney W. Grimes 	(void)printf("%d ", n);
24269cbb335cSTim J. Robbins 	va_start(ap, fmt);
2427ea022d16SRodney W. Grimes 	(void)vprintf(fmt, ap);
24289cbb335cSTim J. Robbins 	va_end(ap);
2429ea022d16SRodney W. Grimes 	(void)printf("\r\n");
2430ea022d16SRodney W. Grimes 	(void)fflush(stdout);
2431618b0bbaSMark Murray 	if (ftpdebug) {
2432ea022d16SRodney W. Grimes 		syslog(LOG_DEBUG, "<--- %d ", n);
24339cbb335cSTim J. Robbins 		va_start(ap, fmt);
2434ea022d16SRodney W. Grimes 		vsyslog(LOG_DEBUG, fmt, ap);
24359cbb335cSTim J. Robbins 		va_end(ap);
2436ea022d16SRodney W. Grimes 	}
2437ea022d16SRodney W. Grimes }
2438ea022d16SRodney W. Grimes 
2439ea022d16SRodney W. Grimes void
2440ea022d16SRodney W. Grimes lreply(int n, const char *fmt, ...)
2441ea022d16SRodney W. Grimes {
2442ea022d16SRodney W. Grimes 	va_list ap;
2443e4bc453cSWarner Losh 
2444ea022d16SRodney W. Grimes 	(void)printf("%d- ", n);
24459cbb335cSTim J. Robbins 	va_start(ap, fmt);
2446ea022d16SRodney W. Grimes 	(void)vprintf(fmt, ap);
24479cbb335cSTim J. Robbins 	va_end(ap);
2448ea022d16SRodney W. Grimes 	(void)printf("\r\n");
2449ea022d16SRodney W. Grimes 	(void)fflush(stdout);
2450618b0bbaSMark Murray 	if (ftpdebug) {
2451ea022d16SRodney W. Grimes 		syslog(LOG_DEBUG, "<--- %d- ", n);
24529cbb335cSTim J. Robbins 		va_start(ap, fmt);
2453ea022d16SRodney W. Grimes 		vsyslog(LOG_DEBUG, fmt, ap);
24549cbb335cSTim J. Robbins 		va_end(ap);
2455ea022d16SRodney W. Grimes 	}
2456ea022d16SRodney W. Grimes }
2457ea022d16SRodney W. Grimes 
2458ea022d16SRodney W. Grimes static void
2459e4bc453cSWarner Losh ack(char *s)
2460ea022d16SRodney W. Grimes {
2461ea022d16SRodney W. Grimes 
2462ea022d16SRodney W. Grimes 	reply(250, "%s command successful.", s);
2463ea022d16SRodney W. Grimes }
2464ea022d16SRodney W. Grimes 
2465ea022d16SRodney W. Grimes void
2466e4bc453cSWarner Losh nack(char *s)
2467ea022d16SRodney W. Grimes {
2468ea022d16SRodney W. Grimes 
2469ea022d16SRodney W. Grimes 	reply(502, "%s command not implemented.", s);
2470ea022d16SRodney W. Grimes }
2471ea022d16SRodney W. Grimes 
2472ea022d16SRodney W. Grimes /* ARGSUSED */
2473ea022d16SRodney W. Grimes void
2474e4bc453cSWarner Losh yyerror(char *s)
2475ea022d16SRodney W. Grimes {
2476ea022d16SRodney W. Grimes 	char *cp;
2477ea022d16SRodney W. Grimes 
24789aca17cbSMark Murray 	if ((cp = strchr(cbuf,'\n')))
2479ea022d16SRodney W. Grimes 		*cp = '\0';
2480ea022d16SRodney W. Grimes 	reply(500, "'%s': command not understood.", cbuf);
2481ea022d16SRodney W. Grimes }
2482ea022d16SRodney W. Grimes 
2483ea022d16SRodney W. Grimes void
2484e4bc453cSWarner Losh delete(char *name)
2485ea022d16SRodney W. Grimes {
2486ea022d16SRodney W. Grimes 	struct stat st;
2487ea022d16SRodney W. Grimes 
2488ea022d16SRodney W. Grimes 	LOGCMD("delete", name);
24891b0e12d7SYaroslav Tykhiy 	if (lstat(name, &st) < 0) {
2490ea022d16SRodney W. Grimes 		perror_reply(550, name);
2491ea022d16SRodney W. Grimes 		return;
2492ea022d16SRodney W. Grimes 	}
2493ea022d16SRodney W. Grimes 	if ((st.st_mode&S_IFMT) == S_IFDIR) {
2494ea022d16SRodney W. Grimes 		if (rmdir(name) < 0) {
2495ea022d16SRodney W. Grimes 			perror_reply(550, name);
2496ea022d16SRodney W. Grimes 			return;
2497ea022d16SRodney W. Grimes 		}
2498ea022d16SRodney W. Grimes 		goto done;
2499ea022d16SRodney W. Grimes 	}
25003f8b9cfeSYaroslav Tykhiy 	if (guest && noguestmod) {
25013f8b9cfeSYaroslav Tykhiy 		reply(550, "Operation not permitted");
25023f8b9cfeSYaroslav Tykhiy 		return;
25033f8b9cfeSYaroslav Tykhiy 	}
25043f8b9cfeSYaroslav Tykhiy 	if (unlink(name) < 0) {
2505ea022d16SRodney W. Grimes 		perror_reply(550, name);
2506ea022d16SRodney W. Grimes 		return;
2507ea022d16SRodney W. Grimes 	}
2508ea022d16SRodney W. Grimes done:
2509ea022d16SRodney W. Grimes 	ack("DELE");
2510ea022d16SRodney W. Grimes }
2511ea022d16SRodney W. Grimes 
2512ea022d16SRodney W. Grimes void
2513e4bc453cSWarner Losh cwd(char *path)
2514ea022d16SRodney W. Grimes {
2515ea022d16SRodney W. Grimes 
2516ea022d16SRodney W. Grimes 	if (chdir(path) < 0)
2517ea022d16SRodney W. Grimes 		perror_reply(550, path);
2518ea022d16SRodney W. Grimes 	else
2519ea022d16SRodney W. Grimes 		ack("CWD");
2520ea022d16SRodney W. Grimes }
2521ea022d16SRodney W. Grimes 
2522ea022d16SRodney W. Grimes void
2523e4bc453cSWarner Losh makedir(char *name)
2524ea022d16SRodney W. Grimes {
25252b748987SYaroslav Tykhiy 	char *s;
2526ea022d16SRodney W. Grimes 
2527ea022d16SRodney W. Grimes 	LOGCMD("mkdir", name);
2528d186bb12SMatthew N. Dodd 	if (guest && noguestmkd)
2529d186bb12SMatthew N. Dodd 		reply(550, "%s: permission denied", name);
2530d186bb12SMatthew N. Dodd 	else if (mkdir(name, 0777) < 0)
2531ea022d16SRodney W. Grimes 		perror_reply(550, name);
25322b748987SYaroslav Tykhiy 	else {
25332b748987SYaroslav Tykhiy 		if ((s = doublequote(name)) == NULL)
25342b748987SYaroslav Tykhiy 			fatalerror("Ran out of memory.");
25352b748987SYaroslav Tykhiy 		reply(257, "\"%s\" directory created.", s);
25362b748987SYaroslav Tykhiy 		free(s);
25372b748987SYaroslav Tykhiy 	}
2538ea022d16SRodney W. Grimes }
2539ea022d16SRodney W. Grimes 
2540ea022d16SRodney W. Grimes void
2541e4bc453cSWarner Losh removedir(char *name)
2542ea022d16SRodney W. Grimes {
2543ea022d16SRodney W. Grimes 
2544ea022d16SRodney W. Grimes 	LOGCMD("rmdir", name);
2545ea022d16SRodney W. Grimes 	if (rmdir(name) < 0)
2546ea022d16SRodney W. Grimes 		perror_reply(550, name);
2547ea022d16SRodney W. Grimes 	else
2548ea022d16SRodney W. Grimes 		ack("RMD");
2549ea022d16SRodney W. Grimes }
2550ea022d16SRodney W. Grimes 
2551ea022d16SRodney W. Grimes void
2552e4bc453cSWarner Losh pwd(void)
2553ea022d16SRodney W. Grimes {
2554e4648f05SYaroslav Tykhiy 	char *s, path[MAXPATHLEN + 1];
2555ea022d16SRodney W. Grimes 
2556de9b6c03SYaroslav Tykhiy 	if (getcwd(path, sizeof(path)) == NULL)
2557ea022d16SRodney W. Grimes 		reply(550, "%s.", path);
2558e4648f05SYaroslav Tykhiy 	else {
2559e4648f05SYaroslav Tykhiy 		if ((s = doublequote(path)) == NULL)
2560e4648f05SYaroslav Tykhiy 			fatalerror("Ran out of memory.");
2561e4648f05SYaroslav Tykhiy 		reply(257, "\"%s\" is current directory.", s);
2562e4648f05SYaroslav Tykhiy 		free(s);
2563e4648f05SYaroslav Tykhiy 	}
2564ea022d16SRodney W. Grimes }
2565ea022d16SRodney W. Grimes 
2566ea022d16SRodney W. Grimes char *
2567e4bc453cSWarner Losh renamefrom(char *name)
2568ea022d16SRodney W. Grimes {
2569ea022d16SRodney W. Grimes 	struct stat st;
2570ea022d16SRodney W. Grimes 
2571b943b3c4SYaroslav Tykhiy 	if (guest && noguestmod) {
2572b943b3c4SYaroslav Tykhiy 		reply(550, "Operation not permitted");
2573b943b3c4SYaroslav Tykhiy 		return (NULL);
2574b943b3c4SYaroslav Tykhiy 	}
25751b0e12d7SYaroslav Tykhiy 	if (lstat(name, &st) < 0) {
2576ea022d16SRodney W. Grimes 		perror_reply(550, name);
2577385f9bf0SYaroslav Tykhiy 		return (NULL);
2578ea022d16SRodney W. Grimes 	}
2579ea022d16SRodney W. Grimes 	reply(350, "File exists, ready for destination name");
2580ea022d16SRodney W. Grimes 	return (name);
2581ea022d16SRodney W. Grimes }
2582ea022d16SRodney W. Grimes 
2583ea022d16SRodney W. Grimes void
2584e4bc453cSWarner Losh renamecmd(char *from, char *to)
2585ea022d16SRodney W. Grimes {
258661f891a6SPaul Traina 	struct stat st;
2587ea022d16SRodney W. Grimes 
2588ea022d16SRodney W. Grimes 	LOGCMD2("rename", from, to);
258961f891a6SPaul Traina 
259061f891a6SPaul Traina 	if (guest && (stat(to, &st) == 0)) {
259161f891a6SPaul Traina 		reply(550, "%s: permission denied", to);
259261f891a6SPaul Traina 		return;
259361f891a6SPaul Traina 	}
259461f891a6SPaul Traina 
2595ea022d16SRodney W. Grimes 	if (rename(from, to) < 0)
2596ea022d16SRodney W. Grimes 		perror_reply(550, "rename");
2597ea022d16SRodney W. Grimes 	else
2598ea022d16SRodney W. Grimes 		ack("RNTO");
2599ea022d16SRodney W. Grimes }
2600ea022d16SRodney W. Grimes 
2601ea022d16SRodney W. Grimes static void
2602e4bc453cSWarner Losh dolog(struct sockaddr *who)
2603ea022d16SRodney W. Grimes {
26044dd8b5abSYoshinobu Inoue 	int error;
26054dd8b5abSYoshinobu Inoue 
26064dd8b5abSYoshinobu Inoue 	realhostname_sa(remotehost, sizeof(remotehost) - 1, who, who->sa_len);
2607ea022d16SRodney W. Grimes 
2608ea022d16SRodney W. Grimes #ifdef SETPROCTITLE
2609ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
2610ea4e54b9SDavid Nugent 	if (thishost != firsthost)
2611ea4e54b9SDavid Nugent 		snprintf(proctitle, sizeof(proctitle), "%s: connected (to %s)",
2612ea4e54b9SDavid Nugent 			 remotehost, hostname);
2613ea4e54b9SDavid Nugent 	else
2614ea4e54b9SDavid Nugent #endif
2615ea4e54b9SDavid Nugent 		snprintf(proctitle, sizeof(proctitle), "%s: connected",
2616ea4e54b9SDavid Nugent 			 remotehost);
2617b63e1fe2SPeter Wemm 	setproctitle("%s", proctitle);
2618ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */
2619ea022d16SRodney W. Grimes 
2620ea4e54b9SDavid Nugent 	if (logging) {
2621ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
2622ea4e54b9SDavid Nugent 		if (thishost != firsthost)
2623ea4e54b9SDavid Nugent 			syslog(LOG_INFO, "connection from %s (to %s)",
2624ea4e54b9SDavid Nugent 			       remotehost, hostname);
2625ea4e54b9SDavid Nugent 		else
2626ea4e54b9SDavid Nugent #endif
26274dd8b5abSYoshinobu Inoue 		{
26284dd8b5abSYoshinobu Inoue 			char	who_name[MAXHOSTNAMELEN];
26294dd8b5abSYoshinobu Inoue 
26304dd8b5abSYoshinobu Inoue 			error = getnameinfo(who, who->sa_len,
26314dd8b5abSYoshinobu Inoue 					    who_name, sizeof(who_name) - 1,
2632b0f06defSHajimu UMEMOTO 					    NULL, 0, NI_NUMERICHOST);
2633f5c57d05SEivind Eklund 			syslog(LOG_INFO, "connection from %s (%s)", remotehost,
26344dd8b5abSYoshinobu Inoue 			       error == 0 ? who_name : "");
26354dd8b5abSYoshinobu Inoue 		}
2636ea022d16SRodney W. Grimes 	}
2637ea4e54b9SDavid Nugent }
2638ea022d16SRodney W. Grimes 
2639ea022d16SRodney W. Grimes /*
2640ea022d16SRodney W. Grimes  * Record logout in wtmp file
2641ea022d16SRodney W. Grimes  * and exit with supplied status.
2642ea022d16SRodney W. Grimes  */
2643ea022d16SRodney W. Grimes void
2644e4bc453cSWarner Losh dologout(int status)
2645ea022d16SRodney W. Grimes {
26460b4df2eeSDavid Greenman 	/*
26470b4df2eeSDavid Greenman 	 * Prevent reception of SIGURG from resulting in a resumption
26480b4df2eeSDavid Greenman 	 * back to the main program loop.
26490b4df2eeSDavid Greenman 	 */
26500b4df2eeSDavid Greenman 	transflag = 0;
2651ea022d16SRodney W. Grimes 
26525d7e0128SYaroslav Tykhiy 	if (logged_in && dowtmp) {
265352e7ee74SYaroslav Tykhiy 		(void) seteuid(0);
265446948173SHajimu UMEMOTO 		ftpd_logwtmp(ttyline, "", NULL);
2655ea022d16SRodney W. Grimes 	}
2656ea022d16SRodney W. Grimes 	/* beware of flushing buffers after a SIGPIPE */
2657ea022d16SRodney W. Grimes 	_exit(status);
2658ea022d16SRodney W. Grimes }
2659ea022d16SRodney W. Grimes 
2660ea022d16SRodney W. Grimes static void
2661e4bc453cSWarner Losh sigurg(int signo)
2662ea022d16SRodney W. Grimes {
26634b82fc95SYaroslav Tykhiy 
26644b82fc95SYaroslav Tykhiy 	recvurg = 1;
26654b82fc95SYaroslav Tykhiy }
26664b82fc95SYaroslav Tykhiy 
26674b82fc95SYaroslav Tykhiy static void
2668e4bc453cSWarner Losh myoob(void)
26694b82fc95SYaroslav Tykhiy {
2670ea022d16SRodney W. Grimes 	char *cp;
2671ea022d16SRodney W. Grimes 
2672ea022d16SRodney W. Grimes 	/* only process if transfer occurring */
2673ea022d16SRodney W. Grimes 	if (!transflag)
2674ea022d16SRodney W. Grimes 		return;
2675ea022d16SRodney W. Grimes 	cp = tmpline;
2676ea022d16SRodney W. Grimes 	if (getline(cp, 7, stdin) == NULL) {
2677ea022d16SRodney W. Grimes 		reply(221, "You could at least say goodbye.");
2678ea022d16SRodney W. Grimes 		dologout(0);
2679ea022d16SRodney W. Grimes 	}
2680ea022d16SRodney W. Grimes 	upper(cp);
2681ea022d16SRodney W. Grimes 	if (strcmp(cp, "ABOR\r\n") == 0) {
2682ea022d16SRodney W. Grimes 		tmpline[0] = '\0';
2683ea022d16SRodney W. Grimes 		reply(426, "Transfer aborted. Data connection closed.");
2684ea022d16SRodney W. Grimes 		reply(226, "Abort successful");
2685ea022d16SRodney W. Grimes 	}
2686ea022d16SRodney W. Grimes 	if (strcmp(cp, "STAT\r\n") == 0) {
26879db4bbf3SMichael Haro 		tmpline[0] = '\0';
26880e519c96SYaroslav Tykhiy 		if (file_size != -1)
2689a57e1ef0SYaroslav Tykhiy 			reply(213, "Status: %jd of %jd bytes transferred",
2690a57e1ef0SYaroslav Tykhiy 				   (intmax_t)byte_count, (intmax_t)file_size);
2691ea022d16SRodney W. Grimes 		else
2692a57e1ef0SYaroslav Tykhiy 			reply(213, "Status: %jd bytes transferred",
2693a57e1ef0SYaroslav Tykhiy 				   (intmax_t)byte_count);
2694ea022d16SRodney W. Grimes 	}
2695ea022d16SRodney W. Grimes }
2696ea022d16SRodney W. Grimes 
2697ea022d16SRodney W. Grimes /*
2698ea022d16SRodney W. Grimes  * Note: a response of 425 is not mentioned as a possible response to
2699ea022d16SRodney W. Grimes  *	the PASV command in RFC959. However, it has been blessed as
2700ea022d16SRodney W. Grimes  *	a legitimate response by Jon Postel in a telephone conversation
2701ea022d16SRodney W. Grimes  *	with Rick Adams on 25 Jan 89.
2702ea022d16SRodney W. Grimes  */
2703ea022d16SRodney W. Grimes void
2704e4bc453cSWarner Losh passive(void)
2705ea022d16SRodney W. Grimes {
27068af7c9a3SYaroslav Tykhiy 	int len, on;
2707ea022d16SRodney W. Grimes 	char *p, *a;
2708ea022d16SRodney W. Grimes 
270961f891a6SPaul Traina 	if (pdata >= 0)		/* close old port if one set */
271061f891a6SPaul Traina 		close(pdata);
271161f891a6SPaul Traina 
27124dd8b5abSYoshinobu Inoue 	pdata = socket(ctrl_addr.su_family, SOCK_STREAM, 0);
2713ea022d16SRodney W. Grimes 	if (pdata < 0) {
2714ea022d16SRodney W. Grimes 		perror_reply(425, "Can't open passive connection");
2715ea022d16SRodney W. Grimes 		return;
2716ea022d16SRodney W. Grimes 	}
27178af7c9a3SYaroslav Tykhiy 	on = 1;
27188af7c9a3SYaroslav Tykhiy 	if (setsockopt(pdata, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
27198af7c9a3SYaroslav Tykhiy 		syslog(LOG_WARNING, "pdata setsockopt (SO_REUSEADDR): %m");
27204c450ad7SPaul Traina 
272152e7ee74SYaroslav Tykhiy 	(void) seteuid(0);
272261f891a6SPaul Traina 
2723dacc9752SPaul Traina #ifdef IP_PORTRANGE
27244dd8b5abSYoshinobu Inoue 	if (ctrl_addr.su_family == AF_INET) {
27258af7c9a3SYaroslav Tykhiy 	    on = restricted_data_ports ? IP_PORTRANGE_HIGH
2726dacc9752SPaul Traina 				       : IP_PORTRANGE_DEFAULT;
2727dacc9752SPaul Traina 
272840e9d39eSPeter Wemm 	    if (setsockopt(pdata, IPPROTO_IP, IP_PORTRANGE,
272957d4ef07SYaroslav Tykhiy 			    &on, sizeof(on)) < 0)
27304c450ad7SPaul Traina 		    goto pasv_error;
27314c450ad7SPaul Traina 	}
2732dacc9752SPaul Traina #endif
27332db39860SNick Sayer #ifdef IPV6_PORTRANGE
27342db39860SNick Sayer 	if (ctrl_addr.su_family == AF_INET6) {
27358af7c9a3SYaroslav Tykhiy 	    on = restricted_data_ports ? IPV6_PORTRANGE_HIGH
27362db39860SNick Sayer 				       : IPV6_PORTRANGE_DEFAULT;
27372db39860SNick Sayer 
27382db39860SNick Sayer 	    if (setsockopt(pdata, IPPROTO_IPV6, IPV6_PORTRANGE,
273957d4ef07SYaroslav Tykhiy 			    &on, sizeof(on)) < 0)
27402db39860SNick Sayer 		    goto pasv_error;
27412db39860SNick Sayer 	}
27422db39860SNick Sayer #endif
274340e9d39eSPeter Wemm 
2744ea022d16SRodney W. Grimes 	pasv_addr = ctrl_addr;
27454dd8b5abSYoshinobu Inoue 	pasv_addr.su_port = 0;
27464dd8b5abSYoshinobu Inoue 	if (bind(pdata, (struct sockaddr *)&pasv_addr, pasv_addr.su_len) < 0)
2747ea022d16SRodney W. Grimes 		goto pasv_error;
274861f891a6SPaul Traina 
274952e7ee74SYaroslav Tykhiy 	(void) seteuid(pw->pw_uid);
27504c450ad7SPaul Traina 
2751ea022d16SRodney W. Grimes 	len = sizeof(pasv_addr);
2752ea022d16SRodney W. Grimes 	if (getsockname(pdata, (struct sockaddr *) &pasv_addr, &len) < 0)
2753ea022d16SRodney W. Grimes 		goto pasv_error;
2754ea022d16SRodney W. Grimes 	if (listen(pdata, 1) < 0)
2755ea022d16SRodney W. Grimes 		goto pasv_error;
27564dd8b5abSYoshinobu Inoue 	if (pasv_addr.su_family == AF_INET)
27574dd8b5abSYoshinobu Inoue 		a = (char *) &pasv_addr.su_sin.sin_addr;
27584dd8b5abSYoshinobu Inoue 	else if (pasv_addr.su_family == AF_INET6 &&
27594dd8b5abSYoshinobu Inoue 		 IN6_IS_ADDR_V4MAPPED(&pasv_addr.su_sin6.sin6_addr))
27604dd8b5abSYoshinobu Inoue 		a = (char *) &pasv_addr.su_sin6.sin6_addr.s6_addr[12];
27614dd8b5abSYoshinobu Inoue 	else
27624dd8b5abSYoshinobu Inoue 		goto pasv_error;
27634dd8b5abSYoshinobu Inoue 
27644dd8b5abSYoshinobu Inoue 	p = (char *) &pasv_addr.su_port;
2765ea022d16SRodney W. Grimes 
2766ea022d16SRodney W. Grimes #define UC(b) (((int) b) & 0xff)
2767ea022d16SRodney W. Grimes 
2768ea022d16SRodney W. Grimes 	reply(227, "Entering Passive Mode (%d,%d,%d,%d,%d,%d)", UC(a[0]),
2769ea022d16SRodney W. Grimes 		UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1]));
2770ea022d16SRodney W. Grimes 	return;
2771ea022d16SRodney W. Grimes 
2772ea022d16SRodney W. Grimes pasv_error:
277352e7ee74SYaroslav Tykhiy 	(void) seteuid(pw->pw_uid);
2774ea022d16SRodney W. Grimes 	(void) close(pdata);
2775ea022d16SRodney W. Grimes 	pdata = -1;
2776ea022d16SRodney W. Grimes 	perror_reply(425, "Can't open passive connection");
2777ea022d16SRodney W. Grimes 	return;
2778ea022d16SRodney W. Grimes }
2779ea022d16SRodney W. Grimes 
2780ea022d16SRodney W. Grimes /*
27814dd8b5abSYoshinobu Inoue  * Long Passive defined in RFC 1639.
27824dd8b5abSYoshinobu Inoue  *     228 Entering Long Passive Mode
27834dd8b5abSYoshinobu Inoue  *         (af, hal, h1, h2, h3,..., pal, p1, p2...)
27844dd8b5abSYoshinobu Inoue  */
27854dd8b5abSYoshinobu Inoue 
27864dd8b5abSYoshinobu Inoue void
2787e4bc453cSWarner Losh long_passive(char *cmd, int pf)
27884dd8b5abSYoshinobu Inoue {
27898af7c9a3SYaroslav Tykhiy 	int len, on;
27904dd8b5abSYoshinobu Inoue 	char *p, *a;
27914dd8b5abSYoshinobu Inoue 
27924dd8b5abSYoshinobu Inoue 	if (pdata >= 0)		/* close old port if one set */
27934dd8b5abSYoshinobu Inoue 		close(pdata);
27944dd8b5abSYoshinobu Inoue 
27954dd8b5abSYoshinobu Inoue 	if (pf != PF_UNSPEC) {
27964dd8b5abSYoshinobu Inoue 		if (ctrl_addr.su_family != pf) {
27974dd8b5abSYoshinobu Inoue 			switch (ctrl_addr.su_family) {
27984dd8b5abSYoshinobu Inoue 			case AF_INET:
27994dd8b5abSYoshinobu Inoue 				pf = 1;
28004dd8b5abSYoshinobu Inoue 				break;
28014dd8b5abSYoshinobu Inoue 			case AF_INET6:
28024dd8b5abSYoshinobu Inoue 				pf = 2;
28034dd8b5abSYoshinobu Inoue 				break;
28044dd8b5abSYoshinobu Inoue 			default:
28054dd8b5abSYoshinobu Inoue 				pf = 0;
28064dd8b5abSYoshinobu Inoue 				break;
28074dd8b5abSYoshinobu Inoue 			}
28084dd8b5abSYoshinobu Inoue 			/*
28094dd8b5abSYoshinobu Inoue 			 * XXX
28104dd8b5abSYoshinobu Inoue 			 * only EPRT/EPSV ready clients will understand this
28114dd8b5abSYoshinobu Inoue 			 */
28124dd8b5abSYoshinobu Inoue 			if (strcmp(cmd, "EPSV") == 0 && pf) {
28134dd8b5abSYoshinobu Inoue 				reply(522, "Network protocol mismatch, "
28144dd8b5abSYoshinobu Inoue 					"use (%d)", pf);
28154dd8b5abSYoshinobu Inoue 			} else
28164dd8b5abSYoshinobu Inoue 				reply(501, "Network protocol mismatch"); /*XXX*/
28174dd8b5abSYoshinobu Inoue 
28184dd8b5abSYoshinobu Inoue 			return;
28194dd8b5abSYoshinobu Inoue 		}
28204dd8b5abSYoshinobu Inoue 	}
28214dd8b5abSYoshinobu Inoue 
28224dd8b5abSYoshinobu Inoue 	pdata = socket(ctrl_addr.su_family, SOCK_STREAM, 0);
28234dd8b5abSYoshinobu Inoue 	if (pdata < 0) {
28244dd8b5abSYoshinobu Inoue 		perror_reply(425, "Can't open passive connection");
28254dd8b5abSYoshinobu Inoue 		return;
28264dd8b5abSYoshinobu Inoue 	}
28278af7c9a3SYaroslav Tykhiy 	on = 1;
28288af7c9a3SYaroslav Tykhiy 	if (setsockopt(pdata, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
28298af7c9a3SYaroslav Tykhiy 		syslog(LOG_WARNING, "pdata setsockopt (SO_REUSEADDR): %m");
28304dd8b5abSYoshinobu Inoue 
283152e7ee74SYaroslav Tykhiy 	(void) seteuid(0);
28324dd8b5abSYoshinobu Inoue 
28334dd8b5abSYoshinobu Inoue 	pasv_addr = ctrl_addr;
28344dd8b5abSYoshinobu Inoue 	pasv_addr.su_port = 0;
28354dd8b5abSYoshinobu Inoue 	len = pasv_addr.su_len;
28364dd8b5abSYoshinobu Inoue 
28372db39860SNick Sayer #ifdef IP_PORTRANGE
28382db39860SNick Sayer 	if (ctrl_addr.su_family == AF_INET) {
28398af7c9a3SYaroslav Tykhiy 	    on = restricted_data_ports ? IP_PORTRANGE_HIGH
28402db39860SNick Sayer 				       : IP_PORTRANGE_DEFAULT;
28412db39860SNick Sayer 
28422db39860SNick Sayer 	    if (setsockopt(pdata, IPPROTO_IP, IP_PORTRANGE,
284357d4ef07SYaroslav Tykhiy 			    &on, sizeof(on)) < 0)
28442db39860SNick Sayer 		    goto pasv_error;
28452db39860SNick Sayer 	}
28462db39860SNick Sayer #endif
28472db39860SNick Sayer #ifdef IPV6_PORTRANGE
28482db39860SNick Sayer 	if (ctrl_addr.su_family == AF_INET6) {
28498af7c9a3SYaroslav Tykhiy 	    on = restricted_data_ports ? IPV6_PORTRANGE_HIGH
28502db39860SNick Sayer 				       : IPV6_PORTRANGE_DEFAULT;
28512db39860SNick Sayer 
28522db39860SNick Sayer 	    if (setsockopt(pdata, IPPROTO_IPV6, IPV6_PORTRANGE,
285357d4ef07SYaroslav Tykhiy 			    &on, sizeof(on)) < 0)
28542db39860SNick Sayer 		    goto pasv_error;
28552db39860SNick Sayer 	}
28562db39860SNick Sayer #endif
28572db39860SNick Sayer 
28584dd8b5abSYoshinobu Inoue 	if (bind(pdata, (struct sockaddr *)&pasv_addr, len) < 0)
28594dd8b5abSYoshinobu Inoue 		goto pasv_error;
28604dd8b5abSYoshinobu Inoue 
286152e7ee74SYaroslav Tykhiy 	(void) seteuid(pw->pw_uid);
28624dd8b5abSYoshinobu Inoue 
28634dd8b5abSYoshinobu Inoue 	if (getsockname(pdata, (struct sockaddr *) &pasv_addr, &len) < 0)
28644dd8b5abSYoshinobu Inoue 		goto pasv_error;
28654dd8b5abSYoshinobu Inoue 	if (listen(pdata, 1) < 0)
28664dd8b5abSYoshinobu Inoue 		goto pasv_error;
28674dd8b5abSYoshinobu Inoue 
28684dd8b5abSYoshinobu Inoue #define UC(b) (((int) b) & 0xff)
28694dd8b5abSYoshinobu Inoue 
28704dd8b5abSYoshinobu Inoue 	if (strcmp(cmd, "LPSV") == 0) {
28714dd8b5abSYoshinobu Inoue 		p = (char *)&pasv_addr.su_port;
28724dd8b5abSYoshinobu Inoue 		switch (pasv_addr.su_family) {
28734dd8b5abSYoshinobu Inoue 		case AF_INET:
28744dd8b5abSYoshinobu Inoue 			a = (char *) &pasv_addr.su_sin.sin_addr;
28754dd8b5abSYoshinobu Inoue 		v4_reply:
28764dd8b5abSYoshinobu Inoue 			reply(228,
28774dd8b5abSYoshinobu Inoue "Entering Long Passive Mode (%d,%d,%d,%d,%d,%d,%d,%d,%d)",
28784dd8b5abSYoshinobu Inoue 			      4, 4, UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
28794dd8b5abSYoshinobu Inoue 			      2, UC(p[0]), UC(p[1]));
28804dd8b5abSYoshinobu Inoue 			return;
28814dd8b5abSYoshinobu Inoue 		case AF_INET6:
28824dd8b5abSYoshinobu Inoue 			if (IN6_IS_ADDR_V4MAPPED(&pasv_addr.su_sin6.sin6_addr)) {
28834dd8b5abSYoshinobu Inoue 				a = (char *) &pasv_addr.su_sin6.sin6_addr.s6_addr[12];
28844dd8b5abSYoshinobu Inoue 				goto v4_reply;
28854dd8b5abSYoshinobu Inoue 			}
28864dd8b5abSYoshinobu Inoue 			a = (char *) &pasv_addr.su_sin6.sin6_addr;
28874dd8b5abSYoshinobu Inoue 			reply(228,
28884dd8b5abSYoshinobu Inoue "Entering Long Passive Mode "
28894dd8b5abSYoshinobu Inoue "(%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d)",
28904dd8b5abSYoshinobu Inoue 			      6, 16, UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
28914dd8b5abSYoshinobu Inoue 			      UC(a[4]), UC(a[5]), UC(a[6]), UC(a[7]),
28924dd8b5abSYoshinobu Inoue 			      UC(a[8]), UC(a[9]), UC(a[10]), UC(a[11]),
28934dd8b5abSYoshinobu Inoue 			      UC(a[12]), UC(a[13]), UC(a[14]), UC(a[15]),
28944dd8b5abSYoshinobu Inoue 			      2, UC(p[0]), UC(p[1]));
28954dd8b5abSYoshinobu Inoue 			return;
28964dd8b5abSYoshinobu Inoue 		}
28974dd8b5abSYoshinobu Inoue 	} else if (strcmp(cmd, "EPSV") == 0) {
28984dd8b5abSYoshinobu Inoue 		switch (pasv_addr.su_family) {
28994dd8b5abSYoshinobu Inoue 		case AF_INET:
29004dd8b5abSYoshinobu Inoue 		case AF_INET6:
29014dd8b5abSYoshinobu Inoue 			reply(229, "Entering Extended Passive Mode (|||%d|)",
29024dd8b5abSYoshinobu Inoue 				ntohs(pasv_addr.su_port));
29034dd8b5abSYoshinobu Inoue 			return;
29044dd8b5abSYoshinobu Inoue 		}
29054dd8b5abSYoshinobu Inoue 	} else {
29064dd8b5abSYoshinobu Inoue 		/* more proper error code? */
29074dd8b5abSYoshinobu Inoue 	}
29084dd8b5abSYoshinobu Inoue 
29094dd8b5abSYoshinobu Inoue pasv_error:
291052e7ee74SYaroslav Tykhiy 	(void) seteuid(pw->pw_uid);
29114dd8b5abSYoshinobu Inoue 	(void) close(pdata);
29124dd8b5abSYoshinobu Inoue 	pdata = -1;
29134dd8b5abSYoshinobu Inoue 	perror_reply(425, "Can't open passive connection");
29144dd8b5abSYoshinobu Inoue 	return;
29154dd8b5abSYoshinobu Inoue }
29164dd8b5abSYoshinobu Inoue 
29174dd8b5abSYoshinobu Inoue /*
2918a117c345SYaroslav Tykhiy  * Generate unique name for file with basename "local"
2919a117c345SYaroslav Tykhiy  * and open the file in order to avoid possible races.
2920a117c345SYaroslav Tykhiy  * Try "local" first, then "local.1", "local.2" etc, up to "local.99".
2921a117c345SYaroslav Tykhiy  * Return descriptor to the file, set "name" to its name.
2922a117c345SYaroslav Tykhiy  *
2923ea022d16SRodney W. Grimes  * Generates failure reply on error.
2924ea022d16SRodney W. Grimes  */
2925a117c345SYaroslav Tykhiy static int
2926a117c345SYaroslav Tykhiy guniquefd(char *local, char **name)
2927ea022d16SRodney W. Grimes {
2928ea022d16SRodney W. Grimes 	static char new[MAXPATHLEN];
2929ea022d16SRodney W. Grimes 	struct stat st;
2930ea022d16SRodney W. Grimes 	char *cp;
2931a117c345SYaroslav Tykhiy 	int count;
2932a117c345SYaroslav Tykhiy 	int fd;
2933ea022d16SRodney W. Grimes 
2934ea022d16SRodney W. Grimes 	cp = strrchr(local, '/');
2935ea022d16SRodney W. Grimes 	if (cp)
2936ea022d16SRodney W. Grimes 		*cp = '\0';
2937ea022d16SRodney W. Grimes 	if (stat(cp ? local : ".", &st) < 0) {
2938ea022d16SRodney W. Grimes 		perror_reply(553, cp ? local : ".");
2939a117c345SYaroslav Tykhiy 		return (-1);
2940ea022d16SRodney W. Grimes 	}
2941a117c345SYaroslav Tykhiy 	if (cp) {
2942a117c345SYaroslav Tykhiy 		/*
2943a117c345SYaroslav Tykhiy 		 * Let not overwrite dirname with counter suffix.
2944a117c345SYaroslav Tykhiy 		 * -4 is for /nn\0
2945a117c345SYaroslav Tykhiy 		 * In this extreme case dot won't be put in front of suffix.
2946a117c345SYaroslav Tykhiy 		 */
2947a117c345SYaroslav Tykhiy 		if (strlen(local) > sizeof(new) - 4) {
2948a117c345SYaroslav Tykhiy 			reply(553, "Pathname too long");
2949a117c345SYaroslav Tykhiy 			return (-1);
2950a117c345SYaroslav Tykhiy 		}
2951ea022d16SRodney W. Grimes 		*cp = '/';
2952a117c345SYaroslav Tykhiy 	}
2953e760ef2cSWarner Losh 	/* -4 is for the .nn<null> we put on the end below */
2954e760ef2cSWarner Losh 	(void) snprintf(new, sizeof(new) - 4, "%s", local);
2955ea022d16SRodney W. Grimes 	cp = new + strlen(new);
2956a117c345SYaroslav Tykhiy 	/*
2957a117c345SYaroslav Tykhiy 	 * Don't generate dotfile unless requested explicitly.
2958a117c345SYaroslav Tykhiy 	 * This covers the case when basename gets truncated off
2959a117c345SYaroslav Tykhiy 	 * by buffer size.
2960a117c345SYaroslav Tykhiy 	 */
2961a117c345SYaroslav Tykhiy 	if (cp > new && cp[-1] != '/')
2962ea022d16SRodney W. Grimes 		*cp++ = '.';
2963a117c345SYaroslav Tykhiy 	for (count = 0; count < 100; count++) {
2964a117c345SYaroslav Tykhiy 		/* At count 0 try unmodified name */
2965a117c345SYaroslav Tykhiy 		if (count)
2966ea022d16SRodney W. Grimes 			(void)sprintf(cp, "%d", count);
2967a117c345SYaroslav Tykhiy 		if ((fd = open(count ? new : local,
2968a117c345SYaroslav Tykhiy 		    O_RDWR | O_CREAT | O_EXCL, 0666)) >= 0) {
2969a117c345SYaroslav Tykhiy 			*name = count ? new : local;
2970a117c345SYaroslav Tykhiy 			return (fd);
2971a117c345SYaroslav Tykhiy 		}
297288b70721SYaroslav Tykhiy 		if (errno != EEXIST) {
297350618d61SYaroslav Tykhiy 			perror_reply(553, count ? new : local);
297488b70721SYaroslav Tykhiy 			return (-1);
297588b70721SYaroslav Tykhiy 		}
2976ea022d16SRodney W. Grimes 	}
2977ea022d16SRodney W. Grimes 	reply(452, "Unique file name cannot be created.");
2978a117c345SYaroslav Tykhiy 	return (-1);
2979ea022d16SRodney W. Grimes }
2980ea022d16SRodney W. Grimes 
2981ea022d16SRodney W. Grimes /*
2982ea022d16SRodney W. Grimes  * Format and send reply containing system error number.
2983ea022d16SRodney W. Grimes  */
2984ea022d16SRodney W. Grimes void
2985e4bc453cSWarner Losh perror_reply(int code, char *string)
2986ea022d16SRodney W. Grimes {
2987ea022d16SRodney W. Grimes 
2988ea022d16SRodney W. Grimes 	reply(code, "%s: %s.", string, strerror(errno));
2989ea022d16SRodney W. Grimes }
2990ea022d16SRodney W. Grimes 
2991ea022d16SRodney W. Grimes static char *onefile[] = {
2992ea022d16SRodney W. Grimes 	"",
2993ea022d16SRodney W. Grimes 	0
2994ea022d16SRodney W. Grimes };
2995ea022d16SRodney W. Grimes 
2996ea022d16SRodney W. Grimes void
2997e4bc453cSWarner Losh send_file_list(char *whichf)
2998ea022d16SRodney W. Grimes {
2999ea022d16SRodney W. Grimes 	struct stat st;
3000ea022d16SRodney W. Grimes 	DIR *dirp = NULL;
3001ea022d16SRodney W. Grimes 	struct dirent *dir;
3002ea022d16SRodney W. Grimes 	FILE *dout = NULL;
3003ea022d16SRodney W. Grimes 	char **dirlist, *dirname;
3004ea022d16SRodney W. Grimes 	int simple = 0;
3005ea022d16SRodney W. Grimes 	int freeglob = 0;
3006ea022d16SRodney W. Grimes 	glob_t gl;
3007ea022d16SRodney W. Grimes 
3008ea022d16SRodney W. Grimes 	if (strpbrk(whichf, "~{[*?") != NULL) {
300912da320bSMike Heffner 		int flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_TILDE;
3010ea022d16SRodney W. Grimes 
3011ea022d16SRodney W. Grimes 		memset(&gl, 0, sizeof(gl));
30126d10cb2fSJonathan Lemon 		gl.gl_matchc = MAXGLOBARGS;
301375dc5f1aSMike Heffner 		flags |= GLOB_LIMIT;
3014ea022d16SRodney W. Grimes 		freeglob = 1;
3015ea022d16SRodney W. Grimes 		if (glob(whichf, flags, 0, &gl)) {
3016ea022d16SRodney W. Grimes 			reply(550, "not found");
3017ea022d16SRodney W. Grimes 			goto out;
3018ea022d16SRodney W. Grimes 		} else if (gl.gl_pathc == 0) {
3019ea022d16SRodney W. Grimes 			errno = ENOENT;
3020ea022d16SRodney W. Grimes 			perror_reply(550, whichf);
3021ea022d16SRodney W. Grimes 			goto out;
3022ea022d16SRodney W. Grimes 		}
3023ea022d16SRodney W. Grimes 		dirlist = gl.gl_pathv;
3024ea022d16SRodney W. Grimes 	} else {
3025ea022d16SRodney W. Grimes 		onefile[0] = whichf;
3026ea022d16SRodney W. Grimes 		dirlist = onefile;
3027ea022d16SRodney W. Grimes 		simple = 1;
3028ea022d16SRodney W. Grimes 	}
3029ea022d16SRodney W. Grimes 
30309aca17cbSMark Murray 	while ((dirname = *dirlist++)) {
3031ea022d16SRodney W. Grimes 		if (stat(dirname, &st) < 0) {
3032ea022d16SRodney W. Grimes 			/*
3033ea022d16SRodney W. Grimes 			 * If user typed "ls -l", etc, and the client
3034ea022d16SRodney W. Grimes 			 * used NLST, do what the user meant.
3035ea022d16SRodney W. Grimes 			 */
3036ea022d16SRodney W. Grimes 			if (dirname[0] == '-' && *dirlist == NULL &&
3037ea022d16SRodney W. Grimes 			    transflag == 0) {
3038af85d782SDavid Nugent 				retrieve(_PATH_LS " %s", dirname);
3039ea022d16SRodney W. Grimes 				goto out;
3040ea022d16SRodney W. Grimes 			}
3041ea022d16SRodney W. Grimes 			perror_reply(550, whichf);
3042ea022d16SRodney W. Grimes 			if (dout != NULL) {
3043ea022d16SRodney W. Grimes 				(void) fclose(dout);
3044ea022d16SRodney W. Grimes 				transflag = 0;
3045ea022d16SRodney W. Grimes 				data = -1;
3046ea022d16SRodney W. Grimes 				pdata = -1;
3047ea022d16SRodney W. Grimes 			}
3048ea022d16SRodney W. Grimes 			goto out;
3049ea022d16SRodney W. Grimes 		}
3050ea022d16SRodney W. Grimes 
3051ea022d16SRodney W. Grimes 		if (S_ISREG(st.st_mode)) {
3052ea022d16SRodney W. Grimes 			if (dout == NULL) {
30530e519c96SYaroslav Tykhiy 				dout = dataconn("file list", -1, "w");
3054ea022d16SRodney W. Grimes 				if (dout == NULL)
3055ea022d16SRodney W. Grimes 					goto out;
3056ea022d16SRodney W. Grimes 				transflag++;
3057ea022d16SRodney W. Grimes 			}
3058ea022d16SRodney W. Grimes 			fprintf(dout, "%s%s\n", dirname,
3059ea022d16SRodney W. Grimes 				type == TYPE_A ? "\r" : "");
3060ea022d16SRodney W. Grimes 			byte_count += strlen(dirname) + 1;
3061ea022d16SRodney W. Grimes 			continue;
3062ea022d16SRodney W. Grimes 		} else if (!S_ISDIR(st.st_mode))
3063ea022d16SRodney W. Grimes 			continue;
3064ea022d16SRodney W. Grimes 
3065ea022d16SRodney W. Grimes 		if ((dirp = opendir(dirname)) == NULL)
3066ea022d16SRodney W. Grimes 			continue;
3067ea022d16SRodney W. Grimes 
3068ea022d16SRodney W. Grimes 		while ((dir = readdir(dirp)) != NULL) {
3069ea022d16SRodney W. Grimes 			char nbuf[MAXPATHLEN];
3070ea022d16SRodney W. Grimes 
30714b82fc95SYaroslav Tykhiy 			if (recvurg) {
30724b82fc95SYaroslav Tykhiy 				myoob();
30734b82fc95SYaroslav Tykhiy 				recvurg = 0;
30744b82fc95SYaroslav Tykhiy 				transflag = 0;
30754b82fc95SYaroslav Tykhiy 				goto out;
30764b82fc95SYaroslav Tykhiy 			}
30774b82fc95SYaroslav Tykhiy 
3078ea022d16SRodney W. Grimes 			if (dir->d_name[0] == '.' && dir->d_namlen == 1)
3079ea022d16SRodney W. Grimes 				continue;
3080ea022d16SRodney W. Grimes 			if (dir->d_name[0] == '.' && dir->d_name[1] == '.' &&
3081ea022d16SRodney W. Grimes 			    dir->d_namlen == 2)
3082ea022d16SRodney W. Grimes 				continue;
3083ea022d16SRodney W. Grimes 
3084e760ef2cSWarner Losh 			snprintf(nbuf, sizeof(nbuf),
3085e760ef2cSWarner Losh 				"%s/%s", dirname, dir->d_name);
3086ea022d16SRodney W. Grimes 
3087ea022d16SRodney W. Grimes 			/*
3088ea022d16SRodney W. Grimes 			 * We have to do a stat to insure it's
3089ea022d16SRodney W. Grimes 			 * not a directory or special file.
3090ea022d16SRodney W. Grimes 			 */
3091ea022d16SRodney W. Grimes 			if (simple || (stat(nbuf, &st) == 0 &&
3092ea022d16SRodney W. Grimes 			    S_ISREG(st.st_mode))) {
3093ea022d16SRodney W. Grimes 				if (dout == NULL) {
30940e519c96SYaroslav Tykhiy 					dout = dataconn("file list", -1, "w");
3095ea022d16SRodney W. Grimes 					if (dout == NULL)
3096ea022d16SRodney W. Grimes 						goto out;
3097ea022d16SRodney W. Grimes 					transflag++;
3098ea022d16SRodney W. Grimes 				}
3099ea022d16SRodney W. Grimes 				if (nbuf[0] == '.' && nbuf[1] == '/')
3100ea022d16SRodney W. Grimes 					fprintf(dout, "%s%s\n", &nbuf[2],
3101ea022d16SRodney W. Grimes 						type == TYPE_A ? "\r" : "");
3102ea022d16SRodney W. Grimes 				else
3103ea022d16SRodney W. Grimes 					fprintf(dout, "%s%s\n", nbuf,
3104ea022d16SRodney W. Grimes 						type == TYPE_A ? "\r" : "");
3105ea022d16SRodney W. Grimes 				byte_count += strlen(nbuf) + 1;
3106ea022d16SRodney W. Grimes 			}
3107ea022d16SRodney W. Grimes 		}
3108ea022d16SRodney W. Grimes 		(void) closedir(dirp);
3109ea022d16SRodney W. Grimes 	}
3110ea022d16SRodney W. Grimes 
3111ea022d16SRodney W. Grimes 	if (dout == NULL)
3112ea022d16SRodney W. Grimes 		reply(550, "No files found.");
3113ea022d16SRodney W. Grimes 	else if (ferror(dout) != 0)
3114ea022d16SRodney W. Grimes 		perror_reply(550, "Data connection");
3115ea022d16SRodney W. Grimes 	else
3116ea022d16SRodney W. Grimes 		reply(226, "Transfer complete.");
3117ea022d16SRodney W. Grimes 
3118ea022d16SRodney W. Grimes 	transflag = 0;
3119ea022d16SRodney W. Grimes 	if (dout != NULL)
3120ea022d16SRodney W. Grimes 		(void) fclose(dout);
3121ea022d16SRodney W. Grimes 	data = -1;
3122ea022d16SRodney W. Grimes 	pdata = -1;
3123ea022d16SRodney W. Grimes out:
3124ea022d16SRodney W. Grimes 	if (freeglob) {
3125ea022d16SRodney W. Grimes 		freeglob = 0;
3126ea022d16SRodney W. Grimes 		globfree(&gl);
3127ea022d16SRodney W. Grimes 	}
3128ea022d16SRodney W. Grimes }
3129ea022d16SRodney W. Grimes 
3130cf09a206SDavid Greenman void
3131e4bc453cSWarner Losh reapchild(int signo)
3132cf09a206SDavid Greenman {
3133de9b6c03SYaroslav Tykhiy 	while (waitpid(-1, NULL, WNOHANG) > 0);
3134cf09a206SDavid Greenman }
3135cf09a206SDavid Greenman 
3136b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE
3137ea022d16SRodney W. Grimes /*
3138ea022d16SRodney W. Grimes  * Clobber argv so ps will show what we're doing.  (Stolen from sendmail.)
3139ea022d16SRodney W. Grimes  * Warning, since this is usually started from inetd.conf, it often doesn't
3140ea022d16SRodney W. Grimes  * have much of an environment or arglist to overwrite.
3141ea022d16SRodney W. Grimes  */
3142ea022d16SRodney W. Grimes void
3143ea022d16SRodney W. Grimes setproctitle(const char *fmt, ...)
3144ea022d16SRodney W. Grimes {
3145ea022d16SRodney W. Grimes 	int i;
3146ea022d16SRodney W. Grimes 	va_list ap;
3147ea022d16SRodney W. Grimes 	char *p, *bp, ch;
3148ea022d16SRodney W. Grimes 	char buf[LINE_MAX];
3149ea022d16SRodney W. Grimes 
3150ea022d16SRodney W. Grimes 	va_start(ap, fmt);
3151ea022d16SRodney W. Grimes 	(void)vsnprintf(buf, sizeof(buf), fmt, ap);
3152ea022d16SRodney W. Grimes 
3153ea022d16SRodney W. Grimes 	/* make ps print our process name */
3154ea022d16SRodney W. Grimes 	p = Argv[0];
3155ea022d16SRodney W. Grimes 	*p++ = '-';
3156ea022d16SRodney W. Grimes 
3157ea022d16SRodney W. Grimes 	i = strlen(buf);
3158ea022d16SRodney W. Grimes 	if (i > LastArgv - p - 2) {
3159ea022d16SRodney W. Grimes 		i = LastArgv - p - 2;
3160ea022d16SRodney W. Grimes 		buf[i] = '\0';
3161ea022d16SRodney W. Grimes 	}
3162ea022d16SRodney W. Grimes 	bp = buf;
3163ea022d16SRodney W. Grimes 	while (ch = *bp++)
3164ea022d16SRodney W. Grimes 		if (ch != '\n' && ch != '\r')
3165ea022d16SRodney W. Grimes 			*p++ = ch;
3166ea022d16SRodney W. Grimes 	while (p < LastArgv)
3167ea022d16SRodney W. Grimes 		*p++ = ' ';
3168ea022d16SRodney W. Grimes }
3169b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */
31703eb568f2SGuido van Rooij 
317161f891a6SPaul Traina static void
3172e4bc453cSWarner Losh logxfer(char *name, off_t size, time_t start)
31733eb568f2SGuido van Rooij {
31748c1c21f2SYaroslav Tykhiy 	char buf[MAXPATHLEN + 1024];
31753eb568f2SGuido van Rooij 	char path[MAXPATHLEN + 1];
3176158a00b2SJohn Birrell 	time_t now;
31773eb568f2SGuido van Rooij 
31788c1c21f2SYaroslav Tykhiy 	if (statfd >= 0) {
31793eb568f2SGuido van Rooij 		time(&now);
31808c1c21f2SYaroslav Tykhiy 		if (realpath(name, path) == NULL) {
31818c1c21f2SYaroslav Tykhiy 			syslog(LOG_NOTICE, "realpath failed on %s: %m", path);
31828c1c21f2SYaroslav Tykhiy 			return;
31838c1c21f2SYaroslav Tykhiy 		}
31848c1c21f2SYaroslav Tykhiy 		snprintf(buf, sizeof(buf), "%.20s!%s!%s!%s!%jd!%ld\n",
31853eb568f2SGuido van Rooij 			ctime(&now)+4, ident, remotehost,
31868c1c21f2SYaroslav Tykhiy 			path, (intmax_t)size,
3187e4a71114SAndrey A. Chernov 			(long)(now - start + (now == start)));
31883eb568f2SGuido van Rooij 		write(statfd, buf, strlen(buf));
31893eb568f2SGuido van Rooij 	}
31903eb568f2SGuido van Rooij }
3191e4648f05SYaroslav Tykhiy 
3192e4648f05SYaroslav Tykhiy static char *
3193e4648f05SYaroslav Tykhiy doublequote(char *s)
3194e4648f05SYaroslav Tykhiy {
3195e4648f05SYaroslav Tykhiy 	int n;
3196e4648f05SYaroslav Tykhiy 	char *p, *s2;
3197e4648f05SYaroslav Tykhiy 
3198e4648f05SYaroslav Tykhiy 	for (p = s, n = 0; *p; p++)
3199e4648f05SYaroslav Tykhiy 		if (*p == '"')
3200e4648f05SYaroslav Tykhiy 			n++;
3201e4648f05SYaroslav Tykhiy 
3202e4648f05SYaroslav Tykhiy 	if ((s2 = malloc(p - s + n + 1)) == NULL)
3203e4648f05SYaroslav Tykhiy 		return (NULL);
3204e4648f05SYaroslav Tykhiy 
3205e4648f05SYaroslav Tykhiy 	for (p = s2; *s; s++, p++) {
3206e4648f05SYaroslav Tykhiy 		if ((*p = *s) == '"')
3207e4648f05SYaroslav Tykhiy 			*(++p) = '"';
3208e4648f05SYaroslav Tykhiy 	}
3209e4648f05SYaroslav Tykhiy 	*p = '\0';
3210e4648f05SYaroslav Tykhiy 
3211e4648f05SYaroslav Tykhiy 	return (s2);
3212e4648f05SYaroslav Tykhiy }
3213206fe568SHajimu UMEMOTO 
3214206fe568SHajimu UMEMOTO /* setup server socket for specified address family */
3215206fe568SHajimu UMEMOTO /* if af is PF_UNSPEC more than one socket may be returned */
3216206fe568SHajimu UMEMOTO /* the returned list is dynamically allocated, so caller needs to free it */
3217206fe568SHajimu UMEMOTO static int *
3218206fe568SHajimu UMEMOTO socksetup(int af, char *bindname, const char *bindport)
3219206fe568SHajimu UMEMOTO {
3220206fe568SHajimu UMEMOTO 	struct addrinfo hints, *res, *r;
3221206fe568SHajimu UMEMOTO 	int error, maxs, *s, *socks;
3222206fe568SHajimu UMEMOTO 	const int on = 1;
3223206fe568SHajimu UMEMOTO 
3224206fe568SHajimu UMEMOTO 	memset(&hints, 0, sizeof(hints));
3225206fe568SHajimu UMEMOTO 	hints.ai_flags = AI_PASSIVE;
3226206fe568SHajimu UMEMOTO 	hints.ai_family = af;
3227206fe568SHajimu UMEMOTO 	hints.ai_socktype = SOCK_STREAM;
3228206fe568SHajimu UMEMOTO 	error = getaddrinfo(bindname, bindport, &hints, &res);
3229206fe568SHajimu UMEMOTO 	if (error) {
3230206fe568SHajimu UMEMOTO 		syslog(LOG_ERR, "%s", gai_strerror(error));
3231206fe568SHajimu UMEMOTO 		if (error == EAI_SYSTEM)
3232206fe568SHajimu UMEMOTO 			syslog(LOG_ERR, "%s", strerror(errno));
3233206fe568SHajimu UMEMOTO 		return NULL;
3234206fe568SHajimu UMEMOTO 	}
3235206fe568SHajimu UMEMOTO 
3236206fe568SHajimu UMEMOTO 	/* Count max number of sockets we may open */
3237206fe568SHajimu UMEMOTO 	for (maxs = 0, r = res; r; r = r->ai_next, maxs++)
3238206fe568SHajimu UMEMOTO 		;
3239206fe568SHajimu UMEMOTO 	socks = malloc((maxs + 1) * sizeof(int));
3240206fe568SHajimu UMEMOTO 	if (!socks) {
3241206fe568SHajimu UMEMOTO 		freeaddrinfo(res);
3242206fe568SHajimu UMEMOTO 		syslog(LOG_ERR, "couldn't allocate memory for sockets");
3243206fe568SHajimu UMEMOTO 		return NULL;
3244206fe568SHajimu UMEMOTO 	}
3245206fe568SHajimu UMEMOTO 
3246206fe568SHajimu UMEMOTO 	*socks = 0;   /* num of sockets counter at start of array */
3247206fe568SHajimu UMEMOTO 	s = socks + 1;
3248206fe568SHajimu UMEMOTO 	for (r = res; r; r = r->ai_next) {
3249206fe568SHajimu UMEMOTO 		*s = socket(r->ai_family, r->ai_socktype, r->ai_protocol);
3250206fe568SHajimu UMEMOTO 		if (*s < 0) {
3251206fe568SHajimu UMEMOTO 			syslog(LOG_DEBUG, "control socket: %m");
3252206fe568SHajimu UMEMOTO 			continue;
3253206fe568SHajimu UMEMOTO 		}
3254206fe568SHajimu UMEMOTO 		if (setsockopt(*s, SOL_SOCKET, SO_REUSEADDR,
3255206fe568SHajimu UMEMOTO 		    &on, sizeof(on)) < 0)
3256206fe568SHajimu UMEMOTO 			syslog(LOG_WARNING,
3257206fe568SHajimu UMEMOTO 			    "control setsockopt (SO_REUSEADDR): %m");
3258206fe568SHajimu UMEMOTO 		if (r->ai_family == AF_INET6) {
3259206fe568SHajimu UMEMOTO 			if (setsockopt(*s, IPPROTO_IPV6, IPV6_V6ONLY,
3260206fe568SHajimu UMEMOTO 			    &on, sizeof(on)) < 0)
3261206fe568SHajimu UMEMOTO 				syslog(LOG_WARNING,
3262206fe568SHajimu UMEMOTO 				    "control setsockopt (IPV6_V6ONLY): %m");
3263206fe568SHajimu UMEMOTO 		}
3264206fe568SHajimu UMEMOTO 		if (bind(*s, r->ai_addr, r->ai_addrlen) < 0) {
3265206fe568SHajimu UMEMOTO 			syslog(LOG_DEBUG, "control bind: %m");
3266206fe568SHajimu UMEMOTO 			close(*s);
3267206fe568SHajimu UMEMOTO 			continue;
3268206fe568SHajimu UMEMOTO 		}
3269206fe568SHajimu UMEMOTO 		(*socks)++;
3270206fe568SHajimu UMEMOTO 		s++;
3271206fe568SHajimu UMEMOTO 	}
3272206fe568SHajimu UMEMOTO 
3273206fe568SHajimu UMEMOTO 	if (res)
3274206fe568SHajimu UMEMOTO 		freeaddrinfo(res);
3275206fe568SHajimu UMEMOTO 
3276206fe568SHajimu UMEMOTO 	if (*socks == 0) {
3277206fe568SHajimu UMEMOTO 		syslog(LOG_ERR, "control socket: Couldn't bind to any socket");
3278206fe568SHajimu UMEMOTO 		free(socks);
3279206fe568SHajimu UMEMOTO 		return NULL;
3280206fe568SHajimu UMEMOTO 	}
3281206fe568SHajimu UMEMOTO 	return(socks);
3282206fe568SHajimu UMEMOTO }
3283