xref: /freebsd/libexec/ftpd/ftpd.c (revision 504422fab39385b8a0e6662b1dcc8ba5b0eacb1f)
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.
135efaea4cSChristian Brueffer  * 3. Neither the name of the University nor the names of its contributors
14ea022d16SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
15ea022d16SRodney W. Grimes  *    without specific prior written permission.
16ea022d16SRodney W. Grimes  *
17ea022d16SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18ea022d16SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19ea022d16SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20ea022d16SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21ea022d16SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22ea022d16SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23ea022d16SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24ea022d16SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25ea022d16SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26ea022d16SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27ea022d16SRodney W. Grimes  * SUCH DAMAGE.
28ea022d16SRodney W. Grimes  */
29ea022d16SRodney W. Grimes 
309aca17cbSMark Murray #if 0
31ea022d16SRodney W. Grimes #ifndef lint
32ea022d16SRodney W. Grimes static char copyright[] =
33ea022d16SRodney W. Grimes "@(#) Copyright (c) 1985, 1988, 1990, 1992, 1993, 1994\n\
34ea022d16SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
35ea022d16SRodney W. Grimes #endif /* not lint */
369aca17cbSMark Murray #endif
37ea022d16SRodney W. Grimes 
38ea022d16SRodney W. Grimes #ifndef lint
39e02897faSPhilippe Charnier #if 0
40ea022d16SRodney W. Grimes static char sccsid[] = "@(#)ftpd.c	8.4 (Berkeley) 4/16/94";
419aca17cbSMark Murray #endif
42e02897faSPhilippe Charnier #endif /* not lint */
43ea022d16SRodney W. Grimes 
440c4b401fSYaroslav Tykhiy #include <sys/cdefs.h>
450c4b401fSYaroslav Tykhiy __FBSDID("$FreeBSD$");
460c4b401fSYaroslav Tykhiy 
47ea022d16SRodney W. Grimes /*
48ea022d16SRodney W. Grimes  * FTP server.
49ea022d16SRodney W. Grimes  */
50ea022d16SRodney W. Grimes #include <sys/param.h>
51ea022d16SRodney W. Grimes #include <sys/ioctl.h>
529fc5823aSGarrett Wollman #include <sys/mman.h>
53eb2fc780SGarrett Wollman #include <sys/socket.h>
54eb2fc780SGarrett Wollman #include <sys/stat.h>
55eb2fc780SGarrett Wollman #include <sys/time.h>
56eb2fc780SGarrett Wollman #include <sys/wait.h>
57ea022d16SRodney W. Grimes 
58ea022d16SRodney W. Grimes #include <netinet/in.h>
59ea022d16SRodney W. Grimes #include <netinet/in_systm.h>
60ea022d16SRodney W. Grimes #include <netinet/ip.h>
619fc5823aSGarrett Wollman #include <netinet/tcp.h>
62ea022d16SRodney W. Grimes 
63ea022d16SRodney W. Grimes #define	FTP_NAMES
64ea022d16SRodney W. Grimes #include <arpa/ftp.h>
65ea022d16SRodney W. Grimes #include <arpa/inet.h>
66ea022d16SRodney W. Grimes #include <arpa/telnet.h>
67ea022d16SRodney W. Grimes 
68ea022d16SRodney W. Grimes #include <ctype.h>
69ea022d16SRodney W. Grimes #include <dirent.h>
70ea022d16SRodney W. Grimes #include <err.h>
71ea022d16SRodney W. Grimes #include <errno.h>
72ea022d16SRodney W. Grimes #include <fcntl.h>
73ea022d16SRodney W. Grimes #include <glob.h>
74ea022d16SRodney W. Grimes #include <limits.h>
75ea022d16SRodney W. Grimes #include <netdb.h>
76ea022d16SRodney W. Grimes #include <pwd.h>
7731fea7b8SDavid Nugent #include <grp.h>
7847499ecdSAndrey A. Chernov #include <opie.h>
79ea022d16SRodney W. Grimes #include <signal.h>
80a57e1ef0SYaroslav Tykhiy #include <stdint.h>
81ea022d16SRodney W. Grimes #include <stdio.h>
82ea022d16SRodney W. Grimes #include <stdlib.h>
83ea022d16SRodney W. Grimes #include <string.h>
84ea022d16SRodney W. Grimes #include <syslog.h>
85ea022d16SRodney W. Grimes #include <time.h>
86ea022d16SRodney W. Grimes #include <unistd.h>
87b63e1fe2SPeter Wemm #include <libutil.h>
88b071c689SDavid Nugent #ifdef	LOGIN_CAP
89b071c689SDavid Nugent #include <login_cap.h>
90b071c689SDavid Nugent #endif
91ea022d16SRodney W. Grimes 
925bc9d93dSMark Murray #ifdef USE_PAM
936c9134c0SMark Murray #include <security/pam_appl.h>
946c9134c0SMark Murray #endif
956c9134c0SMark Murray 
963656f229SKurt Lidl #ifdef USE_BLACKLIST
973656f229SKurt Lidl #include "blacklist_client.h"
983656f229SKurt Lidl #endif
993656f229SKurt Lidl 
100ea022d16SRodney W. Grimes #include "pathnames.h"
101ea022d16SRodney W. Grimes #include "extern.h"
102ea022d16SRodney W. Grimes 
103ea022d16SRodney W. Grimes #include <stdarg.h>
104ea022d16SRodney W. Grimes 
105af85d782SDavid Nugent static char version[] = "Version 6.00LS";
106af85d782SDavid Nugent #undef main
107ea022d16SRodney W. Grimes 
1084dd8b5abSYoshinobu Inoue union sockunion ctrl_addr;
1094dd8b5abSYoshinobu Inoue union sockunion data_source;
1104dd8b5abSYoshinobu Inoue union sockunion data_dest;
1114dd8b5abSYoshinobu Inoue union sockunion his_addr;
1124dd8b5abSYoshinobu Inoue union sockunion pasv_addr;
113ea022d16SRodney W. Grimes 
114cf09a206SDavid Greenman int	daemon_mode;
115ea022d16SRodney W. Grimes int	data;
11663591ba5SYaroslav Tykhiy int	dataport;
117c152df28SYaroslav Tykhiy int	hostinfo = 1;	/* print host-specific info in messages */
118ea022d16SRodney W. Grimes int	logged_in;
119ea022d16SRodney W. Grimes struct	passwd *pw;
120ce9287fcSYaroslav Tykhiy char	*homedir;
121618b0bbaSMark Murray int	ftpdebug;
122ea022d16SRodney W. Grimes int	timeout = 900;    /* timeout after 15 minutes of inactivity */
123ea022d16SRodney W. Grimes int	maxtimeout = 7200;/* don't allow idle time to be set beyond 2 hours */
124ea022d16SRodney W. Grimes int	logging;
1254c450ad7SPaul Traina int	restricted_data_ports = 1;
126a5a4544eSPaul Traina int	paranoid = 1;	  /* be extra careful about security */
1275a392aecSTorsten Blum int	anon_only = 0;    /* Only anonymous ftp allowed */
1282ea42282SYaroslav Tykhiy int	assumeutf8 = 0;   /* Assume that server file names are in UTF-8 */
129ea022d16SRodney W. Grimes int	guest;
130a5a4544eSPaul Traina int	dochroot;
131215a9f9dSYaroslav Tykhiy char	*chrootdir;
1325d7e0128SYaroslav Tykhiy int	dowtmp = 1;
1333eb568f2SGuido van Rooij int	stats;
1343eb568f2SGuido van Rooij int	statfd = -1;
135ea022d16SRodney W. Grimes int	type;
136ea022d16SRodney W. Grimes int	form;
137ea022d16SRodney W. Grimes int	stru;			/* avoid C keyword */
138ea022d16SRodney W. Grimes int	mode;
139ea022d16SRodney W. Grimes int	usedefault = 1;		/* for data transfers */
140ea022d16SRodney W. Grimes int	pdata = -1;		/* for passive mode */
141a4b77a2aSPoul-Henning Kamp int	readonly = 0;		/* Server is in readonly mode.	*/
142a4b77a2aSPoul-Henning Kamp int	noepsv = 0;		/* EPSV command is disabled.	*/
14362513e76SNik Clayton int	noretr = 0;		/* RETR command is disabled.	*/
1441cc9f0bbSSheldon Hearn int	noguestretr = 0;	/* RETR command is disabled for anon users. */
145d186bb12SMatthew N. Dodd int	noguestmkd = 0;		/* MKD command is disabled for anon users. */
146a117c345SYaroslav Tykhiy int	noguestmod = 1;		/* anon users may not modify existing files. */
14762513e76SNik Clayton 
148ea022d16SRodney W. Grimes off_t	file_size;
149ea022d16SRodney W. Grimes off_t	byte_count;
150ea022d16SRodney W. Grimes #if !defined(CMASK) || CMASK == 0
151ea022d16SRodney W. Grimes #undef CMASK
152ea022d16SRodney W. Grimes #define CMASK 027
153ea022d16SRodney W. Grimes #endif
154ea022d16SRodney W. Grimes int	defumask = CMASK;		/* default umask value */
155ea022d16SRodney W. Grimes char	tmpline[7];
156ea4e54b9SDavid Nugent char	*hostname;
157ad442344SDima Dorfman int	epsvall = 0;
158ad442344SDima Dorfman 
1590512556aSDavid Nugent #ifdef VIRTUAL_HOSTING
160ea4e54b9SDavid Nugent char	*ftpuser;
161ea4e54b9SDavid Nugent 
162ea4e54b9SDavid Nugent static struct ftphost {
163ea4e54b9SDavid Nugent 	struct ftphost	*next;
164f38c6cadSYoshinobu Inoue 	struct addrinfo *hostinfo;
165ea4e54b9SDavid Nugent 	char		*hostname;
166ea4e54b9SDavid Nugent 	char		*anonuser;
167ea4e54b9SDavid Nugent 	char		*statfile;
168ea4e54b9SDavid Nugent 	char		*welcome;
169ea4e54b9SDavid Nugent 	char		*loginmsg;
170ea4e54b9SDavid Nugent } *thishost, *firsthost;
171ea4e54b9SDavid Nugent 
172ea4e54b9SDavid Nugent #endif
17304683b2cSYaroslav Tykhiy char	remotehost[NI_MAXHOST];
1743eb568f2SGuido van Rooij char	*ident = NULL;
175a5a4544eSPaul Traina 
17680643af0SEd Schouten static char	wtmpid[20];
177a5a4544eSPaul Traina 
1785bc9d93dSMark Murray #ifdef USE_PAM
179e4bc453cSWarner Losh static int	auth_pam(struct passwd**, const char*);
1805bc9d93dSMark Murray pam_handle_t	*pamh = NULL;
18147499ecdSAndrey A. Chernov #endif
182fa1746c9SMark Murray 
183fa1746c9SMark Murray static struct opie	opiedata;
184fa1746c9SMark Murray static char		opieprompt[OPIE_CHALLENGE_MAX+1];
18547499ecdSAndrey A. Chernov static int		pwok;
1869aca17cbSMark Murray 
187125b9635SYaroslav Tykhiy char	*pid_file = NULL; /* means default location to pidfile(3) */
188105a3c98SJulian Elischer 
189ea022d16SRodney W. Grimes /*
1906d10cb2fSJonathan Lemon  * Limit number of pathnames that glob can return.
1916d10cb2fSJonathan Lemon  * A limit of 0 indicates the number of pathnames is unlimited.
1926d10cb2fSJonathan Lemon  */
1936d10cb2fSJonathan Lemon #define MAXGLOBARGS	16384
1946d10cb2fSJonathan Lemon #
1956d10cb2fSJonathan Lemon 
1966d10cb2fSJonathan Lemon /*
197ea022d16SRodney W. Grimes  * Timeout intervals for retrying connections
198ea022d16SRodney W. Grimes  * to hosts that don't accept PORT cmds.  This
199ea022d16SRodney W. Grimes  * is a kludge, but given the problems with TCP...
200ea022d16SRodney W. Grimes  */
201ea022d16SRodney W. Grimes #define	SWAITMAX	90	/* wait at most 90 seconds */
202ea022d16SRodney W. Grimes #define	SWAITINT	5	/* interval between retries */
203ea022d16SRodney W. Grimes 
204ea022d16SRodney W. Grimes int	swaitmax = SWAITMAX;
205ea022d16SRodney W. Grimes int	swaitint = SWAITINT;
206ea022d16SRodney W. Grimes 
207ea022d16SRodney W. Grimes #ifdef SETPROCTITLE
208b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE
209ea022d16SRodney W. Grimes char	**Argv = NULL;		/* pointer to argument vector */
210ea022d16SRodney W. Grimes char	*LastArgv = NULL;	/* end of argv */
211b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */
212ea022d16SRodney W. Grimes char	proctitle[LINE_MAX];	/* initial part of title */
213ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */
214ea022d16SRodney W. Grimes 
2156b2dee6bSYaroslav Tykhiy #define LOGCMD(cmd, file)		logcmd((cmd), (file), NULL, -1)
2166b2dee6bSYaroslav Tykhiy #define LOGCMD2(cmd, file1, file2)	logcmd((cmd), (file1), (file2), -1)
2176b2dee6bSYaroslav Tykhiy #define LOGBYTES(cmd, file, cnt)	logcmd((cmd), (file), NULL, (cnt))
218ea022d16SRodney W. Grimes 
2194cd51076SYaroslav Tykhiy static	volatile sig_atomic_t recvurg;
2204cd51076SYaroslav Tykhiy static	int transflag;		/* NB: for debugging only */
2214cd51076SYaroslav Tykhiy 
2224cd51076SYaroslav Tykhiy #define STARTXFER	flagxfer(1)
2234cd51076SYaroslav Tykhiy #define ENDXFER		flagxfer(0)
2244cd51076SYaroslav Tykhiy 
2254cd51076SYaroslav Tykhiy #define START_UNSAFE	maskurg(1)
2264cd51076SYaroslav Tykhiy #define END_UNSAFE	maskurg(0)
2274cd51076SYaroslav Tykhiy 
2284cd51076SYaroslav Tykhiy /* It's OK to put an `else' clause after this macro. */
2294cd51076SYaroslav Tykhiy #define CHECKOOB(action)						\
2304cd51076SYaroslav Tykhiy 	if (recvurg) {							\
2314cd51076SYaroslav Tykhiy 		recvurg = 0;						\
2324cd51076SYaroslav Tykhiy 		if (myoob()) {						\
2334cd51076SYaroslav Tykhiy 			ENDXFER;					\
2344cd51076SYaroslav Tykhiy 			action;						\
2354cd51076SYaroslav Tykhiy 		}							\
2364cd51076SYaroslav Tykhiy 	}
2374cd51076SYaroslav Tykhiy 
238ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
2392c9fd5f2SHajimu UMEMOTO static void	 inithosts(int);
240e4bc453cSWarner Losh static void	 selecthost(union sockunion *);
241ea4e54b9SDavid Nugent #endif
242e4bc453cSWarner Losh static void	 ack(char *);
243e4bc453cSWarner Losh static void	 sigurg(int);
2444cd51076SYaroslav Tykhiy static void	 maskurg(int);
2454cd51076SYaroslav Tykhiy static void	 flagxfer(int);
2464cd51076SYaroslav Tykhiy static int	 myoob(void);
247cefb6785SChristian S.J. Peron static int	 checkuser(char *, char *, int, char **, int *);
248e4bc453cSWarner Losh static FILE	*dataconn(char *, off_t, char *);
249e4bc453cSWarner Losh static void	 dolog(struct sockaddr *);
250e4bc453cSWarner Losh static void	 end_login(void);
251e4bc453cSWarner Losh static FILE	*getdatasock(char *);
252a117c345SYaroslav Tykhiy static int	 guniquefd(char *, char **);
253e4bc453cSWarner Losh static void	 lostconn(int);
254e4bc453cSWarner Losh static void	 sigquit(int);
255e4bc453cSWarner Losh static int	 receive_data(FILE *, FILE *);
2566e4b0a55SYaroslav Tykhiy static int	 send_data(FILE *, FILE *, size_t, off_t, int);
257ea022d16SRodney W. Grimes static struct passwd *
258e4bc453cSWarner Losh 		 sgetpwnam(char *);
259e4bc453cSWarner Losh static char	*sgetsave(char *);
260e4bc453cSWarner Losh static void	 reapchild(int);
261eb5b2bb3SYaroslav Tykhiy static void	 appendf(char **, char *, ...) __printflike(2, 3);
2626b2dee6bSYaroslav Tykhiy static void	 logcmd(char *, char *, char *, off_t);
263e4bc453cSWarner Losh static void      logxfer(char *, off_t, time_t);
264e4648f05SYaroslav Tykhiy static char	*doublequote(char *);
265206fe568SHajimu UMEMOTO static int	*socksetup(int, char *, const char *);
266ea022d16SRodney W. Grimes 
267ea022d16SRodney W. Grimes int
268e4bc453cSWarner Losh main(int argc, char *argv[], char **envp)
269ea022d16SRodney W. Grimes {
27078e3eed0SStefan Farfeleder 	socklen_t addrlen;
271*504422faSKurt Lidl 	int ch, on = 1, tos, s = STDIN_FILENO;
272ea022d16SRodney W. Grimes 	char *cp, line[LINE_MAX];
273ea022d16SRodney W. Grimes 	FILE *fd;
2744dd8b5abSYoshinobu Inoue 	char	*bindname = NULL;
27563591ba5SYaroslav Tykhiy 	const char *bindport = "ftp";
2764dd8b5abSYoshinobu Inoue 	int	family = AF_UNSPEC;
2774b82fc95SYaroslav Tykhiy 	struct sigaction sa;
278ea022d16SRodney W. Grimes 
27934d1ba5cSAndrey A. Chernov 	tzset();		/* in case no timezone database in ~ftp */
2804b82fc95SYaroslav Tykhiy 	sigemptyset(&sa.sa_mask);
2814b82fc95SYaroslav Tykhiy 	sa.sa_flags = SA_RESTART;
28234d1ba5cSAndrey A. Chernov 
283b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE
284ea022d16SRodney W. Grimes 	/*
285ea022d16SRodney W. Grimes 	 *  Save start and extent of argv for setproctitle.
286ea022d16SRodney W. Grimes 	 */
287ea022d16SRodney W. Grimes 	Argv = argv;
288ea022d16SRodney W. Grimes 	while (*envp)
289ea022d16SRodney W. Grimes 		envp++;
290ea022d16SRodney W. Grimes 	LastArgv = envp[-1] + strlen(envp[-1]);
291b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */
292ea022d16SRodney W. Grimes 
2936c98f401SYaroslav Tykhiy 	/*
2946c98f401SYaroslav Tykhiy 	 * Prevent diagnostic messages from appearing on stderr.
2956c98f401SYaroslav Tykhiy 	 * We run as a daemon or from inetd; in both cases, there's
2966c98f401SYaroslav Tykhiy 	 * more reason in logging to syslog.
2976c98f401SYaroslav Tykhiy 	 */
2986c98f401SYaroslav Tykhiy 	(void) freopen(_PATH_DEVNULL, "w", stderr);
2996c98f401SYaroslav Tykhiy 	opterr = 0;
3006c98f401SYaroslav Tykhiy 
3016c98f401SYaroslav Tykhiy 	/*
3026c98f401SYaroslav Tykhiy 	 * LOG_NDELAY sets up the logging connection immediately,
3036c98f401SYaroslav Tykhiy 	 * necessary for anonymous ftp's that chroot and can't do it later.
3046c98f401SYaroslav Tykhiy 	 */
3056c98f401SYaroslav Tykhiy 	openlog("ftpd", LOG_PID | LOG_NDELAY, LOG_FTP);
3063eb568f2SGuido van Rooij 
307c152df28SYaroslav Tykhiy 	while ((ch = getopt(argc, argv,
3082ea42282SYaroslav Tykhiy 	                    "468a:AdDEhlmMoOp:P:rRSt:T:u:UvW")) != -1) {
309ea022d16SRodney W. Grimes 		switch (ch) {
3100e063efeSYaroslav Tykhiy 		case '4':
311206fe568SHajimu UMEMOTO 			family = (family == AF_INET6) ? AF_UNSPEC : AF_INET;
3120e063efeSYaroslav Tykhiy 			break;
3130e063efeSYaroslav Tykhiy 
3140e063efeSYaroslav Tykhiy 		case '6':
315206fe568SHajimu UMEMOTO 			family = (family == AF_INET) ? AF_UNSPEC : AF_INET6;
3160e063efeSYaroslav Tykhiy 			break;
3170e063efeSYaroslav Tykhiy 
3182ea42282SYaroslav Tykhiy 		case '8':
3192ea42282SYaroslav Tykhiy 			assumeutf8 = 1;
3202ea42282SYaroslav Tykhiy 			break;
3212ea42282SYaroslav Tykhiy 
3220e063efeSYaroslav Tykhiy 		case 'a':
3230e063efeSYaroslav Tykhiy 			bindname = optarg;
3240e063efeSYaroslav Tykhiy 			break;
3250e063efeSYaroslav Tykhiy 
3260e063efeSYaroslav Tykhiy 		case 'A':
3270e063efeSYaroslav Tykhiy 			anon_only = 1;
328cf09a206SDavid Greenman 			break;
329cf09a206SDavid Greenman 
330ea022d16SRodney W. Grimes 		case 'd':
331618b0bbaSMark Murray 			ftpdebug++;
332ea022d16SRodney W. Grimes 			break;
333ea022d16SRodney W. Grimes 
3340e063efeSYaroslav Tykhiy 		case 'D':
3350e063efeSYaroslav Tykhiy 			daemon_mode++;
3360e063efeSYaroslav Tykhiy 			break;
3370e063efeSYaroslav Tykhiy 
338a4b77a2aSPoul-Henning Kamp 		case 'E':
339a4b77a2aSPoul-Henning Kamp 			noepsv = 1;
340a4b77a2aSPoul-Henning Kamp 			break;
341a4b77a2aSPoul-Henning Kamp 
342c152df28SYaroslav Tykhiy 		case 'h':
343c152df28SYaroslav Tykhiy 			hostinfo = 0;
344c152df28SYaroslav Tykhiy 			break;
345c152df28SYaroslav Tykhiy 
346ea022d16SRodney W. Grimes 		case 'l':
347ea022d16SRodney W. Grimes 			logging++;	/* > 1 == extra logging */
348ea022d16SRodney W. Grimes 			break;
349ea022d16SRodney W. Grimes 
350a117c345SYaroslav Tykhiy 		case 'm':
351a117c345SYaroslav Tykhiy 			noguestmod = 0;
352a117c345SYaroslav Tykhiy 			break;
353a117c345SYaroslav Tykhiy 
3540e063efeSYaroslav Tykhiy 		case 'M':
3550e063efeSYaroslav Tykhiy 			noguestmkd = 1;
3560e063efeSYaroslav Tykhiy 			break;
3570e063efeSYaroslav Tykhiy 
3580e063efeSYaroslav Tykhiy 		case 'o':
3590e063efeSYaroslav Tykhiy 			noretr = 1;
3600e063efeSYaroslav Tykhiy 			break;
3610e063efeSYaroslav Tykhiy 
3620e063efeSYaroslav Tykhiy 		case 'O':
3630e063efeSYaroslav Tykhiy 			noguestretr = 1;
3640e063efeSYaroslav Tykhiy 			break;
3650e063efeSYaroslav Tykhiy 
3660e063efeSYaroslav Tykhiy 		case 'p':
3670e063efeSYaroslav Tykhiy 			pid_file = optarg;
3680e063efeSYaroslav Tykhiy 			break;
3690e063efeSYaroslav Tykhiy 
37063591ba5SYaroslav Tykhiy 		case 'P':
37163591ba5SYaroslav Tykhiy 			bindport = optarg;
37263591ba5SYaroslav Tykhiy 			break;
37363591ba5SYaroslav Tykhiy 
374a4b77a2aSPoul-Henning Kamp 		case 'r':
375a4b77a2aSPoul-Henning Kamp 			readonly = 1;
376a4b77a2aSPoul-Henning Kamp 			break;
377a4b77a2aSPoul-Henning Kamp 
378a5a4544eSPaul Traina 		case 'R':
379a5a4544eSPaul Traina 			paranoid = 0;
380a5a4544eSPaul Traina 			break;
381a5a4544eSPaul Traina 
382a5a4544eSPaul Traina 		case 'S':
383a5a4544eSPaul Traina 			stats++;
384a5a4544eSPaul Traina 			break;
385a5a4544eSPaul Traina 
386ea022d16SRodney W. Grimes 		case 't':
387ea022d16SRodney W. Grimes 			timeout = atoi(optarg);
388ea022d16SRodney W. Grimes 			if (maxtimeout < timeout)
389ea022d16SRodney W. Grimes 				maxtimeout = timeout;
390ea022d16SRodney W. Grimes 			break;
391a5a4544eSPaul Traina 
3920e063efeSYaroslav Tykhiy 		case 'T':
3930e063efeSYaroslav Tykhiy 			maxtimeout = atoi(optarg);
3940e063efeSYaroslav Tykhiy 			if (timeout > maxtimeout)
3950e063efeSYaroslav Tykhiy 				timeout = maxtimeout;
396105a3c98SJulian Elischer 			break;
397105a3c98SJulian Elischer 
398ea022d16SRodney W. Grimes 		case 'u':
399ea022d16SRodney W. Grimes 		    {
400ea022d16SRodney W. Grimes 			long val = 0;
401ea022d16SRodney W. Grimes 
402ea022d16SRodney W. Grimes 			val = strtol(optarg, &optarg, 8);
403ea022d16SRodney W. Grimes 			if (*optarg != '\0' || val < 0)
4046c98f401SYaroslav Tykhiy 				syslog(LOG_WARNING, "bad value for -u");
405ea022d16SRodney W. Grimes 			else
406ea022d16SRodney W. Grimes 				defumask = val;
407ea022d16SRodney W. Grimes 			break;
408ea022d16SRodney W. Grimes 		    }
4090e063efeSYaroslav Tykhiy 		case 'U':
4100e063efeSYaroslav Tykhiy 			restricted_data_ports = 0;
4115a392aecSTorsten Blum 			break;
412ea022d16SRodney W. Grimes 
413ea022d16SRodney W. Grimes 		case 'v':
41493bd9dc5SYaroslav Tykhiy 			ftpdebug++;
415ea022d16SRodney W. Grimes 			break;
416ea022d16SRodney W. Grimes 
4175d7e0128SYaroslav Tykhiy 		case 'W':
4185d7e0128SYaroslav Tykhiy 			dowtmp = 0;
4195d7e0128SYaroslav Tykhiy 			break;
4205d7e0128SYaroslav Tykhiy 
421ea022d16SRodney W. Grimes 		default:
4226c98f401SYaroslav Tykhiy 			syslog(LOG_WARNING, "unknown flag -%c ignored", optopt);
423ea022d16SRodney W. Grimes 			break;
424ea022d16SRodney W. Grimes 		}
425ea022d16SRodney W. Grimes 	}
426cf09a206SDavid Greenman 
427cf09a206SDavid Greenman 	if (daemon_mode) {
428206fe568SHajimu UMEMOTO 		int *ctl_sock, fd, maxfd = -1, nfds, i;
429206fe568SHajimu UMEMOTO 		fd_set defreadfds, readfds;
430206fe568SHajimu UMEMOTO 		pid_t pid;
431125b9635SYaroslav Tykhiy 		struct pidfh *pfh;
432125b9635SYaroslav Tykhiy 
433125b9635SYaroslav Tykhiy 		if ((pfh = pidfile_open(pid_file, 0600, &pid)) == NULL) {
434125b9635SYaroslav Tykhiy 			if (errno == EEXIST) {
435125b9635SYaroslav Tykhiy 				syslog(LOG_ERR, "%s already running, pid %d",
436125b9635SYaroslav Tykhiy 				       getprogname(), (int)pid);
437125b9635SYaroslav Tykhiy 				exit(1);
438125b9635SYaroslav Tykhiy 			}
439125b9635SYaroslav Tykhiy 			syslog(LOG_WARNING, "pidfile_open: %m");
440125b9635SYaroslav Tykhiy 		}
441cf09a206SDavid Greenman 
442cf09a206SDavid Greenman 		/*
443cf09a206SDavid Greenman 		 * Detach from parent.
444cf09a206SDavid Greenman 		 */
445cf09a206SDavid Greenman 		if (daemon(1, 1) < 0) {
446cf09a206SDavid Greenman 			syslog(LOG_ERR, "failed to become a daemon");
447cf09a206SDavid Greenman 			exit(1);
448cf09a206SDavid Greenman 		}
449125b9635SYaroslav Tykhiy 
450125b9635SYaroslav Tykhiy 		if (pfh != NULL && pidfile_write(pfh) == -1)
451125b9635SYaroslav Tykhiy 			syslog(LOG_WARNING, "pidfile_write: %m");
452125b9635SYaroslav Tykhiy 
4534b82fc95SYaroslav Tykhiy 		sa.sa_handler = reapchild;
4544b82fc95SYaroslav Tykhiy 		(void)sigaction(SIGCHLD, &sa, NULL);
4554dd8b5abSYoshinobu Inoue 
4562c9fd5f2SHajimu UMEMOTO #ifdef VIRTUAL_HOSTING
4572c9fd5f2SHajimu UMEMOTO 		inithosts(family);
4582c9fd5f2SHajimu UMEMOTO #endif
4592c9fd5f2SHajimu UMEMOTO 
460cf09a206SDavid Greenman 		/*
461cf09a206SDavid Greenman 		 * Open a socket, bind it to the FTP port, and start
462cf09a206SDavid Greenman 		 * listening.
463cf09a206SDavid Greenman 		 */
464206fe568SHajimu UMEMOTO 		ctl_sock = socksetup(family, bindname, bindport);
465206fe568SHajimu UMEMOTO 		if (ctl_sock == NULL)
466cf09a206SDavid Greenman 			exit(1);
467206fe568SHajimu UMEMOTO 
468206fe568SHajimu UMEMOTO 		FD_ZERO(&defreadfds);
469206fe568SHajimu UMEMOTO 		for (i = 1; i <= *ctl_sock; i++) {
470206fe568SHajimu UMEMOTO 			FD_SET(ctl_sock[i], &defreadfds);
471206fe568SHajimu UMEMOTO 			if (listen(ctl_sock[i], 32) < 0) {
472cf09a206SDavid Greenman 				syslog(LOG_ERR, "control listen: %m");
473cf09a206SDavid Greenman 				exit(1);
474cf09a206SDavid Greenman 			}
475206fe568SHajimu UMEMOTO 			if (maxfd < ctl_sock[i])
476206fe568SHajimu UMEMOTO 				maxfd = ctl_sock[i];
477206fe568SHajimu UMEMOTO 		}
478206fe568SHajimu UMEMOTO 
479cf09a206SDavid Greenman 		/*
480cf09a206SDavid Greenman 		 * Loop forever accepting connection requests and forking off
481cf09a206SDavid Greenman 		 * children to handle them.
482cf09a206SDavid Greenman 		 */
483cf09a206SDavid Greenman 		while (1) {
484206fe568SHajimu UMEMOTO 			FD_COPY(&defreadfds, &readfds);
485206fe568SHajimu UMEMOTO 			nfds = select(maxfd + 1, &readfds, NULL, NULL, 0);
486206fe568SHajimu UMEMOTO 			if (nfds <= 0) {
487206fe568SHajimu UMEMOTO 				if (nfds < 0 && errno != EINTR)
488206fe568SHajimu UMEMOTO 					syslog(LOG_WARNING, "select: %m");
489206fe568SHajimu UMEMOTO 				continue;
490206fe568SHajimu UMEMOTO 			}
491206fe568SHajimu UMEMOTO 
492206fe568SHajimu UMEMOTO 			pid = -1;
493206fe568SHajimu UMEMOTO                         for (i = 1; i <= *ctl_sock; i++)
494206fe568SHajimu UMEMOTO 				if (FD_ISSET(ctl_sock[i], &readfds)) {
495206fe568SHajimu UMEMOTO 					addrlen = sizeof(his_addr);
496206fe568SHajimu UMEMOTO 					fd = accept(ctl_sock[i],
497206fe568SHajimu UMEMOTO 					    (struct sockaddr *)&his_addr,
498206fe568SHajimu UMEMOTO 					    &addrlen);
499a599a64aSYaroslav Tykhiy 					if (fd == -1) {
500a599a64aSYaroslav Tykhiy 						syslog(LOG_WARNING,
501a599a64aSYaroslav Tykhiy 						       "accept: %m");
502a599a64aSYaroslav Tykhiy 						continue;
503cf09a206SDavid Greenman 					}
504a599a64aSYaroslav Tykhiy 					switch (pid = fork()) {
505a599a64aSYaroslav Tykhiy 					case 0:
5068eb0508fSYaroslav Tykhiy 						/* child */
507*504422faSKurt Lidl 						(void) dup2(fd, s);
508*504422faSKurt Lidl 						(void) dup2(fd, STDOUT_FILENO);
509a599a64aSYaroslav Tykhiy 						(void) close(fd);
510a599a64aSYaroslav Tykhiy 						for (i = 1; i <= *ctl_sock; i++)
5118eb0508fSYaroslav Tykhiy 							close(ctl_sock[i]);
512125b9635SYaroslav Tykhiy 						if (pfh != NULL)
513125b9635SYaroslav Tykhiy 							pidfile_close(pfh);
514a599a64aSYaroslav Tykhiy 						goto gotchild;
515a599a64aSYaroslav Tykhiy 					case -1:
516a599a64aSYaroslav Tykhiy 						syslog(LOG_WARNING, "fork: %m");
517a599a64aSYaroslav Tykhiy 						/* FALLTHROUGH */
518a599a64aSYaroslav Tykhiy 					default:
519a599a64aSYaroslav Tykhiy 						close(fd);
520a599a64aSYaroslav Tykhiy 					}
521206fe568SHajimu UMEMOTO 				}
522125b9635SYaroslav Tykhiy 		}
523cf09a206SDavid Greenman 	} else {
524cf09a206SDavid Greenman 		addrlen = sizeof(his_addr);
525*504422faSKurt Lidl 		if (getpeername(s, (struct sockaddr *)&his_addr, &addrlen) < 0) {
526cf09a206SDavid Greenman 			syslog(LOG_ERR, "getpeername (%s): %m",argv[0]);
527cf09a206SDavid Greenman 			exit(1);
528cf09a206SDavid Greenman 		}
5292c9fd5f2SHajimu UMEMOTO 
5302c9fd5f2SHajimu UMEMOTO #ifdef VIRTUAL_HOSTING
5312c9fd5f2SHajimu UMEMOTO 		if (his_addr.su_family == AF_INET6 &&
5322c9fd5f2SHajimu UMEMOTO 		    IN6_IS_ADDR_V4MAPPED(&his_addr.su_sin6.sin6_addr))
5332c9fd5f2SHajimu UMEMOTO 			family = AF_INET;
5342c9fd5f2SHajimu UMEMOTO 		else
5352c9fd5f2SHajimu UMEMOTO 			family = his_addr.su_family;
5362c9fd5f2SHajimu UMEMOTO 		inithosts(family);
5372c9fd5f2SHajimu UMEMOTO #endif
538cf09a206SDavid Greenman 	}
539cf09a206SDavid Greenman 
540a599a64aSYaroslav Tykhiy gotchild:
5414b82fc95SYaroslav Tykhiy 	sa.sa_handler = SIG_DFL;
5424b82fc95SYaroslav Tykhiy 	(void)sigaction(SIGCHLD, &sa, NULL);
5434b82fc95SYaroslav Tykhiy 
5444b82fc95SYaroslav Tykhiy 	sa.sa_handler = sigurg;
5454b82fc95SYaroslav Tykhiy 	sa.sa_flags = 0;		/* don't restart syscalls for SIGURG */
5464b82fc95SYaroslav Tykhiy 	(void)sigaction(SIGURG, &sa, NULL);
5474b82fc95SYaroslav Tykhiy 
5484b82fc95SYaroslav Tykhiy 	sigfillset(&sa.sa_mask);	/* block all signals in handler */
5494b82fc95SYaroslav Tykhiy 	sa.sa_flags = SA_RESTART;
5504b82fc95SYaroslav Tykhiy 	sa.sa_handler = sigquit;
5514b82fc95SYaroslav Tykhiy 	(void)sigaction(SIGHUP, &sa, NULL);
5524b82fc95SYaroslav Tykhiy 	(void)sigaction(SIGINT, &sa, NULL);
5534b82fc95SYaroslav Tykhiy 	(void)sigaction(SIGQUIT, &sa, NULL);
5544b82fc95SYaroslav Tykhiy 	(void)sigaction(SIGTERM, &sa, NULL);
5554b82fc95SYaroslav Tykhiy 
5564b82fc95SYaroslav Tykhiy 	sa.sa_handler = lostconn;
5574b82fc95SYaroslav Tykhiy 	(void)sigaction(SIGPIPE, &sa, NULL);
558ea022d16SRodney W. Grimes 
559cf09a206SDavid Greenman 	addrlen = sizeof(ctrl_addr);
560*504422faSKurt Lidl 	if (getsockname(s, (struct sockaddr *)&ctrl_addr, &addrlen) < 0) {
561cf09a206SDavid Greenman 		syslog(LOG_ERR, "getsockname (%s): %m",argv[0]);
562cf09a206SDavid Greenman 		exit(1);
563cf09a206SDavid Greenman 	}
56463591ba5SYaroslav Tykhiy 	dataport = ntohs(ctrl_addr.su_port) - 1; /* as per RFC 959 */
565ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
566ea4e54b9SDavid Nugent 	/* select our identity from virtual host table */
5674dd8b5abSYoshinobu Inoue 	selecthost(&ctrl_addr);
568ea4e54b9SDavid Nugent #endif
569cf09a206SDavid Greenman #ifdef IP_TOS
5704dd8b5abSYoshinobu Inoue 	if (ctrl_addr.su_family == AF_INET)
5714dd8b5abSYoshinobu Inoue       {
572cf09a206SDavid Greenman 	tos = IPTOS_LOWDELAY;
573*504422faSKurt Lidl 	if (setsockopt(s, IPPROTO_IP, IP_TOS, &tos, sizeof(int)) < 0)
574406d1ae9SYaroslav Tykhiy 		syslog(LOG_WARNING, "control setsockopt (IP_TOS): %m");
5754dd8b5abSYoshinobu Inoue       }
576cf09a206SDavid Greenman #endif
577dadb9fb3SDavid Greenman 	/*
578dadb9fb3SDavid Greenman 	 * Disable Nagle on the control channel so that we don't have to wait
579dadb9fb3SDavid Greenman 	 * for peer's ACK before issuing our next reply.
580dadb9fb3SDavid Greenman 	 */
581*504422faSKurt Lidl 	if (setsockopt(s, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) < 0)
582406d1ae9SYaroslav Tykhiy 		syslog(LOG_WARNING, "control setsockopt (TCP_NODELAY): %m");
583dadb9fb3SDavid Greenman 
5844dd8b5abSYoshinobu Inoue 	data_source.su_port = htons(ntohs(ctrl_addr.su_port) - 1);
585cf09a206SDavid Greenman 
58680643af0SEd Schouten 	(void)snprintf(wtmpid, sizeof(wtmpid), "%xftpd", getpid());
587a5a4544eSPaul Traina 
588ea022d16SRodney W. Grimes 	/* Try to handle urgent data inline */
589ea022d16SRodney W. Grimes #ifdef SO_OOBINLINE
590*504422faSKurt Lidl 	if (setsockopt(s, SOL_SOCKET, SO_OOBINLINE, &on, sizeof(on)) < 0)
591406d1ae9SYaroslav Tykhiy 		syslog(LOG_WARNING, "control setsockopt (SO_OOBINLINE): %m");
592ea022d16SRodney W. Grimes #endif
593ea022d16SRodney W. Grimes 
594ea022d16SRodney W. Grimes #ifdef	F_SETOWN
595*504422faSKurt Lidl 	if (fcntl(s, F_SETOWN, getpid()) == -1)
596ea022d16SRodney W. Grimes 		syslog(LOG_ERR, "fcntl F_SETOWN: %m");
597ea022d16SRodney W. Grimes #endif
5984dd8b5abSYoshinobu Inoue 	dolog((struct sockaddr *)&his_addr);
599ea022d16SRodney W. Grimes 	/*
600ea022d16SRodney W. Grimes 	 * Set up default state
601ea022d16SRodney W. Grimes 	 */
602ea022d16SRodney W. Grimes 	data = -1;
603ea022d16SRodney W. Grimes 	type = TYPE_A;
604ea022d16SRodney W. Grimes 	form = FORM_N;
605ea022d16SRodney W. Grimes 	stru = STRU_F;
606ea022d16SRodney W. Grimes 	mode = MODE_S;
607ea022d16SRodney W. Grimes 	tmpline[0] = '\0';
608ea022d16SRodney W. Grimes 
609ea022d16SRodney W. Grimes 	/* If logins are disabled, print out the message. */
610ea022d16SRodney W. Grimes 	if ((fd = fopen(_PATH_NOLOGIN,"r")) != NULL) {
611ea022d16SRodney W. Grimes 		while (fgets(line, sizeof(line), fd) != NULL) {
612ea022d16SRodney W. Grimes 			if ((cp = strchr(line, '\n')) != NULL)
613ea022d16SRodney W. Grimes 				*cp = '\0';
614ea022d16SRodney W. Grimes 			lreply(530, "%s", line);
615ea022d16SRodney W. Grimes 		}
616ea022d16SRodney W. Grimes 		(void) fflush(stdout);
617ea022d16SRodney W. Grimes 		(void) fclose(fd);
618ea022d16SRodney W. Grimes 		reply(530, "System not available.");
619ea022d16SRodney W. Grimes 		exit(0);
620ea022d16SRodney W. Grimes 	}
621ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
62263047c6fSDavid E. O'Brien 	fd = fopen(thishost->welcome, "r");
623ea4e54b9SDavid Nugent #else
62463047c6fSDavid E. O'Brien 	fd = fopen(_PATH_FTPWELCOME, "r");
625ea4e54b9SDavid Nugent #endif
62663047c6fSDavid E. O'Brien 	if (fd != NULL) {
627ea022d16SRodney W. Grimes 		while (fgets(line, sizeof(line), fd) != NULL) {
628ea022d16SRodney W. Grimes 			if ((cp = strchr(line, '\n')) != NULL)
629ea022d16SRodney W. Grimes 				*cp = '\0';
630ea022d16SRodney W. Grimes 			lreply(220, "%s", line);
631ea022d16SRodney W. Grimes 		}
632ea022d16SRodney W. Grimes 		(void) fflush(stdout);
633ea022d16SRodney W. Grimes 		(void) fclose(fd);
634ea022d16SRodney W. Grimes 		/* reply(220,) must follow */
635ea022d16SRodney W. Grimes 	}
636ea4e54b9SDavid Nugent #ifndef VIRTUAL_HOSTING
6370512556aSDavid Nugent 	if ((hostname = malloc(MAXHOSTNAMELEN)) == NULL)
638618b0bbaSMark Murray 		fatalerror("Ran out of memory.");
63904683b2cSYaroslav Tykhiy 	if (gethostname(hostname, MAXHOSTNAMELEN - 1) < 0)
64004683b2cSYaroslav Tykhiy 		hostname[0] = '\0';
6419e9a43bdSBrian Somers 	hostname[MAXHOSTNAMELEN - 1] = '\0';
642ea4e54b9SDavid Nugent #endif
643c152df28SYaroslav Tykhiy 	if (hostinfo)
644ea022d16SRodney W. Grimes 		reply(220, "%s FTP server (%s) ready.", hostname, version);
645c152df28SYaroslav Tykhiy 	else
646c152df28SYaroslav Tykhiy 		reply(220, "FTP server ready.");
6473656f229SKurt Lidl #ifdef USE_BLACKLIST
6483656f229SKurt Lidl 	blacklist_init();
6493656f229SKurt Lidl #endif
650ea022d16SRodney W. Grimes 	for (;;)
651ea022d16SRodney W. Grimes 		(void) yyparse();
652ea022d16SRodney W. Grimes 	/* NOTREACHED */
653ea022d16SRodney W. Grimes }
654ea022d16SRodney W. Grimes 
655ea022d16SRodney W. Grimes static void
656e4bc453cSWarner Losh lostconn(int signo)
657ea022d16SRodney W. Grimes {
658ea022d16SRodney W. Grimes 
659618b0bbaSMark Murray 	if (ftpdebug)
660ea022d16SRodney W. Grimes 		syslog(LOG_DEBUG, "lost connection");
661e02897faSPhilippe Charnier 	dologout(1);
662ea022d16SRodney W. Grimes }
663ea022d16SRodney W. Grimes 
6644b82fc95SYaroslav Tykhiy static void
665e4bc453cSWarner Losh sigquit(int signo)
6664b82fc95SYaroslav Tykhiy {
6674b82fc95SYaroslav Tykhiy 
6684b82fc95SYaroslav Tykhiy 	syslog(LOG_ERR, "got signal %d", signo);
6694b82fc95SYaroslav Tykhiy 	dologout(1);
6704b82fc95SYaroslav Tykhiy }
6714b82fc95SYaroslav Tykhiy 
672ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
673ea4e54b9SDavid Nugent /*
674ea4e54b9SDavid Nugent  * read in virtual host tables (if they exist)
675ea4e54b9SDavid Nugent  */
676ea4e54b9SDavid Nugent 
677ea4e54b9SDavid Nugent static void
6782c9fd5f2SHajimu UMEMOTO inithosts(int family)
679ea4e54b9SDavid Nugent {
68055b54aa7SYaroslav Tykhiy 	int insert;
681737d08f3SYaroslav Tykhiy 	size_t len;
682ea4e54b9SDavid Nugent 	FILE *fp;
683737d08f3SYaroslav Tykhiy 	char *cp, *mp, *line;
684737d08f3SYaroslav Tykhiy 	char *hostname;
68555b54aa7SYaroslav Tykhiy 	char *vhost, *anonuser, *statfile, *welcome, *loginmsg;
686ea4e54b9SDavid Nugent 	struct ftphost *hrp, *lhrp;
6874dd8b5abSYoshinobu Inoue 	struct addrinfo hints, *res, *ai;
688ea4e54b9SDavid Nugent 
689ea4e54b9SDavid Nugent 	/*
690ea4e54b9SDavid Nugent 	 * Fill in the default host information
691ea4e54b9SDavid Nugent 	 */
692737d08f3SYaroslav Tykhiy 	if ((hostname = malloc(MAXHOSTNAMELEN)) == NULL)
693618b0bbaSMark Murray 		fatalerror("Ran out of memory.");
69404683b2cSYaroslav Tykhiy 	if (gethostname(hostname, MAXHOSTNAMELEN - 1) < 0)
695737d08f3SYaroslav Tykhiy 		hostname[0] = '\0';
696737d08f3SYaroslav Tykhiy 	hostname[MAXHOSTNAMELEN - 1] = '\0';
697737d08f3SYaroslav Tykhiy 	if ((hrp = malloc(sizeof(struct ftphost))) == NULL)
698737d08f3SYaroslav Tykhiy 		fatalerror("Ran out of memory.");
699737d08f3SYaroslav Tykhiy 	hrp->hostname = hostname;
700f38c6cadSYoshinobu Inoue 	hrp->hostinfo = NULL;
7014dd8b5abSYoshinobu Inoue 
7024dd8b5abSYoshinobu Inoue 	memset(&hints, 0, sizeof(hints));
7032c9fd5f2SHajimu UMEMOTO 	hints.ai_flags = AI_PASSIVE;
7042c9fd5f2SHajimu UMEMOTO 	hints.ai_family = family;
7052c9fd5f2SHajimu UMEMOTO 	hints.ai_socktype = SOCK_STREAM;
706b1d8d5cdSYaroslav Tykhiy 	if (getaddrinfo(hrp->hostname, NULL, &hints, &res) == 0)
707f38c6cadSYoshinobu Inoue 		hrp->hostinfo = res;
708ea4e54b9SDavid Nugent 	hrp->statfile = _PATH_FTPDSTATFILE;
709ea4e54b9SDavid Nugent 	hrp->welcome  = _PATH_FTPWELCOME;
710ea4e54b9SDavid Nugent 	hrp->loginmsg = _PATH_FTPLOGINMESG;
711ea4e54b9SDavid Nugent 	hrp->anonuser = "ftp";
712ea4e54b9SDavid Nugent 	hrp->next = NULL;
713ea4e54b9SDavid Nugent 	thishost = firsthost = lhrp = hrp;
714ea4e54b9SDavid Nugent 	if ((fp = fopen(_PATH_FTPHOSTS, "r")) != NULL) {
715ec009cf0SYaroslav Tykhiy 		int addrsize, gothost;
7164dd8b5abSYoshinobu Inoue 		void *addr;
7174dd8b5abSYoshinobu Inoue 		struct hostent *hp;
7184dd8b5abSYoshinobu Inoue 
719737d08f3SYaroslav Tykhiy 		while ((line = fgetln(fp, &len)) != NULL) {
7204dd8b5abSYoshinobu Inoue 			int	i, hp_error;
721ea4e54b9SDavid Nugent 
722737d08f3SYaroslav Tykhiy 			/* skip comments */
723737d08f3SYaroslav Tykhiy 			if (line[0] == '#')
724ea4e54b9SDavid Nugent 				continue;
725737d08f3SYaroslav Tykhiy 			if (line[len - 1] == '\n') {
726737d08f3SYaroslav Tykhiy 				line[len - 1] = '\0';
727737d08f3SYaroslav Tykhiy 				mp = NULL;
728737d08f3SYaroslav Tykhiy 			} else {
729737d08f3SYaroslav Tykhiy 				if ((mp = malloc(len + 1)) == NULL)
730737d08f3SYaroslav Tykhiy 					fatalerror("Ran out of memory.");
731737d08f3SYaroslav Tykhiy 				memcpy(mp, line, len);
732737d08f3SYaroslav Tykhiy 				mp[len] = '\0';
733737d08f3SYaroslav Tykhiy 				line = mp;
734ea4e54b9SDavid Nugent 			}
735ea4e54b9SDavid Nugent 			cp = strtok(line, " \t");
736737d08f3SYaroslav Tykhiy 			/* skip empty lines */
737737d08f3SYaroslav Tykhiy 			if (cp == NULL)
738737d08f3SYaroslav Tykhiy 				goto nextline;
73955b54aa7SYaroslav Tykhiy 			vhost = cp;
74055b54aa7SYaroslav Tykhiy 
74155b54aa7SYaroslav Tykhiy 			/* set defaults */
74255b54aa7SYaroslav Tykhiy 			anonuser = "ftp";
74355b54aa7SYaroslav Tykhiy 			statfile = _PATH_FTPDSTATFILE;
74455b54aa7SYaroslav Tykhiy 			welcome  = _PATH_FTPWELCOME;
74555b54aa7SYaroslav Tykhiy 			loginmsg = _PATH_FTPLOGINMESG;
74655b54aa7SYaroslav Tykhiy 
74755b54aa7SYaroslav Tykhiy 			/*
74855b54aa7SYaroslav Tykhiy 			 * Preparse the line so we can use its info
74955b54aa7SYaroslav Tykhiy 			 * for all the addresses associated with
75055b54aa7SYaroslav Tykhiy 			 * the virtual host name.
75155b54aa7SYaroslav Tykhiy 			 * Field 0, the virtual host name, is special:
75255b54aa7SYaroslav Tykhiy 			 * it's already parsed off and will be strdup'ed
75355b54aa7SYaroslav Tykhiy 			 * later, after we know its canonical form.
75455b54aa7SYaroslav Tykhiy 			 */
75555b54aa7SYaroslav Tykhiy 			for (i = 1; i < 5 && (cp = strtok(NULL, " \t")); i++)
75655b54aa7SYaroslav Tykhiy 				if (*cp != '-' && (cp = strdup(cp)))
75755b54aa7SYaroslav Tykhiy 					switch (i) {
75855b54aa7SYaroslav Tykhiy 					case 1:	/* anon user permissions */
75955b54aa7SYaroslav Tykhiy 						anonuser = cp;
76055b54aa7SYaroslav Tykhiy 						break;
76155b54aa7SYaroslav Tykhiy 					case 2: /* statistics file */
76255b54aa7SYaroslav Tykhiy 						statfile = cp;
76355b54aa7SYaroslav Tykhiy 						break;
76455b54aa7SYaroslav Tykhiy 					case 3: /* welcome message */
76555b54aa7SYaroslav Tykhiy 						welcome  = cp;
76655b54aa7SYaroslav Tykhiy 						break;
76755b54aa7SYaroslav Tykhiy 					case 4: /* login message */
76855b54aa7SYaroslav Tykhiy 						loginmsg = cp;
76955b54aa7SYaroslav Tykhiy 						break;
77055b54aa7SYaroslav Tykhiy 					default: /* programming error */
77155b54aa7SYaroslav Tykhiy 						abort();
77255b54aa7SYaroslav Tykhiy 						/* NOTREACHED */
77355b54aa7SYaroslav Tykhiy 					}
7744dd8b5abSYoshinobu Inoue 
7754dd8b5abSYoshinobu Inoue 			hints.ai_flags = AI_PASSIVE;
7762c9fd5f2SHajimu UMEMOTO 			hints.ai_family = family;
7772c9fd5f2SHajimu UMEMOTO 			hints.ai_socktype = SOCK_STREAM;
778b1d8d5cdSYaroslav Tykhiy 			if (getaddrinfo(vhost, NULL, &hints, &res) != 0)
779737d08f3SYaroslav Tykhiy 				goto nextline;
7804dd8b5abSYoshinobu Inoue 			for (ai = res; ai != NULL && ai->ai_addr != NULL;
781b535a9bfSDavid Nugent 			     ai = ai->ai_next) {
7824dd8b5abSYoshinobu Inoue 
783b535a9bfSDavid Nugent 			gothost = 0;
784ea4e54b9SDavid Nugent 			for (hrp = firsthost; hrp != NULL; hrp = hrp->next) {
785f38c6cadSYoshinobu Inoue 				struct addrinfo *hi;
786f38c6cadSYoshinobu Inoue 
787f38c6cadSYoshinobu Inoue 				for (hi = hrp->hostinfo; hi != NULL;
788f38c6cadSYoshinobu Inoue 				     hi = hi->ai_next)
789f38c6cadSYoshinobu Inoue 					if (hi->ai_addrlen == ai->ai_addrlen &&
790f38c6cadSYoshinobu Inoue 					    memcmp(hi->ai_addr,
7914dd8b5abSYoshinobu Inoue 						   ai->ai_addr,
792b535a9bfSDavid Nugent 						   ai->ai_addr->sa_len) == 0) {
793b535a9bfSDavid Nugent 						gothost++;
794b535a9bfSDavid Nugent 						break;
795b535a9bfSDavid Nugent 					}
796b535a9bfSDavid Nugent 				if (gothost)
797ea4e54b9SDavid Nugent 					break;
798ea4e54b9SDavid Nugent 			}
799ea4e54b9SDavid Nugent 			if (hrp == NULL) {
800ea4e54b9SDavid Nugent 				if ((hrp = malloc(sizeof(struct ftphost))) == NULL)
801737d08f3SYaroslav Tykhiy 					goto nextline;
802b1d8d5cdSYaroslav Tykhiy 				hrp->hostname = NULL;
80355b54aa7SYaroslav Tykhiy 				insert = 1;
804f2fe752dSYaroslav Tykhiy 			} else {
8051f75c13eSYaroslav Tykhiy 				if (hrp->hostinfo && hrp->hostinfo != res)
806b1d8d5cdSYaroslav Tykhiy 					freeaddrinfo(hrp->hostinfo);
807f2fe752dSYaroslav Tykhiy 				insert = 0; /* host already in the chain */
808f2fe752dSYaroslav Tykhiy 			}
809f38c6cadSYoshinobu Inoue 			hrp->hostinfo = res;
810f38c6cadSYoshinobu Inoue 
811ea4e54b9SDavid Nugent 			/*
812ea4e54b9SDavid Nugent 			 * determine hostname to use.
8134dd8b5abSYoshinobu Inoue 			 * force defined name if there is a valid alias
814ea4e54b9SDavid Nugent 			 * otherwise fallback to primary hostname
815ea4e54b9SDavid Nugent 			 */
8164dd8b5abSYoshinobu Inoue 			/* XXX: getaddrinfo() can't do alias check */
817f38c6cadSYoshinobu Inoue 			switch(hrp->hostinfo->ai_family) {
8184dd8b5abSYoshinobu Inoue 			case AF_INET:
8194b4cc4c6SYaroslav Tykhiy 				addr = &((struct sockaddr_in *)hrp->hostinfo->ai_addr)->sin_addr;
8204b4cc4c6SYaroslav Tykhiy 				addrsize = sizeof(struct in_addr);
8214dd8b5abSYoshinobu Inoue 				break;
8224dd8b5abSYoshinobu Inoue 			case AF_INET6:
8234b4cc4c6SYaroslav Tykhiy 				addr = &((struct sockaddr_in6 *)hrp->hostinfo->ai_addr)->sin6_addr;
8244b4cc4c6SYaroslav Tykhiy 				addrsize = sizeof(struct in6_addr);
8254dd8b5abSYoshinobu Inoue 				break;
8264dd8b5abSYoshinobu Inoue 			default:
8274dd8b5abSYoshinobu Inoue 				/* should not reach here */
828f38c6cadSYoshinobu Inoue 				freeaddrinfo(hrp->hostinfo);
829f2fe752dSYaroslav Tykhiy 				if (insert)
830f2fe752dSYaroslav Tykhiy 					free(hrp); /*not in chain, can free*/
831f2fe752dSYaroslav Tykhiy 				else
832f2fe752dSYaroslav Tykhiy 					hrp->hostinfo = NULL; /*mark as blank*/
833737d08f3SYaroslav Tykhiy 				goto nextline;
8344dd8b5abSYoshinobu Inoue 				/* NOTREACHED */
8354dd8b5abSYoshinobu Inoue 			}
83657d4ef07SYaroslav Tykhiy 			if ((hp = getipnodebyaddr(addr, addrsize,
837f38c6cadSYoshinobu Inoue 						  hrp->hostinfo->ai_family,
8384dd8b5abSYoshinobu Inoue 						  &hp_error)) != NULL) {
83955b54aa7SYaroslav Tykhiy 				if (strcmp(vhost, hp->h_name) != 0) {
840ea4e54b9SDavid Nugent 					if (hp->h_aliases == NULL)
84155b54aa7SYaroslav Tykhiy 						vhost = hp->h_name;
842ea4e54b9SDavid Nugent 					else {
843ea4e54b9SDavid Nugent 						i = 0;
844ea4e54b9SDavid Nugent 						while (hp->h_aliases[i] &&
84555b54aa7SYaroslav Tykhiy 						       strcmp(vhost, hp->h_aliases[i]) != 0)
846ea4e54b9SDavid Nugent 							++i;
847ea4e54b9SDavid Nugent 						if (hp->h_aliases[i] == NULL)
84855b54aa7SYaroslav Tykhiy 							vhost = hp->h_name;
849ea4e54b9SDavid Nugent 					}
850ea4e54b9SDavid Nugent 				}
851ea4e54b9SDavid Nugent 			}
852b1d8d5cdSYaroslav Tykhiy 			if (hrp->hostname &&
853b1d8d5cdSYaroslav Tykhiy 			    strcmp(hrp->hostname, vhost) != 0) {
854b1d8d5cdSYaroslav Tykhiy 				free(hrp->hostname);
855b1d8d5cdSYaroslav Tykhiy 				hrp->hostname = NULL;
856b1d8d5cdSYaroslav Tykhiy 			}
857b1d8d5cdSYaroslav Tykhiy 			if (hrp->hostname == NULL &&
858f2fe752dSYaroslav Tykhiy 			    (hrp->hostname = strdup(vhost)) == NULL) {
859f2fe752dSYaroslav Tykhiy 				freeaddrinfo(hrp->hostinfo);
860f2fe752dSYaroslav Tykhiy 				hrp->hostinfo = NULL; /* mark as blank */
861f2fe752dSYaroslav Tykhiy 				if (hp)
862f2fe752dSYaroslav Tykhiy 					freehostent(hp);
86355b54aa7SYaroslav Tykhiy 				goto nextline;
864f2fe752dSYaroslav Tykhiy 			}
86555b54aa7SYaroslav Tykhiy 			hrp->anonuser = anonuser;
86655b54aa7SYaroslav Tykhiy 			hrp->statfile = statfile;
86755b54aa7SYaroslav Tykhiy 			hrp->welcome  = welcome;
86855b54aa7SYaroslav Tykhiy 			hrp->loginmsg = loginmsg;
86955b54aa7SYaroslav Tykhiy 			if (insert) {
87055b54aa7SYaroslav Tykhiy 				hrp->next  = NULL;
87155b54aa7SYaroslav Tykhiy 				lhrp->next = hrp;
87255b54aa7SYaroslav Tykhiy 				lhrp = hrp;
87355b54aa7SYaroslav Tykhiy 			}
874233c0f66SYaroslav Tykhiy 			if (hp)
8754dd8b5abSYoshinobu Inoue 				freehostent(hp);
8764dd8b5abSYoshinobu Inoue 		      }
877737d08f3SYaroslav Tykhiy nextline:
878737d08f3SYaroslav Tykhiy 			if (mp)
879737d08f3SYaroslav Tykhiy 				free(mp);
880ea4e54b9SDavid Nugent 		}
881ea4e54b9SDavid Nugent 		(void) fclose(fp);
882ea4e54b9SDavid Nugent 	}
883ea4e54b9SDavid Nugent }
884ea4e54b9SDavid Nugent 
885ea4e54b9SDavid Nugent static void
886e4bc453cSWarner Losh selecthost(union sockunion *su)
887ea4e54b9SDavid Nugent {
888ea4e54b9SDavid Nugent 	struct ftphost	*hrp;
8894dd8b5abSYoshinobu Inoue 	u_int16_t port;
8904dd8b5abSYoshinobu Inoue #ifdef INET6
8914dd8b5abSYoshinobu Inoue 	struct in6_addr *mapped_in6 = NULL;
8924dd8b5abSYoshinobu Inoue #endif
893f38c6cadSYoshinobu Inoue 	struct addrinfo *hi;
8944dd8b5abSYoshinobu Inoue 
8954dd8b5abSYoshinobu Inoue #ifdef INET6
8964dd8b5abSYoshinobu Inoue 	/*
8974dd8b5abSYoshinobu Inoue 	 * XXX IPv4 mapped IPv6 addr consideraton,
8984dd8b5abSYoshinobu Inoue 	 * specified in rfc2373.
8994dd8b5abSYoshinobu Inoue 	 */
9004dd8b5abSYoshinobu Inoue 	if (su->su_family == AF_INET6 &&
9014dd8b5abSYoshinobu Inoue 	    IN6_IS_ADDR_V4MAPPED(&su->su_sin6.sin6_addr))
9024dd8b5abSYoshinobu Inoue 		mapped_in6 = &su->su_sin6.sin6_addr;
9034dd8b5abSYoshinobu Inoue #endif
904ea4e54b9SDavid Nugent 
905ea4e54b9SDavid Nugent 	hrp = thishost = firsthost;	/* default */
9064dd8b5abSYoshinobu Inoue 	port = su->su_port;
9074dd8b5abSYoshinobu Inoue 	su->su_port = 0;
908ea4e54b9SDavid Nugent 	while (hrp != NULL) {
909b535a9bfSDavid Nugent 	    for (hi = hrp->hostinfo; hi != NULL; hi = hi->ai_next) {
910f38c6cadSYoshinobu Inoue 		if (memcmp(su, hi->ai_addr, hi->ai_addrlen) == 0) {
911ea4e54b9SDavid Nugent 			thishost = hrp;
912ebd83647SYaroslav Tykhiy 			goto found;
913ea4e54b9SDavid Nugent 		}
9144dd8b5abSYoshinobu Inoue #ifdef INET6
9154dd8b5abSYoshinobu Inoue 		/* XXX IPv4 mapped IPv6 addr consideraton */
916f38c6cadSYoshinobu Inoue 		if (hi->ai_addr->sa_family == AF_INET && mapped_in6 != NULL &&
9174dd8b5abSYoshinobu Inoue 		    (memcmp(&mapped_in6->s6_addr[12],
918f38c6cadSYoshinobu Inoue 			    &((struct sockaddr_in *)hi->ai_addr)->sin_addr,
9194dd8b5abSYoshinobu Inoue 			    sizeof(struct in_addr)) == 0)) {
9204dd8b5abSYoshinobu Inoue 			thishost = hrp;
921ebd83647SYaroslav Tykhiy 			goto found;
9224dd8b5abSYoshinobu Inoue 		}
9234dd8b5abSYoshinobu Inoue #endif
924f38c6cadSYoshinobu Inoue 	    }
925ea4e54b9SDavid Nugent 	    hrp = hrp->next;
926ea4e54b9SDavid Nugent 	}
927ebd83647SYaroslav Tykhiy found:
9284dd8b5abSYoshinobu Inoue 	su->su_port = port;
929ea4e54b9SDavid Nugent 	/* setup static variables as appropriate */
930ea4e54b9SDavid Nugent 	hostname = thishost->hostname;
931ea4e54b9SDavid Nugent 	ftpuser = thishost->anonuser;
932ea4e54b9SDavid Nugent }
933ea4e54b9SDavid Nugent #endif
934ea4e54b9SDavid Nugent 
935ea022d16SRodney W. Grimes /*
936ea022d16SRodney W. Grimes  * Helper function for sgetpwnam().
937ea022d16SRodney W. Grimes  */
938ea022d16SRodney W. Grimes static char *
939e4bc453cSWarner Losh sgetsave(char *s)
940ea022d16SRodney W. Grimes {
941e3765043SYaroslav Tykhiy 	char *new = malloc(strlen(s) + 1);
942ea022d16SRodney W. Grimes 
943ea022d16SRodney W. Grimes 	if (new == NULL) {
94402c97492SYaroslav Tykhiy 		reply(421, "Ran out of memory.");
945ea022d16SRodney W. Grimes 		dologout(1);
946ea022d16SRodney W. Grimes 		/* NOTREACHED */
947ea022d16SRodney W. Grimes 	}
948ea022d16SRodney W. Grimes 	(void) strcpy(new, s);
949ea022d16SRodney W. Grimes 	return (new);
950ea022d16SRodney W. Grimes }
951ea022d16SRodney W. Grimes 
952ea022d16SRodney W. Grimes /*
953ea022d16SRodney W. Grimes  * Save the result of a getpwnam.  Used for USER command, since
954ea022d16SRodney W. Grimes  * the data returned must not be clobbered by any other command
955ea022d16SRodney W. Grimes  * (e.g., globbing).
956c29b9b47SYaroslav Tykhiy  * NB: The data returned by sgetpwnam() will remain valid until
957c29b9b47SYaroslav Tykhiy  * the next call to this function.  Its difference from getpwnam()
958c29b9b47SYaroslav Tykhiy  * is that sgetpwnam() is known to be called from ftpd code only.
959ea022d16SRodney W. Grimes  */
960ea022d16SRodney W. Grimes static struct passwd *
961e4bc453cSWarner Losh sgetpwnam(char *name)
962ea022d16SRodney W. Grimes {
963ea022d16SRodney W. Grimes 	static struct passwd save;
964ea022d16SRodney W. Grimes 	struct passwd *p;
965ea022d16SRodney W. Grimes 
966ea022d16SRodney W. Grimes 	if ((p = getpwnam(name)) == NULL)
967ea022d16SRodney W. Grimes 		return (p);
968ea022d16SRodney W. Grimes 	if (save.pw_name) {
969ea022d16SRodney W. Grimes 		free(save.pw_name);
970ea022d16SRodney W. Grimes 		free(save.pw_passwd);
97103d34cccSChristian Brueffer 		free(save.pw_class);
972ea022d16SRodney W. Grimes 		free(save.pw_gecos);
973ea022d16SRodney W. Grimes 		free(save.pw_dir);
974ea022d16SRodney W. Grimes 		free(save.pw_shell);
975ea022d16SRodney W. Grimes 	}
976ea022d16SRodney W. Grimes 	save = *p;
977ea022d16SRodney W. Grimes 	save.pw_name = sgetsave(p->pw_name);
978ea022d16SRodney W. Grimes 	save.pw_passwd = sgetsave(p->pw_passwd);
97903d34cccSChristian Brueffer 	save.pw_class = sgetsave(p->pw_class);
980ea022d16SRodney W. Grimes 	save.pw_gecos = sgetsave(p->pw_gecos);
981ea022d16SRodney W. Grimes 	save.pw_dir = sgetsave(p->pw_dir);
982ea022d16SRodney W. Grimes 	save.pw_shell = sgetsave(p->pw_shell);
983ea022d16SRodney W. Grimes 	return (&save);
984ea022d16SRodney W. Grimes }
985ea022d16SRodney W. Grimes 
986ea022d16SRodney W. Grimes static int login_attempts;	/* number of failed login attempts */
987ea022d16SRodney W. Grimes static int askpasswd;		/* had user command, ask for passwd */
98890906a46SSheldon Hearn static char curname[MAXLOGNAME];	/* current USER name */
989ea022d16SRodney W. Grimes 
990ea022d16SRodney W. Grimes /*
991ea022d16SRodney W. Grimes  * USER command.
992ea022d16SRodney W. Grimes  * Sets global passwd pointer pw if named account exists and is acceptable;
993ea022d16SRodney W. Grimes  * sets askpasswd if a PASS command is expected.  If logged in previously,
994ea022d16SRodney W. Grimes  * need to reset state.  If name is "ftp" or "anonymous", the name is not in
995ea022d16SRodney W. Grimes  * _PATH_FTPUSERS, and ftp account exists, set guest and pw, then just return.
996ea022d16SRodney W. Grimes  * If account doesn't exist, ask for passwd anyway.  Otherwise, check user
997ea022d16SRodney W. Grimes  * requesting login privileges.  Disallow anyone who does not have a standard
998ea022d16SRodney W. Grimes  * shell as returned by getusershell().  Disallow anyone mentioned in the file
999ea022d16SRodney W. Grimes  * _PATH_FTPUSERS to allow people such as root and uucp to be avoided.
1000ea022d16SRodney W. Grimes  */
1001ea022d16SRodney W. Grimes void
1002e4bc453cSWarner Losh user(char *name)
1003ea022d16SRodney W. Grimes {
1004cefb6785SChristian S.J. Peron 	int ecode;
1005ea022d16SRodney W. Grimes 	char *cp, *shell;
1006ea022d16SRodney W. Grimes 
1007ea022d16SRodney W. Grimes 	if (logged_in) {
1008ea022d16SRodney W. Grimes 		if (guest) {
1009ea022d16SRodney W. Grimes 			reply(530, "Can't change user from guest login.");
1010ea022d16SRodney W. Grimes 			return;
1011a5a4544eSPaul Traina 		} else if (dochroot) {
1012a5a4544eSPaul Traina 			reply(530, "Can't change user from chroot user.");
1013a5a4544eSPaul Traina 			return;
1014ea022d16SRodney W. Grimes 		}
1015ea022d16SRodney W. Grimes 		end_login();
1016ea022d16SRodney W. Grimes 	}
1017ea022d16SRodney W. Grimes 
1018ea022d16SRodney W. Grimes 	guest = 0;
101963047c6fSDavid E. O'Brien #ifdef VIRTUAL_HOSTING
102063047c6fSDavid E. O'Brien 	pw = sgetpwnam(thishost->anonuser);
102163047c6fSDavid E. O'Brien #else
102263047c6fSDavid E. O'Brien 	pw = sgetpwnam("ftp");
102363047c6fSDavid E. O'Brien #endif
1024ea022d16SRodney W. Grimes 	if (strcmp(name, "ftp") == 0 || strcmp(name, "anonymous") == 0) {
1025cefb6785SChristian S.J. Peron 		if (checkuser(_PATH_FTPUSERS, "ftp", 0, NULL, &ecode) ||
1026cefb6785SChristian S.J. Peron 		    (ecode != 0 && ecode != ENOENT))
1027cefb6785SChristian S.J. Peron 			reply(530, "User %s access denied.", name);
1028cefb6785SChristian S.J. Peron 		else if (checkuser(_PATH_FTPUSERS, "anonymous", 0, NULL, &ecode) ||
1029cefb6785SChristian S.J. Peron 		    (ecode != 0 && ecode != ENOENT))
1030ea022d16SRodney W. Grimes 			reply(530, "User %s access denied.", name);
103163047c6fSDavid E. O'Brien 		else if (pw != NULL) {
1032ea022d16SRodney W. Grimes 			guest = 1;
1033ea022d16SRodney W. Grimes 			askpasswd = 1;
1034ea022d16SRodney W. Grimes 			reply(331,
10352c60c54cSPaul Traina 			"Guest login ok, send your email address as password.");
1036ea022d16SRodney W. Grimes 		} else
1037ea022d16SRodney W. Grimes 			reply(530, "User %s unknown.", name);
1038ea022d16SRodney W. Grimes 		if (!askpasswd && logging)
1039ea022d16SRodney W. Grimes 			syslog(LOG_NOTICE,
1040ea022d16SRodney W. Grimes 			    "ANONYMOUS FTP LOGIN REFUSED FROM %s", remotehost);
1041ea022d16SRodney W. Grimes 		return;
1042ea022d16SRodney W. Grimes 	}
10435a392aecSTorsten Blum 	if (anon_only != 0) {
10445a392aecSTorsten Blum 		reply(530, "Sorry, only anonymous ftp allowed.");
10455a392aecSTorsten Blum 		return;
10465a392aecSTorsten Blum 	}
10475a392aecSTorsten Blum 
10489aca17cbSMark Murray 	if ((pw = sgetpwnam(name))) {
1049ea022d16SRodney W. Grimes 		if ((shell = pw->pw_shell) == NULL || *shell == 0)
1050ea022d16SRodney W. Grimes 			shell = _PATH_BSHELL;
1051c433c9daSPhilippe Charnier 		setusershell();
1052ea022d16SRodney W. Grimes 		while ((cp = getusershell()) != NULL)
1053ea022d16SRodney W. Grimes 			if (strcmp(cp, shell) == 0)
1054ea022d16SRodney W. Grimes 				break;
1055ea022d16SRodney W. Grimes 		endusershell();
1056ea022d16SRodney W. Grimes 
1057cefb6785SChristian S.J. Peron 		if (cp == NULL ||
1058cefb6785SChristian S.J. Peron 		    (checkuser(_PATH_FTPUSERS, name, 1, NULL, &ecode) ||
1059cefb6785SChristian S.J. Peron 		    (ecode != 0 && ecode != ENOENT))) {
1060ea022d16SRodney W. Grimes 			reply(530, "User %s access denied.", name);
1061ea022d16SRodney W. Grimes 			if (logging)
1062ea022d16SRodney W. Grimes 				syslog(LOG_NOTICE,
1063ea022d16SRodney W. Grimes 				    "FTP LOGIN REFUSED FROM %s, %s",
1064ea022d16SRodney W. Grimes 				    remotehost, name);
10657e295315SYaroslav Tykhiy 			pw = NULL;
1066ea022d16SRodney W. Grimes 			return;
1067ea022d16SRodney W. Grimes 		}
1068ea022d16SRodney W. Grimes 	}
1069ea022d16SRodney W. Grimes 	if (logging)
1070ea022d16SRodney W. Grimes 		strncpy(curname, name, sizeof(curname)-1);
107147499ecdSAndrey A. Chernov 
107247499ecdSAndrey A. Chernov 	pwok = 0;
1073fa1746c9SMark Murray #ifdef USE_PAM
1074fa1746c9SMark Murray 	/* XXX Kluge! The conversation mechanism needs to be fixed. */
1075726040deSGuido van Rooij #endif
107647499ecdSAndrey A. Chernov 	if (opiechallenge(&opiedata, name, opieprompt) == 0) {
107747499ecdSAndrey A. Chernov 		pwok = (pw != NULL) &&
107847499ecdSAndrey A. Chernov 		       opieaccessfile(remotehost) &&
107947499ecdSAndrey A. Chernov 		       opiealways(pw->pw_dir);
108047499ecdSAndrey A. Chernov 		reply(331, "Response to %s %s for %s.",
108147499ecdSAndrey A. Chernov 		      opieprompt, pwok ? "requested" : "required", name);
108247499ecdSAndrey A. Chernov 	} else {
108347499ecdSAndrey A. Chernov 		pwok = 1;
108447499ecdSAndrey A. Chernov 		reply(331, "Password required for %s.", name);
108547499ecdSAndrey A. Chernov 	}
1086ea022d16SRodney W. Grimes 	askpasswd = 1;
1087ea022d16SRodney W. Grimes 	/*
1088ea022d16SRodney W. Grimes 	 * Delay before reading passwd after first failed
1089ea022d16SRodney W. Grimes 	 * attempt to slow down passwd-guessing programs.
1090ea022d16SRodney W. Grimes 	 */
1091ea022d16SRodney W. Grimes 	if (login_attempts)
1092e3765043SYaroslav Tykhiy 		sleep(login_attempts);
1093ea022d16SRodney W. Grimes }
1094ea022d16SRodney W. Grimes 
1095ea022d16SRodney W. Grimes /*
10968657b576SYaroslav Tykhiy  * Check if a user is in the file "fname",
10978657b576SYaroslav Tykhiy  * return a pointer to a malloc'd string with the rest
10988657b576SYaroslav Tykhiy  * of the matching line in "residue" if not NULL.
1099ea022d16SRodney W. Grimes  */
1100ea022d16SRodney W. Grimes static int
1101cefb6785SChristian S.J. Peron checkuser(char *fname, char *name, int pwset, char **residue, int *ecode)
1102ea022d16SRodney W. Grimes {
1103ea022d16SRodney W. Grimes 	FILE *fd;
1104ea022d16SRodney W. Grimes 	int found = 0;
1105737d08f3SYaroslav Tykhiy 	size_t len;
1106737d08f3SYaroslav Tykhiy 	char *line, *mp, *p;
1107ea022d16SRodney W. Grimes 
1108cefb6785SChristian S.J. Peron 	if (ecode != NULL)
1109cefb6785SChristian S.J. Peron 		*ecode = 0;
1110a5a4544eSPaul Traina 	if ((fd = fopen(fname, "r")) != NULL) {
1111737d08f3SYaroslav Tykhiy 		while (!found && (line = fgetln(fd, &len)) != NULL) {
1112737d08f3SYaroslav Tykhiy 			/* skip comments */
1113ea022d16SRodney W. Grimes 			if (line[0] == '#')
1114ea022d16SRodney W. Grimes 				continue;
1115737d08f3SYaroslav Tykhiy 			if (line[len - 1] == '\n') {
1116737d08f3SYaroslav Tykhiy 				line[len - 1] = '\0';
1117737d08f3SYaroslav Tykhiy 				mp = NULL;
1118737d08f3SYaroslav Tykhiy 			} else {
1119737d08f3SYaroslav Tykhiy 				if ((mp = malloc(len + 1)) == NULL)
1120737d08f3SYaroslav Tykhiy 					fatalerror("Ran out of memory.");
1121737d08f3SYaroslav Tykhiy 				memcpy(mp, line, len);
1122737d08f3SYaroslav Tykhiy 				mp[len] = '\0';
1123737d08f3SYaroslav Tykhiy 				line = mp;
1124737d08f3SYaroslav Tykhiy 			}
1125737d08f3SYaroslav Tykhiy 			/* avoid possible leading and trailing whitespace */
1126737d08f3SYaroslav Tykhiy 			p = strtok(line, " \t");
1127737d08f3SYaroslav Tykhiy 			/* skip empty lines */
1128737d08f3SYaroslav Tykhiy 			if (p == NULL)
1129737d08f3SYaroslav Tykhiy 				goto nextline;
113031fea7b8SDavid Nugent 			/*
113131fea7b8SDavid Nugent 			 * if first chr is '@', check group membership
113231fea7b8SDavid Nugent 			 */
1133737d08f3SYaroslav Tykhiy 			if (p[0] == '@') {
113431fea7b8SDavid Nugent 				int i = 0;
113531fea7b8SDavid Nugent 				struct group *grp;
113631fea7b8SDavid Nugent 
11378657b576SYaroslav Tykhiy 				if (p[1] == '\0') /* single @ matches anyone */
11388657b576SYaroslav Tykhiy 					found = 1;
11398657b576SYaroslav Tykhiy 				else {
1140737d08f3SYaroslav Tykhiy 					if ((grp = getgrnam(p+1)) == NULL)
1141737d08f3SYaroslav Tykhiy 						goto nextline;
11427edcb936SSteve Price 					/*
11437edcb936SSteve Price 					 * Check user's default group
11447edcb936SSteve Price 					 */
11457edcb936SSteve Price 					if (pwset && grp->gr_gid == pw->pw_gid)
11467edcb936SSteve Price 						found = 1;
11477edcb936SSteve Price 					/*
11487edcb936SSteve Price 					 * Check supplementary groups
11497edcb936SSteve Price 					 */
115031fea7b8SDavid Nugent 					while (!found && grp->gr_mem[i])
115131fea7b8SDavid Nugent 						found = strcmp(name,
115231fea7b8SDavid Nugent 							grp->gr_mem[i++])
115331fea7b8SDavid Nugent 							== 0;
1154ea022d16SRodney W. Grimes 				}
11558657b576SYaroslav Tykhiy 			}
115631fea7b8SDavid Nugent 			/*
115731fea7b8SDavid Nugent 			 * Otherwise, just check for username match
115831fea7b8SDavid Nugent 			 */
115931fea7b8SDavid Nugent 			else
1160737d08f3SYaroslav Tykhiy 				found = strcmp(p, name) == 0;
11618657b576SYaroslav Tykhiy 			/*
11628657b576SYaroslav Tykhiy 			 * Save the rest of line to "residue" if matched
11638657b576SYaroslav Tykhiy 			 */
11648657b576SYaroslav Tykhiy 			if (found && residue) {
11650ba71e24SYaroslav Tykhiy 				if ((p = strtok(NULL, "")) != NULL)
11660ba71e24SYaroslav Tykhiy 					p += strspn(p, " \t");
11670ba71e24SYaroslav Tykhiy 				if (p && *p) {
11688657b576SYaroslav Tykhiy 				 	if ((*residue = strdup(p)) == NULL)
11698657b576SYaroslav Tykhiy 						fatalerror("Ran out of memory.");
11708657b576SYaroslav Tykhiy 				} else
11718657b576SYaroslav Tykhiy 					*residue = NULL;
11728657b576SYaroslav Tykhiy 			}
1173737d08f3SYaroslav Tykhiy nextline:
1174737d08f3SYaroslav Tykhiy 			if (mp)
1175737d08f3SYaroslav Tykhiy 				free(mp);
1176ea022d16SRodney W. Grimes 		}
1177ea022d16SRodney W. Grimes 		(void) fclose(fd);
1178cefb6785SChristian S.J. Peron 	} else if (ecode != NULL)
1179cefb6785SChristian S.J. Peron 		*ecode = errno;
1180ea022d16SRodney W. Grimes 	return (found);
1181ea022d16SRodney W. Grimes }
1182ea022d16SRodney W. Grimes 
1183ea022d16SRodney W. Grimes /*
1184ea022d16SRodney W. Grimes  * Terminate login as previous user, if any, resetting state;
1185ea022d16SRodney W. Grimes  * used when USER command is given or login fails.
1186ea022d16SRodney W. Grimes  */
1187ea022d16SRodney W. Grimes static void
1188e4bc453cSWarner Losh end_login(void)
1189ea022d16SRodney W. Grimes {
11905bc9d93dSMark Murray #ifdef USE_PAM
11915bc9d93dSMark Murray 	int e;
11925bc9d93dSMark Murray #endif
1193ea022d16SRodney W. Grimes 
119452e7ee74SYaroslav Tykhiy 	(void) seteuid(0);
11959f37b1a2SEd Schouten 	if (logged_in && dowtmp)
11969f37b1a2SEd Schouten 		ftpd_logwtmp(wtmpid, NULL, NULL);
1197ea022d16SRodney W. Grimes 	pw = NULL;
1198b071c689SDavid Nugent #ifdef	LOGIN_CAP
1199e81b1c71SEdward Tomasz Napierala 	setusercontext(NULL, getpwuid(0), 0, LOGIN_SETALL & ~(LOGIN_SETLOGIN |
1200e81b1c71SEdward Tomasz Napierala 		       LOGIN_SETUSER | LOGIN_SETGROUP | LOGIN_SETPATH |
1201e81b1c71SEdward Tomasz Napierala 		       LOGIN_SETENV));
1202b071c689SDavid Nugent #endif
12035bc9d93dSMark Murray #ifdef USE_PAM
1204de45162dSYaroslav Tykhiy 	if (pamh) {
12055bc9d93dSMark Murray 		if ((e = pam_setcred(pamh, PAM_DELETE_CRED)) != PAM_SUCCESS)
12065bc9d93dSMark Murray 			syslog(LOG_ERR, "pam_setcred: %s", pam_strerror(pamh, e));
12075bc9d93dSMark Murray 		if ((e = pam_close_session(pamh,0)) != PAM_SUCCESS)
12085bc9d93dSMark Murray 			syslog(LOG_ERR, "pam_close_session: %s", pam_strerror(pamh, e));
12095bc9d93dSMark Murray 		if ((e = pam_end(pamh, e)) != PAM_SUCCESS)
12105bc9d93dSMark Murray 			syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e));
12115bc9d93dSMark Murray 		pamh = NULL;
1212de45162dSYaroslav Tykhiy 	}
12135bc9d93dSMark Murray #endif
1214ea022d16SRodney W. Grimes 	logged_in = 0;
1215ea022d16SRodney W. Grimes 	guest = 0;
1216a5a4544eSPaul Traina 	dochroot = 0;
1217ea022d16SRodney W. Grimes }
1218ea022d16SRodney W. Grimes 
12195bc9d93dSMark Murray #ifdef USE_PAM
12206c9134c0SMark Murray 
12216c9134c0SMark Murray /*
12226c9134c0SMark Murray  * the following code is stolen from imap-uw PAM authentication module and
12236c9134c0SMark Murray  * login.c
12246c9134c0SMark Murray  */
12256c9134c0SMark Murray #define COPY_STRING(s) (s ? strdup(s) : NULL)
12266c9134c0SMark Murray 
12276c9134c0SMark Murray struct cred_t {
12286c9134c0SMark Murray 	const char *uname;		/* user name */
12296c9134c0SMark Murray 	const char *pass;		/* password */
12306c9134c0SMark Murray };
12316c9134c0SMark Murray typedef struct cred_t cred_t;
12326c9134c0SMark Murray 
12336c9134c0SMark Murray static int
12346c9134c0SMark Murray auth_conv(int num_msg, const struct pam_message **msg,
12356c9134c0SMark Murray 	  struct pam_response **resp, void *appdata)
12366c9134c0SMark Murray {
12376c9134c0SMark Murray 	int i;
12386c9134c0SMark Murray 	cred_t *cred = (cred_t *) appdata;
123960769b19SDag-Erling Smørgrav 	struct pam_response *reply;
124060769b19SDag-Erling Smørgrav 
124160769b19SDag-Erling Smørgrav 	reply = calloc(num_msg, sizeof *reply);
124260769b19SDag-Erling Smørgrav 	if (reply == NULL)
124360769b19SDag-Erling Smørgrav 		return PAM_BUF_ERR;
12446c9134c0SMark Murray 
12456c9134c0SMark Murray 	for (i = 0; i < num_msg; i++) {
12466c9134c0SMark Murray 		switch (msg[i]->msg_style) {
12476c9134c0SMark Murray 		case PAM_PROMPT_ECHO_ON:	/* assume want user name */
12486c9134c0SMark Murray 			reply[i].resp_retcode = PAM_SUCCESS;
12496c9134c0SMark Murray 			reply[i].resp = COPY_STRING(cred->uname);
12506c9134c0SMark Murray 			/* PAM frees resp. */
12516c9134c0SMark Murray 			break;
12526c9134c0SMark Murray 		case PAM_PROMPT_ECHO_OFF:	/* assume want password */
12536c9134c0SMark Murray 			reply[i].resp_retcode = PAM_SUCCESS;
12546c9134c0SMark Murray 			reply[i].resp = COPY_STRING(cred->pass);
12556c9134c0SMark Murray 			/* PAM frees resp. */
12566c9134c0SMark Murray 			break;
12576c9134c0SMark Murray 		case PAM_TEXT_INFO:
12586c9134c0SMark Murray 		case PAM_ERROR_MSG:
12596c9134c0SMark Murray 			reply[i].resp_retcode = PAM_SUCCESS;
12606c9134c0SMark Murray 			reply[i].resp = NULL;
12616c9134c0SMark Murray 			break;
12626c9134c0SMark Murray 		default:			/* unknown message style */
12636c9134c0SMark Murray 			free(reply);
12646c9134c0SMark Murray 			return PAM_CONV_ERR;
12656c9134c0SMark Murray 		}
12666c9134c0SMark Murray 	}
12676c9134c0SMark Murray 
12686c9134c0SMark Murray 	*resp = reply;
12696c9134c0SMark Murray 	return PAM_SUCCESS;
12706c9134c0SMark Murray }
12716c9134c0SMark Murray 
12726c9134c0SMark Murray /*
12736c9134c0SMark Murray  * Attempt to authenticate the user using PAM.  Returns 0 if the user is
12746c9134c0SMark Murray  * authenticated, or 1 if not authenticated.  If some sort of PAM system
12756c9134c0SMark Murray  * error occurs (e.g., the "/etc/pam.conf" file is missing) then this
12766c9134c0SMark Murray  * function returns -1.  This can be used as an indication that we should
12776c9134c0SMark Murray  * fall back to a different authentication mechanism.
12786c9134c0SMark Murray  */
12796c9134c0SMark Murray static int
12806c9134c0SMark Murray auth_pam(struct passwd **ppw, const char *pass)
12816c9134c0SMark Murray {
12826c9134c0SMark Murray 	const char *tmpl_user;
12836c9134c0SMark Murray 	const void *item;
12846c9134c0SMark Murray 	int rval;
12856c9134c0SMark Murray 	int e;
12866c9134c0SMark Murray 	cred_t auth_cred = { (*ppw)->pw_name, pass };
12876c9134c0SMark Murray 	struct pam_conv conv = { &auth_conv, &auth_cred };
12886c9134c0SMark Murray 
12896c9134c0SMark Murray 	e = pam_start("ftpd", (*ppw)->pw_name, &conv, &pamh);
12906c9134c0SMark Murray 	if (e != PAM_SUCCESS) {
1291545ea864SYaroslav Tykhiy 		/*
1292545ea864SYaroslav Tykhiy 		 * In OpenPAM, it's OK to pass NULL to pam_strerror()
1293545ea864SYaroslav Tykhiy 		 * if context creation has failed in the first place.
1294545ea864SYaroslav Tykhiy 		 */
1295545ea864SYaroslav Tykhiy 		syslog(LOG_ERR, "pam_start: %s", pam_strerror(NULL, e));
12966c9134c0SMark Murray 		return -1;
12976c9134c0SMark Murray 	}
12986c9134c0SMark Murray 
1299ea413ab7SGuido van Rooij 	e = pam_set_item(pamh, PAM_RHOST, remotehost);
1300ea413ab7SGuido van Rooij 	if (e != PAM_SUCCESS) {
1301ea413ab7SGuido van Rooij 		syslog(LOG_ERR, "pam_set_item(PAM_RHOST): %s",
1302ea413ab7SGuido van Rooij 			pam_strerror(pamh, e));
1303de45162dSYaroslav Tykhiy 		if ((e = pam_end(pamh, e)) != PAM_SUCCESS) {
1304de45162dSYaroslav Tykhiy 			syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e));
1305de45162dSYaroslav Tykhiy 		}
1306de45162dSYaroslav Tykhiy 		pamh = NULL;
1307ea413ab7SGuido van Rooij 		return -1;
1308ea413ab7SGuido van Rooij 	}
1309ea413ab7SGuido van Rooij 
13106c9134c0SMark Murray 	e = pam_authenticate(pamh, 0);
13116c9134c0SMark Murray 	switch (e) {
13126c9134c0SMark Murray 	case PAM_SUCCESS:
13136c9134c0SMark Murray 		/*
13146c9134c0SMark Murray 		 * With PAM we support the concept of a "template"
13156c9134c0SMark Murray 		 * user.  The user enters a login name which is
13166c9134c0SMark Murray 		 * authenticated by PAM, usually via a remote service
13176c9134c0SMark Murray 		 * such as RADIUS or TACACS+.  If authentication
13186c9134c0SMark Murray 		 * succeeds, a different but related "template" name
13196c9134c0SMark Murray 		 * is used for setting the credentials, shell, and
13206c9134c0SMark Murray 		 * home directory.  The name the user enters need only
13216c9134c0SMark Murray 		 * exist on the remote authentication server, but the
13226c9134c0SMark Murray 		 * template name must be present in the local password
13236c9134c0SMark Murray 		 * database.
13246c9134c0SMark Murray 		 *
13256c9134c0SMark Murray 		 * This is supported by two various mechanisms in the
13266c9134c0SMark Murray 		 * individual modules.  However, from the application's
13276c9134c0SMark Murray 		 * point of view, the template user is always passed
13286c9134c0SMark Murray 		 * back as a changed value of the PAM_USER item.
13296c9134c0SMark Murray 		 */
13306c9134c0SMark Murray 		if ((e = pam_get_item(pamh, PAM_USER, &item)) ==
13316c9134c0SMark Murray 		    PAM_SUCCESS) {
13326c9134c0SMark Murray 			tmpl_user = (const char *) item;
13336c9134c0SMark Murray 			if (strcmp((*ppw)->pw_name, tmpl_user) != 0)
13346c9134c0SMark Murray 				*ppw = getpwnam(tmpl_user);
13356c9134c0SMark Murray 		} else
13366c9134c0SMark Murray 			syslog(LOG_ERR, "Couldn't get PAM_USER: %s",
13376c9134c0SMark Murray 			    pam_strerror(pamh, e));
13386c9134c0SMark Murray 		rval = 0;
13396c9134c0SMark Murray 		break;
13406c9134c0SMark Murray 
13416c9134c0SMark Murray 	case PAM_AUTH_ERR:
13426c9134c0SMark Murray 	case PAM_USER_UNKNOWN:
13436c9134c0SMark Murray 	case PAM_MAXTRIES:
13446c9134c0SMark Murray 		rval = 1;
13456c9134c0SMark Murray 		break;
13466c9134c0SMark Murray 
13476c9134c0SMark Murray 	default:
13485bc9d93dSMark Murray 		syslog(LOG_ERR, "pam_authenticate: %s", pam_strerror(pamh, e));
13496c9134c0SMark Murray 		rval = -1;
13506c9134c0SMark Murray 		break;
13516c9134c0SMark Murray 	}
13526c9134c0SMark Murray 
13535bc9d93dSMark Murray 	if (rval == 0) {
13545bc9d93dSMark Murray 		e = pam_acct_mgmt(pamh, 0);
13555bc9d93dSMark Murray 		if (e != PAM_SUCCESS) {
13564cbc4ad6SYaroslav Tykhiy 			syslog(LOG_ERR, "pam_acct_mgmt: %s",
13574cbc4ad6SYaroslav Tykhiy 						pam_strerror(pamh, e));
13585bc9d93dSMark Murray 			rval = 1;
13595bc9d93dSMark Murray 		}
13605bc9d93dSMark Murray 	}
13615bc9d93dSMark Murray 
13625bc9d93dSMark Murray 	if (rval != 0) {
13636c9134c0SMark Murray 		if ((e = pam_end(pamh, e)) != PAM_SUCCESS) {
13646c9134c0SMark Murray 			syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e));
13655bc9d93dSMark Murray 		}
13665bc9d93dSMark Murray 		pamh = NULL;
13676c9134c0SMark Murray 	}
13686c9134c0SMark Murray 	return rval;
13696c9134c0SMark Murray }
13706c9134c0SMark Murray 
13715bc9d93dSMark Murray #endif /* USE_PAM */
13726c9134c0SMark Murray 
1373ea022d16SRodney W. Grimes void
1374e4bc453cSWarner Losh pass(char *passwd)
1375ea022d16SRodney W. Grimes {
1376cefb6785SChristian S.J. Peron 	int rval, ecode;
1377ea022d16SRodney W. Grimes 	FILE *fd;
1378b071c689SDavid Nugent #ifdef	LOGIN_CAP
1379b071c689SDavid Nugent 	login_cap_t *lc = NULL;
1380b071c689SDavid Nugent #endif
13815bc9d93dSMark Murray #ifdef USE_PAM
13825bc9d93dSMark Murray 	int e;
13835bc9d93dSMark Murray #endif
1384341e476eSYaroslav Tykhiy 	char *residue = NULL;
138547499ecdSAndrey A. Chernov 	char *xpasswd;
1386ea022d16SRodney W. Grimes 
1387ea022d16SRodney W. Grimes 	if (logged_in || askpasswd == 0) {
1388ea022d16SRodney W. Grimes 		reply(503, "Login with USER first.");
1389ea022d16SRodney W. Grimes 		return;
1390ea022d16SRodney W. Grimes 	}
1391ea022d16SRodney W. Grimes 	askpasswd = 0;
1392ea022d16SRodney W. Grimes 	if (!guest) {		/* "ftp" is only account allowed no password */
1393a5a4544eSPaul Traina 		if (pw == NULL) {
1394a5a4544eSPaul Traina 			rval = 1;	/* failure below */
1395a5a4544eSPaul Traina 			goto skip;
1396a5a4544eSPaul Traina 		}
13975bc9d93dSMark Murray #ifdef USE_PAM
13986c9134c0SMark Murray 		rval = auth_pam(&pw, passwd);
1399f650a124SAndrey A. Chernov 		if (rval >= 0) {
1400f650a124SAndrey A. Chernov 			opieunlock();
1401a5a4544eSPaul Traina 			goto skip;
1402f650a124SAndrey A. Chernov 		}
1403f650a124SAndrey A. Chernov #endif
140447499ecdSAndrey A. Chernov 		if (opieverify(&opiedata, passwd) == 0)
140547499ecdSAndrey A. Chernov 			xpasswd = pw->pw_passwd;
1406f650a124SAndrey A. Chernov 		else if (pwok) {
140747499ecdSAndrey A. Chernov 			xpasswd = crypt(passwd, pw->pw_passwd);
1408f650a124SAndrey A. Chernov 			if (passwd[0] == '\0' && pw->pw_passwd[0] != '\0')
1409f650a124SAndrey A. Chernov 				xpasswd = ":";
1410f650a124SAndrey A. Chernov 		} else {
141147499ecdSAndrey A. Chernov 			rval = 1;
141247499ecdSAndrey A. Chernov 			goto skip;
141347499ecdSAndrey A. Chernov 		}
141447499ecdSAndrey A. Chernov 		rval = strcmp(pw->pw_passwd, xpasswd);
1415f650a124SAndrey A. Chernov 		if (pw->pw_expire && time(NULL) >= pw->pw_expire)
1416a5a4544eSPaul Traina 			rval = 1;	/* failure */
1417a5a4544eSPaul Traina skip:
1418a5a4544eSPaul Traina 		/*
1419a5a4544eSPaul Traina 		 * If rval == 1, the user failed the authentication check
14206c9134c0SMark Murray 		 * above.  If rval == 0, either PAM or local authentication
1421a5a4544eSPaul Traina 		 * succeeded.
1422a5a4544eSPaul Traina 		 */
1423a5a4544eSPaul Traina 		if (rval) {
1424ea022d16SRodney W. Grimes 			reply(530, "Login incorrect.");
14253656f229SKurt Lidl #ifdef USE_BLACKLIST
1426*504422faSKurt Lidl 			blacklist_notify(1, STDIN_FILENO, "Login incorrect");
14273656f229SKurt Lidl #endif
1428b8939f6fSYaroslav Tykhiy 			if (logging) {
1429ea022d16SRodney W. Grimes 				syslog(LOG_NOTICE,
1430b8939f6fSYaroslav Tykhiy 				    "FTP LOGIN FAILED FROM %s",
1431b8939f6fSYaroslav Tykhiy 				    remotehost);
1432b8939f6fSYaroslav Tykhiy 				syslog(LOG_AUTHPRIV | LOG_NOTICE,
1433ea022d16SRodney W. Grimes 				    "FTP LOGIN FAILED FROM %s, %s",
1434ea022d16SRodney W. Grimes 				    remotehost, curname);
1435b8939f6fSYaroslav Tykhiy 			}
1436ea022d16SRodney W. Grimes 			pw = NULL;
1437ea022d16SRodney W. Grimes 			if (login_attempts++ >= 5) {
1438ea022d16SRodney W. Grimes 				syslog(LOG_NOTICE,
1439ea022d16SRodney W. Grimes 				    "repeated login failures from %s",
1440ea022d16SRodney W. Grimes 				    remotehost);
1441ea022d16SRodney W. Grimes 				exit(0);
1442ea022d16SRodney W. Grimes 			}
1443ea022d16SRodney W. Grimes 			return;
1444ea022d16SRodney W. Grimes 		}
14453656f229SKurt Lidl #ifdef USE_BLACKLIST
14463656f229SKurt Lidl 		 else {
1447*504422faSKurt Lidl 			blacklist_notify(0, STDIN_FILENO, "Login successful");
14483656f229SKurt Lidl 		}
14493656f229SKurt Lidl #endif
1450ea022d16SRodney W. Grimes 	}
1451ea022d16SRodney W. Grimes 	login_attempts = 0;		/* this time successful */
1452c4536e21SYaroslav Tykhiy 	if (setegid(pw->pw_gid) < 0) {
1453ea022d16SRodney W. Grimes 		reply(550, "Can't set gid.");
1454ea022d16SRodney W. Grimes 		return;
1455ea022d16SRodney W. Grimes 	}
1456b071c689SDavid Nugent 	/* May be overridden by login.conf */
1457b071c689SDavid Nugent 	(void) umask(defumask);
1458b071c689SDavid Nugent #ifdef	LOGIN_CAP
14595d0bfe39SDavid Nugent 	if ((lc = login_getpwclass(pw)) != NULL) {
146004683b2cSYaroslav Tykhiy 		char	remote_ip[NI_MAXHOST];
1461b071c689SDavid Nugent 
146204683b2cSYaroslav Tykhiy 		if (getnameinfo((struct sockaddr *)&his_addr, his_addr.su_len,
14634dd8b5abSYoshinobu Inoue 			remote_ip, sizeof(remote_ip) - 1, NULL, 0,
146404683b2cSYaroslav Tykhiy 			NI_NUMERICHOST))
146504683b2cSYaroslav Tykhiy 				*remote_ip = 0;
1466b071c689SDavid Nugent 		remote_ip[sizeof(remote_ip) - 1] = 0;
1467b071c689SDavid Nugent 		if (!auth_hostok(lc, remotehost, remote_ip)) {
1468b071c689SDavid Nugent 			syslog(LOG_INFO|LOG_AUTH,
1469b071c689SDavid Nugent 			    "FTP LOGIN FAILED (HOST) as %s: permission denied.",
1470b071c689SDavid Nugent 			    pw->pw_name);
14714a3e5acdSYaroslav Tykhiy 			reply(530, "Permission denied.");
1472b071c689SDavid Nugent 			pw = NULL;
1473b071c689SDavid Nugent 			return;
1474b071c689SDavid Nugent 		}
1475b071c689SDavid Nugent 		if (!auth_timeok(lc, time(NULL))) {
14764a3e5acdSYaroslav Tykhiy 			reply(530, "Login not available right now.");
1477b071c689SDavid Nugent 			pw = NULL;
1478b071c689SDavid Nugent 			return;
1479b071c689SDavid Nugent 		}
1480b071c689SDavid Nugent 	}
1481e81b1c71SEdward Tomasz Napierala 	setusercontext(lc, pw, 0, LOGIN_SETALL &
1482e81b1c71SEdward Tomasz Napierala 		       ~(LOGIN_SETUSER | LOGIN_SETPATH | LOGIN_SETENV));
1483b071c689SDavid Nugent #else
1484e6fa0d43SDag-Erling Smørgrav 	setlogin(pw->pw_name);
1485ea022d16SRodney W. Grimes 	(void) initgroups(pw->pw_name, pw->pw_gid);
1486b071c689SDavid Nugent #endif
1487ea022d16SRodney W. Grimes 
14885bc9d93dSMark Murray #ifdef USE_PAM
14895bc9d93dSMark Murray 	if (pamh) {
14905bc9d93dSMark Murray 		if ((e = pam_open_session(pamh, 0)) != PAM_SUCCESS) {
14915bc9d93dSMark Murray 			syslog(LOG_ERR, "pam_open_session: %s", pam_strerror(pamh, e));
14925bc9d93dSMark Murray 		} else if ((e = pam_setcred(pamh, PAM_ESTABLISH_CRED)) != PAM_SUCCESS) {
14935bc9d93dSMark Murray 			syslog(LOG_ERR, "pam_setcred: %s", pam_strerror(pamh, e));
14945bc9d93dSMark Murray 		}
14955bc9d93dSMark Murray 	}
14965bc9d93dSMark Murray #endif
14975bc9d93dSMark Murray 
149880643af0SEd Schouten 	dochroot =
1499cefb6785SChristian S.J. Peron 		checkuser(_PATH_FTPCHROOT, pw->pw_name, 1, &residue, &ecode)
150080643af0SEd Schouten #ifdef	LOGIN_CAP	/* Allow login.conf configuration as well */
150180643af0SEd Schouten 		|| login_getcapbool(lc, "ftp-chroot", 0)
150280643af0SEd Schouten #endif
150380643af0SEd Schouten 	;
1504cefb6785SChristian S.J. Peron 	/*
1505cefb6785SChristian S.J. Peron 	 * It is possible that checkuser() failed to open the chroot file.
1506cefb6785SChristian S.J. Peron 	 * If this is the case, report that logins are un-available, since we
1507cefb6785SChristian S.J. Peron 	 * have no way of checking whether or not the user should be chrooted.
1508cefb6785SChristian S.J. Peron 	 * We ignore ENOENT since it is not required that this file be present.
1509cefb6785SChristian S.J. Peron 	 */
1510cefb6785SChristian S.J. Peron 	if (ecode != 0 && ecode != ENOENT) {
1511cefb6785SChristian S.J. Peron 		reply(530, "Login not available right now.");
1512cefb6785SChristian S.J. Peron 		return;
1513cefb6785SChristian S.J. Peron 	}
151480643af0SEd Schouten 	chrootdir = NULL;
151580643af0SEd Schouten 
15169f37b1a2SEd Schouten 	/* Disable wtmp logging when chrooting. */
15179f37b1a2SEd Schouten 	if (dochroot || guest)
15189f37b1a2SEd Schouten 		dowtmp = 0;
15199f37b1a2SEd Schouten 	if (dowtmp)
152080643af0SEd Schouten 		ftpd_logwtmp(wtmpid, pw->pw_name,
15215d7e0128SYaroslav Tykhiy 		    (struct sockaddr *)&his_addr);
1522ea022d16SRodney W. Grimes 	logged_in = 1;
1523ea022d16SRodney W. Grimes 
1524a5a4544eSPaul Traina 	if (guest && stats && statfd < 0)
1525ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
152663047c6fSDavid E. O'Brien 		statfd = open(thishost->statfile, O_WRONLY|O_APPEND);
1527ea4e54b9SDavid Nugent #else
152863047c6fSDavid E. O'Brien 		statfd = open(_PATH_FTPDSTATFILE, O_WRONLY|O_APPEND);
1529ea4e54b9SDavid Nugent #endif
153063047c6fSDavid E. O'Brien 		if (statfd < 0)
15313eb568f2SGuido van Rooij 			stats = 0;
15323eb568f2SGuido van Rooij 
1533ea022d16SRodney W. Grimes 	/*
1534ce9287fcSYaroslav Tykhiy 	 * For a chrooted local user,
1535ce9287fcSYaroslav Tykhiy 	 * a) see whether ftpchroot(5) specifies a chroot directory,
1536ce9287fcSYaroslav Tykhiy 	 * b) extract the directory pathname from the line,
1537ce9287fcSYaroslav Tykhiy 	 * c) expand it to the absolute pathname if necessary.
1538ea022d16SRodney W. Grimes 	 */
1539ce9287fcSYaroslav Tykhiy 	if (dochroot && residue &&
154039bce482SYaroslav Tykhiy 	    (chrootdir = strtok(residue, " \t")) != NULL) {
154139bce482SYaroslav Tykhiy 		if (chrootdir[0] != '/')
1542341e476eSYaroslav Tykhiy 			asprintf(&chrootdir, "%s/%s", pw->pw_dir, chrootdir);
154339bce482SYaroslav Tykhiy 		else
1544215a9f9dSYaroslav Tykhiy 			chrootdir = strdup(chrootdir); /* make it permanent */
1545341e476eSYaroslav Tykhiy 		if (chrootdir == NULL)
15468657b576SYaroslav Tykhiy 			fatalerror("Ran out of memory.");
15478657b576SYaroslav Tykhiy 	}
1548ce9287fcSYaroslav Tykhiy 	if (guest || dochroot) {
1549ce9287fcSYaroslav Tykhiy 		/*
1550ce9287fcSYaroslav Tykhiy 		 * If no chroot directory set yet, use the login directory.
1551ce9287fcSYaroslav Tykhiy 		 * Copy it so it can be modified while pw->pw_dir stays intact.
1552ce9287fcSYaroslav Tykhiy 		 */
1553ce9287fcSYaroslav Tykhiy 		if (chrootdir == NULL &&
1554ce9287fcSYaroslav Tykhiy 		    (chrootdir = strdup(pw->pw_dir)) == NULL)
1555ce9287fcSYaroslav Tykhiy 			fatalerror("Ran out of memory.");
1556ce9287fcSYaroslav Tykhiy 		/*
1557ce9287fcSYaroslav Tykhiy 		 * Check for the "/chroot/./home" syntax,
1558ce9287fcSYaroslav Tykhiy 		 * separate the chroot and home directory pathnames.
1559ce9287fcSYaroslav Tykhiy 		 */
1560ce9287fcSYaroslav Tykhiy 		if ((homedir = strstr(chrootdir, "/./")) != NULL) {
1561ce9287fcSYaroslav Tykhiy 			*(homedir++) = '\0';	/* wipe '/' */
1562ce9287fcSYaroslav Tykhiy 			homedir++;		/* skip '.' */
1563ce9287fcSYaroslav Tykhiy 		} else {
1564ce9287fcSYaroslav Tykhiy 			/*
1565ce9287fcSYaroslav Tykhiy 			 * We MUST do a chdir() after the chroot. Otherwise
1566ce9287fcSYaroslav Tykhiy 			 * the old current directory will be accessible as "."
1567ce9287fcSYaroslav Tykhiy 			 * outside the new root!
1568ce9287fcSYaroslav Tykhiy 			 */
1569ce9287fcSYaroslav Tykhiy 			homedir = "/";
1570ce9287fcSYaroslav Tykhiy 		}
1571ce9287fcSYaroslav Tykhiy 		/*
1572ce9287fcSYaroslav Tykhiy 		 * Finally, do chroot()
1573ce9287fcSYaroslav Tykhiy 		 */
1574ce9287fcSYaroslav Tykhiy 		if (chroot(chrootdir) < 0) {
1575a5a4544eSPaul Traina 			reply(550, "Can't change root.");
1576a5a4544eSPaul Traina 			goto bad;
1577a5a4544eSPaul Traina 		}
15783e65b9c6SColin Percival 		__FreeBSD_libc_enter_restricted_mode();
1579ce9287fcSYaroslav Tykhiy 	} else	/* real user w/o chroot */
1580ce9287fcSYaroslav Tykhiy 		homedir = pw->pw_dir;
1581ce9287fcSYaroslav Tykhiy 	/*
1582ce9287fcSYaroslav Tykhiy 	 * Set euid *before* doing chdir() so
1583ce9287fcSYaroslav Tykhiy 	 * a) the user won't be carried to a directory that he couldn't reach
1584ce9287fcSYaroslav Tykhiy 	 *    on his own due to no permission to upper path components,
1585ce9287fcSYaroslav Tykhiy 	 * b) NFS mounted homedirs w/restrictive permissions will be accessible
1586ce9287fcSYaroslav Tykhiy 	 *    (uid 0 has no root power over NFS if not mapped explicitly.)
1587ce9287fcSYaroslav Tykhiy 	 */
158852e7ee74SYaroslav Tykhiy 	if (seteuid(pw->pw_uid) < 0) {
1589ea022d16SRodney W. Grimes 		reply(550, "Can't set uid.");
1590ea022d16SRodney W. Grimes 		goto bad;
1591ea022d16SRodney W. Grimes 	}
1592ce9287fcSYaroslav Tykhiy 	if (chdir(homedir) < 0) {
1593ce9287fcSYaroslav Tykhiy 		if (guest || dochroot) {
1594ce9287fcSYaroslav Tykhiy 			reply(550, "Can't change to base directory.");
1595ce9287fcSYaroslav Tykhiy 			goto bad;
1596ce9287fcSYaroslav Tykhiy 		} else {
1597ce9287fcSYaroslav Tykhiy 			if (chdir("/") < 0) {
1598ce9287fcSYaroslav Tykhiy 				reply(550, "Root is inaccessible.");
1599ce9287fcSYaroslav Tykhiy 				goto bad;
1600ce9287fcSYaroslav Tykhiy 			}
160102c97492SYaroslav Tykhiy 			lreply(230, "No directory! Logging in with home=/.");
1602ce9287fcSYaroslav Tykhiy 		}
1603ce9287fcSYaroslav Tykhiy 	}
160482c76939SDavid Greenman 
160582c76939SDavid Greenman 	/*
1606ea022d16SRodney W. Grimes 	 * Display a login message, if it exists.
1607ea022d16SRodney W. Grimes 	 * N.B. reply(230,) must follow the message.
1608ea022d16SRodney W. Grimes 	 */
1609ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
161063047c6fSDavid E. O'Brien 	fd = fopen(thishost->loginmsg, "r");
1611ea4e54b9SDavid Nugent #else
161263047c6fSDavid E. O'Brien 	fd = fopen(_PATH_FTPLOGINMESG, "r");
1613ea4e54b9SDavid Nugent #endif
161463047c6fSDavid E. O'Brien 	if (fd != NULL) {
1615ea022d16SRodney W. Grimes 		char *cp, line[LINE_MAX];
1616ea022d16SRodney W. Grimes 
1617ea022d16SRodney W. Grimes 		while (fgets(line, sizeof(line), fd) != NULL) {
1618ea022d16SRodney W. Grimes 			if ((cp = strchr(line, '\n')) != NULL)
1619ea022d16SRodney W. Grimes 				*cp = '\0';
1620ea022d16SRodney W. Grimes 			lreply(230, "%s", line);
1621ea022d16SRodney W. Grimes 		}
1622ea022d16SRodney W. Grimes 		(void) fflush(stdout);
1623ea022d16SRodney W. Grimes 		(void) fclose(fd);
1624ea022d16SRodney W. Grimes 	}
1625ea022d16SRodney W. Grimes 	if (guest) {
16263eb568f2SGuido van Rooij 		if (ident != NULL)
16273eb568f2SGuido van Rooij 			free(ident);
162861f891a6SPaul Traina 		ident = strdup(passwd);
162961f891a6SPaul Traina 		if (ident == NULL)
1630618b0bbaSMark Murray 			fatalerror("Ran out of memory.");
163161f891a6SPaul Traina 
1632ea022d16SRodney W. Grimes 		reply(230, "Guest login ok, access restrictions apply.");
1633ea022d16SRodney W. Grimes #ifdef SETPROCTITLE
1634ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
1635ea4e54b9SDavid Nugent 		if (thishost != firsthost)
1636ea4e54b9SDavid Nugent 			snprintf(proctitle, sizeof(proctitle),
1637b3a0a7cdSMike Heffner 				 "%s: anonymous(%s)/%s", remotehost, hostname,
1638b3a0a7cdSMike Heffner 				 passwd);
1639ea4e54b9SDavid Nugent 		else
1640ea4e54b9SDavid Nugent #endif
1641ea022d16SRodney W. Grimes 			snprintf(proctitle, sizeof(proctitle),
1642b3a0a7cdSMike Heffner 				 "%s: anonymous/%s", remotehost, passwd);
1643b63e1fe2SPeter Wemm 		setproctitle("%s", proctitle);
1644ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */
1645ea022d16SRodney W. Grimes 		if (logging)
1646ea022d16SRodney W. Grimes 			syslog(LOG_INFO, "ANONYMOUS FTP LOGIN FROM %s, %s",
1647ea022d16SRodney W. Grimes 			    remotehost, passwd);
1648ea022d16SRodney W. Grimes 	} else {
16493401a71fSDaniel O'Callaghan 		if (dochroot)
165011342ab1SYaroslav Tykhiy 			reply(230, "User %s logged in, "
165111342ab1SYaroslav Tykhiy 				   "access restrictions apply.", pw->pw_name);
16523401a71fSDaniel O'Callaghan 		else
1653ea022d16SRodney W. Grimes 			reply(230, "User %s logged in.", pw->pw_name);
16543401a71fSDaniel O'Callaghan 
1655ea022d16SRodney W. Grimes #ifdef SETPROCTITLE
1656ea022d16SRodney W. Grimes 		snprintf(proctitle, sizeof(proctitle),
16577a29d7daSYaroslav Tykhiy 			 "%s: user/%s", remotehost, pw->pw_name);
1658b63e1fe2SPeter Wemm 		setproctitle("%s", proctitle);
1659ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */
1660ea022d16SRodney W. Grimes 		if (logging)
1661ea022d16SRodney W. Grimes 			syslog(LOG_INFO, "FTP LOGIN FROM %s as %s",
1662ea022d16SRodney W. Grimes 			    remotehost, pw->pw_name);
1663ea022d16SRodney W. Grimes 	}
1664220223fdSYaroslav Tykhiy 	if (logging && (guest || dochroot))
1665e897216fSYaroslav Tykhiy 		syslog(LOG_INFO, "session root changed to %s", chrootdir);
1666b071c689SDavid Nugent #ifdef	LOGIN_CAP
1667b071c689SDavid Nugent 	login_close(lc);
1668b071c689SDavid Nugent #endif
1669ce9287fcSYaroslav Tykhiy 	if (residue)
1670ce9287fcSYaroslav Tykhiy 		free(residue);
1671ea022d16SRodney W. Grimes 	return;
1672ea022d16SRodney W. Grimes bad:
1673ea022d16SRodney W. Grimes 	/* Forget all about it... */
1674b071c689SDavid Nugent #ifdef	LOGIN_CAP
1675b071c689SDavid Nugent 	login_close(lc);
1676b071c689SDavid Nugent #endif
1677ce9287fcSYaroslav Tykhiy 	if (residue)
1678ce9287fcSYaroslav Tykhiy 		free(residue);
1679ea022d16SRodney W. Grimes 	end_login();
1680ea022d16SRodney W. Grimes }
1681ea022d16SRodney W. Grimes 
1682ea022d16SRodney W. Grimes void
1683e4bc453cSWarner Losh retrieve(char *cmd, char *name)
1684ea022d16SRodney W. Grimes {
1685ea022d16SRodney W. Grimes 	FILE *fin, *dout;
1686ea022d16SRodney W. Grimes 	struct stat st;
1687e4bc453cSWarner Losh 	int (*closefunc)(FILE *);
1688158a00b2SJohn Birrell 	time_t start;
16898877d1dbSDon Lewis 	char line[BUFSIZ];
1690ea022d16SRodney W. Grimes 
1691ea022d16SRodney W. Grimes 	if (cmd == 0) {
1692ea022d16SRodney W. Grimes 		fin = fopen(name, "r"), closefunc = fclose;
1693ea022d16SRodney W. Grimes 		st.st_size = 0;
1694ea022d16SRodney W. Grimes 	} else {
16958877d1dbSDon Lewis 		(void) snprintf(line, sizeof(line), cmd, name);
16968877d1dbSDon Lewis 		name = line;
1697ea022d16SRodney W. Grimes 		fin = ftpd_popen(line, "r"), closefunc = ftpd_pclose;
1698ea022d16SRodney W. Grimes 		st.st_size = -1;
1699ea022d16SRodney W. Grimes 		st.st_blksize = BUFSIZ;
1700ea022d16SRodney W. Grimes 	}
1701ea022d16SRodney W. Grimes 	if (fin == NULL) {
1702ea022d16SRodney W. Grimes 		if (errno != 0) {
1703ea022d16SRodney W. Grimes 			perror_reply(550, name);
1704ea022d16SRodney W. Grimes 			if (cmd == 0) {
1705ea022d16SRodney W. Grimes 				LOGCMD("get", name);
1706ea022d16SRodney W. Grimes 			}
1707ea022d16SRodney W. Grimes 		}
1708ea022d16SRodney W. Grimes 		return;
1709ea022d16SRodney W. Grimes 	}
1710ea022d16SRodney W. Grimes 	byte_count = -1;
1711ea701226SYaroslav Tykhiy 	if (cmd == 0) {
1712ea701226SYaroslav Tykhiy 		if (fstat(fileno(fin), &st) < 0) {
1713ea701226SYaroslav Tykhiy 			perror_reply(550, name);
1714ea701226SYaroslav Tykhiy 			goto done;
1715ea701226SYaroslav Tykhiy 		}
1716ea701226SYaroslav Tykhiy 		if (!S_ISREG(st.st_mode)) {
171710e89104SYaroslav Tykhiy 			/*
171810e89104SYaroslav Tykhiy 			 * Never sending a raw directory is a workaround
171910e89104SYaroslav Tykhiy 			 * for buggy clients that will attempt to RETR
172010e89104SYaroslav Tykhiy 			 * a directory before listing it, e.g., Mozilla.
172110e89104SYaroslav Tykhiy 			 * Preventing a guest from getting irregular files
172210e89104SYaroslav Tykhiy 			 * is a simple security measure.
172310e89104SYaroslav Tykhiy 			 */
172410e89104SYaroslav Tykhiy 			if (S_ISDIR(st.st_mode) || guest) {
1725ea022d16SRodney W. Grimes 				reply(550, "%s: not a plain file.", name);
1726ea022d16SRodney W. Grimes 				goto done;
1727ea022d16SRodney W. Grimes 			}
1728ea701226SYaroslav Tykhiy 			st.st_size = -1;
1729ea701226SYaroslav Tykhiy 			/* st.st_blksize is set for all descriptor types */
1730ea701226SYaroslav Tykhiy 		}
1731ea701226SYaroslav Tykhiy 	}
1732ea022d16SRodney W. Grimes 	if (restart_point) {
1733ea022d16SRodney W. Grimes 		if (type == TYPE_A) {
1734ea022d16SRodney W. Grimes 			off_t i, n;
1735ea022d16SRodney W. Grimes 			int c;
1736ea022d16SRodney W. Grimes 
1737ea022d16SRodney W. Grimes 			n = restart_point;
1738ea022d16SRodney W. Grimes 			i = 0;
1739ea022d16SRodney W. Grimes 			while (i++ < n) {
1740ea022d16SRodney W. Grimes 				if ((c=getc(fin)) == EOF) {
1741ea022d16SRodney W. Grimes 					perror_reply(550, name);
1742ea022d16SRodney W. Grimes 					goto done;
1743ea022d16SRodney W. Grimes 				}
1744ea022d16SRodney W. Grimes 				if (c == '\n')
1745ea022d16SRodney W. Grimes 					i++;
1746ea022d16SRodney W. Grimes 			}
1747ea022d16SRodney W. Grimes 		} else if (lseek(fileno(fin), restart_point, L_SET) < 0) {
1748ea022d16SRodney W. Grimes 			perror_reply(550, name);
1749ea022d16SRodney W. Grimes 			goto done;
1750ea022d16SRodney W. Grimes 		}
1751ea022d16SRodney W. Grimes 	}
1752ea022d16SRodney W. Grimes 	dout = dataconn(name, st.st_size, "w");
1753ea022d16SRodney W. Grimes 	if (dout == NULL)
1754ea022d16SRodney W. Grimes 		goto done;
17553eb568f2SGuido van Rooij 	time(&start);
17569fc5823aSGarrett Wollman 	send_data(fin, dout, st.st_blksize, st.st_size,
17579fc5823aSGarrett Wollman 		  restart_point == 0 && cmd == 0 && S_ISREG(st.st_mode));
1758c999732bSYaroslav Tykhiy 	if (cmd == 0 && guest && stats && byte_count > 0)
1759c999732bSYaroslav Tykhiy 		logxfer(name, byte_count, start);
1760ea022d16SRodney W. Grimes 	(void) fclose(dout);
1761ea022d16SRodney W. Grimes 	data = -1;
1762ea022d16SRodney W. Grimes 	pdata = -1;
1763ea022d16SRodney W. Grimes done:
1764ea022d16SRodney W. Grimes 	if (cmd == 0)
1765ea022d16SRodney W. Grimes 		LOGBYTES("get", name, byte_count);
1766ea022d16SRodney W. Grimes 	(*closefunc)(fin);
1767ea022d16SRodney W. Grimes }
1768ea022d16SRodney W. Grimes 
1769ea022d16SRodney W. Grimes void
1770e4bc453cSWarner Losh store(char *name, char *mode, int unique)
1771ea022d16SRodney W. Grimes {
1772a117c345SYaroslav Tykhiy 	int fd;
1773ea022d16SRodney W. Grimes 	FILE *fout, *din;
1774e4bc453cSWarner Losh 	int (*closefunc)(FILE *);
1775ea022d16SRodney W. Grimes 
1776a117c345SYaroslav Tykhiy 	if (*mode == 'a') {		/* APPE */
1777a117c345SYaroslav Tykhiy 		if (unique) {
1778a117c345SYaroslav Tykhiy 			/* Programming error */
1779a117c345SYaroslav Tykhiy 			syslog(LOG_ERR, "Internal: unique flag to APPE");
1780a117c345SYaroslav Tykhiy 			unique = 0;
1781a117c345SYaroslav Tykhiy 		}
1782a117c345SYaroslav Tykhiy 		if (guest && noguestmod) {
178302c97492SYaroslav Tykhiy 			reply(550, "Appending to existing file denied.");
1784a117c345SYaroslav Tykhiy 			goto err;
1785a117c345SYaroslav Tykhiy 		}
1786a117c345SYaroslav Tykhiy 		restart_point = 0;	/* not affected by preceding REST */
1787a117c345SYaroslav Tykhiy 	}
1788a117c345SYaroslav Tykhiy 	if (unique)			/* STOU overrides REST */
1789a117c345SYaroslav Tykhiy 		restart_point = 0;
1790a117c345SYaroslav Tykhiy 	if (guest && noguestmod) {
1791a117c345SYaroslav Tykhiy 		if (restart_point) {	/* guest STOR w/REST */
179202c97492SYaroslav Tykhiy 			reply(550, "Modifying existing file denied.");
1793a117c345SYaroslav Tykhiy 			goto err;
1794a117c345SYaroslav Tykhiy 		} else			/* treat guest STOR as STOU */
1795a117c345SYaroslav Tykhiy 			unique = 1;
1796ea022d16SRodney W. Grimes 	}
1797ea022d16SRodney W. Grimes 
1798ea022d16SRodney W. Grimes 	if (restart_point)
1799a117c345SYaroslav Tykhiy 		mode = "r+";	/* so ASCII manual seek can work */
1800a117c345SYaroslav Tykhiy 	if (unique) {
1801a117c345SYaroslav Tykhiy 		if ((fd = guniquefd(name, &name)) < 0)
1802a117c345SYaroslav Tykhiy 			goto err;
1803a117c345SYaroslav Tykhiy 		fout = fdopen(fd, mode);
1804a117c345SYaroslav Tykhiy 	} else
1805ea022d16SRodney W. Grimes 		fout = fopen(name, mode);
1806ea022d16SRodney W. Grimes 	closefunc = fclose;
1807ea022d16SRodney W. Grimes 	if (fout == NULL) {
1808ea022d16SRodney W. Grimes 		perror_reply(553, name);
1809a117c345SYaroslav Tykhiy 		goto err;
1810ea022d16SRodney W. Grimes 	}
1811ea022d16SRodney W. Grimes 	byte_count = -1;
1812ea022d16SRodney W. Grimes 	if (restart_point) {
1813ea022d16SRodney W. Grimes 		if (type == TYPE_A) {
1814ea022d16SRodney W. Grimes 			off_t i, n;
1815ea022d16SRodney W. Grimes 			int c;
1816ea022d16SRodney W. Grimes 
1817ea022d16SRodney W. Grimes 			n = restart_point;
1818ea022d16SRodney W. Grimes 			i = 0;
1819ea022d16SRodney W. Grimes 			while (i++ < n) {
1820ea022d16SRodney W. Grimes 				if ((c=getc(fout)) == EOF) {
1821ea022d16SRodney W. Grimes 					perror_reply(550, name);
1822ea022d16SRodney W. Grimes 					goto done;
1823ea022d16SRodney W. Grimes 				}
1824ea022d16SRodney W. Grimes 				if (c == '\n')
1825ea022d16SRodney W. Grimes 					i++;
1826ea022d16SRodney W. Grimes 			}
1827ea022d16SRodney W. Grimes 			/*
1828ea022d16SRodney W. Grimes 			 * We must do this seek to "current" position
1829ea022d16SRodney W. Grimes 			 * because we are changing from reading to
1830ea022d16SRodney W. Grimes 			 * writing.
1831ea022d16SRodney W. Grimes 			 */
18320e519c96SYaroslav Tykhiy 			if (fseeko(fout, 0, SEEK_CUR) < 0) {
1833ea022d16SRodney W. Grimes 				perror_reply(550, name);
1834ea022d16SRodney W. Grimes 				goto done;
1835ea022d16SRodney W. Grimes 			}
1836ea022d16SRodney W. Grimes 		} else if (lseek(fileno(fout), restart_point, L_SET) < 0) {
1837ea022d16SRodney W. Grimes 			perror_reply(550, name);
1838ea022d16SRodney W. Grimes 			goto done;
1839ea022d16SRodney W. Grimes 		}
1840ea022d16SRodney W. Grimes 	}
18410e519c96SYaroslav Tykhiy 	din = dataconn(name, -1, "r");
1842ea022d16SRodney W. Grimes 	if (din == NULL)
1843ea022d16SRodney W. Grimes 		goto done;
1844ea022d16SRodney W. Grimes 	if (receive_data(din, fout) == 0) {
1845ea022d16SRodney W. Grimes 		if (unique)
1846ea022d16SRodney W. Grimes 			reply(226, "Transfer complete (unique file name:%s).",
1847ea022d16SRodney W. Grimes 			    name);
1848ea022d16SRodney W. Grimes 		else
1849ea022d16SRodney W. Grimes 			reply(226, "Transfer complete.");
1850ea022d16SRodney W. Grimes 	}
1851ea022d16SRodney W. Grimes 	(void) fclose(din);
1852ea022d16SRodney W. Grimes 	data = -1;
1853ea022d16SRodney W. Grimes 	pdata = -1;
1854ea022d16SRodney W. Grimes done:
18557c20f337SYaroslav Tykhiy 	LOGBYTES(*mode == 'a' ? "append" : "put", name, byte_count);
1856ea022d16SRodney W. Grimes 	(*closefunc)(fout);
1857a117c345SYaroslav Tykhiy 	return;
1858a117c345SYaroslav Tykhiy err:
1859a117c345SYaroslav Tykhiy 	LOGCMD(*mode == 'a' ? "append" : "put" , name);
1860a117c345SYaroslav Tykhiy 	return;
1861ea022d16SRodney W. Grimes }
1862ea022d16SRodney W. Grimes 
1863ea022d16SRodney W. Grimes static FILE *
1864e4bc453cSWarner Losh getdatasock(char *mode)
1865ea022d16SRodney W. Grimes {
1866ea022d16SRodney W. Grimes 	int on = 1, s, t, tries;
1867ea022d16SRodney W. Grimes 
1868ea022d16SRodney W. Grimes 	if (data >= 0)
1869ea022d16SRodney W. Grimes 		return (fdopen(data, mode));
18704dd8b5abSYoshinobu Inoue 
18714dd8b5abSYoshinobu Inoue 	s = socket(data_dest.su_family, SOCK_STREAM, 0);
1872ea022d16SRodney W. Grimes 	if (s < 0)
1873ea022d16SRodney W. Grimes 		goto bad;
187457d4ef07SYaroslav Tykhiy 	if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
1875406d1ae9SYaroslav Tykhiy 		syslog(LOG_WARNING, "data setsockopt (SO_REUSEADDR): %m");
1876ea022d16SRodney W. Grimes 	/* anchor socket to avoid multi-homing problems */
18774dd8b5abSYoshinobu Inoue 	data_source = ctrl_addr;
187863591ba5SYaroslav Tykhiy 	data_source.su_port = htons(dataport);
187952e7ee74SYaroslav Tykhiy 	(void) seteuid(0);
1880ea022d16SRodney W. Grimes 	for (tries = 1; ; tries++) {
18819ec7612aSYaroslav Tykhiy 		/*
18829ec7612aSYaroslav Tykhiy 		 * We should loop here since it's possible that
18839ec7612aSYaroslav Tykhiy 		 * another ftpd instance has passed this point and is
18849ec7612aSYaroslav Tykhiy 		 * trying to open a data connection in active mode now.
18859ec7612aSYaroslav Tykhiy 		 * Until the other connection is opened, we'll be getting
18869ec7612aSYaroslav Tykhiy 		 * EADDRINUSE because no SOCK_STREAM sockets in the system
18879ec7612aSYaroslav Tykhiy 		 * can share both local and remote addresses, localIP:20
18889ec7612aSYaroslav Tykhiy 		 * and *:* in this case.
18899ec7612aSYaroslav Tykhiy 		 */
1890ea022d16SRodney W. Grimes 		if (bind(s, (struct sockaddr *)&data_source,
18914dd8b5abSYoshinobu Inoue 		    data_source.su_len) >= 0)
1892ea022d16SRodney W. Grimes 			break;
1893ea022d16SRodney W. Grimes 		if (errno != EADDRINUSE || tries > 10)
1894ea022d16SRodney W. Grimes 			goto bad;
1895ea022d16SRodney W. Grimes 		sleep(tries);
1896ea022d16SRodney W. Grimes 	}
189752e7ee74SYaroslav Tykhiy 	(void) seteuid(pw->pw_uid);
1898ea022d16SRodney W. Grimes #ifdef IP_TOS
18994dd8b5abSYoshinobu Inoue 	if (data_source.su_family == AF_INET)
19004dd8b5abSYoshinobu Inoue       {
1901ea022d16SRodney W. Grimes 	on = IPTOS_THROUGHPUT;
190257d4ef07SYaroslav Tykhiy 	if (setsockopt(s, IPPROTO_IP, IP_TOS, &on, sizeof(int)) < 0)
1903406d1ae9SYaroslav Tykhiy 		syslog(LOG_WARNING, "data setsockopt (IP_TOS): %m");
19044dd8b5abSYoshinobu Inoue       }
1905ea022d16SRodney W. Grimes #endif
19069fc5823aSGarrett Wollman #ifdef TCP_NOPUSH
19079fc5823aSGarrett Wollman 	/*
19089fc5823aSGarrett Wollman 	 * Turn off push flag to keep sender TCP from sending short packets
190932072720SYaroslav Tykhiy 	 * at the boundaries of each write().
19109fc5823aSGarrett Wollman 	 */
19119fc5823aSGarrett Wollman 	on = 1;
191257d4ef07SYaroslav Tykhiy 	if (setsockopt(s, IPPROTO_TCP, TCP_NOPUSH, &on, sizeof on) < 0)
1913406d1ae9SYaroslav Tykhiy 		syslog(LOG_WARNING, "data setsockopt (TCP_NOPUSH): %m");
19149fc5823aSGarrett Wollman #endif
1915ea022d16SRodney W. Grimes 	return (fdopen(s, mode));
1916ea022d16SRodney W. Grimes bad:
1917ea022d16SRodney W. Grimes 	/* Return the real value of errno (close may change it) */
1918ea022d16SRodney W. Grimes 	t = errno;
191952e7ee74SYaroslav Tykhiy 	(void) seteuid(pw->pw_uid);
1920ea022d16SRodney W. Grimes 	(void) close(s);
1921ea022d16SRodney W. Grimes 	errno = t;
1922ea022d16SRodney W. Grimes 	return (NULL);
1923ea022d16SRodney W. Grimes }
1924ea022d16SRodney W. Grimes 
1925ea022d16SRodney W. Grimes static FILE *
1926e4bc453cSWarner Losh dataconn(char *name, off_t size, char *mode)
1927ea022d16SRodney W. Grimes {
1928ea022d16SRodney W. Grimes 	char sizebuf[32];
1929ea022d16SRodney W. Grimes 	FILE *file;
1930e5094456SCrist J. Clark 	int retry = 0, tos, conerrno;
1931ea022d16SRodney W. Grimes 
1932ea022d16SRodney W. Grimes 	file_size = size;
1933ea022d16SRodney W. Grimes 	byte_count = 0;
19340e519c96SYaroslav Tykhiy 	if (size != -1)
1935a57e1ef0SYaroslav Tykhiy 		(void) snprintf(sizebuf, sizeof(sizebuf),
1936a57e1ef0SYaroslav Tykhiy 				" (%jd bytes)", (intmax_t)size);
1937ea022d16SRodney W. Grimes 	else
1938e760ef2cSWarner Losh 		*sizebuf = '\0';
1939ea022d16SRodney W. Grimes 	if (pdata >= 0) {
19404dd8b5abSYoshinobu Inoue 		union sockunion from;
194178e3eed0SStefan Farfeleder 		socklen_t fromlen = ctrl_addr.su_len;
194278e3eed0SStefan Farfeleder 		int flags, s;
1943d6ed3c37SGuido van Rooij 		struct timeval timeout;
1944d6ed3c37SGuido van Rooij 		fd_set set;
1945ea022d16SRodney W. Grimes 
1946d6ed3c37SGuido van Rooij 		FD_ZERO(&set);
1947d6ed3c37SGuido van Rooij 		FD_SET(pdata, &set);
1948d6ed3c37SGuido van Rooij 
1949d6ed3c37SGuido van Rooij 		timeout.tv_usec = 0;
1950d6ed3c37SGuido van Rooij 		timeout.tv_sec = 120;
1951d6ed3c37SGuido van Rooij 
19524cd48bacSYaroslav Tykhiy 		/*
19534cd48bacSYaroslav Tykhiy 		 * Granted a socket is in the blocking I/O mode,
19544cd48bacSYaroslav Tykhiy 		 * accept() will block after a successful select()
19554cd48bacSYaroslav Tykhiy 		 * if the selected connection dies in between.
19564cd48bacSYaroslav Tykhiy 		 * Therefore set the non-blocking I/O flag here.
19574cd48bacSYaroslav Tykhiy 		 */
19584cd48bacSYaroslav Tykhiy 		if ((flags = fcntl(pdata, F_GETFL, 0)) == -1 ||
19594cd48bacSYaroslav Tykhiy 		    fcntl(pdata, F_SETFL, flags | O_NONBLOCK) == -1)
19604cd48bacSYaroslav Tykhiy 			goto pdata_err;
1961aa5a9d3fSYaroslav Tykhiy 		if (select(pdata+1, &set, NULL, NULL, &timeout) <= 0 ||
19624cd48bacSYaroslav Tykhiy 		    (s = accept(pdata, (struct sockaddr *) &from, &fromlen)) < 0)
19634cd48bacSYaroslav Tykhiy 			goto pdata_err;
1964ea022d16SRodney W. Grimes 		(void) close(pdata);
1965ea022d16SRodney W. Grimes 		pdata = s;
19664cd48bacSYaroslav Tykhiy 		/*
1967f6daca0dSYaroslav Tykhiy 		 * Unset the inherited non-blocking I/O flag
1968f6daca0dSYaroslav Tykhiy 		 * on the child socket so stdio can work on it.
19694cd48bacSYaroslav Tykhiy 		 */
19704cd48bacSYaroslav Tykhiy 		if ((flags = fcntl(pdata, F_GETFL, 0)) == -1 ||
19714cd48bacSYaroslav Tykhiy 		    fcntl(pdata, F_SETFL, flags & ~O_NONBLOCK) == -1)
19724cd48bacSYaroslav Tykhiy 			goto pdata_err;
1973ea022d16SRodney W. Grimes #ifdef IP_TOS
19744dd8b5abSYoshinobu Inoue 		if (from.su_family == AF_INET)
19754dd8b5abSYoshinobu Inoue 	      {
1976a5a4544eSPaul Traina 		tos = IPTOS_THROUGHPUT;
197757d4ef07SYaroslav Tykhiy 		if (setsockopt(s, IPPROTO_IP, IP_TOS, &tos, sizeof(int)) < 0)
1978406d1ae9SYaroslav Tykhiy 			syslog(LOG_WARNING, "pdata setsockopt (IP_TOS): %m");
19794dd8b5abSYoshinobu Inoue 	      }
1980ea022d16SRodney W. Grimes #endif
1981ea022d16SRodney W. Grimes 		reply(150, "Opening %s mode data connection for '%s'%s.",
1982ea022d16SRodney W. Grimes 		     type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf);
1983ea022d16SRodney W. Grimes 		return (fdopen(pdata, mode));
19844cd48bacSYaroslav Tykhiy pdata_err:
19854cd48bacSYaroslav Tykhiy 		reply(425, "Can't open data connection.");
19864cd48bacSYaroslav Tykhiy 		(void) close(pdata);
19874cd48bacSYaroslav Tykhiy 		pdata = -1;
19884cd48bacSYaroslav Tykhiy 		return (NULL);
1989ea022d16SRodney W. Grimes 	}
1990ea022d16SRodney W. Grimes 	if (data >= 0) {
1991ea022d16SRodney W. Grimes 		reply(125, "Using existing data connection for '%s'%s.",
1992ea022d16SRodney W. Grimes 		    name, sizebuf);
1993ea022d16SRodney W. Grimes 		usedefault = 1;
1994ea022d16SRodney W. Grimes 		return (fdopen(data, mode));
1995ea022d16SRodney W. Grimes 	}
1996ea022d16SRodney W. Grimes 	if (usedefault)
1997ea022d16SRodney W. Grimes 		data_dest = his_addr;
1998ea022d16SRodney W. Grimes 	usedefault = 1;
1999e5094456SCrist J. Clark 	do {
2000ea022d16SRodney W. Grimes 		file = getdatasock(mode);
2001ea022d16SRodney W. Grimes 		if (file == NULL) {
200204683b2cSYaroslav Tykhiy 			char hostbuf[NI_MAXHOST], portbuf[NI_MAXSERV];
200304683b2cSYaroslav Tykhiy 
200404683b2cSYaroslav Tykhiy 			if (getnameinfo((struct sockaddr *)&data_source,
200504683b2cSYaroslav Tykhiy 				data_source.su_len,
200604683b2cSYaroslav Tykhiy 				hostbuf, sizeof(hostbuf) - 1,
200704683b2cSYaroslav Tykhiy 				portbuf, sizeof(portbuf) - 1,
200804683b2cSYaroslav Tykhiy 				NI_NUMERICHOST|NI_NUMERICSERV))
200904683b2cSYaroslav Tykhiy 					*hostbuf = *portbuf = 0;
201004683b2cSYaroslav Tykhiy 			hostbuf[sizeof(hostbuf) - 1] = 0;
201104683b2cSYaroslav Tykhiy 			portbuf[sizeof(portbuf) - 1] = 0;
20124dd8b5abSYoshinobu Inoue 			reply(425, "Can't create data socket (%s,%s): %s.",
20134dd8b5abSYoshinobu Inoue 				hostbuf, portbuf, strerror(errno));
2014ea022d16SRodney W. Grimes 			return (NULL);
2015ea022d16SRodney W. Grimes 		}
2016ea022d16SRodney W. Grimes 		data = fileno(file);
2017e5094456SCrist J. Clark 		conerrno = 0;
2018e5094456SCrist J. Clark 		if (connect(data, (struct sockaddr *)&data_dest,
2019e5094456SCrist J. Clark 		    data_dest.su_len) == 0)
2020e5094456SCrist J. Clark 			break;
2021e5094456SCrist J. Clark 		conerrno = errno;
2022ea022d16SRodney W. Grimes 		(void) fclose(file);
2023ea022d16SRodney W. Grimes 		data = -1;
2024e5094456SCrist J. Clark 		if (conerrno == EADDRINUSE) {
2025e3765043SYaroslav Tykhiy 			sleep(swaitint);
2026e5094456SCrist J. Clark 			retry += swaitint;
2027e5094456SCrist J. Clark 		} else {
2028e5094456SCrist J. Clark 			break;
2029e5094456SCrist J. Clark 		}
2030e5094456SCrist J. Clark 	} while (retry <= swaitmax);
2031e5094456SCrist J. Clark 	if (conerrno != 0) {
203282c03024SYaroslav Tykhiy 		reply(425, "Can't build data connection: %s.",
203382c03024SYaroslav Tykhiy 			   strerror(conerrno));
2034ea022d16SRodney W. Grimes 		return (NULL);
2035ea022d16SRodney W. Grimes 	}
2036ea022d16SRodney W. Grimes 	reply(150, "Opening %s mode data connection for '%s'%s.",
2037ea022d16SRodney W. Grimes 	     type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf);
2038ea022d16SRodney W. Grimes 	return (file);
2039ea022d16SRodney W. Grimes }
2040ea022d16SRodney W. Grimes 
2041ea022d16SRodney W. Grimes /*
20424cd51076SYaroslav Tykhiy  * A helper macro to avoid code duplication
20434cd51076SYaroslav Tykhiy  * in send_data() and receive_data().
20444cd51076SYaroslav Tykhiy  *
20454cd51076SYaroslav Tykhiy  * XXX We have to block SIGURG during putc() because BSD stdio
20464cd51076SYaroslav Tykhiy  * is unable to restart interrupted write operations and hence
20474cd51076SYaroslav Tykhiy  * the entire buffer contents will be lost as soon as a write()
20484cd51076SYaroslav Tykhiy  * call indicates EINTR to stdio.
20494cd51076SYaroslav Tykhiy  */
20504cd51076SYaroslav Tykhiy #define FTPD_PUTC(ch, file, label)					\
20514cd51076SYaroslav Tykhiy 	do {								\
20524cd51076SYaroslav Tykhiy 		int ret;						\
20534cd51076SYaroslav Tykhiy 									\
20544cd51076SYaroslav Tykhiy 		do {							\
20554cd51076SYaroslav Tykhiy 			START_UNSAFE;					\
20564cd51076SYaroslav Tykhiy 			ret = putc((ch), (file));			\
20574cd51076SYaroslav Tykhiy 			END_UNSAFE;					\
20584cd51076SYaroslav Tykhiy 			CHECKOOB(return (-1))				\
20594cd51076SYaroslav Tykhiy 			else if (ferror(file))				\
20604cd51076SYaroslav Tykhiy 				goto label;				\
20614cd51076SYaroslav Tykhiy 			clearerr(file);					\
20624cd51076SYaroslav Tykhiy 		} while (ret == EOF);					\
20634cd51076SYaroslav Tykhiy 	} while (0)
20644cd51076SYaroslav Tykhiy 
20654cd51076SYaroslav Tykhiy /*
2066ec489d64SPedro F. Giffuni  * Transfer the contents of "instr" to "outstr" peer using the appropriate
20679fc5823aSGarrett Wollman  * encapsulation of the data subject to Mode, Structure, and Type.
2068ea022d16SRodney W. Grimes  *
2069ea022d16SRodney W. Grimes  * NB: Form isn't handled.
2070ea022d16SRodney W. Grimes  */
20714b82fc95SYaroslav Tykhiy static int
20726e4b0a55SYaroslav Tykhiy send_data(FILE *instr, FILE *outstr, size_t blksize, off_t filesize, int isreg)
2073ea022d16SRodney W. Grimes {
2074db1c2da3SYaroslav Tykhiy 	int c, cp, filefd, netfd;
2075f6f0c4b9SDan Moschuk 	char *buf;
2076ea022d16SRodney W. Grimes 
20774cd51076SYaroslav Tykhiy 	STARTXFER;
20784cd51076SYaroslav Tykhiy 
2079ea022d16SRodney W. Grimes 	switch (type) {
2080ea022d16SRodney W. Grimes 
2081ea022d16SRodney W. Grimes 	case TYPE_A:
20824cd51076SYaroslav Tykhiy 		cp = EOF;
20834cd51076SYaroslav Tykhiy 		for (;;) {
20844cd51076SYaroslav Tykhiy 			c = getc(instr);
20854cd51076SYaroslav Tykhiy 			CHECKOOB(return (-1))
20864cd51076SYaroslav Tykhiy 			else if (c == EOF && ferror(instr))
20874cd51076SYaroslav Tykhiy 				goto file_err;
20884cd51076SYaroslav Tykhiy 			if (c == EOF) {
20894cd51076SYaroslav Tykhiy 				if (ferror(instr)) {	/* resume after OOB */
20904cd51076SYaroslav Tykhiy 					clearerr(instr);
20914cd51076SYaroslav Tykhiy 					continue;
2092ea022d16SRodney W. Grimes 				}
20934cd51076SYaroslav Tykhiy 				if (feof(instr))	/* EOF */
20944cd51076SYaroslav Tykhiy 					break;
20954cd51076SYaroslav Tykhiy 				syslog(LOG_ERR, "Internal: impossible condition"
20964cd51076SYaroslav Tykhiy 						" on file after getc()");
20974cd51076SYaroslav Tykhiy 				goto file_err;
20984cd51076SYaroslav Tykhiy 			}
20994cd51076SYaroslav Tykhiy 			if (c == '\n' && cp != '\r') {
21004cd51076SYaroslav Tykhiy 				FTPD_PUTC('\r', outstr, data_err);
21014cd51076SYaroslav Tykhiy 				byte_count++;
21024cd51076SYaroslav Tykhiy 			}
21034cd51076SYaroslav Tykhiy 			FTPD_PUTC(c, outstr, data_err);
21044cd51076SYaroslav Tykhiy 			byte_count++;
2105db1c2da3SYaroslav Tykhiy 			cp = c;
2106ea022d16SRodney W. Grimes 		}
21074cd51076SYaroslav Tykhiy #ifdef notyet	/* BSD stdio isn't ready for that */
21084cd51076SYaroslav Tykhiy 		while (fflush(outstr) == EOF) {
21094cd51076SYaroslav Tykhiy 			CHECKOOB(return (-1))
21104cd51076SYaroslav Tykhiy 			else
2111ea022d16SRodney W. Grimes 				goto data_err;
21124cd51076SYaroslav Tykhiy 			clearerr(outstr);
21134cd51076SYaroslav Tykhiy 		}
21144cd51076SYaroslav Tykhiy 		ENDXFER;
21154cd51076SYaroslav Tykhiy #else
21164cd51076SYaroslav Tykhiy 		ENDXFER;
21174cd51076SYaroslav Tykhiy 		if (fflush(outstr) == EOF)
21184cd51076SYaroslav Tykhiy 			goto data_err;
21194cd51076SYaroslav Tykhiy #endif
2120ea022d16SRodney W. Grimes 		reply(226, "Transfer complete.");
21214b82fc95SYaroslav Tykhiy 		return (0);
2122ea022d16SRodney W. Grimes 
2123ea022d16SRodney W. Grimes 	case TYPE_I:
2124ea022d16SRodney W. Grimes 	case TYPE_L:
21259fc5823aSGarrett Wollman 		/*
21269fc5823aSGarrett Wollman 		 * isreg is only set if we are not doing restart and we
21279fc5823aSGarrett Wollman 		 * are sending a regular file
21289fc5823aSGarrett Wollman 		 */
21299fc5823aSGarrett Wollman 		netfd = fileno(outstr);
21309fc5823aSGarrett Wollman 		filefd = fileno(instr);
21319fc5823aSGarrett Wollman 
2132f6f0c4b9SDan Moschuk 		if (isreg) {
21332e22b914SYaroslav Tykhiy 			char *msg = "Transfer complete.";
21344cd51076SYaroslav Tykhiy 			off_t cnt, offset;
2135f6f0c4b9SDan Moschuk 			int err;
2136f6f0c4b9SDan Moschuk 
21372f492fc8SYaroslav Tykhiy 			cnt = offset = 0;
2138f6f0c4b9SDan Moschuk 
21392f492fc8SYaroslav Tykhiy 			while (filesize > 0) {
2140492f1d9cSMaxim Konovalov 				err = sendfile(filefd, netfd, offset, 0,
21417e295315SYaroslav Tykhiy 					       NULL, &cnt, 0);
21423af48c42SMaxim Konovalov 				/*
21433af48c42SMaxim Konovalov 				 * Calculate byte_count before OOB processing.
21443af48c42SMaxim Konovalov 				 * It can be used in myoob() later.
21453af48c42SMaxim Konovalov 				 */
21463af48c42SMaxim Konovalov 				byte_count += cnt;
2147f6f0c4b9SDan Moschuk 				offset += cnt;
2148492f1d9cSMaxim Konovalov 				filesize -= cnt;
21494cd51076SYaroslav Tykhiy 				CHECKOOB(return (-1))
21504cd51076SYaroslav Tykhiy 				else if (err == -1) {
21514cd51076SYaroslav Tykhiy 					if (errno != EINTR &&
21524cd51076SYaroslav Tykhiy 					    cnt == 0 && offset == 0)
2153f6f0c4b9SDan Moschuk 						goto oldway;
21549fc5823aSGarrett Wollman 					goto data_err;
2155f6f0c4b9SDan Moschuk 				}
21564cd51076SYaroslav Tykhiy 				if (err == -1)	/* resume after OOB */
21574cd51076SYaroslav Tykhiy 					continue;
21582e22b914SYaroslav Tykhiy 				/*
21592e22b914SYaroslav Tykhiy 				 * We hit the EOF prematurely.
21602e22b914SYaroslav Tykhiy 				 * Perhaps the file was externally truncated.
21612e22b914SYaroslav Tykhiy 				 */
21622e22b914SYaroslav Tykhiy 				if (cnt == 0) {
21632e22b914SYaroslav Tykhiy 					msg = "Transfer finished due to "
21642e22b914SYaroslav Tykhiy 					      "premature end of file.";
21652e22b914SYaroslav Tykhiy 					break;
21662e22b914SYaroslav Tykhiy 				}
2167f6f0c4b9SDan Moschuk 			}
21684cd51076SYaroslav Tykhiy 			ENDXFER;
216962f390ecSEd Maste 			reply(226, "%s", msg);
21704b82fc95SYaroslav Tykhiy 			return (0);
21719fc5823aSGarrett Wollman 		}
21729fc5823aSGarrett Wollman 
21739fc5823aSGarrett Wollman oldway:
2174e3765043SYaroslav Tykhiy 		if ((buf = malloc(blksize)) == NULL) {
21754cd51076SYaroslav Tykhiy 			ENDXFER;
217602c97492SYaroslav Tykhiy 			reply(451, "Ran out of memory.");
21774b82fc95SYaroslav Tykhiy 			return (-1);
2178ea022d16SRodney W. Grimes 		}
21799fc5823aSGarrett Wollman 
21804cd51076SYaroslav Tykhiy 		for (;;) {
21814cd51076SYaroslav Tykhiy 			int cnt, len;
21824cd51076SYaroslav Tykhiy 			char *bp;
21834cd51076SYaroslav Tykhiy 
21844cd51076SYaroslav Tykhiy 			cnt = read(filefd, buf, blksize);
21854cd51076SYaroslav Tykhiy 			CHECKOOB(free(buf); return (-1))
21864cd51076SYaroslav Tykhiy 			else if (cnt < 0) {
21878efc8b18SYaroslav Tykhiy 				free(buf);
2188ea022d16SRodney W. Grimes 				goto file_err;
21894cd51076SYaroslav Tykhiy 			}
21904cd51076SYaroslav Tykhiy 			if (cnt < 0)	/* resume after OOB */
21914cd51076SYaroslav Tykhiy 				continue;
21924cd51076SYaroslav Tykhiy 			if (cnt == 0)	/* EOF */
21934cd51076SYaroslav Tykhiy 				break;
21944cd51076SYaroslav Tykhiy 			for (len = cnt, bp = buf; len > 0;) {
21954cd51076SYaroslav Tykhiy 				cnt = write(netfd, bp, len);
21964cd51076SYaroslav Tykhiy 				CHECKOOB(free(buf); return (-1))
21974cd51076SYaroslav Tykhiy 				else if (cnt < 0) {
21984cd51076SYaroslav Tykhiy 					free(buf);
2199ea022d16SRodney W. Grimes 					goto data_err;
2200ea022d16SRodney W. Grimes 				}
22014cd51076SYaroslav Tykhiy 				if (cnt <= 0)
22024cd51076SYaroslav Tykhiy 					continue;
22034cd51076SYaroslav Tykhiy 				len -= cnt;
22044cd51076SYaroslav Tykhiy 				bp += cnt;
22054cd51076SYaroslav Tykhiy 				byte_count += cnt;
22064cd51076SYaroslav Tykhiy 			}
22074cd51076SYaroslav Tykhiy 		}
22084cd51076SYaroslav Tykhiy 		ENDXFER;
22094cd51076SYaroslav Tykhiy 		free(buf);
2210ea022d16SRodney W. Grimes 		reply(226, "Transfer complete.");
22114b82fc95SYaroslav Tykhiy 		return (0);
2212ea022d16SRodney W. Grimes 	default:
22134cd51076SYaroslav Tykhiy 		ENDXFER;
221402c97492SYaroslav Tykhiy 		reply(550, "Unimplemented TYPE %d in send_data.", type);
22154b82fc95SYaroslav Tykhiy 		return (-1);
2216ea022d16SRodney W. Grimes 	}
2217ea022d16SRodney W. Grimes 
2218ea022d16SRodney W. Grimes data_err:
22194cd51076SYaroslav Tykhiy 	ENDXFER;
2220ea022d16SRodney W. Grimes 	perror_reply(426, "Data connection");
22214b82fc95SYaroslav Tykhiy 	return (-1);
2222ea022d16SRodney W. Grimes 
2223ea022d16SRodney W. Grimes file_err:
22244cd51076SYaroslav Tykhiy 	ENDXFER;
2225ea022d16SRodney W. Grimes 	perror_reply(551, "Error on input file");
22264b82fc95SYaroslav Tykhiy 	return (-1);
2227ea022d16SRodney W. Grimes }
2228ea022d16SRodney W. Grimes 
2229ea022d16SRodney W. Grimes /*
2230ea022d16SRodney W. Grimes  * Transfer data from peer to "outstr" using the appropriate encapulation of
2231ea022d16SRodney W. Grimes  * the data subject to Mode, Structure, and Type.
2232ea022d16SRodney W. Grimes  *
2233ea022d16SRodney W. Grimes  * N.B.: Form isn't handled.
2234ea022d16SRodney W. Grimes  */
2235ea022d16SRodney W. Grimes static int
2236e4bc453cSWarner Losh receive_data(FILE *instr, FILE *outstr)
2237ea022d16SRodney W. Grimes {
22384cd51076SYaroslav Tykhiy 	int c, cp;
22394cd51076SYaroslav Tykhiy 	int bare_lfs = 0;
2240ea022d16SRodney W. Grimes 
22414cd51076SYaroslav Tykhiy 	STARTXFER;
224261f891a6SPaul Traina 
2243ea022d16SRodney W. Grimes 	switch (type) {
2244ea022d16SRodney W. Grimes 
2245ea022d16SRodney W. Grimes 	case TYPE_I:
2246ea022d16SRodney W. Grimes 	case TYPE_L:
22474cd51076SYaroslav Tykhiy 		for (;;) {
22484cd51076SYaroslav Tykhiy 			int cnt, len;
22494cd51076SYaroslav Tykhiy 			char *bp;
22504cd51076SYaroslav Tykhiy 			char buf[BUFSIZ];
22514cd51076SYaroslav Tykhiy 
22524cd51076SYaroslav Tykhiy 			cnt = read(fileno(instr), buf, sizeof(buf));
22534cd51076SYaroslav Tykhiy 			CHECKOOB(return (-1))
22544cd51076SYaroslav Tykhiy 			else if (cnt < 0)
22554cd51076SYaroslav Tykhiy 				goto data_err;
22564cd51076SYaroslav Tykhiy 			if (cnt < 0)	/* resume after OOB */
22574cd51076SYaroslav Tykhiy 				continue;
22584cd51076SYaroslav Tykhiy 			if (cnt == 0)	/* EOF */
22594cd51076SYaroslav Tykhiy 				break;
22604cd51076SYaroslav Tykhiy 			for (len = cnt, bp = buf; len > 0;) {
22614cd51076SYaroslav Tykhiy 				cnt = write(fileno(outstr), bp, len);
22624cd51076SYaroslav Tykhiy 				CHECKOOB(return (-1))
22634cd51076SYaroslav Tykhiy 				else if (cnt < 0)
2264ea022d16SRodney W. Grimes 					goto file_err;
22654cd51076SYaroslav Tykhiy 				if (cnt <= 0)
22664cd51076SYaroslav Tykhiy 					continue;
22674cd51076SYaroslav Tykhiy 				len -= cnt;
22684cd51076SYaroslav Tykhiy 				bp += cnt;
2269ea022d16SRodney W. Grimes 				byte_count += cnt;
2270ea022d16SRodney W. Grimes 			}
22714cd51076SYaroslav Tykhiy 		}
22724cd51076SYaroslav Tykhiy 		ENDXFER;
2273ea022d16SRodney W. Grimes 		return (0);
2274ea022d16SRodney W. Grimes 
2275ea022d16SRodney W. Grimes 	case TYPE_E:
22764cd51076SYaroslav Tykhiy 		ENDXFER;
2277ea022d16SRodney W. Grimes 		reply(553, "TYPE E not implemented.");
2278ea022d16SRodney W. Grimes 		return (-1);
2279ea022d16SRodney W. Grimes 
2280ea022d16SRodney W. Grimes 	case TYPE_A:
22814cd51076SYaroslav Tykhiy 		cp = EOF;
22824cd51076SYaroslav Tykhiy 		for (;;) {
22834cd51076SYaroslav Tykhiy 			c = getc(instr);
22844cd51076SYaroslav Tykhiy 			CHECKOOB(return (-1))
22854cd51076SYaroslav Tykhiy 			else if (c == EOF && ferror(instr))
22864cd51076SYaroslav Tykhiy 				goto data_err;
22874cd51076SYaroslav Tykhiy 			if (c == EOF && ferror(instr)) { /* resume after OOB */
22884cd51076SYaroslav Tykhiy 				clearerr(instr);
22894cd51076SYaroslav Tykhiy 				continue;
22904cd51076SYaroslav Tykhiy 			}
22914cd51076SYaroslav Tykhiy 
22924cd51076SYaroslav Tykhiy 			if (cp == '\r') {
22934cd51076SYaroslav Tykhiy 				if (c != '\n')
22944cd51076SYaroslav Tykhiy 					FTPD_PUTC('\r', outstr, file_err);
22954cd51076SYaroslav Tykhiy 			} else
2296ea022d16SRodney W. Grimes 				if (c == '\n')
2297ea022d16SRodney W. Grimes 					bare_lfs++;
22984cd51076SYaroslav Tykhiy 			if (c == '\r') {
22994cd51076SYaroslav Tykhiy 				byte_count++;
23004cd51076SYaroslav Tykhiy 				cp = c;
23014cd51076SYaroslav Tykhiy 				continue;
23024cd51076SYaroslav Tykhiy 			}
23034cd51076SYaroslav Tykhiy 
23044cd51076SYaroslav Tykhiy 			/* Check for EOF here in order not to lose last \r. */
23054cd51076SYaroslav Tykhiy 			if (c == EOF) {
23064cd51076SYaroslav Tykhiy 				if (feof(instr))	/* EOF */
23074cd51076SYaroslav Tykhiy 					break;
23084cd51076SYaroslav Tykhiy 				syslog(LOG_ERR, "Internal: impossible condition"
23094cd51076SYaroslav Tykhiy 						" on data stream after getc()");
2310ea022d16SRodney W. Grimes 				goto data_err;
2311ea022d16SRodney W. Grimes 			}
23124cd51076SYaroslav Tykhiy 
23134cd51076SYaroslav Tykhiy 			byte_count++;
23144cd51076SYaroslav Tykhiy 			FTPD_PUTC(c, outstr, file_err);
23154cd51076SYaroslav Tykhiy 			cp = c;
2316ea022d16SRodney W. Grimes 		}
23174cd51076SYaroslav Tykhiy #ifdef notyet	/* BSD stdio isn't ready for that */
23184cd51076SYaroslav Tykhiy 		while (fflush(outstr) == EOF) {
23194cd51076SYaroslav Tykhiy 			CHECKOOB(return (-1))
23204cd51076SYaroslav Tykhiy 			else
2321ea022d16SRodney W. Grimes 				goto file_err;
23224cd51076SYaroslav Tykhiy 			clearerr(outstr);
23234cd51076SYaroslav Tykhiy 		}
23244cd51076SYaroslav Tykhiy 		ENDXFER;
23254cd51076SYaroslav Tykhiy #else
23264cd51076SYaroslav Tykhiy 		ENDXFER;
23274cd51076SYaroslav Tykhiy 		if (fflush(outstr) == EOF)
23284cd51076SYaroslav Tykhiy 			goto file_err;
23294cd51076SYaroslav Tykhiy #endif
2330ea022d16SRodney W. Grimes 		if (bare_lfs) {
2331ea022d16SRodney W. Grimes 			lreply(226,
233202c97492SYaroslav Tykhiy 		"WARNING! %d bare linefeeds received in ASCII mode.",
2333ea022d16SRodney W. Grimes 			    bare_lfs);
2334ea022d16SRodney W. Grimes 		(void)printf("   File may not have transferred correctly.\r\n");
2335ea022d16SRodney W. Grimes 		}
2336ea022d16SRodney W. Grimes 		return (0);
2337ea022d16SRodney W. Grimes 	default:
23384cd51076SYaroslav Tykhiy 		ENDXFER;
233902c97492SYaroslav Tykhiy 		reply(550, "Unimplemented TYPE %d in receive_data.", type);
2340ea022d16SRodney W. Grimes 		return (-1);
2341ea022d16SRodney W. Grimes 	}
2342ea022d16SRodney W. Grimes 
2343ea022d16SRodney W. Grimes data_err:
23444cd51076SYaroslav Tykhiy 	ENDXFER;
234502c97492SYaroslav Tykhiy 	perror_reply(426, "Data connection");
2346ea022d16SRodney W. Grimes 	return (-1);
2347ea022d16SRodney W. Grimes 
2348ea022d16SRodney W. Grimes file_err:
23494cd51076SYaroslav Tykhiy 	ENDXFER;
235002c97492SYaroslav Tykhiy 	perror_reply(452, "Error writing to file");
2351ea022d16SRodney W. Grimes 	return (-1);
2352ea022d16SRodney W. Grimes }
2353ea022d16SRodney W. Grimes 
2354ea022d16SRodney W. Grimes void
2355e4bc453cSWarner Losh statfilecmd(char *filename)
2356ea022d16SRodney W. Grimes {
2357ea022d16SRodney W. Grimes 	FILE *fin;
2358f8a581a0SYaroslav Tykhiy 	int atstart;
235941c57b48SYaroslav Tykhiy 	int c, code;
2360ea022d16SRodney W. Grimes 	char line[LINE_MAX];
236141c57b48SYaroslav Tykhiy 	struct stat st;
2362ea022d16SRodney W. Grimes 
236341c57b48SYaroslav Tykhiy 	code = lstat(filename, &st) == 0 && S_ISDIR(st.st_mode) ? 212 : 213;
2364af85d782SDavid Nugent 	(void)snprintf(line, sizeof(line), _PATH_LS " -lgA %s", filename);
2365ea022d16SRodney W. Grimes 	fin = ftpd_popen(line, "r");
2366763e8c96SEd Maste 	if (fin == NULL) {
2367763e8c96SEd Maste 		perror_reply(551, filename);
2368763e8c96SEd Maste 		return;
2369763e8c96SEd Maste 	}
23703b48b877SYaroslav Tykhiy 	lreply(code, "Status of %s:", filename);
2371f8a581a0SYaroslav Tykhiy 	atstart = 1;
2372ea022d16SRodney W. Grimes 	while ((c = getc(fin)) != EOF) {
2373ea022d16SRodney W. Grimes 		if (c == '\n') {
2374ea022d16SRodney W. Grimes 			if (ferror(stdout)){
237502c97492SYaroslav Tykhiy 				perror_reply(421, "Control connection");
2376ea022d16SRodney W. Grimes 				(void) ftpd_pclose(fin);
2377ea022d16SRodney W. Grimes 				dologout(1);
2378ea022d16SRodney W. Grimes 				/* NOTREACHED */
2379ea022d16SRodney W. Grimes 			}
2380ea022d16SRodney W. Grimes 			if (ferror(fin)) {
2381ea022d16SRodney W. Grimes 				perror_reply(551, filename);
2382ea022d16SRodney W. Grimes 				(void) ftpd_pclose(fin);
2383ea022d16SRodney W. Grimes 				return;
2384ea022d16SRodney W. Grimes 			}
2385ea022d16SRodney W. Grimes 			(void) putc('\r', stdout);
2386ea022d16SRodney W. Grimes 		}
2387f8a581a0SYaroslav Tykhiy 		/*
2388f8a581a0SYaroslav Tykhiy 		 * RFC 959 says neutral text should be prepended before
2389f8a581a0SYaroslav Tykhiy 		 * a leading 3-digit number followed by whitespace, but
2390f8a581a0SYaroslav Tykhiy 		 * many ftp clients can be confused by any leading digits,
2391f8a581a0SYaroslav Tykhiy 		 * as a matter of fact.
2392f8a581a0SYaroslav Tykhiy 		 */
2393f8a581a0SYaroslav Tykhiy 		if (atstart && isdigit(c))
2394f8a581a0SYaroslav Tykhiy 			(void) putc(' ', stdout);
2395ea022d16SRodney W. Grimes 		(void) putc(c, stdout);
2396f8a581a0SYaroslav Tykhiy 		atstart = (c == '\n');
2397ea022d16SRodney W. Grimes 	}
2398ea022d16SRodney W. Grimes 	(void) ftpd_pclose(fin);
239902c97492SYaroslav Tykhiy 	reply(code, "End of status.");
2400ea022d16SRodney W. Grimes }
2401ea022d16SRodney W. Grimes 
2402ea022d16SRodney W. Grimes void
2403e4bc453cSWarner Losh statcmd(void)
2404ea022d16SRodney W. Grimes {
24054dd8b5abSYoshinobu Inoue 	union sockunion *su;
2406ea022d16SRodney W. Grimes 	u_char *a, *p;
2407b0f06defSHajimu UMEMOTO 	char hname[NI_MAXHOST];
24084dd8b5abSYoshinobu Inoue 	int ispassive;
2409ea022d16SRodney W. Grimes 
2410c152df28SYaroslav Tykhiy 	if (hostinfo) {
2411a23f61bcSYaroslav Tykhiy 		lreply(211, "%s FTP server status:", hostname);
2412ea022d16SRodney W. Grimes 		printf("     %s\r\n", version);
2413c152df28SYaroslav Tykhiy 	} else
2414c152df28SYaroslav Tykhiy 		lreply(211, "FTP server status:");
2415ea022d16SRodney W. Grimes 	printf("     Connected to %s", remotehost);
24164dd8b5abSYoshinobu Inoue 	if (!getnameinfo((struct sockaddr *)&his_addr, his_addr.su_len,
2417b0f06defSHajimu UMEMOTO 			 hname, sizeof(hname) - 1, NULL, 0, NI_NUMERICHOST)) {
241804683b2cSYaroslav Tykhiy 		hname[sizeof(hname) - 1] = 0;
24194dd8b5abSYoshinobu Inoue 		if (strcmp(hname, remotehost) != 0)
24204dd8b5abSYoshinobu Inoue 			printf(" (%s)", hname);
24214dd8b5abSYoshinobu Inoue 	}
2422ea022d16SRodney W. Grimes 	printf("\r\n");
2423ea022d16SRodney W. Grimes 	if (logged_in) {
2424ea022d16SRodney W. Grimes 		if (guest)
2425ea022d16SRodney W. Grimes 			printf("     Logged in anonymously\r\n");
2426ea022d16SRodney W. Grimes 		else
2427ea022d16SRodney W. Grimes 			printf("     Logged in as %s\r\n", pw->pw_name);
2428ea022d16SRodney W. Grimes 	} else if (askpasswd)
2429ea022d16SRodney W. Grimes 		printf("     Waiting for password\r\n");
2430ea022d16SRodney W. Grimes 	else
2431ea022d16SRodney W. Grimes 		printf("     Waiting for user name\r\n");
2432ea022d16SRodney W. Grimes 	printf("     TYPE: %s", typenames[type]);
2433ea022d16SRodney W. Grimes 	if (type == TYPE_A || type == TYPE_E)
2434ea022d16SRodney W. Grimes 		printf(", FORM: %s", formnames[form]);
2435ea022d16SRodney W. Grimes 	if (type == TYPE_L)
243689fdc4e1SMike Barcroft #if CHAR_BIT == 8
243789fdc4e1SMike Barcroft 		printf(" %d", CHAR_BIT);
2438ea022d16SRodney W. Grimes #else
2439ea022d16SRodney W. Grimes 		printf(" %d", bytesize);	/* need definition! */
2440ea022d16SRodney W. Grimes #endif
2441ea022d16SRodney W. Grimes 	printf("; STRUcture: %s; transfer MODE: %s\r\n",
2442ea022d16SRodney W. Grimes 	    strunames[stru], modenames[mode]);
2443ea022d16SRodney W. Grimes 	if (data != -1)
2444ea022d16SRodney W. Grimes 		printf("     Data connection open\r\n");
2445ea022d16SRodney W. Grimes 	else if (pdata != -1) {
24464dd8b5abSYoshinobu Inoue 		ispassive = 1;
24474dd8b5abSYoshinobu Inoue 		su = &pasv_addr;
2448ea022d16SRodney W. Grimes 		goto printaddr;
2449ea022d16SRodney W. Grimes 	} else if (usedefault == 0) {
24504dd8b5abSYoshinobu Inoue 		ispassive = 0;
24514dd8b5abSYoshinobu Inoue 		su = &data_dest;
2452ea022d16SRodney W. Grimes printaddr:
2453ea022d16SRodney W. Grimes #define UC(b) (((int) b) & 0xff)
24544dd8b5abSYoshinobu Inoue 		if (epsvall) {
24554dd8b5abSYoshinobu Inoue 			printf("     EPSV only mode (EPSV ALL)\r\n");
24564dd8b5abSYoshinobu Inoue 			goto epsvonly;
24574dd8b5abSYoshinobu Inoue 		}
24584dd8b5abSYoshinobu Inoue 
24594dd8b5abSYoshinobu Inoue 		/* PORT/PASV */
24604dd8b5abSYoshinobu Inoue 		if (su->su_family == AF_INET) {
24614dd8b5abSYoshinobu Inoue 			a = (u_char *) &su->su_sin.sin_addr;
24624dd8b5abSYoshinobu Inoue 			p = (u_char *) &su->su_sin.sin_port;
24634dd8b5abSYoshinobu Inoue 			printf("     %s (%d,%d,%d,%d,%d,%d)\r\n",
24644dd8b5abSYoshinobu Inoue 				ispassive ? "PASV" : "PORT",
24654dd8b5abSYoshinobu Inoue 				UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
24664dd8b5abSYoshinobu Inoue 				UC(p[0]), UC(p[1]));
24674dd8b5abSYoshinobu Inoue 		}
24684dd8b5abSYoshinobu Inoue 
24694dd8b5abSYoshinobu Inoue 		/* LPRT/LPSV */
24704dd8b5abSYoshinobu Inoue 	    {
24714dd8b5abSYoshinobu Inoue 		int alen, af, i;
24724dd8b5abSYoshinobu Inoue 
24734dd8b5abSYoshinobu Inoue 		switch (su->su_family) {
24744dd8b5abSYoshinobu Inoue 		case AF_INET:
24754dd8b5abSYoshinobu Inoue 			a = (u_char *) &su->su_sin.sin_addr;
24764dd8b5abSYoshinobu Inoue 			p = (u_char *) &su->su_sin.sin_port;
24774dd8b5abSYoshinobu Inoue 			alen = sizeof(su->su_sin.sin_addr);
24784dd8b5abSYoshinobu Inoue 			af = 4;
24794dd8b5abSYoshinobu Inoue 			break;
24804dd8b5abSYoshinobu Inoue 		case AF_INET6:
24814dd8b5abSYoshinobu Inoue 			a = (u_char *) &su->su_sin6.sin6_addr;
24824dd8b5abSYoshinobu Inoue 			p = (u_char *) &su->su_sin6.sin6_port;
24834dd8b5abSYoshinobu Inoue 			alen = sizeof(su->su_sin6.sin6_addr);
24844dd8b5abSYoshinobu Inoue 			af = 6;
24854dd8b5abSYoshinobu Inoue 			break;
24864dd8b5abSYoshinobu Inoue 		default:
24874dd8b5abSYoshinobu Inoue 			af = 0;
24884dd8b5abSYoshinobu Inoue 			break;
24894dd8b5abSYoshinobu Inoue 		}
24904dd8b5abSYoshinobu Inoue 		if (af) {
24914dd8b5abSYoshinobu Inoue 			printf("     %s (%d,%d,", ispassive ? "LPSV" : "LPRT",
24924dd8b5abSYoshinobu Inoue 				af, alen);
24934dd8b5abSYoshinobu Inoue 			for (i = 0; i < alen; i++)
24944dd8b5abSYoshinobu Inoue 				printf("%d,", UC(a[i]));
24954dd8b5abSYoshinobu Inoue 			printf("%d,%d,%d)\r\n", 2, UC(p[0]), UC(p[1]));
24964dd8b5abSYoshinobu Inoue 		}
24974dd8b5abSYoshinobu Inoue 	    }
24984dd8b5abSYoshinobu Inoue 
24994dd8b5abSYoshinobu Inoue epsvonly:;
25004dd8b5abSYoshinobu Inoue 		/* EPRT/EPSV */
25014dd8b5abSYoshinobu Inoue 	    {
25024dd8b5abSYoshinobu Inoue 		int af;
25034dd8b5abSYoshinobu Inoue 
25044dd8b5abSYoshinobu Inoue 		switch (su->su_family) {
25054dd8b5abSYoshinobu Inoue 		case AF_INET:
25064dd8b5abSYoshinobu Inoue 			af = 1;
25074dd8b5abSYoshinobu Inoue 			break;
25084dd8b5abSYoshinobu Inoue 		case AF_INET6:
25094dd8b5abSYoshinobu Inoue 			af = 2;
25104dd8b5abSYoshinobu Inoue 			break;
25114dd8b5abSYoshinobu Inoue 		default:
25124dd8b5abSYoshinobu Inoue 			af = 0;
25134dd8b5abSYoshinobu Inoue 			break;
25144dd8b5abSYoshinobu Inoue 		}
25154dd8b5abSYoshinobu Inoue 		if (af) {
2516b0f06defSHajimu UMEMOTO 			union sockunion tmp;
2517b0f06defSHajimu UMEMOTO 
2518b0f06defSHajimu UMEMOTO 			tmp = *su;
2519b0f06defSHajimu UMEMOTO 			if (tmp.su_family == AF_INET6)
2520b0f06defSHajimu UMEMOTO 				tmp.su_sin6.sin6_scope_id = 0;
2521b0f06defSHajimu UMEMOTO 			if (!getnameinfo((struct sockaddr *)&tmp, tmp.su_len,
25224dd8b5abSYoshinobu Inoue 					hname, sizeof(hname) - 1, NULL, 0,
25234dd8b5abSYoshinobu Inoue 					NI_NUMERICHOST)) {
252404683b2cSYaroslav Tykhiy 				hname[sizeof(hname) - 1] = 0;
25254dd8b5abSYoshinobu Inoue 				printf("     %s |%d|%s|%d|\r\n",
25264dd8b5abSYoshinobu Inoue 					ispassive ? "EPSV" : "EPRT",
2527b0f06defSHajimu UMEMOTO 					af, hname, htons(tmp.su_port));
25284dd8b5abSYoshinobu Inoue 			}
25294dd8b5abSYoshinobu Inoue 		}
25304dd8b5abSYoshinobu Inoue 	    }
2531ea022d16SRodney W. Grimes #undef UC
2532ea022d16SRodney W. Grimes 	} else
2533ea022d16SRodney W. Grimes 		printf("     No data connection\r\n");
253402c97492SYaroslav Tykhiy 	reply(211, "End of status.");
2535ea022d16SRodney W. Grimes }
2536ea022d16SRodney W. Grimes 
2537ea022d16SRodney W. Grimes void
2538e4bc453cSWarner Losh fatalerror(char *s)
2539ea022d16SRodney W. Grimes {
2540ea022d16SRodney W. Grimes 
25414a3e5acdSYaroslav Tykhiy 	reply(451, "Error in server: %s", s);
2542ea022d16SRodney W. Grimes 	reply(221, "Closing connection due to server error.");
2543ea022d16SRodney W. Grimes 	dologout(0);
2544ea022d16SRodney W. Grimes 	/* NOTREACHED */
2545ea022d16SRodney W. Grimes }
2546ea022d16SRodney W. Grimes 
2547ea022d16SRodney W. Grimes void
2548ea022d16SRodney W. Grimes reply(int n, const char *fmt, ...)
2549ea022d16SRodney W. Grimes {
2550ea022d16SRodney W. Grimes 	va_list ap;
2551e4bc453cSWarner Losh 
2552ea022d16SRodney W. Grimes 	(void)printf("%d ", n);
25539cbb335cSTim J. Robbins 	va_start(ap, fmt);
2554ea022d16SRodney W. Grimes 	(void)vprintf(fmt, ap);
25559cbb335cSTim J. Robbins 	va_end(ap);
2556ea022d16SRodney W. Grimes 	(void)printf("\r\n");
2557ea022d16SRodney W. Grimes 	(void)fflush(stdout);
2558618b0bbaSMark Murray 	if (ftpdebug) {
2559ea022d16SRodney W. Grimes 		syslog(LOG_DEBUG, "<--- %d ", n);
25609cbb335cSTim J. Robbins 		va_start(ap, fmt);
2561ea022d16SRodney W. Grimes 		vsyslog(LOG_DEBUG, fmt, ap);
25629cbb335cSTim J. Robbins 		va_end(ap);
2563ea022d16SRodney W. Grimes 	}
2564ea022d16SRodney W. Grimes }
2565ea022d16SRodney W. Grimes 
2566ea022d16SRodney W. Grimes void
2567ea022d16SRodney W. Grimes lreply(int n, const char *fmt, ...)
2568ea022d16SRodney W. Grimes {
2569ea022d16SRodney W. Grimes 	va_list ap;
2570e4bc453cSWarner Losh 
2571ea022d16SRodney W. Grimes 	(void)printf("%d- ", n);
25729cbb335cSTim J. Robbins 	va_start(ap, fmt);
2573ea022d16SRodney W. Grimes 	(void)vprintf(fmt, ap);
25749cbb335cSTim J. Robbins 	va_end(ap);
2575ea022d16SRodney W. Grimes 	(void)printf("\r\n");
2576ea022d16SRodney W. Grimes 	(void)fflush(stdout);
2577618b0bbaSMark Murray 	if (ftpdebug) {
2578ea022d16SRodney W. Grimes 		syslog(LOG_DEBUG, "<--- %d- ", n);
25799cbb335cSTim J. Robbins 		va_start(ap, fmt);
2580ea022d16SRodney W. Grimes 		vsyslog(LOG_DEBUG, fmt, ap);
25819cbb335cSTim J. Robbins 		va_end(ap);
2582ea022d16SRodney W. Grimes 	}
2583ea022d16SRodney W. Grimes }
2584ea022d16SRodney W. Grimes 
2585ea022d16SRodney W. Grimes static void
2586e4bc453cSWarner Losh ack(char *s)
2587ea022d16SRodney W. Grimes {
2588ea022d16SRodney W. Grimes 
2589ea022d16SRodney W. Grimes 	reply(250, "%s command successful.", s);
2590ea022d16SRodney W. Grimes }
2591ea022d16SRodney W. Grimes 
2592ea022d16SRodney W. Grimes void
2593e4bc453cSWarner Losh nack(char *s)
2594ea022d16SRodney W. Grimes {
2595ea022d16SRodney W. Grimes 
2596ea022d16SRodney W. Grimes 	reply(502, "%s command not implemented.", s);
2597ea022d16SRodney W. Grimes }
2598ea022d16SRodney W. Grimes 
2599ea022d16SRodney W. Grimes /* ARGSUSED */
2600ea022d16SRodney W. Grimes void
2601e4bc453cSWarner Losh yyerror(char *s)
2602ea022d16SRodney W. Grimes {
2603ea022d16SRodney W. Grimes 	char *cp;
2604ea022d16SRodney W. Grimes 
26059aca17cbSMark Murray 	if ((cp = strchr(cbuf,'\n')))
2606ea022d16SRodney W. Grimes 		*cp = '\0';
260702c97492SYaroslav Tykhiy 	reply(500, "%s: command not understood.", cbuf);
2608ea022d16SRodney W. Grimes }
2609ea022d16SRodney W. Grimes 
2610ea022d16SRodney W. Grimes void
2611e4bc453cSWarner Losh delete(char *name)
2612ea022d16SRodney W. Grimes {
2613ea022d16SRodney W. Grimes 	struct stat st;
2614ea022d16SRodney W. Grimes 
2615ea022d16SRodney W. Grimes 	LOGCMD("delete", name);
26161b0e12d7SYaroslav Tykhiy 	if (lstat(name, &st) < 0) {
2617ea022d16SRodney W. Grimes 		perror_reply(550, name);
2618ea022d16SRodney W. Grimes 		return;
2619ea022d16SRodney W. Grimes 	}
2620ac4f2391SYaroslav Tykhiy 	if (S_ISDIR(st.st_mode)) {
2621ea022d16SRodney W. Grimes 		if (rmdir(name) < 0) {
2622ea022d16SRodney W. Grimes 			perror_reply(550, name);
2623ea022d16SRodney W. Grimes 			return;
2624ea022d16SRodney W. Grimes 		}
2625ea022d16SRodney W. Grimes 		goto done;
2626ea022d16SRodney W. Grimes 	}
26273f8b9cfeSYaroslav Tykhiy 	if (guest && noguestmod) {
262802c97492SYaroslav Tykhiy 		reply(550, "Operation not permitted.");
26293f8b9cfeSYaroslav Tykhiy 		return;
26303f8b9cfeSYaroslav Tykhiy 	}
26313f8b9cfeSYaroslav Tykhiy 	if (unlink(name) < 0) {
2632ea022d16SRodney W. Grimes 		perror_reply(550, name);
2633ea022d16SRodney W. Grimes 		return;
2634ea022d16SRodney W. Grimes 	}
2635ea022d16SRodney W. Grimes done:
2636ea022d16SRodney W. Grimes 	ack("DELE");
2637ea022d16SRodney W. Grimes }
2638ea022d16SRodney W. Grimes 
2639ea022d16SRodney W. Grimes void
2640e4bc453cSWarner Losh cwd(char *path)
2641ea022d16SRodney W. Grimes {
2642ea022d16SRodney W. Grimes 
2643ea022d16SRodney W. Grimes 	if (chdir(path) < 0)
2644ea022d16SRodney W. Grimes 		perror_reply(550, path);
2645ea022d16SRodney W. Grimes 	else
2646ea022d16SRodney W. Grimes 		ack("CWD");
2647ea022d16SRodney W. Grimes }
2648ea022d16SRodney W. Grimes 
2649ea022d16SRodney W. Grimes void
2650e4bc453cSWarner Losh makedir(char *name)
2651ea022d16SRodney W. Grimes {
26522b748987SYaroslav Tykhiy 	char *s;
2653ea022d16SRodney W. Grimes 
2654ea022d16SRodney W. Grimes 	LOGCMD("mkdir", name);
2655d186bb12SMatthew N. Dodd 	if (guest && noguestmkd)
2656405e2987SYaroslav Tykhiy 		reply(550, "Operation not permitted.");
2657d186bb12SMatthew N. Dodd 	else if (mkdir(name, 0777) < 0)
2658ea022d16SRodney W. Grimes 		perror_reply(550, name);
26592b748987SYaroslav Tykhiy 	else {
26602b748987SYaroslav Tykhiy 		if ((s = doublequote(name)) == NULL)
26612b748987SYaroslav Tykhiy 			fatalerror("Ran out of memory.");
26622b748987SYaroslav Tykhiy 		reply(257, "\"%s\" directory created.", s);
26632b748987SYaroslav Tykhiy 		free(s);
26642b748987SYaroslav Tykhiy 	}
2665ea022d16SRodney W. Grimes }
2666ea022d16SRodney W. Grimes 
2667ea022d16SRodney W. Grimes void
2668e4bc453cSWarner Losh removedir(char *name)
2669ea022d16SRodney W. Grimes {
2670ea022d16SRodney W. Grimes 
2671ea022d16SRodney W. Grimes 	LOGCMD("rmdir", name);
2672ea022d16SRodney W. Grimes 	if (rmdir(name) < 0)
2673ea022d16SRodney W. Grimes 		perror_reply(550, name);
2674ea022d16SRodney W. Grimes 	else
2675ea022d16SRodney W. Grimes 		ack("RMD");
2676ea022d16SRodney W. Grimes }
2677ea022d16SRodney W. Grimes 
2678ea022d16SRodney W. Grimes void
2679e4bc453cSWarner Losh pwd(void)
2680ea022d16SRodney W. Grimes {
2681e4648f05SYaroslav Tykhiy 	char *s, path[MAXPATHLEN + 1];
2682ea022d16SRodney W. Grimes 
2683de9b6c03SYaroslav Tykhiy 	if (getcwd(path, sizeof(path)) == NULL)
268475933089SYaroslav Tykhiy 		perror_reply(550, "Get current directory");
2685e4648f05SYaroslav Tykhiy 	else {
2686e4648f05SYaroslav Tykhiy 		if ((s = doublequote(path)) == NULL)
2687e4648f05SYaroslav Tykhiy 			fatalerror("Ran out of memory.");
2688e4648f05SYaroslav Tykhiy 		reply(257, "\"%s\" is current directory.", s);
2689e4648f05SYaroslav Tykhiy 		free(s);
2690e4648f05SYaroslav Tykhiy 	}
2691ea022d16SRodney W. Grimes }
2692ea022d16SRodney W. Grimes 
2693ea022d16SRodney W. Grimes char *
2694e4bc453cSWarner Losh renamefrom(char *name)
2695ea022d16SRodney W. Grimes {
2696ea022d16SRodney W. Grimes 	struct stat st;
2697ea022d16SRodney W. Grimes 
2698b943b3c4SYaroslav Tykhiy 	if (guest && noguestmod) {
269902c97492SYaroslav Tykhiy 		reply(550, "Operation not permitted.");
2700b943b3c4SYaroslav Tykhiy 		return (NULL);
2701b943b3c4SYaroslav Tykhiy 	}
27021b0e12d7SYaroslav Tykhiy 	if (lstat(name, &st) < 0) {
2703ea022d16SRodney W. Grimes 		perror_reply(550, name);
2704385f9bf0SYaroslav Tykhiy 		return (NULL);
2705ea022d16SRodney W. Grimes 	}
270602c97492SYaroslav Tykhiy 	reply(350, "File exists, ready for destination name.");
2707ea022d16SRodney W. Grimes 	return (name);
2708ea022d16SRodney W. Grimes }
2709ea022d16SRodney W. Grimes 
2710ea022d16SRodney W. Grimes void
2711e4bc453cSWarner Losh renamecmd(char *from, char *to)
2712ea022d16SRodney W. Grimes {
271361f891a6SPaul Traina 	struct stat st;
2714ea022d16SRodney W. Grimes 
2715ea022d16SRodney W. Grimes 	LOGCMD2("rename", from, to);
271661f891a6SPaul Traina 
271761f891a6SPaul Traina 	if (guest && (stat(to, &st) == 0)) {
271802c97492SYaroslav Tykhiy 		reply(550, "%s: permission denied.", to);
271961f891a6SPaul Traina 		return;
272061f891a6SPaul Traina 	}
272161f891a6SPaul Traina 
2722ea022d16SRodney W. Grimes 	if (rename(from, to) < 0)
2723ea022d16SRodney W. Grimes 		perror_reply(550, "rename");
2724ea022d16SRodney W. Grimes 	else
2725ea022d16SRodney W. Grimes 		ack("RNTO");
2726ea022d16SRodney W. Grimes }
2727ea022d16SRodney W. Grimes 
2728ea022d16SRodney W. Grimes static void
2729e4bc453cSWarner Losh dolog(struct sockaddr *who)
2730ea022d16SRodney W. Grimes {
27317cdd3cb7SYaroslav Tykhiy 	char who_name[NI_MAXHOST];
27324dd8b5abSYoshinobu Inoue 
27334dd8b5abSYoshinobu Inoue 	realhostname_sa(remotehost, sizeof(remotehost) - 1, who, who->sa_len);
273404683b2cSYaroslav Tykhiy 	remotehost[sizeof(remotehost) - 1] = 0;
27357cdd3cb7SYaroslav Tykhiy 	if (getnameinfo(who, who->sa_len,
27367cdd3cb7SYaroslav Tykhiy 		who_name, sizeof(who_name) - 1, NULL, 0, NI_NUMERICHOST))
27377cdd3cb7SYaroslav Tykhiy 			*who_name = 0;
27387cdd3cb7SYaroslav Tykhiy 	who_name[sizeof(who_name) - 1] = 0;
2739ea022d16SRodney W. Grimes 
2740ea022d16SRodney W. Grimes #ifdef SETPROCTITLE
2741ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
2742ea4e54b9SDavid Nugent 	if (thishost != firsthost)
2743ea4e54b9SDavid Nugent 		snprintf(proctitle, sizeof(proctitle), "%s: connected (to %s)",
2744ea4e54b9SDavid Nugent 			 remotehost, hostname);
2745ea4e54b9SDavid Nugent 	else
2746ea4e54b9SDavid Nugent #endif
2747ea4e54b9SDavid Nugent 		snprintf(proctitle, sizeof(proctitle), "%s: connected",
2748ea4e54b9SDavid Nugent 			 remotehost);
2749b63e1fe2SPeter Wemm 	setproctitle("%s", proctitle);
2750ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */
2751ea022d16SRodney W. Grimes 
2752ea4e54b9SDavid Nugent 	if (logging) {
2753ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
2754ea4e54b9SDavid Nugent 		if (thishost != firsthost)
27557cdd3cb7SYaroslav Tykhiy 			syslog(LOG_INFO, "connection from %s (%s) to %s",
27567cdd3cb7SYaroslav Tykhiy 			       remotehost, who_name, hostname);
2757ea4e54b9SDavid Nugent 		else
2758ea4e54b9SDavid Nugent #endif
27597cdd3cb7SYaroslav Tykhiy 			syslog(LOG_INFO, "connection from %s (%s)",
27607cdd3cb7SYaroslav Tykhiy 			       remotehost, who_name);
2761ea022d16SRodney W. Grimes 	}
2762ea4e54b9SDavid Nugent }
2763ea022d16SRodney W. Grimes 
2764ea022d16SRodney W. Grimes /*
2765ea022d16SRodney W. Grimes  * Record logout in wtmp file
2766ea022d16SRodney W. Grimes  * and exit with supplied status.
2767ea022d16SRodney W. Grimes  */
2768ea022d16SRodney W. Grimes void
2769e4bc453cSWarner Losh dologout(int status)
2770ea022d16SRodney W. Grimes {
2771ea022d16SRodney W. Grimes 
27729f37b1a2SEd Schouten 	if (logged_in && dowtmp) {
277352e7ee74SYaroslav Tykhiy 		(void) seteuid(0);
27749f37b1a2SEd Schouten 		ftpd_logwtmp(wtmpid, NULL, NULL);
2775ea022d16SRodney W. Grimes 	}
2776ea022d16SRodney W. Grimes 	/* beware of flushing buffers after a SIGPIPE */
2777ea022d16SRodney W. Grimes 	_exit(status);
2778ea022d16SRodney W. Grimes }
2779ea022d16SRodney W. Grimes 
2780ea022d16SRodney W. Grimes static void
2781e4bc453cSWarner Losh sigurg(int signo)
2782ea022d16SRodney W. Grimes {
27834b82fc95SYaroslav Tykhiy 
27844b82fc95SYaroslav Tykhiy 	recvurg = 1;
27854b82fc95SYaroslav Tykhiy }
27864b82fc95SYaroslav Tykhiy 
27874b82fc95SYaroslav Tykhiy static void
27884cd51076SYaroslav Tykhiy maskurg(int flag)
27894cd51076SYaroslav Tykhiy {
27904cd51076SYaroslav Tykhiy 	int oerrno;
27914cd51076SYaroslav Tykhiy 	sigset_t sset;
27924cd51076SYaroslav Tykhiy 
27934cd51076SYaroslav Tykhiy 	if (!transflag) {
27944cd51076SYaroslav Tykhiy 		syslog(LOG_ERR, "Internal: maskurg() while no transfer");
27954cd51076SYaroslav Tykhiy 		return;
27964cd51076SYaroslav Tykhiy 	}
27974cd51076SYaroslav Tykhiy 	oerrno = errno;
27984cd51076SYaroslav Tykhiy 	sigemptyset(&sset);
27994cd51076SYaroslav Tykhiy 	sigaddset(&sset, SIGURG);
28004cd51076SYaroslav Tykhiy 	sigprocmask(flag ? SIG_BLOCK : SIG_UNBLOCK, &sset, NULL);
28014cd51076SYaroslav Tykhiy 	errno = oerrno;
28024cd51076SYaroslav Tykhiy }
28034cd51076SYaroslav Tykhiy 
28044cd51076SYaroslav Tykhiy static void
28054cd51076SYaroslav Tykhiy flagxfer(int flag)
28064cd51076SYaroslav Tykhiy {
28074cd51076SYaroslav Tykhiy 
28084cd51076SYaroslav Tykhiy 	if (flag) {
2809f9036ce6SYaroslav Tykhiy 		if (transflag)
2810f9036ce6SYaroslav Tykhiy 			syslog(LOG_ERR, "Internal: flagxfer(1): "
2811f9036ce6SYaroslav Tykhiy 					"transfer already under way");
28124cd51076SYaroslav Tykhiy 		transflag = 1;
281391ae7779SYaroslav Tykhiy 		maskurg(0);
281491ae7779SYaroslav Tykhiy 		recvurg = 0;
281591ae7779SYaroslav Tykhiy 	} else {
2816f9036ce6SYaroslav Tykhiy 		if (!transflag)
2817f9036ce6SYaroslav Tykhiy 			syslog(LOG_ERR, "Internal: flagxfer(0): "
2818f9036ce6SYaroslav Tykhiy 					"no active transfer");
281991ae7779SYaroslav Tykhiy 		maskurg(1);
28204cd51076SYaroslav Tykhiy 		transflag = 0;
28214cd51076SYaroslav Tykhiy 	}
282291ae7779SYaroslav Tykhiy }
28234cd51076SYaroslav Tykhiy 
28244cd51076SYaroslav Tykhiy /*
28254cd51076SYaroslav Tykhiy  * Returns 0 if OK to resume or -1 if abort requested.
28264cd51076SYaroslav Tykhiy  */
28274cd51076SYaroslav Tykhiy static int
2828e4bc453cSWarner Losh myoob(void)
28294b82fc95SYaroslav Tykhiy {
2830ea022d16SRodney W. Grimes 	char *cp;
2831f0b40b1cSColin Percival 	int ret;
2832ea022d16SRodney W. Grimes 
28334cd51076SYaroslav Tykhiy 	if (!transflag) {
28344cd51076SYaroslav Tykhiy 		syslog(LOG_ERR, "Internal: myoob() while no transfer");
28354cd51076SYaroslav Tykhiy 		return (0);
28364cd51076SYaroslav Tykhiy 	}
2837ea022d16SRodney W. Grimes 	cp = tmpline;
2838f03ef840SBaptiste Daroussin 	ret = get_line(cp, 7, stdin);
2839f0b40b1cSColin Percival 	if (ret == -1) {
2840ea022d16SRodney W. Grimes 		reply(221, "You could at least say goodbye.");
2841ea022d16SRodney W. Grimes 		dologout(0);
2842f0b40b1cSColin Percival 	} else if (ret == -2) {
2843f0b40b1cSColin Percival 		/* Ignore truncated command. */
2844f0b40b1cSColin Percival 		return (0);
2845ea022d16SRodney W. Grimes 	}
2846ea022d16SRodney W. Grimes 	upper(cp);
2847ea022d16SRodney W. Grimes 	if (strcmp(cp, "ABOR\r\n") == 0) {
2848ea022d16SRodney W. Grimes 		tmpline[0] = '\0';
2849ea022d16SRodney W. Grimes 		reply(426, "Transfer aborted. Data connection closed.");
285002c97492SYaroslav Tykhiy 		reply(226, "Abort successful.");
28514cd51076SYaroslav Tykhiy 		return (-1);
2852ea022d16SRodney W. Grimes 	}
2853ea022d16SRodney W. Grimes 	if (strcmp(cp, "STAT\r\n") == 0) {
28549db4bbf3SMichael Haro 		tmpline[0] = '\0';
28550e519c96SYaroslav Tykhiy 		if (file_size != -1)
285602c97492SYaroslav Tykhiy 			reply(213, "Status: %jd of %jd bytes transferred.",
2857a57e1ef0SYaroslav Tykhiy 				   (intmax_t)byte_count, (intmax_t)file_size);
2858ea022d16SRodney W. Grimes 		else
285902c97492SYaroslav Tykhiy 			reply(213, "Status: %jd bytes transferred.",
2860a57e1ef0SYaroslav Tykhiy 				   (intmax_t)byte_count);
2861ea022d16SRodney W. Grimes 	}
28624cd51076SYaroslav Tykhiy 	return (0);
2863ea022d16SRodney W. Grimes }
2864ea022d16SRodney W. Grimes 
2865ea022d16SRodney W. Grimes /*
2866ea022d16SRodney W. Grimes  * Note: a response of 425 is not mentioned as a possible response to
2867ea022d16SRodney W. Grimes  *	the PASV command in RFC959. However, it has been blessed as
2868ea022d16SRodney W. Grimes  *	a legitimate response by Jon Postel in a telephone conversation
2869ea022d16SRodney W. Grimes  *	with Rick Adams on 25 Jan 89.
2870ea022d16SRodney W. Grimes  */
2871ea022d16SRodney W. Grimes void
2872e4bc453cSWarner Losh passive(void)
2873ea022d16SRodney W. Grimes {
287478e3eed0SStefan Farfeleder 	socklen_t len;
287578e3eed0SStefan Farfeleder 	int on;
2876ea022d16SRodney W. Grimes 	char *p, *a;
2877ea022d16SRodney W. Grimes 
287861f891a6SPaul Traina 	if (pdata >= 0)		/* close old port if one set */
287961f891a6SPaul Traina 		close(pdata);
288061f891a6SPaul Traina 
28814dd8b5abSYoshinobu Inoue 	pdata = socket(ctrl_addr.su_family, SOCK_STREAM, 0);
2882ea022d16SRodney W. Grimes 	if (pdata < 0) {
2883ea022d16SRodney W. Grimes 		perror_reply(425, "Can't open passive connection");
2884ea022d16SRodney W. Grimes 		return;
2885ea022d16SRodney W. Grimes 	}
28868af7c9a3SYaroslav Tykhiy 	on = 1;
28878af7c9a3SYaroslav Tykhiy 	if (setsockopt(pdata, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
28888af7c9a3SYaroslav Tykhiy 		syslog(LOG_WARNING, "pdata setsockopt (SO_REUSEADDR): %m");
28894c450ad7SPaul Traina 
289052e7ee74SYaroslav Tykhiy 	(void) seteuid(0);
289161f891a6SPaul Traina 
2892dacc9752SPaul Traina #ifdef IP_PORTRANGE
28934dd8b5abSYoshinobu Inoue 	if (ctrl_addr.su_family == AF_INET) {
28948af7c9a3SYaroslav Tykhiy 	    on = restricted_data_ports ? IP_PORTRANGE_HIGH
2895dacc9752SPaul Traina 				       : IP_PORTRANGE_DEFAULT;
2896dacc9752SPaul Traina 
289740e9d39eSPeter Wemm 	    if (setsockopt(pdata, IPPROTO_IP, IP_PORTRANGE,
289857d4ef07SYaroslav Tykhiy 			    &on, sizeof(on)) < 0)
28994c450ad7SPaul Traina 		    goto pasv_error;
29004c450ad7SPaul Traina 	}
2901dacc9752SPaul Traina #endif
29022db39860SNick Sayer #ifdef IPV6_PORTRANGE
29032db39860SNick Sayer 	if (ctrl_addr.su_family == AF_INET6) {
29048af7c9a3SYaroslav Tykhiy 	    on = restricted_data_ports ? IPV6_PORTRANGE_HIGH
29052db39860SNick Sayer 				       : IPV6_PORTRANGE_DEFAULT;
29062db39860SNick Sayer 
29072db39860SNick Sayer 	    if (setsockopt(pdata, IPPROTO_IPV6, IPV6_PORTRANGE,
290857d4ef07SYaroslav Tykhiy 			    &on, sizeof(on)) < 0)
29092db39860SNick Sayer 		    goto pasv_error;
29102db39860SNick Sayer 	}
29112db39860SNick Sayer #endif
291240e9d39eSPeter Wemm 
2913ea022d16SRodney W. Grimes 	pasv_addr = ctrl_addr;
29144dd8b5abSYoshinobu Inoue 	pasv_addr.su_port = 0;
29154dd8b5abSYoshinobu Inoue 	if (bind(pdata, (struct sockaddr *)&pasv_addr, pasv_addr.su_len) < 0)
2916ea022d16SRodney W. Grimes 		goto pasv_error;
291761f891a6SPaul Traina 
291852e7ee74SYaroslav Tykhiy 	(void) seteuid(pw->pw_uid);
29194c450ad7SPaul Traina 
2920ea022d16SRodney W. Grimes 	len = sizeof(pasv_addr);
2921ea022d16SRodney W. Grimes 	if (getsockname(pdata, (struct sockaddr *) &pasv_addr, &len) < 0)
2922ea022d16SRodney W. Grimes 		goto pasv_error;
2923ea022d16SRodney W. Grimes 	if (listen(pdata, 1) < 0)
2924ea022d16SRodney W. Grimes 		goto pasv_error;
29254dd8b5abSYoshinobu Inoue 	if (pasv_addr.su_family == AF_INET)
29264dd8b5abSYoshinobu Inoue 		a = (char *) &pasv_addr.su_sin.sin_addr;
29274dd8b5abSYoshinobu Inoue 	else if (pasv_addr.su_family == AF_INET6 &&
29284dd8b5abSYoshinobu Inoue 		 IN6_IS_ADDR_V4MAPPED(&pasv_addr.su_sin6.sin6_addr))
29294dd8b5abSYoshinobu Inoue 		a = (char *) &pasv_addr.su_sin6.sin6_addr.s6_addr[12];
29304dd8b5abSYoshinobu Inoue 	else
29314dd8b5abSYoshinobu Inoue 		goto pasv_error;
29324dd8b5abSYoshinobu Inoue 
29334dd8b5abSYoshinobu Inoue 	p = (char *) &pasv_addr.su_port;
2934ea022d16SRodney W. Grimes 
2935ea022d16SRodney W. Grimes #define UC(b) (((int) b) & 0xff)
2936ea022d16SRodney W. Grimes 
2937ea022d16SRodney W. Grimes 	reply(227, "Entering Passive Mode (%d,%d,%d,%d,%d,%d)", UC(a[0]),
2938ea022d16SRodney W. Grimes 		UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1]));
2939ea022d16SRodney W. Grimes 	return;
2940ea022d16SRodney W. Grimes 
2941ea022d16SRodney W. Grimes pasv_error:
294252e7ee74SYaroslav Tykhiy 	(void) seteuid(pw->pw_uid);
2943ea022d16SRodney W. Grimes 	(void) close(pdata);
2944ea022d16SRodney W. Grimes 	pdata = -1;
2945ea022d16SRodney W. Grimes 	perror_reply(425, "Can't open passive connection");
2946ea022d16SRodney W. Grimes 	return;
2947ea022d16SRodney W. Grimes }
2948ea022d16SRodney W. Grimes 
2949ea022d16SRodney W. Grimes /*
29504dd8b5abSYoshinobu Inoue  * Long Passive defined in RFC 1639.
29514dd8b5abSYoshinobu Inoue  *     228 Entering Long Passive Mode
29524dd8b5abSYoshinobu Inoue  *         (af, hal, h1, h2, h3,..., pal, p1, p2...)
29534dd8b5abSYoshinobu Inoue  */
29544dd8b5abSYoshinobu Inoue 
29554dd8b5abSYoshinobu Inoue void
2956e4bc453cSWarner Losh long_passive(char *cmd, int pf)
29574dd8b5abSYoshinobu Inoue {
295878e3eed0SStefan Farfeleder 	socklen_t len;
295978e3eed0SStefan Farfeleder 	int on;
29604dd8b5abSYoshinobu Inoue 	char *p, *a;
29614dd8b5abSYoshinobu Inoue 
29624dd8b5abSYoshinobu Inoue 	if (pdata >= 0)		/* close old port if one set */
29634dd8b5abSYoshinobu Inoue 		close(pdata);
29644dd8b5abSYoshinobu Inoue 
29654dd8b5abSYoshinobu Inoue 	if (pf != PF_UNSPEC) {
29664dd8b5abSYoshinobu Inoue 		if (ctrl_addr.su_family != pf) {
29674dd8b5abSYoshinobu Inoue 			switch (ctrl_addr.su_family) {
29684dd8b5abSYoshinobu Inoue 			case AF_INET:
29694dd8b5abSYoshinobu Inoue 				pf = 1;
29704dd8b5abSYoshinobu Inoue 				break;
29714dd8b5abSYoshinobu Inoue 			case AF_INET6:
29724dd8b5abSYoshinobu Inoue 				pf = 2;
29734dd8b5abSYoshinobu Inoue 				break;
29744dd8b5abSYoshinobu Inoue 			default:
29754dd8b5abSYoshinobu Inoue 				pf = 0;
29764dd8b5abSYoshinobu Inoue 				break;
29774dd8b5abSYoshinobu Inoue 			}
29784dd8b5abSYoshinobu Inoue 			/*
29794dd8b5abSYoshinobu Inoue 			 * XXX
29804dd8b5abSYoshinobu Inoue 			 * only EPRT/EPSV ready clients will understand this
29814dd8b5abSYoshinobu Inoue 			 */
29824dd8b5abSYoshinobu Inoue 			if (strcmp(cmd, "EPSV") == 0 && pf) {
29834dd8b5abSYoshinobu Inoue 				reply(522, "Network protocol mismatch, "
29844dd8b5abSYoshinobu Inoue 					"use (%d)", pf);
29854dd8b5abSYoshinobu Inoue 			} else
298602c97492SYaroslav Tykhiy 				reply(501, "Network protocol mismatch."); /*XXX*/
29874dd8b5abSYoshinobu Inoue 
29884dd8b5abSYoshinobu Inoue 			return;
29894dd8b5abSYoshinobu Inoue 		}
29904dd8b5abSYoshinobu Inoue 	}
29914dd8b5abSYoshinobu Inoue 
29924dd8b5abSYoshinobu Inoue 	pdata = socket(ctrl_addr.su_family, SOCK_STREAM, 0);
29934dd8b5abSYoshinobu Inoue 	if (pdata < 0) {
29944dd8b5abSYoshinobu Inoue 		perror_reply(425, "Can't open passive connection");
29954dd8b5abSYoshinobu Inoue 		return;
29964dd8b5abSYoshinobu Inoue 	}
29978af7c9a3SYaroslav Tykhiy 	on = 1;
29988af7c9a3SYaroslav Tykhiy 	if (setsockopt(pdata, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
29998af7c9a3SYaroslav Tykhiy 		syslog(LOG_WARNING, "pdata setsockopt (SO_REUSEADDR): %m");
30004dd8b5abSYoshinobu Inoue 
300152e7ee74SYaroslav Tykhiy 	(void) seteuid(0);
30024dd8b5abSYoshinobu Inoue 
30034dd8b5abSYoshinobu Inoue 	pasv_addr = ctrl_addr;
30044dd8b5abSYoshinobu Inoue 	pasv_addr.su_port = 0;
30054dd8b5abSYoshinobu Inoue 	len = pasv_addr.su_len;
30064dd8b5abSYoshinobu Inoue 
30072db39860SNick Sayer #ifdef IP_PORTRANGE
30082db39860SNick Sayer 	if (ctrl_addr.su_family == AF_INET) {
30098af7c9a3SYaroslav Tykhiy 	    on = restricted_data_ports ? IP_PORTRANGE_HIGH
30102db39860SNick Sayer 				       : IP_PORTRANGE_DEFAULT;
30112db39860SNick Sayer 
30122db39860SNick Sayer 	    if (setsockopt(pdata, IPPROTO_IP, IP_PORTRANGE,
301357d4ef07SYaroslav Tykhiy 			    &on, sizeof(on)) < 0)
30142db39860SNick Sayer 		    goto pasv_error;
30152db39860SNick Sayer 	}
30162db39860SNick Sayer #endif
30172db39860SNick Sayer #ifdef IPV6_PORTRANGE
30182db39860SNick Sayer 	if (ctrl_addr.su_family == AF_INET6) {
30198af7c9a3SYaroslav Tykhiy 	    on = restricted_data_ports ? IPV6_PORTRANGE_HIGH
30202db39860SNick Sayer 				       : IPV6_PORTRANGE_DEFAULT;
30212db39860SNick Sayer 
30222db39860SNick Sayer 	    if (setsockopt(pdata, IPPROTO_IPV6, IPV6_PORTRANGE,
302357d4ef07SYaroslav Tykhiy 			    &on, sizeof(on)) < 0)
30242db39860SNick Sayer 		    goto pasv_error;
30252db39860SNick Sayer 	}
30262db39860SNick Sayer #endif
30272db39860SNick Sayer 
30284dd8b5abSYoshinobu Inoue 	if (bind(pdata, (struct sockaddr *)&pasv_addr, len) < 0)
30294dd8b5abSYoshinobu Inoue 		goto pasv_error;
30304dd8b5abSYoshinobu Inoue 
303152e7ee74SYaroslav Tykhiy 	(void) seteuid(pw->pw_uid);
30324dd8b5abSYoshinobu Inoue 
30334dd8b5abSYoshinobu Inoue 	if (getsockname(pdata, (struct sockaddr *) &pasv_addr, &len) < 0)
30344dd8b5abSYoshinobu Inoue 		goto pasv_error;
30354dd8b5abSYoshinobu Inoue 	if (listen(pdata, 1) < 0)
30364dd8b5abSYoshinobu Inoue 		goto pasv_error;
30374dd8b5abSYoshinobu Inoue 
30384dd8b5abSYoshinobu Inoue #define UC(b) (((int) b) & 0xff)
30394dd8b5abSYoshinobu Inoue 
30404dd8b5abSYoshinobu Inoue 	if (strcmp(cmd, "LPSV") == 0) {
30414dd8b5abSYoshinobu Inoue 		p = (char *)&pasv_addr.su_port;
30424dd8b5abSYoshinobu Inoue 		switch (pasv_addr.su_family) {
30434dd8b5abSYoshinobu Inoue 		case AF_INET:
30444dd8b5abSYoshinobu Inoue 			a = (char *) &pasv_addr.su_sin.sin_addr;
30454dd8b5abSYoshinobu Inoue 		v4_reply:
30464dd8b5abSYoshinobu Inoue 			reply(228,
30474dd8b5abSYoshinobu Inoue "Entering Long Passive Mode (%d,%d,%d,%d,%d,%d,%d,%d,%d)",
30484dd8b5abSYoshinobu Inoue 			      4, 4, UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
30494dd8b5abSYoshinobu Inoue 			      2, UC(p[0]), UC(p[1]));
30504dd8b5abSYoshinobu Inoue 			return;
30514dd8b5abSYoshinobu Inoue 		case AF_INET6:
30524dd8b5abSYoshinobu Inoue 			if (IN6_IS_ADDR_V4MAPPED(&pasv_addr.su_sin6.sin6_addr)) {
30534dd8b5abSYoshinobu Inoue 				a = (char *) &pasv_addr.su_sin6.sin6_addr.s6_addr[12];
30544dd8b5abSYoshinobu Inoue 				goto v4_reply;
30554dd8b5abSYoshinobu Inoue 			}
30564dd8b5abSYoshinobu Inoue 			a = (char *) &pasv_addr.su_sin6.sin6_addr;
30574dd8b5abSYoshinobu Inoue 			reply(228,
30584dd8b5abSYoshinobu Inoue "Entering Long Passive Mode "
30594dd8b5abSYoshinobu Inoue "(%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d)",
30604dd8b5abSYoshinobu Inoue 			      6, 16, UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
30614dd8b5abSYoshinobu Inoue 			      UC(a[4]), UC(a[5]), UC(a[6]), UC(a[7]),
30624dd8b5abSYoshinobu Inoue 			      UC(a[8]), UC(a[9]), UC(a[10]), UC(a[11]),
30634dd8b5abSYoshinobu Inoue 			      UC(a[12]), UC(a[13]), UC(a[14]), UC(a[15]),
30644dd8b5abSYoshinobu Inoue 			      2, UC(p[0]), UC(p[1]));
30654dd8b5abSYoshinobu Inoue 			return;
30664dd8b5abSYoshinobu Inoue 		}
30674dd8b5abSYoshinobu Inoue 	} else if (strcmp(cmd, "EPSV") == 0) {
30684dd8b5abSYoshinobu Inoue 		switch (pasv_addr.su_family) {
30694dd8b5abSYoshinobu Inoue 		case AF_INET:
30704dd8b5abSYoshinobu Inoue 		case AF_INET6:
30714dd8b5abSYoshinobu Inoue 			reply(229, "Entering Extended Passive Mode (|||%d|)",
30724dd8b5abSYoshinobu Inoue 				ntohs(pasv_addr.su_port));
30734dd8b5abSYoshinobu Inoue 			return;
30744dd8b5abSYoshinobu Inoue 		}
30754dd8b5abSYoshinobu Inoue 	} else {
30764dd8b5abSYoshinobu Inoue 		/* more proper error code? */
30774dd8b5abSYoshinobu Inoue 	}
30784dd8b5abSYoshinobu Inoue 
30794dd8b5abSYoshinobu Inoue pasv_error:
308052e7ee74SYaroslav Tykhiy 	(void) seteuid(pw->pw_uid);
30814dd8b5abSYoshinobu Inoue 	(void) close(pdata);
30824dd8b5abSYoshinobu Inoue 	pdata = -1;
30834dd8b5abSYoshinobu Inoue 	perror_reply(425, "Can't open passive connection");
30844dd8b5abSYoshinobu Inoue 	return;
30854dd8b5abSYoshinobu Inoue }
30864dd8b5abSYoshinobu Inoue 
30874dd8b5abSYoshinobu Inoue /*
3088a117c345SYaroslav Tykhiy  * Generate unique name for file with basename "local"
3089a117c345SYaroslav Tykhiy  * and open the file in order to avoid possible races.
3090a117c345SYaroslav Tykhiy  * Try "local" first, then "local.1", "local.2" etc, up to "local.99".
3091a117c345SYaroslav Tykhiy  * Return descriptor to the file, set "name" to its name.
3092a117c345SYaroslav Tykhiy  *
3093ea022d16SRodney W. Grimes  * Generates failure reply on error.
3094ea022d16SRodney W. Grimes  */
3095a117c345SYaroslav Tykhiy static int
3096a117c345SYaroslav Tykhiy guniquefd(char *local, char **name)
3097ea022d16SRodney W. Grimes {
3098ea022d16SRodney W. Grimes 	static char new[MAXPATHLEN];
3099ea022d16SRodney W. Grimes 	struct stat st;
3100ea022d16SRodney W. Grimes 	char *cp;
3101a117c345SYaroslav Tykhiy 	int count;
3102a117c345SYaroslav Tykhiy 	int fd;
3103ea022d16SRodney W. Grimes 
3104ea022d16SRodney W. Grimes 	cp = strrchr(local, '/');
3105ea022d16SRodney W. Grimes 	if (cp)
3106ea022d16SRodney W. Grimes 		*cp = '\0';
3107ea022d16SRodney W. Grimes 	if (stat(cp ? local : ".", &st) < 0) {
3108ea022d16SRodney W. Grimes 		perror_reply(553, cp ? local : ".");
3109a117c345SYaroslav Tykhiy 		return (-1);
3110ea022d16SRodney W. Grimes 	}
3111a117c345SYaroslav Tykhiy 	if (cp) {
3112a117c345SYaroslav Tykhiy 		/*
3113a117c345SYaroslav Tykhiy 		 * Let not overwrite dirname with counter suffix.
3114a117c345SYaroslav Tykhiy 		 * -4 is for /nn\0
3115a117c345SYaroslav Tykhiy 		 * In this extreme case dot won't be put in front of suffix.
3116a117c345SYaroslav Tykhiy 		 */
3117a117c345SYaroslav Tykhiy 		if (strlen(local) > sizeof(new) - 4) {
311802c97492SYaroslav Tykhiy 			reply(553, "Pathname too long.");
3119a117c345SYaroslav Tykhiy 			return (-1);
3120a117c345SYaroslav Tykhiy 		}
3121ea022d16SRodney W. Grimes 		*cp = '/';
3122a117c345SYaroslav Tykhiy 	}
3123e760ef2cSWarner Losh 	/* -4 is for the .nn<null> we put on the end below */
3124e760ef2cSWarner Losh 	(void) snprintf(new, sizeof(new) - 4, "%s", local);
3125ea022d16SRodney W. Grimes 	cp = new + strlen(new);
3126a117c345SYaroslav Tykhiy 	/*
3127a117c345SYaroslav Tykhiy 	 * Don't generate dotfile unless requested explicitly.
3128a117c345SYaroslav Tykhiy 	 * This covers the case when basename gets truncated off
3129a117c345SYaroslav Tykhiy 	 * by buffer size.
3130a117c345SYaroslav Tykhiy 	 */
3131a117c345SYaroslav Tykhiy 	if (cp > new && cp[-1] != '/')
3132ea022d16SRodney W. Grimes 		*cp++ = '.';
3133a117c345SYaroslav Tykhiy 	for (count = 0; count < 100; count++) {
3134a117c345SYaroslav Tykhiy 		/* At count 0 try unmodified name */
3135a117c345SYaroslav Tykhiy 		if (count)
3136ea022d16SRodney W. Grimes 			(void)sprintf(cp, "%d", count);
3137a117c345SYaroslav Tykhiy 		if ((fd = open(count ? new : local,
3138a117c345SYaroslav Tykhiy 		    O_RDWR | O_CREAT | O_EXCL, 0666)) >= 0) {
3139a117c345SYaroslav Tykhiy 			*name = count ? new : local;
3140a117c345SYaroslav Tykhiy 			return (fd);
3141a117c345SYaroslav Tykhiy 		}
314288b70721SYaroslav Tykhiy 		if (errno != EEXIST) {
314350618d61SYaroslav Tykhiy 			perror_reply(553, count ? new : local);
314488b70721SYaroslav Tykhiy 			return (-1);
314588b70721SYaroslav Tykhiy 		}
3146ea022d16SRodney W. Grimes 	}
3147ea022d16SRodney W. Grimes 	reply(452, "Unique file name cannot be created.");
3148a117c345SYaroslav Tykhiy 	return (-1);
3149ea022d16SRodney W. Grimes }
3150ea022d16SRodney W. Grimes 
3151ea022d16SRodney W. Grimes /*
3152ea022d16SRodney W. Grimes  * Format and send reply containing system error number.
3153ea022d16SRodney W. Grimes  */
3154ea022d16SRodney W. Grimes void
3155e4bc453cSWarner Losh perror_reply(int code, char *string)
3156ea022d16SRodney W. Grimes {
3157ea022d16SRodney W. Grimes 
3158ea022d16SRodney W. Grimes 	reply(code, "%s: %s.", string, strerror(errno));
3159ea022d16SRodney W. Grimes }
3160ea022d16SRodney W. Grimes 
3161ea022d16SRodney W. Grimes static char *onefile[] = {
3162ea022d16SRodney W. Grimes 	"",
3163ea022d16SRodney W. Grimes 	0
3164ea022d16SRodney W. Grimes };
3165ea022d16SRodney W. Grimes 
3166ea022d16SRodney W. Grimes void
3167e4bc453cSWarner Losh send_file_list(char *whichf)
3168ea022d16SRodney W. Grimes {
3169ea022d16SRodney W. Grimes 	struct stat st;
3170ea022d16SRodney W. Grimes 	DIR *dirp = NULL;
3171ea022d16SRodney W. Grimes 	struct dirent *dir;
3172ea022d16SRodney W. Grimes 	FILE *dout = NULL;
3173ea022d16SRodney W. Grimes 	char **dirlist, *dirname;
3174ea022d16SRodney W. Grimes 	int simple = 0;
3175ea022d16SRodney W. Grimes 	int freeglob = 0;
3176ea022d16SRodney W. Grimes 	glob_t gl;
3177ea022d16SRodney W. Grimes 
3178ea022d16SRodney W. Grimes 	if (strpbrk(whichf, "~{[*?") != NULL) {
317912da320bSMike Heffner 		int flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_TILDE;
3180ea022d16SRodney W. Grimes 
3181ea022d16SRodney W. Grimes 		memset(&gl, 0, sizeof(gl));
31826d10cb2fSJonathan Lemon 		gl.gl_matchc = MAXGLOBARGS;
318375dc5f1aSMike Heffner 		flags |= GLOB_LIMIT;
3184ea022d16SRodney W. Grimes 		freeglob = 1;
3185ea022d16SRodney W. Grimes 		if (glob(whichf, flags, 0, &gl)) {
318602c97492SYaroslav Tykhiy 			reply(550, "No matching files found.");
3187ea022d16SRodney W. Grimes 			goto out;
3188ea022d16SRodney W. Grimes 		} else if (gl.gl_pathc == 0) {
3189ea022d16SRodney W. Grimes 			errno = ENOENT;
3190ea022d16SRodney W. Grimes 			perror_reply(550, whichf);
3191ea022d16SRodney W. Grimes 			goto out;
3192ea022d16SRodney W. Grimes 		}
3193ea022d16SRodney W. Grimes 		dirlist = gl.gl_pathv;
3194ea022d16SRodney W. Grimes 	} else {
3195ea022d16SRodney W. Grimes 		onefile[0] = whichf;
3196ea022d16SRodney W. Grimes 		dirlist = onefile;
3197ea022d16SRodney W. Grimes 		simple = 1;
3198ea022d16SRodney W. Grimes 	}
3199ea022d16SRodney W. Grimes 
32009aca17cbSMark Murray 	while ((dirname = *dirlist++)) {
3201ea022d16SRodney W. Grimes 		if (stat(dirname, &st) < 0) {
3202ea022d16SRodney W. Grimes 			/*
3203ea022d16SRodney W. Grimes 			 * If user typed "ls -l", etc, and the client
3204ea022d16SRodney W. Grimes 			 * used NLST, do what the user meant.
3205ea022d16SRodney W. Grimes 			 */
3206ea022d16SRodney W. Grimes 			if (dirname[0] == '-' && *dirlist == NULL &&
32074cd51076SYaroslav Tykhiy 			    dout == NULL)
3208af85d782SDavid Nugent 				retrieve(_PATH_LS " %s", dirname);
32094cd51076SYaroslav Tykhiy 			else
3210ea022d16SRodney W. Grimes 				perror_reply(550, whichf);
3211ea022d16SRodney W. Grimes 			goto out;
3212ea022d16SRodney W. Grimes 		}
3213ea022d16SRodney W. Grimes 
3214ea022d16SRodney W. Grimes 		if (S_ISREG(st.st_mode)) {
3215ea022d16SRodney W. Grimes 			if (dout == NULL) {
32160e519c96SYaroslav Tykhiy 				dout = dataconn("file list", -1, "w");
3217ea022d16SRodney W. Grimes 				if (dout == NULL)
3218ea022d16SRodney W. Grimes 					goto out;
32194cd51076SYaroslav Tykhiy 				STARTXFER;
3220ea022d16SRodney W. Grimes 			}
32214cd51076SYaroslav Tykhiy 			START_UNSAFE;
3222ea022d16SRodney W. Grimes 			fprintf(dout, "%s%s\n", dirname,
3223ea022d16SRodney W. Grimes 				type == TYPE_A ? "\r" : "");
32244cd51076SYaroslav Tykhiy 			END_UNSAFE;
32254cd51076SYaroslav Tykhiy 			if (ferror(dout))
32264cd51076SYaroslav Tykhiy 				goto data_err;
32274cd51076SYaroslav Tykhiy 			byte_count += strlen(dirname) +
32284cd51076SYaroslav Tykhiy 				      (type == TYPE_A ? 2 : 1);
32294cd51076SYaroslav Tykhiy 			CHECKOOB(goto abrt);
3230ea022d16SRodney W. Grimes 			continue;
3231ea022d16SRodney W. Grimes 		} else if (!S_ISDIR(st.st_mode))
3232ea022d16SRodney W. Grimes 			continue;
3233ea022d16SRodney W. Grimes 
3234ea022d16SRodney W. Grimes 		if ((dirp = opendir(dirname)) == NULL)
3235ea022d16SRodney W. Grimes 			continue;
3236ea022d16SRodney W. Grimes 
3237ea022d16SRodney W. Grimes 		while ((dir = readdir(dirp)) != NULL) {
3238ea022d16SRodney W. Grimes 			char nbuf[MAXPATHLEN];
3239ea022d16SRodney W. Grimes 
32404cd51076SYaroslav Tykhiy 			CHECKOOB(goto abrt);
32414b82fc95SYaroslav Tykhiy 
3242ea022d16SRodney W. Grimes 			if (dir->d_name[0] == '.' && dir->d_namlen == 1)
3243ea022d16SRodney W. Grimes 				continue;
3244ea022d16SRodney W. Grimes 			if (dir->d_name[0] == '.' && dir->d_name[1] == '.' &&
3245ea022d16SRodney W. Grimes 			    dir->d_namlen == 2)
3246ea022d16SRodney W. Grimes 				continue;
3247ea022d16SRodney W. Grimes 
3248e760ef2cSWarner Losh 			snprintf(nbuf, sizeof(nbuf),
3249e760ef2cSWarner Losh 				"%s/%s", dirname, dir->d_name);
3250ea022d16SRodney W. Grimes 
3251ea022d16SRodney W. Grimes 			/*
3252ea022d16SRodney W. Grimes 			 * We have to do a stat to insure it's
3253ea022d16SRodney W. Grimes 			 * not a directory or special file.
3254ea022d16SRodney W. Grimes 			 */
3255ea022d16SRodney W. Grimes 			if (simple || (stat(nbuf, &st) == 0 &&
3256ea022d16SRodney W. Grimes 			    S_ISREG(st.st_mode))) {
3257ea022d16SRodney W. Grimes 				if (dout == NULL) {
32580e519c96SYaroslav Tykhiy 					dout = dataconn("file list", -1, "w");
3259ea022d16SRodney W. Grimes 					if (dout == NULL)
3260ea022d16SRodney W. Grimes 						goto out;
32614cd51076SYaroslav Tykhiy 					STARTXFER;
3262ea022d16SRodney W. Grimes 				}
32634cd51076SYaroslav Tykhiy 				START_UNSAFE;
3264ea022d16SRodney W. Grimes 				if (nbuf[0] == '.' && nbuf[1] == '/')
3265ea022d16SRodney W. Grimes 					fprintf(dout, "%s%s\n", &nbuf[2],
3266ea022d16SRodney W. Grimes 						type == TYPE_A ? "\r" : "");
3267ea022d16SRodney W. Grimes 				else
3268ea022d16SRodney W. Grimes 					fprintf(dout, "%s%s\n", nbuf,
3269ea022d16SRodney W. Grimes 						type == TYPE_A ? "\r" : "");
32704cd51076SYaroslav Tykhiy 				END_UNSAFE;
32714cd51076SYaroslav Tykhiy 				if (ferror(dout))
32724cd51076SYaroslav Tykhiy 					goto data_err;
32734cd51076SYaroslav Tykhiy 				byte_count += strlen(nbuf) +
32744cd51076SYaroslav Tykhiy 					      (type == TYPE_A ? 2 : 1);
32754cd51076SYaroslav Tykhiy 				CHECKOOB(goto abrt);
3276ea022d16SRodney W. Grimes 			}
3277ea022d16SRodney W. Grimes 		}
3278ea022d16SRodney W. Grimes 		(void) closedir(dirp);
32794cd51076SYaroslav Tykhiy 		dirp = NULL;
3280ea022d16SRodney W. Grimes 	}
3281ea022d16SRodney W. Grimes 
3282ea022d16SRodney W. Grimes 	if (dout == NULL)
3283ea022d16SRodney W. Grimes 		reply(550, "No files found.");
32844cd51076SYaroslav Tykhiy 	else if (ferror(dout))
32854cd51076SYaroslav Tykhiy data_err:	perror_reply(550, "Data connection");
3286ea022d16SRodney W. Grimes 	else
3287ea022d16SRodney W. Grimes 		reply(226, "Transfer complete.");
32884cd51076SYaroslav Tykhiy out:
32894cd51076SYaroslav Tykhiy 	if (dout) {
32904cd51076SYaroslav Tykhiy 		ENDXFER;
32914cd51076SYaroslav Tykhiy abrt:
3292ea022d16SRodney W. Grimes 		(void) fclose(dout);
3293ea022d16SRodney W. Grimes 		data = -1;
3294ea022d16SRodney W. Grimes 		pdata = -1;
32954cd51076SYaroslav Tykhiy 	}
32964cd51076SYaroslav Tykhiy 	if (dirp)
32974cd51076SYaroslav Tykhiy 		(void) closedir(dirp);
3298ea022d16SRodney W. Grimes 	if (freeglob) {
3299ea022d16SRodney W. Grimes 		freeglob = 0;
3300ea022d16SRodney W. Grimes 		globfree(&gl);
3301ea022d16SRodney W. Grimes 	}
3302ea022d16SRodney W. Grimes }
3303ea022d16SRodney W. Grimes 
3304cf09a206SDavid Greenman void
3305e4bc453cSWarner Losh reapchild(int signo)
3306cf09a206SDavid Greenman {
3307de9b6c03SYaroslav Tykhiy 	while (waitpid(-1, NULL, WNOHANG) > 0);
3308cf09a206SDavid Greenman }
3309cf09a206SDavid Greenman 
3310b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE
3311ea022d16SRodney W. Grimes /*
3312ea022d16SRodney W. Grimes  * Clobber argv so ps will show what we're doing.  (Stolen from sendmail.)
3313ea022d16SRodney W. Grimes  * Warning, since this is usually started from inetd.conf, it often doesn't
3314ea022d16SRodney W. Grimes  * have much of an environment or arglist to overwrite.
3315ea022d16SRodney W. Grimes  */
3316ea022d16SRodney W. Grimes void
3317ea022d16SRodney W. Grimes setproctitle(const char *fmt, ...)
3318ea022d16SRodney W. Grimes {
3319ea022d16SRodney W. Grimes 	int i;
3320ea022d16SRodney W. Grimes 	va_list ap;
3321ea022d16SRodney W. Grimes 	char *p, *bp, ch;
3322ea022d16SRodney W. Grimes 	char buf[LINE_MAX];
3323ea022d16SRodney W. Grimes 
3324ea022d16SRodney W. Grimes 	va_start(ap, fmt);
3325ea022d16SRodney W. Grimes 	(void)vsnprintf(buf, sizeof(buf), fmt, ap);
3326ea022d16SRodney W. Grimes 
3327ea022d16SRodney W. Grimes 	/* make ps print our process name */
3328ea022d16SRodney W. Grimes 	p = Argv[0];
3329ea022d16SRodney W. Grimes 	*p++ = '-';
3330ea022d16SRodney W. Grimes 
3331ea022d16SRodney W. Grimes 	i = strlen(buf);
3332ea022d16SRodney W. Grimes 	if (i > LastArgv - p - 2) {
3333ea022d16SRodney W. Grimes 		i = LastArgv - p - 2;
3334ea022d16SRodney W. Grimes 		buf[i] = '\0';
3335ea022d16SRodney W. Grimes 	}
3336ea022d16SRodney W. Grimes 	bp = buf;
3337ea022d16SRodney W. Grimes 	while (ch = *bp++)
3338ea022d16SRodney W. Grimes 		if (ch != '\n' && ch != '\r')
3339ea022d16SRodney W. Grimes 			*p++ = ch;
3340ea022d16SRodney W. Grimes 	while (p < LastArgv)
3341ea022d16SRodney W. Grimes 		*p++ = ' ';
3342ea022d16SRodney W. Grimes }
3343b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */
33443eb568f2SGuido van Rooij 
334561f891a6SPaul Traina static void
33466b2dee6bSYaroslav Tykhiy appendf(char **strp, char *fmt, ...)
33476b2dee6bSYaroslav Tykhiy {
33486b2dee6bSYaroslav Tykhiy 	va_list ap;
33496b2dee6bSYaroslav Tykhiy 	char *ostr, *p;
33506b2dee6bSYaroslav Tykhiy 
33516b2dee6bSYaroslav Tykhiy 	va_start(ap, fmt);
33526b2dee6bSYaroslav Tykhiy 	vasprintf(&p, fmt, ap);
33536b2dee6bSYaroslav Tykhiy 	va_end(ap);
33546b2dee6bSYaroslav Tykhiy 	if (p == NULL)
33556b2dee6bSYaroslav Tykhiy 		fatalerror("Ran out of memory.");
33566b2dee6bSYaroslav Tykhiy 	if (*strp == NULL)
33576b2dee6bSYaroslav Tykhiy 		*strp = p;
33586b2dee6bSYaroslav Tykhiy 	else {
33596b2dee6bSYaroslav Tykhiy 		ostr = *strp;
33606b2dee6bSYaroslav Tykhiy 		asprintf(strp, "%s%s", ostr, p);
33616b2dee6bSYaroslav Tykhiy 		if (*strp == NULL)
33626b2dee6bSYaroslav Tykhiy 			fatalerror("Ran out of memory.");
33636b2dee6bSYaroslav Tykhiy 		free(ostr);
33646b2dee6bSYaroslav Tykhiy 	}
33656b2dee6bSYaroslav Tykhiy }
33666b2dee6bSYaroslav Tykhiy 
33676b2dee6bSYaroslav Tykhiy static void
33686b2dee6bSYaroslav Tykhiy logcmd(char *cmd, char *file1, char *file2, off_t cnt)
33696b2dee6bSYaroslav Tykhiy {
33706b2dee6bSYaroslav Tykhiy 	char *msg = NULL;
33716b2dee6bSYaroslav Tykhiy 	char wd[MAXPATHLEN + 1];
33726b2dee6bSYaroslav Tykhiy 
33736b2dee6bSYaroslav Tykhiy 	if (logging <= 1)
33746b2dee6bSYaroslav Tykhiy 		return;
3375e897216fSYaroslav Tykhiy 
33766b2dee6bSYaroslav Tykhiy 	if (getcwd(wd, sizeof(wd) - 1) == NULL)
33776b2dee6bSYaroslav Tykhiy 		strcpy(wd, strerror(errno));
33786b2dee6bSYaroslav Tykhiy 
33796b2dee6bSYaroslav Tykhiy 	appendf(&msg, "%s", cmd);
33806b2dee6bSYaroslav Tykhiy 	if (file1)
33816b2dee6bSYaroslav Tykhiy 		appendf(&msg, " %s", file1);
33826b2dee6bSYaroslav Tykhiy 	if (file2)
33836b2dee6bSYaroslav Tykhiy 		appendf(&msg, " %s", file2);
33846b2dee6bSYaroslav Tykhiy 	if (cnt >= 0)
33856b2dee6bSYaroslav Tykhiy 		appendf(&msg, " = %jd bytes", (intmax_t)cnt);
3386e897216fSYaroslav Tykhiy 	appendf(&msg, " (wd: %s", wd);
3387215a9f9dSYaroslav Tykhiy 	if (guest || dochroot)
3388e897216fSYaroslav Tykhiy 		appendf(&msg, "; chrooted");
3389e897216fSYaroslav Tykhiy 	appendf(&msg, ")");
33906b2dee6bSYaroslav Tykhiy 	syslog(LOG_INFO, "%s", msg);
33916b2dee6bSYaroslav Tykhiy 	free(msg);
33926b2dee6bSYaroslav Tykhiy }
33936b2dee6bSYaroslav Tykhiy 
33946b2dee6bSYaroslav Tykhiy static void
3395e4bc453cSWarner Losh logxfer(char *name, off_t size, time_t start)
33963eb568f2SGuido van Rooij {
33978c1c21f2SYaroslav Tykhiy 	char buf[MAXPATHLEN + 1024];
33983eb568f2SGuido van Rooij 	char path[MAXPATHLEN + 1];
3399158a00b2SJohn Birrell 	time_t now;
34003eb568f2SGuido van Rooij 
34018c1c21f2SYaroslav Tykhiy 	if (statfd >= 0) {
34023eb568f2SGuido van Rooij 		time(&now);
34038c1c21f2SYaroslav Tykhiy 		if (realpath(name, path) == NULL) {
34048c1c21f2SYaroslav Tykhiy 			syslog(LOG_NOTICE, "realpath failed on %s: %m", path);
34058c1c21f2SYaroslav Tykhiy 			return;
34068c1c21f2SYaroslav Tykhiy 		}
34078c1c21f2SYaroslav Tykhiy 		snprintf(buf, sizeof(buf), "%.20s!%s!%s!%s!%jd!%ld\n",
34083eb568f2SGuido van Rooij 			ctime(&now)+4, ident, remotehost,
34098c1c21f2SYaroslav Tykhiy 			path, (intmax_t)size,
3410e4a71114SAndrey A. Chernov 			(long)(now - start + (now == start)));
34113eb568f2SGuido van Rooij 		write(statfd, buf, strlen(buf));
34123eb568f2SGuido van Rooij 	}
34133eb568f2SGuido van Rooij }
3414e4648f05SYaroslav Tykhiy 
3415e4648f05SYaroslav Tykhiy static char *
3416e4648f05SYaroslav Tykhiy doublequote(char *s)
3417e4648f05SYaroslav Tykhiy {
3418e4648f05SYaroslav Tykhiy 	int n;
3419e4648f05SYaroslav Tykhiy 	char *p, *s2;
3420e4648f05SYaroslav Tykhiy 
3421e4648f05SYaroslav Tykhiy 	for (p = s, n = 0; *p; p++)
3422e4648f05SYaroslav Tykhiy 		if (*p == '"')
3423e4648f05SYaroslav Tykhiy 			n++;
3424e4648f05SYaroslav Tykhiy 
3425e4648f05SYaroslav Tykhiy 	if ((s2 = malloc(p - s + n + 1)) == NULL)
3426e4648f05SYaroslav Tykhiy 		return (NULL);
3427e4648f05SYaroslav Tykhiy 
3428e4648f05SYaroslav Tykhiy 	for (p = s2; *s; s++, p++) {
3429e4648f05SYaroslav Tykhiy 		if ((*p = *s) == '"')
3430e4648f05SYaroslav Tykhiy 			*(++p) = '"';
3431e4648f05SYaroslav Tykhiy 	}
3432e4648f05SYaroslav Tykhiy 	*p = '\0';
3433e4648f05SYaroslav Tykhiy 
3434e4648f05SYaroslav Tykhiy 	return (s2);
3435e4648f05SYaroslav Tykhiy }
3436206fe568SHajimu UMEMOTO 
3437206fe568SHajimu UMEMOTO /* setup server socket for specified address family */
3438206fe568SHajimu UMEMOTO /* if af is PF_UNSPEC more than one socket may be returned */
3439206fe568SHajimu UMEMOTO /* the returned list is dynamically allocated, so caller needs to free it */
3440206fe568SHajimu UMEMOTO static int *
3441206fe568SHajimu UMEMOTO socksetup(int af, char *bindname, const char *bindport)
3442206fe568SHajimu UMEMOTO {
3443206fe568SHajimu UMEMOTO 	struct addrinfo hints, *res, *r;
3444206fe568SHajimu UMEMOTO 	int error, maxs, *s, *socks;
3445206fe568SHajimu UMEMOTO 	const int on = 1;
3446206fe568SHajimu UMEMOTO 
3447206fe568SHajimu UMEMOTO 	memset(&hints, 0, sizeof(hints));
3448206fe568SHajimu UMEMOTO 	hints.ai_flags = AI_PASSIVE;
3449206fe568SHajimu UMEMOTO 	hints.ai_family = af;
3450206fe568SHajimu UMEMOTO 	hints.ai_socktype = SOCK_STREAM;
3451206fe568SHajimu UMEMOTO 	error = getaddrinfo(bindname, bindport, &hints, &res);
3452206fe568SHajimu UMEMOTO 	if (error) {
3453206fe568SHajimu UMEMOTO 		syslog(LOG_ERR, "%s", gai_strerror(error));
3454206fe568SHajimu UMEMOTO 		if (error == EAI_SYSTEM)
3455206fe568SHajimu UMEMOTO 			syslog(LOG_ERR, "%s", strerror(errno));
3456206fe568SHajimu UMEMOTO 		return NULL;
3457206fe568SHajimu UMEMOTO 	}
3458206fe568SHajimu UMEMOTO 
3459206fe568SHajimu UMEMOTO 	/* Count max number of sockets we may open */
3460206fe568SHajimu UMEMOTO 	for (maxs = 0, r = res; r; r = r->ai_next, maxs++)
3461206fe568SHajimu UMEMOTO 		;
3462206fe568SHajimu UMEMOTO 	socks = malloc((maxs + 1) * sizeof(int));
3463206fe568SHajimu UMEMOTO 	if (!socks) {
3464206fe568SHajimu UMEMOTO 		freeaddrinfo(res);
3465206fe568SHajimu UMEMOTO 		syslog(LOG_ERR, "couldn't allocate memory for sockets");
3466206fe568SHajimu UMEMOTO 		return NULL;
3467206fe568SHajimu UMEMOTO 	}
3468206fe568SHajimu UMEMOTO 
3469206fe568SHajimu UMEMOTO 	*socks = 0;   /* num of sockets counter at start of array */
3470206fe568SHajimu UMEMOTO 	s = socks + 1;
3471206fe568SHajimu UMEMOTO 	for (r = res; r; r = r->ai_next) {
3472206fe568SHajimu UMEMOTO 		*s = socket(r->ai_family, r->ai_socktype, r->ai_protocol);
3473206fe568SHajimu UMEMOTO 		if (*s < 0) {
3474206fe568SHajimu UMEMOTO 			syslog(LOG_DEBUG, "control socket: %m");
3475206fe568SHajimu UMEMOTO 			continue;
3476206fe568SHajimu UMEMOTO 		}
3477206fe568SHajimu UMEMOTO 		if (setsockopt(*s, SOL_SOCKET, SO_REUSEADDR,
3478206fe568SHajimu UMEMOTO 		    &on, sizeof(on)) < 0)
3479206fe568SHajimu UMEMOTO 			syslog(LOG_WARNING,
3480206fe568SHajimu UMEMOTO 			    "control setsockopt (SO_REUSEADDR): %m");
3481206fe568SHajimu UMEMOTO 		if (r->ai_family == AF_INET6) {
3482206fe568SHajimu UMEMOTO 			if (setsockopt(*s, IPPROTO_IPV6, IPV6_V6ONLY,
3483206fe568SHajimu UMEMOTO 			    &on, sizeof(on)) < 0)
3484206fe568SHajimu UMEMOTO 				syslog(LOG_WARNING,
3485206fe568SHajimu UMEMOTO 				    "control setsockopt (IPV6_V6ONLY): %m");
3486206fe568SHajimu UMEMOTO 		}
3487206fe568SHajimu UMEMOTO 		if (bind(*s, r->ai_addr, r->ai_addrlen) < 0) {
3488206fe568SHajimu UMEMOTO 			syslog(LOG_DEBUG, "control bind: %m");
3489206fe568SHajimu UMEMOTO 			close(*s);
3490206fe568SHajimu UMEMOTO 			continue;
3491206fe568SHajimu UMEMOTO 		}
3492206fe568SHajimu UMEMOTO 		(*socks)++;
3493206fe568SHajimu UMEMOTO 		s++;
3494206fe568SHajimu UMEMOTO 	}
3495206fe568SHajimu UMEMOTO 
3496206fe568SHajimu UMEMOTO 	if (res)
3497206fe568SHajimu UMEMOTO 		freeaddrinfo(res);
3498206fe568SHajimu UMEMOTO 
3499206fe568SHajimu UMEMOTO 	if (*socks == 0) {
3500206fe568SHajimu UMEMOTO 		syslog(LOG_ERR, "control socket: Couldn't bind to any socket");
3501206fe568SHajimu UMEMOTO 		free(socks);
3502206fe568SHajimu UMEMOTO 		return NULL;
3503206fe568SHajimu UMEMOTO 	}
3504206fe568SHajimu UMEMOTO 	return(socks);
3505206fe568SHajimu UMEMOTO }
3506