xref: /minix3/libexec/ftpd/ftpd.c (revision 5bded7c030da60e0fc007e73f9191ef5aa867ea0)
10a6a1f1dSLionel Sambuc /*	$NetBSD: ftpd.c,v 1.202 2015/08/10 07:32:49 shm Exp $	*/
262da0113SBen Gras 
362da0113SBen Gras /*
462da0113SBen Gras  * Copyright (c) 1997-2009 The NetBSD Foundation, Inc.
562da0113SBen Gras  * All rights reserved.
662da0113SBen Gras  *
762da0113SBen Gras  * This code is derived from software contributed to The NetBSD Foundation
862da0113SBen Gras  * by Luke Mewburn.
962da0113SBen Gras  *
1062da0113SBen Gras  * Redistribution and use in source and binary forms, with or without
1162da0113SBen Gras  * modification, are permitted provided that the following conditions
1262da0113SBen Gras  * are met:
1362da0113SBen Gras  * 1. Redistributions of source code must retain the above copyright
1462da0113SBen Gras  *    notice, this list of conditions and the following disclaimer.
1562da0113SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
1662da0113SBen Gras  *    notice, this list of conditions and the following disclaimer in the
1762da0113SBen Gras  *    documentation and/or other materials provided with the distribution.
1862da0113SBen Gras  *
1962da0113SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2062da0113SBen Gras  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2162da0113SBen Gras  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2262da0113SBen Gras  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2362da0113SBen Gras  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2462da0113SBen Gras  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2562da0113SBen Gras  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2662da0113SBen Gras  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2762da0113SBen Gras  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2862da0113SBen Gras  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2962da0113SBen Gras  * POSSIBILITY OF SUCH DAMAGE.
3062da0113SBen Gras  */
3162da0113SBen Gras 
3262da0113SBen Gras /*
3362da0113SBen Gras  * Copyright (c) 1985, 1988, 1990, 1992, 1993, 1994
3462da0113SBen Gras  *	The Regents of the University of California.  All rights reserved.
3562da0113SBen Gras  *
3662da0113SBen Gras  * Redistribution and use in source and binary forms, with or without
3762da0113SBen Gras  * modification, are permitted provided that the following conditions
3862da0113SBen Gras  * are met:
3962da0113SBen Gras  * 1. Redistributions of source code must retain the above copyright
4062da0113SBen Gras  *    notice, this list of conditions and the following disclaimer.
4162da0113SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
4262da0113SBen Gras  *    notice, this list of conditions and the following disclaimer in the
4362da0113SBen Gras  *    documentation and/or other materials provided with the distribution.
4462da0113SBen Gras  * 3. Neither the name of the University nor the names of its contributors
4562da0113SBen Gras  *    may be used to endorse or promote products derived from this software
4662da0113SBen Gras  *    without specific prior written permission.
4762da0113SBen Gras  *
4862da0113SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
4962da0113SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
5062da0113SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5162da0113SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
5262da0113SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5362da0113SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
5462da0113SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
5562da0113SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
5662da0113SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5762da0113SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5862da0113SBen Gras  * SUCH DAMAGE.
5962da0113SBen Gras  */
6062da0113SBen Gras 
6162da0113SBen Gras /*
6262da0113SBen Gras  * Copyright (C) 1997 and 1998 WIDE Project.
6362da0113SBen Gras  * All rights reserved.
6462da0113SBen Gras  *
6562da0113SBen Gras  * Redistribution and use in source and binary forms, with or without
6662da0113SBen Gras  * modification, are permitted provided that the following conditions
6762da0113SBen Gras  * are met:
6862da0113SBen Gras  * 1. Redistributions of source code must retain the above copyright
6962da0113SBen Gras  *    notice, this list of conditions and the following disclaimer.
7062da0113SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
7162da0113SBen Gras  *    notice, this list of conditions and the following disclaimer in the
7262da0113SBen Gras  *    documentation and/or other materials provided with the distribution.
7362da0113SBen Gras  * 3. Neither the name of the project nor the names of its contributors
7462da0113SBen Gras  *    may be used to endorse or promote products derived from this software
7562da0113SBen Gras  *    without specific prior written permission.
7662da0113SBen Gras  *
7762da0113SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
7862da0113SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
7962da0113SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
8062da0113SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
8162da0113SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
8262da0113SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
8362da0113SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
8462da0113SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
8562da0113SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
8662da0113SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
8762da0113SBen Gras  * SUCH DAMAGE.
8862da0113SBen Gras  */
8962da0113SBen Gras 
9062da0113SBen Gras #include <sys/cdefs.h>
9162da0113SBen Gras #ifndef lint
9262da0113SBen Gras __COPYRIGHT("@(#) Copyright (c) 1985, 1988, 1990, 1992, 1993, 1994\
9362da0113SBen Gras  The Regents of the University of California.  All rights reserved.");
9462da0113SBen Gras #endif /* not lint */
9562da0113SBen Gras 
9662da0113SBen Gras #ifndef lint
9762da0113SBen Gras #if 0
9862da0113SBen Gras static char sccsid[] = "@(#)ftpd.c	8.5 (Berkeley) 4/28/95";
9962da0113SBen Gras #else
1000a6a1f1dSLionel Sambuc __RCSID("$NetBSD: ftpd.c,v 1.202 2015/08/10 07:32:49 shm Exp $");
10162da0113SBen Gras #endif
10262da0113SBen Gras #endif /* not lint */
10362da0113SBen Gras 
10462da0113SBen Gras /*
10562da0113SBen Gras  * FTP server.
10662da0113SBen Gras  */
10762da0113SBen Gras #include <sys/param.h>
10862da0113SBen Gras #include <sys/stat.h>
10962da0113SBen Gras #include <sys/ioctl.h>
11062da0113SBen Gras #include <sys/socket.h>
11162da0113SBen Gras #include <sys/wait.h>
11262da0113SBen Gras #include <sys/mman.h>
11362da0113SBen Gras #include <sys/resource.h>
11462da0113SBen Gras 
11562da0113SBen Gras #include <netinet/in.h>
11662da0113SBen Gras #include <netinet/in_systm.h>
11762da0113SBen Gras #include <netinet/ip.h>
11862da0113SBen Gras 
11962da0113SBen Gras #define	FTP_NAMES
12062da0113SBen Gras #include <arpa/ftp.h>
12162da0113SBen Gras #include <arpa/inet.h>
12262da0113SBen Gras #include <arpa/telnet.h>
12362da0113SBen Gras 
12462da0113SBen Gras #include <ctype.h>
12562da0113SBen Gras #include <dirent.h>
12662da0113SBen Gras #include <err.h>
12762da0113SBen Gras #include <errno.h>
12862da0113SBen Gras #include <fcntl.h>
12962da0113SBen Gras #include <fnmatch.h>
13062da0113SBen Gras #include <glob.h>
13162da0113SBen Gras #include <grp.h>
13262da0113SBen Gras #include <limits.h>
13362da0113SBen Gras #include <netdb.h>
13462da0113SBen Gras #include <pwd.h>
13562da0113SBen Gras #include <poll.h>
13662da0113SBen Gras #include <signal.h>
13762da0113SBen Gras #include <stdarg.h>
13862da0113SBen Gras #include <stdio.h>
13962da0113SBen Gras #include <stdlib.h>
14062da0113SBen Gras #include <string.h>
14162da0113SBen Gras #include <syslog.h>
14262da0113SBen Gras #include <time.h>
14362da0113SBen Gras #include <tzfile.h>
14462da0113SBen Gras #include <unistd.h>
14562da0113SBen Gras #include <util.h>
14662da0113SBen Gras #ifdef SUPPORT_UTMP
14762da0113SBen Gras #include <utmp.h>
14862da0113SBen Gras #endif
14962da0113SBen Gras #ifdef SUPPORT_UTMPX
15062da0113SBen Gras #include <utmpx.h>
15162da0113SBen Gras #endif
15262da0113SBen Gras #ifdef SKEY
15362da0113SBen Gras #include <skey.h>
15462da0113SBen Gras #endif
15562da0113SBen Gras #ifdef KERBEROS5
15662da0113SBen Gras #include <com_err.h>
15762da0113SBen Gras #include <krb5/krb5.h>
15862da0113SBen Gras #endif
15962da0113SBen Gras 
16062da0113SBen Gras #ifdef	LOGIN_CAP
16162da0113SBen Gras #include <login_cap.h>
16262da0113SBen Gras #endif
16362da0113SBen Gras 
16462da0113SBen Gras #ifdef USE_PAM
16562da0113SBen Gras #include <security/pam_appl.h>
16662da0113SBen Gras #endif
16762da0113SBen Gras 
1680a6a1f1dSLionel Sambuc #include "pfilter.h"
1690a6a1f1dSLionel Sambuc 
17062da0113SBen Gras #define	GLOBAL
17162da0113SBen Gras #include "extern.h"
17262da0113SBen Gras #include "pathnames.h"
17362da0113SBen Gras #include "version.h"
17462da0113SBen Gras 
17562da0113SBen Gras static sig_atomic_t	transflag;
17662da0113SBen Gras static sig_atomic_t	urgflag;
17762da0113SBen Gras 
17862da0113SBen Gras int	data;
17962da0113SBen Gras int	Dflag;
18062da0113SBen Gras int	sflag;
18162da0113SBen Gras int	stru;			/* avoid C keyword */
18262da0113SBen Gras int	mode;
18362da0113SBen Gras int	dataport;		/* use specific data port */
18462da0113SBen Gras int	dopidfile;		/* maintain pid file */
18562da0113SBen Gras int	doutmp;			/* update utmp file */
18662da0113SBen Gras int	dowtmp;			/* update wtmp file */
18762da0113SBen Gras int	doxferlog;		/* syslog/write wu-ftpd style xferlog entries */
18862da0113SBen Gras int	xferlogfd;		/* fd to write wu-ftpd xferlog entries to */
18962da0113SBen Gras int	getnameopts;		/* flags for use with getname() */
19062da0113SBen Gras int	dropprivs;		/* if privileges should or have been dropped */
19162da0113SBen Gras int	mapped;			/* IPv4 connection on AF_INET6 socket */
19262da0113SBen Gras off_t	file_size;
19362da0113SBen Gras off_t	byte_count;
19462da0113SBen Gras static char ttyline[20];
19562da0113SBen Gras 
19662da0113SBen Gras #ifdef USE_PAM
19762da0113SBen Gras static int	auth_pam(void);
19862da0113SBen Gras pam_handle_t	*pamh = NULL;
19962da0113SBen Gras #endif
20062da0113SBen Gras 
20162da0113SBen Gras #ifdef SUPPORT_UTMP
20262da0113SBen Gras static struct utmp utmp;	/* for utmp */
20362da0113SBen Gras #endif
20462da0113SBen Gras #ifdef SUPPORT_UTMPX
20562da0113SBen Gras static struct utmpx utmpx;	/* for utmpx */
20662da0113SBen Gras #endif
20762da0113SBen Gras 
20862da0113SBen Gras static const char *anondir = NULL;
20962da0113SBen Gras static const char *confdir = _DEFAULT_CONFDIR;
21062da0113SBen Gras 
21162da0113SBen Gras static char	*curname;		/* current USER name */
21262da0113SBen Gras static size_t	curname_len;		/* length of curname (include NUL) */
21362da0113SBen Gras 
21462da0113SBen Gras #if defined(KERBEROS) || defined(KERBEROS5)
21562da0113SBen Gras int	has_ccache = 0;
21662da0113SBen Gras int	notickets = 1;
21762da0113SBen Gras char	*krbtkfile_env = NULL;
21862da0113SBen Gras char	*tty = ttyline;
21962da0113SBen Gras int	login_krb5_forwardable_tgt = 0;
22062da0113SBen Gras #endif
22162da0113SBen Gras 
22262da0113SBen Gras int epsvall = 0;
22362da0113SBen Gras 
22462da0113SBen Gras /*
22562da0113SBen Gras  * Timeout intervals for retrying connections
22662da0113SBen Gras  * to hosts that don't accept PORT cmds.  This
22762da0113SBen Gras  * is a kludge, but given the problems with TCP...
22862da0113SBen Gras  */
22962da0113SBen Gras #define	SWAITMAX	90	/* wait at most 90 seconds */
23062da0113SBen Gras #define	SWAITINT	5	/* interval between retries */
23162da0113SBen Gras 
23262da0113SBen Gras int	swaitmax = SWAITMAX;
23362da0113SBen Gras int	swaitint = SWAITINT;
23462da0113SBen Gras 
23562da0113SBen Gras enum send_status {
23662da0113SBen Gras 	SS_SUCCESS,
23762da0113SBen Gras 	SS_ABORTED,			/* transfer aborted */
23862da0113SBen Gras 	SS_NO_TRANSFER,			/* no transfer made yet */
23962da0113SBen Gras 	SS_FILE_ERROR,			/* file read error */
24062da0113SBen Gras 	SS_DATA_ERROR			/* data send error */
24162da0113SBen Gras };
24262da0113SBen Gras 
24362da0113SBen Gras static int	 bind_pasv_addr(void);
24462da0113SBen Gras static int	 checkuser(const char *, const char *, int, int, char **);
24562da0113SBen Gras static int	 checkaccess(const char *);
24662da0113SBen Gras static int	 checkpassword(const struct passwd *, const char *);
24762da0113SBen Gras static void	 do_pass(int, int, const char *);
24862da0113SBen Gras static void	 end_login(void);
24962da0113SBen Gras static FILE	*getdatasock(const char *);
25062da0113SBen Gras static char	*gunique(const char *);
25162da0113SBen Gras static void	 login_utmp(const char *, const char *, const char *,
25262da0113SBen Gras 		     struct sockinet *);
25362da0113SBen Gras static void	 logremotehost(struct sockinet *);
25462da0113SBen Gras __dead static void	 lostconn(int);
25562da0113SBen Gras __dead static void	 toolong(int);
25662da0113SBen Gras __dead static void	 sigquit(int);
25762da0113SBen Gras static void	 sigurg(int);
25862da0113SBen Gras static int	 handleoobcmd(void);
25962da0113SBen Gras static int	 receive_data(FILE *, FILE *);
26062da0113SBen Gras static int	 send_data(FILE *, FILE *, const struct stat *, int);
26162da0113SBen Gras static struct passwd *sgetpwnam(const char *);
26262da0113SBen Gras static int	 write_data(int, char *, size_t, off_t *, struct timeval *,
26362da0113SBen Gras 		     int);
26462da0113SBen Gras static enum send_status
26562da0113SBen Gras 		 send_data_with_read(int, int, const struct stat *, int);
26684d9c625SLionel Sambuc #if !defined(__minix)
26762da0113SBen Gras static enum send_status
26862da0113SBen Gras 		 send_data_with_mmap(int, int, const struct stat *, int);
26984d9c625SLionel Sambuc #endif /* !defined(__minix) */
2700a6a1f1dSLionel Sambuc static void	 logrusage(const struct rusage *, const struct rusage *);
27162da0113SBen Gras static void	 logout_utmp(void);
27262da0113SBen Gras 
27362da0113SBen Gras int	main(int, char *[]);
27462da0113SBen Gras 
27562da0113SBen Gras #if defined(KERBEROS)
27662da0113SBen Gras int	klogin(struct passwd *, char *, char *, char *);
27762da0113SBen Gras void	kdestroy(void);
27862da0113SBen Gras #endif
27962da0113SBen Gras #if defined(KERBEROS5)
28062da0113SBen Gras int	k5login(struct passwd *, char *, char *, char *);
28162da0113SBen Gras void	k5destroy(void);
28262da0113SBen Gras #endif
28362da0113SBen Gras 
28462da0113SBen Gras int
main(int argc,char * argv[])28562da0113SBen Gras main(int argc, char *argv[])
28662da0113SBen Gras {
28762da0113SBen Gras 	int		ch, on = 1, tos, keepalive;
28862da0113SBen Gras 	socklen_t	addrlen;
28962da0113SBen Gras #ifdef KERBEROS5
29062da0113SBen Gras 	krb5_error_code	kerror;
29162da0113SBen Gras #endif
29262da0113SBen Gras 	char		*p;
29362da0113SBen Gras 	const char	*xferlogname = NULL;
29462da0113SBen Gras 	long		l;
29562da0113SBen Gras 	struct sigaction sa;
29662da0113SBen Gras 	sa_family_t	af = AF_UNSPEC;
29762da0113SBen Gras 
29862da0113SBen Gras 	connections = 1;
29962da0113SBen Gras 	ftpd_debug = 0;
30062da0113SBen Gras 	logging = 0;
30162da0113SBen Gras 	pdata = -1;
30262da0113SBen Gras 	Dflag = 0;
30362da0113SBen Gras 	sflag = 0;
30462da0113SBen Gras 	dataport = 0;
30562da0113SBen Gras 	dopidfile = 1;		/* default: DO use a pid file to count users */
30662da0113SBen Gras 	doutmp = 0;		/* default: Do NOT log to utmp */
30762da0113SBen Gras 	dowtmp = 1;		/* default: DO log to wtmp */
30862da0113SBen Gras 	doxferlog = 0;		/* default: Do NOT syslog xferlog */
30962da0113SBen Gras 	xferlogfd = -1;		/* default: Do NOT write xferlog file */
31062da0113SBen Gras 	getnameopts = 0;	/* default: xlate addrs to name */
31162da0113SBen Gras 	dropprivs = 0;
31262da0113SBen Gras 	mapped = 0;
31362da0113SBen Gras 	usedefault = 1;
31462da0113SBen Gras 	emailaddr = NULL;
31562da0113SBen Gras 	hostname[0] = '\0';
31662da0113SBen Gras 	homedir[0] = '\0';
31762da0113SBen Gras 	gidcount = 0;
31862da0113SBen Gras 	is_oob = 0;
31962da0113SBen Gras 	version = FTPD_VERSION;
32062da0113SBen Gras 
32162da0113SBen Gras 	/*
32262da0113SBen Gras 	 * LOG_NDELAY sets up the logging connection immediately,
32362da0113SBen Gras 	 * necessary for anonymous ftp's that chroot and can't do it later.
32462da0113SBen Gras 	 */
32562da0113SBen Gras 	openlog("ftpd", LOG_PID | LOG_NDELAY, LOG_FTP);
32662da0113SBen Gras 
32762da0113SBen Gras 	while ((ch = getopt(argc, argv,
32862da0113SBen Gras 	    "46a:c:C:Dde:h:HlL:nP:qQrst:T:uUvV:wWX")) != -1) {
32962da0113SBen Gras 		switch (ch) {
33062da0113SBen Gras 		case '4':
33162da0113SBen Gras 			af = AF_INET;
33262da0113SBen Gras 			break;
33362da0113SBen Gras 
33462da0113SBen Gras 		case '6':
33562da0113SBen Gras 			af = AF_INET6;
33662da0113SBen Gras 			break;
33762da0113SBen Gras 
33862da0113SBen Gras 		case 'a':
33962da0113SBen Gras 			anondir = optarg;
34062da0113SBen Gras 			break;
34162da0113SBen Gras 
34262da0113SBen Gras 		case 'c':
34362da0113SBen Gras 			confdir = optarg;
34462da0113SBen Gras 			break;
34562da0113SBen Gras 
34662da0113SBen Gras 		case 'C':
34762da0113SBen Gras 			if ((p = strchr(optarg, '@')) != NULL) {
34862da0113SBen Gras 				*p++ = '\0';
34962da0113SBen Gras 				strlcpy(remotehost, p, MAXHOSTNAMELEN + 1);
35062da0113SBen Gras 				if (inet_pton(AF_INET, p,
35162da0113SBen Gras 				    &his_addr.su_addr) == 1) {
35262da0113SBen Gras 					his_addr.su_family = AF_INET;
35362da0113SBen Gras 					his_addr.su_len =
35462da0113SBen Gras 					    sizeof(his_addr.si_su.su_sin);
35562da0113SBen Gras #ifdef INET6
35662da0113SBen Gras 				} else if (inet_pton(AF_INET6, p,
35762da0113SBen Gras 				    &his_addr.su_6addr) == 1) {
35862da0113SBen Gras 					his_addr.su_family = AF_INET6;
35962da0113SBen Gras 					his_addr.su_len =
36062da0113SBen Gras 					    sizeof(his_addr.si_su.su_sin6);
36162da0113SBen Gras #endif
36262da0113SBen Gras 				} else
36362da0113SBen Gras 					his_addr.su_family = AF_UNSPEC;
36462da0113SBen Gras 			}
36562da0113SBen Gras 			pw = sgetpwnam(optarg);
36662da0113SBen Gras 			exit(checkaccess(optarg) ? 0 : 1);
36762da0113SBen Gras 			/* NOTREACHED */
36862da0113SBen Gras 
36962da0113SBen Gras 		case 'D':
37062da0113SBen Gras 			Dflag = 1;
37162da0113SBen Gras 			break;
37262da0113SBen Gras 
37362da0113SBen Gras 		case 'd':
37462da0113SBen Gras 		case 'v':		/* deprecated */
37562da0113SBen Gras 			ftpd_debug = 1;
37662da0113SBen Gras 			break;
37762da0113SBen Gras 
37862da0113SBen Gras 		case 'e':
37962da0113SBen Gras 			emailaddr = optarg;
38062da0113SBen Gras 			break;
38162da0113SBen Gras 
38262da0113SBen Gras 		case 'h':
38362da0113SBen Gras 			strlcpy(hostname, optarg, sizeof(hostname));
38462da0113SBen Gras 			break;
38562da0113SBen Gras 
38662da0113SBen Gras 		case 'H':
38762da0113SBen Gras 			if (gethostname(hostname, sizeof(hostname)) == -1)
38862da0113SBen Gras 				hostname[0] = '\0';
38962da0113SBen Gras 			hostname[sizeof(hostname) - 1] = '\0';
39062da0113SBen Gras 			break;
39162da0113SBen Gras 
39262da0113SBen Gras 		case 'l':
39362da0113SBen Gras 			logging++;	/* > 1 == extra logging */
39462da0113SBen Gras 			break;
39562da0113SBen Gras 
39662da0113SBen Gras 		case 'L':
39762da0113SBen Gras 			xferlogname = optarg;
39862da0113SBen Gras 			break;
39962da0113SBen Gras 
40062da0113SBen Gras 		case 'n':
40162da0113SBen Gras 			getnameopts = NI_NUMERICHOST;
40262da0113SBen Gras 			break;
40362da0113SBen Gras 
40462da0113SBen Gras 		case 'P':
40562da0113SBen Gras 			errno = 0;
40662da0113SBen Gras 			p = NULL;
40762da0113SBen Gras 			l = strtol(optarg, &p, 10);
40862da0113SBen Gras 			if (errno || *optarg == '\0' || *p != '\0' ||
40962da0113SBen Gras 			    l < IPPORT_RESERVED ||
41062da0113SBen Gras 			    l > IPPORT_ANONMAX) {
41162da0113SBen Gras 				syslog(LOG_WARNING, "Invalid dataport %s",
41262da0113SBen Gras 				    optarg);
41362da0113SBen Gras 				dataport = 0;
41462da0113SBen Gras 			}
41562da0113SBen Gras 			dataport = (int)l;
41662da0113SBen Gras 			break;
41762da0113SBen Gras 
41862da0113SBen Gras 		case 'q':
41962da0113SBen Gras 			dopidfile = 1;
42062da0113SBen Gras 			break;
42162da0113SBen Gras 
42262da0113SBen Gras 		case 'Q':
42362da0113SBen Gras 			dopidfile = 0;
42462da0113SBen Gras 			break;
42562da0113SBen Gras 
42662da0113SBen Gras 		case 'r':
42762da0113SBen Gras 			dropprivs = 1;
42862da0113SBen Gras 			break;
42962da0113SBen Gras 
43062da0113SBen Gras 		case 's':
43162da0113SBen Gras 			sflag = 1;
43262da0113SBen Gras 			break;
43362da0113SBen Gras 
43462da0113SBen Gras 		case 't':
43562da0113SBen Gras 		case 'T':
43662da0113SBen Gras 			syslog(LOG_WARNING,
43762da0113SBen Gras 			    "-%c has been deprecated in favour of ftpd.conf",
43862da0113SBen Gras 			    ch);
43962da0113SBen Gras 			break;
44062da0113SBen Gras 
44162da0113SBen Gras 		case 'u':
44262da0113SBen Gras 			doutmp = 1;
44362da0113SBen Gras 			break;
44462da0113SBen Gras 
44562da0113SBen Gras 		case 'U':
44662da0113SBen Gras 			doutmp = 0;
44762da0113SBen Gras 			break;
44862da0113SBen Gras 
44962da0113SBen Gras 		case 'V':
45062da0113SBen Gras 			if (EMPTYSTR(optarg) || strcmp(optarg, "-") == 0)
45162da0113SBen Gras 				version = NULL;
45262da0113SBen Gras 			else
45362da0113SBen Gras 				version = ftpd_strdup(optarg);
45462da0113SBen Gras 			break;
45562da0113SBen Gras 
45662da0113SBen Gras 		case 'w':
45762da0113SBen Gras 			dowtmp = 1;
45862da0113SBen Gras 			break;
45962da0113SBen Gras 
46062da0113SBen Gras 		case 'W':
46162da0113SBen Gras 			dowtmp = 0;
46262da0113SBen Gras 			break;
46362da0113SBen Gras 
46462da0113SBen Gras 		case 'X':
46562da0113SBen Gras 			doxferlog |= 1;
46662da0113SBen Gras 			break;
46762da0113SBen Gras 
46862da0113SBen Gras 		default:
46962da0113SBen Gras 			if (optopt == 'a' || optopt == 'C')
47062da0113SBen Gras 				exit(1);
47162da0113SBen Gras 			syslog(LOG_WARNING, "unknown flag -%c ignored", optopt);
47262da0113SBen Gras 			break;
47362da0113SBen Gras 		}
47462da0113SBen Gras 	}
47562da0113SBen Gras 	if (EMPTYSTR(confdir))
47662da0113SBen Gras 		confdir = _DEFAULT_CONFDIR;
47762da0113SBen Gras 
4780a6a1f1dSLionel Sambuc 	pfilter_open();
4790a6a1f1dSLionel Sambuc 
48062da0113SBen Gras 	if (dowtmp) {
48162da0113SBen Gras #ifdef SUPPORT_UTMPX
48262da0113SBen Gras 		ftpd_initwtmpx();
48362da0113SBen Gras #endif
48462da0113SBen Gras #ifdef SUPPORT_UTMP
48562da0113SBen Gras 		ftpd_initwtmp();
48662da0113SBen Gras #endif
48762da0113SBen Gras 	}
48862da0113SBen Gras 	errno = 0;
48984d9c625SLionel Sambuc #if !defined(__minix)
49062da0113SBen Gras 	l = sysconf(_SC_LOGIN_NAME_MAX);
49162da0113SBen Gras 	if (l == -1 && errno != 0) {
49262da0113SBen Gras 		syslog(LOG_ERR, "sysconf _SC_LOGIN_NAME_MAX: %m");
49362da0113SBen Gras 		exit(1);
49462da0113SBen Gras 	} else if (l <= 0) {
49562da0113SBen Gras 		syslog(LOG_WARNING, "using conservative LOGIN_NAME_MAX value");
49662da0113SBen Gras 		curname_len = _POSIX_LOGIN_NAME_MAX;
49762da0113SBen Gras 	} else
49862da0113SBen Gras 		curname_len = (size_t)l;
49962da0113SBen Gras #else
50062da0113SBen Gras 	curname_len = _POSIX_LOGIN_NAME_MAX;
50184d9c625SLionel Sambuc #endif /* !defined(__minix) */
50262da0113SBen Gras 	curname = malloc(curname_len);
50362da0113SBen Gras 	if (curname == NULL) {
50462da0113SBen Gras 		syslog(LOG_ERR, "malloc: %m");
50562da0113SBen Gras 		exit(1);
50662da0113SBen Gras 	}
50762da0113SBen Gras 	curname[0] = '\0';
50862da0113SBen Gras 
50962da0113SBen Gras 	if (Dflag) {
51062da0113SBen Gras 		int error, fd, i, n, *socks;
51162da0113SBen Gras 		struct pollfd *fds;
51262da0113SBen Gras 		struct addrinfo hints, *res, *res0;
51362da0113SBen Gras 
51462da0113SBen Gras 		if (daemon(1, 0) == -1) {
51562da0113SBen Gras 			syslog(LOG_ERR, "failed to daemonize: %m");
51662da0113SBen Gras 			exit(1);
51762da0113SBen Gras 		}
51862da0113SBen Gras 		(void)memset(&sa, 0, sizeof(sa));
51962da0113SBen Gras 		sa.sa_handler = SIG_IGN;
52062da0113SBen Gras 		sa.sa_flags = SA_NOCLDWAIT;
52162da0113SBen Gras 		sigemptyset(&sa.sa_mask);
52262da0113SBen Gras 		(void)sigaction(SIGCHLD, &sa, NULL);
52362da0113SBen Gras 
52462da0113SBen Gras 		(void)memset(&hints, 0, sizeof(hints));
52562da0113SBen Gras 		hints.ai_flags = AI_PASSIVE;
52662da0113SBen Gras 		hints.ai_family = af;
52762da0113SBen Gras 		hints.ai_socktype = SOCK_STREAM;
52862da0113SBen Gras 		error = getaddrinfo(NULL, "ftp", &hints, &res0);
52962da0113SBen Gras 		if (error) {
53062da0113SBen Gras 			syslog(LOG_ERR, "getaddrinfo: %s", gai_strerror(error));
53162da0113SBen Gras 			exit(1);
53262da0113SBen Gras 		}
53362da0113SBen Gras 
53462da0113SBen Gras 		for (n = 0, res = res0; res != NULL; res = res->ai_next)
53562da0113SBen Gras 			n++;
53662da0113SBen Gras 		if (n == 0) {
53762da0113SBen Gras 			syslog(LOG_ERR, "no addresses available");
53862da0113SBen Gras 			exit(1);
53962da0113SBen Gras 		}
54062da0113SBen Gras 		socks = malloc(n * sizeof(int));
54162da0113SBen Gras 		fds = malloc(n * sizeof(struct pollfd));
54262da0113SBen Gras 		if (socks == NULL || fds == NULL) {
54362da0113SBen Gras 			syslog(LOG_ERR, "malloc: %m");
54462da0113SBen Gras 			exit(1);
54562da0113SBen Gras 		}
54662da0113SBen Gras 
54762da0113SBen Gras 		for (n = 0, res = res0; res != NULL; res = res->ai_next) {
54862da0113SBen Gras 			socks[n] = socket(res->ai_family, res->ai_socktype,
54962da0113SBen Gras 			    res->ai_protocol);
55062da0113SBen Gras 			if (socks[n] == -1)
55162da0113SBen Gras 				continue;
55262da0113SBen Gras 			(void)setsockopt(socks[n], SOL_SOCKET, SO_REUSEADDR,
55362da0113SBen Gras 			    &on, sizeof(on));
55462da0113SBen Gras 			if (bind(socks[n], res->ai_addr, res->ai_addrlen)
55562da0113SBen Gras 			    == -1) {
55662da0113SBen Gras 				(void)close(socks[n]);
55762da0113SBen Gras 				continue;
55862da0113SBen Gras 			}
55962da0113SBen Gras 			if (listen(socks[n], 12) == -1) {
56062da0113SBen Gras 				(void)close(socks[n]);
56162da0113SBen Gras 				continue;
56262da0113SBen Gras 			}
56362da0113SBen Gras 
56462da0113SBen Gras 			fds[n].fd = socks[n];
56562da0113SBen Gras 			fds[n].events = POLLIN;
56662da0113SBen Gras 			n++;
56762da0113SBen Gras 		}
56862da0113SBen Gras 		if (n == 0) {
56962da0113SBen Gras 			syslog(LOG_ERR, "%m");
57062da0113SBen Gras 			exit(1);
57162da0113SBen Gras 		}
57262da0113SBen Gras 		freeaddrinfo(res0);
57362da0113SBen Gras 
57462da0113SBen Gras 		if (pidfile(NULL) == -1)
57562da0113SBen Gras 			syslog(LOG_ERR, "failed to write a pid file: %m");
57662da0113SBen Gras 
57762da0113SBen Gras 		for (;;) {
578*5bded7c0SDavid van Moolenbroek #ifdef __minix
579*5bded7c0SDavid van Moolenbroek 			/*
580*5bded7c0SDavid van Moolenbroek 			 * MINIX 3 does not yet support SA_NOCLDWAIT.  Cleaning
581*5bded7c0SDavid van Moolenbroek 			 * up zombies this way is not perfect, but at least it
582*5bded7c0SDavid van Moolenbroek 			 * prevents accumulation of zombies over time.
583*5bded7c0SDavid van Moolenbroek 			 */
584*5bded7c0SDavid van Moolenbroek 			while (waitpid(-1, NULL, WNOHANG) > 0)
585*5bded7c0SDavid van Moolenbroek 				;
586*5bded7c0SDavid van Moolenbroek #endif /* __minix */
58762da0113SBen Gras 			if (poll(fds, n, INFTIM) == -1) {
58862da0113SBen Gras 				if (errno == EINTR)
58962da0113SBen Gras 					continue;
59062da0113SBen Gras 				syslog(LOG_ERR, "poll: %m");
59162da0113SBen Gras 				exit(1);
59262da0113SBen Gras 			}
59362da0113SBen Gras 			for (i = 0; i < n; i++) {
59462da0113SBen Gras 				if (fds[i].revents & POLLIN) {
59562da0113SBen Gras 					fd = accept(fds[i].fd, NULL, NULL);
59662da0113SBen Gras 					if (fd == -1) {
59762da0113SBen Gras 						syslog(LOG_ERR, "accept: %m");
59862da0113SBen Gras 						continue;
59962da0113SBen Gras 					}
60062da0113SBen Gras 					switch (fork()) {
60162da0113SBen Gras 					case -1:
60262da0113SBen Gras 						syslog(LOG_ERR, "fork: %m");
60362da0113SBen Gras 						break;
60462da0113SBen Gras 					case 0:
60562da0113SBen Gras 						goto child;
60662da0113SBen Gras 						/* NOTREACHED */
60762da0113SBen Gras 					}
60862da0113SBen Gras 					(void)close(fd);
60962da0113SBen Gras 				}
61062da0113SBen Gras 			}
61162da0113SBen Gras 		}
61262da0113SBen Gras  child:
61362da0113SBen Gras 		(void)dup2(fd, STDIN_FILENO);
61462da0113SBen Gras 		(void)dup2(fd, STDOUT_FILENO);
61562da0113SBen Gras 		(void)dup2(fd, STDERR_FILENO);
61662da0113SBen Gras 		for (i = 0; i < n; i++)
61762da0113SBen Gras 			(void)close(socks[i]);
61862da0113SBen Gras 	}
61962da0113SBen Gras 
62062da0113SBen Gras 	memset((char *)&his_addr, 0, sizeof(his_addr));
62162da0113SBen Gras 	addrlen = sizeof(his_addr.si_su);
62262da0113SBen Gras 	if (getpeername(0, (struct sockaddr *)&his_addr.si_su, &addrlen) < 0) {
62362da0113SBen Gras 		syslog((errno == ENOTCONN) ? LOG_NOTICE : LOG_ERR,
62462da0113SBen Gras 		    "getpeername (%s): %m",argv[0]);
62562da0113SBen Gras 		exit(1);
62662da0113SBen Gras 	}
62762da0113SBen Gras 	his_addr.su_len = addrlen;
62862da0113SBen Gras 	memset((char *)&ctrl_addr, 0, sizeof(ctrl_addr));
62962da0113SBen Gras 	addrlen = sizeof(ctrl_addr.si_su);
63062da0113SBen Gras 	if (getsockname(0, (struct sockaddr *)&ctrl_addr, &addrlen) < 0) {
63162da0113SBen Gras 		syslog(LOG_ERR, "getsockname (%s): %m",argv[0]);
63262da0113SBen Gras 		exit(1);
63362da0113SBen Gras 	}
63462da0113SBen Gras 	ctrl_addr.su_len = addrlen;
63562da0113SBen Gras #ifdef INET6
63662da0113SBen Gras 	if (his_addr.su_family == AF_INET6
63762da0113SBen Gras 	 && IN6_IS_ADDR_V4MAPPED(&his_addr.su_6addr)) {
63862da0113SBen Gras #if 1
63962da0113SBen Gras 		/*
64062da0113SBen Gras 		 * IPv4 control connection arrived to AF_INET6 socket.
64162da0113SBen Gras 		 * I hate to do this, but this is the easiest solution.
64262da0113SBen Gras 		 *
64362da0113SBen Gras 		 * The assumption is untrue on SIIT environment.
64462da0113SBen Gras 		 */
64562da0113SBen Gras 		struct sockinet tmp_addr;
64662da0113SBen Gras 		const int off = sizeof(struct in6_addr) - sizeof(struct in_addr);
64762da0113SBen Gras 
64862da0113SBen Gras 		tmp_addr = his_addr;
64962da0113SBen Gras 		memset(&his_addr, 0, sizeof(his_addr));
65062da0113SBen Gras 		his_addr.su_family = AF_INET;
65162da0113SBen Gras 		his_addr.su_len = sizeof(his_addr.si_su.su_sin);
65262da0113SBen Gras 		memcpy(&his_addr.su_addr, &tmp_addr.su_6addr.s6_addr[off],
65362da0113SBen Gras 		    sizeof(his_addr.su_addr));
65462da0113SBen Gras 		his_addr.su_port = tmp_addr.su_port;
65562da0113SBen Gras 
65662da0113SBen Gras 		tmp_addr = ctrl_addr;
65762da0113SBen Gras 		memset(&ctrl_addr, 0, sizeof(ctrl_addr));
65862da0113SBen Gras 		ctrl_addr.su_family = AF_INET;
65962da0113SBen Gras 		ctrl_addr.su_len = sizeof(ctrl_addr.si_su.su_sin);
66062da0113SBen Gras 		memcpy(&ctrl_addr.su_addr, &tmp_addr.su_6addr.s6_addr[off],
66162da0113SBen Gras 		    sizeof(ctrl_addr.su_addr));
66262da0113SBen Gras 		ctrl_addr.su_port = tmp_addr.su_port;
66362da0113SBen Gras #else
66462da0113SBen Gras 		while (fgets(line, sizeof(line), fd) != NULL) {
66562da0113SBen Gras 			if ((cp = strchr(line, '\n')) != NULL)
66662da0113SBen Gras 				*cp = '\0';
66762da0113SBen Gras 			reply(-530, "%s", line);
66862da0113SBen Gras 		}
66962da0113SBen Gras 		(void) fflush(stdout);
67062da0113SBen Gras 		(void) fclose(fd);
67162da0113SBen Gras 		reply(530,
67262da0113SBen Gras 		    "Connection from IPv4 mapped address is not supported.");
67362da0113SBen Gras 		exit(0);
67462da0113SBen Gras #endif
67562da0113SBen Gras 
67662da0113SBen Gras 		mapped = 1;
67762da0113SBen Gras 	} else
67862da0113SBen Gras #endif /* INET6 */
67962da0113SBen Gras 		mapped = 0;
68062da0113SBen Gras #ifdef IP_TOS
68162da0113SBen Gras 	if (!mapped && his_addr.su_family == AF_INET) {
68262da0113SBen Gras 		tos = IPTOS_LOWDELAY;
68362da0113SBen Gras 		if (setsockopt(0, IPPROTO_IP, IP_TOS, (char *)&tos,
68462da0113SBen Gras 			       sizeof(int)) < 0)
68562da0113SBen Gras 			syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
68662da0113SBen Gras 	}
68762da0113SBen Gras #endif
68862da0113SBen Gras 	/* if the hostname hasn't been given, attempt to determine it */
68962da0113SBen Gras 	if (hostname[0] == '\0') {
69062da0113SBen Gras 		if (getnameinfo((struct sockaddr *)&ctrl_addr.si_su,
69162da0113SBen Gras 		    ctrl_addr.su_len, hostname, sizeof(hostname), NULL, 0,
69262da0113SBen Gras 			getnameopts) != 0)
69362da0113SBen Gras 			(void)gethostname(hostname, sizeof(hostname));
69462da0113SBen Gras 		hostname[sizeof(hostname) - 1] = '\0';
69562da0113SBen Gras 	}
69662da0113SBen Gras 
69762da0113SBen Gras 	/* set this here so klogin can use it... */
69862da0113SBen Gras 	(void)snprintf(ttyline, sizeof(ttyline), "ftp%d", getpid());
69962da0113SBen Gras 
70062da0113SBen Gras 	(void) freopen(_PATH_DEVNULL, "w", stderr);
70162da0113SBen Gras 
70262da0113SBen Gras 	memset(&sa, 0, sizeof(sa));
70362da0113SBen Gras 	sa.sa_handler = SIG_DFL;
70462da0113SBen Gras 	sa.sa_flags = SA_RESTART;
70562da0113SBen Gras 	sigemptyset(&sa.sa_mask);
70662da0113SBen Gras 	(void) sigaction(SIGCHLD, &sa, NULL);
70762da0113SBen Gras 
70862da0113SBen Gras 	sa.sa_handler = sigquit;
70962da0113SBen Gras 	sa.sa_flags = SA_RESTART;
71062da0113SBen Gras 	sigfillset(&sa.sa_mask);	/* block all sigs in these handlers */
71162da0113SBen Gras 	(void) sigaction(SIGHUP, &sa, NULL);
71262da0113SBen Gras 	(void) sigaction(SIGINT, &sa, NULL);
71362da0113SBen Gras 	(void) sigaction(SIGQUIT, &sa, NULL);
71462da0113SBen Gras 	(void) sigaction(SIGTERM, &sa, NULL);
71562da0113SBen Gras 	sa.sa_handler = lostconn;
71662da0113SBen Gras 	(void) sigaction(SIGPIPE, &sa, NULL);
71762da0113SBen Gras 	sa.sa_handler = toolong;
71862da0113SBen Gras 	(void) sigaction(SIGALRM, &sa, NULL);
71962da0113SBen Gras 	sa.sa_handler = sigurg;
72062da0113SBen Gras 	(void) sigaction(SIGURG, &sa, NULL);
72162da0113SBen Gras 
72262da0113SBen Gras 	/* Try to handle urgent data inline */
72362da0113SBen Gras #ifdef SO_OOBINLINE
72462da0113SBen Gras 	if (setsockopt(0, SOL_SOCKET, SO_OOBINLINE, (char *)&on, sizeof(on)) < 0)
72562da0113SBen Gras 		syslog(LOG_WARNING, "setsockopt: %m");
72662da0113SBen Gras #endif
72762da0113SBen Gras 	/* Set keepalives on the socket to detect dropped connections.  */
72862da0113SBen Gras #ifdef SO_KEEPALIVE
72962da0113SBen Gras 	keepalive = 1;
73062da0113SBen Gras 	if (setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, (char *)&keepalive,
73162da0113SBen Gras 	    sizeof(int)) < 0)
73262da0113SBen Gras 		syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
73362da0113SBen Gras #endif
73462da0113SBen Gras 
73562da0113SBen Gras #ifdef	F_SETOWN
73662da0113SBen Gras 	if (fcntl(fileno(stdin), F_SETOWN, getpid()) == -1)
73762da0113SBen Gras 		syslog(LOG_WARNING, "fcntl F_SETOWN: %m");
73862da0113SBen Gras #endif
73962da0113SBen Gras 	logremotehost(&his_addr);
74062da0113SBen Gras 	/*
74162da0113SBen Gras 	 * Set up default state
74262da0113SBen Gras 	 */
74362da0113SBen Gras 	data = -1;
74462da0113SBen Gras 	type = TYPE_A;
74562da0113SBen Gras 	form = FORM_N;
74662da0113SBen Gras 	stru = STRU_F;
74762da0113SBen Gras 	mode = MODE_S;
74862da0113SBen Gras 	tmpline[0] = '\0';
74962da0113SBen Gras 	hasyyerrored = 0;
75062da0113SBen Gras 
75162da0113SBen Gras #ifdef KERBEROS5
75262da0113SBen Gras 	kerror = krb5_init_context(&kcontext);
75362da0113SBen Gras 	if (kerror) {
75462da0113SBen Gras 		syslog(LOG_ERR, "%s when initializing Kerberos context",
75562da0113SBen Gras 		    error_message(kerror));
75662da0113SBen Gras 		exit(0);
75762da0113SBen Gras 	}
75862da0113SBen Gras #endif /* KERBEROS5 */
75962da0113SBen Gras 
76062da0113SBen Gras 	init_curclass();
76162da0113SBen Gras 	curclass.timeout = 300;		/* 5 minutes, as per login(1) */
76262da0113SBen Gras 	curclass.type = CLASS_REAL;
76362da0113SBen Gras 
76462da0113SBen Gras 	/* If logins are disabled, print out the message. */
76562da0113SBen Gras 	if (display_file(_PATH_NOLOGIN, 530)) {
76662da0113SBen Gras 		reply(530, "System not available.");
76762da0113SBen Gras 		exit(0);
76862da0113SBen Gras 	}
76962da0113SBen Gras 	(void)display_file(conffilename(_NAME_FTPWELCOME), 220);
77062da0113SBen Gras 		/* reply(220,) must follow */
77162da0113SBen Gras 	if (EMPTYSTR(version))
77262da0113SBen Gras 		reply(220, "%s FTP server ready.", hostname);
77362da0113SBen Gras 	else
77462da0113SBen Gras 		reply(220, "%s FTP server (%s) ready.", hostname, version);
77562da0113SBen Gras 
77662da0113SBen Gras 	if (xferlogname != NULL) {
77762da0113SBen Gras 		xferlogfd = open(xferlogname, O_WRONLY | O_APPEND | O_CREAT,
77862da0113SBen Gras 		    0660);
77962da0113SBen Gras 		if (xferlogfd == -1)
78062da0113SBen Gras 			syslog(LOG_WARNING, "open xferlog `%s': %m",
78162da0113SBen Gras 			    xferlogname);
78262da0113SBen Gras 		else
78362da0113SBen Gras 			doxferlog |= 2;
78462da0113SBen Gras 	}
78562da0113SBen Gras 
78662da0113SBen Gras 	ftp_loop();
78762da0113SBen Gras 	/* NOTREACHED */
78862da0113SBen Gras }
78962da0113SBen Gras 
79062da0113SBen Gras static void
lostconn(int signo __unused)79162da0113SBen Gras lostconn(int signo __unused)
79262da0113SBen Gras {
79362da0113SBen Gras 
79462da0113SBen Gras 	if (ftpd_debug)
79562da0113SBen Gras 		syslog(LOG_DEBUG, "lost connection");
79662da0113SBen Gras 	dologout(1);
79762da0113SBen Gras }
79862da0113SBen Gras 
79962da0113SBen Gras static void
toolong(int signo __unused)80062da0113SBen Gras toolong(int signo __unused)
80162da0113SBen Gras {
80262da0113SBen Gras 
80362da0113SBen Gras 		/* XXXSIGRACE */
80462da0113SBen Gras 	reply(421,
80562da0113SBen Gras 	    "Timeout (" LLF " seconds): closing control connection.",
80662da0113SBen Gras 	    (LLT)curclass.timeout);
80762da0113SBen Gras 	if (logging)
80862da0113SBen Gras 		syslog(LOG_INFO, "User %s timed out after " LLF " seconds",
80962da0113SBen Gras 		    (pw ? pw->pw_name : "unknown"), (LLT)curclass.timeout);
81062da0113SBen Gras 	dologout(1);
81162da0113SBen Gras }
81262da0113SBen Gras 
81362da0113SBen Gras static void
sigquit(int signo)81462da0113SBen Gras sigquit(int signo)
81562da0113SBen Gras {
81662da0113SBen Gras 
81762da0113SBen Gras 	if (ftpd_debug)
81862da0113SBen Gras 		syslog(LOG_DEBUG, "got signal %d", signo);
81962da0113SBen Gras 	dologout(1);
82062da0113SBen Gras }
82162da0113SBen Gras 
82262da0113SBen Gras static void
sigurg(int signo __unused)82362da0113SBen Gras sigurg(int signo __unused)
82462da0113SBen Gras {
82562da0113SBen Gras 
82662da0113SBen Gras 	urgflag = 1;
82762da0113SBen Gras }
82862da0113SBen Gras 
82962da0113SBen Gras 
83062da0113SBen Gras /*
83162da0113SBen Gras  * Save the result of a getpwnam.  Used for USER command, since
83262da0113SBen Gras  * the data returned must not be clobbered by any other command
83362da0113SBen Gras  * (e.g., globbing).
83462da0113SBen Gras  */
83562da0113SBen Gras static struct passwd *
sgetpwnam(const char * name)83662da0113SBen Gras sgetpwnam(const char *name)
83762da0113SBen Gras {
83862da0113SBen Gras 	static struct passwd save;
83962da0113SBen Gras 	struct passwd *p;
84062da0113SBen Gras 
84162da0113SBen Gras 	if ((p = getpwnam(name)) == NULL)
84262da0113SBen Gras 		return (p);
84362da0113SBen Gras 	if (save.pw_name) {
84462da0113SBen Gras 		free((char *)save.pw_name);
84562da0113SBen Gras 		memset(save.pw_passwd, 0, strlen(save.pw_passwd));
84662da0113SBen Gras 		free((char *)save.pw_passwd);
84762da0113SBen Gras 		free((char *)save.pw_gecos);
84862da0113SBen Gras 		free((char *)save.pw_dir);
84962da0113SBen Gras 		free((char *)save.pw_shell);
85062da0113SBen Gras 	}
85162da0113SBen Gras 	save = *p;
85262da0113SBen Gras 	save.pw_name = ftpd_strdup(p->pw_name);
85362da0113SBen Gras 	save.pw_passwd = ftpd_strdup(p->pw_passwd);
85462da0113SBen Gras 	save.pw_gecos = ftpd_strdup(p->pw_gecos);
85562da0113SBen Gras 	save.pw_dir = ftpd_strdup(p->pw_dir);
85662da0113SBen Gras 	save.pw_shell = ftpd_strdup(p->pw_shell);
85762da0113SBen Gras 	return (&save);
85862da0113SBen Gras }
85962da0113SBen Gras 
86062da0113SBen Gras static int	login_attempts;	/* number of failed login attempts */
86162da0113SBen Gras static int	askpasswd;	/* had USER command, ask for PASSwd */
86262da0113SBen Gras static int	permitted;	/* USER permitted */
86362da0113SBen Gras 
86462da0113SBen Gras /*
86562da0113SBen Gras  * USER command.
86662da0113SBen Gras  * Sets global passwd pointer pw if named account exists and is acceptable;
86762da0113SBen Gras  * sets askpasswd if a PASS command is expected.  If logged in previously,
86862da0113SBen Gras  * need to reset state.  If name is "ftp" or "anonymous", the name is not in
86962da0113SBen Gras  * _NAME_FTPUSERS, and ftp account exists, set guest and pw, then just return.
87062da0113SBen Gras  * If account doesn't exist, ask for passwd anyway.  Otherwise, check user
87162da0113SBen Gras  * requesting login privileges.  Disallow anyone who does not have a standard
87262da0113SBen Gras  * shell as returned by getusershell().  Disallow anyone mentioned in the file
87362da0113SBen Gras  * _NAME_FTPUSERS to allow people such as root and uucp to be avoided.
87462da0113SBen Gras  */
87562da0113SBen Gras void
user(const char * name)87662da0113SBen Gras user(const char *name)
87762da0113SBen Gras {
87862da0113SBen Gras 	char	*class;
87962da0113SBen Gras #ifdef	LOGIN_CAP
88062da0113SBen Gras 	login_cap_t *lc = NULL;
88162da0113SBen Gras #endif
88262da0113SBen Gras #ifdef USE_PAM
88362da0113SBen Gras 	int e;
88462da0113SBen Gras #endif
88562da0113SBen Gras 
88662da0113SBen Gras 	class = NULL;
88762da0113SBen Gras 	if (logged_in) {
88862da0113SBen Gras 		switch (curclass.type) {
88962da0113SBen Gras 		case CLASS_GUEST:
89062da0113SBen Gras 			reply(530, "Can't change user from guest login.");
89162da0113SBen Gras 			return;
89262da0113SBen Gras 		case CLASS_CHROOT:
89362da0113SBen Gras 			reply(530, "Can't change user from chroot user.");
89462da0113SBen Gras 			return;
89562da0113SBen Gras 		case CLASS_REAL:
89662da0113SBen Gras 			if (dropprivs) {
89762da0113SBen Gras 				reply(530, "Can't change user.");
89862da0113SBen Gras 				return;
89962da0113SBen Gras 			}
90062da0113SBen Gras 			end_login();
90162da0113SBen Gras 			break;
90262da0113SBen Gras 		default:
90362da0113SBen Gras 			abort();
90462da0113SBen Gras 		}
90562da0113SBen Gras 	}
90662da0113SBen Gras 
90762da0113SBen Gras #if defined(KERBEROS)
90862da0113SBen Gras 	kdestroy();
90962da0113SBen Gras #endif
91062da0113SBen Gras #if defined(KERBEROS5)
91162da0113SBen Gras 	k5destroy();
91262da0113SBen Gras #endif
91362da0113SBen Gras 
91462da0113SBen Gras 	curclass.type = CLASS_REAL;
91562da0113SBen Gras 	askpasswd = 0;
91662da0113SBen Gras 	permitted = 0;
91762da0113SBen Gras 
91862da0113SBen Gras 	if (strcmp(name, "ftp") == 0 || strcmp(name, "anonymous") == 0) {
91962da0113SBen Gras 			/* need `pw' setup for checkaccess() and checkuser () */
92062da0113SBen Gras 		if ((pw = sgetpwnam("ftp")) == NULL)
92162da0113SBen Gras 			reply(530, "User %s unknown.", name);
92262da0113SBen Gras 		else if (! checkaccess("ftp") || ! checkaccess("anonymous"))
92362da0113SBen Gras 			reply(530, "User %s access denied.", name);
92462da0113SBen Gras 		else {
92562da0113SBen Gras 			curclass.type = CLASS_GUEST;
92662da0113SBen Gras 			askpasswd = 1;
92762da0113SBen Gras 			reply(331,
92862da0113SBen Gras 			    "Guest login ok, type your name as password.");
92962da0113SBen Gras 		}
93062da0113SBen Gras 		if (!askpasswd) {
93162da0113SBen Gras 			if (logging)
93262da0113SBen Gras 				syslog(LOG_NOTICE,
93362da0113SBen Gras 				    "ANONYMOUS FTP LOGIN REFUSED FROM %s",
93462da0113SBen Gras 				    remoteloghost);
93562da0113SBen Gras 			end_login();
93662da0113SBen Gras 			goto cleanup_user;
93762da0113SBen Gras 		}
93862da0113SBen Gras 		name = "ftp";
93962da0113SBen Gras 	} else
94062da0113SBen Gras 		pw = sgetpwnam(name);
94162da0113SBen Gras 
94262da0113SBen Gras 	strlcpy(curname, name, curname_len);
94362da0113SBen Gras 
94462da0113SBen Gras 			/* check user in /etc/ftpusers, and setup class */
94562da0113SBen Gras 	permitted = checkuser(_NAME_FTPUSERS, curname, 1, 0, &class);
94662da0113SBen Gras 
94762da0113SBen Gras 			/* check user in /etc/ftpchroot */
94862da0113SBen Gras #ifdef	LOGIN_CAP
94962da0113SBen Gras 	lc = login_getpwclass(pw);
95062da0113SBen Gras #endif
95162da0113SBen Gras 	if (checkuser(_NAME_FTPCHROOT, curname, 0, 0, NULL)
95262da0113SBen Gras #ifdef	LOGIN_CAP	/* Allow login.conf configuration as well */
95362da0113SBen Gras 	    || login_getcapbool(lc, "ftp-chroot", 0)
95462da0113SBen Gras #endif
95562da0113SBen Gras 	) {
95662da0113SBen Gras 		if (curclass.type == CLASS_GUEST) {
95762da0113SBen Gras 			syslog(LOG_NOTICE,
95862da0113SBen Gras 	    "Can't change guest user to chroot class; remove entry in %s",
95962da0113SBen Gras 			    _NAME_FTPCHROOT);
96062da0113SBen Gras 			exit(1);
96162da0113SBen Gras 		}
96262da0113SBen Gras 		curclass.type = CLASS_CHROOT;
96362da0113SBen Gras 	}
96462da0113SBen Gras 
96562da0113SBen Gras 			/* determine default class */
96662da0113SBen Gras 	if (class == NULL) {
96762da0113SBen Gras 		switch (curclass.type) {
96862da0113SBen Gras 		case CLASS_GUEST:
96962da0113SBen Gras 			class = ftpd_strdup("guest");
97062da0113SBen Gras 			break;
97162da0113SBen Gras 		case CLASS_CHROOT:
97262da0113SBen Gras 			class = ftpd_strdup("chroot");
97362da0113SBen Gras 			break;
97462da0113SBen Gras 		case CLASS_REAL:
97562da0113SBen Gras 			class = ftpd_strdup("real");
97662da0113SBen Gras 			break;
97762da0113SBen Gras 		default:
97862da0113SBen Gras 			syslog(LOG_ERR, "unknown curclass.type %d; aborting",
97962da0113SBen Gras 			    curclass.type);
98062da0113SBen Gras 			abort();
98162da0113SBen Gras 		}
98262da0113SBen Gras 	}
98362da0113SBen Gras 			/* parse ftpd.conf, setting up various parameters */
98462da0113SBen Gras 	parse_conf(class);
98562da0113SBen Gras 			/* if not guest user, check for valid shell */
98662da0113SBen Gras 	if (pw == NULL)
98762da0113SBen Gras 		permitted = 0;
98862da0113SBen Gras 	else {
98962da0113SBen Gras 		const char	*cp, *shell;
99062da0113SBen Gras 
99162da0113SBen Gras 		if ((shell = pw->pw_shell) == NULL || *shell == 0)
99262da0113SBen Gras 			shell = _PATH_BSHELL;
99362da0113SBen Gras 		while ((cp = getusershell()) != NULL)
99462da0113SBen Gras 			if (strcmp(cp, shell) == 0)
99562da0113SBen Gras 				break;
99662da0113SBen Gras 		endusershell();
99762da0113SBen Gras 		if (cp == NULL && curclass.type != CLASS_GUEST)
99862da0113SBen Gras 			permitted = 0;
99962da0113SBen Gras 	}
100062da0113SBen Gras 
100162da0113SBen Gras 			/* deny quickly (after USER not PASS) if requested */
100262da0113SBen Gras 	if (CURCLASS_FLAGS_ISSET(denyquick) && !permitted) {
100362da0113SBen Gras 		reply(530, "User %s may not use FTP.", curname);
100462da0113SBen Gras 		if (logging)
100562da0113SBen Gras 			syslog(LOG_NOTICE, "FTP LOGIN REFUSED FROM %s, %s",
100662da0113SBen Gras 			    remoteloghost, curname);
100762da0113SBen Gras 		end_login();
100862da0113SBen Gras 		goto cleanup_user;
100962da0113SBen Gras 	}
101062da0113SBen Gras 
101162da0113SBen Gras 			/* if haven't asked yet (i.e, not anon), ask now */
101262da0113SBen Gras 	if (!askpasswd) {
101362da0113SBen Gras 		askpasswd = 1;
101462da0113SBen Gras #ifdef USE_PAM
101562da0113SBen Gras 		e = auth_pam();		/* this does reply(331, ...) */
101662da0113SBen Gras 		do_pass(1, e, "");
101762da0113SBen Gras 		goto cleanup_user;
101862da0113SBen Gras #else /* !USE_PAM */
101962da0113SBen Gras #ifdef SKEY
102062da0113SBen Gras 		if (skey_haskey(curname) == 0) {
102162da0113SBen Gras 			const char *myskey;
102262da0113SBen Gras 
102362da0113SBen Gras 			myskey = skey_keyinfo(curname);
102462da0113SBen Gras 			reply(331, "Password [ %s ] required for %s.",
102562da0113SBen Gras 			    myskey ? myskey : "error getting challenge",
102662da0113SBen Gras 			    curname);
102762da0113SBen Gras 		} else
102862da0113SBen Gras #endif
102962da0113SBen Gras 			reply(331, "Password required for %s.", curname);
103062da0113SBen Gras #endif /* !USE_PAM */
103162da0113SBen Gras 	}
103262da0113SBen Gras 
103362da0113SBen Gras  cleanup_user:
103462da0113SBen Gras #ifdef LOGIN_CAP
103562da0113SBen Gras 	login_close(lc);
103662da0113SBen Gras #endif
103762da0113SBen Gras 	/*
103862da0113SBen Gras 	 * Delay before reading passwd after first failed
103962da0113SBen Gras 	 * attempt to slow down passwd-guessing programs.
104062da0113SBen Gras 	 */
104162da0113SBen Gras 	if (login_attempts)
104262da0113SBen Gras 		sleep((unsigned) login_attempts);
104362da0113SBen Gras 
104462da0113SBen Gras 	if (class)
104562da0113SBen Gras 		free(class);
104662da0113SBen Gras }
104762da0113SBen Gras 
104862da0113SBen Gras /*
104962da0113SBen Gras  * Determine whether something is to happen (allow access, chroot)
105062da0113SBen Gras  * for a user. Each line is a shell-style glob followed by
105162da0113SBen Gras  * `yes' or `no'.
105262da0113SBen Gras  *
105362da0113SBen Gras  * For backward compatibility, `allow' and `deny' are synonymns
105462da0113SBen Gras  * for `yes' and `no', respectively.
105562da0113SBen Gras  *
105662da0113SBen Gras  * Each glob is matched against the username in turn, and the first
105762da0113SBen Gras  * match found is used. If no match is found, the result is the
105862da0113SBen Gras  * argument `def'. If a match is found but without and explicit
105962da0113SBen Gras  * `yes'/`no', the result is the opposite of def.
106062da0113SBen Gras  *
106162da0113SBen Gras  * If the file doesn't exist at all, the result is the argument
106262da0113SBen Gras  * `nofile'
106362da0113SBen Gras  *
106462da0113SBen Gras  * Any line starting with `#' is considered a comment and ignored.
106562da0113SBen Gras  *
106662da0113SBen Gras  * Returns 0 if the user is denied, or 1 if they are allowed.
106762da0113SBen Gras  *
106862da0113SBen Gras  * NOTE: needs struct passwd *pw setup before use.
106962da0113SBen Gras  */
107062da0113SBen Gras static int
checkuser(const char * fname,const char * name,int def,int nofile,char ** retclass)107162da0113SBen Gras checkuser(const char *fname, const char *name, int def, int nofile,
107262da0113SBen Gras 	    char **retclass)
107362da0113SBen Gras {
107462da0113SBen Gras 	FILE	*fd;
107562da0113SBen Gras 	int	 retval;
107662da0113SBen Gras 	char	*word, *perm, *class, *buf, *p;
107762da0113SBen Gras 	size_t	 len, line;
107862da0113SBen Gras 
107962da0113SBen Gras 	retval = def;
108062da0113SBen Gras 	if (retclass != NULL)
108162da0113SBen Gras 		*retclass = NULL;
108262da0113SBen Gras 	if ((fd = fopen(conffilename(fname), "r")) == NULL)
108362da0113SBen Gras 		return nofile;
108462da0113SBen Gras 
108562da0113SBen Gras 	line = 0;
108662da0113SBen Gras 	for (;
108762da0113SBen Gras 	    (buf = fparseln(fd, &len, &line, NULL, FPARSELN_UNESCCOMM |
108862da0113SBen Gras 			    FPARSELN_UNESCCONT | FPARSELN_UNESCESC)) != NULL;
108962da0113SBen Gras 	    free(buf), buf = NULL) {
109062da0113SBen Gras 		word = perm = class = NULL;
109162da0113SBen Gras 		p = buf;
109262da0113SBen Gras 		if (len < 1)
109362da0113SBen Gras 			continue;
109462da0113SBen Gras 		if (p[len - 1] == '\n')
109562da0113SBen Gras 			p[--len] = '\0';
109662da0113SBen Gras 		if (EMPTYSTR(p))
109762da0113SBen Gras 			continue;
109862da0113SBen Gras 
109962da0113SBen Gras 		NEXTWORD(p, word);
110062da0113SBen Gras 		NEXTWORD(p, perm);
110162da0113SBen Gras 		NEXTWORD(p, class);
110262da0113SBen Gras 		if (EMPTYSTR(word))
110362da0113SBen Gras 			continue;
110462da0113SBen Gras 		if (!EMPTYSTR(class)) {
110562da0113SBen Gras 			if (strcasecmp(class, "all") == 0 ||
110662da0113SBen Gras 			    strcasecmp(class, "none") == 0) {
110762da0113SBen Gras 				syslog(LOG_WARNING,
110862da0113SBen Gras 		"%s line %d: illegal user-defined class `%s' - skipping entry",
110962da0113SBen Gras 					    fname, (int)line, class);
111062da0113SBen Gras 				continue;
111162da0113SBen Gras 			}
111262da0113SBen Gras 		}
111362da0113SBen Gras 
111462da0113SBen Gras 					/* have a host specifier */
111562da0113SBen Gras 		if ((p = strchr(word, '@')) != NULL) {
111662da0113SBen Gras 			unsigned char	net[16], mask[16], *addr;
111762da0113SBen Gras 			int		addrlen, bits, bytes, a;
111862da0113SBen Gras 
111962da0113SBen Gras 			*p++ = '\0';
112062da0113SBen Gras 					/* check against network or CIDR */
112162da0113SBen Gras 			memset(net, 0x00, sizeof(net));
112262da0113SBen Gras 			if ((bits = inet_net_pton(his_addr.su_family, p, net,
112362da0113SBen Gras 			    sizeof(net))) != -1) {
112462da0113SBen Gras #ifdef INET6
112562da0113SBen Gras 				if (his_addr.su_family == AF_INET) {
112662da0113SBen Gras #endif
112762da0113SBen Gras 					addrlen = 4;
112862da0113SBen Gras 					addr = (unsigned char *)&his_addr.su_addr;
112962da0113SBen Gras #ifdef INET6
113062da0113SBen Gras 				} else {
113162da0113SBen Gras 					addrlen = 16;
113262da0113SBen Gras 					addr = (unsigned char *)&his_addr.su_6addr;
113362da0113SBen Gras 				}
113462da0113SBen Gras #endif
113562da0113SBen Gras 				bytes = bits / 8;
113662da0113SBen Gras 				bits = bits % 8;
113762da0113SBen Gras 				if (bytes > 0)
113862da0113SBen Gras 					memset(mask, 0xFF, bytes);
113962da0113SBen Gras 				if (bytes < addrlen)
114062da0113SBen Gras 					mask[bytes] = 0xFF << (8 - bits);
114162da0113SBen Gras 				if (bytes + 1 < addrlen)
114262da0113SBen Gras 					memset(mask + bytes + 1, 0x00,
114362da0113SBen Gras 					    addrlen - bytes - 1);
114462da0113SBen Gras 				for (a = 0; a < addrlen; a++)
114562da0113SBen Gras 					if ((addr[a] & mask[a]) != net[a])
114662da0113SBen Gras 						break;
114762da0113SBen Gras 				if (a < addrlen)
114862da0113SBen Gras 					continue;
114962da0113SBen Gras 
115062da0113SBen Gras 					/* check against hostname glob */
115162da0113SBen Gras 			} else if (fnmatch(p, remotehost, FNM_CASEFOLD) != 0)
115262da0113SBen Gras 				continue;
115362da0113SBen Gras 		}
115462da0113SBen Gras 
115562da0113SBen Gras 					/* have a group specifier */
115662da0113SBen Gras 		if ((p = strchr(word, ':')) != NULL) {
115762da0113SBen Gras 			gid_t	*groups, *ng;
115862da0113SBen Gras 			int	 gsize, i, found;
115962da0113SBen Gras 
116062da0113SBen Gras 			if (pw == NULL)
116162da0113SBen Gras 				continue;	/* no match for unknown user */
116262da0113SBen Gras 			*p++ = '\0';
116362da0113SBen Gras 			groups = NULL;
116462da0113SBen Gras 			gsize = 16;
116562da0113SBen Gras 			do {
116662da0113SBen Gras 				ng = realloc(groups, gsize * sizeof(gid_t));
116762da0113SBen Gras 				if (ng == NULL)
116862da0113SBen Gras 					fatal(
116962da0113SBen Gras 					    "Local resource failure: realloc");
117062da0113SBen Gras 				groups = ng;
117162da0113SBen Gras 			} while (getgrouplist(pw->pw_name, pw->pw_gid,
117262da0113SBen Gras 						groups, &gsize) == -1);
117362da0113SBen Gras 			found = 0;
117462da0113SBen Gras 			for (i = 0; i < gsize; i++) {
117562da0113SBen Gras 				struct group *g;
117662da0113SBen Gras 
117762da0113SBen Gras 				if ((g = getgrgid(groups[i])) == NULL)
117862da0113SBen Gras 					continue;
117962da0113SBen Gras 				if (fnmatch(p, g->gr_name, 0) == 0) {
118062da0113SBen Gras 					found = 1;
118162da0113SBen Gras 					break;
118262da0113SBen Gras 				}
118362da0113SBen Gras 			}
118462da0113SBen Gras 			free(groups);
118562da0113SBen Gras 			if (!found)
118662da0113SBen Gras 				continue;
118762da0113SBen Gras 		}
118862da0113SBen Gras 
118962da0113SBen Gras 					/* check against username glob */
119062da0113SBen Gras 		if (fnmatch(word, name, 0) != 0)
119162da0113SBen Gras 			continue;
119262da0113SBen Gras 
119362da0113SBen Gras 		if (perm != NULL &&
119462da0113SBen Gras 		    ((strcasecmp(perm, "allow") == 0) ||
119562da0113SBen Gras 		     (strcasecmp(perm, "yes") == 0)))
119662da0113SBen Gras 			retval = 1;
119762da0113SBen Gras 		else if (perm != NULL &&
119862da0113SBen Gras 		    ((strcasecmp(perm, "deny") == 0) ||
119962da0113SBen Gras 		     (strcasecmp(perm, "no") == 0)))
120062da0113SBen Gras 			retval = 0;
120162da0113SBen Gras 		else
120262da0113SBen Gras 			retval = !def;
120362da0113SBen Gras 		if (!EMPTYSTR(class) && retclass != NULL)
120462da0113SBen Gras 			*retclass = ftpd_strdup(class);
120562da0113SBen Gras 		free(buf);
120662da0113SBen Gras 		break;
120762da0113SBen Gras 	}
120862da0113SBen Gras 	(void) fclose(fd);
120962da0113SBen Gras 	return (retval);
121062da0113SBen Gras }
121162da0113SBen Gras 
121262da0113SBen Gras /*
121362da0113SBen Gras  * Check if user is allowed by /etc/ftpusers
121462da0113SBen Gras  * returns 1 for yes, 0 for no
121562da0113SBen Gras  *
121662da0113SBen Gras  * NOTE: needs struct passwd *pw setup (for checkuser())
121762da0113SBen Gras  */
121862da0113SBen Gras static int
checkaccess(const char * name)121962da0113SBen Gras checkaccess(const char *name)
122062da0113SBen Gras {
122162da0113SBen Gras 
122262da0113SBen Gras 	return (checkuser(_NAME_FTPUSERS, name, 1, 0, NULL));
122362da0113SBen Gras }
122462da0113SBen Gras 
122562da0113SBen Gras static void
login_utmp(const char * line,const char * name,const char * host,struct sockinet * haddr)122662da0113SBen Gras login_utmp(const char *line, const char *name, const char *host,
122762da0113SBen Gras     struct sockinet *haddr)
122862da0113SBen Gras {
122962da0113SBen Gras #if defined(SUPPORT_UTMPX) || defined(SUPPORT_UTMP)
123062da0113SBen Gras 	struct timeval tv;
123162da0113SBen Gras 	(void)gettimeofday(&tv, NULL);
123262da0113SBen Gras #endif
123362da0113SBen Gras #ifdef SUPPORT_UTMPX
123462da0113SBen Gras 	if (doutmp) {
123562da0113SBen Gras 		(void)memset(&utmpx, 0, sizeof(utmpx));
123662da0113SBen Gras 		utmpx.ut_tv = tv;
123762da0113SBen Gras 		utmpx.ut_pid = getpid();
123862da0113SBen Gras 		utmpx.ut_id[0] = 'f';
123962da0113SBen Gras 		utmpx.ut_id[1] = 't';
124062da0113SBen Gras 		utmpx.ut_id[2] = 'p';
124162da0113SBen Gras 		utmpx.ut_id[3] = '*';
124262da0113SBen Gras 		utmpx.ut_type = USER_PROCESS;
124362da0113SBen Gras 		(void)strncpy(utmpx.ut_name, name, sizeof(utmpx.ut_name));
124462da0113SBen Gras 		(void)strncpy(utmpx.ut_line, line, sizeof(utmpx.ut_line));
124562da0113SBen Gras 		(void)strncpy(utmpx.ut_host, host, sizeof(utmpx.ut_host));
124662da0113SBen Gras 		(void)memcpy(&utmpx.ut_ss, &haddr->si_su, haddr->su_len);
124762da0113SBen Gras 		ftpd_loginx(&utmpx);
124862da0113SBen Gras 	}
124962da0113SBen Gras 	if (dowtmp)
125062da0113SBen Gras 		ftpd_logwtmpx(line, name, host, haddr, 0, USER_PROCESS);
125162da0113SBen Gras #endif
125262da0113SBen Gras #ifdef SUPPORT_UTMP
125362da0113SBen Gras 	if (doutmp) {
125462da0113SBen Gras 		(void)memset(&utmp, 0, sizeof(utmp));
125562da0113SBen Gras 		(void)time(&utmp.ut_time);
125662da0113SBen Gras 		(void)strncpy(utmp.ut_name, name, sizeof(utmp.ut_name));
125762da0113SBen Gras 		(void)strncpy(utmp.ut_line, line, sizeof(utmp.ut_line));
125862da0113SBen Gras 		(void)strncpy(utmp.ut_host, host, sizeof(utmp.ut_host));
125962da0113SBen Gras 		ftpd_login(&utmp);
126062da0113SBen Gras 	}
126162da0113SBen Gras 	if (dowtmp)
126262da0113SBen Gras 		ftpd_logwtmp(line, name, host);
126362da0113SBen Gras #endif
126462da0113SBen Gras }
126562da0113SBen Gras 
126662da0113SBen Gras static void
logout_utmp(void)126762da0113SBen Gras logout_utmp(void)
126862da0113SBen Gras {
126962da0113SBen Gras #ifdef SUPPORT_UTMPX
127062da0113SBen Gras 	int okwtmpx = dowtmp;
127162da0113SBen Gras #endif
127262da0113SBen Gras #ifdef SUPPORT_UTMP
127362da0113SBen Gras 	int okwtmp = dowtmp;
127462da0113SBen Gras #endif
127562da0113SBen Gras 	if (logged_in) {
127662da0113SBen Gras #ifdef SUPPORT_UTMPX
127762da0113SBen Gras 		if (doutmp)
127862da0113SBen Gras 			okwtmpx &= ftpd_logoutx(ttyline, 0, DEAD_PROCESS);
127962da0113SBen Gras 		if (okwtmpx)
128062da0113SBen Gras 			ftpd_logwtmpx(ttyline, "", "", NULL, 0, DEAD_PROCESS);
128162da0113SBen Gras #endif
128262da0113SBen Gras #ifdef SUPPORT_UTMP
128362da0113SBen Gras 		if (doutmp)
128462da0113SBen Gras 			okwtmp &= ftpd_logout(ttyline);
128562da0113SBen Gras 		if (okwtmp)
128662da0113SBen Gras 			ftpd_logwtmp(ttyline, "", "");
128762da0113SBen Gras #endif
128862da0113SBen Gras 	}
128962da0113SBen Gras }
129062da0113SBen Gras 
129162da0113SBen Gras /*
129262da0113SBen Gras  * Terminate login as previous user (if any), resetting state;
129362da0113SBen Gras  * used when USER command is given or login fails.
129462da0113SBen Gras  */
129562da0113SBen Gras static void
end_login(void)129662da0113SBen Gras end_login(void)
129762da0113SBen Gras {
129862da0113SBen Gras #ifdef USE_PAM
129962da0113SBen Gras 	int e;
130062da0113SBen Gras #endif
130162da0113SBen Gras 	logout_utmp();
130262da0113SBen Gras 	show_chdir_messages(-1);		/* flush chdir cache */
130362da0113SBen Gras 	if (pw != NULL && pw->pw_passwd != NULL)
130462da0113SBen Gras 		memset(pw->pw_passwd, 0, strlen(pw->pw_passwd));
130562da0113SBen Gras 	pw = NULL;
130662da0113SBen Gras 	logged_in = 0;
130762da0113SBen Gras 	askpasswd = 0;
130862da0113SBen Gras 	permitted = 0;
130962da0113SBen Gras 	quietmessages = 0;
131062da0113SBen Gras 	gidcount = 0;
131162da0113SBen Gras 	curclass.type = CLASS_REAL;
131262da0113SBen Gras 	(void) seteuid((uid_t)0);
131362da0113SBen Gras #ifdef	LOGIN_CAP
131462da0113SBen Gras 	setusercontext(NULL, getpwuid(0), 0,
131562da0113SBen Gras 		       LOGIN_SETPRIORITY|LOGIN_SETRESOURCES|LOGIN_SETUMASK);
131662da0113SBen Gras #endif
131762da0113SBen Gras #ifdef USE_PAM
131862da0113SBen Gras 	if (pamh) {
131962da0113SBen Gras 		if ((e = pam_setcred(pamh, PAM_DELETE_CRED)) != PAM_SUCCESS)
132062da0113SBen Gras 			syslog(LOG_ERR, "pam_setcred: %s",
132162da0113SBen Gras 			    pam_strerror(pamh, e));
132262da0113SBen Gras 		if ((e = pam_close_session(pamh,0)) != PAM_SUCCESS)
132362da0113SBen Gras 			syslog(LOG_ERR, "pam_close_session: %s",
132462da0113SBen Gras 			    pam_strerror(pamh, e));
132562da0113SBen Gras 		if ((e = pam_end(pamh, e)) != PAM_SUCCESS)
132662da0113SBen Gras 			syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e));
132762da0113SBen Gras 		pamh = NULL;
132862da0113SBen Gras 	}
132962da0113SBen Gras #endif
133062da0113SBen Gras }
133162da0113SBen Gras 
133262da0113SBen Gras void
pass(const char * passwd)133362da0113SBen Gras pass(const char *passwd)
133462da0113SBen Gras {
133562da0113SBen Gras 	do_pass(0, 0, passwd);
133662da0113SBen Gras }
133762da0113SBen Gras 
133862da0113SBen Gras /*
133962da0113SBen Gras  * Perform the passwd confirmation and login.
134062da0113SBen Gras  *
134162da0113SBen Gras  * If pass_checked is zero, confirm passwd is correct, & ignore pass_rval.
134262da0113SBen Gras  * This is the traditional PASS implementation.
134362da0113SBen Gras  *
134462da0113SBen Gras  * If pass_checked is non-zero, use pass_rval and ignore passwd.
134562da0113SBen Gras  * This is used by auth_pam() which has already parsed PASS.
134662da0113SBen Gras  * This only applies to curclass.type != CLASS_GUEST.
134762da0113SBen Gras  */
134862da0113SBen Gras static void
do_pass(int pass_checked,int pass_rval,const char * passwd)134962da0113SBen Gras do_pass(int pass_checked, int pass_rval, const char *passwd)
135062da0113SBen Gras {
135162da0113SBen Gras 	int		 rval;
135262da0113SBen Gras 	char		 root[MAXPATHLEN];
135362da0113SBen Gras #ifdef	LOGIN_CAP
135462da0113SBen Gras 	login_cap_t *lc = NULL;
135562da0113SBen Gras #endif
135662da0113SBen Gras #ifdef USE_PAM
135762da0113SBen Gras 	int e;
135862da0113SBen Gras #endif
135962da0113SBen Gras 
136062da0113SBen Gras 	rval = 1;
136162da0113SBen Gras 
136262da0113SBen Gras 	if (logged_in || askpasswd == 0) {
136362da0113SBen Gras 		reply(503, "Login with USER first.");
136462da0113SBen Gras 		return;
136562da0113SBen Gras 	}
136662da0113SBen Gras 	askpasswd = 0;
136762da0113SBen Gras 	if (curclass.type != CLASS_GUEST) {
136862da0113SBen Gras 			/* "ftp" is the only account allowed with no password */
136962da0113SBen Gras 		if (pw == NULL) {
137062da0113SBen Gras 			rval = 1;	/* failure below */
137162da0113SBen Gras 			goto skip;
137262da0113SBen Gras 		}
137362da0113SBen Gras 		if (pass_checked) {	/* password validated in user() */
137462da0113SBen Gras 			rval = pass_rval;
137562da0113SBen Gras 			goto skip;
137662da0113SBen Gras 		}
137762da0113SBen Gras #ifdef USE_PAM
137862da0113SBen Gras 		syslog(LOG_ERR, "do_pass: USE_PAM shouldn't get here");
137962da0113SBen Gras 		rval = 1;
138062da0113SBen Gras 		goto skip;
138162da0113SBen Gras #endif
138262da0113SBen Gras #if defined(KERBEROS)
138362da0113SBen Gras 		if (klogin(pw, "", hostname, (char *)passwd) == 0) {
138462da0113SBen Gras 			rval = 0;
138562da0113SBen Gras 			goto skip;
138662da0113SBen Gras 		}
138762da0113SBen Gras #endif
138862da0113SBen Gras #if defined(KERBEROS5)
138962da0113SBen Gras 		if (k5login(pw, "", hostname, (char *)passwd) == 0) {
139062da0113SBen Gras 			rval = 0;
139162da0113SBen Gras 			goto skip;
139262da0113SBen Gras 		}
139362da0113SBen Gras #endif
139462da0113SBen Gras #ifdef SKEY
139562da0113SBen Gras 		if (skey_haskey(pw->pw_name) == 0) {
139662da0113SBen Gras 			char *p;
139762da0113SBen Gras 			int r;
139862da0113SBen Gras 
139962da0113SBen Gras 			p = ftpd_strdup(passwd);
140062da0113SBen Gras 			r = skey_passcheck(pw->pw_name, p);
140162da0113SBen Gras 			free(p);
140262da0113SBen Gras 			if (r != -1) {
140362da0113SBen Gras 				rval = 0;
140462da0113SBen Gras 				goto skip;
140562da0113SBen Gras 			}
140662da0113SBen Gras 		}
140762da0113SBen Gras #endif
140862da0113SBen Gras 		if (!sflag)
140962da0113SBen Gras 			rval = checkpassword(pw, passwd);
141062da0113SBen Gras 		else
141162da0113SBen Gras 			rval = 1;
141262da0113SBen Gras 
141362da0113SBen Gras  skip:
141462da0113SBen Gras 
141562da0113SBen Gras 			/*
141662da0113SBen Gras 			 * If rval > 0, the user failed the authentication check
141762da0113SBen Gras 			 * above.  If rval == 0, either Kerberos or local
141862da0113SBen Gras 			 * authentication succeeded.
141962da0113SBen Gras 			 */
142062da0113SBen Gras 		if (rval) {
142162da0113SBen Gras 			reply(530, "%s", rval == 2 ? "Password expired." :
142262da0113SBen Gras 			    "Login incorrect.");
14230a6a1f1dSLionel Sambuc 			pfilter_notify(1, rval == 2 ? "exppass" : "badpass");
142462da0113SBen Gras 			if (logging) {
142562da0113SBen Gras 				syslog(LOG_NOTICE,
142662da0113SBen Gras 				    "FTP LOGIN FAILED FROM %s", remoteloghost);
142762da0113SBen Gras 				syslog(LOG_AUTHPRIV | LOG_NOTICE,
142862da0113SBen Gras 				    "FTP LOGIN FAILED FROM %s, %s",
142962da0113SBen Gras 				    remoteloghost, curname);
143062da0113SBen Gras 			}
143162da0113SBen Gras 			pw = NULL;
143262da0113SBen Gras 			if (login_attempts++ >= 5) {
143362da0113SBen Gras 				syslog(LOG_NOTICE,
143462da0113SBen Gras 				    "repeated login failures from %s",
143562da0113SBen Gras 				    remoteloghost);
143662da0113SBen Gras 				exit(0);
143762da0113SBen Gras 			}
143862da0113SBen Gras 			return;
143962da0113SBen Gras 		}
144062da0113SBen Gras 	}
144162da0113SBen Gras 
144262da0113SBen Gras 			/* password ok; check if anything else prevents login */
144362da0113SBen Gras 	if (! permitted) {
144462da0113SBen Gras 		reply(530, "User %s may not use FTP.", pw->pw_name);
144562da0113SBen Gras 		if (logging)
144662da0113SBen Gras 			syslog(LOG_NOTICE, "FTP LOGIN REFUSED FROM %s, %s",
144762da0113SBen Gras 			    remoteloghost, pw->pw_name);
144862da0113SBen Gras 		goto bad;
144962da0113SBen Gras 	}
145062da0113SBen Gras 
145162da0113SBen Gras 	login_attempts = 0;		/* this time successful */
145262da0113SBen Gras 	if (setegid((gid_t)pw->pw_gid) < 0) {
145362da0113SBen Gras 		reply(550, "Can't set gid.");
145462da0113SBen Gras 		goto bad;
145562da0113SBen Gras 	}
145662da0113SBen Gras #ifdef	LOGIN_CAP
145762da0113SBen Gras 	if ((lc = login_getpwclass(pw)) != NULL) {
145862da0113SBen Gras #ifdef notyet
145962da0113SBen Gras 		char	remote_ip[NI_MAXHOST];
146062da0113SBen Gras 
146162da0113SBen Gras 		if (getnameinfo((struct sockaddr *)&his_addr, his_addr.su_len,
146262da0113SBen Gras 			remote_ip, sizeof(remote_ip) - 1, NULL, 0,
146362da0113SBen Gras 			NI_NUMERICHOST))
146462da0113SBen Gras 				*remote_ip = 0;
146562da0113SBen Gras 		remote_ip[sizeof(remote_ip) - 1] = 0;
146662da0113SBen Gras 		if (!auth_hostok(lc, remotehost, remote_ip)) {
14670a6a1f1dSLionel Sambuc 			pfilter_notify(1, "bannedhost");
146862da0113SBen Gras 			syslog(LOG_INFO|LOG_AUTH,
146962da0113SBen Gras 			    "FTP LOGIN FAILED (HOST) as %s: permission denied.",
147062da0113SBen Gras 			    pw->pw_name);
147162da0113SBen Gras 			reply(530, "Permission denied.");
147262da0113SBen Gras 			pw = NULL;
147362da0113SBen Gras 			return;
147462da0113SBen Gras 		}
147562da0113SBen Gras 		if (!auth_timeok(lc, time(NULL))) {
147662da0113SBen Gras 			reply(530, "Login not available right now.");
147762da0113SBen Gras 			pw = NULL;
147862da0113SBen Gras 			return;
147962da0113SBen Gras 		}
148062da0113SBen Gras #endif
148162da0113SBen Gras 	}
148262da0113SBen Gras 	setsid();
148362da0113SBen Gras 	setusercontext(lc, pw, 0,
148462da0113SBen Gras 		LOGIN_SETLOGIN|LOGIN_SETGROUP|LOGIN_SETPRIORITY|
148562da0113SBen Gras 		LOGIN_SETRESOURCES|LOGIN_SETUMASK);
148662da0113SBen Gras #else
148762da0113SBen Gras 	(void) initgroups(pw->pw_name, pw->pw_gid);
148862da0113SBen Gras 			/* cache groups for cmds.c::matchgroup() */
148962da0113SBen Gras #endif
149062da0113SBen Gras #ifdef USE_PAM
149162da0113SBen Gras 	if (pamh) {
149262da0113SBen Gras 		if ((e = pam_open_session(pamh, 0)) != PAM_SUCCESS) {
149362da0113SBen Gras 			syslog(LOG_ERR, "pam_open_session: %s",
149462da0113SBen Gras 			    pam_strerror(pamh, e));
149562da0113SBen Gras 		} else if ((e = pam_setcred(pamh, PAM_ESTABLISH_CRED))
149662da0113SBen Gras 		    != PAM_SUCCESS) {
149762da0113SBen Gras 			syslog(LOG_ERR, "pam_setcred: %s",
149862da0113SBen Gras 			    pam_strerror(pamh, e));
149962da0113SBen Gras 		}
150062da0113SBen Gras 	}
150162da0113SBen Gras #endif
150262da0113SBen Gras 	gidcount = getgroups(0, NULL);
150362da0113SBen Gras 	if (gidlist)
150462da0113SBen Gras 		free(gidlist);
150562da0113SBen Gras 	gidlist = malloc(gidcount * sizeof *gidlist);
150662da0113SBen Gras 	gidcount = getgroups(gidcount, gidlist);
150762da0113SBen Gras 
150862da0113SBen Gras 	/* open utmp/wtmp before chroot */
150962da0113SBen Gras 	login_utmp(ttyline, pw->pw_name, remotehost, &his_addr);
151062da0113SBen Gras 
151162da0113SBen Gras 	logged_in = 1;
151262da0113SBen Gras 
151362da0113SBen Gras 	connections = 1;
151462da0113SBen Gras 	if (dopidfile)
151562da0113SBen Gras 		count_users();
151662da0113SBen Gras 	if (curclass.limit != -1 && connections > curclass.limit) {
151762da0113SBen Gras 		if (! EMPTYSTR(curclass.limitfile))
151862da0113SBen Gras 			(void)display_file(conffilename(curclass.limitfile),
151962da0113SBen Gras 			    530);
152062da0113SBen Gras 		reply(530,
152162da0113SBen Gras 		    "User %s access denied, connection limit of " LLF
152262da0113SBen Gras 		    " reached.",
152362da0113SBen Gras 		    pw->pw_name, (LLT)curclass.limit);
152462da0113SBen Gras 		syslog(LOG_NOTICE,
152562da0113SBen Gras 		    "Maximum connection limit of " LLF
152662da0113SBen Gras 		    " for class %s reached, login refused for %s",
152762da0113SBen Gras 		    (LLT)curclass.limit, curclass.classname, pw->pw_name);
152862da0113SBen Gras 		goto bad;
152962da0113SBen Gras 	}
153062da0113SBen Gras 
153162da0113SBen Gras 	homedir[0] = '/';
153262da0113SBen Gras 	switch (curclass.type) {
153362da0113SBen Gras 	case CLASS_GUEST:
153462da0113SBen Gras 			/*
153562da0113SBen Gras 			 * We MUST do a chdir() after the chroot. Otherwise
153662da0113SBen Gras 			 * the old current directory will be accessible as "."
153762da0113SBen Gras 			 * outside the new root!
153862da0113SBen Gras 			 */
153962da0113SBen Gras 		format_path(root,
154062da0113SBen Gras 		    curclass.chroot ? curclass.chroot :
154162da0113SBen Gras 		    anondir ? anondir :
154262da0113SBen Gras 		    pw->pw_dir);
154362da0113SBen Gras 		format_path(homedir,
154462da0113SBen Gras 		    curclass.homedir ? curclass.homedir :
154562da0113SBen Gras 		    "/");
154662da0113SBen Gras 		if (EMPTYSTR(homedir))
154762da0113SBen Gras 			homedir[0] = '/';
154862da0113SBen Gras 		if (EMPTYSTR(root) || chroot(root) < 0) {
154962da0113SBen Gras 			syslog(LOG_NOTICE,
155062da0113SBen Gras 			    "GUEST user %s: can't chroot to %s: %m",
155162da0113SBen Gras 			    pw->pw_name, root);
155262da0113SBen Gras 			goto bad_guest;
155362da0113SBen Gras 		}
155462da0113SBen Gras 		if (chdir(homedir) < 0) {
155562da0113SBen Gras 			syslog(LOG_NOTICE,
155662da0113SBen Gras 			    "GUEST user %s: can't chdir to %s: %m",
155762da0113SBen Gras 			    pw->pw_name, homedir);
155862da0113SBen Gras  bad_guest:
155962da0113SBen Gras 			reply(550, "Can't set guest privileges.");
156062da0113SBen Gras 			goto bad;
156162da0113SBen Gras 		}
156262da0113SBen Gras 		break;
156362da0113SBen Gras 	case CLASS_CHROOT:
156462da0113SBen Gras 		format_path(root,
156562da0113SBen Gras 		    curclass.chroot ? curclass.chroot :
156662da0113SBen Gras 		    pw->pw_dir);
156762da0113SBen Gras 		format_path(homedir,
156862da0113SBen Gras 		    curclass.homedir ? curclass.homedir :
156962da0113SBen Gras 		    "/");
157062da0113SBen Gras 		if (EMPTYSTR(homedir))
157162da0113SBen Gras 			homedir[0] = '/';
157262da0113SBen Gras 		if (EMPTYSTR(root) || chroot(root) < 0) {
157362da0113SBen Gras 			syslog(LOG_NOTICE,
157462da0113SBen Gras 			    "CHROOT user %s: can't chroot to %s: %m",
157562da0113SBen Gras 			    pw->pw_name, root);
157662da0113SBen Gras 			goto bad_chroot;
157762da0113SBen Gras 		}
157862da0113SBen Gras 		if (chdir(homedir) < 0) {
157962da0113SBen Gras 			syslog(LOG_NOTICE,
158062da0113SBen Gras 			    "CHROOT user %s: can't chdir to %s: %m",
158162da0113SBen Gras 			    pw->pw_name, homedir);
158262da0113SBen Gras  bad_chroot:
158362da0113SBen Gras 			reply(550, "Can't change root.");
158462da0113SBen Gras 			goto bad;
158562da0113SBen Gras 		}
158662da0113SBen Gras 		break;
158762da0113SBen Gras 	case CLASS_REAL:
158862da0113SBen Gras 			/* only chroot REAL if explicitly requested */
158962da0113SBen Gras 		if (! EMPTYSTR(curclass.chroot)) {
159062da0113SBen Gras 			format_path(root, curclass.chroot);
159162da0113SBen Gras 			if (EMPTYSTR(root) || chroot(root) < 0) {
159262da0113SBen Gras 				syslog(LOG_NOTICE,
159362da0113SBen Gras 				    "REAL user %s: can't chroot to %s: %m",
159462da0113SBen Gras 				    pw->pw_name, root);
159562da0113SBen Gras 				goto bad_chroot;
159662da0113SBen Gras 			}
159762da0113SBen Gras 		}
159862da0113SBen Gras 		format_path(homedir,
159962da0113SBen Gras 		    curclass.homedir ? curclass.homedir :
160062da0113SBen Gras 		    pw->pw_dir);
160162da0113SBen Gras 		if (EMPTYSTR(homedir) || chdir(homedir) < 0) {
160262da0113SBen Gras 			if (chdir("/") < 0) {
160362da0113SBen Gras 				syslog(LOG_NOTICE,
160462da0113SBen Gras 				    "REAL user %s: can't chdir to %s: %m",
160562da0113SBen Gras 				    pw->pw_name,
160662da0113SBen Gras 				    !EMPTYSTR(homedir) ?  homedir : "/");
160762da0113SBen Gras 				reply(530,
160862da0113SBen Gras 				    "User %s: can't change directory to %s.",
160962da0113SBen Gras 				    pw->pw_name,
161062da0113SBen Gras 				    !EMPTYSTR(homedir) ? homedir : "/");
161162da0113SBen Gras 				goto bad;
161262da0113SBen Gras 			} else {
161362da0113SBen Gras 				reply(-230,
161462da0113SBen Gras 				    "No directory! Logging in with home=/");
161562da0113SBen Gras 				homedir[0] = '/';
161662da0113SBen Gras 			}
161762da0113SBen Gras 		}
161862da0113SBen Gras 		break;
161962da0113SBen Gras 	}
162062da0113SBen Gras #ifndef LOGIN_CAP
162162da0113SBen Gras 	setsid();
162284d9c625SLionel Sambuc #if !defined(__minix)
162362da0113SBen Gras 	setlogin(pw->pw_name);
162484d9c625SLionel Sambuc #endif /* !defined(__minix) */
16250a6a1f1dSLionel Sambuc #endif
162662da0113SBen Gras 	if (dropprivs ||
162762da0113SBen Gras 	    (curclass.type != CLASS_REAL &&
162862da0113SBen Gras 	    ntohs(ctrl_addr.su_port) > IPPORT_RESERVED + 1)) {
162962da0113SBen Gras 		dropprivs++;
163062da0113SBen Gras 		if (setgid((gid_t)pw->pw_gid) < 0) {
163162da0113SBen Gras 			reply(550, "Can't set gid.");
163262da0113SBen Gras 			goto bad;
163362da0113SBen Gras 		}
163462da0113SBen Gras 		if (setuid((uid_t)pw->pw_uid) < 0) {
163562da0113SBen Gras 			reply(550, "Can't set uid.");
163662da0113SBen Gras 			goto bad;
163762da0113SBen Gras 		}
163862da0113SBen Gras 	} else {
163962da0113SBen Gras 		if (seteuid((uid_t)pw->pw_uid) < 0) {
164062da0113SBen Gras 			reply(550, "Can't set uid.");
164162da0113SBen Gras 			goto bad;
164262da0113SBen Gras 		}
164362da0113SBen Gras 	}
164462da0113SBen Gras 	setenv("HOME", homedir, 1);
164562da0113SBen Gras 
164662da0113SBen Gras 	if (curclass.type == CLASS_GUEST && passwd[0] == '-')
164762da0113SBen Gras 		quietmessages = 1;
164862da0113SBen Gras 
164962da0113SBen Gras 			/*
165062da0113SBen Gras 			 * Display a login message, if it exists.
165162da0113SBen Gras 			 * N.B. reply(230,) must follow the message.
165262da0113SBen Gras 			 */
165362da0113SBen Gras 	if (! EMPTYSTR(curclass.motd))
165462da0113SBen Gras 		(void)display_file(conffilename(curclass.motd), 230);
165562da0113SBen Gras 	show_chdir_messages(230);
165662da0113SBen Gras 	if (curclass.type == CLASS_GUEST) {
165762da0113SBen Gras 		char *p;
165862da0113SBen Gras 
165962da0113SBen Gras 		reply(230, "Guest login ok, access restrictions apply.");
166062da0113SBen Gras #if defined(HAVE_SETPROCTITLE)
166162da0113SBen Gras 		snprintf(proctitle, sizeof(proctitle),
166262da0113SBen Gras 		    "%s: anonymous/%s", remotehost, passwd);
166362da0113SBen Gras 		setproctitle("%s", proctitle);
166462da0113SBen Gras #endif /* defined(HAVE_SETPROCTITLE) */
166562da0113SBen Gras 		if (logging)
166662da0113SBen Gras 			syslog(LOG_INFO,
166762da0113SBen Gras 			"ANONYMOUS FTP LOGIN FROM %s, %s (class: %s, type: %s)",
166862da0113SBen Gras 			    remoteloghost, passwd,
166962da0113SBen Gras 			    curclass.classname, CURCLASSTYPE);
167062da0113SBen Gras 			/* store guest password reply into pw_passwd */
167162da0113SBen Gras 		REASSIGN(pw->pw_passwd, ftpd_strdup(passwd));
167262da0113SBen Gras 		for (p = pw->pw_passwd; *p; p++)
167362da0113SBen Gras 			if (!isgraph((unsigned char)*p))
167462da0113SBen Gras 				*p = '_';
167562da0113SBen Gras 	} else {
167662da0113SBen Gras 		reply(230, "User %s logged in.", pw->pw_name);
167762da0113SBen Gras #if defined(HAVE_SETPROCTITLE)
167862da0113SBen Gras 		snprintf(proctitle, sizeof(proctitle),
167962da0113SBen Gras 		    "%s: %s", remotehost, pw->pw_name);
168062da0113SBen Gras 		setproctitle("%s", proctitle);
168162da0113SBen Gras #endif /* defined(HAVE_SETPROCTITLE) */
168262da0113SBen Gras 		if (logging)
168362da0113SBen Gras 			syslog(LOG_INFO,
168462da0113SBen Gras 			    "FTP LOGIN FROM %s as %s (class: %s, type: %s)",
168562da0113SBen Gras 			    remoteloghost, pw->pw_name,
168662da0113SBen Gras 			    curclass.classname, CURCLASSTYPE);
168762da0113SBen Gras 	}
168862da0113SBen Gras 	(void) umask(curclass.umask);
168962da0113SBen Gras #ifdef	LOGIN_CAP
169062da0113SBen Gras 	login_close(lc);
169162da0113SBen Gras #endif
169262da0113SBen Gras 	return;
169362da0113SBen Gras 
169462da0113SBen Gras  bad:
169562da0113SBen Gras #ifdef	LOGIN_CAP
169662da0113SBen Gras 	login_close(lc);
169762da0113SBen Gras #endif
169862da0113SBen Gras 			/* Forget all about it... */
169962da0113SBen Gras 	end_login();
170062da0113SBen Gras }
170162da0113SBen Gras 
170262da0113SBen Gras void
retrieve(const char * argv[],const char * name)170362da0113SBen Gras retrieve(const char *argv[], const char *name)
170462da0113SBen Gras {
170562da0113SBen Gras 	FILE *fin, *dout;
170662da0113SBen Gras 	struct stat st;
170762da0113SBen Gras 	int (*closefunc)(FILE *) = NULL;
170862da0113SBen Gras 	int dolog, sendrv, closerv, stderrfd, isconversion, isdata, isls;
170962da0113SBen Gras 	struct timeval start, finish, td, *tdp;
171062da0113SBen Gras 	struct rusage rusage_before, rusage_after;
171162da0113SBen Gras 	const char *dispname;
171262da0113SBen Gras 	const char *error;
171362da0113SBen Gras 
171462da0113SBen Gras 	sendrv = closerv = stderrfd = -1;
171562da0113SBen Gras 	isconversion = isdata = isls = dolog = 0;
171662da0113SBen Gras 	tdp = NULL;
171762da0113SBen Gras 	dispname = name;
171862da0113SBen Gras 	fin = dout = NULL;
171962da0113SBen Gras 	error = NULL;
172062da0113SBen Gras 	if (argv == NULL) {		/* if not running a command ... */
172162da0113SBen Gras 		dolog = 1;
172262da0113SBen Gras 		isdata = 1;
172362da0113SBen Gras 		fin = fopen(name, "r");
172462da0113SBen Gras 		closefunc = fclose;
172562da0113SBen Gras 		if (fin == NULL)	/* doesn't exist?; try a conversion */
172662da0113SBen Gras 			argv = do_conversion(name);
172762da0113SBen Gras 		if (argv != NULL) {
172862da0113SBen Gras 			isconversion++;
172962da0113SBen Gras 			syslog(LOG_DEBUG, "get command: '%s' on '%s'",
173062da0113SBen Gras 			    argv[0], name);
173162da0113SBen Gras 		}
173262da0113SBen Gras 	}
173362da0113SBen Gras 	if (argv != NULL) {
173462da0113SBen Gras 		char temp[MAXPATHLEN];
173562da0113SBen Gras 
173662da0113SBen Gras 		if (strcmp(argv[0], INTERNAL_LS) == 0) {
173762da0113SBen Gras 			isls = 1;
173862da0113SBen Gras 			stderrfd = -1;
173962da0113SBen Gras 		} else {
174062da0113SBen Gras 			(void)snprintf(temp, sizeof(temp), "%s", TMPFILE);
174162da0113SBen Gras 			stderrfd = mkstemp(temp);
174262da0113SBen Gras 			if (stderrfd != -1)
174362da0113SBen Gras 				(void)unlink(temp);
174462da0113SBen Gras 		}
174562da0113SBen Gras 		dispname = argv[0];
174662da0113SBen Gras 		fin = ftpd_popen(argv, "r", stderrfd);
174762da0113SBen Gras 		closefunc = ftpd_pclose;
174862da0113SBen Gras 		st.st_size = -1;
174962da0113SBen Gras 		st.st_blksize = BUFSIZ;
175062da0113SBen Gras 	}
175162da0113SBen Gras 	if (fin == NULL) {
175262da0113SBen Gras 		if (errno != 0) {
175362da0113SBen Gras 			perror_reply(550, dispname);
175462da0113SBen Gras 			if (dolog)
175562da0113SBen Gras 				logxfer("get", -1, name, NULL, NULL,
175662da0113SBen Gras 				    strerror(errno));
175762da0113SBen Gras 		}
175862da0113SBen Gras 		goto cleanupretrieve;
175962da0113SBen Gras 	}
176062da0113SBen Gras 	byte_count = -1;
176162da0113SBen Gras 	if (argv == NULL
176262da0113SBen Gras 	    && (fstat(fileno(fin), &st) < 0 || !S_ISREG(st.st_mode))) {
176362da0113SBen Gras 		error = "Not a plain file";
176462da0113SBen Gras 		reply(550, "%s: %s.", dispname, error);
176562da0113SBen Gras 		goto done;
176662da0113SBen Gras 	}
176762da0113SBen Gras 	if (restart_point) {
176862da0113SBen Gras 		if (type == TYPE_A) {
176962da0113SBen Gras 			off_t i;
177062da0113SBen Gras 			int c;
177162da0113SBen Gras 
177262da0113SBen Gras 			for (i = 0; i < restart_point; i++) {
177362da0113SBen Gras 				if ((c=getc(fin)) == EOF) {
177462da0113SBen Gras 					error = strerror(errno);
177562da0113SBen Gras 					perror_reply(550, dispname);
177662da0113SBen Gras 					goto done;
177762da0113SBen Gras 				}
177862da0113SBen Gras 				if (c == '\n')
177962da0113SBen Gras 					i++;
178062da0113SBen Gras 			}
178162da0113SBen Gras 		} else if (lseek(fileno(fin), restart_point, SEEK_SET) < 0) {
178262da0113SBen Gras 			error = strerror(errno);
178362da0113SBen Gras 			perror_reply(550, dispname);
178462da0113SBen Gras 			goto done;
178562da0113SBen Gras 		}
178662da0113SBen Gras 	}
178762da0113SBen Gras 	dout = dataconn(dispname, st.st_size, "w");
178862da0113SBen Gras 	if (dout == NULL)
178962da0113SBen Gras 		goto done;
179062da0113SBen Gras 
179162da0113SBen Gras 	(void)getrusage(RUSAGE_SELF, &rusage_before);
179262da0113SBen Gras 	(void)gettimeofday(&start, NULL);
179362da0113SBen Gras 	sendrv = send_data(fin, dout, &st, isdata);
179462da0113SBen Gras 	(void)gettimeofday(&finish, NULL);
179562da0113SBen Gras 	(void)getrusage(RUSAGE_SELF, &rusage_after);
179662da0113SBen Gras 	closedataconn(dout);		/* close now to affect timing stats */
179762da0113SBen Gras 	timersub(&finish, &start, &td);
179862da0113SBen Gras 	tdp = &td;
179962da0113SBen Gras  done:
180062da0113SBen Gras 	if (dolog) {
180162da0113SBen Gras 		logxfer("get", byte_count, name, NULL, tdp, error);
180262da0113SBen Gras 		if (tdp != NULL)
180362da0113SBen Gras 			logrusage(&rusage_before, &rusage_after);
180462da0113SBen Gras 	}
180562da0113SBen Gras 	closerv = (*closefunc)(fin);
180662da0113SBen Gras 	if (sendrv == 0) {
180762da0113SBen Gras 		FILE *errf;
180862da0113SBen Gras 		struct stat sb;
180962da0113SBen Gras 
181062da0113SBen Gras 		if (!isls && argv != NULL && closerv != 0) {
181162da0113SBen Gras 			reply(-226,
181262da0113SBen Gras 			    "Command returned an exit status of %d",
181362da0113SBen Gras 			    closerv);
181462da0113SBen Gras 			if (isconversion)
181562da0113SBen Gras 				syslog(LOG_WARNING,
181662da0113SBen Gras 				    "retrieve command: '%s' returned %d",
181762da0113SBen Gras 				    argv[0], closerv);
181862da0113SBen Gras 		}
181962da0113SBen Gras 		if (!isls && argv != NULL && stderrfd != -1 &&
182062da0113SBen Gras 		    (fstat(stderrfd, &sb) == 0) && sb.st_size > 0 &&
182162da0113SBen Gras 		    ((errf = fdopen(stderrfd, "r")) != NULL)) {
182262da0113SBen Gras 			char *cp, line[LINE_MAX];
182362da0113SBen Gras 
182462da0113SBen Gras 			reply(-226, "Command error messages:");
182562da0113SBen Gras 			rewind(errf);
182662da0113SBen Gras 			while (fgets(line, sizeof(line), errf) != NULL) {
182762da0113SBen Gras 				if ((cp = strchr(line, '\n')) != NULL)
182862da0113SBen Gras 					*cp = '\0';
182962da0113SBen Gras 				reply(0, "  %s", line);
183062da0113SBen Gras 			}
183162da0113SBen Gras 			(void) fflush(stdout);
183262da0113SBen Gras 			(void) fclose(errf);
183362da0113SBen Gras 				/* a reply(226,) must follow */
183462da0113SBen Gras 		}
183562da0113SBen Gras 		reply(226, "Transfer complete.");
183662da0113SBen Gras 	}
183762da0113SBen Gras  cleanupretrieve:
183862da0113SBen Gras 	if (stderrfd != -1)
183962da0113SBen Gras 		(void)close(stderrfd);
184062da0113SBen Gras 	if (isconversion)
184162da0113SBen Gras 		free(argv);
184262da0113SBen Gras }
184362da0113SBen Gras 
184462da0113SBen Gras void
store(const char * name,const char * fmode,int unique)184562da0113SBen Gras store(const char *name, const char *fmode, int unique)
184662da0113SBen Gras {
184762da0113SBen Gras 	FILE *fout, *din;
184862da0113SBen Gras 	struct stat st;
184962da0113SBen Gras 	int (*closefunc)(FILE *);
185062da0113SBen Gras 	struct timeval start, finish, td, *tdp;
185162da0113SBen Gras 	const char *desc, *error;
185262da0113SBen Gras 
185362da0113SBen Gras 	din = NULL;
185462da0113SBen Gras 	desc = (*fmode == 'w') ? "put" : "append";
185562da0113SBen Gras 	error = NULL;
185662da0113SBen Gras 	if (unique && stat(name, &st) == 0 &&
185762da0113SBen Gras 	    (name = gunique(name)) == NULL) {
185862da0113SBen Gras 		logxfer(desc, -1, name, NULL, NULL,
185962da0113SBen Gras 		    "cannot create unique file");
186062da0113SBen Gras 		goto cleanupstore;
186162da0113SBen Gras 	}
186262da0113SBen Gras 
186362da0113SBen Gras 	if (restart_point)
186462da0113SBen Gras 		fmode = "r+";
186562da0113SBen Gras 	fout = fopen(name, fmode);
186662da0113SBen Gras 	closefunc = fclose;
186762da0113SBen Gras 	tdp = NULL;
186862da0113SBen Gras 	if (fout == NULL) {
186962da0113SBen Gras 		perror_reply(553, name);
187062da0113SBen Gras 		logxfer(desc, -1, name, NULL, NULL, strerror(errno));
187162da0113SBen Gras 		goto cleanupstore;
187262da0113SBen Gras 	}
187362da0113SBen Gras 	byte_count = -1;
187462da0113SBen Gras 	if (restart_point) {
187562da0113SBen Gras 		if (type == TYPE_A) {
187662da0113SBen Gras 			off_t i;
187762da0113SBen Gras 			int c;
187862da0113SBen Gras 
187962da0113SBen Gras 			for (i = 0; i < restart_point; i++) {
188062da0113SBen Gras 				if ((c=getc(fout)) == EOF) {
188162da0113SBen Gras 					error = strerror(errno);
188262da0113SBen Gras 					perror_reply(550, name);
188362da0113SBen Gras 					goto done;
188462da0113SBen Gras 				}
188562da0113SBen Gras 				if (c == '\n')
188662da0113SBen Gras 					i++;
188762da0113SBen Gras 			}
188862da0113SBen Gras 			/*
188962da0113SBen Gras 			 * We must do this seek to "current" position
189062da0113SBen Gras 			 * because we are changing from reading to
189162da0113SBen Gras 			 * writing.
189262da0113SBen Gras 			 */
189362da0113SBen Gras 			if (fseek(fout, 0L, SEEK_CUR) < 0) {
189462da0113SBen Gras 				error = strerror(errno);
189562da0113SBen Gras 				perror_reply(550, name);
189662da0113SBen Gras 				goto done;
189762da0113SBen Gras 			}
189862da0113SBen Gras 		} else if (lseek(fileno(fout), restart_point, SEEK_SET) < 0) {
189962da0113SBen Gras 			error = strerror(errno);
190062da0113SBen Gras 			perror_reply(550, name);
190162da0113SBen Gras 			goto done;
190262da0113SBen Gras 		}
190362da0113SBen Gras 	}
190462da0113SBen Gras 	din = dataconn(name, (off_t)-1, "r");
190562da0113SBen Gras 	if (din == NULL)
190662da0113SBen Gras 		goto done;
190762da0113SBen Gras 	(void)gettimeofday(&start, NULL);
190862da0113SBen Gras 	if (receive_data(din, fout) == 0) {
190962da0113SBen Gras 		if (unique)
191062da0113SBen Gras 			reply(226, "Transfer complete (unique file name:%s).",
191162da0113SBen Gras 			    name);
191262da0113SBen Gras 		else
191362da0113SBen Gras 			reply(226, "Transfer complete.");
191462da0113SBen Gras 	}
191562da0113SBen Gras 	(void)gettimeofday(&finish, NULL);
191662da0113SBen Gras 	closedataconn(din);		/* close now to affect timing stats */
191762da0113SBen Gras 	timersub(&finish, &start, &td);
191862da0113SBen Gras 	tdp = &td;
191962da0113SBen Gras  done:
192062da0113SBen Gras 	logxfer(desc, byte_count, name, NULL, tdp, error);
192162da0113SBen Gras 	(*closefunc)(fout);
192262da0113SBen Gras  cleanupstore:
192362da0113SBen Gras 	;
192462da0113SBen Gras }
192562da0113SBen Gras 
192662da0113SBen Gras static FILE *
getdatasock(const char * fmode)192762da0113SBen Gras getdatasock(const char *fmode)
192862da0113SBen Gras {
192962da0113SBen Gras 	int		on, s, t, tries;
193062da0113SBen Gras 	in_port_t	port;
193162da0113SBen Gras 
193262da0113SBen Gras 	on = 1;
193362da0113SBen Gras 	if (data >= 0)
193462da0113SBen Gras 		return (fdopen(data, fmode));
193562da0113SBen Gras 	if (! dropprivs)
193662da0113SBen Gras 		(void) seteuid((uid_t)0);
193762da0113SBen Gras 	s = socket(ctrl_addr.su_family, SOCK_STREAM, 0);
193862da0113SBen Gras 	if (s < 0)
193962da0113SBen Gras 		goto bad;
194062da0113SBen Gras 	if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
194162da0113SBen Gras 	    (char *) &on, sizeof(on)) < 0)
194262da0113SBen Gras 		goto bad;
194362da0113SBen Gras 	if (setsockopt(s, SOL_SOCKET, SO_KEEPALIVE,
194462da0113SBen Gras 	    (char *) &on, sizeof(on)) < 0)
194562da0113SBen Gras 		goto bad;
194662da0113SBen Gras 			/* anchor socket to avoid multi-homing problems */
194762da0113SBen Gras 	data_source = ctrl_addr;
194862da0113SBen Gras 			/*
194962da0113SBen Gras 			 * By default source port for PORT connctions is
195062da0113SBen Gras 			 * ctrlport-1 (see RFC959 section 5.2).
195162da0113SBen Gras 			 * However, if privs have been dropped and that
195262da0113SBen Gras 			 * would be < IPPORT_RESERVED, use a random port
195362da0113SBen Gras 			 * instead.
195462da0113SBen Gras 			 */
195562da0113SBen Gras 	if (dataport)
195662da0113SBen Gras 		port = dataport;
195762da0113SBen Gras 	else
195862da0113SBen Gras 		port = ntohs(ctrl_addr.su_port) - 1;
195962da0113SBen Gras 	if (dropprivs && port < IPPORT_RESERVED)
196062da0113SBen Gras 		port = 0;		/* use random port */
196162da0113SBen Gras 	data_source.su_port = htons(port);
196262da0113SBen Gras 
196362da0113SBen Gras 	for (tries = 1; ; tries++) {
196462da0113SBen Gras 		if (bind(s, (struct sockaddr *)&data_source.si_su,
196562da0113SBen Gras 		    data_source.su_len) >= 0)
196662da0113SBen Gras 			break;
196762da0113SBen Gras 		if (errno != EADDRINUSE || tries > 10)
196862da0113SBen Gras 			goto bad;
196962da0113SBen Gras 		sleep(tries);
197062da0113SBen Gras 	}
197162da0113SBen Gras 	if (! dropprivs)
197262da0113SBen Gras 		(void) seteuid((uid_t)pw->pw_uid);
197362da0113SBen Gras #ifdef IP_TOS
197462da0113SBen Gras 	if (!mapped && ctrl_addr.su_family == AF_INET) {
197562da0113SBen Gras 		on = IPTOS_THROUGHPUT;
197662da0113SBen Gras 		if (setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&on,
197762da0113SBen Gras 			       sizeof(int)) < 0)
197862da0113SBen Gras 			syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
197962da0113SBen Gras 	}
198062da0113SBen Gras #endif
198162da0113SBen Gras 	return (fdopen(s, fmode));
198262da0113SBen Gras  bad:
198362da0113SBen Gras 		/* Return the real value of errno (close may change it) */
198462da0113SBen Gras 	t = errno;
198562da0113SBen Gras 	if (! dropprivs)
198662da0113SBen Gras 		(void) seteuid((uid_t)pw->pw_uid);
198762da0113SBen Gras 	(void) close(s);
198862da0113SBen Gras 	errno = t;
198962da0113SBen Gras 	return (NULL);
199062da0113SBen Gras }
199162da0113SBen Gras 
199262da0113SBen Gras FILE *
dataconn(const char * name,off_t size,const char * fmode)199362da0113SBen Gras dataconn(const char *name, off_t size, const char *fmode)
199462da0113SBen Gras {
199562da0113SBen Gras 	char sizebuf[32];
199662da0113SBen Gras 	FILE *file;
199762da0113SBen Gras 	int retry, tos, keepalive, conerrno;
199862da0113SBen Gras 
199962da0113SBen Gras 	file_size = size;
200062da0113SBen Gras 	byte_count = 0;
200162da0113SBen Gras 	if (size != (off_t) -1)
200262da0113SBen Gras 		(void)snprintf(sizebuf, sizeof(sizebuf), " (" LLF " byte%s)",
200362da0113SBen Gras 		    (LLT)size, PLURAL(size));
200462da0113SBen Gras 	else
200562da0113SBen Gras 		sizebuf[0] = '\0';
200662da0113SBen Gras 	if (pdata >= 0) {
200762da0113SBen Gras 		struct sockinet from;
200862da0113SBen Gras 		int s;
200962da0113SBen Gras 		socklen_t fromlen = sizeof(from.su_len);
201062da0113SBen Gras 
201162da0113SBen Gras 		(void) alarm(curclass.timeout);
201262da0113SBen Gras 		s = accept(pdata, (struct sockaddr *)&from.si_su, &fromlen);
201362da0113SBen Gras 		(void) alarm(0);
201462da0113SBen Gras 		if (s < 0) {
201562da0113SBen Gras 			reply(425, "Can't open data connection.");
201662da0113SBen Gras 			(void) close(pdata);
201762da0113SBen Gras 			pdata = -1;
201862da0113SBen Gras 			return (NULL);
201962da0113SBen Gras 		}
202062da0113SBen Gras 		(void) close(pdata);
202162da0113SBen Gras 		pdata = s;
202262da0113SBen Gras 		switch (from.su_family) {
202362da0113SBen Gras 		case AF_INET:
202462da0113SBen Gras #ifdef IP_TOS
202562da0113SBen Gras 			if (!mapped) {
202662da0113SBen Gras 				tos = IPTOS_THROUGHPUT;
202762da0113SBen Gras 				(void) setsockopt(s, IPPROTO_IP, IP_TOS,
202862da0113SBen Gras 				    (char *)&tos, sizeof(int));
202962da0113SBen Gras 			}
203062da0113SBen Gras 			break;
203162da0113SBen Gras #endif
203262da0113SBen Gras 		}
203362da0113SBen Gras 		/* Set keepalives on the socket to detect dropped conns. */
203462da0113SBen Gras #ifdef SO_KEEPALIVE
203562da0113SBen Gras 		keepalive = 1;
203662da0113SBen Gras 		(void) setsockopt(s, SOL_SOCKET, SO_KEEPALIVE,
203762da0113SBen Gras 		    (char *)&keepalive, sizeof(int));
203862da0113SBen Gras #endif
203962da0113SBen Gras 		reply(150, "Opening %s mode data connection for '%s'%s.",
204062da0113SBen Gras 		     type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf);
204162da0113SBen Gras 		return (fdopen(pdata, fmode));
204262da0113SBen Gras 	}
204362da0113SBen Gras 	if (data >= 0) {
204462da0113SBen Gras 		reply(125, "Using existing data connection for '%s'%s.",
204562da0113SBen Gras 		    name, sizebuf);
204662da0113SBen Gras 		usedefault = 1;
204762da0113SBen Gras 		return (fdopen(data, fmode));
204862da0113SBen Gras 	}
204962da0113SBen Gras 	if (usedefault)
205062da0113SBen Gras 		data_dest = his_addr;
205162da0113SBen Gras 	usedefault = 1;
205262da0113SBen Gras 	retry = conerrno = 0;
205362da0113SBen Gras 	do {
205462da0113SBen Gras 		file = getdatasock(fmode);
205562da0113SBen Gras 		if (file == NULL) {
205662da0113SBen Gras 			char hbuf[NI_MAXHOST];
205762da0113SBen Gras 			char pbuf[NI_MAXSERV];
205862da0113SBen Gras 
205962da0113SBen Gras 			if (getnameinfo((struct sockaddr *)&data_source.si_su,
206062da0113SBen Gras 			    data_source.su_len, hbuf, sizeof(hbuf), pbuf,
206162da0113SBen Gras 			    sizeof(pbuf), NI_NUMERICHOST | NI_NUMERICSERV))
206262da0113SBen Gras 				strlcpy(hbuf, "?", sizeof(hbuf));
206362da0113SBen Gras 			reply(425, "Can't create data socket (%s,%s): %s.",
206462da0113SBen Gras 			      hbuf, pbuf, strerror(errno));
206562da0113SBen Gras 			return (NULL);
206662da0113SBen Gras 		}
206762da0113SBen Gras 		data = fileno(file);
206862da0113SBen Gras 		conerrno = 0;
206962da0113SBen Gras 		if (connect(data, (struct sockaddr *)&data_dest.si_su,
207062da0113SBen Gras 		    data_dest.su_len) == 0)
207162da0113SBen Gras 			break;
207262da0113SBen Gras 		conerrno = errno;
207362da0113SBen Gras 		(void) fclose(file);
207462da0113SBen Gras 		file = NULL;
207562da0113SBen Gras 		data = -1;
207662da0113SBen Gras 		if (conerrno == EADDRINUSE) {
207762da0113SBen Gras 			sleep((unsigned) swaitint);
207862da0113SBen Gras 			retry += swaitint;
207962da0113SBen Gras 		} else {
208062da0113SBen Gras 			break;
208162da0113SBen Gras 		}
208262da0113SBen Gras 	} while (retry <= swaitmax);
208362da0113SBen Gras 	if (conerrno != 0) {
208462da0113SBen Gras 		perror_reply(425, "Can't build data connection");
208562da0113SBen Gras 		return (NULL);
208662da0113SBen Gras 	}
208762da0113SBen Gras 	reply(150, "Opening %s mode data connection for '%s'%s.",
208862da0113SBen Gras 	     type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf);
208962da0113SBen Gras 	return (file);
209062da0113SBen Gras }
209162da0113SBen Gras 
209262da0113SBen Gras void
closedataconn(FILE * fd)209362da0113SBen Gras closedataconn(FILE *fd)
209462da0113SBen Gras {
209562da0113SBen Gras 
209662da0113SBen Gras 	if (fd == NULL)
209762da0113SBen Gras 		return;
209862da0113SBen Gras 	(void)fclose(fd);
209962da0113SBen Gras 	data = -1;
210062da0113SBen Gras 	if (pdata >= 0)
210162da0113SBen Gras 		(void)close(pdata);
210262da0113SBen Gras 	pdata = -1;
210362da0113SBen Gras }
210462da0113SBen Gras 
210562da0113SBen Gras int
write_data(int fd,char * buf,size_t size,off_t * bufrem,struct timeval * then,int isdata)210662da0113SBen Gras write_data(int fd, char *buf, size_t size, off_t *bufrem,
210762da0113SBen Gras     struct timeval *then, int isdata)
210862da0113SBen Gras {
210962da0113SBen Gras 	struct timeval now, td;
211062da0113SBen Gras 	ssize_t c;
211162da0113SBen Gras 
211262da0113SBen Gras 	while (size > 0) {
211362da0113SBen Gras 		c = size;
211462da0113SBen Gras 		if (curclass.writesize) {
211562da0113SBen Gras 			if (curclass.writesize < c)
211662da0113SBen Gras 				c = curclass.writesize;
211762da0113SBen Gras 		}
211862da0113SBen Gras 		if (curclass.rateget) {
211962da0113SBen Gras 			if (*bufrem < c)
212062da0113SBen Gras 				c = *bufrem;
212162da0113SBen Gras 		}
212262da0113SBen Gras 		(void) alarm(curclass.timeout);
212362da0113SBen Gras 		c = write(fd, buf, c);
212462da0113SBen Gras 		if (c <= 0)
212562da0113SBen Gras 			return (1);
212662da0113SBen Gras 		buf += c;
212762da0113SBen Gras 		size -= c;
212862da0113SBen Gras 		byte_count += c;
212962da0113SBen Gras 		if (isdata) {
213062da0113SBen Gras 			total_data_out += c;
213162da0113SBen Gras 			total_data += c;
213262da0113SBen Gras 		}
213362da0113SBen Gras 		total_bytes_out += c;
213462da0113SBen Gras 		total_bytes += c;
213562da0113SBen Gras 		if (curclass.rateget) {
213662da0113SBen Gras 			*bufrem -= c;
213762da0113SBen Gras 			if (*bufrem == 0) {
213862da0113SBen Gras 				(void)gettimeofday(&now, NULL);
213962da0113SBen Gras 				timersub(&now, then, &td);
214062da0113SBen Gras 				if (td.tv_sec == 0) {
214162da0113SBen Gras 					usleep(1000000 - td.tv_usec);
214262da0113SBen Gras 					(void)gettimeofday(then, NULL);
214362da0113SBen Gras 				} else
214462da0113SBen Gras 					*then = now;
214562da0113SBen Gras 				*bufrem = curclass.rateget;
214662da0113SBen Gras 			}
214762da0113SBen Gras 		}
214862da0113SBen Gras 	}
214962da0113SBen Gras 	return (0);
215062da0113SBen Gras }
215162da0113SBen Gras 
215262da0113SBen Gras static enum send_status
send_data_with_read(int filefd,int netfd,const struct stat * st,int isdata)215362da0113SBen Gras send_data_with_read(int filefd, int netfd, const struct stat *st, int isdata)
215462da0113SBen Gras {
215562da0113SBen Gras 	struct timeval then;
215662da0113SBen Gras 	off_t bufrem;
215762da0113SBen Gras 	ssize_t readsize;
215862da0113SBen Gras 	char *buf;
215962da0113SBen Gras 	int c, error;
216062da0113SBen Gras 
216162da0113SBen Gras 	if (curclass.readsize > 0)
216262da0113SBen Gras 		readsize = curclass.readsize;
216362da0113SBen Gras 	else
216462da0113SBen Gras 		readsize = st->st_blksize;
216562da0113SBen Gras 	if ((buf = malloc(readsize)) == NULL) {
216662da0113SBen Gras 		perror_reply(451, "Local resource failure: malloc");
216762da0113SBen Gras 		return (SS_NO_TRANSFER);
216862da0113SBen Gras 	}
216962da0113SBen Gras 
217062da0113SBen Gras 	if (curclass.rateget) {
217162da0113SBen Gras 		bufrem = curclass.rateget;
217262da0113SBen Gras 		(void)gettimeofday(&then, NULL);
217362da0113SBen Gras 	} else
217462da0113SBen Gras 		bufrem = readsize;
217584d9c625SLionel Sambuc 	for (;;) {
217662da0113SBen Gras 		(void) alarm(curclass.timeout);
217762da0113SBen Gras 		c = read(filefd, buf, readsize);
217862da0113SBen Gras 		if (c == 0)
217962da0113SBen Gras 			error = SS_SUCCESS;
218062da0113SBen Gras 		else if (c < 0)
218162da0113SBen Gras 			error = SS_FILE_ERROR;
218262da0113SBen Gras 		else if (write_data(netfd, buf, c, &bufrem, &then, isdata))
218362da0113SBen Gras 			error = SS_DATA_ERROR;
218462da0113SBen Gras 		else if (urgflag && handleoobcmd())
218562da0113SBen Gras 			error = SS_ABORTED;
218662da0113SBen Gras 		else
218762da0113SBen Gras 			continue;
218862da0113SBen Gras 
218962da0113SBen Gras 		free(buf);
219062da0113SBen Gras 		return (error);
219162da0113SBen Gras 	}
219262da0113SBen Gras }
219362da0113SBen Gras 
219462da0113SBen Gras static enum send_status
send_data_with_mmap(int filefd,int netfd,const struct stat * st,int isdata)219562da0113SBen Gras send_data_with_mmap(int filefd, int netfd, const struct stat *st, int isdata)
219662da0113SBen Gras {
219762da0113SBen Gras 	struct timeval then;
219862da0113SBen Gras 	off_t bufrem, filesize, off, origoff;
219962da0113SBen Gras 	ssize_t mapsize, winsize;
220062da0113SBen Gras 	int error, sendbufsize, sendlowat;
220162da0113SBen Gras 	void *win;
220262da0113SBen Gras 
220362da0113SBen Gras 	bufrem = 0;
220462da0113SBen Gras 	if (curclass.sendbufsize) {
220562da0113SBen Gras 		sendbufsize = curclass.sendbufsize;
220662da0113SBen Gras 		if (setsockopt(netfd, SOL_SOCKET, SO_SNDBUF,
220762da0113SBen Gras 		    &sendbufsize, sizeof(int)) == -1)
220862da0113SBen Gras 			syslog(LOG_WARNING, "setsockopt(SO_SNDBUF, %d): %m",
220962da0113SBen Gras 			    sendbufsize);
221062da0113SBen Gras 	}
221162da0113SBen Gras 
221262da0113SBen Gras 	if (curclass.sendlowat) {
221362da0113SBen Gras 		sendlowat = curclass.sendlowat;
221462da0113SBen Gras 		if (setsockopt(netfd, SOL_SOCKET, SO_SNDLOWAT,
221562da0113SBen Gras 		    &sendlowat, sizeof(int)) == -1)
221662da0113SBen Gras 			syslog(LOG_WARNING, "setsockopt(SO_SNDLOWAT, %d): %m",
221762da0113SBen Gras 			    sendlowat);
221862da0113SBen Gras 	}
221962da0113SBen Gras 
222062da0113SBen Gras 	winsize = curclass.mmapsize;
222162da0113SBen Gras 	filesize = st->st_size;
222262da0113SBen Gras 	if (ftpd_debug)
222362da0113SBen Gras 		syslog(LOG_INFO, "mmapsize = " LLF ", writesize = " LLF,
222462da0113SBen Gras 		    (LLT)winsize, (LLT)curclass.writesize);
222562da0113SBen Gras 	if (winsize <= 0)
222662da0113SBen Gras 		goto try_read;
222762da0113SBen Gras 
222862da0113SBen Gras 	off = lseek(filefd, (off_t)0, SEEK_CUR);
222962da0113SBen Gras 	if (off == -1)
223062da0113SBen Gras 		goto try_read;
223162da0113SBen Gras 
223262da0113SBen Gras 	origoff = off;
223362da0113SBen Gras 	if (curclass.rateget) {
223462da0113SBen Gras 		bufrem = curclass.rateget;
223562da0113SBen Gras 		(void)gettimeofday(&then, NULL);
223662da0113SBen Gras 	} else
223762da0113SBen Gras 		bufrem = winsize;
223862da0113SBen Gras 	while (1) {
223962da0113SBen Gras 		mapsize = MIN(filesize - off, winsize);
224062da0113SBen Gras 		if (mapsize == 0)
224162da0113SBen Gras 			break;
224262da0113SBen Gras 		win = mmap(NULL, mapsize, PROT_READ,
224362da0113SBen Gras 		    MAP_FILE|MAP_SHARED, filefd, off);
224462da0113SBen Gras 		if (win == MAP_FAILED) {
224562da0113SBen Gras 			if (off == origoff)
224662da0113SBen Gras 				goto try_read;
224762da0113SBen Gras 			return (SS_FILE_ERROR);
224862da0113SBen Gras 		}
22490a6a1f1dSLionel Sambuc #if !defined(__minix)
225062da0113SBen Gras 		(void) madvise(win, mapsize, MADV_SEQUENTIAL);
22510a6a1f1dSLionel Sambuc #endif /* !defined(__minix) */
225262da0113SBen Gras 		error = write_data(netfd, win, mapsize, &bufrem, &then,
225362da0113SBen Gras 		    isdata);
22540a6a1f1dSLionel Sambuc #if !defined(__minix)
225562da0113SBen Gras 		(void) madvise(win, mapsize, MADV_DONTNEED);
22560a6a1f1dSLionel Sambuc #endif /* !defined(__minix) */
225762da0113SBen Gras 		munmap(win, mapsize);
225862da0113SBen Gras 		if (urgflag && handleoobcmd())
225962da0113SBen Gras 			return (SS_ABORTED);
226062da0113SBen Gras 		if (error)
226162da0113SBen Gras 			return (SS_DATA_ERROR);
226262da0113SBen Gras 		off += mapsize;
226362da0113SBen Gras 	}
226462da0113SBen Gras 	return (SS_SUCCESS);
226562da0113SBen Gras 
226662da0113SBen Gras  try_read:
226762da0113SBen Gras 	return (send_data_with_read(filefd, netfd, st, isdata));
226862da0113SBen Gras }
226962da0113SBen Gras 
227062da0113SBen Gras /*
227162da0113SBen Gras  * Transfer the contents of "instr" to "outstr" peer using the appropriate
227262da0113SBen Gras  * encapsulation of the data subject to Mode, Structure, and Type.
227362da0113SBen Gras  *
227462da0113SBen Gras  * NB: Form isn't handled.
227562da0113SBen Gras  */
227662da0113SBen Gras static int
send_data(FILE * instr,FILE * outstr,const struct stat * st,int isdata)227762da0113SBen Gras send_data(FILE *instr, FILE *outstr, const struct stat *st, int isdata)
227862da0113SBen Gras {
227962da0113SBen Gras 	int	 c, filefd, netfd, rval;
228062da0113SBen Gras 
228162da0113SBen Gras 	urgflag = 0;
228262da0113SBen Gras 	transflag = 1;
228362da0113SBen Gras 	rval = -1;
228462da0113SBen Gras 
228562da0113SBen Gras 	switch (type) {
228662da0113SBen Gras 
228762da0113SBen Gras 	case TYPE_A:
228862da0113SBen Gras  /* XXXLUKEM: rate limit ascii send (get) */
228962da0113SBen Gras 		(void) alarm(curclass.timeout);
229062da0113SBen Gras 		while ((c = getc(instr)) != EOF) {
229162da0113SBen Gras 			if (urgflag && handleoobcmd())
229262da0113SBen Gras 				goto cleanup_send_data;
229362da0113SBen Gras 			byte_count++;
229462da0113SBen Gras 			if (c == '\n') {
229562da0113SBen Gras 				if (ferror(outstr))
229662da0113SBen Gras 					goto data_err;
229762da0113SBen Gras 				(void) putc('\r', outstr);
229862da0113SBen Gras 				if (isdata) {
229962da0113SBen Gras 					total_data_out++;
230062da0113SBen Gras 					total_data++;
230162da0113SBen Gras 				}
230262da0113SBen Gras 				total_bytes_out++;
230362da0113SBen Gras 				total_bytes++;
230462da0113SBen Gras 			}
230562da0113SBen Gras 			(void) putc(c, outstr);
230662da0113SBen Gras 			if (isdata) {
230762da0113SBen Gras 				total_data_out++;
230862da0113SBen Gras 				total_data++;
230962da0113SBen Gras 			}
231062da0113SBen Gras 			total_bytes_out++;
231162da0113SBen Gras 			total_bytes++;
231262da0113SBen Gras 			if ((byte_count % 4096) == 0)
231362da0113SBen Gras 				(void) alarm(curclass.timeout);
231462da0113SBen Gras 		}
231562da0113SBen Gras 		(void) alarm(0);
231662da0113SBen Gras 		fflush(outstr);
231762da0113SBen Gras 		if (ferror(instr))
231862da0113SBen Gras 			goto file_err;
231962da0113SBen Gras 		if (ferror(outstr))
232062da0113SBen Gras 			goto data_err;
232162da0113SBen Gras 		rval = 0;
232262da0113SBen Gras 		goto cleanup_send_data;
232362da0113SBen Gras 
232462da0113SBen Gras 	case TYPE_I:
232562da0113SBen Gras 	case TYPE_L:
232662da0113SBen Gras 		filefd = fileno(instr);
232762da0113SBen Gras 		netfd = fileno(outstr);
232862da0113SBen Gras 		switch (send_data_with_mmap(filefd, netfd, st, isdata)) {
232962da0113SBen Gras 
233062da0113SBen Gras 		case SS_SUCCESS:
233162da0113SBen Gras 			break;
233262da0113SBen Gras 
233362da0113SBen Gras 		case SS_ABORTED:
233462da0113SBen Gras 		case SS_NO_TRANSFER:
233562da0113SBen Gras 			goto cleanup_send_data;
233662da0113SBen Gras 
233762da0113SBen Gras 		case SS_FILE_ERROR:
233862da0113SBen Gras 			goto file_err;
233962da0113SBen Gras 
234062da0113SBen Gras 		case SS_DATA_ERROR:
234162da0113SBen Gras 			goto data_err;
234262da0113SBen Gras 		}
234362da0113SBen Gras 		rval = 0;
234462da0113SBen Gras 		goto cleanup_send_data;
234562da0113SBen Gras 
234662da0113SBen Gras 	default:
234762da0113SBen Gras 		reply(550, "Unimplemented TYPE %d in send_data", type);
234862da0113SBen Gras 		goto cleanup_send_data;
234962da0113SBen Gras 	}
235062da0113SBen Gras 
235162da0113SBen Gras  data_err:
235262da0113SBen Gras 	(void) alarm(0);
235362da0113SBen Gras 	perror_reply(426, "Data connection");
235462da0113SBen Gras 	goto cleanup_send_data;
235562da0113SBen Gras 
235662da0113SBen Gras  file_err:
235762da0113SBen Gras 	(void) alarm(0);
235862da0113SBen Gras 	perror_reply(551, "Error on input file");
235962da0113SBen Gras 	goto cleanup_send_data;
236062da0113SBen Gras 
236162da0113SBen Gras  cleanup_send_data:
236262da0113SBen Gras 	(void) alarm(0);
236362da0113SBen Gras 	transflag = 0;
236462da0113SBen Gras 	urgflag = 0;
236562da0113SBen Gras 	if (isdata) {
236662da0113SBen Gras 		total_files_out++;
236762da0113SBen Gras 		total_files++;
236862da0113SBen Gras 	}
236962da0113SBen Gras 	total_xfers_out++;
237062da0113SBen Gras 	total_xfers++;
237162da0113SBen Gras 	return (rval);
237262da0113SBen Gras }
237362da0113SBen Gras 
237462da0113SBen Gras /*
237562da0113SBen Gras  * Transfer data from peer to "outstr" using the appropriate encapulation of
237662da0113SBen Gras  * the data subject to Mode, Structure, and Type.
237762da0113SBen Gras  *
237862da0113SBen Gras  * N.B.: Form isn't handled.
237962da0113SBen Gras  */
238062da0113SBen Gras static int
receive_data(FILE * instr,FILE * outstr)238162da0113SBen Gras receive_data(FILE *instr, FILE *outstr)
238262da0113SBen Gras {
238362da0113SBen Gras 	int	c, netfd, filefd, rval;
238462da0113SBen Gras 	int	volatile bare_lfs;
238562da0113SBen Gras 	off_t	byteswritten;
238662da0113SBen Gras 	char	*buf;
238762da0113SBen Gras 	ssize_t	readsize;
238862da0113SBen Gras 	struct sigaction sa, sa_saved;
238962da0113SBen Gras 	struct stat st;
239062da0113SBen Gras 
239162da0113SBen Gras 	memset(&sa, 0, sizeof(sa));
239262da0113SBen Gras 	sigfillset(&sa.sa_mask);
239362da0113SBen Gras 	sa.sa_flags = SA_RESTART;
239462da0113SBen Gras 	sa.sa_handler = lostconn;
239562da0113SBen Gras 	(void) sigaction(SIGALRM, &sa, &sa_saved);
239662da0113SBen Gras 
239762da0113SBen Gras 	bare_lfs = 0;
239862da0113SBen Gras 	urgflag = 0;
239962da0113SBen Gras 	transflag = 1;
240062da0113SBen Gras 	rval = -1;
240162da0113SBen Gras 	byteswritten = 0;
240262da0113SBen Gras 	buf = NULL;
240362da0113SBen Gras 
240462da0113SBen Gras #define FILESIZECHECK(x) \
240562da0113SBen Gras 			do { \
240662da0113SBen Gras 				if (curclass.maxfilesize != -1 && \
240762da0113SBen Gras 				    (x) > curclass.maxfilesize) { \
240862da0113SBen Gras 					errno = EFBIG; \
240962da0113SBen Gras 					goto file_err; \
241062da0113SBen Gras 				} \
241162da0113SBen Gras 			} while (0)
241262da0113SBen Gras 
241362da0113SBen Gras 	switch (type) {
241462da0113SBen Gras 
241562da0113SBen Gras 	case TYPE_I:
241662da0113SBen Gras 	case TYPE_L:
241762da0113SBen Gras 		netfd = fileno(instr);
241862da0113SBen Gras 		filefd = fileno(outstr);
241962da0113SBen Gras 		(void) alarm(curclass.timeout);
242062da0113SBen Gras 		if (curclass.readsize)
242162da0113SBen Gras 			readsize = curclass.readsize;
242284d9c625SLionel Sambuc 		else if (fstat(filefd, &st) != -1)
242362da0113SBen Gras 			readsize = (ssize_t)st.st_blksize;
242462da0113SBen Gras 		else
242562da0113SBen Gras 			readsize = BUFSIZ;
242662da0113SBen Gras 		if ((buf = malloc(readsize)) == NULL) {
242762da0113SBen Gras 			perror_reply(451, "Local resource failure: malloc");
242862da0113SBen Gras 			goto cleanup_recv_data;
242962da0113SBen Gras 		}
243062da0113SBen Gras 		if (curclass.rateput) {
243162da0113SBen Gras 			while (1) {
243262da0113SBen Gras 				int d;
243362da0113SBen Gras 				struct timeval then, now, td;
243462da0113SBen Gras 				off_t bufrem;
243562da0113SBen Gras 
243662da0113SBen Gras 				(void)gettimeofday(&then, NULL);
243762da0113SBen Gras 				errno = c = d = 0;
243862da0113SBen Gras 				for (bufrem = curclass.rateput; bufrem > 0; ) {
243962da0113SBen Gras 					if ((c = read(netfd, buf,
244062da0113SBen Gras 					    MIN(readsize, bufrem))) <= 0)
244162da0113SBen Gras 						goto recvdone;
244262da0113SBen Gras 					if (urgflag && handleoobcmd())
244362da0113SBen Gras 						goto cleanup_recv_data;
244462da0113SBen Gras 					FILESIZECHECK(byte_count + c);
244562da0113SBen Gras 					if ((d = write(filefd, buf, c)) != c)
244662da0113SBen Gras 						goto file_err;
244762da0113SBen Gras 					(void) alarm(curclass.timeout);
244862da0113SBen Gras 					bufrem -= c;
244962da0113SBen Gras 					byte_count += c;
245062da0113SBen Gras 					total_data_in += c;
245162da0113SBen Gras 					total_data += c;
245262da0113SBen Gras 					total_bytes_in += c;
245362da0113SBen Gras 					total_bytes += c;
245462da0113SBen Gras 				}
245562da0113SBen Gras 				(void)gettimeofday(&now, NULL);
245662da0113SBen Gras 				timersub(&now, &then, &td);
245762da0113SBen Gras 				if (td.tv_sec == 0)
245862da0113SBen Gras 					usleep(1000000 - td.tv_usec);
245962da0113SBen Gras 			}
246062da0113SBen Gras 		} else {
246162da0113SBen Gras 			while ((c = read(netfd, buf, readsize)) > 0) {
246262da0113SBen Gras 				if (urgflag && handleoobcmd())
246362da0113SBen Gras 					goto cleanup_recv_data;
246462da0113SBen Gras 				FILESIZECHECK(byte_count + c);
246562da0113SBen Gras 				if (write(filefd, buf, c) != c)
246662da0113SBen Gras 					goto file_err;
246762da0113SBen Gras 				(void) alarm(curclass.timeout);
246862da0113SBen Gras 				byte_count += c;
246962da0113SBen Gras 				total_data_in += c;
247062da0113SBen Gras 				total_data += c;
247162da0113SBen Gras 				total_bytes_in += c;
247262da0113SBen Gras 				total_bytes += c;
247362da0113SBen Gras 			}
247462da0113SBen Gras 		}
247562da0113SBen Gras  recvdone:
247662da0113SBen Gras 		if (c < 0)
247762da0113SBen Gras 			goto data_err;
247862da0113SBen Gras 		rval = 0;
247962da0113SBen Gras 		goto cleanup_recv_data;
248062da0113SBen Gras 
248162da0113SBen Gras 	case TYPE_E:
248262da0113SBen Gras 		reply(553, "TYPE E not implemented.");
248362da0113SBen Gras 		goto cleanup_recv_data;
248462da0113SBen Gras 
248562da0113SBen Gras 	case TYPE_A:
248662da0113SBen Gras 		(void) alarm(curclass.timeout);
248762da0113SBen Gras  /* XXXLUKEM: rate limit ascii receive (put) */
248862da0113SBen Gras 		while ((c = getc(instr)) != EOF) {
248962da0113SBen Gras 			if (urgflag && handleoobcmd())
249062da0113SBen Gras 				goto cleanup_recv_data;
249162da0113SBen Gras 			byte_count++;
249262da0113SBen Gras 			total_data_in++;
249362da0113SBen Gras 			total_data++;
249462da0113SBen Gras 			total_bytes_in++;
249562da0113SBen Gras 			total_bytes++;
249662da0113SBen Gras 			if ((byte_count % 4096) == 0)
249762da0113SBen Gras 				(void) alarm(curclass.timeout);
249862da0113SBen Gras 			if (c == '\n')
249962da0113SBen Gras 				bare_lfs++;
250062da0113SBen Gras 			while (c == '\r') {
250162da0113SBen Gras 				if (ferror(outstr))
250262da0113SBen Gras 					goto data_err;
250362da0113SBen Gras 				if ((c = getc(instr)) != '\n') {
250462da0113SBen Gras 					byte_count++;
250562da0113SBen Gras 					total_data_in++;
250662da0113SBen Gras 					total_data++;
250762da0113SBen Gras 					total_bytes_in++;
250862da0113SBen Gras 					total_bytes++;
250962da0113SBen Gras 					if ((byte_count % 4096) == 0)
251062da0113SBen Gras 						(void) alarm(curclass.timeout);
251162da0113SBen Gras 					byteswritten++;
251262da0113SBen Gras 					FILESIZECHECK(byteswritten);
251362da0113SBen Gras 					(void) putc ('\r', outstr);
251462da0113SBen Gras 					if (c == '\0' || c == EOF)
251562da0113SBen Gras 						goto contin2;
251662da0113SBen Gras 				}
251762da0113SBen Gras 			}
251862da0113SBen Gras 			byteswritten++;
251962da0113SBen Gras 			FILESIZECHECK(byteswritten);
252062da0113SBen Gras 			(void) putc(c, outstr);
252162da0113SBen Gras  contin2:	;
252262da0113SBen Gras 		}
252362da0113SBen Gras 		(void) alarm(0);
252462da0113SBen Gras 		fflush(outstr);
252562da0113SBen Gras 		if (ferror(instr))
252662da0113SBen Gras 			goto data_err;
252762da0113SBen Gras 		if (ferror(outstr))
252862da0113SBen Gras 			goto file_err;
252962da0113SBen Gras 		if (bare_lfs) {
253062da0113SBen Gras 			reply(-226,
253162da0113SBen Gras 			    "WARNING! %d bare linefeeds received in ASCII mode",
253262da0113SBen Gras 			    bare_lfs);
253362da0113SBen Gras 			reply(0, "File may not have transferred correctly.");
253462da0113SBen Gras 		}
253562da0113SBen Gras 		rval = 0;
253662da0113SBen Gras 		goto cleanup_recv_data;
253762da0113SBen Gras 
253862da0113SBen Gras 	default:
253962da0113SBen Gras 		reply(550, "Unimplemented TYPE %d in receive_data", type);
254062da0113SBen Gras 		goto cleanup_recv_data;
254162da0113SBen Gras 	}
254262da0113SBen Gras #undef FILESIZECHECK
254362da0113SBen Gras 
254462da0113SBen Gras  data_err:
254562da0113SBen Gras 	(void) alarm(0);
254662da0113SBen Gras 	perror_reply(426, "Data Connection");
254762da0113SBen Gras 	goto cleanup_recv_data;
254862da0113SBen Gras 
254962da0113SBen Gras  file_err:
255062da0113SBen Gras 	(void) alarm(0);
255162da0113SBen Gras 	perror_reply(452, "Error writing file");
255262da0113SBen Gras 	goto cleanup_recv_data;
255362da0113SBen Gras 
255462da0113SBen Gras  cleanup_recv_data:
255562da0113SBen Gras 	(void) alarm(0);
255662da0113SBen Gras 	(void) sigaction(SIGALRM, &sa_saved, NULL);
255762da0113SBen Gras 	if (buf)
255862da0113SBen Gras 		free(buf);
255962da0113SBen Gras 	transflag = 0;
256062da0113SBen Gras 	urgflag = 0;
256162da0113SBen Gras 	total_files_in++;
256262da0113SBen Gras 	total_files++;
256362da0113SBen Gras 	total_xfers_in++;
256462da0113SBen Gras 	total_xfers++;
256562da0113SBen Gras 	return (rval);
256662da0113SBen Gras }
256762da0113SBen Gras 
256862da0113SBen Gras void
statcmd(void)256962da0113SBen Gras statcmd(void)
257062da0113SBen Gras {
257162da0113SBen Gras 	struct sockinet *su = NULL;
257262da0113SBen Gras 	static char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
257362da0113SBen Gras 	unsigned char *a, *p;
257462da0113SBen Gras 	int ispassive, af;
257562da0113SBen Gras 	off_t otbi, otbo, otb;
257662da0113SBen Gras 
257762da0113SBen Gras 	a = p = NULL;
257862da0113SBen Gras 
257962da0113SBen Gras 	reply(-211, "%s FTP server status:", hostname);
258062da0113SBen Gras 	reply(0, "Version: %s", EMPTYSTR(version) ? "<suppressed>" : version);
258162da0113SBen Gras 	hbuf[0] = '\0';
258262da0113SBen Gras 	if (!getnameinfo((struct sockaddr *)&his_addr.si_su, his_addr.su_len,
258362da0113SBen Gras 			hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST)
258462da0113SBen Gras 	    && strcmp(remotehost, hbuf) != 0)
258562da0113SBen Gras 		reply(0, "Connected to %s (%s)", remotehost, hbuf);
258662da0113SBen Gras 	else
258762da0113SBen Gras 		reply(0, "Connected to %s", remotehost);
258862da0113SBen Gras 
258962da0113SBen Gras 	if (logged_in) {
259062da0113SBen Gras 		if (curclass.type == CLASS_GUEST)
259162da0113SBen Gras 			reply(0, "Logged in anonymously");
259262da0113SBen Gras 		else
259362da0113SBen Gras 			reply(0, "Logged in as %s%s", pw->pw_name,
259462da0113SBen Gras 			    curclass.type == CLASS_CHROOT ? " (chroot)" : "");
259562da0113SBen Gras 	} else if (askpasswd)
259662da0113SBen Gras 		reply(0, "Waiting for password");
259762da0113SBen Gras 	else
259862da0113SBen Gras 		reply(0, "Waiting for user name");
259962da0113SBen Gras 	cprintf(stdout, "    TYPE: %s", typenames[type]);
260062da0113SBen Gras 	if (type == TYPE_A || type == TYPE_E)
260162da0113SBen Gras 		cprintf(stdout, ", FORM: %s", formnames[form]);
260262da0113SBen Gras 	if (type == TYPE_L) {
260362da0113SBen Gras #if NBBY == 8
260462da0113SBen Gras 		cprintf(stdout, " %d", NBBY);
260562da0113SBen Gras #else
260662da0113SBen Gras 			/* XXX: `bytesize' needs to be defined in this case */
260762da0113SBen Gras 		cprintf(stdout, " %d", bytesize);
260862da0113SBen Gras #endif
260962da0113SBen Gras 	}
261062da0113SBen Gras 	cprintf(stdout, "; STRUcture: %s; transfer MODE: %s\r\n",
261162da0113SBen Gras 	    strunames[stru], modenames[mode]);
261262da0113SBen Gras 	ispassive = 0;
261362da0113SBen Gras 	if (data != -1) {
261462da0113SBen Gras 		reply(0, "Data connection open");
261562da0113SBen Gras 		su = NULL;
261662da0113SBen Gras 	} else if (pdata != -1) {
261762da0113SBen Gras 		reply(0, "in Passive mode");
261862da0113SBen Gras 		if (curclass.advertise.su_len != 0)
261962da0113SBen Gras 			su = &curclass.advertise;
262062da0113SBen Gras 		else
262162da0113SBen Gras 			su = &pasv_addr;
262262da0113SBen Gras 		ispassive = 1;
262362da0113SBen Gras 		goto printaddr;
262462da0113SBen Gras 	} else if (usedefault == 0) {
262562da0113SBen Gras 		su = (struct sockinet *)&data_dest;
262662da0113SBen Gras 
262762da0113SBen Gras 		if (epsvall) {
262862da0113SBen Gras 			reply(0, "EPSV only mode (EPSV ALL)");
262962da0113SBen Gras 			goto epsvonly;
263062da0113SBen Gras 		}
263162da0113SBen Gras  printaddr:
263262da0113SBen Gras 							/* PASV/PORT */
263362da0113SBen Gras 		if (su->su_family == AF_INET) {
263462da0113SBen Gras 			a = (unsigned char *) &su->su_addr;
263562da0113SBen Gras 			p = (unsigned char *) &su->su_port;
263662da0113SBen Gras #define UC(b) (((int) b) & 0xff)
263762da0113SBen Gras 			reply(0, "%s (%d,%d,%d,%d,%d,%d)",
263862da0113SBen Gras 				ispassive ? "PASV" : "PORT" ,
263962da0113SBen Gras 				UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
264062da0113SBen Gras 				UC(p[0]), UC(p[1]));
264162da0113SBen Gras 		}
264262da0113SBen Gras 
264362da0113SBen Gras 							/* LPSV/LPRT */
264462da0113SBen Gras 	    {
264562da0113SBen Gras 		int alen, i;
264662da0113SBen Gras 
264762da0113SBen Gras 		alen = 0;
264862da0113SBen Gras 		switch (su->su_family) {
264962da0113SBen Gras 		case AF_INET:
265062da0113SBen Gras 			a = (unsigned char *) &su->su_addr;
265162da0113SBen Gras 			p = (unsigned char *) &su->su_port;
265262da0113SBen Gras 			alen = sizeof(su->su_addr);
265362da0113SBen Gras 			af = 4;
265462da0113SBen Gras 			break;
265562da0113SBen Gras #ifdef INET6
265662da0113SBen Gras 		case AF_INET6:
265762da0113SBen Gras 			a = (unsigned char *) &su->su_6addr;
265862da0113SBen Gras 			p = (unsigned char *) &su->su_port;
265962da0113SBen Gras 			alen = sizeof(su->su_6addr);
266062da0113SBen Gras 			af = 6;
266162da0113SBen Gras 			break;
266262da0113SBen Gras #endif
266362da0113SBen Gras 		default:
266462da0113SBen Gras 			af = 0;
266562da0113SBen Gras 			break;
266662da0113SBen Gras 		}
266762da0113SBen Gras 		if (af) {
266862da0113SBen Gras 			cprintf(stdout, "    %s (%d,%d",
266962da0113SBen Gras 			    ispassive ? "LPSV" : "LPRT", af, alen);
267062da0113SBen Gras 			for (i = 0; i < alen; i++)
267162da0113SBen Gras 				cprintf(stdout, ",%d", UC(a[i]));
267262da0113SBen Gras 			cprintf(stdout, ",%d,%d,%d)\r\n",
267362da0113SBen Gras 			    2, UC(p[0]), UC(p[1]));
267462da0113SBen Gras #undef UC
267562da0113SBen Gras 		}
267662da0113SBen Gras 	    }
267762da0113SBen Gras 
267862da0113SBen Gras 		/* EPRT/EPSV */
267962da0113SBen Gras  epsvonly:
268062da0113SBen Gras 		af = af2epsvproto(su->su_family);
268162da0113SBen Gras 		hbuf[0] = '\0';
268262da0113SBen Gras 		if (af > 0) {
268362da0113SBen Gras 			struct sockinet tmp;
268462da0113SBen Gras 
268562da0113SBen Gras 			tmp = *su;
268662da0113SBen Gras #ifdef INET6
268762da0113SBen Gras 			if (tmp.su_family == AF_INET6)
268862da0113SBen Gras 				tmp.su_scope_id = 0;
268962da0113SBen Gras #endif
269062da0113SBen Gras 			if (getnameinfo((struct sockaddr *)&tmp.si_su,
269162da0113SBen Gras 			    tmp.su_len, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf),
269262da0113SBen Gras 			    NI_NUMERICHOST | NI_NUMERICSERV) == 0)
269362da0113SBen Gras 				reply(0, "%s (|%d|%s|%s|)",
269462da0113SBen Gras 				    ispassive ? "EPSV" : "EPRT",
269562da0113SBen Gras 				    af, hbuf, sbuf);
269662da0113SBen Gras 		}
269762da0113SBen Gras 	} else
269862da0113SBen Gras 		reply(0, "No data connection");
269962da0113SBen Gras 
270062da0113SBen Gras 	if (logged_in) {
270162da0113SBen Gras 		reply(0,
270262da0113SBen Gras 		    "Data sent:        " LLF " byte%s in " LLF " file%s",
270362da0113SBen Gras 		    (LLT)total_data_out, PLURAL(total_data_out),
270462da0113SBen Gras 		    (LLT)total_files_out, PLURAL(total_files_out));
270562da0113SBen Gras 		reply(0,
270662da0113SBen Gras 		    "Data received:    " LLF " byte%s in " LLF " file%s",
270762da0113SBen Gras 		    (LLT)total_data_in, PLURAL(total_data_in),
270862da0113SBen Gras 		    (LLT)total_files_in, PLURAL(total_files_in));
270962da0113SBen Gras 		reply(0,
271062da0113SBen Gras 		    "Total data:       " LLF " byte%s in " LLF " file%s",
271162da0113SBen Gras 		    (LLT)total_data, PLURAL(total_data),
271262da0113SBen Gras 		    (LLT)total_files, PLURAL(total_files));
271362da0113SBen Gras 	}
271462da0113SBen Gras 	otbi = total_bytes_in;
271562da0113SBen Gras 	otbo = total_bytes_out;
271662da0113SBen Gras 	otb = total_bytes;
271762da0113SBen Gras 	reply(0, "Traffic sent:     " LLF " byte%s in " LLF " transfer%s",
271862da0113SBen Gras 	    (LLT)otbo, PLURAL(otbo),
271962da0113SBen Gras 	    (LLT)total_xfers_out, PLURAL(total_xfers_out));
272062da0113SBen Gras 	reply(0, "Traffic received: " LLF " byte%s in " LLF " transfer%s",
272162da0113SBen Gras 	    (LLT)otbi, PLURAL(otbi),
272262da0113SBen Gras 	    (LLT)total_xfers_in, PLURAL(total_xfers_in));
272362da0113SBen Gras 	reply(0, "Total traffic:    " LLF " byte%s in " LLF " transfer%s",
272462da0113SBen Gras 	    (LLT)otb, PLURAL(otb),
272562da0113SBen Gras 	    (LLT)total_xfers, PLURAL(total_xfers));
272662da0113SBen Gras 
272762da0113SBen Gras 	if (logged_in && !CURCLASS_FLAGS_ISSET(private)) {
272862da0113SBen Gras 		struct ftpconv *cp;
272962da0113SBen Gras 
273062da0113SBen Gras 		reply(0, "%s", "");
273162da0113SBen Gras 		reply(0, "Class: %s, type: %s",
273262da0113SBen Gras 		    curclass.classname, CURCLASSTYPE);
273362da0113SBen Gras 		reply(0, "Check PORT/LPRT commands: %sabled",
273462da0113SBen Gras 		    CURCLASS_FLAGS_ISSET(checkportcmd) ? "en" : "dis");
273562da0113SBen Gras 		if (! EMPTYSTR(curclass.display))
273662da0113SBen Gras 			reply(0, "Display file: %s", curclass.display);
273762da0113SBen Gras 		if (! EMPTYSTR(curclass.notify))
273862da0113SBen Gras 			reply(0, "Notify fileglob: %s", curclass.notify);
273962da0113SBen Gras 		reply(0, "Idle timeout: " LLF ", maximum timeout: " LLF,
274062da0113SBen Gras 		    (LLT)curclass.timeout, (LLT)curclass.maxtimeout);
274162da0113SBen Gras 		reply(0, "Current connections: %d", connections);
274262da0113SBen Gras 		if (curclass.limit == -1)
274362da0113SBen Gras 			reply(0, "Maximum connections: unlimited");
274462da0113SBen Gras 		else
274562da0113SBen Gras 			reply(0, "Maximum connections: " LLF,
274662da0113SBen Gras 			    (LLT)curclass.limit);
274762da0113SBen Gras 		if (curclass.limitfile)
274862da0113SBen Gras 			reply(0, "Connection limit exceeded message file: %s",
274962da0113SBen Gras 			    conffilename(curclass.limitfile));
275062da0113SBen Gras 		if (! EMPTYSTR(curclass.chroot))
275162da0113SBen Gras 			reply(0, "Chroot format: %s", curclass.chroot);
275262da0113SBen Gras 		reply(0, "Deny bad ftpusers(5) quickly: %sabled",
275362da0113SBen Gras 		    CURCLASS_FLAGS_ISSET(denyquick) ? "en" : "dis");
275462da0113SBen Gras 		if (! EMPTYSTR(curclass.homedir))
275562da0113SBen Gras 			reply(0, "Homedir format: %s", curclass.homedir);
275662da0113SBen Gras 		if (curclass.maxfilesize == -1)
275762da0113SBen Gras 			reply(0, "Maximum file size: unlimited");
275862da0113SBen Gras 		else
275962da0113SBen Gras 			reply(0, "Maximum file size: " LLF,
276062da0113SBen Gras 			    (LLT)curclass.maxfilesize);
276162da0113SBen Gras 		if (! EMPTYSTR(curclass.motd))
276262da0113SBen Gras 			reply(0, "MotD file: %s", conffilename(curclass.motd));
276362da0113SBen Gras 		reply(0,
276462da0113SBen Gras 	    "Modify commands (CHMOD, DELE, MKD, RMD, RNFR, UMASK): %sabled",
276562da0113SBen Gras 		    CURCLASS_FLAGS_ISSET(modify) ? "en" : "dis");
276662da0113SBen Gras 		reply(0, "Upload commands (APPE, STOR, STOU): %sabled",
276762da0113SBen Gras 		    CURCLASS_FLAGS_ISSET(upload) ? "en" : "dis");
276862da0113SBen Gras 		reply(0, "Sanitize file names: %sabled",
276962da0113SBen Gras 		    CURCLASS_FLAGS_ISSET(sanenames) ? "en" : "dis");
277062da0113SBen Gras 		reply(0, "PASV/LPSV/EPSV connections: %sabled",
277162da0113SBen Gras 		    CURCLASS_FLAGS_ISSET(passive) ? "en" : "dis");
277262da0113SBen Gras 		if (curclass.advertise.su_len != 0) {
277362da0113SBen Gras 			char buf[50];	/* big enough for IPv6 address */
277462da0113SBen Gras 			const char *bp;
277562da0113SBen Gras 
277662da0113SBen Gras 			bp = inet_ntop(curclass.advertise.su_family,
277762da0113SBen Gras 			    (void *)&curclass.advertise.su_addr,
277862da0113SBen Gras 			    buf, sizeof(buf));
277962da0113SBen Gras 			if (bp != NULL)
278062da0113SBen Gras 				reply(0, "PASV advertise address: %s", bp);
278162da0113SBen Gras 		}
278262da0113SBen Gras 		if (curclass.portmin && curclass.portmax)
278362da0113SBen Gras 			reply(0, "PASV port range: " LLF " - " LLF,
278462da0113SBen Gras 			    (LLT)curclass.portmin, (LLT)curclass.portmax);
278562da0113SBen Gras 		if (curclass.rateget)
278662da0113SBen Gras 			reply(0, "Rate get limit: " LLF " bytes/sec",
278762da0113SBen Gras 			    (LLT)curclass.rateget);
278862da0113SBen Gras 		else
278962da0113SBen Gras 			reply(0, "Rate get limit: disabled");
279062da0113SBen Gras 		if (curclass.rateput)
279162da0113SBen Gras 			reply(0, "Rate put limit: " LLF " bytes/sec",
279262da0113SBen Gras 			    (LLT)curclass.rateput);
279362da0113SBen Gras 		else
279462da0113SBen Gras 			reply(0, "Rate put limit: disabled");
279562da0113SBen Gras 		if (curclass.mmapsize)
279662da0113SBen Gras 			reply(0, "Mmap size: " LLF, (LLT)curclass.mmapsize);
279762da0113SBen Gras 		else
279862da0113SBen Gras 			reply(0, "Mmap size: disabled");
279962da0113SBen Gras 		if (curclass.readsize)
280062da0113SBen Gras 			reply(0, "Read size: " LLF, (LLT)curclass.readsize);
280162da0113SBen Gras 		else
280262da0113SBen Gras 			reply(0, "Read size: default");
280362da0113SBen Gras 		if (curclass.writesize)
280462da0113SBen Gras 			reply(0, "Write size: " LLF, (LLT)curclass.writesize);
280562da0113SBen Gras 		else
280662da0113SBen Gras 			reply(0, "Write size: default");
280762da0113SBen Gras 		if (curclass.recvbufsize)
280862da0113SBen Gras 			reply(0, "Receive buffer size: " LLF,
280962da0113SBen Gras 			    (LLT)curclass.recvbufsize);
281062da0113SBen Gras 		else
281162da0113SBen Gras 			reply(0, "Receive buffer size: default");
281262da0113SBen Gras 		if (curclass.sendbufsize)
281362da0113SBen Gras 			reply(0, "Send buffer size: " LLF,
281462da0113SBen Gras 			    (LLT)curclass.sendbufsize);
281562da0113SBen Gras 		else
281662da0113SBen Gras 			reply(0, "Send buffer size: default");
281762da0113SBen Gras 		if (curclass.sendlowat)
281862da0113SBen Gras 			reply(0, "Send low water mark: " LLF,
281962da0113SBen Gras 			    (LLT)curclass.sendlowat);
282062da0113SBen Gras 		else
282162da0113SBen Gras 			reply(0, "Send low water mark: default");
282262da0113SBen Gras 		reply(0, "Umask: %.04o", curclass.umask);
282362da0113SBen Gras 		for (cp = curclass.conversions; cp != NULL; cp=cp->next) {
282462da0113SBen Gras 			if (cp->suffix == NULL || cp->types == NULL ||
282562da0113SBen Gras 			    cp->command == NULL)
282662da0113SBen Gras 				continue;
282762da0113SBen Gras 			reply(0, "Conversion: %s [%s] disable: %s, command: %s",
282862da0113SBen Gras 			    cp->suffix, cp->types, cp->disable, cp->command);
282962da0113SBen Gras 		}
283062da0113SBen Gras 	}
283162da0113SBen Gras 
283262da0113SBen Gras 	reply(211, "End of status");
283362da0113SBen Gras }
283462da0113SBen Gras 
283562da0113SBen Gras void
fatal(const char * s)283662da0113SBen Gras fatal(const char *s)
283762da0113SBen Gras {
283862da0113SBen Gras 
283962da0113SBen Gras 	reply(451, "Error in server: %s\n", s);
284062da0113SBen Gras 	reply(221, "Closing connection due to server error.");
284162da0113SBen Gras 	dologout(0);
284262da0113SBen Gras 	/* NOTREACHED */
284362da0113SBen Gras }
284462da0113SBen Gras 
284562da0113SBen Gras /*
284662da0113SBen Gras  * reply() --
284762da0113SBen Gras  *	depending on the value of n, display fmt with a trailing CRLF and
284862da0113SBen Gras  *	prefix of:
284962da0113SBen Gras  *	n < -1		prefix the message with abs(n) + "-"	(initial line)
285062da0113SBen Gras  *	n == 0		prefix the message with 4 spaces	(middle lines)
285162da0113SBen Gras  *	n >  0		prefix the message with n + " "		(final line)
285262da0113SBen Gras  */
285362da0113SBen Gras void
reply(int n,const char * fmt,...)285462da0113SBen Gras reply(int n, const char *fmt, ...)
285562da0113SBen Gras {
285662da0113SBen Gras 	char	msg[MAXPATHLEN * 2 + 100];
285762da0113SBen Gras 	size_t	b;
285862da0113SBen Gras 	va_list	ap;
285962da0113SBen Gras 
286062da0113SBen Gras 	if (n == 0)
286162da0113SBen Gras 		b = snprintf(msg, sizeof(msg), "    ");
286262da0113SBen Gras 	else if (n < 0)
286362da0113SBen Gras 		b = snprintf(msg, sizeof(msg), "%d-", -n);
286462da0113SBen Gras 	else
286562da0113SBen Gras 		b = snprintf(msg, sizeof(msg), "%d ", n);
286662da0113SBen Gras 	va_start(ap, fmt);
286762da0113SBen Gras 	vsnprintf(msg + b, sizeof(msg) - b, fmt, ap);
286862da0113SBen Gras 	va_end(ap);
286962da0113SBen Gras 	cprintf(stdout, "%s\r\n", msg);
287062da0113SBen Gras 	(void)fflush(stdout);
287162da0113SBen Gras 	if (ftpd_debug)
287262da0113SBen Gras 		syslog(LOG_DEBUG, "<--- %s", msg);
287362da0113SBen Gras }
287462da0113SBen Gras 
287562da0113SBen Gras static void
logremotehost(struct sockinet * who)287662da0113SBen Gras logremotehost(struct sockinet *who)
287762da0113SBen Gras {
287862da0113SBen Gras 
287962da0113SBen Gras #if defined(HAVE_SOCKADDR_SNPRINTF)
288062da0113SBen Gras 	char abuf[BUFSIZ];
288162da0113SBen Gras #endif
288262da0113SBen Gras 
288362da0113SBen Gras 	struct sockaddr *sa = (struct sockaddr *)&who->si_su;
288462da0113SBen Gras 	if (getnameinfo(sa, who->su_len, remotehost, sizeof(remotehost), NULL,
288562da0113SBen Gras 	    0, getnameopts))
288662da0113SBen Gras 		strlcpy(remotehost, "?", sizeof(remotehost));
288762da0113SBen Gras #if defined(HAVE_SOCKADDR_SNPRINTF)
288862da0113SBen Gras 	sockaddr_snprintf(abuf, sizeof(abuf), "%a", sa);
288962da0113SBen Gras 	snprintf(remoteloghost, sizeof(remoteloghost), "%s(%s)", remotehost,
289062da0113SBen Gras 	    abuf);
289162da0113SBen Gras #else
289262da0113SBen Gras 	strlcpy(remoteloghost, remotehost, sizeof(remoteloghost));
289362da0113SBen Gras #endif
289462da0113SBen Gras 
289562da0113SBen Gras #if defined(HAVE_SETPROCTITLE)
289662da0113SBen Gras 	snprintf(proctitle, sizeof(proctitle), "%s: connected", remotehost);
289762da0113SBen Gras 	setproctitle("%s", proctitle);
289862da0113SBen Gras #endif /* defined(HAVE_SETPROCTITLE) */
289962da0113SBen Gras 	if (logging)
290062da0113SBen Gras 		syslog(LOG_INFO, "connection from %s to %s",
290162da0113SBen Gras 		    remoteloghost, hostname);
290262da0113SBen Gras }
290362da0113SBen Gras 
290462da0113SBen Gras /*
290562da0113SBen Gras  * Record logout in wtmp file and exit with supplied status.
290662da0113SBen Gras  * NOTE: because this is called from signal handlers it cannot
290762da0113SBen Gras  *       use stdio (or call other functions that use stdio).
290862da0113SBen Gras  */
290962da0113SBen Gras void
dologout(int status)291062da0113SBen Gras dologout(int status)
291162da0113SBen Gras {
291262da0113SBen Gras 	/*
291362da0113SBen Gras 	* Prevent reception of SIGURG from resulting in a resumption
291462da0113SBen Gras 	* back to the main program loop.
291562da0113SBen Gras 	*/
291662da0113SBen Gras 	transflag = 0;
291762da0113SBen Gras 	logout_utmp();
291862da0113SBen Gras 	if (logged_in) {
291962da0113SBen Gras #ifdef KERBEROS
292062da0113SBen Gras 		if (!notickets && krbtkfile_env)
292162da0113SBen Gras 			unlink(krbtkfile_env);
292262da0113SBen Gras #endif
292362da0113SBen Gras 	}
292462da0113SBen Gras 	/* beware of flushing buffers after a SIGPIPE */
292562da0113SBen Gras 	if (xferlogfd != -1)
292662da0113SBen Gras 		close(xferlogfd);
292762da0113SBen Gras 	_exit(status);
292862da0113SBen Gras }
292962da0113SBen Gras 
293062da0113SBen Gras void
abor(void)293162da0113SBen Gras abor(void)
293262da0113SBen Gras {
293362da0113SBen Gras 
293462da0113SBen Gras 	if (!transflag)
293562da0113SBen Gras 		return;
293662da0113SBen Gras 	tmpline[0] = '\0';
293762da0113SBen Gras 	is_oob = 0;
293862da0113SBen Gras 	reply(426, "Transfer aborted. Data connection closed.");
293962da0113SBen Gras 	reply(226, "Abort successful");
294062da0113SBen Gras 	transflag = 0;		/* flag that the transfer has aborted */
294162da0113SBen Gras }
294262da0113SBen Gras 
294362da0113SBen Gras void
statxfer(void)294462da0113SBen Gras statxfer(void)
294562da0113SBen Gras {
294662da0113SBen Gras 
294762da0113SBen Gras 	if (!transflag)
294862da0113SBen Gras 		return;
294962da0113SBen Gras 	tmpline[0] = '\0';
295062da0113SBen Gras 	is_oob = 0;
295162da0113SBen Gras 	if (file_size != (off_t) -1)
295262da0113SBen Gras 		reply(213,
295362da0113SBen Gras 		    "Status: " LLF " of " LLF " byte%s transferred",
295462da0113SBen Gras 		    (LLT)byte_count, (LLT)file_size,
295562da0113SBen Gras 		    PLURAL(byte_count));
295662da0113SBen Gras 	else
295762da0113SBen Gras 		reply(213, "Status: " LLF " byte%s transferred",
295862da0113SBen Gras 		    (LLT)byte_count, PLURAL(byte_count));
295962da0113SBen Gras }
296062da0113SBen Gras 
296162da0113SBen Gras /*
296262da0113SBen Gras  * Call when urgflag != 0 to handle Out Of Band commands.
296362da0113SBen Gras  * Returns non zero if the OOB command aborted the transfer
296462da0113SBen Gras  * by setting transflag to 0. (c.f., "ABOR").
296562da0113SBen Gras  */
296662da0113SBen Gras static int
handleoobcmd(void)296762da0113SBen Gras handleoobcmd(void)
296862da0113SBen Gras {
296962da0113SBen Gras 	char *cp;
297062da0113SBen Gras 	int ret;
297162da0113SBen Gras 
297262da0113SBen Gras 	if (!urgflag)
297362da0113SBen Gras 		return (0);
297462da0113SBen Gras 	urgflag = 0;
297562da0113SBen Gras 	/* only process if transfer occurring */
297662da0113SBen Gras 	if (!transflag)
297762da0113SBen Gras 		return (0);
297862da0113SBen Gras 	cp = tmpline;
297962da0113SBen Gras 	ret = get_line(cp, sizeof(tmpline)-1, stdin);
298062da0113SBen Gras 	if (ret == -1) {
298162da0113SBen Gras 		reply(221, "You could at least say goodbye.");
298262da0113SBen Gras 		dologout(0);
298362da0113SBen Gras 	} else if (ret == -2) {
298462da0113SBen Gras 		/* Ignore truncated command */
298562da0113SBen Gras 		/* XXX: abort xfer with "500 command too long", & return 1 ? */
298662da0113SBen Gras 		return 0;
298762da0113SBen Gras 	}
298862da0113SBen Gras 		/*
298962da0113SBen Gras 		 * Manually parse OOB commands, because we can't
299062da0113SBen Gras 		 * recursively call the yacc parser...
299162da0113SBen Gras 		 */
299262da0113SBen Gras 	if (strcasecmp(cp, "ABOR\r\n") == 0) {
299362da0113SBen Gras 		abor();
299462da0113SBen Gras 	} else if (strcasecmp(cp, "STAT\r\n") == 0) {
299562da0113SBen Gras 		statxfer();
299662da0113SBen Gras 	} else {
299762da0113SBen Gras 		/* XXX: error with "500 unknown command" ? */
299862da0113SBen Gras 	}
299962da0113SBen Gras 	return (transflag == 0);
300062da0113SBen Gras }
300162da0113SBen Gras 
300262da0113SBen Gras static int
bind_pasv_addr(void)300362da0113SBen Gras bind_pasv_addr(void)
300462da0113SBen Gras {
300562da0113SBen Gras 	static int passiveport;
300662da0113SBen Gras 	int port, len;
300762da0113SBen Gras 
300862da0113SBen Gras 	len = pasv_addr.su_len;
300962da0113SBen Gras 	if (curclass.portmin == 0 && curclass.portmax == 0) {
301062da0113SBen Gras 		pasv_addr.su_port = 0;
301162da0113SBen Gras 		return (bind(pdata, (struct sockaddr *)&pasv_addr.si_su, len));
301262da0113SBen Gras 	}
301362da0113SBen Gras 
301462da0113SBen Gras 	if (passiveport == 0) {
301562da0113SBen Gras 		srand(getpid());
301662da0113SBen Gras 		passiveport = rand() % (curclass.portmax - curclass.portmin)
301762da0113SBen Gras 		    + curclass.portmin;
301862da0113SBen Gras 	}
301962da0113SBen Gras 
302062da0113SBen Gras 	port = passiveport;
302162da0113SBen Gras 	while (1) {
302262da0113SBen Gras 		port++;
302362da0113SBen Gras 		if (port > curclass.portmax)
302462da0113SBen Gras 			port = curclass.portmin;
302562da0113SBen Gras 		else if (port == passiveport) {
302662da0113SBen Gras 			errno = EAGAIN;
302762da0113SBen Gras 			return (-1);
302862da0113SBen Gras 		}
302962da0113SBen Gras 		pasv_addr.su_port = htons(port);
303062da0113SBen Gras 		if (bind(pdata, (struct sockaddr *)&pasv_addr.si_su, len) == 0)
303162da0113SBen Gras 			break;
303262da0113SBen Gras 		if (errno != EADDRINUSE)
303362da0113SBen Gras 			return (-1);
303462da0113SBen Gras 	}
303562da0113SBen Gras 	passiveport = port;
303662da0113SBen Gras 	return (0);
303762da0113SBen Gras }
303862da0113SBen Gras 
303962da0113SBen Gras /*
304062da0113SBen Gras  * Note: a response of 425 is not mentioned as a possible response to
304162da0113SBen Gras  *	the PASV command in RFC959. However, it has been blessed as
304262da0113SBen Gras  *	a legitimate response by Jon Postel in a telephone conversation
304362da0113SBen Gras  *	with Rick Adams on 25 Jan 89.
304462da0113SBen Gras  */
304562da0113SBen Gras void
passive(void)304662da0113SBen Gras passive(void)
304762da0113SBen Gras {
304862da0113SBen Gras 	socklen_t len;
304962da0113SBen Gras 	int recvbufsize;
305062da0113SBen Gras 	char *p, *a;
305162da0113SBen Gras 
305262da0113SBen Gras 	if (pdata >= 0)
305362da0113SBen Gras 		close(pdata);
305462da0113SBen Gras 	pdata = socket(AF_INET, SOCK_STREAM, 0);
305562da0113SBen Gras 	if (pdata < 0 || !logged_in) {
305662da0113SBen Gras 		perror_reply(425, "Can't open passive connection");
305762da0113SBen Gras 		return;
305862da0113SBen Gras 	}
305962da0113SBen Gras 	pasv_addr = ctrl_addr;
306062da0113SBen Gras 
306162da0113SBen Gras 	if (bind_pasv_addr() < 0)
306262da0113SBen Gras 		goto pasv_error;
306362da0113SBen Gras 	len = pasv_addr.su_len;
306462da0113SBen Gras 	if (getsockname(pdata, (struct sockaddr *) &pasv_addr.si_su, &len) < 0)
306562da0113SBen Gras 		goto pasv_error;
306662da0113SBen Gras 	pasv_addr.su_len = len;
306762da0113SBen Gras 	if (curclass.recvbufsize) {
306862da0113SBen Gras 		recvbufsize = curclass.recvbufsize;
306962da0113SBen Gras 		if (setsockopt(pdata, SOL_SOCKET, SO_RCVBUF, &recvbufsize,
307062da0113SBen Gras 			       sizeof(int)) == -1)
307162da0113SBen Gras 			syslog(LOG_WARNING, "setsockopt(SO_RCVBUF, %d): %m",
307262da0113SBen Gras 			       recvbufsize);
307362da0113SBen Gras 	}
307462da0113SBen Gras 	if (listen(pdata, 1) < 0)
307562da0113SBen Gras 		goto pasv_error;
307662da0113SBen Gras 	if (curclass.advertise.su_len != 0)
307762da0113SBen Gras 		a = (char *) &curclass.advertise.su_addr;
307862da0113SBen Gras 	else
307962da0113SBen Gras 		a = (char *) &pasv_addr.su_addr;
308062da0113SBen Gras 	p = (char *) &pasv_addr.su_port;
308162da0113SBen Gras 
308262da0113SBen Gras #define UC(b) (((int) b) & 0xff)
308362da0113SBen Gras 
308462da0113SBen Gras 	reply(227, "Entering Passive Mode (%d,%d,%d,%d,%d,%d)", UC(a[0]),
308562da0113SBen Gras 		UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1]));
308662da0113SBen Gras 	return;
308762da0113SBen Gras 
308862da0113SBen Gras  pasv_error:
308962da0113SBen Gras 	(void) close(pdata);
309062da0113SBen Gras 	pdata = -1;
309162da0113SBen Gras 	perror_reply(425, "Can't open passive connection");
309262da0113SBen Gras 	return;
309362da0113SBen Gras }
309462da0113SBen Gras 
309562da0113SBen Gras /*
309662da0113SBen Gras  * convert protocol identifier to/from AF
309762da0113SBen Gras  */
309862da0113SBen Gras int
lpsvproto2af(int proto)309962da0113SBen Gras lpsvproto2af(int proto)
310062da0113SBen Gras {
310162da0113SBen Gras 
310262da0113SBen Gras 	switch (proto) {
310362da0113SBen Gras 	case 4:
310462da0113SBen Gras 		return AF_INET;
310562da0113SBen Gras #ifdef INET6
310662da0113SBen Gras 	case 6:
310762da0113SBen Gras 		return AF_INET6;
310862da0113SBen Gras #endif
310962da0113SBen Gras 	default:
311062da0113SBen Gras 		return -1;
311162da0113SBen Gras 	}
311262da0113SBen Gras }
311362da0113SBen Gras 
311462da0113SBen Gras int
af2lpsvproto(int af)311562da0113SBen Gras af2lpsvproto(int af)
311662da0113SBen Gras {
311762da0113SBen Gras 
311862da0113SBen Gras 	switch (af) {
311962da0113SBen Gras 	case AF_INET:
312062da0113SBen Gras 		return 4;
312162da0113SBen Gras #ifdef INET6
312262da0113SBen Gras 	case AF_INET6:
312362da0113SBen Gras 		return 6;
312462da0113SBen Gras #endif
312562da0113SBen Gras 	default:
312662da0113SBen Gras 		return -1;
312762da0113SBen Gras 	}
312862da0113SBen Gras }
312962da0113SBen Gras 
313062da0113SBen Gras int
epsvproto2af(int proto)313162da0113SBen Gras epsvproto2af(int proto)
313262da0113SBen Gras {
313362da0113SBen Gras 
313462da0113SBen Gras 	switch (proto) {
313562da0113SBen Gras 	case 1:
313662da0113SBen Gras 		return AF_INET;
313762da0113SBen Gras #ifdef INET6
313862da0113SBen Gras 	case 2:
313962da0113SBen Gras 		return AF_INET6;
314062da0113SBen Gras #endif
314162da0113SBen Gras 	default:
314262da0113SBen Gras 		return -1;
314362da0113SBen Gras 	}
314462da0113SBen Gras }
314562da0113SBen Gras 
314662da0113SBen Gras int
af2epsvproto(int af)314762da0113SBen Gras af2epsvproto(int af)
314862da0113SBen Gras {
314962da0113SBen Gras 
315062da0113SBen Gras 	switch (af) {
315162da0113SBen Gras 	case AF_INET:
315262da0113SBen Gras 		return 1;
315362da0113SBen Gras #ifdef INET6
315462da0113SBen Gras 	case AF_INET6:
315562da0113SBen Gras 		return 2;
315662da0113SBen Gras #endif
315762da0113SBen Gras 	default:
315862da0113SBen Gras 		return -1;
315962da0113SBen Gras 	}
316062da0113SBen Gras }
316162da0113SBen Gras 
316262da0113SBen Gras /*
316362da0113SBen Gras  * 228 Entering Long Passive Mode (af, hal, h1, h2, h3,..., pal, p1, p2...)
316462da0113SBen Gras  * 229 Entering Extended Passive Mode (|||port|)
316562da0113SBen Gras  */
316662da0113SBen Gras void
long_passive(const char * cmd,int pf)316762da0113SBen Gras long_passive(const char *cmd, int pf)
316862da0113SBen Gras {
316962da0113SBen Gras 	socklen_t len;
317062da0113SBen Gras 	char *p, *a;
317162da0113SBen Gras 
317262da0113SBen Gras 	if (!logged_in) {
317362da0113SBen Gras 		syslog(LOG_NOTICE, "long passive but not logged in");
317462da0113SBen Gras 		reply(503, "Login with USER first.");
317562da0113SBen Gras 		return;
317662da0113SBen Gras 	}
317762da0113SBen Gras 
317862da0113SBen Gras 	if (pf != PF_UNSPEC && ctrl_addr.su_family != pf) {
317962da0113SBen Gras 		/*
318062da0113SBen Gras 		 * XXX: only EPRT/EPSV ready clients will understand this
318162da0113SBen Gras 		 */
318262da0113SBen Gras 		if (strcmp(cmd, "EPSV") != 0)
318362da0113SBen Gras 			reply(501, "Network protocol mismatch"); /*XXX*/
318462da0113SBen Gras 		else
318562da0113SBen Gras 			epsv_protounsupp("Network protocol mismatch");
318662da0113SBen Gras 
318762da0113SBen Gras 		return;
318862da0113SBen Gras 	}
318962da0113SBen Gras 
319062da0113SBen Gras 	if (pdata >= 0)
319162da0113SBen Gras 		close(pdata);
319262da0113SBen Gras 	pdata = socket(ctrl_addr.su_family, SOCK_STREAM, 0);
319362da0113SBen Gras 	if (pdata < 0) {
319462da0113SBen Gras 		perror_reply(425, "Can't open passive connection");
319562da0113SBen Gras 		return;
319662da0113SBen Gras 	}
319762da0113SBen Gras 	pasv_addr = ctrl_addr;
319862da0113SBen Gras 	if (bind_pasv_addr() < 0)
319962da0113SBen Gras 		goto pasv_error;
320062da0113SBen Gras 	len = pasv_addr.su_len;
320162da0113SBen Gras 	if (getsockname(pdata, (struct sockaddr *) &pasv_addr.si_su, &len) < 0)
320262da0113SBen Gras 		goto pasv_error;
320362da0113SBen Gras 	pasv_addr.su_len = len;
320462da0113SBen Gras 	if (listen(pdata, 1) < 0)
320562da0113SBen Gras 		goto pasv_error;
320662da0113SBen Gras 	p = (char *) &pasv_addr.su_port;
320762da0113SBen Gras 
320862da0113SBen Gras #define UC(b) (((int) b) & 0xff)
320962da0113SBen Gras 
321062da0113SBen Gras 	if (strcmp(cmd, "LPSV") == 0) {
321162da0113SBen Gras 		struct sockinet *advert;
321262da0113SBen Gras 
321362da0113SBen Gras 		if (curclass.advertise.su_len != 0)
321462da0113SBen Gras 			advert = &curclass.advertise;
321562da0113SBen Gras 		else
321662da0113SBen Gras 			advert = &pasv_addr;
321762da0113SBen Gras 		switch (advert->su_family) {
321862da0113SBen Gras 		case AF_INET:
321962da0113SBen Gras 			a = (char *) &advert->su_addr;
322062da0113SBen Gras 			reply(228,
322162da0113SBen Gras     "Entering Long Passive Mode (%d,%d,%d,%d,%d,%d,%d,%d,%d)",
322262da0113SBen Gras 				4, 4, UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
322362da0113SBen Gras 				2, UC(p[0]), UC(p[1]));
322462da0113SBen Gras 			return;
322562da0113SBen Gras #ifdef INET6
322662da0113SBen Gras 		case AF_INET6:
322762da0113SBen Gras 			a = (char *) &advert->su_6addr;
322862da0113SBen Gras 			reply(228,
322962da0113SBen Gras     "Entering Long Passive Mode (%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d)",
323062da0113SBen Gras 				6, 16,
323162da0113SBen Gras 				UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
323262da0113SBen Gras 				UC(a[4]), UC(a[5]), UC(a[6]), UC(a[7]),
323362da0113SBen Gras 				UC(a[8]), UC(a[9]), UC(a[10]), UC(a[11]),
323462da0113SBen Gras 				UC(a[12]), UC(a[13]), UC(a[14]), UC(a[15]),
323562da0113SBen Gras 				2, UC(p[0]), UC(p[1]));
323662da0113SBen Gras 			return;
323762da0113SBen Gras #endif
323862da0113SBen Gras 		}
323962da0113SBen Gras #undef UC
324062da0113SBen Gras 	} else if (strcmp(cmd, "EPSV") == 0) {
324162da0113SBen Gras 		switch (pasv_addr.su_family) {
324262da0113SBen Gras 		case AF_INET:
324362da0113SBen Gras #ifdef INET6
324462da0113SBen Gras 		case AF_INET6:
324562da0113SBen Gras #endif
324662da0113SBen Gras 			reply(229, "Entering Extended Passive Mode (|||%d|)",
324762da0113SBen Gras 			    ntohs(pasv_addr.su_port));
324862da0113SBen Gras 			return;
324962da0113SBen Gras 		}
325062da0113SBen Gras 	} else {
325162da0113SBen Gras 		/* more proper error code? */
325262da0113SBen Gras 	}
325362da0113SBen Gras 
325462da0113SBen Gras  pasv_error:
325562da0113SBen Gras 	(void) close(pdata);
325662da0113SBen Gras 	pdata = -1;
325762da0113SBen Gras 	perror_reply(425, "Can't open passive connection");
325862da0113SBen Gras 	return;
325962da0113SBen Gras }
326062da0113SBen Gras 
326162da0113SBen Gras int
extended_port(const char * arg)326262da0113SBen Gras extended_port(const char *arg)
326362da0113SBen Gras {
326462da0113SBen Gras 	char *tmp = NULL;
326562da0113SBen Gras 	char *result[3];
326662da0113SBen Gras 	char *p, *q;
326762da0113SBen Gras 	char delim;
326862da0113SBen Gras 	struct addrinfo hints;
326962da0113SBen Gras 	struct addrinfo *res = NULL;
327062da0113SBen Gras 	int i;
327162da0113SBen Gras 	unsigned long proto;
327262da0113SBen Gras 
327362da0113SBen Gras 	tmp = ftpd_strdup(arg);
327462da0113SBen Gras 	p = tmp;
327562da0113SBen Gras 	delim = p[0];
327662da0113SBen Gras 	p++;
327762da0113SBen Gras 	memset(result, 0, sizeof(result));
327862da0113SBen Gras 	for (i = 0; i < 3; i++) {
327962da0113SBen Gras 		q = strchr(p, delim);
328062da0113SBen Gras 		if (!q || *q != delim)
328162da0113SBen Gras 			goto parsefail;
328262da0113SBen Gras 		*q++ = '\0';
328362da0113SBen Gras 		result[i] = p;
328462da0113SBen Gras 		p = q;
328562da0113SBen Gras 	}
328662da0113SBen Gras 
328762da0113SBen Gras 			/* some more sanity checks */
328862da0113SBen Gras 	errno = 0;
328962da0113SBen Gras 	p = NULL;
329062da0113SBen Gras 	(void)strtoul(result[2], &p, 10);
329162da0113SBen Gras 	if (errno || !*result[2] || *p)
329262da0113SBen Gras 		goto parsefail;
329362da0113SBen Gras 	errno = 0;
329462da0113SBen Gras 	p = NULL;
329562da0113SBen Gras 	proto = strtoul(result[0], &p, 10);
329662da0113SBen Gras 	if (errno || !*result[0] || *p)
329762da0113SBen Gras 		goto protounsupp;
329862da0113SBen Gras 
329962da0113SBen Gras 	memset(&hints, 0, sizeof(hints));
330062da0113SBen Gras 	hints.ai_family = epsvproto2af((int)proto);
330162da0113SBen Gras 	if (hints.ai_family < 0)
330262da0113SBen Gras 		goto protounsupp;
330362da0113SBen Gras 	hints.ai_socktype = SOCK_STREAM;
330462da0113SBen Gras 	hints.ai_flags = AI_NUMERICHOST;
330562da0113SBen Gras 	if (getaddrinfo(result[1], result[2], &hints, &res))
330662da0113SBen Gras 		goto parsefail;
330762da0113SBen Gras 	if (res->ai_next)
330862da0113SBen Gras 		goto parsefail;
330962da0113SBen Gras 	if (sizeof(data_dest) < res->ai_addrlen)
331062da0113SBen Gras 		goto parsefail;
331162da0113SBen Gras 	memcpy(&data_dest.si_su, res->ai_addr, res->ai_addrlen);
331262da0113SBen Gras 	data_dest.su_len = res->ai_addrlen;
331362da0113SBen Gras #ifdef INET6
331462da0113SBen Gras 	if (his_addr.su_family == AF_INET6 &&
331562da0113SBen Gras 	    data_dest.su_family == AF_INET6) {
331662da0113SBen Gras 			/* XXX: more sanity checks! */
331762da0113SBen Gras 		data_dest.su_scope_id = his_addr.su_scope_id;
331862da0113SBen Gras 	}
331962da0113SBen Gras #endif
332062da0113SBen Gras 
332162da0113SBen Gras 	if (tmp != NULL)
332262da0113SBen Gras 		free(tmp);
332362da0113SBen Gras 	if (res)
332462da0113SBen Gras 		freeaddrinfo(res);
332562da0113SBen Gras 	return 0;
332662da0113SBen Gras 
332762da0113SBen Gras  parsefail:
332862da0113SBen Gras 	reply(500, "Invalid argument, rejected.");
332962da0113SBen Gras 	usedefault = 1;
333062da0113SBen Gras 	if (tmp != NULL)
333162da0113SBen Gras 		free(tmp);
333262da0113SBen Gras 	if (res)
333362da0113SBen Gras 		freeaddrinfo(res);
333462da0113SBen Gras 	return -1;
333562da0113SBen Gras 
333662da0113SBen Gras  protounsupp:
333762da0113SBen Gras 	epsv_protounsupp("Protocol not supported");
333862da0113SBen Gras 	usedefault = 1;
333962da0113SBen Gras 	if (tmp != NULL)
334062da0113SBen Gras 		free(tmp);
334162da0113SBen Gras 	return -1;
334262da0113SBen Gras }
334362da0113SBen Gras 
334462da0113SBen Gras /*
334562da0113SBen Gras  * 522 Protocol not supported (proto,...)
334662da0113SBen Gras  * as we assume address family for control and data connections are the same,
334762da0113SBen Gras  * we do not return the list of address families we support - instead, we
334862da0113SBen Gras  * return the address family of the control connection.
334962da0113SBen Gras  */
335062da0113SBen Gras void
epsv_protounsupp(const char * message)335162da0113SBen Gras epsv_protounsupp(const char *message)
335262da0113SBen Gras {
335362da0113SBen Gras 	int proto;
335462da0113SBen Gras 
335562da0113SBen Gras 	proto = af2epsvproto(ctrl_addr.su_family);
335662da0113SBen Gras 	if (proto < 0)
335762da0113SBen Gras 		reply(501, "%s", message);	/* XXX */
335862da0113SBen Gras 	else
335962da0113SBen Gras 		reply(522, "%s, use (%d)", message, proto);
336062da0113SBen Gras }
336162da0113SBen Gras 
336262da0113SBen Gras /*
336362da0113SBen Gras  * Generate unique name for file with basename "local".
336462da0113SBen Gras  * The file named "local" is already known to exist.
336562da0113SBen Gras  * Generates failure reply on error.
336662da0113SBen Gras  *
336762da0113SBen Gras  * XXX:	this function should under go changes similar to
336862da0113SBen Gras  *	the mktemp(3)/mkstemp(3) changes.
336962da0113SBen Gras  */
337062da0113SBen Gras static char *
gunique(const char * local)337162da0113SBen Gras gunique(const char *local)
337262da0113SBen Gras {
337362da0113SBen Gras 	static char new[MAXPATHLEN];
337462da0113SBen Gras 	struct stat st;
337562da0113SBen Gras 	char *cp;
337662da0113SBen Gras 	int count;
337762da0113SBen Gras 
337862da0113SBen Gras 	cp = strrchr(local, '/');
337962da0113SBen Gras 	if (cp)
338062da0113SBen Gras 		*cp = '\0';
338162da0113SBen Gras 	if (stat(cp ? local : ".", &st) < 0) {
338262da0113SBen Gras 		perror_reply(553, cp ? local : ".");
338362da0113SBen Gras 		return (NULL);
338462da0113SBen Gras 	}
338562da0113SBen Gras 	if (cp)
338662da0113SBen Gras 		*cp = '/';
338762da0113SBen Gras 	for (count = 1; count < 100; count++) {
338862da0113SBen Gras 		(void)snprintf(new, sizeof(new) - 1, "%s.%d", local, count);
338962da0113SBen Gras 		if (stat(new, &st) < 0)
339062da0113SBen Gras 			return (new);
339162da0113SBen Gras 	}
339262da0113SBen Gras 	reply(452, "Unique file name cannot be created.");
339362da0113SBen Gras 	return (NULL);
339462da0113SBen Gras }
339562da0113SBen Gras 
339662da0113SBen Gras /*
339762da0113SBen Gras  * Format and send reply containing system error number.
339862da0113SBen Gras  */
339962da0113SBen Gras void
perror_reply(int code,const char * string)340062da0113SBen Gras perror_reply(int code, const char *string)
340162da0113SBen Gras {
340262da0113SBen Gras 	int save_errno;
340362da0113SBen Gras 
340462da0113SBen Gras 	save_errno = errno;
340562da0113SBen Gras 	reply(code, "%s: %s.", string, strerror(errno));
340662da0113SBen Gras 	errno = save_errno;
340762da0113SBen Gras }
340862da0113SBen Gras 
340962da0113SBen Gras static char *onefile[] = {
341062da0113SBen Gras 	NULL,
341162da0113SBen Gras 	0
341262da0113SBen Gras };
341362da0113SBen Gras 
341462da0113SBen Gras void
send_file_list(const char * whichf)341562da0113SBen Gras send_file_list(const char *whichf)
341662da0113SBen Gras {
341762da0113SBen Gras 	struct stat st;
341862da0113SBen Gras 	DIR *dirp;
341962da0113SBen Gras 	struct dirent *dir;
342062da0113SBen Gras 	FILE *volatile dout;
342162da0113SBen Gras 	char **volatile dirlist;
342262da0113SBen Gras 	char *dirname, *p;
342362da0113SBen Gras 	char *notglob;
342462da0113SBen Gras 	int volatile simple;
342562da0113SBen Gras 	int volatile freeglob;
342662da0113SBen Gras 	glob_t gl;
342762da0113SBen Gras 
342862da0113SBen Gras 	dirp = NULL;
342962da0113SBen Gras 	dout = NULL;
343062da0113SBen Gras 	notglob = NULL;
343162da0113SBen Gras 	simple = 0;
343262da0113SBen Gras 	freeglob = 0;
343362da0113SBen Gras 	urgflag = 0;
343462da0113SBen Gras 
343562da0113SBen Gras 	p = NULL;
343662da0113SBen Gras 	if (strpbrk(whichf, "~{[*?") != NULL) {
343762da0113SBen Gras 		int flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_TILDE|GLOB_LIMIT;
343862da0113SBen Gras 
343962da0113SBen Gras 		memset(&gl, 0, sizeof(gl));
344062da0113SBen Gras 		freeglob = 1;
344162da0113SBen Gras 		if (glob(whichf, flags, 0, &gl)) {
344262da0113SBen Gras 			reply(450, "Not found");
344362da0113SBen Gras 			goto cleanup_send_file_list;
344462da0113SBen Gras 		} else if (gl.gl_pathc == 0) {
344562da0113SBen Gras 			errno = ENOENT;
344662da0113SBen Gras 			perror_reply(450, whichf);
344762da0113SBen Gras 			goto cleanup_send_file_list;
344862da0113SBen Gras 		}
344962da0113SBen Gras 		dirlist = gl.gl_pathv;
345062da0113SBen Gras 	} else {
345162da0113SBen Gras 		notglob = ftpd_strdup(whichf);
345262da0113SBen Gras 		onefile[0] = notglob;
345362da0113SBen Gras 		dirlist = onefile;
345462da0113SBen Gras 		simple = 1;
345562da0113SBen Gras 	}
345662da0113SBen Gras 					/* XXX: } for vi sm */
345762da0113SBen Gras 
345862da0113SBen Gras 	while ((dirname = *dirlist++) != NULL) {
345962da0113SBen Gras 		int trailingslash = 0;
346062da0113SBen Gras 
346162da0113SBen Gras 		if (stat(dirname, &st) < 0) {
346262da0113SBen Gras 			/*
346362da0113SBen Gras 			 * If user typed "ls -l", etc, and the client
346462da0113SBen Gras 			 * used NLST, do what the user meant.
346562da0113SBen Gras 			 */
346662da0113SBen Gras 			/* XXX: nuke this support? */
346762da0113SBen Gras 			if (dirname[0] == '-' && *dirlist == NULL &&
346862da0113SBen Gras 			    transflag == 0) {
346962da0113SBen Gras 				const char *argv[] = { INTERNAL_LS, "", NULL };
347062da0113SBen Gras 
347162da0113SBen Gras 				argv[1] = dirname;
347262da0113SBen Gras 				retrieve(argv, dirname);
347362da0113SBen Gras 				goto cleanup_send_file_list;
347462da0113SBen Gras 			}
347562da0113SBen Gras 			perror_reply(450, whichf);
347662da0113SBen Gras 			goto cleanup_send_file_list;
347762da0113SBen Gras 		}
347862da0113SBen Gras 
347962da0113SBen Gras 		if (S_ISREG(st.st_mode)) {
348062da0113SBen Gras 			/*
348162da0113SBen Gras 			 * XXXRFC:
348262da0113SBen Gras 			 *	should we follow RFC959 and not work
348362da0113SBen Gras 			 *	for non directories?
348462da0113SBen Gras 			 */
348562da0113SBen Gras 			if (dout == NULL) {
348662da0113SBen Gras 				dout = dataconn("file list", (off_t)-1, "w");
348762da0113SBen Gras 				if (dout == NULL)
348862da0113SBen Gras 					goto cleanup_send_file_list;
348962da0113SBen Gras 				transflag = 1;
349062da0113SBen Gras 			}
349162da0113SBen Gras 			cprintf(dout, "%s%s\n", dirname,
349262da0113SBen Gras 			    type == TYPE_A ? "\r" : "");
349362da0113SBen Gras 			continue;
349462da0113SBen Gras 		} else if (!S_ISDIR(st.st_mode))
349562da0113SBen Gras 			continue;
349662da0113SBen Gras 
349762da0113SBen Gras 		if (dirname[strlen(dirname) - 1] == '/')
349862da0113SBen Gras 			trailingslash++;
349962da0113SBen Gras 
350062da0113SBen Gras 		if ((dirp = opendir(dirname)) == NULL)
350162da0113SBen Gras 			continue;
350262da0113SBen Gras 
350362da0113SBen Gras 		while ((dir = readdir(dirp)) != NULL) {
350462da0113SBen Gras 			char nbuf[MAXPATHLEN];
350562da0113SBen Gras 
35060a6a1f1dSLionel Sambuc 			if (urgflag && handleoobcmd()) {
35070a6a1f1dSLionel Sambuc 				(void) closedir(dirp);
350862da0113SBen Gras 				goto cleanup_send_file_list;
35090a6a1f1dSLionel Sambuc 			}
351062da0113SBen Gras 
351162da0113SBen Gras 			if (ISDOTDIR(dir->d_name) || ISDOTDOTDIR(dir->d_name))
351262da0113SBen Gras 				continue;
351362da0113SBen Gras 
351462da0113SBen Gras 			(void)snprintf(nbuf, sizeof(nbuf), "%s%s%s", dirname,
351562da0113SBen Gras 			    trailingslash ? "" : "/", dir->d_name);
351662da0113SBen Gras 
351762da0113SBen Gras 			/*
351862da0113SBen Gras 			 * We have to do a stat to ensure it's
351962da0113SBen Gras 			 * not a directory or special file.
352062da0113SBen Gras 			 */
352162da0113SBen Gras 			/*
352262da0113SBen Gras 			 * XXXRFC:
352362da0113SBen Gras 			 *	should we follow RFC959 and filter out
352462da0113SBen Gras 			 *	non files ?   lukem - NO!, or not until
352562da0113SBen Gras 			 *	our ftp client uses MLS{T,D} for completion.
352662da0113SBen Gras 			 */
352762da0113SBen Gras 			if (simple || (stat(nbuf, &st) == 0 &&
352862da0113SBen Gras 			    S_ISREG(st.st_mode))) {
352962da0113SBen Gras 				if (dout == NULL) {
353062da0113SBen Gras 					dout = dataconn("file list", (off_t)-1,
353162da0113SBen Gras 						"w");
35320a6a1f1dSLionel Sambuc 					if (dout == NULL) {
35330a6a1f1dSLionel Sambuc 						(void) closedir(dirp);
353462da0113SBen Gras 						goto cleanup_send_file_list;
35350a6a1f1dSLionel Sambuc 					}
353662da0113SBen Gras 					transflag = 1;
353762da0113SBen Gras 				}
353862da0113SBen Gras 				p = nbuf;
353962da0113SBen Gras 				if (nbuf[0] == '.' && nbuf[1] == '/')
354062da0113SBen Gras 					p = &nbuf[2];
354162da0113SBen Gras 				cprintf(dout, "%s%s\n", p,
354262da0113SBen Gras 				    type == TYPE_A ? "\r" : "");
354362da0113SBen Gras 			}
354462da0113SBen Gras 		}
354562da0113SBen Gras 		(void) closedir(dirp);
354662da0113SBen Gras 	}
354762da0113SBen Gras 
354862da0113SBen Gras 	if (dout == NULL)
354962da0113SBen Gras 		reply(450, "No files found.");
355062da0113SBen Gras 	else if (ferror(dout) != 0)
355162da0113SBen Gras 		perror_reply(451, "Data connection");
355262da0113SBen Gras 	else
355362da0113SBen Gras 		reply(226, "Transfer complete.");
355462da0113SBen Gras 
355562da0113SBen Gras  cleanup_send_file_list:
355662da0113SBen Gras 	closedataconn(dout);
355762da0113SBen Gras 	transflag = 0;
355862da0113SBen Gras 	urgflag = 0;
355962da0113SBen Gras 	total_xfers++;
356062da0113SBen Gras 	total_xfers_out++;
356162da0113SBen Gras 	if (notglob)
356262da0113SBen Gras 		free(notglob);
356362da0113SBen Gras 	if (freeglob)
356462da0113SBen Gras 		globfree(&gl);
356562da0113SBen Gras }
356662da0113SBen Gras 
356762da0113SBen Gras char *
conffilename(const char * s)356862da0113SBen Gras conffilename(const char *s)
356962da0113SBen Gras {
357062da0113SBen Gras 	static char filename[MAXPATHLEN];
357162da0113SBen Gras 
357262da0113SBen Gras 	if (*s == '/')
357362da0113SBen Gras 		strlcpy(filename, s, sizeof(filename));
357462da0113SBen Gras 	else
357562da0113SBen Gras 		(void)snprintf(filename, sizeof(filename), "%s/%s", confdir ,s);
357662da0113SBen Gras 	return (filename);
357762da0113SBen Gras }
357862da0113SBen Gras 
357962da0113SBen Gras /*
358062da0113SBen Gras  * logxfer --
358162da0113SBen Gras  *	if logging > 1, then based on the arguments, syslog a message:
358262da0113SBen Gras  *	 if bytes != -1		"<command> <file1> = <bytes> bytes"
358362da0113SBen Gras  *	 else if file2 != NULL	"<command> <file1> <file2>"
358462da0113SBen Gras  *	 else			"<command> <file1>"
358562da0113SBen Gras  *	if elapsed != NULL, append "in xxx.yyy seconds"
358662da0113SBen Gras  *	if error != NULL, append ": " + error
358762da0113SBen Gras  *
358862da0113SBen Gras  *	if doxferlog != 0, bytes != -1, and command is "get", "put",
358962da0113SBen Gras  *	or "append", syslog and/or write a wu-ftpd style xferlog entry
359062da0113SBen Gras  */
359162da0113SBen Gras void
logxfer(const char * command,off_t bytes,const char * file1,const char * file2,const struct timeval * elapsed,const char * error)359262da0113SBen Gras logxfer(const char *command, off_t bytes, const char *file1, const char *file2,
359362da0113SBen Gras     const struct timeval *elapsed, const char *error)
359462da0113SBen Gras {
359562da0113SBen Gras 	char		 buf[MAXPATHLEN * 2 + 100];
359662da0113SBen Gras 	char		 realfile1[MAXPATHLEN], realfile2[MAXPATHLEN];
359762da0113SBen Gras 	const char	*r1, *r2;
359862da0113SBen Gras 	char		 direction;
359962da0113SBen Gras 	size_t		 len;
360062da0113SBen Gras 	time_t		 now;
360162da0113SBen Gras 
360262da0113SBen Gras 	if (logging <=1 && !doxferlog)
360362da0113SBen Gras 		return;
360462da0113SBen Gras 
360562da0113SBen Gras 	r1 = r2 = NULL;
360662da0113SBen Gras 	if ((r1 = realpath(file1, realfile1)) == NULL)
360762da0113SBen Gras 		r1 = file1;
360862da0113SBen Gras 	if (file2 != NULL)
360962da0113SBen Gras 		if ((r2 = realpath(file2, realfile2)) == NULL)
361062da0113SBen Gras 			r2 = file2;
361162da0113SBen Gras 
361262da0113SBen Gras 		/*
361362da0113SBen Gras 		 * syslog command
361462da0113SBen Gras 		 */
361562da0113SBen Gras 	if (logging > 1) {
361662da0113SBen Gras 		len = snprintf(buf, sizeof(buf), "%s %s", command, r1);
361762da0113SBen Gras 		if (bytes != (off_t)-1)
361862da0113SBen Gras 			len += snprintf(buf + len, sizeof(buf) - len,
361962da0113SBen Gras 			    " = " LLF " byte%s", (LLT) bytes, PLURAL(bytes));
362062da0113SBen Gras 		else if (r2 != NULL)
362162da0113SBen Gras 			len += snprintf(buf + len, sizeof(buf) - len,
362262da0113SBen Gras 			    " %s", r2);
362362da0113SBen Gras 		if (elapsed != NULL)
362462da0113SBen Gras 			len += snprintf(buf + len, sizeof(buf) - len,
362562da0113SBen Gras 			    " in " LLF ".%.03ld seconds",
362662da0113SBen Gras 			    (LLT)elapsed->tv_sec,
362762da0113SBen Gras 			    (long)(elapsed->tv_usec / 1000));
362862da0113SBen Gras 		if (error != NULL)
362962da0113SBen Gras 			len += snprintf(buf + len, sizeof(buf) - len,
363062da0113SBen Gras 			    ": %s", error);
363162da0113SBen Gras 		syslog(LOG_INFO, "%s", buf);
363262da0113SBen Gras 	}
363362da0113SBen Gras 
363462da0113SBen Gras 		/*
363562da0113SBen Gras 		 * syslog wu-ftpd style log entry, prefixed with "xferlog: "
363662da0113SBen Gras 		 */
363762da0113SBen Gras 	if (!doxferlog || bytes == -1)
363862da0113SBen Gras 		return;
363962da0113SBen Gras 
364062da0113SBen Gras 	if (strcmp(command, "get") == 0)
364162da0113SBen Gras 		direction = 'o';
364262da0113SBen Gras 	else if (strcmp(command, "put") == 0 || strcmp(command, "append") == 0)
364362da0113SBen Gras 		direction = 'i';
364462da0113SBen Gras 	else
364562da0113SBen Gras 		return;
364662da0113SBen Gras 
364762da0113SBen Gras 	time(&now);
364862da0113SBen Gras 	len = snprintf(buf, sizeof(buf),
364962da0113SBen Gras 	    "%.24s " LLF " %s " LLF " %s %c %s %c %c %s FTP 0 * %c\n",
365062da0113SBen Gras 
365162da0113SBen Gras /*
365262da0113SBen Gras  * XXX: wu-ftpd puts ' (send)' or ' (recv)' in the syslog message, and removes
365362da0113SBen Gras  *	the full date.  This may be problematic for accurate log parsing,
365462da0113SBen Gras  *	given that syslog messages don't contain the full date.
365562da0113SBen Gras  */
365662da0113SBen Gras 	    ctime(&now),
365762da0113SBen Gras 	    (LLT)
365862da0113SBen Gras 	    (elapsed == NULL ? 0 : elapsed->tv_sec + (elapsed->tv_usec > 0)),
365962da0113SBen Gras 	    remotehost,
366062da0113SBen Gras 	    (LLT) bytes,
366162da0113SBen Gras 	    r1,
366262da0113SBen Gras 	    type == TYPE_A ? 'a' : 'b',
366362da0113SBen Gras 	    "_",		/* XXX: take conversions into account? */
366462da0113SBen Gras 	    direction,
366562da0113SBen Gras 
366662da0113SBen Gras 	    curclass.type == CLASS_GUEST ?  'a' :
366762da0113SBen Gras 	    curclass.type == CLASS_CHROOT ? 'g' :
366862da0113SBen Gras 	    curclass.type == CLASS_REAL ?   'r' : '?',
366962da0113SBen Gras 
367062da0113SBen Gras 	    curclass.type == CLASS_GUEST ? pw->pw_passwd : pw->pw_name,
367162da0113SBen Gras 	    error != NULL ? 'i' : 'c'
367262da0113SBen Gras 	    );
367362da0113SBen Gras 
367462da0113SBen Gras 	if ((doxferlog & 2) && xferlogfd != -1)
367562da0113SBen Gras 		write(xferlogfd, buf, len);
367662da0113SBen Gras 	if ((doxferlog & 1)) {
367762da0113SBen Gras 		buf[len-1] = '\n';	/* strip \n from syslog message */
367862da0113SBen Gras 		syslog(LOG_INFO, "xferlog: %s", buf);
367962da0113SBen Gras 	}
368062da0113SBen Gras }
368162da0113SBen Gras 
368262da0113SBen Gras /*
368362da0113SBen Gras  * Log the resource usage.
368462da0113SBen Gras  *
368562da0113SBen Gras  * XXX: more resource usage to logging?
368662da0113SBen Gras  */
368762da0113SBen Gras void
logrusage(const struct rusage * rusage_before,const struct rusage * rusage_after)368862da0113SBen Gras logrusage(const struct rusage *rusage_before,
368962da0113SBen Gras     const struct rusage *rusage_after)
369062da0113SBen Gras {
369162da0113SBen Gras 	struct timeval usrtime, systime;
369262da0113SBen Gras 
369362da0113SBen Gras 	if (logging <= 1)
369462da0113SBen Gras 		return;
369562da0113SBen Gras 
369662da0113SBen Gras 	timersub(&rusage_after->ru_utime, &rusage_before->ru_utime, &usrtime);
369762da0113SBen Gras 	timersub(&rusage_after->ru_stime, &rusage_before->ru_stime, &systime);
369862da0113SBen Gras 	syslog(LOG_INFO, LLF ".%.03ldu " LLF ".%.03lds %ld+%ldio %ldpf+%ldw",
369962da0113SBen Gras 	    (LLT)usrtime.tv_sec, (long)(usrtime.tv_usec / 1000),
370062da0113SBen Gras 	    (LLT)systime.tv_sec, (long)(systime.tv_usec / 1000),
370162da0113SBen Gras 	    rusage_after->ru_inblock - rusage_before->ru_inblock,
370262da0113SBen Gras 	    rusage_after->ru_oublock - rusage_before->ru_oublock,
370362da0113SBen Gras 	    rusage_after->ru_majflt - rusage_before->ru_majflt,
370462da0113SBen Gras 	    rusage_after->ru_nswap - rusage_before->ru_nswap);
370562da0113SBen Gras }
370662da0113SBen Gras 
370762da0113SBen Gras /*
370862da0113SBen Gras  * Determine if `password' is valid for user given in `pw'.
370962da0113SBen Gras  * Returns 2 if password expired, 1 if otherwise failed, 0 if ok
371062da0113SBen Gras  */
371162da0113SBen Gras int
checkpassword(const struct passwd * pwent,const char * password)371262da0113SBen Gras checkpassword(const struct passwd *pwent, const char *password)
371362da0113SBen Gras {
371462da0113SBen Gras 	const char *orig;
371562da0113SBen Gras 	char	*new;
371662da0113SBen Gras 	time_t	 change, expire, now;
371762da0113SBen Gras 
371862da0113SBen Gras 	change = expire = 0;
371962da0113SBen Gras 	if (pwent == NULL)
372062da0113SBen Gras 		return 1;
372162da0113SBen Gras 
372262da0113SBen Gras 	time(&now);
372362da0113SBen Gras 	orig = pwent->pw_passwd;	/* save existing password */
372462da0113SBen Gras 	expire = pwent->pw_expire;
372562da0113SBen Gras 	change = pwent->pw_change;
372662da0113SBen Gras 	if (change == _PASSWORD_CHGNOW)
372762da0113SBen Gras 		change = now;
372862da0113SBen Gras 
372962da0113SBen Gras 	if (orig[0] == '\0')		/* don't allow empty passwords */
373062da0113SBen Gras 		return 1;
373162da0113SBen Gras 
373262da0113SBen Gras 	new = crypt(password, orig);	/* encrypt given password */
373362da0113SBen Gras 	if (strcmp(new, orig) != 0)	/* compare */
373462da0113SBen Gras 		return 1;
373562da0113SBen Gras 
373662da0113SBen Gras 	if ((expire && now >= expire) || (change && now >= change))
373762da0113SBen Gras 		return 2;		/* check if expired */
373862da0113SBen Gras 
373962da0113SBen Gras 	return 0;			/* OK! */
374062da0113SBen Gras }
374162da0113SBen Gras 
374262da0113SBen Gras char *
ftpd_strdup(const char * s)374362da0113SBen Gras ftpd_strdup(const char *s)
374462da0113SBen Gras {
374562da0113SBen Gras 	char *new = strdup(s);
374662da0113SBen Gras 
374762da0113SBen Gras 	if (new == NULL)
374862da0113SBen Gras 		fatal("Local resource failure: malloc");
374962da0113SBen Gras 		/* NOTREACHED */
375062da0113SBen Gras 	return (new);
375162da0113SBen Gras }
375262da0113SBen Gras 
375362da0113SBen Gras /*
375462da0113SBen Gras  * As per fprintf(), but increment total_bytes and total_bytes_out,
375562da0113SBen Gras  * by the appropriate amount.
375662da0113SBen Gras  */
375762da0113SBen Gras void
cprintf(FILE * fd,const char * fmt,...)375862da0113SBen Gras cprintf(FILE *fd, const char *fmt, ...)
375962da0113SBen Gras {
376062da0113SBen Gras 	off_t b;
376162da0113SBen Gras 	va_list ap;
376262da0113SBen Gras 
376362da0113SBen Gras 	va_start(ap, fmt);
376462da0113SBen Gras 	b = vfprintf(fd, fmt, ap);
376562da0113SBen Gras 	va_end(ap);
376662da0113SBen Gras 	total_bytes += b;
376762da0113SBen Gras 	total_bytes_out += b;
376862da0113SBen Gras }
376962da0113SBen Gras 
377062da0113SBen Gras #ifdef USE_PAM
377162da0113SBen Gras /*
377262da0113SBen Gras  * the following code is stolen from imap-uw PAM authentication module and
377362da0113SBen Gras  * login.c
377462da0113SBen Gras  */
377562da0113SBen Gras typedef struct {
377662da0113SBen Gras 	const char *uname;	/* user name */
377762da0113SBen Gras 	int	    triedonce;	/* if non-zero, tried before */
377862da0113SBen Gras } ftpd_cred_t;
377962da0113SBen Gras 
378062da0113SBen Gras static int
auth_conv(int num_msg,const struct pam_message ** msg,struct pam_response ** resp,void * appdata)378162da0113SBen Gras auth_conv(int num_msg, const struct pam_message **msg,
378262da0113SBen Gras     struct pam_response **resp, void *appdata)
378362da0113SBen Gras {
378462da0113SBen Gras 	int i, ret;
378562da0113SBen Gras 	size_t n;
378662da0113SBen Gras 	ftpd_cred_t *cred = (ftpd_cred_t *) appdata;
378762da0113SBen Gras 	struct pam_response *myreply;
378862da0113SBen Gras 	char pbuf[FTP_BUFLEN];
378962da0113SBen Gras 
379062da0113SBen Gras 	if (num_msg <= 0 || num_msg > PAM_MAX_NUM_MSG)
379162da0113SBen Gras 		return (PAM_CONV_ERR);
379262da0113SBen Gras 	myreply = calloc(num_msg, sizeof *myreply);
379362da0113SBen Gras 	if (myreply == NULL)
379462da0113SBen Gras 		return PAM_BUF_ERR;
379562da0113SBen Gras 
379662da0113SBen Gras 	for (i = 0; i < num_msg; i++) {
379762da0113SBen Gras 		myreply[i].resp_retcode = 0;
379862da0113SBen Gras 		myreply[i].resp = NULL;
379962da0113SBen Gras 		switch (msg[i]->msg_style) {
380062da0113SBen Gras 		case PAM_PROMPT_ECHO_ON:	/* user */
380162da0113SBen Gras 			myreply[i].resp = ftpd_strdup(cred->uname);
380262da0113SBen Gras 			/* PAM frees resp. */
380362da0113SBen Gras 			break;
380462da0113SBen Gras 		case PAM_PROMPT_ECHO_OFF:	/* authtok (password) */
380562da0113SBen Gras 				/*
380662da0113SBen Gras 				 * Only send a single 331 reply and
380762da0113SBen Gras 				 * then expect a PASS.
380862da0113SBen Gras 				 */
380962da0113SBen Gras 			if (cred->triedonce) {
381062da0113SBen Gras 				syslog(LOG_ERR,
381162da0113SBen Gras 			"auth_conv: already performed PAM_PROMPT_ECHO_OFF");
381262da0113SBen Gras 				goto fail;
381362da0113SBen Gras 			}
381462da0113SBen Gras 			cred->triedonce++;
381562da0113SBen Gras 			if (msg[i]->msg[0] == '\0') {
381662da0113SBen Gras 				(void)strlcpy(pbuf, "password", sizeof(pbuf));
381762da0113SBen Gras 			} else {
381862da0113SBen Gras 					/* Uncapitalize msg */
381962da0113SBen Gras 				(void)strlcpy(pbuf, msg[i]->msg, sizeof(pbuf));
382062da0113SBen Gras 				if (isupper((unsigned char)pbuf[0]))
382162da0113SBen Gras 					pbuf[0] = tolower(
382262da0113SBen Gras 					    (unsigned char)pbuf[0]);
382362da0113SBen Gras 					/* Remove trailing ':' and whitespace */
382462da0113SBen Gras 				n = strlen(pbuf);
382562da0113SBen Gras 				while (n-- > 0) {
382662da0113SBen Gras 					if (isspace((unsigned char)pbuf[n]) ||
382762da0113SBen Gras 					    pbuf[n] == ':')
382862da0113SBen Gras 						pbuf[n] = '\0';
382962da0113SBen Gras 					else
383062da0113SBen Gras 						break;
383162da0113SBen Gras 				}
383262da0113SBen Gras 			}
383362da0113SBen Gras 				/* Send reply, wait for a response. */
383462da0113SBen Gras 			reply(331, "User %s accepted, provide %s.",
383562da0113SBen Gras 			    cred->uname, pbuf);
383662da0113SBen Gras 			(void) alarm(curclass.timeout);
383762da0113SBen Gras 			ret = get_line(pbuf, sizeof(pbuf)-1, stdin);
383862da0113SBen Gras 			(void) alarm(0);
383962da0113SBen Gras 			if (ret == -1) {
384062da0113SBen Gras 				reply(221, "You could at least say goodbye.");
384162da0113SBen Gras 				dologout(0);
384262da0113SBen Gras 			} else if (ret == -2) {
384362da0113SBen Gras 			    /* XXX: should we do this reply(-530, ..) ? */
384462da0113SBen Gras 				reply(-530, "Command too long.");
384562da0113SBen Gras 				goto fail;
384662da0113SBen Gras 			}
384762da0113SBen Gras 				/* Ensure it is PASS */
384862da0113SBen Gras 			if (strncasecmp(pbuf, "PASS ", 5) != 0) {
384962da0113SBen Gras 				syslog(LOG_ERR,
385062da0113SBen Gras 				    "auth_conv: unexpected reply '%.4s'", pbuf);
385162da0113SBen Gras 				/* XXX: should we do this reply(-530, ..) ? */
385262da0113SBen Gras 				reply(-530, "Unexpected reply '%.4s'.", pbuf);
385362da0113SBen Gras 				goto fail;
385462da0113SBen Gras 			}
385562da0113SBen Gras 				/* Strip CRLF from "PASS" reply */
385662da0113SBen Gras 			n = strlen(pbuf);
385762da0113SBen Gras 			while (--n >= 5 &&
385862da0113SBen Gras 			    (pbuf[n] == '\r' || pbuf[n] == '\n'))
385962da0113SBen Gras 			    pbuf[n] = '\0';
386062da0113SBen Gras 				/* Copy password into reply */
386162da0113SBen Gras 			myreply[i].resp = ftpd_strdup(pbuf+5);
386262da0113SBen Gras 				/* PAM frees resp. */
386362da0113SBen Gras 			break;
386462da0113SBen Gras 		case PAM_TEXT_INFO:
386562da0113SBen Gras 		case PAM_ERROR_MSG:
386662da0113SBen Gras 			break;
386762da0113SBen Gras 		default:			/* unknown message style */
386862da0113SBen Gras 			goto fail;
386962da0113SBen Gras 		}
387062da0113SBen Gras 	}
387162da0113SBen Gras 
387262da0113SBen Gras 	*resp = myreply;
387362da0113SBen Gras 	return PAM_SUCCESS;
387462da0113SBen Gras 
387562da0113SBen Gras  fail:
387662da0113SBen Gras 	free(myreply);
387762da0113SBen Gras 	*resp = NULL;
387862da0113SBen Gras 	return PAM_CONV_ERR;
387962da0113SBen Gras }
388062da0113SBen Gras 
388162da0113SBen Gras /*
388262da0113SBen Gras  * Attempt to authenticate the user using PAM.  Returns 0 if the user is
388362da0113SBen Gras  * authenticated, or 1 if not authenticated.  If some sort of PAM system
388462da0113SBen Gras  * error occurs (e.g., the "/etc/pam.conf" file is missing) then this
388562da0113SBen Gras  * function returns -1.  This can be used as an indication that we should
388662da0113SBen Gras  * fall back to a different authentication mechanism.
388762da0113SBen Gras  * pw maybe be updated to a new user if PAM_USER changes from curname.
388862da0113SBen Gras  */
388962da0113SBen Gras static int
auth_pam(void)389062da0113SBen Gras auth_pam(void)
389162da0113SBen Gras {
389262da0113SBen Gras 	const char *tmpl_user;
389362da0113SBen Gras 	const void *item;
389462da0113SBen Gras 	int rval;
389562da0113SBen Gras 	int e;
389662da0113SBen Gras 	ftpd_cred_t auth_cred = { curname, 0 };
389762da0113SBen Gras 	struct pam_conv conv = { &auth_conv, &auth_cred };
389862da0113SBen Gras 
389962da0113SBen Gras 	e = pam_start("ftpd", curname, &conv, &pamh);
390062da0113SBen Gras 	if (e != PAM_SUCCESS) {
390162da0113SBen Gras 		/*
390262da0113SBen Gras 		 * In OpenPAM, it's OK to pass NULL to pam_strerror()
390362da0113SBen Gras 		 * if context creation has failed in the first place.
390462da0113SBen Gras 		 */
390562da0113SBen Gras 		syslog(LOG_ERR, "pam_start: %s", pam_strerror(NULL, e));
390662da0113SBen Gras 		return -1;
390762da0113SBen Gras 	}
390862da0113SBen Gras 
390962da0113SBen Gras 	e = pam_set_item(pamh, PAM_RHOST, remotehost);
391062da0113SBen Gras 	if (e != PAM_SUCCESS) {
391162da0113SBen Gras 		syslog(LOG_ERR, "pam_set_item(PAM_RHOST): %s",
391262da0113SBen Gras 			pam_strerror(pamh, e));
391362da0113SBen Gras 		if ((e = pam_end(pamh, e)) != PAM_SUCCESS) {
391462da0113SBen Gras 			syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e));
391562da0113SBen Gras 		}
391662da0113SBen Gras 		pamh = NULL;
391762da0113SBen Gras 		return -1;
391862da0113SBen Gras 	}
391962da0113SBen Gras 
392062da0113SBen Gras 	e = pam_set_item(pamh, PAM_SOCKADDR, &his_addr);
392162da0113SBen Gras 	if (e != PAM_SUCCESS) {
392262da0113SBen Gras 		syslog(LOG_ERR, "pam_set_item(PAM_SOCKADDR): %s",
392362da0113SBen Gras 			pam_strerror(pamh, e));
392462da0113SBen Gras 		if ((e = pam_end(pamh, e)) != PAM_SUCCESS) {
392562da0113SBen Gras 			syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e));
392662da0113SBen Gras 		}
392762da0113SBen Gras 		pamh = NULL;
392862da0113SBen Gras 		return -1;
392962da0113SBen Gras 	}
393062da0113SBen Gras 
393162da0113SBen Gras 	e = pam_authenticate(pamh, 0);
393262da0113SBen Gras 	if (ftpd_debug)
393362da0113SBen Gras 		syslog(LOG_DEBUG, "pam_authenticate: user '%s' returned %d",
393462da0113SBen Gras 		    curname, e);
393562da0113SBen Gras 	switch (e) {
393662da0113SBen Gras 	case PAM_SUCCESS:
393762da0113SBen Gras 		/*
393862da0113SBen Gras 		 * With PAM we support the concept of a "template"
393962da0113SBen Gras 		 * user.  The user enters a login name which is
394062da0113SBen Gras 		 * authenticated by PAM, usually via a remote service
394162da0113SBen Gras 		 * such as RADIUS or TACACS+.  If authentication
394262da0113SBen Gras 		 * succeeds, a different but related "template" name
394362da0113SBen Gras 		 * is used for setting the credentials, shell, and
394462da0113SBen Gras 		 * home directory.  The name the user enters need only
394562da0113SBen Gras 		 * exist on the remote authentication server, but the
394662da0113SBen Gras 		 * template name must be present in the local password
394762da0113SBen Gras 		 * database.
394862da0113SBen Gras 		 *
394962da0113SBen Gras 		 * This is supported by two various mechanisms in the
395062da0113SBen Gras 		 * individual modules.  However, from the application's
395162da0113SBen Gras 		 * point of view, the template user is always passed
395262da0113SBen Gras 		 * back as a changed value of the PAM_USER item.
395362da0113SBen Gras 		 */
395462da0113SBen Gras 		if ((e = pam_get_item(pamh, PAM_USER, &item)) ==
395562da0113SBen Gras 		    PAM_SUCCESS) {
395662da0113SBen Gras 			tmpl_user = (const char *) item;
395762da0113SBen Gras 			if (pw == NULL
395862da0113SBen Gras 			    || strcmp(pw->pw_name, tmpl_user) != 0) {
395962da0113SBen Gras 				pw = sgetpwnam(tmpl_user);
396062da0113SBen Gras 				if (ftpd_debug)
396162da0113SBen Gras 					syslog(LOG_DEBUG,
396262da0113SBen Gras 					    "auth_pam: PAM changed "
396362da0113SBen Gras 					    "user from '%s' to '%s'",
396462da0113SBen Gras 					    curname, pw->pw_name);
396562da0113SBen Gras 				(void)strlcpy(curname, pw->pw_name,
396662da0113SBen Gras 				    curname_len);
396762da0113SBen Gras 			}
396862da0113SBen Gras 		} else
396962da0113SBen Gras 			syslog(LOG_ERR, "Couldn't get PAM_USER: %s",
397062da0113SBen Gras 			    pam_strerror(pamh, e));
397162da0113SBen Gras 		rval = 0;
397262da0113SBen Gras 		break;
397362da0113SBen Gras 
397462da0113SBen Gras 	case PAM_AUTH_ERR:
397562da0113SBen Gras 	case PAM_USER_UNKNOWN:
397662da0113SBen Gras 	case PAM_MAXTRIES:
397762da0113SBen Gras 		rval = 1;
397862da0113SBen Gras 		break;
397962da0113SBen Gras 
398062da0113SBen Gras 	default:
398162da0113SBen Gras 		syslog(LOG_ERR, "pam_authenticate: %s", pam_strerror(pamh, e));
398262da0113SBen Gras 		rval = -1;
398362da0113SBen Gras 		break;
398462da0113SBen Gras 	}
398562da0113SBen Gras 
398662da0113SBen Gras 	if (rval == 0) {
398762da0113SBen Gras 		e = pam_acct_mgmt(pamh, 0);
398862da0113SBen Gras 		if (e != PAM_SUCCESS) {
398962da0113SBen Gras 			syslog(LOG_ERR, "pam_acct_mgmt: %s",
399062da0113SBen Gras 						pam_strerror(pamh, e));
399162da0113SBen Gras 			rval = 1;
399262da0113SBen Gras 		}
399362da0113SBen Gras 	}
399462da0113SBen Gras 
399562da0113SBen Gras 	if (rval != 0) {
399662da0113SBen Gras 		if ((e = pam_end(pamh, e)) != PAM_SUCCESS) {
399762da0113SBen Gras 			syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e));
399862da0113SBen Gras 		}
399962da0113SBen Gras 		pamh = NULL;
400062da0113SBen Gras 	}
400162da0113SBen Gras 	return rval;
400262da0113SBen Gras }
400362da0113SBen Gras 
400462da0113SBen Gras #endif /* USE_PAM */
4005