xref: /freebsd/libexec/ftpd/ftpd.c (revision 93bd9dc528f8811d81f61a6a6c0309826d94e40c)
1ea022d16SRodney W. Grimes /*
2ea022d16SRodney W. Grimes  * Copyright (c) 1985, 1988, 1990, 1992, 1993, 1994
3ea022d16SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4ea022d16SRodney W. Grimes  *
5ea022d16SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6ea022d16SRodney W. Grimes  * modification, are permitted provided that the following conditions
7ea022d16SRodney W. Grimes  * are met:
8ea022d16SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9ea022d16SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10ea022d16SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11ea022d16SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12ea022d16SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13ea022d16SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
14ea022d16SRodney W. Grimes  *    must display the following acknowledgement:
15ea022d16SRodney W. Grimes  *	This product includes software developed by the University of
16ea022d16SRodney W. Grimes  *	California, Berkeley and its contributors.
17ea022d16SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
18ea022d16SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
19ea022d16SRodney W. Grimes  *    without specific prior written permission.
20ea022d16SRodney W. Grimes  *
21ea022d16SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22ea022d16SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23ea022d16SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24ea022d16SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25ea022d16SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26ea022d16SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27ea022d16SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28ea022d16SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29ea022d16SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30ea022d16SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31ea022d16SRodney W. Grimes  * SUCH DAMAGE.
32ea022d16SRodney W. Grimes  */
33ea022d16SRodney W. Grimes 
349aca17cbSMark Murray #if 0
35ea022d16SRodney W. Grimes #ifndef lint
36ea022d16SRodney W. Grimes static char copyright[] =
37ea022d16SRodney W. Grimes "@(#) Copyright (c) 1985, 1988, 1990, 1992, 1993, 1994\n\
38ea022d16SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
39ea022d16SRodney W. Grimes #endif /* not lint */
409aca17cbSMark Murray #endif
41ea022d16SRodney W. Grimes 
42ea022d16SRodney W. Grimes #ifndef lint
43e02897faSPhilippe Charnier #if 0
44ea022d16SRodney W. Grimes static char sccsid[] = "@(#)ftpd.c	8.4 (Berkeley) 4/16/94";
459aca17cbSMark Murray #endif
46e02897faSPhilippe Charnier static const char rcsid[] =
477f3dea24SPeter Wemm   "$FreeBSD$";
48e02897faSPhilippe Charnier #endif /* not lint */
49ea022d16SRodney W. Grimes 
50ea022d16SRodney W. Grimes /*
51ea022d16SRodney W. Grimes  * FTP server.
52ea022d16SRodney W. Grimes  */
53ea022d16SRodney W. Grimes #include <sys/param.h>
54ea022d16SRodney W. Grimes #include <sys/ioctl.h>
559fc5823aSGarrett Wollman #include <sys/mman.h>
56eb2fc780SGarrett Wollman #include <sys/socket.h>
57eb2fc780SGarrett Wollman #include <sys/stat.h>
58eb2fc780SGarrett Wollman #include <sys/time.h>
59eb2fc780SGarrett Wollman #include <sys/wait.h>
60ea022d16SRodney W. Grimes 
61ea022d16SRodney W. Grimes #include <netinet/in.h>
62ea022d16SRodney W. Grimes #include <netinet/in_systm.h>
63ea022d16SRodney W. Grimes #include <netinet/ip.h>
649fc5823aSGarrett Wollman #include <netinet/tcp.h>
65ea022d16SRodney W. Grimes 
66ea022d16SRodney W. Grimes #define	FTP_NAMES
67ea022d16SRodney W. Grimes #include <arpa/ftp.h>
68ea022d16SRodney W. Grimes #include <arpa/inet.h>
69ea022d16SRodney W. Grimes #include <arpa/telnet.h>
70ea022d16SRodney W. Grimes 
71ea022d16SRodney W. Grimes #include <ctype.h>
72ea022d16SRodney W. Grimes #include <dirent.h>
73ea022d16SRodney W. Grimes #include <err.h>
74ea022d16SRodney W. Grimes #include <errno.h>
75ea022d16SRodney W. Grimes #include <fcntl.h>
76ea022d16SRodney W. Grimes #include <glob.h>
77ea022d16SRodney W. Grimes #include <limits.h>
78ea022d16SRodney W. Grimes #include <netdb.h>
79ea022d16SRodney W. Grimes #include <pwd.h>
8031fea7b8SDavid Nugent #include <grp.h>
8147499ecdSAndrey A. Chernov #include <opie.h>
82ea022d16SRodney W. Grimes #include <signal.h>
83ea022d16SRodney W. Grimes #include <stdio.h>
84ea022d16SRodney W. Grimes #include <stdlib.h>
85ea022d16SRodney W. Grimes #include <string.h>
86ea022d16SRodney W. Grimes #include <syslog.h>
87ea022d16SRodney W. Grimes #include <time.h>
88ea022d16SRodney W. Grimes #include <unistd.h>
89b63e1fe2SPeter Wemm #include <libutil.h>
90b071c689SDavid Nugent #ifdef	LOGIN_CAP
91b071c689SDavid Nugent #include <login_cap.h>
92b071c689SDavid Nugent #endif
93ea022d16SRodney W. Grimes 
945bc9d93dSMark Murray #ifdef USE_PAM
956c9134c0SMark Murray #include <security/pam_appl.h>
966c9134c0SMark Murray #endif
976c9134c0SMark Murray 
98ea022d16SRodney W. Grimes #include "pathnames.h"
99ea022d16SRodney W. Grimes #include "extern.h"
100ea022d16SRodney W. Grimes 
101ea022d16SRodney W. Grimes #include <stdarg.h>
102ea022d16SRodney W. Grimes 
103af85d782SDavid Nugent static char version[] = "Version 6.00LS";
104af85d782SDavid Nugent #undef main
105ea022d16SRodney W. Grimes 
106ea022d16SRodney W. Grimes extern	off_t restart_point;
107ea022d16SRodney W. Grimes extern	char cbuf[];
108ea022d16SRodney W. Grimes 
1094dd8b5abSYoshinobu Inoue union sockunion server_addr;
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;
118ea022d16SRodney W. Grimes int	logged_in;
119ea022d16SRodney W. Grimes struct	passwd *pw;
120618b0bbaSMark Murray int	ftpdebug;
121ea022d16SRodney W. Grimes int	timeout = 900;    /* timeout after 15 minutes of inactivity */
122ea022d16SRodney W. Grimes int	maxtimeout = 7200;/* don't allow idle time to be set beyond 2 hours */
123ea022d16SRodney W. Grimes int	logging;
1244c450ad7SPaul Traina int	restricted_data_ports = 1;
125a5a4544eSPaul Traina int	paranoid = 1;	  /* be extra careful about security */
1265a392aecSTorsten Blum int	anon_only = 0;    /* Only anonymous ftp allowed */
127ea022d16SRodney W. Grimes int	guest;
128a5a4544eSPaul Traina int	dochroot;
1293eb568f2SGuido van Rooij int	stats;
1303eb568f2SGuido van Rooij int	statfd = -1;
131ea022d16SRodney W. Grimes int	type;
132ea022d16SRodney W. Grimes int	form;
133ea022d16SRodney W. Grimes int	stru;			/* avoid C keyword */
134ea022d16SRodney W. Grimes int	mode;
135ea022d16SRodney W. Grimes int	usedefault = 1;		/* for data transfers */
136ea022d16SRodney W. Grimes int	pdata = -1;		/* for passive mode */
137a4b77a2aSPoul-Henning Kamp int	readonly=0;		/* Server is in readonly mode.	*/
138a4b77a2aSPoul-Henning Kamp int	noepsv=0;		/* EPSV command is disabled.	*/
13962513e76SNik Clayton int	noretr=0;		/* RETR command is disabled.	*/
1401cc9f0bbSSheldon Hearn int	noguestretr=0;		/* RETR command is disabled for anon users. */
141d186bb12SMatthew N. Dodd int	noguestmkd=0;		/* MKD command is disabled for anon users. */
14262513e76SNik Clayton 
1434b82fc95SYaroslav Tykhiy static volatile sig_atomic_t recvurg;
144ea022d16SRodney W. Grimes sig_atomic_t transflag;
145ea022d16SRodney W. Grimes off_t	file_size;
146ea022d16SRodney W. Grimes off_t	byte_count;
147ea022d16SRodney W. Grimes #if !defined(CMASK) || CMASK == 0
148ea022d16SRodney W. Grimes #undef CMASK
149ea022d16SRodney W. Grimes #define CMASK 027
150ea022d16SRodney W. Grimes #endif
151ea022d16SRodney W. Grimes int	defumask = CMASK;		/* default umask value */
152ea022d16SRodney W. Grimes char	tmpline[7];
153ea4e54b9SDavid Nugent char	*hostname;
154ad442344SDima Dorfman int	epsvall = 0;
155ad442344SDima Dorfman 
1560512556aSDavid Nugent #ifdef VIRTUAL_HOSTING
157ea4e54b9SDavid Nugent char	*ftpuser;
158ea4e54b9SDavid Nugent 
159ea4e54b9SDavid Nugent static struct ftphost {
160ea4e54b9SDavid Nugent 	struct ftphost	*next;
161f38c6cadSYoshinobu Inoue 	struct addrinfo *hostinfo;
162ea4e54b9SDavid Nugent 	char		*hostname;
163ea4e54b9SDavid Nugent 	char		*anonuser;
164ea4e54b9SDavid Nugent 	char		*statfile;
165ea4e54b9SDavid Nugent 	char		*welcome;
166ea4e54b9SDavid Nugent 	char		*loginmsg;
167ea4e54b9SDavid Nugent } *thishost, *firsthost;
168ea4e54b9SDavid Nugent 
169ea4e54b9SDavid Nugent #endif
1709e9a43bdSBrian Somers char	remotehost[MAXHOSTNAMELEN];
1713eb568f2SGuido van Rooij char	*ident = NULL;
172a5a4544eSPaul Traina 
173a5a4544eSPaul Traina static char ttyline[20];
174a5a4544eSPaul Traina char	*tty = ttyline;		/* for klogin */
175a5a4544eSPaul Traina 
1765bc9d93dSMark Murray #ifdef USE_PAM
177e4bc453cSWarner Losh static int	auth_pam(struct passwd**, const char*);
1785bc9d93dSMark Murray pam_handle_t *pamh = NULL;
17947499ecdSAndrey A. Chernov #endif
180fa1746c9SMark Murray 
181fa1746c9SMark Murray static struct opie opiedata;
182fa1746c9SMark Murray static char opieprompt[OPIE_CHALLENGE_MAX+1];
18347499ecdSAndrey A. Chernov static int pwok;
1849aca17cbSMark Murray 
185105a3c98SJulian Elischer char	*pid_file = NULL;
186105a3c98SJulian Elischer 
187ea022d16SRodney W. Grimes /*
1886d10cb2fSJonathan Lemon  * Limit number of pathnames that glob can return.
1896d10cb2fSJonathan Lemon  * A limit of 0 indicates the number of pathnames is unlimited.
1906d10cb2fSJonathan Lemon  */
1916d10cb2fSJonathan Lemon #define MAXGLOBARGS	16384
1926d10cb2fSJonathan Lemon #
1936d10cb2fSJonathan Lemon 
1946d10cb2fSJonathan Lemon /*
195ea022d16SRodney W. Grimes  * Timeout intervals for retrying connections
196ea022d16SRodney W. Grimes  * to hosts that don't accept PORT cmds.  This
197ea022d16SRodney W. Grimes  * is a kludge, but given the problems with TCP...
198ea022d16SRodney W. Grimes  */
199ea022d16SRodney W. Grimes #define	SWAITMAX	90	/* wait at most 90 seconds */
200ea022d16SRodney W. Grimes #define	SWAITINT	5	/* interval between retries */
201ea022d16SRodney W. Grimes 
202ea022d16SRodney W. Grimes int	swaitmax = SWAITMAX;
203ea022d16SRodney W. Grimes int	swaitint = SWAITINT;
204ea022d16SRodney W. Grimes 
205ea022d16SRodney W. Grimes #ifdef SETPROCTITLE
206b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE
207ea022d16SRodney W. Grimes char	**Argv = NULL;		/* pointer to argument vector */
208ea022d16SRodney W. Grimes char	*LastArgv = NULL;	/* end of argv */
209b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */
210ea022d16SRodney W. Grimes char	proctitle[LINE_MAX];	/* initial part of title */
211ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */
212ea022d16SRodney W. Grimes 
213ea022d16SRodney W. Grimes #define LOGCMD(cmd, file) \
214ea022d16SRodney W. Grimes 	if (logging > 1) \
215ea022d16SRodney W. Grimes 	    syslog(LOG_INFO,"%s %s%s", cmd, \
216ea022d16SRodney W. Grimes 		*(file) == '/' ? "" : curdir(), file);
217ea022d16SRodney W. Grimes #define LOGCMD2(cmd, file1, file2) \
218ea022d16SRodney W. Grimes 	 if (logging > 1) \
219ea022d16SRodney W. Grimes 	    syslog(LOG_INFO,"%s %s%s %s%s", cmd, \
220ea022d16SRodney W. Grimes 		*(file1) == '/' ? "" : curdir(), file1, \
221ea022d16SRodney W. Grimes 		*(file2) == '/' ? "" : curdir(), file2);
222ea022d16SRodney W. Grimes #define LOGBYTES(cmd, file, cnt) \
223ea022d16SRodney W. Grimes 	if (logging > 1) { \
224ea022d16SRodney W. Grimes 		if (cnt == (off_t)-1) \
225ea022d16SRodney W. Grimes 		    syslog(LOG_INFO,"%s %s%s", cmd, \
226ea022d16SRodney W. Grimes 			*(file) == '/' ? "" : curdir(), file); \
227ea022d16SRodney W. Grimes 		else \
228ea022d16SRodney W. Grimes 		    syslog(LOG_INFO, "%s %s%s = %qd bytes", \
229ea022d16SRodney W. Grimes 			cmd, (*(file) == '/') ? "" : curdir(), file, cnt); \
230ea022d16SRodney W. Grimes 	}
231ea022d16SRodney W. Grimes 
232ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
233e4bc453cSWarner Losh static void	 inithosts(void);
234e4bc453cSWarner Losh static void	selecthost(union sockunion *);
235ea4e54b9SDavid Nugent #endif
236e4bc453cSWarner Losh static void	 ack(char *);
237e4bc453cSWarner Losh static void	 sigurg(int);
238e4bc453cSWarner Losh static void	 myoob(void);
239e4bc453cSWarner Losh static int	 checkuser(char *, char *, int);
240e4bc453cSWarner Losh static FILE	*dataconn(char *, off_t, char *);
241e4bc453cSWarner Losh static void	 dolog(struct sockaddr *);
242e4bc453cSWarner Losh static char	*curdir(void);
243e4bc453cSWarner Losh static void	 end_login(void);
244e4bc453cSWarner Losh static FILE	*getdatasock(char *);
245e4bc453cSWarner Losh static char	*gunique(char *);
246e4bc453cSWarner Losh static void	 lostconn(int);
247e4bc453cSWarner Losh static void	 sigquit(int);
248e4bc453cSWarner Losh static int	 receive_data(FILE *, FILE *);
249e4bc453cSWarner Losh static int	 send_data(FILE *, FILE *, off_t, off_t, int);
250ea022d16SRodney W. Grimes static struct passwd *
251e4bc453cSWarner Losh 		 sgetpwnam(char *);
252e4bc453cSWarner Losh static char	*sgetsave(char *);
253e4bc453cSWarner Losh static void	 reapchild(int);
254e4bc453cSWarner Losh static void      logxfer(char *, off_t, time_t);
255e4648f05SYaroslav Tykhiy static char	*doublequote(char *);
256ea022d16SRodney W. Grimes 
257ea022d16SRodney W. Grimes static char *
258e4bc453cSWarner Losh curdir(void)
259ea022d16SRodney W. Grimes {
260ea022d16SRodney W. Grimes 	static char path[MAXPATHLEN+1+1];	/* path + '/' + '\0' */
261ea022d16SRodney W. Grimes 
262ea022d16SRodney W. Grimes 	if (getcwd(path, sizeof(path)-2) == NULL)
263ea022d16SRodney W. Grimes 		return ("");
264ea022d16SRodney W. Grimes 	if (path[1] != '\0')		/* special case for root dir. */
265ea022d16SRodney W. Grimes 		strcat(path, "/");
266ea022d16SRodney W. Grimes 	/* For guest account, skip / since it's chrooted */
267ea022d16SRodney W. Grimes 	return (guest ? path+1 : path);
268ea022d16SRodney W. Grimes }
269ea022d16SRodney W. Grimes 
270ea022d16SRodney W. Grimes int
271e4bc453cSWarner Losh main(int argc, char *argv[], char **envp)
272ea022d16SRodney W. Grimes {
273ea022d16SRodney W. Grimes 	int addrlen, ch, on = 1, tos;
274ea022d16SRodney W. Grimes 	char *cp, line[LINE_MAX];
275ea022d16SRodney W. Grimes 	FILE *fd;
2764dd8b5abSYoshinobu Inoue 	int error;
2774dd8b5abSYoshinobu Inoue 	char	*bindname = NULL;
2784dd8b5abSYoshinobu Inoue 	int	family = AF_UNSPEC;
2794dd8b5abSYoshinobu Inoue 	int	enable_v4 = 0;
2804b82fc95SYaroslav Tykhiy 	struct sigaction sa;
281ea022d16SRodney W. Grimes 
28234d1ba5cSAndrey A. Chernov 	tzset();		/* in case no timezone database in ~ftp */
2834b82fc95SYaroslav Tykhiy 	sigemptyset(&sa.sa_mask);
2844b82fc95SYaroslav Tykhiy 	sa.sa_flags = SA_RESTART;
28534d1ba5cSAndrey A. Chernov 
286b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE
287ea022d16SRodney W. Grimes 	/*
288ea022d16SRodney W. Grimes 	 *  Save start and extent of argv for setproctitle.
289ea022d16SRodney W. Grimes 	 */
290ea022d16SRodney W. Grimes 	Argv = argv;
291ea022d16SRodney W. Grimes 	while (*envp)
292ea022d16SRodney W. Grimes 		envp++;
293ea022d16SRodney W. Grimes 	LastArgv = envp[-1] + strlen(envp[-1]);
294b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */
295ea022d16SRodney W. Grimes 
2963eb568f2SGuido van Rooij 
2970e063efeSYaroslav Tykhiy 	while ((ch = getopt(argc, argv, "46a:AdDElMoOp:rRSt:T:u:Uv")) != -1) {
298ea022d16SRodney W. Grimes 		switch (ch) {
2990e063efeSYaroslav Tykhiy 		case '4':
3000e063efeSYaroslav Tykhiy 			enable_v4 = 1;
3010e063efeSYaroslav Tykhiy 			if (family == AF_UNSPEC)
3020e063efeSYaroslav Tykhiy 				family = AF_INET;
3030e063efeSYaroslav Tykhiy 			break;
3040e063efeSYaroslav Tykhiy 
3050e063efeSYaroslav Tykhiy 		case '6':
3060e063efeSYaroslav Tykhiy 			family = AF_INET6;
3070e063efeSYaroslav Tykhiy 			break;
3080e063efeSYaroslav Tykhiy 
3090e063efeSYaroslav Tykhiy 		case 'a':
3100e063efeSYaroslav Tykhiy 			bindname = optarg;
3110e063efeSYaroslav Tykhiy 			break;
3120e063efeSYaroslav Tykhiy 
3130e063efeSYaroslav Tykhiy 		case 'A':
3140e063efeSYaroslav Tykhiy 			anon_only = 1;
315cf09a206SDavid Greenman 			break;
316cf09a206SDavid Greenman 
317ea022d16SRodney W. Grimes 		case 'd':
318618b0bbaSMark Murray 			ftpdebug++;
319ea022d16SRodney W. Grimes 			break;
320ea022d16SRodney W. Grimes 
3210e063efeSYaroslav Tykhiy 		case 'D':
3220e063efeSYaroslav Tykhiy 			daemon_mode++;
3230e063efeSYaroslav Tykhiy 			break;
3240e063efeSYaroslav Tykhiy 
325a4b77a2aSPoul-Henning Kamp 		case 'E':
326a4b77a2aSPoul-Henning Kamp 			noepsv = 1;
327a4b77a2aSPoul-Henning Kamp 			break;
328a4b77a2aSPoul-Henning Kamp 
329ea022d16SRodney W. Grimes 		case 'l':
330ea022d16SRodney W. Grimes 			logging++;	/* > 1 == extra logging */
331ea022d16SRodney W. Grimes 			break;
332ea022d16SRodney W. Grimes 
3330e063efeSYaroslav Tykhiy 		case 'M':
3340e063efeSYaroslav Tykhiy 			noguestmkd = 1;
3350e063efeSYaroslav Tykhiy 			break;
3360e063efeSYaroslav Tykhiy 
3370e063efeSYaroslav Tykhiy 		case 'o':
3380e063efeSYaroslav Tykhiy 			noretr = 1;
3390e063efeSYaroslav Tykhiy 			break;
3400e063efeSYaroslav Tykhiy 
3410e063efeSYaroslav Tykhiy 		case 'O':
3420e063efeSYaroslav Tykhiy 			noguestretr = 1;
3430e063efeSYaroslav Tykhiy 			break;
3440e063efeSYaroslav Tykhiy 
3450e063efeSYaroslav Tykhiy 		case 'p':
3460e063efeSYaroslav Tykhiy 			pid_file = optarg;
3470e063efeSYaroslav Tykhiy 			break;
3480e063efeSYaroslav Tykhiy 
349a4b77a2aSPoul-Henning Kamp 		case 'r':
350a4b77a2aSPoul-Henning Kamp 			readonly = 1;
351a4b77a2aSPoul-Henning Kamp 			break;
352a4b77a2aSPoul-Henning Kamp 
353a5a4544eSPaul Traina 		case 'R':
354a5a4544eSPaul Traina 			paranoid = 0;
355a5a4544eSPaul Traina 			break;
356a5a4544eSPaul Traina 
357a5a4544eSPaul Traina 		case 'S':
358a5a4544eSPaul Traina 			stats++;
359a5a4544eSPaul Traina 			break;
360a5a4544eSPaul Traina 
361ea022d16SRodney W. Grimes 		case 't':
362ea022d16SRodney W. Grimes 			timeout = atoi(optarg);
363ea022d16SRodney W. Grimes 			if (maxtimeout < timeout)
364ea022d16SRodney W. Grimes 				maxtimeout = timeout;
365ea022d16SRodney W. Grimes 			break;
366a5a4544eSPaul Traina 
3670e063efeSYaroslav Tykhiy 		case 'T':
3680e063efeSYaroslav Tykhiy 			maxtimeout = atoi(optarg);
3690e063efeSYaroslav Tykhiy 			if (timeout > maxtimeout)
3700e063efeSYaroslav Tykhiy 				timeout = maxtimeout;
371105a3c98SJulian Elischer 			break;
372105a3c98SJulian Elischer 
373ea022d16SRodney W. Grimes 		case 'u':
374ea022d16SRodney W. Grimes 		    {
375ea022d16SRodney W. Grimes 			long val = 0;
376ea022d16SRodney W. Grimes 
377ea022d16SRodney W. Grimes 			val = strtol(optarg, &optarg, 8);
378ea022d16SRodney W. Grimes 			if (*optarg != '\0' || val < 0)
379ea022d16SRodney W. Grimes 				warnx("bad value for -u");
380ea022d16SRodney W. Grimes 			else
381ea022d16SRodney W. Grimes 				defumask = val;
382ea022d16SRodney W. Grimes 			break;
383ea022d16SRodney W. Grimes 		    }
3840e063efeSYaroslav Tykhiy 		case 'U':
3850e063efeSYaroslav Tykhiy 			restricted_data_ports = 0;
3865a392aecSTorsten Blum 			break;
387ea022d16SRodney W. Grimes 
388ea022d16SRodney W. Grimes 		case 'v':
38993bd9dc5SYaroslav Tykhiy 			ftpdebug++;
390ea022d16SRodney W. Grimes 			break;
391ea022d16SRodney W. Grimes 
392ea022d16SRodney W. Grimes 		default:
393ea022d16SRodney W. Grimes 			warnx("unknown flag -%c ignored", optopt);
394ea022d16SRodney W. Grimes 			break;
395ea022d16SRodney W. Grimes 		}
396ea022d16SRodney W. Grimes 	}
397cf09a206SDavid Greenman 
398ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
399ea4e54b9SDavid Nugent 	inithosts();
400ea4e54b9SDavid Nugent #endif
401ea022d16SRodney W. Grimes 	(void) freopen(_PATH_DEVNULL, "w", stderr);
402cf09a206SDavid Greenman 
403cf09a206SDavid Greenman 	/*
404cf09a206SDavid Greenman 	 * LOG_NDELAY sets up the logging connection immediately,
405cf09a206SDavid Greenman 	 * necessary for anonymous ftp's that chroot and can't do it later.
406cf09a206SDavid Greenman 	 */
407cf09a206SDavid Greenman 	openlog("ftpd", LOG_PID | LOG_NDELAY, LOG_FTP);
408cf09a206SDavid Greenman 
409cf09a206SDavid Greenman 	if (daemon_mode) {
410cf09a206SDavid Greenman 		int ctl_sock, fd;
4114dd8b5abSYoshinobu Inoue 		struct addrinfo hints, *res;
412cf09a206SDavid Greenman 
413cf09a206SDavid Greenman 		/*
414cf09a206SDavid Greenman 		 * Detach from parent.
415cf09a206SDavid Greenman 		 */
416cf09a206SDavid Greenman 		if (daemon(1, 1) < 0) {
417cf09a206SDavid Greenman 			syslog(LOG_ERR, "failed to become a daemon");
418cf09a206SDavid Greenman 			exit(1);
419cf09a206SDavid Greenman 		}
4204b82fc95SYaroslav Tykhiy 		sa.sa_handler = reapchild;
4214b82fc95SYaroslav Tykhiy 		(void)sigaction(SIGCHLD, &sa, NULL);
4224dd8b5abSYoshinobu Inoue 		/* init bind_sa */
4234dd8b5abSYoshinobu Inoue 		memset(&hints, 0, sizeof(hints));
4244dd8b5abSYoshinobu Inoue 
4254dd8b5abSYoshinobu Inoue 		hints.ai_family = family == AF_UNSPEC ? AF_INET : family;
4264dd8b5abSYoshinobu Inoue 		hints.ai_socktype = SOCK_STREAM;
4274dd8b5abSYoshinobu Inoue 		hints.ai_protocol = 0;
4284dd8b5abSYoshinobu Inoue 		hints.ai_flags = AI_PASSIVE;
4294dd8b5abSYoshinobu Inoue 		error = getaddrinfo(bindname, "ftp", &hints, &res);
4304dd8b5abSYoshinobu Inoue 		if (error) {
4314dd8b5abSYoshinobu Inoue 			if (family == AF_UNSPEC) {
4324dd8b5abSYoshinobu Inoue 				hints.ai_family = AF_UNSPEC;
4334dd8b5abSYoshinobu Inoue 				error = getaddrinfo(bindname, "ftp", &hints,
4344dd8b5abSYoshinobu Inoue 						    &res);
4354dd8b5abSYoshinobu Inoue 			}
4364dd8b5abSYoshinobu Inoue 		}
4374dd8b5abSYoshinobu Inoue 		if (error) {
4383fb3b78fSKris Kennaway 			syslog(LOG_ERR, "%s", gai_strerror(error));
4394dd8b5abSYoshinobu Inoue 			if (error == EAI_SYSTEM)
4403fb3b78fSKris Kennaway 				syslog(LOG_ERR, "%s", strerror(errno));
4414dd8b5abSYoshinobu Inoue 			exit(1);
4424dd8b5abSYoshinobu Inoue 		}
4434dd8b5abSYoshinobu Inoue 		if (res->ai_addr == NULL) {
4444dd8b5abSYoshinobu Inoue 			syslog(LOG_ERR, "-a %s: getaddrinfo failed", hostname);
445cf09a206SDavid Greenman 			exit(1);
4462310b8c6SRuslan Ermilov 		} else
4472310b8c6SRuslan Ermilov 			family = res->ai_addr->sa_family;
448cf09a206SDavid Greenman 		/*
449cf09a206SDavid Greenman 		 * Open a socket, bind it to the FTP port, and start
450cf09a206SDavid Greenman 		 * listening.
451cf09a206SDavid Greenman 		 */
4524dd8b5abSYoshinobu Inoue 		ctl_sock = socket(family, SOCK_STREAM, 0);
453cf09a206SDavid Greenman 		if (ctl_sock < 0) {
454cf09a206SDavid Greenman 			syslog(LOG_ERR, "control socket: %m");
455cf09a206SDavid Greenman 			exit(1);
456cf09a206SDavid Greenman 		}
457cf09a206SDavid Greenman 		if (setsockopt(ctl_sock, SOL_SOCKET, SO_REUSEADDR,
45857d4ef07SYaroslav Tykhiy 		    &on, sizeof(on)) < 0)
459406d1ae9SYaroslav Tykhiy 			syslog(LOG_WARNING,
460406d1ae9SYaroslav Tykhiy 			       "control setsockopt (SO_REUSEADDR): %m");
4614dd8b5abSYoshinobu Inoue 		if (family == AF_INET6 && enable_v4 == 0) {
462fc99a00cSHajimu UMEMOTO 			if (setsockopt(ctl_sock, IPPROTO_IPV6, IPV6_V6ONLY,
46357d4ef07SYaroslav Tykhiy 				       &on, sizeof (on)) < 0)
464406d1ae9SYaroslav Tykhiy 				syslog(LOG_WARNING,
465fc99a00cSHajimu UMEMOTO 				       "control setsockopt (IPV6_V6ONLY): %m");
4664dd8b5abSYoshinobu Inoue 		}
4674dd8b5abSYoshinobu Inoue 		memcpy(&server_addr, res->ai_addr, res->ai_addr->sa_len);
4684dd8b5abSYoshinobu Inoue 		if (bind(ctl_sock, (struct sockaddr *)&server_addr,
4694dd8b5abSYoshinobu Inoue 			 server_addr.su_len) < 0) {
470cf09a206SDavid Greenman 			syslog(LOG_ERR, "control bind: %m");
471cf09a206SDavid Greenman 			exit(1);
472cf09a206SDavid Greenman 		}
473cf09a206SDavid Greenman 		if (listen(ctl_sock, 32) < 0) {
474cf09a206SDavid Greenman 			syslog(LOG_ERR, "control listen: %m");
475cf09a206SDavid Greenman 			exit(1);
476cf09a206SDavid Greenman 		}
477cf09a206SDavid Greenman 		/*
478105a3c98SJulian Elischer 		 * Atomically write process ID
479105a3c98SJulian Elischer 		 */
480105a3c98SJulian Elischer 		if (pid_file)
481105a3c98SJulian Elischer 		{
482105a3c98SJulian Elischer 			int fd;
483105a3c98SJulian Elischer 			char buf[20];
484105a3c98SJulian Elischer 
485105a3c98SJulian Elischer 			fd = open(pid_file, O_CREAT | O_WRONLY | O_TRUNC
486105a3c98SJulian Elischer 				| O_NONBLOCK | O_EXLOCK, 0644);
48785966371SWarner Losh 			if (fd < 0) {
488105a3c98SJulian Elischer 				if (errno == EAGAIN)
489105a3c98SJulian Elischer 					errx(1, "%s: file locked", pid_file);
490105a3c98SJulian Elischer 				else
491105a3c98SJulian Elischer 					err(1, "%s", pid_file);
49285966371SWarner Losh 			}
493105a3c98SJulian Elischer 			snprintf(buf, sizeof(buf),
494105a3c98SJulian Elischer 				"%lu\n", (unsigned long) getpid());
495105a3c98SJulian Elischer 			if (write(fd, buf, strlen(buf)) < 0)
496105a3c98SJulian Elischer 				err(1, "%s: write", pid_file);
497105a3c98SJulian Elischer 			/* Leave the pid file open and locked */
498105a3c98SJulian Elischer 		}
499105a3c98SJulian Elischer 		/*
500cf09a206SDavid Greenman 		 * Loop forever accepting connection requests and forking off
501cf09a206SDavid Greenman 		 * children to handle them.
502cf09a206SDavid Greenman 		 */
503cf09a206SDavid Greenman 		while (1) {
5044dd8b5abSYoshinobu Inoue 			addrlen = server_addr.su_len;
505cf09a206SDavid Greenman 			fd = accept(ctl_sock, (struct sockaddr *)&his_addr, &addrlen);
506cf09a206SDavid Greenman 			if (fork() == 0) {
507cf09a206SDavid Greenman 				/* child */
508cf09a206SDavid Greenman 				(void) dup2(fd, 0);
509cf09a206SDavid Greenman 				(void) dup2(fd, 1);
510cf09a206SDavid Greenman 				close(ctl_sock);
511cf09a206SDavid Greenman 				break;
512cf09a206SDavid Greenman 			}
513cf09a206SDavid Greenman 			close(fd);
514cf09a206SDavid Greenman 		}
515cf09a206SDavid Greenman 	} else {
516cf09a206SDavid Greenman 		addrlen = sizeof(his_addr);
517cf09a206SDavid Greenman 		if (getpeername(0, (struct sockaddr *)&his_addr, &addrlen) < 0) {
518cf09a206SDavid Greenman 			syslog(LOG_ERR, "getpeername (%s): %m",argv[0]);
519cf09a206SDavid Greenman 			exit(1);
520cf09a206SDavid Greenman 		}
521cf09a206SDavid Greenman 	}
522cf09a206SDavid Greenman 
5234b82fc95SYaroslav Tykhiy 	sa.sa_handler = SIG_DFL;
5244b82fc95SYaroslav Tykhiy 	(void)sigaction(SIGCHLD, &sa, NULL);
5254b82fc95SYaroslav Tykhiy 
5264b82fc95SYaroslav Tykhiy 	sa.sa_handler = sigurg;
5274b82fc95SYaroslav Tykhiy 	sa.sa_flags = 0;		/* don't restart syscalls for SIGURG */
5284b82fc95SYaroslav Tykhiy 	(void)sigaction(SIGURG, &sa, NULL);
5294b82fc95SYaroslav Tykhiy 
5304b82fc95SYaroslav Tykhiy 	sigfillset(&sa.sa_mask);	/* block all signals in handler */
5314b82fc95SYaroslav Tykhiy 	sa.sa_flags = SA_RESTART;
5324b82fc95SYaroslav Tykhiy 	sa.sa_handler = sigquit;
5334b82fc95SYaroslav Tykhiy 	(void)sigaction(SIGHUP, &sa, NULL);
5344b82fc95SYaroslav Tykhiy 	(void)sigaction(SIGINT, &sa, NULL);
5354b82fc95SYaroslav Tykhiy 	(void)sigaction(SIGQUIT, &sa, NULL);
5364b82fc95SYaroslav Tykhiy 	(void)sigaction(SIGTERM, &sa, NULL);
5374b82fc95SYaroslav Tykhiy 
5384b82fc95SYaroslav Tykhiy 	sa.sa_handler = lostconn;
5394b82fc95SYaroslav Tykhiy 	(void)sigaction(SIGPIPE, &sa, NULL);
540ea022d16SRodney W. Grimes 
541cf09a206SDavid Greenman 	addrlen = sizeof(ctrl_addr);
542cf09a206SDavid Greenman 	if (getsockname(0, (struct sockaddr *)&ctrl_addr, &addrlen) < 0) {
543cf09a206SDavid Greenman 		syslog(LOG_ERR, "getsockname (%s): %m",argv[0]);
544cf09a206SDavid Greenman 		exit(1);
545cf09a206SDavid Greenman 	}
546ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
547ea4e54b9SDavid Nugent 	/* select our identity from virtual host table */
5484dd8b5abSYoshinobu Inoue 	selecthost(&ctrl_addr);
549ea4e54b9SDavid Nugent #endif
550cf09a206SDavid Greenman #ifdef IP_TOS
5514dd8b5abSYoshinobu Inoue 	if (ctrl_addr.su_family == AF_INET)
5524dd8b5abSYoshinobu Inoue       {
553cf09a206SDavid Greenman 	tos = IPTOS_LOWDELAY;
55457d4ef07SYaroslav Tykhiy 	if (setsockopt(0, IPPROTO_IP, IP_TOS, &tos, sizeof(int)) < 0)
555406d1ae9SYaroslav Tykhiy 		syslog(LOG_WARNING, "control setsockopt (IP_TOS): %m");
5564dd8b5abSYoshinobu Inoue       }
557cf09a206SDavid Greenman #endif
558dadb9fb3SDavid Greenman 	/*
559dadb9fb3SDavid Greenman 	 * Disable Nagle on the control channel so that we don't have to wait
560dadb9fb3SDavid Greenman 	 * for peer's ACK before issuing our next reply.
561dadb9fb3SDavid Greenman 	 */
562dadb9fb3SDavid Greenman 	if (setsockopt(0, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) < 0)
563406d1ae9SYaroslav Tykhiy 		syslog(LOG_WARNING, "control setsockopt (TCP_NODELAY): %m");
564dadb9fb3SDavid Greenman 
5654dd8b5abSYoshinobu Inoue 	data_source.su_port = htons(ntohs(ctrl_addr.su_port) - 1);
566cf09a206SDavid Greenman 
567a5a4544eSPaul Traina 	/* set this here so klogin can use it... */
568e760ef2cSWarner Losh 	(void)snprintf(ttyline, sizeof(ttyline), "ftp%d", getpid());
569a5a4544eSPaul Traina 
570ea022d16SRodney W. Grimes 	/* Try to handle urgent data inline */
571ea022d16SRodney W. Grimes #ifdef SO_OOBINLINE
57257d4ef07SYaroslav Tykhiy 	if (setsockopt(0, SOL_SOCKET, SO_OOBINLINE, &on, sizeof(on)) < 0)
573406d1ae9SYaroslav Tykhiy 		syslog(LOG_WARNING, "control setsockopt (SO_OOBINLINE): %m");
574ea022d16SRodney W. Grimes #endif
575ea022d16SRodney W. Grimes 
576ea022d16SRodney W. Grimes #ifdef	F_SETOWN
577ea022d16SRodney W. Grimes 	if (fcntl(fileno(stdin), F_SETOWN, getpid()) == -1)
578ea022d16SRodney W. Grimes 		syslog(LOG_ERR, "fcntl F_SETOWN: %m");
579ea022d16SRodney W. Grimes #endif
5804dd8b5abSYoshinobu Inoue 	dolog((struct sockaddr *)&his_addr);
581ea022d16SRodney W. Grimes 	/*
582ea022d16SRodney W. Grimes 	 * Set up default state
583ea022d16SRodney W. Grimes 	 */
584ea022d16SRodney W. Grimes 	data = -1;
585ea022d16SRodney W. Grimes 	type = TYPE_A;
586ea022d16SRodney W. Grimes 	form = FORM_N;
587ea022d16SRodney W. Grimes 	stru = STRU_F;
588ea022d16SRodney W. Grimes 	mode = MODE_S;
589ea022d16SRodney W. Grimes 	tmpline[0] = '\0';
590ea022d16SRodney W. Grimes 
591ea022d16SRodney W. Grimes 	/* If logins are disabled, print out the message. */
592ea022d16SRodney W. Grimes 	if ((fd = fopen(_PATH_NOLOGIN,"r")) != NULL) {
593ea022d16SRodney W. Grimes 		while (fgets(line, sizeof(line), fd) != NULL) {
594ea022d16SRodney W. Grimes 			if ((cp = strchr(line, '\n')) != NULL)
595ea022d16SRodney W. Grimes 				*cp = '\0';
596ea022d16SRodney W. Grimes 			lreply(530, "%s", line);
597ea022d16SRodney W. Grimes 		}
598ea022d16SRodney W. Grimes 		(void) fflush(stdout);
599ea022d16SRodney W. Grimes 		(void) fclose(fd);
600ea022d16SRodney W. Grimes 		reply(530, "System not available.");
601ea022d16SRodney W. Grimes 		exit(0);
602ea022d16SRodney W. Grimes 	}
603ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
604ea4e54b9SDavid Nugent 	if ((fd = fopen(thishost->welcome, "r")) != NULL) {
605ea4e54b9SDavid Nugent #else
606ea022d16SRodney W. Grimes 	if ((fd = fopen(_PATH_FTPWELCOME, "r")) != NULL) {
607ea4e54b9SDavid Nugent #endif
608ea022d16SRodney W. Grimes 		while (fgets(line, sizeof(line), fd) != NULL) {
609ea022d16SRodney W. Grimes 			if ((cp = strchr(line, '\n')) != NULL)
610ea022d16SRodney W. Grimes 				*cp = '\0';
611ea022d16SRodney W. Grimes 			lreply(220, "%s", line);
612ea022d16SRodney W. Grimes 		}
613ea022d16SRodney W. Grimes 		(void) fflush(stdout);
614ea022d16SRodney W. Grimes 		(void) fclose(fd);
615ea022d16SRodney W. Grimes 		/* reply(220,) must follow */
616ea022d16SRodney W. Grimes 	}
617ea4e54b9SDavid Nugent #ifndef VIRTUAL_HOSTING
6180512556aSDavid Nugent 	if ((hostname = malloc(MAXHOSTNAMELEN)) == NULL)
619618b0bbaSMark Murray 		fatalerror("Ran out of memory.");
6209e9a43bdSBrian Somers 	(void) gethostname(hostname, MAXHOSTNAMELEN - 1);
6219e9a43bdSBrian Somers 	hostname[MAXHOSTNAMELEN - 1] = '\0';
622ea4e54b9SDavid Nugent #endif
623ea022d16SRodney W. Grimes 	reply(220, "%s FTP server (%s) ready.", hostname, version);
624ea022d16SRodney W. Grimes 	for (;;)
625ea022d16SRodney W. Grimes 		(void) yyparse();
626ea022d16SRodney W. Grimes 	/* NOTREACHED */
627ea022d16SRodney W. Grimes }
628ea022d16SRodney W. Grimes 
629ea022d16SRodney W. Grimes static void
630e4bc453cSWarner Losh lostconn(int signo)
631ea022d16SRodney W. Grimes {
632ea022d16SRodney W. Grimes 
633618b0bbaSMark Murray 	if (ftpdebug)
634ea022d16SRodney W. Grimes 		syslog(LOG_DEBUG, "lost connection");
635e02897faSPhilippe Charnier 	dologout(1);
636ea022d16SRodney W. Grimes }
637ea022d16SRodney W. Grimes 
6384b82fc95SYaroslav Tykhiy static void
639e4bc453cSWarner Losh sigquit(int signo)
6404b82fc95SYaroslav Tykhiy {
6414b82fc95SYaroslav Tykhiy 
6424b82fc95SYaroslav Tykhiy 	syslog(LOG_ERR, "got signal %d", signo);
6434b82fc95SYaroslav Tykhiy 	dologout(1);
6444b82fc95SYaroslav Tykhiy }
6454b82fc95SYaroslav Tykhiy 
646ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
647ea4e54b9SDavid Nugent /*
648ea4e54b9SDavid Nugent  * read in virtual host tables (if they exist)
649ea4e54b9SDavid Nugent  */
650ea4e54b9SDavid Nugent 
651ea4e54b9SDavid Nugent static void
652e4bc453cSWarner Losh inithosts(void)
653ea4e54b9SDavid Nugent {
65455b54aa7SYaroslav Tykhiy 	int insert;
655737d08f3SYaroslav Tykhiy 	size_t len;
656ea4e54b9SDavid Nugent 	FILE *fp;
657737d08f3SYaroslav Tykhiy 	char *cp, *mp, *line;
658737d08f3SYaroslav Tykhiy 	char *hostname;
65955b54aa7SYaroslav Tykhiy 	char *vhost, *anonuser, *statfile, *welcome, *loginmsg;
660ea4e54b9SDavid Nugent 	struct ftphost *hrp, *lhrp;
6614dd8b5abSYoshinobu Inoue 	struct addrinfo hints, *res, *ai;
662ea4e54b9SDavid Nugent 
663ea4e54b9SDavid Nugent 	/*
664ea4e54b9SDavid Nugent 	 * Fill in the default host information
665ea4e54b9SDavid Nugent 	 */
666737d08f3SYaroslav Tykhiy 	if ((hostname = malloc(MAXHOSTNAMELEN)) == NULL)
667618b0bbaSMark Murray 		fatalerror("Ran out of memory.");
668737d08f3SYaroslav Tykhiy 	if (gethostname(hostname, MAXHOSTNAMELEN) < 0)
669737d08f3SYaroslav Tykhiy 		hostname[0] = '\0';
670737d08f3SYaroslav Tykhiy 	hostname[MAXHOSTNAMELEN - 1] = '\0';
671737d08f3SYaroslav Tykhiy 	if ((hrp = malloc(sizeof(struct ftphost))) == NULL)
672737d08f3SYaroslav Tykhiy 		fatalerror("Ran out of memory.");
673737d08f3SYaroslav Tykhiy 	hrp->hostname = hostname;
674f38c6cadSYoshinobu Inoue 	hrp->hostinfo = NULL;
6754dd8b5abSYoshinobu Inoue 
6764dd8b5abSYoshinobu Inoue 	memset(&hints, 0, sizeof(hints));
6774dd8b5abSYoshinobu Inoue 	hints.ai_flags = AI_CANONNAME;
6784dd8b5abSYoshinobu Inoue 	hints.ai_family = AF_UNSPEC;
6794dd8b5abSYoshinobu Inoue 	getaddrinfo(hrp->hostname, NULL, &hints, &res);
6804dd8b5abSYoshinobu Inoue 	if (res)
681f38c6cadSYoshinobu Inoue 		hrp->hostinfo = res;
682ea4e54b9SDavid Nugent 	hrp->statfile = _PATH_FTPDSTATFILE;
683ea4e54b9SDavid Nugent 	hrp->welcome  = _PATH_FTPWELCOME;
684ea4e54b9SDavid Nugent 	hrp->loginmsg = _PATH_FTPLOGINMESG;
685ea4e54b9SDavid Nugent 	hrp->anonuser = "ftp";
686ea4e54b9SDavid Nugent 	hrp->next = NULL;
687ea4e54b9SDavid Nugent 	thishost = firsthost = lhrp = hrp;
688ea4e54b9SDavid Nugent 	if ((fp = fopen(_PATH_FTPHOSTS, "r")) != NULL) {
689b535a9bfSDavid Nugent 		int addrsize, error, gothost;
6904dd8b5abSYoshinobu Inoue 		void *addr;
6914dd8b5abSYoshinobu Inoue 		struct hostent *hp;
6924dd8b5abSYoshinobu Inoue 
693737d08f3SYaroslav Tykhiy 		while ((line = fgetln(fp, &len)) != NULL) {
6944dd8b5abSYoshinobu Inoue 			int	i, hp_error;
695ea4e54b9SDavid Nugent 
696737d08f3SYaroslav Tykhiy 			/* skip comments */
697737d08f3SYaroslav Tykhiy 			if (line[0] == '#')
698ea4e54b9SDavid Nugent 				continue;
699737d08f3SYaroslav Tykhiy 			if (line[len - 1] == '\n') {
700737d08f3SYaroslav Tykhiy 				line[len - 1] = '\0';
701737d08f3SYaroslav Tykhiy 				mp = NULL;
702737d08f3SYaroslav Tykhiy 			} else {
703737d08f3SYaroslav Tykhiy 				if ((mp = malloc(len + 1)) == NULL)
704737d08f3SYaroslav Tykhiy 					fatalerror("Ran out of memory.");
705737d08f3SYaroslav Tykhiy 				memcpy(mp, line, len);
706737d08f3SYaroslav Tykhiy 				mp[len] = '\0';
707737d08f3SYaroslav Tykhiy 				line = mp;
708ea4e54b9SDavid Nugent 			}
709ea4e54b9SDavid Nugent 			cp = strtok(line, " \t");
710737d08f3SYaroslav Tykhiy 			/* skip empty lines */
711737d08f3SYaroslav Tykhiy 			if (cp == NULL)
712737d08f3SYaroslav Tykhiy 				goto nextline;
71355b54aa7SYaroslav Tykhiy 			vhost = cp;
71455b54aa7SYaroslav Tykhiy 
71555b54aa7SYaroslav Tykhiy 			/* set defaults */
71655b54aa7SYaroslav Tykhiy 			anonuser = "ftp";
71755b54aa7SYaroslav Tykhiy 			statfile = _PATH_FTPDSTATFILE;
71855b54aa7SYaroslav Tykhiy 			welcome  = _PATH_FTPWELCOME;
71955b54aa7SYaroslav Tykhiy 			loginmsg = _PATH_FTPLOGINMESG;
72055b54aa7SYaroslav Tykhiy 
72155b54aa7SYaroslav Tykhiy 			/*
72255b54aa7SYaroslav Tykhiy 			 * Preparse the line so we can use its info
72355b54aa7SYaroslav Tykhiy 			 * for all the addresses associated with
72455b54aa7SYaroslav Tykhiy 			 * the virtual host name.
72555b54aa7SYaroslav Tykhiy 			 * Field 0, the virtual host name, is special:
72655b54aa7SYaroslav Tykhiy 			 * it's already parsed off and will be strdup'ed
72755b54aa7SYaroslav Tykhiy 			 * later, after we know its canonical form.
72855b54aa7SYaroslav Tykhiy 			 */
72955b54aa7SYaroslav Tykhiy 			for (i = 1; i < 5 && (cp = strtok(NULL, " \t")); i++)
73055b54aa7SYaroslav Tykhiy 				if (*cp != '-' && (cp = strdup(cp)))
73155b54aa7SYaroslav Tykhiy 					switch (i) {
73255b54aa7SYaroslav Tykhiy 					case 1:	/* anon user permissions */
73355b54aa7SYaroslav Tykhiy 						anonuser = cp;
73455b54aa7SYaroslav Tykhiy 						break;
73555b54aa7SYaroslav Tykhiy 					case 2: /* statistics file */
73655b54aa7SYaroslav Tykhiy 						statfile = cp;
73755b54aa7SYaroslav Tykhiy 						break;
73855b54aa7SYaroslav Tykhiy 					case 3: /* welcome message */
73955b54aa7SYaroslav Tykhiy 						welcome  = cp;
74055b54aa7SYaroslav Tykhiy 						break;
74155b54aa7SYaroslav Tykhiy 					case 4: /* login message */
74255b54aa7SYaroslav Tykhiy 						loginmsg = cp;
74355b54aa7SYaroslav Tykhiy 						break;
74455b54aa7SYaroslav Tykhiy 					default: /* programming error */
74555b54aa7SYaroslav Tykhiy 						abort();
74655b54aa7SYaroslav Tykhiy 						/* NOTREACHED */
74755b54aa7SYaroslav Tykhiy 					}
7484dd8b5abSYoshinobu Inoue 
7494dd8b5abSYoshinobu Inoue 			hints.ai_flags = 0;
7504dd8b5abSYoshinobu Inoue 			hints.ai_family = AF_UNSPEC;
7514dd8b5abSYoshinobu Inoue 			hints.ai_flags = AI_PASSIVE;
75255b54aa7SYaroslav Tykhiy 			error = getaddrinfo(vhost, NULL, &hints, &res);
7534dd8b5abSYoshinobu Inoue 			if (error != NULL)
754737d08f3SYaroslav Tykhiy 				goto nextline;
7554dd8b5abSYoshinobu Inoue 			for (ai = res; ai != NULL && ai->ai_addr != NULL;
756b535a9bfSDavid Nugent 			     ai = ai->ai_next) {
7574dd8b5abSYoshinobu Inoue 
758b535a9bfSDavid Nugent 			gothost = 0;
759ea4e54b9SDavid Nugent 			for (hrp = firsthost; hrp != NULL; hrp = hrp->next) {
760f38c6cadSYoshinobu Inoue 				struct addrinfo *hi;
761f38c6cadSYoshinobu Inoue 
762f38c6cadSYoshinobu Inoue 				for (hi = hrp->hostinfo; hi != NULL;
763f38c6cadSYoshinobu Inoue 				     hi = hi->ai_next)
764f38c6cadSYoshinobu Inoue 					if (hi->ai_addrlen == ai->ai_addrlen &&
765f38c6cadSYoshinobu Inoue 					    memcmp(hi->ai_addr,
7664dd8b5abSYoshinobu Inoue 						   ai->ai_addr,
767b535a9bfSDavid Nugent 						   ai->ai_addr->sa_len) == 0) {
768b535a9bfSDavid Nugent 						gothost++;
769b535a9bfSDavid Nugent 						break;
770b535a9bfSDavid Nugent 					}
771b535a9bfSDavid Nugent 				if (gothost)
772ea4e54b9SDavid Nugent 					break;
773ea4e54b9SDavid Nugent 			}
774ea4e54b9SDavid Nugent 			if (hrp == NULL) {
775ea4e54b9SDavid Nugent 				if ((hrp = malloc(sizeof(struct ftphost))) == NULL)
776737d08f3SYaroslav Tykhiy 					goto nextline;
77755b54aa7SYaroslav Tykhiy 				insert = 1;
77855b54aa7SYaroslav Tykhiy 			} else
77955b54aa7SYaroslav Tykhiy 				insert = 0; /* host already in the chain */
780f38c6cadSYoshinobu Inoue 			hrp->hostinfo = res;
781f38c6cadSYoshinobu Inoue 
782ea4e54b9SDavid Nugent 			/*
783ea4e54b9SDavid Nugent 			 * determine hostname to use.
7844dd8b5abSYoshinobu Inoue 			 * force defined name if there is a valid alias
785ea4e54b9SDavid Nugent 			 * otherwise fallback to primary hostname
786ea4e54b9SDavid Nugent 			 */
7874dd8b5abSYoshinobu Inoue 			/* XXX: getaddrinfo() can't do alias check */
788f38c6cadSYoshinobu Inoue 			switch(hrp->hostinfo->ai_family) {
7894dd8b5abSYoshinobu Inoue 			case AF_INET:
7904b4cc4c6SYaroslav Tykhiy 				addr = &((struct sockaddr_in *)hrp->hostinfo->ai_addr)->sin_addr;
7914b4cc4c6SYaroslav Tykhiy 				addrsize = sizeof(struct in_addr);
7924dd8b5abSYoshinobu Inoue 				break;
7934dd8b5abSYoshinobu Inoue 			case AF_INET6:
7944b4cc4c6SYaroslav Tykhiy 				addr = &((struct sockaddr_in6 *)hrp->hostinfo->ai_addr)->sin6_addr;
7954b4cc4c6SYaroslav Tykhiy 				addrsize = sizeof(struct in6_addr);
7964dd8b5abSYoshinobu Inoue 				break;
7974dd8b5abSYoshinobu Inoue 			default:
7984dd8b5abSYoshinobu Inoue 				/* should not reach here */
799f38c6cadSYoshinobu Inoue 				if (hrp->hostinfo != NULL)
800f38c6cadSYoshinobu Inoue 					freeaddrinfo(hrp->hostinfo);
8014dd8b5abSYoshinobu Inoue 				free(hrp);
802737d08f3SYaroslav Tykhiy 				goto nextline;
8034dd8b5abSYoshinobu Inoue 				/* NOTREACHED */
8044dd8b5abSYoshinobu Inoue 			}
80557d4ef07SYaroslav Tykhiy 			if ((hp = getipnodebyaddr(addr, addrsize,
806f38c6cadSYoshinobu Inoue 						  hrp->hostinfo->ai_family,
8074dd8b5abSYoshinobu Inoue 						  &hp_error)) != NULL) {
80855b54aa7SYaroslav Tykhiy 				if (strcmp(vhost, hp->h_name) != 0) {
809ea4e54b9SDavid Nugent 					if (hp->h_aliases == NULL)
81055b54aa7SYaroslav Tykhiy 						vhost = hp->h_name;
811ea4e54b9SDavid Nugent 					else {
812ea4e54b9SDavid Nugent 						i = 0;
813ea4e54b9SDavid Nugent 						while (hp->h_aliases[i] &&
81455b54aa7SYaroslav Tykhiy 						       strcmp(vhost, hp->h_aliases[i]) != 0)
815ea4e54b9SDavid Nugent 							++i;
816ea4e54b9SDavid Nugent 						if (hp->h_aliases[i] == NULL)
81755b54aa7SYaroslav Tykhiy 							vhost = hp->h_name;
818ea4e54b9SDavid Nugent 					}
819ea4e54b9SDavid Nugent 				}
820ea4e54b9SDavid Nugent 			}
82155b54aa7SYaroslav Tykhiy 			if ((hrp->hostname = strdup(vhost)) == NULL)
82255b54aa7SYaroslav Tykhiy 				goto nextline;
82355b54aa7SYaroslav Tykhiy 			hrp->anonuser = anonuser;
82455b54aa7SYaroslav Tykhiy 			hrp->statfile = statfile;
82555b54aa7SYaroslav Tykhiy 			hrp->welcome  = welcome;
82655b54aa7SYaroslav Tykhiy 			hrp->loginmsg = loginmsg;
82755b54aa7SYaroslav Tykhiy 			if (insert) {
82855b54aa7SYaroslav Tykhiy 				hrp->next  = NULL;
82955b54aa7SYaroslav Tykhiy 				lhrp->next = hrp;
83055b54aa7SYaroslav Tykhiy 				lhrp = hrp;
83155b54aa7SYaroslav Tykhiy 			}
832233c0f66SYaroslav Tykhiy 			if (hp)
8334dd8b5abSYoshinobu Inoue 				freehostent(hp);
8344dd8b5abSYoshinobu Inoue 		      }
835737d08f3SYaroslav Tykhiy nextline:
836737d08f3SYaroslav Tykhiy 			if (mp)
837737d08f3SYaroslav Tykhiy 				free(mp);
838ea4e54b9SDavid Nugent 		}
839ea4e54b9SDavid Nugent 		(void) fclose(fp);
840ea4e54b9SDavid Nugent 	}
841ea4e54b9SDavid Nugent }
842ea4e54b9SDavid Nugent 
843ea4e54b9SDavid Nugent static void
844e4bc453cSWarner Losh selecthost(union sockunion *su)
845ea4e54b9SDavid Nugent {
846ea4e54b9SDavid Nugent 	struct ftphost	*hrp;
8474dd8b5abSYoshinobu Inoue 	u_int16_t port;
8484dd8b5abSYoshinobu Inoue #ifdef INET6
8494dd8b5abSYoshinobu Inoue 	struct in6_addr *mapped_in6 = NULL;
8504dd8b5abSYoshinobu Inoue #endif
851f38c6cadSYoshinobu Inoue 	struct addrinfo *hi;
8524dd8b5abSYoshinobu Inoue 
8534dd8b5abSYoshinobu Inoue #ifdef INET6
8544dd8b5abSYoshinobu Inoue 	/*
8554dd8b5abSYoshinobu Inoue 	 * XXX IPv4 mapped IPv6 addr consideraton,
8564dd8b5abSYoshinobu Inoue 	 * specified in rfc2373.
8574dd8b5abSYoshinobu Inoue 	 */
8584dd8b5abSYoshinobu Inoue 	if (su->su_family == AF_INET6 &&
8594dd8b5abSYoshinobu Inoue 	    IN6_IS_ADDR_V4MAPPED(&su->su_sin6.sin6_addr))
8604dd8b5abSYoshinobu Inoue 		mapped_in6 = &su->su_sin6.sin6_addr;
8614dd8b5abSYoshinobu Inoue #endif
862ea4e54b9SDavid Nugent 
863ea4e54b9SDavid Nugent 	hrp = thishost = firsthost;	/* default */
8644dd8b5abSYoshinobu Inoue 	port = su->su_port;
8654dd8b5abSYoshinobu Inoue 	su->su_port = 0;
866ea4e54b9SDavid Nugent 	while (hrp != NULL) {
867b535a9bfSDavid Nugent 	    for (hi = hrp->hostinfo; hi != NULL; hi = hi->ai_next) {
868f38c6cadSYoshinobu Inoue 		if (memcmp(su, hi->ai_addr, hi->ai_addrlen) == 0) {
869ea4e54b9SDavid Nugent 			thishost = hrp;
870ea4e54b9SDavid Nugent 			break;
871ea4e54b9SDavid Nugent 		}
8724dd8b5abSYoshinobu Inoue #ifdef INET6
8734dd8b5abSYoshinobu Inoue 		/* XXX IPv4 mapped IPv6 addr consideraton */
874f38c6cadSYoshinobu Inoue 		if (hi->ai_addr->sa_family == AF_INET && mapped_in6 != NULL &&
8754dd8b5abSYoshinobu Inoue 		    (memcmp(&mapped_in6->s6_addr[12],
876f38c6cadSYoshinobu Inoue 			    &((struct sockaddr_in *)hi->ai_addr)->sin_addr,
8774dd8b5abSYoshinobu Inoue 			    sizeof(struct in_addr)) == 0)) {
8784dd8b5abSYoshinobu Inoue 			thishost = hrp;
8794dd8b5abSYoshinobu Inoue 			break;
8804dd8b5abSYoshinobu Inoue 		}
8814dd8b5abSYoshinobu Inoue #endif
882f38c6cadSYoshinobu Inoue 	    }
883ea4e54b9SDavid Nugent 	    hrp = hrp->next;
884ea4e54b9SDavid Nugent 	}
8854dd8b5abSYoshinobu Inoue 	su->su_port = port;
886ea4e54b9SDavid Nugent 	/* setup static variables as appropriate */
887ea4e54b9SDavid Nugent 	hostname = thishost->hostname;
888ea4e54b9SDavid Nugent 	ftpuser = thishost->anonuser;
889ea4e54b9SDavid Nugent }
890ea4e54b9SDavid Nugent #endif
891ea4e54b9SDavid Nugent 
892ea022d16SRodney W. Grimes /*
893ea022d16SRodney W. Grimes  * Helper function for sgetpwnam().
894ea022d16SRodney W. Grimes  */
895ea022d16SRodney W. Grimes static char *
896e4bc453cSWarner Losh sgetsave(char *s)
897ea022d16SRodney W. Grimes {
898ea022d16SRodney W. Grimes 	char *new = malloc((unsigned) strlen(s) + 1);
899ea022d16SRodney W. Grimes 
900ea022d16SRodney W. Grimes 	if (new == NULL) {
901ea022d16SRodney W. Grimes 		perror_reply(421, "Local resource failure: malloc");
902ea022d16SRodney W. Grimes 		dologout(1);
903ea022d16SRodney W. Grimes 		/* NOTREACHED */
904ea022d16SRodney W. Grimes 	}
905ea022d16SRodney W. Grimes 	(void) strcpy(new, s);
906ea022d16SRodney W. Grimes 	return (new);
907ea022d16SRodney W. Grimes }
908ea022d16SRodney W. Grimes 
909ea022d16SRodney W. Grimes /*
910ea022d16SRodney W. Grimes  * Save the result of a getpwnam.  Used for USER command, since
911ea022d16SRodney W. Grimes  * the data returned must not be clobbered by any other command
912ea022d16SRodney W. Grimes  * (e.g., globbing).
913ea022d16SRodney W. Grimes  */
914ea022d16SRodney W. Grimes static struct passwd *
915e4bc453cSWarner Losh sgetpwnam(char *name)
916ea022d16SRodney W. Grimes {
917ea022d16SRodney W. Grimes 	static struct passwd save;
918ea022d16SRodney W. Grimes 	struct passwd *p;
919ea022d16SRodney W. Grimes 
920ea022d16SRodney W. Grimes 	if ((p = getpwnam(name)) == NULL)
921ea022d16SRodney W. Grimes 		return (p);
922ea022d16SRodney W. Grimes 	if (save.pw_name) {
923ea022d16SRodney W. Grimes 		free(save.pw_name);
924ea022d16SRodney W. Grimes 		free(save.pw_passwd);
925ea022d16SRodney W. Grimes 		free(save.pw_gecos);
926ea022d16SRodney W. Grimes 		free(save.pw_dir);
927ea022d16SRodney W. Grimes 		free(save.pw_shell);
928ea022d16SRodney W. Grimes 	}
929ea022d16SRodney W. Grimes 	save = *p;
930ea022d16SRodney W. Grimes 	save.pw_name = sgetsave(p->pw_name);
931ea022d16SRodney W. Grimes 	save.pw_passwd = sgetsave(p->pw_passwd);
932ea022d16SRodney W. Grimes 	save.pw_gecos = sgetsave(p->pw_gecos);
933ea022d16SRodney W. Grimes 	save.pw_dir = sgetsave(p->pw_dir);
934ea022d16SRodney W. Grimes 	save.pw_shell = sgetsave(p->pw_shell);
935ea022d16SRodney W. Grimes 	return (&save);
936ea022d16SRodney W. Grimes }
937ea022d16SRodney W. Grimes 
938ea022d16SRodney W. Grimes static int login_attempts;	/* number of failed login attempts */
939ea022d16SRodney W. Grimes static int askpasswd;		/* had user command, ask for passwd */
94090906a46SSheldon Hearn static char curname[MAXLOGNAME];	/* current USER name */
941ea022d16SRodney W. Grimes 
942ea022d16SRodney W. Grimes /*
943ea022d16SRodney W. Grimes  * USER command.
944ea022d16SRodney W. Grimes  * Sets global passwd pointer pw if named account exists and is acceptable;
945ea022d16SRodney W. Grimes  * sets askpasswd if a PASS command is expected.  If logged in previously,
946ea022d16SRodney W. Grimes  * need to reset state.  If name is "ftp" or "anonymous", the name is not in
947ea022d16SRodney W. Grimes  * _PATH_FTPUSERS, and ftp account exists, set guest and pw, then just return.
948ea022d16SRodney W. Grimes  * If account doesn't exist, ask for passwd anyway.  Otherwise, check user
949ea022d16SRodney W. Grimes  * requesting login privileges.  Disallow anyone who does not have a standard
950ea022d16SRodney W. Grimes  * shell as returned by getusershell().  Disallow anyone mentioned in the file
951ea022d16SRodney W. Grimes  * _PATH_FTPUSERS to allow people such as root and uucp to be avoided.
952ea022d16SRodney W. Grimes  */
953ea022d16SRodney W. Grimes void
954e4bc453cSWarner Losh user(char *name)
955ea022d16SRodney W. Grimes {
956ea022d16SRodney W. Grimes 	char *cp, *shell;
957ea022d16SRodney W. Grimes 
958ea022d16SRodney W. Grimes 	if (logged_in) {
959ea022d16SRodney W. Grimes 		if (guest) {
960ea022d16SRodney W. Grimes 			reply(530, "Can't change user from guest login.");
961ea022d16SRodney W. Grimes 			return;
962a5a4544eSPaul Traina 		} else if (dochroot) {
963a5a4544eSPaul Traina 			reply(530, "Can't change user from chroot user.");
964a5a4544eSPaul Traina 			return;
965ea022d16SRodney W. Grimes 		}
966ea022d16SRodney W. Grimes 		end_login();
967ea022d16SRodney W. Grimes 	}
968ea022d16SRodney W. Grimes 
969ea022d16SRodney W. Grimes 	guest = 0;
970ea022d16SRodney W. Grimes 	if (strcmp(name, "ftp") == 0 || strcmp(name, "anonymous") == 0) {
9717edcb936SSteve Price 		if (checkuser(_PATH_FTPUSERS, "ftp", 0) ||
9727edcb936SSteve Price 		    checkuser(_PATH_FTPUSERS, "anonymous", 0))
973ea022d16SRodney W. Grimes 			reply(530, "User %s access denied.", name);
974ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
975ea4e54b9SDavid Nugent 		else if ((pw = sgetpwnam(thishost->anonuser)) != NULL) {
976ea4e54b9SDavid Nugent #else
977ea022d16SRodney W. Grimes 		else if ((pw = sgetpwnam("ftp")) != NULL) {
978ea4e54b9SDavid Nugent #endif
979ea022d16SRodney W. Grimes 			guest = 1;
980ea022d16SRodney W. Grimes 			askpasswd = 1;
981ea022d16SRodney W. Grimes 			reply(331,
9822c60c54cSPaul Traina 			"Guest login ok, send your email address as password.");
983ea022d16SRodney W. Grimes 		} else
984ea022d16SRodney W. Grimes 			reply(530, "User %s unknown.", name);
985ea022d16SRodney W. Grimes 		if (!askpasswd && logging)
986ea022d16SRodney W. Grimes 			syslog(LOG_NOTICE,
987ea022d16SRodney W. Grimes 			    "ANONYMOUS FTP LOGIN REFUSED FROM %s", remotehost);
988ea022d16SRodney W. Grimes 		return;
989ea022d16SRodney W. Grimes 	}
9905a392aecSTorsten Blum 	if (anon_only != 0) {
9915a392aecSTorsten Blum 		reply(530, "Sorry, only anonymous ftp allowed.");
9925a392aecSTorsten Blum 		return;
9935a392aecSTorsten Blum 	}
9945a392aecSTorsten Blum 
9959aca17cbSMark Murray 	if ((pw = sgetpwnam(name))) {
996ea022d16SRodney W. Grimes 		if ((shell = pw->pw_shell) == NULL || *shell == 0)
997ea022d16SRodney W. Grimes 			shell = _PATH_BSHELL;
998ea022d16SRodney W. Grimes 		while ((cp = getusershell()) != NULL)
999ea022d16SRodney W. Grimes 			if (strcmp(cp, shell) == 0)
1000ea022d16SRodney W. Grimes 				break;
1001ea022d16SRodney W. Grimes 		endusershell();
1002ea022d16SRodney W. Grimes 
10037edcb936SSteve Price 		if (cp == NULL || checkuser(_PATH_FTPUSERS, name, 1)) {
1004ea022d16SRodney W. Grimes 			reply(530, "User %s access denied.", name);
1005ea022d16SRodney W. Grimes 			if (logging)
1006ea022d16SRodney W. Grimes 				syslog(LOG_NOTICE,
1007ea022d16SRodney W. Grimes 				    "FTP LOGIN REFUSED FROM %s, %s",
1008ea022d16SRodney W. Grimes 				    remotehost, name);
1009ea022d16SRodney W. Grimes 			pw = (struct passwd *) NULL;
1010ea022d16SRodney W. Grimes 			return;
1011ea022d16SRodney W. Grimes 		}
1012ea022d16SRodney W. Grimes 	}
1013ea022d16SRodney W. Grimes 	if (logging)
1014ea022d16SRodney W. Grimes 		strncpy(curname, name, sizeof(curname)-1);
101547499ecdSAndrey A. Chernov 
101647499ecdSAndrey A. Chernov 	pwok = 0;
1017fa1746c9SMark Murray #ifdef USE_PAM
1018fa1746c9SMark Murray 	/* XXX Kluge! The conversation mechanism needs to be fixed. */
1019726040deSGuido van Rooij #endif
102047499ecdSAndrey A. Chernov 	if (opiechallenge(&opiedata, name, opieprompt) == 0) {
102147499ecdSAndrey A. Chernov 		pwok = (pw != NULL) &&
102247499ecdSAndrey A. Chernov 		       opieaccessfile(remotehost) &&
102347499ecdSAndrey A. Chernov 		       opiealways(pw->pw_dir);
102447499ecdSAndrey A. Chernov 		reply(331, "Response to %s %s for %s.",
102547499ecdSAndrey A. Chernov 		      opieprompt, pwok ? "requested" : "required", name);
102647499ecdSAndrey A. Chernov 	} else {
102747499ecdSAndrey A. Chernov 		pwok = 1;
102847499ecdSAndrey A. Chernov 		reply(331, "Password required for %s.", name);
102947499ecdSAndrey A. Chernov 	}
1030ea022d16SRodney W. Grimes 	askpasswd = 1;
1031ea022d16SRodney W. Grimes 	/*
1032ea022d16SRodney W. Grimes 	 * Delay before reading passwd after first failed
1033ea022d16SRodney W. Grimes 	 * attempt to slow down passwd-guessing programs.
1034ea022d16SRodney W. Grimes 	 */
1035ea022d16SRodney W. Grimes 	if (login_attempts)
1036ea022d16SRodney W. Grimes 		sleep((unsigned) login_attempts);
1037ea022d16SRodney W. Grimes }
1038ea022d16SRodney W. Grimes 
1039ea022d16SRodney W. Grimes /*
1040a5a4544eSPaul Traina  * Check if a user is in the file "fname"
1041ea022d16SRodney W. Grimes  */
1042ea022d16SRodney W. Grimes static int
1043e4bc453cSWarner Losh checkuser(char *fname, char *name, int pwset)
1044ea022d16SRodney W. Grimes {
1045ea022d16SRodney W. Grimes 	FILE *fd;
1046ea022d16SRodney W. Grimes 	int found = 0;
1047737d08f3SYaroslav Tykhiy 	size_t len;
1048737d08f3SYaroslav Tykhiy 	char *line, *mp, *p;
1049ea022d16SRodney W. Grimes 
1050a5a4544eSPaul Traina 	if ((fd = fopen(fname, "r")) != NULL) {
1051737d08f3SYaroslav Tykhiy 		while (!found && (line = fgetln(fd, &len)) != NULL) {
1052737d08f3SYaroslav Tykhiy 			/* skip comments */
1053ea022d16SRodney W. Grimes 			if (line[0] == '#')
1054ea022d16SRodney W. Grimes 				continue;
1055737d08f3SYaroslav Tykhiy 			if (line[len - 1] == '\n') {
1056737d08f3SYaroslav Tykhiy 				line[len - 1] = '\0';
1057737d08f3SYaroslav Tykhiy 				mp = NULL;
1058737d08f3SYaroslav Tykhiy 			} else {
1059737d08f3SYaroslav Tykhiy 				if ((mp = malloc(len + 1)) == NULL)
1060737d08f3SYaroslav Tykhiy 					fatalerror("Ran out of memory.");
1061737d08f3SYaroslav Tykhiy 				memcpy(mp, line, len);
1062737d08f3SYaroslav Tykhiy 				mp[len] = '\0';
1063737d08f3SYaroslav Tykhiy 				line = mp;
1064737d08f3SYaroslav Tykhiy 			}
1065737d08f3SYaroslav Tykhiy 			/* avoid possible leading and trailing whitespace */
1066737d08f3SYaroslav Tykhiy 			p = strtok(line, " \t");
1067737d08f3SYaroslav Tykhiy 			/* skip empty lines */
1068737d08f3SYaroslav Tykhiy 			if (p == NULL)
1069737d08f3SYaroslav Tykhiy 				goto nextline;
107031fea7b8SDavid Nugent 			/*
107131fea7b8SDavid Nugent 			 * if first chr is '@', check group membership
107231fea7b8SDavid Nugent 			 */
1073737d08f3SYaroslav Tykhiy 			if (p[0] == '@') {
107431fea7b8SDavid Nugent 				int i = 0;
107531fea7b8SDavid Nugent 				struct group *grp;
107631fea7b8SDavid Nugent 
1077737d08f3SYaroslav Tykhiy 				if ((grp = getgrnam(p+1)) == NULL)
1078737d08f3SYaroslav Tykhiy 					goto nextline;
10797edcb936SSteve Price 				/*
10807edcb936SSteve Price 				 * Check user's default group
10817edcb936SSteve Price 				 */
10827edcb936SSteve Price 				if (pwset && grp->gr_gid == pw->pw_gid)
10837edcb936SSteve Price 					found = 1;
10847edcb936SSteve Price 				/*
10857edcb936SSteve Price 				 * Check supplementary groups
10867edcb936SSteve Price 				 */
108731fea7b8SDavid Nugent 				while (!found && grp->gr_mem[i])
108831fea7b8SDavid Nugent 					found = strcmp(name,
108931fea7b8SDavid Nugent 						grp->gr_mem[i++])
109031fea7b8SDavid Nugent 						== 0;
1091ea022d16SRodney W. Grimes 			}
109231fea7b8SDavid Nugent 			/*
109331fea7b8SDavid Nugent 			 * Otherwise, just check for username match
109431fea7b8SDavid Nugent 			 */
109531fea7b8SDavid Nugent 			else
1096737d08f3SYaroslav Tykhiy 				found = strcmp(p, name) == 0;
1097737d08f3SYaroslav Tykhiy nextline:
1098737d08f3SYaroslav Tykhiy 			if (mp)
1099737d08f3SYaroslav Tykhiy 				free(mp);
1100ea022d16SRodney W. Grimes 		}
1101ea022d16SRodney W. Grimes 		(void) fclose(fd);
1102ea022d16SRodney W. Grimes 	}
1103ea022d16SRodney W. Grimes 	return (found);
1104ea022d16SRodney W. Grimes }
1105ea022d16SRodney W. Grimes 
1106ea022d16SRodney W. Grimes /*
1107ea022d16SRodney W. Grimes  * Terminate login as previous user, if any, resetting state;
1108ea022d16SRodney W. Grimes  * used when USER command is given or login fails.
1109ea022d16SRodney W. Grimes  */
1110ea022d16SRodney W. Grimes static void
1111e4bc453cSWarner Losh end_login(void)
1112ea022d16SRodney W. Grimes {
11135bc9d93dSMark Murray #ifdef USE_PAM
11145bc9d93dSMark Murray 	int e;
11155bc9d93dSMark Murray #endif
1116ea022d16SRodney W. Grimes 
1117ea022d16SRodney W. Grimes 	(void) seteuid((uid_t)0);
1118ea022d16SRodney W. Grimes 	if (logged_in)
111946948173SHajimu UMEMOTO 		ftpd_logwtmp(ttyline, "", NULL);
1120ea022d16SRodney W. Grimes 	pw = NULL;
1121b071c689SDavid Nugent #ifdef	LOGIN_CAP
1122b071c689SDavid Nugent 	setusercontext(NULL, getpwuid(0), (uid_t)0,
1123b071c689SDavid Nugent 		       LOGIN_SETPRIORITY|LOGIN_SETRESOURCES|LOGIN_SETUMASK);
1124b071c689SDavid Nugent #endif
11255bc9d93dSMark Murray #ifdef USE_PAM
11265bc9d93dSMark Murray 	if ((e = pam_setcred(pamh, PAM_DELETE_CRED)) != PAM_SUCCESS)
11275bc9d93dSMark Murray 		syslog(LOG_ERR, "pam_setcred: %s", pam_strerror(pamh, e));
11285bc9d93dSMark Murray 	if ((e = pam_close_session(pamh,0)) != PAM_SUCCESS)
11295bc9d93dSMark Murray 		syslog(LOG_ERR, "pam_close_session: %s", pam_strerror(pamh, e));
11305bc9d93dSMark Murray 	if ((e = pam_end(pamh, e)) != PAM_SUCCESS)
11315bc9d93dSMark Murray 		syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e));
11325bc9d93dSMark Murray 	pamh = NULL;
11335bc9d93dSMark Murray #endif
1134ea022d16SRodney W. Grimes 	logged_in = 0;
1135ea022d16SRodney W. Grimes 	guest = 0;
1136a5a4544eSPaul Traina 	dochroot = 0;
1137ea022d16SRodney W. Grimes }
1138ea022d16SRodney W. Grimes 
11395bc9d93dSMark Murray #ifdef USE_PAM
11406c9134c0SMark Murray 
11416c9134c0SMark Murray /*
11426c9134c0SMark Murray  * the following code is stolen from imap-uw PAM authentication module and
11436c9134c0SMark Murray  * login.c
11446c9134c0SMark Murray  */
11456c9134c0SMark Murray #define COPY_STRING(s) (s ? strdup(s) : NULL)
11466c9134c0SMark Murray 
11476c9134c0SMark Murray struct cred_t {
11486c9134c0SMark Murray 	const char *uname;		/* user name */
11496c9134c0SMark Murray 	const char *pass;		/* password */
11506c9134c0SMark Murray };
11516c9134c0SMark Murray typedef struct cred_t cred_t;
11526c9134c0SMark Murray 
11536c9134c0SMark Murray static int
11546c9134c0SMark Murray auth_conv(int num_msg, const struct pam_message **msg,
11556c9134c0SMark Murray 	  struct pam_response **resp, void *appdata)
11566c9134c0SMark Murray {
11576c9134c0SMark Murray 	int i;
11586c9134c0SMark Murray 	cred_t *cred = (cred_t *) appdata;
115960769b19SDag-Erling Smørgrav 	struct pam_response *reply;
116060769b19SDag-Erling Smørgrav 
116160769b19SDag-Erling Smørgrav 	reply = calloc(num_msg, sizeof *reply);
116260769b19SDag-Erling Smørgrav 	if (reply == NULL)
116360769b19SDag-Erling Smørgrav 		return PAM_BUF_ERR;
11646c9134c0SMark Murray 
11656c9134c0SMark Murray 	for (i = 0; i < num_msg; i++) {
11666c9134c0SMark Murray 		switch (msg[i]->msg_style) {
11676c9134c0SMark Murray 		case PAM_PROMPT_ECHO_ON:	/* assume want user name */
11686c9134c0SMark Murray 			reply[i].resp_retcode = PAM_SUCCESS;
11696c9134c0SMark Murray 			reply[i].resp = COPY_STRING(cred->uname);
11706c9134c0SMark Murray 			/* PAM frees resp. */
11716c9134c0SMark Murray 			break;
11726c9134c0SMark Murray 		case PAM_PROMPT_ECHO_OFF:	/* assume want password */
11736c9134c0SMark Murray 			reply[i].resp_retcode = PAM_SUCCESS;
11746c9134c0SMark Murray 			reply[i].resp = COPY_STRING(cred->pass);
11756c9134c0SMark Murray 			/* PAM frees resp. */
11766c9134c0SMark Murray 			break;
11776c9134c0SMark Murray 		case PAM_TEXT_INFO:
11786c9134c0SMark Murray 		case PAM_ERROR_MSG:
11796c9134c0SMark Murray 			reply[i].resp_retcode = PAM_SUCCESS;
11806c9134c0SMark Murray 			reply[i].resp = NULL;
11816c9134c0SMark Murray 			break;
11826c9134c0SMark Murray 		default:			/* unknown message style */
11836c9134c0SMark Murray 			free(reply);
11846c9134c0SMark Murray 			return PAM_CONV_ERR;
11856c9134c0SMark Murray 		}
11866c9134c0SMark Murray 	}
11876c9134c0SMark Murray 
11886c9134c0SMark Murray 	*resp = reply;
11896c9134c0SMark Murray 	return PAM_SUCCESS;
11906c9134c0SMark Murray }
11916c9134c0SMark Murray 
11926c9134c0SMark Murray /*
11936c9134c0SMark Murray  * Attempt to authenticate the user using PAM.  Returns 0 if the user is
11946c9134c0SMark Murray  * authenticated, or 1 if not authenticated.  If some sort of PAM system
11956c9134c0SMark Murray  * error occurs (e.g., the "/etc/pam.conf" file is missing) then this
11966c9134c0SMark Murray  * function returns -1.  This can be used as an indication that we should
11976c9134c0SMark Murray  * fall back to a different authentication mechanism.
11986c9134c0SMark Murray  */
11996c9134c0SMark Murray static int
12006c9134c0SMark Murray auth_pam(struct passwd **ppw, const char *pass)
12016c9134c0SMark Murray {
12026c9134c0SMark Murray 	pam_handle_t *pamh = NULL;
12036c9134c0SMark Murray 	const char *tmpl_user;
12046c9134c0SMark Murray 	const void *item;
12056c9134c0SMark Murray 	int rval;
12066c9134c0SMark Murray 	int e;
12076c9134c0SMark Murray 	cred_t auth_cred = { (*ppw)->pw_name, pass };
12086c9134c0SMark Murray 	struct pam_conv conv = { &auth_conv, &auth_cred };
12096c9134c0SMark Murray 
12106c9134c0SMark Murray 	e = pam_start("ftpd", (*ppw)->pw_name, &conv, &pamh);
12116c9134c0SMark Murray 	if (e != PAM_SUCCESS) {
12126c9134c0SMark Murray 		syslog(LOG_ERR, "pam_start: %s", pam_strerror(pamh, e));
12136c9134c0SMark Murray 		return -1;
12146c9134c0SMark Murray 	}
12156c9134c0SMark Murray 
1216ea413ab7SGuido van Rooij 	e = pam_set_item(pamh, PAM_RHOST, remotehost);
1217ea413ab7SGuido van Rooij 	if (e != PAM_SUCCESS) {
1218ea413ab7SGuido van Rooij 		syslog(LOG_ERR, "pam_set_item(PAM_RHOST): %s",
1219ea413ab7SGuido van Rooij 			pam_strerror(pamh, e));
1220ea413ab7SGuido van Rooij 		return -1;
1221ea413ab7SGuido van Rooij 	}
1222ea413ab7SGuido van Rooij 
12236c9134c0SMark Murray 	e = pam_authenticate(pamh, 0);
12246c9134c0SMark Murray 	switch (e) {
12256c9134c0SMark Murray 	case PAM_SUCCESS:
12266c9134c0SMark Murray 		/*
12276c9134c0SMark Murray 		 * With PAM we support the concept of a "template"
12286c9134c0SMark Murray 		 * user.  The user enters a login name which is
12296c9134c0SMark Murray 		 * authenticated by PAM, usually via a remote service
12306c9134c0SMark Murray 		 * such as RADIUS or TACACS+.  If authentication
12316c9134c0SMark Murray 		 * succeeds, a different but related "template" name
12326c9134c0SMark Murray 		 * is used for setting the credentials, shell, and
12336c9134c0SMark Murray 		 * home directory.  The name the user enters need only
12346c9134c0SMark Murray 		 * exist on the remote authentication server, but the
12356c9134c0SMark Murray 		 * template name must be present in the local password
12366c9134c0SMark Murray 		 * database.
12376c9134c0SMark Murray 		 *
12386c9134c0SMark Murray 		 * This is supported by two various mechanisms in the
12396c9134c0SMark Murray 		 * individual modules.  However, from the application's
12406c9134c0SMark Murray 		 * point of view, the template user is always passed
12416c9134c0SMark Murray 		 * back as a changed value of the PAM_USER item.
12426c9134c0SMark Murray 		 */
12436c9134c0SMark Murray 		if ((e = pam_get_item(pamh, PAM_USER, &item)) ==
12446c9134c0SMark Murray 		    PAM_SUCCESS) {
12456c9134c0SMark Murray 			tmpl_user = (const char *) item;
12466c9134c0SMark Murray 			if (strcmp((*ppw)->pw_name, tmpl_user) != 0)
12476c9134c0SMark Murray 				*ppw = getpwnam(tmpl_user);
12486c9134c0SMark Murray 		} else
12496c9134c0SMark Murray 			syslog(LOG_ERR, "Couldn't get PAM_USER: %s",
12506c9134c0SMark Murray 			    pam_strerror(pamh, e));
12516c9134c0SMark Murray 		rval = 0;
12526c9134c0SMark Murray 		break;
12536c9134c0SMark Murray 
12546c9134c0SMark Murray 	case PAM_AUTH_ERR:
12556c9134c0SMark Murray 	case PAM_USER_UNKNOWN:
12566c9134c0SMark Murray 	case PAM_MAXTRIES:
12576c9134c0SMark Murray 		rval = 1;
12586c9134c0SMark Murray 		break;
12596c9134c0SMark Murray 
12606c9134c0SMark Murray 	default:
12615bc9d93dSMark Murray 		syslog(LOG_ERR, "pam_authenticate: %s", pam_strerror(pamh, e));
12626c9134c0SMark Murray 		rval = -1;
12636c9134c0SMark Murray 		break;
12646c9134c0SMark Murray 	}
12656c9134c0SMark Murray 
12665bc9d93dSMark Murray 	if (rval == 0) {
12675bc9d93dSMark Murray 		e = pam_acct_mgmt(pamh, 0);
12685bc9d93dSMark Murray 		if (e == PAM_NEW_AUTHTOK_REQD) {
12695bc9d93dSMark Murray 			e = pam_chauthtok(pamh, PAM_CHANGE_EXPIRED_AUTHTOK);
12705bc9d93dSMark Murray 			if (e != PAM_SUCCESS) {
12715bc9d93dSMark Murray 				syslog(LOG_ERR, "pam_chauthtok: %s", pam_strerror(pamh, e));
12725bc9d93dSMark Murray 				rval = 1;
12735bc9d93dSMark Murray 			}
12745bc9d93dSMark Murray 		} else if (e != PAM_SUCCESS) {
12755bc9d93dSMark Murray 			rval = 1;
12765bc9d93dSMark Murray 		}
12775bc9d93dSMark Murray 	}
12785bc9d93dSMark Murray 
12795bc9d93dSMark Murray 	if (rval != 0) {
12806c9134c0SMark Murray 		if ((e = pam_end(pamh, e)) != PAM_SUCCESS) {
12816c9134c0SMark Murray 			syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e));
12825bc9d93dSMark Murray 		}
12835bc9d93dSMark Murray 		pamh = NULL;
12846c9134c0SMark Murray 	}
12856c9134c0SMark Murray 	return rval;
12866c9134c0SMark Murray }
12876c9134c0SMark Murray 
12885bc9d93dSMark Murray #endif /* USE_PAM */
12896c9134c0SMark Murray 
1290ea022d16SRodney W. Grimes void
1291e4bc453cSWarner Losh pass(char *passwd)
1292ea022d16SRodney W. Grimes {
1293a5a4544eSPaul Traina 	int rval;
1294ea022d16SRodney W. Grimes 	FILE *fd;
1295b071c689SDavid Nugent #ifdef	LOGIN_CAP
1296b071c689SDavid Nugent 	login_cap_t *lc = NULL;
1297b071c689SDavid Nugent #endif
12985bc9d93dSMark Murray #ifdef USE_PAM
12995bc9d93dSMark Murray 	int e;
13005bc9d93dSMark Murray #endif
130147499ecdSAndrey A. Chernov 	char *xpasswd;
1302ea022d16SRodney W. Grimes 
1303ea022d16SRodney W. Grimes 	if (logged_in || askpasswd == 0) {
1304ea022d16SRodney W. Grimes 		reply(503, "Login with USER first.");
1305ea022d16SRodney W. Grimes 		return;
1306ea022d16SRodney W. Grimes 	}
1307ea022d16SRodney W. Grimes 	askpasswd = 0;
1308ea022d16SRodney W. Grimes 	if (!guest) {		/* "ftp" is only account allowed no password */
1309a5a4544eSPaul Traina 		if (pw == NULL) {
1310a5a4544eSPaul Traina 			rval = 1;	/* failure below */
1311a5a4544eSPaul Traina 			goto skip;
1312a5a4544eSPaul Traina 		}
13135bc9d93dSMark Murray #ifdef USE_PAM
13146c9134c0SMark Murray 		rval = auth_pam(&pw, passwd);
1315f650a124SAndrey A. Chernov 		if (rval >= 0) {
1316f650a124SAndrey A. Chernov 			opieunlock();
1317a5a4544eSPaul Traina 			goto skip;
1318f650a124SAndrey A. Chernov 		}
1319f650a124SAndrey A. Chernov #endif
132047499ecdSAndrey A. Chernov 		if (opieverify(&opiedata, passwd) == 0)
132147499ecdSAndrey A. Chernov 			xpasswd = pw->pw_passwd;
1322f650a124SAndrey A. Chernov 		else if (pwok) {
132347499ecdSAndrey A. Chernov 			xpasswd = crypt(passwd, pw->pw_passwd);
1324f650a124SAndrey A. Chernov 			if (passwd[0] == '\0' && pw->pw_passwd[0] != '\0')
1325f650a124SAndrey A. Chernov 				xpasswd = ":";
1326f650a124SAndrey A. Chernov 		} else {
132747499ecdSAndrey A. Chernov 			rval = 1;
132847499ecdSAndrey A. Chernov 			goto skip;
132947499ecdSAndrey A. Chernov 		}
133047499ecdSAndrey A. Chernov 		rval = strcmp(pw->pw_passwd, xpasswd);
1331f650a124SAndrey A. Chernov 		if (pw->pw_expire && time(NULL) >= pw->pw_expire)
1332a5a4544eSPaul Traina 			rval = 1;	/* failure */
1333a5a4544eSPaul Traina skip:
1334a5a4544eSPaul Traina 		/*
1335a5a4544eSPaul Traina 		 * If rval == 1, the user failed the authentication check
13366c9134c0SMark Murray 		 * above.  If rval == 0, either PAM or local authentication
1337a5a4544eSPaul Traina 		 * succeeded.
1338a5a4544eSPaul Traina 		 */
1339a5a4544eSPaul Traina 		if (rval) {
1340ea022d16SRodney W. Grimes 			reply(530, "Login incorrect.");
1341ea022d16SRodney W. Grimes 			if (logging)
1342ea022d16SRodney W. Grimes 				syslog(LOG_NOTICE,
1343ea022d16SRodney W. Grimes 				    "FTP LOGIN FAILED FROM %s, %s",
1344ea022d16SRodney W. Grimes 				    remotehost, curname);
1345ea022d16SRodney W. Grimes 			pw = NULL;
1346ea022d16SRodney W. Grimes 			if (login_attempts++ >= 5) {
1347ea022d16SRodney W. Grimes 				syslog(LOG_NOTICE,
1348ea022d16SRodney W. Grimes 				    "repeated login failures from %s",
1349ea022d16SRodney W. Grimes 				    remotehost);
1350ea022d16SRodney W. Grimes 				exit(0);
1351ea022d16SRodney W. Grimes 			}
1352ea022d16SRodney W. Grimes 			return;
1353ea022d16SRodney W. Grimes 		}
1354ea022d16SRodney W. Grimes 	}
1355ea022d16SRodney W. Grimes 	login_attempts = 0;		/* this time successful */
1356ea022d16SRodney W. Grimes 	if (setegid((gid_t)pw->pw_gid) < 0) {
1357ea022d16SRodney W. Grimes 		reply(550, "Can't set gid.");
1358ea022d16SRodney W. Grimes 		return;
1359ea022d16SRodney W. Grimes 	}
1360b071c689SDavid Nugent 	/* May be overridden by login.conf */
1361b071c689SDavid Nugent 	(void) umask(defumask);
1362b071c689SDavid Nugent #ifdef	LOGIN_CAP
13635d0bfe39SDavid Nugent 	if ((lc = login_getpwclass(pw)) != NULL) {
1364b071c689SDavid Nugent 		char	remote_ip[MAXHOSTNAMELEN];
1365b071c689SDavid Nugent 
13664dd8b5abSYoshinobu Inoue 		getnameinfo((struct sockaddr *)&his_addr, his_addr.su_len,
13674dd8b5abSYoshinobu Inoue 			remote_ip, sizeof(remote_ip) - 1, NULL, 0,
1368b0f06defSHajimu UMEMOTO 			NI_NUMERICHOST);
1369b071c689SDavid Nugent 		remote_ip[sizeof(remote_ip) - 1] = 0;
1370b071c689SDavid Nugent 		if (!auth_hostok(lc, remotehost, remote_ip)) {
1371b071c689SDavid Nugent 			syslog(LOG_INFO|LOG_AUTH,
1372b071c689SDavid Nugent 			    "FTP LOGIN FAILED (HOST) as %s: permission denied.",
1373b071c689SDavid Nugent 			    pw->pw_name);
1374b071c689SDavid Nugent 			reply(530, "Permission denied.\n");
1375b071c689SDavid Nugent 			pw = NULL;
1376b071c689SDavid Nugent 			return;
1377b071c689SDavid Nugent 		}
1378b071c689SDavid Nugent 		if (!auth_timeok(lc, time(NULL))) {
1379b071c689SDavid Nugent 			reply(530, "Login not available right now.\n");
1380b071c689SDavid Nugent 			pw = NULL;
1381b071c689SDavid Nugent 			return;
1382b071c689SDavid Nugent 		}
1383b071c689SDavid Nugent 	}
1384b071c689SDavid Nugent 	setusercontext(lc, pw, (uid_t)0,
1385e6fa0d43SDag-Erling Smørgrav 		LOGIN_SETLOGIN|LOGIN_SETGROUP|LOGIN_SETPRIORITY|
1386e6fa0d43SDag-Erling Smørgrav 		LOGIN_SETRESOURCES|LOGIN_SETUMASK);
1387b071c689SDavid Nugent #else
1388e6fa0d43SDag-Erling Smørgrav 	setlogin(pw->pw_name);
1389ea022d16SRodney W. Grimes 	(void) initgroups(pw->pw_name, pw->pw_gid);
1390b071c689SDavid Nugent #endif
1391ea022d16SRodney W. Grimes 
13925bc9d93dSMark Murray #ifdef USE_PAM
13935bc9d93dSMark Murray 	if (pamh) {
13945bc9d93dSMark Murray 		if ((e = pam_open_session(pamh, 0)) != PAM_SUCCESS) {
13955bc9d93dSMark Murray 			syslog(LOG_ERR, "pam_open_session: %s", pam_strerror(pamh, e));
13965bc9d93dSMark Murray 		} else if ((e = pam_setcred(pamh, PAM_ESTABLISH_CRED)) != PAM_SUCCESS) {
13975bc9d93dSMark Murray 			syslog(LOG_ERR, "pam_setcred: %s", pam_strerror(pamh, e));
13985bc9d93dSMark Murray 		}
13995bc9d93dSMark Murray 	}
14005bc9d93dSMark Murray #endif
14015bc9d93dSMark Murray 
1402ea022d16SRodney W. Grimes 	/* open wtmp before chroot */
140346948173SHajimu UMEMOTO 	ftpd_logwtmp(ttyline, pw->pw_name, (struct sockaddr *)&his_addr);
1404ea022d16SRodney W. Grimes 	logged_in = 1;
1405ea022d16SRodney W. Grimes 
1406a5a4544eSPaul Traina 	if (guest && stats && statfd < 0)
1407ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
1408ea4e54b9SDavid Nugent 		if ((statfd = open(thishost->statfile, O_WRONLY|O_APPEND)) < 0)
1409ea4e54b9SDavid Nugent #else
14103eb568f2SGuido van Rooij 		if ((statfd = open(_PATH_FTPDSTATFILE, O_WRONLY|O_APPEND)) < 0)
1411ea4e54b9SDavid Nugent #endif
14123eb568f2SGuido van Rooij 			stats = 0;
14133eb568f2SGuido van Rooij 
1414b071c689SDavid Nugent 	dochroot =
1415b071c689SDavid Nugent #ifdef	LOGIN_CAP	/* Allow login.conf configuration as well */
1416b071c689SDavid Nugent 		login_getcapbool(lc, "ftp-chroot", 0) ||
1417b071c689SDavid Nugent #endif
14187edcb936SSteve Price 		checkuser(_PATH_FTPCHROOT, pw->pw_name, 1);
1419ea022d16SRodney W. Grimes 	if (guest) {
1420ea022d16SRodney W. Grimes 		/*
1421ea022d16SRodney W. Grimes 		 * We MUST do a chdir() after the chroot. Otherwise
1422ea022d16SRodney W. Grimes 		 * the old current directory will be accessible as "."
1423ea022d16SRodney W. Grimes 		 * outside the new root!
1424ea022d16SRodney W. Grimes 		 */
1425ea022d16SRodney W. Grimes 		if (chroot(pw->pw_dir) < 0 || chdir("/") < 0) {
1426ea022d16SRodney W. Grimes 			reply(550, "Can't set guest privileges.");
1427ea022d16SRodney W. Grimes 			goto bad;
1428ea022d16SRodney W. Grimes 		}
1429a5a4544eSPaul Traina 	} else if (dochroot) {
1430a5a4544eSPaul Traina 		if (chroot(pw->pw_dir) < 0 || chdir("/") < 0) {
1431a5a4544eSPaul Traina 			reply(550, "Can't change root.");
1432a5a4544eSPaul Traina 			goto bad;
1433a5a4544eSPaul Traina 		}
1434ea022d16SRodney W. Grimes 	} else if (chdir(pw->pw_dir) < 0) {
1435ea022d16SRodney W. Grimes 		if (chdir("/") < 0) {
1436ea022d16SRodney W. Grimes 			reply(530, "User %s: can't change directory to %s.",
1437ea022d16SRodney W. Grimes 			    pw->pw_name, pw->pw_dir);
1438ea022d16SRodney W. Grimes 			goto bad;
1439ea022d16SRodney W. Grimes 		} else
1440ea022d16SRodney W. Grimes 			lreply(230, "No directory! Logging in with home=/");
1441ea022d16SRodney W. Grimes 	}
1442ea022d16SRodney W. Grimes 	if (seteuid((uid_t)pw->pw_uid) < 0) {
1443ea022d16SRodney W. Grimes 		reply(550, "Can't set uid.");
1444ea022d16SRodney W. Grimes 		goto bad;
1445ea022d16SRodney W. Grimes 	}
144682c76939SDavid Greenman 
144782c76939SDavid Greenman 	/*
1448ea022d16SRodney W. Grimes 	 * Display a login message, if it exists.
1449ea022d16SRodney W. Grimes 	 * N.B. reply(230,) must follow the message.
1450ea022d16SRodney W. Grimes 	 */
1451ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
1452ea4e54b9SDavid Nugent 	if ((fd = fopen(thishost->loginmsg, "r")) != NULL) {
1453ea4e54b9SDavid Nugent #else
1454ea022d16SRodney W. Grimes 	if ((fd = fopen(_PATH_FTPLOGINMESG, "r")) != NULL) {
1455ea4e54b9SDavid Nugent #endif
1456ea022d16SRodney W. Grimes 		char *cp, line[LINE_MAX];
1457ea022d16SRodney W. Grimes 
1458ea022d16SRodney W. Grimes 		while (fgets(line, sizeof(line), fd) != NULL) {
1459ea022d16SRodney W. Grimes 			if ((cp = strchr(line, '\n')) != NULL)
1460ea022d16SRodney W. Grimes 				*cp = '\0';
1461ea022d16SRodney W. Grimes 			lreply(230, "%s", line);
1462ea022d16SRodney W. Grimes 		}
1463ea022d16SRodney W. Grimes 		(void) fflush(stdout);
1464ea022d16SRodney W. Grimes 		(void) fclose(fd);
1465ea022d16SRodney W. Grimes 	}
1466ea022d16SRodney W. Grimes 	if (guest) {
14673eb568f2SGuido van Rooij 		if (ident != NULL)
14683eb568f2SGuido van Rooij 			free(ident);
146961f891a6SPaul Traina 		ident = strdup(passwd);
147061f891a6SPaul Traina 		if (ident == NULL)
1471618b0bbaSMark Murray 			fatalerror("Ran out of memory.");
147261f891a6SPaul Traina 
1473ea022d16SRodney W. Grimes 		reply(230, "Guest login ok, access restrictions apply.");
1474ea022d16SRodney W. Grimes #ifdef SETPROCTITLE
1475ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
1476ea4e54b9SDavid Nugent 		if (thishost != firsthost)
1477ea4e54b9SDavid Nugent 			snprintf(proctitle, sizeof(proctitle),
1478b3a0a7cdSMike Heffner 				 "%s: anonymous(%s)/%s", remotehost, hostname,
1479b3a0a7cdSMike Heffner 				 passwd);
1480ea4e54b9SDavid Nugent 		else
1481ea4e54b9SDavid Nugent #endif
1482ea022d16SRodney W. Grimes 			snprintf(proctitle, sizeof(proctitle),
1483b3a0a7cdSMike Heffner 				 "%s: anonymous/%s", remotehost, passwd);
1484b63e1fe2SPeter Wemm 		setproctitle("%s", proctitle);
1485ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */
1486ea022d16SRodney W. Grimes 		if (logging)
1487ea022d16SRodney W. Grimes 			syslog(LOG_INFO, "ANONYMOUS FTP LOGIN FROM %s, %s",
1488ea022d16SRodney W. Grimes 			    remotehost, passwd);
1489ea022d16SRodney W. Grimes 	} else {
14903401a71fSDaniel O'Callaghan 		if (dochroot)
149111342ab1SYaroslav Tykhiy 			reply(230, "User %s logged in, "
149211342ab1SYaroslav Tykhiy 				   "access restrictions apply.", pw->pw_name);
14933401a71fSDaniel O'Callaghan 		else
1494ea022d16SRodney W. Grimes 			reply(230, "User %s logged in.", pw->pw_name);
14953401a71fSDaniel O'Callaghan 
1496ea022d16SRodney W. Grimes #ifdef SETPROCTITLE
1497ea022d16SRodney W. Grimes 		snprintf(proctitle, sizeof(proctitle),
14987a29d7daSYaroslav Tykhiy 			 "%s: user/%s", remotehost, pw->pw_name);
1499b63e1fe2SPeter Wemm 		setproctitle("%s", proctitle);
1500ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */
1501ea022d16SRodney W. Grimes 		if (logging)
1502ea022d16SRodney W. Grimes 			syslog(LOG_INFO, "FTP LOGIN FROM %s as %s",
1503ea022d16SRodney W. Grimes 			    remotehost, pw->pw_name);
1504ea022d16SRodney W. Grimes 	}
1505b071c689SDavid Nugent #ifdef	LOGIN_CAP
1506b071c689SDavid Nugent 	login_close(lc);
1507b071c689SDavid Nugent #endif
1508ea022d16SRodney W. Grimes 	return;
1509ea022d16SRodney W. Grimes bad:
1510ea022d16SRodney W. Grimes 	/* Forget all about it... */
1511b071c689SDavid Nugent #ifdef	LOGIN_CAP
1512b071c689SDavid Nugent 	login_close(lc);
1513b071c689SDavid Nugent #endif
1514ea022d16SRodney W. Grimes 	end_login();
1515ea022d16SRodney W. Grimes }
1516ea022d16SRodney W. Grimes 
1517ea022d16SRodney W. Grimes void
1518e4bc453cSWarner Losh retrieve(char *cmd, char *name)
1519ea022d16SRodney W. Grimes {
1520ea022d16SRodney W. Grimes 	FILE *fin, *dout;
1521ea022d16SRodney W. Grimes 	struct stat st;
1522e4bc453cSWarner Losh 	int (*closefunc)(FILE *);
1523158a00b2SJohn Birrell 	time_t start;
1524ea022d16SRodney W. Grimes 
1525ea022d16SRodney W. Grimes 	if (cmd == 0) {
1526ea022d16SRodney W. Grimes 		fin = fopen(name, "r"), closefunc = fclose;
1527ea022d16SRodney W. Grimes 		st.st_size = 0;
1528ea022d16SRodney W. Grimes 	} else {
1529ea022d16SRodney W. Grimes 		char line[BUFSIZ];
1530ea022d16SRodney W. Grimes 
1531e760ef2cSWarner Losh 		(void) snprintf(line, sizeof(line), cmd, name), name = line;
1532ea022d16SRodney W. Grimes 		fin = ftpd_popen(line, "r"), closefunc = ftpd_pclose;
1533ea022d16SRodney W. Grimes 		st.st_size = -1;
1534ea022d16SRodney W. Grimes 		st.st_blksize = BUFSIZ;
1535ea022d16SRodney W. Grimes 	}
1536ea022d16SRodney W. Grimes 	if (fin == NULL) {
1537ea022d16SRodney W. Grimes 		if (errno != 0) {
1538ea022d16SRodney W. Grimes 			perror_reply(550, name);
1539ea022d16SRodney W. Grimes 			if (cmd == 0) {
1540ea022d16SRodney W. Grimes 				LOGCMD("get", name);
1541ea022d16SRodney W. Grimes 			}
1542ea022d16SRodney W. Grimes 		}
1543ea022d16SRodney W. Grimes 		return;
1544ea022d16SRodney W. Grimes 	}
1545ea022d16SRodney W. Grimes 	byte_count = -1;
1546ea022d16SRodney W. Grimes 	if (cmd == 0 && (fstat(fileno(fin), &st) < 0 || !S_ISREG(st.st_mode))) {
1547ea022d16SRodney W. Grimes 		reply(550, "%s: not a plain file.", name);
1548ea022d16SRodney W. Grimes 		goto done;
1549ea022d16SRodney W. Grimes 	}
1550ea022d16SRodney W. Grimes 	if (restart_point) {
1551ea022d16SRodney W. Grimes 		if (type == TYPE_A) {
1552ea022d16SRodney W. Grimes 			off_t i, n;
1553ea022d16SRodney W. Grimes 			int c;
1554ea022d16SRodney W. Grimes 
1555ea022d16SRodney W. Grimes 			n = restart_point;
1556ea022d16SRodney W. Grimes 			i = 0;
1557ea022d16SRodney W. Grimes 			while (i++ < n) {
1558ea022d16SRodney W. Grimes 				if ((c=getc(fin)) == EOF) {
1559ea022d16SRodney W. Grimes 					perror_reply(550, name);
1560ea022d16SRodney W. Grimes 					goto done;
1561ea022d16SRodney W. Grimes 				}
1562ea022d16SRodney W. Grimes 				if (c == '\n')
1563ea022d16SRodney W. Grimes 					i++;
1564ea022d16SRodney W. Grimes 			}
1565ea022d16SRodney W. Grimes 		} else if (lseek(fileno(fin), restart_point, L_SET) < 0) {
1566ea022d16SRodney W. Grimes 			perror_reply(550, name);
1567ea022d16SRodney W. Grimes 			goto done;
1568ea022d16SRodney W. Grimes 		}
1569ea022d16SRodney W. Grimes 	}
1570ea022d16SRodney W. Grimes 	dout = dataconn(name, st.st_size, "w");
1571ea022d16SRodney W. Grimes 	if (dout == NULL)
1572ea022d16SRodney W. Grimes 		goto done;
15733eb568f2SGuido van Rooij 	time(&start);
15749fc5823aSGarrett Wollman 	send_data(fin, dout, st.st_blksize, st.st_size,
15759fc5823aSGarrett Wollman 		  restart_point == 0 && cmd == 0 && S_ISREG(st.st_mode));
15763eb568f2SGuido van Rooij 	if (cmd == 0 && guest && stats)
15773eb568f2SGuido van Rooij 		logxfer(name, st.st_size, start);
1578ea022d16SRodney W. Grimes 	(void) fclose(dout);
1579ea022d16SRodney W. Grimes 	data = -1;
1580ea022d16SRodney W. Grimes 	pdata = -1;
1581ea022d16SRodney W. Grimes done:
1582ea022d16SRodney W. Grimes 	if (cmd == 0)
1583ea022d16SRodney W. Grimes 		LOGBYTES("get", name, byte_count);
1584ea022d16SRodney W. Grimes 	(*closefunc)(fin);
1585ea022d16SRodney W. Grimes }
1586ea022d16SRodney W. Grimes 
1587ea022d16SRodney W. Grimes void
1588e4bc453cSWarner Losh store(char *name, char *mode, int unique)
1589ea022d16SRodney W. Grimes {
1590ea022d16SRodney W. Grimes 	FILE *fout, *din;
1591ea022d16SRodney W. Grimes 	struct stat st;
1592e4bc453cSWarner Losh 	int (*closefunc)(FILE *);
1593ea022d16SRodney W. Grimes 
159461f891a6SPaul Traina 	if ((unique || guest) && stat(name, &st) == 0 &&
1595ea022d16SRodney W. Grimes 	    (name = gunique(name)) == NULL) {
1596ea022d16SRodney W. Grimes 		LOGCMD(*mode == 'w' ? "put" : "append", name);
1597ea022d16SRodney W. Grimes 		return;
1598ea022d16SRodney W. Grimes 	}
1599ea022d16SRodney W. Grimes 
1600ea022d16SRodney W. Grimes 	if (restart_point)
1601ea022d16SRodney W. Grimes 		mode = "r+";
1602ea022d16SRodney W. Grimes 	fout = fopen(name, mode);
1603ea022d16SRodney W. Grimes 	closefunc = fclose;
1604ea022d16SRodney W. Grimes 	if (fout == NULL) {
1605ea022d16SRodney W. Grimes 		perror_reply(553, name);
1606ea022d16SRodney W. Grimes 		LOGCMD(*mode == 'w' ? "put" : "append", name);
1607ea022d16SRodney W. Grimes 		return;
1608ea022d16SRodney W. Grimes 	}
1609ea022d16SRodney W. Grimes 	byte_count = -1;
1610ea022d16SRodney W. Grimes 	if (restart_point) {
1611ea022d16SRodney W. Grimes 		if (type == TYPE_A) {
1612ea022d16SRodney W. Grimes 			off_t i, n;
1613ea022d16SRodney W. Grimes 			int c;
1614ea022d16SRodney W. Grimes 
1615ea022d16SRodney W. Grimes 			n = restart_point;
1616ea022d16SRodney W. Grimes 			i = 0;
1617ea022d16SRodney W. Grimes 			while (i++ < n) {
1618ea022d16SRodney W. Grimes 				if ((c=getc(fout)) == EOF) {
1619ea022d16SRodney W. Grimes 					perror_reply(550, name);
1620ea022d16SRodney W. Grimes 					goto done;
1621ea022d16SRodney W. Grimes 				}
1622ea022d16SRodney W. Grimes 				if (c == '\n')
1623ea022d16SRodney W. Grimes 					i++;
1624ea022d16SRodney W. Grimes 			}
1625ea022d16SRodney W. Grimes 			/*
1626ea022d16SRodney W. Grimes 			 * We must do this seek to "current" position
1627ea022d16SRodney W. Grimes 			 * because we are changing from reading to
1628ea022d16SRodney W. Grimes 			 * writing.
1629ea022d16SRodney W. Grimes 			 */
1630e4a71114SAndrey A. Chernov 			if (fseeko(fout, (off_t)0, SEEK_CUR) < 0) {
1631ea022d16SRodney W. Grimes 				perror_reply(550, name);
1632ea022d16SRodney W. Grimes 				goto done;
1633ea022d16SRodney W. Grimes 			}
1634ea022d16SRodney W. Grimes 		} else if (lseek(fileno(fout), restart_point, L_SET) < 0) {
1635ea022d16SRodney W. Grimes 			perror_reply(550, name);
1636ea022d16SRodney W. Grimes 			goto done;
1637ea022d16SRodney W. Grimes 		}
1638ea022d16SRodney W. Grimes 	}
1639ea022d16SRodney W. Grimes 	din = dataconn(name, (off_t)-1, "r");
1640ea022d16SRodney W. Grimes 	if (din == NULL)
1641ea022d16SRodney W. Grimes 		goto done;
1642ea022d16SRodney W. Grimes 	if (receive_data(din, fout) == 0) {
1643ea022d16SRodney W. Grimes 		if (unique)
1644ea022d16SRodney W. Grimes 			reply(226, "Transfer complete (unique file name:%s).",
1645ea022d16SRodney W. Grimes 			    name);
1646ea022d16SRodney W. Grimes 		else
1647ea022d16SRodney W. Grimes 			reply(226, "Transfer complete.");
1648ea022d16SRodney W. Grimes 	}
1649ea022d16SRodney W. Grimes 	(void) fclose(din);
1650ea022d16SRodney W. Grimes 	data = -1;
1651ea022d16SRodney W. Grimes 	pdata = -1;
1652ea022d16SRodney W. Grimes done:
1653ea022d16SRodney W. Grimes 	LOGBYTES(*mode == 'w' ? "put" : "append", name, byte_count);
1654ea022d16SRodney W. Grimes 	(*closefunc)(fout);
1655ea022d16SRodney W. Grimes }
1656ea022d16SRodney W. Grimes 
1657ea022d16SRodney W. Grimes static FILE *
1658e4bc453cSWarner Losh getdatasock(char *mode)
1659ea022d16SRodney W. Grimes {
1660ea022d16SRodney W. Grimes 	int on = 1, s, t, tries;
1661ea022d16SRodney W. Grimes 
1662ea022d16SRodney W. Grimes 	if (data >= 0)
1663ea022d16SRodney W. Grimes 		return (fdopen(data, mode));
1664ea022d16SRodney W. Grimes 	(void) seteuid((uid_t)0);
16654dd8b5abSYoshinobu Inoue 
16664dd8b5abSYoshinobu Inoue 	s = socket(data_dest.su_family, SOCK_STREAM, 0);
1667ea022d16SRodney W. Grimes 	if (s < 0)
1668ea022d16SRodney W. Grimes 		goto bad;
166957d4ef07SYaroslav Tykhiy 	if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
1670406d1ae9SYaroslav Tykhiy 		syslog(LOG_WARNING, "data setsockopt (SO_REUSEADDR): %m");
1671ea022d16SRodney W. Grimes 	/* anchor socket to avoid multi-homing problems */
16724dd8b5abSYoshinobu Inoue 	data_source = ctrl_addr;
16734dd8b5abSYoshinobu Inoue 	data_source.su_port = htons(20); /* ftp-data port */
1674ea022d16SRodney W. Grimes 	for (tries = 1; ; tries++) {
1675ea022d16SRodney W. Grimes 		if (bind(s, (struct sockaddr *)&data_source,
16764dd8b5abSYoshinobu Inoue 		    data_source.su_len) >= 0)
1677ea022d16SRodney W. Grimes 			break;
1678ea022d16SRodney W. Grimes 		if (errno != EADDRINUSE || tries > 10)
1679ea022d16SRodney W. Grimes 			goto bad;
1680ea022d16SRodney W. Grimes 		sleep(tries);
1681ea022d16SRodney W. Grimes 	}
1682ea022d16SRodney W. Grimes 	(void) seteuid((uid_t)pw->pw_uid);
1683ea022d16SRodney W. Grimes #ifdef IP_TOS
16844dd8b5abSYoshinobu Inoue 	if (data_source.su_family == AF_INET)
16854dd8b5abSYoshinobu Inoue       {
1686ea022d16SRodney W. Grimes 	on = IPTOS_THROUGHPUT;
168757d4ef07SYaroslav Tykhiy 	if (setsockopt(s, IPPROTO_IP, IP_TOS, &on, sizeof(int)) < 0)
1688406d1ae9SYaroslav Tykhiy 		syslog(LOG_WARNING, "data setsockopt (IP_TOS): %m");
16894dd8b5abSYoshinobu Inoue       }
1690ea022d16SRodney W. Grimes #endif
16919fc5823aSGarrett Wollman #ifdef TCP_NOPUSH
16929fc5823aSGarrett Wollman 	/*
16939fc5823aSGarrett Wollman 	 * Turn off push flag to keep sender TCP from sending short packets
16949fc5823aSGarrett Wollman 	 * at the boundaries of each write().  Should probably do a SO_SNDBUF
16959fc5823aSGarrett Wollman 	 * to set the send buffer size as well, but that may not be desirable
16969fc5823aSGarrett Wollman 	 * in heavy-load situations.
16979fc5823aSGarrett Wollman 	 */
16989fc5823aSGarrett Wollman 	on = 1;
169957d4ef07SYaroslav Tykhiy 	if (setsockopt(s, IPPROTO_TCP, TCP_NOPUSH, &on, sizeof on) < 0)
1700406d1ae9SYaroslav Tykhiy 		syslog(LOG_WARNING, "data setsockopt (TCP_NOPUSH): %m");
17019fc5823aSGarrett Wollman #endif
17029fc5823aSGarrett Wollman #ifdef SO_SNDBUF
17039fc5823aSGarrett Wollman 	on = 65536;
170457d4ef07SYaroslav Tykhiy 	if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &on, sizeof on) < 0)
1705406d1ae9SYaroslav Tykhiy 		syslog(LOG_WARNING, "data setsockopt (SO_SNDBUF): %m");
17069fc5823aSGarrett Wollman #endif
17079fc5823aSGarrett Wollman 
1708ea022d16SRodney W. Grimes 	return (fdopen(s, mode));
1709ea022d16SRodney W. Grimes bad:
1710ea022d16SRodney W. Grimes 	/* Return the real value of errno (close may change it) */
1711ea022d16SRodney W. Grimes 	t = errno;
1712ea022d16SRodney W. Grimes 	(void) seteuid((uid_t)pw->pw_uid);
1713ea022d16SRodney W. Grimes 	(void) close(s);
1714ea022d16SRodney W. Grimes 	errno = t;
1715ea022d16SRodney W. Grimes 	return (NULL);
1716ea022d16SRodney W. Grimes }
1717ea022d16SRodney W. Grimes 
1718ea022d16SRodney W. Grimes static FILE *
1719e4bc453cSWarner Losh dataconn(char *name, off_t size, char *mode)
1720ea022d16SRodney W. Grimes {
1721ea022d16SRodney W. Grimes 	char sizebuf[32];
1722ea022d16SRodney W. Grimes 	FILE *file;
1723ea022d16SRodney W. Grimes 	int retry = 0, tos;
1724ea022d16SRodney W. Grimes 
1725ea022d16SRodney W. Grimes 	file_size = size;
1726ea022d16SRodney W. Grimes 	byte_count = 0;
1727ea022d16SRodney W. Grimes 	if (size != (off_t) -1)
1728e760ef2cSWarner Losh 		(void) snprintf(sizebuf, sizeof(sizebuf), " (%qd bytes)", size);
1729ea022d16SRodney W. Grimes 	else
1730e760ef2cSWarner Losh 		*sizebuf = '\0';
1731ea022d16SRodney W. Grimes 	if (pdata >= 0) {
17324dd8b5abSYoshinobu Inoue 		union sockunion from;
17334cd48bacSYaroslav Tykhiy 		int flags;
17344dd8b5abSYoshinobu Inoue 		int s, fromlen = ctrl_addr.su_len;
1735d6ed3c37SGuido van Rooij 		struct timeval timeout;
1736d6ed3c37SGuido van Rooij 		fd_set set;
1737ea022d16SRodney W. Grimes 
1738d6ed3c37SGuido van Rooij 		FD_ZERO(&set);
1739d6ed3c37SGuido van Rooij 		FD_SET(pdata, &set);
1740d6ed3c37SGuido van Rooij 
1741d6ed3c37SGuido van Rooij 		timeout.tv_usec = 0;
1742d6ed3c37SGuido van Rooij 		timeout.tv_sec = 120;
1743d6ed3c37SGuido van Rooij 
17444cd48bacSYaroslav Tykhiy 		/*
17454cd48bacSYaroslav Tykhiy 		 * Granted a socket is in the blocking I/O mode,
17464cd48bacSYaroslav Tykhiy 		 * accept() will block after a successful select()
17474cd48bacSYaroslav Tykhiy 		 * if the selected connection dies in between.
17484cd48bacSYaroslav Tykhiy 		 * Therefore set the non-blocking I/O flag here.
17494cd48bacSYaroslav Tykhiy 		 */
17504cd48bacSYaroslav Tykhiy 		if ((flags = fcntl(pdata, F_GETFL, 0)) == -1 ||
17514cd48bacSYaroslav Tykhiy 		    fcntl(pdata, F_SETFL, flags | O_NONBLOCK) == -1)
17524cd48bacSYaroslav Tykhiy 			goto pdata_err;
17534cd48bacSYaroslav Tykhiy 		if (select(pdata+1, &set, (fd_set *) 0, (fd_set *) 0, &timeout) <= 0 ||
17544cd48bacSYaroslav Tykhiy 		    (s = accept(pdata, (struct sockaddr *) &from, &fromlen)) < 0)
17554cd48bacSYaroslav Tykhiy 			goto pdata_err;
1756ea022d16SRodney W. Grimes 		(void) close(pdata);
1757ea022d16SRodney W. Grimes 		pdata = s;
17584cd48bacSYaroslav Tykhiy 		/*
17594cd48bacSYaroslav Tykhiy 		 * Unset the blocking I/O flag on the child socket
17604cd48bacSYaroslav Tykhiy 		 * again so stdio can work on it.
17614cd48bacSYaroslav Tykhiy 		 */
17624cd48bacSYaroslav Tykhiy 		if ((flags = fcntl(pdata, F_GETFL, 0)) == -1 ||
17634cd48bacSYaroslav Tykhiy 		    fcntl(pdata, F_SETFL, flags & ~O_NONBLOCK) == -1)
17644cd48bacSYaroslav Tykhiy 			goto pdata_err;
1765ea022d16SRodney W. Grimes #ifdef IP_TOS
17664dd8b5abSYoshinobu Inoue 		if (from.su_family == AF_INET)
17674dd8b5abSYoshinobu Inoue 	      {
1768a5a4544eSPaul Traina 		tos = IPTOS_THROUGHPUT;
176957d4ef07SYaroslav Tykhiy 		if (setsockopt(s, IPPROTO_IP, IP_TOS, &tos, sizeof(int)) < 0)
1770406d1ae9SYaroslav Tykhiy 			syslog(LOG_WARNING, "pdata setsockopt (IP_TOS): %m");
17714dd8b5abSYoshinobu Inoue 	      }
1772ea022d16SRodney W. Grimes #endif
1773ea022d16SRodney W. Grimes 		reply(150, "Opening %s mode data connection for '%s'%s.",
1774ea022d16SRodney W. Grimes 		     type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf);
1775ea022d16SRodney W. Grimes 		return (fdopen(pdata, mode));
17764cd48bacSYaroslav Tykhiy pdata_err:
17774cd48bacSYaroslav Tykhiy 		reply(425, "Can't open data connection.");
17784cd48bacSYaroslav Tykhiy 		(void) close(pdata);
17794cd48bacSYaroslav Tykhiy 		pdata = -1;
17804cd48bacSYaroslav Tykhiy 		return (NULL);
1781ea022d16SRodney W. Grimes 	}
1782ea022d16SRodney W. Grimes 	if (data >= 0) {
1783ea022d16SRodney W. Grimes 		reply(125, "Using existing data connection for '%s'%s.",
1784ea022d16SRodney W. Grimes 		    name, sizebuf);
1785ea022d16SRodney W. Grimes 		usedefault = 1;
1786ea022d16SRodney W. Grimes 		return (fdopen(data, mode));
1787ea022d16SRodney W. Grimes 	}
1788ea022d16SRodney W. Grimes 	if (usedefault)
1789ea022d16SRodney W. Grimes 		data_dest = his_addr;
1790ea022d16SRodney W. Grimes 	usedefault = 1;
1791ea022d16SRodney W. Grimes 	file = getdatasock(mode);
1792ea022d16SRodney W. Grimes 	if (file == NULL) {
17934dd8b5abSYoshinobu Inoue 		char hostbuf[BUFSIZ], portbuf[BUFSIZ];
17944dd8b5abSYoshinobu Inoue 		getnameinfo((struct sockaddr *)&data_source,
17954dd8b5abSYoshinobu Inoue 			data_source.su_len, hostbuf, sizeof(hostbuf) - 1,
17964dd8b5abSYoshinobu Inoue 			portbuf, sizeof(portbuf),
1797b0f06defSHajimu UMEMOTO 			NI_NUMERICHOST|NI_NUMERICSERV);
17984dd8b5abSYoshinobu Inoue 		reply(425, "Can't create data socket (%s,%s): %s.",
17994dd8b5abSYoshinobu Inoue 			hostbuf, portbuf, strerror(errno));
1800ea022d16SRodney W. Grimes 		return (NULL);
1801ea022d16SRodney W. Grimes 	}
1802ea022d16SRodney W. Grimes 	data = fileno(file);
1803ea022d16SRodney W. Grimes 	while (connect(data, (struct sockaddr *)&data_dest,
18044dd8b5abSYoshinobu Inoue 	    data_dest.su_len) < 0) {
1805ea022d16SRodney W. Grimes 		if (errno == EADDRINUSE && retry < swaitmax) {
1806ea022d16SRodney W. Grimes 			sleep((unsigned) swaitint);
1807ea022d16SRodney W. Grimes 			retry += swaitint;
1808ea022d16SRodney W. Grimes 			continue;
1809ea022d16SRodney W. Grimes 		}
1810ea022d16SRodney W. Grimes 		perror_reply(425, "Can't build data connection");
1811ea022d16SRodney W. Grimes 		(void) fclose(file);
1812ea022d16SRodney W. Grimes 		data = -1;
1813ea022d16SRodney W. Grimes 		return (NULL);
1814ea022d16SRodney W. Grimes 	}
1815ea022d16SRodney W. Grimes 	reply(150, "Opening %s mode data connection for '%s'%s.",
1816ea022d16SRodney W. Grimes 	     type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf);
1817ea022d16SRodney W. Grimes 	return (file);
1818ea022d16SRodney W. Grimes }
1819ea022d16SRodney W. Grimes 
1820ea022d16SRodney W. Grimes /*
1821ea022d16SRodney W. Grimes  * Tranfer the contents of "instr" to "outstr" peer using the appropriate
18229fc5823aSGarrett Wollman  * encapsulation of the data subject to Mode, Structure, and Type.
1823ea022d16SRodney W. Grimes  *
1824ea022d16SRodney W. Grimes  * NB: Form isn't handled.
1825ea022d16SRodney W. Grimes  */
18264b82fc95SYaroslav Tykhiy static int
1827e4bc453cSWarner Losh send_data(FILE *instr, FILE *outstr, off_t blksize, off_t filesize, int isreg)
1828ea022d16SRodney W. Grimes {
1829f6f0c4b9SDan Moschuk 	int c, filefd, netfd;
1830f6f0c4b9SDan Moschuk 	char *buf;
1831f6f0c4b9SDan Moschuk 	off_t cnt;
1832ea022d16SRodney W. Grimes 
1833ea022d16SRodney W. Grimes 	transflag++;
1834ea022d16SRodney W. Grimes 	switch (type) {
1835ea022d16SRodney W. Grimes 
1836ea022d16SRodney W. Grimes 	case TYPE_A:
1837ea022d16SRodney W. Grimes 		while ((c = getc(instr)) != EOF) {
18384b82fc95SYaroslav Tykhiy 			if (recvurg)
18394b82fc95SYaroslav Tykhiy 				goto got_oob;
1840ea022d16SRodney W. Grimes 			byte_count++;
1841ea022d16SRodney W. Grimes 			if (c == '\n') {
1842ea022d16SRodney W. Grimes 				if (ferror(outstr))
1843ea022d16SRodney W. Grimes 					goto data_err;
1844ea022d16SRodney W. Grimes 				(void) putc('\r', outstr);
1845ea022d16SRodney W. Grimes 			}
1846ea022d16SRodney W. Grimes 			(void) putc(c, outstr);
1847ea022d16SRodney W. Grimes 		}
18484b82fc95SYaroslav Tykhiy 		if (recvurg)
18494b82fc95SYaroslav Tykhiy 			goto got_oob;
1850ea022d16SRodney W. Grimes 		fflush(outstr);
1851ea022d16SRodney W. Grimes 		transflag = 0;
1852ea022d16SRodney W. Grimes 		if (ferror(instr))
1853ea022d16SRodney W. Grimes 			goto file_err;
1854ea022d16SRodney W. Grimes 		if (ferror(outstr))
1855ea022d16SRodney W. Grimes 			goto data_err;
1856ea022d16SRodney W. Grimes 		reply(226, "Transfer complete.");
18574b82fc95SYaroslav Tykhiy 		return (0);
1858ea022d16SRodney W. Grimes 
1859ea022d16SRodney W. Grimes 	case TYPE_I:
1860ea022d16SRodney W. Grimes 	case TYPE_L:
18619fc5823aSGarrett Wollman 		/*
18629fc5823aSGarrett Wollman 		 * isreg is only set if we are not doing restart and we
18639fc5823aSGarrett Wollman 		 * are sending a regular file
18649fc5823aSGarrett Wollman 		 */
18659fc5823aSGarrett Wollman 		netfd = fileno(outstr);
18669fc5823aSGarrett Wollman 		filefd = fileno(instr);
18679fc5823aSGarrett Wollman 
1868f6f0c4b9SDan Moschuk 		if (isreg) {
18699fc5823aSGarrett Wollman 
1870f6f0c4b9SDan Moschuk 			off_t offset;
1871f6f0c4b9SDan Moschuk 			int err;
1872f6f0c4b9SDan Moschuk 
1873f6f0c4b9SDan Moschuk 			err = cnt = offset = 0;
1874f6f0c4b9SDan Moschuk 
1875492f1d9cSMaxim Konovalov 			while (err != -1 && filesize > 0) {
1876492f1d9cSMaxim Konovalov 				err = sendfile(filefd, netfd, offset, 0,
1877f6f0c4b9SDan Moschuk 					(struct sf_hdtr *) NULL, &cnt, 0);
18783af48c42SMaxim Konovalov 				/*
18793af48c42SMaxim Konovalov 				 * Calculate byte_count before OOB processing.
18803af48c42SMaxim Konovalov 				 * It can be used in myoob() later.
18813af48c42SMaxim Konovalov 				 */
18823af48c42SMaxim Konovalov 				byte_count += cnt;
18834b82fc95SYaroslav Tykhiy 				if (recvurg)
18844b82fc95SYaroslav Tykhiy 					goto got_oob;
1885f6f0c4b9SDan Moschuk 				offset += cnt;
1886492f1d9cSMaxim Konovalov 				filesize -= cnt;
1887f6f0c4b9SDan Moschuk 
1888f6f0c4b9SDan Moschuk 				if (err == -1) {
1889f6f0c4b9SDan Moschuk 					if (!cnt)
1890f6f0c4b9SDan Moschuk 						goto oldway;
1891f6f0c4b9SDan Moschuk 
18929fc5823aSGarrett Wollman 					goto data_err;
1893f6f0c4b9SDan Moschuk 				}
1894f6f0c4b9SDan Moschuk 			}
1895f6f0c4b9SDan Moschuk 
18960849c184SDan Moschuk 			transflag = 0;
18979fc5823aSGarrett Wollman 			reply(226, "Transfer complete.");
18984b82fc95SYaroslav Tykhiy 			return (0);
18999fc5823aSGarrett Wollman 		}
19009fc5823aSGarrett Wollman 
19019fc5823aSGarrett Wollman oldway:
1902ea022d16SRodney W. Grimes 		if ((buf = malloc((u_int)blksize)) == NULL) {
1903ea022d16SRodney W. Grimes 			transflag = 0;
1904ea022d16SRodney W. Grimes 			perror_reply(451, "Local resource failure: malloc");
19054b82fc95SYaroslav Tykhiy 			return (-1);
1906ea022d16SRodney W. Grimes 		}
19079fc5823aSGarrett Wollman 
1908ea022d16SRodney W. Grimes 		while ((cnt = read(filefd, buf, (u_int)blksize)) > 0 &&
1909ea022d16SRodney W. Grimes 		    write(netfd, buf, cnt) == cnt)
1910ea022d16SRodney W. Grimes 			byte_count += cnt;
1911ea022d16SRodney W. Grimes 		transflag = 0;
1912ea022d16SRodney W. Grimes 		(void)free(buf);
1913ea022d16SRodney W. Grimes 		if (cnt != 0) {
1914ea022d16SRodney W. Grimes 			if (cnt < 0)
1915ea022d16SRodney W. Grimes 				goto file_err;
1916ea022d16SRodney W. Grimes 			goto data_err;
1917ea022d16SRodney W. Grimes 		}
1918ea022d16SRodney W. Grimes 		reply(226, "Transfer complete.");
19194b82fc95SYaroslav Tykhiy 		return (0);
1920ea022d16SRodney W. Grimes 	default:
1921ea022d16SRodney W. Grimes 		transflag = 0;
1922ea022d16SRodney W. Grimes 		reply(550, "Unimplemented TYPE %d in send_data", type);
19234b82fc95SYaroslav Tykhiy 		return (-1);
1924ea022d16SRodney W. Grimes 	}
1925ea022d16SRodney W. Grimes 
1926ea022d16SRodney W. Grimes data_err:
1927ea022d16SRodney W. Grimes 	transflag = 0;
1928ea022d16SRodney W. Grimes 	perror_reply(426, "Data connection");
19294b82fc95SYaroslav Tykhiy 	return (-1);
1930ea022d16SRodney W. Grimes 
1931ea022d16SRodney W. Grimes file_err:
1932ea022d16SRodney W. Grimes 	transflag = 0;
1933ea022d16SRodney W. Grimes 	perror_reply(551, "Error on input file");
19344b82fc95SYaroslav Tykhiy 	return (-1);
19354b82fc95SYaroslav Tykhiy 
19364b82fc95SYaroslav Tykhiy got_oob:
19374b82fc95SYaroslav Tykhiy 	myoob();
19384b82fc95SYaroslav Tykhiy 	recvurg = 0;
19394b82fc95SYaroslav Tykhiy 	transflag = 0;
19404b82fc95SYaroslav Tykhiy 	return (-1);
1941ea022d16SRodney W. Grimes }
1942ea022d16SRodney W. Grimes 
1943ea022d16SRodney W. Grimes /*
1944ea022d16SRodney W. Grimes  * Transfer data from peer to "outstr" using the appropriate encapulation of
1945ea022d16SRodney W. Grimes  * the data subject to Mode, Structure, and Type.
1946ea022d16SRodney W. Grimes  *
1947ea022d16SRodney W. Grimes  * N.B.: Form isn't handled.
1948ea022d16SRodney W. Grimes  */
1949ea022d16SRodney W. Grimes static int
1950e4bc453cSWarner Losh receive_data(FILE *instr, FILE *outstr)
1951ea022d16SRodney W. Grimes {
1952ea022d16SRodney W. Grimes 	int c;
195361f891a6SPaul Traina 	int cnt, bare_lfs;
1954ea022d16SRodney W. Grimes 	char buf[BUFSIZ];
1955ea022d16SRodney W. Grimes 
1956ea022d16SRodney W. Grimes 	transflag++;
195761f891a6SPaul Traina 	bare_lfs = 0;
195861f891a6SPaul Traina 
1959ea022d16SRodney W. Grimes 	switch (type) {
1960ea022d16SRodney W. Grimes 
1961ea022d16SRodney W. Grimes 	case TYPE_I:
1962ea022d16SRodney W. Grimes 	case TYPE_L:
1963ea022d16SRodney W. Grimes 		while ((cnt = read(fileno(instr), buf, sizeof(buf))) > 0) {
19644b82fc95SYaroslav Tykhiy 			if (recvurg)
19654b82fc95SYaroslav Tykhiy 				goto got_oob;
1966ea022d16SRodney W. Grimes 			if (write(fileno(outstr), buf, cnt) != cnt)
1967ea022d16SRodney W. Grimes 				goto file_err;
1968ea022d16SRodney W. Grimes 			byte_count += cnt;
1969ea022d16SRodney W. Grimes 		}
19704b82fc95SYaroslav Tykhiy 		if (recvurg)
19714b82fc95SYaroslav Tykhiy 			goto got_oob;
1972ea022d16SRodney W. Grimes 		if (cnt < 0)
1973ea022d16SRodney W. Grimes 			goto data_err;
1974ea022d16SRodney W. Grimes 		transflag = 0;
1975ea022d16SRodney W. Grimes 		return (0);
1976ea022d16SRodney W. Grimes 
1977ea022d16SRodney W. Grimes 	case TYPE_E:
1978ea022d16SRodney W. Grimes 		reply(553, "TYPE E not implemented.");
1979ea022d16SRodney W. Grimes 		transflag = 0;
1980ea022d16SRodney W. Grimes 		return (-1);
1981ea022d16SRodney W. Grimes 
1982ea022d16SRodney W. Grimes 	case TYPE_A:
1983ea022d16SRodney W. Grimes 		while ((c = getc(instr)) != EOF) {
19844b82fc95SYaroslav Tykhiy 			if (recvurg)
19854b82fc95SYaroslav Tykhiy 				goto got_oob;
1986ea022d16SRodney W. Grimes 			byte_count++;
1987ea022d16SRodney W. Grimes 			if (c == '\n')
1988ea022d16SRodney W. Grimes 				bare_lfs++;
1989ea022d16SRodney W. Grimes 			while (c == '\r') {
1990ea022d16SRodney W. Grimes 				if (ferror(outstr))
1991ea022d16SRodney W. Grimes 					goto data_err;
1992ea022d16SRodney W. Grimes 				if ((c = getc(instr)) != '\n') {
1993ea022d16SRodney W. Grimes 					(void) putc ('\r', outstr);
1994ea022d16SRodney W. Grimes 					if (c == '\0' || c == EOF)
1995ea022d16SRodney W. Grimes 						goto contin2;
1996ea022d16SRodney W. Grimes 				}
1997ea022d16SRodney W. Grimes 			}
1998ea022d16SRodney W. Grimes 			(void) putc(c, outstr);
1999ea022d16SRodney W. Grimes 	contin2:	;
2000ea022d16SRodney W. Grimes 		}
20014b82fc95SYaroslav Tykhiy 		if (recvurg)
20024b82fc95SYaroslav Tykhiy 			goto got_oob;
2003ea022d16SRodney W. Grimes 		fflush(outstr);
2004ea022d16SRodney W. Grimes 		if (ferror(instr))
2005ea022d16SRodney W. Grimes 			goto data_err;
2006ea022d16SRodney W. Grimes 		if (ferror(outstr))
2007ea022d16SRodney W. Grimes 			goto file_err;
2008ea022d16SRodney W. Grimes 		transflag = 0;
2009ea022d16SRodney W. Grimes 		if (bare_lfs) {
2010ea022d16SRodney W. Grimes 			lreply(226,
2011ea022d16SRodney W. Grimes 		"WARNING! %d bare linefeeds received in ASCII mode",
2012ea022d16SRodney W. Grimes 			    bare_lfs);
2013ea022d16SRodney W. Grimes 		(void)printf("   File may not have transferred correctly.\r\n");
2014ea022d16SRodney W. Grimes 		}
2015ea022d16SRodney W. Grimes 		return (0);
2016ea022d16SRodney W. Grimes 	default:
2017ea022d16SRodney W. Grimes 		reply(550, "Unimplemented TYPE %d in receive_data", type);
2018ea022d16SRodney W. Grimes 		transflag = 0;
2019ea022d16SRodney W. Grimes 		return (-1);
2020ea022d16SRodney W. Grimes 	}
2021ea022d16SRodney W. Grimes 
2022ea022d16SRodney W. Grimes data_err:
2023ea022d16SRodney W. Grimes 	transflag = 0;
2024ea022d16SRodney W. Grimes 	perror_reply(426, "Data Connection");
2025ea022d16SRodney W. Grimes 	return (-1);
2026ea022d16SRodney W. Grimes 
2027ea022d16SRodney W. Grimes file_err:
2028ea022d16SRodney W. Grimes 	transflag = 0;
2029ea022d16SRodney W. Grimes 	perror_reply(452, "Error writing file");
2030ea022d16SRodney W. Grimes 	return (-1);
20314b82fc95SYaroslav Tykhiy 
20324b82fc95SYaroslav Tykhiy got_oob:
20334b82fc95SYaroslav Tykhiy 	myoob();
20344b82fc95SYaroslav Tykhiy 	recvurg = 0;
20354b82fc95SYaroslav Tykhiy 	transflag = 0;
20364b82fc95SYaroslav Tykhiy 	return (-1);
2037ea022d16SRodney W. Grimes }
2038ea022d16SRodney W. Grimes 
2039ea022d16SRodney W. Grimes void
2040e4bc453cSWarner Losh statfilecmd(char *filename)
2041ea022d16SRodney W. Grimes {
2042ea022d16SRodney W. Grimes 	FILE *fin;
2043ea022d16SRodney W. Grimes 	int c;
2044ea022d16SRodney W. Grimes 	char line[LINE_MAX];
2045ea022d16SRodney W. Grimes 
2046af85d782SDavid Nugent 	(void)snprintf(line, sizeof(line), _PATH_LS " -lgA %s", filename);
2047ea022d16SRodney W. Grimes 	fin = ftpd_popen(line, "r");
2048ea022d16SRodney W. Grimes 	lreply(211, "status of %s:", filename);
2049ea022d16SRodney W. Grimes 	while ((c = getc(fin)) != EOF) {
2050ea022d16SRodney W. Grimes 		if (c == '\n') {
2051ea022d16SRodney W. Grimes 			if (ferror(stdout)){
2052ea022d16SRodney W. Grimes 				perror_reply(421, "control connection");
2053ea022d16SRodney W. Grimes 				(void) ftpd_pclose(fin);
2054ea022d16SRodney W. Grimes 				dologout(1);
2055ea022d16SRodney W. Grimes 				/* NOTREACHED */
2056ea022d16SRodney W. Grimes 			}
2057ea022d16SRodney W. Grimes 			if (ferror(fin)) {
2058ea022d16SRodney W. Grimes 				perror_reply(551, filename);
2059ea022d16SRodney W. Grimes 				(void) ftpd_pclose(fin);
2060ea022d16SRodney W. Grimes 				return;
2061ea022d16SRodney W. Grimes 			}
2062ea022d16SRodney W. Grimes 			(void) putc('\r', stdout);
2063ea022d16SRodney W. Grimes 		}
2064ea022d16SRodney W. Grimes 		(void) putc(c, stdout);
2065ea022d16SRodney W. Grimes 	}
2066ea022d16SRodney W. Grimes 	(void) ftpd_pclose(fin);
2067ea022d16SRodney W. Grimes 	reply(211, "End of Status");
2068ea022d16SRodney W. Grimes }
2069ea022d16SRodney W. Grimes 
2070ea022d16SRodney W. Grimes void
2071e4bc453cSWarner Losh statcmd(void)
2072ea022d16SRodney W. Grimes {
20734dd8b5abSYoshinobu Inoue 	union sockunion *su;
2074ea022d16SRodney W. Grimes 	u_char *a, *p;
2075b0f06defSHajimu UMEMOTO 	char hname[NI_MAXHOST];
20764dd8b5abSYoshinobu Inoue 	int ispassive;
2077ea022d16SRodney W. Grimes 
2078ea022d16SRodney W. Grimes 	lreply(211, "%s FTP server status:", hostname, version);
2079ea022d16SRodney W. Grimes 	printf("     %s\r\n", version);
2080ea022d16SRodney W. Grimes 	printf("     Connected to %s", remotehost);
20814dd8b5abSYoshinobu Inoue 	if (!getnameinfo((struct sockaddr *)&his_addr, his_addr.su_len,
2082b0f06defSHajimu UMEMOTO 			 hname, sizeof(hname) - 1, NULL, 0, NI_NUMERICHOST)) {
20834dd8b5abSYoshinobu Inoue 		if (strcmp(hname, remotehost) != 0)
20844dd8b5abSYoshinobu Inoue 			printf(" (%s)", hname);
20854dd8b5abSYoshinobu Inoue 	}
2086ea022d16SRodney W. Grimes 	printf("\r\n");
2087ea022d16SRodney W. Grimes 	if (logged_in) {
2088ea022d16SRodney W. Grimes 		if (guest)
2089ea022d16SRodney W. Grimes 			printf("     Logged in anonymously\r\n");
2090ea022d16SRodney W. Grimes 		else
2091ea022d16SRodney W. Grimes 			printf("     Logged in as %s\r\n", pw->pw_name);
2092ea022d16SRodney W. Grimes 	} else if (askpasswd)
2093ea022d16SRodney W. Grimes 		printf("     Waiting for password\r\n");
2094ea022d16SRodney W. Grimes 	else
2095ea022d16SRodney W. Grimes 		printf("     Waiting for user name\r\n");
2096ea022d16SRodney W. Grimes 	printf("     TYPE: %s", typenames[type]);
2097ea022d16SRodney W. Grimes 	if (type == TYPE_A || type == TYPE_E)
2098ea022d16SRodney W. Grimes 		printf(", FORM: %s", formnames[form]);
2099ea022d16SRodney W. Grimes 	if (type == TYPE_L)
2100ea022d16SRodney W. Grimes #if NBBY == 8
2101ea022d16SRodney W. Grimes 		printf(" %d", NBBY);
2102ea022d16SRodney W. Grimes #else
2103ea022d16SRodney W. Grimes 		printf(" %d", bytesize);	/* need definition! */
2104ea022d16SRodney W. Grimes #endif
2105ea022d16SRodney W. Grimes 	printf("; STRUcture: %s; transfer MODE: %s\r\n",
2106ea022d16SRodney W. Grimes 	    strunames[stru], modenames[mode]);
2107ea022d16SRodney W. Grimes 	if (data != -1)
2108ea022d16SRodney W. Grimes 		printf("     Data connection open\r\n");
2109ea022d16SRodney W. Grimes 	else if (pdata != -1) {
21104dd8b5abSYoshinobu Inoue 		ispassive = 1;
21114dd8b5abSYoshinobu Inoue 		su = &pasv_addr;
2112ea022d16SRodney W. Grimes 		goto printaddr;
2113ea022d16SRodney W. Grimes 	} else if (usedefault == 0) {
21144dd8b5abSYoshinobu Inoue 		ispassive = 0;
21154dd8b5abSYoshinobu Inoue 		su = &data_dest;
2116ea022d16SRodney W. Grimes printaddr:
2117ea022d16SRodney W. Grimes #define UC(b) (((int) b) & 0xff)
21184dd8b5abSYoshinobu Inoue 		if (epsvall) {
21194dd8b5abSYoshinobu Inoue 			printf("     EPSV only mode (EPSV ALL)\r\n");
21204dd8b5abSYoshinobu Inoue 			goto epsvonly;
21214dd8b5abSYoshinobu Inoue 		}
21224dd8b5abSYoshinobu Inoue 
21234dd8b5abSYoshinobu Inoue 		/* PORT/PASV */
21244dd8b5abSYoshinobu Inoue 		if (su->su_family == AF_INET) {
21254dd8b5abSYoshinobu Inoue 			a = (u_char *) &su->su_sin.sin_addr;
21264dd8b5abSYoshinobu Inoue 			p = (u_char *) &su->su_sin.sin_port;
21274dd8b5abSYoshinobu Inoue 			printf("     %s (%d,%d,%d,%d,%d,%d)\r\n",
21284dd8b5abSYoshinobu Inoue 				ispassive ? "PASV" : "PORT",
21294dd8b5abSYoshinobu Inoue 				UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
21304dd8b5abSYoshinobu Inoue 				UC(p[0]), UC(p[1]));
21314dd8b5abSYoshinobu Inoue 		}
21324dd8b5abSYoshinobu Inoue 
21334dd8b5abSYoshinobu Inoue 		/* LPRT/LPSV */
21344dd8b5abSYoshinobu Inoue 	    {
21354dd8b5abSYoshinobu Inoue 		int alen, af, i;
21364dd8b5abSYoshinobu Inoue 
21374dd8b5abSYoshinobu Inoue 		switch (su->su_family) {
21384dd8b5abSYoshinobu Inoue 		case AF_INET:
21394dd8b5abSYoshinobu Inoue 			a = (u_char *) &su->su_sin.sin_addr;
21404dd8b5abSYoshinobu Inoue 			p = (u_char *) &su->su_sin.sin_port;
21414dd8b5abSYoshinobu Inoue 			alen = sizeof(su->su_sin.sin_addr);
21424dd8b5abSYoshinobu Inoue 			af = 4;
21434dd8b5abSYoshinobu Inoue 			break;
21444dd8b5abSYoshinobu Inoue 		case AF_INET6:
21454dd8b5abSYoshinobu Inoue 			a = (u_char *) &su->su_sin6.sin6_addr;
21464dd8b5abSYoshinobu Inoue 			p = (u_char *) &su->su_sin6.sin6_port;
21474dd8b5abSYoshinobu Inoue 			alen = sizeof(su->su_sin6.sin6_addr);
21484dd8b5abSYoshinobu Inoue 			af = 6;
21494dd8b5abSYoshinobu Inoue 			break;
21504dd8b5abSYoshinobu Inoue 		default:
21514dd8b5abSYoshinobu Inoue 			af = 0;
21524dd8b5abSYoshinobu Inoue 			break;
21534dd8b5abSYoshinobu Inoue 		}
21544dd8b5abSYoshinobu Inoue 		if (af) {
21554dd8b5abSYoshinobu Inoue 			printf("     %s (%d,%d,", ispassive ? "LPSV" : "LPRT",
21564dd8b5abSYoshinobu Inoue 				af, alen);
21574dd8b5abSYoshinobu Inoue 			for (i = 0; i < alen; i++)
21584dd8b5abSYoshinobu Inoue 				printf("%d,", UC(a[i]));
21594dd8b5abSYoshinobu Inoue 			printf("%d,%d,%d)\r\n", 2, UC(p[0]), UC(p[1]));
21604dd8b5abSYoshinobu Inoue 		}
21614dd8b5abSYoshinobu Inoue 	    }
21624dd8b5abSYoshinobu Inoue 
21634dd8b5abSYoshinobu Inoue epsvonly:;
21644dd8b5abSYoshinobu Inoue 		/* EPRT/EPSV */
21654dd8b5abSYoshinobu Inoue 	    {
21664dd8b5abSYoshinobu Inoue 		int af;
21674dd8b5abSYoshinobu Inoue 
21684dd8b5abSYoshinobu Inoue 		switch (su->su_family) {
21694dd8b5abSYoshinobu Inoue 		case AF_INET:
21704dd8b5abSYoshinobu Inoue 			af = 1;
21714dd8b5abSYoshinobu Inoue 			break;
21724dd8b5abSYoshinobu Inoue 		case AF_INET6:
21734dd8b5abSYoshinobu Inoue 			af = 2;
21744dd8b5abSYoshinobu Inoue 			break;
21754dd8b5abSYoshinobu Inoue 		default:
21764dd8b5abSYoshinobu Inoue 			af = 0;
21774dd8b5abSYoshinobu Inoue 			break;
21784dd8b5abSYoshinobu Inoue 		}
21794dd8b5abSYoshinobu Inoue 		if (af) {
2180b0f06defSHajimu UMEMOTO 			union sockunion tmp;
2181b0f06defSHajimu UMEMOTO 
2182b0f06defSHajimu UMEMOTO 			tmp = *su;
2183b0f06defSHajimu UMEMOTO 			if (tmp.su_family == AF_INET6)
2184b0f06defSHajimu UMEMOTO 				tmp.su_sin6.sin6_scope_id = 0;
2185b0f06defSHajimu UMEMOTO 			if (!getnameinfo((struct sockaddr *)&tmp, tmp.su_len,
21864dd8b5abSYoshinobu Inoue 					hname, sizeof(hname) - 1, NULL, 0,
21874dd8b5abSYoshinobu Inoue 					NI_NUMERICHOST)) {
21884dd8b5abSYoshinobu Inoue 				printf("     %s |%d|%s|%d|\r\n",
21894dd8b5abSYoshinobu Inoue 					ispassive ? "EPSV" : "EPRT",
2190b0f06defSHajimu UMEMOTO 					af, hname, htons(tmp.su_port));
21914dd8b5abSYoshinobu Inoue 			}
21924dd8b5abSYoshinobu Inoue 		}
21934dd8b5abSYoshinobu Inoue 	    }
2194ea022d16SRodney W. Grimes #undef UC
2195ea022d16SRodney W. Grimes 	} else
2196ea022d16SRodney W. Grimes 		printf("     No data connection\r\n");
2197ea022d16SRodney W. Grimes 	reply(211, "End of status");
2198ea022d16SRodney W. Grimes }
2199ea022d16SRodney W. Grimes 
2200ea022d16SRodney W. Grimes void
2201e4bc453cSWarner Losh fatalerror(char *s)
2202ea022d16SRodney W. Grimes {
2203ea022d16SRodney W. Grimes 
2204ea022d16SRodney W. Grimes 	reply(451, "Error in server: %s\n", s);
2205ea022d16SRodney W. Grimes 	reply(221, "Closing connection due to server error.");
2206ea022d16SRodney W. Grimes 	dologout(0);
2207ea022d16SRodney W. Grimes 	/* NOTREACHED */
2208ea022d16SRodney W. Grimes }
2209ea022d16SRodney W. Grimes 
2210ea022d16SRodney W. Grimes void
2211ea022d16SRodney W. Grimes reply(int n, const char *fmt, ...)
2212ea022d16SRodney W. Grimes {
2213ea022d16SRodney W. Grimes 	va_list ap;
2214e4bc453cSWarner Losh 
2215ea022d16SRodney W. Grimes 	va_start(ap, fmt);
2216ea022d16SRodney W. Grimes 	(void)printf("%d ", n);
2217ea022d16SRodney W. Grimes 	(void)vprintf(fmt, ap);
2218ea022d16SRodney W. Grimes 	(void)printf("\r\n");
2219ea022d16SRodney W. Grimes 	(void)fflush(stdout);
2220618b0bbaSMark Murray 	if (ftpdebug) {
2221ea022d16SRodney W. Grimes 		syslog(LOG_DEBUG, "<--- %d ", n);
2222ea022d16SRodney W. Grimes 		vsyslog(LOG_DEBUG, fmt, ap);
2223ea022d16SRodney W. Grimes 	}
2224ea022d16SRodney W. Grimes }
2225ea022d16SRodney W. Grimes 
2226ea022d16SRodney W. Grimes void
2227ea022d16SRodney W. Grimes lreply(int n, const char *fmt, ...)
2228ea022d16SRodney W. Grimes {
2229ea022d16SRodney W. Grimes 	va_list ap;
2230e4bc453cSWarner Losh 
2231ea022d16SRodney W. Grimes 	va_start(ap, fmt);
2232ea022d16SRodney W. Grimes 	(void)printf("%d- ", n);
2233ea022d16SRodney W. Grimes 	(void)vprintf(fmt, ap);
2234ea022d16SRodney W. Grimes 	(void)printf("\r\n");
2235ea022d16SRodney W. Grimes 	(void)fflush(stdout);
2236618b0bbaSMark Murray 	if (ftpdebug) {
2237ea022d16SRodney W. Grimes 		syslog(LOG_DEBUG, "<--- %d- ", n);
2238ea022d16SRodney W. Grimes 		vsyslog(LOG_DEBUG, fmt, ap);
2239ea022d16SRodney W. Grimes 	}
2240ea022d16SRodney W. Grimes }
2241ea022d16SRodney W. Grimes 
2242ea022d16SRodney W. Grimes static void
2243e4bc453cSWarner Losh ack(char *s)
2244ea022d16SRodney W. Grimes {
2245ea022d16SRodney W. Grimes 
2246ea022d16SRodney W. Grimes 	reply(250, "%s command successful.", s);
2247ea022d16SRodney W. Grimes }
2248ea022d16SRodney W. Grimes 
2249ea022d16SRodney W. Grimes void
2250e4bc453cSWarner Losh nack(char *s)
2251ea022d16SRodney W. Grimes {
2252ea022d16SRodney W. Grimes 
2253ea022d16SRodney W. Grimes 	reply(502, "%s command not implemented.", s);
2254ea022d16SRodney W. Grimes }
2255ea022d16SRodney W. Grimes 
2256ea022d16SRodney W. Grimes /* ARGSUSED */
2257ea022d16SRodney W. Grimes void
2258e4bc453cSWarner Losh yyerror(char *s)
2259ea022d16SRodney W. Grimes {
2260ea022d16SRodney W. Grimes 	char *cp;
2261ea022d16SRodney W. Grimes 
22629aca17cbSMark Murray 	if ((cp = strchr(cbuf,'\n')))
2263ea022d16SRodney W. Grimes 		*cp = '\0';
2264ea022d16SRodney W. Grimes 	reply(500, "'%s': command not understood.", cbuf);
2265ea022d16SRodney W. Grimes }
2266ea022d16SRodney W. Grimes 
2267ea022d16SRodney W. Grimes void
2268e4bc453cSWarner Losh delete(char *name)
2269ea022d16SRodney W. Grimes {
2270ea022d16SRodney W. Grimes 	struct stat st;
2271ea022d16SRodney W. Grimes 
2272ea022d16SRodney W. Grimes 	LOGCMD("delete", name);
22731b0e12d7SYaroslav Tykhiy 	if (lstat(name, &st) < 0) {
2274ea022d16SRodney W. Grimes 		perror_reply(550, name);
2275ea022d16SRodney W. Grimes 		return;
2276ea022d16SRodney W. Grimes 	}
2277ea022d16SRodney W. Grimes 	if ((st.st_mode&S_IFMT) == S_IFDIR) {
2278ea022d16SRodney W. Grimes 		if (rmdir(name) < 0) {
2279ea022d16SRodney W. Grimes 			perror_reply(550, name);
2280ea022d16SRodney W. Grimes 			return;
2281ea022d16SRodney W. Grimes 		}
2282ea022d16SRodney W. Grimes 		goto done;
2283ea022d16SRodney W. Grimes 	}
2284ea022d16SRodney W. Grimes 	if (unlink(name) < 0) {
2285ea022d16SRodney W. Grimes 		perror_reply(550, name);
2286ea022d16SRodney W. Grimes 		return;
2287ea022d16SRodney W. Grimes 	}
2288ea022d16SRodney W. Grimes done:
2289ea022d16SRodney W. Grimes 	ack("DELE");
2290ea022d16SRodney W. Grimes }
2291ea022d16SRodney W. Grimes 
2292ea022d16SRodney W. Grimes void
2293e4bc453cSWarner Losh cwd(char *path)
2294ea022d16SRodney W. Grimes {
2295ea022d16SRodney W. Grimes 
2296ea022d16SRodney W. Grimes 	if (chdir(path) < 0)
2297ea022d16SRodney W. Grimes 		perror_reply(550, path);
2298ea022d16SRodney W. Grimes 	else
2299ea022d16SRodney W. Grimes 		ack("CWD");
2300ea022d16SRodney W. Grimes }
2301ea022d16SRodney W. Grimes 
2302ea022d16SRodney W. Grimes void
2303e4bc453cSWarner Losh makedir(char *name)
2304ea022d16SRodney W. Grimes {
2305ea022d16SRodney W. Grimes 
2306ea022d16SRodney W. Grimes 	LOGCMD("mkdir", name);
2307d186bb12SMatthew N. Dodd 	if (guest && noguestmkd)
2308d186bb12SMatthew N. Dodd 		reply(550, "%s: permission denied", name);
2309d186bb12SMatthew N. Dodd 	else if (mkdir(name, 0777) < 0)
2310ea022d16SRodney W. Grimes 		perror_reply(550, name);
2311ea022d16SRodney W. Grimes 	else
2312ea022d16SRodney W. Grimes 		reply(257, "MKD command successful.");
2313ea022d16SRodney W. Grimes }
2314ea022d16SRodney W. Grimes 
2315ea022d16SRodney W. Grimes void
2316e4bc453cSWarner Losh removedir(char *name)
2317ea022d16SRodney W. Grimes {
2318ea022d16SRodney W. Grimes 
2319ea022d16SRodney W. Grimes 	LOGCMD("rmdir", name);
2320ea022d16SRodney W. Grimes 	if (rmdir(name) < 0)
2321ea022d16SRodney W. Grimes 		perror_reply(550, name);
2322ea022d16SRodney W. Grimes 	else
2323ea022d16SRodney W. Grimes 		ack("RMD");
2324ea022d16SRodney W. Grimes }
2325ea022d16SRodney W. Grimes 
2326ea022d16SRodney W. Grimes void
2327e4bc453cSWarner Losh pwd(void)
2328ea022d16SRodney W. Grimes {
2329e4648f05SYaroslav Tykhiy 	char *s, path[MAXPATHLEN + 1];
2330ea022d16SRodney W. Grimes 
2331ea022d16SRodney W. Grimes 	if (getwd(path) == (char *)NULL)
2332ea022d16SRodney W. Grimes 		reply(550, "%s.", path);
2333e4648f05SYaroslav Tykhiy 	else {
2334e4648f05SYaroslav Tykhiy 		if ((s = doublequote(path)) == NULL)
2335e4648f05SYaroslav Tykhiy 			fatalerror("Ran out of memory.");
2336e4648f05SYaroslav Tykhiy 		reply(257, "\"%s\" is current directory.", s);
2337e4648f05SYaroslav Tykhiy 		free(s);
2338e4648f05SYaroslav Tykhiy 	}
2339ea022d16SRodney W. Grimes }
2340ea022d16SRodney W. Grimes 
2341ea022d16SRodney W. Grimes char *
2342e4bc453cSWarner Losh renamefrom(char *name)
2343ea022d16SRodney W. Grimes {
2344ea022d16SRodney W. Grimes 	struct stat st;
2345ea022d16SRodney W. Grimes 
23461b0e12d7SYaroslav Tykhiy 	if (lstat(name, &st) < 0) {
2347ea022d16SRodney W. Grimes 		perror_reply(550, name);
2348ea022d16SRodney W. Grimes 		return ((char *)0);
2349ea022d16SRodney W. Grimes 	}
2350ea022d16SRodney W. Grimes 	reply(350, "File exists, ready for destination name");
2351ea022d16SRodney W. Grimes 	return (name);
2352ea022d16SRodney W. Grimes }
2353ea022d16SRodney W. Grimes 
2354ea022d16SRodney W. Grimes void
2355e4bc453cSWarner Losh renamecmd(char *from, char *to)
2356ea022d16SRodney W. Grimes {
235761f891a6SPaul Traina 	struct stat st;
2358ea022d16SRodney W. Grimes 
2359ea022d16SRodney W. Grimes 	LOGCMD2("rename", from, to);
236061f891a6SPaul Traina 
236161f891a6SPaul Traina 	if (guest && (stat(to, &st) == 0)) {
236261f891a6SPaul Traina 		reply(550, "%s: permission denied", to);
236361f891a6SPaul Traina 		return;
236461f891a6SPaul Traina 	}
236561f891a6SPaul Traina 
2366ea022d16SRodney W. Grimes 	if (rename(from, to) < 0)
2367ea022d16SRodney W. Grimes 		perror_reply(550, "rename");
2368ea022d16SRodney W. Grimes 	else
2369ea022d16SRodney W. Grimes 		ack("RNTO");
2370ea022d16SRodney W. Grimes }
2371ea022d16SRodney W. Grimes 
2372ea022d16SRodney W. Grimes static void
2373e4bc453cSWarner Losh dolog(struct sockaddr *who)
2374ea022d16SRodney W. Grimes {
23754dd8b5abSYoshinobu Inoue 	int error;
23764dd8b5abSYoshinobu Inoue 
23774dd8b5abSYoshinobu Inoue 	realhostname_sa(remotehost, sizeof(remotehost) - 1, who, who->sa_len);
2378ea022d16SRodney W. Grimes 
2379ea022d16SRodney W. Grimes #ifdef SETPROCTITLE
2380ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
2381ea4e54b9SDavid Nugent 	if (thishost != firsthost)
2382ea4e54b9SDavid Nugent 		snprintf(proctitle, sizeof(proctitle), "%s: connected (to %s)",
2383ea4e54b9SDavid Nugent 			 remotehost, hostname);
2384ea4e54b9SDavid Nugent 	else
2385ea4e54b9SDavid Nugent #endif
2386ea4e54b9SDavid Nugent 		snprintf(proctitle, sizeof(proctitle), "%s: connected",
2387ea4e54b9SDavid Nugent 			 remotehost);
2388b63e1fe2SPeter Wemm 	setproctitle("%s", proctitle);
2389ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */
2390ea022d16SRodney W. Grimes 
2391ea4e54b9SDavid Nugent 	if (logging) {
2392ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
2393ea4e54b9SDavid Nugent 		if (thishost != firsthost)
2394ea4e54b9SDavid Nugent 			syslog(LOG_INFO, "connection from %s (to %s)",
2395ea4e54b9SDavid Nugent 			       remotehost, hostname);
2396ea4e54b9SDavid Nugent 		else
2397ea4e54b9SDavid Nugent #endif
23984dd8b5abSYoshinobu Inoue 		{
23994dd8b5abSYoshinobu Inoue 			char	who_name[MAXHOSTNAMELEN];
24004dd8b5abSYoshinobu Inoue 
24014dd8b5abSYoshinobu Inoue 			error = getnameinfo(who, who->sa_len,
24024dd8b5abSYoshinobu Inoue 					    who_name, sizeof(who_name) - 1,
2403b0f06defSHajimu UMEMOTO 					    NULL, 0, NI_NUMERICHOST);
2404f5c57d05SEivind Eklund 			syslog(LOG_INFO, "connection from %s (%s)", remotehost,
24054dd8b5abSYoshinobu Inoue 			       error == 0 ? who_name : "");
24064dd8b5abSYoshinobu Inoue 		}
2407ea022d16SRodney W. Grimes 	}
2408ea4e54b9SDavid Nugent }
2409ea022d16SRodney W. Grimes 
2410ea022d16SRodney W. Grimes /*
2411ea022d16SRodney W. Grimes  * Record logout in wtmp file
2412ea022d16SRodney W. Grimes  * and exit with supplied status.
2413ea022d16SRodney W. Grimes  */
2414ea022d16SRodney W. Grimes void
2415e4bc453cSWarner Losh dologout(int status)
2416ea022d16SRodney W. Grimes {
24170b4df2eeSDavid Greenman 	/*
24180b4df2eeSDavid Greenman 	 * Prevent reception of SIGURG from resulting in a resumption
24190b4df2eeSDavid Greenman 	 * back to the main program loop.
24200b4df2eeSDavid Greenman 	 */
24210b4df2eeSDavid Greenman 	transflag = 0;
2422ea022d16SRodney W. Grimes 
2423ea022d16SRodney W. Grimes 	if (logged_in) {
2424ea022d16SRodney W. Grimes 		(void) seteuid((uid_t)0);
242546948173SHajimu UMEMOTO 		ftpd_logwtmp(ttyline, "", NULL);
2426ea022d16SRodney W. Grimes 	}
2427ea022d16SRodney W. Grimes 	/* beware of flushing buffers after a SIGPIPE */
2428ea022d16SRodney W. Grimes 	_exit(status);
2429ea022d16SRodney W. Grimes }
2430ea022d16SRodney W. Grimes 
2431ea022d16SRodney W. Grimes static void
2432e4bc453cSWarner Losh sigurg(int signo)
2433ea022d16SRodney W. Grimes {
24344b82fc95SYaroslav Tykhiy 
24354b82fc95SYaroslav Tykhiy 	recvurg = 1;
24364b82fc95SYaroslav Tykhiy }
24374b82fc95SYaroslav Tykhiy 
24384b82fc95SYaroslav Tykhiy static void
2439e4bc453cSWarner Losh myoob(void)
24404b82fc95SYaroslav Tykhiy {
2441ea022d16SRodney W. Grimes 	char *cp;
2442ea022d16SRodney W. Grimes 
2443ea022d16SRodney W. Grimes 	/* only process if transfer occurring */
2444ea022d16SRodney W. Grimes 	if (!transflag)
2445ea022d16SRodney W. Grimes 		return;
2446ea022d16SRodney W. Grimes 	cp = tmpline;
2447ea022d16SRodney W. Grimes 	if (getline(cp, 7, stdin) == NULL) {
2448ea022d16SRodney W. Grimes 		reply(221, "You could at least say goodbye.");
2449ea022d16SRodney W. Grimes 		dologout(0);
2450ea022d16SRodney W. Grimes 	}
2451ea022d16SRodney W. Grimes 	upper(cp);
2452ea022d16SRodney W. Grimes 	if (strcmp(cp, "ABOR\r\n") == 0) {
2453ea022d16SRodney W. Grimes 		tmpline[0] = '\0';
2454ea022d16SRodney W. Grimes 		reply(426, "Transfer aborted. Data connection closed.");
2455ea022d16SRodney W. Grimes 		reply(226, "Abort successful");
2456ea022d16SRodney W. Grimes 	}
2457ea022d16SRodney W. Grimes 	if (strcmp(cp, "STAT\r\n") == 0) {
24589db4bbf3SMichael Haro 		tmpline[0] = '\0';
2459ea022d16SRodney W. Grimes 		if (file_size != (off_t) -1)
2460ea022d16SRodney W. Grimes 			reply(213, "Status: %qd of %qd bytes transferred",
2461ea022d16SRodney W. Grimes 			    byte_count, file_size);
2462ea022d16SRodney W. Grimes 		else
2463ea022d16SRodney W. Grimes 			reply(213, "Status: %qd bytes transferred", byte_count);
2464ea022d16SRodney W. Grimes 	}
2465ea022d16SRodney W. Grimes }
2466ea022d16SRodney W. Grimes 
2467ea022d16SRodney W. Grimes /*
2468ea022d16SRodney W. Grimes  * Note: a response of 425 is not mentioned as a possible response to
2469ea022d16SRodney W. Grimes  *	the PASV command in RFC959. However, it has been blessed as
2470ea022d16SRodney W. Grimes  *	a legitimate response by Jon Postel in a telephone conversation
2471ea022d16SRodney W. Grimes  *	with Rick Adams on 25 Jan 89.
2472ea022d16SRodney W. Grimes  */
2473ea022d16SRodney W. Grimes void
2474e4bc453cSWarner Losh passive(void)
2475ea022d16SRodney W. Grimes {
24768af7c9a3SYaroslav Tykhiy 	int len, on;
2477ea022d16SRodney W. Grimes 	char *p, *a;
2478ea022d16SRodney W. Grimes 
247961f891a6SPaul Traina 	if (pdata >= 0)		/* close old port if one set */
248061f891a6SPaul Traina 		close(pdata);
248161f891a6SPaul Traina 
24824dd8b5abSYoshinobu Inoue 	pdata = socket(ctrl_addr.su_family, SOCK_STREAM, 0);
2483ea022d16SRodney W. Grimes 	if (pdata < 0) {
2484ea022d16SRodney W. Grimes 		perror_reply(425, "Can't open passive connection");
2485ea022d16SRodney W. Grimes 		return;
2486ea022d16SRodney W. Grimes 	}
24878af7c9a3SYaroslav Tykhiy 	on = 1;
24888af7c9a3SYaroslav Tykhiy 	if (setsockopt(pdata, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
24898af7c9a3SYaroslav Tykhiy 		syslog(LOG_WARNING, "pdata setsockopt (SO_REUSEADDR): %m");
24904c450ad7SPaul Traina 
24914c450ad7SPaul Traina 	(void) seteuid((uid_t)0);
249261f891a6SPaul Traina 
2493dacc9752SPaul Traina #ifdef IP_PORTRANGE
24944dd8b5abSYoshinobu Inoue 	if (ctrl_addr.su_family == AF_INET) {
24958af7c9a3SYaroslav Tykhiy 	    on = restricted_data_ports ? IP_PORTRANGE_HIGH
2496dacc9752SPaul Traina 				       : IP_PORTRANGE_DEFAULT;
2497dacc9752SPaul Traina 
249840e9d39eSPeter Wemm 	    if (setsockopt(pdata, IPPROTO_IP, IP_PORTRANGE,
249957d4ef07SYaroslav Tykhiy 			    &on, sizeof(on)) < 0)
25004c450ad7SPaul Traina 		    goto pasv_error;
25014c450ad7SPaul Traina 	}
2502dacc9752SPaul Traina #endif
25032db39860SNick Sayer #ifdef IPV6_PORTRANGE
25042db39860SNick Sayer 	if (ctrl_addr.su_family == AF_INET6) {
25058af7c9a3SYaroslav Tykhiy 	    on = restricted_data_ports ? IPV6_PORTRANGE_HIGH
25062db39860SNick Sayer 				       : IPV6_PORTRANGE_DEFAULT;
25072db39860SNick Sayer 
25082db39860SNick Sayer 	    if (setsockopt(pdata, IPPROTO_IPV6, IPV6_PORTRANGE,
250957d4ef07SYaroslav Tykhiy 			    &on, sizeof(on)) < 0)
25102db39860SNick Sayer 		    goto pasv_error;
25112db39860SNick Sayer 	}
25122db39860SNick Sayer #endif
251340e9d39eSPeter Wemm 
2514ea022d16SRodney W. Grimes 	pasv_addr = ctrl_addr;
25154dd8b5abSYoshinobu Inoue 	pasv_addr.su_port = 0;
25164dd8b5abSYoshinobu Inoue 	if (bind(pdata, (struct sockaddr *)&pasv_addr, pasv_addr.su_len) < 0)
2517ea022d16SRodney W. Grimes 		goto pasv_error;
251861f891a6SPaul Traina 
2519ea022d16SRodney W. Grimes 	(void) seteuid((uid_t)pw->pw_uid);
25204c450ad7SPaul Traina 
2521ea022d16SRodney W. Grimes 	len = sizeof(pasv_addr);
2522ea022d16SRodney W. Grimes 	if (getsockname(pdata, (struct sockaddr *) &pasv_addr, &len) < 0)
2523ea022d16SRodney W. Grimes 		goto pasv_error;
2524ea022d16SRodney W. Grimes 	if (listen(pdata, 1) < 0)
2525ea022d16SRodney W. Grimes 		goto pasv_error;
25264dd8b5abSYoshinobu Inoue 	if (pasv_addr.su_family == AF_INET)
25274dd8b5abSYoshinobu Inoue 		a = (char *) &pasv_addr.su_sin.sin_addr;
25284dd8b5abSYoshinobu Inoue 	else if (pasv_addr.su_family == AF_INET6 &&
25294dd8b5abSYoshinobu Inoue 		 IN6_IS_ADDR_V4MAPPED(&pasv_addr.su_sin6.sin6_addr))
25304dd8b5abSYoshinobu Inoue 		a = (char *) &pasv_addr.su_sin6.sin6_addr.s6_addr[12];
25314dd8b5abSYoshinobu Inoue 	else
25324dd8b5abSYoshinobu Inoue 		goto pasv_error;
25334dd8b5abSYoshinobu Inoue 
25344dd8b5abSYoshinobu Inoue 	p = (char *) &pasv_addr.su_port;
2535ea022d16SRodney W. Grimes 
2536ea022d16SRodney W. Grimes #define UC(b) (((int) b) & 0xff)
2537ea022d16SRodney W. Grimes 
2538ea022d16SRodney W. Grimes 	reply(227, "Entering Passive Mode (%d,%d,%d,%d,%d,%d)", UC(a[0]),
2539ea022d16SRodney W. Grimes 		UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1]));
2540ea022d16SRodney W. Grimes 	return;
2541ea022d16SRodney W. Grimes 
2542ea022d16SRodney W. Grimes pasv_error:
254361f891a6SPaul Traina 	(void) seteuid((uid_t)pw->pw_uid);
2544ea022d16SRodney W. Grimes 	(void) close(pdata);
2545ea022d16SRodney W. Grimes 	pdata = -1;
2546ea022d16SRodney W. Grimes 	perror_reply(425, "Can't open passive connection");
2547ea022d16SRodney W. Grimes 	return;
2548ea022d16SRodney W. Grimes }
2549ea022d16SRodney W. Grimes 
2550ea022d16SRodney W. Grimes /*
25514dd8b5abSYoshinobu Inoue  * Long Passive defined in RFC 1639.
25524dd8b5abSYoshinobu Inoue  *     228 Entering Long Passive Mode
25534dd8b5abSYoshinobu Inoue  *         (af, hal, h1, h2, h3,..., pal, p1, p2...)
25544dd8b5abSYoshinobu Inoue  */
25554dd8b5abSYoshinobu Inoue 
25564dd8b5abSYoshinobu Inoue void
2557e4bc453cSWarner Losh long_passive(char *cmd, int pf)
25584dd8b5abSYoshinobu Inoue {
25598af7c9a3SYaroslav Tykhiy 	int len, on;
25604dd8b5abSYoshinobu Inoue 	char *p, *a;
25614dd8b5abSYoshinobu Inoue 
25624dd8b5abSYoshinobu Inoue 	if (pdata >= 0)		/* close old port if one set */
25634dd8b5abSYoshinobu Inoue 		close(pdata);
25644dd8b5abSYoshinobu Inoue 
25654dd8b5abSYoshinobu Inoue 	if (pf != PF_UNSPEC) {
25664dd8b5abSYoshinobu Inoue 		if (ctrl_addr.su_family != pf) {
25674dd8b5abSYoshinobu Inoue 			switch (ctrl_addr.su_family) {
25684dd8b5abSYoshinobu Inoue 			case AF_INET:
25694dd8b5abSYoshinobu Inoue 				pf = 1;
25704dd8b5abSYoshinobu Inoue 				break;
25714dd8b5abSYoshinobu Inoue 			case AF_INET6:
25724dd8b5abSYoshinobu Inoue 				pf = 2;
25734dd8b5abSYoshinobu Inoue 				break;
25744dd8b5abSYoshinobu Inoue 			default:
25754dd8b5abSYoshinobu Inoue 				pf = 0;
25764dd8b5abSYoshinobu Inoue 				break;
25774dd8b5abSYoshinobu Inoue 			}
25784dd8b5abSYoshinobu Inoue 			/*
25794dd8b5abSYoshinobu Inoue 			 * XXX
25804dd8b5abSYoshinobu Inoue 			 * only EPRT/EPSV ready clients will understand this
25814dd8b5abSYoshinobu Inoue 			 */
25824dd8b5abSYoshinobu Inoue 			if (strcmp(cmd, "EPSV") == 0 && pf) {
25834dd8b5abSYoshinobu Inoue 				reply(522, "Network protocol mismatch, "
25844dd8b5abSYoshinobu Inoue 					"use (%d)", pf);
25854dd8b5abSYoshinobu Inoue 			} else
25864dd8b5abSYoshinobu Inoue 				reply(501, "Network protocol mismatch"); /*XXX*/
25874dd8b5abSYoshinobu Inoue 
25884dd8b5abSYoshinobu Inoue 			return;
25894dd8b5abSYoshinobu Inoue 		}
25904dd8b5abSYoshinobu Inoue 	}
25914dd8b5abSYoshinobu Inoue 
25924dd8b5abSYoshinobu Inoue 	pdata = socket(ctrl_addr.su_family, SOCK_STREAM, 0);
25934dd8b5abSYoshinobu Inoue 	if (pdata < 0) {
25944dd8b5abSYoshinobu Inoue 		perror_reply(425, "Can't open passive connection");
25954dd8b5abSYoshinobu Inoue 		return;
25964dd8b5abSYoshinobu Inoue 	}
25978af7c9a3SYaroslav Tykhiy 	on = 1;
25988af7c9a3SYaroslav Tykhiy 	if (setsockopt(pdata, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
25998af7c9a3SYaroslav Tykhiy 		syslog(LOG_WARNING, "pdata setsockopt (SO_REUSEADDR): %m");
26004dd8b5abSYoshinobu Inoue 
26014dd8b5abSYoshinobu Inoue 	(void) seteuid((uid_t)0);
26024dd8b5abSYoshinobu Inoue 
26034dd8b5abSYoshinobu Inoue 	pasv_addr = ctrl_addr;
26044dd8b5abSYoshinobu Inoue 	pasv_addr.su_port = 0;
26054dd8b5abSYoshinobu Inoue 	len = pasv_addr.su_len;
26064dd8b5abSYoshinobu Inoue 
26072db39860SNick Sayer #ifdef IP_PORTRANGE
26082db39860SNick Sayer 	if (ctrl_addr.su_family == AF_INET) {
26098af7c9a3SYaroslav Tykhiy 	    on = restricted_data_ports ? IP_PORTRANGE_HIGH
26102db39860SNick Sayer 				       : IP_PORTRANGE_DEFAULT;
26112db39860SNick Sayer 
26122db39860SNick Sayer 	    if (setsockopt(pdata, IPPROTO_IP, IP_PORTRANGE,
261357d4ef07SYaroslav Tykhiy 			    &on, sizeof(on)) < 0)
26142db39860SNick Sayer 		    goto pasv_error;
26152db39860SNick Sayer 	}
26162db39860SNick Sayer #endif
26172db39860SNick Sayer #ifdef IPV6_PORTRANGE
26182db39860SNick Sayer 	if (ctrl_addr.su_family == AF_INET6) {
26198af7c9a3SYaroslav Tykhiy 	    on = restricted_data_ports ? IPV6_PORTRANGE_HIGH
26202db39860SNick Sayer 				       : IPV6_PORTRANGE_DEFAULT;
26212db39860SNick Sayer 
26222db39860SNick Sayer 	    if (setsockopt(pdata, IPPROTO_IPV6, IPV6_PORTRANGE,
262357d4ef07SYaroslav Tykhiy 			    &on, sizeof(on)) < 0)
26242db39860SNick Sayer 		    goto pasv_error;
26252db39860SNick Sayer 	}
26262db39860SNick Sayer #endif
26272db39860SNick Sayer 
26284dd8b5abSYoshinobu Inoue 	if (bind(pdata, (struct sockaddr *)&pasv_addr, len) < 0)
26294dd8b5abSYoshinobu Inoue 		goto pasv_error;
26304dd8b5abSYoshinobu Inoue 
26314dd8b5abSYoshinobu Inoue 	(void) seteuid((uid_t)pw->pw_uid);
26324dd8b5abSYoshinobu Inoue 
26334dd8b5abSYoshinobu Inoue 	if (getsockname(pdata, (struct sockaddr *) &pasv_addr, &len) < 0)
26344dd8b5abSYoshinobu Inoue 		goto pasv_error;
26354dd8b5abSYoshinobu Inoue 	if (listen(pdata, 1) < 0)
26364dd8b5abSYoshinobu Inoue 		goto pasv_error;
26374dd8b5abSYoshinobu Inoue 
26384dd8b5abSYoshinobu Inoue #define UC(b) (((int) b) & 0xff)
26394dd8b5abSYoshinobu Inoue 
26404dd8b5abSYoshinobu Inoue 	if (strcmp(cmd, "LPSV") == 0) {
26414dd8b5abSYoshinobu Inoue 		p = (char *)&pasv_addr.su_port;
26424dd8b5abSYoshinobu Inoue 		switch (pasv_addr.su_family) {
26434dd8b5abSYoshinobu Inoue 		case AF_INET:
26444dd8b5abSYoshinobu Inoue 			a = (char *) &pasv_addr.su_sin.sin_addr;
26454dd8b5abSYoshinobu Inoue 		v4_reply:
26464dd8b5abSYoshinobu Inoue 			reply(228,
26474dd8b5abSYoshinobu Inoue "Entering Long Passive Mode (%d,%d,%d,%d,%d,%d,%d,%d,%d)",
26484dd8b5abSYoshinobu Inoue 			      4, 4, UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
26494dd8b5abSYoshinobu Inoue 			      2, UC(p[0]), UC(p[1]));
26504dd8b5abSYoshinobu Inoue 			return;
26514dd8b5abSYoshinobu Inoue 		case AF_INET6:
26524dd8b5abSYoshinobu Inoue 			if (IN6_IS_ADDR_V4MAPPED(&pasv_addr.su_sin6.sin6_addr)) {
26534dd8b5abSYoshinobu Inoue 				a = (char *) &pasv_addr.su_sin6.sin6_addr.s6_addr[12];
26544dd8b5abSYoshinobu Inoue 				goto v4_reply;
26554dd8b5abSYoshinobu Inoue 			}
26564dd8b5abSYoshinobu Inoue 			a = (char *) &pasv_addr.su_sin6.sin6_addr;
26574dd8b5abSYoshinobu Inoue 			reply(228,
26584dd8b5abSYoshinobu Inoue "Entering Long Passive Mode "
26594dd8b5abSYoshinobu Inoue "(%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d)",
26604dd8b5abSYoshinobu Inoue 			      6, 16, UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
26614dd8b5abSYoshinobu Inoue 			      UC(a[4]), UC(a[5]), UC(a[6]), UC(a[7]),
26624dd8b5abSYoshinobu Inoue 			      UC(a[8]), UC(a[9]), UC(a[10]), UC(a[11]),
26634dd8b5abSYoshinobu Inoue 			      UC(a[12]), UC(a[13]), UC(a[14]), UC(a[15]),
26644dd8b5abSYoshinobu Inoue 			      2, UC(p[0]), UC(p[1]));
26654dd8b5abSYoshinobu Inoue 			return;
26664dd8b5abSYoshinobu Inoue 		}
26674dd8b5abSYoshinobu Inoue 	} else if (strcmp(cmd, "EPSV") == 0) {
26684dd8b5abSYoshinobu Inoue 		switch (pasv_addr.su_family) {
26694dd8b5abSYoshinobu Inoue 		case AF_INET:
26704dd8b5abSYoshinobu Inoue 		case AF_INET6:
26714dd8b5abSYoshinobu Inoue 			reply(229, "Entering Extended Passive Mode (|||%d|)",
26724dd8b5abSYoshinobu Inoue 				ntohs(pasv_addr.su_port));
26734dd8b5abSYoshinobu Inoue 			return;
26744dd8b5abSYoshinobu Inoue 		}
26754dd8b5abSYoshinobu Inoue 	} else {
26764dd8b5abSYoshinobu Inoue 		/* more proper error code? */
26774dd8b5abSYoshinobu Inoue 	}
26784dd8b5abSYoshinobu Inoue 
26794dd8b5abSYoshinobu Inoue pasv_error:
26804dd8b5abSYoshinobu Inoue 	(void) seteuid((uid_t)pw->pw_uid);
26814dd8b5abSYoshinobu Inoue 	(void) close(pdata);
26824dd8b5abSYoshinobu Inoue 	pdata = -1;
26834dd8b5abSYoshinobu Inoue 	perror_reply(425, "Can't open passive connection");
26844dd8b5abSYoshinobu Inoue 	return;
26854dd8b5abSYoshinobu Inoue }
26864dd8b5abSYoshinobu Inoue 
26874dd8b5abSYoshinobu Inoue /*
2688ea022d16SRodney W. Grimes  * Generate unique name for file with basename "local".
2689ea022d16SRodney W. Grimes  * The file named "local" is already known to exist.
2690ea022d16SRodney W. Grimes  * Generates failure reply on error.
2691ea022d16SRodney W. Grimes  */
2692ea022d16SRodney W. Grimes static char *
2693e4bc453cSWarner Losh gunique(char *local)
2694ea022d16SRodney W. Grimes {
2695ea022d16SRodney W. Grimes 	static char new[MAXPATHLEN];
2696ea022d16SRodney W. Grimes 	struct stat st;
2697ea022d16SRodney W. Grimes 	int count;
2698ea022d16SRodney W. Grimes 	char *cp;
2699ea022d16SRodney W. Grimes 
2700ea022d16SRodney W. Grimes 	cp = strrchr(local, '/');
2701ea022d16SRodney W. Grimes 	if (cp)
2702ea022d16SRodney W. Grimes 		*cp = '\0';
2703ea022d16SRodney W. Grimes 	if (stat(cp ? local : ".", &st) < 0) {
2704ea022d16SRodney W. Grimes 		perror_reply(553, cp ? local : ".");
2705ea022d16SRodney W. Grimes 		return ((char *) 0);
2706ea022d16SRodney W. Grimes 	}
2707ea022d16SRodney W. Grimes 	if (cp)
2708ea022d16SRodney W. Grimes 		*cp = '/';
2709e760ef2cSWarner Losh 	/* -4 is for the .nn<null> we put on the end below */
2710e760ef2cSWarner Losh 	(void) snprintf(new, sizeof(new) - 4, "%s", local);
2711ea022d16SRodney W. Grimes 	cp = new + strlen(new);
2712ea022d16SRodney W. Grimes 	*cp++ = '.';
2713ea022d16SRodney W. Grimes 	for (count = 1; count < 100; count++) {
2714ea022d16SRodney W. Grimes 		(void)sprintf(cp, "%d", count);
2715ea022d16SRodney W. Grimes 		if (stat(new, &st) < 0)
2716ea022d16SRodney W. Grimes 			return (new);
2717ea022d16SRodney W. Grimes 	}
2718ea022d16SRodney W. Grimes 	reply(452, "Unique file name cannot be created.");
2719ea022d16SRodney W. Grimes 	return (NULL);
2720ea022d16SRodney W. Grimes }
2721ea022d16SRodney W. Grimes 
2722ea022d16SRodney W. Grimes /*
2723ea022d16SRodney W. Grimes  * Format and send reply containing system error number.
2724ea022d16SRodney W. Grimes  */
2725ea022d16SRodney W. Grimes void
2726e4bc453cSWarner Losh perror_reply(int code, char *string)
2727ea022d16SRodney W. Grimes {
2728ea022d16SRodney W. Grimes 
2729ea022d16SRodney W. Grimes 	reply(code, "%s: %s.", string, strerror(errno));
2730ea022d16SRodney W. Grimes }
2731ea022d16SRodney W. Grimes 
2732ea022d16SRodney W. Grimes static char *onefile[] = {
2733ea022d16SRodney W. Grimes 	"",
2734ea022d16SRodney W. Grimes 	0
2735ea022d16SRodney W. Grimes };
2736ea022d16SRodney W. Grimes 
2737ea022d16SRodney W. Grimes void
2738e4bc453cSWarner Losh send_file_list(char *whichf)
2739ea022d16SRodney W. Grimes {
2740ea022d16SRodney W. Grimes 	struct stat st;
2741ea022d16SRodney W. Grimes 	DIR *dirp = NULL;
2742ea022d16SRodney W. Grimes 	struct dirent *dir;
2743ea022d16SRodney W. Grimes 	FILE *dout = NULL;
2744ea022d16SRodney W. Grimes 	char **dirlist, *dirname;
2745ea022d16SRodney W. Grimes 	int simple = 0;
2746ea022d16SRodney W. Grimes 	int freeglob = 0;
2747ea022d16SRodney W. Grimes 	glob_t gl;
2748ea022d16SRodney W. Grimes 
2749ea022d16SRodney W. Grimes 	if (strpbrk(whichf, "~{[*?") != NULL) {
275012da320bSMike Heffner 		int flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_TILDE;
2751ea022d16SRodney W. Grimes 
2752ea022d16SRodney W. Grimes 		memset(&gl, 0, sizeof(gl));
27536d10cb2fSJonathan Lemon 		gl.gl_matchc = MAXGLOBARGS;
275475dc5f1aSMike Heffner 		flags |= GLOB_LIMIT;
2755ea022d16SRodney W. Grimes 		freeglob = 1;
2756ea022d16SRodney W. Grimes 		if (glob(whichf, flags, 0, &gl)) {
2757ea022d16SRodney W. Grimes 			reply(550, "not found");
2758ea022d16SRodney W. Grimes 			goto out;
2759ea022d16SRodney W. Grimes 		} else if (gl.gl_pathc == 0) {
2760ea022d16SRodney W. Grimes 			errno = ENOENT;
2761ea022d16SRodney W. Grimes 			perror_reply(550, whichf);
2762ea022d16SRodney W. Grimes 			goto out;
2763ea022d16SRodney W. Grimes 		}
2764ea022d16SRodney W. Grimes 		dirlist = gl.gl_pathv;
2765ea022d16SRodney W. Grimes 	} else {
2766ea022d16SRodney W. Grimes 		onefile[0] = whichf;
2767ea022d16SRodney W. Grimes 		dirlist = onefile;
2768ea022d16SRodney W. Grimes 		simple = 1;
2769ea022d16SRodney W. Grimes 	}
2770ea022d16SRodney W. Grimes 
27719aca17cbSMark Murray 	while ((dirname = *dirlist++)) {
2772ea022d16SRodney W. Grimes 		if (stat(dirname, &st) < 0) {
2773ea022d16SRodney W. Grimes 			/*
2774ea022d16SRodney W. Grimes 			 * If user typed "ls -l", etc, and the client
2775ea022d16SRodney W. Grimes 			 * used NLST, do what the user meant.
2776ea022d16SRodney W. Grimes 			 */
2777ea022d16SRodney W. Grimes 			if (dirname[0] == '-' && *dirlist == NULL &&
2778ea022d16SRodney W. Grimes 			    transflag == 0) {
2779af85d782SDavid Nugent 				retrieve(_PATH_LS " %s", dirname);
2780ea022d16SRodney W. Grimes 				goto out;
2781ea022d16SRodney W. Grimes 			}
2782ea022d16SRodney W. Grimes 			perror_reply(550, whichf);
2783ea022d16SRodney W. Grimes 			if (dout != NULL) {
2784ea022d16SRodney W. Grimes 				(void) fclose(dout);
2785ea022d16SRodney W. Grimes 				transflag = 0;
2786ea022d16SRodney W. Grimes 				data = -1;
2787ea022d16SRodney W. Grimes 				pdata = -1;
2788ea022d16SRodney W. Grimes 			}
2789ea022d16SRodney W. Grimes 			goto out;
2790ea022d16SRodney W. Grimes 		}
2791ea022d16SRodney W. Grimes 
2792ea022d16SRodney W. Grimes 		if (S_ISREG(st.st_mode)) {
2793ea022d16SRodney W. Grimes 			if (dout == NULL) {
2794ea022d16SRodney W. Grimes 				dout = dataconn("file list", (off_t)-1, "w");
2795ea022d16SRodney W. Grimes 				if (dout == NULL)
2796ea022d16SRodney W. Grimes 					goto out;
2797ea022d16SRodney W. Grimes 				transflag++;
2798ea022d16SRodney W. Grimes 			}
2799ea022d16SRodney W. Grimes 			fprintf(dout, "%s%s\n", dirname,
2800ea022d16SRodney W. Grimes 				type == TYPE_A ? "\r" : "");
2801ea022d16SRodney W. Grimes 			byte_count += strlen(dirname) + 1;
2802ea022d16SRodney W. Grimes 			continue;
2803ea022d16SRodney W. Grimes 		} else if (!S_ISDIR(st.st_mode))
2804ea022d16SRodney W. Grimes 			continue;
2805ea022d16SRodney W. Grimes 
2806ea022d16SRodney W. Grimes 		if ((dirp = opendir(dirname)) == NULL)
2807ea022d16SRodney W. Grimes 			continue;
2808ea022d16SRodney W. Grimes 
2809ea022d16SRodney W. Grimes 		while ((dir = readdir(dirp)) != NULL) {
2810ea022d16SRodney W. Grimes 			char nbuf[MAXPATHLEN];
2811ea022d16SRodney W. Grimes 
28124b82fc95SYaroslav Tykhiy 			if (recvurg) {
28134b82fc95SYaroslav Tykhiy 				myoob();
28144b82fc95SYaroslav Tykhiy 				recvurg = 0;
28154b82fc95SYaroslav Tykhiy 				transflag = 0;
28164b82fc95SYaroslav Tykhiy 				goto out;
28174b82fc95SYaroslav Tykhiy 			}
28184b82fc95SYaroslav Tykhiy 
2819ea022d16SRodney W. Grimes 			if (dir->d_name[0] == '.' && dir->d_namlen == 1)
2820ea022d16SRodney W. Grimes 				continue;
2821ea022d16SRodney W. Grimes 			if (dir->d_name[0] == '.' && dir->d_name[1] == '.' &&
2822ea022d16SRodney W. Grimes 			    dir->d_namlen == 2)
2823ea022d16SRodney W. Grimes 				continue;
2824ea022d16SRodney W. Grimes 
2825e760ef2cSWarner Losh 			snprintf(nbuf, sizeof(nbuf),
2826e760ef2cSWarner Losh 				"%s/%s", dirname, dir->d_name);
2827ea022d16SRodney W. Grimes 
2828ea022d16SRodney W. Grimes 			/*
2829ea022d16SRodney W. Grimes 			 * We have to do a stat to insure it's
2830ea022d16SRodney W. Grimes 			 * not a directory or special file.
2831ea022d16SRodney W. Grimes 			 */
2832ea022d16SRodney W. Grimes 			if (simple || (stat(nbuf, &st) == 0 &&
2833ea022d16SRodney W. Grimes 			    S_ISREG(st.st_mode))) {
2834ea022d16SRodney W. Grimes 				if (dout == NULL) {
2835ea022d16SRodney W. Grimes 					dout = dataconn("file list", (off_t)-1,
2836ea022d16SRodney W. Grimes 						"w");
2837ea022d16SRodney W. Grimes 					if (dout == NULL)
2838ea022d16SRodney W. Grimes 						goto out;
2839ea022d16SRodney W. Grimes 					transflag++;
2840ea022d16SRodney W. Grimes 				}
2841ea022d16SRodney W. Grimes 				if (nbuf[0] == '.' && nbuf[1] == '/')
2842ea022d16SRodney W. Grimes 					fprintf(dout, "%s%s\n", &nbuf[2],
2843ea022d16SRodney W. Grimes 						type == TYPE_A ? "\r" : "");
2844ea022d16SRodney W. Grimes 				else
2845ea022d16SRodney W. Grimes 					fprintf(dout, "%s%s\n", nbuf,
2846ea022d16SRodney W. Grimes 						type == TYPE_A ? "\r" : "");
2847ea022d16SRodney W. Grimes 				byte_count += strlen(nbuf) + 1;
2848ea022d16SRodney W. Grimes 			}
2849ea022d16SRodney W. Grimes 		}
2850ea022d16SRodney W. Grimes 		(void) closedir(dirp);
2851ea022d16SRodney W. Grimes 	}
2852ea022d16SRodney W. Grimes 
2853ea022d16SRodney W. Grimes 	if (dout == NULL)
2854ea022d16SRodney W. Grimes 		reply(550, "No files found.");
2855ea022d16SRodney W. Grimes 	else if (ferror(dout) != 0)
2856ea022d16SRodney W. Grimes 		perror_reply(550, "Data connection");
2857ea022d16SRodney W. Grimes 	else
2858ea022d16SRodney W. Grimes 		reply(226, "Transfer complete.");
2859ea022d16SRodney W. Grimes 
2860ea022d16SRodney W. Grimes 	transflag = 0;
2861ea022d16SRodney W. Grimes 	if (dout != NULL)
2862ea022d16SRodney W. Grimes 		(void) fclose(dout);
2863ea022d16SRodney W. Grimes 	data = -1;
2864ea022d16SRodney W. Grimes 	pdata = -1;
2865ea022d16SRodney W. Grimes out:
2866ea022d16SRodney W. Grimes 	if (freeglob) {
2867ea022d16SRodney W. Grimes 		freeglob = 0;
2868ea022d16SRodney W. Grimes 		globfree(&gl);
2869ea022d16SRodney W. Grimes 	}
2870ea022d16SRodney W. Grimes }
2871ea022d16SRodney W. Grimes 
2872cf09a206SDavid Greenman void
2873e4bc453cSWarner Losh reapchild(int signo)
2874cf09a206SDavid Greenman {
2875cf09a206SDavid Greenman 	while (wait3(NULL, WNOHANG, NULL) > 0);
2876cf09a206SDavid Greenman }
2877cf09a206SDavid Greenman 
2878b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE
2879ea022d16SRodney W. Grimes /*
2880ea022d16SRodney W. Grimes  * Clobber argv so ps will show what we're doing.  (Stolen from sendmail.)
2881ea022d16SRodney W. Grimes  * Warning, since this is usually started from inetd.conf, it often doesn't
2882ea022d16SRodney W. Grimes  * have much of an environment or arglist to overwrite.
2883ea022d16SRodney W. Grimes  */
2884ea022d16SRodney W. Grimes void
2885ea022d16SRodney W. Grimes setproctitle(const char *fmt, ...)
2886ea022d16SRodney W. Grimes {
2887ea022d16SRodney W. Grimes 	int i;
2888ea022d16SRodney W. Grimes 	va_list ap;
2889ea022d16SRodney W. Grimes 	char *p, *bp, ch;
2890ea022d16SRodney W. Grimes 	char buf[LINE_MAX];
2891ea022d16SRodney W. Grimes 
2892ea022d16SRodney W. Grimes 	va_start(ap, fmt);
2893ea022d16SRodney W. Grimes 	(void)vsnprintf(buf, sizeof(buf), fmt, ap);
2894ea022d16SRodney W. Grimes 
2895ea022d16SRodney W. Grimes 	/* make ps print our process name */
2896ea022d16SRodney W. Grimes 	p = Argv[0];
2897ea022d16SRodney W. Grimes 	*p++ = '-';
2898ea022d16SRodney W. Grimes 
2899ea022d16SRodney W. Grimes 	i = strlen(buf);
2900ea022d16SRodney W. Grimes 	if (i > LastArgv - p - 2) {
2901ea022d16SRodney W. Grimes 		i = LastArgv - p - 2;
2902ea022d16SRodney W. Grimes 		buf[i] = '\0';
2903ea022d16SRodney W. Grimes 	}
2904ea022d16SRodney W. Grimes 	bp = buf;
2905ea022d16SRodney W. Grimes 	while (ch = *bp++)
2906ea022d16SRodney W. Grimes 		if (ch != '\n' && ch != '\r')
2907ea022d16SRodney W. Grimes 			*p++ = ch;
2908ea022d16SRodney W. Grimes 	while (p < LastArgv)
2909ea022d16SRodney W. Grimes 		*p++ = ' ';
2910ea022d16SRodney W. Grimes }
2911b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */
29123eb568f2SGuido van Rooij 
291361f891a6SPaul Traina static void
2914e4bc453cSWarner Losh logxfer(char *name, off_t size, time_t start)
29153eb568f2SGuido van Rooij {
29163eb568f2SGuido van Rooij 	char buf[1024];
29173eb568f2SGuido van Rooij 	char path[MAXPATHLEN + 1];
2918158a00b2SJohn Birrell 	time_t now;
29193eb568f2SGuido van Rooij 
29203eb568f2SGuido van Rooij 	if (statfd >= 0 && getwd(path) != NULL) {
29213eb568f2SGuido van Rooij 		time(&now);
2922e4a71114SAndrey A. Chernov 		snprintf(buf, sizeof(buf), "%.20s!%s!%s!%s/%s!%qd!%ld\n",
29233eb568f2SGuido van Rooij 			ctime(&now)+4, ident, remotehost,
2924e4a71114SAndrey A. Chernov 			path, name, (long long)size,
2925e4a71114SAndrey A. Chernov 			(long)(now - start + (now == start)));
29263eb568f2SGuido van Rooij 		write(statfd, buf, strlen(buf));
29273eb568f2SGuido van Rooij 	}
29283eb568f2SGuido van Rooij }
2929e4648f05SYaroslav Tykhiy 
2930e4648f05SYaroslav Tykhiy static char *
2931e4648f05SYaroslav Tykhiy doublequote(char *s)
2932e4648f05SYaroslav Tykhiy {
2933e4648f05SYaroslav Tykhiy 	int n;
2934e4648f05SYaroslav Tykhiy 	char *p, *s2;
2935e4648f05SYaroslav Tykhiy 
2936e4648f05SYaroslav Tykhiy 	for (p = s, n = 0; *p; p++)
2937e4648f05SYaroslav Tykhiy 		if (*p == '"')
2938e4648f05SYaroslav Tykhiy 			n++;
2939e4648f05SYaroslav Tykhiy 
2940e4648f05SYaroslav Tykhiy 	if ((s2 = malloc(p - s + n + 1)) == NULL)
2941e4648f05SYaroslav Tykhiy 		return (NULL);
2942e4648f05SYaroslav Tykhiy 
2943e4648f05SYaroslav Tykhiy 	for (p = s2; *s; s++, p++) {
2944e4648f05SYaroslav Tykhiy 		if ((*p = *s) == '"')
2945e4648f05SYaroslav Tykhiy 			*(++p) = '"';
2946e4648f05SYaroslav Tykhiy 	}
2947e4648f05SYaroslav Tykhiy 	*p = '\0';
2948e4648f05SYaroslav Tykhiy 
2949e4648f05SYaroslav Tykhiy 	return (s2);
2950e4648f05SYaroslav Tykhiy }
2951