xref: /freebsd/libexec/ftpd/ftpd.c (revision e07d11b69199483fd3175b5f17ad5fff35ae0677)
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. */
147*e07d11b6SKurt Lidl int	use_blacklist = 0;
14862513e76SNik Clayton 
149ea022d16SRodney W. Grimes off_t	file_size;
150ea022d16SRodney W. Grimes off_t	byte_count;
151ea022d16SRodney W. Grimes #if !defined(CMASK) || CMASK == 0
152ea022d16SRodney W. Grimes #undef CMASK
153ea022d16SRodney W. Grimes #define CMASK 027
154ea022d16SRodney W. Grimes #endif
155ea022d16SRodney W. Grimes int	defumask = CMASK;		/* default umask value */
156ea022d16SRodney W. Grimes char	tmpline[7];
157ea4e54b9SDavid Nugent char	*hostname;
158ad442344SDima Dorfman int	epsvall = 0;
159ad442344SDima Dorfman 
1600512556aSDavid Nugent #ifdef VIRTUAL_HOSTING
161ea4e54b9SDavid Nugent char	*ftpuser;
162ea4e54b9SDavid Nugent 
163ea4e54b9SDavid Nugent static struct ftphost {
164ea4e54b9SDavid Nugent 	struct ftphost	*next;
165f38c6cadSYoshinobu Inoue 	struct addrinfo *hostinfo;
166ea4e54b9SDavid Nugent 	char		*hostname;
167ea4e54b9SDavid Nugent 	char		*anonuser;
168ea4e54b9SDavid Nugent 	char		*statfile;
169ea4e54b9SDavid Nugent 	char		*welcome;
170ea4e54b9SDavid Nugent 	char		*loginmsg;
171ea4e54b9SDavid Nugent } *thishost, *firsthost;
172ea4e54b9SDavid Nugent 
173ea4e54b9SDavid Nugent #endif
17404683b2cSYaroslav Tykhiy char	remotehost[NI_MAXHOST];
1753eb568f2SGuido van Rooij char	*ident = NULL;
176a5a4544eSPaul Traina 
17780643af0SEd Schouten static char	wtmpid[20];
178a5a4544eSPaul Traina 
1795bc9d93dSMark Murray #ifdef USE_PAM
180e4bc453cSWarner Losh static int	auth_pam(struct passwd**, const char*);
1815bc9d93dSMark Murray pam_handle_t	*pamh = NULL;
18247499ecdSAndrey A. Chernov #endif
183fa1746c9SMark Murray 
184fa1746c9SMark Murray static struct opie	opiedata;
185fa1746c9SMark Murray static char		opieprompt[OPIE_CHALLENGE_MAX+1];
18647499ecdSAndrey A. Chernov static int		pwok;
1879aca17cbSMark Murray 
188125b9635SYaroslav Tykhiy char	*pid_file = NULL; /* means default location to pidfile(3) */
189105a3c98SJulian Elischer 
190ea022d16SRodney W. Grimes /*
1916d10cb2fSJonathan Lemon  * Limit number of pathnames that glob can return.
1926d10cb2fSJonathan Lemon  * A limit of 0 indicates the number of pathnames is unlimited.
1936d10cb2fSJonathan Lemon  */
1946d10cb2fSJonathan Lemon #define MAXGLOBARGS	16384
1956d10cb2fSJonathan Lemon #
1966d10cb2fSJonathan Lemon 
1976d10cb2fSJonathan Lemon /*
198ea022d16SRodney W. Grimes  * Timeout intervals for retrying connections
199ea022d16SRodney W. Grimes  * to hosts that don't accept PORT cmds.  This
200ea022d16SRodney W. Grimes  * is a kludge, but given the problems with TCP...
201ea022d16SRodney W. Grimes  */
202ea022d16SRodney W. Grimes #define	SWAITMAX	90	/* wait at most 90 seconds */
203ea022d16SRodney W. Grimes #define	SWAITINT	5	/* interval between retries */
204ea022d16SRodney W. Grimes 
205ea022d16SRodney W. Grimes int	swaitmax = SWAITMAX;
206ea022d16SRodney W. Grimes int	swaitint = SWAITINT;
207ea022d16SRodney W. Grimes 
208ea022d16SRodney W. Grimes #ifdef SETPROCTITLE
209b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE
210ea022d16SRodney W. Grimes char	**Argv = NULL;		/* pointer to argument vector */
211ea022d16SRodney W. Grimes char	*LastArgv = NULL;	/* end of argv */
212b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */
213ea022d16SRodney W. Grimes char	proctitle[LINE_MAX];	/* initial part of title */
214ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */
215ea022d16SRodney W. Grimes 
2166b2dee6bSYaroslav Tykhiy #define LOGCMD(cmd, file)		logcmd((cmd), (file), NULL, -1)
2176b2dee6bSYaroslav Tykhiy #define LOGCMD2(cmd, file1, file2)	logcmd((cmd), (file1), (file2), -1)
2186b2dee6bSYaroslav Tykhiy #define LOGBYTES(cmd, file, cnt)	logcmd((cmd), (file), NULL, (cnt))
219ea022d16SRodney W. Grimes 
2204cd51076SYaroslav Tykhiy static	volatile sig_atomic_t recvurg;
2214cd51076SYaroslav Tykhiy static	int transflag;		/* NB: for debugging only */
2224cd51076SYaroslav Tykhiy 
2234cd51076SYaroslav Tykhiy #define STARTXFER	flagxfer(1)
2244cd51076SYaroslav Tykhiy #define ENDXFER		flagxfer(0)
2254cd51076SYaroslav Tykhiy 
2264cd51076SYaroslav Tykhiy #define START_UNSAFE	maskurg(1)
2274cd51076SYaroslav Tykhiy #define END_UNSAFE	maskurg(0)
2284cd51076SYaroslav Tykhiy 
2294cd51076SYaroslav Tykhiy /* It's OK to put an `else' clause after this macro. */
2304cd51076SYaroslav Tykhiy #define CHECKOOB(action)						\
2314cd51076SYaroslav Tykhiy 	if (recvurg) {							\
2324cd51076SYaroslav Tykhiy 		recvurg = 0;						\
2334cd51076SYaroslav Tykhiy 		if (myoob()) {						\
2344cd51076SYaroslav Tykhiy 			ENDXFER;					\
2354cd51076SYaroslav Tykhiy 			action;						\
2364cd51076SYaroslav Tykhiy 		}							\
2374cd51076SYaroslav Tykhiy 	}
2384cd51076SYaroslav Tykhiy 
239ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
2402c9fd5f2SHajimu UMEMOTO static void	 inithosts(int);
241e4bc453cSWarner Losh static void	 selecthost(union sockunion *);
242ea4e54b9SDavid Nugent #endif
243e4bc453cSWarner Losh static void	 ack(char *);
244e4bc453cSWarner Losh static void	 sigurg(int);
2454cd51076SYaroslav Tykhiy static void	 maskurg(int);
2464cd51076SYaroslav Tykhiy static void	 flagxfer(int);
2474cd51076SYaroslav Tykhiy static int	 myoob(void);
248cefb6785SChristian S.J. Peron static int	 checkuser(char *, char *, int, char **, int *);
249e4bc453cSWarner Losh static FILE	*dataconn(char *, off_t, char *);
250e4bc453cSWarner Losh static void	 dolog(struct sockaddr *);
251e4bc453cSWarner Losh static void	 end_login(void);
252e4bc453cSWarner Losh static FILE	*getdatasock(char *);
253a117c345SYaroslav Tykhiy static int	 guniquefd(char *, char **);
254e4bc453cSWarner Losh static void	 lostconn(int);
255e4bc453cSWarner Losh static void	 sigquit(int);
256e4bc453cSWarner Losh static int	 receive_data(FILE *, FILE *);
2576e4b0a55SYaroslav Tykhiy static int	 send_data(FILE *, FILE *, size_t, off_t, int);
258ea022d16SRodney W. Grimes static struct passwd *
259e4bc453cSWarner Losh 		 sgetpwnam(char *);
260e4bc453cSWarner Losh static char	*sgetsave(char *);
261e4bc453cSWarner Losh static void	 reapchild(int);
262eb5b2bb3SYaroslav Tykhiy static void	 appendf(char **, char *, ...) __printflike(2, 3);
2636b2dee6bSYaroslav Tykhiy static void	 logcmd(char *, char *, char *, off_t);
264e4bc453cSWarner Losh static void      logxfer(char *, off_t, time_t);
265e4648f05SYaroslav Tykhiy static char	*doublequote(char *);
266206fe568SHajimu UMEMOTO static int	*socksetup(int, char *, const char *);
267ea022d16SRodney W. Grimes 
268ea022d16SRodney W. Grimes int
269e4bc453cSWarner Losh main(int argc, char *argv[], char **envp)
270ea022d16SRodney W. Grimes {
27178e3eed0SStefan Farfeleder 	socklen_t addrlen;
272504422faSKurt Lidl 	int ch, on = 1, tos, s = STDIN_FILENO;
273ea022d16SRodney W. Grimes 	char *cp, line[LINE_MAX];
274ea022d16SRodney W. Grimes 	FILE *fd;
2754dd8b5abSYoshinobu Inoue 	char	*bindname = NULL;
27663591ba5SYaroslav Tykhiy 	const char *bindport = "ftp";
2774dd8b5abSYoshinobu Inoue 	int	family = AF_UNSPEC;
2784b82fc95SYaroslav Tykhiy 	struct sigaction sa;
279ea022d16SRodney W. Grimes 
28034d1ba5cSAndrey A. Chernov 	tzset();		/* in case no timezone database in ~ftp */
2814b82fc95SYaroslav Tykhiy 	sigemptyset(&sa.sa_mask);
2824b82fc95SYaroslav Tykhiy 	sa.sa_flags = SA_RESTART;
28334d1ba5cSAndrey A. Chernov 
284b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE
285ea022d16SRodney W. Grimes 	/*
286ea022d16SRodney W. Grimes 	 *  Save start and extent of argv for setproctitle.
287ea022d16SRodney W. Grimes 	 */
288ea022d16SRodney W. Grimes 	Argv = argv;
289ea022d16SRodney W. Grimes 	while (*envp)
290ea022d16SRodney W. Grimes 		envp++;
291ea022d16SRodney W. Grimes 	LastArgv = envp[-1] + strlen(envp[-1]);
292b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */
293ea022d16SRodney W. Grimes 
2946c98f401SYaroslav Tykhiy 	/*
2956c98f401SYaroslav Tykhiy 	 * Prevent diagnostic messages from appearing on stderr.
2966c98f401SYaroslav Tykhiy 	 * We run as a daemon or from inetd; in both cases, there's
2976c98f401SYaroslav Tykhiy 	 * more reason in logging to syslog.
2986c98f401SYaroslav Tykhiy 	 */
2996c98f401SYaroslav Tykhiy 	(void) freopen(_PATH_DEVNULL, "w", stderr);
3006c98f401SYaroslav Tykhiy 	opterr = 0;
3016c98f401SYaroslav Tykhiy 
3026c98f401SYaroslav Tykhiy 	/*
3036c98f401SYaroslav Tykhiy 	 * LOG_NDELAY sets up the logging connection immediately,
3046c98f401SYaroslav Tykhiy 	 * necessary for anonymous ftp's that chroot and can't do it later.
3056c98f401SYaroslav Tykhiy 	 */
3066c98f401SYaroslav Tykhiy 	openlog("ftpd", LOG_PID | LOG_NDELAY, LOG_FTP);
3073eb568f2SGuido van Rooij 
308c152df28SYaroslav Tykhiy 	while ((ch = getopt(argc, argv,
309*e07d11b6SKurt Lidl 	                    "468a:ABdDEhlmMoOp:P:rRSt:T:u:UvW")) != -1) {
310ea022d16SRodney W. Grimes 		switch (ch) {
3110e063efeSYaroslav Tykhiy 		case '4':
312206fe568SHajimu UMEMOTO 			family = (family == AF_INET6) ? AF_UNSPEC : AF_INET;
3130e063efeSYaroslav Tykhiy 			break;
3140e063efeSYaroslav Tykhiy 
3150e063efeSYaroslav Tykhiy 		case '6':
316206fe568SHajimu UMEMOTO 			family = (family == AF_INET) ? AF_UNSPEC : AF_INET6;
3170e063efeSYaroslav Tykhiy 			break;
3180e063efeSYaroslav Tykhiy 
3192ea42282SYaroslav Tykhiy 		case '8':
3202ea42282SYaroslav Tykhiy 			assumeutf8 = 1;
3212ea42282SYaroslav Tykhiy 			break;
3222ea42282SYaroslav Tykhiy 
3230e063efeSYaroslav Tykhiy 		case 'a':
3240e063efeSYaroslav Tykhiy 			bindname = optarg;
3250e063efeSYaroslav Tykhiy 			break;
3260e063efeSYaroslav Tykhiy 
3270e063efeSYaroslav Tykhiy 		case 'A':
3280e063efeSYaroslav Tykhiy 			anon_only = 1;
329cf09a206SDavid Greenman 			break;
330cf09a206SDavid Greenman 
331*e07d11b6SKurt Lidl 		case 'B':
332*e07d11b6SKurt Lidl #ifdef USE_BLACKLIST
333*e07d11b6SKurt Lidl 			use_blacklist = 1;
334*e07d11b6SKurt Lidl #else
335*e07d11b6SKurt Lidl 			syslog(LOG_WARNING, "not compiled with USE_BLACKLIST support");
336*e07d11b6SKurt Lidl #endif
337*e07d11b6SKurt Lidl 			break;
338*e07d11b6SKurt Lidl 
339ea022d16SRodney W. Grimes 		case 'd':
340618b0bbaSMark Murray 			ftpdebug++;
341ea022d16SRodney W. Grimes 			break;
342ea022d16SRodney W. Grimes 
3430e063efeSYaroslav Tykhiy 		case 'D':
3440e063efeSYaroslav Tykhiy 			daemon_mode++;
3450e063efeSYaroslav Tykhiy 			break;
3460e063efeSYaroslav Tykhiy 
347a4b77a2aSPoul-Henning Kamp 		case 'E':
348a4b77a2aSPoul-Henning Kamp 			noepsv = 1;
349a4b77a2aSPoul-Henning Kamp 			break;
350a4b77a2aSPoul-Henning Kamp 
351c152df28SYaroslav Tykhiy 		case 'h':
352c152df28SYaroslav Tykhiy 			hostinfo = 0;
353c152df28SYaroslav Tykhiy 			break;
354c152df28SYaroslav Tykhiy 
355ea022d16SRodney W. Grimes 		case 'l':
356ea022d16SRodney W. Grimes 			logging++;	/* > 1 == extra logging */
357ea022d16SRodney W. Grimes 			break;
358ea022d16SRodney W. Grimes 
359a117c345SYaroslav Tykhiy 		case 'm':
360a117c345SYaroslav Tykhiy 			noguestmod = 0;
361a117c345SYaroslav Tykhiy 			break;
362a117c345SYaroslav Tykhiy 
3630e063efeSYaroslav Tykhiy 		case 'M':
3640e063efeSYaroslav Tykhiy 			noguestmkd = 1;
3650e063efeSYaroslav Tykhiy 			break;
3660e063efeSYaroslav Tykhiy 
3670e063efeSYaroslav Tykhiy 		case 'o':
3680e063efeSYaroslav Tykhiy 			noretr = 1;
3690e063efeSYaroslav Tykhiy 			break;
3700e063efeSYaroslav Tykhiy 
3710e063efeSYaroslav Tykhiy 		case 'O':
3720e063efeSYaroslav Tykhiy 			noguestretr = 1;
3730e063efeSYaroslav Tykhiy 			break;
3740e063efeSYaroslav Tykhiy 
3750e063efeSYaroslav Tykhiy 		case 'p':
3760e063efeSYaroslav Tykhiy 			pid_file = optarg;
3770e063efeSYaroslav Tykhiy 			break;
3780e063efeSYaroslav Tykhiy 
37963591ba5SYaroslav Tykhiy 		case 'P':
38063591ba5SYaroslav Tykhiy 			bindport = optarg;
38163591ba5SYaroslav Tykhiy 			break;
38263591ba5SYaroslav Tykhiy 
383a4b77a2aSPoul-Henning Kamp 		case 'r':
384a4b77a2aSPoul-Henning Kamp 			readonly = 1;
385a4b77a2aSPoul-Henning Kamp 			break;
386a4b77a2aSPoul-Henning Kamp 
387a5a4544eSPaul Traina 		case 'R':
388a5a4544eSPaul Traina 			paranoid = 0;
389a5a4544eSPaul Traina 			break;
390a5a4544eSPaul Traina 
391a5a4544eSPaul Traina 		case 'S':
392a5a4544eSPaul Traina 			stats++;
393a5a4544eSPaul Traina 			break;
394a5a4544eSPaul Traina 
395ea022d16SRodney W. Grimes 		case 't':
396ea022d16SRodney W. Grimes 			timeout = atoi(optarg);
397ea022d16SRodney W. Grimes 			if (maxtimeout < timeout)
398ea022d16SRodney W. Grimes 				maxtimeout = timeout;
399ea022d16SRodney W. Grimes 			break;
400a5a4544eSPaul Traina 
4010e063efeSYaroslav Tykhiy 		case 'T':
4020e063efeSYaroslav Tykhiy 			maxtimeout = atoi(optarg);
4030e063efeSYaroslav Tykhiy 			if (timeout > maxtimeout)
4040e063efeSYaroslav Tykhiy 				timeout = maxtimeout;
405105a3c98SJulian Elischer 			break;
406105a3c98SJulian Elischer 
407ea022d16SRodney W. Grimes 		case 'u':
408ea022d16SRodney W. Grimes 		    {
409ea022d16SRodney W. Grimes 			long val = 0;
410ea022d16SRodney W. Grimes 
411ea022d16SRodney W. Grimes 			val = strtol(optarg, &optarg, 8);
412ea022d16SRodney W. Grimes 			if (*optarg != '\0' || val < 0)
4136c98f401SYaroslav Tykhiy 				syslog(LOG_WARNING, "bad value for -u");
414ea022d16SRodney W. Grimes 			else
415ea022d16SRodney W. Grimes 				defumask = val;
416ea022d16SRodney W. Grimes 			break;
417ea022d16SRodney W. Grimes 		    }
4180e063efeSYaroslav Tykhiy 		case 'U':
4190e063efeSYaroslav Tykhiy 			restricted_data_ports = 0;
4205a392aecSTorsten Blum 			break;
421ea022d16SRodney W. Grimes 
422ea022d16SRodney W. Grimes 		case 'v':
42393bd9dc5SYaroslav Tykhiy 			ftpdebug++;
424ea022d16SRodney W. Grimes 			break;
425ea022d16SRodney W. Grimes 
4265d7e0128SYaroslav Tykhiy 		case 'W':
4275d7e0128SYaroslav Tykhiy 			dowtmp = 0;
4285d7e0128SYaroslav Tykhiy 			break;
4295d7e0128SYaroslav Tykhiy 
430ea022d16SRodney W. Grimes 		default:
4316c98f401SYaroslav Tykhiy 			syslog(LOG_WARNING, "unknown flag -%c ignored", optopt);
432ea022d16SRodney W. Grimes 			break;
433ea022d16SRodney W. Grimes 		}
434ea022d16SRodney W. Grimes 	}
435cf09a206SDavid Greenman 
436cf09a206SDavid Greenman 	if (daemon_mode) {
437206fe568SHajimu UMEMOTO 		int *ctl_sock, fd, maxfd = -1, nfds, i;
438206fe568SHajimu UMEMOTO 		fd_set defreadfds, readfds;
439206fe568SHajimu UMEMOTO 		pid_t pid;
440125b9635SYaroslav Tykhiy 		struct pidfh *pfh;
441125b9635SYaroslav Tykhiy 
442125b9635SYaroslav Tykhiy 		if ((pfh = pidfile_open(pid_file, 0600, &pid)) == NULL) {
443125b9635SYaroslav Tykhiy 			if (errno == EEXIST) {
444125b9635SYaroslav Tykhiy 				syslog(LOG_ERR, "%s already running, pid %d",
445125b9635SYaroslav Tykhiy 				       getprogname(), (int)pid);
446125b9635SYaroslav Tykhiy 				exit(1);
447125b9635SYaroslav Tykhiy 			}
448125b9635SYaroslav Tykhiy 			syslog(LOG_WARNING, "pidfile_open: %m");
449125b9635SYaroslav Tykhiy 		}
450cf09a206SDavid Greenman 
451cf09a206SDavid Greenman 		/*
452cf09a206SDavid Greenman 		 * Detach from parent.
453cf09a206SDavid Greenman 		 */
454cf09a206SDavid Greenman 		if (daemon(1, 1) < 0) {
455cf09a206SDavid Greenman 			syslog(LOG_ERR, "failed to become a daemon");
456cf09a206SDavid Greenman 			exit(1);
457cf09a206SDavid Greenman 		}
458125b9635SYaroslav Tykhiy 
459125b9635SYaroslav Tykhiy 		if (pfh != NULL && pidfile_write(pfh) == -1)
460125b9635SYaroslav Tykhiy 			syslog(LOG_WARNING, "pidfile_write: %m");
461125b9635SYaroslav Tykhiy 
4624b82fc95SYaroslav Tykhiy 		sa.sa_handler = reapchild;
4634b82fc95SYaroslav Tykhiy 		(void)sigaction(SIGCHLD, &sa, NULL);
4644dd8b5abSYoshinobu Inoue 
4652c9fd5f2SHajimu UMEMOTO #ifdef VIRTUAL_HOSTING
4662c9fd5f2SHajimu UMEMOTO 		inithosts(family);
4672c9fd5f2SHajimu UMEMOTO #endif
4682c9fd5f2SHajimu UMEMOTO 
469cf09a206SDavid Greenman 		/*
470cf09a206SDavid Greenman 		 * Open a socket, bind it to the FTP port, and start
471cf09a206SDavid Greenman 		 * listening.
472cf09a206SDavid Greenman 		 */
473206fe568SHajimu UMEMOTO 		ctl_sock = socksetup(family, bindname, bindport);
474206fe568SHajimu UMEMOTO 		if (ctl_sock == NULL)
475cf09a206SDavid Greenman 			exit(1);
476206fe568SHajimu UMEMOTO 
477206fe568SHajimu UMEMOTO 		FD_ZERO(&defreadfds);
478206fe568SHajimu UMEMOTO 		for (i = 1; i <= *ctl_sock; i++) {
479206fe568SHajimu UMEMOTO 			FD_SET(ctl_sock[i], &defreadfds);
480206fe568SHajimu UMEMOTO 			if (listen(ctl_sock[i], 32) < 0) {
481cf09a206SDavid Greenman 				syslog(LOG_ERR, "control listen: %m");
482cf09a206SDavid Greenman 				exit(1);
483cf09a206SDavid Greenman 			}
484206fe568SHajimu UMEMOTO 			if (maxfd < ctl_sock[i])
485206fe568SHajimu UMEMOTO 				maxfd = ctl_sock[i];
486206fe568SHajimu UMEMOTO 		}
487206fe568SHajimu UMEMOTO 
488cf09a206SDavid Greenman 		/*
489cf09a206SDavid Greenman 		 * Loop forever accepting connection requests and forking off
490cf09a206SDavid Greenman 		 * children to handle them.
491cf09a206SDavid Greenman 		 */
492cf09a206SDavid Greenman 		while (1) {
493206fe568SHajimu UMEMOTO 			FD_COPY(&defreadfds, &readfds);
494206fe568SHajimu UMEMOTO 			nfds = select(maxfd + 1, &readfds, NULL, NULL, 0);
495206fe568SHajimu UMEMOTO 			if (nfds <= 0) {
496206fe568SHajimu UMEMOTO 				if (nfds < 0 && errno != EINTR)
497206fe568SHajimu UMEMOTO 					syslog(LOG_WARNING, "select: %m");
498206fe568SHajimu UMEMOTO 				continue;
499206fe568SHajimu UMEMOTO 			}
500206fe568SHajimu UMEMOTO 
501206fe568SHajimu UMEMOTO 			pid = -1;
502206fe568SHajimu UMEMOTO                         for (i = 1; i <= *ctl_sock; i++)
503206fe568SHajimu UMEMOTO 				if (FD_ISSET(ctl_sock[i], &readfds)) {
504206fe568SHajimu UMEMOTO 					addrlen = sizeof(his_addr);
505206fe568SHajimu UMEMOTO 					fd = accept(ctl_sock[i],
506206fe568SHajimu UMEMOTO 					    (struct sockaddr *)&his_addr,
507206fe568SHajimu UMEMOTO 					    &addrlen);
508a599a64aSYaroslav Tykhiy 					if (fd == -1) {
509a599a64aSYaroslav Tykhiy 						syslog(LOG_WARNING,
510a599a64aSYaroslav Tykhiy 						       "accept: %m");
511a599a64aSYaroslav Tykhiy 						continue;
512cf09a206SDavid Greenman 					}
513a599a64aSYaroslav Tykhiy 					switch (pid = fork()) {
514a599a64aSYaroslav Tykhiy 					case 0:
5158eb0508fSYaroslav Tykhiy 						/* child */
516504422faSKurt Lidl 						(void) dup2(fd, s);
517504422faSKurt Lidl 						(void) dup2(fd, STDOUT_FILENO);
518a599a64aSYaroslav Tykhiy 						(void) close(fd);
519a599a64aSYaroslav Tykhiy 						for (i = 1; i <= *ctl_sock; i++)
5208eb0508fSYaroslav Tykhiy 							close(ctl_sock[i]);
521125b9635SYaroslav Tykhiy 						if (pfh != NULL)
522125b9635SYaroslav Tykhiy 							pidfile_close(pfh);
523a599a64aSYaroslav Tykhiy 						goto gotchild;
524a599a64aSYaroslav Tykhiy 					case -1:
525a599a64aSYaroslav Tykhiy 						syslog(LOG_WARNING, "fork: %m");
526a599a64aSYaroslav Tykhiy 						/* FALLTHROUGH */
527a599a64aSYaroslav Tykhiy 					default:
528a599a64aSYaroslav Tykhiy 						close(fd);
529a599a64aSYaroslav Tykhiy 					}
530206fe568SHajimu UMEMOTO 				}
531125b9635SYaroslav Tykhiy 		}
532cf09a206SDavid Greenman 	} else {
533cf09a206SDavid Greenman 		addrlen = sizeof(his_addr);
534504422faSKurt Lidl 		if (getpeername(s, (struct sockaddr *)&his_addr, &addrlen) < 0) {
535cf09a206SDavid Greenman 			syslog(LOG_ERR, "getpeername (%s): %m",argv[0]);
536cf09a206SDavid Greenman 			exit(1);
537cf09a206SDavid Greenman 		}
5382c9fd5f2SHajimu UMEMOTO 
5392c9fd5f2SHajimu UMEMOTO #ifdef VIRTUAL_HOSTING
5402c9fd5f2SHajimu UMEMOTO 		if (his_addr.su_family == AF_INET6 &&
5412c9fd5f2SHajimu UMEMOTO 		    IN6_IS_ADDR_V4MAPPED(&his_addr.su_sin6.sin6_addr))
5422c9fd5f2SHajimu UMEMOTO 			family = AF_INET;
5432c9fd5f2SHajimu UMEMOTO 		else
5442c9fd5f2SHajimu UMEMOTO 			family = his_addr.su_family;
5452c9fd5f2SHajimu UMEMOTO 		inithosts(family);
5462c9fd5f2SHajimu UMEMOTO #endif
547cf09a206SDavid Greenman 	}
548cf09a206SDavid Greenman 
549a599a64aSYaroslav Tykhiy gotchild:
5504b82fc95SYaroslav Tykhiy 	sa.sa_handler = SIG_DFL;
5514b82fc95SYaroslav Tykhiy 	(void)sigaction(SIGCHLD, &sa, NULL);
5524b82fc95SYaroslav Tykhiy 
5534b82fc95SYaroslav Tykhiy 	sa.sa_handler = sigurg;
5544b82fc95SYaroslav Tykhiy 	sa.sa_flags = 0;		/* don't restart syscalls for SIGURG */
5554b82fc95SYaroslav Tykhiy 	(void)sigaction(SIGURG, &sa, NULL);
5564b82fc95SYaroslav Tykhiy 
5574b82fc95SYaroslav Tykhiy 	sigfillset(&sa.sa_mask);	/* block all signals in handler */
5584b82fc95SYaroslav Tykhiy 	sa.sa_flags = SA_RESTART;
5594b82fc95SYaroslav Tykhiy 	sa.sa_handler = sigquit;
5604b82fc95SYaroslav Tykhiy 	(void)sigaction(SIGHUP, &sa, NULL);
5614b82fc95SYaroslav Tykhiy 	(void)sigaction(SIGINT, &sa, NULL);
5624b82fc95SYaroslav Tykhiy 	(void)sigaction(SIGQUIT, &sa, NULL);
5634b82fc95SYaroslav Tykhiy 	(void)sigaction(SIGTERM, &sa, NULL);
5644b82fc95SYaroslav Tykhiy 
5654b82fc95SYaroslav Tykhiy 	sa.sa_handler = lostconn;
5664b82fc95SYaroslav Tykhiy 	(void)sigaction(SIGPIPE, &sa, NULL);
567ea022d16SRodney W. Grimes 
568cf09a206SDavid Greenman 	addrlen = sizeof(ctrl_addr);
569504422faSKurt Lidl 	if (getsockname(s, (struct sockaddr *)&ctrl_addr, &addrlen) < 0) {
570cf09a206SDavid Greenman 		syslog(LOG_ERR, "getsockname (%s): %m",argv[0]);
571cf09a206SDavid Greenman 		exit(1);
572cf09a206SDavid Greenman 	}
57363591ba5SYaroslav Tykhiy 	dataport = ntohs(ctrl_addr.su_port) - 1; /* as per RFC 959 */
574ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
575ea4e54b9SDavid Nugent 	/* select our identity from virtual host table */
5764dd8b5abSYoshinobu Inoue 	selecthost(&ctrl_addr);
577ea4e54b9SDavid Nugent #endif
578cf09a206SDavid Greenman #ifdef IP_TOS
5794dd8b5abSYoshinobu Inoue 	if (ctrl_addr.su_family == AF_INET)
5804dd8b5abSYoshinobu Inoue       {
581cf09a206SDavid Greenman 	tos = IPTOS_LOWDELAY;
582504422faSKurt Lidl 	if (setsockopt(s, IPPROTO_IP, IP_TOS, &tos, sizeof(int)) < 0)
583406d1ae9SYaroslav Tykhiy 		syslog(LOG_WARNING, "control setsockopt (IP_TOS): %m");
5844dd8b5abSYoshinobu Inoue       }
585cf09a206SDavid Greenman #endif
586dadb9fb3SDavid Greenman 	/*
587dadb9fb3SDavid Greenman 	 * Disable Nagle on the control channel so that we don't have to wait
588dadb9fb3SDavid Greenman 	 * for peer's ACK before issuing our next reply.
589dadb9fb3SDavid Greenman 	 */
590504422faSKurt Lidl 	if (setsockopt(s, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) < 0)
591406d1ae9SYaroslav Tykhiy 		syslog(LOG_WARNING, "control setsockopt (TCP_NODELAY): %m");
592dadb9fb3SDavid Greenman 
5934dd8b5abSYoshinobu Inoue 	data_source.su_port = htons(ntohs(ctrl_addr.su_port) - 1);
594cf09a206SDavid Greenman 
59580643af0SEd Schouten 	(void)snprintf(wtmpid, sizeof(wtmpid), "%xftpd", getpid());
596a5a4544eSPaul Traina 
597ea022d16SRodney W. Grimes 	/* Try to handle urgent data inline */
598ea022d16SRodney W. Grimes #ifdef SO_OOBINLINE
599504422faSKurt Lidl 	if (setsockopt(s, SOL_SOCKET, SO_OOBINLINE, &on, sizeof(on)) < 0)
600406d1ae9SYaroslav Tykhiy 		syslog(LOG_WARNING, "control setsockopt (SO_OOBINLINE): %m");
601ea022d16SRodney W. Grimes #endif
602ea022d16SRodney W. Grimes 
603ea022d16SRodney W. Grimes #ifdef	F_SETOWN
604504422faSKurt Lidl 	if (fcntl(s, F_SETOWN, getpid()) == -1)
605ea022d16SRodney W. Grimes 		syslog(LOG_ERR, "fcntl F_SETOWN: %m");
606ea022d16SRodney W. Grimes #endif
6074dd8b5abSYoshinobu Inoue 	dolog((struct sockaddr *)&his_addr);
608ea022d16SRodney W. Grimes 	/*
609ea022d16SRodney W. Grimes 	 * Set up default state
610ea022d16SRodney W. Grimes 	 */
611ea022d16SRodney W. Grimes 	data = -1;
612ea022d16SRodney W. Grimes 	type = TYPE_A;
613ea022d16SRodney W. Grimes 	form = FORM_N;
614ea022d16SRodney W. Grimes 	stru = STRU_F;
615ea022d16SRodney W. Grimes 	mode = MODE_S;
616ea022d16SRodney W. Grimes 	tmpline[0] = '\0';
617ea022d16SRodney W. Grimes 
618ea022d16SRodney W. Grimes 	/* If logins are disabled, print out the message. */
619ea022d16SRodney W. Grimes 	if ((fd = fopen(_PATH_NOLOGIN,"r")) != NULL) {
620ea022d16SRodney W. Grimes 		while (fgets(line, sizeof(line), fd) != NULL) {
621ea022d16SRodney W. Grimes 			if ((cp = strchr(line, '\n')) != NULL)
622ea022d16SRodney W. Grimes 				*cp = '\0';
623ea022d16SRodney W. Grimes 			lreply(530, "%s", line);
624ea022d16SRodney W. Grimes 		}
625ea022d16SRodney W. Grimes 		(void) fflush(stdout);
626ea022d16SRodney W. Grimes 		(void) fclose(fd);
627ea022d16SRodney W. Grimes 		reply(530, "System not available.");
628ea022d16SRodney W. Grimes 		exit(0);
629ea022d16SRodney W. Grimes 	}
630ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
63163047c6fSDavid E. O'Brien 	fd = fopen(thishost->welcome, "r");
632ea4e54b9SDavid Nugent #else
63363047c6fSDavid E. O'Brien 	fd = fopen(_PATH_FTPWELCOME, "r");
634ea4e54b9SDavid Nugent #endif
63563047c6fSDavid E. O'Brien 	if (fd != NULL) {
636ea022d16SRodney W. Grimes 		while (fgets(line, sizeof(line), fd) != NULL) {
637ea022d16SRodney W. Grimes 			if ((cp = strchr(line, '\n')) != NULL)
638ea022d16SRodney W. Grimes 				*cp = '\0';
639ea022d16SRodney W. Grimes 			lreply(220, "%s", line);
640ea022d16SRodney W. Grimes 		}
641ea022d16SRodney W. Grimes 		(void) fflush(stdout);
642ea022d16SRodney W. Grimes 		(void) fclose(fd);
643ea022d16SRodney W. Grimes 		/* reply(220,) must follow */
644ea022d16SRodney W. Grimes 	}
645ea4e54b9SDavid Nugent #ifndef VIRTUAL_HOSTING
6460512556aSDavid Nugent 	if ((hostname = malloc(MAXHOSTNAMELEN)) == NULL)
647618b0bbaSMark Murray 		fatalerror("Ran out of memory.");
64804683b2cSYaroslav Tykhiy 	if (gethostname(hostname, MAXHOSTNAMELEN - 1) < 0)
64904683b2cSYaroslav Tykhiy 		hostname[0] = '\0';
6509e9a43bdSBrian Somers 	hostname[MAXHOSTNAMELEN - 1] = '\0';
651ea4e54b9SDavid Nugent #endif
652c152df28SYaroslav Tykhiy 	if (hostinfo)
653ea022d16SRodney W. Grimes 		reply(220, "%s FTP server (%s) ready.", hostname, version);
654c152df28SYaroslav Tykhiy 	else
655c152df28SYaroslav Tykhiy 		reply(220, "FTP server ready.");
656*e07d11b6SKurt Lidl 	BLACKLIST_INIT();
657ea022d16SRodney W. Grimes 	for (;;)
658ea022d16SRodney W. Grimes 		(void) yyparse();
659ea022d16SRodney W. Grimes 	/* NOTREACHED */
660ea022d16SRodney W. Grimes }
661ea022d16SRodney W. Grimes 
662ea022d16SRodney W. Grimes static void
663e4bc453cSWarner Losh lostconn(int signo)
664ea022d16SRodney W. Grimes {
665ea022d16SRodney W. Grimes 
666618b0bbaSMark Murray 	if (ftpdebug)
667ea022d16SRodney W. Grimes 		syslog(LOG_DEBUG, "lost connection");
668e02897faSPhilippe Charnier 	dologout(1);
669ea022d16SRodney W. Grimes }
670ea022d16SRodney W. Grimes 
6714b82fc95SYaroslav Tykhiy static void
672e4bc453cSWarner Losh sigquit(int signo)
6734b82fc95SYaroslav Tykhiy {
6744b82fc95SYaroslav Tykhiy 
6754b82fc95SYaroslav Tykhiy 	syslog(LOG_ERR, "got signal %d", signo);
6764b82fc95SYaroslav Tykhiy 	dologout(1);
6774b82fc95SYaroslav Tykhiy }
6784b82fc95SYaroslav Tykhiy 
679ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
680ea4e54b9SDavid Nugent /*
681ea4e54b9SDavid Nugent  * read in virtual host tables (if they exist)
682ea4e54b9SDavid Nugent  */
683ea4e54b9SDavid Nugent 
684ea4e54b9SDavid Nugent static void
6852c9fd5f2SHajimu UMEMOTO inithosts(int family)
686ea4e54b9SDavid Nugent {
68755b54aa7SYaroslav Tykhiy 	int insert;
688737d08f3SYaroslav Tykhiy 	size_t len;
689ea4e54b9SDavid Nugent 	FILE *fp;
690737d08f3SYaroslav Tykhiy 	char *cp, *mp, *line;
691737d08f3SYaroslav Tykhiy 	char *hostname;
69255b54aa7SYaroslav Tykhiy 	char *vhost, *anonuser, *statfile, *welcome, *loginmsg;
693ea4e54b9SDavid Nugent 	struct ftphost *hrp, *lhrp;
6944dd8b5abSYoshinobu Inoue 	struct addrinfo hints, *res, *ai;
695ea4e54b9SDavid Nugent 
696ea4e54b9SDavid Nugent 	/*
697ea4e54b9SDavid Nugent 	 * Fill in the default host information
698ea4e54b9SDavid Nugent 	 */
699737d08f3SYaroslav Tykhiy 	if ((hostname = malloc(MAXHOSTNAMELEN)) == NULL)
700618b0bbaSMark Murray 		fatalerror("Ran out of memory.");
70104683b2cSYaroslav Tykhiy 	if (gethostname(hostname, MAXHOSTNAMELEN - 1) < 0)
702737d08f3SYaroslav Tykhiy 		hostname[0] = '\0';
703737d08f3SYaroslav Tykhiy 	hostname[MAXHOSTNAMELEN - 1] = '\0';
704737d08f3SYaroslav Tykhiy 	if ((hrp = malloc(sizeof(struct ftphost))) == NULL)
705737d08f3SYaroslav Tykhiy 		fatalerror("Ran out of memory.");
706737d08f3SYaroslav Tykhiy 	hrp->hostname = hostname;
707f38c6cadSYoshinobu Inoue 	hrp->hostinfo = NULL;
7084dd8b5abSYoshinobu Inoue 
7094dd8b5abSYoshinobu Inoue 	memset(&hints, 0, sizeof(hints));
7102c9fd5f2SHajimu UMEMOTO 	hints.ai_flags = AI_PASSIVE;
7112c9fd5f2SHajimu UMEMOTO 	hints.ai_family = family;
7122c9fd5f2SHajimu UMEMOTO 	hints.ai_socktype = SOCK_STREAM;
713b1d8d5cdSYaroslav Tykhiy 	if (getaddrinfo(hrp->hostname, NULL, &hints, &res) == 0)
714f38c6cadSYoshinobu Inoue 		hrp->hostinfo = res;
715ea4e54b9SDavid Nugent 	hrp->statfile = _PATH_FTPDSTATFILE;
716ea4e54b9SDavid Nugent 	hrp->welcome  = _PATH_FTPWELCOME;
717ea4e54b9SDavid Nugent 	hrp->loginmsg = _PATH_FTPLOGINMESG;
718ea4e54b9SDavid Nugent 	hrp->anonuser = "ftp";
719ea4e54b9SDavid Nugent 	hrp->next = NULL;
720ea4e54b9SDavid Nugent 	thishost = firsthost = lhrp = hrp;
721ea4e54b9SDavid Nugent 	if ((fp = fopen(_PATH_FTPHOSTS, "r")) != NULL) {
722ec009cf0SYaroslav Tykhiy 		int addrsize, gothost;
7234dd8b5abSYoshinobu Inoue 		void *addr;
7244dd8b5abSYoshinobu Inoue 		struct hostent *hp;
7254dd8b5abSYoshinobu Inoue 
726737d08f3SYaroslav Tykhiy 		while ((line = fgetln(fp, &len)) != NULL) {
7274dd8b5abSYoshinobu Inoue 			int	i, hp_error;
728ea4e54b9SDavid Nugent 
729737d08f3SYaroslav Tykhiy 			/* skip comments */
730737d08f3SYaroslav Tykhiy 			if (line[0] == '#')
731ea4e54b9SDavid Nugent 				continue;
732737d08f3SYaroslav Tykhiy 			if (line[len - 1] == '\n') {
733737d08f3SYaroslav Tykhiy 				line[len - 1] = '\0';
734737d08f3SYaroslav Tykhiy 				mp = NULL;
735737d08f3SYaroslav Tykhiy 			} else {
736737d08f3SYaroslav Tykhiy 				if ((mp = malloc(len + 1)) == NULL)
737737d08f3SYaroslav Tykhiy 					fatalerror("Ran out of memory.");
738737d08f3SYaroslav Tykhiy 				memcpy(mp, line, len);
739737d08f3SYaroslav Tykhiy 				mp[len] = '\0';
740737d08f3SYaroslav Tykhiy 				line = mp;
741ea4e54b9SDavid Nugent 			}
742ea4e54b9SDavid Nugent 			cp = strtok(line, " \t");
743737d08f3SYaroslav Tykhiy 			/* skip empty lines */
744737d08f3SYaroslav Tykhiy 			if (cp == NULL)
745737d08f3SYaroslav Tykhiy 				goto nextline;
74655b54aa7SYaroslav Tykhiy 			vhost = cp;
74755b54aa7SYaroslav Tykhiy 
74855b54aa7SYaroslav Tykhiy 			/* set defaults */
74955b54aa7SYaroslav Tykhiy 			anonuser = "ftp";
75055b54aa7SYaroslav Tykhiy 			statfile = _PATH_FTPDSTATFILE;
75155b54aa7SYaroslav Tykhiy 			welcome  = _PATH_FTPWELCOME;
75255b54aa7SYaroslav Tykhiy 			loginmsg = _PATH_FTPLOGINMESG;
75355b54aa7SYaroslav Tykhiy 
75455b54aa7SYaroslav Tykhiy 			/*
75555b54aa7SYaroslav Tykhiy 			 * Preparse the line so we can use its info
75655b54aa7SYaroslav Tykhiy 			 * for all the addresses associated with
75755b54aa7SYaroslav Tykhiy 			 * the virtual host name.
75855b54aa7SYaroslav Tykhiy 			 * Field 0, the virtual host name, is special:
75955b54aa7SYaroslav Tykhiy 			 * it's already parsed off and will be strdup'ed
76055b54aa7SYaroslav Tykhiy 			 * later, after we know its canonical form.
76155b54aa7SYaroslav Tykhiy 			 */
76255b54aa7SYaroslav Tykhiy 			for (i = 1; i < 5 && (cp = strtok(NULL, " \t")); i++)
76355b54aa7SYaroslav Tykhiy 				if (*cp != '-' && (cp = strdup(cp)))
76455b54aa7SYaroslav Tykhiy 					switch (i) {
76555b54aa7SYaroslav Tykhiy 					case 1:	/* anon user permissions */
76655b54aa7SYaroslav Tykhiy 						anonuser = cp;
76755b54aa7SYaroslav Tykhiy 						break;
76855b54aa7SYaroslav Tykhiy 					case 2: /* statistics file */
76955b54aa7SYaroslav Tykhiy 						statfile = cp;
77055b54aa7SYaroslav Tykhiy 						break;
77155b54aa7SYaroslav Tykhiy 					case 3: /* welcome message */
77255b54aa7SYaroslav Tykhiy 						welcome  = cp;
77355b54aa7SYaroslav Tykhiy 						break;
77455b54aa7SYaroslav Tykhiy 					case 4: /* login message */
77555b54aa7SYaroslav Tykhiy 						loginmsg = cp;
77655b54aa7SYaroslav Tykhiy 						break;
77755b54aa7SYaroslav Tykhiy 					default: /* programming error */
77855b54aa7SYaroslav Tykhiy 						abort();
77955b54aa7SYaroslav Tykhiy 						/* NOTREACHED */
78055b54aa7SYaroslav Tykhiy 					}
7814dd8b5abSYoshinobu Inoue 
7824dd8b5abSYoshinobu Inoue 			hints.ai_flags = AI_PASSIVE;
7832c9fd5f2SHajimu UMEMOTO 			hints.ai_family = family;
7842c9fd5f2SHajimu UMEMOTO 			hints.ai_socktype = SOCK_STREAM;
785b1d8d5cdSYaroslav Tykhiy 			if (getaddrinfo(vhost, NULL, &hints, &res) != 0)
786737d08f3SYaroslav Tykhiy 				goto nextline;
7874dd8b5abSYoshinobu Inoue 			for (ai = res; ai != NULL && ai->ai_addr != NULL;
788b535a9bfSDavid Nugent 			     ai = ai->ai_next) {
7894dd8b5abSYoshinobu Inoue 
790b535a9bfSDavid Nugent 			gothost = 0;
791ea4e54b9SDavid Nugent 			for (hrp = firsthost; hrp != NULL; hrp = hrp->next) {
792f38c6cadSYoshinobu Inoue 				struct addrinfo *hi;
793f38c6cadSYoshinobu Inoue 
794f38c6cadSYoshinobu Inoue 				for (hi = hrp->hostinfo; hi != NULL;
795f38c6cadSYoshinobu Inoue 				     hi = hi->ai_next)
796f38c6cadSYoshinobu Inoue 					if (hi->ai_addrlen == ai->ai_addrlen &&
797f38c6cadSYoshinobu Inoue 					    memcmp(hi->ai_addr,
7984dd8b5abSYoshinobu Inoue 						   ai->ai_addr,
799b535a9bfSDavid Nugent 						   ai->ai_addr->sa_len) == 0) {
800b535a9bfSDavid Nugent 						gothost++;
801b535a9bfSDavid Nugent 						break;
802b535a9bfSDavid Nugent 					}
803b535a9bfSDavid Nugent 				if (gothost)
804ea4e54b9SDavid Nugent 					break;
805ea4e54b9SDavid Nugent 			}
806ea4e54b9SDavid Nugent 			if (hrp == NULL) {
807ea4e54b9SDavid Nugent 				if ((hrp = malloc(sizeof(struct ftphost))) == NULL)
808737d08f3SYaroslav Tykhiy 					goto nextline;
809b1d8d5cdSYaroslav Tykhiy 				hrp->hostname = NULL;
81055b54aa7SYaroslav Tykhiy 				insert = 1;
811f2fe752dSYaroslav Tykhiy 			} else {
8121f75c13eSYaroslav Tykhiy 				if (hrp->hostinfo && hrp->hostinfo != res)
813b1d8d5cdSYaroslav Tykhiy 					freeaddrinfo(hrp->hostinfo);
814f2fe752dSYaroslav Tykhiy 				insert = 0; /* host already in the chain */
815f2fe752dSYaroslav Tykhiy 			}
816f38c6cadSYoshinobu Inoue 			hrp->hostinfo = res;
817f38c6cadSYoshinobu Inoue 
818ea4e54b9SDavid Nugent 			/*
819ea4e54b9SDavid Nugent 			 * determine hostname to use.
8204dd8b5abSYoshinobu Inoue 			 * force defined name if there is a valid alias
821ea4e54b9SDavid Nugent 			 * otherwise fallback to primary hostname
822ea4e54b9SDavid Nugent 			 */
8234dd8b5abSYoshinobu Inoue 			/* XXX: getaddrinfo() can't do alias check */
824f38c6cadSYoshinobu Inoue 			switch(hrp->hostinfo->ai_family) {
8254dd8b5abSYoshinobu Inoue 			case AF_INET:
8264b4cc4c6SYaroslav Tykhiy 				addr = &((struct sockaddr_in *)hrp->hostinfo->ai_addr)->sin_addr;
8274b4cc4c6SYaroslav Tykhiy 				addrsize = sizeof(struct in_addr);
8284dd8b5abSYoshinobu Inoue 				break;
8294dd8b5abSYoshinobu Inoue 			case AF_INET6:
8304b4cc4c6SYaroslav Tykhiy 				addr = &((struct sockaddr_in6 *)hrp->hostinfo->ai_addr)->sin6_addr;
8314b4cc4c6SYaroslav Tykhiy 				addrsize = sizeof(struct in6_addr);
8324dd8b5abSYoshinobu Inoue 				break;
8334dd8b5abSYoshinobu Inoue 			default:
8344dd8b5abSYoshinobu Inoue 				/* should not reach here */
835f38c6cadSYoshinobu Inoue 				freeaddrinfo(hrp->hostinfo);
836f2fe752dSYaroslav Tykhiy 				if (insert)
837f2fe752dSYaroslav Tykhiy 					free(hrp); /*not in chain, can free*/
838f2fe752dSYaroslav Tykhiy 				else
839f2fe752dSYaroslav Tykhiy 					hrp->hostinfo = NULL; /*mark as blank*/
840737d08f3SYaroslav Tykhiy 				goto nextline;
8414dd8b5abSYoshinobu Inoue 				/* NOTREACHED */
8424dd8b5abSYoshinobu Inoue 			}
84357d4ef07SYaroslav Tykhiy 			if ((hp = getipnodebyaddr(addr, addrsize,
844f38c6cadSYoshinobu Inoue 						  hrp->hostinfo->ai_family,
8454dd8b5abSYoshinobu Inoue 						  &hp_error)) != NULL) {
84655b54aa7SYaroslav Tykhiy 				if (strcmp(vhost, hp->h_name) != 0) {
847ea4e54b9SDavid Nugent 					if (hp->h_aliases == NULL)
84855b54aa7SYaroslav Tykhiy 						vhost = hp->h_name;
849ea4e54b9SDavid Nugent 					else {
850ea4e54b9SDavid Nugent 						i = 0;
851ea4e54b9SDavid Nugent 						while (hp->h_aliases[i] &&
85255b54aa7SYaroslav Tykhiy 						       strcmp(vhost, hp->h_aliases[i]) != 0)
853ea4e54b9SDavid Nugent 							++i;
854ea4e54b9SDavid Nugent 						if (hp->h_aliases[i] == NULL)
85555b54aa7SYaroslav Tykhiy 							vhost = hp->h_name;
856ea4e54b9SDavid Nugent 					}
857ea4e54b9SDavid Nugent 				}
858ea4e54b9SDavid Nugent 			}
859b1d8d5cdSYaroslav Tykhiy 			if (hrp->hostname &&
860b1d8d5cdSYaroslav Tykhiy 			    strcmp(hrp->hostname, vhost) != 0) {
861b1d8d5cdSYaroslav Tykhiy 				free(hrp->hostname);
862b1d8d5cdSYaroslav Tykhiy 				hrp->hostname = NULL;
863b1d8d5cdSYaroslav Tykhiy 			}
864b1d8d5cdSYaroslav Tykhiy 			if (hrp->hostname == NULL &&
865f2fe752dSYaroslav Tykhiy 			    (hrp->hostname = strdup(vhost)) == NULL) {
866f2fe752dSYaroslav Tykhiy 				freeaddrinfo(hrp->hostinfo);
867f2fe752dSYaroslav Tykhiy 				hrp->hostinfo = NULL; /* mark as blank */
868f2fe752dSYaroslav Tykhiy 				if (hp)
869f2fe752dSYaroslav Tykhiy 					freehostent(hp);
87055b54aa7SYaroslav Tykhiy 				goto nextline;
871f2fe752dSYaroslav Tykhiy 			}
87255b54aa7SYaroslav Tykhiy 			hrp->anonuser = anonuser;
87355b54aa7SYaroslav Tykhiy 			hrp->statfile = statfile;
87455b54aa7SYaroslav Tykhiy 			hrp->welcome  = welcome;
87555b54aa7SYaroslav Tykhiy 			hrp->loginmsg = loginmsg;
87655b54aa7SYaroslav Tykhiy 			if (insert) {
87755b54aa7SYaroslav Tykhiy 				hrp->next  = NULL;
87855b54aa7SYaroslav Tykhiy 				lhrp->next = hrp;
87955b54aa7SYaroslav Tykhiy 				lhrp = hrp;
88055b54aa7SYaroslav Tykhiy 			}
881233c0f66SYaroslav Tykhiy 			if (hp)
8824dd8b5abSYoshinobu Inoue 				freehostent(hp);
8834dd8b5abSYoshinobu Inoue 		      }
884737d08f3SYaroslav Tykhiy nextline:
885737d08f3SYaroslav Tykhiy 			if (mp)
886737d08f3SYaroslav Tykhiy 				free(mp);
887ea4e54b9SDavid Nugent 		}
888ea4e54b9SDavid Nugent 		(void) fclose(fp);
889ea4e54b9SDavid Nugent 	}
890ea4e54b9SDavid Nugent }
891ea4e54b9SDavid Nugent 
892ea4e54b9SDavid Nugent static void
893e4bc453cSWarner Losh selecthost(union sockunion *su)
894ea4e54b9SDavid Nugent {
895ea4e54b9SDavid Nugent 	struct ftphost	*hrp;
8964dd8b5abSYoshinobu Inoue 	u_int16_t port;
8974dd8b5abSYoshinobu Inoue #ifdef INET6
8984dd8b5abSYoshinobu Inoue 	struct in6_addr *mapped_in6 = NULL;
8994dd8b5abSYoshinobu Inoue #endif
900f38c6cadSYoshinobu Inoue 	struct addrinfo *hi;
9014dd8b5abSYoshinobu Inoue 
9024dd8b5abSYoshinobu Inoue #ifdef INET6
9034dd8b5abSYoshinobu Inoue 	/*
9044dd8b5abSYoshinobu Inoue 	 * XXX IPv4 mapped IPv6 addr consideraton,
9054dd8b5abSYoshinobu Inoue 	 * specified in rfc2373.
9064dd8b5abSYoshinobu Inoue 	 */
9074dd8b5abSYoshinobu Inoue 	if (su->su_family == AF_INET6 &&
9084dd8b5abSYoshinobu Inoue 	    IN6_IS_ADDR_V4MAPPED(&su->su_sin6.sin6_addr))
9094dd8b5abSYoshinobu Inoue 		mapped_in6 = &su->su_sin6.sin6_addr;
9104dd8b5abSYoshinobu Inoue #endif
911ea4e54b9SDavid Nugent 
912ea4e54b9SDavid Nugent 	hrp = thishost = firsthost;	/* default */
9134dd8b5abSYoshinobu Inoue 	port = su->su_port;
9144dd8b5abSYoshinobu Inoue 	su->su_port = 0;
915ea4e54b9SDavid Nugent 	while (hrp != NULL) {
916b535a9bfSDavid Nugent 	    for (hi = hrp->hostinfo; hi != NULL; hi = hi->ai_next) {
917f38c6cadSYoshinobu Inoue 		if (memcmp(su, hi->ai_addr, hi->ai_addrlen) == 0) {
918ea4e54b9SDavid Nugent 			thishost = hrp;
919ebd83647SYaroslav Tykhiy 			goto found;
920ea4e54b9SDavid Nugent 		}
9214dd8b5abSYoshinobu Inoue #ifdef INET6
9224dd8b5abSYoshinobu Inoue 		/* XXX IPv4 mapped IPv6 addr consideraton */
923f38c6cadSYoshinobu Inoue 		if (hi->ai_addr->sa_family == AF_INET && mapped_in6 != NULL &&
9244dd8b5abSYoshinobu Inoue 		    (memcmp(&mapped_in6->s6_addr[12],
925f38c6cadSYoshinobu Inoue 			    &((struct sockaddr_in *)hi->ai_addr)->sin_addr,
9264dd8b5abSYoshinobu Inoue 			    sizeof(struct in_addr)) == 0)) {
9274dd8b5abSYoshinobu Inoue 			thishost = hrp;
928ebd83647SYaroslav Tykhiy 			goto found;
9294dd8b5abSYoshinobu Inoue 		}
9304dd8b5abSYoshinobu Inoue #endif
931f38c6cadSYoshinobu Inoue 	    }
932ea4e54b9SDavid Nugent 	    hrp = hrp->next;
933ea4e54b9SDavid Nugent 	}
934ebd83647SYaroslav Tykhiy found:
9354dd8b5abSYoshinobu Inoue 	su->su_port = port;
936ea4e54b9SDavid Nugent 	/* setup static variables as appropriate */
937ea4e54b9SDavid Nugent 	hostname = thishost->hostname;
938ea4e54b9SDavid Nugent 	ftpuser = thishost->anonuser;
939ea4e54b9SDavid Nugent }
940ea4e54b9SDavid Nugent #endif
941ea4e54b9SDavid Nugent 
942ea022d16SRodney W. Grimes /*
943ea022d16SRodney W. Grimes  * Helper function for sgetpwnam().
944ea022d16SRodney W. Grimes  */
945ea022d16SRodney W. Grimes static char *
946e4bc453cSWarner Losh sgetsave(char *s)
947ea022d16SRodney W. Grimes {
948e3765043SYaroslav Tykhiy 	char *new = malloc(strlen(s) + 1);
949ea022d16SRodney W. Grimes 
950ea022d16SRodney W. Grimes 	if (new == NULL) {
95102c97492SYaroslav Tykhiy 		reply(421, "Ran out of memory.");
952ea022d16SRodney W. Grimes 		dologout(1);
953ea022d16SRodney W. Grimes 		/* NOTREACHED */
954ea022d16SRodney W. Grimes 	}
955ea022d16SRodney W. Grimes 	(void) strcpy(new, s);
956ea022d16SRodney W. Grimes 	return (new);
957ea022d16SRodney W. Grimes }
958ea022d16SRodney W. Grimes 
959ea022d16SRodney W. Grimes /*
960ea022d16SRodney W. Grimes  * Save the result of a getpwnam.  Used for USER command, since
961ea022d16SRodney W. Grimes  * the data returned must not be clobbered by any other command
962ea022d16SRodney W. Grimes  * (e.g., globbing).
963c29b9b47SYaroslav Tykhiy  * NB: The data returned by sgetpwnam() will remain valid until
964c29b9b47SYaroslav Tykhiy  * the next call to this function.  Its difference from getpwnam()
965c29b9b47SYaroslav Tykhiy  * is that sgetpwnam() is known to be called from ftpd code only.
966ea022d16SRodney W. Grimes  */
967ea022d16SRodney W. Grimes static struct passwd *
968e4bc453cSWarner Losh sgetpwnam(char *name)
969ea022d16SRodney W. Grimes {
970ea022d16SRodney W. Grimes 	static struct passwd save;
971ea022d16SRodney W. Grimes 	struct passwd *p;
972ea022d16SRodney W. Grimes 
973ea022d16SRodney W. Grimes 	if ((p = getpwnam(name)) == NULL)
974ea022d16SRodney W. Grimes 		return (p);
975ea022d16SRodney W. Grimes 	if (save.pw_name) {
976ea022d16SRodney W. Grimes 		free(save.pw_name);
977ea022d16SRodney W. Grimes 		free(save.pw_passwd);
97803d34cccSChristian Brueffer 		free(save.pw_class);
979ea022d16SRodney W. Grimes 		free(save.pw_gecos);
980ea022d16SRodney W. Grimes 		free(save.pw_dir);
981ea022d16SRodney W. Grimes 		free(save.pw_shell);
982ea022d16SRodney W. Grimes 	}
983ea022d16SRodney W. Grimes 	save = *p;
984ea022d16SRodney W. Grimes 	save.pw_name = sgetsave(p->pw_name);
985ea022d16SRodney W. Grimes 	save.pw_passwd = sgetsave(p->pw_passwd);
98603d34cccSChristian Brueffer 	save.pw_class = sgetsave(p->pw_class);
987ea022d16SRodney W. Grimes 	save.pw_gecos = sgetsave(p->pw_gecos);
988ea022d16SRodney W. Grimes 	save.pw_dir = sgetsave(p->pw_dir);
989ea022d16SRodney W. Grimes 	save.pw_shell = sgetsave(p->pw_shell);
990ea022d16SRodney W. Grimes 	return (&save);
991ea022d16SRodney W. Grimes }
992ea022d16SRodney W. Grimes 
993ea022d16SRodney W. Grimes static int login_attempts;	/* number of failed login attempts */
994ea022d16SRodney W. Grimes static int askpasswd;		/* had user command, ask for passwd */
99590906a46SSheldon Hearn static char curname[MAXLOGNAME];	/* current USER name */
996ea022d16SRodney W. Grimes 
997ea022d16SRodney W. Grimes /*
998ea022d16SRodney W. Grimes  * USER command.
999ea022d16SRodney W. Grimes  * Sets global passwd pointer pw if named account exists and is acceptable;
1000ea022d16SRodney W. Grimes  * sets askpasswd if a PASS command is expected.  If logged in previously,
1001ea022d16SRodney W. Grimes  * need to reset state.  If name is "ftp" or "anonymous", the name is not in
1002ea022d16SRodney W. Grimes  * _PATH_FTPUSERS, and ftp account exists, set guest and pw, then just return.
1003ea022d16SRodney W. Grimes  * If account doesn't exist, ask for passwd anyway.  Otherwise, check user
1004ea022d16SRodney W. Grimes  * requesting login privileges.  Disallow anyone who does not have a standard
1005ea022d16SRodney W. Grimes  * shell as returned by getusershell().  Disallow anyone mentioned in the file
1006ea022d16SRodney W. Grimes  * _PATH_FTPUSERS to allow people such as root and uucp to be avoided.
1007ea022d16SRodney W. Grimes  */
1008ea022d16SRodney W. Grimes void
1009e4bc453cSWarner Losh user(char *name)
1010ea022d16SRodney W. Grimes {
1011cefb6785SChristian S.J. Peron 	int ecode;
1012ea022d16SRodney W. Grimes 	char *cp, *shell;
1013ea022d16SRodney W. Grimes 
1014ea022d16SRodney W. Grimes 	if (logged_in) {
1015ea022d16SRodney W. Grimes 		if (guest) {
1016ea022d16SRodney W. Grimes 			reply(530, "Can't change user from guest login.");
1017ea022d16SRodney W. Grimes 			return;
1018a5a4544eSPaul Traina 		} else if (dochroot) {
1019a5a4544eSPaul Traina 			reply(530, "Can't change user from chroot user.");
1020a5a4544eSPaul Traina 			return;
1021ea022d16SRodney W. Grimes 		}
1022ea022d16SRodney W. Grimes 		end_login();
1023ea022d16SRodney W. Grimes 	}
1024ea022d16SRodney W. Grimes 
1025ea022d16SRodney W. Grimes 	guest = 0;
102663047c6fSDavid E. O'Brien #ifdef VIRTUAL_HOSTING
102763047c6fSDavid E. O'Brien 	pw = sgetpwnam(thishost->anonuser);
102863047c6fSDavid E. O'Brien #else
102963047c6fSDavid E. O'Brien 	pw = sgetpwnam("ftp");
103063047c6fSDavid E. O'Brien #endif
1031ea022d16SRodney W. Grimes 	if (strcmp(name, "ftp") == 0 || strcmp(name, "anonymous") == 0) {
1032cefb6785SChristian S.J. Peron 		if (checkuser(_PATH_FTPUSERS, "ftp", 0, NULL, &ecode) ||
1033cefb6785SChristian S.J. Peron 		    (ecode != 0 && ecode != ENOENT))
1034cefb6785SChristian S.J. Peron 			reply(530, "User %s access denied.", name);
1035cefb6785SChristian S.J. Peron 		else if (checkuser(_PATH_FTPUSERS, "anonymous", 0, NULL, &ecode) ||
1036cefb6785SChristian S.J. Peron 		    (ecode != 0 && ecode != ENOENT))
1037ea022d16SRodney W. Grimes 			reply(530, "User %s access denied.", name);
103863047c6fSDavid E. O'Brien 		else if (pw != NULL) {
1039ea022d16SRodney W. Grimes 			guest = 1;
1040ea022d16SRodney W. Grimes 			askpasswd = 1;
1041ea022d16SRodney W. Grimes 			reply(331,
10422c60c54cSPaul Traina 			"Guest login ok, send your email address as password.");
1043ea022d16SRodney W. Grimes 		} else
1044ea022d16SRodney W. Grimes 			reply(530, "User %s unknown.", name);
1045ea022d16SRodney W. Grimes 		if (!askpasswd && logging)
1046ea022d16SRodney W. Grimes 			syslog(LOG_NOTICE,
1047ea022d16SRodney W. Grimes 			    "ANONYMOUS FTP LOGIN REFUSED FROM %s", remotehost);
1048ea022d16SRodney W. Grimes 		return;
1049ea022d16SRodney W. Grimes 	}
10505a392aecSTorsten Blum 	if (anon_only != 0) {
10515a392aecSTorsten Blum 		reply(530, "Sorry, only anonymous ftp allowed.");
10525a392aecSTorsten Blum 		return;
10535a392aecSTorsten Blum 	}
10545a392aecSTorsten Blum 
10559aca17cbSMark Murray 	if ((pw = sgetpwnam(name))) {
1056ea022d16SRodney W. Grimes 		if ((shell = pw->pw_shell) == NULL || *shell == 0)
1057ea022d16SRodney W. Grimes 			shell = _PATH_BSHELL;
1058c433c9daSPhilippe Charnier 		setusershell();
1059ea022d16SRodney W. Grimes 		while ((cp = getusershell()) != NULL)
1060ea022d16SRodney W. Grimes 			if (strcmp(cp, shell) == 0)
1061ea022d16SRodney W. Grimes 				break;
1062ea022d16SRodney W. Grimes 		endusershell();
1063ea022d16SRodney W. Grimes 
1064cefb6785SChristian S.J. Peron 		if (cp == NULL ||
1065cefb6785SChristian S.J. Peron 		    (checkuser(_PATH_FTPUSERS, name, 1, NULL, &ecode) ||
1066cefb6785SChristian S.J. Peron 		    (ecode != 0 && ecode != ENOENT))) {
1067ea022d16SRodney W. Grimes 			reply(530, "User %s access denied.", name);
1068ea022d16SRodney W. Grimes 			if (logging)
1069ea022d16SRodney W. Grimes 				syslog(LOG_NOTICE,
1070ea022d16SRodney W. Grimes 				    "FTP LOGIN REFUSED FROM %s, %s",
1071ea022d16SRodney W. Grimes 				    remotehost, name);
10727e295315SYaroslav Tykhiy 			pw = NULL;
1073ea022d16SRodney W. Grimes 			return;
1074ea022d16SRodney W. Grimes 		}
1075ea022d16SRodney W. Grimes 	}
1076ea022d16SRodney W. Grimes 	if (logging)
1077ea022d16SRodney W. Grimes 		strncpy(curname, name, sizeof(curname)-1);
107847499ecdSAndrey A. Chernov 
107947499ecdSAndrey A. Chernov 	pwok = 0;
1080fa1746c9SMark Murray #ifdef USE_PAM
1081fa1746c9SMark Murray 	/* XXX Kluge! The conversation mechanism needs to be fixed. */
1082726040deSGuido van Rooij #endif
108347499ecdSAndrey A. Chernov 	if (opiechallenge(&opiedata, name, opieprompt) == 0) {
108447499ecdSAndrey A. Chernov 		pwok = (pw != NULL) &&
108547499ecdSAndrey A. Chernov 		       opieaccessfile(remotehost) &&
108647499ecdSAndrey A. Chernov 		       opiealways(pw->pw_dir);
108747499ecdSAndrey A. Chernov 		reply(331, "Response to %s %s for %s.",
108847499ecdSAndrey A. Chernov 		      opieprompt, pwok ? "requested" : "required", name);
108947499ecdSAndrey A. Chernov 	} else {
109047499ecdSAndrey A. Chernov 		pwok = 1;
109147499ecdSAndrey A. Chernov 		reply(331, "Password required for %s.", name);
109247499ecdSAndrey A. Chernov 	}
1093ea022d16SRodney W. Grimes 	askpasswd = 1;
1094ea022d16SRodney W. Grimes 	/*
1095ea022d16SRodney W. Grimes 	 * Delay before reading passwd after first failed
1096ea022d16SRodney W. Grimes 	 * attempt to slow down passwd-guessing programs.
1097ea022d16SRodney W. Grimes 	 */
1098ea022d16SRodney W. Grimes 	if (login_attempts)
1099e3765043SYaroslav Tykhiy 		sleep(login_attempts);
1100ea022d16SRodney W. Grimes }
1101ea022d16SRodney W. Grimes 
1102ea022d16SRodney W. Grimes /*
11038657b576SYaroslav Tykhiy  * Check if a user is in the file "fname",
11048657b576SYaroslav Tykhiy  * return a pointer to a malloc'd string with the rest
11058657b576SYaroslav Tykhiy  * of the matching line in "residue" if not NULL.
1106ea022d16SRodney W. Grimes  */
1107ea022d16SRodney W. Grimes static int
1108cefb6785SChristian S.J. Peron checkuser(char *fname, char *name, int pwset, char **residue, int *ecode)
1109ea022d16SRodney W. Grimes {
1110ea022d16SRodney W. Grimes 	FILE *fd;
1111ea022d16SRodney W. Grimes 	int found = 0;
1112737d08f3SYaroslav Tykhiy 	size_t len;
1113737d08f3SYaroslav Tykhiy 	char *line, *mp, *p;
1114ea022d16SRodney W. Grimes 
1115cefb6785SChristian S.J. Peron 	if (ecode != NULL)
1116cefb6785SChristian S.J. Peron 		*ecode = 0;
1117a5a4544eSPaul Traina 	if ((fd = fopen(fname, "r")) != NULL) {
1118737d08f3SYaroslav Tykhiy 		while (!found && (line = fgetln(fd, &len)) != NULL) {
1119737d08f3SYaroslav Tykhiy 			/* skip comments */
1120ea022d16SRodney W. Grimes 			if (line[0] == '#')
1121ea022d16SRodney W. Grimes 				continue;
1122737d08f3SYaroslav Tykhiy 			if (line[len - 1] == '\n') {
1123737d08f3SYaroslav Tykhiy 				line[len - 1] = '\0';
1124737d08f3SYaroslav Tykhiy 				mp = NULL;
1125737d08f3SYaroslav Tykhiy 			} else {
1126737d08f3SYaroslav Tykhiy 				if ((mp = malloc(len + 1)) == NULL)
1127737d08f3SYaroslav Tykhiy 					fatalerror("Ran out of memory.");
1128737d08f3SYaroslav Tykhiy 				memcpy(mp, line, len);
1129737d08f3SYaroslav Tykhiy 				mp[len] = '\0';
1130737d08f3SYaroslav Tykhiy 				line = mp;
1131737d08f3SYaroslav Tykhiy 			}
1132737d08f3SYaroslav Tykhiy 			/* avoid possible leading and trailing whitespace */
1133737d08f3SYaroslav Tykhiy 			p = strtok(line, " \t");
1134737d08f3SYaroslav Tykhiy 			/* skip empty lines */
1135737d08f3SYaroslav Tykhiy 			if (p == NULL)
1136737d08f3SYaroslav Tykhiy 				goto nextline;
113731fea7b8SDavid Nugent 			/*
113831fea7b8SDavid Nugent 			 * if first chr is '@', check group membership
113931fea7b8SDavid Nugent 			 */
1140737d08f3SYaroslav Tykhiy 			if (p[0] == '@') {
114131fea7b8SDavid Nugent 				int i = 0;
114231fea7b8SDavid Nugent 				struct group *grp;
114331fea7b8SDavid Nugent 
11448657b576SYaroslav Tykhiy 				if (p[1] == '\0') /* single @ matches anyone */
11458657b576SYaroslav Tykhiy 					found = 1;
11468657b576SYaroslav Tykhiy 				else {
1147737d08f3SYaroslav Tykhiy 					if ((grp = getgrnam(p+1)) == NULL)
1148737d08f3SYaroslav Tykhiy 						goto nextline;
11497edcb936SSteve Price 					/*
11507edcb936SSteve Price 					 * Check user's default group
11517edcb936SSteve Price 					 */
11527edcb936SSteve Price 					if (pwset && grp->gr_gid == pw->pw_gid)
11537edcb936SSteve Price 						found = 1;
11547edcb936SSteve Price 					/*
11557edcb936SSteve Price 					 * Check supplementary groups
11567edcb936SSteve Price 					 */
115731fea7b8SDavid Nugent 					while (!found && grp->gr_mem[i])
115831fea7b8SDavid Nugent 						found = strcmp(name,
115931fea7b8SDavid Nugent 							grp->gr_mem[i++])
116031fea7b8SDavid Nugent 							== 0;
1161ea022d16SRodney W. Grimes 				}
11628657b576SYaroslav Tykhiy 			}
116331fea7b8SDavid Nugent 			/*
116431fea7b8SDavid Nugent 			 * Otherwise, just check for username match
116531fea7b8SDavid Nugent 			 */
116631fea7b8SDavid Nugent 			else
1167737d08f3SYaroslav Tykhiy 				found = strcmp(p, name) == 0;
11688657b576SYaroslav Tykhiy 			/*
11698657b576SYaroslav Tykhiy 			 * Save the rest of line to "residue" if matched
11708657b576SYaroslav Tykhiy 			 */
11718657b576SYaroslav Tykhiy 			if (found && residue) {
11720ba71e24SYaroslav Tykhiy 				if ((p = strtok(NULL, "")) != NULL)
11730ba71e24SYaroslav Tykhiy 					p += strspn(p, " \t");
11740ba71e24SYaroslav Tykhiy 				if (p && *p) {
11758657b576SYaroslav Tykhiy 				 	if ((*residue = strdup(p)) == NULL)
11768657b576SYaroslav Tykhiy 						fatalerror("Ran out of memory.");
11778657b576SYaroslav Tykhiy 				} else
11788657b576SYaroslav Tykhiy 					*residue = NULL;
11798657b576SYaroslav Tykhiy 			}
1180737d08f3SYaroslav Tykhiy nextline:
1181737d08f3SYaroslav Tykhiy 			if (mp)
1182737d08f3SYaroslav Tykhiy 				free(mp);
1183ea022d16SRodney W. Grimes 		}
1184ea022d16SRodney W. Grimes 		(void) fclose(fd);
1185cefb6785SChristian S.J. Peron 	} else if (ecode != NULL)
1186cefb6785SChristian S.J. Peron 		*ecode = errno;
1187ea022d16SRodney W. Grimes 	return (found);
1188ea022d16SRodney W. Grimes }
1189ea022d16SRodney W. Grimes 
1190ea022d16SRodney W. Grimes /*
1191ea022d16SRodney W. Grimes  * Terminate login as previous user, if any, resetting state;
1192ea022d16SRodney W. Grimes  * used when USER command is given or login fails.
1193ea022d16SRodney W. Grimes  */
1194ea022d16SRodney W. Grimes static void
1195e4bc453cSWarner Losh end_login(void)
1196ea022d16SRodney W. Grimes {
11975bc9d93dSMark Murray #ifdef USE_PAM
11985bc9d93dSMark Murray 	int e;
11995bc9d93dSMark Murray #endif
1200ea022d16SRodney W. Grimes 
120152e7ee74SYaroslav Tykhiy 	(void) seteuid(0);
12029f37b1a2SEd Schouten 	if (logged_in && dowtmp)
12039f37b1a2SEd Schouten 		ftpd_logwtmp(wtmpid, NULL, NULL);
1204ea022d16SRodney W. Grimes 	pw = NULL;
1205b071c689SDavid Nugent #ifdef	LOGIN_CAP
1206e81b1c71SEdward Tomasz Napierala 	setusercontext(NULL, getpwuid(0), 0, LOGIN_SETALL & ~(LOGIN_SETLOGIN |
1207e81b1c71SEdward Tomasz Napierala 		       LOGIN_SETUSER | LOGIN_SETGROUP | LOGIN_SETPATH |
1208e81b1c71SEdward Tomasz Napierala 		       LOGIN_SETENV));
1209b071c689SDavid Nugent #endif
12105bc9d93dSMark Murray #ifdef USE_PAM
1211de45162dSYaroslav Tykhiy 	if (pamh) {
12125bc9d93dSMark Murray 		if ((e = pam_setcred(pamh, PAM_DELETE_CRED)) != PAM_SUCCESS)
12135bc9d93dSMark Murray 			syslog(LOG_ERR, "pam_setcred: %s", pam_strerror(pamh, e));
12145bc9d93dSMark Murray 		if ((e = pam_close_session(pamh,0)) != PAM_SUCCESS)
12155bc9d93dSMark Murray 			syslog(LOG_ERR, "pam_close_session: %s", pam_strerror(pamh, e));
12165bc9d93dSMark Murray 		if ((e = pam_end(pamh, e)) != PAM_SUCCESS)
12175bc9d93dSMark Murray 			syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e));
12185bc9d93dSMark Murray 		pamh = NULL;
1219de45162dSYaroslav Tykhiy 	}
12205bc9d93dSMark Murray #endif
1221ea022d16SRodney W. Grimes 	logged_in = 0;
1222ea022d16SRodney W. Grimes 	guest = 0;
1223a5a4544eSPaul Traina 	dochroot = 0;
1224ea022d16SRodney W. Grimes }
1225ea022d16SRodney W. Grimes 
12265bc9d93dSMark Murray #ifdef USE_PAM
12276c9134c0SMark Murray 
12286c9134c0SMark Murray /*
12296c9134c0SMark Murray  * the following code is stolen from imap-uw PAM authentication module and
12306c9134c0SMark Murray  * login.c
12316c9134c0SMark Murray  */
12326c9134c0SMark Murray #define COPY_STRING(s) (s ? strdup(s) : NULL)
12336c9134c0SMark Murray 
12346c9134c0SMark Murray struct cred_t {
12356c9134c0SMark Murray 	const char *uname;		/* user name */
12366c9134c0SMark Murray 	const char *pass;		/* password */
12376c9134c0SMark Murray };
12386c9134c0SMark Murray typedef struct cred_t cred_t;
12396c9134c0SMark Murray 
12406c9134c0SMark Murray static int
12416c9134c0SMark Murray auth_conv(int num_msg, const struct pam_message **msg,
12426c9134c0SMark Murray 	  struct pam_response **resp, void *appdata)
12436c9134c0SMark Murray {
12446c9134c0SMark Murray 	int i;
12456c9134c0SMark Murray 	cred_t *cred = (cred_t *) appdata;
124660769b19SDag-Erling Smørgrav 	struct pam_response *reply;
124760769b19SDag-Erling Smørgrav 
124860769b19SDag-Erling Smørgrav 	reply = calloc(num_msg, sizeof *reply);
124960769b19SDag-Erling Smørgrav 	if (reply == NULL)
125060769b19SDag-Erling Smørgrav 		return PAM_BUF_ERR;
12516c9134c0SMark Murray 
12526c9134c0SMark Murray 	for (i = 0; i < num_msg; i++) {
12536c9134c0SMark Murray 		switch (msg[i]->msg_style) {
12546c9134c0SMark Murray 		case PAM_PROMPT_ECHO_ON:	/* assume want user name */
12556c9134c0SMark Murray 			reply[i].resp_retcode = PAM_SUCCESS;
12566c9134c0SMark Murray 			reply[i].resp = COPY_STRING(cred->uname);
12576c9134c0SMark Murray 			/* PAM frees resp. */
12586c9134c0SMark Murray 			break;
12596c9134c0SMark Murray 		case PAM_PROMPT_ECHO_OFF:	/* assume want password */
12606c9134c0SMark Murray 			reply[i].resp_retcode = PAM_SUCCESS;
12616c9134c0SMark Murray 			reply[i].resp = COPY_STRING(cred->pass);
12626c9134c0SMark Murray 			/* PAM frees resp. */
12636c9134c0SMark Murray 			break;
12646c9134c0SMark Murray 		case PAM_TEXT_INFO:
12656c9134c0SMark Murray 		case PAM_ERROR_MSG:
12666c9134c0SMark Murray 			reply[i].resp_retcode = PAM_SUCCESS;
12676c9134c0SMark Murray 			reply[i].resp = NULL;
12686c9134c0SMark Murray 			break;
12696c9134c0SMark Murray 		default:			/* unknown message style */
12706c9134c0SMark Murray 			free(reply);
12716c9134c0SMark Murray 			return PAM_CONV_ERR;
12726c9134c0SMark Murray 		}
12736c9134c0SMark Murray 	}
12746c9134c0SMark Murray 
12756c9134c0SMark Murray 	*resp = reply;
12766c9134c0SMark Murray 	return PAM_SUCCESS;
12776c9134c0SMark Murray }
12786c9134c0SMark Murray 
12796c9134c0SMark Murray /*
12806c9134c0SMark Murray  * Attempt to authenticate the user using PAM.  Returns 0 if the user is
12816c9134c0SMark Murray  * authenticated, or 1 if not authenticated.  If some sort of PAM system
12826c9134c0SMark Murray  * error occurs (e.g., the "/etc/pam.conf" file is missing) then this
12836c9134c0SMark Murray  * function returns -1.  This can be used as an indication that we should
12846c9134c0SMark Murray  * fall back to a different authentication mechanism.
12856c9134c0SMark Murray  */
12866c9134c0SMark Murray static int
12876c9134c0SMark Murray auth_pam(struct passwd **ppw, const char *pass)
12886c9134c0SMark Murray {
12896c9134c0SMark Murray 	const char *tmpl_user;
12906c9134c0SMark Murray 	const void *item;
12916c9134c0SMark Murray 	int rval;
12926c9134c0SMark Murray 	int e;
12936c9134c0SMark Murray 	cred_t auth_cred = { (*ppw)->pw_name, pass };
12946c9134c0SMark Murray 	struct pam_conv conv = { &auth_conv, &auth_cred };
12956c9134c0SMark Murray 
12966c9134c0SMark Murray 	e = pam_start("ftpd", (*ppw)->pw_name, &conv, &pamh);
12976c9134c0SMark Murray 	if (e != PAM_SUCCESS) {
1298545ea864SYaroslav Tykhiy 		/*
1299545ea864SYaroslav Tykhiy 		 * In OpenPAM, it's OK to pass NULL to pam_strerror()
1300545ea864SYaroslav Tykhiy 		 * if context creation has failed in the first place.
1301545ea864SYaroslav Tykhiy 		 */
1302545ea864SYaroslav Tykhiy 		syslog(LOG_ERR, "pam_start: %s", pam_strerror(NULL, e));
13036c9134c0SMark Murray 		return -1;
13046c9134c0SMark Murray 	}
13056c9134c0SMark Murray 
1306ea413ab7SGuido van Rooij 	e = pam_set_item(pamh, PAM_RHOST, remotehost);
1307ea413ab7SGuido van Rooij 	if (e != PAM_SUCCESS) {
1308ea413ab7SGuido van Rooij 		syslog(LOG_ERR, "pam_set_item(PAM_RHOST): %s",
1309ea413ab7SGuido van Rooij 			pam_strerror(pamh, e));
1310de45162dSYaroslav Tykhiy 		if ((e = pam_end(pamh, e)) != PAM_SUCCESS) {
1311de45162dSYaroslav Tykhiy 			syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e));
1312de45162dSYaroslav Tykhiy 		}
1313de45162dSYaroslav Tykhiy 		pamh = NULL;
1314ea413ab7SGuido van Rooij 		return -1;
1315ea413ab7SGuido van Rooij 	}
1316ea413ab7SGuido van Rooij 
13176c9134c0SMark Murray 	e = pam_authenticate(pamh, 0);
13186c9134c0SMark Murray 	switch (e) {
13196c9134c0SMark Murray 	case PAM_SUCCESS:
13206c9134c0SMark Murray 		/*
13216c9134c0SMark Murray 		 * With PAM we support the concept of a "template"
13226c9134c0SMark Murray 		 * user.  The user enters a login name which is
13236c9134c0SMark Murray 		 * authenticated by PAM, usually via a remote service
13246c9134c0SMark Murray 		 * such as RADIUS or TACACS+.  If authentication
13256c9134c0SMark Murray 		 * succeeds, a different but related "template" name
13266c9134c0SMark Murray 		 * is used for setting the credentials, shell, and
13276c9134c0SMark Murray 		 * home directory.  The name the user enters need only
13286c9134c0SMark Murray 		 * exist on the remote authentication server, but the
13296c9134c0SMark Murray 		 * template name must be present in the local password
13306c9134c0SMark Murray 		 * database.
13316c9134c0SMark Murray 		 *
13326c9134c0SMark Murray 		 * This is supported by two various mechanisms in the
13336c9134c0SMark Murray 		 * individual modules.  However, from the application's
13346c9134c0SMark Murray 		 * point of view, the template user is always passed
13356c9134c0SMark Murray 		 * back as a changed value of the PAM_USER item.
13366c9134c0SMark Murray 		 */
13376c9134c0SMark Murray 		if ((e = pam_get_item(pamh, PAM_USER, &item)) ==
13386c9134c0SMark Murray 		    PAM_SUCCESS) {
13396c9134c0SMark Murray 			tmpl_user = (const char *) item;
13406c9134c0SMark Murray 			if (strcmp((*ppw)->pw_name, tmpl_user) != 0)
13416c9134c0SMark Murray 				*ppw = getpwnam(tmpl_user);
13426c9134c0SMark Murray 		} else
13436c9134c0SMark Murray 			syslog(LOG_ERR, "Couldn't get PAM_USER: %s",
13446c9134c0SMark Murray 			    pam_strerror(pamh, e));
13456c9134c0SMark Murray 		rval = 0;
13466c9134c0SMark Murray 		break;
13476c9134c0SMark Murray 
13486c9134c0SMark Murray 	case PAM_AUTH_ERR:
13496c9134c0SMark Murray 	case PAM_USER_UNKNOWN:
13506c9134c0SMark Murray 	case PAM_MAXTRIES:
13516c9134c0SMark Murray 		rval = 1;
13526c9134c0SMark Murray 		break;
13536c9134c0SMark Murray 
13546c9134c0SMark Murray 	default:
13555bc9d93dSMark Murray 		syslog(LOG_ERR, "pam_authenticate: %s", pam_strerror(pamh, e));
13566c9134c0SMark Murray 		rval = -1;
13576c9134c0SMark Murray 		break;
13586c9134c0SMark Murray 	}
13596c9134c0SMark Murray 
13605bc9d93dSMark Murray 	if (rval == 0) {
13615bc9d93dSMark Murray 		e = pam_acct_mgmt(pamh, 0);
13625bc9d93dSMark Murray 		if (e != PAM_SUCCESS) {
13634cbc4ad6SYaroslav Tykhiy 			syslog(LOG_ERR, "pam_acct_mgmt: %s",
13644cbc4ad6SYaroslav Tykhiy 						pam_strerror(pamh, e));
13655bc9d93dSMark Murray 			rval = 1;
13665bc9d93dSMark Murray 		}
13675bc9d93dSMark Murray 	}
13685bc9d93dSMark Murray 
13695bc9d93dSMark Murray 	if (rval != 0) {
13706c9134c0SMark Murray 		if ((e = pam_end(pamh, e)) != PAM_SUCCESS) {
13716c9134c0SMark Murray 			syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e));
13725bc9d93dSMark Murray 		}
13735bc9d93dSMark Murray 		pamh = NULL;
13746c9134c0SMark Murray 	}
13756c9134c0SMark Murray 	return rval;
13766c9134c0SMark Murray }
13776c9134c0SMark Murray 
13785bc9d93dSMark Murray #endif /* USE_PAM */
13796c9134c0SMark Murray 
1380ea022d16SRodney W. Grimes void
1381e4bc453cSWarner Losh pass(char *passwd)
1382ea022d16SRodney W. Grimes {
1383cefb6785SChristian S.J. Peron 	int rval, ecode;
1384ea022d16SRodney W. Grimes 	FILE *fd;
1385b071c689SDavid Nugent #ifdef	LOGIN_CAP
1386b071c689SDavid Nugent 	login_cap_t *lc = NULL;
1387b071c689SDavid Nugent #endif
13885bc9d93dSMark Murray #ifdef USE_PAM
13895bc9d93dSMark Murray 	int e;
13905bc9d93dSMark Murray #endif
1391341e476eSYaroslav Tykhiy 	char *residue = NULL;
139247499ecdSAndrey A. Chernov 	char *xpasswd;
1393ea022d16SRodney W. Grimes 
1394ea022d16SRodney W. Grimes 	if (logged_in || askpasswd == 0) {
1395ea022d16SRodney W. Grimes 		reply(503, "Login with USER first.");
1396ea022d16SRodney W. Grimes 		return;
1397ea022d16SRodney W. Grimes 	}
1398ea022d16SRodney W. Grimes 	askpasswd = 0;
1399ea022d16SRodney W. Grimes 	if (!guest) {		/* "ftp" is only account allowed no password */
1400a5a4544eSPaul Traina 		if (pw == NULL) {
1401a5a4544eSPaul Traina 			rval = 1;	/* failure below */
1402a5a4544eSPaul Traina 			goto skip;
1403a5a4544eSPaul Traina 		}
14045bc9d93dSMark Murray #ifdef USE_PAM
14056c9134c0SMark Murray 		rval = auth_pam(&pw, passwd);
1406f650a124SAndrey A. Chernov 		if (rval >= 0) {
1407f650a124SAndrey A. Chernov 			opieunlock();
1408a5a4544eSPaul Traina 			goto skip;
1409f650a124SAndrey A. Chernov 		}
1410f650a124SAndrey A. Chernov #endif
141147499ecdSAndrey A. Chernov 		if (opieverify(&opiedata, passwd) == 0)
141247499ecdSAndrey A. Chernov 			xpasswd = pw->pw_passwd;
1413f650a124SAndrey A. Chernov 		else if (pwok) {
141447499ecdSAndrey A. Chernov 			xpasswd = crypt(passwd, pw->pw_passwd);
1415f650a124SAndrey A. Chernov 			if (passwd[0] == '\0' && pw->pw_passwd[0] != '\0')
1416f650a124SAndrey A. Chernov 				xpasswd = ":";
1417f650a124SAndrey A. Chernov 		} else {
141847499ecdSAndrey A. Chernov 			rval = 1;
141947499ecdSAndrey A. Chernov 			goto skip;
142047499ecdSAndrey A. Chernov 		}
142147499ecdSAndrey A. Chernov 		rval = strcmp(pw->pw_passwd, xpasswd);
1422f650a124SAndrey A. Chernov 		if (pw->pw_expire && time(NULL) >= pw->pw_expire)
1423a5a4544eSPaul Traina 			rval = 1;	/* failure */
1424a5a4544eSPaul Traina skip:
1425a5a4544eSPaul Traina 		/*
1426a5a4544eSPaul Traina 		 * If rval == 1, the user failed the authentication check
14276c9134c0SMark Murray 		 * above.  If rval == 0, either PAM or local authentication
1428a5a4544eSPaul Traina 		 * succeeded.
1429a5a4544eSPaul Traina 		 */
1430a5a4544eSPaul Traina 		if (rval) {
1431ea022d16SRodney W. Grimes 			reply(530, "Login incorrect.");
1432*e07d11b6SKurt Lidl 			BLACKLIST_NOTIFY(BLACKLIST_AUTH_FAIL, STDIN_FILENO, "Login incorrect");
1433b8939f6fSYaroslav Tykhiy 			if (logging) {
1434ea022d16SRodney W. Grimes 				syslog(LOG_NOTICE,
1435b8939f6fSYaroslav Tykhiy 				    "FTP LOGIN FAILED FROM %s",
1436b8939f6fSYaroslav Tykhiy 				    remotehost);
1437b8939f6fSYaroslav Tykhiy 				syslog(LOG_AUTHPRIV | LOG_NOTICE,
1438ea022d16SRodney W. Grimes 				    "FTP LOGIN FAILED FROM %s, %s",
1439ea022d16SRodney W. Grimes 				    remotehost, curname);
1440b8939f6fSYaroslav Tykhiy 			}
1441ea022d16SRodney W. Grimes 			pw = NULL;
1442ea022d16SRodney W. Grimes 			if (login_attempts++ >= 5) {
1443ea022d16SRodney W. Grimes 				syslog(LOG_NOTICE,
1444ea022d16SRodney W. Grimes 				    "repeated login failures from %s",
1445ea022d16SRodney W. Grimes 				    remotehost);
1446ea022d16SRodney W. Grimes 				exit(0);
1447ea022d16SRodney W. Grimes 			}
1448ea022d16SRodney W. Grimes 			return;
1449*e07d11b6SKurt Lidl 		} else {
1450*e07d11b6SKurt Lidl 			BLACKLIST_NOTIFY(BLACKLIST_AUTH_OK, STDIN_FILENO, "Login successful");
1451ea022d16SRodney W. Grimes 		}
1452ea022d16SRodney W. Grimes 	}
1453ea022d16SRodney W. Grimes 	login_attempts = 0;		/* this time successful */
1454c4536e21SYaroslav Tykhiy 	if (setegid(pw->pw_gid) < 0) {
1455ea022d16SRodney W. Grimes 		reply(550, "Can't set gid.");
1456ea022d16SRodney W. Grimes 		return;
1457ea022d16SRodney W. Grimes 	}
1458b071c689SDavid Nugent 	/* May be overridden by login.conf */
1459b071c689SDavid Nugent 	(void) umask(defumask);
1460b071c689SDavid Nugent #ifdef	LOGIN_CAP
14615d0bfe39SDavid Nugent 	if ((lc = login_getpwclass(pw)) != NULL) {
146204683b2cSYaroslav Tykhiy 		char	remote_ip[NI_MAXHOST];
1463b071c689SDavid Nugent 
146404683b2cSYaroslav Tykhiy 		if (getnameinfo((struct sockaddr *)&his_addr, his_addr.su_len,
14654dd8b5abSYoshinobu Inoue 			remote_ip, sizeof(remote_ip) - 1, NULL, 0,
146604683b2cSYaroslav Tykhiy 			NI_NUMERICHOST))
146704683b2cSYaroslav Tykhiy 				*remote_ip = 0;
1468b071c689SDavid Nugent 		remote_ip[sizeof(remote_ip) - 1] = 0;
1469b071c689SDavid Nugent 		if (!auth_hostok(lc, remotehost, remote_ip)) {
1470b071c689SDavid Nugent 			syslog(LOG_INFO|LOG_AUTH,
1471b071c689SDavid Nugent 			    "FTP LOGIN FAILED (HOST) as %s: permission denied.",
1472b071c689SDavid Nugent 			    pw->pw_name);
14734a3e5acdSYaroslav Tykhiy 			reply(530, "Permission denied.");
1474b071c689SDavid Nugent 			pw = NULL;
1475b071c689SDavid Nugent 			return;
1476b071c689SDavid Nugent 		}
1477b071c689SDavid Nugent 		if (!auth_timeok(lc, time(NULL))) {
14784a3e5acdSYaroslav Tykhiy 			reply(530, "Login not available right now.");
1479b071c689SDavid Nugent 			pw = NULL;
1480b071c689SDavid Nugent 			return;
1481b071c689SDavid Nugent 		}
1482b071c689SDavid Nugent 	}
1483e81b1c71SEdward Tomasz Napierala 	setusercontext(lc, pw, 0, LOGIN_SETALL &
1484e81b1c71SEdward Tomasz Napierala 		       ~(LOGIN_SETUSER | LOGIN_SETPATH | LOGIN_SETENV));
1485b071c689SDavid Nugent #else
1486e6fa0d43SDag-Erling Smørgrav 	setlogin(pw->pw_name);
1487ea022d16SRodney W. Grimes 	(void) initgroups(pw->pw_name, pw->pw_gid);
1488b071c689SDavid Nugent #endif
1489ea022d16SRodney W. Grimes 
14905bc9d93dSMark Murray #ifdef USE_PAM
14915bc9d93dSMark Murray 	if (pamh) {
14925bc9d93dSMark Murray 		if ((e = pam_open_session(pamh, 0)) != PAM_SUCCESS) {
14935bc9d93dSMark Murray 			syslog(LOG_ERR, "pam_open_session: %s", pam_strerror(pamh, e));
14945bc9d93dSMark Murray 		} else if ((e = pam_setcred(pamh, PAM_ESTABLISH_CRED)) != PAM_SUCCESS) {
14955bc9d93dSMark Murray 			syslog(LOG_ERR, "pam_setcred: %s", pam_strerror(pamh, e));
14965bc9d93dSMark Murray 		}
14975bc9d93dSMark Murray 	}
14985bc9d93dSMark Murray #endif
14995bc9d93dSMark Murray 
150080643af0SEd Schouten 	dochroot =
1501cefb6785SChristian S.J. Peron 		checkuser(_PATH_FTPCHROOT, pw->pw_name, 1, &residue, &ecode)
150280643af0SEd Schouten #ifdef	LOGIN_CAP	/* Allow login.conf configuration as well */
150380643af0SEd Schouten 		|| login_getcapbool(lc, "ftp-chroot", 0)
150480643af0SEd Schouten #endif
150580643af0SEd Schouten 	;
1506cefb6785SChristian S.J. Peron 	/*
1507cefb6785SChristian S.J. Peron 	 * It is possible that checkuser() failed to open the chroot file.
1508cefb6785SChristian S.J. Peron 	 * If this is the case, report that logins are un-available, since we
1509cefb6785SChristian S.J. Peron 	 * have no way of checking whether or not the user should be chrooted.
1510cefb6785SChristian S.J. Peron 	 * We ignore ENOENT since it is not required that this file be present.
1511cefb6785SChristian S.J. Peron 	 */
1512cefb6785SChristian S.J. Peron 	if (ecode != 0 && ecode != ENOENT) {
1513cefb6785SChristian S.J. Peron 		reply(530, "Login not available right now.");
1514cefb6785SChristian S.J. Peron 		return;
1515cefb6785SChristian S.J. Peron 	}
151680643af0SEd Schouten 	chrootdir = NULL;
151780643af0SEd Schouten 
15189f37b1a2SEd Schouten 	/* Disable wtmp logging when chrooting. */
15199f37b1a2SEd Schouten 	if (dochroot || guest)
15209f37b1a2SEd Schouten 		dowtmp = 0;
15219f37b1a2SEd Schouten 	if (dowtmp)
152280643af0SEd Schouten 		ftpd_logwtmp(wtmpid, pw->pw_name,
15235d7e0128SYaroslav Tykhiy 		    (struct sockaddr *)&his_addr);
1524ea022d16SRodney W. Grimes 	logged_in = 1;
1525ea022d16SRodney W. Grimes 
1526a5a4544eSPaul Traina 	if (guest && stats && statfd < 0)
1527ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
152863047c6fSDavid E. O'Brien 		statfd = open(thishost->statfile, O_WRONLY|O_APPEND);
1529ea4e54b9SDavid Nugent #else
153063047c6fSDavid E. O'Brien 		statfd = open(_PATH_FTPDSTATFILE, O_WRONLY|O_APPEND);
1531ea4e54b9SDavid Nugent #endif
153263047c6fSDavid E. O'Brien 		if (statfd < 0)
15333eb568f2SGuido van Rooij 			stats = 0;
15343eb568f2SGuido van Rooij 
1535ea022d16SRodney W. Grimes 	/*
1536ce9287fcSYaroslav Tykhiy 	 * For a chrooted local user,
1537ce9287fcSYaroslav Tykhiy 	 * a) see whether ftpchroot(5) specifies a chroot directory,
1538ce9287fcSYaroslav Tykhiy 	 * b) extract the directory pathname from the line,
1539ce9287fcSYaroslav Tykhiy 	 * c) expand it to the absolute pathname if necessary.
1540ea022d16SRodney W. Grimes 	 */
1541ce9287fcSYaroslav Tykhiy 	if (dochroot && residue &&
154239bce482SYaroslav Tykhiy 	    (chrootdir = strtok(residue, " \t")) != NULL) {
154339bce482SYaroslav Tykhiy 		if (chrootdir[0] != '/')
1544341e476eSYaroslav Tykhiy 			asprintf(&chrootdir, "%s/%s", pw->pw_dir, chrootdir);
154539bce482SYaroslav Tykhiy 		else
1546215a9f9dSYaroslav Tykhiy 			chrootdir = strdup(chrootdir); /* make it permanent */
1547341e476eSYaroslav Tykhiy 		if (chrootdir == NULL)
15488657b576SYaroslav Tykhiy 			fatalerror("Ran out of memory.");
15498657b576SYaroslav Tykhiy 	}
1550ce9287fcSYaroslav Tykhiy 	if (guest || dochroot) {
1551ce9287fcSYaroslav Tykhiy 		/*
1552ce9287fcSYaroslav Tykhiy 		 * If no chroot directory set yet, use the login directory.
1553ce9287fcSYaroslav Tykhiy 		 * Copy it so it can be modified while pw->pw_dir stays intact.
1554ce9287fcSYaroslav Tykhiy 		 */
1555ce9287fcSYaroslav Tykhiy 		if (chrootdir == NULL &&
1556ce9287fcSYaroslav Tykhiy 		    (chrootdir = strdup(pw->pw_dir)) == NULL)
1557ce9287fcSYaroslav Tykhiy 			fatalerror("Ran out of memory.");
1558ce9287fcSYaroslav Tykhiy 		/*
1559ce9287fcSYaroslav Tykhiy 		 * Check for the "/chroot/./home" syntax,
1560ce9287fcSYaroslav Tykhiy 		 * separate the chroot and home directory pathnames.
1561ce9287fcSYaroslav Tykhiy 		 */
1562ce9287fcSYaroslav Tykhiy 		if ((homedir = strstr(chrootdir, "/./")) != NULL) {
1563ce9287fcSYaroslav Tykhiy 			*(homedir++) = '\0';	/* wipe '/' */
1564ce9287fcSYaroslav Tykhiy 			homedir++;		/* skip '.' */
1565ce9287fcSYaroslav Tykhiy 		} else {
1566ce9287fcSYaroslav Tykhiy 			/*
1567ce9287fcSYaroslav Tykhiy 			 * We MUST do a chdir() after the chroot. Otherwise
1568ce9287fcSYaroslav Tykhiy 			 * the old current directory will be accessible as "."
1569ce9287fcSYaroslav Tykhiy 			 * outside the new root!
1570ce9287fcSYaroslav Tykhiy 			 */
1571ce9287fcSYaroslav Tykhiy 			homedir = "/";
1572ce9287fcSYaroslav Tykhiy 		}
1573ce9287fcSYaroslav Tykhiy 		/*
1574ce9287fcSYaroslav Tykhiy 		 * Finally, do chroot()
1575ce9287fcSYaroslav Tykhiy 		 */
1576ce9287fcSYaroslav Tykhiy 		if (chroot(chrootdir) < 0) {
1577a5a4544eSPaul Traina 			reply(550, "Can't change root.");
1578a5a4544eSPaul Traina 			goto bad;
1579a5a4544eSPaul Traina 		}
15803e65b9c6SColin Percival 		__FreeBSD_libc_enter_restricted_mode();
1581ce9287fcSYaroslav Tykhiy 	} else	/* real user w/o chroot */
1582ce9287fcSYaroslav Tykhiy 		homedir = pw->pw_dir;
1583ce9287fcSYaroslav Tykhiy 	/*
1584ce9287fcSYaroslav Tykhiy 	 * Set euid *before* doing chdir() so
1585ce9287fcSYaroslav Tykhiy 	 * a) the user won't be carried to a directory that he couldn't reach
1586ce9287fcSYaroslav Tykhiy 	 *    on his own due to no permission to upper path components,
1587ce9287fcSYaroslav Tykhiy 	 * b) NFS mounted homedirs w/restrictive permissions will be accessible
1588ce9287fcSYaroslav Tykhiy 	 *    (uid 0 has no root power over NFS if not mapped explicitly.)
1589ce9287fcSYaroslav Tykhiy 	 */
159052e7ee74SYaroslav Tykhiy 	if (seteuid(pw->pw_uid) < 0) {
1591ea022d16SRodney W. Grimes 		reply(550, "Can't set uid.");
1592ea022d16SRodney W. Grimes 		goto bad;
1593ea022d16SRodney W. Grimes 	}
1594ce9287fcSYaroslav Tykhiy 	if (chdir(homedir) < 0) {
1595ce9287fcSYaroslav Tykhiy 		if (guest || dochroot) {
1596ce9287fcSYaroslav Tykhiy 			reply(550, "Can't change to base directory.");
1597ce9287fcSYaroslav Tykhiy 			goto bad;
1598ce9287fcSYaroslav Tykhiy 		} else {
1599ce9287fcSYaroslav Tykhiy 			if (chdir("/") < 0) {
1600ce9287fcSYaroslav Tykhiy 				reply(550, "Root is inaccessible.");
1601ce9287fcSYaroslav Tykhiy 				goto bad;
1602ce9287fcSYaroslav Tykhiy 			}
160302c97492SYaroslav Tykhiy 			lreply(230, "No directory! Logging in with home=/.");
1604ce9287fcSYaroslav Tykhiy 		}
1605ce9287fcSYaroslav Tykhiy 	}
160682c76939SDavid Greenman 
160782c76939SDavid Greenman 	/*
1608ea022d16SRodney W. Grimes 	 * Display a login message, if it exists.
1609ea022d16SRodney W. Grimes 	 * N.B. reply(230,) must follow the message.
1610ea022d16SRodney W. Grimes 	 */
1611ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
161263047c6fSDavid E. O'Brien 	fd = fopen(thishost->loginmsg, "r");
1613ea4e54b9SDavid Nugent #else
161463047c6fSDavid E. O'Brien 	fd = fopen(_PATH_FTPLOGINMESG, "r");
1615ea4e54b9SDavid Nugent #endif
161663047c6fSDavid E. O'Brien 	if (fd != NULL) {
1617ea022d16SRodney W. Grimes 		char *cp, line[LINE_MAX];
1618ea022d16SRodney W. Grimes 
1619ea022d16SRodney W. Grimes 		while (fgets(line, sizeof(line), fd) != NULL) {
1620ea022d16SRodney W. Grimes 			if ((cp = strchr(line, '\n')) != NULL)
1621ea022d16SRodney W. Grimes 				*cp = '\0';
1622ea022d16SRodney W. Grimes 			lreply(230, "%s", line);
1623ea022d16SRodney W. Grimes 		}
1624ea022d16SRodney W. Grimes 		(void) fflush(stdout);
1625ea022d16SRodney W. Grimes 		(void) fclose(fd);
1626ea022d16SRodney W. Grimes 	}
1627ea022d16SRodney W. Grimes 	if (guest) {
16283eb568f2SGuido van Rooij 		if (ident != NULL)
16293eb568f2SGuido van Rooij 			free(ident);
163061f891a6SPaul Traina 		ident = strdup(passwd);
163161f891a6SPaul Traina 		if (ident == NULL)
1632618b0bbaSMark Murray 			fatalerror("Ran out of memory.");
163361f891a6SPaul Traina 
1634ea022d16SRodney W. Grimes 		reply(230, "Guest login ok, access restrictions apply.");
1635ea022d16SRodney W. Grimes #ifdef SETPROCTITLE
1636ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
1637ea4e54b9SDavid Nugent 		if (thishost != firsthost)
1638ea4e54b9SDavid Nugent 			snprintf(proctitle, sizeof(proctitle),
1639b3a0a7cdSMike Heffner 				 "%s: anonymous(%s)/%s", remotehost, hostname,
1640b3a0a7cdSMike Heffner 				 passwd);
1641ea4e54b9SDavid Nugent 		else
1642ea4e54b9SDavid Nugent #endif
1643ea022d16SRodney W. Grimes 			snprintf(proctitle, sizeof(proctitle),
1644b3a0a7cdSMike Heffner 				 "%s: anonymous/%s", remotehost, passwd);
1645b63e1fe2SPeter Wemm 		setproctitle("%s", proctitle);
1646ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */
1647ea022d16SRodney W. Grimes 		if (logging)
1648ea022d16SRodney W. Grimes 			syslog(LOG_INFO, "ANONYMOUS FTP LOGIN FROM %s, %s",
1649ea022d16SRodney W. Grimes 			    remotehost, passwd);
1650ea022d16SRodney W. Grimes 	} else {
16513401a71fSDaniel O'Callaghan 		if (dochroot)
165211342ab1SYaroslav Tykhiy 			reply(230, "User %s logged in, "
165311342ab1SYaroslav Tykhiy 				   "access restrictions apply.", pw->pw_name);
16543401a71fSDaniel O'Callaghan 		else
1655ea022d16SRodney W. Grimes 			reply(230, "User %s logged in.", pw->pw_name);
16563401a71fSDaniel O'Callaghan 
1657ea022d16SRodney W. Grimes #ifdef SETPROCTITLE
1658ea022d16SRodney W. Grimes 		snprintf(proctitle, sizeof(proctitle),
16597a29d7daSYaroslav Tykhiy 			 "%s: user/%s", remotehost, pw->pw_name);
1660b63e1fe2SPeter Wemm 		setproctitle("%s", proctitle);
1661ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */
1662ea022d16SRodney W. Grimes 		if (logging)
1663ea022d16SRodney W. Grimes 			syslog(LOG_INFO, "FTP LOGIN FROM %s as %s",
1664ea022d16SRodney W. Grimes 			    remotehost, pw->pw_name);
1665ea022d16SRodney W. Grimes 	}
1666220223fdSYaroslav Tykhiy 	if (logging && (guest || dochroot))
1667e897216fSYaroslav Tykhiy 		syslog(LOG_INFO, "session root changed to %s", chrootdir);
1668b071c689SDavid Nugent #ifdef	LOGIN_CAP
1669b071c689SDavid Nugent 	login_close(lc);
1670b071c689SDavid Nugent #endif
1671ce9287fcSYaroslav Tykhiy 	if (residue)
1672ce9287fcSYaroslav Tykhiy 		free(residue);
1673ea022d16SRodney W. Grimes 	return;
1674ea022d16SRodney W. Grimes bad:
1675ea022d16SRodney W. Grimes 	/* Forget all about it... */
1676b071c689SDavid Nugent #ifdef	LOGIN_CAP
1677b071c689SDavid Nugent 	login_close(lc);
1678b071c689SDavid Nugent #endif
1679ce9287fcSYaroslav Tykhiy 	if (residue)
1680ce9287fcSYaroslav Tykhiy 		free(residue);
1681ea022d16SRodney W. Grimes 	end_login();
1682ea022d16SRodney W. Grimes }
1683ea022d16SRodney W. Grimes 
1684ea022d16SRodney W. Grimes void
1685e4bc453cSWarner Losh retrieve(char *cmd, char *name)
1686ea022d16SRodney W. Grimes {
1687ea022d16SRodney W. Grimes 	FILE *fin, *dout;
1688ea022d16SRodney W. Grimes 	struct stat st;
1689e4bc453cSWarner Losh 	int (*closefunc)(FILE *);
1690158a00b2SJohn Birrell 	time_t start;
16918877d1dbSDon Lewis 	char line[BUFSIZ];
1692ea022d16SRodney W. Grimes 
1693ea022d16SRodney W. Grimes 	if (cmd == 0) {
1694ea022d16SRodney W. Grimes 		fin = fopen(name, "r"), closefunc = fclose;
1695ea022d16SRodney W. Grimes 		st.st_size = 0;
1696ea022d16SRodney W. Grimes 	} else {
16978877d1dbSDon Lewis 		(void) snprintf(line, sizeof(line), cmd, name);
16988877d1dbSDon Lewis 		name = line;
1699ea022d16SRodney W. Grimes 		fin = ftpd_popen(line, "r"), closefunc = ftpd_pclose;
1700ea022d16SRodney W. Grimes 		st.st_size = -1;
1701ea022d16SRodney W. Grimes 		st.st_blksize = BUFSIZ;
1702ea022d16SRodney W. Grimes 	}
1703ea022d16SRodney W. Grimes 	if (fin == NULL) {
1704ea022d16SRodney W. Grimes 		if (errno != 0) {
1705ea022d16SRodney W. Grimes 			perror_reply(550, name);
1706ea022d16SRodney W. Grimes 			if (cmd == 0) {
1707ea022d16SRodney W. Grimes 				LOGCMD("get", name);
1708ea022d16SRodney W. Grimes 			}
1709ea022d16SRodney W. Grimes 		}
1710ea022d16SRodney W. Grimes 		return;
1711ea022d16SRodney W. Grimes 	}
1712ea022d16SRodney W. Grimes 	byte_count = -1;
1713ea701226SYaroslav Tykhiy 	if (cmd == 0) {
1714ea701226SYaroslav Tykhiy 		if (fstat(fileno(fin), &st) < 0) {
1715ea701226SYaroslav Tykhiy 			perror_reply(550, name);
1716ea701226SYaroslav Tykhiy 			goto done;
1717ea701226SYaroslav Tykhiy 		}
1718ea701226SYaroslav Tykhiy 		if (!S_ISREG(st.st_mode)) {
171910e89104SYaroslav Tykhiy 			/*
172010e89104SYaroslav Tykhiy 			 * Never sending a raw directory is a workaround
172110e89104SYaroslav Tykhiy 			 * for buggy clients that will attempt to RETR
172210e89104SYaroslav Tykhiy 			 * a directory before listing it, e.g., Mozilla.
172310e89104SYaroslav Tykhiy 			 * Preventing a guest from getting irregular files
172410e89104SYaroslav Tykhiy 			 * is a simple security measure.
172510e89104SYaroslav Tykhiy 			 */
172610e89104SYaroslav Tykhiy 			if (S_ISDIR(st.st_mode) || guest) {
1727ea022d16SRodney W. Grimes 				reply(550, "%s: not a plain file.", name);
1728ea022d16SRodney W. Grimes 				goto done;
1729ea022d16SRodney W. Grimes 			}
1730ea701226SYaroslav Tykhiy 			st.st_size = -1;
1731ea701226SYaroslav Tykhiy 			/* st.st_blksize is set for all descriptor types */
1732ea701226SYaroslav Tykhiy 		}
1733ea701226SYaroslav Tykhiy 	}
1734ea022d16SRodney W. Grimes 	if (restart_point) {
1735ea022d16SRodney W. Grimes 		if (type == TYPE_A) {
1736ea022d16SRodney W. Grimes 			off_t i, n;
1737ea022d16SRodney W. Grimes 			int c;
1738ea022d16SRodney W. Grimes 
1739ea022d16SRodney W. Grimes 			n = restart_point;
1740ea022d16SRodney W. Grimes 			i = 0;
1741ea022d16SRodney W. Grimes 			while (i++ < n) {
1742ea022d16SRodney W. Grimes 				if ((c=getc(fin)) == EOF) {
1743ea022d16SRodney W. Grimes 					perror_reply(550, name);
1744ea022d16SRodney W. Grimes 					goto done;
1745ea022d16SRodney W. Grimes 				}
1746ea022d16SRodney W. Grimes 				if (c == '\n')
1747ea022d16SRodney W. Grimes 					i++;
1748ea022d16SRodney W. Grimes 			}
1749ea022d16SRodney W. Grimes 		} else if (lseek(fileno(fin), restart_point, L_SET) < 0) {
1750ea022d16SRodney W. Grimes 			perror_reply(550, name);
1751ea022d16SRodney W. Grimes 			goto done;
1752ea022d16SRodney W. Grimes 		}
1753ea022d16SRodney W. Grimes 	}
1754ea022d16SRodney W. Grimes 	dout = dataconn(name, st.st_size, "w");
1755ea022d16SRodney W. Grimes 	if (dout == NULL)
1756ea022d16SRodney W. Grimes 		goto done;
17573eb568f2SGuido van Rooij 	time(&start);
17589fc5823aSGarrett Wollman 	send_data(fin, dout, st.st_blksize, st.st_size,
17599fc5823aSGarrett Wollman 		  restart_point == 0 && cmd == 0 && S_ISREG(st.st_mode));
1760c999732bSYaroslav Tykhiy 	if (cmd == 0 && guest && stats && byte_count > 0)
1761c999732bSYaroslav Tykhiy 		logxfer(name, byte_count, start);
1762ea022d16SRodney W. Grimes 	(void) fclose(dout);
1763ea022d16SRodney W. Grimes 	data = -1;
1764ea022d16SRodney W. Grimes 	pdata = -1;
1765ea022d16SRodney W. Grimes done:
1766ea022d16SRodney W. Grimes 	if (cmd == 0)
1767ea022d16SRodney W. Grimes 		LOGBYTES("get", name, byte_count);
1768ea022d16SRodney W. Grimes 	(*closefunc)(fin);
1769ea022d16SRodney W. Grimes }
1770ea022d16SRodney W. Grimes 
1771ea022d16SRodney W. Grimes void
1772e4bc453cSWarner Losh store(char *name, char *mode, int unique)
1773ea022d16SRodney W. Grimes {
1774a117c345SYaroslav Tykhiy 	int fd;
1775ea022d16SRodney W. Grimes 	FILE *fout, *din;
1776e4bc453cSWarner Losh 	int (*closefunc)(FILE *);
1777ea022d16SRodney W. Grimes 
1778a117c345SYaroslav Tykhiy 	if (*mode == 'a') {		/* APPE */
1779a117c345SYaroslav Tykhiy 		if (unique) {
1780a117c345SYaroslav Tykhiy 			/* Programming error */
1781a117c345SYaroslav Tykhiy 			syslog(LOG_ERR, "Internal: unique flag to APPE");
1782a117c345SYaroslav Tykhiy 			unique = 0;
1783a117c345SYaroslav Tykhiy 		}
1784a117c345SYaroslav Tykhiy 		if (guest && noguestmod) {
178502c97492SYaroslav Tykhiy 			reply(550, "Appending to existing file denied.");
1786a117c345SYaroslav Tykhiy 			goto err;
1787a117c345SYaroslav Tykhiy 		}
1788a117c345SYaroslav Tykhiy 		restart_point = 0;	/* not affected by preceding REST */
1789a117c345SYaroslav Tykhiy 	}
1790a117c345SYaroslav Tykhiy 	if (unique)			/* STOU overrides REST */
1791a117c345SYaroslav Tykhiy 		restart_point = 0;
1792a117c345SYaroslav Tykhiy 	if (guest && noguestmod) {
1793a117c345SYaroslav Tykhiy 		if (restart_point) {	/* guest STOR w/REST */
179402c97492SYaroslav Tykhiy 			reply(550, "Modifying existing file denied.");
1795a117c345SYaroslav Tykhiy 			goto err;
1796a117c345SYaroslav Tykhiy 		} else			/* treat guest STOR as STOU */
1797a117c345SYaroslav Tykhiy 			unique = 1;
1798ea022d16SRodney W. Grimes 	}
1799ea022d16SRodney W. Grimes 
1800ea022d16SRodney W. Grimes 	if (restart_point)
1801a117c345SYaroslav Tykhiy 		mode = "r+";	/* so ASCII manual seek can work */
1802a117c345SYaroslav Tykhiy 	if (unique) {
1803a117c345SYaroslav Tykhiy 		if ((fd = guniquefd(name, &name)) < 0)
1804a117c345SYaroslav Tykhiy 			goto err;
1805a117c345SYaroslav Tykhiy 		fout = fdopen(fd, mode);
1806a117c345SYaroslav Tykhiy 	} else
1807ea022d16SRodney W. Grimes 		fout = fopen(name, mode);
1808ea022d16SRodney W. Grimes 	closefunc = fclose;
1809ea022d16SRodney W. Grimes 	if (fout == NULL) {
1810ea022d16SRodney W. Grimes 		perror_reply(553, name);
1811a117c345SYaroslav Tykhiy 		goto err;
1812ea022d16SRodney W. Grimes 	}
1813ea022d16SRodney W. Grimes 	byte_count = -1;
1814ea022d16SRodney W. Grimes 	if (restart_point) {
1815ea022d16SRodney W. Grimes 		if (type == TYPE_A) {
1816ea022d16SRodney W. Grimes 			off_t i, n;
1817ea022d16SRodney W. Grimes 			int c;
1818ea022d16SRodney W. Grimes 
1819ea022d16SRodney W. Grimes 			n = restart_point;
1820ea022d16SRodney W. Grimes 			i = 0;
1821ea022d16SRodney W. Grimes 			while (i++ < n) {
1822ea022d16SRodney W. Grimes 				if ((c=getc(fout)) == EOF) {
1823ea022d16SRodney W. Grimes 					perror_reply(550, name);
1824ea022d16SRodney W. Grimes 					goto done;
1825ea022d16SRodney W. Grimes 				}
1826ea022d16SRodney W. Grimes 				if (c == '\n')
1827ea022d16SRodney W. Grimes 					i++;
1828ea022d16SRodney W. Grimes 			}
1829ea022d16SRodney W. Grimes 			/*
1830ea022d16SRodney W. Grimes 			 * We must do this seek to "current" position
1831ea022d16SRodney W. Grimes 			 * because we are changing from reading to
1832ea022d16SRodney W. Grimes 			 * writing.
1833ea022d16SRodney W. Grimes 			 */
18340e519c96SYaroslav Tykhiy 			if (fseeko(fout, 0, SEEK_CUR) < 0) {
1835ea022d16SRodney W. Grimes 				perror_reply(550, name);
1836ea022d16SRodney W. Grimes 				goto done;
1837ea022d16SRodney W. Grimes 			}
1838ea022d16SRodney W. Grimes 		} else if (lseek(fileno(fout), restart_point, L_SET) < 0) {
1839ea022d16SRodney W. Grimes 			perror_reply(550, name);
1840ea022d16SRodney W. Grimes 			goto done;
1841ea022d16SRodney W. Grimes 		}
1842ea022d16SRodney W. Grimes 	}
18430e519c96SYaroslav Tykhiy 	din = dataconn(name, -1, "r");
1844ea022d16SRodney W. Grimes 	if (din == NULL)
1845ea022d16SRodney W. Grimes 		goto done;
1846ea022d16SRodney W. Grimes 	if (receive_data(din, fout) == 0) {
1847ea022d16SRodney W. Grimes 		if (unique)
1848ea022d16SRodney W. Grimes 			reply(226, "Transfer complete (unique file name:%s).",
1849ea022d16SRodney W. Grimes 			    name);
1850ea022d16SRodney W. Grimes 		else
1851ea022d16SRodney W. Grimes 			reply(226, "Transfer complete.");
1852ea022d16SRodney W. Grimes 	}
1853ea022d16SRodney W. Grimes 	(void) fclose(din);
1854ea022d16SRodney W. Grimes 	data = -1;
1855ea022d16SRodney W. Grimes 	pdata = -1;
1856ea022d16SRodney W. Grimes done:
18577c20f337SYaroslav Tykhiy 	LOGBYTES(*mode == 'a' ? "append" : "put", name, byte_count);
1858ea022d16SRodney W. Grimes 	(*closefunc)(fout);
1859a117c345SYaroslav Tykhiy 	return;
1860a117c345SYaroslav Tykhiy err:
1861a117c345SYaroslav Tykhiy 	LOGCMD(*mode == 'a' ? "append" : "put" , name);
1862a117c345SYaroslav Tykhiy 	return;
1863ea022d16SRodney W. Grimes }
1864ea022d16SRodney W. Grimes 
1865ea022d16SRodney W. Grimes static FILE *
1866e4bc453cSWarner Losh getdatasock(char *mode)
1867ea022d16SRodney W. Grimes {
1868ea022d16SRodney W. Grimes 	int on = 1, s, t, tries;
1869ea022d16SRodney W. Grimes 
1870ea022d16SRodney W. Grimes 	if (data >= 0)
1871ea022d16SRodney W. Grimes 		return (fdopen(data, mode));
18724dd8b5abSYoshinobu Inoue 
18734dd8b5abSYoshinobu Inoue 	s = socket(data_dest.su_family, SOCK_STREAM, 0);
1874ea022d16SRodney W. Grimes 	if (s < 0)
1875ea022d16SRodney W. Grimes 		goto bad;
187657d4ef07SYaroslav Tykhiy 	if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
1877406d1ae9SYaroslav Tykhiy 		syslog(LOG_WARNING, "data setsockopt (SO_REUSEADDR): %m");
1878ea022d16SRodney W. Grimes 	/* anchor socket to avoid multi-homing problems */
18794dd8b5abSYoshinobu Inoue 	data_source = ctrl_addr;
188063591ba5SYaroslav Tykhiy 	data_source.su_port = htons(dataport);
188152e7ee74SYaroslav Tykhiy 	(void) seteuid(0);
1882ea022d16SRodney W. Grimes 	for (tries = 1; ; tries++) {
18839ec7612aSYaroslav Tykhiy 		/*
18849ec7612aSYaroslav Tykhiy 		 * We should loop here since it's possible that
18859ec7612aSYaroslav Tykhiy 		 * another ftpd instance has passed this point and is
18869ec7612aSYaroslav Tykhiy 		 * trying to open a data connection in active mode now.
18879ec7612aSYaroslav Tykhiy 		 * Until the other connection is opened, we'll be getting
18889ec7612aSYaroslav Tykhiy 		 * EADDRINUSE because no SOCK_STREAM sockets in the system
18899ec7612aSYaroslav Tykhiy 		 * can share both local and remote addresses, localIP:20
18909ec7612aSYaroslav Tykhiy 		 * and *:* in this case.
18919ec7612aSYaroslav Tykhiy 		 */
1892ea022d16SRodney W. Grimes 		if (bind(s, (struct sockaddr *)&data_source,
18934dd8b5abSYoshinobu Inoue 		    data_source.su_len) >= 0)
1894ea022d16SRodney W. Grimes 			break;
1895ea022d16SRodney W. Grimes 		if (errno != EADDRINUSE || tries > 10)
1896ea022d16SRodney W. Grimes 			goto bad;
1897ea022d16SRodney W. Grimes 		sleep(tries);
1898ea022d16SRodney W. Grimes 	}
189952e7ee74SYaroslav Tykhiy 	(void) seteuid(pw->pw_uid);
1900ea022d16SRodney W. Grimes #ifdef IP_TOS
19014dd8b5abSYoshinobu Inoue 	if (data_source.su_family == AF_INET)
19024dd8b5abSYoshinobu Inoue       {
1903ea022d16SRodney W. Grimes 	on = IPTOS_THROUGHPUT;
190457d4ef07SYaroslav Tykhiy 	if (setsockopt(s, IPPROTO_IP, IP_TOS, &on, sizeof(int)) < 0)
1905406d1ae9SYaroslav Tykhiy 		syslog(LOG_WARNING, "data setsockopt (IP_TOS): %m");
19064dd8b5abSYoshinobu Inoue       }
1907ea022d16SRodney W. Grimes #endif
19089fc5823aSGarrett Wollman #ifdef TCP_NOPUSH
19099fc5823aSGarrett Wollman 	/*
19109fc5823aSGarrett Wollman 	 * Turn off push flag to keep sender TCP from sending short packets
191132072720SYaroslav Tykhiy 	 * at the boundaries of each write().
19129fc5823aSGarrett Wollman 	 */
19139fc5823aSGarrett Wollman 	on = 1;
191457d4ef07SYaroslav Tykhiy 	if (setsockopt(s, IPPROTO_TCP, TCP_NOPUSH, &on, sizeof on) < 0)
1915406d1ae9SYaroslav Tykhiy 		syslog(LOG_WARNING, "data setsockopt (TCP_NOPUSH): %m");
19169fc5823aSGarrett Wollman #endif
1917ea022d16SRodney W. Grimes 	return (fdopen(s, mode));
1918ea022d16SRodney W. Grimes bad:
1919ea022d16SRodney W. Grimes 	/* Return the real value of errno (close may change it) */
1920ea022d16SRodney W. Grimes 	t = errno;
192152e7ee74SYaroslav Tykhiy 	(void) seteuid(pw->pw_uid);
1922ea022d16SRodney W. Grimes 	(void) close(s);
1923ea022d16SRodney W. Grimes 	errno = t;
1924ea022d16SRodney W. Grimes 	return (NULL);
1925ea022d16SRodney W. Grimes }
1926ea022d16SRodney W. Grimes 
1927ea022d16SRodney W. Grimes static FILE *
1928e4bc453cSWarner Losh dataconn(char *name, off_t size, char *mode)
1929ea022d16SRodney W. Grimes {
1930ea022d16SRodney W. Grimes 	char sizebuf[32];
1931ea022d16SRodney W. Grimes 	FILE *file;
1932e5094456SCrist J. Clark 	int retry = 0, tos, conerrno;
1933ea022d16SRodney W. Grimes 
1934ea022d16SRodney W. Grimes 	file_size = size;
1935ea022d16SRodney W. Grimes 	byte_count = 0;
19360e519c96SYaroslav Tykhiy 	if (size != -1)
1937a57e1ef0SYaroslav Tykhiy 		(void) snprintf(sizebuf, sizeof(sizebuf),
1938a57e1ef0SYaroslav Tykhiy 				" (%jd bytes)", (intmax_t)size);
1939ea022d16SRodney W. Grimes 	else
1940e760ef2cSWarner Losh 		*sizebuf = '\0';
1941ea022d16SRodney W. Grimes 	if (pdata >= 0) {
19424dd8b5abSYoshinobu Inoue 		union sockunion from;
194378e3eed0SStefan Farfeleder 		socklen_t fromlen = ctrl_addr.su_len;
194478e3eed0SStefan Farfeleder 		int flags, s;
1945d6ed3c37SGuido van Rooij 		struct timeval timeout;
1946d6ed3c37SGuido van Rooij 		fd_set set;
1947ea022d16SRodney W. Grimes 
1948d6ed3c37SGuido van Rooij 		FD_ZERO(&set);
1949d6ed3c37SGuido van Rooij 		FD_SET(pdata, &set);
1950d6ed3c37SGuido van Rooij 
1951d6ed3c37SGuido van Rooij 		timeout.tv_usec = 0;
1952d6ed3c37SGuido van Rooij 		timeout.tv_sec = 120;
1953d6ed3c37SGuido van Rooij 
19544cd48bacSYaroslav Tykhiy 		/*
19554cd48bacSYaroslav Tykhiy 		 * Granted a socket is in the blocking I/O mode,
19564cd48bacSYaroslav Tykhiy 		 * accept() will block after a successful select()
19574cd48bacSYaroslav Tykhiy 		 * if the selected connection dies in between.
19584cd48bacSYaroslav Tykhiy 		 * Therefore set the non-blocking I/O flag here.
19594cd48bacSYaroslav Tykhiy 		 */
19604cd48bacSYaroslav Tykhiy 		if ((flags = fcntl(pdata, F_GETFL, 0)) == -1 ||
19614cd48bacSYaroslav Tykhiy 		    fcntl(pdata, F_SETFL, flags | O_NONBLOCK) == -1)
19624cd48bacSYaroslav Tykhiy 			goto pdata_err;
1963aa5a9d3fSYaroslav Tykhiy 		if (select(pdata+1, &set, NULL, NULL, &timeout) <= 0 ||
19644cd48bacSYaroslav Tykhiy 		    (s = accept(pdata, (struct sockaddr *) &from, &fromlen)) < 0)
19654cd48bacSYaroslav Tykhiy 			goto pdata_err;
1966ea022d16SRodney W. Grimes 		(void) close(pdata);
1967ea022d16SRodney W. Grimes 		pdata = s;
19684cd48bacSYaroslav Tykhiy 		/*
1969f6daca0dSYaroslav Tykhiy 		 * Unset the inherited non-blocking I/O flag
1970f6daca0dSYaroslav Tykhiy 		 * on the child socket so stdio can work on it.
19714cd48bacSYaroslav Tykhiy 		 */
19724cd48bacSYaroslav Tykhiy 		if ((flags = fcntl(pdata, F_GETFL, 0)) == -1 ||
19734cd48bacSYaroslav Tykhiy 		    fcntl(pdata, F_SETFL, flags & ~O_NONBLOCK) == -1)
19744cd48bacSYaroslav Tykhiy 			goto pdata_err;
1975ea022d16SRodney W. Grimes #ifdef IP_TOS
19764dd8b5abSYoshinobu Inoue 		if (from.su_family == AF_INET)
19774dd8b5abSYoshinobu Inoue 	      {
1978a5a4544eSPaul Traina 		tos = IPTOS_THROUGHPUT;
197957d4ef07SYaroslav Tykhiy 		if (setsockopt(s, IPPROTO_IP, IP_TOS, &tos, sizeof(int)) < 0)
1980406d1ae9SYaroslav Tykhiy 			syslog(LOG_WARNING, "pdata setsockopt (IP_TOS): %m");
19814dd8b5abSYoshinobu Inoue 	      }
1982ea022d16SRodney W. Grimes #endif
1983ea022d16SRodney W. Grimes 		reply(150, "Opening %s mode data connection for '%s'%s.",
1984ea022d16SRodney W. Grimes 		     type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf);
1985ea022d16SRodney W. Grimes 		return (fdopen(pdata, mode));
19864cd48bacSYaroslav Tykhiy pdata_err:
19874cd48bacSYaroslav Tykhiy 		reply(425, "Can't open data connection.");
19884cd48bacSYaroslav Tykhiy 		(void) close(pdata);
19894cd48bacSYaroslav Tykhiy 		pdata = -1;
19904cd48bacSYaroslav Tykhiy 		return (NULL);
1991ea022d16SRodney W. Grimes 	}
1992ea022d16SRodney W. Grimes 	if (data >= 0) {
1993ea022d16SRodney W. Grimes 		reply(125, "Using existing data connection for '%s'%s.",
1994ea022d16SRodney W. Grimes 		    name, sizebuf);
1995ea022d16SRodney W. Grimes 		usedefault = 1;
1996ea022d16SRodney W. Grimes 		return (fdopen(data, mode));
1997ea022d16SRodney W. Grimes 	}
1998ea022d16SRodney W. Grimes 	if (usedefault)
1999ea022d16SRodney W. Grimes 		data_dest = his_addr;
2000ea022d16SRodney W. Grimes 	usedefault = 1;
2001e5094456SCrist J. Clark 	do {
2002ea022d16SRodney W. Grimes 		file = getdatasock(mode);
2003ea022d16SRodney W. Grimes 		if (file == NULL) {
200404683b2cSYaroslav Tykhiy 			char hostbuf[NI_MAXHOST], portbuf[NI_MAXSERV];
200504683b2cSYaroslav Tykhiy 
200604683b2cSYaroslav Tykhiy 			if (getnameinfo((struct sockaddr *)&data_source,
200704683b2cSYaroslav Tykhiy 				data_source.su_len,
200804683b2cSYaroslav Tykhiy 				hostbuf, sizeof(hostbuf) - 1,
200904683b2cSYaroslav Tykhiy 				portbuf, sizeof(portbuf) - 1,
201004683b2cSYaroslav Tykhiy 				NI_NUMERICHOST|NI_NUMERICSERV))
201104683b2cSYaroslav Tykhiy 					*hostbuf = *portbuf = 0;
201204683b2cSYaroslav Tykhiy 			hostbuf[sizeof(hostbuf) - 1] = 0;
201304683b2cSYaroslav Tykhiy 			portbuf[sizeof(portbuf) - 1] = 0;
20144dd8b5abSYoshinobu Inoue 			reply(425, "Can't create data socket (%s,%s): %s.",
20154dd8b5abSYoshinobu Inoue 				hostbuf, portbuf, strerror(errno));
2016ea022d16SRodney W. Grimes 			return (NULL);
2017ea022d16SRodney W. Grimes 		}
2018ea022d16SRodney W. Grimes 		data = fileno(file);
2019e5094456SCrist J. Clark 		conerrno = 0;
2020e5094456SCrist J. Clark 		if (connect(data, (struct sockaddr *)&data_dest,
2021e5094456SCrist J. Clark 		    data_dest.su_len) == 0)
2022e5094456SCrist J. Clark 			break;
2023e5094456SCrist J. Clark 		conerrno = errno;
2024ea022d16SRodney W. Grimes 		(void) fclose(file);
2025ea022d16SRodney W. Grimes 		data = -1;
2026e5094456SCrist J. Clark 		if (conerrno == EADDRINUSE) {
2027e3765043SYaroslav Tykhiy 			sleep(swaitint);
2028e5094456SCrist J. Clark 			retry += swaitint;
2029e5094456SCrist J. Clark 		} else {
2030e5094456SCrist J. Clark 			break;
2031e5094456SCrist J. Clark 		}
2032e5094456SCrist J. Clark 	} while (retry <= swaitmax);
2033e5094456SCrist J. Clark 	if (conerrno != 0) {
203482c03024SYaroslav Tykhiy 		reply(425, "Can't build data connection: %s.",
203582c03024SYaroslav Tykhiy 			   strerror(conerrno));
2036ea022d16SRodney W. Grimes 		return (NULL);
2037ea022d16SRodney W. Grimes 	}
2038ea022d16SRodney W. Grimes 	reply(150, "Opening %s mode data connection for '%s'%s.",
2039ea022d16SRodney W. Grimes 	     type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf);
2040ea022d16SRodney W. Grimes 	return (file);
2041ea022d16SRodney W. Grimes }
2042ea022d16SRodney W. Grimes 
2043ea022d16SRodney W. Grimes /*
20444cd51076SYaroslav Tykhiy  * A helper macro to avoid code duplication
20454cd51076SYaroslav Tykhiy  * in send_data() and receive_data().
20464cd51076SYaroslav Tykhiy  *
20474cd51076SYaroslav Tykhiy  * XXX We have to block SIGURG during putc() because BSD stdio
20484cd51076SYaroslav Tykhiy  * is unable to restart interrupted write operations and hence
20494cd51076SYaroslav Tykhiy  * the entire buffer contents will be lost as soon as a write()
20504cd51076SYaroslav Tykhiy  * call indicates EINTR to stdio.
20514cd51076SYaroslav Tykhiy  */
20524cd51076SYaroslav Tykhiy #define FTPD_PUTC(ch, file, label)					\
20534cd51076SYaroslav Tykhiy 	do {								\
20544cd51076SYaroslav Tykhiy 		int ret;						\
20554cd51076SYaroslav Tykhiy 									\
20564cd51076SYaroslav Tykhiy 		do {							\
20574cd51076SYaroslav Tykhiy 			START_UNSAFE;					\
20584cd51076SYaroslav Tykhiy 			ret = putc((ch), (file));			\
20594cd51076SYaroslav Tykhiy 			END_UNSAFE;					\
20604cd51076SYaroslav Tykhiy 			CHECKOOB(return (-1))				\
20614cd51076SYaroslav Tykhiy 			else if (ferror(file))				\
20624cd51076SYaroslav Tykhiy 				goto label;				\
20634cd51076SYaroslav Tykhiy 			clearerr(file);					\
20644cd51076SYaroslav Tykhiy 		} while (ret == EOF);					\
20654cd51076SYaroslav Tykhiy 	} while (0)
20664cd51076SYaroslav Tykhiy 
20674cd51076SYaroslav Tykhiy /*
2068ec489d64SPedro F. Giffuni  * Transfer the contents of "instr" to "outstr" peer using the appropriate
20699fc5823aSGarrett Wollman  * encapsulation of the data subject to Mode, Structure, and Type.
2070ea022d16SRodney W. Grimes  *
2071ea022d16SRodney W. Grimes  * NB: Form isn't handled.
2072ea022d16SRodney W. Grimes  */
20734b82fc95SYaroslav Tykhiy static int
20746e4b0a55SYaroslav Tykhiy send_data(FILE *instr, FILE *outstr, size_t blksize, off_t filesize, int isreg)
2075ea022d16SRodney W. Grimes {
2076db1c2da3SYaroslav Tykhiy 	int c, cp, filefd, netfd;
2077f6f0c4b9SDan Moschuk 	char *buf;
2078ea022d16SRodney W. Grimes 
20794cd51076SYaroslav Tykhiy 	STARTXFER;
20804cd51076SYaroslav Tykhiy 
2081ea022d16SRodney W. Grimes 	switch (type) {
2082ea022d16SRodney W. Grimes 
2083ea022d16SRodney W. Grimes 	case TYPE_A:
20844cd51076SYaroslav Tykhiy 		cp = EOF;
20854cd51076SYaroslav Tykhiy 		for (;;) {
20864cd51076SYaroslav Tykhiy 			c = getc(instr);
20874cd51076SYaroslav Tykhiy 			CHECKOOB(return (-1))
20884cd51076SYaroslav Tykhiy 			else if (c == EOF && ferror(instr))
20894cd51076SYaroslav Tykhiy 				goto file_err;
20904cd51076SYaroslav Tykhiy 			if (c == EOF) {
20914cd51076SYaroslav Tykhiy 				if (ferror(instr)) {	/* resume after OOB */
20924cd51076SYaroslav Tykhiy 					clearerr(instr);
20934cd51076SYaroslav Tykhiy 					continue;
2094ea022d16SRodney W. Grimes 				}
20954cd51076SYaroslav Tykhiy 				if (feof(instr))	/* EOF */
20964cd51076SYaroslav Tykhiy 					break;
20974cd51076SYaroslav Tykhiy 				syslog(LOG_ERR, "Internal: impossible condition"
20984cd51076SYaroslav Tykhiy 						" on file after getc()");
20994cd51076SYaroslav Tykhiy 				goto file_err;
21004cd51076SYaroslav Tykhiy 			}
21014cd51076SYaroslav Tykhiy 			if (c == '\n' && cp != '\r') {
21024cd51076SYaroslav Tykhiy 				FTPD_PUTC('\r', outstr, data_err);
21034cd51076SYaroslav Tykhiy 				byte_count++;
21044cd51076SYaroslav Tykhiy 			}
21054cd51076SYaroslav Tykhiy 			FTPD_PUTC(c, outstr, data_err);
21064cd51076SYaroslav Tykhiy 			byte_count++;
2107db1c2da3SYaroslav Tykhiy 			cp = c;
2108ea022d16SRodney W. Grimes 		}
21094cd51076SYaroslav Tykhiy #ifdef notyet	/* BSD stdio isn't ready for that */
21104cd51076SYaroslav Tykhiy 		while (fflush(outstr) == EOF) {
21114cd51076SYaroslav Tykhiy 			CHECKOOB(return (-1))
21124cd51076SYaroslav Tykhiy 			else
2113ea022d16SRodney W. Grimes 				goto data_err;
21144cd51076SYaroslav Tykhiy 			clearerr(outstr);
21154cd51076SYaroslav Tykhiy 		}
21164cd51076SYaroslav Tykhiy 		ENDXFER;
21174cd51076SYaroslav Tykhiy #else
21184cd51076SYaroslav Tykhiy 		ENDXFER;
21194cd51076SYaroslav Tykhiy 		if (fflush(outstr) == EOF)
21204cd51076SYaroslav Tykhiy 			goto data_err;
21214cd51076SYaroslav Tykhiy #endif
2122ea022d16SRodney W. Grimes 		reply(226, "Transfer complete.");
21234b82fc95SYaroslav Tykhiy 		return (0);
2124ea022d16SRodney W. Grimes 
2125ea022d16SRodney W. Grimes 	case TYPE_I:
2126ea022d16SRodney W. Grimes 	case TYPE_L:
21279fc5823aSGarrett Wollman 		/*
21289fc5823aSGarrett Wollman 		 * isreg is only set if we are not doing restart and we
21299fc5823aSGarrett Wollman 		 * are sending a regular file
21309fc5823aSGarrett Wollman 		 */
21319fc5823aSGarrett Wollman 		netfd = fileno(outstr);
21329fc5823aSGarrett Wollman 		filefd = fileno(instr);
21339fc5823aSGarrett Wollman 
2134f6f0c4b9SDan Moschuk 		if (isreg) {
21352e22b914SYaroslav Tykhiy 			char *msg = "Transfer complete.";
21364cd51076SYaroslav Tykhiy 			off_t cnt, offset;
2137f6f0c4b9SDan Moschuk 			int err;
2138f6f0c4b9SDan Moschuk 
21392f492fc8SYaroslav Tykhiy 			cnt = offset = 0;
2140f6f0c4b9SDan Moschuk 
21412f492fc8SYaroslav Tykhiy 			while (filesize > 0) {
2142492f1d9cSMaxim Konovalov 				err = sendfile(filefd, netfd, offset, 0,
21437e295315SYaroslav Tykhiy 					       NULL, &cnt, 0);
21443af48c42SMaxim Konovalov 				/*
21453af48c42SMaxim Konovalov 				 * Calculate byte_count before OOB processing.
21463af48c42SMaxim Konovalov 				 * It can be used in myoob() later.
21473af48c42SMaxim Konovalov 				 */
21483af48c42SMaxim Konovalov 				byte_count += cnt;
2149f6f0c4b9SDan Moschuk 				offset += cnt;
2150492f1d9cSMaxim Konovalov 				filesize -= cnt;
21514cd51076SYaroslav Tykhiy 				CHECKOOB(return (-1))
21524cd51076SYaroslav Tykhiy 				else if (err == -1) {
21534cd51076SYaroslav Tykhiy 					if (errno != EINTR &&
21544cd51076SYaroslav Tykhiy 					    cnt == 0 && offset == 0)
2155f6f0c4b9SDan Moschuk 						goto oldway;
21569fc5823aSGarrett Wollman 					goto data_err;
2157f6f0c4b9SDan Moschuk 				}
21584cd51076SYaroslav Tykhiy 				if (err == -1)	/* resume after OOB */
21594cd51076SYaroslav Tykhiy 					continue;
21602e22b914SYaroslav Tykhiy 				/*
21612e22b914SYaroslav Tykhiy 				 * We hit the EOF prematurely.
21622e22b914SYaroslav Tykhiy 				 * Perhaps the file was externally truncated.
21632e22b914SYaroslav Tykhiy 				 */
21642e22b914SYaroslav Tykhiy 				if (cnt == 0) {
21652e22b914SYaroslav Tykhiy 					msg = "Transfer finished due to "
21662e22b914SYaroslav Tykhiy 					      "premature end of file.";
21672e22b914SYaroslav Tykhiy 					break;
21682e22b914SYaroslav Tykhiy 				}
2169f6f0c4b9SDan Moschuk 			}
21704cd51076SYaroslav Tykhiy 			ENDXFER;
217162f390ecSEd Maste 			reply(226, "%s", msg);
21724b82fc95SYaroslav Tykhiy 			return (0);
21739fc5823aSGarrett Wollman 		}
21749fc5823aSGarrett Wollman 
21759fc5823aSGarrett Wollman oldway:
2176e3765043SYaroslav Tykhiy 		if ((buf = malloc(blksize)) == NULL) {
21774cd51076SYaroslav Tykhiy 			ENDXFER;
217802c97492SYaroslav Tykhiy 			reply(451, "Ran out of memory.");
21794b82fc95SYaroslav Tykhiy 			return (-1);
2180ea022d16SRodney W. Grimes 		}
21819fc5823aSGarrett Wollman 
21824cd51076SYaroslav Tykhiy 		for (;;) {
21834cd51076SYaroslav Tykhiy 			int cnt, len;
21844cd51076SYaroslav Tykhiy 			char *bp;
21854cd51076SYaroslav Tykhiy 
21864cd51076SYaroslav Tykhiy 			cnt = read(filefd, buf, blksize);
21874cd51076SYaroslav Tykhiy 			CHECKOOB(free(buf); return (-1))
21884cd51076SYaroslav Tykhiy 			else if (cnt < 0) {
21898efc8b18SYaroslav Tykhiy 				free(buf);
2190ea022d16SRodney W. Grimes 				goto file_err;
21914cd51076SYaroslav Tykhiy 			}
21924cd51076SYaroslav Tykhiy 			if (cnt < 0)	/* resume after OOB */
21934cd51076SYaroslav Tykhiy 				continue;
21944cd51076SYaroslav Tykhiy 			if (cnt == 0)	/* EOF */
21954cd51076SYaroslav Tykhiy 				break;
21964cd51076SYaroslav Tykhiy 			for (len = cnt, bp = buf; len > 0;) {
21974cd51076SYaroslav Tykhiy 				cnt = write(netfd, bp, len);
21984cd51076SYaroslav Tykhiy 				CHECKOOB(free(buf); return (-1))
21994cd51076SYaroslav Tykhiy 				else if (cnt < 0) {
22004cd51076SYaroslav Tykhiy 					free(buf);
2201ea022d16SRodney W. Grimes 					goto data_err;
2202ea022d16SRodney W. Grimes 				}
22034cd51076SYaroslav Tykhiy 				if (cnt <= 0)
22044cd51076SYaroslav Tykhiy 					continue;
22054cd51076SYaroslav Tykhiy 				len -= cnt;
22064cd51076SYaroslav Tykhiy 				bp += cnt;
22074cd51076SYaroslav Tykhiy 				byte_count += cnt;
22084cd51076SYaroslav Tykhiy 			}
22094cd51076SYaroslav Tykhiy 		}
22104cd51076SYaroslav Tykhiy 		ENDXFER;
22114cd51076SYaroslav Tykhiy 		free(buf);
2212ea022d16SRodney W. Grimes 		reply(226, "Transfer complete.");
22134b82fc95SYaroslav Tykhiy 		return (0);
2214ea022d16SRodney W. Grimes 	default:
22154cd51076SYaroslav Tykhiy 		ENDXFER;
221602c97492SYaroslav Tykhiy 		reply(550, "Unimplemented TYPE %d in send_data.", type);
22174b82fc95SYaroslav Tykhiy 		return (-1);
2218ea022d16SRodney W. Grimes 	}
2219ea022d16SRodney W. Grimes 
2220ea022d16SRodney W. Grimes data_err:
22214cd51076SYaroslav Tykhiy 	ENDXFER;
2222ea022d16SRodney W. Grimes 	perror_reply(426, "Data connection");
22234b82fc95SYaroslav Tykhiy 	return (-1);
2224ea022d16SRodney W. Grimes 
2225ea022d16SRodney W. Grimes file_err:
22264cd51076SYaroslav Tykhiy 	ENDXFER;
2227ea022d16SRodney W. Grimes 	perror_reply(551, "Error on input file");
22284b82fc95SYaroslav Tykhiy 	return (-1);
2229ea022d16SRodney W. Grimes }
2230ea022d16SRodney W. Grimes 
2231ea022d16SRodney W. Grimes /*
2232ea022d16SRodney W. Grimes  * Transfer data from peer to "outstr" using the appropriate encapulation of
2233ea022d16SRodney W. Grimes  * the data subject to Mode, Structure, and Type.
2234ea022d16SRodney W. Grimes  *
2235ea022d16SRodney W. Grimes  * N.B.: Form isn't handled.
2236ea022d16SRodney W. Grimes  */
2237ea022d16SRodney W. Grimes static int
2238e4bc453cSWarner Losh receive_data(FILE *instr, FILE *outstr)
2239ea022d16SRodney W. Grimes {
22404cd51076SYaroslav Tykhiy 	int c, cp;
22414cd51076SYaroslav Tykhiy 	int bare_lfs = 0;
2242ea022d16SRodney W. Grimes 
22434cd51076SYaroslav Tykhiy 	STARTXFER;
224461f891a6SPaul Traina 
2245ea022d16SRodney W. Grimes 	switch (type) {
2246ea022d16SRodney W. Grimes 
2247ea022d16SRodney W. Grimes 	case TYPE_I:
2248ea022d16SRodney W. Grimes 	case TYPE_L:
22494cd51076SYaroslav Tykhiy 		for (;;) {
22504cd51076SYaroslav Tykhiy 			int cnt, len;
22514cd51076SYaroslav Tykhiy 			char *bp;
22524cd51076SYaroslav Tykhiy 			char buf[BUFSIZ];
22534cd51076SYaroslav Tykhiy 
22544cd51076SYaroslav Tykhiy 			cnt = read(fileno(instr), buf, sizeof(buf));
22554cd51076SYaroslav Tykhiy 			CHECKOOB(return (-1))
22564cd51076SYaroslav Tykhiy 			else if (cnt < 0)
22574cd51076SYaroslav Tykhiy 				goto data_err;
22584cd51076SYaroslav Tykhiy 			if (cnt < 0)	/* resume after OOB */
22594cd51076SYaroslav Tykhiy 				continue;
22604cd51076SYaroslav Tykhiy 			if (cnt == 0)	/* EOF */
22614cd51076SYaroslav Tykhiy 				break;
22624cd51076SYaroslav Tykhiy 			for (len = cnt, bp = buf; len > 0;) {
22634cd51076SYaroslav Tykhiy 				cnt = write(fileno(outstr), bp, len);
22644cd51076SYaroslav Tykhiy 				CHECKOOB(return (-1))
22654cd51076SYaroslav Tykhiy 				else if (cnt < 0)
2266ea022d16SRodney W. Grimes 					goto file_err;
22674cd51076SYaroslav Tykhiy 				if (cnt <= 0)
22684cd51076SYaroslav Tykhiy 					continue;
22694cd51076SYaroslav Tykhiy 				len -= cnt;
22704cd51076SYaroslav Tykhiy 				bp += cnt;
2271ea022d16SRodney W. Grimes 				byte_count += cnt;
2272ea022d16SRodney W. Grimes 			}
22734cd51076SYaroslav Tykhiy 		}
22744cd51076SYaroslav Tykhiy 		ENDXFER;
2275ea022d16SRodney W. Grimes 		return (0);
2276ea022d16SRodney W. Grimes 
2277ea022d16SRodney W. Grimes 	case TYPE_E:
22784cd51076SYaroslav Tykhiy 		ENDXFER;
2279ea022d16SRodney W. Grimes 		reply(553, "TYPE E not implemented.");
2280ea022d16SRodney W. Grimes 		return (-1);
2281ea022d16SRodney W. Grimes 
2282ea022d16SRodney W. Grimes 	case TYPE_A:
22834cd51076SYaroslav Tykhiy 		cp = EOF;
22844cd51076SYaroslav Tykhiy 		for (;;) {
22854cd51076SYaroslav Tykhiy 			c = getc(instr);
22864cd51076SYaroslav Tykhiy 			CHECKOOB(return (-1))
22874cd51076SYaroslav Tykhiy 			else if (c == EOF && ferror(instr))
22884cd51076SYaroslav Tykhiy 				goto data_err;
22894cd51076SYaroslav Tykhiy 			if (c == EOF && ferror(instr)) { /* resume after OOB */
22904cd51076SYaroslav Tykhiy 				clearerr(instr);
22914cd51076SYaroslav Tykhiy 				continue;
22924cd51076SYaroslav Tykhiy 			}
22934cd51076SYaroslav Tykhiy 
22944cd51076SYaroslav Tykhiy 			if (cp == '\r') {
22954cd51076SYaroslav Tykhiy 				if (c != '\n')
22964cd51076SYaroslav Tykhiy 					FTPD_PUTC('\r', outstr, file_err);
22974cd51076SYaroslav Tykhiy 			} else
2298ea022d16SRodney W. Grimes 				if (c == '\n')
2299ea022d16SRodney W. Grimes 					bare_lfs++;
23004cd51076SYaroslav Tykhiy 			if (c == '\r') {
23014cd51076SYaroslav Tykhiy 				byte_count++;
23024cd51076SYaroslav Tykhiy 				cp = c;
23034cd51076SYaroslav Tykhiy 				continue;
23044cd51076SYaroslav Tykhiy 			}
23054cd51076SYaroslav Tykhiy 
23064cd51076SYaroslav Tykhiy 			/* Check for EOF here in order not to lose last \r. */
23074cd51076SYaroslav Tykhiy 			if (c == EOF) {
23084cd51076SYaroslav Tykhiy 				if (feof(instr))	/* EOF */
23094cd51076SYaroslav Tykhiy 					break;
23104cd51076SYaroslav Tykhiy 				syslog(LOG_ERR, "Internal: impossible condition"
23114cd51076SYaroslav Tykhiy 						" on data stream after getc()");
2312ea022d16SRodney W. Grimes 				goto data_err;
2313ea022d16SRodney W. Grimes 			}
23144cd51076SYaroslav Tykhiy 
23154cd51076SYaroslav Tykhiy 			byte_count++;
23164cd51076SYaroslav Tykhiy 			FTPD_PUTC(c, outstr, file_err);
23174cd51076SYaroslav Tykhiy 			cp = c;
2318ea022d16SRodney W. Grimes 		}
23194cd51076SYaroslav Tykhiy #ifdef notyet	/* BSD stdio isn't ready for that */
23204cd51076SYaroslav Tykhiy 		while (fflush(outstr) == EOF) {
23214cd51076SYaroslav Tykhiy 			CHECKOOB(return (-1))
23224cd51076SYaroslav Tykhiy 			else
2323ea022d16SRodney W. Grimes 				goto file_err;
23244cd51076SYaroslav Tykhiy 			clearerr(outstr);
23254cd51076SYaroslav Tykhiy 		}
23264cd51076SYaroslav Tykhiy 		ENDXFER;
23274cd51076SYaroslav Tykhiy #else
23284cd51076SYaroslav Tykhiy 		ENDXFER;
23294cd51076SYaroslav Tykhiy 		if (fflush(outstr) == EOF)
23304cd51076SYaroslav Tykhiy 			goto file_err;
23314cd51076SYaroslav Tykhiy #endif
2332ea022d16SRodney W. Grimes 		if (bare_lfs) {
2333ea022d16SRodney W. Grimes 			lreply(226,
233402c97492SYaroslav Tykhiy 		"WARNING! %d bare linefeeds received in ASCII mode.",
2335ea022d16SRodney W. Grimes 			    bare_lfs);
2336ea022d16SRodney W. Grimes 		(void)printf("   File may not have transferred correctly.\r\n");
2337ea022d16SRodney W. Grimes 		}
2338ea022d16SRodney W. Grimes 		return (0);
2339ea022d16SRodney W. Grimes 	default:
23404cd51076SYaroslav Tykhiy 		ENDXFER;
234102c97492SYaroslav Tykhiy 		reply(550, "Unimplemented TYPE %d in receive_data.", type);
2342ea022d16SRodney W. Grimes 		return (-1);
2343ea022d16SRodney W. Grimes 	}
2344ea022d16SRodney W. Grimes 
2345ea022d16SRodney W. Grimes data_err:
23464cd51076SYaroslav Tykhiy 	ENDXFER;
234702c97492SYaroslav Tykhiy 	perror_reply(426, "Data connection");
2348ea022d16SRodney W. Grimes 	return (-1);
2349ea022d16SRodney W. Grimes 
2350ea022d16SRodney W. Grimes file_err:
23514cd51076SYaroslav Tykhiy 	ENDXFER;
235202c97492SYaroslav Tykhiy 	perror_reply(452, "Error writing to file");
2353ea022d16SRodney W. Grimes 	return (-1);
2354ea022d16SRodney W. Grimes }
2355ea022d16SRodney W. Grimes 
2356ea022d16SRodney W. Grimes void
2357e4bc453cSWarner Losh statfilecmd(char *filename)
2358ea022d16SRodney W. Grimes {
2359ea022d16SRodney W. Grimes 	FILE *fin;
2360f8a581a0SYaroslav Tykhiy 	int atstart;
236141c57b48SYaroslav Tykhiy 	int c, code;
2362ea022d16SRodney W. Grimes 	char line[LINE_MAX];
236341c57b48SYaroslav Tykhiy 	struct stat st;
2364ea022d16SRodney W. Grimes 
236541c57b48SYaroslav Tykhiy 	code = lstat(filename, &st) == 0 && S_ISDIR(st.st_mode) ? 212 : 213;
2366af85d782SDavid Nugent 	(void)snprintf(line, sizeof(line), _PATH_LS " -lgA %s", filename);
2367ea022d16SRodney W. Grimes 	fin = ftpd_popen(line, "r");
2368763e8c96SEd Maste 	if (fin == NULL) {
2369763e8c96SEd Maste 		perror_reply(551, filename);
2370763e8c96SEd Maste 		return;
2371763e8c96SEd Maste 	}
23723b48b877SYaroslav Tykhiy 	lreply(code, "Status of %s:", filename);
2373f8a581a0SYaroslav Tykhiy 	atstart = 1;
2374ea022d16SRodney W. Grimes 	while ((c = getc(fin)) != EOF) {
2375ea022d16SRodney W. Grimes 		if (c == '\n') {
2376ea022d16SRodney W. Grimes 			if (ferror(stdout)){
237702c97492SYaroslav Tykhiy 				perror_reply(421, "Control connection");
2378ea022d16SRodney W. Grimes 				(void) ftpd_pclose(fin);
2379ea022d16SRodney W. Grimes 				dologout(1);
2380ea022d16SRodney W. Grimes 				/* NOTREACHED */
2381ea022d16SRodney W. Grimes 			}
2382ea022d16SRodney W. Grimes 			if (ferror(fin)) {
2383ea022d16SRodney W. Grimes 				perror_reply(551, filename);
2384ea022d16SRodney W. Grimes 				(void) ftpd_pclose(fin);
2385ea022d16SRodney W. Grimes 				return;
2386ea022d16SRodney W. Grimes 			}
2387ea022d16SRodney W. Grimes 			(void) putc('\r', stdout);
2388ea022d16SRodney W. Grimes 		}
2389f8a581a0SYaroslav Tykhiy 		/*
2390f8a581a0SYaroslav Tykhiy 		 * RFC 959 says neutral text should be prepended before
2391f8a581a0SYaroslav Tykhiy 		 * a leading 3-digit number followed by whitespace, but
2392f8a581a0SYaroslav Tykhiy 		 * many ftp clients can be confused by any leading digits,
2393f8a581a0SYaroslav Tykhiy 		 * as a matter of fact.
2394f8a581a0SYaroslav Tykhiy 		 */
2395f8a581a0SYaroslav Tykhiy 		if (atstart && isdigit(c))
2396f8a581a0SYaroslav Tykhiy 			(void) putc(' ', stdout);
2397ea022d16SRodney W. Grimes 		(void) putc(c, stdout);
2398f8a581a0SYaroslav Tykhiy 		atstart = (c == '\n');
2399ea022d16SRodney W. Grimes 	}
2400ea022d16SRodney W. Grimes 	(void) ftpd_pclose(fin);
240102c97492SYaroslav Tykhiy 	reply(code, "End of status.");
2402ea022d16SRodney W. Grimes }
2403ea022d16SRodney W. Grimes 
2404ea022d16SRodney W. Grimes void
2405e4bc453cSWarner Losh statcmd(void)
2406ea022d16SRodney W. Grimes {
24074dd8b5abSYoshinobu Inoue 	union sockunion *su;
2408ea022d16SRodney W. Grimes 	u_char *a, *p;
2409b0f06defSHajimu UMEMOTO 	char hname[NI_MAXHOST];
24104dd8b5abSYoshinobu Inoue 	int ispassive;
2411ea022d16SRodney W. Grimes 
2412c152df28SYaroslav Tykhiy 	if (hostinfo) {
2413a23f61bcSYaroslav Tykhiy 		lreply(211, "%s FTP server status:", hostname);
2414ea022d16SRodney W. Grimes 		printf("     %s\r\n", version);
2415c152df28SYaroslav Tykhiy 	} else
2416c152df28SYaroslav Tykhiy 		lreply(211, "FTP server status:");
2417ea022d16SRodney W. Grimes 	printf("     Connected to %s", remotehost);
24184dd8b5abSYoshinobu Inoue 	if (!getnameinfo((struct sockaddr *)&his_addr, his_addr.su_len,
2419b0f06defSHajimu UMEMOTO 			 hname, sizeof(hname) - 1, NULL, 0, NI_NUMERICHOST)) {
242004683b2cSYaroslav Tykhiy 		hname[sizeof(hname) - 1] = 0;
24214dd8b5abSYoshinobu Inoue 		if (strcmp(hname, remotehost) != 0)
24224dd8b5abSYoshinobu Inoue 			printf(" (%s)", hname);
24234dd8b5abSYoshinobu Inoue 	}
2424ea022d16SRodney W. Grimes 	printf("\r\n");
2425ea022d16SRodney W. Grimes 	if (logged_in) {
2426ea022d16SRodney W. Grimes 		if (guest)
2427ea022d16SRodney W. Grimes 			printf("     Logged in anonymously\r\n");
2428ea022d16SRodney W. Grimes 		else
2429ea022d16SRodney W. Grimes 			printf("     Logged in as %s\r\n", pw->pw_name);
2430ea022d16SRodney W. Grimes 	} else if (askpasswd)
2431ea022d16SRodney W. Grimes 		printf("     Waiting for password\r\n");
2432ea022d16SRodney W. Grimes 	else
2433ea022d16SRodney W. Grimes 		printf("     Waiting for user name\r\n");
2434ea022d16SRodney W. Grimes 	printf("     TYPE: %s", typenames[type]);
2435ea022d16SRodney W. Grimes 	if (type == TYPE_A || type == TYPE_E)
2436ea022d16SRodney W. Grimes 		printf(", FORM: %s", formnames[form]);
2437ea022d16SRodney W. Grimes 	if (type == TYPE_L)
243889fdc4e1SMike Barcroft #if CHAR_BIT == 8
243989fdc4e1SMike Barcroft 		printf(" %d", CHAR_BIT);
2440ea022d16SRodney W. Grimes #else
2441ea022d16SRodney W. Grimes 		printf(" %d", bytesize);	/* need definition! */
2442ea022d16SRodney W. Grimes #endif
2443ea022d16SRodney W. Grimes 	printf("; STRUcture: %s; transfer MODE: %s\r\n",
2444ea022d16SRodney W. Grimes 	    strunames[stru], modenames[mode]);
2445ea022d16SRodney W. Grimes 	if (data != -1)
2446ea022d16SRodney W. Grimes 		printf("     Data connection open\r\n");
2447ea022d16SRodney W. Grimes 	else if (pdata != -1) {
24484dd8b5abSYoshinobu Inoue 		ispassive = 1;
24494dd8b5abSYoshinobu Inoue 		su = &pasv_addr;
2450ea022d16SRodney W. Grimes 		goto printaddr;
2451ea022d16SRodney W. Grimes 	} else if (usedefault == 0) {
24524dd8b5abSYoshinobu Inoue 		ispassive = 0;
24534dd8b5abSYoshinobu Inoue 		su = &data_dest;
2454ea022d16SRodney W. Grimes printaddr:
2455ea022d16SRodney W. Grimes #define UC(b) (((int) b) & 0xff)
24564dd8b5abSYoshinobu Inoue 		if (epsvall) {
24574dd8b5abSYoshinobu Inoue 			printf("     EPSV only mode (EPSV ALL)\r\n");
24584dd8b5abSYoshinobu Inoue 			goto epsvonly;
24594dd8b5abSYoshinobu Inoue 		}
24604dd8b5abSYoshinobu Inoue 
24614dd8b5abSYoshinobu Inoue 		/* PORT/PASV */
24624dd8b5abSYoshinobu Inoue 		if (su->su_family == AF_INET) {
24634dd8b5abSYoshinobu Inoue 			a = (u_char *) &su->su_sin.sin_addr;
24644dd8b5abSYoshinobu Inoue 			p = (u_char *) &su->su_sin.sin_port;
24654dd8b5abSYoshinobu Inoue 			printf("     %s (%d,%d,%d,%d,%d,%d)\r\n",
24664dd8b5abSYoshinobu Inoue 				ispassive ? "PASV" : "PORT",
24674dd8b5abSYoshinobu Inoue 				UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
24684dd8b5abSYoshinobu Inoue 				UC(p[0]), UC(p[1]));
24694dd8b5abSYoshinobu Inoue 		}
24704dd8b5abSYoshinobu Inoue 
24714dd8b5abSYoshinobu Inoue 		/* LPRT/LPSV */
24724dd8b5abSYoshinobu Inoue 	    {
24734dd8b5abSYoshinobu Inoue 		int alen, af, i;
24744dd8b5abSYoshinobu Inoue 
24754dd8b5abSYoshinobu Inoue 		switch (su->su_family) {
24764dd8b5abSYoshinobu Inoue 		case AF_INET:
24774dd8b5abSYoshinobu Inoue 			a = (u_char *) &su->su_sin.sin_addr;
24784dd8b5abSYoshinobu Inoue 			p = (u_char *) &su->su_sin.sin_port;
24794dd8b5abSYoshinobu Inoue 			alen = sizeof(su->su_sin.sin_addr);
24804dd8b5abSYoshinobu Inoue 			af = 4;
24814dd8b5abSYoshinobu Inoue 			break;
24824dd8b5abSYoshinobu Inoue 		case AF_INET6:
24834dd8b5abSYoshinobu Inoue 			a = (u_char *) &su->su_sin6.sin6_addr;
24844dd8b5abSYoshinobu Inoue 			p = (u_char *) &su->su_sin6.sin6_port;
24854dd8b5abSYoshinobu Inoue 			alen = sizeof(su->su_sin6.sin6_addr);
24864dd8b5abSYoshinobu Inoue 			af = 6;
24874dd8b5abSYoshinobu Inoue 			break;
24884dd8b5abSYoshinobu Inoue 		default:
24894dd8b5abSYoshinobu Inoue 			af = 0;
24904dd8b5abSYoshinobu Inoue 			break;
24914dd8b5abSYoshinobu Inoue 		}
24924dd8b5abSYoshinobu Inoue 		if (af) {
24934dd8b5abSYoshinobu Inoue 			printf("     %s (%d,%d,", ispassive ? "LPSV" : "LPRT",
24944dd8b5abSYoshinobu Inoue 				af, alen);
24954dd8b5abSYoshinobu Inoue 			for (i = 0; i < alen; i++)
24964dd8b5abSYoshinobu Inoue 				printf("%d,", UC(a[i]));
24974dd8b5abSYoshinobu Inoue 			printf("%d,%d,%d)\r\n", 2, UC(p[0]), UC(p[1]));
24984dd8b5abSYoshinobu Inoue 		}
24994dd8b5abSYoshinobu Inoue 	    }
25004dd8b5abSYoshinobu Inoue 
25014dd8b5abSYoshinobu Inoue epsvonly:;
25024dd8b5abSYoshinobu Inoue 		/* EPRT/EPSV */
25034dd8b5abSYoshinobu Inoue 	    {
25044dd8b5abSYoshinobu Inoue 		int af;
25054dd8b5abSYoshinobu Inoue 
25064dd8b5abSYoshinobu Inoue 		switch (su->su_family) {
25074dd8b5abSYoshinobu Inoue 		case AF_INET:
25084dd8b5abSYoshinobu Inoue 			af = 1;
25094dd8b5abSYoshinobu Inoue 			break;
25104dd8b5abSYoshinobu Inoue 		case AF_INET6:
25114dd8b5abSYoshinobu Inoue 			af = 2;
25124dd8b5abSYoshinobu Inoue 			break;
25134dd8b5abSYoshinobu Inoue 		default:
25144dd8b5abSYoshinobu Inoue 			af = 0;
25154dd8b5abSYoshinobu Inoue 			break;
25164dd8b5abSYoshinobu Inoue 		}
25174dd8b5abSYoshinobu Inoue 		if (af) {
2518b0f06defSHajimu UMEMOTO 			union sockunion tmp;
2519b0f06defSHajimu UMEMOTO 
2520b0f06defSHajimu UMEMOTO 			tmp = *su;
2521b0f06defSHajimu UMEMOTO 			if (tmp.su_family == AF_INET6)
2522b0f06defSHajimu UMEMOTO 				tmp.su_sin6.sin6_scope_id = 0;
2523b0f06defSHajimu UMEMOTO 			if (!getnameinfo((struct sockaddr *)&tmp, tmp.su_len,
25244dd8b5abSYoshinobu Inoue 					hname, sizeof(hname) - 1, NULL, 0,
25254dd8b5abSYoshinobu Inoue 					NI_NUMERICHOST)) {
252604683b2cSYaroslav Tykhiy 				hname[sizeof(hname) - 1] = 0;
25274dd8b5abSYoshinobu Inoue 				printf("     %s |%d|%s|%d|\r\n",
25284dd8b5abSYoshinobu Inoue 					ispassive ? "EPSV" : "EPRT",
2529b0f06defSHajimu UMEMOTO 					af, hname, htons(tmp.su_port));
25304dd8b5abSYoshinobu Inoue 			}
25314dd8b5abSYoshinobu Inoue 		}
25324dd8b5abSYoshinobu Inoue 	    }
2533ea022d16SRodney W. Grimes #undef UC
2534ea022d16SRodney W. Grimes 	} else
2535ea022d16SRodney W. Grimes 		printf("     No data connection\r\n");
253602c97492SYaroslav Tykhiy 	reply(211, "End of status.");
2537ea022d16SRodney W. Grimes }
2538ea022d16SRodney W. Grimes 
2539ea022d16SRodney W. Grimes void
2540e4bc453cSWarner Losh fatalerror(char *s)
2541ea022d16SRodney W. Grimes {
2542ea022d16SRodney W. Grimes 
25434a3e5acdSYaroslav Tykhiy 	reply(451, "Error in server: %s", s);
2544ea022d16SRodney W. Grimes 	reply(221, "Closing connection due to server error.");
2545ea022d16SRodney W. Grimes 	dologout(0);
2546ea022d16SRodney W. Grimes 	/* NOTREACHED */
2547ea022d16SRodney W. Grimes }
2548ea022d16SRodney W. Grimes 
2549ea022d16SRodney W. Grimes void
2550ea022d16SRodney W. Grimes reply(int n, const char *fmt, ...)
2551ea022d16SRodney W. Grimes {
2552ea022d16SRodney W. Grimes 	va_list ap;
2553e4bc453cSWarner Losh 
2554ea022d16SRodney W. Grimes 	(void)printf("%d ", n);
25559cbb335cSTim J. Robbins 	va_start(ap, fmt);
2556ea022d16SRodney W. Grimes 	(void)vprintf(fmt, ap);
25579cbb335cSTim J. Robbins 	va_end(ap);
2558ea022d16SRodney W. Grimes 	(void)printf("\r\n");
2559ea022d16SRodney W. Grimes 	(void)fflush(stdout);
2560618b0bbaSMark Murray 	if (ftpdebug) {
2561ea022d16SRodney W. Grimes 		syslog(LOG_DEBUG, "<--- %d ", n);
25629cbb335cSTim J. Robbins 		va_start(ap, fmt);
2563ea022d16SRodney W. Grimes 		vsyslog(LOG_DEBUG, fmt, ap);
25649cbb335cSTim J. Robbins 		va_end(ap);
2565ea022d16SRodney W. Grimes 	}
2566ea022d16SRodney W. Grimes }
2567ea022d16SRodney W. Grimes 
2568ea022d16SRodney W. Grimes void
2569ea022d16SRodney W. Grimes lreply(int n, const char *fmt, ...)
2570ea022d16SRodney W. Grimes {
2571ea022d16SRodney W. Grimes 	va_list ap;
2572e4bc453cSWarner Losh 
2573ea022d16SRodney W. Grimes 	(void)printf("%d- ", n);
25749cbb335cSTim J. Robbins 	va_start(ap, fmt);
2575ea022d16SRodney W. Grimes 	(void)vprintf(fmt, ap);
25769cbb335cSTim J. Robbins 	va_end(ap);
2577ea022d16SRodney W. Grimes 	(void)printf("\r\n");
2578ea022d16SRodney W. Grimes 	(void)fflush(stdout);
2579618b0bbaSMark Murray 	if (ftpdebug) {
2580ea022d16SRodney W. Grimes 		syslog(LOG_DEBUG, "<--- %d- ", n);
25819cbb335cSTim J. Robbins 		va_start(ap, fmt);
2582ea022d16SRodney W. Grimes 		vsyslog(LOG_DEBUG, fmt, ap);
25839cbb335cSTim J. Robbins 		va_end(ap);
2584ea022d16SRodney W. Grimes 	}
2585ea022d16SRodney W. Grimes }
2586ea022d16SRodney W. Grimes 
2587ea022d16SRodney W. Grimes static void
2588e4bc453cSWarner Losh ack(char *s)
2589ea022d16SRodney W. Grimes {
2590ea022d16SRodney W. Grimes 
2591ea022d16SRodney W. Grimes 	reply(250, "%s command successful.", s);
2592ea022d16SRodney W. Grimes }
2593ea022d16SRodney W. Grimes 
2594ea022d16SRodney W. Grimes void
2595e4bc453cSWarner Losh nack(char *s)
2596ea022d16SRodney W. Grimes {
2597ea022d16SRodney W. Grimes 
2598ea022d16SRodney W. Grimes 	reply(502, "%s command not implemented.", s);
2599ea022d16SRodney W. Grimes }
2600ea022d16SRodney W. Grimes 
2601ea022d16SRodney W. Grimes /* ARGSUSED */
2602ea022d16SRodney W. Grimes void
2603e4bc453cSWarner Losh yyerror(char *s)
2604ea022d16SRodney W. Grimes {
2605ea022d16SRodney W. Grimes 	char *cp;
2606ea022d16SRodney W. Grimes 
26079aca17cbSMark Murray 	if ((cp = strchr(cbuf,'\n')))
2608ea022d16SRodney W. Grimes 		*cp = '\0';
260902c97492SYaroslav Tykhiy 	reply(500, "%s: command not understood.", cbuf);
2610ea022d16SRodney W. Grimes }
2611ea022d16SRodney W. Grimes 
2612ea022d16SRodney W. Grimes void
2613e4bc453cSWarner Losh delete(char *name)
2614ea022d16SRodney W. Grimes {
2615ea022d16SRodney W. Grimes 	struct stat st;
2616ea022d16SRodney W. Grimes 
2617ea022d16SRodney W. Grimes 	LOGCMD("delete", name);
26181b0e12d7SYaroslav Tykhiy 	if (lstat(name, &st) < 0) {
2619ea022d16SRodney W. Grimes 		perror_reply(550, name);
2620ea022d16SRodney W. Grimes 		return;
2621ea022d16SRodney W. Grimes 	}
2622ac4f2391SYaroslav Tykhiy 	if (S_ISDIR(st.st_mode)) {
2623ea022d16SRodney W. Grimes 		if (rmdir(name) < 0) {
2624ea022d16SRodney W. Grimes 			perror_reply(550, name);
2625ea022d16SRodney W. Grimes 			return;
2626ea022d16SRodney W. Grimes 		}
2627ea022d16SRodney W. Grimes 		goto done;
2628ea022d16SRodney W. Grimes 	}
26293f8b9cfeSYaroslav Tykhiy 	if (guest && noguestmod) {
263002c97492SYaroslav Tykhiy 		reply(550, "Operation not permitted.");
26313f8b9cfeSYaroslav Tykhiy 		return;
26323f8b9cfeSYaroslav Tykhiy 	}
26333f8b9cfeSYaroslav Tykhiy 	if (unlink(name) < 0) {
2634ea022d16SRodney W. Grimes 		perror_reply(550, name);
2635ea022d16SRodney W. Grimes 		return;
2636ea022d16SRodney W. Grimes 	}
2637ea022d16SRodney W. Grimes done:
2638ea022d16SRodney W. Grimes 	ack("DELE");
2639ea022d16SRodney W. Grimes }
2640ea022d16SRodney W. Grimes 
2641ea022d16SRodney W. Grimes void
2642e4bc453cSWarner Losh cwd(char *path)
2643ea022d16SRodney W. Grimes {
2644ea022d16SRodney W. Grimes 
2645ea022d16SRodney W. Grimes 	if (chdir(path) < 0)
2646ea022d16SRodney W. Grimes 		perror_reply(550, path);
2647ea022d16SRodney W. Grimes 	else
2648ea022d16SRodney W. Grimes 		ack("CWD");
2649ea022d16SRodney W. Grimes }
2650ea022d16SRodney W. Grimes 
2651ea022d16SRodney W. Grimes void
2652e4bc453cSWarner Losh makedir(char *name)
2653ea022d16SRodney W. Grimes {
26542b748987SYaroslav Tykhiy 	char *s;
2655ea022d16SRodney W. Grimes 
2656ea022d16SRodney W. Grimes 	LOGCMD("mkdir", name);
2657d186bb12SMatthew N. Dodd 	if (guest && noguestmkd)
2658405e2987SYaroslav Tykhiy 		reply(550, "Operation not permitted.");
2659d186bb12SMatthew N. Dodd 	else if (mkdir(name, 0777) < 0)
2660ea022d16SRodney W. Grimes 		perror_reply(550, name);
26612b748987SYaroslav Tykhiy 	else {
26622b748987SYaroslav Tykhiy 		if ((s = doublequote(name)) == NULL)
26632b748987SYaroslav Tykhiy 			fatalerror("Ran out of memory.");
26642b748987SYaroslav Tykhiy 		reply(257, "\"%s\" directory created.", s);
26652b748987SYaroslav Tykhiy 		free(s);
26662b748987SYaroslav Tykhiy 	}
2667ea022d16SRodney W. Grimes }
2668ea022d16SRodney W. Grimes 
2669ea022d16SRodney W. Grimes void
2670e4bc453cSWarner Losh removedir(char *name)
2671ea022d16SRodney W. Grimes {
2672ea022d16SRodney W. Grimes 
2673ea022d16SRodney W. Grimes 	LOGCMD("rmdir", name);
2674ea022d16SRodney W. Grimes 	if (rmdir(name) < 0)
2675ea022d16SRodney W. Grimes 		perror_reply(550, name);
2676ea022d16SRodney W. Grimes 	else
2677ea022d16SRodney W. Grimes 		ack("RMD");
2678ea022d16SRodney W. Grimes }
2679ea022d16SRodney W. Grimes 
2680ea022d16SRodney W. Grimes void
2681e4bc453cSWarner Losh pwd(void)
2682ea022d16SRodney W. Grimes {
2683e4648f05SYaroslav Tykhiy 	char *s, path[MAXPATHLEN + 1];
2684ea022d16SRodney W. Grimes 
2685de9b6c03SYaroslav Tykhiy 	if (getcwd(path, sizeof(path)) == NULL)
268675933089SYaroslav Tykhiy 		perror_reply(550, "Get current directory");
2687e4648f05SYaroslav Tykhiy 	else {
2688e4648f05SYaroslav Tykhiy 		if ((s = doublequote(path)) == NULL)
2689e4648f05SYaroslav Tykhiy 			fatalerror("Ran out of memory.");
2690e4648f05SYaroslav Tykhiy 		reply(257, "\"%s\" is current directory.", s);
2691e4648f05SYaroslav Tykhiy 		free(s);
2692e4648f05SYaroslav Tykhiy 	}
2693ea022d16SRodney W. Grimes }
2694ea022d16SRodney W. Grimes 
2695ea022d16SRodney W. Grimes char *
2696e4bc453cSWarner Losh renamefrom(char *name)
2697ea022d16SRodney W. Grimes {
2698ea022d16SRodney W. Grimes 	struct stat st;
2699ea022d16SRodney W. Grimes 
2700b943b3c4SYaroslav Tykhiy 	if (guest && noguestmod) {
270102c97492SYaroslav Tykhiy 		reply(550, "Operation not permitted.");
2702b943b3c4SYaroslav Tykhiy 		return (NULL);
2703b943b3c4SYaroslav Tykhiy 	}
27041b0e12d7SYaroslav Tykhiy 	if (lstat(name, &st) < 0) {
2705ea022d16SRodney W. Grimes 		perror_reply(550, name);
2706385f9bf0SYaroslav Tykhiy 		return (NULL);
2707ea022d16SRodney W. Grimes 	}
270802c97492SYaroslav Tykhiy 	reply(350, "File exists, ready for destination name.");
2709ea022d16SRodney W. Grimes 	return (name);
2710ea022d16SRodney W. Grimes }
2711ea022d16SRodney W. Grimes 
2712ea022d16SRodney W. Grimes void
2713e4bc453cSWarner Losh renamecmd(char *from, char *to)
2714ea022d16SRodney W. Grimes {
271561f891a6SPaul Traina 	struct stat st;
2716ea022d16SRodney W. Grimes 
2717ea022d16SRodney W. Grimes 	LOGCMD2("rename", from, to);
271861f891a6SPaul Traina 
271961f891a6SPaul Traina 	if (guest && (stat(to, &st) == 0)) {
272002c97492SYaroslav Tykhiy 		reply(550, "%s: permission denied.", to);
272161f891a6SPaul Traina 		return;
272261f891a6SPaul Traina 	}
272361f891a6SPaul Traina 
2724ea022d16SRodney W. Grimes 	if (rename(from, to) < 0)
2725ea022d16SRodney W. Grimes 		perror_reply(550, "rename");
2726ea022d16SRodney W. Grimes 	else
2727ea022d16SRodney W. Grimes 		ack("RNTO");
2728ea022d16SRodney W. Grimes }
2729ea022d16SRodney W. Grimes 
2730ea022d16SRodney W. Grimes static void
2731e4bc453cSWarner Losh dolog(struct sockaddr *who)
2732ea022d16SRodney W. Grimes {
27337cdd3cb7SYaroslav Tykhiy 	char who_name[NI_MAXHOST];
27344dd8b5abSYoshinobu Inoue 
27354dd8b5abSYoshinobu Inoue 	realhostname_sa(remotehost, sizeof(remotehost) - 1, who, who->sa_len);
273604683b2cSYaroslav Tykhiy 	remotehost[sizeof(remotehost) - 1] = 0;
27377cdd3cb7SYaroslav Tykhiy 	if (getnameinfo(who, who->sa_len,
27387cdd3cb7SYaroslav Tykhiy 		who_name, sizeof(who_name) - 1, NULL, 0, NI_NUMERICHOST))
27397cdd3cb7SYaroslav Tykhiy 			*who_name = 0;
27407cdd3cb7SYaroslav Tykhiy 	who_name[sizeof(who_name) - 1] = 0;
2741ea022d16SRodney W. Grimes 
2742ea022d16SRodney W. Grimes #ifdef SETPROCTITLE
2743ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
2744ea4e54b9SDavid Nugent 	if (thishost != firsthost)
2745ea4e54b9SDavid Nugent 		snprintf(proctitle, sizeof(proctitle), "%s: connected (to %s)",
2746ea4e54b9SDavid Nugent 			 remotehost, hostname);
2747ea4e54b9SDavid Nugent 	else
2748ea4e54b9SDavid Nugent #endif
2749ea4e54b9SDavid Nugent 		snprintf(proctitle, sizeof(proctitle), "%s: connected",
2750ea4e54b9SDavid Nugent 			 remotehost);
2751b63e1fe2SPeter Wemm 	setproctitle("%s", proctitle);
2752ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */
2753ea022d16SRodney W. Grimes 
2754ea4e54b9SDavid Nugent 	if (logging) {
2755ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
2756ea4e54b9SDavid Nugent 		if (thishost != firsthost)
27577cdd3cb7SYaroslav Tykhiy 			syslog(LOG_INFO, "connection from %s (%s) to %s",
27587cdd3cb7SYaroslav Tykhiy 			       remotehost, who_name, hostname);
2759ea4e54b9SDavid Nugent 		else
2760ea4e54b9SDavid Nugent #endif
27617cdd3cb7SYaroslav Tykhiy 			syslog(LOG_INFO, "connection from %s (%s)",
27627cdd3cb7SYaroslav Tykhiy 			       remotehost, who_name);
2763ea022d16SRodney W. Grimes 	}
2764ea4e54b9SDavid Nugent }
2765ea022d16SRodney W. Grimes 
2766ea022d16SRodney W. Grimes /*
2767ea022d16SRodney W. Grimes  * Record logout in wtmp file
2768ea022d16SRodney W. Grimes  * and exit with supplied status.
2769ea022d16SRodney W. Grimes  */
2770ea022d16SRodney W. Grimes void
2771e4bc453cSWarner Losh dologout(int status)
2772ea022d16SRodney W. Grimes {
2773ea022d16SRodney W. Grimes 
27749f37b1a2SEd Schouten 	if (logged_in && dowtmp) {
277552e7ee74SYaroslav Tykhiy 		(void) seteuid(0);
27769f37b1a2SEd Schouten 		ftpd_logwtmp(wtmpid, NULL, NULL);
2777ea022d16SRodney W. Grimes 	}
2778ea022d16SRodney W. Grimes 	/* beware of flushing buffers after a SIGPIPE */
2779ea022d16SRodney W. Grimes 	_exit(status);
2780ea022d16SRodney W. Grimes }
2781ea022d16SRodney W. Grimes 
2782ea022d16SRodney W. Grimes static void
2783e4bc453cSWarner Losh sigurg(int signo)
2784ea022d16SRodney W. Grimes {
27854b82fc95SYaroslav Tykhiy 
27864b82fc95SYaroslav Tykhiy 	recvurg = 1;
27874b82fc95SYaroslav Tykhiy }
27884b82fc95SYaroslav Tykhiy 
27894b82fc95SYaroslav Tykhiy static void
27904cd51076SYaroslav Tykhiy maskurg(int flag)
27914cd51076SYaroslav Tykhiy {
27924cd51076SYaroslav Tykhiy 	int oerrno;
27934cd51076SYaroslav Tykhiy 	sigset_t sset;
27944cd51076SYaroslav Tykhiy 
27954cd51076SYaroslav Tykhiy 	if (!transflag) {
27964cd51076SYaroslav Tykhiy 		syslog(LOG_ERR, "Internal: maskurg() while no transfer");
27974cd51076SYaroslav Tykhiy 		return;
27984cd51076SYaroslav Tykhiy 	}
27994cd51076SYaroslav Tykhiy 	oerrno = errno;
28004cd51076SYaroslav Tykhiy 	sigemptyset(&sset);
28014cd51076SYaroslav Tykhiy 	sigaddset(&sset, SIGURG);
28024cd51076SYaroslav Tykhiy 	sigprocmask(flag ? SIG_BLOCK : SIG_UNBLOCK, &sset, NULL);
28034cd51076SYaroslav Tykhiy 	errno = oerrno;
28044cd51076SYaroslav Tykhiy }
28054cd51076SYaroslav Tykhiy 
28064cd51076SYaroslav Tykhiy static void
28074cd51076SYaroslav Tykhiy flagxfer(int flag)
28084cd51076SYaroslav Tykhiy {
28094cd51076SYaroslav Tykhiy 
28104cd51076SYaroslav Tykhiy 	if (flag) {
2811f9036ce6SYaroslav Tykhiy 		if (transflag)
2812f9036ce6SYaroslav Tykhiy 			syslog(LOG_ERR, "Internal: flagxfer(1): "
2813f9036ce6SYaroslav Tykhiy 					"transfer already under way");
28144cd51076SYaroslav Tykhiy 		transflag = 1;
281591ae7779SYaroslav Tykhiy 		maskurg(0);
281691ae7779SYaroslav Tykhiy 		recvurg = 0;
281791ae7779SYaroslav Tykhiy 	} else {
2818f9036ce6SYaroslav Tykhiy 		if (!transflag)
2819f9036ce6SYaroslav Tykhiy 			syslog(LOG_ERR, "Internal: flagxfer(0): "
2820f9036ce6SYaroslav Tykhiy 					"no active transfer");
282191ae7779SYaroslav Tykhiy 		maskurg(1);
28224cd51076SYaroslav Tykhiy 		transflag = 0;
28234cd51076SYaroslav Tykhiy 	}
282491ae7779SYaroslav Tykhiy }
28254cd51076SYaroslav Tykhiy 
28264cd51076SYaroslav Tykhiy /*
28274cd51076SYaroslav Tykhiy  * Returns 0 if OK to resume or -1 if abort requested.
28284cd51076SYaroslav Tykhiy  */
28294cd51076SYaroslav Tykhiy static int
2830e4bc453cSWarner Losh myoob(void)
28314b82fc95SYaroslav Tykhiy {
2832ea022d16SRodney W. Grimes 	char *cp;
2833f0b40b1cSColin Percival 	int ret;
2834ea022d16SRodney W. Grimes 
28354cd51076SYaroslav Tykhiy 	if (!transflag) {
28364cd51076SYaroslav Tykhiy 		syslog(LOG_ERR, "Internal: myoob() while no transfer");
28374cd51076SYaroslav Tykhiy 		return (0);
28384cd51076SYaroslav Tykhiy 	}
2839ea022d16SRodney W. Grimes 	cp = tmpline;
2840f03ef840SBaptiste Daroussin 	ret = get_line(cp, 7, stdin);
2841f0b40b1cSColin Percival 	if (ret == -1) {
2842ea022d16SRodney W. Grimes 		reply(221, "You could at least say goodbye.");
2843ea022d16SRodney W. Grimes 		dologout(0);
2844f0b40b1cSColin Percival 	} else if (ret == -2) {
2845f0b40b1cSColin Percival 		/* Ignore truncated command. */
2846f0b40b1cSColin Percival 		return (0);
2847ea022d16SRodney W. Grimes 	}
2848ea022d16SRodney W. Grimes 	upper(cp);
2849ea022d16SRodney W. Grimes 	if (strcmp(cp, "ABOR\r\n") == 0) {
2850ea022d16SRodney W. Grimes 		tmpline[0] = '\0';
2851ea022d16SRodney W. Grimes 		reply(426, "Transfer aborted. Data connection closed.");
285202c97492SYaroslav Tykhiy 		reply(226, "Abort successful.");
28534cd51076SYaroslav Tykhiy 		return (-1);
2854ea022d16SRodney W. Grimes 	}
2855ea022d16SRodney W. Grimes 	if (strcmp(cp, "STAT\r\n") == 0) {
28569db4bbf3SMichael Haro 		tmpline[0] = '\0';
28570e519c96SYaroslav Tykhiy 		if (file_size != -1)
285802c97492SYaroslav Tykhiy 			reply(213, "Status: %jd of %jd bytes transferred.",
2859a57e1ef0SYaroslav Tykhiy 				   (intmax_t)byte_count, (intmax_t)file_size);
2860ea022d16SRodney W. Grimes 		else
286102c97492SYaroslav Tykhiy 			reply(213, "Status: %jd bytes transferred.",
2862a57e1ef0SYaroslav Tykhiy 				   (intmax_t)byte_count);
2863ea022d16SRodney W. Grimes 	}
28644cd51076SYaroslav Tykhiy 	return (0);
2865ea022d16SRodney W. Grimes }
2866ea022d16SRodney W. Grimes 
2867ea022d16SRodney W. Grimes /*
2868ea022d16SRodney W. Grimes  * Note: a response of 425 is not mentioned as a possible response to
2869ea022d16SRodney W. Grimes  *	the PASV command in RFC959. However, it has been blessed as
2870ea022d16SRodney W. Grimes  *	a legitimate response by Jon Postel in a telephone conversation
2871ea022d16SRodney W. Grimes  *	with Rick Adams on 25 Jan 89.
2872ea022d16SRodney W. Grimes  */
2873ea022d16SRodney W. Grimes void
2874e4bc453cSWarner Losh passive(void)
2875ea022d16SRodney W. Grimes {
287678e3eed0SStefan Farfeleder 	socklen_t len;
287778e3eed0SStefan Farfeleder 	int on;
2878ea022d16SRodney W. Grimes 	char *p, *a;
2879ea022d16SRodney W. Grimes 
288061f891a6SPaul Traina 	if (pdata >= 0)		/* close old port if one set */
288161f891a6SPaul Traina 		close(pdata);
288261f891a6SPaul Traina 
28834dd8b5abSYoshinobu Inoue 	pdata = socket(ctrl_addr.su_family, SOCK_STREAM, 0);
2884ea022d16SRodney W. Grimes 	if (pdata < 0) {
2885ea022d16SRodney W. Grimes 		perror_reply(425, "Can't open passive connection");
2886ea022d16SRodney W. Grimes 		return;
2887ea022d16SRodney W. Grimes 	}
28888af7c9a3SYaroslav Tykhiy 	on = 1;
28898af7c9a3SYaroslav Tykhiy 	if (setsockopt(pdata, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
28908af7c9a3SYaroslav Tykhiy 		syslog(LOG_WARNING, "pdata setsockopt (SO_REUSEADDR): %m");
28914c450ad7SPaul Traina 
289252e7ee74SYaroslav Tykhiy 	(void) seteuid(0);
289361f891a6SPaul Traina 
2894dacc9752SPaul Traina #ifdef IP_PORTRANGE
28954dd8b5abSYoshinobu Inoue 	if (ctrl_addr.su_family == AF_INET) {
28968af7c9a3SYaroslav Tykhiy 	    on = restricted_data_ports ? IP_PORTRANGE_HIGH
2897dacc9752SPaul Traina 				       : IP_PORTRANGE_DEFAULT;
2898dacc9752SPaul Traina 
289940e9d39eSPeter Wemm 	    if (setsockopt(pdata, IPPROTO_IP, IP_PORTRANGE,
290057d4ef07SYaroslav Tykhiy 			    &on, sizeof(on)) < 0)
29014c450ad7SPaul Traina 		    goto pasv_error;
29024c450ad7SPaul Traina 	}
2903dacc9752SPaul Traina #endif
29042db39860SNick Sayer #ifdef IPV6_PORTRANGE
29052db39860SNick Sayer 	if (ctrl_addr.su_family == AF_INET6) {
29068af7c9a3SYaroslav Tykhiy 	    on = restricted_data_ports ? IPV6_PORTRANGE_HIGH
29072db39860SNick Sayer 				       : IPV6_PORTRANGE_DEFAULT;
29082db39860SNick Sayer 
29092db39860SNick Sayer 	    if (setsockopt(pdata, IPPROTO_IPV6, IPV6_PORTRANGE,
291057d4ef07SYaroslav Tykhiy 			    &on, sizeof(on)) < 0)
29112db39860SNick Sayer 		    goto pasv_error;
29122db39860SNick Sayer 	}
29132db39860SNick Sayer #endif
291440e9d39eSPeter Wemm 
2915ea022d16SRodney W. Grimes 	pasv_addr = ctrl_addr;
29164dd8b5abSYoshinobu Inoue 	pasv_addr.su_port = 0;
29174dd8b5abSYoshinobu Inoue 	if (bind(pdata, (struct sockaddr *)&pasv_addr, pasv_addr.su_len) < 0)
2918ea022d16SRodney W. Grimes 		goto pasv_error;
291961f891a6SPaul Traina 
292052e7ee74SYaroslav Tykhiy 	(void) seteuid(pw->pw_uid);
29214c450ad7SPaul Traina 
2922ea022d16SRodney W. Grimes 	len = sizeof(pasv_addr);
2923ea022d16SRodney W. Grimes 	if (getsockname(pdata, (struct sockaddr *) &pasv_addr, &len) < 0)
2924ea022d16SRodney W. Grimes 		goto pasv_error;
2925ea022d16SRodney W. Grimes 	if (listen(pdata, 1) < 0)
2926ea022d16SRodney W. Grimes 		goto pasv_error;
29274dd8b5abSYoshinobu Inoue 	if (pasv_addr.su_family == AF_INET)
29284dd8b5abSYoshinobu Inoue 		a = (char *) &pasv_addr.su_sin.sin_addr;
29294dd8b5abSYoshinobu Inoue 	else if (pasv_addr.su_family == AF_INET6 &&
29304dd8b5abSYoshinobu Inoue 		 IN6_IS_ADDR_V4MAPPED(&pasv_addr.su_sin6.sin6_addr))
29314dd8b5abSYoshinobu Inoue 		a = (char *) &pasv_addr.su_sin6.sin6_addr.s6_addr[12];
29324dd8b5abSYoshinobu Inoue 	else
29334dd8b5abSYoshinobu Inoue 		goto pasv_error;
29344dd8b5abSYoshinobu Inoue 
29354dd8b5abSYoshinobu Inoue 	p = (char *) &pasv_addr.su_port;
2936ea022d16SRodney W. Grimes 
2937ea022d16SRodney W. Grimes #define UC(b) (((int) b) & 0xff)
2938ea022d16SRodney W. Grimes 
2939ea022d16SRodney W. Grimes 	reply(227, "Entering Passive Mode (%d,%d,%d,%d,%d,%d)", UC(a[0]),
2940ea022d16SRodney W. Grimes 		UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1]));
2941ea022d16SRodney W. Grimes 	return;
2942ea022d16SRodney W. Grimes 
2943ea022d16SRodney W. Grimes pasv_error:
294452e7ee74SYaroslav Tykhiy 	(void) seteuid(pw->pw_uid);
2945ea022d16SRodney W. Grimes 	(void) close(pdata);
2946ea022d16SRodney W. Grimes 	pdata = -1;
2947ea022d16SRodney W. Grimes 	perror_reply(425, "Can't open passive connection");
2948ea022d16SRodney W. Grimes 	return;
2949ea022d16SRodney W. Grimes }
2950ea022d16SRodney W. Grimes 
2951ea022d16SRodney W. Grimes /*
29524dd8b5abSYoshinobu Inoue  * Long Passive defined in RFC 1639.
29534dd8b5abSYoshinobu Inoue  *     228 Entering Long Passive Mode
29544dd8b5abSYoshinobu Inoue  *         (af, hal, h1, h2, h3,..., pal, p1, p2...)
29554dd8b5abSYoshinobu Inoue  */
29564dd8b5abSYoshinobu Inoue 
29574dd8b5abSYoshinobu Inoue void
2958e4bc453cSWarner Losh long_passive(char *cmd, int pf)
29594dd8b5abSYoshinobu Inoue {
296078e3eed0SStefan Farfeleder 	socklen_t len;
296178e3eed0SStefan Farfeleder 	int on;
29624dd8b5abSYoshinobu Inoue 	char *p, *a;
29634dd8b5abSYoshinobu Inoue 
29644dd8b5abSYoshinobu Inoue 	if (pdata >= 0)		/* close old port if one set */
29654dd8b5abSYoshinobu Inoue 		close(pdata);
29664dd8b5abSYoshinobu Inoue 
29674dd8b5abSYoshinobu Inoue 	if (pf != PF_UNSPEC) {
29684dd8b5abSYoshinobu Inoue 		if (ctrl_addr.su_family != pf) {
29694dd8b5abSYoshinobu Inoue 			switch (ctrl_addr.su_family) {
29704dd8b5abSYoshinobu Inoue 			case AF_INET:
29714dd8b5abSYoshinobu Inoue 				pf = 1;
29724dd8b5abSYoshinobu Inoue 				break;
29734dd8b5abSYoshinobu Inoue 			case AF_INET6:
29744dd8b5abSYoshinobu Inoue 				pf = 2;
29754dd8b5abSYoshinobu Inoue 				break;
29764dd8b5abSYoshinobu Inoue 			default:
29774dd8b5abSYoshinobu Inoue 				pf = 0;
29784dd8b5abSYoshinobu Inoue 				break;
29794dd8b5abSYoshinobu Inoue 			}
29804dd8b5abSYoshinobu Inoue 			/*
29814dd8b5abSYoshinobu Inoue 			 * XXX
29824dd8b5abSYoshinobu Inoue 			 * only EPRT/EPSV ready clients will understand this
29834dd8b5abSYoshinobu Inoue 			 */
29844dd8b5abSYoshinobu Inoue 			if (strcmp(cmd, "EPSV") == 0 && pf) {
29854dd8b5abSYoshinobu Inoue 				reply(522, "Network protocol mismatch, "
29864dd8b5abSYoshinobu Inoue 					"use (%d)", pf);
29874dd8b5abSYoshinobu Inoue 			} else
298802c97492SYaroslav Tykhiy 				reply(501, "Network protocol mismatch."); /*XXX*/
29894dd8b5abSYoshinobu Inoue 
29904dd8b5abSYoshinobu Inoue 			return;
29914dd8b5abSYoshinobu Inoue 		}
29924dd8b5abSYoshinobu Inoue 	}
29934dd8b5abSYoshinobu Inoue 
29944dd8b5abSYoshinobu Inoue 	pdata = socket(ctrl_addr.su_family, SOCK_STREAM, 0);
29954dd8b5abSYoshinobu Inoue 	if (pdata < 0) {
29964dd8b5abSYoshinobu Inoue 		perror_reply(425, "Can't open passive connection");
29974dd8b5abSYoshinobu Inoue 		return;
29984dd8b5abSYoshinobu Inoue 	}
29998af7c9a3SYaroslav Tykhiy 	on = 1;
30008af7c9a3SYaroslav Tykhiy 	if (setsockopt(pdata, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
30018af7c9a3SYaroslav Tykhiy 		syslog(LOG_WARNING, "pdata setsockopt (SO_REUSEADDR): %m");
30024dd8b5abSYoshinobu Inoue 
300352e7ee74SYaroslav Tykhiy 	(void) seteuid(0);
30044dd8b5abSYoshinobu Inoue 
30054dd8b5abSYoshinobu Inoue 	pasv_addr = ctrl_addr;
30064dd8b5abSYoshinobu Inoue 	pasv_addr.su_port = 0;
30074dd8b5abSYoshinobu Inoue 	len = pasv_addr.su_len;
30084dd8b5abSYoshinobu Inoue 
30092db39860SNick Sayer #ifdef IP_PORTRANGE
30102db39860SNick Sayer 	if (ctrl_addr.su_family == AF_INET) {
30118af7c9a3SYaroslav Tykhiy 	    on = restricted_data_ports ? IP_PORTRANGE_HIGH
30122db39860SNick Sayer 				       : IP_PORTRANGE_DEFAULT;
30132db39860SNick Sayer 
30142db39860SNick Sayer 	    if (setsockopt(pdata, IPPROTO_IP, IP_PORTRANGE,
301557d4ef07SYaroslav Tykhiy 			    &on, sizeof(on)) < 0)
30162db39860SNick Sayer 		    goto pasv_error;
30172db39860SNick Sayer 	}
30182db39860SNick Sayer #endif
30192db39860SNick Sayer #ifdef IPV6_PORTRANGE
30202db39860SNick Sayer 	if (ctrl_addr.su_family == AF_INET6) {
30218af7c9a3SYaroslav Tykhiy 	    on = restricted_data_ports ? IPV6_PORTRANGE_HIGH
30222db39860SNick Sayer 				       : IPV6_PORTRANGE_DEFAULT;
30232db39860SNick Sayer 
30242db39860SNick Sayer 	    if (setsockopt(pdata, IPPROTO_IPV6, IPV6_PORTRANGE,
302557d4ef07SYaroslav Tykhiy 			    &on, sizeof(on)) < 0)
30262db39860SNick Sayer 		    goto pasv_error;
30272db39860SNick Sayer 	}
30282db39860SNick Sayer #endif
30292db39860SNick Sayer 
30304dd8b5abSYoshinobu Inoue 	if (bind(pdata, (struct sockaddr *)&pasv_addr, len) < 0)
30314dd8b5abSYoshinobu Inoue 		goto pasv_error;
30324dd8b5abSYoshinobu Inoue 
303352e7ee74SYaroslav Tykhiy 	(void) seteuid(pw->pw_uid);
30344dd8b5abSYoshinobu Inoue 
30354dd8b5abSYoshinobu Inoue 	if (getsockname(pdata, (struct sockaddr *) &pasv_addr, &len) < 0)
30364dd8b5abSYoshinobu Inoue 		goto pasv_error;
30374dd8b5abSYoshinobu Inoue 	if (listen(pdata, 1) < 0)
30384dd8b5abSYoshinobu Inoue 		goto pasv_error;
30394dd8b5abSYoshinobu Inoue 
30404dd8b5abSYoshinobu Inoue #define UC(b) (((int) b) & 0xff)
30414dd8b5abSYoshinobu Inoue 
30424dd8b5abSYoshinobu Inoue 	if (strcmp(cmd, "LPSV") == 0) {
30434dd8b5abSYoshinobu Inoue 		p = (char *)&pasv_addr.su_port;
30444dd8b5abSYoshinobu Inoue 		switch (pasv_addr.su_family) {
30454dd8b5abSYoshinobu Inoue 		case AF_INET:
30464dd8b5abSYoshinobu Inoue 			a = (char *) &pasv_addr.su_sin.sin_addr;
30474dd8b5abSYoshinobu Inoue 		v4_reply:
30484dd8b5abSYoshinobu Inoue 			reply(228,
30494dd8b5abSYoshinobu Inoue "Entering Long Passive Mode (%d,%d,%d,%d,%d,%d,%d,%d,%d)",
30504dd8b5abSYoshinobu Inoue 			      4, 4, UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
30514dd8b5abSYoshinobu Inoue 			      2, UC(p[0]), UC(p[1]));
30524dd8b5abSYoshinobu Inoue 			return;
30534dd8b5abSYoshinobu Inoue 		case AF_INET6:
30544dd8b5abSYoshinobu Inoue 			if (IN6_IS_ADDR_V4MAPPED(&pasv_addr.su_sin6.sin6_addr)) {
30554dd8b5abSYoshinobu Inoue 				a = (char *) &pasv_addr.su_sin6.sin6_addr.s6_addr[12];
30564dd8b5abSYoshinobu Inoue 				goto v4_reply;
30574dd8b5abSYoshinobu Inoue 			}
30584dd8b5abSYoshinobu Inoue 			a = (char *) &pasv_addr.su_sin6.sin6_addr;
30594dd8b5abSYoshinobu Inoue 			reply(228,
30604dd8b5abSYoshinobu Inoue "Entering Long Passive Mode "
30614dd8b5abSYoshinobu Inoue "(%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d)",
30624dd8b5abSYoshinobu Inoue 			      6, 16, UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
30634dd8b5abSYoshinobu Inoue 			      UC(a[4]), UC(a[5]), UC(a[6]), UC(a[7]),
30644dd8b5abSYoshinobu Inoue 			      UC(a[8]), UC(a[9]), UC(a[10]), UC(a[11]),
30654dd8b5abSYoshinobu Inoue 			      UC(a[12]), UC(a[13]), UC(a[14]), UC(a[15]),
30664dd8b5abSYoshinobu Inoue 			      2, UC(p[0]), UC(p[1]));
30674dd8b5abSYoshinobu Inoue 			return;
30684dd8b5abSYoshinobu Inoue 		}
30694dd8b5abSYoshinobu Inoue 	} else if (strcmp(cmd, "EPSV") == 0) {
30704dd8b5abSYoshinobu Inoue 		switch (pasv_addr.su_family) {
30714dd8b5abSYoshinobu Inoue 		case AF_INET:
30724dd8b5abSYoshinobu Inoue 		case AF_INET6:
30734dd8b5abSYoshinobu Inoue 			reply(229, "Entering Extended Passive Mode (|||%d|)",
30744dd8b5abSYoshinobu Inoue 				ntohs(pasv_addr.su_port));
30754dd8b5abSYoshinobu Inoue 			return;
30764dd8b5abSYoshinobu Inoue 		}
30774dd8b5abSYoshinobu Inoue 	} else {
30784dd8b5abSYoshinobu Inoue 		/* more proper error code? */
30794dd8b5abSYoshinobu Inoue 	}
30804dd8b5abSYoshinobu Inoue 
30814dd8b5abSYoshinobu Inoue pasv_error:
308252e7ee74SYaroslav Tykhiy 	(void) seteuid(pw->pw_uid);
30834dd8b5abSYoshinobu Inoue 	(void) close(pdata);
30844dd8b5abSYoshinobu Inoue 	pdata = -1;
30854dd8b5abSYoshinobu Inoue 	perror_reply(425, "Can't open passive connection");
30864dd8b5abSYoshinobu Inoue 	return;
30874dd8b5abSYoshinobu Inoue }
30884dd8b5abSYoshinobu Inoue 
30894dd8b5abSYoshinobu Inoue /*
3090a117c345SYaroslav Tykhiy  * Generate unique name for file with basename "local"
3091a117c345SYaroslav Tykhiy  * and open the file in order to avoid possible races.
3092a117c345SYaroslav Tykhiy  * Try "local" first, then "local.1", "local.2" etc, up to "local.99".
3093a117c345SYaroslav Tykhiy  * Return descriptor to the file, set "name" to its name.
3094a117c345SYaroslav Tykhiy  *
3095ea022d16SRodney W. Grimes  * Generates failure reply on error.
3096ea022d16SRodney W. Grimes  */
3097a117c345SYaroslav Tykhiy static int
3098a117c345SYaroslav Tykhiy guniquefd(char *local, char **name)
3099ea022d16SRodney W. Grimes {
3100ea022d16SRodney W. Grimes 	static char new[MAXPATHLEN];
3101ea022d16SRodney W. Grimes 	struct stat st;
3102ea022d16SRodney W. Grimes 	char *cp;
3103a117c345SYaroslav Tykhiy 	int count;
3104a117c345SYaroslav Tykhiy 	int fd;
3105ea022d16SRodney W. Grimes 
3106ea022d16SRodney W. Grimes 	cp = strrchr(local, '/');
3107ea022d16SRodney W. Grimes 	if (cp)
3108ea022d16SRodney W. Grimes 		*cp = '\0';
3109ea022d16SRodney W. Grimes 	if (stat(cp ? local : ".", &st) < 0) {
3110ea022d16SRodney W. Grimes 		perror_reply(553, cp ? local : ".");
3111a117c345SYaroslav Tykhiy 		return (-1);
3112ea022d16SRodney W. Grimes 	}
3113a117c345SYaroslav Tykhiy 	if (cp) {
3114a117c345SYaroslav Tykhiy 		/*
3115a117c345SYaroslav Tykhiy 		 * Let not overwrite dirname with counter suffix.
3116a117c345SYaroslav Tykhiy 		 * -4 is for /nn\0
3117a117c345SYaroslav Tykhiy 		 * In this extreme case dot won't be put in front of suffix.
3118a117c345SYaroslav Tykhiy 		 */
3119a117c345SYaroslav Tykhiy 		if (strlen(local) > sizeof(new) - 4) {
312002c97492SYaroslav Tykhiy 			reply(553, "Pathname too long.");
3121a117c345SYaroslav Tykhiy 			return (-1);
3122a117c345SYaroslav Tykhiy 		}
3123ea022d16SRodney W. Grimes 		*cp = '/';
3124a117c345SYaroslav Tykhiy 	}
3125e760ef2cSWarner Losh 	/* -4 is for the .nn<null> we put on the end below */
3126e760ef2cSWarner Losh 	(void) snprintf(new, sizeof(new) - 4, "%s", local);
3127ea022d16SRodney W. Grimes 	cp = new + strlen(new);
3128a117c345SYaroslav Tykhiy 	/*
3129a117c345SYaroslav Tykhiy 	 * Don't generate dotfile unless requested explicitly.
3130a117c345SYaroslav Tykhiy 	 * This covers the case when basename gets truncated off
3131a117c345SYaroslav Tykhiy 	 * by buffer size.
3132a117c345SYaroslav Tykhiy 	 */
3133a117c345SYaroslav Tykhiy 	if (cp > new && cp[-1] != '/')
3134ea022d16SRodney W. Grimes 		*cp++ = '.';
3135a117c345SYaroslav Tykhiy 	for (count = 0; count < 100; count++) {
3136a117c345SYaroslav Tykhiy 		/* At count 0 try unmodified name */
3137a117c345SYaroslav Tykhiy 		if (count)
3138ea022d16SRodney W. Grimes 			(void)sprintf(cp, "%d", count);
3139a117c345SYaroslav Tykhiy 		if ((fd = open(count ? new : local,
3140a117c345SYaroslav Tykhiy 		    O_RDWR | O_CREAT | O_EXCL, 0666)) >= 0) {
3141a117c345SYaroslav Tykhiy 			*name = count ? new : local;
3142a117c345SYaroslav Tykhiy 			return (fd);
3143a117c345SYaroslav Tykhiy 		}
314488b70721SYaroslav Tykhiy 		if (errno != EEXIST) {
314550618d61SYaroslav Tykhiy 			perror_reply(553, count ? new : local);
314688b70721SYaroslav Tykhiy 			return (-1);
314788b70721SYaroslav Tykhiy 		}
3148ea022d16SRodney W. Grimes 	}
3149ea022d16SRodney W. Grimes 	reply(452, "Unique file name cannot be created.");
3150a117c345SYaroslav Tykhiy 	return (-1);
3151ea022d16SRodney W. Grimes }
3152ea022d16SRodney W. Grimes 
3153ea022d16SRodney W. Grimes /*
3154ea022d16SRodney W. Grimes  * Format and send reply containing system error number.
3155ea022d16SRodney W. Grimes  */
3156ea022d16SRodney W. Grimes void
3157e4bc453cSWarner Losh perror_reply(int code, char *string)
3158ea022d16SRodney W. Grimes {
3159ea022d16SRodney W. Grimes 
3160ea022d16SRodney W. Grimes 	reply(code, "%s: %s.", string, strerror(errno));
3161ea022d16SRodney W. Grimes }
3162ea022d16SRodney W. Grimes 
3163ea022d16SRodney W. Grimes static char *onefile[] = {
3164ea022d16SRodney W. Grimes 	"",
3165ea022d16SRodney W. Grimes 	0
3166ea022d16SRodney W. Grimes };
3167ea022d16SRodney W. Grimes 
3168ea022d16SRodney W. Grimes void
3169e4bc453cSWarner Losh send_file_list(char *whichf)
3170ea022d16SRodney W. Grimes {
3171ea022d16SRodney W. Grimes 	struct stat st;
3172ea022d16SRodney W. Grimes 	DIR *dirp = NULL;
3173ea022d16SRodney W. Grimes 	struct dirent *dir;
3174ea022d16SRodney W. Grimes 	FILE *dout = NULL;
3175ea022d16SRodney W. Grimes 	char **dirlist, *dirname;
3176ea022d16SRodney W. Grimes 	int simple = 0;
3177ea022d16SRodney W. Grimes 	int freeglob = 0;
3178ea022d16SRodney W. Grimes 	glob_t gl;
3179ea022d16SRodney W. Grimes 
3180ea022d16SRodney W. Grimes 	if (strpbrk(whichf, "~{[*?") != NULL) {
318112da320bSMike Heffner 		int flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_TILDE;
3182ea022d16SRodney W. Grimes 
3183ea022d16SRodney W. Grimes 		memset(&gl, 0, sizeof(gl));
31846d10cb2fSJonathan Lemon 		gl.gl_matchc = MAXGLOBARGS;
318575dc5f1aSMike Heffner 		flags |= GLOB_LIMIT;
3186ea022d16SRodney W. Grimes 		freeglob = 1;
3187ea022d16SRodney W. Grimes 		if (glob(whichf, flags, 0, &gl)) {
318802c97492SYaroslav Tykhiy 			reply(550, "No matching files found.");
3189ea022d16SRodney W. Grimes 			goto out;
3190ea022d16SRodney W. Grimes 		} else if (gl.gl_pathc == 0) {
3191ea022d16SRodney W. Grimes 			errno = ENOENT;
3192ea022d16SRodney W. Grimes 			perror_reply(550, whichf);
3193ea022d16SRodney W. Grimes 			goto out;
3194ea022d16SRodney W. Grimes 		}
3195ea022d16SRodney W. Grimes 		dirlist = gl.gl_pathv;
3196ea022d16SRodney W. Grimes 	} else {
3197ea022d16SRodney W. Grimes 		onefile[0] = whichf;
3198ea022d16SRodney W. Grimes 		dirlist = onefile;
3199ea022d16SRodney W. Grimes 		simple = 1;
3200ea022d16SRodney W. Grimes 	}
3201ea022d16SRodney W. Grimes 
32029aca17cbSMark Murray 	while ((dirname = *dirlist++)) {
3203ea022d16SRodney W. Grimes 		if (stat(dirname, &st) < 0) {
3204ea022d16SRodney W. Grimes 			/*
3205ea022d16SRodney W. Grimes 			 * If user typed "ls -l", etc, and the client
3206ea022d16SRodney W. Grimes 			 * used NLST, do what the user meant.
3207ea022d16SRodney W. Grimes 			 */
3208ea022d16SRodney W. Grimes 			if (dirname[0] == '-' && *dirlist == NULL &&
32094cd51076SYaroslav Tykhiy 			    dout == NULL)
3210af85d782SDavid Nugent 				retrieve(_PATH_LS " %s", dirname);
32114cd51076SYaroslav Tykhiy 			else
3212ea022d16SRodney W. Grimes 				perror_reply(550, whichf);
3213ea022d16SRodney W. Grimes 			goto out;
3214ea022d16SRodney W. Grimes 		}
3215ea022d16SRodney W. Grimes 
3216ea022d16SRodney W. Grimes 		if (S_ISREG(st.st_mode)) {
3217ea022d16SRodney W. Grimes 			if (dout == NULL) {
32180e519c96SYaroslav Tykhiy 				dout = dataconn("file list", -1, "w");
3219ea022d16SRodney W. Grimes 				if (dout == NULL)
3220ea022d16SRodney W. Grimes 					goto out;
32214cd51076SYaroslav Tykhiy 				STARTXFER;
3222ea022d16SRodney W. Grimes 			}
32234cd51076SYaroslav Tykhiy 			START_UNSAFE;
3224ea022d16SRodney W. Grimes 			fprintf(dout, "%s%s\n", dirname,
3225ea022d16SRodney W. Grimes 				type == TYPE_A ? "\r" : "");
32264cd51076SYaroslav Tykhiy 			END_UNSAFE;
32274cd51076SYaroslav Tykhiy 			if (ferror(dout))
32284cd51076SYaroslav Tykhiy 				goto data_err;
32294cd51076SYaroslav Tykhiy 			byte_count += strlen(dirname) +
32304cd51076SYaroslav Tykhiy 				      (type == TYPE_A ? 2 : 1);
32314cd51076SYaroslav Tykhiy 			CHECKOOB(goto abrt);
3232ea022d16SRodney W. Grimes 			continue;
3233ea022d16SRodney W. Grimes 		} else if (!S_ISDIR(st.st_mode))
3234ea022d16SRodney W. Grimes 			continue;
3235ea022d16SRodney W. Grimes 
3236ea022d16SRodney W. Grimes 		if ((dirp = opendir(dirname)) == NULL)
3237ea022d16SRodney W. Grimes 			continue;
3238ea022d16SRodney W. Grimes 
3239ea022d16SRodney W. Grimes 		while ((dir = readdir(dirp)) != NULL) {
3240ea022d16SRodney W. Grimes 			char nbuf[MAXPATHLEN];
3241ea022d16SRodney W. Grimes 
32424cd51076SYaroslav Tykhiy 			CHECKOOB(goto abrt);
32434b82fc95SYaroslav Tykhiy 
3244ea022d16SRodney W. Grimes 			if (dir->d_name[0] == '.' && dir->d_namlen == 1)
3245ea022d16SRodney W. Grimes 				continue;
3246ea022d16SRodney W. Grimes 			if (dir->d_name[0] == '.' && dir->d_name[1] == '.' &&
3247ea022d16SRodney W. Grimes 			    dir->d_namlen == 2)
3248ea022d16SRodney W. Grimes 				continue;
3249ea022d16SRodney W. Grimes 
3250e760ef2cSWarner Losh 			snprintf(nbuf, sizeof(nbuf),
3251e760ef2cSWarner Losh 				"%s/%s", dirname, dir->d_name);
3252ea022d16SRodney W. Grimes 
3253ea022d16SRodney W. Grimes 			/*
3254ea022d16SRodney W. Grimes 			 * We have to do a stat to insure it's
3255ea022d16SRodney W. Grimes 			 * not a directory or special file.
3256ea022d16SRodney W. Grimes 			 */
3257ea022d16SRodney W. Grimes 			if (simple || (stat(nbuf, &st) == 0 &&
3258ea022d16SRodney W. Grimes 			    S_ISREG(st.st_mode))) {
3259ea022d16SRodney W. Grimes 				if (dout == NULL) {
32600e519c96SYaroslav Tykhiy 					dout = dataconn("file list", -1, "w");
3261ea022d16SRodney W. Grimes 					if (dout == NULL)
3262ea022d16SRodney W. Grimes 						goto out;
32634cd51076SYaroslav Tykhiy 					STARTXFER;
3264ea022d16SRodney W. Grimes 				}
32654cd51076SYaroslav Tykhiy 				START_UNSAFE;
3266ea022d16SRodney W. Grimes 				if (nbuf[0] == '.' && nbuf[1] == '/')
3267ea022d16SRodney W. Grimes 					fprintf(dout, "%s%s\n", &nbuf[2],
3268ea022d16SRodney W. Grimes 						type == TYPE_A ? "\r" : "");
3269ea022d16SRodney W. Grimes 				else
3270ea022d16SRodney W. Grimes 					fprintf(dout, "%s%s\n", nbuf,
3271ea022d16SRodney W. Grimes 						type == TYPE_A ? "\r" : "");
32724cd51076SYaroslav Tykhiy 				END_UNSAFE;
32734cd51076SYaroslav Tykhiy 				if (ferror(dout))
32744cd51076SYaroslav Tykhiy 					goto data_err;
32754cd51076SYaroslav Tykhiy 				byte_count += strlen(nbuf) +
32764cd51076SYaroslav Tykhiy 					      (type == TYPE_A ? 2 : 1);
32774cd51076SYaroslav Tykhiy 				CHECKOOB(goto abrt);
3278ea022d16SRodney W. Grimes 			}
3279ea022d16SRodney W. Grimes 		}
3280ea022d16SRodney W. Grimes 		(void) closedir(dirp);
32814cd51076SYaroslav Tykhiy 		dirp = NULL;
3282ea022d16SRodney W. Grimes 	}
3283ea022d16SRodney W. Grimes 
3284ea022d16SRodney W. Grimes 	if (dout == NULL)
3285ea022d16SRodney W. Grimes 		reply(550, "No files found.");
32864cd51076SYaroslav Tykhiy 	else if (ferror(dout))
32874cd51076SYaroslav Tykhiy data_err:	perror_reply(550, "Data connection");
3288ea022d16SRodney W. Grimes 	else
3289ea022d16SRodney W. Grimes 		reply(226, "Transfer complete.");
32904cd51076SYaroslav Tykhiy out:
32914cd51076SYaroslav Tykhiy 	if (dout) {
32924cd51076SYaroslav Tykhiy 		ENDXFER;
32934cd51076SYaroslav Tykhiy abrt:
3294ea022d16SRodney W. Grimes 		(void) fclose(dout);
3295ea022d16SRodney W. Grimes 		data = -1;
3296ea022d16SRodney W. Grimes 		pdata = -1;
32974cd51076SYaroslav Tykhiy 	}
32984cd51076SYaroslav Tykhiy 	if (dirp)
32994cd51076SYaroslav Tykhiy 		(void) closedir(dirp);
3300ea022d16SRodney W. Grimes 	if (freeglob) {
3301ea022d16SRodney W. Grimes 		freeglob = 0;
3302ea022d16SRodney W. Grimes 		globfree(&gl);
3303ea022d16SRodney W. Grimes 	}
3304ea022d16SRodney W. Grimes }
3305ea022d16SRodney W. Grimes 
3306cf09a206SDavid Greenman void
3307e4bc453cSWarner Losh reapchild(int signo)
3308cf09a206SDavid Greenman {
3309de9b6c03SYaroslav Tykhiy 	while (waitpid(-1, NULL, WNOHANG) > 0);
3310cf09a206SDavid Greenman }
3311cf09a206SDavid Greenman 
3312b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE
3313ea022d16SRodney W. Grimes /*
3314ea022d16SRodney W. Grimes  * Clobber argv so ps will show what we're doing.  (Stolen from sendmail.)
3315ea022d16SRodney W. Grimes  * Warning, since this is usually started from inetd.conf, it often doesn't
3316ea022d16SRodney W. Grimes  * have much of an environment or arglist to overwrite.
3317ea022d16SRodney W. Grimes  */
3318ea022d16SRodney W. Grimes void
3319ea022d16SRodney W. Grimes setproctitle(const char *fmt, ...)
3320ea022d16SRodney W. Grimes {
3321ea022d16SRodney W. Grimes 	int i;
3322ea022d16SRodney W. Grimes 	va_list ap;
3323ea022d16SRodney W. Grimes 	char *p, *bp, ch;
3324ea022d16SRodney W. Grimes 	char buf[LINE_MAX];
3325ea022d16SRodney W. Grimes 
3326ea022d16SRodney W. Grimes 	va_start(ap, fmt);
3327ea022d16SRodney W. Grimes 	(void)vsnprintf(buf, sizeof(buf), fmt, ap);
3328ea022d16SRodney W. Grimes 
3329ea022d16SRodney W. Grimes 	/* make ps print our process name */
3330ea022d16SRodney W. Grimes 	p = Argv[0];
3331ea022d16SRodney W. Grimes 	*p++ = '-';
3332ea022d16SRodney W. Grimes 
3333ea022d16SRodney W. Grimes 	i = strlen(buf);
3334ea022d16SRodney W. Grimes 	if (i > LastArgv - p - 2) {
3335ea022d16SRodney W. Grimes 		i = LastArgv - p - 2;
3336ea022d16SRodney W. Grimes 		buf[i] = '\0';
3337ea022d16SRodney W. Grimes 	}
3338ea022d16SRodney W. Grimes 	bp = buf;
3339ea022d16SRodney W. Grimes 	while (ch = *bp++)
3340ea022d16SRodney W. Grimes 		if (ch != '\n' && ch != '\r')
3341ea022d16SRodney W. Grimes 			*p++ = ch;
3342ea022d16SRodney W. Grimes 	while (p < LastArgv)
3343ea022d16SRodney W. Grimes 		*p++ = ' ';
3344ea022d16SRodney W. Grimes }
3345b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */
33463eb568f2SGuido van Rooij 
334761f891a6SPaul Traina static void
33486b2dee6bSYaroslav Tykhiy appendf(char **strp, char *fmt, ...)
33496b2dee6bSYaroslav Tykhiy {
33506b2dee6bSYaroslav Tykhiy 	va_list ap;
33516b2dee6bSYaroslav Tykhiy 	char *ostr, *p;
33526b2dee6bSYaroslav Tykhiy 
33536b2dee6bSYaroslav Tykhiy 	va_start(ap, fmt);
33546b2dee6bSYaroslav Tykhiy 	vasprintf(&p, fmt, ap);
33556b2dee6bSYaroslav Tykhiy 	va_end(ap);
33566b2dee6bSYaroslav Tykhiy 	if (p == NULL)
33576b2dee6bSYaroslav Tykhiy 		fatalerror("Ran out of memory.");
33586b2dee6bSYaroslav Tykhiy 	if (*strp == NULL)
33596b2dee6bSYaroslav Tykhiy 		*strp = p;
33606b2dee6bSYaroslav Tykhiy 	else {
33616b2dee6bSYaroslav Tykhiy 		ostr = *strp;
33626b2dee6bSYaroslav Tykhiy 		asprintf(strp, "%s%s", ostr, p);
33636b2dee6bSYaroslav Tykhiy 		if (*strp == NULL)
33646b2dee6bSYaroslav Tykhiy 			fatalerror("Ran out of memory.");
33656b2dee6bSYaroslav Tykhiy 		free(ostr);
33666b2dee6bSYaroslav Tykhiy 	}
33676b2dee6bSYaroslav Tykhiy }
33686b2dee6bSYaroslav Tykhiy 
33696b2dee6bSYaroslav Tykhiy static void
33706b2dee6bSYaroslav Tykhiy logcmd(char *cmd, char *file1, char *file2, off_t cnt)
33716b2dee6bSYaroslav Tykhiy {
33726b2dee6bSYaroslav Tykhiy 	char *msg = NULL;
33736b2dee6bSYaroslav Tykhiy 	char wd[MAXPATHLEN + 1];
33746b2dee6bSYaroslav Tykhiy 
33756b2dee6bSYaroslav Tykhiy 	if (logging <= 1)
33766b2dee6bSYaroslav Tykhiy 		return;
3377e897216fSYaroslav Tykhiy 
33786b2dee6bSYaroslav Tykhiy 	if (getcwd(wd, sizeof(wd) - 1) == NULL)
33796b2dee6bSYaroslav Tykhiy 		strcpy(wd, strerror(errno));
33806b2dee6bSYaroslav Tykhiy 
33816b2dee6bSYaroslav Tykhiy 	appendf(&msg, "%s", cmd);
33826b2dee6bSYaroslav Tykhiy 	if (file1)
33836b2dee6bSYaroslav Tykhiy 		appendf(&msg, " %s", file1);
33846b2dee6bSYaroslav Tykhiy 	if (file2)
33856b2dee6bSYaroslav Tykhiy 		appendf(&msg, " %s", file2);
33866b2dee6bSYaroslav Tykhiy 	if (cnt >= 0)
33876b2dee6bSYaroslav Tykhiy 		appendf(&msg, " = %jd bytes", (intmax_t)cnt);
3388e897216fSYaroslav Tykhiy 	appendf(&msg, " (wd: %s", wd);
3389215a9f9dSYaroslav Tykhiy 	if (guest || dochroot)
3390e897216fSYaroslav Tykhiy 		appendf(&msg, "; chrooted");
3391e897216fSYaroslav Tykhiy 	appendf(&msg, ")");
33926b2dee6bSYaroslav Tykhiy 	syslog(LOG_INFO, "%s", msg);
33936b2dee6bSYaroslav Tykhiy 	free(msg);
33946b2dee6bSYaroslav Tykhiy }
33956b2dee6bSYaroslav Tykhiy 
33966b2dee6bSYaroslav Tykhiy static void
3397e4bc453cSWarner Losh logxfer(char *name, off_t size, time_t start)
33983eb568f2SGuido van Rooij {
33998c1c21f2SYaroslav Tykhiy 	char buf[MAXPATHLEN + 1024];
34003eb568f2SGuido van Rooij 	char path[MAXPATHLEN + 1];
3401158a00b2SJohn Birrell 	time_t now;
34023eb568f2SGuido van Rooij 
34038c1c21f2SYaroslav Tykhiy 	if (statfd >= 0) {
34043eb568f2SGuido van Rooij 		time(&now);
34058c1c21f2SYaroslav Tykhiy 		if (realpath(name, path) == NULL) {
34068c1c21f2SYaroslav Tykhiy 			syslog(LOG_NOTICE, "realpath failed on %s: %m", path);
34078c1c21f2SYaroslav Tykhiy 			return;
34088c1c21f2SYaroslav Tykhiy 		}
34098c1c21f2SYaroslav Tykhiy 		snprintf(buf, sizeof(buf), "%.20s!%s!%s!%s!%jd!%ld\n",
34103eb568f2SGuido van Rooij 			ctime(&now)+4, ident, remotehost,
34118c1c21f2SYaroslav Tykhiy 			path, (intmax_t)size,
3412e4a71114SAndrey A. Chernov 			(long)(now - start + (now == start)));
34133eb568f2SGuido van Rooij 		write(statfd, buf, strlen(buf));
34143eb568f2SGuido van Rooij 	}
34153eb568f2SGuido van Rooij }
3416e4648f05SYaroslav Tykhiy 
3417e4648f05SYaroslav Tykhiy static char *
3418e4648f05SYaroslav Tykhiy doublequote(char *s)
3419e4648f05SYaroslav Tykhiy {
3420e4648f05SYaroslav Tykhiy 	int n;
3421e4648f05SYaroslav Tykhiy 	char *p, *s2;
3422e4648f05SYaroslav Tykhiy 
3423e4648f05SYaroslav Tykhiy 	for (p = s, n = 0; *p; p++)
3424e4648f05SYaroslav Tykhiy 		if (*p == '"')
3425e4648f05SYaroslav Tykhiy 			n++;
3426e4648f05SYaroslav Tykhiy 
3427e4648f05SYaroslav Tykhiy 	if ((s2 = malloc(p - s + n + 1)) == NULL)
3428e4648f05SYaroslav Tykhiy 		return (NULL);
3429e4648f05SYaroslav Tykhiy 
3430e4648f05SYaroslav Tykhiy 	for (p = s2; *s; s++, p++) {
3431e4648f05SYaroslav Tykhiy 		if ((*p = *s) == '"')
3432e4648f05SYaroslav Tykhiy 			*(++p) = '"';
3433e4648f05SYaroslav Tykhiy 	}
3434e4648f05SYaroslav Tykhiy 	*p = '\0';
3435e4648f05SYaroslav Tykhiy 
3436e4648f05SYaroslav Tykhiy 	return (s2);
3437e4648f05SYaroslav Tykhiy }
3438206fe568SHajimu UMEMOTO 
3439206fe568SHajimu UMEMOTO /* setup server socket for specified address family */
3440206fe568SHajimu UMEMOTO /* if af is PF_UNSPEC more than one socket may be returned */
3441206fe568SHajimu UMEMOTO /* the returned list is dynamically allocated, so caller needs to free it */
3442206fe568SHajimu UMEMOTO static int *
3443206fe568SHajimu UMEMOTO socksetup(int af, char *bindname, const char *bindport)
3444206fe568SHajimu UMEMOTO {
3445206fe568SHajimu UMEMOTO 	struct addrinfo hints, *res, *r;
3446206fe568SHajimu UMEMOTO 	int error, maxs, *s, *socks;
3447206fe568SHajimu UMEMOTO 	const int on = 1;
3448206fe568SHajimu UMEMOTO 
3449206fe568SHajimu UMEMOTO 	memset(&hints, 0, sizeof(hints));
3450206fe568SHajimu UMEMOTO 	hints.ai_flags = AI_PASSIVE;
3451206fe568SHajimu UMEMOTO 	hints.ai_family = af;
3452206fe568SHajimu UMEMOTO 	hints.ai_socktype = SOCK_STREAM;
3453206fe568SHajimu UMEMOTO 	error = getaddrinfo(bindname, bindport, &hints, &res);
3454206fe568SHajimu UMEMOTO 	if (error) {
3455206fe568SHajimu UMEMOTO 		syslog(LOG_ERR, "%s", gai_strerror(error));
3456206fe568SHajimu UMEMOTO 		if (error == EAI_SYSTEM)
3457206fe568SHajimu UMEMOTO 			syslog(LOG_ERR, "%s", strerror(errno));
3458206fe568SHajimu UMEMOTO 		return NULL;
3459206fe568SHajimu UMEMOTO 	}
3460206fe568SHajimu UMEMOTO 
3461206fe568SHajimu UMEMOTO 	/* Count max number of sockets we may open */
3462206fe568SHajimu UMEMOTO 	for (maxs = 0, r = res; r; r = r->ai_next, maxs++)
3463206fe568SHajimu UMEMOTO 		;
3464206fe568SHajimu UMEMOTO 	socks = malloc((maxs + 1) * sizeof(int));
3465206fe568SHajimu UMEMOTO 	if (!socks) {
3466206fe568SHajimu UMEMOTO 		freeaddrinfo(res);
3467206fe568SHajimu UMEMOTO 		syslog(LOG_ERR, "couldn't allocate memory for sockets");
3468206fe568SHajimu UMEMOTO 		return NULL;
3469206fe568SHajimu UMEMOTO 	}
3470206fe568SHajimu UMEMOTO 
3471206fe568SHajimu UMEMOTO 	*socks = 0;   /* num of sockets counter at start of array */
3472206fe568SHajimu UMEMOTO 	s = socks + 1;
3473206fe568SHajimu UMEMOTO 	for (r = res; r; r = r->ai_next) {
3474206fe568SHajimu UMEMOTO 		*s = socket(r->ai_family, r->ai_socktype, r->ai_protocol);
3475206fe568SHajimu UMEMOTO 		if (*s < 0) {
3476206fe568SHajimu UMEMOTO 			syslog(LOG_DEBUG, "control socket: %m");
3477206fe568SHajimu UMEMOTO 			continue;
3478206fe568SHajimu UMEMOTO 		}
3479206fe568SHajimu UMEMOTO 		if (setsockopt(*s, SOL_SOCKET, SO_REUSEADDR,
3480206fe568SHajimu UMEMOTO 		    &on, sizeof(on)) < 0)
3481206fe568SHajimu UMEMOTO 			syslog(LOG_WARNING,
3482206fe568SHajimu UMEMOTO 			    "control setsockopt (SO_REUSEADDR): %m");
3483206fe568SHajimu UMEMOTO 		if (r->ai_family == AF_INET6) {
3484206fe568SHajimu UMEMOTO 			if (setsockopt(*s, IPPROTO_IPV6, IPV6_V6ONLY,
3485206fe568SHajimu UMEMOTO 			    &on, sizeof(on)) < 0)
3486206fe568SHajimu UMEMOTO 				syslog(LOG_WARNING,
3487206fe568SHajimu UMEMOTO 				    "control setsockopt (IPV6_V6ONLY): %m");
3488206fe568SHajimu UMEMOTO 		}
3489206fe568SHajimu UMEMOTO 		if (bind(*s, r->ai_addr, r->ai_addrlen) < 0) {
3490206fe568SHajimu UMEMOTO 			syslog(LOG_DEBUG, "control bind: %m");
3491206fe568SHajimu UMEMOTO 			close(*s);
3492206fe568SHajimu UMEMOTO 			continue;
3493206fe568SHajimu UMEMOTO 		}
3494206fe568SHajimu UMEMOTO 		(*socks)++;
3495206fe568SHajimu UMEMOTO 		s++;
3496206fe568SHajimu UMEMOTO 	}
3497206fe568SHajimu UMEMOTO 
3498206fe568SHajimu UMEMOTO 	if (res)
3499206fe568SHajimu UMEMOTO 		freeaddrinfo(res);
3500206fe568SHajimu UMEMOTO 
3501206fe568SHajimu UMEMOTO 	if (*socks == 0) {
3502206fe568SHajimu UMEMOTO 		syslog(LOG_ERR, "control socket: Couldn't bind to any socket");
3503206fe568SHajimu UMEMOTO 		free(socks);
3504206fe568SHajimu UMEMOTO 		return NULL;
3505206fe568SHajimu UMEMOTO 	}
3506206fe568SHajimu UMEMOTO 	return(socks);
3507206fe568SHajimu UMEMOTO }
3508