xref: /freebsd/libexec/ftpd/ftpd.c (revision 763e8c9623f8ac3c6d075ccb3d22afa8730afa19)
1ea022d16SRodney W. Grimes /*
2ea022d16SRodney W. Grimes  * Copyright (c) 1985, 1988, 1990, 1992, 1993, 1994
3ea022d16SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4ea022d16SRodney W. Grimes  *
5ea022d16SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6ea022d16SRodney W. Grimes  * modification, are permitted provided that the following conditions
7ea022d16SRodney W. Grimes  * are met:
8ea022d16SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9ea022d16SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10ea022d16SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11ea022d16SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12ea022d16SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13ea022d16SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
14ea022d16SRodney W. Grimes  *    must display the following acknowledgement:
15ea022d16SRodney W. Grimes  *	This product includes software developed by the University of
16ea022d16SRodney W. Grimes  *	California, Berkeley and its contributors.
17ea022d16SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
18ea022d16SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
19ea022d16SRodney W. Grimes  *    without specific prior written permission.
20ea022d16SRodney W. Grimes  *
21ea022d16SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22ea022d16SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23ea022d16SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24ea022d16SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25ea022d16SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26ea022d16SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27ea022d16SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28ea022d16SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29ea022d16SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30ea022d16SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31ea022d16SRodney W. Grimes  * SUCH DAMAGE.
32ea022d16SRodney W. Grimes  */
33ea022d16SRodney W. Grimes 
349aca17cbSMark Murray #if 0
35ea022d16SRodney W. Grimes #ifndef lint
36ea022d16SRodney W. Grimes static char copyright[] =
37ea022d16SRodney W. Grimes "@(#) Copyright (c) 1985, 1988, 1990, 1992, 1993, 1994\n\
38ea022d16SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
39ea022d16SRodney W. Grimes #endif /* not lint */
409aca17cbSMark Murray #endif
41ea022d16SRodney W. Grimes 
42ea022d16SRodney W. Grimes #ifndef lint
43e02897faSPhilippe Charnier #if 0
44ea022d16SRodney W. Grimes static char sccsid[] = "@(#)ftpd.c	8.4 (Berkeley) 4/16/94";
459aca17cbSMark Murray #endif
46e02897faSPhilippe Charnier #endif /* not lint */
47ea022d16SRodney W. Grimes 
480c4b401fSYaroslav Tykhiy #include <sys/cdefs.h>
490c4b401fSYaroslav Tykhiy __FBSDID("$FreeBSD$");
500c4b401fSYaroslav Tykhiy 
51ea022d16SRodney W. Grimes /*
52ea022d16SRodney W. Grimes  * FTP server.
53ea022d16SRodney W. Grimes  */
54ea022d16SRodney W. Grimes #include <sys/param.h>
55ea022d16SRodney W. Grimes #include <sys/ioctl.h>
569fc5823aSGarrett Wollman #include <sys/mman.h>
57eb2fc780SGarrett Wollman #include <sys/socket.h>
58eb2fc780SGarrett Wollman #include <sys/stat.h>
59eb2fc780SGarrett Wollman #include <sys/time.h>
60eb2fc780SGarrett Wollman #include <sys/wait.h>
61ea022d16SRodney W. Grimes 
62ea022d16SRodney W. Grimes #include <netinet/in.h>
63ea022d16SRodney W. Grimes #include <netinet/in_systm.h>
64ea022d16SRodney W. Grimes #include <netinet/ip.h>
659fc5823aSGarrett Wollman #include <netinet/tcp.h>
66ea022d16SRodney W. Grimes 
67ea022d16SRodney W. Grimes #define	FTP_NAMES
68ea022d16SRodney W. Grimes #include <arpa/ftp.h>
69ea022d16SRodney W. Grimes #include <arpa/inet.h>
70ea022d16SRodney W. Grimes #include <arpa/telnet.h>
71ea022d16SRodney W. Grimes 
72ea022d16SRodney W. Grimes #include <ctype.h>
73ea022d16SRodney W. Grimes #include <dirent.h>
74ea022d16SRodney W. Grimes #include <err.h>
75ea022d16SRodney W. Grimes #include <errno.h>
76ea022d16SRodney W. Grimes #include <fcntl.h>
77ea022d16SRodney W. Grimes #include <glob.h>
78ea022d16SRodney W. Grimes #include <limits.h>
79ea022d16SRodney W. Grimes #include <netdb.h>
80ea022d16SRodney W. Grimes #include <pwd.h>
8131fea7b8SDavid Nugent #include <grp.h>
8247499ecdSAndrey A. Chernov #include <opie.h>
83ea022d16SRodney W. Grimes #include <signal.h>
84a57e1ef0SYaroslav Tykhiy #include <stdint.h>
85ea022d16SRodney W. Grimes #include <stdio.h>
86ea022d16SRodney W. Grimes #include <stdlib.h>
87ea022d16SRodney W. Grimes #include <string.h>
88ea022d16SRodney W. Grimes #include <syslog.h>
89ea022d16SRodney W. Grimes #include <time.h>
90ea022d16SRodney W. Grimes #include <unistd.h>
91b63e1fe2SPeter Wemm #include <libutil.h>
92b071c689SDavid Nugent #ifdef	LOGIN_CAP
93b071c689SDavid Nugent #include <login_cap.h>
94b071c689SDavid Nugent #endif
95ea022d16SRodney W. Grimes 
965bc9d93dSMark Murray #ifdef USE_PAM
976c9134c0SMark Murray #include <security/pam_appl.h>
986c9134c0SMark Murray #endif
996c9134c0SMark Murray 
100ea022d16SRodney W. Grimes #include "pathnames.h"
101ea022d16SRodney W. Grimes #include "extern.h"
102ea022d16SRodney W. Grimes 
103ea022d16SRodney W. Grimes #include <stdarg.h>
104ea022d16SRodney W. Grimes 
105af85d782SDavid Nugent static char version[] = "Version 6.00LS";
106af85d782SDavid Nugent #undef main
107ea022d16SRodney W. Grimes 
1084dd8b5abSYoshinobu Inoue union sockunion ctrl_addr;
1094dd8b5abSYoshinobu Inoue union sockunion data_source;
1104dd8b5abSYoshinobu Inoue union sockunion data_dest;
1114dd8b5abSYoshinobu Inoue union sockunion his_addr;
1124dd8b5abSYoshinobu Inoue union sockunion pasv_addr;
113ea022d16SRodney W. Grimes 
114cf09a206SDavid Greenman int	daemon_mode;
115ea022d16SRodney W. Grimes int	data;
11663591ba5SYaroslav Tykhiy int	dataport;
117c152df28SYaroslav Tykhiy int	hostinfo = 1;	/* print host-specific info in messages */
118ea022d16SRodney W. Grimes int	logged_in;
119ea022d16SRodney W. Grimes struct	passwd *pw;
120ce9287fcSYaroslav Tykhiy char	*homedir;
121618b0bbaSMark Murray int	ftpdebug;
122ea022d16SRodney W. Grimes int	timeout = 900;    /* timeout after 15 minutes of inactivity */
123ea022d16SRodney W. Grimes int	maxtimeout = 7200;/* don't allow idle time to be set beyond 2 hours */
124ea022d16SRodney W. Grimes int	logging;
1254c450ad7SPaul Traina int	restricted_data_ports = 1;
126a5a4544eSPaul Traina int	paranoid = 1;	  /* be extra careful about security */
1275a392aecSTorsten Blum int	anon_only = 0;    /* Only anonymous ftp allowed */
1282ea42282SYaroslav Tykhiy int	assumeutf8 = 0;   /* Assume that server file names are in UTF-8 */
129ea022d16SRodney W. Grimes int	guest;
130a5a4544eSPaul Traina int	dochroot;
131215a9f9dSYaroslav Tykhiy char	*chrootdir;
1325d7e0128SYaroslav Tykhiy int	dowtmp = 1;
1333eb568f2SGuido van Rooij int	stats;
1343eb568f2SGuido van Rooij int	statfd = -1;
135ea022d16SRodney W. Grimes int	type;
136ea022d16SRodney W. Grimes int	form;
137ea022d16SRodney W. Grimes int	stru;			/* avoid C keyword */
138ea022d16SRodney W. Grimes int	mode;
139ea022d16SRodney W. Grimes int	usedefault = 1;		/* for data transfers */
140ea022d16SRodney W. Grimes int	pdata = -1;		/* for passive mode */
141a4b77a2aSPoul-Henning Kamp int	readonly = 0;		/* Server is in readonly mode.	*/
142a4b77a2aSPoul-Henning Kamp int	noepsv = 0;		/* EPSV command is disabled.	*/
14362513e76SNik Clayton int	noretr = 0;		/* RETR command is disabled.	*/
1441cc9f0bbSSheldon Hearn int	noguestretr = 0;	/* RETR command is disabled for anon users. */
145d186bb12SMatthew N. Dodd int	noguestmkd = 0;		/* MKD command is disabled for anon users. */
146a117c345SYaroslav Tykhiy int	noguestmod = 1;		/* anon users may not modify existing files. */
14762513e76SNik Clayton 
148ea022d16SRodney W. Grimes off_t	file_size;
149ea022d16SRodney W. Grimes off_t	byte_count;
150ea022d16SRodney W. Grimes #if !defined(CMASK) || CMASK == 0
151ea022d16SRodney W. Grimes #undef CMASK
152ea022d16SRodney W. Grimes #define CMASK 027
153ea022d16SRodney W. Grimes #endif
154ea022d16SRodney W. Grimes int	defumask = CMASK;		/* default umask value */
155ea022d16SRodney W. Grimes char	tmpline[7];
156ea4e54b9SDavid Nugent char	*hostname;
157ad442344SDima Dorfman int	epsvall = 0;
158ad442344SDima Dorfman 
1590512556aSDavid Nugent #ifdef VIRTUAL_HOSTING
160ea4e54b9SDavid Nugent char	*ftpuser;
161ea4e54b9SDavid Nugent 
162ea4e54b9SDavid Nugent static struct ftphost {
163ea4e54b9SDavid Nugent 	struct ftphost	*next;
164f38c6cadSYoshinobu Inoue 	struct addrinfo *hostinfo;
165ea4e54b9SDavid Nugent 	char		*hostname;
166ea4e54b9SDavid Nugent 	char		*anonuser;
167ea4e54b9SDavid Nugent 	char		*statfile;
168ea4e54b9SDavid Nugent 	char		*welcome;
169ea4e54b9SDavid Nugent 	char		*loginmsg;
170ea4e54b9SDavid Nugent } *thishost, *firsthost;
171ea4e54b9SDavid Nugent 
172ea4e54b9SDavid Nugent #endif
17304683b2cSYaroslav Tykhiy char	remotehost[NI_MAXHOST];
1743eb568f2SGuido van Rooij char	*ident = NULL;
175a5a4544eSPaul Traina 
17680643af0SEd Schouten static char	wtmpid[20];
177a5a4544eSPaul Traina 
1785bc9d93dSMark Murray #ifdef USE_PAM
179e4bc453cSWarner Losh static int	auth_pam(struct passwd**, const char*);
1805bc9d93dSMark Murray pam_handle_t	*pamh = NULL;
18147499ecdSAndrey A. Chernov #endif
182fa1746c9SMark Murray 
183fa1746c9SMark Murray static struct opie	opiedata;
184fa1746c9SMark Murray static char		opieprompt[OPIE_CHALLENGE_MAX+1];
18547499ecdSAndrey A. Chernov static int		pwok;
1869aca17cbSMark Murray 
187125b9635SYaroslav Tykhiy char	*pid_file = NULL; /* means default location to pidfile(3) */
188105a3c98SJulian Elischer 
189ea022d16SRodney W. Grimes /*
1906d10cb2fSJonathan Lemon  * Limit number of pathnames that glob can return.
1916d10cb2fSJonathan Lemon  * A limit of 0 indicates the number of pathnames is unlimited.
1926d10cb2fSJonathan Lemon  */
1936d10cb2fSJonathan Lemon #define MAXGLOBARGS	16384
1946d10cb2fSJonathan Lemon #
1956d10cb2fSJonathan Lemon 
1966d10cb2fSJonathan Lemon /*
197ea022d16SRodney W. Grimes  * Timeout intervals for retrying connections
198ea022d16SRodney W. Grimes  * to hosts that don't accept PORT cmds.  This
199ea022d16SRodney W. Grimes  * is a kludge, but given the problems with TCP...
200ea022d16SRodney W. Grimes  */
201ea022d16SRodney W. Grimes #define	SWAITMAX	90	/* wait at most 90 seconds */
202ea022d16SRodney W. Grimes #define	SWAITINT	5	/* interval between retries */
203ea022d16SRodney W. Grimes 
204ea022d16SRodney W. Grimes int	swaitmax = SWAITMAX;
205ea022d16SRodney W. Grimes int	swaitint = SWAITINT;
206ea022d16SRodney W. Grimes 
207ea022d16SRodney W. Grimes #ifdef SETPROCTITLE
208b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE
209ea022d16SRodney W. Grimes char	**Argv = NULL;		/* pointer to argument vector */
210ea022d16SRodney W. Grimes char	*LastArgv = NULL;	/* end of argv */
211b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */
212ea022d16SRodney W. Grimes char	proctitle[LINE_MAX];	/* initial part of title */
213ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */
214ea022d16SRodney W. Grimes 
2156b2dee6bSYaroslav Tykhiy #define LOGCMD(cmd, file)		logcmd((cmd), (file), NULL, -1)
2166b2dee6bSYaroslav Tykhiy #define LOGCMD2(cmd, file1, file2)	logcmd((cmd), (file1), (file2), -1)
2176b2dee6bSYaroslav Tykhiy #define LOGBYTES(cmd, file, cnt)	logcmd((cmd), (file), NULL, (cnt))
218ea022d16SRodney W. Grimes 
2194cd51076SYaroslav Tykhiy static	volatile sig_atomic_t recvurg;
2204cd51076SYaroslav Tykhiy static	int transflag;		/* NB: for debugging only */
2214cd51076SYaroslav Tykhiy 
2224cd51076SYaroslav Tykhiy #define STARTXFER	flagxfer(1)
2234cd51076SYaroslav Tykhiy #define ENDXFER		flagxfer(0)
2244cd51076SYaroslav Tykhiy 
2254cd51076SYaroslav Tykhiy #define START_UNSAFE	maskurg(1)
2264cd51076SYaroslav Tykhiy #define END_UNSAFE	maskurg(0)
2274cd51076SYaroslav Tykhiy 
2284cd51076SYaroslav Tykhiy /* It's OK to put an `else' clause after this macro. */
2294cd51076SYaroslav Tykhiy #define CHECKOOB(action)						\
2304cd51076SYaroslav Tykhiy 	if (recvurg) {							\
2314cd51076SYaroslav Tykhiy 		recvurg = 0;						\
2324cd51076SYaroslav Tykhiy 		if (myoob()) {						\
2334cd51076SYaroslav Tykhiy 			ENDXFER;					\
2344cd51076SYaroslav Tykhiy 			action;						\
2354cd51076SYaroslav Tykhiy 		}							\
2364cd51076SYaroslav Tykhiy 	}
2374cd51076SYaroslav Tykhiy 
238ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
2392c9fd5f2SHajimu UMEMOTO static void	 inithosts(int);
240e4bc453cSWarner Losh static void	 selecthost(union sockunion *);
241ea4e54b9SDavid Nugent #endif
242e4bc453cSWarner Losh static void	 ack(char *);
243e4bc453cSWarner Losh static void	 sigurg(int);
2444cd51076SYaroslav Tykhiy static void	 maskurg(int);
2454cd51076SYaroslav Tykhiy static void	 flagxfer(int);
2464cd51076SYaroslav Tykhiy static int	 myoob(void);
247cefb6785SChristian S.J. Peron static int	 checkuser(char *, char *, int, char **, int *);
248e4bc453cSWarner Losh static FILE	*dataconn(char *, off_t, char *);
249e4bc453cSWarner Losh static void	 dolog(struct sockaddr *);
250e4bc453cSWarner Losh static void	 end_login(void);
251e4bc453cSWarner Losh static FILE	*getdatasock(char *);
252a117c345SYaroslav Tykhiy static int	 guniquefd(char *, char **);
253e4bc453cSWarner Losh static void	 lostconn(int);
254e4bc453cSWarner Losh static void	 sigquit(int);
255e4bc453cSWarner Losh static int	 receive_data(FILE *, FILE *);
2566e4b0a55SYaroslav Tykhiy static int	 send_data(FILE *, FILE *, size_t, off_t, int);
257ea022d16SRodney W. Grimes static struct passwd *
258e4bc453cSWarner Losh 		 sgetpwnam(char *);
259e4bc453cSWarner Losh static char	*sgetsave(char *);
260e4bc453cSWarner Losh static void	 reapchild(int);
261eb5b2bb3SYaroslav Tykhiy static void	 appendf(char **, char *, ...) __printflike(2, 3);
2626b2dee6bSYaroslav Tykhiy static void	 logcmd(char *, char *, char *, off_t);
263e4bc453cSWarner Losh static void      logxfer(char *, off_t, time_t);
264e4648f05SYaroslav Tykhiy static char	*doublequote(char *);
265206fe568SHajimu UMEMOTO static int	*socksetup(int, char *, const char *);
266ea022d16SRodney W. Grimes 
267ea022d16SRodney W. Grimes int
268e4bc453cSWarner Losh main(int argc, char *argv[], char **envp)
269ea022d16SRodney W. Grimes {
27078e3eed0SStefan Farfeleder 	socklen_t addrlen;
27178e3eed0SStefan Farfeleder 	int ch, on = 1, tos;
272ea022d16SRodney W. Grimes 	char *cp, line[LINE_MAX];
273ea022d16SRodney W. Grimes 	FILE *fd;
2744dd8b5abSYoshinobu Inoue 	char	*bindname = NULL;
27563591ba5SYaroslav Tykhiy 	const char *bindport = "ftp";
2764dd8b5abSYoshinobu Inoue 	int	family = AF_UNSPEC;
2774b82fc95SYaroslav Tykhiy 	struct sigaction sa;
278ea022d16SRodney W. Grimes 
27934d1ba5cSAndrey A. Chernov 	tzset();		/* in case no timezone database in ~ftp */
2804b82fc95SYaroslav Tykhiy 	sigemptyset(&sa.sa_mask);
2814b82fc95SYaroslav Tykhiy 	sa.sa_flags = SA_RESTART;
28234d1ba5cSAndrey A. Chernov 
283b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE
284ea022d16SRodney W. Grimes 	/*
285ea022d16SRodney W. Grimes 	 *  Save start and extent of argv for setproctitle.
286ea022d16SRodney W. Grimes 	 */
287ea022d16SRodney W. Grimes 	Argv = argv;
288ea022d16SRodney W. Grimes 	while (*envp)
289ea022d16SRodney W. Grimes 		envp++;
290ea022d16SRodney W. Grimes 	LastArgv = envp[-1] + strlen(envp[-1]);
291b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */
292ea022d16SRodney W. Grimes 
2936c98f401SYaroslav Tykhiy 	/*
2946c98f401SYaroslav Tykhiy 	 * Prevent diagnostic messages from appearing on stderr.
2956c98f401SYaroslav Tykhiy 	 * We run as a daemon or from inetd; in both cases, there's
2966c98f401SYaroslav Tykhiy 	 * more reason in logging to syslog.
2976c98f401SYaroslav Tykhiy 	 */
2986c98f401SYaroslav Tykhiy 	(void) freopen(_PATH_DEVNULL, "w", stderr);
2996c98f401SYaroslav Tykhiy 	opterr = 0;
3006c98f401SYaroslav Tykhiy 
3016c98f401SYaroslav Tykhiy 	/*
3026c98f401SYaroslav Tykhiy 	 * LOG_NDELAY sets up the logging connection immediately,
3036c98f401SYaroslav Tykhiy 	 * necessary for anonymous ftp's that chroot and can't do it later.
3046c98f401SYaroslav Tykhiy 	 */
3056c98f401SYaroslav Tykhiy 	openlog("ftpd", LOG_PID | LOG_NDELAY, LOG_FTP);
3063eb568f2SGuido van Rooij 
307c152df28SYaroslav Tykhiy 	while ((ch = getopt(argc, argv,
3082ea42282SYaroslav Tykhiy 	                    "468a:AdDEhlmMoOp:P:rRSt:T:u:UvW")) != -1) {
309ea022d16SRodney W. Grimes 		switch (ch) {
3100e063efeSYaroslav Tykhiy 		case '4':
311206fe568SHajimu UMEMOTO 			family = (family == AF_INET6) ? AF_UNSPEC : AF_INET;
3120e063efeSYaroslav Tykhiy 			break;
3130e063efeSYaroslav Tykhiy 
3140e063efeSYaroslav Tykhiy 		case '6':
315206fe568SHajimu UMEMOTO 			family = (family == AF_INET) ? AF_UNSPEC : AF_INET6;
3160e063efeSYaroslav Tykhiy 			break;
3170e063efeSYaroslav Tykhiy 
3182ea42282SYaroslav Tykhiy 		case '8':
3192ea42282SYaroslav Tykhiy 			assumeutf8 = 1;
3202ea42282SYaroslav Tykhiy 			break;
3212ea42282SYaroslav Tykhiy 
3220e063efeSYaroslav Tykhiy 		case 'a':
3230e063efeSYaroslav Tykhiy 			bindname = optarg;
3240e063efeSYaroslav Tykhiy 			break;
3250e063efeSYaroslav Tykhiy 
3260e063efeSYaroslav Tykhiy 		case 'A':
3270e063efeSYaroslav Tykhiy 			anon_only = 1;
328cf09a206SDavid Greenman 			break;
329cf09a206SDavid Greenman 
330ea022d16SRodney W. Grimes 		case 'd':
331618b0bbaSMark Murray 			ftpdebug++;
332ea022d16SRodney W. Grimes 			break;
333ea022d16SRodney W. Grimes 
3340e063efeSYaroslav Tykhiy 		case 'D':
3350e063efeSYaroslav Tykhiy 			daemon_mode++;
3360e063efeSYaroslav Tykhiy 			break;
3370e063efeSYaroslav Tykhiy 
338a4b77a2aSPoul-Henning Kamp 		case 'E':
339a4b77a2aSPoul-Henning Kamp 			noepsv = 1;
340a4b77a2aSPoul-Henning Kamp 			break;
341a4b77a2aSPoul-Henning Kamp 
342c152df28SYaroslav Tykhiy 		case 'h':
343c152df28SYaroslav Tykhiy 			hostinfo = 0;
344c152df28SYaroslav Tykhiy 			break;
345c152df28SYaroslav Tykhiy 
346ea022d16SRodney W. Grimes 		case 'l':
347ea022d16SRodney W. Grimes 			logging++;	/* > 1 == extra logging */
348ea022d16SRodney W. Grimes 			break;
349ea022d16SRodney W. Grimes 
350a117c345SYaroslav Tykhiy 		case 'm':
351a117c345SYaroslav Tykhiy 			noguestmod = 0;
352a117c345SYaroslav Tykhiy 			break;
353a117c345SYaroslav Tykhiy 
3540e063efeSYaroslav Tykhiy 		case 'M':
3550e063efeSYaroslav Tykhiy 			noguestmkd = 1;
3560e063efeSYaroslav Tykhiy 			break;
3570e063efeSYaroslav Tykhiy 
3580e063efeSYaroslav Tykhiy 		case 'o':
3590e063efeSYaroslav Tykhiy 			noretr = 1;
3600e063efeSYaroslav Tykhiy 			break;
3610e063efeSYaroslav Tykhiy 
3620e063efeSYaroslav Tykhiy 		case 'O':
3630e063efeSYaroslav Tykhiy 			noguestretr = 1;
3640e063efeSYaroslav Tykhiy 			break;
3650e063efeSYaroslav Tykhiy 
3660e063efeSYaroslav Tykhiy 		case 'p':
3670e063efeSYaroslav Tykhiy 			pid_file = optarg;
3680e063efeSYaroslav Tykhiy 			break;
3690e063efeSYaroslav Tykhiy 
37063591ba5SYaroslav Tykhiy 		case 'P':
37163591ba5SYaroslav Tykhiy 			bindport = optarg;
37263591ba5SYaroslav Tykhiy 			break;
37363591ba5SYaroslav Tykhiy 
374a4b77a2aSPoul-Henning Kamp 		case 'r':
375a4b77a2aSPoul-Henning Kamp 			readonly = 1;
376a4b77a2aSPoul-Henning Kamp 			break;
377a4b77a2aSPoul-Henning Kamp 
378a5a4544eSPaul Traina 		case 'R':
379a5a4544eSPaul Traina 			paranoid = 0;
380a5a4544eSPaul Traina 			break;
381a5a4544eSPaul Traina 
382a5a4544eSPaul Traina 		case 'S':
383a5a4544eSPaul Traina 			stats++;
384a5a4544eSPaul Traina 			break;
385a5a4544eSPaul Traina 
386ea022d16SRodney W. Grimes 		case 't':
387ea022d16SRodney W. Grimes 			timeout = atoi(optarg);
388ea022d16SRodney W. Grimes 			if (maxtimeout < timeout)
389ea022d16SRodney W. Grimes 				maxtimeout = timeout;
390ea022d16SRodney W. Grimes 			break;
391a5a4544eSPaul Traina 
3920e063efeSYaroslav Tykhiy 		case 'T':
3930e063efeSYaroslav Tykhiy 			maxtimeout = atoi(optarg);
3940e063efeSYaroslav Tykhiy 			if (timeout > maxtimeout)
3950e063efeSYaroslav Tykhiy 				timeout = maxtimeout;
396105a3c98SJulian Elischer 			break;
397105a3c98SJulian Elischer 
398ea022d16SRodney W. Grimes 		case 'u':
399ea022d16SRodney W. Grimes 		    {
400ea022d16SRodney W. Grimes 			long val = 0;
401ea022d16SRodney W. Grimes 
402ea022d16SRodney W. Grimes 			val = strtol(optarg, &optarg, 8);
403ea022d16SRodney W. Grimes 			if (*optarg != '\0' || val < 0)
4046c98f401SYaroslav Tykhiy 				syslog(LOG_WARNING, "bad value for -u");
405ea022d16SRodney W. Grimes 			else
406ea022d16SRodney W. Grimes 				defumask = val;
407ea022d16SRodney W. Grimes 			break;
408ea022d16SRodney W. Grimes 		    }
4090e063efeSYaroslav Tykhiy 		case 'U':
4100e063efeSYaroslav Tykhiy 			restricted_data_ports = 0;
4115a392aecSTorsten Blum 			break;
412ea022d16SRodney W. Grimes 
413ea022d16SRodney W. Grimes 		case 'v':
41493bd9dc5SYaroslav Tykhiy 			ftpdebug++;
415ea022d16SRodney W. Grimes 			break;
416ea022d16SRodney W. Grimes 
4175d7e0128SYaroslav Tykhiy 		case 'W':
4185d7e0128SYaroslav Tykhiy 			dowtmp = 0;
4195d7e0128SYaroslav Tykhiy 			break;
4205d7e0128SYaroslav Tykhiy 
421ea022d16SRodney W. Grimes 		default:
4226c98f401SYaroslav Tykhiy 			syslog(LOG_WARNING, "unknown flag -%c ignored", optopt);
423ea022d16SRodney W. Grimes 			break;
424ea022d16SRodney W. Grimes 		}
425ea022d16SRodney W. Grimes 	}
426cf09a206SDavid Greenman 
427cf09a206SDavid Greenman 	if (daemon_mode) {
428206fe568SHajimu UMEMOTO 		int *ctl_sock, fd, maxfd = -1, nfds, i;
429206fe568SHajimu UMEMOTO 		fd_set defreadfds, readfds;
430206fe568SHajimu UMEMOTO 		pid_t pid;
431125b9635SYaroslav Tykhiy 		struct pidfh *pfh;
432125b9635SYaroslav Tykhiy 
433125b9635SYaroslav Tykhiy 		if ((pfh = pidfile_open(pid_file, 0600, &pid)) == NULL) {
434125b9635SYaroslav Tykhiy 			if (errno == EEXIST) {
435125b9635SYaroslav Tykhiy 				syslog(LOG_ERR, "%s already running, pid %d",
436125b9635SYaroslav Tykhiy 				       getprogname(), (int)pid);
437125b9635SYaroslav Tykhiy 				exit(1);
438125b9635SYaroslav Tykhiy 			}
439125b9635SYaroslav Tykhiy 			syslog(LOG_WARNING, "pidfile_open: %m");
440125b9635SYaroslav Tykhiy 		}
441cf09a206SDavid Greenman 
442cf09a206SDavid Greenman 		/*
443cf09a206SDavid Greenman 		 * Detach from parent.
444cf09a206SDavid Greenman 		 */
445cf09a206SDavid Greenman 		if (daemon(1, 1) < 0) {
446cf09a206SDavid Greenman 			syslog(LOG_ERR, "failed to become a daemon");
447cf09a206SDavid Greenman 			exit(1);
448cf09a206SDavid Greenman 		}
449125b9635SYaroslav Tykhiy 
450125b9635SYaroslav Tykhiy 		if (pfh != NULL && pidfile_write(pfh) == -1)
451125b9635SYaroslav Tykhiy 			syslog(LOG_WARNING, "pidfile_write: %m");
452125b9635SYaroslav Tykhiy 
4534b82fc95SYaroslav Tykhiy 		sa.sa_handler = reapchild;
4544b82fc95SYaroslav Tykhiy 		(void)sigaction(SIGCHLD, &sa, NULL);
4554dd8b5abSYoshinobu Inoue 
4562c9fd5f2SHajimu UMEMOTO #ifdef VIRTUAL_HOSTING
4572c9fd5f2SHajimu UMEMOTO 		inithosts(family);
4582c9fd5f2SHajimu UMEMOTO #endif
4592c9fd5f2SHajimu UMEMOTO 
460cf09a206SDavid Greenman 		/*
461cf09a206SDavid Greenman 		 * Open a socket, bind it to the FTP port, and start
462cf09a206SDavid Greenman 		 * listening.
463cf09a206SDavid Greenman 		 */
464206fe568SHajimu UMEMOTO 		ctl_sock = socksetup(family, bindname, bindport);
465206fe568SHajimu UMEMOTO 		if (ctl_sock == NULL)
466cf09a206SDavid Greenman 			exit(1);
467206fe568SHajimu UMEMOTO 
468206fe568SHajimu UMEMOTO 		FD_ZERO(&defreadfds);
469206fe568SHajimu UMEMOTO 		for (i = 1; i <= *ctl_sock; i++) {
470206fe568SHajimu UMEMOTO 			FD_SET(ctl_sock[i], &defreadfds);
471206fe568SHajimu UMEMOTO 			if (listen(ctl_sock[i], 32) < 0) {
472cf09a206SDavid Greenman 				syslog(LOG_ERR, "control listen: %m");
473cf09a206SDavid Greenman 				exit(1);
474cf09a206SDavid Greenman 			}
475206fe568SHajimu UMEMOTO 			if (maxfd < ctl_sock[i])
476206fe568SHajimu UMEMOTO 				maxfd = ctl_sock[i];
477206fe568SHajimu UMEMOTO 		}
478206fe568SHajimu UMEMOTO 
479cf09a206SDavid Greenman 		/*
480cf09a206SDavid Greenman 		 * Loop forever accepting connection requests and forking off
481cf09a206SDavid Greenman 		 * children to handle them.
482cf09a206SDavid Greenman 		 */
483cf09a206SDavid Greenman 		while (1) {
484206fe568SHajimu UMEMOTO 			FD_COPY(&defreadfds, &readfds);
485206fe568SHajimu UMEMOTO 			nfds = select(maxfd + 1, &readfds, NULL, NULL, 0);
486206fe568SHajimu UMEMOTO 			if (nfds <= 0) {
487206fe568SHajimu UMEMOTO 				if (nfds < 0 && errno != EINTR)
488206fe568SHajimu UMEMOTO 					syslog(LOG_WARNING, "select: %m");
489206fe568SHajimu UMEMOTO 				continue;
490206fe568SHajimu UMEMOTO 			}
491206fe568SHajimu UMEMOTO 
492206fe568SHajimu UMEMOTO 			pid = -1;
493206fe568SHajimu UMEMOTO                         for (i = 1; i <= *ctl_sock; i++)
494206fe568SHajimu UMEMOTO 				if (FD_ISSET(ctl_sock[i], &readfds)) {
495206fe568SHajimu UMEMOTO 					addrlen = sizeof(his_addr);
496206fe568SHajimu UMEMOTO 					fd = accept(ctl_sock[i],
497206fe568SHajimu UMEMOTO 					    (struct sockaddr *)&his_addr,
498206fe568SHajimu UMEMOTO 					    &addrlen);
499a599a64aSYaroslav Tykhiy 					if (fd == -1) {
500a599a64aSYaroslav Tykhiy 						syslog(LOG_WARNING,
501a599a64aSYaroslav Tykhiy 						       "accept: %m");
502a599a64aSYaroslav Tykhiy 						continue;
503cf09a206SDavid Greenman 					}
504a599a64aSYaroslav Tykhiy 					switch (pid = fork()) {
505a599a64aSYaroslav Tykhiy 					case 0:
5068eb0508fSYaroslav Tykhiy 						/* child */
5078eb0508fSYaroslav Tykhiy 						(void) dup2(fd, 0);
5088eb0508fSYaroslav Tykhiy 						(void) dup2(fd, 1);
509a599a64aSYaroslav Tykhiy 						(void) close(fd);
510a599a64aSYaroslav Tykhiy 						for (i = 1; i <= *ctl_sock; i++)
5118eb0508fSYaroslav Tykhiy 							close(ctl_sock[i]);
512125b9635SYaroslav Tykhiy 						if (pfh != NULL)
513125b9635SYaroslav Tykhiy 							pidfile_close(pfh);
514a599a64aSYaroslav Tykhiy 						goto gotchild;
515a599a64aSYaroslav Tykhiy 					case -1:
516a599a64aSYaroslav Tykhiy 						syslog(LOG_WARNING, "fork: %m");
517a599a64aSYaroslav Tykhiy 						/* FALLTHROUGH */
518a599a64aSYaroslav Tykhiy 					default:
519a599a64aSYaroslav Tykhiy 						close(fd);
520a599a64aSYaroslav Tykhiy 					}
521206fe568SHajimu UMEMOTO 				}
522125b9635SYaroslav Tykhiy 		}
523cf09a206SDavid Greenman 	} else {
524cf09a206SDavid Greenman 		addrlen = sizeof(his_addr);
525cf09a206SDavid Greenman 		if (getpeername(0, (struct sockaddr *)&his_addr, &addrlen) < 0) {
526cf09a206SDavid Greenman 			syslog(LOG_ERR, "getpeername (%s): %m",argv[0]);
527cf09a206SDavid Greenman 			exit(1);
528cf09a206SDavid Greenman 		}
5292c9fd5f2SHajimu UMEMOTO 
5302c9fd5f2SHajimu UMEMOTO #ifdef VIRTUAL_HOSTING
5312c9fd5f2SHajimu UMEMOTO 		if (his_addr.su_family == AF_INET6 &&
5322c9fd5f2SHajimu UMEMOTO 		    IN6_IS_ADDR_V4MAPPED(&his_addr.su_sin6.sin6_addr))
5332c9fd5f2SHajimu UMEMOTO 			family = AF_INET;
5342c9fd5f2SHajimu UMEMOTO 		else
5352c9fd5f2SHajimu UMEMOTO 			family = his_addr.su_family;
5362c9fd5f2SHajimu UMEMOTO 		inithosts(family);
5372c9fd5f2SHajimu UMEMOTO #endif
538cf09a206SDavid Greenman 	}
539cf09a206SDavid Greenman 
540a599a64aSYaroslav Tykhiy gotchild:
5414b82fc95SYaroslav Tykhiy 	sa.sa_handler = SIG_DFL;
5424b82fc95SYaroslav Tykhiy 	(void)sigaction(SIGCHLD, &sa, NULL);
5434b82fc95SYaroslav Tykhiy 
5444b82fc95SYaroslav Tykhiy 	sa.sa_handler = sigurg;
5454b82fc95SYaroslav Tykhiy 	sa.sa_flags = 0;		/* don't restart syscalls for SIGURG */
5464b82fc95SYaroslav Tykhiy 	(void)sigaction(SIGURG, &sa, NULL);
5474b82fc95SYaroslav Tykhiy 
5484b82fc95SYaroslav Tykhiy 	sigfillset(&sa.sa_mask);	/* block all signals in handler */
5494b82fc95SYaroslav Tykhiy 	sa.sa_flags = SA_RESTART;
5504b82fc95SYaroslav Tykhiy 	sa.sa_handler = sigquit;
5514b82fc95SYaroslav Tykhiy 	(void)sigaction(SIGHUP, &sa, NULL);
5524b82fc95SYaroslav Tykhiy 	(void)sigaction(SIGINT, &sa, NULL);
5534b82fc95SYaroslav Tykhiy 	(void)sigaction(SIGQUIT, &sa, NULL);
5544b82fc95SYaroslav Tykhiy 	(void)sigaction(SIGTERM, &sa, NULL);
5554b82fc95SYaroslav Tykhiy 
5564b82fc95SYaroslav Tykhiy 	sa.sa_handler = lostconn;
5574b82fc95SYaroslav Tykhiy 	(void)sigaction(SIGPIPE, &sa, NULL);
558ea022d16SRodney W. Grimes 
559cf09a206SDavid Greenman 	addrlen = sizeof(ctrl_addr);
560cf09a206SDavid Greenman 	if (getsockname(0, (struct sockaddr *)&ctrl_addr, &addrlen) < 0) {
561cf09a206SDavid Greenman 		syslog(LOG_ERR, "getsockname (%s): %m",argv[0]);
562cf09a206SDavid Greenman 		exit(1);
563cf09a206SDavid Greenman 	}
56463591ba5SYaroslav Tykhiy 	dataport = ntohs(ctrl_addr.su_port) - 1; /* as per RFC 959 */
565ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
566ea4e54b9SDavid Nugent 	/* select our identity from virtual host table */
5674dd8b5abSYoshinobu Inoue 	selecthost(&ctrl_addr);
568ea4e54b9SDavid Nugent #endif
569cf09a206SDavid Greenman #ifdef IP_TOS
5704dd8b5abSYoshinobu Inoue 	if (ctrl_addr.su_family == AF_INET)
5714dd8b5abSYoshinobu Inoue       {
572cf09a206SDavid Greenman 	tos = IPTOS_LOWDELAY;
57357d4ef07SYaroslav Tykhiy 	if (setsockopt(0, IPPROTO_IP, IP_TOS, &tos, sizeof(int)) < 0)
574406d1ae9SYaroslav Tykhiy 		syslog(LOG_WARNING, "control setsockopt (IP_TOS): %m");
5754dd8b5abSYoshinobu Inoue       }
576cf09a206SDavid Greenman #endif
577dadb9fb3SDavid Greenman 	/*
578dadb9fb3SDavid Greenman 	 * Disable Nagle on the control channel so that we don't have to wait
579dadb9fb3SDavid Greenman 	 * for peer's ACK before issuing our next reply.
580dadb9fb3SDavid Greenman 	 */
581dadb9fb3SDavid Greenman 	if (setsockopt(0, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) < 0)
582406d1ae9SYaroslav Tykhiy 		syslog(LOG_WARNING, "control setsockopt (TCP_NODELAY): %m");
583dadb9fb3SDavid Greenman 
5844dd8b5abSYoshinobu Inoue 	data_source.su_port = htons(ntohs(ctrl_addr.su_port) - 1);
585cf09a206SDavid Greenman 
58680643af0SEd Schouten 	(void)snprintf(wtmpid, sizeof(wtmpid), "%xftpd", getpid());
587a5a4544eSPaul Traina 
588ea022d16SRodney W. Grimes 	/* Try to handle urgent data inline */
589ea022d16SRodney W. Grimes #ifdef SO_OOBINLINE
59057d4ef07SYaroslav Tykhiy 	if (setsockopt(0, SOL_SOCKET, SO_OOBINLINE, &on, sizeof(on)) < 0)
591406d1ae9SYaroslav Tykhiy 		syslog(LOG_WARNING, "control setsockopt (SO_OOBINLINE): %m");
592ea022d16SRodney W. Grimes #endif
593ea022d16SRodney W. Grimes 
594ea022d16SRodney W. Grimes #ifdef	F_SETOWN
595ea022d16SRodney W. Grimes 	if (fcntl(fileno(stdin), F_SETOWN, getpid()) == -1)
596ea022d16SRodney W. Grimes 		syslog(LOG_ERR, "fcntl F_SETOWN: %m");
597ea022d16SRodney W. Grimes #endif
5984dd8b5abSYoshinobu Inoue 	dolog((struct sockaddr *)&his_addr);
599ea022d16SRodney W. Grimes 	/*
600ea022d16SRodney W. Grimes 	 * Set up default state
601ea022d16SRodney W. Grimes 	 */
602ea022d16SRodney W. Grimes 	data = -1;
603ea022d16SRodney W. Grimes 	type = TYPE_A;
604ea022d16SRodney W. Grimes 	form = FORM_N;
605ea022d16SRodney W. Grimes 	stru = STRU_F;
606ea022d16SRodney W. Grimes 	mode = MODE_S;
607ea022d16SRodney W. Grimes 	tmpline[0] = '\0';
608ea022d16SRodney W. Grimes 
609ea022d16SRodney W. Grimes 	/* If logins are disabled, print out the message. */
610ea022d16SRodney W. Grimes 	if ((fd = fopen(_PATH_NOLOGIN,"r")) != NULL) {
611ea022d16SRodney W. Grimes 		while (fgets(line, sizeof(line), fd) != NULL) {
612ea022d16SRodney W. Grimes 			if ((cp = strchr(line, '\n')) != NULL)
613ea022d16SRodney W. Grimes 				*cp = '\0';
614ea022d16SRodney W. Grimes 			lreply(530, "%s", line);
615ea022d16SRodney W. Grimes 		}
616ea022d16SRodney W. Grimes 		(void) fflush(stdout);
617ea022d16SRodney W. Grimes 		(void) fclose(fd);
618ea022d16SRodney W. Grimes 		reply(530, "System not available.");
619ea022d16SRodney W. Grimes 		exit(0);
620ea022d16SRodney W. Grimes 	}
621ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
62263047c6fSDavid E. O'Brien 	fd = fopen(thishost->welcome, "r");
623ea4e54b9SDavid Nugent #else
62463047c6fSDavid E. O'Brien 	fd = fopen(_PATH_FTPWELCOME, "r");
625ea4e54b9SDavid Nugent #endif
62663047c6fSDavid E. O'Brien 	if (fd != NULL) {
627ea022d16SRodney W. Grimes 		while (fgets(line, sizeof(line), fd) != NULL) {
628ea022d16SRodney W. Grimes 			if ((cp = strchr(line, '\n')) != NULL)
629ea022d16SRodney W. Grimes 				*cp = '\0';
630ea022d16SRodney W. Grimes 			lreply(220, "%s", line);
631ea022d16SRodney W. Grimes 		}
632ea022d16SRodney W. Grimes 		(void) fflush(stdout);
633ea022d16SRodney W. Grimes 		(void) fclose(fd);
634ea022d16SRodney W. Grimes 		/* reply(220,) must follow */
635ea022d16SRodney W. Grimes 	}
636ea4e54b9SDavid Nugent #ifndef VIRTUAL_HOSTING
6370512556aSDavid Nugent 	if ((hostname = malloc(MAXHOSTNAMELEN)) == NULL)
638618b0bbaSMark Murray 		fatalerror("Ran out of memory.");
63904683b2cSYaroslav Tykhiy 	if (gethostname(hostname, MAXHOSTNAMELEN - 1) < 0)
64004683b2cSYaroslav Tykhiy 		hostname[0] = '\0';
6419e9a43bdSBrian Somers 	hostname[MAXHOSTNAMELEN - 1] = '\0';
642ea4e54b9SDavid Nugent #endif
643c152df28SYaroslav Tykhiy 	if (hostinfo)
644ea022d16SRodney W. Grimes 		reply(220, "%s FTP server (%s) ready.", hostname, version);
645c152df28SYaroslav Tykhiy 	else
646c152df28SYaroslav Tykhiy 		reply(220, "FTP server ready.");
647ea022d16SRodney W. Grimes 	for (;;)
648ea022d16SRodney W. Grimes 		(void) yyparse();
649ea022d16SRodney W. Grimes 	/* NOTREACHED */
650ea022d16SRodney W. Grimes }
651ea022d16SRodney W. Grimes 
652ea022d16SRodney W. Grimes static void
653e4bc453cSWarner Losh lostconn(int signo)
654ea022d16SRodney W. Grimes {
655ea022d16SRodney W. Grimes 
656618b0bbaSMark Murray 	if (ftpdebug)
657ea022d16SRodney W. Grimes 		syslog(LOG_DEBUG, "lost connection");
658e02897faSPhilippe Charnier 	dologout(1);
659ea022d16SRodney W. Grimes }
660ea022d16SRodney W. Grimes 
6614b82fc95SYaroslav Tykhiy static void
662e4bc453cSWarner Losh sigquit(int signo)
6634b82fc95SYaroslav Tykhiy {
6644b82fc95SYaroslav Tykhiy 
6654b82fc95SYaroslav Tykhiy 	syslog(LOG_ERR, "got signal %d", signo);
6664b82fc95SYaroslav Tykhiy 	dologout(1);
6674b82fc95SYaroslav Tykhiy }
6684b82fc95SYaroslav Tykhiy 
669ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
670ea4e54b9SDavid Nugent /*
671ea4e54b9SDavid Nugent  * read in virtual host tables (if they exist)
672ea4e54b9SDavid Nugent  */
673ea4e54b9SDavid Nugent 
674ea4e54b9SDavid Nugent static void
6752c9fd5f2SHajimu UMEMOTO inithosts(int family)
676ea4e54b9SDavid Nugent {
67755b54aa7SYaroslav Tykhiy 	int insert;
678737d08f3SYaroslav Tykhiy 	size_t len;
679ea4e54b9SDavid Nugent 	FILE *fp;
680737d08f3SYaroslav Tykhiy 	char *cp, *mp, *line;
681737d08f3SYaroslav Tykhiy 	char *hostname;
68255b54aa7SYaroslav Tykhiy 	char *vhost, *anonuser, *statfile, *welcome, *loginmsg;
683ea4e54b9SDavid Nugent 	struct ftphost *hrp, *lhrp;
6844dd8b5abSYoshinobu Inoue 	struct addrinfo hints, *res, *ai;
685ea4e54b9SDavid Nugent 
686ea4e54b9SDavid Nugent 	/*
687ea4e54b9SDavid Nugent 	 * Fill in the default host information
688ea4e54b9SDavid Nugent 	 */
689737d08f3SYaroslav Tykhiy 	if ((hostname = malloc(MAXHOSTNAMELEN)) == NULL)
690618b0bbaSMark Murray 		fatalerror("Ran out of memory.");
69104683b2cSYaroslav Tykhiy 	if (gethostname(hostname, MAXHOSTNAMELEN - 1) < 0)
692737d08f3SYaroslav Tykhiy 		hostname[0] = '\0';
693737d08f3SYaroslav Tykhiy 	hostname[MAXHOSTNAMELEN - 1] = '\0';
694737d08f3SYaroslav Tykhiy 	if ((hrp = malloc(sizeof(struct ftphost))) == NULL)
695737d08f3SYaroslav Tykhiy 		fatalerror("Ran out of memory.");
696737d08f3SYaroslav Tykhiy 	hrp->hostname = hostname;
697f38c6cadSYoshinobu Inoue 	hrp->hostinfo = NULL;
6984dd8b5abSYoshinobu Inoue 
6994dd8b5abSYoshinobu Inoue 	memset(&hints, 0, sizeof(hints));
7002c9fd5f2SHajimu UMEMOTO 	hints.ai_flags = AI_PASSIVE;
7012c9fd5f2SHajimu UMEMOTO 	hints.ai_family = family;
7022c9fd5f2SHajimu UMEMOTO 	hints.ai_socktype = SOCK_STREAM;
703b1d8d5cdSYaroslav Tykhiy 	if (getaddrinfo(hrp->hostname, NULL, &hints, &res) == 0)
704f38c6cadSYoshinobu Inoue 		hrp->hostinfo = res;
705ea4e54b9SDavid Nugent 	hrp->statfile = _PATH_FTPDSTATFILE;
706ea4e54b9SDavid Nugent 	hrp->welcome  = _PATH_FTPWELCOME;
707ea4e54b9SDavid Nugent 	hrp->loginmsg = _PATH_FTPLOGINMESG;
708ea4e54b9SDavid Nugent 	hrp->anonuser = "ftp";
709ea4e54b9SDavid Nugent 	hrp->next = NULL;
710ea4e54b9SDavid Nugent 	thishost = firsthost = lhrp = hrp;
711ea4e54b9SDavid Nugent 	if ((fp = fopen(_PATH_FTPHOSTS, "r")) != NULL) {
712ec009cf0SYaroslav Tykhiy 		int addrsize, gothost;
7134dd8b5abSYoshinobu Inoue 		void *addr;
7144dd8b5abSYoshinobu Inoue 		struct hostent *hp;
7154dd8b5abSYoshinobu Inoue 
716737d08f3SYaroslav Tykhiy 		while ((line = fgetln(fp, &len)) != NULL) {
7174dd8b5abSYoshinobu Inoue 			int	i, hp_error;
718ea4e54b9SDavid Nugent 
719737d08f3SYaroslav Tykhiy 			/* skip comments */
720737d08f3SYaroslav Tykhiy 			if (line[0] == '#')
721ea4e54b9SDavid Nugent 				continue;
722737d08f3SYaroslav Tykhiy 			if (line[len - 1] == '\n') {
723737d08f3SYaroslav Tykhiy 				line[len - 1] = '\0';
724737d08f3SYaroslav Tykhiy 				mp = NULL;
725737d08f3SYaroslav Tykhiy 			} else {
726737d08f3SYaroslav Tykhiy 				if ((mp = malloc(len + 1)) == NULL)
727737d08f3SYaroslav Tykhiy 					fatalerror("Ran out of memory.");
728737d08f3SYaroslav Tykhiy 				memcpy(mp, line, len);
729737d08f3SYaroslav Tykhiy 				mp[len] = '\0';
730737d08f3SYaroslav Tykhiy 				line = mp;
731ea4e54b9SDavid Nugent 			}
732ea4e54b9SDavid Nugent 			cp = strtok(line, " \t");
733737d08f3SYaroslav Tykhiy 			/* skip empty lines */
734737d08f3SYaroslav Tykhiy 			if (cp == NULL)
735737d08f3SYaroslav Tykhiy 				goto nextline;
73655b54aa7SYaroslav Tykhiy 			vhost = cp;
73755b54aa7SYaroslav Tykhiy 
73855b54aa7SYaroslav Tykhiy 			/* set defaults */
73955b54aa7SYaroslav Tykhiy 			anonuser = "ftp";
74055b54aa7SYaroslav Tykhiy 			statfile = _PATH_FTPDSTATFILE;
74155b54aa7SYaroslav Tykhiy 			welcome  = _PATH_FTPWELCOME;
74255b54aa7SYaroslav Tykhiy 			loginmsg = _PATH_FTPLOGINMESG;
74355b54aa7SYaroslav Tykhiy 
74455b54aa7SYaroslav Tykhiy 			/*
74555b54aa7SYaroslav Tykhiy 			 * Preparse the line so we can use its info
74655b54aa7SYaroslav Tykhiy 			 * for all the addresses associated with
74755b54aa7SYaroslav Tykhiy 			 * the virtual host name.
74855b54aa7SYaroslav Tykhiy 			 * Field 0, the virtual host name, is special:
74955b54aa7SYaroslav Tykhiy 			 * it's already parsed off and will be strdup'ed
75055b54aa7SYaroslav Tykhiy 			 * later, after we know its canonical form.
75155b54aa7SYaroslav Tykhiy 			 */
75255b54aa7SYaroslav Tykhiy 			for (i = 1; i < 5 && (cp = strtok(NULL, " \t")); i++)
75355b54aa7SYaroslav Tykhiy 				if (*cp != '-' && (cp = strdup(cp)))
75455b54aa7SYaroslav Tykhiy 					switch (i) {
75555b54aa7SYaroslav Tykhiy 					case 1:	/* anon user permissions */
75655b54aa7SYaroslav Tykhiy 						anonuser = cp;
75755b54aa7SYaroslav Tykhiy 						break;
75855b54aa7SYaroslav Tykhiy 					case 2: /* statistics file */
75955b54aa7SYaroslav Tykhiy 						statfile = cp;
76055b54aa7SYaroslav Tykhiy 						break;
76155b54aa7SYaroslav Tykhiy 					case 3: /* welcome message */
76255b54aa7SYaroslav Tykhiy 						welcome  = cp;
76355b54aa7SYaroslav Tykhiy 						break;
76455b54aa7SYaroslav Tykhiy 					case 4: /* login message */
76555b54aa7SYaroslav Tykhiy 						loginmsg = cp;
76655b54aa7SYaroslav Tykhiy 						break;
76755b54aa7SYaroslav Tykhiy 					default: /* programming error */
76855b54aa7SYaroslav Tykhiy 						abort();
76955b54aa7SYaroslav Tykhiy 						/* NOTREACHED */
77055b54aa7SYaroslav Tykhiy 					}
7714dd8b5abSYoshinobu Inoue 
7724dd8b5abSYoshinobu Inoue 			hints.ai_flags = AI_PASSIVE;
7732c9fd5f2SHajimu UMEMOTO 			hints.ai_family = family;
7742c9fd5f2SHajimu UMEMOTO 			hints.ai_socktype = SOCK_STREAM;
775b1d8d5cdSYaroslav Tykhiy 			if (getaddrinfo(vhost, NULL, &hints, &res) != 0)
776737d08f3SYaroslav Tykhiy 				goto nextline;
7774dd8b5abSYoshinobu Inoue 			for (ai = res; ai != NULL && ai->ai_addr != NULL;
778b535a9bfSDavid Nugent 			     ai = ai->ai_next) {
7794dd8b5abSYoshinobu Inoue 
780b535a9bfSDavid Nugent 			gothost = 0;
781ea4e54b9SDavid Nugent 			for (hrp = firsthost; hrp != NULL; hrp = hrp->next) {
782f38c6cadSYoshinobu Inoue 				struct addrinfo *hi;
783f38c6cadSYoshinobu Inoue 
784f38c6cadSYoshinobu Inoue 				for (hi = hrp->hostinfo; hi != NULL;
785f38c6cadSYoshinobu Inoue 				     hi = hi->ai_next)
786f38c6cadSYoshinobu Inoue 					if (hi->ai_addrlen == ai->ai_addrlen &&
787f38c6cadSYoshinobu Inoue 					    memcmp(hi->ai_addr,
7884dd8b5abSYoshinobu Inoue 						   ai->ai_addr,
789b535a9bfSDavid Nugent 						   ai->ai_addr->sa_len) == 0) {
790b535a9bfSDavid Nugent 						gothost++;
791b535a9bfSDavid Nugent 						break;
792b535a9bfSDavid Nugent 					}
793b535a9bfSDavid Nugent 				if (gothost)
794ea4e54b9SDavid Nugent 					break;
795ea4e54b9SDavid Nugent 			}
796ea4e54b9SDavid Nugent 			if (hrp == NULL) {
797ea4e54b9SDavid Nugent 				if ((hrp = malloc(sizeof(struct ftphost))) == NULL)
798737d08f3SYaroslav Tykhiy 					goto nextline;
799b1d8d5cdSYaroslav Tykhiy 				hrp->hostname = NULL;
80055b54aa7SYaroslav Tykhiy 				insert = 1;
801f2fe752dSYaroslav Tykhiy 			} else {
8021f75c13eSYaroslav Tykhiy 				if (hrp->hostinfo && hrp->hostinfo != res)
803b1d8d5cdSYaroslav Tykhiy 					freeaddrinfo(hrp->hostinfo);
804f2fe752dSYaroslav Tykhiy 				insert = 0; /* host already in the chain */
805f2fe752dSYaroslav Tykhiy 			}
806f38c6cadSYoshinobu Inoue 			hrp->hostinfo = res;
807f38c6cadSYoshinobu Inoue 
808ea4e54b9SDavid Nugent 			/*
809ea4e54b9SDavid Nugent 			 * determine hostname to use.
8104dd8b5abSYoshinobu Inoue 			 * force defined name if there is a valid alias
811ea4e54b9SDavid Nugent 			 * otherwise fallback to primary hostname
812ea4e54b9SDavid Nugent 			 */
8134dd8b5abSYoshinobu Inoue 			/* XXX: getaddrinfo() can't do alias check */
814f38c6cadSYoshinobu Inoue 			switch(hrp->hostinfo->ai_family) {
8154dd8b5abSYoshinobu Inoue 			case AF_INET:
8164b4cc4c6SYaroslav Tykhiy 				addr = &((struct sockaddr_in *)hrp->hostinfo->ai_addr)->sin_addr;
8174b4cc4c6SYaroslav Tykhiy 				addrsize = sizeof(struct in_addr);
8184dd8b5abSYoshinobu Inoue 				break;
8194dd8b5abSYoshinobu Inoue 			case AF_INET6:
8204b4cc4c6SYaroslav Tykhiy 				addr = &((struct sockaddr_in6 *)hrp->hostinfo->ai_addr)->sin6_addr;
8214b4cc4c6SYaroslav Tykhiy 				addrsize = sizeof(struct in6_addr);
8224dd8b5abSYoshinobu Inoue 				break;
8234dd8b5abSYoshinobu Inoue 			default:
8244dd8b5abSYoshinobu Inoue 				/* should not reach here */
825f38c6cadSYoshinobu Inoue 				freeaddrinfo(hrp->hostinfo);
826f2fe752dSYaroslav Tykhiy 				if (insert)
827f2fe752dSYaroslav Tykhiy 					free(hrp); /*not in chain, can free*/
828f2fe752dSYaroslav Tykhiy 				else
829f2fe752dSYaroslav Tykhiy 					hrp->hostinfo = NULL; /*mark as blank*/
830737d08f3SYaroslav Tykhiy 				goto nextline;
8314dd8b5abSYoshinobu Inoue 				/* NOTREACHED */
8324dd8b5abSYoshinobu Inoue 			}
83357d4ef07SYaroslav Tykhiy 			if ((hp = getipnodebyaddr(addr, addrsize,
834f38c6cadSYoshinobu Inoue 						  hrp->hostinfo->ai_family,
8354dd8b5abSYoshinobu Inoue 						  &hp_error)) != NULL) {
83655b54aa7SYaroslav Tykhiy 				if (strcmp(vhost, hp->h_name) != 0) {
837ea4e54b9SDavid Nugent 					if (hp->h_aliases == NULL)
83855b54aa7SYaroslav Tykhiy 						vhost = hp->h_name;
839ea4e54b9SDavid Nugent 					else {
840ea4e54b9SDavid Nugent 						i = 0;
841ea4e54b9SDavid Nugent 						while (hp->h_aliases[i] &&
84255b54aa7SYaroslav Tykhiy 						       strcmp(vhost, hp->h_aliases[i]) != 0)
843ea4e54b9SDavid Nugent 							++i;
844ea4e54b9SDavid Nugent 						if (hp->h_aliases[i] == NULL)
84555b54aa7SYaroslav Tykhiy 							vhost = hp->h_name;
846ea4e54b9SDavid Nugent 					}
847ea4e54b9SDavid Nugent 				}
848ea4e54b9SDavid Nugent 			}
849b1d8d5cdSYaroslav Tykhiy 			if (hrp->hostname &&
850b1d8d5cdSYaroslav Tykhiy 			    strcmp(hrp->hostname, vhost) != 0) {
851b1d8d5cdSYaroslav Tykhiy 				free(hrp->hostname);
852b1d8d5cdSYaroslav Tykhiy 				hrp->hostname = NULL;
853b1d8d5cdSYaroslav Tykhiy 			}
854b1d8d5cdSYaroslav Tykhiy 			if (hrp->hostname == NULL &&
855f2fe752dSYaroslav Tykhiy 			    (hrp->hostname = strdup(vhost)) == NULL) {
856f2fe752dSYaroslav Tykhiy 				freeaddrinfo(hrp->hostinfo);
857f2fe752dSYaroslav Tykhiy 				hrp->hostinfo = NULL; /* mark as blank */
858f2fe752dSYaroslav Tykhiy 				if (hp)
859f2fe752dSYaroslav Tykhiy 					freehostent(hp);
86055b54aa7SYaroslav Tykhiy 				goto nextline;
861f2fe752dSYaroslav Tykhiy 			}
86255b54aa7SYaroslav Tykhiy 			hrp->anonuser = anonuser;
86355b54aa7SYaroslav Tykhiy 			hrp->statfile = statfile;
86455b54aa7SYaroslav Tykhiy 			hrp->welcome  = welcome;
86555b54aa7SYaroslav Tykhiy 			hrp->loginmsg = loginmsg;
86655b54aa7SYaroslav Tykhiy 			if (insert) {
86755b54aa7SYaroslav Tykhiy 				hrp->next  = NULL;
86855b54aa7SYaroslav Tykhiy 				lhrp->next = hrp;
86955b54aa7SYaroslav Tykhiy 				lhrp = hrp;
87055b54aa7SYaroslav Tykhiy 			}
871233c0f66SYaroslav Tykhiy 			if (hp)
8724dd8b5abSYoshinobu Inoue 				freehostent(hp);
8734dd8b5abSYoshinobu Inoue 		      }
874737d08f3SYaroslav Tykhiy nextline:
875737d08f3SYaroslav Tykhiy 			if (mp)
876737d08f3SYaroslav Tykhiy 				free(mp);
877ea4e54b9SDavid Nugent 		}
878ea4e54b9SDavid Nugent 		(void) fclose(fp);
879ea4e54b9SDavid Nugent 	}
880ea4e54b9SDavid Nugent }
881ea4e54b9SDavid Nugent 
882ea4e54b9SDavid Nugent static void
883e4bc453cSWarner Losh selecthost(union sockunion *su)
884ea4e54b9SDavid Nugent {
885ea4e54b9SDavid Nugent 	struct ftphost	*hrp;
8864dd8b5abSYoshinobu Inoue 	u_int16_t port;
8874dd8b5abSYoshinobu Inoue #ifdef INET6
8884dd8b5abSYoshinobu Inoue 	struct in6_addr *mapped_in6 = NULL;
8894dd8b5abSYoshinobu Inoue #endif
890f38c6cadSYoshinobu Inoue 	struct addrinfo *hi;
8914dd8b5abSYoshinobu Inoue 
8924dd8b5abSYoshinobu Inoue #ifdef INET6
8934dd8b5abSYoshinobu Inoue 	/*
8944dd8b5abSYoshinobu Inoue 	 * XXX IPv4 mapped IPv6 addr consideraton,
8954dd8b5abSYoshinobu Inoue 	 * specified in rfc2373.
8964dd8b5abSYoshinobu Inoue 	 */
8974dd8b5abSYoshinobu Inoue 	if (su->su_family == AF_INET6 &&
8984dd8b5abSYoshinobu Inoue 	    IN6_IS_ADDR_V4MAPPED(&su->su_sin6.sin6_addr))
8994dd8b5abSYoshinobu Inoue 		mapped_in6 = &su->su_sin6.sin6_addr;
9004dd8b5abSYoshinobu Inoue #endif
901ea4e54b9SDavid Nugent 
902ea4e54b9SDavid Nugent 	hrp = thishost = firsthost;	/* default */
9034dd8b5abSYoshinobu Inoue 	port = su->su_port;
9044dd8b5abSYoshinobu Inoue 	su->su_port = 0;
905ea4e54b9SDavid Nugent 	while (hrp != NULL) {
906b535a9bfSDavid Nugent 	    for (hi = hrp->hostinfo; hi != NULL; hi = hi->ai_next) {
907f38c6cadSYoshinobu Inoue 		if (memcmp(su, hi->ai_addr, hi->ai_addrlen) == 0) {
908ea4e54b9SDavid Nugent 			thishost = hrp;
909ebd83647SYaroslav Tykhiy 			goto found;
910ea4e54b9SDavid Nugent 		}
9114dd8b5abSYoshinobu Inoue #ifdef INET6
9124dd8b5abSYoshinobu Inoue 		/* XXX IPv4 mapped IPv6 addr consideraton */
913f38c6cadSYoshinobu Inoue 		if (hi->ai_addr->sa_family == AF_INET && mapped_in6 != NULL &&
9144dd8b5abSYoshinobu Inoue 		    (memcmp(&mapped_in6->s6_addr[12],
915f38c6cadSYoshinobu Inoue 			    &((struct sockaddr_in *)hi->ai_addr)->sin_addr,
9164dd8b5abSYoshinobu Inoue 			    sizeof(struct in_addr)) == 0)) {
9174dd8b5abSYoshinobu Inoue 			thishost = hrp;
918ebd83647SYaroslav Tykhiy 			goto found;
9194dd8b5abSYoshinobu Inoue 		}
9204dd8b5abSYoshinobu Inoue #endif
921f38c6cadSYoshinobu Inoue 	    }
922ea4e54b9SDavid Nugent 	    hrp = hrp->next;
923ea4e54b9SDavid Nugent 	}
924ebd83647SYaroslav Tykhiy found:
9254dd8b5abSYoshinobu Inoue 	su->su_port = port;
926ea4e54b9SDavid Nugent 	/* setup static variables as appropriate */
927ea4e54b9SDavid Nugent 	hostname = thishost->hostname;
928ea4e54b9SDavid Nugent 	ftpuser = thishost->anonuser;
929ea4e54b9SDavid Nugent }
930ea4e54b9SDavid Nugent #endif
931ea4e54b9SDavid Nugent 
932ea022d16SRodney W. Grimes /*
933ea022d16SRodney W. Grimes  * Helper function for sgetpwnam().
934ea022d16SRodney W. Grimes  */
935ea022d16SRodney W. Grimes static char *
936e4bc453cSWarner Losh sgetsave(char *s)
937ea022d16SRodney W. Grimes {
938e3765043SYaroslav Tykhiy 	char *new = malloc(strlen(s) + 1);
939ea022d16SRodney W. Grimes 
940ea022d16SRodney W. Grimes 	if (new == NULL) {
94102c97492SYaroslav Tykhiy 		reply(421, "Ran out of memory.");
942ea022d16SRodney W. Grimes 		dologout(1);
943ea022d16SRodney W. Grimes 		/* NOTREACHED */
944ea022d16SRodney W. Grimes 	}
945ea022d16SRodney W. Grimes 	(void) strcpy(new, s);
946ea022d16SRodney W. Grimes 	return (new);
947ea022d16SRodney W. Grimes }
948ea022d16SRodney W. Grimes 
949ea022d16SRodney W. Grimes /*
950ea022d16SRodney W. Grimes  * Save the result of a getpwnam.  Used for USER command, since
951ea022d16SRodney W. Grimes  * the data returned must not be clobbered by any other command
952ea022d16SRodney W. Grimes  * (e.g., globbing).
953c29b9b47SYaroslav Tykhiy  * NB: The data returned by sgetpwnam() will remain valid until
954c29b9b47SYaroslav Tykhiy  * the next call to this function.  Its difference from getpwnam()
955c29b9b47SYaroslav Tykhiy  * is that sgetpwnam() is known to be called from ftpd code only.
956ea022d16SRodney W. Grimes  */
957ea022d16SRodney W. Grimes static struct passwd *
958e4bc453cSWarner Losh sgetpwnam(char *name)
959ea022d16SRodney W. Grimes {
960ea022d16SRodney W. Grimes 	static struct passwd save;
961ea022d16SRodney W. Grimes 	struct passwd *p;
962ea022d16SRodney W. Grimes 
963ea022d16SRodney W. Grimes 	if ((p = getpwnam(name)) == NULL)
964ea022d16SRodney W. Grimes 		return (p);
965ea022d16SRodney W. Grimes 	if (save.pw_name) {
966ea022d16SRodney W. Grimes 		free(save.pw_name);
967ea022d16SRodney W. Grimes 		free(save.pw_passwd);
968ea022d16SRodney W. Grimes 		free(save.pw_gecos);
969ea022d16SRodney W. Grimes 		free(save.pw_dir);
970ea022d16SRodney W. Grimes 		free(save.pw_shell);
971ea022d16SRodney W. Grimes 	}
972ea022d16SRodney W. Grimes 	save = *p;
973ea022d16SRodney W. Grimes 	save.pw_name = sgetsave(p->pw_name);
974ea022d16SRodney W. Grimes 	save.pw_passwd = sgetsave(p->pw_passwd);
975ea022d16SRodney W. Grimes 	save.pw_gecos = sgetsave(p->pw_gecos);
976ea022d16SRodney W. Grimes 	save.pw_dir = sgetsave(p->pw_dir);
977ea022d16SRodney W. Grimes 	save.pw_shell = sgetsave(p->pw_shell);
978ea022d16SRodney W. Grimes 	return (&save);
979ea022d16SRodney W. Grimes }
980ea022d16SRodney W. Grimes 
981ea022d16SRodney W. Grimes static int login_attempts;	/* number of failed login attempts */
982ea022d16SRodney W. Grimes static int askpasswd;		/* had user command, ask for passwd */
98390906a46SSheldon Hearn static char curname[MAXLOGNAME];	/* current USER name */
984ea022d16SRodney W. Grimes 
985ea022d16SRodney W. Grimes /*
986ea022d16SRodney W. Grimes  * USER command.
987ea022d16SRodney W. Grimes  * Sets global passwd pointer pw if named account exists and is acceptable;
988ea022d16SRodney W. Grimes  * sets askpasswd if a PASS command is expected.  If logged in previously,
989ea022d16SRodney W. Grimes  * need to reset state.  If name is "ftp" or "anonymous", the name is not in
990ea022d16SRodney W. Grimes  * _PATH_FTPUSERS, and ftp account exists, set guest and pw, then just return.
991ea022d16SRodney W. Grimes  * If account doesn't exist, ask for passwd anyway.  Otherwise, check user
992ea022d16SRodney W. Grimes  * requesting login privileges.  Disallow anyone who does not have a standard
993ea022d16SRodney W. Grimes  * shell as returned by getusershell().  Disallow anyone mentioned in the file
994ea022d16SRodney W. Grimes  * _PATH_FTPUSERS to allow people such as root and uucp to be avoided.
995ea022d16SRodney W. Grimes  */
996ea022d16SRodney W. Grimes void
997e4bc453cSWarner Losh user(char *name)
998ea022d16SRodney W. Grimes {
999cefb6785SChristian S.J. Peron 	int ecode;
1000ea022d16SRodney W. Grimes 	char *cp, *shell;
1001ea022d16SRodney W. Grimes 
1002ea022d16SRodney W. Grimes 	if (logged_in) {
1003ea022d16SRodney W. Grimes 		if (guest) {
1004ea022d16SRodney W. Grimes 			reply(530, "Can't change user from guest login.");
1005ea022d16SRodney W. Grimes 			return;
1006a5a4544eSPaul Traina 		} else if (dochroot) {
1007a5a4544eSPaul Traina 			reply(530, "Can't change user from chroot user.");
1008a5a4544eSPaul Traina 			return;
1009ea022d16SRodney W. Grimes 		}
1010ea022d16SRodney W. Grimes 		end_login();
1011ea022d16SRodney W. Grimes 	}
1012ea022d16SRodney W. Grimes 
1013ea022d16SRodney W. Grimes 	guest = 0;
101463047c6fSDavid E. O'Brien #ifdef VIRTUAL_HOSTING
101563047c6fSDavid E. O'Brien 	pw = sgetpwnam(thishost->anonuser);
101663047c6fSDavid E. O'Brien #else
101763047c6fSDavid E. O'Brien 	pw = sgetpwnam("ftp");
101863047c6fSDavid E. O'Brien #endif
1019ea022d16SRodney W. Grimes 	if (strcmp(name, "ftp") == 0 || strcmp(name, "anonymous") == 0) {
1020cefb6785SChristian S.J. Peron 		if (checkuser(_PATH_FTPUSERS, "ftp", 0, NULL, &ecode) ||
1021cefb6785SChristian S.J. Peron 		    (ecode != 0 && ecode != ENOENT))
1022cefb6785SChristian S.J. Peron 			reply(530, "User %s access denied.", name);
1023cefb6785SChristian S.J. Peron 		else if (checkuser(_PATH_FTPUSERS, "anonymous", 0, NULL, &ecode) ||
1024cefb6785SChristian S.J. Peron 		    (ecode != 0 && ecode != ENOENT))
1025ea022d16SRodney W. Grimes 			reply(530, "User %s access denied.", name);
102663047c6fSDavid E. O'Brien 		else if (pw != NULL) {
1027ea022d16SRodney W. Grimes 			guest = 1;
1028ea022d16SRodney W. Grimes 			askpasswd = 1;
1029ea022d16SRodney W. Grimes 			reply(331,
10302c60c54cSPaul Traina 			"Guest login ok, send your email address as password.");
1031ea022d16SRodney W. Grimes 		} else
1032ea022d16SRodney W. Grimes 			reply(530, "User %s unknown.", name);
1033ea022d16SRodney W. Grimes 		if (!askpasswd && logging)
1034ea022d16SRodney W. Grimes 			syslog(LOG_NOTICE,
1035ea022d16SRodney W. Grimes 			    "ANONYMOUS FTP LOGIN REFUSED FROM %s", remotehost);
1036ea022d16SRodney W. Grimes 		return;
1037ea022d16SRodney W. Grimes 	}
10385a392aecSTorsten Blum 	if (anon_only != 0) {
10395a392aecSTorsten Blum 		reply(530, "Sorry, only anonymous ftp allowed.");
10405a392aecSTorsten Blum 		return;
10415a392aecSTorsten Blum 	}
10425a392aecSTorsten Blum 
10439aca17cbSMark Murray 	if ((pw = sgetpwnam(name))) {
1044ea022d16SRodney W. Grimes 		if ((shell = pw->pw_shell) == NULL || *shell == 0)
1045ea022d16SRodney W. Grimes 			shell = _PATH_BSHELL;
1046c433c9daSPhilippe Charnier 		setusershell();
1047ea022d16SRodney W. Grimes 		while ((cp = getusershell()) != NULL)
1048ea022d16SRodney W. Grimes 			if (strcmp(cp, shell) == 0)
1049ea022d16SRodney W. Grimes 				break;
1050ea022d16SRodney W. Grimes 		endusershell();
1051ea022d16SRodney W. Grimes 
1052cefb6785SChristian S.J. Peron 		if (cp == NULL ||
1053cefb6785SChristian S.J. Peron 		    (checkuser(_PATH_FTPUSERS, name, 1, NULL, &ecode) ||
1054cefb6785SChristian S.J. Peron 		    (ecode != 0 && ecode != ENOENT))) {
1055ea022d16SRodney W. Grimes 			reply(530, "User %s access denied.", name);
1056ea022d16SRodney W. Grimes 			if (logging)
1057ea022d16SRodney W. Grimes 				syslog(LOG_NOTICE,
1058ea022d16SRodney W. Grimes 				    "FTP LOGIN REFUSED FROM %s, %s",
1059ea022d16SRodney W. Grimes 				    remotehost, name);
10607e295315SYaroslav Tykhiy 			pw = NULL;
1061ea022d16SRodney W. Grimes 			return;
1062ea022d16SRodney W. Grimes 		}
1063ea022d16SRodney W. Grimes 	}
1064ea022d16SRodney W. Grimes 	if (logging)
1065ea022d16SRodney W. Grimes 		strncpy(curname, name, sizeof(curname)-1);
106647499ecdSAndrey A. Chernov 
106747499ecdSAndrey A. Chernov 	pwok = 0;
1068fa1746c9SMark Murray #ifdef USE_PAM
1069fa1746c9SMark Murray 	/* XXX Kluge! The conversation mechanism needs to be fixed. */
1070726040deSGuido van Rooij #endif
107147499ecdSAndrey A. Chernov 	if (opiechallenge(&opiedata, name, opieprompt) == 0) {
107247499ecdSAndrey A. Chernov 		pwok = (pw != NULL) &&
107347499ecdSAndrey A. Chernov 		       opieaccessfile(remotehost) &&
107447499ecdSAndrey A. Chernov 		       opiealways(pw->pw_dir);
107547499ecdSAndrey A. Chernov 		reply(331, "Response to %s %s for %s.",
107647499ecdSAndrey A. Chernov 		      opieprompt, pwok ? "requested" : "required", name);
107747499ecdSAndrey A. Chernov 	} else {
107847499ecdSAndrey A. Chernov 		pwok = 1;
107947499ecdSAndrey A. Chernov 		reply(331, "Password required for %s.", name);
108047499ecdSAndrey A. Chernov 	}
1081ea022d16SRodney W. Grimes 	askpasswd = 1;
1082ea022d16SRodney W. Grimes 	/*
1083ea022d16SRodney W. Grimes 	 * Delay before reading passwd after first failed
1084ea022d16SRodney W. Grimes 	 * attempt to slow down passwd-guessing programs.
1085ea022d16SRodney W. Grimes 	 */
1086ea022d16SRodney W. Grimes 	if (login_attempts)
1087e3765043SYaroslav Tykhiy 		sleep(login_attempts);
1088ea022d16SRodney W. Grimes }
1089ea022d16SRodney W. Grimes 
1090ea022d16SRodney W. Grimes /*
10918657b576SYaroslav Tykhiy  * Check if a user is in the file "fname",
10928657b576SYaroslav Tykhiy  * return a pointer to a malloc'd string with the rest
10938657b576SYaroslav Tykhiy  * of the matching line in "residue" if not NULL.
1094ea022d16SRodney W. Grimes  */
1095ea022d16SRodney W. Grimes static int
1096cefb6785SChristian S.J. Peron checkuser(char *fname, char *name, int pwset, char **residue, int *ecode)
1097ea022d16SRodney W. Grimes {
1098ea022d16SRodney W. Grimes 	FILE *fd;
1099ea022d16SRodney W. Grimes 	int found = 0;
1100737d08f3SYaroslav Tykhiy 	size_t len;
1101737d08f3SYaroslav Tykhiy 	char *line, *mp, *p;
1102ea022d16SRodney W. Grimes 
1103cefb6785SChristian S.J. Peron 	if (ecode != NULL)
1104cefb6785SChristian S.J. Peron 		*ecode = 0;
1105a5a4544eSPaul Traina 	if ((fd = fopen(fname, "r")) != NULL) {
1106737d08f3SYaroslav Tykhiy 		while (!found && (line = fgetln(fd, &len)) != NULL) {
1107737d08f3SYaroslav Tykhiy 			/* skip comments */
1108ea022d16SRodney W. Grimes 			if (line[0] == '#')
1109ea022d16SRodney W. Grimes 				continue;
1110737d08f3SYaroslav Tykhiy 			if (line[len - 1] == '\n') {
1111737d08f3SYaroslav Tykhiy 				line[len - 1] = '\0';
1112737d08f3SYaroslav Tykhiy 				mp = NULL;
1113737d08f3SYaroslav Tykhiy 			} else {
1114737d08f3SYaroslav Tykhiy 				if ((mp = malloc(len + 1)) == NULL)
1115737d08f3SYaroslav Tykhiy 					fatalerror("Ran out of memory.");
1116737d08f3SYaroslav Tykhiy 				memcpy(mp, line, len);
1117737d08f3SYaroslav Tykhiy 				mp[len] = '\0';
1118737d08f3SYaroslav Tykhiy 				line = mp;
1119737d08f3SYaroslav Tykhiy 			}
1120737d08f3SYaroslav Tykhiy 			/* avoid possible leading and trailing whitespace */
1121737d08f3SYaroslav Tykhiy 			p = strtok(line, " \t");
1122737d08f3SYaroslav Tykhiy 			/* skip empty lines */
1123737d08f3SYaroslav Tykhiy 			if (p == NULL)
1124737d08f3SYaroslav Tykhiy 				goto nextline;
112531fea7b8SDavid Nugent 			/*
112631fea7b8SDavid Nugent 			 * if first chr is '@', check group membership
112731fea7b8SDavid Nugent 			 */
1128737d08f3SYaroslav Tykhiy 			if (p[0] == '@') {
112931fea7b8SDavid Nugent 				int i = 0;
113031fea7b8SDavid Nugent 				struct group *grp;
113131fea7b8SDavid Nugent 
11328657b576SYaroslav Tykhiy 				if (p[1] == '\0') /* single @ matches anyone */
11338657b576SYaroslav Tykhiy 					found = 1;
11348657b576SYaroslav Tykhiy 				else {
1135737d08f3SYaroslav Tykhiy 					if ((grp = getgrnam(p+1)) == NULL)
1136737d08f3SYaroslav Tykhiy 						goto nextline;
11377edcb936SSteve Price 					/*
11387edcb936SSteve Price 					 * Check user's default group
11397edcb936SSteve Price 					 */
11407edcb936SSteve Price 					if (pwset && grp->gr_gid == pw->pw_gid)
11417edcb936SSteve Price 						found = 1;
11427edcb936SSteve Price 					/*
11437edcb936SSteve Price 					 * Check supplementary groups
11447edcb936SSteve Price 					 */
114531fea7b8SDavid Nugent 					while (!found && grp->gr_mem[i])
114631fea7b8SDavid Nugent 						found = strcmp(name,
114731fea7b8SDavid Nugent 							grp->gr_mem[i++])
114831fea7b8SDavid Nugent 							== 0;
1149ea022d16SRodney W. Grimes 				}
11508657b576SYaroslav Tykhiy 			}
115131fea7b8SDavid Nugent 			/*
115231fea7b8SDavid Nugent 			 * Otherwise, just check for username match
115331fea7b8SDavid Nugent 			 */
115431fea7b8SDavid Nugent 			else
1155737d08f3SYaroslav Tykhiy 				found = strcmp(p, name) == 0;
11568657b576SYaroslav Tykhiy 			/*
11578657b576SYaroslav Tykhiy 			 * Save the rest of line to "residue" if matched
11588657b576SYaroslav Tykhiy 			 */
11598657b576SYaroslav Tykhiy 			if (found && residue) {
11600ba71e24SYaroslav Tykhiy 				if ((p = strtok(NULL, "")) != NULL)
11610ba71e24SYaroslav Tykhiy 					p += strspn(p, " \t");
11620ba71e24SYaroslav Tykhiy 				if (p && *p) {
11638657b576SYaroslav Tykhiy 				 	if ((*residue = strdup(p)) == NULL)
11648657b576SYaroslav Tykhiy 						fatalerror("Ran out of memory.");
11658657b576SYaroslav Tykhiy 				} else
11668657b576SYaroslav Tykhiy 					*residue = NULL;
11678657b576SYaroslav Tykhiy 			}
1168737d08f3SYaroslav Tykhiy nextline:
1169737d08f3SYaroslav Tykhiy 			if (mp)
1170737d08f3SYaroslav Tykhiy 				free(mp);
1171ea022d16SRodney W. Grimes 		}
1172ea022d16SRodney W. Grimes 		(void) fclose(fd);
1173cefb6785SChristian S.J. Peron 	} else if (ecode != NULL)
1174cefb6785SChristian S.J. Peron 		*ecode = errno;
1175ea022d16SRodney W. Grimes 	return (found);
1176ea022d16SRodney W. Grimes }
1177ea022d16SRodney W. Grimes 
1178ea022d16SRodney W. Grimes /*
1179ea022d16SRodney W. Grimes  * Terminate login as previous user, if any, resetting state;
1180ea022d16SRodney W. Grimes  * used when USER command is given or login fails.
1181ea022d16SRodney W. Grimes  */
1182ea022d16SRodney W. Grimes static void
1183e4bc453cSWarner Losh end_login(void)
1184ea022d16SRodney W. Grimes {
11855bc9d93dSMark Murray #ifdef USE_PAM
11865bc9d93dSMark Murray 	int e;
11875bc9d93dSMark Murray #endif
1188ea022d16SRodney W. Grimes 
118952e7ee74SYaroslav Tykhiy 	(void) seteuid(0);
11909f37b1a2SEd Schouten 	if (logged_in && dowtmp)
11919f37b1a2SEd Schouten 		ftpd_logwtmp(wtmpid, NULL, NULL);
1192ea022d16SRodney W. Grimes 	pw = NULL;
1193b071c689SDavid Nugent #ifdef	LOGIN_CAP
119452e7ee74SYaroslav Tykhiy 	setusercontext(NULL, getpwuid(0), 0,
1195d9e2c424SRobert Watson 		       LOGIN_SETPRIORITY|LOGIN_SETRESOURCES|LOGIN_SETUMASK|
1196d9e2c424SRobert Watson 		       LOGIN_SETMAC);
1197b071c689SDavid Nugent #endif
11985bc9d93dSMark Murray #ifdef USE_PAM
1199de45162dSYaroslav Tykhiy 	if (pamh) {
12005bc9d93dSMark Murray 		if ((e = pam_setcred(pamh, PAM_DELETE_CRED)) != PAM_SUCCESS)
12015bc9d93dSMark Murray 			syslog(LOG_ERR, "pam_setcred: %s", pam_strerror(pamh, e));
12025bc9d93dSMark Murray 		if ((e = pam_close_session(pamh,0)) != PAM_SUCCESS)
12035bc9d93dSMark Murray 			syslog(LOG_ERR, "pam_close_session: %s", pam_strerror(pamh, e));
12045bc9d93dSMark Murray 		if ((e = pam_end(pamh, e)) != PAM_SUCCESS)
12055bc9d93dSMark Murray 			syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e));
12065bc9d93dSMark Murray 		pamh = NULL;
1207de45162dSYaroslav Tykhiy 	}
12085bc9d93dSMark Murray #endif
1209ea022d16SRodney W. Grimes 	logged_in = 0;
1210ea022d16SRodney W. Grimes 	guest = 0;
1211a5a4544eSPaul Traina 	dochroot = 0;
1212ea022d16SRodney W. Grimes }
1213ea022d16SRodney W. Grimes 
12145bc9d93dSMark Murray #ifdef USE_PAM
12156c9134c0SMark Murray 
12166c9134c0SMark Murray /*
12176c9134c0SMark Murray  * the following code is stolen from imap-uw PAM authentication module and
12186c9134c0SMark Murray  * login.c
12196c9134c0SMark Murray  */
12206c9134c0SMark Murray #define COPY_STRING(s) (s ? strdup(s) : NULL)
12216c9134c0SMark Murray 
12226c9134c0SMark Murray struct cred_t {
12236c9134c0SMark Murray 	const char *uname;		/* user name */
12246c9134c0SMark Murray 	const char *pass;		/* password */
12256c9134c0SMark Murray };
12266c9134c0SMark Murray typedef struct cred_t cred_t;
12276c9134c0SMark Murray 
12286c9134c0SMark Murray static int
12296c9134c0SMark Murray auth_conv(int num_msg, const struct pam_message **msg,
12306c9134c0SMark Murray 	  struct pam_response **resp, void *appdata)
12316c9134c0SMark Murray {
12326c9134c0SMark Murray 	int i;
12336c9134c0SMark Murray 	cred_t *cred = (cred_t *) appdata;
123460769b19SDag-Erling Smørgrav 	struct pam_response *reply;
123560769b19SDag-Erling Smørgrav 
123660769b19SDag-Erling Smørgrav 	reply = calloc(num_msg, sizeof *reply);
123760769b19SDag-Erling Smørgrav 	if (reply == NULL)
123860769b19SDag-Erling Smørgrav 		return PAM_BUF_ERR;
12396c9134c0SMark Murray 
12406c9134c0SMark Murray 	for (i = 0; i < num_msg; i++) {
12416c9134c0SMark Murray 		switch (msg[i]->msg_style) {
12426c9134c0SMark Murray 		case PAM_PROMPT_ECHO_ON:	/* assume want user name */
12436c9134c0SMark Murray 			reply[i].resp_retcode = PAM_SUCCESS;
12446c9134c0SMark Murray 			reply[i].resp = COPY_STRING(cred->uname);
12456c9134c0SMark Murray 			/* PAM frees resp. */
12466c9134c0SMark Murray 			break;
12476c9134c0SMark Murray 		case PAM_PROMPT_ECHO_OFF:	/* assume want password */
12486c9134c0SMark Murray 			reply[i].resp_retcode = PAM_SUCCESS;
12496c9134c0SMark Murray 			reply[i].resp = COPY_STRING(cred->pass);
12506c9134c0SMark Murray 			/* PAM frees resp. */
12516c9134c0SMark Murray 			break;
12526c9134c0SMark Murray 		case PAM_TEXT_INFO:
12536c9134c0SMark Murray 		case PAM_ERROR_MSG:
12546c9134c0SMark Murray 			reply[i].resp_retcode = PAM_SUCCESS;
12556c9134c0SMark Murray 			reply[i].resp = NULL;
12566c9134c0SMark Murray 			break;
12576c9134c0SMark Murray 		default:			/* unknown message style */
12586c9134c0SMark Murray 			free(reply);
12596c9134c0SMark Murray 			return PAM_CONV_ERR;
12606c9134c0SMark Murray 		}
12616c9134c0SMark Murray 	}
12626c9134c0SMark Murray 
12636c9134c0SMark Murray 	*resp = reply;
12646c9134c0SMark Murray 	return PAM_SUCCESS;
12656c9134c0SMark Murray }
12666c9134c0SMark Murray 
12676c9134c0SMark Murray /*
12686c9134c0SMark Murray  * Attempt to authenticate the user using PAM.  Returns 0 if the user is
12696c9134c0SMark Murray  * authenticated, or 1 if not authenticated.  If some sort of PAM system
12706c9134c0SMark Murray  * error occurs (e.g., the "/etc/pam.conf" file is missing) then this
12716c9134c0SMark Murray  * function returns -1.  This can be used as an indication that we should
12726c9134c0SMark Murray  * fall back to a different authentication mechanism.
12736c9134c0SMark Murray  */
12746c9134c0SMark Murray static int
12756c9134c0SMark Murray auth_pam(struct passwd **ppw, const char *pass)
12766c9134c0SMark Murray {
12776c9134c0SMark Murray 	const char *tmpl_user;
12786c9134c0SMark Murray 	const void *item;
12796c9134c0SMark Murray 	int rval;
12806c9134c0SMark Murray 	int e;
12816c9134c0SMark Murray 	cred_t auth_cred = { (*ppw)->pw_name, pass };
12826c9134c0SMark Murray 	struct pam_conv conv = { &auth_conv, &auth_cred };
12836c9134c0SMark Murray 
12846c9134c0SMark Murray 	e = pam_start("ftpd", (*ppw)->pw_name, &conv, &pamh);
12856c9134c0SMark Murray 	if (e != PAM_SUCCESS) {
1286545ea864SYaroslav Tykhiy 		/*
1287545ea864SYaroslav Tykhiy 		 * In OpenPAM, it's OK to pass NULL to pam_strerror()
1288545ea864SYaroslav Tykhiy 		 * if context creation has failed in the first place.
1289545ea864SYaroslav Tykhiy 		 */
1290545ea864SYaroslav Tykhiy 		syslog(LOG_ERR, "pam_start: %s", pam_strerror(NULL, e));
12916c9134c0SMark Murray 		return -1;
12926c9134c0SMark Murray 	}
12936c9134c0SMark Murray 
1294ea413ab7SGuido van Rooij 	e = pam_set_item(pamh, PAM_RHOST, remotehost);
1295ea413ab7SGuido van Rooij 	if (e != PAM_SUCCESS) {
1296ea413ab7SGuido van Rooij 		syslog(LOG_ERR, "pam_set_item(PAM_RHOST): %s",
1297ea413ab7SGuido van Rooij 			pam_strerror(pamh, e));
1298de45162dSYaroslav Tykhiy 		if ((e = pam_end(pamh, e)) != PAM_SUCCESS) {
1299de45162dSYaroslav Tykhiy 			syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e));
1300de45162dSYaroslav Tykhiy 		}
1301de45162dSYaroslav Tykhiy 		pamh = NULL;
1302ea413ab7SGuido van Rooij 		return -1;
1303ea413ab7SGuido van Rooij 	}
1304ea413ab7SGuido van Rooij 
13056c9134c0SMark Murray 	e = pam_authenticate(pamh, 0);
13066c9134c0SMark Murray 	switch (e) {
13076c9134c0SMark Murray 	case PAM_SUCCESS:
13086c9134c0SMark Murray 		/*
13096c9134c0SMark Murray 		 * With PAM we support the concept of a "template"
13106c9134c0SMark Murray 		 * user.  The user enters a login name which is
13116c9134c0SMark Murray 		 * authenticated by PAM, usually via a remote service
13126c9134c0SMark Murray 		 * such as RADIUS or TACACS+.  If authentication
13136c9134c0SMark Murray 		 * succeeds, a different but related "template" name
13146c9134c0SMark Murray 		 * is used for setting the credentials, shell, and
13156c9134c0SMark Murray 		 * home directory.  The name the user enters need only
13166c9134c0SMark Murray 		 * exist on the remote authentication server, but the
13176c9134c0SMark Murray 		 * template name must be present in the local password
13186c9134c0SMark Murray 		 * database.
13196c9134c0SMark Murray 		 *
13206c9134c0SMark Murray 		 * This is supported by two various mechanisms in the
13216c9134c0SMark Murray 		 * individual modules.  However, from the application's
13226c9134c0SMark Murray 		 * point of view, the template user is always passed
13236c9134c0SMark Murray 		 * back as a changed value of the PAM_USER item.
13246c9134c0SMark Murray 		 */
13256c9134c0SMark Murray 		if ((e = pam_get_item(pamh, PAM_USER, &item)) ==
13266c9134c0SMark Murray 		    PAM_SUCCESS) {
13276c9134c0SMark Murray 			tmpl_user = (const char *) item;
13286c9134c0SMark Murray 			if (strcmp((*ppw)->pw_name, tmpl_user) != 0)
13296c9134c0SMark Murray 				*ppw = getpwnam(tmpl_user);
13306c9134c0SMark Murray 		} else
13316c9134c0SMark Murray 			syslog(LOG_ERR, "Couldn't get PAM_USER: %s",
13326c9134c0SMark Murray 			    pam_strerror(pamh, e));
13336c9134c0SMark Murray 		rval = 0;
13346c9134c0SMark Murray 		break;
13356c9134c0SMark Murray 
13366c9134c0SMark Murray 	case PAM_AUTH_ERR:
13376c9134c0SMark Murray 	case PAM_USER_UNKNOWN:
13386c9134c0SMark Murray 	case PAM_MAXTRIES:
13396c9134c0SMark Murray 		rval = 1;
13406c9134c0SMark Murray 		break;
13416c9134c0SMark Murray 
13426c9134c0SMark Murray 	default:
13435bc9d93dSMark Murray 		syslog(LOG_ERR, "pam_authenticate: %s", pam_strerror(pamh, e));
13446c9134c0SMark Murray 		rval = -1;
13456c9134c0SMark Murray 		break;
13466c9134c0SMark Murray 	}
13476c9134c0SMark Murray 
13485bc9d93dSMark Murray 	if (rval == 0) {
13495bc9d93dSMark Murray 		e = pam_acct_mgmt(pamh, 0);
13505bc9d93dSMark Murray 		if (e != PAM_SUCCESS) {
13514cbc4ad6SYaroslav Tykhiy 			syslog(LOG_ERR, "pam_acct_mgmt: %s",
13524cbc4ad6SYaroslav Tykhiy 						pam_strerror(pamh, e));
13535bc9d93dSMark Murray 			rval = 1;
13545bc9d93dSMark Murray 		}
13555bc9d93dSMark Murray 	}
13565bc9d93dSMark Murray 
13575bc9d93dSMark Murray 	if (rval != 0) {
13586c9134c0SMark Murray 		if ((e = pam_end(pamh, e)) != PAM_SUCCESS) {
13596c9134c0SMark Murray 			syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e));
13605bc9d93dSMark Murray 		}
13615bc9d93dSMark Murray 		pamh = NULL;
13626c9134c0SMark Murray 	}
13636c9134c0SMark Murray 	return rval;
13646c9134c0SMark Murray }
13656c9134c0SMark Murray 
13665bc9d93dSMark Murray #endif /* USE_PAM */
13676c9134c0SMark Murray 
1368ea022d16SRodney W. Grimes void
1369e4bc453cSWarner Losh pass(char *passwd)
1370ea022d16SRodney W. Grimes {
1371cefb6785SChristian S.J. Peron 	int rval, ecode;
1372ea022d16SRodney W. Grimes 	FILE *fd;
1373b071c689SDavid Nugent #ifdef	LOGIN_CAP
1374b071c689SDavid Nugent 	login_cap_t *lc = NULL;
1375b071c689SDavid Nugent #endif
13765bc9d93dSMark Murray #ifdef USE_PAM
13775bc9d93dSMark Murray 	int e;
13785bc9d93dSMark Murray #endif
1379341e476eSYaroslav Tykhiy 	char *residue = NULL;
138047499ecdSAndrey A. Chernov 	char *xpasswd;
1381ea022d16SRodney W. Grimes 
1382ea022d16SRodney W. Grimes 	if (logged_in || askpasswd == 0) {
1383ea022d16SRodney W. Grimes 		reply(503, "Login with USER first.");
1384ea022d16SRodney W. Grimes 		return;
1385ea022d16SRodney W. Grimes 	}
1386ea022d16SRodney W. Grimes 	askpasswd = 0;
1387ea022d16SRodney W. Grimes 	if (!guest) {		/* "ftp" is only account allowed no password */
1388a5a4544eSPaul Traina 		if (pw == NULL) {
1389a5a4544eSPaul Traina 			rval = 1;	/* failure below */
1390a5a4544eSPaul Traina 			goto skip;
1391a5a4544eSPaul Traina 		}
13925bc9d93dSMark Murray #ifdef USE_PAM
13936c9134c0SMark Murray 		rval = auth_pam(&pw, passwd);
1394f650a124SAndrey A. Chernov 		if (rval >= 0) {
1395f650a124SAndrey A. Chernov 			opieunlock();
1396a5a4544eSPaul Traina 			goto skip;
1397f650a124SAndrey A. Chernov 		}
1398f650a124SAndrey A. Chernov #endif
139947499ecdSAndrey A. Chernov 		if (opieverify(&opiedata, passwd) == 0)
140047499ecdSAndrey A. Chernov 			xpasswd = pw->pw_passwd;
1401f650a124SAndrey A. Chernov 		else if (pwok) {
140247499ecdSAndrey A. Chernov 			xpasswd = crypt(passwd, pw->pw_passwd);
1403f650a124SAndrey A. Chernov 			if (passwd[0] == '\0' && pw->pw_passwd[0] != '\0')
1404f650a124SAndrey A. Chernov 				xpasswd = ":";
1405f650a124SAndrey A. Chernov 		} else {
140647499ecdSAndrey A. Chernov 			rval = 1;
140747499ecdSAndrey A. Chernov 			goto skip;
140847499ecdSAndrey A. Chernov 		}
140947499ecdSAndrey A. Chernov 		rval = strcmp(pw->pw_passwd, xpasswd);
1410f650a124SAndrey A. Chernov 		if (pw->pw_expire && time(NULL) >= pw->pw_expire)
1411a5a4544eSPaul Traina 			rval = 1;	/* failure */
1412a5a4544eSPaul Traina skip:
1413a5a4544eSPaul Traina 		/*
1414a5a4544eSPaul Traina 		 * If rval == 1, the user failed the authentication check
14156c9134c0SMark Murray 		 * above.  If rval == 0, either PAM or local authentication
1416a5a4544eSPaul Traina 		 * succeeded.
1417a5a4544eSPaul Traina 		 */
1418a5a4544eSPaul Traina 		if (rval) {
1419ea022d16SRodney W. Grimes 			reply(530, "Login incorrect.");
1420b8939f6fSYaroslav Tykhiy 			if (logging) {
1421ea022d16SRodney W. Grimes 				syslog(LOG_NOTICE,
1422b8939f6fSYaroslav Tykhiy 				    "FTP LOGIN FAILED FROM %s",
1423b8939f6fSYaroslav Tykhiy 				    remotehost);
1424b8939f6fSYaroslav Tykhiy 				syslog(LOG_AUTHPRIV | LOG_NOTICE,
1425ea022d16SRodney W. Grimes 				    "FTP LOGIN FAILED FROM %s, %s",
1426ea022d16SRodney W. Grimes 				    remotehost, curname);
1427b8939f6fSYaroslav Tykhiy 			}
1428ea022d16SRodney W. Grimes 			pw = NULL;
1429ea022d16SRodney W. Grimes 			if (login_attempts++ >= 5) {
1430ea022d16SRodney W. Grimes 				syslog(LOG_NOTICE,
1431ea022d16SRodney W. Grimes 				    "repeated login failures from %s",
1432ea022d16SRodney W. Grimes 				    remotehost);
1433ea022d16SRodney W. Grimes 				exit(0);
1434ea022d16SRodney W. Grimes 			}
1435ea022d16SRodney W. Grimes 			return;
1436ea022d16SRodney W. Grimes 		}
1437ea022d16SRodney W. Grimes 	}
1438ea022d16SRodney W. Grimes 	login_attempts = 0;		/* this time successful */
1439c4536e21SYaroslav Tykhiy 	if (setegid(pw->pw_gid) < 0) {
1440ea022d16SRodney W. Grimes 		reply(550, "Can't set gid.");
1441ea022d16SRodney W. Grimes 		return;
1442ea022d16SRodney W. Grimes 	}
1443b071c689SDavid Nugent 	/* May be overridden by login.conf */
1444b071c689SDavid Nugent 	(void) umask(defumask);
1445b071c689SDavid Nugent #ifdef	LOGIN_CAP
14465d0bfe39SDavid Nugent 	if ((lc = login_getpwclass(pw)) != NULL) {
144704683b2cSYaroslav Tykhiy 		char	remote_ip[NI_MAXHOST];
1448b071c689SDavid Nugent 
144904683b2cSYaroslav Tykhiy 		if (getnameinfo((struct sockaddr *)&his_addr, his_addr.su_len,
14504dd8b5abSYoshinobu Inoue 			remote_ip, sizeof(remote_ip) - 1, NULL, 0,
145104683b2cSYaroslav Tykhiy 			NI_NUMERICHOST))
145204683b2cSYaroslav Tykhiy 				*remote_ip = 0;
1453b071c689SDavid Nugent 		remote_ip[sizeof(remote_ip) - 1] = 0;
1454b071c689SDavid Nugent 		if (!auth_hostok(lc, remotehost, remote_ip)) {
1455b071c689SDavid Nugent 			syslog(LOG_INFO|LOG_AUTH,
1456b071c689SDavid Nugent 			    "FTP LOGIN FAILED (HOST) as %s: permission denied.",
1457b071c689SDavid Nugent 			    pw->pw_name);
14584a3e5acdSYaroslav Tykhiy 			reply(530, "Permission denied.");
1459b071c689SDavid Nugent 			pw = NULL;
1460b071c689SDavid Nugent 			return;
1461b071c689SDavid Nugent 		}
1462b071c689SDavid Nugent 		if (!auth_timeok(lc, time(NULL))) {
14634a3e5acdSYaroslav Tykhiy 			reply(530, "Login not available right now.");
1464b071c689SDavid Nugent 			pw = NULL;
1465b071c689SDavid Nugent 			return;
1466b071c689SDavid Nugent 		}
1467b071c689SDavid Nugent 	}
146852e7ee74SYaroslav Tykhiy 	setusercontext(lc, pw, 0,
1469e6fa0d43SDag-Erling Smørgrav 		LOGIN_SETLOGIN|LOGIN_SETGROUP|LOGIN_SETPRIORITY|
1470d9e2c424SRobert Watson 		LOGIN_SETRESOURCES|LOGIN_SETUMASK|LOGIN_SETMAC);
1471b071c689SDavid Nugent #else
1472e6fa0d43SDag-Erling Smørgrav 	setlogin(pw->pw_name);
1473ea022d16SRodney W. Grimes 	(void) initgroups(pw->pw_name, pw->pw_gid);
1474b071c689SDavid Nugent #endif
1475ea022d16SRodney W. Grimes 
14765bc9d93dSMark Murray #ifdef USE_PAM
14775bc9d93dSMark Murray 	if (pamh) {
14785bc9d93dSMark Murray 		if ((e = pam_open_session(pamh, 0)) != PAM_SUCCESS) {
14795bc9d93dSMark Murray 			syslog(LOG_ERR, "pam_open_session: %s", pam_strerror(pamh, e));
14805bc9d93dSMark Murray 		} else if ((e = pam_setcred(pamh, PAM_ESTABLISH_CRED)) != PAM_SUCCESS) {
14815bc9d93dSMark Murray 			syslog(LOG_ERR, "pam_setcred: %s", pam_strerror(pamh, e));
14825bc9d93dSMark Murray 		}
14835bc9d93dSMark Murray 	}
14845bc9d93dSMark Murray #endif
14855bc9d93dSMark Murray 
148680643af0SEd Schouten 	dochroot =
1487cefb6785SChristian S.J. Peron 		checkuser(_PATH_FTPCHROOT, pw->pw_name, 1, &residue, &ecode)
148880643af0SEd Schouten #ifdef	LOGIN_CAP	/* Allow login.conf configuration as well */
148980643af0SEd Schouten 		|| login_getcapbool(lc, "ftp-chroot", 0)
149080643af0SEd Schouten #endif
149180643af0SEd Schouten 	;
1492cefb6785SChristian S.J. Peron 	/*
1493cefb6785SChristian S.J. Peron 	 * It is possible that checkuser() failed to open the chroot file.
1494cefb6785SChristian S.J. Peron 	 * If this is the case, report that logins are un-available, since we
1495cefb6785SChristian S.J. Peron 	 * have no way of checking whether or not the user should be chrooted.
1496cefb6785SChristian S.J. Peron 	 * We ignore ENOENT since it is not required that this file be present.
1497cefb6785SChristian S.J. Peron 	 */
1498cefb6785SChristian S.J. Peron 	if (ecode != 0 && ecode != ENOENT) {
1499cefb6785SChristian S.J. Peron 		reply(530, "Login not available right now.");
1500cefb6785SChristian S.J. Peron 		return;
1501cefb6785SChristian S.J. Peron 	}
150280643af0SEd Schouten 	chrootdir = NULL;
150380643af0SEd Schouten 
15049f37b1a2SEd Schouten 	/* Disable wtmp logging when chrooting. */
15059f37b1a2SEd Schouten 	if (dochroot || guest)
15069f37b1a2SEd Schouten 		dowtmp = 0;
15079f37b1a2SEd Schouten 	if (dowtmp)
150880643af0SEd Schouten 		ftpd_logwtmp(wtmpid, pw->pw_name,
15095d7e0128SYaroslav Tykhiy 		    (struct sockaddr *)&his_addr);
1510ea022d16SRodney W. Grimes 	logged_in = 1;
1511ea022d16SRodney W. Grimes 
1512a5a4544eSPaul Traina 	if (guest && stats && statfd < 0)
1513ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
151463047c6fSDavid E. O'Brien 		statfd = open(thishost->statfile, O_WRONLY|O_APPEND);
1515ea4e54b9SDavid Nugent #else
151663047c6fSDavid E. O'Brien 		statfd = open(_PATH_FTPDSTATFILE, O_WRONLY|O_APPEND);
1517ea4e54b9SDavid Nugent #endif
151863047c6fSDavid E. O'Brien 		if (statfd < 0)
15193eb568f2SGuido van Rooij 			stats = 0;
15203eb568f2SGuido van Rooij 
1521ea022d16SRodney W. Grimes 	/*
1522ce9287fcSYaroslav Tykhiy 	 * For a chrooted local user,
1523ce9287fcSYaroslav Tykhiy 	 * a) see whether ftpchroot(5) specifies a chroot directory,
1524ce9287fcSYaroslav Tykhiy 	 * b) extract the directory pathname from the line,
1525ce9287fcSYaroslav Tykhiy 	 * c) expand it to the absolute pathname if necessary.
1526ea022d16SRodney W. Grimes 	 */
1527ce9287fcSYaroslav Tykhiy 	if (dochroot && residue &&
152839bce482SYaroslav Tykhiy 	    (chrootdir = strtok(residue, " \t")) != NULL) {
152939bce482SYaroslav Tykhiy 		if (chrootdir[0] != '/')
1530341e476eSYaroslav Tykhiy 			asprintf(&chrootdir, "%s/%s", pw->pw_dir, chrootdir);
153139bce482SYaroslav Tykhiy 		else
1532215a9f9dSYaroslav Tykhiy 			chrootdir = strdup(chrootdir); /* make it permanent */
1533341e476eSYaroslav Tykhiy 		if (chrootdir == NULL)
15348657b576SYaroslav Tykhiy 			fatalerror("Ran out of memory.");
15358657b576SYaroslav Tykhiy 	}
1536ce9287fcSYaroslav Tykhiy 	if (guest || dochroot) {
1537ce9287fcSYaroslav Tykhiy 		/*
1538ce9287fcSYaroslav Tykhiy 		 * If no chroot directory set yet, use the login directory.
1539ce9287fcSYaroslav Tykhiy 		 * Copy it so it can be modified while pw->pw_dir stays intact.
1540ce9287fcSYaroslav Tykhiy 		 */
1541ce9287fcSYaroslav Tykhiy 		if (chrootdir == NULL &&
1542ce9287fcSYaroslav Tykhiy 		    (chrootdir = strdup(pw->pw_dir)) == NULL)
1543ce9287fcSYaroslav Tykhiy 			fatalerror("Ran out of memory.");
1544ce9287fcSYaroslav Tykhiy 		/*
1545ce9287fcSYaroslav Tykhiy 		 * Check for the "/chroot/./home" syntax,
1546ce9287fcSYaroslav Tykhiy 		 * separate the chroot and home directory pathnames.
1547ce9287fcSYaroslav Tykhiy 		 */
1548ce9287fcSYaroslav Tykhiy 		if ((homedir = strstr(chrootdir, "/./")) != NULL) {
1549ce9287fcSYaroslav Tykhiy 			*(homedir++) = '\0';	/* wipe '/' */
1550ce9287fcSYaroslav Tykhiy 			homedir++;		/* skip '.' */
1551ce9287fcSYaroslav Tykhiy 		} else {
1552ce9287fcSYaroslav Tykhiy 			/*
1553ce9287fcSYaroslav Tykhiy 			 * We MUST do a chdir() after the chroot. Otherwise
1554ce9287fcSYaroslav Tykhiy 			 * the old current directory will be accessible as "."
1555ce9287fcSYaroslav Tykhiy 			 * outside the new root!
1556ce9287fcSYaroslav Tykhiy 			 */
1557ce9287fcSYaroslav Tykhiy 			homedir = "/";
1558ce9287fcSYaroslav Tykhiy 		}
1559ce9287fcSYaroslav Tykhiy 		/*
1560ce9287fcSYaroslav Tykhiy 		 * Finally, do chroot()
1561ce9287fcSYaroslav Tykhiy 		 */
1562ce9287fcSYaroslav Tykhiy 		if (chroot(chrootdir) < 0) {
1563a5a4544eSPaul Traina 			reply(550, "Can't change root.");
1564a5a4544eSPaul Traina 			goto bad;
1565a5a4544eSPaul Traina 		}
1566ce9287fcSYaroslav Tykhiy 	} else	/* real user w/o chroot */
1567ce9287fcSYaroslav Tykhiy 		homedir = pw->pw_dir;
1568ce9287fcSYaroslav Tykhiy 	/*
1569ce9287fcSYaroslav Tykhiy 	 * Set euid *before* doing chdir() so
1570ce9287fcSYaroslav Tykhiy 	 * a) the user won't be carried to a directory that he couldn't reach
1571ce9287fcSYaroslav Tykhiy 	 *    on his own due to no permission to upper path components,
1572ce9287fcSYaroslav Tykhiy 	 * b) NFS mounted homedirs w/restrictive permissions will be accessible
1573ce9287fcSYaroslav Tykhiy 	 *    (uid 0 has no root power over NFS if not mapped explicitly.)
1574ce9287fcSYaroslav Tykhiy 	 */
157552e7ee74SYaroslav Tykhiy 	if (seteuid(pw->pw_uid) < 0) {
1576ea022d16SRodney W. Grimes 		reply(550, "Can't set uid.");
1577ea022d16SRodney W. Grimes 		goto bad;
1578ea022d16SRodney W. Grimes 	}
1579ce9287fcSYaroslav Tykhiy 	if (chdir(homedir) < 0) {
1580ce9287fcSYaroslav Tykhiy 		if (guest || dochroot) {
1581ce9287fcSYaroslav Tykhiy 			reply(550, "Can't change to base directory.");
1582ce9287fcSYaroslav Tykhiy 			goto bad;
1583ce9287fcSYaroslav Tykhiy 		} else {
1584ce9287fcSYaroslav Tykhiy 			if (chdir("/") < 0) {
1585ce9287fcSYaroslav Tykhiy 				reply(550, "Root is inaccessible.");
1586ce9287fcSYaroslav Tykhiy 				goto bad;
1587ce9287fcSYaroslav Tykhiy 			}
158802c97492SYaroslav Tykhiy 			lreply(230, "No directory! Logging in with home=/.");
1589ce9287fcSYaroslav Tykhiy 		}
1590ce9287fcSYaroslav Tykhiy 	}
159182c76939SDavid Greenman 
159282c76939SDavid Greenman 	/*
1593ea022d16SRodney W. Grimes 	 * Display a login message, if it exists.
1594ea022d16SRodney W. Grimes 	 * N.B. reply(230,) must follow the message.
1595ea022d16SRodney W. Grimes 	 */
1596ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
159763047c6fSDavid E. O'Brien 	fd = fopen(thishost->loginmsg, "r");
1598ea4e54b9SDavid Nugent #else
159963047c6fSDavid E. O'Brien 	fd = fopen(_PATH_FTPLOGINMESG, "r");
1600ea4e54b9SDavid Nugent #endif
160163047c6fSDavid E. O'Brien 	if (fd != NULL) {
1602ea022d16SRodney W. Grimes 		char *cp, line[LINE_MAX];
1603ea022d16SRodney W. Grimes 
1604ea022d16SRodney W. Grimes 		while (fgets(line, sizeof(line), fd) != NULL) {
1605ea022d16SRodney W. Grimes 			if ((cp = strchr(line, '\n')) != NULL)
1606ea022d16SRodney W. Grimes 				*cp = '\0';
1607ea022d16SRodney W. Grimes 			lreply(230, "%s", line);
1608ea022d16SRodney W. Grimes 		}
1609ea022d16SRodney W. Grimes 		(void) fflush(stdout);
1610ea022d16SRodney W. Grimes 		(void) fclose(fd);
1611ea022d16SRodney W. Grimes 	}
1612ea022d16SRodney W. Grimes 	if (guest) {
16133eb568f2SGuido van Rooij 		if (ident != NULL)
16143eb568f2SGuido van Rooij 			free(ident);
161561f891a6SPaul Traina 		ident = strdup(passwd);
161661f891a6SPaul Traina 		if (ident == NULL)
1617618b0bbaSMark Murray 			fatalerror("Ran out of memory.");
161861f891a6SPaul Traina 
1619ea022d16SRodney W. Grimes 		reply(230, "Guest login ok, access restrictions apply.");
1620ea022d16SRodney W. Grimes #ifdef SETPROCTITLE
1621ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
1622ea4e54b9SDavid Nugent 		if (thishost != firsthost)
1623ea4e54b9SDavid Nugent 			snprintf(proctitle, sizeof(proctitle),
1624b3a0a7cdSMike Heffner 				 "%s: anonymous(%s)/%s", remotehost, hostname,
1625b3a0a7cdSMike Heffner 				 passwd);
1626ea4e54b9SDavid Nugent 		else
1627ea4e54b9SDavid Nugent #endif
1628ea022d16SRodney W. Grimes 			snprintf(proctitle, sizeof(proctitle),
1629b3a0a7cdSMike Heffner 				 "%s: anonymous/%s", remotehost, passwd);
1630b63e1fe2SPeter Wemm 		setproctitle("%s", proctitle);
1631ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */
1632ea022d16SRodney W. Grimes 		if (logging)
1633ea022d16SRodney W. Grimes 			syslog(LOG_INFO, "ANONYMOUS FTP LOGIN FROM %s, %s",
1634ea022d16SRodney W. Grimes 			    remotehost, passwd);
1635ea022d16SRodney W. Grimes 	} else {
16363401a71fSDaniel O'Callaghan 		if (dochroot)
163711342ab1SYaroslav Tykhiy 			reply(230, "User %s logged in, "
163811342ab1SYaroslav Tykhiy 				   "access restrictions apply.", pw->pw_name);
16393401a71fSDaniel O'Callaghan 		else
1640ea022d16SRodney W. Grimes 			reply(230, "User %s logged in.", pw->pw_name);
16413401a71fSDaniel O'Callaghan 
1642ea022d16SRodney W. Grimes #ifdef SETPROCTITLE
1643ea022d16SRodney W. Grimes 		snprintf(proctitle, sizeof(proctitle),
16447a29d7daSYaroslav Tykhiy 			 "%s: user/%s", remotehost, pw->pw_name);
1645b63e1fe2SPeter Wemm 		setproctitle("%s", proctitle);
1646ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */
1647ea022d16SRodney W. Grimes 		if (logging)
1648ea022d16SRodney W. Grimes 			syslog(LOG_INFO, "FTP LOGIN FROM %s as %s",
1649ea022d16SRodney W. Grimes 			    remotehost, pw->pw_name);
1650ea022d16SRodney W. Grimes 	}
1651220223fdSYaroslav Tykhiy 	if (logging && (guest || dochroot))
1652e897216fSYaroslav Tykhiy 		syslog(LOG_INFO, "session root changed to %s", chrootdir);
1653b071c689SDavid Nugent #ifdef	LOGIN_CAP
1654b071c689SDavid Nugent 	login_close(lc);
1655b071c689SDavid Nugent #endif
1656ce9287fcSYaroslav Tykhiy 	if (residue)
1657ce9287fcSYaroslav Tykhiy 		free(residue);
1658ea022d16SRodney W. Grimes 	return;
1659ea022d16SRodney W. Grimes bad:
1660ea022d16SRodney W. Grimes 	/* Forget all about it... */
1661b071c689SDavid Nugent #ifdef	LOGIN_CAP
1662b071c689SDavid Nugent 	login_close(lc);
1663b071c689SDavid Nugent #endif
1664ce9287fcSYaroslav Tykhiy 	if (residue)
1665ce9287fcSYaroslav Tykhiy 		free(residue);
1666ea022d16SRodney W. Grimes 	end_login();
1667ea022d16SRodney W. Grimes }
1668ea022d16SRodney W. Grimes 
1669ea022d16SRodney W. Grimes void
1670e4bc453cSWarner Losh retrieve(char *cmd, char *name)
1671ea022d16SRodney W. Grimes {
1672ea022d16SRodney W. Grimes 	FILE *fin, *dout;
1673ea022d16SRodney W. Grimes 	struct stat st;
1674e4bc453cSWarner Losh 	int (*closefunc)(FILE *);
1675158a00b2SJohn Birrell 	time_t start;
1676ea022d16SRodney W. Grimes 
1677ea022d16SRodney W. Grimes 	if (cmd == 0) {
1678ea022d16SRodney W. Grimes 		fin = fopen(name, "r"), closefunc = fclose;
1679ea022d16SRodney W. Grimes 		st.st_size = 0;
1680ea022d16SRodney W. Grimes 	} else {
1681ea022d16SRodney W. Grimes 		char line[BUFSIZ];
1682ea022d16SRodney W. Grimes 
1683e760ef2cSWarner Losh 		(void) snprintf(line, sizeof(line), cmd, name), name = line;
1684ea022d16SRodney W. Grimes 		fin = ftpd_popen(line, "r"), closefunc = ftpd_pclose;
1685ea022d16SRodney W. Grimes 		st.st_size = -1;
1686ea022d16SRodney W. Grimes 		st.st_blksize = BUFSIZ;
1687ea022d16SRodney W. Grimes 	}
1688ea022d16SRodney W. Grimes 	if (fin == NULL) {
1689ea022d16SRodney W. Grimes 		if (errno != 0) {
1690ea022d16SRodney W. Grimes 			perror_reply(550, name);
1691ea022d16SRodney W. Grimes 			if (cmd == 0) {
1692ea022d16SRodney W. Grimes 				LOGCMD("get", name);
1693ea022d16SRodney W. Grimes 			}
1694ea022d16SRodney W. Grimes 		}
1695ea022d16SRodney W. Grimes 		return;
1696ea022d16SRodney W. Grimes 	}
1697ea022d16SRodney W. Grimes 	byte_count = -1;
1698ea701226SYaroslav Tykhiy 	if (cmd == 0) {
1699ea701226SYaroslav Tykhiy 		if (fstat(fileno(fin), &st) < 0) {
1700ea701226SYaroslav Tykhiy 			perror_reply(550, name);
1701ea701226SYaroslav Tykhiy 			goto done;
1702ea701226SYaroslav Tykhiy 		}
1703ea701226SYaroslav Tykhiy 		if (!S_ISREG(st.st_mode)) {
170410e89104SYaroslav Tykhiy 			/*
170510e89104SYaroslav Tykhiy 			 * Never sending a raw directory is a workaround
170610e89104SYaroslav Tykhiy 			 * for buggy clients that will attempt to RETR
170710e89104SYaroslav Tykhiy 			 * a directory before listing it, e.g., Mozilla.
170810e89104SYaroslav Tykhiy 			 * Preventing a guest from getting irregular files
170910e89104SYaroslav Tykhiy 			 * is a simple security measure.
171010e89104SYaroslav Tykhiy 			 */
171110e89104SYaroslav Tykhiy 			if (S_ISDIR(st.st_mode) || guest) {
1712ea022d16SRodney W. Grimes 				reply(550, "%s: not a plain file.", name);
1713ea022d16SRodney W. Grimes 				goto done;
1714ea022d16SRodney W. Grimes 			}
1715ea701226SYaroslav Tykhiy 			st.st_size = -1;
1716ea701226SYaroslav Tykhiy 			/* st.st_blksize is set for all descriptor types */
1717ea701226SYaroslav Tykhiy 		}
1718ea701226SYaroslav Tykhiy 	}
1719ea022d16SRodney W. Grimes 	if (restart_point) {
1720ea022d16SRodney W. Grimes 		if (type == TYPE_A) {
1721ea022d16SRodney W. Grimes 			off_t i, n;
1722ea022d16SRodney W. Grimes 			int c;
1723ea022d16SRodney W. Grimes 
1724ea022d16SRodney W. Grimes 			n = restart_point;
1725ea022d16SRodney W. Grimes 			i = 0;
1726ea022d16SRodney W. Grimes 			while (i++ < n) {
1727ea022d16SRodney W. Grimes 				if ((c=getc(fin)) == EOF) {
1728ea022d16SRodney W. Grimes 					perror_reply(550, name);
1729ea022d16SRodney W. Grimes 					goto done;
1730ea022d16SRodney W. Grimes 				}
1731ea022d16SRodney W. Grimes 				if (c == '\n')
1732ea022d16SRodney W. Grimes 					i++;
1733ea022d16SRodney W. Grimes 			}
1734ea022d16SRodney W. Grimes 		} else if (lseek(fileno(fin), restart_point, L_SET) < 0) {
1735ea022d16SRodney W. Grimes 			perror_reply(550, name);
1736ea022d16SRodney W. Grimes 			goto done;
1737ea022d16SRodney W. Grimes 		}
1738ea022d16SRodney W. Grimes 	}
1739ea022d16SRodney W. Grimes 	dout = dataconn(name, st.st_size, "w");
1740ea022d16SRodney W. Grimes 	if (dout == NULL)
1741ea022d16SRodney W. Grimes 		goto done;
17423eb568f2SGuido van Rooij 	time(&start);
17439fc5823aSGarrett Wollman 	send_data(fin, dout, st.st_blksize, st.st_size,
17449fc5823aSGarrett Wollman 		  restart_point == 0 && cmd == 0 && S_ISREG(st.st_mode));
1745c999732bSYaroslav Tykhiy 	if (cmd == 0 && guest && stats && byte_count > 0)
1746c999732bSYaroslav Tykhiy 		logxfer(name, byte_count, start);
1747ea022d16SRodney W. Grimes 	(void) fclose(dout);
1748ea022d16SRodney W. Grimes 	data = -1;
1749ea022d16SRodney W. Grimes 	pdata = -1;
1750ea022d16SRodney W. Grimes done:
1751ea022d16SRodney W. Grimes 	if (cmd == 0)
1752ea022d16SRodney W. Grimes 		LOGBYTES("get", name, byte_count);
1753ea022d16SRodney W. Grimes 	(*closefunc)(fin);
1754ea022d16SRodney W. Grimes }
1755ea022d16SRodney W. Grimes 
1756ea022d16SRodney W. Grimes void
1757e4bc453cSWarner Losh store(char *name, char *mode, int unique)
1758ea022d16SRodney W. Grimes {
1759a117c345SYaroslav Tykhiy 	int fd;
1760ea022d16SRodney W. Grimes 	FILE *fout, *din;
1761e4bc453cSWarner Losh 	int (*closefunc)(FILE *);
1762ea022d16SRodney W. Grimes 
1763a117c345SYaroslav Tykhiy 	if (*mode == 'a') {		/* APPE */
1764a117c345SYaroslav Tykhiy 		if (unique) {
1765a117c345SYaroslav Tykhiy 			/* Programming error */
1766a117c345SYaroslav Tykhiy 			syslog(LOG_ERR, "Internal: unique flag to APPE");
1767a117c345SYaroslav Tykhiy 			unique = 0;
1768a117c345SYaroslav Tykhiy 		}
1769a117c345SYaroslav Tykhiy 		if (guest && noguestmod) {
177002c97492SYaroslav Tykhiy 			reply(550, "Appending to existing file denied.");
1771a117c345SYaroslav Tykhiy 			goto err;
1772a117c345SYaroslav Tykhiy 		}
1773a117c345SYaroslav Tykhiy 		restart_point = 0;	/* not affected by preceding REST */
1774a117c345SYaroslav Tykhiy 	}
1775a117c345SYaroslav Tykhiy 	if (unique)			/* STOU overrides REST */
1776a117c345SYaroslav Tykhiy 		restart_point = 0;
1777a117c345SYaroslav Tykhiy 	if (guest && noguestmod) {
1778a117c345SYaroslav Tykhiy 		if (restart_point) {	/* guest STOR w/REST */
177902c97492SYaroslav Tykhiy 			reply(550, "Modifying existing file denied.");
1780a117c345SYaroslav Tykhiy 			goto err;
1781a117c345SYaroslav Tykhiy 		} else			/* treat guest STOR as STOU */
1782a117c345SYaroslav Tykhiy 			unique = 1;
1783ea022d16SRodney W. Grimes 	}
1784ea022d16SRodney W. Grimes 
1785ea022d16SRodney W. Grimes 	if (restart_point)
1786a117c345SYaroslav Tykhiy 		mode = "r+";	/* so ASCII manual seek can work */
1787a117c345SYaroslav Tykhiy 	if (unique) {
1788a117c345SYaroslav Tykhiy 		if ((fd = guniquefd(name, &name)) < 0)
1789a117c345SYaroslav Tykhiy 			goto err;
1790a117c345SYaroslav Tykhiy 		fout = fdopen(fd, mode);
1791a117c345SYaroslav Tykhiy 	} else
1792ea022d16SRodney W. Grimes 		fout = fopen(name, mode);
1793ea022d16SRodney W. Grimes 	closefunc = fclose;
1794ea022d16SRodney W. Grimes 	if (fout == NULL) {
1795ea022d16SRodney W. Grimes 		perror_reply(553, name);
1796a117c345SYaroslav Tykhiy 		goto err;
1797ea022d16SRodney W. Grimes 	}
1798ea022d16SRodney W. Grimes 	byte_count = -1;
1799ea022d16SRodney W. Grimes 	if (restart_point) {
1800ea022d16SRodney W. Grimes 		if (type == TYPE_A) {
1801ea022d16SRodney W. Grimes 			off_t i, n;
1802ea022d16SRodney W. Grimes 			int c;
1803ea022d16SRodney W. Grimes 
1804ea022d16SRodney W. Grimes 			n = restart_point;
1805ea022d16SRodney W. Grimes 			i = 0;
1806ea022d16SRodney W. Grimes 			while (i++ < n) {
1807ea022d16SRodney W. Grimes 				if ((c=getc(fout)) == EOF) {
1808ea022d16SRodney W. Grimes 					perror_reply(550, name);
1809ea022d16SRodney W. Grimes 					goto done;
1810ea022d16SRodney W. Grimes 				}
1811ea022d16SRodney W. Grimes 				if (c == '\n')
1812ea022d16SRodney W. Grimes 					i++;
1813ea022d16SRodney W. Grimes 			}
1814ea022d16SRodney W. Grimes 			/*
1815ea022d16SRodney W. Grimes 			 * We must do this seek to "current" position
1816ea022d16SRodney W. Grimes 			 * because we are changing from reading to
1817ea022d16SRodney W. Grimes 			 * writing.
1818ea022d16SRodney W. Grimes 			 */
18190e519c96SYaroslav Tykhiy 			if (fseeko(fout, 0, SEEK_CUR) < 0) {
1820ea022d16SRodney W. Grimes 				perror_reply(550, name);
1821ea022d16SRodney W. Grimes 				goto done;
1822ea022d16SRodney W. Grimes 			}
1823ea022d16SRodney W. Grimes 		} else if (lseek(fileno(fout), restart_point, L_SET) < 0) {
1824ea022d16SRodney W. Grimes 			perror_reply(550, name);
1825ea022d16SRodney W. Grimes 			goto done;
1826ea022d16SRodney W. Grimes 		}
1827ea022d16SRodney W. Grimes 	}
18280e519c96SYaroslav Tykhiy 	din = dataconn(name, -1, "r");
1829ea022d16SRodney W. Grimes 	if (din == NULL)
1830ea022d16SRodney W. Grimes 		goto done;
1831ea022d16SRodney W. Grimes 	if (receive_data(din, fout) == 0) {
1832ea022d16SRodney W. Grimes 		if (unique)
1833ea022d16SRodney W. Grimes 			reply(226, "Transfer complete (unique file name:%s).",
1834ea022d16SRodney W. Grimes 			    name);
1835ea022d16SRodney W. Grimes 		else
1836ea022d16SRodney W. Grimes 			reply(226, "Transfer complete.");
1837ea022d16SRodney W. Grimes 	}
1838ea022d16SRodney W. Grimes 	(void) fclose(din);
1839ea022d16SRodney W. Grimes 	data = -1;
1840ea022d16SRodney W. Grimes 	pdata = -1;
1841ea022d16SRodney W. Grimes done:
18427c20f337SYaroslav Tykhiy 	LOGBYTES(*mode == 'a' ? "append" : "put", name, byte_count);
1843ea022d16SRodney W. Grimes 	(*closefunc)(fout);
1844a117c345SYaroslav Tykhiy 	return;
1845a117c345SYaroslav Tykhiy err:
1846a117c345SYaroslav Tykhiy 	LOGCMD(*mode == 'a' ? "append" : "put" , name);
1847a117c345SYaroslav Tykhiy 	return;
1848ea022d16SRodney W. Grimes }
1849ea022d16SRodney W. Grimes 
1850ea022d16SRodney W. Grimes static FILE *
1851e4bc453cSWarner Losh getdatasock(char *mode)
1852ea022d16SRodney W. Grimes {
1853ea022d16SRodney W. Grimes 	int on = 1, s, t, tries;
1854ea022d16SRodney W. Grimes 
1855ea022d16SRodney W. Grimes 	if (data >= 0)
1856ea022d16SRodney W. Grimes 		return (fdopen(data, mode));
18574dd8b5abSYoshinobu Inoue 
18584dd8b5abSYoshinobu Inoue 	s = socket(data_dest.su_family, SOCK_STREAM, 0);
1859ea022d16SRodney W. Grimes 	if (s < 0)
1860ea022d16SRodney W. Grimes 		goto bad;
186157d4ef07SYaroslav Tykhiy 	if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
1862406d1ae9SYaroslav Tykhiy 		syslog(LOG_WARNING, "data setsockopt (SO_REUSEADDR): %m");
1863ea022d16SRodney W. Grimes 	/* anchor socket to avoid multi-homing problems */
18644dd8b5abSYoshinobu Inoue 	data_source = ctrl_addr;
186563591ba5SYaroslav Tykhiy 	data_source.su_port = htons(dataport);
186652e7ee74SYaroslav Tykhiy 	(void) seteuid(0);
1867ea022d16SRodney W. Grimes 	for (tries = 1; ; tries++) {
18689ec7612aSYaroslav Tykhiy 		/*
18699ec7612aSYaroslav Tykhiy 		 * We should loop here since it's possible that
18709ec7612aSYaroslav Tykhiy 		 * another ftpd instance has passed this point and is
18719ec7612aSYaroslav Tykhiy 		 * trying to open a data connection in active mode now.
18729ec7612aSYaroslav Tykhiy 		 * Until the other connection is opened, we'll be getting
18739ec7612aSYaroslav Tykhiy 		 * EADDRINUSE because no SOCK_STREAM sockets in the system
18749ec7612aSYaroslav Tykhiy 		 * can share both local and remote addresses, localIP:20
18759ec7612aSYaroslav Tykhiy 		 * and *:* in this case.
18769ec7612aSYaroslav Tykhiy 		 */
1877ea022d16SRodney W. Grimes 		if (bind(s, (struct sockaddr *)&data_source,
18784dd8b5abSYoshinobu Inoue 		    data_source.su_len) >= 0)
1879ea022d16SRodney W. Grimes 			break;
1880ea022d16SRodney W. Grimes 		if (errno != EADDRINUSE || tries > 10)
1881ea022d16SRodney W. Grimes 			goto bad;
1882ea022d16SRodney W. Grimes 		sleep(tries);
1883ea022d16SRodney W. Grimes 	}
188452e7ee74SYaroslav Tykhiy 	(void) seteuid(pw->pw_uid);
1885ea022d16SRodney W. Grimes #ifdef IP_TOS
18864dd8b5abSYoshinobu Inoue 	if (data_source.su_family == AF_INET)
18874dd8b5abSYoshinobu Inoue       {
1888ea022d16SRodney W. Grimes 	on = IPTOS_THROUGHPUT;
188957d4ef07SYaroslav Tykhiy 	if (setsockopt(s, IPPROTO_IP, IP_TOS, &on, sizeof(int)) < 0)
1890406d1ae9SYaroslav Tykhiy 		syslog(LOG_WARNING, "data setsockopt (IP_TOS): %m");
18914dd8b5abSYoshinobu Inoue       }
1892ea022d16SRodney W. Grimes #endif
18939fc5823aSGarrett Wollman #ifdef TCP_NOPUSH
18949fc5823aSGarrett Wollman 	/*
18959fc5823aSGarrett Wollman 	 * Turn off push flag to keep sender TCP from sending short packets
189632072720SYaroslav Tykhiy 	 * at the boundaries of each write().
18979fc5823aSGarrett Wollman 	 */
18989fc5823aSGarrett Wollman 	on = 1;
189957d4ef07SYaroslav Tykhiy 	if (setsockopt(s, IPPROTO_TCP, TCP_NOPUSH, &on, sizeof on) < 0)
1900406d1ae9SYaroslav Tykhiy 		syslog(LOG_WARNING, "data setsockopt (TCP_NOPUSH): %m");
19019fc5823aSGarrett Wollman #endif
1902ea022d16SRodney W. Grimes 	return (fdopen(s, mode));
1903ea022d16SRodney W. Grimes bad:
1904ea022d16SRodney W. Grimes 	/* Return the real value of errno (close may change it) */
1905ea022d16SRodney W. Grimes 	t = errno;
190652e7ee74SYaroslav Tykhiy 	(void) seteuid(pw->pw_uid);
1907ea022d16SRodney W. Grimes 	(void) close(s);
1908ea022d16SRodney W. Grimes 	errno = t;
1909ea022d16SRodney W. Grimes 	return (NULL);
1910ea022d16SRodney W. Grimes }
1911ea022d16SRodney W. Grimes 
1912ea022d16SRodney W. Grimes static FILE *
1913e4bc453cSWarner Losh dataconn(char *name, off_t size, char *mode)
1914ea022d16SRodney W. Grimes {
1915ea022d16SRodney W. Grimes 	char sizebuf[32];
1916ea022d16SRodney W. Grimes 	FILE *file;
1917e5094456SCrist J. Clark 	int retry = 0, tos, conerrno;
1918ea022d16SRodney W. Grimes 
1919ea022d16SRodney W. Grimes 	file_size = size;
1920ea022d16SRodney W. Grimes 	byte_count = 0;
19210e519c96SYaroslav Tykhiy 	if (size != -1)
1922a57e1ef0SYaroslav Tykhiy 		(void) snprintf(sizebuf, sizeof(sizebuf),
1923a57e1ef0SYaroslav Tykhiy 				" (%jd bytes)", (intmax_t)size);
1924ea022d16SRodney W. Grimes 	else
1925e760ef2cSWarner Losh 		*sizebuf = '\0';
1926ea022d16SRodney W. Grimes 	if (pdata >= 0) {
19274dd8b5abSYoshinobu Inoue 		union sockunion from;
192878e3eed0SStefan Farfeleder 		socklen_t fromlen = ctrl_addr.su_len;
192978e3eed0SStefan Farfeleder 		int flags, s;
1930d6ed3c37SGuido van Rooij 		struct timeval timeout;
1931d6ed3c37SGuido van Rooij 		fd_set set;
1932ea022d16SRodney W. Grimes 
1933d6ed3c37SGuido van Rooij 		FD_ZERO(&set);
1934d6ed3c37SGuido van Rooij 		FD_SET(pdata, &set);
1935d6ed3c37SGuido van Rooij 
1936d6ed3c37SGuido van Rooij 		timeout.tv_usec = 0;
1937d6ed3c37SGuido van Rooij 		timeout.tv_sec = 120;
1938d6ed3c37SGuido van Rooij 
19394cd48bacSYaroslav Tykhiy 		/*
19404cd48bacSYaroslav Tykhiy 		 * Granted a socket is in the blocking I/O mode,
19414cd48bacSYaroslav Tykhiy 		 * accept() will block after a successful select()
19424cd48bacSYaroslav Tykhiy 		 * if the selected connection dies in between.
19434cd48bacSYaroslav Tykhiy 		 * Therefore set the non-blocking I/O flag here.
19444cd48bacSYaroslav Tykhiy 		 */
19454cd48bacSYaroslav Tykhiy 		if ((flags = fcntl(pdata, F_GETFL, 0)) == -1 ||
19464cd48bacSYaroslav Tykhiy 		    fcntl(pdata, F_SETFL, flags | O_NONBLOCK) == -1)
19474cd48bacSYaroslav Tykhiy 			goto pdata_err;
1948aa5a9d3fSYaroslav Tykhiy 		if (select(pdata+1, &set, NULL, NULL, &timeout) <= 0 ||
19494cd48bacSYaroslav Tykhiy 		    (s = accept(pdata, (struct sockaddr *) &from, &fromlen)) < 0)
19504cd48bacSYaroslav Tykhiy 			goto pdata_err;
1951ea022d16SRodney W. Grimes 		(void) close(pdata);
1952ea022d16SRodney W. Grimes 		pdata = s;
19534cd48bacSYaroslav Tykhiy 		/*
1954f6daca0dSYaroslav Tykhiy 		 * Unset the inherited non-blocking I/O flag
1955f6daca0dSYaroslav Tykhiy 		 * on the child socket so stdio can work on it.
19564cd48bacSYaroslav Tykhiy 		 */
19574cd48bacSYaroslav Tykhiy 		if ((flags = fcntl(pdata, F_GETFL, 0)) == -1 ||
19584cd48bacSYaroslav Tykhiy 		    fcntl(pdata, F_SETFL, flags & ~O_NONBLOCK) == -1)
19594cd48bacSYaroslav Tykhiy 			goto pdata_err;
1960ea022d16SRodney W. Grimes #ifdef IP_TOS
19614dd8b5abSYoshinobu Inoue 		if (from.su_family == AF_INET)
19624dd8b5abSYoshinobu Inoue 	      {
1963a5a4544eSPaul Traina 		tos = IPTOS_THROUGHPUT;
196457d4ef07SYaroslav Tykhiy 		if (setsockopt(s, IPPROTO_IP, IP_TOS, &tos, sizeof(int)) < 0)
1965406d1ae9SYaroslav Tykhiy 			syslog(LOG_WARNING, "pdata setsockopt (IP_TOS): %m");
19664dd8b5abSYoshinobu Inoue 	      }
1967ea022d16SRodney W. Grimes #endif
1968ea022d16SRodney W. Grimes 		reply(150, "Opening %s mode data connection for '%s'%s.",
1969ea022d16SRodney W. Grimes 		     type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf);
1970ea022d16SRodney W. Grimes 		return (fdopen(pdata, mode));
19714cd48bacSYaroslav Tykhiy pdata_err:
19724cd48bacSYaroslav Tykhiy 		reply(425, "Can't open data connection.");
19734cd48bacSYaroslav Tykhiy 		(void) close(pdata);
19744cd48bacSYaroslav Tykhiy 		pdata = -1;
19754cd48bacSYaroslav Tykhiy 		return (NULL);
1976ea022d16SRodney W. Grimes 	}
1977ea022d16SRodney W. Grimes 	if (data >= 0) {
1978ea022d16SRodney W. Grimes 		reply(125, "Using existing data connection for '%s'%s.",
1979ea022d16SRodney W. Grimes 		    name, sizebuf);
1980ea022d16SRodney W. Grimes 		usedefault = 1;
1981ea022d16SRodney W. Grimes 		return (fdopen(data, mode));
1982ea022d16SRodney W. Grimes 	}
1983ea022d16SRodney W. Grimes 	if (usedefault)
1984ea022d16SRodney W. Grimes 		data_dest = his_addr;
1985ea022d16SRodney W. Grimes 	usedefault = 1;
1986e5094456SCrist J. Clark 	do {
1987ea022d16SRodney W. Grimes 		file = getdatasock(mode);
1988ea022d16SRodney W. Grimes 		if (file == NULL) {
198904683b2cSYaroslav Tykhiy 			char hostbuf[NI_MAXHOST], portbuf[NI_MAXSERV];
199004683b2cSYaroslav Tykhiy 
199104683b2cSYaroslav Tykhiy 			if (getnameinfo((struct sockaddr *)&data_source,
199204683b2cSYaroslav Tykhiy 				data_source.su_len,
199304683b2cSYaroslav Tykhiy 				hostbuf, sizeof(hostbuf) - 1,
199404683b2cSYaroslav Tykhiy 				portbuf, sizeof(portbuf) - 1,
199504683b2cSYaroslav Tykhiy 				NI_NUMERICHOST|NI_NUMERICSERV))
199604683b2cSYaroslav Tykhiy 					*hostbuf = *portbuf = 0;
199704683b2cSYaroslav Tykhiy 			hostbuf[sizeof(hostbuf) - 1] = 0;
199804683b2cSYaroslav Tykhiy 			portbuf[sizeof(portbuf) - 1] = 0;
19994dd8b5abSYoshinobu Inoue 			reply(425, "Can't create data socket (%s,%s): %s.",
20004dd8b5abSYoshinobu Inoue 				hostbuf, portbuf, strerror(errno));
2001ea022d16SRodney W. Grimes 			return (NULL);
2002ea022d16SRodney W. Grimes 		}
2003ea022d16SRodney W. Grimes 		data = fileno(file);
2004e5094456SCrist J. Clark 		conerrno = 0;
2005e5094456SCrist J. Clark 		if (connect(data, (struct sockaddr *)&data_dest,
2006e5094456SCrist J. Clark 		    data_dest.su_len) == 0)
2007e5094456SCrist J. Clark 			break;
2008e5094456SCrist J. Clark 		conerrno = errno;
2009ea022d16SRodney W. Grimes 		(void) fclose(file);
2010ea022d16SRodney W. Grimes 		data = -1;
2011e5094456SCrist J. Clark 		if (conerrno == EADDRINUSE) {
2012e3765043SYaroslav Tykhiy 			sleep(swaitint);
2013e5094456SCrist J. Clark 			retry += swaitint;
2014e5094456SCrist J. Clark 		} else {
2015e5094456SCrist J. Clark 			break;
2016e5094456SCrist J. Clark 		}
2017e5094456SCrist J. Clark 	} while (retry <= swaitmax);
2018e5094456SCrist J. Clark 	if (conerrno != 0) {
201982c03024SYaroslav Tykhiy 		reply(425, "Can't build data connection: %s.",
202082c03024SYaroslav Tykhiy 			   strerror(conerrno));
2021ea022d16SRodney W. Grimes 		return (NULL);
2022ea022d16SRodney W. Grimes 	}
2023ea022d16SRodney W. Grimes 	reply(150, "Opening %s mode data connection for '%s'%s.",
2024ea022d16SRodney W. Grimes 	     type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf);
2025ea022d16SRodney W. Grimes 	return (file);
2026ea022d16SRodney W. Grimes }
2027ea022d16SRodney W. Grimes 
2028ea022d16SRodney W. Grimes /*
20294cd51076SYaroslav Tykhiy  * A helper macro to avoid code duplication
20304cd51076SYaroslav Tykhiy  * in send_data() and receive_data().
20314cd51076SYaroslav Tykhiy  *
20324cd51076SYaroslav Tykhiy  * XXX We have to block SIGURG during putc() because BSD stdio
20334cd51076SYaroslav Tykhiy  * is unable to restart interrupted write operations and hence
20344cd51076SYaroslav Tykhiy  * the entire buffer contents will be lost as soon as a write()
20354cd51076SYaroslav Tykhiy  * call indicates EINTR to stdio.
20364cd51076SYaroslav Tykhiy  */
20374cd51076SYaroslav Tykhiy #define FTPD_PUTC(ch, file, label)					\
20384cd51076SYaroslav Tykhiy 	do {								\
20394cd51076SYaroslav Tykhiy 		int ret;						\
20404cd51076SYaroslav Tykhiy 									\
20414cd51076SYaroslav Tykhiy 		do {							\
20424cd51076SYaroslav Tykhiy 			START_UNSAFE;					\
20434cd51076SYaroslav Tykhiy 			ret = putc((ch), (file));			\
20444cd51076SYaroslav Tykhiy 			END_UNSAFE;					\
20454cd51076SYaroslav Tykhiy 			CHECKOOB(return (-1))				\
20464cd51076SYaroslav Tykhiy 			else if (ferror(file))				\
20474cd51076SYaroslav Tykhiy 				goto label;				\
20484cd51076SYaroslav Tykhiy 			clearerr(file);					\
20494cd51076SYaroslav Tykhiy 		} while (ret == EOF);					\
20504cd51076SYaroslav Tykhiy 	} while (0)
20514cd51076SYaroslav Tykhiy 
20524cd51076SYaroslav Tykhiy /*
2053ea022d16SRodney W. Grimes  * Tranfer the contents of "instr" to "outstr" peer using the appropriate
20549fc5823aSGarrett Wollman  * encapsulation of the data subject to Mode, Structure, and Type.
2055ea022d16SRodney W. Grimes  *
2056ea022d16SRodney W. Grimes  * NB: Form isn't handled.
2057ea022d16SRodney W. Grimes  */
20584b82fc95SYaroslav Tykhiy static int
20596e4b0a55SYaroslav Tykhiy send_data(FILE *instr, FILE *outstr, size_t blksize, off_t filesize, int isreg)
2060ea022d16SRodney W. Grimes {
2061db1c2da3SYaroslav Tykhiy 	int c, cp, filefd, netfd;
2062f6f0c4b9SDan Moschuk 	char *buf;
2063ea022d16SRodney W. Grimes 
20644cd51076SYaroslav Tykhiy 	STARTXFER;
20654cd51076SYaroslav Tykhiy 
2066ea022d16SRodney W. Grimes 	switch (type) {
2067ea022d16SRodney W. Grimes 
2068ea022d16SRodney W. Grimes 	case TYPE_A:
20694cd51076SYaroslav Tykhiy 		cp = EOF;
20704cd51076SYaroslav Tykhiy 		for (;;) {
20714cd51076SYaroslav Tykhiy 			c = getc(instr);
20724cd51076SYaroslav Tykhiy 			CHECKOOB(return (-1))
20734cd51076SYaroslav Tykhiy 			else if (c == EOF && ferror(instr))
20744cd51076SYaroslav Tykhiy 				goto file_err;
20754cd51076SYaroslav Tykhiy 			if (c == EOF) {
20764cd51076SYaroslav Tykhiy 				if (ferror(instr)) {	/* resume after OOB */
20774cd51076SYaroslav Tykhiy 					clearerr(instr);
20784cd51076SYaroslav Tykhiy 					continue;
2079ea022d16SRodney W. Grimes 				}
20804cd51076SYaroslav Tykhiy 				if (feof(instr))	/* EOF */
20814cd51076SYaroslav Tykhiy 					break;
20824cd51076SYaroslav Tykhiy 				syslog(LOG_ERR, "Internal: impossible condition"
20834cd51076SYaroslav Tykhiy 						" on file after getc()");
20844cd51076SYaroslav Tykhiy 				goto file_err;
20854cd51076SYaroslav Tykhiy 			}
20864cd51076SYaroslav Tykhiy 			if (c == '\n' && cp != '\r') {
20874cd51076SYaroslav Tykhiy 				FTPD_PUTC('\r', outstr, data_err);
20884cd51076SYaroslav Tykhiy 				byte_count++;
20894cd51076SYaroslav Tykhiy 			}
20904cd51076SYaroslav Tykhiy 			FTPD_PUTC(c, outstr, data_err);
20914cd51076SYaroslav Tykhiy 			byte_count++;
2092db1c2da3SYaroslav Tykhiy 			cp = c;
2093ea022d16SRodney W. Grimes 		}
20944cd51076SYaroslav Tykhiy #ifdef notyet	/* BSD stdio isn't ready for that */
20954cd51076SYaroslav Tykhiy 		while (fflush(outstr) == EOF) {
20964cd51076SYaroslav Tykhiy 			CHECKOOB(return (-1))
20974cd51076SYaroslav Tykhiy 			else
2098ea022d16SRodney W. Grimes 				goto data_err;
20994cd51076SYaroslav Tykhiy 			clearerr(outstr);
21004cd51076SYaroslav Tykhiy 		}
21014cd51076SYaroslav Tykhiy 		ENDXFER;
21024cd51076SYaroslav Tykhiy #else
21034cd51076SYaroslav Tykhiy 		ENDXFER;
21044cd51076SYaroslav Tykhiy 		if (fflush(outstr) == EOF)
21054cd51076SYaroslav Tykhiy 			goto data_err;
21064cd51076SYaroslav Tykhiy #endif
2107ea022d16SRodney W. Grimes 		reply(226, "Transfer complete.");
21084b82fc95SYaroslav Tykhiy 		return (0);
2109ea022d16SRodney W. Grimes 
2110ea022d16SRodney W. Grimes 	case TYPE_I:
2111ea022d16SRodney W. Grimes 	case TYPE_L:
21129fc5823aSGarrett Wollman 		/*
21139fc5823aSGarrett Wollman 		 * isreg is only set if we are not doing restart and we
21149fc5823aSGarrett Wollman 		 * are sending a regular file
21159fc5823aSGarrett Wollman 		 */
21169fc5823aSGarrett Wollman 		netfd = fileno(outstr);
21179fc5823aSGarrett Wollman 		filefd = fileno(instr);
21189fc5823aSGarrett Wollman 
2119f6f0c4b9SDan Moschuk 		if (isreg) {
21202e22b914SYaroslav Tykhiy 			char *msg = "Transfer complete.";
21214cd51076SYaroslav Tykhiy 			off_t cnt, offset;
2122f6f0c4b9SDan Moschuk 			int err;
2123f6f0c4b9SDan Moschuk 
21242f492fc8SYaroslav Tykhiy 			cnt = offset = 0;
2125f6f0c4b9SDan Moschuk 
21262f492fc8SYaroslav Tykhiy 			while (filesize > 0) {
2127492f1d9cSMaxim Konovalov 				err = sendfile(filefd, netfd, offset, 0,
21287e295315SYaroslav Tykhiy 					       NULL, &cnt, 0);
21293af48c42SMaxim Konovalov 				/*
21303af48c42SMaxim Konovalov 				 * Calculate byte_count before OOB processing.
21313af48c42SMaxim Konovalov 				 * It can be used in myoob() later.
21323af48c42SMaxim Konovalov 				 */
21333af48c42SMaxim Konovalov 				byte_count += cnt;
2134f6f0c4b9SDan Moschuk 				offset += cnt;
2135492f1d9cSMaxim Konovalov 				filesize -= cnt;
21364cd51076SYaroslav Tykhiy 				CHECKOOB(return (-1))
21374cd51076SYaroslav Tykhiy 				else if (err == -1) {
21384cd51076SYaroslav Tykhiy 					if (errno != EINTR &&
21394cd51076SYaroslav Tykhiy 					    cnt == 0 && offset == 0)
2140f6f0c4b9SDan Moschuk 						goto oldway;
21419fc5823aSGarrett Wollman 					goto data_err;
2142f6f0c4b9SDan Moschuk 				}
21434cd51076SYaroslav Tykhiy 				if (err == -1)	/* resume after OOB */
21444cd51076SYaroslav Tykhiy 					continue;
21452e22b914SYaroslav Tykhiy 				/*
21462e22b914SYaroslav Tykhiy 				 * We hit the EOF prematurely.
21472e22b914SYaroslav Tykhiy 				 * Perhaps the file was externally truncated.
21482e22b914SYaroslav Tykhiy 				 */
21492e22b914SYaroslav Tykhiy 				if (cnt == 0) {
21502e22b914SYaroslav Tykhiy 					msg = "Transfer finished due to "
21512e22b914SYaroslav Tykhiy 					      "premature end of file.";
21522e22b914SYaroslav Tykhiy 					break;
21532e22b914SYaroslav Tykhiy 				}
2154f6f0c4b9SDan Moschuk 			}
21554cd51076SYaroslav Tykhiy 			ENDXFER;
21562e22b914SYaroslav Tykhiy 			reply(226, msg);
21574b82fc95SYaroslav Tykhiy 			return (0);
21589fc5823aSGarrett Wollman 		}
21599fc5823aSGarrett Wollman 
21609fc5823aSGarrett Wollman oldway:
2161e3765043SYaroslav Tykhiy 		if ((buf = malloc(blksize)) == NULL) {
21624cd51076SYaroslav Tykhiy 			ENDXFER;
216302c97492SYaroslav Tykhiy 			reply(451, "Ran out of memory.");
21644b82fc95SYaroslav Tykhiy 			return (-1);
2165ea022d16SRodney W. Grimes 		}
21669fc5823aSGarrett Wollman 
21674cd51076SYaroslav Tykhiy 		for (;;) {
21684cd51076SYaroslav Tykhiy 			int cnt, len;
21694cd51076SYaroslav Tykhiy 			char *bp;
21704cd51076SYaroslav Tykhiy 
21714cd51076SYaroslav Tykhiy 			cnt = read(filefd, buf, blksize);
21724cd51076SYaroslav Tykhiy 			CHECKOOB(free(buf); return (-1))
21734cd51076SYaroslav Tykhiy 			else if (cnt < 0) {
21748efc8b18SYaroslav Tykhiy 				free(buf);
2175ea022d16SRodney W. Grimes 				goto file_err;
21764cd51076SYaroslav Tykhiy 			}
21774cd51076SYaroslav Tykhiy 			if (cnt < 0)	/* resume after OOB */
21784cd51076SYaroslav Tykhiy 				continue;
21794cd51076SYaroslav Tykhiy 			if (cnt == 0)	/* EOF */
21804cd51076SYaroslav Tykhiy 				break;
21814cd51076SYaroslav Tykhiy 			for (len = cnt, bp = buf; len > 0;) {
21824cd51076SYaroslav Tykhiy 				cnt = write(netfd, bp, len);
21834cd51076SYaroslav Tykhiy 				CHECKOOB(free(buf); return (-1))
21844cd51076SYaroslav Tykhiy 				else if (cnt < 0) {
21854cd51076SYaroslav Tykhiy 					free(buf);
2186ea022d16SRodney W. Grimes 					goto data_err;
2187ea022d16SRodney W. Grimes 				}
21884cd51076SYaroslav Tykhiy 				if (cnt <= 0)
21894cd51076SYaroslav Tykhiy 					continue;
21904cd51076SYaroslav Tykhiy 				len -= cnt;
21914cd51076SYaroslav Tykhiy 				bp += cnt;
21924cd51076SYaroslav Tykhiy 				byte_count += cnt;
21934cd51076SYaroslav Tykhiy 			}
21944cd51076SYaroslav Tykhiy 		}
21954cd51076SYaroslav Tykhiy 		ENDXFER;
21964cd51076SYaroslav Tykhiy 		free(buf);
2197ea022d16SRodney W. Grimes 		reply(226, "Transfer complete.");
21984b82fc95SYaroslav Tykhiy 		return (0);
2199ea022d16SRodney W. Grimes 	default:
22004cd51076SYaroslav Tykhiy 		ENDXFER;
220102c97492SYaroslav Tykhiy 		reply(550, "Unimplemented TYPE %d in send_data.", type);
22024b82fc95SYaroslav Tykhiy 		return (-1);
2203ea022d16SRodney W. Grimes 	}
2204ea022d16SRodney W. Grimes 
2205ea022d16SRodney W. Grimes data_err:
22064cd51076SYaroslav Tykhiy 	ENDXFER;
2207ea022d16SRodney W. Grimes 	perror_reply(426, "Data connection");
22084b82fc95SYaroslav Tykhiy 	return (-1);
2209ea022d16SRodney W. Grimes 
2210ea022d16SRodney W. Grimes file_err:
22114cd51076SYaroslav Tykhiy 	ENDXFER;
2212ea022d16SRodney W. Grimes 	perror_reply(551, "Error on input file");
22134b82fc95SYaroslav Tykhiy 	return (-1);
2214ea022d16SRodney W. Grimes }
2215ea022d16SRodney W. Grimes 
2216ea022d16SRodney W. Grimes /*
2217ea022d16SRodney W. Grimes  * Transfer data from peer to "outstr" using the appropriate encapulation of
2218ea022d16SRodney W. Grimes  * the data subject to Mode, Structure, and Type.
2219ea022d16SRodney W. Grimes  *
2220ea022d16SRodney W. Grimes  * N.B.: Form isn't handled.
2221ea022d16SRodney W. Grimes  */
2222ea022d16SRodney W. Grimes static int
2223e4bc453cSWarner Losh receive_data(FILE *instr, FILE *outstr)
2224ea022d16SRodney W. Grimes {
22254cd51076SYaroslav Tykhiy 	int c, cp;
22264cd51076SYaroslav Tykhiy 	int bare_lfs = 0;
2227ea022d16SRodney W. Grimes 
22284cd51076SYaroslav Tykhiy 	STARTXFER;
222961f891a6SPaul Traina 
2230ea022d16SRodney W. Grimes 	switch (type) {
2231ea022d16SRodney W. Grimes 
2232ea022d16SRodney W. Grimes 	case TYPE_I:
2233ea022d16SRodney W. Grimes 	case TYPE_L:
22344cd51076SYaroslav Tykhiy 		for (;;) {
22354cd51076SYaroslav Tykhiy 			int cnt, len;
22364cd51076SYaroslav Tykhiy 			char *bp;
22374cd51076SYaroslav Tykhiy 			char buf[BUFSIZ];
22384cd51076SYaroslav Tykhiy 
22394cd51076SYaroslav Tykhiy 			cnt = read(fileno(instr), buf, sizeof(buf));
22404cd51076SYaroslav Tykhiy 			CHECKOOB(return (-1))
22414cd51076SYaroslav Tykhiy 			else if (cnt < 0)
22424cd51076SYaroslav Tykhiy 				goto data_err;
22434cd51076SYaroslav Tykhiy 			if (cnt < 0)	/* resume after OOB */
22444cd51076SYaroslav Tykhiy 				continue;
22454cd51076SYaroslav Tykhiy 			if (cnt == 0)	/* EOF */
22464cd51076SYaroslav Tykhiy 				break;
22474cd51076SYaroslav Tykhiy 			for (len = cnt, bp = buf; len > 0;) {
22484cd51076SYaroslav Tykhiy 				cnt = write(fileno(outstr), bp, len);
22494cd51076SYaroslav Tykhiy 				CHECKOOB(return (-1))
22504cd51076SYaroslav Tykhiy 				else if (cnt < 0)
2251ea022d16SRodney W. Grimes 					goto file_err;
22524cd51076SYaroslav Tykhiy 				if (cnt <= 0)
22534cd51076SYaroslav Tykhiy 					continue;
22544cd51076SYaroslav Tykhiy 				len -= cnt;
22554cd51076SYaroslav Tykhiy 				bp += cnt;
2256ea022d16SRodney W. Grimes 				byte_count += cnt;
2257ea022d16SRodney W. Grimes 			}
22584cd51076SYaroslav Tykhiy 		}
22594cd51076SYaroslav Tykhiy 		ENDXFER;
2260ea022d16SRodney W. Grimes 		return (0);
2261ea022d16SRodney W. Grimes 
2262ea022d16SRodney W. Grimes 	case TYPE_E:
22634cd51076SYaroslav Tykhiy 		ENDXFER;
2264ea022d16SRodney W. Grimes 		reply(553, "TYPE E not implemented.");
2265ea022d16SRodney W. Grimes 		return (-1);
2266ea022d16SRodney W. Grimes 
2267ea022d16SRodney W. Grimes 	case TYPE_A:
22684cd51076SYaroslav Tykhiy 		cp = EOF;
22694cd51076SYaroslav Tykhiy 		for (;;) {
22704cd51076SYaroslav Tykhiy 			c = getc(instr);
22714cd51076SYaroslav Tykhiy 			CHECKOOB(return (-1))
22724cd51076SYaroslav Tykhiy 			else if (c == EOF && ferror(instr))
22734cd51076SYaroslav Tykhiy 				goto data_err;
22744cd51076SYaroslav Tykhiy 			if (c == EOF && ferror(instr)) { /* resume after OOB */
22754cd51076SYaroslav Tykhiy 				clearerr(instr);
22764cd51076SYaroslav Tykhiy 				continue;
22774cd51076SYaroslav Tykhiy 			}
22784cd51076SYaroslav Tykhiy 
22794cd51076SYaroslav Tykhiy 			if (cp == '\r') {
22804cd51076SYaroslav Tykhiy 				if (c != '\n')
22814cd51076SYaroslav Tykhiy 					FTPD_PUTC('\r', outstr, file_err);
22824cd51076SYaroslav Tykhiy 			} else
2283ea022d16SRodney W. Grimes 				if (c == '\n')
2284ea022d16SRodney W. Grimes 					bare_lfs++;
22854cd51076SYaroslav Tykhiy 			if (c == '\r') {
22864cd51076SYaroslav Tykhiy 				byte_count++;
22874cd51076SYaroslav Tykhiy 				cp = c;
22884cd51076SYaroslav Tykhiy 				continue;
22894cd51076SYaroslav Tykhiy 			}
22904cd51076SYaroslav Tykhiy 
22914cd51076SYaroslav Tykhiy 			/* Check for EOF here in order not to lose last \r. */
22924cd51076SYaroslav Tykhiy 			if (c == EOF) {
22934cd51076SYaroslav Tykhiy 				if (feof(instr))	/* EOF */
22944cd51076SYaroslav Tykhiy 					break;
22954cd51076SYaroslav Tykhiy 				syslog(LOG_ERR, "Internal: impossible condition"
22964cd51076SYaroslav Tykhiy 						" on data stream after getc()");
2297ea022d16SRodney W. Grimes 				goto data_err;
2298ea022d16SRodney W. Grimes 			}
22994cd51076SYaroslav Tykhiy 
23004cd51076SYaroslav Tykhiy 			byte_count++;
23014cd51076SYaroslav Tykhiy 			FTPD_PUTC(c, outstr, file_err);
23024cd51076SYaroslav Tykhiy 			cp = c;
2303ea022d16SRodney W. Grimes 		}
23044cd51076SYaroslav Tykhiy #ifdef notyet	/* BSD stdio isn't ready for that */
23054cd51076SYaroslav Tykhiy 		while (fflush(outstr) == EOF) {
23064cd51076SYaroslav Tykhiy 			CHECKOOB(return (-1))
23074cd51076SYaroslav Tykhiy 			else
2308ea022d16SRodney W. Grimes 				goto file_err;
23094cd51076SYaroslav Tykhiy 			clearerr(outstr);
23104cd51076SYaroslav Tykhiy 		}
23114cd51076SYaroslav Tykhiy 		ENDXFER;
23124cd51076SYaroslav Tykhiy #else
23134cd51076SYaroslav Tykhiy 		ENDXFER;
23144cd51076SYaroslav Tykhiy 		if (fflush(outstr) == EOF)
23154cd51076SYaroslav Tykhiy 			goto file_err;
23164cd51076SYaroslav Tykhiy #endif
2317ea022d16SRodney W. Grimes 		if (bare_lfs) {
2318ea022d16SRodney W. Grimes 			lreply(226,
231902c97492SYaroslav Tykhiy 		"WARNING! %d bare linefeeds received in ASCII mode.",
2320ea022d16SRodney W. Grimes 			    bare_lfs);
2321ea022d16SRodney W. Grimes 		(void)printf("   File may not have transferred correctly.\r\n");
2322ea022d16SRodney W. Grimes 		}
2323ea022d16SRodney W. Grimes 		return (0);
2324ea022d16SRodney W. Grimes 	default:
23254cd51076SYaroslav Tykhiy 		ENDXFER;
232602c97492SYaroslav Tykhiy 		reply(550, "Unimplemented TYPE %d in receive_data.", type);
2327ea022d16SRodney W. Grimes 		return (-1);
2328ea022d16SRodney W. Grimes 	}
2329ea022d16SRodney W. Grimes 
2330ea022d16SRodney W. Grimes data_err:
23314cd51076SYaroslav Tykhiy 	ENDXFER;
233202c97492SYaroslav Tykhiy 	perror_reply(426, "Data connection");
2333ea022d16SRodney W. Grimes 	return (-1);
2334ea022d16SRodney W. Grimes 
2335ea022d16SRodney W. Grimes file_err:
23364cd51076SYaroslav Tykhiy 	ENDXFER;
233702c97492SYaroslav Tykhiy 	perror_reply(452, "Error writing to file");
2338ea022d16SRodney W. Grimes 	return (-1);
2339ea022d16SRodney W. Grimes }
2340ea022d16SRodney W. Grimes 
2341ea022d16SRodney W. Grimes void
2342e4bc453cSWarner Losh statfilecmd(char *filename)
2343ea022d16SRodney W. Grimes {
2344ea022d16SRodney W. Grimes 	FILE *fin;
2345f8a581a0SYaroslav Tykhiy 	int atstart;
234641c57b48SYaroslav Tykhiy 	int c, code;
2347ea022d16SRodney W. Grimes 	char line[LINE_MAX];
234841c57b48SYaroslav Tykhiy 	struct stat st;
2349ea022d16SRodney W. Grimes 
235041c57b48SYaroslav Tykhiy 	code = lstat(filename, &st) == 0 && S_ISDIR(st.st_mode) ? 212 : 213;
2351af85d782SDavid Nugent 	(void)snprintf(line, sizeof(line), _PATH_LS " -lgA %s", filename);
2352ea022d16SRodney W. Grimes 	fin = ftpd_popen(line, "r");
2353*763e8c96SEd Maste 	if (fin == NULL) {
2354*763e8c96SEd Maste 		perror_reply(551, filename);
2355*763e8c96SEd Maste 		return;
2356*763e8c96SEd Maste 	}
23573b48b877SYaroslav Tykhiy 	lreply(code, "Status of %s:", filename);
2358f8a581a0SYaroslav Tykhiy 	atstart = 1;
2359ea022d16SRodney W. Grimes 	while ((c = getc(fin)) != EOF) {
2360ea022d16SRodney W. Grimes 		if (c == '\n') {
2361ea022d16SRodney W. Grimes 			if (ferror(stdout)){
236202c97492SYaroslav Tykhiy 				perror_reply(421, "Control connection");
2363ea022d16SRodney W. Grimes 				(void) ftpd_pclose(fin);
2364ea022d16SRodney W. Grimes 				dologout(1);
2365ea022d16SRodney W. Grimes 				/* NOTREACHED */
2366ea022d16SRodney W. Grimes 			}
2367ea022d16SRodney W. Grimes 			if (ferror(fin)) {
2368ea022d16SRodney W. Grimes 				perror_reply(551, filename);
2369ea022d16SRodney W. Grimes 				(void) ftpd_pclose(fin);
2370ea022d16SRodney W. Grimes 				return;
2371ea022d16SRodney W. Grimes 			}
2372ea022d16SRodney W. Grimes 			(void) putc('\r', stdout);
2373ea022d16SRodney W. Grimes 		}
2374f8a581a0SYaroslav Tykhiy 		/*
2375f8a581a0SYaroslav Tykhiy 		 * RFC 959 says neutral text should be prepended before
2376f8a581a0SYaroslav Tykhiy 		 * a leading 3-digit number followed by whitespace, but
2377f8a581a0SYaroslav Tykhiy 		 * many ftp clients can be confused by any leading digits,
2378f8a581a0SYaroslav Tykhiy 		 * as a matter of fact.
2379f8a581a0SYaroslav Tykhiy 		 */
2380f8a581a0SYaroslav Tykhiy 		if (atstart && isdigit(c))
2381f8a581a0SYaroslav Tykhiy 			(void) putc(' ', stdout);
2382ea022d16SRodney W. Grimes 		(void) putc(c, stdout);
2383f8a581a0SYaroslav Tykhiy 		atstart = (c == '\n');
2384ea022d16SRodney W. Grimes 	}
2385ea022d16SRodney W. Grimes 	(void) ftpd_pclose(fin);
238602c97492SYaroslav Tykhiy 	reply(code, "End of status.");
2387ea022d16SRodney W. Grimes }
2388ea022d16SRodney W. Grimes 
2389ea022d16SRodney W. Grimes void
2390e4bc453cSWarner Losh statcmd(void)
2391ea022d16SRodney W. Grimes {
23924dd8b5abSYoshinobu Inoue 	union sockunion *su;
2393ea022d16SRodney W. Grimes 	u_char *a, *p;
2394b0f06defSHajimu UMEMOTO 	char hname[NI_MAXHOST];
23954dd8b5abSYoshinobu Inoue 	int ispassive;
2396ea022d16SRodney W. Grimes 
2397c152df28SYaroslav Tykhiy 	if (hostinfo) {
2398a23f61bcSYaroslav Tykhiy 		lreply(211, "%s FTP server status:", hostname);
2399ea022d16SRodney W. Grimes 		printf("     %s\r\n", version);
2400c152df28SYaroslav Tykhiy 	} else
2401c152df28SYaroslav Tykhiy 		lreply(211, "FTP server status:");
2402ea022d16SRodney W. Grimes 	printf("     Connected to %s", remotehost);
24034dd8b5abSYoshinobu Inoue 	if (!getnameinfo((struct sockaddr *)&his_addr, his_addr.su_len,
2404b0f06defSHajimu UMEMOTO 			 hname, sizeof(hname) - 1, NULL, 0, NI_NUMERICHOST)) {
240504683b2cSYaroslav Tykhiy 		hname[sizeof(hname) - 1] = 0;
24064dd8b5abSYoshinobu Inoue 		if (strcmp(hname, remotehost) != 0)
24074dd8b5abSYoshinobu Inoue 			printf(" (%s)", hname);
24084dd8b5abSYoshinobu Inoue 	}
2409ea022d16SRodney W. Grimes 	printf("\r\n");
2410ea022d16SRodney W. Grimes 	if (logged_in) {
2411ea022d16SRodney W. Grimes 		if (guest)
2412ea022d16SRodney W. Grimes 			printf("     Logged in anonymously\r\n");
2413ea022d16SRodney W. Grimes 		else
2414ea022d16SRodney W. Grimes 			printf("     Logged in as %s\r\n", pw->pw_name);
2415ea022d16SRodney W. Grimes 	} else if (askpasswd)
2416ea022d16SRodney W. Grimes 		printf("     Waiting for password\r\n");
2417ea022d16SRodney W. Grimes 	else
2418ea022d16SRodney W. Grimes 		printf("     Waiting for user name\r\n");
2419ea022d16SRodney W. Grimes 	printf("     TYPE: %s", typenames[type]);
2420ea022d16SRodney W. Grimes 	if (type == TYPE_A || type == TYPE_E)
2421ea022d16SRodney W. Grimes 		printf(", FORM: %s", formnames[form]);
2422ea022d16SRodney W. Grimes 	if (type == TYPE_L)
242389fdc4e1SMike Barcroft #if CHAR_BIT == 8
242489fdc4e1SMike Barcroft 		printf(" %d", CHAR_BIT);
2425ea022d16SRodney W. Grimes #else
2426ea022d16SRodney W. Grimes 		printf(" %d", bytesize);	/* need definition! */
2427ea022d16SRodney W. Grimes #endif
2428ea022d16SRodney W. Grimes 	printf("; STRUcture: %s; transfer MODE: %s\r\n",
2429ea022d16SRodney W. Grimes 	    strunames[stru], modenames[mode]);
2430ea022d16SRodney W. Grimes 	if (data != -1)
2431ea022d16SRodney W. Grimes 		printf("     Data connection open\r\n");
2432ea022d16SRodney W. Grimes 	else if (pdata != -1) {
24334dd8b5abSYoshinobu Inoue 		ispassive = 1;
24344dd8b5abSYoshinobu Inoue 		su = &pasv_addr;
2435ea022d16SRodney W. Grimes 		goto printaddr;
2436ea022d16SRodney W. Grimes 	} else if (usedefault == 0) {
24374dd8b5abSYoshinobu Inoue 		ispassive = 0;
24384dd8b5abSYoshinobu Inoue 		su = &data_dest;
2439ea022d16SRodney W. Grimes printaddr:
2440ea022d16SRodney W. Grimes #define UC(b) (((int) b) & 0xff)
24414dd8b5abSYoshinobu Inoue 		if (epsvall) {
24424dd8b5abSYoshinobu Inoue 			printf("     EPSV only mode (EPSV ALL)\r\n");
24434dd8b5abSYoshinobu Inoue 			goto epsvonly;
24444dd8b5abSYoshinobu Inoue 		}
24454dd8b5abSYoshinobu Inoue 
24464dd8b5abSYoshinobu Inoue 		/* PORT/PASV */
24474dd8b5abSYoshinobu Inoue 		if (su->su_family == AF_INET) {
24484dd8b5abSYoshinobu Inoue 			a = (u_char *) &su->su_sin.sin_addr;
24494dd8b5abSYoshinobu Inoue 			p = (u_char *) &su->su_sin.sin_port;
24504dd8b5abSYoshinobu Inoue 			printf("     %s (%d,%d,%d,%d,%d,%d)\r\n",
24514dd8b5abSYoshinobu Inoue 				ispassive ? "PASV" : "PORT",
24524dd8b5abSYoshinobu Inoue 				UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
24534dd8b5abSYoshinobu Inoue 				UC(p[0]), UC(p[1]));
24544dd8b5abSYoshinobu Inoue 		}
24554dd8b5abSYoshinobu Inoue 
24564dd8b5abSYoshinobu Inoue 		/* LPRT/LPSV */
24574dd8b5abSYoshinobu Inoue 	    {
24584dd8b5abSYoshinobu Inoue 		int alen, af, i;
24594dd8b5abSYoshinobu Inoue 
24604dd8b5abSYoshinobu Inoue 		switch (su->su_family) {
24614dd8b5abSYoshinobu Inoue 		case AF_INET:
24624dd8b5abSYoshinobu Inoue 			a = (u_char *) &su->su_sin.sin_addr;
24634dd8b5abSYoshinobu Inoue 			p = (u_char *) &su->su_sin.sin_port;
24644dd8b5abSYoshinobu Inoue 			alen = sizeof(su->su_sin.sin_addr);
24654dd8b5abSYoshinobu Inoue 			af = 4;
24664dd8b5abSYoshinobu Inoue 			break;
24674dd8b5abSYoshinobu Inoue 		case AF_INET6:
24684dd8b5abSYoshinobu Inoue 			a = (u_char *) &su->su_sin6.sin6_addr;
24694dd8b5abSYoshinobu Inoue 			p = (u_char *) &su->su_sin6.sin6_port;
24704dd8b5abSYoshinobu Inoue 			alen = sizeof(su->su_sin6.sin6_addr);
24714dd8b5abSYoshinobu Inoue 			af = 6;
24724dd8b5abSYoshinobu Inoue 			break;
24734dd8b5abSYoshinobu Inoue 		default:
24744dd8b5abSYoshinobu Inoue 			af = 0;
24754dd8b5abSYoshinobu Inoue 			break;
24764dd8b5abSYoshinobu Inoue 		}
24774dd8b5abSYoshinobu Inoue 		if (af) {
24784dd8b5abSYoshinobu Inoue 			printf("     %s (%d,%d,", ispassive ? "LPSV" : "LPRT",
24794dd8b5abSYoshinobu Inoue 				af, alen);
24804dd8b5abSYoshinobu Inoue 			for (i = 0; i < alen; i++)
24814dd8b5abSYoshinobu Inoue 				printf("%d,", UC(a[i]));
24824dd8b5abSYoshinobu Inoue 			printf("%d,%d,%d)\r\n", 2, UC(p[0]), UC(p[1]));
24834dd8b5abSYoshinobu Inoue 		}
24844dd8b5abSYoshinobu Inoue 	    }
24854dd8b5abSYoshinobu Inoue 
24864dd8b5abSYoshinobu Inoue epsvonly:;
24874dd8b5abSYoshinobu Inoue 		/* EPRT/EPSV */
24884dd8b5abSYoshinobu Inoue 	    {
24894dd8b5abSYoshinobu Inoue 		int af;
24904dd8b5abSYoshinobu Inoue 
24914dd8b5abSYoshinobu Inoue 		switch (su->su_family) {
24924dd8b5abSYoshinobu Inoue 		case AF_INET:
24934dd8b5abSYoshinobu Inoue 			af = 1;
24944dd8b5abSYoshinobu Inoue 			break;
24954dd8b5abSYoshinobu Inoue 		case AF_INET6:
24964dd8b5abSYoshinobu Inoue 			af = 2;
24974dd8b5abSYoshinobu Inoue 			break;
24984dd8b5abSYoshinobu Inoue 		default:
24994dd8b5abSYoshinobu Inoue 			af = 0;
25004dd8b5abSYoshinobu Inoue 			break;
25014dd8b5abSYoshinobu Inoue 		}
25024dd8b5abSYoshinobu Inoue 		if (af) {
2503b0f06defSHajimu UMEMOTO 			union sockunion tmp;
2504b0f06defSHajimu UMEMOTO 
2505b0f06defSHajimu UMEMOTO 			tmp = *su;
2506b0f06defSHajimu UMEMOTO 			if (tmp.su_family == AF_INET6)
2507b0f06defSHajimu UMEMOTO 				tmp.su_sin6.sin6_scope_id = 0;
2508b0f06defSHajimu UMEMOTO 			if (!getnameinfo((struct sockaddr *)&tmp, tmp.su_len,
25094dd8b5abSYoshinobu Inoue 					hname, sizeof(hname) - 1, NULL, 0,
25104dd8b5abSYoshinobu Inoue 					NI_NUMERICHOST)) {
251104683b2cSYaroslav Tykhiy 				hname[sizeof(hname) - 1] = 0;
25124dd8b5abSYoshinobu Inoue 				printf("     %s |%d|%s|%d|\r\n",
25134dd8b5abSYoshinobu Inoue 					ispassive ? "EPSV" : "EPRT",
2514b0f06defSHajimu UMEMOTO 					af, hname, htons(tmp.su_port));
25154dd8b5abSYoshinobu Inoue 			}
25164dd8b5abSYoshinobu Inoue 		}
25174dd8b5abSYoshinobu Inoue 	    }
2518ea022d16SRodney W. Grimes #undef UC
2519ea022d16SRodney W. Grimes 	} else
2520ea022d16SRodney W. Grimes 		printf("     No data connection\r\n");
252102c97492SYaroslav Tykhiy 	reply(211, "End of status.");
2522ea022d16SRodney W. Grimes }
2523ea022d16SRodney W. Grimes 
2524ea022d16SRodney W. Grimes void
2525e4bc453cSWarner Losh fatalerror(char *s)
2526ea022d16SRodney W. Grimes {
2527ea022d16SRodney W. Grimes 
25284a3e5acdSYaroslav Tykhiy 	reply(451, "Error in server: %s", s);
2529ea022d16SRodney W. Grimes 	reply(221, "Closing connection due to server error.");
2530ea022d16SRodney W. Grimes 	dologout(0);
2531ea022d16SRodney W. Grimes 	/* NOTREACHED */
2532ea022d16SRodney W. Grimes }
2533ea022d16SRodney W. Grimes 
2534ea022d16SRodney W. Grimes void
2535ea022d16SRodney W. Grimes reply(int n, const char *fmt, ...)
2536ea022d16SRodney W. Grimes {
2537ea022d16SRodney W. Grimes 	va_list ap;
2538e4bc453cSWarner Losh 
2539ea022d16SRodney W. Grimes 	(void)printf("%d ", n);
25409cbb335cSTim J. Robbins 	va_start(ap, fmt);
2541ea022d16SRodney W. Grimes 	(void)vprintf(fmt, ap);
25429cbb335cSTim J. Robbins 	va_end(ap);
2543ea022d16SRodney W. Grimes 	(void)printf("\r\n");
2544ea022d16SRodney W. Grimes 	(void)fflush(stdout);
2545618b0bbaSMark Murray 	if (ftpdebug) {
2546ea022d16SRodney W. Grimes 		syslog(LOG_DEBUG, "<--- %d ", n);
25479cbb335cSTim J. Robbins 		va_start(ap, fmt);
2548ea022d16SRodney W. Grimes 		vsyslog(LOG_DEBUG, fmt, ap);
25499cbb335cSTim J. Robbins 		va_end(ap);
2550ea022d16SRodney W. Grimes 	}
2551ea022d16SRodney W. Grimes }
2552ea022d16SRodney W. Grimes 
2553ea022d16SRodney W. Grimes void
2554ea022d16SRodney W. Grimes lreply(int n, const char *fmt, ...)
2555ea022d16SRodney W. Grimes {
2556ea022d16SRodney W. Grimes 	va_list ap;
2557e4bc453cSWarner Losh 
2558ea022d16SRodney W. Grimes 	(void)printf("%d- ", n);
25599cbb335cSTim J. Robbins 	va_start(ap, fmt);
2560ea022d16SRodney W. Grimes 	(void)vprintf(fmt, ap);
25619cbb335cSTim J. Robbins 	va_end(ap);
2562ea022d16SRodney W. Grimes 	(void)printf("\r\n");
2563ea022d16SRodney W. Grimes 	(void)fflush(stdout);
2564618b0bbaSMark Murray 	if (ftpdebug) {
2565ea022d16SRodney W. Grimes 		syslog(LOG_DEBUG, "<--- %d- ", n);
25669cbb335cSTim J. Robbins 		va_start(ap, fmt);
2567ea022d16SRodney W. Grimes 		vsyslog(LOG_DEBUG, fmt, ap);
25689cbb335cSTim J. Robbins 		va_end(ap);
2569ea022d16SRodney W. Grimes 	}
2570ea022d16SRodney W. Grimes }
2571ea022d16SRodney W. Grimes 
2572ea022d16SRodney W. Grimes static void
2573e4bc453cSWarner Losh ack(char *s)
2574ea022d16SRodney W. Grimes {
2575ea022d16SRodney W. Grimes 
2576ea022d16SRodney W. Grimes 	reply(250, "%s command successful.", s);
2577ea022d16SRodney W. Grimes }
2578ea022d16SRodney W. Grimes 
2579ea022d16SRodney W. Grimes void
2580e4bc453cSWarner Losh nack(char *s)
2581ea022d16SRodney W. Grimes {
2582ea022d16SRodney W. Grimes 
2583ea022d16SRodney W. Grimes 	reply(502, "%s command not implemented.", s);
2584ea022d16SRodney W. Grimes }
2585ea022d16SRodney W. Grimes 
2586ea022d16SRodney W. Grimes /* ARGSUSED */
2587ea022d16SRodney W. Grimes void
2588e4bc453cSWarner Losh yyerror(char *s)
2589ea022d16SRodney W. Grimes {
2590ea022d16SRodney W. Grimes 	char *cp;
2591ea022d16SRodney W. Grimes 
25929aca17cbSMark Murray 	if ((cp = strchr(cbuf,'\n')))
2593ea022d16SRodney W. Grimes 		*cp = '\0';
259402c97492SYaroslav Tykhiy 	reply(500, "%s: command not understood.", cbuf);
2595ea022d16SRodney W. Grimes }
2596ea022d16SRodney W. Grimes 
2597ea022d16SRodney W. Grimes void
2598e4bc453cSWarner Losh delete(char *name)
2599ea022d16SRodney W. Grimes {
2600ea022d16SRodney W. Grimes 	struct stat st;
2601ea022d16SRodney W. Grimes 
2602ea022d16SRodney W. Grimes 	LOGCMD("delete", name);
26031b0e12d7SYaroslav Tykhiy 	if (lstat(name, &st) < 0) {
2604ea022d16SRodney W. Grimes 		perror_reply(550, name);
2605ea022d16SRodney W. Grimes 		return;
2606ea022d16SRodney W. Grimes 	}
2607ac4f2391SYaroslav Tykhiy 	if (S_ISDIR(st.st_mode)) {
2608ea022d16SRodney W. Grimes 		if (rmdir(name) < 0) {
2609ea022d16SRodney W. Grimes 			perror_reply(550, name);
2610ea022d16SRodney W. Grimes 			return;
2611ea022d16SRodney W. Grimes 		}
2612ea022d16SRodney W. Grimes 		goto done;
2613ea022d16SRodney W. Grimes 	}
26143f8b9cfeSYaroslav Tykhiy 	if (guest && noguestmod) {
261502c97492SYaroslav Tykhiy 		reply(550, "Operation not permitted.");
26163f8b9cfeSYaroslav Tykhiy 		return;
26173f8b9cfeSYaroslav Tykhiy 	}
26183f8b9cfeSYaroslav Tykhiy 	if (unlink(name) < 0) {
2619ea022d16SRodney W. Grimes 		perror_reply(550, name);
2620ea022d16SRodney W. Grimes 		return;
2621ea022d16SRodney W. Grimes 	}
2622ea022d16SRodney W. Grimes done:
2623ea022d16SRodney W. Grimes 	ack("DELE");
2624ea022d16SRodney W. Grimes }
2625ea022d16SRodney W. Grimes 
2626ea022d16SRodney W. Grimes void
2627e4bc453cSWarner Losh cwd(char *path)
2628ea022d16SRodney W. Grimes {
2629ea022d16SRodney W. Grimes 
2630ea022d16SRodney W. Grimes 	if (chdir(path) < 0)
2631ea022d16SRodney W. Grimes 		perror_reply(550, path);
2632ea022d16SRodney W. Grimes 	else
2633ea022d16SRodney W. Grimes 		ack("CWD");
2634ea022d16SRodney W. Grimes }
2635ea022d16SRodney W. Grimes 
2636ea022d16SRodney W. Grimes void
2637e4bc453cSWarner Losh makedir(char *name)
2638ea022d16SRodney W. Grimes {
26392b748987SYaroslav Tykhiy 	char *s;
2640ea022d16SRodney W. Grimes 
2641ea022d16SRodney W. Grimes 	LOGCMD("mkdir", name);
2642d186bb12SMatthew N. Dodd 	if (guest && noguestmkd)
2643405e2987SYaroslav Tykhiy 		reply(550, "Operation not permitted.");
2644d186bb12SMatthew N. Dodd 	else if (mkdir(name, 0777) < 0)
2645ea022d16SRodney W. Grimes 		perror_reply(550, name);
26462b748987SYaroslav Tykhiy 	else {
26472b748987SYaroslav Tykhiy 		if ((s = doublequote(name)) == NULL)
26482b748987SYaroslav Tykhiy 			fatalerror("Ran out of memory.");
26492b748987SYaroslav Tykhiy 		reply(257, "\"%s\" directory created.", s);
26502b748987SYaroslav Tykhiy 		free(s);
26512b748987SYaroslav Tykhiy 	}
2652ea022d16SRodney W. Grimes }
2653ea022d16SRodney W. Grimes 
2654ea022d16SRodney W. Grimes void
2655e4bc453cSWarner Losh removedir(char *name)
2656ea022d16SRodney W. Grimes {
2657ea022d16SRodney W. Grimes 
2658ea022d16SRodney W. Grimes 	LOGCMD("rmdir", name);
2659ea022d16SRodney W. Grimes 	if (rmdir(name) < 0)
2660ea022d16SRodney W. Grimes 		perror_reply(550, name);
2661ea022d16SRodney W. Grimes 	else
2662ea022d16SRodney W. Grimes 		ack("RMD");
2663ea022d16SRodney W. Grimes }
2664ea022d16SRodney W. Grimes 
2665ea022d16SRodney W. Grimes void
2666e4bc453cSWarner Losh pwd(void)
2667ea022d16SRodney W. Grimes {
2668e4648f05SYaroslav Tykhiy 	char *s, path[MAXPATHLEN + 1];
2669ea022d16SRodney W. Grimes 
2670de9b6c03SYaroslav Tykhiy 	if (getcwd(path, sizeof(path)) == NULL)
267175933089SYaroslav Tykhiy 		perror_reply(550, "Get current directory");
2672e4648f05SYaroslav Tykhiy 	else {
2673e4648f05SYaroslav Tykhiy 		if ((s = doublequote(path)) == NULL)
2674e4648f05SYaroslav Tykhiy 			fatalerror("Ran out of memory.");
2675e4648f05SYaroslav Tykhiy 		reply(257, "\"%s\" is current directory.", s);
2676e4648f05SYaroslav Tykhiy 		free(s);
2677e4648f05SYaroslav Tykhiy 	}
2678ea022d16SRodney W. Grimes }
2679ea022d16SRodney W. Grimes 
2680ea022d16SRodney W. Grimes char *
2681e4bc453cSWarner Losh renamefrom(char *name)
2682ea022d16SRodney W. Grimes {
2683ea022d16SRodney W. Grimes 	struct stat st;
2684ea022d16SRodney W. Grimes 
2685b943b3c4SYaroslav Tykhiy 	if (guest && noguestmod) {
268602c97492SYaroslav Tykhiy 		reply(550, "Operation not permitted.");
2687b943b3c4SYaroslav Tykhiy 		return (NULL);
2688b943b3c4SYaroslav Tykhiy 	}
26891b0e12d7SYaroslav Tykhiy 	if (lstat(name, &st) < 0) {
2690ea022d16SRodney W. Grimes 		perror_reply(550, name);
2691385f9bf0SYaroslav Tykhiy 		return (NULL);
2692ea022d16SRodney W. Grimes 	}
269302c97492SYaroslav Tykhiy 	reply(350, "File exists, ready for destination name.");
2694ea022d16SRodney W. Grimes 	return (name);
2695ea022d16SRodney W. Grimes }
2696ea022d16SRodney W. Grimes 
2697ea022d16SRodney W. Grimes void
2698e4bc453cSWarner Losh renamecmd(char *from, char *to)
2699ea022d16SRodney W. Grimes {
270061f891a6SPaul Traina 	struct stat st;
2701ea022d16SRodney W. Grimes 
2702ea022d16SRodney W. Grimes 	LOGCMD2("rename", from, to);
270361f891a6SPaul Traina 
270461f891a6SPaul Traina 	if (guest && (stat(to, &st) == 0)) {
270502c97492SYaroslav Tykhiy 		reply(550, "%s: permission denied.", to);
270661f891a6SPaul Traina 		return;
270761f891a6SPaul Traina 	}
270861f891a6SPaul Traina 
2709ea022d16SRodney W. Grimes 	if (rename(from, to) < 0)
2710ea022d16SRodney W. Grimes 		perror_reply(550, "rename");
2711ea022d16SRodney W. Grimes 	else
2712ea022d16SRodney W. Grimes 		ack("RNTO");
2713ea022d16SRodney W. Grimes }
2714ea022d16SRodney W. Grimes 
2715ea022d16SRodney W. Grimes static void
2716e4bc453cSWarner Losh dolog(struct sockaddr *who)
2717ea022d16SRodney W. Grimes {
27187cdd3cb7SYaroslav Tykhiy 	char who_name[NI_MAXHOST];
27194dd8b5abSYoshinobu Inoue 
27204dd8b5abSYoshinobu Inoue 	realhostname_sa(remotehost, sizeof(remotehost) - 1, who, who->sa_len);
272104683b2cSYaroslav Tykhiy 	remotehost[sizeof(remotehost) - 1] = 0;
27227cdd3cb7SYaroslav Tykhiy 	if (getnameinfo(who, who->sa_len,
27237cdd3cb7SYaroslav Tykhiy 		who_name, sizeof(who_name) - 1, NULL, 0, NI_NUMERICHOST))
27247cdd3cb7SYaroslav Tykhiy 			*who_name = 0;
27257cdd3cb7SYaroslav Tykhiy 	who_name[sizeof(who_name) - 1] = 0;
2726ea022d16SRodney W. Grimes 
2727ea022d16SRodney W. Grimes #ifdef SETPROCTITLE
2728ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
2729ea4e54b9SDavid Nugent 	if (thishost != firsthost)
2730ea4e54b9SDavid Nugent 		snprintf(proctitle, sizeof(proctitle), "%s: connected (to %s)",
2731ea4e54b9SDavid Nugent 			 remotehost, hostname);
2732ea4e54b9SDavid Nugent 	else
2733ea4e54b9SDavid Nugent #endif
2734ea4e54b9SDavid Nugent 		snprintf(proctitle, sizeof(proctitle), "%s: connected",
2735ea4e54b9SDavid Nugent 			 remotehost);
2736b63e1fe2SPeter Wemm 	setproctitle("%s", proctitle);
2737ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */
2738ea022d16SRodney W. Grimes 
2739ea4e54b9SDavid Nugent 	if (logging) {
2740ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
2741ea4e54b9SDavid Nugent 		if (thishost != firsthost)
27427cdd3cb7SYaroslav Tykhiy 			syslog(LOG_INFO, "connection from %s (%s) to %s",
27437cdd3cb7SYaroslav Tykhiy 			       remotehost, who_name, hostname);
2744ea4e54b9SDavid Nugent 		else
2745ea4e54b9SDavid Nugent #endif
27467cdd3cb7SYaroslav Tykhiy 			syslog(LOG_INFO, "connection from %s (%s)",
27477cdd3cb7SYaroslav Tykhiy 			       remotehost, who_name);
2748ea022d16SRodney W. Grimes 	}
2749ea4e54b9SDavid Nugent }
2750ea022d16SRodney W. Grimes 
2751ea022d16SRodney W. Grimes /*
2752ea022d16SRodney W. Grimes  * Record logout in wtmp file
2753ea022d16SRodney W. Grimes  * and exit with supplied status.
2754ea022d16SRodney W. Grimes  */
2755ea022d16SRodney W. Grimes void
2756e4bc453cSWarner Losh dologout(int status)
2757ea022d16SRodney W. Grimes {
2758ea022d16SRodney W. Grimes 
27599f37b1a2SEd Schouten 	if (logged_in && dowtmp) {
276052e7ee74SYaroslav Tykhiy 		(void) seteuid(0);
27619f37b1a2SEd Schouten 		ftpd_logwtmp(wtmpid, NULL, NULL);
2762ea022d16SRodney W. Grimes 	}
2763ea022d16SRodney W. Grimes 	/* beware of flushing buffers after a SIGPIPE */
2764ea022d16SRodney W. Grimes 	_exit(status);
2765ea022d16SRodney W. Grimes }
2766ea022d16SRodney W. Grimes 
2767ea022d16SRodney W. Grimes static void
2768e4bc453cSWarner Losh sigurg(int signo)
2769ea022d16SRodney W. Grimes {
27704b82fc95SYaroslav Tykhiy 
27714b82fc95SYaroslav Tykhiy 	recvurg = 1;
27724b82fc95SYaroslav Tykhiy }
27734b82fc95SYaroslav Tykhiy 
27744b82fc95SYaroslav Tykhiy static void
27754cd51076SYaroslav Tykhiy maskurg(int flag)
27764cd51076SYaroslav Tykhiy {
27774cd51076SYaroslav Tykhiy 	int oerrno;
27784cd51076SYaroslav Tykhiy 	sigset_t sset;
27794cd51076SYaroslav Tykhiy 
27804cd51076SYaroslav Tykhiy 	if (!transflag) {
27814cd51076SYaroslav Tykhiy 		syslog(LOG_ERR, "Internal: maskurg() while no transfer");
27824cd51076SYaroslav Tykhiy 		return;
27834cd51076SYaroslav Tykhiy 	}
27844cd51076SYaroslav Tykhiy 	oerrno = errno;
27854cd51076SYaroslav Tykhiy 	sigemptyset(&sset);
27864cd51076SYaroslav Tykhiy 	sigaddset(&sset, SIGURG);
27874cd51076SYaroslav Tykhiy 	sigprocmask(flag ? SIG_BLOCK : SIG_UNBLOCK, &sset, NULL);
27884cd51076SYaroslav Tykhiy 	errno = oerrno;
27894cd51076SYaroslav Tykhiy }
27904cd51076SYaroslav Tykhiy 
27914cd51076SYaroslav Tykhiy static void
27924cd51076SYaroslav Tykhiy flagxfer(int flag)
27934cd51076SYaroslav Tykhiy {
27944cd51076SYaroslav Tykhiy 
27954cd51076SYaroslav Tykhiy 	if (flag) {
2796f9036ce6SYaroslav Tykhiy 		if (transflag)
2797f9036ce6SYaroslav Tykhiy 			syslog(LOG_ERR, "Internal: flagxfer(1): "
2798f9036ce6SYaroslav Tykhiy 					"transfer already under way");
27994cd51076SYaroslav Tykhiy 		transflag = 1;
280091ae7779SYaroslav Tykhiy 		maskurg(0);
280191ae7779SYaroslav Tykhiy 		recvurg = 0;
280291ae7779SYaroslav Tykhiy 	} else {
2803f9036ce6SYaroslav Tykhiy 		if (!transflag)
2804f9036ce6SYaroslav Tykhiy 			syslog(LOG_ERR, "Internal: flagxfer(0): "
2805f9036ce6SYaroslav Tykhiy 					"no active transfer");
280691ae7779SYaroslav Tykhiy 		maskurg(1);
28074cd51076SYaroslav Tykhiy 		transflag = 0;
28084cd51076SYaroslav Tykhiy 	}
280991ae7779SYaroslav Tykhiy }
28104cd51076SYaroslav Tykhiy 
28114cd51076SYaroslav Tykhiy /*
28124cd51076SYaroslav Tykhiy  * Returns 0 if OK to resume or -1 if abort requested.
28134cd51076SYaroslav Tykhiy  */
28144cd51076SYaroslav Tykhiy static int
2815e4bc453cSWarner Losh myoob(void)
28164b82fc95SYaroslav Tykhiy {
2817ea022d16SRodney W. Grimes 	char *cp;
2818f0b40b1cSColin Percival 	int ret;
2819ea022d16SRodney W. Grimes 
28204cd51076SYaroslav Tykhiy 	if (!transflag) {
28214cd51076SYaroslav Tykhiy 		syslog(LOG_ERR, "Internal: myoob() while no transfer");
28224cd51076SYaroslav Tykhiy 		return (0);
28234cd51076SYaroslav Tykhiy 	}
2824ea022d16SRodney W. Grimes 	cp = tmpline;
2825f0b40b1cSColin Percival 	ret = getline(cp, 7, stdin);
2826f0b40b1cSColin Percival 	if (ret == -1) {
2827ea022d16SRodney W. Grimes 		reply(221, "You could at least say goodbye.");
2828ea022d16SRodney W. Grimes 		dologout(0);
2829f0b40b1cSColin Percival 	} else if (ret == -2) {
2830f0b40b1cSColin Percival 		/* Ignore truncated command. */
2831f0b40b1cSColin Percival 		return (0);
2832ea022d16SRodney W. Grimes 	}
2833ea022d16SRodney W. Grimes 	upper(cp);
2834ea022d16SRodney W. Grimes 	if (strcmp(cp, "ABOR\r\n") == 0) {
2835ea022d16SRodney W. Grimes 		tmpline[0] = '\0';
2836ea022d16SRodney W. Grimes 		reply(426, "Transfer aborted. Data connection closed.");
283702c97492SYaroslav Tykhiy 		reply(226, "Abort successful.");
28384cd51076SYaroslav Tykhiy 		return (-1);
2839ea022d16SRodney W. Grimes 	}
2840ea022d16SRodney W. Grimes 	if (strcmp(cp, "STAT\r\n") == 0) {
28419db4bbf3SMichael Haro 		tmpline[0] = '\0';
28420e519c96SYaroslav Tykhiy 		if (file_size != -1)
284302c97492SYaroslav Tykhiy 			reply(213, "Status: %jd of %jd bytes transferred.",
2844a57e1ef0SYaroslav Tykhiy 				   (intmax_t)byte_count, (intmax_t)file_size);
2845ea022d16SRodney W. Grimes 		else
284602c97492SYaroslav Tykhiy 			reply(213, "Status: %jd bytes transferred.",
2847a57e1ef0SYaroslav Tykhiy 				   (intmax_t)byte_count);
2848ea022d16SRodney W. Grimes 	}
28494cd51076SYaroslav Tykhiy 	return (0);
2850ea022d16SRodney W. Grimes }
2851ea022d16SRodney W. Grimes 
2852ea022d16SRodney W. Grimes /*
2853ea022d16SRodney W. Grimes  * Note: a response of 425 is not mentioned as a possible response to
2854ea022d16SRodney W. Grimes  *	the PASV command in RFC959. However, it has been blessed as
2855ea022d16SRodney W. Grimes  *	a legitimate response by Jon Postel in a telephone conversation
2856ea022d16SRodney W. Grimes  *	with Rick Adams on 25 Jan 89.
2857ea022d16SRodney W. Grimes  */
2858ea022d16SRodney W. Grimes void
2859e4bc453cSWarner Losh passive(void)
2860ea022d16SRodney W. Grimes {
286178e3eed0SStefan Farfeleder 	socklen_t len;
286278e3eed0SStefan Farfeleder 	int on;
2863ea022d16SRodney W. Grimes 	char *p, *a;
2864ea022d16SRodney W. Grimes 
286561f891a6SPaul Traina 	if (pdata >= 0)		/* close old port if one set */
286661f891a6SPaul Traina 		close(pdata);
286761f891a6SPaul Traina 
28684dd8b5abSYoshinobu Inoue 	pdata = socket(ctrl_addr.su_family, SOCK_STREAM, 0);
2869ea022d16SRodney W. Grimes 	if (pdata < 0) {
2870ea022d16SRodney W. Grimes 		perror_reply(425, "Can't open passive connection");
2871ea022d16SRodney W. Grimes 		return;
2872ea022d16SRodney W. Grimes 	}
28738af7c9a3SYaroslav Tykhiy 	on = 1;
28748af7c9a3SYaroslav Tykhiy 	if (setsockopt(pdata, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
28758af7c9a3SYaroslav Tykhiy 		syslog(LOG_WARNING, "pdata setsockopt (SO_REUSEADDR): %m");
28764c450ad7SPaul Traina 
287752e7ee74SYaroslav Tykhiy 	(void) seteuid(0);
287861f891a6SPaul Traina 
2879dacc9752SPaul Traina #ifdef IP_PORTRANGE
28804dd8b5abSYoshinobu Inoue 	if (ctrl_addr.su_family == AF_INET) {
28818af7c9a3SYaroslav Tykhiy 	    on = restricted_data_ports ? IP_PORTRANGE_HIGH
2882dacc9752SPaul Traina 				       : IP_PORTRANGE_DEFAULT;
2883dacc9752SPaul Traina 
288440e9d39eSPeter Wemm 	    if (setsockopt(pdata, IPPROTO_IP, IP_PORTRANGE,
288557d4ef07SYaroslav Tykhiy 			    &on, sizeof(on)) < 0)
28864c450ad7SPaul Traina 		    goto pasv_error;
28874c450ad7SPaul Traina 	}
2888dacc9752SPaul Traina #endif
28892db39860SNick Sayer #ifdef IPV6_PORTRANGE
28902db39860SNick Sayer 	if (ctrl_addr.su_family == AF_INET6) {
28918af7c9a3SYaroslav Tykhiy 	    on = restricted_data_ports ? IPV6_PORTRANGE_HIGH
28922db39860SNick Sayer 				       : IPV6_PORTRANGE_DEFAULT;
28932db39860SNick Sayer 
28942db39860SNick Sayer 	    if (setsockopt(pdata, IPPROTO_IPV6, IPV6_PORTRANGE,
289557d4ef07SYaroslav Tykhiy 			    &on, sizeof(on)) < 0)
28962db39860SNick Sayer 		    goto pasv_error;
28972db39860SNick Sayer 	}
28982db39860SNick Sayer #endif
289940e9d39eSPeter Wemm 
2900ea022d16SRodney W. Grimes 	pasv_addr = ctrl_addr;
29014dd8b5abSYoshinobu Inoue 	pasv_addr.su_port = 0;
29024dd8b5abSYoshinobu Inoue 	if (bind(pdata, (struct sockaddr *)&pasv_addr, pasv_addr.su_len) < 0)
2903ea022d16SRodney W. Grimes 		goto pasv_error;
290461f891a6SPaul Traina 
290552e7ee74SYaroslav Tykhiy 	(void) seteuid(pw->pw_uid);
29064c450ad7SPaul Traina 
2907ea022d16SRodney W. Grimes 	len = sizeof(pasv_addr);
2908ea022d16SRodney W. Grimes 	if (getsockname(pdata, (struct sockaddr *) &pasv_addr, &len) < 0)
2909ea022d16SRodney W. Grimes 		goto pasv_error;
2910ea022d16SRodney W. Grimes 	if (listen(pdata, 1) < 0)
2911ea022d16SRodney W. Grimes 		goto pasv_error;
29124dd8b5abSYoshinobu Inoue 	if (pasv_addr.su_family == AF_INET)
29134dd8b5abSYoshinobu Inoue 		a = (char *) &pasv_addr.su_sin.sin_addr;
29144dd8b5abSYoshinobu Inoue 	else if (pasv_addr.su_family == AF_INET6 &&
29154dd8b5abSYoshinobu Inoue 		 IN6_IS_ADDR_V4MAPPED(&pasv_addr.su_sin6.sin6_addr))
29164dd8b5abSYoshinobu Inoue 		a = (char *) &pasv_addr.su_sin6.sin6_addr.s6_addr[12];
29174dd8b5abSYoshinobu Inoue 	else
29184dd8b5abSYoshinobu Inoue 		goto pasv_error;
29194dd8b5abSYoshinobu Inoue 
29204dd8b5abSYoshinobu Inoue 	p = (char *) &pasv_addr.su_port;
2921ea022d16SRodney W. Grimes 
2922ea022d16SRodney W. Grimes #define UC(b) (((int) b) & 0xff)
2923ea022d16SRodney W. Grimes 
2924ea022d16SRodney W. Grimes 	reply(227, "Entering Passive Mode (%d,%d,%d,%d,%d,%d)", UC(a[0]),
2925ea022d16SRodney W. Grimes 		UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1]));
2926ea022d16SRodney W. Grimes 	return;
2927ea022d16SRodney W. Grimes 
2928ea022d16SRodney W. Grimes pasv_error:
292952e7ee74SYaroslav Tykhiy 	(void) seteuid(pw->pw_uid);
2930ea022d16SRodney W. Grimes 	(void) close(pdata);
2931ea022d16SRodney W. Grimes 	pdata = -1;
2932ea022d16SRodney W. Grimes 	perror_reply(425, "Can't open passive connection");
2933ea022d16SRodney W. Grimes 	return;
2934ea022d16SRodney W. Grimes }
2935ea022d16SRodney W. Grimes 
2936ea022d16SRodney W. Grimes /*
29374dd8b5abSYoshinobu Inoue  * Long Passive defined in RFC 1639.
29384dd8b5abSYoshinobu Inoue  *     228 Entering Long Passive Mode
29394dd8b5abSYoshinobu Inoue  *         (af, hal, h1, h2, h3,..., pal, p1, p2...)
29404dd8b5abSYoshinobu Inoue  */
29414dd8b5abSYoshinobu Inoue 
29424dd8b5abSYoshinobu Inoue void
2943e4bc453cSWarner Losh long_passive(char *cmd, int pf)
29444dd8b5abSYoshinobu Inoue {
294578e3eed0SStefan Farfeleder 	socklen_t len;
294678e3eed0SStefan Farfeleder 	int on;
29474dd8b5abSYoshinobu Inoue 	char *p, *a;
29484dd8b5abSYoshinobu Inoue 
29494dd8b5abSYoshinobu Inoue 	if (pdata >= 0)		/* close old port if one set */
29504dd8b5abSYoshinobu Inoue 		close(pdata);
29514dd8b5abSYoshinobu Inoue 
29524dd8b5abSYoshinobu Inoue 	if (pf != PF_UNSPEC) {
29534dd8b5abSYoshinobu Inoue 		if (ctrl_addr.su_family != pf) {
29544dd8b5abSYoshinobu Inoue 			switch (ctrl_addr.su_family) {
29554dd8b5abSYoshinobu Inoue 			case AF_INET:
29564dd8b5abSYoshinobu Inoue 				pf = 1;
29574dd8b5abSYoshinobu Inoue 				break;
29584dd8b5abSYoshinobu Inoue 			case AF_INET6:
29594dd8b5abSYoshinobu Inoue 				pf = 2;
29604dd8b5abSYoshinobu Inoue 				break;
29614dd8b5abSYoshinobu Inoue 			default:
29624dd8b5abSYoshinobu Inoue 				pf = 0;
29634dd8b5abSYoshinobu Inoue 				break;
29644dd8b5abSYoshinobu Inoue 			}
29654dd8b5abSYoshinobu Inoue 			/*
29664dd8b5abSYoshinobu Inoue 			 * XXX
29674dd8b5abSYoshinobu Inoue 			 * only EPRT/EPSV ready clients will understand this
29684dd8b5abSYoshinobu Inoue 			 */
29694dd8b5abSYoshinobu Inoue 			if (strcmp(cmd, "EPSV") == 0 && pf) {
29704dd8b5abSYoshinobu Inoue 				reply(522, "Network protocol mismatch, "
29714dd8b5abSYoshinobu Inoue 					"use (%d)", pf);
29724dd8b5abSYoshinobu Inoue 			} else
297302c97492SYaroslav Tykhiy 				reply(501, "Network protocol mismatch."); /*XXX*/
29744dd8b5abSYoshinobu Inoue 
29754dd8b5abSYoshinobu Inoue 			return;
29764dd8b5abSYoshinobu Inoue 		}
29774dd8b5abSYoshinobu Inoue 	}
29784dd8b5abSYoshinobu Inoue 
29794dd8b5abSYoshinobu Inoue 	pdata = socket(ctrl_addr.su_family, SOCK_STREAM, 0);
29804dd8b5abSYoshinobu Inoue 	if (pdata < 0) {
29814dd8b5abSYoshinobu Inoue 		perror_reply(425, "Can't open passive connection");
29824dd8b5abSYoshinobu Inoue 		return;
29834dd8b5abSYoshinobu Inoue 	}
29848af7c9a3SYaroslav Tykhiy 	on = 1;
29858af7c9a3SYaroslav Tykhiy 	if (setsockopt(pdata, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
29868af7c9a3SYaroslav Tykhiy 		syslog(LOG_WARNING, "pdata setsockopt (SO_REUSEADDR): %m");
29874dd8b5abSYoshinobu Inoue 
298852e7ee74SYaroslav Tykhiy 	(void) seteuid(0);
29894dd8b5abSYoshinobu Inoue 
29904dd8b5abSYoshinobu Inoue 	pasv_addr = ctrl_addr;
29914dd8b5abSYoshinobu Inoue 	pasv_addr.su_port = 0;
29924dd8b5abSYoshinobu Inoue 	len = pasv_addr.su_len;
29934dd8b5abSYoshinobu Inoue 
29942db39860SNick Sayer #ifdef IP_PORTRANGE
29952db39860SNick Sayer 	if (ctrl_addr.su_family == AF_INET) {
29968af7c9a3SYaroslav Tykhiy 	    on = restricted_data_ports ? IP_PORTRANGE_HIGH
29972db39860SNick Sayer 				       : IP_PORTRANGE_DEFAULT;
29982db39860SNick Sayer 
29992db39860SNick Sayer 	    if (setsockopt(pdata, IPPROTO_IP, IP_PORTRANGE,
300057d4ef07SYaroslav Tykhiy 			    &on, sizeof(on)) < 0)
30012db39860SNick Sayer 		    goto pasv_error;
30022db39860SNick Sayer 	}
30032db39860SNick Sayer #endif
30042db39860SNick Sayer #ifdef IPV6_PORTRANGE
30052db39860SNick Sayer 	if (ctrl_addr.su_family == AF_INET6) {
30068af7c9a3SYaroslav Tykhiy 	    on = restricted_data_ports ? IPV6_PORTRANGE_HIGH
30072db39860SNick Sayer 				       : IPV6_PORTRANGE_DEFAULT;
30082db39860SNick Sayer 
30092db39860SNick Sayer 	    if (setsockopt(pdata, IPPROTO_IPV6, IPV6_PORTRANGE,
301057d4ef07SYaroslav Tykhiy 			    &on, sizeof(on)) < 0)
30112db39860SNick Sayer 		    goto pasv_error;
30122db39860SNick Sayer 	}
30132db39860SNick Sayer #endif
30142db39860SNick Sayer 
30154dd8b5abSYoshinobu Inoue 	if (bind(pdata, (struct sockaddr *)&pasv_addr, len) < 0)
30164dd8b5abSYoshinobu Inoue 		goto pasv_error;
30174dd8b5abSYoshinobu Inoue 
301852e7ee74SYaroslav Tykhiy 	(void) seteuid(pw->pw_uid);
30194dd8b5abSYoshinobu Inoue 
30204dd8b5abSYoshinobu Inoue 	if (getsockname(pdata, (struct sockaddr *) &pasv_addr, &len) < 0)
30214dd8b5abSYoshinobu Inoue 		goto pasv_error;
30224dd8b5abSYoshinobu Inoue 	if (listen(pdata, 1) < 0)
30234dd8b5abSYoshinobu Inoue 		goto pasv_error;
30244dd8b5abSYoshinobu Inoue 
30254dd8b5abSYoshinobu Inoue #define UC(b) (((int) b) & 0xff)
30264dd8b5abSYoshinobu Inoue 
30274dd8b5abSYoshinobu Inoue 	if (strcmp(cmd, "LPSV") == 0) {
30284dd8b5abSYoshinobu Inoue 		p = (char *)&pasv_addr.su_port;
30294dd8b5abSYoshinobu Inoue 		switch (pasv_addr.su_family) {
30304dd8b5abSYoshinobu Inoue 		case AF_INET:
30314dd8b5abSYoshinobu Inoue 			a = (char *) &pasv_addr.su_sin.sin_addr;
30324dd8b5abSYoshinobu Inoue 		v4_reply:
30334dd8b5abSYoshinobu Inoue 			reply(228,
30344dd8b5abSYoshinobu Inoue "Entering Long Passive Mode (%d,%d,%d,%d,%d,%d,%d,%d,%d)",
30354dd8b5abSYoshinobu Inoue 			      4, 4, UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
30364dd8b5abSYoshinobu Inoue 			      2, UC(p[0]), UC(p[1]));
30374dd8b5abSYoshinobu Inoue 			return;
30384dd8b5abSYoshinobu Inoue 		case AF_INET6:
30394dd8b5abSYoshinobu Inoue 			if (IN6_IS_ADDR_V4MAPPED(&pasv_addr.su_sin6.sin6_addr)) {
30404dd8b5abSYoshinobu Inoue 				a = (char *) &pasv_addr.su_sin6.sin6_addr.s6_addr[12];
30414dd8b5abSYoshinobu Inoue 				goto v4_reply;
30424dd8b5abSYoshinobu Inoue 			}
30434dd8b5abSYoshinobu Inoue 			a = (char *) &pasv_addr.su_sin6.sin6_addr;
30444dd8b5abSYoshinobu Inoue 			reply(228,
30454dd8b5abSYoshinobu Inoue "Entering Long Passive Mode "
30464dd8b5abSYoshinobu Inoue "(%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d)",
30474dd8b5abSYoshinobu Inoue 			      6, 16, UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
30484dd8b5abSYoshinobu Inoue 			      UC(a[4]), UC(a[5]), UC(a[6]), UC(a[7]),
30494dd8b5abSYoshinobu Inoue 			      UC(a[8]), UC(a[9]), UC(a[10]), UC(a[11]),
30504dd8b5abSYoshinobu Inoue 			      UC(a[12]), UC(a[13]), UC(a[14]), UC(a[15]),
30514dd8b5abSYoshinobu Inoue 			      2, UC(p[0]), UC(p[1]));
30524dd8b5abSYoshinobu Inoue 			return;
30534dd8b5abSYoshinobu Inoue 		}
30544dd8b5abSYoshinobu Inoue 	} else if (strcmp(cmd, "EPSV") == 0) {
30554dd8b5abSYoshinobu Inoue 		switch (pasv_addr.su_family) {
30564dd8b5abSYoshinobu Inoue 		case AF_INET:
30574dd8b5abSYoshinobu Inoue 		case AF_INET6:
30584dd8b5abSYoshinobu Inoue 			reply(229, "Entering Extended Passive Mode (|||%d|)",
30594dd8b5abSYoshinobu Inoue 				ntohs(pasv_addr.su_port));
30604dd8b5abSYoshinobu Inoue 			return;
30614dd8b5abSYoshinobu Inoue 		}
30624dd8b5abSYoshinobu Inoue 	} else {
30634dd8b5abSYoshinobu Inoue 		/* more proper error code? */
30644dd8b5abSYoshinobu Inoue 	}
30654dd8b5abSYoshinobu Inoue 
30664dd8b5abSYoshinobu Inoue pasv_error:
306752e7ee74SYaroslav Tykhiy 	(void) seteuid(pw->pw_uid);
30684dd8b5abSYoshinobu Inoue 	(void) close(pdata);
30694dd8b5abSYoshinobu Inoue 	pdata = -1;
30704dd8b5abSYoshinobu Inoue 	perror_reply(425, "Can't open passive connection");
30714dd8b5abSYoshinobu Inoue 	return;
30724dd8b5abSYoshinobu Inoue }
30734dd8b5abSYoshinobu Inoue 
30744dd8b5abSYoshinobu Inoue /*
3075a117c345SYaroslav Tykhiy  * Generate unique name for file with basename "local"
3076a117c345SYaroslav Tykhiy  * and open the file in order to avoid possible races.
3077a117c345SYaroslav Tykhiy  * Try "local" first, then "local.1", "local.2" etc, up to "local.99".
3078a117c345SYaroslav Tykhiy  * Return descriptor to the file, set "name" to its name.
3079a117c345SYaroslav Tykhiy  *
3080ea022d16SRodney W. Grimes  * Generates failure reply on error.
3081ea022d16SRodney W. Grimes  */
3082a117c345SYaroslav Tykhiy static int
3083a117c345SYaroslav Tykhiy guniquefd(char *local, char **name)
3084ea022d16SRodney W. Grimes {
3085ea022d16SRodney W. Grimes 	static char new[MAXPATHLEN];
3086ea022d16SRodney W. Grimes 	struct stat st;
3087ea022d16SRodney W. Grimes 	char *cp;
3088a117c345SYaroslav Tykhiy 	int count;
3089a117c345SYaroslav Tykhiy 	int fd;
3090ea022d16SRodney W. Grimes 
3091ea022d16SRodney W. Grimes 	cp = strrchr(local, '/');
3092ea022d16SRodney W. Grimes 	if (cp)
3093ea022d16SRodney W. Grimes 		*cp = '\0';
3094ea022d16SRodney W. Grimes 	if (stat(cp ? local : ".", &st) < 0) {
3095ea022d16SRodney W. Grimes 		perror_reply(553, cp ? local : ".");
3096a117c345SYaroslav Tykhiy 		return (-1);
3097ea022d16SRodney W. Grimes 	}
3098a117c345SYaroslav Tykhiy 	if (cp) {
3099a117c345SYaroslav Tykhiy 		/*
3100a117c345SYaroslav Tykhiy 		 * Let not overwrite dirname with counter suffix.
3101a117c345SYaroslav Tykhiy 		 * -4 is for /nn\0
3102a117c345SYaroslav Tykhiy 		 * In this extreme case dot won't be put in front of suffix.
3103a117c345SYaroslav Tykhiy 		 */
3104a117c345SYaroslav Tykhiy 		if (strlen(local) > sizeof(new) - 4) {
310502c97492SYaroslav Tykhiy 			reply(553, "Pathname too long.");
3106a117c345SYaroslav Tykhiy 			return (-1);
3107a117c345SYaroslav Tykhiy 		}
3108ea022d16SRodney W. Grimes 		*cp = '/';
3109a117c345SYaroslav Tykhiy 	}
3110e760ef2cSWarner Losh 	/* -4 is for the .nn<null> we put on the end below */
3111e760ef2cSWarner Losh 	(void) snprintf(new, sizeof(new) - 4, "%s", local);
3112ea022d16SRodney W. Grimes 	cp = new + strlen(new);
3113a117c345SYaroslav Tykhiy 	/*
3114a117c345SYaroslav Tykhiy 	 * Don't generate dotfile unless requested explicitly.
3115a117c345SYaroslav Tykhiy 	 * This covers the case when basename gets truncated off
3116a117c345SYaroslav Tykhiy 	 * by buffer size.
3117a117c345SYaroslav Tykhiy 	 */
3118a117c345SYaroslav Tykhiy 	if (cp > new && cp[-1] != '/')
3119ea022d16SRodney W. Grimes 		*cp++ = '.';
3120a117c345SYaroslav Tykhiy 	for (count = 0; count < 100; count++) {
3121a117c345SYaroslav Tykhiy 		/* At count 0 try unmodified name */
3122a117c345SYaroslav Tykhiy 		if (count)
3123ea022d16SRodney W. Grimes 			(void)sprintf(cp, "%d", count);
3124a117c345SYaroslav Tykhiy 		if ((fd = open(count ? new : local,
3125a117c345SYaroslav Tykhiy 		    O_RDWR | O_CREAT | O_EXCL, 0666)) >= 0) {
3126a117c345SYaroslav Tykhiy 			*name = count ? new : local;
3127a117c345SYaroslav Tykhiy 			return (fd);
3128a117c345SYaroslav Tykhiy 		}
312988b70721SYaroslav Tykhiy 		if (errno != EEXIST) {
313050618d61SYaroslav Tykhiy 			perror_reply(553, count ? new : local);
313188b70721SYaroslav Tykhiy 			return (-1);
313288b70721SYaroslav Tykhiy 		}
3133ea022d16SRodney W. Grimes 	}
3134ea022d16SRodney W. Grimes 	reply(452, "Unique file name cannot be created.");
3135a117c345SYaroslav Tykhiy 	return (-1);
3136ea022d16SRodney W. Grimes }
3137ea022d16SRodney W. Grimes 
3138ea022d16SRodney W. Grimes /*
3139ea022d16SRodney W. Grimes  * Format and send reply containing system error number.
3140ea022d16SRodney W. Grimes  */
3141ea022d16SRodney W. Grimes void
3142e4bc453cSWarner Losh perror_reply(int code, char *string)
3143ea022d16SRodney W. Grimes {
3144ea022d16SRodney W. Grimes 
3145ea022d16SRodney W. Grimes 	reply(code, "%s: %s.", string, strerror(errno));
3146ea022d16SRodney W. Grimes }
3147ea022d16SRodney W. Grimes 
3148ea022d16SRodney W. Grimes static char *onefile[] = {
3149ea022d16SRodney W. Grimes 	"",
3150ea022d16SRodney W. Grimes 	0
3151ea022d16SRodney W. Grimes };
3152ea022d16SRodney W. Grimes 
3153ea022d16SRodney W. Grimes void
3154e4bc453cSWarner Losh send_file_list(char *whichf)
3155ea022d16SRodney W. Grimes {
3156ea022d16SRodney W. Grimes 	struct stat st;
3157ea022d16SRodney W. Grimes 	DIR *dirp = NULL;
3158ea022d16SRodney W. Grimes 	struct dirent *dir;
3159ea022d16SRodney W. Grimes 	FILE *dout = NULL;
3160ea022d16SRodney W. Grimes 	char **dirlist, *dirname;
3161ea022d16SRodney W. Grimes 	int simple = 0;
3162ea022d16SRodney W. Grimes 	int freeglob = 0;
3163ea022d16SRodney W. Grimes 	glob_t gl;
3164ea022d16SRodney W. Grimes 
3165ea022d16SRodney W. Grimes 	if (strpbrk(whichf, "~{[*?") != NULL) {
316612da320bSMike Heffner 		int flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_TILDE;
3167ea022d16SRodney W. Grimes 
3168ea022d16SRodney W. Grimes 		memset(&gl, 0, sizeof(gl));
31696d10cb2fSJonathan Lemon 		gl.gl_matchc = MAXGLOBARGS;
317075dc5f1aSMike Heffner 		flags |= GLOB_LIMIT;
3171ea022d16SRodney W. Grimes 		freeglob = 1;
3172ea022d16SRodney W. Grimes 		if (glob(whichf, flags, 0, &gl)) {
317302c97492SYaroslav Tykhiy 			reply(550, "No matching files found.");
3174ea022d16SRodney W. Grimes 			goto out;
3175ea022d16SRodney W. Grimes 		} else if (gl.gl_pathc == 0) {
3176ea022d16SRodney W. Grimes 			errno = ENOENT;
3177ea022d16SRodney W. Grimes 			perror_reply(550, whichf);
3178ea022d16SRodney W. Grimes 			goto out;
3179ea022d16SRodney W. Grimes 		}
3180ea022d16SRodney W. Grimes 		dirlist = gl.gl_pathv;
3181ea022d16SRodney W. Grimes 	} else {
3182ea022d16SRodney W. Grimes 		onefile[0] = whichf;
3183ea022d16SRodney W. Grimes 		dirlist = onefile;
3184ea022d16SRodney W. Grimes 		simple = 1;
3185ea022d16SRodney W. Grimes 	}
3186ea022d16SRodney W. Grimes 
31879aca17cbSMark Murray 	while ((dirname = *dirlist++)) {
3188ea022d16SRodney W. Grimes 		if (stat(dirname, &st) < 0) {
3189ea022d16SRodney W. Grimes 			/*
3190ea022d16SRodney W. Grimes 			 * If user typed "ls -l", etc, and the client
3191ea022d16SRodney W. Grimes 			 * used NLST, do what the user meant.
3192ea022d16SRodney W. Grimes 			 */
3193ea022d16SRodney W. Grimes 			if (dirname[0] == '-' && *dirlist == NULL &&
31944cd51076SYaroslav Tykhiy 			    dout == NULL)
3195af85d782SDavid Nugent 				retrieve(_PATH_LS " %s", dirname);
31964cd51076SYaroslav Tykhiy 			else
3197ea022d16SRodney W. Grimes 				perror_reply(550, whichf);
3198ea022d16SRodney W. Grimes 			goto out;
3199ea022d16SRodney W. Grimes 		}
3200ea022d16SRodney W. Grimes 
3201ea022d16SRodney W. Grimes 		if (S_ISREG(st.st_mode)) {
3202ea022d16SRodney W. Grimes 			if (dout == NULL) {
32030e519c96SYaroslav Tykhiy 				dout = dataconn("file list", -1, "w");
3204ea022d16SRodney W. Grimes 				if (dout == NULL)
3205ea022d16SRodney W. Grimes 					goto out;
32064cd51076SYaroslav Tykhiy 				STARTXFER;
3207ea022d16SRodney W. Grimes 			}
32084cd51076SYaroslav Tykhiy 			START_UNSAFE;
3209ea022d16SRodney W. Grimes 			fprintf(dout, "%s%s\n", dirname,
3210ea022d16SRodney W. Grimes 				type == TYPE_A ? "\r" : "");
32114cd51076SYaroslav Tykhiy 			END_UNSAFE;
32124cd51076SYaroslav Tykhiy 			if (ferror(dout))
32134cd51076SYaroslav Tykhiy 				goto data_err;
32144cd51076SYaroslav Tykhiy 			byte_count += strlen(dirname) +
32154cd51076SYaroslav Tykhiy 				      (type == TYPE_A ? 2 : 1);
32164cd51076SYaroslav Tykhiy 			CHECKOOB(goto abrt);
3217ea022d16SRodney W. Grimes 			continue;
3218ea022d16SRodney W. Grimes 		} else if (!S_ISDIR(st.st_mode))
3219ea022d16SRodney W. Grimes 			continue;
3220ea022d16SRodney W. Grimes 
3221ea022d16SRodney W. Grimes 		if ((dirp = opendir(dirname)) == NULL)
3222ea022d16SRodney W. Grimes 			continue;
3223ea022d16SRodney W. Grimes 
3224ea022d16SRodney W. Grimes 		while ((dir = readdir(dirp)) != NULL) {
3225ea022d16SRodney W. Grimes 			char nbuf[MAXPATHLEN];
3226ea022d16SRodney W. Grimes 
32274cd51076SYaroslav Tykhiy 			CHECKOOB(goto abrt);
32284b82fc95SYaroslav Tykhiy 
3229ea022d16SRodney W. Grimes 			if (dir->d_name[0] == '.' && dir->d_namlen == 1)
3230ea022d16SRodney W. Grimes 				continue;
3231ea022d16SRodney W. Grimes 			if (dir->d_name[0] == '.' && dir->d_name[1] == '.' &&
3232ea022d16SRodney W. Grimes 			    dir->d_namlen == 2)
3233ea022d16SRodney W. Grimes 				continue;
3234ea022d16SRodney W. Grimes 
3235e760ef2cSWarner Losh 			snprintf(nbuf, sizeof(nbuf),
3236e760ef2cSWarner Losh 				"%s/%s", dirname, dir->d_name);
3237ea022d16SRodney W. Grimes 
3238ea022d16SRodney W. Grimes 			/*
3239ea022d16SRodney W. Grimes 			 * We have to do a stat to insure it's
3240ea022d16SRodney W. Grimes 			 * not a directory or special file.
3241ea022d16SRodney W. Grimes 			 */
3242ea022d16SRodney W. Grimes 			if (simple || (stat(nbuf, &st) == 0 &&
3243ea022d16SRodney W. Grimes 			    S_ISREG(st.st_mode))) {
3244ea022d16SRodney W. Grimes 				if (dout == NULL) {
32450e519c96SYaroslav Tykhiy 					dout = dataconn("file list", -1, "w");
3246ea022d16SRodney W. Grimes 					if (dout == NULL)
3247ea022d16SRodney W. Grimes 						goto out;
32484cd51076SYaroslav Tykhiy 					STARTXFER;
3249ea022d16SRodney W. Grimes 				}
32504cd51076SYaroslav Tykhiy 				START_UNSAFE;
3251ea022d16SRodney W. Grimes 				if (nbuf[0] == '.' && nbuf[1] == '/')
3252ea022d16SRodney W. Grimes 					fprintf(dout, "%s%s\n", &nbuf[2],
3253ea022d16SRodney W. Grimes 						type == TYPE_A ? "\r" : "");
3254ea022d16SRodney W. Grimes 				else
3255ea022d16SRodney W. Grimes 					fprintf(dout, "%s%s\n", nbuf,
3256ea022d16SRodney W. Grimes 						type == TYPE_A ? "\r" : "");
32574cd51076SYaroslav Tykhiy 				END_UNSAFE;
32584cd51076SYaroslav Tykhiy 				if (ferror(dout))
32594cd51076SYaroslav Tykhiy 					goto data_err;
32604cd51076SYaroslav Tykhiy 				byte_count += strlen(nbuf) +
32614cd51076SYaroslav Tykhiy 					      (type == TYPE_A ? 2 : 1);
32624cd51076SYaroslav Tykhiy 				CHECKOOB(goto abrt);
3263ea022d16SRodney W. Grimes 			}
3264ea022d16SRodney W. Grimes 		}
3265ea022d16SRodney W. Grimes 		(void) closedir(dirp);
32664cd51076SYaroslav Tykhiy 		dirp = NULL;
3267ea022d16SRodney W. Grimes 	}
3268ea022d16SRodney W. Grimes 
3269ea022d16SRodney W. Grimes 	if (dout == NULL)
3270ea022d16SRodney W. Grimes 		reply(550, "No files found.");
32714cd51076SYaroslav Tykhiy 	else if (ferror(dout))
32724cd51076SYaroslav Tykhiy data_err:	perror_reply(550, "Data connection");
3273ea022d16SRodney W. Grimes 	else
3274ea022d16SRodney W. Grimes 		reply(226, "Transfer complete.");
32754cd51076SYaroslav Tykhiy out:
32764cd51076SYaroslav Tykhiy 	if (dout) {
32774cd51076SYaroslav Tykhiy 		ENDXFER;
32784cd51076SYaroslav Tykhiy abrt:
3279ea022d16SRodney W. Grimes 		(void) fclose(dout);
3280ea022d16SRodney W. Grimes 		data = -1;
3281ea022d16SRodney W. Grimes 		pdata = -1;
32824cd51076SYaroslav Tykhiy 	}
32834cd51076SYaroslav Tykhiy 	if (dirp)
32844cd51076SYaroslav Tykhiy 		(void) closedir(dirp);
3285ea022d16SRodney W. Grimes 	if (freeglob) {
3286ea022d16SRodney W. Grimes 		freeglob = 0;
3287ea022d16SRodney W. Grimes 		globfree(&gl);
3288ea022d16SRodney W. Grimes 	}
3289ea022d16SRodney W. Grimes }
3290ea022d16SRodney W. Grimes 
3291cf09a206SDavid Greenman void
3292e4bc453cSWarner Losh reapchild(int signo)
3293cf09a206SDavid Greenman {
3294de9b6c03SYaroslav Tykhiy 	while (waitpid(-1, NULL, WNOHANG) > 0);
3295cf09a206SDavid Greenman }
3296cf09a206SDavid Greenman 
3297b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE
3298ea022d16SRodney W. Grimes /*
3299ea022d16SRodney W. Grimes  * Clobber argv so ps will show what we're doing.  (Stolen from sendmail.)
3300ea022d16SRodney W. Grimes  * Warning, since this is usually started from inetd.conf, it often doesn't
3301ea022d16SRodney W. Grimes  * have much of an environment or arglist to overwrite.
3302ea022d16SRodney W. Grimes  */
3303ea022d16SRodney W. Grimes void
3304ea022d16SRodney W. Grimes setproctitle(const char *fmt, ...)
3305ea022d16SRodney W. Grimes {
3306ea022d16SRodney W. Grimes 	int i;
3307ea022d16SRodney W. Grimes 	va_list ap;
3308ea022d16SRodney W. Grimes 	char *p, *bp, ch;
3309ea022d16SRodney W. Grimes 	char buf[LINE_MAX];
3310ea022d16SRodney W. Grimes 
3311ea022d16SRodney W. Grimes 	va_start(ap, fmt);
3312ea022d16SRodney W. Grimes 	(void)vsnprintf(buf, sizeof(buf), fmt, ap);
3313ea022d16SRodney W. Grimes 
3314ea022d16SRodney W. Grimes 	/* make ps print our process name */
3315ea022d16SRodney W. Grimes 	p = Argv[0];
3316ea022d16SRodney W. Grimes 	*p++ = '-';
3317ea022d16SRodney W. Grimes 
3318ea022d16SRodney W. Grimes 	i = strlen(buf);
3319ea022d16SRodney W. Grimes 	if (i > LastArgv - p - 2) {
3320ea022d16SRodney W. Grimes 		i = LastArgv - p - 2;
3321ea022d16SRodney W. Grimes 		buf[i] = '\0';
3322ea022d16SRodney W. Grimes 	}
3323ea022d16SRodney W. Grimes 	bp = buf;
3324ea022d16SRodney W. Grimes 	while (ch = *bp++)
3325ea022d16SRodney W. Grimes 		if (ch != '\n' && ch != '\r')
3326ea022d16SRodney W. Grimes 			*p++ = ch;
3327ea022d16SRodney W. Grimes 	while (p < LastArgv)
3328ea022d16SRodney W. Grimes 		*p++ = ' ';
3329ea022d16SRodney W. Grimes }
3330b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */
33313eb568f2SGuido van Rooij 
333261f891a6SPaul Traina static void
33336b2dee6bSYaroslav Tykhiy appendf(char **strp, char *fmt, ...)
33346b2dee6bSYaroslav Tykhiy {
33356b2dee6bSYaroslav Tykhiy 	va_list ap;
33366b2dee6bSYaroslav Tykhiy 	char *ostr, *p;
33376b2dee6bSYaroslav Tykhiy 
33386b2dee6bSYaroslav Tykhiy 	va_start(ap, fmt);
33396b2dee6bSYaroslav Tykhiy 	vasprintf(&p, fmt, ap);
33406b2dee6bSYaroslav Tykhiy 	va_end(ap);
33416b2dee6bSYaroslav Tykhiy 	if (p == NULL)
33426b2dee6bSYaroslav Tykhiy 		fatalerror("Ran out of memory.");
33436b2dee6bSYaroslav Tykhiy 	if (*strp == NULL)
33446b2dee6bSYaroslav Tykhiy 		*strp = p;
33456b2dee6bSYaroslav Tykhiy 	else {
33466b2dee6bSYaroslav Tykhiy 		ostr = *strp;
33476b2dee6bSYaroslav Tykhiy 		asprintf(strp, "%s%s", ostr, p);
33486b2dee6bSYaroslav Tykhiy 		if (*strp == NULL)
33496b2dee6bSYaroslav Tykhiy 			fatalerror("Ran out of memory.");
33506b2dee6bSYaroslav Tykhiy 		free(ostr);
33516b2dee6bSYaroslav Tykhiy 	}
33526b2dee6bSYaroslav Tykhiy }
33536b2dee6bSYaroslav Tykhiy 
33546b2dee6bSYaroslav Tykhiy static void
33556b2dee6bSYaroslav Tykhiy logcmd(char *cmd, char *file1, char *file2, off_t cnt)
33566b2dee6bSYaroslav Tykhiy {
33576b2dee6bSYaroslav Tykhiy 	char *msg = NULL;
33586b2dee6bSYaroslav Tykhiy 	char wd[MAXPATHLEN + 1];
33596b2dee6bSYaroslav Tykhiy 
33606b2dee6bSYaroslav Tykhiy 	if (logging <= 1)
33616b2dee6bSYaroslav Tykhiy 		return;
3362e897216fSYaroslav Tykhiy 
33636b2dee6bSYaroslav Tykhiy 	if (getcwd(wd, sizeof(wd) - 1) == NULL)
33646b2dee6bSYaroslav Tykhiy 		strcpy(wd, strerror(errno));
33656b2dee6bSYaroslav Tykhiy 
33666b2dee6bSYaroslav Tykhiy 	appendf(&msg, "%s", cmd);
33676b2dee6bSYaroslav Tykhiy 	if (file1)
33686b2dee6bSYaroslav Tykhiy 		appendf(&msg, " %s", file1);
33696b2dee6bSYaroslav Tykhiy 	if (file2)
33706b2dee6bSYaroslav Tykhiy 		appendf(&msg, " %s", file2);
33716b2dee6bSYaroslav Tykhiy 	if (cnt >= 0)
33726b2dee6bSYaroslav Tykhiy 		appendf(&msg, " = %jd bytes", (intmax_t)cnt);
3373e897216fSYaroslav Tykhiy 	appendf(&msg, " (wd: %s", wd);
3374215a9f9dSYaroslav Tykhiy 	if (guest || dochroot)
3375e897216fSYaroslav Tykhiy 		appendf(&msg, "; chrooted");
3376e897216fSYaroslav Tykhiy 	appendf(&msg, ")");
33776b2dee6bSYaroslav Tykhiy 	syslog(LOG_INFO, "%s", msg);
33786b2dee6bSYaroslav Tykhiy 	free(msg);
33796b2dee6bSYaroslav Tykhiy }
33806b2dee6bSYaroslav Tykhiy 
33816b2dee6bSYaroslav Tykhiy static void
3382e4bc453cSWarner Losh logxfer(char *name, off_t size, time_t start)
33833eb568f2SGuido van Rooij {
33848c1c21f2SYaroslav Tykhiy 	char buf[MAXPATHLEN + 1024];
33853eb568f2SGuido van Rooij 	char path[MAXPATHLEN + 1];
3386158a00b2SJohn Birrell 	time_t now;
33873eb568f2SGuido van Rooij 
33888c1c21f2SYaroslav Tykhiy 	if (statfd >= 0) {
33893eb568f2SGuido van Rooij 		time(&now);
33908c1c21f2SYaroslav Tykhiy 		if (realpath(name, path) == NULL) {
33918c1c21f2SYaroslav Tykhiy 			syslog(LOG_NOTICE, "realpath failed on %s: %m", path);
33928c1c21f2SYaroslav Tykhiy 			return;
33938c1c21f2SYaroslav Tykhiy 		}
33948c1c21f2SYaroslav Tykhiy 		snprintf(buf, sizeof(buf), "%.20s!%s!%s!%s!%jd!%ld\n",
33953eb568f2SGuido van Rooij 			ctime(&now)+4, ident, remotehost,
33968c1c21f2SYaroslav Tykhiy 			path, (intmax_t)size,
3397e4a71114SAndrey A. Chernov 			(long)(now - start + (now == start)));
33983eb568f2SGuido van Rooij 		write(statfd, buf, strlen(buf));
33993eb568f2SGuido van Rooij 	}
34003eb568f2SGuido van Rooij }
3401e4648f05SYaroslav Tykhiy 
3402e4648f05SYaroslav Tykhiy static char *
3403e4648f05SYaroslav Tykhiy doublequote(char *s)
3404e4648f05SYaroslav Tykhiy {
3405e4648f05SYaroslav Tykhiy 	int n;
3406e4648f05SYaroslav Tykhiy 	char *p, *s2;
3407e4648f05SYaroslav Tykhiy 
3408e4648f05SYaroslav Tykhiy 	for (p = s, n = 0; *p; p++)
3409e4648f05SYaroslav Tykhiy 		if (*p == '"')
3410e4648f05SYaroslav Tykhiy 			n++;
3411e4648f05SYaroslav Tykhiy 
3412e4648f05SYaroslav Tykhiy 	if ((s2 = malloc(p - s + n + 1)) == NULL)
3413e4648f05SYaroslav Tykhiy 		return (NULL);
3414e4648f05SYaroslav Tykhiy 
3415e4648f05SYaroslav Tykhiy 	for (p = s2; *s; s++, p++) {
3416e4648f05SYaroslav Tykhiy 		if ((*p = *s) == '"')
3417e4648f05SYaroslav Tykhiy 			*(++p) = '"';
3418e4648f05SYaroslav Tykhiy 	}
3419e4648f05SYaroslav Tykhiy 	*p = '\0';
3420e4648f05SYaroslav Tykhiy 
3421e4648f05SYaroslav Tykhiy 	return (s2);
3422e4648f05SYaroslav Tykhiy }
3423206fe568SHajimu UMEMOTO 
3424206fe568SHajimu UMEMOTO /* setup server socket for specified address family */
3425206fe568SHajimu UMEMOTO /* if af is PF_UNSPEC more than one socket may be returned */
3426206fe568SHajimu UMEMOTO /* the returned list is dynamically allocated, so caller needs to free it */
3427206fe568SHajimu UMEMOTO static int *
3428206fe568SHajimu UMEMOTO socksetup(int af, char *bindname, const char *bindport)
3429206fe568SHajimu UMEMOTO {
3430206fe568SHajimu UMEMOTO 	struct addrinfo hints, *res, *r;
3431206fe568SHajimu UMEMOTO 	int error, maxs, *s, *socks;
3432206fe568SHajimu UMEMOTO 	const int on = 1;
3433206fe568SHajimu UMEMOTO 
3434206fe568SHajimu UMEMOTO 	memset(&hints, 0, sizeof(hints));
3435206fe568SHajimu UMEMOTO 	hints.ai_flags = AI_PASSIVE;
3436206fe568SHajimu UMEMOTO 	hints.ai_family = af;
3437206fe568SHajimu UMEMOTO 	hints.ai_socktype = SOCK_STREAM;
3438206fe568SHajimu UMEMOTO 	error = getaddrinfo(bindname, bindport, &hints, &res);
3439206fe568SHajimu UMEMOTO 	if (error) {
3440206fe568SHajimu UMEMOTO 		syslog(LOG_ERR, "%s", gai_strerror(error));
3441206fe568SHajimu UMEMOTO 		if (error == EAI_SYSTEM)
3442206fe568SHajimu UMEMOTO 			syslog(LOG_ERR, "%s", strerror(errno));
3443206fe568SHajimu UMEMOTO 		return NULL;
3444206fe568SHajimu UMEMOTO 	}
3445206fe568SHajimu UMEMOTO 
3446206fe568SHajimu UMEMOTO 	/* Count max number of sockets we may open */
3447206fe568SHajimu UMEMOTO 	for (maxs = 0, r = res; r; r = r->ai_next, maxs++)
3448206fe568SHajimu UMEMOTO 		;
3449206fe568SHajimu UMEMOTO 	socks = malloc((maxs + 1) * sizeof(int));
3450206fe568SHajimu UMEMOTO 	if (!socks) {
3451206fe568SHajimu UMEMOTO 		freeaddrinfo(res);
3452206fe568SHajimu UMEMOTO 		syslog(LOG_ERR, "couldn't allocate memory for sockets");
3453206fe568SHajimu UMEMOTO 		return NULL;
3454206fe568SHajimu UMEMOTO 	}
3455206fe568SHajimu UMEMOTO 
3456206fe568SHajimu UMEMOTO 	*socks = 0;   /* num of sockets counter at start of array */
3457206fe568SHajimu UMEMOTO 	s = socks + 1;
3458206fe568SHajimu UMEMOTO 	for (r = res; r; r = r->ai_next) {
3459206fe568SHajimu UMEMOTO 		*s = socket(r->ai_family, r->ai_socktype, r->ai_protocol);
3460206fe568SHajimu UMEMOTO 		if (*s < 0) {
3461206fe568SHajimu UMEMOTO 			syslog(LOG_DEBUG, "control socket: %m");
3462206fe568SHajimu UMEMOTO 			continue;
3463206fe568SHajimu UMEMOTO 		}
3464206fe568SHajimu UMEMOTO 		if (setsockopt(*s, SOL_SOCKET, SO_REUSEADDR,
3465206fe568SHajimu UMEMOTO 		    &on, sizeof(on)) < 0)
3466206fe568SHajimu UMEMOTO 			syslog(LOG_WARNING,
3467206fe568SHajimu UMEMOTO 			    "control setsockopt (SO_REUSEADDR): %m");
3468206fe568SHajimu UMEMOTO 		if (r->ai_family == AF_INET6) {
3469206fe568SHajimu UMEMOTO 			if (setsockopt(*s, IPPROTO_IPV6, IPV6_V6ONLY,
3470206fe568SHajimu UMEMOTO 			    &on, sizeof(on)) < 0)
3471206fe568SHajimu UMEMOTO 				syslog(LOG_WARNING,
3472206fe568SHajimu UMEMOTO 				    "control setsockopt (IPV6_V6ONLY): %m");
3473206fe568SHajimu UMEMOTO 		}
3474206fe568SHajimu UMEMOTO 		if (bind(*s, r->ai_addr, r->ai_addrlen) < 0) {
3475206fe568SHajimu UMEMOTO 			syslog(LOG_DEBUG, "control bind: %m");
3476206fe568SHajimu UMEMOTO 			close(*s);
3477206fe568SHajimu UMEMOTO 			continue;
3478206fe568SHajimu UMEMOTO 		}
3479206fe568SHajimu UMEMOTO 		(*socks)++;
3480206fe568SHajimu UMEMOTO 		s++;
3481206fe568SHajimu UMEMOTO 	}
3482206fe568SHajimu UMEMOTO 
3483206fe568SHajimu UMEMOTO 	if (res)
3484206fe568SHajimu UMEMOTO 		freeaddrinfo(res);
3485206fe568SHajimu UMEMOTO 
3486206fe568SHajimu UMEMOTO 	if (*socks == 0) {
3487206fe568SHajimu UMEMOTO 		syslog(LOG_ERR, "control socket: Couldn't bind to any socket");
3488206fe568SHajimu UMEMOTO 		free(socks);
3489206fe568SHajimu UMEMOTO 		return NULL;
3490206fe568SHajimu UMEMOTO 	}
3491206fe568SHajimu UMEMOTO 	return(socks);
3492206fe568SHajimu UMEMOTO }
3493