xref: /freebsd/libexec/ftpd/ftpd.c (revision ce9287fc02f73645761e9cd5fbcf8f5f8246dc7a)
1ea022d16SRodney W. Grimes /*
2ea022d16SRodney W. Grimes  * Copyright (c) 1985, 1988, 1990, 1992, 1993, 1994
3ea022d16SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4ea022d16SRodney W. Grimes  *
5ea022d16SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6ea022d16SRodney W. Grimes  * modification, are permitted provided that the following conditions
7ea022d16SRodney W. Grimes  * are met:
8ea022d16SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9ea022d16SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10ea022d16SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11ea022d16SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12ea022d16SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13ea022d16SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
14ea022d16SRodney W. Grimes  *    must display the following acknowledgement:
15ea022d16SRodney W. Grimes  *	This product includes software developed by the University of
16ea022d16SRodney W. Grimes  *	California, Berkeley and its contributors.
17ea022d16SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
18ea022d16SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
19ea022d16SRodney W. Grimes  *    without specific prior written permission.
20ea022d16SRodney W. Grimes  *
21ea022d16SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22ea022d16SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23ea022d16SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24ea022d16SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25ea022d16SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26ea022d16SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27ea022d16SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28ea022d16SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29ea022d16SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30ea022d16SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31ea022d16SRodney W. Grimes  * SUCH DAMAGE.
32ea022d16SRodney W. Grimes  */
33ea022d16SRodney W. Grimes 
349aca17cbSMark Murray #if 0
35ea022d16SRodney W. Grimes #ifndef lint
36ea022d16SRodney W. Grimes static char copyright[] =
37ea022d16SRodney W. Grimes "@(#) Copyright (c) 1985, 1988, 1990, 1992, 1993, 1994\n\
38ea022d16SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
39ea022d16SRodney W. Grimes #endif /* not lint */
409aca17cbSMark Murray #endif
41ea022d16SRodney W. Grimes 
42ea022d16SRodney W. Grimes #ifndef lint
43e02897faSPhilippe Charnier #if 0
44ea022d16SRodney W. Grimes static char sccsid[] = "@(#)ftpd.c	8.4 (Berkeley) 4/16/94";
459aca17cbSMark Murray #endif
46e02897faSPhilippe Charnier static const char rcsid[] =
477f3dea24SPeter Wemm   "$FreeBSD$";
48e02897faSPhilippe Charnier #endif /* not lint */
49ea022d16SRodney W. Grimes 
50ea022d16SRodney W. Grimes /*
51ea022d16SRodney W. Grimes  * FTP server.
52ea022d16SRodney W. Grimes  */
53ea022d16SRodney W. Grimes #include <sys/param.h>
54ea022d16SRodney W. Grimes #include <sys/ioctl.h>
559fc5823aSGarrett Wollman #include <sys/mman.h>
56eb2fc780SGarrett Wollman #include <sys/socket.h>
57eb2fc780SGarrett Wollman #include <sys/stat.h>
58eb2fc780SGarrett Wollman #include <sys/time.h>
59eb2fc780SGarrett Wollman #include <sys/wait.h>
60ea022d16SRodney W. Grimes 
61ea022d16SRodney W. Grimes #include <netinet/in.h>
62ea022d16SRodney W. Grimes #include <netinet/in_systm.h>
63ea022d16SRodney W. Grimes #include <netinet/ip.h>
649fc5823aSGarrett Wollman #include <netinet/tcp.h>
65ea022d16SRodney W. Grimes 
66ea022d16SRodney W. Grimes #define	FTP_NAMES
67ea022d16SRodney W. Grimes #include <arpa/ftp.h>
68ea022d16SRodney W. Grimes #include <arpa/inet.h>
69ea022d16SRodney W. Grimes #include <arpa/telnet.h>
70ea022d16SRodney W. Grimes 
71ea022d16SRodney W. Grimes #include <ctype.h>
72ea022d16SRodney W. Grimes #include <dirent.h>
73ea022d16SRodney W. Grimes #include <err.h>
74ea022d16SRodney W. Grimes #include <errno.h>
75ea022d16SRodney W. Grimes #include <fcntl.h>
76ea022d16SRodney W. Grimes #include <glob.h>
77ea022d16SRodney W. Grimes #include <limits.h>
78ea022d16SRodney W. Grimes #include <netdb.h>
79ea022d16SRodney W. Grimes #include <pwd.h>
8031fea7b8SDavid Nugent #include <grp.h>
8147499ecdSAndrey A. Chernov #include <opie.h>
82ea022d16SRodney W. Grimes #include <signal.h>
83ea022d16SRodney W. Grimes #include <stdio.h>
84ea022d16SRodney W. Grimes #include <stdlib.h>
85ea022d16SRodney W. Grimes #include <string.h>
86ea022d16SRodney W. Grimes #include <syslog.h>
87ea022d16SRodney W. Grimes #include <time.h>
88ea022d16SRodney W. Grimes #include <unistd.h>
89b63e1fe2SPeter Wemm #include <libutil.h>
90b071c689SDavid Nugent #ifdef	LOGIN_CAP
91b071c689SDavid Nugent #include <login_cap.h>
92b071c689SDavid Nugent #endif
93ea022d16SRodney W. Grimes 
945bc9d93dSMark Murray #ifdef USE_PAM
956c9134c0SMark Murray #include <security/pam_appl.h>
966c9134c0SMark Murray #endif
976c9134c0SMark Murray 
98ea022d16SRodney W. Grimes #include "pathnames.h"
99ea022d16SRodney W. Grimes #include "extern.h"
100ea022d16SRodney W. Grimes 
101ea022d16SRodney W. Grimes #include <stdarg.h>
102ea022d16SRodney W. Grimes 
103af85d782SDavid Nugent static char version[] = "Version 6.00LS";
104af85d782SDavid Nugent #undef main
105ea022d16SRodney W. Grimes 
106ea022d16SRodney W. Grimes extern	off_t restart_point;
107ea022d16SRodney W. Grimes extern	char cbuf[];
108ea022d16SRodney W. Grimes 
1094dd8b5abSYoshinobu Inoue union sockunion server_addr;
1104dd8b5abSYoshinobu Inoue union sockunion ctrl_addr;
1114dd8b5abSYoshinobu Inoue union sockunion data_source;
1124dd8b5abSYoshinobu Inoue union sockunion data_dest;
1134dd8b5abSYoshinobu Inoue union sockunion his_addr;
1144dd8b5abSYoshinobu Inoue union sockunion pasv_addr;
115ea022d16SRodney W. Grimes 
116cf09a206SDavid Greenman int	daemon_mode;
117ea022d16SRodney W. Grimes int	data;
11863591ba5SYaroslav Tykhiy int	dataport;
119ea022d16SRodney W. Grimes int	logged_in;
120ea022d16SRodney W. Grimes struct	passwd *pw;
121ce9287fcSYaroslav Tykhiy char	*homedir;
122618b0bbaSMark Murray int	ftpdebug;
123ea022d16SRodney W. Grimes int	timeout = 900;    /* timeout after 15 minutes of inactivity */
124ea022d16SRodney W. Grimes int	maxtimeout = 7200;/* don't allow idle time to be set beyond 2 hours */
125ea022d16SRodney W. Grimes int	logging;
1264c450ad7SPaul Traina int	restricted_data_ports = 1;
127a5a4544eSPaul Traina int	paranoid = 1;	  /* be extra careful about security */
1285a392aecSTorsten Blum int	anon_only = 0;    /* Only anonymous ftp allowed */
129ea022d16SRodney W. Grimes int	guest;
130a5a4544eSPaul Traina int	dochroot;
1315d7e0128SYaroslav Tykhiy int	dowtmp = 1;
1323eb568f2SGuido van Rooij int	stats;
1333eb568f2SGuido van Rooij int	statfd = -1;
134ea022d16SRodney W. Grimes int	type;
135ea022d16SRodney W. Grimes int	form;
136ea022d16SRodney W. Grimes int	stru;			/* avoid C keyword */
137ea022d16SRodney W. Grimes int	mode;
138ea022d16SRodney W. Grimes int	usedefault = 1;		/* for data transfers */
139ea022d16SRodney W. Grimes int	pdata = -1;		/* for passive mode */
140a4b77a2aSPoul-Henning Kamp int	readonly=0;		/* Server is in readonly mode.	*/
141a4b77a2aSPoul-Henning Kamp int	noepsv=0;		/* EPSV command is disabled.	*/
14262513e76SNik Clayton int	noretr=0;		/* RETR command is disabled.	*/
1431cc9f0bbSSheldon Hearn int	noguestretr=0;		/* RETR command is disabled for anon users. */
144d186bb12SMatthew N. Dodd int	noguestmkd=0;		/* MKD command is disabled for anon users. */
145a117c345SYaroslav Tykhiy int	noguestmod=1;		/* anon users may not modify existing files. */
14662513e76SNik Clayton 
1474b82fc95SYaroslav Tykhiy static volatile sig_atomic_t recvurg;
148ea022d16SRodney W. Grimes sig_atomic_t transflag;
149ea022d16SRodney W. Grimes off_t	file_size;
150ea022d16SRodney W. Grimes off_t	byte_count;
151ea022d16SRodney W. Grimes #if !defined(CMASK) || CMASK == 0
152ea022d16SRodney W. Grimes #undef CMASK
153ea022d16SRodney W. Grimes #define CMASK 027
154ea022d16SRodney W. Grimes #endif
155ea022d16SRodney W. Grimes int	defumask = CMASK;		/* default umask value */
156ea022d16SRodney W. Grimes char	tmpline[7];
157ea4e54b9SDavid Nugent char	*hostname;
158ad442344SDima Dorfman int	epsvall = 0;
159ad442344SDima Dorfman 
1600512556aSDavid Nugent #ifdef VIRTUAL_HOSTING
161ea4e54b9SDavid Nugent char	*ftpuser;
162ea4e54b9SDavid Nugent 
163ea4e54b9SDavid Nugent static struct ftphost {
164ea4e54b9SDavid Nugent 	struct ftphost	*next;
165f38c6cadSYoshinobu Inoue 	struct addrinfo *hostinfo;
166ea4e54b9SDavid Nugent 	char		*hostname;
167ea4e54b9SDavid Nugent 	char		*anonuser;
168ea4e54b9SDavid Nugent 	char		*statfile;
169ea4e54b9SDavid Nugent 	char		*welcome;
170ea4e54b9SDavid Nugent 	char		*loginmsg;
171ea4e54b9SDavid Nugent } *thishost, *firsthost;
172ea4e54b9SDavid Nugent 
173ea4e54b9SDavid Nugent #endif
1749e9a43bdSBrian Somers char	remotehost[MAXHOSTNAMELEN];
1753eb568f2SGuido van Rooij char	*ident = NULL;
176a5a4544eSPaul Traina 
177a5a4544eSPaul Traina static char ttyline[20];
178a5a4544eSPaul Traina char	*tty = ttyline;		/* for klogin */
179a5a4544eSPaul Traina 
1805bc9d93dSMark Murray #ifdef USE_PAM
181e4bc453cSWarner Losh static int	auth_pam(struct passwd**, const char*);
1825bc9d93dSMark Murray pam_handle_t *pamh = NULL;
18347499ecdSAndrey A. Chernov #endif
184fa1746c9SMark Murray 
185fa1746c9SMark Murray static struct opie opiedata;
186fa1746c9SMark Murray static char opieprompt[OPIE_CHALLENGE_MAX+1];
18747499ecdSAndrey A. Chernov static int pwok;
1889aca17cbSMark Murray 
189105a3c98SJulian Elischer char	*pid_file = NULL;
190105a3c98SJulian Elischer 
191ea022d16SRodney W. Grimes /*
1926d10cb2fSJonathan Lemon  * Limit number of pathnames that glob can return.
1936d10cb2fSJonathan Lemon  * A limit of 0 indicates the number of pathnames is unlimited.
1946d10cb2fSJonathan Lemon  */
1956d10cb2fSJonathan Lemon #define MAXGLOBARGS	16384
1966d10cb2fSJonathan Lemon #
1976d10cb2fSJonathan Lemon 
1986d10cb2fSJonathan Lemon /*
199ea022d16SRodney W. Grimes  * Timeout intervals for retrying connections
200ea022d16SRodney W. Grimes  * to hosts that don't accept PORT cmds.  This
201ea022d16SRodney W. Grimes  * is a kludge, but given the problems with TCP...
202ea022d16SRodney W. Grimes  */
203ea022d16SRodney W. Grimes #define	SWAITMAX	90	/* wait at most 90 seconds */
204ea022d16SRodney W. Grimes #define	SWAITINT	5	/* interval between retries */
205ea022d16SRodney W. Grimes 
206ea022d16SRodney W. Grimes int	swaitmax = SWAITMAX;
207ea022d16SRodney W. Grimes int	swaitint = SWAITINT;
208ea022d16SRodney W. Grimes 
209ea022d16SRodney W. Grimes #ifdef SETPROCTITLE
210b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE
211ea022d16SRodney W. Grimes char	**Argv = NULL;		/* pointer to argument vector */
212ea022d16SRodney W. Grimes char	*LastArgv = NULL;	/* end of argv */
213b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */
214ea022d16SRodney W. Grimes char	proctitle[LINE_MAX];	/* initial part of title */
215ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */
216ea022d16SRodney W. Grimes 
217ea022d16SRodney W. Grimes #define LOGCMD(cmd, file) \
218ea022d16SRodney W. Grimes 	if (logging > 1) \
219ea022d16SRodney W. Grimes 	    syslog(LOG_INFO,"%s %s%s", cmd, \
220ea022d16SRodney W. Grimes 		*(file) == '/' ? "" : curdir(), file);
221ea022d16SRodney W. Grimes #define LOGCMD2(cmd, file1, file2) \
222ea022d16SRodney W. Grimes 	 if (logging > 1) \
223ea022d16SRodney W. Grimes 	    syslog(LOG_INFO,"%s %s%s %s%s", cmd, \
224ea022d16SRodney W. Grimes 		*(file1) == '/' ? "" : curdir(), file1, \
225ea022d16SRodney W. Grimes 		*(file2) == '/' ? "" : curdir(), file2);
226ea022d16SRodney W. Grimes #define LOGBYTES(cmd, file, cnt) \
227ea022d16SRodney W. Grimes 	if (logging > 1) { \
228ea022d16SRodney W. Grimes 		if (cnt == (off_t)-1) \
229ea022d16SRodney W. Grimes 		    syslog(LOG_INFO,"%s %s%s", cmd, \
230ea022d16SRodney W. Grimes 			*(file) == '/' ? "" : curdir(), file); \
231ea022d16SRodney W. Grimes 		else \
232ea022d16SRodney W. Grimes 		    syslog(LOG_INFO, "%s %s%s = %qd bytes", \
233ea022d16SRodney W. Grimes 			cmd, (*(file) == '/') ? "" : curdir(), file, cnt); \
234ea022d16SRodney W. Grimes 	}
235ea022d16SRodney W. Grimes 
236ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
237e4bc453cSWarner Losh static void	 inithosts(void);
238e4bc453cSWarner Losh static void	selecthost(union sockunion *);
239ea4e54b9SDavid Nugent #endif
240e4bc453cSWarner Losh static void	 ack(char *);
241e4bc453cSWarner Losh static void	 sigurg(int);
242e4bc453cSWarner Losh static void	 myoob(void);
2438657b576SYaroslav Tykhiy static int	 checkuser(char *, char *, int, char **);
244e4bc453cSWarner Losh static FILE	*dataconn(char *, off_t, char *);
245e4bc453cSWarner Losh static void	 dolog(struct sockaddr *);
246e4bc453cSWarner Losh static char	*curdir(void);
247e4bc453cSWarner Losh static void	 end_login(void);
248e4bc453cSWarner Losh static FILE	*getdatasock(char *);
249a117c345SYaroslav Tykhiy static int	 guniquefd(char *, char **);
250e4bc453cSWarner Losh static void	 lostconn(int);
251e4bc453cSWarner Losh static void	 sigquit(int);
252e4bc453cSWarner Losh static int	 receive_data(FILE *, FILE *);
253e4bc453cSWarner Losh static int	 send_data(FILE *, FILE *, off_t, off_t, int);
254ea022d16SRodney W. Grimes static struct passwd *
255e4bc453cSWarner Losh 		 sgetpwnam(char *);
256e4bc453cSWarner Losh static char	*sgetsave(char *);
257e4bc453cSWarner Losh static void	 reapchild(int);
258e4bc453cSWarner Losh static void      logxfer(char *, off_t, time_t);
259e4648f05SYaroslav Tykhiy static char	*doublequote(char *);
260ea022d16SRodney W. Grimes 
261ea022d16SRodney W. Grimes static char *
262e4bc453cSWarner Losh curdir(void)
263ea022d16SRodney W. Grimes {
264ea022d16SRodney W. Grimes 	static char path[MAXPATHLEN+1+1];	/* path + '/' + '\0' */
265ea022d16SRodney W. Grimes 
266ea022d16SRodney W. Grimes 	if (getcwd(path, sizeof(path)-2) == NULL)
267ea022d16SRodney W. Grimes 		return ("");
268ea022d16SRodney W. Grimes 	if (path[1] != '\0')		/* special case for root dir. */
269ea022d16SRodney W. Grimes 		strcat(path, "/");
270ea022d16SRodney W. Grimes 	/* For guest account, skip / since it's chrooted */
271ea022d16SRodney W. Grimes 	return (guest ? path+1 : path);
272ea022d16SRodney W. Grimes }
273ea022d16SRodney W. Grimes 
274ea022d16SRodney W. Grimes int
275e4bc453cSWarner Losh main(int argc, char *argv[], char **envp)
276ea022d16SRodney W. Grimes {
277ea022d16SRodney W. Grimes 	int addrlen, ch, on = 1, tos;
278ea022d16SRodney W. Grimes 	char *cp, line[LINE_MAX];
279ea022d16SRodney W. Grimes 	FILE *fd;
2804dd8b5abSYoshinobu Inoue 	int error;
2814dd8b5abSYoshinobu Inoue 	char	*bindname = NULL;
28263591ba5SYaroslav Tykhiy 	const char *bindport = "ftp";
2834dd8b5abSYoshinobu Inoue 	int	family = AF_UNSPEC;
2844dd8b5abSYoshinobu Inoue 	int	enable_v4 = 0;
2854b82fc95SYaroslav Tykhiy 	struct sigaction sa;
286ea022d16SRodney W. Grimes 
28734d1ba5cSAndrey A. Chernov 	tzset();		/* in case no timezone database in ~ftp */
2884b82fc95SYaroslav Tykhiy 	sigemptyset(&sa.sa_mask);
2894b82fc95SYaroslav Tykhiy 	sa.sa_flags = SA_RESTART;
29034d1ba5cSAndrey A. Chernov 
291b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE
292ea022d16SRodney W. Grimes 	/*
293ea022d16SRodney W. Grimes 	 *  Save start and extent of argv for setproctitle.
294ea022d16SRodney W. Grimes 	 */
295ea022d16SRodney W. Grimes 	Argv = argv;
296ea022d16SRodney W. Grimes 	while (*envp)
297ea022d16SRodney W. Grimes 		envp++;
298ea022d16SRodney W. Grimes 	LastArgv = envp[-1] + strlen(envp[-1]);
299b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */
300ea022d16SRodney W. Grimes 
3013eb568f2SGuido van Rooij 
30263591ba5SYaroslav Tykhiy 	while ((ch = getopt(argc, argv, "46a:AdDElmMoOp:P:rRSt:T:u:UvW")) != -1) {
303ea022d16SRodney W. Grimes 		switch (ch) {
3040e063efeSYaroslav Tykhiy 		case '4':
3050e063efeSYaroslav Tykhiy 			enable_v4 = 1;
3060e063efeSYaroslav Tykhiy 			if (family == AF_UNSPEC)
3070e063efeSYaroslav Tykhiy 				family = AF_INET;
3080e063efeSYaroslav Tykhiy 			break;
3090e063efeSYaroslav Tykhiy 
3100e063efeSYaroslav Tykhiy 		case '6':
3110e063efeSYaroslav Tykhiy 			family = AF_INET6;
3120e063efeSYaroslav Tykhiy 			break;
3130e063efeSYaroslav Tykhiy 
3140e063efeSYaroslav Tykhiy 		case 'a':
3150e063efeSYaroslav Tykhiy 			bindname = optarg;
3160e063efeSYaroslav Tykhiy 			break;
3170e063efeSYaroslav Tykhiy 
3180e063efeSYaroslav Tykhiy 		case 'A':
3190e063efeSYaroslav Tykhiy 			anon_only = 1;
320cf09a206SDavid Greenman 			break;
321cf09a206SDavid Greenman 
322ea022d16SRodney W. Grimes 		case 'd':
323618b0bbaSMark Murray 			ftpdebug++;
324ea022d16SRodney W. Grimes 			break;
325ea022d16SRodney W. Grimes 
3260e063efeSYaroslav Tykhiy 		case 'D':
3270e063efeSYaroslav Tykhiy 			daemon_mode++;
3280e063efeSYaroslav Tykhiy 			break;
3290e063efeSYaroslav Tykhiy 
330a4b77a2aSPoul-Henning Kamp 		case 'E':
331a4b77a2aSPoul-Henning Kamp 			noepsv = 1;
332a4b77a2aSPoul-Henning Kamp 			break;
333a4b77a2aSPoul-Henning Kamp 
334ea022d16SRodney W. Grimes 		case 'l':
335ea022d16SRodney W. Grimes 			logging++;	/* > 1 == extra logging */
336ea022d16SRodney W. Grimes 			break;
337ea022d16SRodney W. Grimes 
338a117c345SYaroslav Tykhiy 		case 'm':
339a117c345SYaroslav Tykhiy 			noguestmod = 0;
340a117c345SYaroslav Tykhiy 			break;
341a117c345SYaroslav Tykhiy 
3420e063efeSYaroslav Tykhiy 		case 'M':
3430e063efeSYaroslav Tykhiy 			noguestmkd = 1;
3440e063efeSYaroslav Tykhiy 			break;
3450e063efeSYaroslav Tykhiy 
3460e063efeSYaroslav Tykhiy 		case 'o':
3470e063efeSYaroslav Tykhiy 			noretr = 1;
3480e063efeSYaroslav Tykhiy 			break;
3490e063efeSYaroslav Tykhiy 
3500e063efeSYaroslav Tykhiy 		case 'O':
3510e063efeSYaroslav Tykhiy 			noguestretr = 1;
3520e063efeSYaroslav Tykhiy 			break;
3530e063efeSYaroslav Tykhiy 
3540e063efeSYaroslav Tykhiy 		case 'p':
3550e063efeSYaroslav Tykhiy 			pid_file = optarg;
3560e063efeSYaroslav Tykhiy 			break;
3570e063efeSYaroslav Tykhiy 
35863591ba5SYaroslav Tykhiy 		case 'P':
35963591ba5SYaroslav Tykhiy 			bindport = optarg;
36063591ba5SYaroslav Tykhiy 			break;
36163591ba5SYaroslav Tykhiy 
362a4b77a2aSPoul-Henning Kamp 		case 'r':
363a4b77a2aSPoul-Henning Kamp 			readonly = 1;
364a4b77a2aSPoul-Henning Kamp 			break;
365a4b77a2aSPoul-Henning Kamp 
366a5a4544eSPaul Traina 		case 'R':
367a5a4544eSPaul Traina 			paranoid = 0;
368a5a4544eSPaul Traina 			break;
369a5a4544eSPaul Traina 
370a5a4544eSPaul Traina 		case 'S':
371a5a4544eSPaul Traina 			stats++;
372a5a4544eSPaul Traina 			break;
373a5a4544eSPaul Traina 
374ea022d16SRodney W. Grimes 		case 't':
375ea022d16SRodney W. Grimes 			timeout = atoi(optarg);
376ea022d16SRodney W. Grimes 			if (maxtimeout < timeout)
377ea022d16SRodney W. Grimes 				maxtimeout = timeout;
378ea022d16SRodney W. Grimes 			break;
379a5a4544eSPaul Traina 
3800e063efeSYaroslav Tykhiy 		case 'T':
3810e063efeSYaroslav Tykhiy 			maxtimeout = atoi(optarg);
3820e063efeSYaroslav Tykhiy 			if (timeout > maxtimeout)
3830e063efeSYaroslav Tykhiy 				timeout = maxtimeout;
384105a3c98SJulian Elischer 			break;
385105a3c98SJulian Elischer 
386ea022d16SRodney W. Grimes 		case 'u':
387ea022d16SRodney W. Grimes 		    {
388ea022d16SRodney W. Grimes 			long val = 0;
389ea022d16SRodney W. Grimes 
390ea022d16SRodney W. Grimes 			val = strtol(optarg, &optarg, 8);
391ea022d16SRodney W. Grimes 			if (*optarg != '\0' || val < 0)
392ea022d16SRodney W. Grimes 				warnx("bad value for -u");
393ea022d16SRodney W. Grimes 			else
394ea022d16SRodney W. Grimes 				defumask = val;
395ea022d16SRodney W. Grimes 			break;
396ea022d16SRodney W. Grimes 		    }
3970e063efeSYaroslav Tykhiy 		case 'U':
3980e063efeSYaroslav Tykhiy 			restricted_data_ports = 0;
3995a392aecSTorsten Blum 			break;
400ea022d16SRodney W. Grimes 
401ea022d16SRodney W. Grimes 		case 'v':
40293bd9dc5SYaroslav Tykhiy 			ftpdebug++;
403ea022d16SRodney W. Grimes 			break;
404ea022d16SRodney W. Grimes 
4055d7e0128SYaroslav Tykhiy 		case 'W':
4065d7e0128SYaroslav Tykhiy 			dowtmp = 0;
4075d7e0128SYaroslav Tykhiy 			break;
4085d7e0128SYaroslav Tykhiy 
409ea022d16SRodney W. Grimes 		default:
410ea022d16SRodney W. Grimes 			warnx("unknown flag -%c ignored", optopt);
411ea022d16SRodney W. Grimes 			break;
412ea022d16SRodney W. Grimes 		}
413ea022d16SRodney W. Grimes 	}
414cf09a206SDavid Greenman 
415ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
416ea4e54b9SDavid Nugent 	inithosts();
417ea4e54b9SDavid Nugent #endif
418ea022d16SRodney W. Grimes 	(void) freopen(_PATH_DEVNULL, "w", stderr);
419cf09a206SDavid Greenman 
420cf09a206SDavid Greenman 	/*
421cf09a206SDavid Greenman 	 * LOG_NDELAY sets up the logging connection immediately,
422cf09a206SDavid Greenman 	 * necessary for anonymous ftp's that chroot and can't do it later.
423cf09a206SDavid Greenman 	 */
424cf09a206SDavid Greenman 	openlog("ftpd", LOG_PID | LOG_NDELAY, LOG_FTP);
425cf09a206SDavid Greenman 
426cf09a206SDavid Greenman 	if (daemon_mode) {
427cf09a206SDavid Greenman 		int ctl_sock, fd;
4284dd8b5abSYoshinobu Inoue 		struct addrinfo hints, *res;
429cf09a206SDavid Greenman 
430cf09a206SDavid Greenman 		/*
431cf09a206SDavid Greenman 		 * Detach from parent.
432cf09a206SDavid Greenman 		 */
433cf09a206SDavid Greenman 		if (daemon(1, 1) < 0) {
434cf09a206SDavid Greenman 			syslog(LOG_ERR, "failed to become a daemon");
435cf09a206SDavid Greenman 			exit(1);
436cf09a206SDavid Greenman 		}
4374b82fc95SYaroslav Tykhiy 		sa.sa_handler = reapchild;
4384b82fc95SYaroslav Tykhiy 		(void)sigaction(SIGCHLD, &sa, NULL);
4394dd8b5abSYoshinobu Inoue 		/* init bind_sa */
4404dd8b5abSYoshinobu Inoue 		memset(&hints, 0, sizeof(hints));
4414dd8b5abSYoshinobu Inoue 
4424dd8b5abSYoshinobu Inoue 		hints.ai_family = family == AF_UNSPEC ? AF_INET : family;
4434dd8b5abSYoshinobu Inoue 		hints.ai_socktype = SOCK_STREAM;
4444dd8b5abSYoshinobu Inoue 		hints.ai_protocol = 0;
4454dd8b5abSYoshinobu Inoue 		hints.ai_flags = AI_PASSIVE;
44663591ba5SYaroslav Tykhiy 		error = getaddrinfo(bindname, bindport, &hints, &res);
4474dd8b5abSYoshinobu Inoue 		if (error) {
4484dd8b5abSYoshinobu Inoue 			if (family == AF_UNSPEC) {
4494dd8b5abSYoshinobu Inoue 				hints.ai_family = AF_UNSPEC;
45063591ba5SYaroslav Tykhiy 				error = getaddrinfo(bindname, bindport, &hints,
4514dd8b5abSYoshinobu Inoue 						    &res);
4524dd8b5abSYoshinobu Inoue 			}
4534dd8b5abSYoshinobu Inoue 		}
4544dd8b5abSYoshinobu Inoue 		if (error) {
4553fb3b78fSKris Kennaway 			syslog(LOG_ERR, "%s", gai_strerror(error));
4564dd8b5abSYoshinobu Inoue 			if (error == EAI_SYSTEM)
4573fb3b78fSKris Kennaway 				syslog(LOG_ERR, "%s", strerror(errno));
4584dd8b5abSYoshinobu Inoue 			exit(1);
4594dd8b5abSYoshinobu Inoue 		}
4604dd8b5abSYoshinobu Inoue 		if (res->ai_addr == NULL) {
4614dd8b5abSYoshinobu Inoue 			syslog(LOG_ERR, "-a %s: getaddrinfo failed", hostname);
462cf09a206SDavid Greenman 			exit(1);
4632310b8c6SRuslan Ermilov 		} else
4642310b8c6SRuslan Ermilov 			family = res->ai_addr->sa_family;
465cf09a206SDavid Greenman 		/*
466cf09a206SDavid Greenman 		 * Open a socket, bind it to the FTP port, and start
467cf09a206SDavid Greenman 		 * listening.
468cf09a206SDavid Greenman 		 */
4694dd8b5abSYoshinobu Inoue 		ctl_sock = socket(family, SOCK_STREAM, 0);
470cf09a206SDavid Greenman 		if (ctl_sock < 0) {
471cf09a206SDavid Greenman 			syslog(LOG_ERR, "control socket: %m");
472cf09a206SDavid Greenman 			exit(1);
473cf09a206SDavid Greenman 		}
474cf09a206SDavid Greenman 		if (setsockopt(ctl_sock, SOL_SOCKET, SO_REUSEADDR,
47557d4ef07SYaroslav Tykhiy 		    &on, sizeof(on)) < 0)
476406d1ae9SYaroslav Tykhiy 			syslog(LOG_WARNING,
477406d1ae9SYaroslav Tykhiy 			       "control setsockopt (SO_REUSEADDR): %m");
4784dd8b5abSYoshinobu Inoue 		if (family == AF_INET6 && enable_v4 == 0) {
479fc99a00cSHajimu UMEMOTO 			if (setsockopt(ctl_sock, IPPROTO_IPV6, IPV6_V6ONLY,
48057d4ef07SYaroslav Tykhiy 				       &on, sizeof (on)) < 0)
481406d1ae9SYaroslav Tykhiy 				syslog(LOG_WARNING,
482fc99a00cSHajimu UMEMOTO 				       "control setsockopt (IPV6_V6ONLY): %m");
4834dd8b5abSYoshinobu Inoue 		}
4844dd8b5abSYoshinobu Inoue 		memcpy(&server_addr, res->ai_addr, res->ai_addr->sa_len);
4854dd8b5abSYoshinobu Inoue 		if (bind(ctl_sock, (struct sockaddr *)&server_addr,
4864dd8b5abSYoshinobu Inoue 			 server_addr.su_len) < 0) {
487cf09a206SDavid Greenman 			syslog(LOG_ERR, "control bind: %m");
488cf09a206SDavid Greenman 			exit(1);
489cf09a206SDavid Greenman 		}
490cf09a206SDavid Greenman 		if (listen(ctl_sock, 32) < 0) {
491cf09a206SDavid Greenman 			syslog(LOG_ERR, "control listen: %m");
492cf09a206SDavid Greenman 			exit(1);
493cf09a206SDavid Greenman 		}
494cf09a206SDavid Greenman 		/*
495105a3c98SJulian Elischer 		 * Atomically write process ID
496105a3c98SJulian Elischer 		 */
497105a3c98SJulian Elischer 		if (pid_file)
498105a3c98SJulian Elischer 		{
499105a3c98SJulian Elischer 			int fd;
500105a3c98SJulian Elischer 			char buf[20];
501105a3c98SJulian Elischer 
502105a3c98SJulian Elischer 			fd = open(pid_file, O_CREAT | O_WRONLY | O_TRUNC
503105a3c98SJulian Elischer 				| O_NONBLOCK | O_EXLOCK, 0644);
50485966371SWarner Losh 			if (fd < 0) {
505105a3c98SJulian Elischer 				if (errno == EAGAIN)
506105a3c98SJulian Elischer 					errx(1, "%s: file locked", pid_file);
507105a3c98SJulian Elischer 				else
508105a3c98SJulian Elischer 					err(1, "%s", pid_file);
50985966371SWarner Losh 			}
510105a3c98SJulian Elischer 			snprintf(buf, sizeof(buf),
511105a3c98SJulian Elischer 				"%lu\n", (unsigned long) getpid());
512105a3c98SJulian Elischer 			if (write(fd, buf, strlen(buf)) < 0)
513105a3c98SJulian Elischer 				err(1, "%s: write", pid_file);
514105a3c98SJulian Elischer 			/* Leave the pid file open and locked */
515105a3c98SJulian Elischer 		}
516105a3c98SJulian Elischer 		/*
517cf09a206SDavid Greenman 		 * Loop forever accepting connection requests and forking off
518cf09a206SDavid Greenman 		 * children to handle them.
519cf09a206SDavid Greenman 		 */
520cf09a206SDavid Greenman 		while (1) {
5214dd8b5abSYoshinobu Inoue 			addrlen = server_addr.su_len;
522cf09a206SDavid Greenman 			fd = accept(ctl_sock, (struct sockaddr *)&his_addr, &addrlen);
523cf09a206SDavid Greenman 			if (fork() == 0) {
524cf09a206SDavid Greenman 				/* child */
525cf09a206SDavid Greenman 				(void) dup2(fd, 0);
526cf09a206SDavid Greenman 				(void) dup2(fd, 1);
527cf09a206SDavid Greenman 				close(ctl_sock);
528cf09a206SDavid Greenman 				break;
529cf09a206SDavid Greenman 			}
530cf09a206SDavid Greenman 			close(fd);
531cf09a206SDavid Greenman 		}
532cf09a206SDavid Greenman 	} else {
533cf09a206SDavid Greenman 		addrlen = sizeof(his_addr);
534cf09a206SDavid Greenman 		if (getpeername(0, (struct sockaddr *)&his_addr, &addrlen) < 0) {
535cf09a206SDavid Greenman 			syslog(LOG_ERR, "getpeername (%s): %m",argv[0]);
536cf09a206SDavid Greenman 			exit(1);
537cf09a206SDavid Greenman 		}
538cf09a206SDavid Greenman 	}
539cf09a206SDavid Greenman 
5404b82fc95SYaroslav Tykhiy 	sa.sa_handler = SIG_DFL;
5414b82fc95SYaroslav Tykhiy 	(void)sigaction(SIGCHLD, &sa, NULL);
5424b82fc95SYaroslav Tykhiy 
5434b82fc95SYaroslav Tykhiy 	sa.sa_handler = sigurg;
5444b82fc95SYaroslav Tykhiy 	sa.sa_flags = 0;		/* don't restart syscalls for SIGURG */
5454b82fc95SYaroslav Tykhiy 	(void)sigaction(SIGURG, &sa, NULL);
5464b82fc95SYaroslav Tykhiy 
5474b82fc95SYaroslav Tykhiy 	sigfillset(&sa.sa_mask);	/* block all signals in handler */
5484b82fc95SYaroslav Tykhiy 	sa.sa_flags = SA_RESTART;
5494b82fc95SYaroslav Tykhiy 	sa.sa_handler = sigquit;
5504b82fc95SYaroslav Tykhiy 	(void)sigaction(SIGHUP, &sa, NULL);
5514b82fc95SYaroslav Tykhiy 	(void)sigaction(SIGINT, &sa, NULL);
5524b82fc95SYaroslav Tykhiy 	(void)sigaction(SIGQUIT, &sa, NULL);
5534b82fc95SYaroslav Tykhiy 	(void)sigaction(SIGTERM, &sa, NULL);
5544b82fc95SYaroslav Tykhiy 
5554b82fc95SYaroslav Tykhiy 	sa.sa_handler = lostconn;
5564b82fc95SYaroslav Tykhiy 	(void)sigaction(SIGPIPE, &sa, NULL);
557ea022d16SRodney W. Grimes 
558cf09a206SDavid Greenman 	addrlen = sizeof(ctrl_addr);
559cf09a206SDavid Greenman 	if (getsockname(0, (struct sockaddr *)&ctrl_addr, &addrlen) < 0) {
560cf09a206SDavid Greenman 		syslog(LOG_ERR, "getsockname (%s): %m",argv[0]);
561cf09a206SDavid Greenman 		exit(1);
562cf09a206SDavid Greenman 	}
56363591ba5SYaroslav Tykhiy 	dataport = ntohs(ctrl_addr.su_port) - 1; /* as per RFC 959 */
564ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
565ea4e54b9SDavid Nugent 	/* select our identity from virtual host table */
5664dd8b5abSYoshinobu Inoue 	selecthost(&ctrl_addr);
567ea4e54b9SDavid Nugent #endif
568cf09a206SDavid Greenman #ifdef IP_TOS
5694dd8b5abSYoshinobu Inoue 	if (ctrl_addr.su_family == AF_INET)
5704dd8b5abSYoshinobu Inoue       {
571cf09a206SDavid Greenman 	tos = IPTOS_LOWDELAY;
57257d4ef07SYaroslav Tykhiy 	if (setsockopt(0, IPPROTO_IP, IP_TOS, &tos, sizeof(int)) < 0)
573406d1ae9SYaroslav Tykhiy 		syslog(LOG_WARNING, "control setsockopt (IP_TOS): %m");
5744dd8b5abSYoshinobu Inoue       }
575cf09a206SDavid Greenman #endif
576dadb9fb3SDavid Greenman 	/*
577dadb9fb3SDavid Greenman 	 * Disable Nagle on the control channel so that we don't have to wait
578dadb9fb3SDavid Greenman 	 * for peer's ACK before issuing our next reply.
579dadb9fb3SDavid Greenman 	 */
580dadb9fb3SDavid Greenman 	if (setsockopt(0, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) < 0)
581406d1ae9SYaroslav Tykhiy 		syslog(LOG_WARNING, "control setsockopt (TCP_NODELAY): %m");
582dadb9fb3SDavid Greenman 
5834dd8b5abSYoshinobu Inoue 	data_source.su_port = htons(ntohs(ctrl_addr.su_port) - 1);
584cf09a206SDavid Greenman 
585a5a4544eSPaul Traina 	/* set this here so klogin can use it... */
586e760ef2cSWarner Losh 	(void)snprintf(ttyline, sizeof(ttyline), "ftp%d", getpid());
587a5a4544eSPaul Traina 
588ea022d16SRodney W. Grimes 	/* Try to handle urgent data inline */
589ea022d16SRodney W. Grimes #ifdef SO_OOBINLINE
59057d4ef07SYaroslav Tykhiy 	if (setsockopt(0, SOL_SOCKET, SO_OOBINLINE, &on, sizeof(on)) < 0)
591406d1ae9SYaroslav Tykhiy 		syslog(LOG_WARNING, "control setsockopt (SO_OOBINLINE): %m");
592ea022d16SRodney W. Grimes #endif
593ea022d16SRodney W. Grimes 
594ea022d16SRodney W. Grimes #ifdef	F_SETOWN
595ea022d16SRodney W. Grimes 	if (fcntl(fileno(stdin), F_SETOWN, getpid()) == -1)
596ea022d16SRodney W. Grimes 		syslog(LOG_ERR, "fcntl F_SETOWN: %m");
597ea022d16SRodney W. Grimes #endif
5984dd8b5abSYoshinobu Inoue 	dolog((struct sockaddr *)&his_addr);
599ea022d16SRodney W. Grimes 	/*
600ea022d16SRodney W. Grimes 	 * Set up default state
601ea022d16SRodney W. Grimes 	 */
602ea022d16SRodney W. Grimes 	data = -1;
603ea022d16SRodney W. Grimes 	type = TYPE_A;
604ea022d16SRodney W. Grimes 	form = FORM_N;
605ea022d16SRodney W. Grimes 	stru = STRU_F;
606ea022d16SRodney W. Grimes 	mode = MODE_S;
607ea022d16SRodney W. Grimes 	tmpline[0] = '\0';
608ea022d16SRodney W. Grimes 
609ea022d16SRodney W. Grimes 	/* If logins are disabled, print out the message. */
610ea022d16SRodney W. Grimes 	if ((fd = fopen(_PATH_NOLOGIN,"r")) != NULL) {
611ea022d16SRodney W. Grimes 		while (fgets(line, sizeof(line), fd) != NULL) {
612ea022d16SRodney W. Grimes 			if ((cp = strchr(line, '\n')) != NULL)
613ea022d16SRodney W. Grimes 				*cp = '\0';
614ea022d16SRodney W. Grimes 			lreply(530, "%s", line);
615ea022d16SRodney W. Grimes 		}
616ea022d16SRodney W. Grimes 		(void) fflush(stdout);
617ea022d16SRodney W. Grimes 		(void) fclose(fd);
618ea022d16SRodney W. Grimes 		reply(530, "System not available.");
619ea022d16SRodney W. Grimes 		exit(0);
620ea022d16SRodney W. Grimes 	}
621ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
622ea4e54b9SDavid Nugent 	if ((fd = fopen(thishost->welcome, "r")) != NULL) {
623ea4e54b9SDavid Nugent #else
624ea022d16SRodney W. Grimes 	if ((fd = fopen(_PATH_FTPWELCOME, "r")) != NULL) {
625ea4e54b9SDavid Nugent #endif
626ea022d16SRodney W. Grimes 		while (fgets(line, sizeof(line), fd) != NULL) {
627ea022d16SRodney W. Grimes 			if ((cp = strchr(line, '\n')) != NULL)
628ea022d16SRodney W. Grimes 				*cp = '\0';
629ea022d16SRodney W. Grimes 			lreply(220, "%s", line);
630ea022d16SRodney W. Grimes 		}
631ea022d16SRodney W. Grimes 		(void) fflush(stdout);
632ea022d16SRodney W. Grimes 		(void) fclose(fd);
633ea022d16SRodney W. Grimes 		/* reply(220,) must follow */
634ea022d16SRodney W. Grimes 	}
635ea4e54b9SDavid Nugent #ifndef VIRTUAL_HOSTING
6360512556aSDavid Nugent 	if ((hostname = malloc(MAXHOSTNAMELEN)) == NULL)
637618b0bbaSMark Murray 		fatalerror("Ran out of memory.");
6389e9a43bdSBrian Somers 	(void) gethostname(hostname, MAXHOSTNAMELEN - 1);
6399e9a43bdSBrian Somers 	hostname[MAXHOSTNAMELEN - 1] = '\0';
640ea4e54b9SDavid Nugent #endif
641ea022d16SRodney W. Grimes 	reply(220, "%s FTP server (%s) ready.", hostname, version);
642ea022d16SRodney W. Grimes 	for (;;)
643ea022d16SRodney W. Grimes 		(void) yyparse();
644ea022d16SRodney W. Grimes 	/* NOTREACHED */
645ea022d16SRodney W. Grimes }
646ea022d16SRodney W. Grimes 
647ea022d16SRodney W. Grimes static void
648e4bc453cSWarner Losh lostconn(int signo)
649ea022d16SRodney W. Grimes {
650ea022d16SRodney W. Grimes 
651618b0bbaSMark Murray 	if (ftpdebug)
652ea022d16SRodney W. Grimes 		syslog(LOG_DEBUG, "lost connection");
653e02897faSPhilippe Charnier 	dologout(1);
654ea022d16SRodney W. Grimes }
655ea022d16SRodney W. Grimes 
6564b82fc95SYaroslav Tykhiy static void
657e4bc453cSWarner Losh sigquit(int signo)
6584b82fc95SYaroslav Tykhiy {
6594b82fc95SYaroslav Tykhiy 
6604b82fc95SYaroslav Tykhiy 	syslog(LOG_ERR, "got signal %d", signo);
6614b82fc95SYaroslav Tykhiy 	dologout(1);
6624b82fc95SYaroslav Tykhiy }
6634b82fc95SYaroslav Tykhiy 
664ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
665ea4e54b9SDavid Nugent /*
666ea4e54b9SDavid Nugent  * read in virtual host tables (if they exist)
667ea4e54b9SDavid Nugent  */
668ea4e54b9SDavid Nugent 
669ea4e54b9SDavid Nugent static void
670e4bc453cSWarner Losh inithosts(void)
671ea4e54b9SDavid Nugent {
67255b54aa7SYaroslav Tykhiy 	int insert;
673737d08f3SYaroslav Tykhiy 	size_t len;
674ea4e54b9SDavid Nugent 	FILE *fp;
675737d08f3SYaroslav Tykhiy 	char *cp, *mp, *line;
676737d08f3SYaroslav Tykhiy 	char *hostname;
67755b54aa7SYaroslav Tykhiy 	char *vhost, *anonuser, *statfile, *welcome, *loginmsg;
678ea4e54b9SDavid Nugent 	struct ftphost *hrp, *lhrp;
6794dd8b5abSYoshinobu Inoue 	struct addrinfo hints, *res, *ai;
680ea4e54b9SDavid Nugent 
681ea4e54b9SDavid Nugent 	/*
682ea4e54b9SDavid Nugent 	 * Fill in the default host information
683ea4e54b9SDavid Nugent 	 */
684737d08f3SYaroslav Tykhiy 	if ((hostname = malloc(MAXHOSTNAMELEN)) == NULL)
685618b0bbaSMark Murray 		fatalerror("Ran out of memory.");
686737d08f3SYaroslav Tykhiy 	if (gethostname(hostname, MAXHOSTNAMELEN) < 0)
687737d08f3SYaroslav Tykhiy 		hostname[0] = '\0';
688737d08f3SYaroslav Tykhiy 	hostname[MAXHOSTNAMELEN - 1] = '\0';
689737d08f3SYaroslav Tykhiy 	if ((hrp = malloc(sizeof(struct ftphost))) == NULL)
690737d08f3SYaroslav Tykhiy 		fatalerror("Ran out of memory.");
691737d08f3SYaroslav Tykhiy 	hrp->hostname = hostname;
692f38c6cadSYoshinobu Inoue 	hrp->hostinfo = NULL;
6934dd8b5abSYoshinobu Inoue 
6944dd8b5abSYoshinobu Inoue 	memset(&hints, 0, sizeof(hints));
6954dd8b5abSYoshinobu Inoue 	hints.ai_flags = AI_CANONNAME;
6964dd8b5abSYoshinobu Inoue 	hints.ai_family = AF_UNSPEC;
697b1d8d5cdSYaroslav Tykhiy 	if (getaddrinfo(hrp->hostname, NULL, &hints, &res) == 0)
698f38c6cadSYoshinobu Inoue 		hrp->hostinfo = res;
699ea4e54b9SDavid Nugent 	hrp->statfile = _PATH_FTPDSTATFILE;
700ea4e54b9SDavid Nugent 	hrp->welcome  = _PATH_FTPWELCOME;
701ea4e54b9SDavid Nugent 	hrp->loginmsg = _PATH_FTPLOGINMESG;
702ea4e54b9SDavid Nugent 	hrp->anonuser = "ftp";
703ea4e54b9SDavid Nugent 	hrp->next = NULL;
704ea4e54b9SDavid Nugent 	thishost = firsthost = lhrp = hrp;
705ea4e54b9SDavid Nugent 	if ((fp = fopen(_PATH_FTPHOSTS, "r")) != NULL) {
706ec009cf0SYaroslav Tykhiy 		int addrsize, gothost;
7074dd8b5abSYoshinobu Inoue 		void *addr;
7084dd8b5abSYoshinobu Inoue 		struct hostent *hp;
7094dd8b5abSYoshinobu Inoue 
710737d08f3SYaroslav Tykhiy 		while ((line = fgetln(fp, &len)) != NULL) {
7114dd8b5abSYoshinobu Inoue 			int	i, hp_error;
712ea4e54b9SDavid Nugent 
713737d08f3SYaroslav Tykhiy 			/* skip comments */
714737d08f3SYaroslav Tykhiy 			if (line[0] == '#')
715ea4e54b9SDavid Nugent 				continue;
716737d08f3SYaroslav Tykhiy 			if (line[len - 1] == '\n') {
717737d08f3SYaroslav Tykhiy 				line[len - 1] = '\0';
718737d08f3SYaroslav Tykhiy 				mp = NULL;
719737d08f3SYaroslav Tykhiy 			} else {
720737d08f3SYaroslav Tykhiy 				if ((mp = malloc(len + 1)) == NULL)
721737d08f3SYaroslav Tykhiy 					fatalerror("Ran out of memory.");
722737d08f3SYaroslav Tykhiy 				memcpy(mp, line, len);
723737d08f3SYaroslav Tykhiy 				mp[len] = '\0';
724737d08f3SYaroslav Tykhiy 				line = mp;
725ea4e54b9SDavid Nugent 			}
726ea4e54b9SDavid Nugent 			cp = strtok(line, " \t");
727737d08f3SYaroslav Tykhiy 			/* skip empty lines */
728737d08f3SYaroslav Tykhiy 			if (cp == NULL)
729737d08f3SYaroslav Tykhiy 				goto nextline;
73055b54aa7SYaroslav Tykhiy 			vhost = cp;
73155b54aa7SYaroslav Tykhiy 
73255b54aa7SYaroslav Tykhiy 			/* set defaults */
73355b54aa7SYaroslav Tykhiy 			anonuser = "ftp";
73455b54aa7SYaroslav Tykhiy 			statfile = _PATH_FTPDSTATFILE;
73555b54aa7SYaroslav Tykhiy 			welcome  = _PATH_FTPWELCOME;
73655b54aa7SYaroslav Tykhiy 			loginmsg = _PATH_FTPLOGINMESG;
73755b54aa7SYaroslav Tykhiy 
73855b54aa7SYaroslav Tykhiy 			/*
73955b54aa7SYaroslav Tykhiy 			 * Preparse the line so we can use its info
74055b54aa7SYaroslav Tykhiy 			 * for all the addresses associated with
74155b54aa7SYaroslav Tykhiy 			 * the virtual host name.
74255b54aa7SYaroslav Tykhiy 			 * Field 0, the virtual host name, is special:
74355b54aa7SYaroslav Tykhiy 			 * it's already parsed off and will be strdup'ed
74455b54aa7SYaroslav Tykhiy 			 * later, after we know its canonical form.
74555b54aa7SYaroslav Tykhiy 			 */
74655b54aa7SYaroslav Tykhiy 			for (i = 1; i < 5 && (cp = strtok(NULL, " \t")); i++)
74755b54aa7SYaroslav Tykhiy 				if (*cp != '-' && (cp = strdup(cp)))
74855b54aa7SYaroslav Tykhiy 					switch (i) {
74955b54aa7SYaroslav Tykhiy 					case 1:	/* anon user permissions */
75055b54aa7SYaroslav Tykhiy 						anonuser = cp;
75155b54aa7SYaroslav Tykhiy 						break;
75255b54aa7SYaroslav Tykhiy 					case 2: /* statistics file */
75355b54aa7SYaroslav Tykhiy 						statfile = cp;
75455b54aa7SYaroslav Tykhiy 						break;
75555b54aa7SYaroslav Tykhiy 					case 3: /* welcome message */
75655b54aa7SYaroslav Tykhiy 						welcome  = cp;
75755b54aa7SYaroslav Tykhiy 						break;
75855b54aa7SYaroslav Tykhiy 					case 4: /* login message */
75955b54aa7SYaroslav Tykhiy 						loginmsg = cp;
76055b54aa7SYaroslav Tykhiy 						break;
76155b54aa7SYaroslav Tykhiy 					default: /* programming error */
76255b54aa7SYaroslav Tykhiy 						abort();
76355b54aa7SYaroslav Tykhiy 						/* NOTREACHED */
76455b54aa7SYaroslav Tykhiy 					}
7654dd8b5abSYoshinobu Inoue 
7664dd8b5abSYoshinobu Inoue 			hints.ai_flags = 0;
7674dd8b5abSYoshinobu Inoue 			hints.ai_family = AF_UNSPEC;
7684dd8b5abSYoshinobu Inoue 			hints.ai_flags = AI_PASSIVE;
769b1d8d5cdSYaroslav Tykhiy 			if (getaddrinfo(vhost, NULL, &hints, &res) != 0)
770737d08f3SYaroslav Tykhiy 				goto nextline;
7714dd8b5abSYoshinobu Inoue 			for (ai = res; ai != NULL && ai->ai_addr != NULL;
772b535a9bfSDavid Nugent 			     ai = ai->ai_next) {
7734dd8b5abSYoshinobu Inoue 
774b535a9bfSDavid Nugent 			gothost = 0;
775ea4e54b9SDavid Nugent 			for (hrp = firsthost; hrp != NULL; hrp = hrp->next) {
776f38c6cadSYoshinobu Inoue 				struct addrinfo *hi;
777f38c6cadSYoshinobu Inoue 
778f38c6cadSYoshinobu Inoue 				for (hi = hrp->hostinfo; hi != NULL;
779f38c6cadSYoshinobu Inoue 				     hi = hi->ai_next)
780f38c6cadSYoshinobu Inoue 					if (hi->ai_addrlen == ai->ai_addrlen &&
781f38c6cadSYoshinobu Inoue 					    memcmp(hi->ai_addr,
7824dd8b5abSYoshinobu Inoue 						   ai->ai_addr,
783b535a9bfSDavid Nugent 						   ai->ai_addr->sa_len) == 0) {
784b535a9bfSDavid Nugent 						gothost++;
785b535a9bfSDavid Nugent 						break;
786b535a9bfSDavid Nugent 					}
787b535a9bfSDavid Nugent 				if (gothost)
788ea4e54b9SDavid Nugent 					break;
789ea4e54b9SDavid Nugent 			}
790ea4e54b9SDavid Nugent 			if (hrp == NULL) {
791ea4e54b9SDavid Nugent 				if ((hrp = malloc(sizeof(struct ftphost))) == NULL)
792737d08f3SYaroslav Tykhiy 					goto nextline;
793b1d8d5cdSYaroslav Tykhiy 				hrp->hostname = NULL;
79455b54aa7SYaroslav Tykhiy 				insert = 1;
795f2fe752dSYaroslav Tykhiy 			} else {
7961f75c13eSYaroslav Tykhiy 				if (hrp->hostinfo && hrp->hostinfo != res)
797b1d8d5cdSYaroslav Tykhiy 					freeaddrinfo(hrp->hostinfo);
798f2fe752dSYaroslav Tykhiy 				insert = 0; /* host already in the chain */
799f2fe752dSYaroslav Tykhiy 			}
800f38c6cadSYoshinobu Inoue 			hrp->hostinfo = res;
801f38c6cadSYoshinobu Inoue 
802ea4e54b9SDavid Nugent 			/*
803ea4e54b9SDavid Nugent 			 * determine hostname to use.
8044dd8b5abSYoshinobu Inoue 			 * force defined name if there is a valid alias
805ea4e54b9SDavid Nugent 			 * otherwise fallback to primary hostname
806ea4e54b9SDavid Nugent 			 */
8074dd8b5abSYoshinobu Inoue 			/* XXX: getaddrinfo() can't do alias check */
808f38c6cadSYoshinobu Inoue 			switch(hrp->hostinfo->ai_family) {
8094dd8b5abSYoshinobu Inoue 			case AF_INET:
8104b4cc4c6SYaroslav Tykhiy 				addr = &((struct sockaddr_in *)hrp->hostinfo->ai_addr)->sin_addr;
8114b4cc4c6SYaroslav Tykhiy 				addrsize = sizeof(struct in_addr);
8124dd8b5abSYoshinobu Inoue 				break;
8134dd8b5abSYoshinobu Inoue 			case AF_INET6:
8144b4cc4c6SYaroslav Tykhiy 				addr = &((struct sockaddr_in6 *)hrp->hostinfo->ai_addr)->sin6_addr;
8154b4cc4c6SYaroslav Tykhiy 				addrsize = sizeof(struct in6_addr);
8164dd8b5abSYoshinobu Inoue 				break;
8174dd8b5abSYoshinobu Inoue 			default:
8184dd8b5abSYoshinobu Inoue 				/* should not reach here */
819f38c6cadSYoshinobu Inoue 				freeaddrinfo(hrp->hostinfo);
820f2fe752dSYaroslav Tykhiy 				if (insert)
821f2fe752dSYaroslav Tykhiy 					free(hrp); /*not in chain, can free*/
822f2fe752dSYaroslav Tykhiy 				else
823f2fe752dSYaroslav Tykhiy 					hrp->hostinfo = NULL; /*mark as blank*/
824737d08f3SYaroslav Tykhiy 				goto nextline;
8254dd8b5abSYoshinobu Inoue 				/* NOTREACHED */
8264dd8b5abSYoshinobu Inoue 			}
82757d4ef07SYaroslav Tykhiy 			if ((hp = getipnodebyaddr(addr, addrsize,
828f38c6cadSYoshinobu Inoue 						  hrp->hostinfo->ai_family,
8294dd8b5abSYoshinobu Inoue 						  &hp_error)) != NULL) {
83055b54aa7SYaroslav Tykhiy 				if (strcmp(vhost, hp->h_name) != 0) {
831ea4e54b9SDavid Nugent 					if (hp->h_aliases == NULL)
83255b54aa7SYaroslav Tykhiy 						vhost = hp->h_name;
833ea4e54b9SDavid Nugent 					else {
834ea4e54b9SDavid Nugent 						i = 0;
835ea4e54b9SDavid Nugent 						while (hp->h_aliases[i] &&
83655b54aa7SYaroslav Tykhiy 						       strcmp(vhost, hp->h_aliases[i]) != 0)
837ea4e54b9SDavid Nugent 							++i;
838ea4e54b9SDavid Nugent 						if (hp->h_aliases[i] == NULL)
83955b54aa7SYaroslav Tykhiy 							vhost = hp->h_name;
840ea4e54b9SDavid Nugent 					}
841ea4e54b9SDavid Nugent 				}
842ea4e54b9SDavid Nugent 			}
843b1d8d5cdSYaroslav Tykhiy 			if (hrp->hostname &&
844b1d8d5cdSYaroslav Tykhiy 			    strcmp(hrp->hostname, vhost) != 0) {
845b1d8d5cdSYaroslav Tykhiy 				free(hrp->hostname);
846b1d8d5cdSYaroslav Tykhiy 				hrp->hostname = NULL;
847b1d8d5cdSYaroslav Tykhiy 			}
848b1d8d5cdSYaroslav Tykhiy 			if (hrp->hostname == NULL &&
849f2fe752dSYaroslav Tykhiy 			    (hrp->hostname = strdup(vhost)) == NULL) {
850f2fe752dSYaroslav Tykhiy 				freeaddrinfo(hrp->hostinfo);
851f2fe752dSYaroslav Tykhiy 				hrp->hostinfo = NULL; /* mark as blank */
852f2fe752dSYaroslav Tykhiy 				if (hp)
853f2fe752dSYaroslav Tykhiy 					freehostent(hp);
85455b54aa7SYaroslav Tykhiy 				goto nextline;
855f2fe752dSYaroslav Tykhiy 			}
85655b54aa7SYaroslav Tykhiy 			hrp->anonuser = anonuser;
85755b54aa7SYaroslav Tykhiy 			hrp->statfile = statfile;
85855b54aa7SYaroslav Tykhiy 			hrp->welcome  = welcome;
85955b54aa7SYaroslav Tykhiy 			hrp->loginmsg = loginmsg;
86055b54aa7SYaroslav Tykhiy 			if (insert) {
86155b54aa7SYaroslav Tykhiy 				hrp->next  = NULL;
86255b54aa7SYaroslav Tykhiy 				lhrp->next = hrp;
86355b54aa7SYaroslav Tykhiy 				lhrp = hrp;
86455b54aa7SYaroslav Tykhiy 			}
865233c0f66SYaroslav Tykhiy 			if (hp)
8664dd8b5abSYoshinobu Inoue 				freehostent(hp);
8674dd8b5abSYoshinobu Inoue 		      }
868737d08f3SYaroslav Tykhiy nextline:
869737d08f3SYaroslav Tykhiy 			if (mp)
870737d08f3SYaroslav Tykhiy 				free(mp);
871ea4e54b9SDavid Nugent 		}
872ea4e54b9SDavid Nugent 		(void) fclose(fp);
873ea4e54b9SDavid Nugent 	}
874ea4e54b9SDavid Nugent }
875ea4e54b9SDavid Nugent 
876ea4e54b9SDavid Nugent static void
877e4bc453cSWarner Losh selecthost(union sockunion *su)
878ea4e54b9SDavid Nugent {
879ea4e54b9SDavid Nugent 	struct ftphost	*hrp;
8804dd8b5abSYoshinobu Inoue 	u_int16_t port;
8814dd8b5abSYoshinobu Inoue #ifdef INET6
8824dd8b5abSYoshinobu Inoue 	struct in6_addr *mapped_in6 = NULL;
8834dd8b5abSYoshinobu Inoue #endif
884f38c6cadSYoshinobu Inoue 	struct addrinfo *hi;
8854dd8b5abSYoshinobu Inoue 
8864dd8b5abSYoshinobu Inoue #ifdef INET6
8874dd8b5abSYoshinobu Inoue 	/*
8884dd8b5abSYoshinobu Inoue 	 * XXX IPv4 mapped IPv6 addr consideraton,
8894dd8b5abSYoshinobu Inoue 	 * specified in rfc2373.
8904dd8b5abSYoshinobu Inoue 	 */
8914dd8b5abSYoshinobu Inoue 	if (su->su_family == AF_INET6 &&
8924dd8b5abSYoshinobu Inoue 	    IN6_IS_ADDR_V4MAPPED(&su->su_sin6.sin6_addr))
8934dd8b5abSYoshinobu Inoue 		mapped_in6 = &su->su_sin6.sin6_addr;
8944dd8b5abSYoshinobu Inoue #endif
895ea4e54b9SDavid Nugent 
896ea4e54b9SDavid Nugent 	hrp = thishost = firsthost;	/* default */
8974dd8b5abSYoshinobu Inoue 	port = su->su_port;
8984dd8b5abSYoshinobu Inoue 	su->su_port = 0;
899ea4e54b9SDavid Nugent 	while (hrp != NULL) {
900b535a9bfSDavid Nugent 	    for (hi = hrp->hostinfo; hi != NULL; hi = hi->ai_next) {
901f38c6cadSYoshinobu Inoue 		if (memcmp(su, hi->ai_addr, hi->ai_addrlen) == 0) {
902ea4e54b9SDavid Nugent 			thishost = hrp;
903ea4e54b9SDavid Nugent 			break;
904ea4e54b9SDavid Nugent 		}
9054dd8b5abSYoshinobu Inoue #ifdef INET6
9064dd8b5abSYoshinobu Inoue 		/* XXX IPv4 mapped IPv6 addr consideraton */
907f38c6cadSYoshinobu Inoue 		if (hi->ai_addr->sa_family == AF_INET && mapped_in6 != NULL &&
9084dd8b5abSYoshinobu Inoue 		    (memcmp(&mapped_in6->s6_addr[12],
909f38c6cadSYoshinobu Inoue 			    &((struct sockaddr_in *)hi->ai_addr)->sin_addr,
9104dd8b5abSYoshinobu Inoue 			    sizeof(struct in_addr)) == 0)) {
9114dd8b5abSYoshinobu Inoue 			thishost = hrp;
9124dd8b5abSYoshinobu Inoue 			break;
9134dd8b5abSYoshinobu Inoue 		}
9144dd8b5abSYoshinobu Inoue #endif
915f38c6cadSYoshinobu Inoue 	    }
916ea4e54b9SDavid Nugent 	    hrp = hrp->next;
917ea4e54b9SDavid Nugent 	}
9184dd8b5abSYoshinobu Inoue 	su->su_port = port;
919ea4e54b9SDavid Nugent 	/* setup static variables as appropriate */
920ea4e54b9SDavid Nugent 	hostname = thishost->hostname;
921ea4e54b9SDavid Nugent 	ftpuser = thishost->anonuser;
922ea4e54b9SDavid Nugent }
923ea4e54b9SDavid Nugent #endif
924ea4e54b9SDavid Nugent 
925ea022d16SRodney W. Grimes /*
926ea022d16SRodney W. Grimes  * Helper function for sgetpwnam().
927ea022d16SRodney W. Grimes  */
928ea022d16SRodney W. Grimes static char *
929e4bc453cSWarner Losh sgetsave(char *s)
930ea022d16SRodney W. Grimes {
931ea022d16SRodney W. Grimes 	char *new = malloc((unsigned) strlen(s) + 1);
932ea022d16SRodney W. Grimes 
933ea022d16SRodney W. Grimes 	if (new == NULL) {
934ea022d16SRodney W. Grimes 		perror_reply(421, "Local resource failure: malloc");
935ea022d16SRodney W. Grimes 		dologout(1);
936ea022d16SRodney W. Grimes 		/* NOTREACHED */
937ea022d16SRodney W. Grimes 	}
938ea022d16SRodney W. Grimes 	(void) strcpy(new, s);
939ea022d16SRodney W. Grimes 	return (new);
940ea022d16SRodney W. Grimes }
941ea022d16SRodney W. Grimes 
942ea022d16SRodney W. Grimes /*
943ea022d16SRodney W. Grimes  * Save the result of a getpwnam.  Used for USER command, since
944ea022d16SRodney W. Grimes  * the data returned must not be clobbered by any other command
945ea022d16SRodney W. Grimes  * (e.g., globbing).
946ea022d16SRodney W. Grimes  */
947ea022d16SRodney W. Grimes static struct passwd *
948e4bc453cSWarner Losh sgetpwnam(char *name)
949ea022d16SRodney W. Grimes {
950ea022d16SRodney W. Grimes 	static struct passwd save;
951ea022d16SRodney W. Grimes 	struct passwd *p;
952ea022d16SRodney W. Grimes 
953ea022d16SRodney W. Grimes 	if ((p = getpwnam(name)) == NULL)
954ea022d16SRodney W. Grimes 		return (p);
955ea022d16SRodney W. Grimes 	if (save.pw_name) {
956ea022d16SRodney W. Grimes 		free(save.pw_name);
957ea022d16SRodney W. Grimes 		free(save.pw_passwd);
958ea022d16SRodney W. Grimes 		free(save.pw_gecos);
959ea022d16SRodney W. Grimes 		free(save.pw_dir);
960ea022d16SRodney W. Grimes 		free(save.pw_shell);
961ea022d16SRodney W. Grimes 	}
962ea022d16SRodney W. Grimes 	save = *p;
963ea022d16SRodney W. Grimes 	save.pw_name = sgetsave(p->pw_name);
964ea022d16SRodney W. Grimes 	save.pw_passwd = sgetsave(p->pw_passwd);
965ea022d16SRodney W. Grimes 	save.pw_gecos = sgetsave(p->pw_gecos);
966ea022d16SRodney W. Grimes 	save.pw_dir = sgetsave(p->pw_dir);
967ea022d16SRodney W. Grimes 	save.pw_shell = sgetsave(p->pw_shell);
968ea022d16SRodney W. Grimes 	return (&save);
969ea022d16SRodney W. Grimes }
970ea022d16SRodney W. Grimes 
971ea022d16SRodney W. Grimes static int login_attempts;	/* number of failed login attempts */
972ea022d16SRodney W. Grimes static int askpasswd;		/* had user command, ask for passwd */
97390906a46SSheldon Hearn static char curname[MAXLOGNAME];	/* current USER name */
974ea022d16SRodney W. Grimes 
975ea022d16SRodney W. Grimes /*
976ea022d16SRodney W. Grimes  * USER command.
977ea022d16SRodney W. Grimes  * Sets global passwd pointer pw if named account exists and is acceptable;
978ea022d16SRodney W. Grimes  * sets askpasswd if a PASS command is expected.  If logged in previously,
979ea022d16SRodney W. Grimes  * need to reset state.  If name is "ftp" or "anonymous", the name is not in
980ea022d16SRodney W. Grimes  * _PATH_FTPUSERS, and ftp account exists, set guest and pw, then just return.
981ea022d16SRodney W. Grimes  * If account doesn't exist, ask for passwd anyway.  Otherwise, check user
982ea022d16SRodney W. Grimes  * requesting login privileges.  Disallow anyone who does not have a standard
983ea022d16SRodney W. Grimes  * shell as returned by getusershell().  Disallow anyone mentioned in the file
984ea022d16SRodney W. Grimes  * _PATH_FTPUSERS to allow people such as root and uucp to be avoided.
985ea022d16SRodney W. Grimes  */
986ea022d16SRodney W. Grimes void
987e4bc453cSWarner Losh user(char *name)
988ea022d16SRodney W. Grimes {
989ea022d16SRodney W. Grimes 	char *cp, *shell;
990ea022d16SRodney W. Grimes 
991ea022d16SRodney W. Grimes 	if (logged_in) {
992ea022d16SRodney W. Grimes 		if (guest) {
993ea022d16SRodney W. Grimes 			reply(530, "Can't change user from guest login.");
994ea022d16SRodney W. Grimes 			return;
995a5a4544eSPaul Traina 		} else if (dochroot) {
996a5a4544eSPaul Traina 			reply(530, "Can't change user from chroot user.");
997a5a4544eSPaul Traina 			return;
998ea022d16SRodney W. Grimes 		}
999ea022d16SRodney W. Grimes 		end_login();
1000ea022d16SRodney W. Grimes 	}
1001ea022d16SRodney W. Grimes 
1002ea022d16SRodney W. Grimes 	guest = 0;
1003ea022d16SRodney W. Grimes 	if (strcmp(name, "ftp") == 0 || strcmp(name, "anonymous") == 0) {
10048657b576SYaroslav Tykhiy 		if (checkuser(_PATH_FTPUSERS, "ftp", 0, NULL) ||
10058657b576SYaroslav Tykhiy 		    checkuser(_PATH_FTPUSERS, "anonymous", 0, NULL))
1006ea022d16SRodney W. Grimes 			reply(530, "User %s access denied.", name);
1007ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
1008ea4e54b9SDavid Nugent 		else if ((pw = sgetpwnam(thishost->anonuser)) != NULL) {
1009ea4e54b9SDavid Nugent #else
1010ea022d16SRodney W. Grimes 		else if ((pw = sgetpwnam("ftp")) != NULL) {
1011ea4e54b9SDavid Nugent #endif
1012ea022d16SRodney W. Grimes 			guest = 1;
1013ea022d16SRodney W. Grimes 			askpasswd = 1;
1014ea022d16SRodney W. Grimes 			reply(331,
10152c60c54cSPaul Traina 			"Guest login ok, send your email address as password.");
1016ea022d16SRodney W. Grimes 		} else
1017ea022d16SRodney W. Grimes 			reply(530, "User %s unknown.", name);
1018ea022d16SRodney W. Grimes 		if (!askpasswd && logging)
1019ea022d16SRodney W. Grimes 			syslog(LOG_NOTICE,
1020ea022d16SRodney W. Grimes 			    "ANONYMOUS FTP LOGIN REFUSED FROM %s", remotehost);
1021ea022d16SRodney W. Grimes 		return;
1022ea022d16SRodney W. Grimes 	}
10235a392aecSTorsten Blum 	if (anon_only != 0) {
10245a392aecSTorsten Blum 		reply(530, "Sorry, only anonymous ftp allowed.");
10255a392aecSTorsten Blum 		return;
10265a392aecSTorsten Blum 	}
10275a392aecSTorsten Blum 
10289aca17cbSMark Murray 	if ((pw = sgetpwnam(name))) {
1029ea022d16SRodney W. Grimes 		if ((shell = pw->pw_shell) == NULL || *shell == 0)
1030ea022d16SRodney W. Grimes 			shell = _PATH_BSHELL;
1031ea022d16SRodney W. Grimes 		while ((cp = getusershell()) != NULL)
1032ea022d16SRodney W. Grimes 			if (strcmp(cp, shell) == 0)
1033ea022d16SRodney W. Grimes 				break;
1034ea022d16SRodney W. Grimes 		endusershell();
1035ea022d16SRodney W. Grimes 
10368657b576SYaroslav Tykhiy 		if (cp == NULL || checkuser(_PATH_FTPUSERS, name, 1, NULL)) {
1037ea022d16SRodney W. Grimes 			reply(530, "User %s access denied.", name);
1038ea022d16SRodney W. Grimes 			if (logging)
1039ea022d16SRodney W. Grimes 				syslog(LOG_NOTICE,
1040ea022d16SRodney W. Grimes 				    "FTP LOGIN REFUSED FROM %s, %s",
1041ea022d16SRodney W. Grimes 				    remotehost, name);
1042ea022d16SRodney W. Grimes 			pw = (struct passwd *) NULL;
1043ea022d16SRodney W. Grimes 			return;
1044ea022d16SRodney W. Grimes 		}
1045ea022d16SRodney W. Grimes 	}
1046ea022d16SRodney W. Grimes 	if (logging)
1047ea022d16SRodney W. Grimes 		strncpy(curname, name, sizeof(curname)-1);
104847499ecdSAndrey A. Chernov 
104947499ecdSAndrey A. Chernov 	pwok = 0;
1050fa1746c9SMark Murray #ifdef USE_PAM
1051fa1746c9SMark Murray 	/* XXX Kluge! The conversation mechanism needs to be fixed. */
1052726040deSGuido van Rooij #endif
105347499ecdSAndrey A. Chernov 	if (opiechallenge(&opiedata, name, opieprompt) == 0) {
105447499ecdSAndrey A. Chernov 		pwok = (pw != NULL) &&
105547499ecdSAndrey A. Chernov 		       opieaccessfile(remotehost) &&
105647499ecdSAndrey A. Chernov 		       opiealways(pw->pw_dir);
105747499ecdSAndrey A. Chernov 		reply(331, "Response to %s %s for %s.",
105847499ecdSAndrey A. Chernov 		      opieprompt, pwok ? "requested" : "required", name);
105947499ecdSAndrey A. Chernov 	} else {
106047499ecdSAndrey A. Chernov 		pwok = 1;
106147499ecdSAndrey A. Chernov 		reply(331, "Password required for %s.", name);
106247499ecdSAndrey A. Chernov 	}
1063ea022d16SRodney W. Grimes 	askpasswd = 1;
1064ea022d16SRodney W. Grimes 	/*
1065ea022d16SRodney W. Grimes 	 * Delay before reading passwd after first failed
1066ea022d16SRodney W. Grimes 	 * attempt to slow down passwd-guessing programs.
1067ea022d16SRodney W. Grimes 	 */
1068ea022d16SRodney W. Grimes 	if (login_attempts)
1069ea022d16SRodney W. Grimes 		sleep((unsigned) login_attempts);
1070ea022d16SRodney W. Grimes }
1071ea022d16SRodney W. Grimes 
1072ea022d16SRodney W. Grimes /*
10738657b576SYaroslav Tykhiy  * Check if a user is in the file "fname",
10748657b576SYaroslav Tykhiy  * return a pointer to a malloc'd string with the rest
10758657b576SYaroslav Tykhiy  * of the matching line in "residue" if not NULL.
1076ea022d16SRodney W. Grimes  */
1077ea022d16SRodney W. Grimes static int
10788657b576SYaroslav Tykhiy checkuser(char *fname, char *name, int pwset, char **residue)
1079ea022d16SRodney W. Grimes {
1080ea022d16SRodney W. Grimes 	FILE *fd;
1081ea022d16SRodney W. Grimes 	int found = 0;
1082737d08f3SYaroslav Tykhiy 	size_t len;
1083737d08f3SYaroslav Tykhiy 	char *line, *mp, *p;
1084ea022d16SRodney W. Grimes 
1085a5a4544eSPaul Traina 	if ((fd = fopen(fname, "r")) != NULL) {
1086737d08f3SYaroslav Tykhiy 		while (!found && (line = fgetln(fd, &len)) != NULL) {
1087737d08f3SYaroslav Tykhiy 			/* skip comments */
1088ea022d16SRodney W. Grimes 			if (line[0] == '#')
1089ea022d16SRodney W. Grimes 				continue;
1090737d08f3SYaroslav Tykhiy 			if (line[len - 1] == '\n') {
1091737d08f3SYaroslav Tykhiy 				line[len - 1] = '\0';
1092737d08f3SYaroslav Tykhiy 				mp = NULL;
1093737d08f3SYaroslav Tykhiy 			} else {
1094737d08f3SYaroslav Tykhiy 				if ((mp = malloc(len + 1)) == NULL)
1095737d08f3SYaroslav Tykhiy 					fatalerror("Ran out of memory.");
1096737d08f3SYaroslav Tykhiy 				memcpy(mp, line, len);
1097737d08f3SYaroslav Tykhiy 				mp[len] = '\0';
1098737d08f3SYaroslav Tykhiy 				line = mp;
1099737d08f3SYaroslav Tykhiy 			}
1100737d08f3SYaroslav Tykhiy 			/* avoid possible leading and trailing whitespace */
1101737d08f3SYaroslav Tykhiy 			p = strtok(line, " \t");
1102737d08f3SYaroslav Tykhiy 			/* skip empty lines */
1103737d08f3SYaroslav Tykhiy 			if (p == NULL)
1104737d08f3SYaroslav Tykhiy 				goto nextline;
110531fea7b8SDavid Nugent 			/*
110631fea7b8SDavid Nugent 			 * if first chr is '@', check group membership
110731fea7b8SDavid Nugent 			 */
1108737d08f3SYaroslav Tykhiy 			if (p[0] == '@') {
110931fea7b8SDavid Nugent 				int i = 0;
111031fea7b8SDavid Nugent 				struct group *grp;
111131fea7b8SDavid Nugent 
11128657b576SYaroslav Tykhiy 				if (p[1] == '\0') /* single @ matches anyone */
11138657b576SYaroslav Tykhiy 					found = 1;
11148657b576SYaroslav Tykhiy 				else {
1115737d08f3SYaroslav Tykhiy 					if ((grp = getgrnam(p+1)) == NULL)
1116737d08f3SYaroslav Tykhiy 						goto nextline;
11177edcb936SSteve Price 					/*
11187edcb936SSteve Price 					 * Check user's default group
11197edcb936SSteve Price 					 */
11207edcb936SSteve Price 					if (pwset && grp->gr_gid == pw->pw_gid)
11217edcb936SSteve Price 						found = 1;
11227edcb936SSteve Price 					/*
11237edcb936SSteve Price 					 * Check supplementary groups
11247edcb936SSteve Price 					 */
112531fea7b8SDavid Nugent 					while (!found && grp->gr_mem[i])
112631fea7b8SDavid Nugent 						found = strcmp(name,
112731fea7b8SDavid Nugent 							grp->gr_mem[i++])
112831fea7b8SDavid Nugent 							== 0;
1129ea022d16SRodney W. Grimes 				}
11308657b576SYaroslav Tykhiy 			}
113131fea7b8SDavid Nugent 			/*
113231fea7b8SDavid Nugent 			 * Otherwise, just check for username match
113331fea7b8SDavid Nugent 			 */
113431fea7b8SDavid Nugent 			else
1135737d08f3SYaroslav Tykhiy 				found = strcmp(p, name) == 0;
11368657b576SYaroslav Tykhiy 			/*
11378657b576SYaroslav Tykhiy 			 * Save the rest of line to "residue" if matched
11388657b576SYaroslav Tykhiy 			 */
11398657b576SYaroslav Tykhiy 			if (found && residue) {
11400ba71e24SYaroslav Tykhiy 				if ((p = strtok(NULL, "")) != NULL)
11410ba71e24SYaroslav Tykhiy 					p += strspn(p, " \t");
11420ba71e24SYaroslav Tykhiy 				if (p && *p) {
11438657b576SYaroslav Tykhiy 				 	if ((*residue = strdup(p)) == NULL)
11448657b576SYaroslav Tykhiy 						fatalerror("Ran out of memory.");
11458657b576SYaroslav Tykhiy 				} else
11468657b576SYaroslav Tykhiy 					*residue = NULL;
11478657b576SYaroslav Tykhiy 			}
1148737d08f3SYaroslav Tykhiy nextline:
1149737d08f3SYaroslav Tykhiy 			if (mp)
1150737d08f3SYaroslav Tykhiy 				free(mp);
1151ea022d16SRodney W. Grimes 		}
1152ea022d16SRodney W. Grimes 		(void) fclose(fd);
1153ea022d16SRodney W. Grimes 	}
1154ea022d16SRodney W. Grimes 	return (found);
1155ea022d16SRodney W. Grimes }
1156ea022d16SRodney W. Grimes 
1157ea022d16SRodney W. Grimes /*
1158ea022d16SRodney W. Grimes  * Terminate login as previous user, if any, resetting state;
1159ea022d16SRodney W. Grimes  * used when USER command is given or login fails.
1160ea022d16SRodney W. Grimes  */
1161ea022d16SRodney W. Grimes static void
1162e4bc453cSWarner Losh end_login(void)
1163ea022d16SRodney W. Grimes {
11645bc9d93dSMark Murray #ifdef USE_PAM
11655bc9d93dSMark Murray 	int e;
11665bc9d93dSMark Murray #endif
1167ea022d16SRodney W. Grimes 
1168ea022d16SRodney W. Grimes 	(void) seteuid((uid_t)0);
11695d7e0128SYaroslav Tykhiy 	if (logged_in && dowtmp)
117046948173SHajimu UMEMOTO 		ftpd_logwtmp(ttyline, "", NULL);
1171ea022d16SRodney W. Grimes 	pw = NULL;
1172b071c689SDavid Nugent #ifdef	LOGIN_CAP
1173b071c689SDavid Nugent 	setusercontext(NULL, getpwuid(0), (uid_t)0,
1174d9e2c424SRobert Watson 		       LOGIN_SETPRIORITY|LOGIN_SETRESOURCES|LOGIN_SETUMASK|
1175d9e2c424SRobert Watson 		       LOGIN_SETMAC);
1176b071c689SDavid Nugent #endif
11775bc9d93dSMark Murray #ifdef USE_PAM
11785bc9d93dSMark Murray 	if ((e = pam_setcred(pamh, PAM_DELETE_CRED)) != PAM_SUCCESS)
11795bc9d93dSMark Murray 		syslog(LOG_ERR, "pam_setcred: %s", pam_strerror(pamh, e));
11805bc9d93dSMark Murray 	if ((e = pam_close_session(pamh,0)) != PAM_SUCCESS)
11815bc9d93dSMark Murray 		syslog(LOG_ERR, "pam_close_session: %s", pam_strerror(pamh, e));
11825bc9d93dSMark Murray 	if ((e = pam_end(pamh, e)) != PAM_SUCCESS)
11835bc9d93dSMark Murray 		syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e));
11845bc9d93dSMark Murray 	pamh = NULL;
11855bc9d93dSMark Murray #endif
1186ea022d16SRodney W. Grimes 	logged_in = 0;
1187ea022d16SRodney W. Grimes 	guest = 0;
1188a5a4544eSPaul Traina 	dochroot = 0;
1189ea022d16SRodney W. Grimes }
1190ea022d16SRodney W. Grimes 
11915bc9d93dSMark Murray #ifdef USE_PAM
11926c9134c0SMark Murray 
11936c9134c0SMark Murray /*
11946c9134c0SMark Murray  * the following code is stolen from imap-uw PAM authentication module and
11956c9134c0SMark Murray  * login.c
11966c9134c0SMark Murray  */
11976c9134c0SMark Murray #define COPY_STRING(s) (s ? strdup(s) : NULL)
11986c9134c0SMark Murray 
11996c9134c0SMark Murray struct cred_t {
12006c9134c0SMark Murray 	const char *uname;		/* user name */
12016c9134c0SMark Murray 	const char *pass;		/* password */
12026c9134c0SMark Murray };
12036c9134c0SMark Murray typedef struct cred_t cred_t;
12046c9134c0SMark Murray 
12056c9134c0SMark Murray static int
12066c9134c0SMark Murray auth_conv(int num_msg, const struct pam_message **msg,
12076c9134c0SMark Murray 	  struct pam_response **resp, void *appdata)
12086c9134c0SMark Murray {
12096c9134c0SMark Murray 	int i;
12106c9134c0SMark Murray 	cred_t *cred = (cred_t *) appdata;
121160769b19SDag-Erling Smørgrav 	struct pam_response *reply;
121260769b19SDag-Erling Smørgrav 
121360769b19SDag-Erling Smørgrav 	reply = calloc(num_msg, sizeof *reply);
121460769b19SDag-Erling Smørgrav 	if (reply == NULL)
121560769b19SDag-Erling Smørgrav 		return PAM_BUF_ERR;
12166c9134c0SMark Murray 
12176c9134c0SMark Murray 	for (i = 0; i < num_msg; i++) {
12186c9134c0SMark Murray 		switch (msg[i]->msg_style) {
12196c9134c0SMark Murray 		case PAM_PROMPT_ECHO_ON:	/* assume want user name */
12206c9134c0SMark Murray 			reply[i].resp_retcode = PAM_SUCCESS;
12216c9134c0SMark Murray 			reply[i].resp = COPY_STRING(cred->uname);
12226c9134c0SMark Murray 			/* PAM frees resp. */
12236c9134c0SMark Murray 			break;
12246c9134c0SMark Murray 		case PAM_PROMPT_ECHO_OFF:	/* assume want password */
12256c9134c0SMark Murray 			reply[i].resp_retcode = PAM_SUCCESS;
12266c9134c0SMark Murray 			reply[i].resp = COPY_STRING(cred->pass);
12276c9134c0SMark Murray 			/* PAM frees resp. */
12286c9134c0SMark Murray 			break;
12296c9134c0SMark Murray 		case PAM_TEXT_INFO:
12306c9134c0SMark Murray 		case PAM_ERROR_MSG:
12316c9134c0SMark Murray 			reply[i].resp_retcode = PAM_SUCCESS;
12326c9134c0SMark Murray 			reply[i].resp = NULL;
12336c9134c0SMark Murray 			break;
12346c9134c0SMark Murray 		default:			/* unknown message style */
12356c9134c0SMark Murray 			free(reply);
12366c9134c0SMark Murray 			return PAM_CONV_ERR;
12376c9134c0SMark Murray 		}
12386c9134c0SMark Murray 	}
12396c9134c0SMark Murray 
12406c9134c0SMark Murray 	*resp = reply;
12416c9134c0SMark Murray 	return PAM_SUCCESS;
12426c9134c0SMark Murray }
12436c9134c0SMark Murray 
12446c9134c0SMark Murray /*
12456c9134c0SMark Murray  * Attempt to authenticate the user using PAM.  Returns 0 if the user is
12466c9134c0SMark Murray  * authenticated, or 1 if not authenticated.  If some sort of PAM system
12476c9134c0SMark Murray  * error occurs (e.g., the "/etc/pam.conf" file is missing) then this
12486c9134c0SMark Murray  * function returns -1.  This can be used as an indication that we should
12496c9134c0SMark Murray  * fall back to a different authentication mechanism.
12506c9134c0SMark Murray  */
12516c9134c0SMark Murray static int
12526c9134c0SMark Murray auth_pam(struct passwd **ppw, const char *pass)
12536c9134c0SMark Murray {
12546c9134c0SMark Murray 	pam_handle_t *pamh = NULL;
12556c9134c0SMark Murray 	const char *tmpl_user;
12566c9134c0SMark Murray 	const void *item;
12576c9134c0SMark Murray 	int rval;
12586c9134c0SMark Murray 	int e;
12596c9134c0SMark Murray 	cred_t auth_cred = { (*ppw)->pw_name, pass };
12606c9134c0SMark Murray 	struct pam_conv conv = { &auth_conv, &auth_cred };
12616c9134c0SMark Murray 
12626c9134c0SMark Murray 	e = pam_start("ftpd", (*ppw)->pw_name, &conv, &pamh);
12636c9134c0SMark Murray 	if (e != PAM_SUCCESS) {
12646c9134c0SMark Murray 		syslog(LOG_ERR, "pam_start: %s", pam_strerror(pamh, e));
12656c9134c0SMark Murray 		return -1;
12666c9134c0SMark Murray 	}
12676c9134c0SMark Murray 
1268ea413ab7SGuido van Rooij 	e = pam_set_item(pamh, PAM_RHOST, remotehost);
1269ea413ab7SGuido van Rooij 	if (e != PAM_SUCCESS) {
1270ea413ab7SGuido van Rooij 		syslog(LOG_ERR, "pam_set_item(PAM_RHOST): %s",
1271ea413ab7SGuido van Rooij 			pam_strerror(pamh, e));
1272ea413ab7SGuido van Rooij 		return -1;
1273ea413ab7SGuido van Rooij 	}
1274ea413ab7SGuido van Rooij 
12756c9134c0SMark Murray 	e = pam_authenticate(pamh, 0);
12766c9134c0SMark Murray 	switch (e) {
12776c9134c0SMark Murray 	case PAM_SUCCESS:
12786c9134c0SMark Murray 		/*
12796c9134c0SMark Murray 		 * With PAM we support the concept of a "template"
12806c9134c0SMark Murray 		 * user.  The user enters a login name which is
12816c9134c0SMark Murray 		 * authenticated by PAM, usually via a remote service
12826c9134c0SMark Murray 		 * such as RADIUS or TACACS+.  If authentication
12836c9134c0SMark Murray 		 * succeeds, a different but related "template" name
12846c9134c0SMark Murray 		 * is used for setting the credentials, shell, and
12856c9134c0SMark Murray 		 * home directory.  The name the user enters need only
12866c9134c0SMark Murray 		 * exist on the remote authentication server, but the
12876c9134c0SMark Murray 		 * template name must be present in the local password
12886c9134c0SMark Murray 		 * database.
12896c9134c0SMark Murray 		 *
12906c9134c0SMark Murray 		 * This is supported by two various mechanisms in the
12916c9134c0SMark Murray 		 * individual modules.  However, from the application's
12926c9134c0SMark Murray 		 * point of view, the template user is always passed
12936c9134c0SMark Murray 		 * back as a changed value of the PAM_USER item.
12946c9134c0SMark Murray 		 */
12956c9134c0SMark Murray 		if ((e = pam_get_item(pamh, PAM_USER, &item)) ==
12966c9134c0SMark Murray 		    PAM_SUCCESS) {
12976c9134c0SMark Murray 			tmpl_user = (const char *) item;
12986c9134c0SMark Murray 			if (strcmp((*ppw)->pw_name, tmpl_user) != 0)
12996c9134c0SMark Murray 				*ppw = getpwnam(tmpl_user);
13006c9134c0SMark Murray 		} else
13016c9134c0SMark Murray 			syslog(LOG_ERR, "Couldn't get PAM_USER: %s",
13026c9134c0SMark Murray 			    pam_strerror(pamh, e));
13036c9134c0SMark Murray 		rval = 0;
13046c9134c0SMark Murray 		break;
13056c9134c0SMark Murray 
13066c9134c0SMark Murray 	case PAM_AUTH_ERR:
13076c9134c0SMark Murray 	case PAM_USER_UNKNOWN:
13086c9134c0SMark Murray 	case PAM_MAXTRIES:
13096c9134c0SMark Murray 		rval = 1;
13106c9134c0SMark Murray 		break;
13116c9134c0SMark Murray 
13126c9134c0SMark Murray 	default:
13135bc9d93dSMark Murray 		syslog(LOG_ERR, "pam_authenticate: %s", pam_strerror(pamh, e));
13146c9134c0SMark Murray 		rval = -1;
13156c9134c0SMark Murray 		break;
13166c9134c0SMark Murray 	}
13176c9134c0SMark Murray 
13185bc9d93dSMark Murray 	if (rval == 0) {
13195bc9d93dSMark Murray 		e = pam_acct_mgmt(pamh, 0);
13205bc9d93dSMark Murray 		if (e == PAM_NEW_AUTHTOK_REQD) {
13215bc9d93dSMark Murray 			e = pam_chauthtok(pamh, PAM_CHANGE_EXPIRED_AUTHTOK);
13225bc9d93dSMark Murray 			if (e != PAM_SUCCESS) {
13235bc9d93dSMark Murray 				syslog(LOG_ERR, "pam_chauthtok: %s", pam_strerror(pamh, e));
13245bc9d93dSMark Murray 				rval = 1;
13255bc9d93dSMark Murray 			}
13265bc9d93dSMark Murray 		} else if (e != PAM_SUCCESS) {
13275bc9d93dSMark Murray 			rval = 1;
13285bc9d93dSMark Murray 		}
13295bc9d93dSMark Murray 	}
13305bc9d93dSMark Murray 
13315bc9d93dSMark Murray 	if (rval != 0) {
13326c9134c0SMark Murray 		if ((e = pam_end(pamh, e)) != PAM_SUCCESS) {
13336c9134c0SMark Murray 			syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e));
13345bc9d93dSMark Murray 		}
13355bc9d93dSMark Murray 		pamh = NULL;
13366c9134c0SMark Murray 	}
13376c9134c0SMark Murray 	return rval;
13386c9134c0SMark Murray }
13396c9134c0SMark Murray 
13405bc9d93dSMark Murray #endif /* USE_PAM */
13416c9134c0SMark Murray 
1342ea022d16SRodney W. Grimes void
1343e4bc453cSWarner Losh pass(char *passwd)
1344ea022d16SRodney W. Grimes {
1345a5a4544eSPaul Traina 	int rval;
1346ea022d16SRodney W. Grimes 	FILE *fd;
1347b071c689SDavid Nugent #ifdef	LOGIN_CAP
1348b071c689SDavid Nugent 	login_cap_t *lc = NULL;
1349b071c689SDavid Nugent #endif
13505bc9d93dSMark Murray #ifdef USE_PAM
13515bc9d93dSMark Murray 	int e;
13525bc9d93dSMark Murray #endif
1353ce9287fcSYaroslav Tykhiy 	char *chrootdir;
1354341e476eSYaroslav Tykhiy 	char *residue = NULL;
135547499ecdSAndrey A. Chernov 	char *xpasswd;
1356ea022d16SRodney W. Grimes 
1357ea022d16SRodney W. Grimes 	if (logged_in || askpasswd == 0) {
1358ea022d16SRodney W. Grimes 		reply(503, "Login with USER first.");
1359ea022d16SRodney W. Grimes 		return;
1360ea022d16SRodney W. Grimes 	}
1361ea022d16SRodney W. Grimes 	askpasswd = 0;
1362ea022d16SRodney W. Grimes 	if (!guest) {		/* "ftp" is only account allowed no password */
1363a5a4544eSPaul Traina 		if (pw == NULL) {
1364a5a4544eSPaul Traina 			rval = 1;	/* failure below */
1365a5a4544eSPaul Traina 			goto skip;
1366a5a4544eSPaul Traina 		}
13675bc9d93dSMark Murray #ifdef USE_PAM
13686c9134c0SMark Murray 		rval = auth_pam(&pw, passwd);
1369f650a124SAndrey A. Chernov 		if (rval >= 0) {
1370f650a124SAndrey A. Chernov 			opieunlock();
1371a5a4544eSPaul Traina 			goto skip;
1372f650a124SAndrey A. Chernov 		}
1373f650a124SAndrey A. Chernov #endif
137447499ecdSAndrey A. Chernov 		if (opieverify(&opiedata, passwd) == 0)
137547499ecdSAndrey A. Chernov 			xpasswd = pw->pw_passwd;
1376f650a124SAndrey A. Chernov 		else if (pwok) {
137747499ecdSAndrey A. Chernov 			xpasswd = crypt(passwd, pw->pw_passwd);
1378f650a124SAndrey A. Chernov 			if (passwd[0] == '\0' && pw->pw_passwd[0] != '\0')
1379f650a124SAndrey A. Chernov 				xpasswd = ":";
1380f650a124SAndrey A. Chernov 		} else {
138147499ecdSAndrey A. Chernov 			rval = 1;
138247499ecdSAndrey A. Chernov 			goto skip;
138347499ecdSAndrey A. Chernov 		}
138447499ecdSAndrey A. Chernov 		rval = strcmp(pw->pw_passwd, xpasswd);
1385f650a124SAndrey A. Chernov 		if (pw->pw_expire && time(NULL) >= pw->pw_expire)
1386a5a4544eSPaul Traina 			rval = 1;	/* failure */
1387a5a4544eSPaul Traina skip:
1388a5a4544eSPaul Traina 		/*
1389a5a4544eSPaul Traina 		 * If rval == 1, the user failed the authentication check
13906c9134c0SMark Murray 		 * above.  If rval == 0, either PAM or local authentication
1391a5a4544eSPaul Traina 		 * succeeded.
1392a5a4544eSPaul Traina 		 */
1393a5a4544eSPaul Traina 		if (rval) {
1394ea022d16SRodney W. Grimes 			reply(530, "Login incorrect.");
1395ea022d16SRodney W. Grimes 			if (logging)
1396ea022d16SRodney W. Grimes 				syslog(LOG_NOTICE,
1397ea022d16SRodney W. Grimes 				    "FTP LOGIN FAILED FROM %s, %s",
1398ea022d16SRodney W. Grimes 				    remotehost, curname);
1399ea022d16SRodney W. Grimes 			pw = NULL;
1400ea022d16SRodney W. Grimes 			if (login_attempts++ >= 5) {
1401ea022d16SRodney W. Grimes 				syslog(LOG_NOTICE,
1402ea022d16SRodney W. Grimes 				    "repeated login failures from %s",
1403ea022d16SRodney W. Grimes 				    remotehost);
1404ea022d16SRodney W. Grimes 				exit(0);
1405ea022d16SRodney W. Grimes 			}
1406ea022d16SRodney W. Grimes 			return;
1407ea022d16SRodney W. Grimes 		}
1408ea022d16SRodney W. Grimes 	}
1409ea022d16SRodney W. Grimes 	login_attempts = 0;		/* this time successful */
1410ea022d16SRodney W. Grimes 	if (setegid((gid_t)pw->pw_gid) < 0) {
1411ea022d16SRodney W. Grimes 		reply(550, "Can't set gid.");
1412ea022d16SRodney W. Grimes 		return;
1413ea022d16SRodney W. Grimes 	}
1414b071c689SDavid Nugent 	/* May be overridden by login.conf */
1415b071c689SDavid Nugent 	(void) umask(defumask);
1416b071c689SDavid Nugent #ifdef	LOGIN_CAP
14175d0bfe39SDavid Nugent 	if ((lc = login_getpwclass(pw)) != NULL) {
1418b071c689SDavid Nugent 		char	remote_ip[MAXHOSTNAMELEN];
1419b071c689SDavid Nugent 
14204dd8b5abSYoshinobu Inoue 		getnameinfo((struct sockaddr *)&his_addr, his_addr.su_len,
14214dd8b5abSYoshinobu Inoue 			remote_ip, sizeof(remote_ip) - 1, NULL, 0,
1422b0f06defSHajimu UMEMOTO 			NI_NUMERICHOST);
1423b071c689SDavid Nugent 		remote_ip[sizeof(remote_ip) - 1] = 0;
1424b071c689SDavid Nugent 		if (!auth_hostok(lc, remotehost, remote_ip)) {
1425b071c689SDavid Nugent 			syslog(LOG_INFO|LOG_AUTH,
1426b071c689SDavid Nugent 			    "FTP LOGIN FAILED (HOST) as %s: permission denied.",
1427b071c689SDavid Nugent 			    pw->pw_name);
1428b071c689SDavid Nugent 			reply(530, "Permission denied.\n");
1429b071c689SDavid Nugent 			pw = NULL;
1430b071c689SDavid Nugent 			return;
1431b071c689SDavid Nugent 		}
1432b071c689SDavid Nugent 		if (!auth_timeok(lc, time(NULL))) {
1433b071c689SDavid Nugent 			reply(530, "Login not available right now.\n");
1434b071c689SDavid Nugent 			pw = NULL;
1435b071c689SDavid Nugent 			return;
1436b071c689SDavid Nugent 		}
1437b071c689SDavid Nugent 	}
1438b071c689SDavid Nugent 	setusercontext(lc, pw, (uid_t)0,
1439e6fa0d43SDag-Erling Smørgrav 		LOGIN_SETLOGIN|LOGIN_SETGROUP|LOGIN_SETPRIORITY|
1440d9e2c424SRobert Watson 		LOGIN_SETRESOURCES|LOGIN_SETUMASK|LOGIN_SETMAC);
1441b071c689SDavid Nugent #else
1442e6fa0d43SDag-Erling Smørgrav 	setlogin(pw->pw_name);
1443ea022d16SRodney W. Grimes 	(void) initgroups(pw->pw_name, pw->pw_gid);
1444b071c689SDavid Nugent #endif
1445ea022d16SRodney W. Grimes 
14465bc9d93dSMark Murray #ifdef USE_PAM
14475bc9d93dSMark Murray 	if (pamh) {
14485bc9d93dSMark Murray 		if ((e = pam_open_session(pamh, 0)) != PAM_SUCCESS) {
14495bc9d93dSMark Murray 			syslog(LOG_ERR, "pam_open_session: %s", pam_strerror(pamh, e));
14505bc9d93dSMark Murray 		} else if ((e = pam_setcred(pamh, PAM_ESTABLISH_CRED)) != PAM_SUCCESS) {
14515bc9d93dSMark Murray 			syslog(LOG_ERR, "pam_setcred: %s", pam_strerror(pamh, e));
14525bc9d93dSMark Murray 		}
14535bc9d93dSMark Murray 	}
14545bc9d93dSMark Murray #endif
14555bc9d93dSMark Murray 
1456ea022d16SRodney W. Grimes 	/* open wtmp before chroot */
14575d7e0128SYaroslav Tykhiy 	if (dowtmp)
14585d7e0128SYaroslav Tykhiy 		ftpd_logwtmp(ttyline, pw->pw_name,
14595d7e0128SYaroslav Tykhiy 		    (struct sockaddr *)&his_addr);
1460ea022d16SRodney W. Grimes 	logged_in = 1;
1461ea022d16SRodney W. Grimes 
1462a5a4544eSPaul Traina 	if (guest && stats && statfd < 0)
1463ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
1464ea4e54b9SDavid Nugent 		if ((statfd = open(thishost->statfile, O_WRONLY|O_APPEND)) < 0)
1465ea4e54b9SDavid Nugent #else
14663eb568f2SGuido van Rooij 		if ((statfd = open(_PATH_FTPDSTATFILE, O_WRONLY|O_APPEND)) < 0)
1467ea4e54b9SDavid Nugent #endif
14683eb568f2SGuido van Rooij 			stats = 0;
14693eb568f2SGuido van Rooij 
1470b071c689SDavid Nugent 	dochroot =
1471341e476eSYaroslav Tykhiy 		checkuser(_PATH_FTPCHROOT, pw->pw_name, 1, &residue)
1472b071c689SDavid Nugent #ifdef	LOGIN_CAP	/* Allow login.conf configuration as well */
14738657b576SYaroslav Tykhiy 		|| login_getcapbool(lc, "ftp-chroot", 0)
1474b071c689SDavid Nugent #endif
14758657b576SYaroslav Tykhiy 	;
1476ce9287fcSYaroslav Tykhiy 	chrootdir = NULL;
1477ea022d16SRodney W. Grimes 	/*
1478ce9287fcSYaroslav Tykhiy 	 * For a chrooted local user,
1479ce9287fcSYaroslav Tykhiy 	 * a) see whether ftpchroot(5) specifies a chroot directory,
1480ce9287fcSYaroslav Tykhiy 	 * b) extract the directory pathname from the line,
1481ce9287fcSYaroslav Tykhiy 	 * c) expand it to the absolute pathname if necessary.
1482ea022d16SRodney W. Grimes 	 */
1483ce9287fcSYaroslav Tykhiy 	if (dochroot && residue &&
1484341e476eSYaroslav Tykhiy 	    (chrootdir = strtok(residue, " \t")) != NULL &&
1485341e476eSYaroslav Tykhiy 	    chrootdir[0] != '/') {
1486341e476eSYaroslav Tykhiy 		asprintf(&chrootdir, "%s/%s", pw->pw_dir, chrootdir);
1487341e476eSYaroslav Tykhiy 		if (chrootdir == NULL)
14888657b576SYaroslav Tykhiy 			fatalerror("Ran out of memory.");
1489ce9287fcSYaroslav Tykhiy 
14908657b576SYaroslav Tykhiy 	}
1491ce9287fcSYaroslav Tykhiy 	if (guest || dochroot) {
1492ce9287fcSYaroslav Tykhiy 		/*
1493ce9287fcSYaroslav Tykhiy 		 * If no chroot directory set yet, use the login directory.
1494ce9287fcSYaroslav Tykhiy 		 * Copy it so it can be modified while pw->pw_dir stays intact.
1495ce9287fcSYaroslav Tykhiy 		 */
1496ce9287fcSYaroslav Tykhiy 		if (chrootdir == NULL &&
1497ce9287fcSYaroslav Tykhiy 		    (chrootdir = strdup(pw->pw_dir)) == NULL)
1498ce9287fcSYaroslav Tykhiy 			fatalerror("Ran out of memory.");
1499ce9287fcSYaroslav Tykhiy 		/*
1500ce9287fcSYaroslav Tykhiy 		 * Check for the "/chroot/./home" syntax,
1501ce9287fcSYaroslav Tykhiy 		 * separate the chroot and home directory pathnames.
1502ce9287fcSYaroslav Tykhiy 		 */
1503ce9287fcSYaroslav Tykhiy 		if ((homedir = strstr(chrootdir, "/./")) != NULL) {
1504ce9287fcSYaroslav Tykhiy 			*(homedir++) = '\0';	/* wipe '/' */
1505ce9287fcSYaroslav Tykhiy 			homedir++;		/* skip '.' */
1506ce9287fcSYaroslav Tykhiy 			/* so chrootdir can be freed later */
1507ce9287fcSYaroslav Tykhiy 			if ((homedir = strdup(homedir)) == NULL)
1508ce9287fcSYaroslav Tykhiy 				fatalerror("Ran out of memory.");
1509ce9287fcSYaroslav Tykhiy 		} else {
1510ce9287fcSYaroslav Tykhiy 			/*
1511ce9287fcSYaroslav Tykhiy 			 * We MUST do a chdir() after the chroot. Otherwise
1512ce9287fcSYaroslav Tykhiy 			 * the old current directory will be accessible as "."
1513ce9287fcSYaroslav Tykhiy 			 * outside the new root!
1514ce9287fcSYaroslav Tykhiy 			 */
1515ce9287fcSYaroslav Tykhiy 			homedir = "/";
1516ce9287fcSYaroslav Tykhiy 		}
1517ce9287fcSYaroslav Tykhiy 		/*
1518ce9287fcSYaroslav Tykhiy 		 * Finally, do chroot()
1519ce9287fcSYaroslav Tykhiy 		 */
1520ce9287fcSYaroslav Tykhiy 		if (chroot(chrootdir) < 0) {
1521a5a4544eSPaul Traina 			reply(550, "Can't change root.");
1522a5a4544eSPaul Traina 			goto bad;
1523a5a4544eSPaul Traina 		}
1524ce9287fcSYaroslav Tykhiy 	} else	/* real user w/o chroot */
1525ce9287fcSYaroslav Tykhiy 		homedir = pw->pw_dir;
1526ce9287fcSYaroslav Tykhiy 	/*
1527ce9287fcSYaroslav Tykhiy 	 * Set euid *before* doing chdir() so
1528ce9287fcSYaroslav Tykhiy 	 * a) the user won't be carried to a directory that he couldn't reach
1529ce9287fcSYaroslav Tykhiy 	 *    on his own due to no permission to upper path components,
1530ce9287fcSYaroslav Tykhiy 	 * b) NFS mounted homedirs w/restrictive permissions will be accessible
1531ce9287fcSYaroslav Tykhiy 	 *    (uid 0 has no root power over NFS if not mapped explicitly.)
1532ce9287fcSYaroslav Tykhiy 	 */
1533ea022d16SRodney W. Grimes 	if (seteuid((uid_t)pw->pw_uid) < 0) {
1534ea022d16SRodney W. Grimes 		reply(550, "Can't set uid.");
1535ea022d16SRodney W. Grimes 		goto bad;
1536ea022d16SRodney W. Grimes 	}
1537ce9287fcSYaroslav Tykhiy 	if (chdir(homedir) < 0) {
1538ce9287fcSYaroslav Tykhiy 		if (guest || dochroot) {
1539ce9287fcSYaroslav Tykhiy 			reply(550, "Can't change to base directory.");
1540ce9287fcSYaroslav Tykhiy 			goto bad;
1541ce9287fcSYaroslav Tykhiy 		} else {
1542ce9287fcSYaroslav Tykhiy 			if (chdir("/") < 0) {
1543ce9287fcSYaroslav Tykhiy 				reply(550, "Root is inaccessible.");
1544ce9287fcSYaroslav Tykhiy 				goto bad;
1545ce9287fcSYaroslav Tykhiy 			}
1546ce9287fcSYaroslav Tykhiy 			lreply(230, "No directory! Logging in with home=/");
1547ce9287fcSYaroslav Tykhiy 		}
1548ce9287fcSYaroslav Tykhiy 	}
154982c76939SDavid Greenman 
155082c76939SDavid Greenman 	/*
1551ea022d16SRodney W. Grimes 	 * Display a login message, if it exists.
1552ea022d16SRodney W. Grimes 	 * N.B. reply(230,) must follow the message.
1553ea022d16SRodney W. Grimes 	 */
1554ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
1555ea4e54b9SDavid Nugent 	if ((fd = fopen(thishost->loginmsg, "r")) != NULL) {
1556ea4e54b9SDavid Nugent #else
1557ea022d16SRodney W. Grimes 	if ((fd = fopen(_PATH_FTPLOGINMESG, "r")) != NULL) {
1558ea4e54b9SDavid Nugent #endif
1559ea022d16SRodney W. Grimes 		char *cp, line[LINE_MAX];
1560ea022d16SRodney W. Grimes 
1561ea022d16SRodney W. Grimes 		while (fgets(line, sizeof(line), fd) != NULL) {
1562ea022d16SRodney W. Grimes 			if ((cp = strchr(line, '\n')) != NULL)
1563ea022d16SRodney W. Grimes 				*cp = '\0';
1564ea022d16SRodney W. Grimes 			lreply(230, "%s", line);
1565ea022d16SRodney W. Grimes 		}
1566ea022d16SRodney W. Grimes 		(void) fflush(stdout);
1567ea022d16SRodney W. Grimes 		(void) fclose(fd);
1568ea022d16SRodney W. Grimes 	}
1569ea022d16SRodney W. Grimes 	if (guest) {
15703eb568f2SGuido van Rooij 		if (ident != NULL)
15713eb568f2SGuido van Rooij 			free(ident);
157261f891a6SPaul Traina 		ident = strdup(passwd);
157361f891a6SPaul Traina 		if (ident == NULL)
1574618b0bbaSMark Murray 			fatalerror("Ran out of memory.");
157561f891a6SPaul Traina 
1576ea022d16SRodney W. Grimes 		reply(230, "Guest login ok, access restrictions apply.");
1577ea022d16SRodney W. Grimes #ifdef SETPROCTITLE
1578ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
1579ea4e54b9SDavid Nugent 		if (thishost != firsthost)
1580ea4e54b9SDavid Nugent 			snprintf(proctitle, sizeof(proctitle),
1581b3a0a7cdSMike Heffner 				 "%s: anonymous(%s)/%s", remotehost, hostname,
1582b3a0a7cdSMike Heffner 				 passwd);
1583ea4e54b9SDavid Nugent 		else
1584ea4e54b9SDavid Nugent #endif
1585ea022d16SRodney W. Grimes 			snprintf(proctitle, sizeof(proctitle),
1586b3a0a7cdSMike Heffner 				 "%s: anonymous/%s", remotehost, passwd);
1587b63e1fe2SPeter Wemm 		setproctitle("%s", proctitle);
1588ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */
1589ea022d16SRodney W. Grimes 		if (logging)
1590ea022d16SRodney W. Grimes 			syslog(LOG_INFO, "ANONYMOUS FTP LOGIN FROM %s, %s",
1591ea022d16SRodney W. Grimes 			    remotehost, passwd);
1592ea022d16SRodney W. Grimes 	} else {
15933401a71fSDaniel O'Callaghan 		if (dochroot)
159411342ab1SYaroslav Tykhiy 			reply(230, "User %s logged in, "
159511342ab1SYaroslav Tykhiy 				   "access restrictions apply.", pw->pw_name);
15963401a71fSDaniel O'Callaghan 		else
1597ea022d16SRodney W. Grimes 			reply(230, "User %s logged in.", pw->pw_name);
15983401a71fSDaniel O'Callaghan 
1599ea022d16SRodney W. Grimes #ifdef SETPROCTITLE
1600ea022d16SRodney W. Grimes 		snprintf(proctitle, sizeof(proctitle),
16017a29d7daSYaroslav Tykhiy 			 "%s: user/%s", remotehost, pw->pw_name);
1602b63e1fe2SPeter Wemm 		setproctitle("%s", proctitle);
1603ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */
1604ea022d16SRodney W. Grimes 		if (logging)
1605ea022d16SRodney W. Grimes 			syslog(LOG_INFO, "FTP LOGIN FROM %s as %s",
1606ea022d16SRodney W. Grimes 			    remotehost, pw->pw_name);
1607ea022d16SRodney W. Grimes 	}
1608b071c689SDavid Nugent #ifdef	LOGIN_CAP
1609b071c689SDavid Nugent 	login_close(lc);
1610b071c689SDavid Nugent #endif
1611ce9287fcSYaroslav Tykhiy 	if (chrootdir)
1612ce9287fcSYaroslav Tykhiy 		free(chrootdir);
1613ce9287fcSYaroslav Tykhiy 	if (residue)
1614ce9287fcSYaroslav Tykhiy 		free(residue);
1615ea022d16SRodney W. Grimes 	return;
1616ea022d16SRodney W. Grimes bad:
1617ea022d16SRodney W. Grimes 	/* Forget all about it... */
1618b071c689SDavid Nugent #ifdef	LOGIN_CAP
1619b071c689SDavid Nugent 	login_close(lc);
1620b071c689SDavid Nugent #endif
1621ce9287fcSYaroslav Tykhiy 	if (chrootdir)
1622ce9287fcSYaroslav Tykhiy 		free(chrootdir);
1623ce9287fcSYaroslav Tykhiy 	if (residue)
1624ce9287fcSYaroslav Tykhiy 		free(residue);
1625ea022d16SRodney W. Grimes 	end_login();
1626ea022d16SRodney W. Grimes }
1627ea022d16SRodney W. Grimes 
1628ea022d16SRodney W. Grimes void
1629e4bc453cSWarner Losh retrieve(char *cmd, char *name)
1630ea022d16SRodney W. Grimes {
1631ea022d16SRodney W. Grimes 	FILE *fin, *dout;
1632ea022d16SRodney W. Grimes 	struct stat st;
1633e4bc453cSWarner Losh 	int (*closefunc)(FILE *);
1634158a00b2SJohn Birrell 	time_t start;
1635ea022d16SRodney W. Grimes 
1636ea022d16SRodney W. Grimes 	if (cmd == 0) {
1637ea022d16SRodney W. Grimes 		fin = fopen(name, "r"), closefunc = fclose;
1638ea022d16SRodney W. Grimes 		st.st_size = 0;
1639ea022d16SRodney W. Grimes 	} else {
1640ea022d16SRodney W. Grimes 		char line[BUFSIZ];
1641ea022d16SRodney W. Grimes 
1642e760ef2cSWarner Losh 		(void) snprintf(line, sizeof(line), cmd, name), name = line;
1643ea022d16SRodney W. Grimes 		fin = ftpd_popen(line, "r"), closefunc = ftpd_pclose;
1644ea022d16SRodney W. Grimes 		st.st_size = -1;
1645ea022d16SRodney W. Grimes 		st.st_blksize = BUFSIZ;
1646ea022d16SRodney W. Grimes 	}
1647ea022d16SRodney W. Grimes 	if (fin == NULL) {
1648ea022d16SRodney W. Grimes 		if (errno != 0) {
1649ea022d16SRodney W. Grimes 			perror_reply(550, name);
1650ea022d16SRodney W. Grimes 			if (cmd == 0) {
1651ea022d16SRodney W. Grimes 				LOGCMD("get", name);
1652ea022d16SRodney W. Grimes 			}
1653ea022d16SRodney W. Grimes 		}
1654ea022d16SRodney W. Grimes 		return;
1655ea022d16SRodney W. Grimes 	}
1656ea022d16SRodney W. Grimes 	byte_count = -1;
1657ea022d16SRodney W. Grimes 	if (cmd == 0 && (fstat(fileno(fin), &st) < 0 || !S_ISREG(st.st_mode))) {
1658ea022d16SRodney W. Grimes 		reply(550, "%s: not a plain file.", name);
1659ea022d16SRodney W. Grimes 		goto done;
1660ea022d16SRodney W. Grimes 	}
1661ea022d16SRodney W. Grimes 	if (restart_point) {
1662ea022d16SRodney W. Grimes 		if (type == TYPE_A) {
1663ea022d16SRodney W. Grimes 			off_t i, n;
1664ea022d16SRodney W. Grimes 			int c;
1665ea022d16SRodney W. Grimes 
1666ea022d16SRodney W. Grimes 			n = restart_point;
1667ea022d16SRodney W. Grimes 			i = 0;
1668ea022d16SRodney W. Grimes 			while (i++ < n) {
1669ea022d16SRodney W. Grimes 				if ((c=getc(fin)) == EOF) {
1670ea022d16SRodney W. Grimes 					perror_reply(550, name);
1671ea022d16SRodney W. Grimes 					goto done;
1672ea022d16SRodney W. Grimes 				}
1673ea022d16SRodney W. Grimes 				if (c == '\n')
1674ea022d16SRodney W. Grimes 					i++;
1675ea022d16SRodney W. Grimes 			}
1676ea022d16SRodney W. Grimes 		} else if (lseek(fileno(fin), restart_point, L_SET) < 0) {
1677ea022d16SRodney W. Grimes 			perror_reply(550, name);
1678ea022d16SRodney W. Grimes 			goto done;
1679ea022d16SRodney W. Grimes 		}
1680ea022d16SRodney W. Grimes 	}
1681ea022d16SRodney W. Grimes 	dout = dataconn(name, st.st_size, "w");
1682ea022d16SRodney W. Grimes 	if (dout == NULL)
1683ea022d16SRodney W. Grimes 		goto done;
16843eb568f2SGuido van Rooij 	time(&start);
16859fc5823aSGarrett Wollman 	send_data(fin, dout, st.st_blksize, st.st_size,
16869fc5823aSGarrett Wollman 		  restart_point == 0 && cmd == 0 && S_ISREG(st.st_mode));
16873eb568f2SGuido van Rooij 	if (cmd == 0 && guest && stats)
16883eb568f2SGuido van Rooij 		logxfer(name, st.st_size, start);
1689ea022d16SRodney W. Grimes 	(void) fclose(dout);
1690ea022d16SRodney W. Grimes 	data = -1;
1691ea022d16SRodney W. Grimes 	pdata = -1;
1692ea022d16SRodney W. Grimes done:
1693ea022d16SRodney W. Grimes 	if (cmd == 0)
1694ea022d16SRodney W. Grimes 		LOGBYTES("get", name, byte_count);
1695ea022d16SRodney W. Grimes 	(*closefunc)(fin);
1696ea022d16SRodney W. Grimes }
1697ea022d16SRodney W. Grimes 
1698ea022d16SRodney W. Grimes void
1699e4bc453cSWarner Losh store(char *name, char *mode, int unique)
1700ea022d16SRodney W. Grimes {
1701a117c345SYaroslav Tykhiy 	int fd;
1702ea022d16SRodney W. Grimes 	FILE *fout, *din;
1703e4bc453cSWarner Losh 	int (*closefunc)(FILE *);
1704ea022d16SRodney W. Grimes 
1705a117c345SYaroslav Tykhiy 	if (*mode == 'a') {		/* APPE */
1706a117c345SYaroslav Tykhiy 		if (unique) {
1707a117c345SYaroslav Tykhiy 			/* Programming error */
1708a117c345SYaroslav Tykhiy 			syslog(LOG_ERR, "Internal: unique flag to APPE");
1709a117c345SYaroslav Tykhiy 			unique = 0;
1710a117c345SYaroslav Tykhiy 		}
1711a117c345SYaroslav Tykhiy 		if (guest && noguestmod) {
1712a117c345SYaroslav Tykhiy 			reply(550, "Appending to existing file denied");
1713a117c345SYaroslav Tykhiy 			goto err;
1714a117c345SYaroslav Tykhiy 		}
1715a117c345SYaroslav Tykhiy 		restart_point = 0;	/* not affected by preceding REST */
1716a117c345SYaroslav Tykhiy 	}
1717a117c345SYaroslav Tykhiy 	if (unique)			/* STOU overrides REST */
1718a117c345SYaroslav Tykhiy 		restart_point = 0;
1719a117c345SYaroslav Tykhiy 	if (guest && noguestmod) {
1720a117c345SYaroslav Tykhiy 		if (restart_point) {	/* guest STOR w/REST */
1721a117c345SYaroslav Tykhiy 			reply(550, "Modifying existing file denied");
1722a117c345SYaroslav Tykhiy 			goto err;
1723a117c345SYaroslav Tykhiy 		} else			/* treat guest STOR as STOU */
1724a117c345SYaroslav Tykhiy 			unique = 1;
1725ea022d16SRodney W. Grimes 	}
1726ea022d16SRodney W. Grimes 
1727ea022d16SRodney W. Grimes 	if (restart_point)
1728a117c345SYaroslav Tykhiy 		mode = "r+";	/* so ASCII manual seek can work */
1729a117c345SYaroslav Tykhiy 	if (unique) {
1730a117c345SYaroslav Tykhiy 		if ((fd = guniquefd(name, &name)) < 0)
1731a117c345SYaroslav Tykhiy 			goto err;
1732a117c345SYaroslav Tykhiy 		fout = fdopen(fd, mode);
1733a117c345SYaroslav Tykhiy 	} else
1734ea022d16SRodney W. Grimes 		fout = fopen(name, mode);
1735ea022d16SRodney W. Grimes 	closefunc = fclose;
1736ea022d16SRodney W. Grimes 	if (fout == NULL) {
1737ea022d16SRodney W. Grimes 		perror_reply(553, name);
1738a117c345SYaroslav Tykhiy 		goto err;
1739ea022d16SRodney W. Grimes 	}
1740ea022d16SRodney W. Grimes 	byte_count = -1;
1741ea022d16SRodney W. Grimes 	if (restart_point) {
1742ea022d16SRodney W. Grimes 		if (type == TYPE_A) {
1743ea022d16SRodney W. Grimes 			off_t i, n;
1744ea022d16SRodney W. Grimes 			int c;
1745ea022d16SRodney W. Grimes 
1746ea022d16SRodney W. Grimes 			n = restart_point;
1747ea022d16SRodney W. Grimes 			i = 0;
1748ea022d16SRodney W. Grimes 			while (i++ < n) {
1749ea022d16SRodney W. Grimes 				if ((c=getc(fout)) == EOF) {
1750ea022d16SRodney W. Grimes 					perror_reply(550, name);
1751ea022d16SRodney W. Grimes 					goto done;
1752ea022d16SRodney W. Grimes 				}
1753ea022d16SRodney W. Grimes 				if (c == '\n')
1754ea022d16SRodney W. Grimes 					i++;
1755ea022d16SRodney W. Grimes 			}
1756ea022d16SRodney W. Grimes 			/*
1757ea022d16SRodney W. Grimes 			 * We must do this seek to "current" position
1758ea022d16SRodney W. Grimes 			 * because we are changing from reading to
1759ea022d16SRodney W. Grimes 			 * writing.
1760ea022d16SRodney W. Grimes 			 */
1761e4a71114SAndrey A. Chernov 			if (fseeko(fout, (off_t)0, SEEK_CUR) < 0) {
1762ea022d16SRodney W. Grimes 				perror_reply(550, name);
1763ea022d16SRodney W. Grimes 				goto done;
1764ea022d16SRodney W. Grimes 			}
1765ea022d16SRodney W. Grimes 		} else if (lseek(fileno(fout), restart_point, L_SET) < 0) {
1766ea022d16SRodney W. Grimes 			perror_reply(550, name);
1767ea022d16SRodney W. Grimes 			goto done;
1768ea022d16SRodney W. Grimes 		}
1769ea022d16SRodney W. Grimes 	}
1770ea022d16SRodney W. Grimes 	din = dataconn(name, (off_t)-1, "r");
1771ea022d16SRodney W. Grimes 	if (din == NULL)
1772ea022d16SRodney W. Grimes 		goto done;
1773ea022d16SRodney W. Grimes 	if (receive_data(din, fout) == 0) {
1774ea022d16SRodney W. Grimes 		if (unique)
1775ea022d16SRodney W. Grimes 			reply(226, "Transfer complete (unique file name:%s).",
1776ea022d16SRodney W. Grimes 			    name);
1777ea022d16SRodney W. Grimes 		else
1778ea022d16SRodney W. Grimes 			reply(226, "Transfer complete.");
1779ea022d16SRodney W. Grimes 	}
1780ea022d16SRodney W. Grimes 	(void) fclose(din);
1781ea022d16SRodney W. Grimes 	data = -1;
1782ea022d16SRodney W. Grimes 	pdata = -1;
1783ea022d16SRodney W. Grimes done:
17847c20f337SYaroslav Tykhiy 	LOGBYTES(*mode == 'a' ? "append" : "put", name, byte_count);
1785ea022d16SRodney W. Grimes 	(*closefunc)(fout);
1786a117c345SYaroslav Tykhiy 	return;
1787a117c345SYaroslav Tykhiy err:
1788a117c345SYaroslav Tykhiy 	LOGCMD(*mode == 'a' ? "append" : "put" , name);
1789a117c345SYaroslav Tykhiy 	return;
1790ea022d16SRodney W. Grimes }
1791ea022d16SRodney W. Grimes 
1792ea022d16SRodney W. Grimes static FILE *
1793e4bc453cSWarner Losh getdatasock(char *mode)
1794ea022d16SRodney W. Grimes {
1795ea022d16SRodney W. Grimes 	int on = 1, s, t, tries;
1796ea022d16SRodney W. Grimes 
1797ea022d16SRodney W. Grimes 	if (data >= 0)
1798ea022d16SRodney W. Grimes 		return (fdopen(data, mode));
1799ea022d16SRodney W. Grimes 	(void) seteuid((uid_t)0);
18004dd8b5abSYoshinobu Inoue 
18014dd8b5abSYoshinobu Inoue 	s = socket(data_dest.su_family, SOCK_STREAM, 0);
1802ea022d16SRodney W. Grimes 	if (s < 0)
1803ea022d16SRodney W. Grimes 		goto bad;
180457d4ef07SYaroslav Tykhiy 	if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
1805406d1ae9SYaroslav Tykhiy 		syslog(LOG_WARNING, "data setsockopt (SO_REUSEADDR): %m");
1806ea022d16SRodney W. Grimes 	/* anchor socket to avoid multi-homing problems */
18074dd8b5abSYoshinobu Inoue 	data_source = ctrl_addr;
180863591ba5SYaroslav Tykhiy 	data_source.su_port = htons(dataport);
1809ea022d16SRodney W. Grimes 	for (tries = 1; ; tries++) {
1810ea022d16SRodney W. Grimes 		if (bind(s, (struct sockaddr *)&data_source,
18114dd8b5abSYoshinobu Inoue 		    data_source.su_len) >= 0)
1812ea022d16SRodney W. Grimes 			break;
1813ea022d16SRodney W. Grimes 		if (errno != EADDRINUSE || tries > 10)
1814ea022d16SRodney W. Grimes 			goto bad;
1815ea022d16SRodney W. Grimes 		sleep(tries);
1816ea022d16SRodney W. Grimes 	}
1817ea022d16SRodney W. Grimes 	(void) seteuid((uid_t)pw->pw_uid);
1818ea022d16SRodney W. Grimes #ifdef IP_TOS
18194dd8b5abSYoshinobu Inoue 	if (data_source.su_family == AF_INET)
18204dd8b5abSYoshinobu Inoue       {
1821ea022d16SRodney W. Grimes 	on = IPTOS_THROUGHPUT;
182257d4ef07SYaroslav Tykhiy 	if (setsockopt(s, IPPROTO_IP, IP_TOS, &on, sizeof(int)) < 0)
1823406d1ae9SYaroslav Tykhiy 		syslog(LOG_WARNING, "data setsockopt (IP_TOS): %m");
18244dd8b5abSYoshinobu Inoue       }
1825ea022d16SRodney W. Grimes #endif
18269fc5823aSGarrett Wollman #ifdef TCP_NOPUSH
18279fc5823aSGarrett Wollman 	/*
18289fc5823aSGarrett Wollman 	 * Turn off push flag to keep sender TCP from sending short packets
18299fc5823aSGarrett Wollman 	 * at the boundaries of each write().  Should probably do a SO_SNDBUF
18309fc5823aSGarrett Wollman 	 * to set the send buffer size as well, but that may not be desirable
18319fc5823aSGarrett Wollman 	 * in heavy-load situations.
18329fc5823aSGarrett Wollman 	 */
18339fc5823aSGarrett Wollman 	on = 1;
183457d4ef07SYaroslav Tykhiy 	if (setsockopt(s, IPPROTO_TCP, TCP_NOPUSH, &on, sizeof on) < 0)
1835406d1ae9SYaroslav Tykhiy 		syslog(LOG_WARNING, "data setsockopt (TCP_NOPUSH): %m");
18369fc5823aSGarrett Wollman #endif
18379fc5823aSGarrett Wollman #ifdef SO_SNDBUF
18389fc5823aSGarrett Wollman 	on = 65536;
183957d4ef07SYaroslav Tykhiy 	if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &on, sizeof on) < 0)
1840406d1ae9SYaroslav Tykhiy 		syslog(LOG_WARNING, "data setsockopt (SO_SNDBUF): %m");
18419fc5823aSGarrett Wollman #endif
18429fc5823aSGarrett Wollman 
1843ea022d16SRodney W. Grimes 	return (fdopen(s, mode));
1844ea022d16SRodney W. Grimes bad:
1845ea022d16SRodney W. Grimes 	/* Return the real value of errno (close may change it) */
1846ea022d16SRodney W. Grimes 	t = errno;
1847ea022d16SRodney W. Grimes 	(void) seteuid((uid_t)pw->pw_uid);
1848ea022d16SRodney W. Grimes 	(void) close(s);
1849ea022d16SRodney W. Grimes 	errno = t;
1850ea022d16SRodney W. Grimes 	return (NULL);
1851ea022d16SRodney W. Grimes }
1852ea022d16SRodney W. Grimes 
1853ea022d16SRodney W. Grimes static FILE *
1854e4bc453cSWarner Losh dataconn(char *name, off_t size, char *mode)
1855ea022d16SRodney W. Grimes {
1856ea022d16SRodney W. Grimes 	char sizebuf[32];
1857ea022d16SRodney W. Grimes 	FILE *file;
1858e5094456SCrist J. Clark 	int retry = 0, tos, conerrno;
1859ea022d16SRodney W. Grimes 
1860ea022d16SRodney W. Grimes 	file_size = size;
1861ea022d16SRodney W. Grimes 	byte_count = 0;
1862ea022d16SRodney W. Grimes 	if (size != (off_t) -1)
1863e760ef2cSWarner Losh 		(void) snprintf(sizebuf, sizeof(sizebuf), " (%qd bytes)", size);
1864ea022d16SRodney W. Grimes 	else
1865e760ef2cSWarner Losh 		*sizebuf = '\0';
1866ea022d16SRodney W. Grimes 	if (pdata >= 0) {
18674dd8b5abSYoshinobu Inoue 		union sockunion from;
18684cd48bacSYaroslav Tykhiy 		int flags;
18694dd8b5abSYoshinobu Inoue 		int s, fromlen = ctrl_addr.su_len;
1870d6ed3c37SGuido van Rooij 		struct timeval timeout;
1871d6ed3c37SGuido van Rooij 		fd_set set;
1872ea022d16SRodney W. Grimes 
1873d6ed3c37SGuido van Rooij 		FD_ZERO(&set);
1874d6ed3c37SGuido van Rooij 		FD_SET(pdata, &set);
1875d6ed3c37SGuido van Rooij 
1876d6ed3c37SGuido van Rooij 		timeout.tv_usec = 0;
1877d6ed3c37SGuido van Rooij 		timeout.tv_sec = 120;
1878d6ed3c37SGuido van Rooij 
18794cd48bacSYaroslav Tykhiy 		/*
18804cd48bacSYaroslav Tykhiy 		 * Granted a socket is in the blocking I/O mode,
18814cd48bacSYaroslav Tykhiy 		 * accept() will block after a successful select()
18824cd48bacSYaroslav Tykhiy 		 * if the selected connection dies in between.
18834cd48bacSYaroslav Tykhiy 		 * Therefore set the non-blocking I/O flag here.
18844cd48bacSYaroslav Tykhiy 		 */
18854cd48bacSYaroslav Tykhiy 		if ((flags = fcntl(pdata, F_GETFL, 0)) == -1 ||
18864cd48bacSYaroslav Tykhiy 		    fcntl(pdata, F_SETFL, flags | O_NONBLOCK) == -1)
18874cd48bacSYaroslav Tykhiy 			goto pdata_err;
18884cd48bacSYaroslav Tykhiy 		if (select(pdata+1, &set, (fd_set *) 0, (fd_set *) 0, &timeout) <= 0 ||
18894cd48bacSYaroslav Tykhiy 		    (s = accept(pdata, (struct sockaddr *) &from, &fromlen)) < 0)
18904cd48bacSYaroslav Tykhiy 			goto pdata_err;
1891ea022d16SRodney W. Grimes 		(void) close(pdata);
1892ea022d16SRodney W. Grimes 		pdata = s;
18934cd48bacSYaroslav Tykhiy 		/*
1894f6daca0dSYaroslav Tykhiy 		 * Unset the inherited non-blocking I/O flag
1895f6daca0dSYaroslav Tykhiy 		 * on the child socket so stdio can work on it.
18964cd48bacSYaroslav Tykhiy 		 */
18974cd48bacSYaroslav Tykhiy 		if ((flags = fcntl(pdata, F_GETFL, 0)) == -1 ||
18984cd48bacSYaroslav Tykhiy 		    fcntl(pdata, F_SETFL, flags & ~O_NONBLOCK) == -1)
18994cd48bacSYaroslav Tykhiy 			goto pdata_err;
1900ea022d16SRodney W. Grimes #ifdef IP_TOS
19014dd8b5abSYoshinobu Inoue 		if (from.su_family == AF_INET)
19024dd8b5abSYoshinobu Inoue 	      {
1903a5a4544eSPaul Traina 		tos = IPTOS_THROUGHPUT;
190457d4ef07SYaroslav Tykhiy 		if (setsockopt(s, IPPROTO_IP, IP_TOS, &tos, sizeof(int)) < 0)
1905406d1ae9SYaroslav Tykhiy 			syslog(LOG_WARNING, "pdata setsockopt (IP_TOS): %m");
19064dd8b5abSYoshinobu Inoue 	      }
1907ea022d16SRodney W. Grimes #endif
1908ea022d16SRodney W. Grimes 		reply(150, "Opening %s mode data connection for '%s'%s.",
1909ea022d16SRodney W. Grimes 		     type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf);
1910ea022d16SRodney W. Grimes 		return (fdopen(pdata, mode));
19114cd48bacSYaroslav Tykhiy pdata_err:
19124cd48bacSYaroslav Tykhiy 		reply(425, "Can't open data connection.");
19134cd48bacSYaroslav Tykhiy 		(void) close(pdata);
19144cd48bacSYaroslav Tykhiy 		pdata = -1;
19154cd48bacSYaroslav Tykhiy 		return (NULL);
1916ea022d16SRodney W. Grimes 	}
1917ea022d16SRodney W. Grimes 	if (data >= 0) {
1918ea022d16SRodney W. Grimes 		reply(125, "Using existing data connection for '%s'%s.",
1919ea022d16SRodney W. Grimes 		    name, sizebuf);
1920ea022d16SRodney W. Grimes 		usedefault = 1;
1921ea022d16SRodney W. Grimes 		return (fdopen(data, mode));
1922ea022d16SRodney W. Grimes 	}
1923ea022d16SRodney W. Grimes 	if (usedefault)
1924ea022d16SRodney W. Grimes 		data_dest = his_addr;
1925ea022d16SRodney W. Grimes 	usedefault = 1;
1926e5094456SCrist J. Clark 	do {
1927ea022d16SRodney W. Grimes 		file = getdatasock(mode);
1928ea022d16SRodney W. Grimes 		if (file == NULL) {
19294dd8b5abSYoshinobu Inoue 			char hostbuf[BUFSIZ], portbuf[BUFSIZ];
19304dd8b5abSYoshinobu Inoue 			getnameinfo((struct sockaddr *)&data_source,
19314dd8b5abSYoshinobu Inoue 				data_source.su_len, hostbuf, sizeof(hostbuf) - 1,
19324dd8b5abSYoshinobu Inoue 				portbuf, sizeof(portbuf),
1933b0f06defSHajimu UMEMOTO 				NI_NUMERICHOST|NI_NUMERICSERV);
19344dd8b5abSYoshinobu Inoue 			reply(425, "Can't create data socket (%s,%s): %s.",
19354dd8b5abSYoshinobu Inoue 				hostbuf, portbuf, strerror(errno));
1936ea022d16SRodney W. Grimes 			return (NULL);
1937ea022d16SRodney W. Grimes 		}
1938ea022d16SRodney W. Grimes 		data = fileno(file);
1939e5094456SCrist J. Clark 		conerrno = 0;
1940e5094456SCrist J. Clark 		if (connect(data, (struct sockaddr *)&data_dest,
1941e5094456SCrist J. Clark 		    data_dest.su_len) == 0)
1942e5094456SCrist J. Clark 			break;
1943e5094456SCrist J. Clark 		conerrno = errno;
1944ea022d16SRodney W. Grimes 		(void) fclose(file);
1945ea022d16SRodney W. Grimes 		data = -1;
1946e5094456SCrist J. Clark 		if (conerrno == EADDRINUSE) {
1947e5094456SCrist J. Clark 			sleep((unsigned) swaitint);
1948e5094456SCrist J. Clark 			retry += swaitint;
1949e5094456SCrist J. Clark 		} else {
1950e5094456SCrist J. Clark 			break;
1951e5094456SCrist J. Clark 		}
1952e5094456SCrist J. Clark 	} while (retry <= swaitmax);
1953e5094456SCrist J. Clark 	if (conerrno != 0) {
1954e5094456SCrist J. Clark 		perror_reply(425, "Can't build data connection");
1955ea022d16SRodney W. Grimes 		return (NULL);
1956ea022d16SRodney W. Grimes 	}
1957ea022d16SRodney W. Grimes 	reply(150, "Opening %s mode data connection for '%s'%s.",
1958ea022d16SRodney W. Grimes 	     type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf);
1959ea022d16SRodney W. Grimes 	return (file);
1960ea022d16SRodney W. Grimes }
1961ea022d16SRodney W. Grimes 
1962ea022d16SRodney W. Grimes /*
1963ea022d16SRodney W. Grimes  * Tranfer the contents of "instr" to "outstr" peer using the appropriate
19649fc5823aSGarrett Wollman  * encapsulation of the data subject to Mode, Structure, and Type.
1965ea022d16SRodney W. Grimes  *
1966ea022d16SRodney W. Grimes  * NB: Form isn't handled.
1967ea022d16SRodney W. Grimes  */
19684b82fc95SYaroslav Tykhiy static int
1969e4bc453cSWarner Losh send_data(FILE *instr, FILE *outstr, off_t blksize, off_t filesize, int isreg)
1970ea022d16SRodney W. Grimes {
1971f6f0c4b9SDan Moschuk 	int c, filefd, netfd;
1972f6f0c4b9SDan Moschuk 	char *buf;
1973f6f0c4b9SDan Moschuk 	off_t cnt;
1974ea022d16SRodney W. Grimes 
1975ea022d16SRodney W. Grimes 	transflag++;
1976ea022d16SRodney W. Grimes 	switch (type) {
1977ea022d16SRodney W. Grimes 
1978ea022d16SRodney W. Grimes 	case TYPE_A:
1979ea022d16SRodney W. Grimes 		while ((c = getc(instr)) != EOF) {
19804b82fc95SYaroslav Tykhiy 			if (recvurg)
19814b82fc95SYaroslav Tykhiy 				goto got_oob;
1982ea022d16SRodney W. Grimes 			byte_count++;
1983ea022d16SRodney W. Grimes 			if (c == '\n') {
1984ea022d16SRodney W. Grimes 				if (ferror(outstr))
1985ea022d16SRodney W. Grimes 					goto data_err;
1986ea022d16SRodney W. Grimes 				(void) putc('\r', outstr);
1987ea022d16SRodney W. Grimes 			}
1988ea022d16SRodney W. Grimes 			(void) putc(c, outstr);
1989ea022d16SRodney W. Grimes 		}
19904b82fc95SYaroslav Tykhiy 		if (recvurg)
19914b82fc95SYaroslav Tykhiy 			goto got_oob;
1992ea022d16SRodney W. Grimes 		fflush(outstr);
1993ea022d16SRodney W. Grimes 		transflag = 0;
1994ea022d16SRodney W. Grimes 		if (ferror(instr))
1995ea022d16SRodney W. Grimes 			goto file_err;
1996ea022d16SRodney W. Grimes 		if (ferror(outstr))
1997ea022d16SRodney W. Grimes 			goto data_err;
1998ea022d16SRodney W. Grimes 		reply(226, "Transfer complete.");
19994b82fc95SYaroslav Tykhiy 		return (0);
2000ea022d16SRodney W. Grimes 
2001ea022d16SRodney W. Grimes 	case TYPE_I:
2002ea022d16SRodney W. Grimes 	case TYPE_L:
20039fc5823aSGarrett Wollman 		/*
20049fc5823aSGarrett Wollman 		 * isreg is only set if we are not doing restart and we
20059fc5823aSGarrett Wollman 		 * are sending a regular file
20069fc5823aSGarrett Wollman 		 */
20079fc5823aSGarrett Wollman 		netfd = fileno(outstr);
20089fc5823aSGarrett Wollman 		filefd = fileno(instr);
20099fc5823aSGarrett Wollman 
2010f6f0c4b9SDan Moschuk 		if (isreg) {
20119fc5823aSGarrett Wollman 
2012f6f0c4b9SDan Moschuk 			off_t offset;
2013f6f0c4b9SDan Moschuk 			int err;
2014f6f0c4b9SDan Moschuk 
2015f6f0c4b9SDan Moschuk 			err = cnt = offset = 0;
2016f6f0c4b9SDan Moschuk 
2017492f1d9cSMaxim Konovalov 			while (err != -1 && filesize > 0) {
2018492f1d9cSMaxim Konovalov 				err = sendfile(filefd, netfd, offset, 0,
2019f6f0c4b9SDan Moschuk 					(struct sf_hdtr *) NULL, &cnt, 0);
20203af48c42SMaxim Konovalov 				/*
20213af48c42SMaxim Konovalov 				 * Calculate byte_count before OOB processing.
20223af48c42SMaxim Konovalov 				 * It can be used in myoob() later.
20233af48c42SMaxim Konovalov 				 */
20243af48c42SMaxim Konovalov 				byte_count += cnt;
20254b82fc95SYaroslav Tykhiy 				if (recvurg)
20264b82fc95SYaroslav Tykhiy 					goto got_oob;
2027f6f0c4b9SDan Moschuk 				offset += cnt;
2028492f1d9cSMaxim Konovalov 				filesize -= cnt;
2029f6f0c4b9SDan Moschuk 
2030f6f0c4b9SDan Moschuk 				if (err == -1) {
2031f6f0c4b9SDan Moschuk 					if (!cnt)
2032f6f0c4b9SDan Moschuk 						goto oldway;
2033f6f0c4b9SDan Moschuk 
20349fc5823aSGarrett Wollman 					goto data_err;
2035f6f0c4b9SDan Moschuk 				}
2036f6f0c4b9SDan Moschuk 			}
2037f6f0c4b9SDan Moschuk 
20380849c184SDan Moschuk 			transflag = 0;
20399fc5823aSGarrett Wollman 			reply(226, "Transfer complete.");
20404b82fc95SYaroslav Tykhiy 			return (0);
20419fc5823aSGarrett Wollman 		}
20429fc5823aSGarrett Wollman 
20439fc5823aSGarrett Wollman oldway:
2044ea022d16SRodney W. Grimes 		if ((buf = malloc((u_int)blksize)) == NULL) {
2045ea022d16SRodney W. Grimes 			transflag = 0;
2046ea022d16SRodney W. Grimes 			perror_reply(451, "Local resource failure: malloc");
20474b82fc95SYaroslav Tykhiy 			return (-1);
2048ea022d16SRodney W. Grimes 		}
20499fc5823aSGarrett Wollman 
2050ea022d16SRodney W. Grimes 		while ((cnt = read(filefd, buf, (u_int)blksize)) > 0 &&
2051ea022d16SRodney W. Grimes 		    write(netfd, buf, cnt) == cnt)
2052ea022d16SRodney W. Grimes 			byte_count += cnt;
2053ea022d16SRodney W. Grimes 		transflag = 0;
2054ea022d16SRodney W. Grimes 		(void)free(buf);
2055ea022d16SRodney W. Grimes 		if (cnt != 0) {
2056ea022d16SRodney W. Grimes 			if (cnt < 0)
2057ea022d16SRodney W. Grimes 				goto file_err;
2058ea022d16SRodney W. Grimes 			goto data_err;
2059ea022d16SRodney W. Grimes 		}
2060ea022d16SRodney W. Grimes 		reply(226, "Transfer complete.");
20614b82fc95SYaroslav Tykhiy 		return (0);
2062ea022d16SRodney W. Grimes 	default:
2063ea022d16SRodney W. Grimes 		transflag = 0;
2064ea022d16SRodney W. Grimes 		reply(550, "Unimplemented TYPE %d in send_data", type);
20654b82fc95SYaroslav Tykhiy 		return (-1);
2066ea022d16SRodney W. Grimes 	}
2067ea022d16SRodney W. Grimes 
2068ea022d16SRodney W. Grimes data_err:
2069ea022d16SRodney W. Grimes 	transflag = 0;
2070ea022d16SRodney W. Grimes 	perror_reply(426, "Data connection");
20714b82fc95SYaroslav Tykhiy 	return (-1);
2072ea022d16SRodney W. Grimes 
2073ea022d16SRodney W. Grimes file_err:
2074ea022d16SRodney W. Grimes 	transflag = 0;
2075ea022d16SRodney W. Grimes 	perror_reply(551, "Error on input file");
20764b82fc95SYaroslav Tykhiy 	return (-1);
20774b82fc95SYaroslav Tykhiy 
20784b82fc95SYaroslav Tykhiy got_oob:
20794b82fc95SYaroslav Tykhiy 	myoob();
20804b82fc95SYaroslav Tykhiy 	recvurg = 0;
20814b82fc95SYaroslav Tykhiy 	transflag = 0;
20824b82fc95SYaroslav Tykhiy 	return (-1);
2083ea022d16SRodney W. Grimes }
2084ea022d16SRodney W. Grimes 
2085ea022d16SRodney W. Grimes /*
2086ea022d16SRodney W. Grimes  * Transfer data from peer to "outstr" using the appropriate encapulation of
2087ea022d16SRodney W. Grimes  * the data subject to Mode, Structure, and Type.
2088ea022d16SRodney W. Grimes  *
2089ea022d16SRodney W. Grimes  * N.B.: Form isn't handled.
2090ea022d16SRodney W. Grimes  */
2091ea022d16SRodney W. Grimes static int
2092e4bc453cSWarner Losh receive_data(FILE *instr, FILE *outstr)
2093ea022d16SRodney W. Grimes {
2094ea022d16SRodney W. Grimes 	int c;
209561f891a6SPaul Traina 	int cnt, bare_lfs;
2096ea022d16SRodney W. Grimes 	char buf[BUFSIZ];
2097ea022d16SRodney W. Grimes 
2098ea022d16SRodney W. Grimes 	transflag++;
209961f891a6SPaul Traina 	bare_lfs = 0;
210061f891a6SPaul Traina 
2101ea022d16SRodney W. Grimes 	switch (type) {
2102ea022d16SRodney W. Grimes 
2103ea022d16SRodney W. Grimes 	case TYPE_I:
2104ea022d16SRodney W. Grimes 	case TYPE_L:
2105ea022d16SRodney W. Grimes 		while ((cnt = read(fileno(instr), buf, sizeof(buf))) > 0) {
21064b82fc95SYaroslav Tykhiy 			if (recvurg)
21074b82fc95SYaroslav Tykhiy 				goto got_oob;
2108ea022d16SRodney W. Grimes 			if (write(fileno(outstr), buf, cnt) != cnt)
2109ea022d16SRodney W. Grimes 				goto file_err;
2110ea022d16SRodney W. Grimes 			byte_count += cnt;
2111ea022d16SRodney W. Grimes 		}
21124b82fc95SYaroslav Tykhiy 		if (recvurg)
21134b82fc95SYaroslav Tykhiy 			goto got_oob;
2114ea022d16SRodney W. Grimes 		if (cnt < 0)
2115ea022d16SRodney W. Grimes 			goto data_err;
2116ea022d16SRodney W. Grimes 		transflag = 0;
2117ea022d16SRodney W. Grimes 		return (0);
2118ea022d16SRodney W. Grimes 
2119ea022d16SRodney W. Grimes 	case TYPE_E:
2120ea022d16SRodney W. Grimes 		reply(553, "TYPE E not implemented.");
2121ea022d16SRodney W. Grimes 		transflag = 0;
2122ea022d16SRodney W. Grimes 		return (-1);
2123ea022d16SRodney W. Grimes 
2124ea022d16SRodney W. Grimes 	case TYPE_A:
2125ea022d16SRodney W. Grimes 		while ((c = getc(instr)) != EOF) {
21264b82fc95SYaroslav Tykhiy 			if (recvurg)
21274b82fc95SYaroslav Tykhiy 				goto got_oob;
2128ea022d16SRodney W. Grimes 			byte_count++;
2129ea022d16SRodney W. Grimes 			if (c == '\n')
2130ea022d16SRodney W. Grimes 				bare_lfs++;
2131ea022d16SRodney W. Grimes 			while (c == '\r') {
2132ea022d16SRodney W. Grimes 				if (ferror(outstr))
2133ea022d16SRodney W. Grimes 					goto data_err;
2134ea022d16SRodney W. Grimes 				if ((c = getc(instr)) != '\n') {
2135ea022d16SRodney W. Grimes 					(void) putc ('\r', outstr);
2136ea022d16SRodney W. Grimes 					if (c == '\0' || c == EOF)
2137ea022d16SRodney W. Grimes 						goto contin2;
2138ea022d16SRodney W. Grimes 				}
2139ea022d16SRodney W. Grimes 			}
2140ea022d16SRodney W. Grimes 			(void) putc(c, outstr);
2141ea022d16SRodney W. Grimes 	contin2:	;
2142ea022d16SRodney W. Grimes 		}
21434b82fc95SYaroslav Tykhiy 		if (recvurg)
21444b82fc95SYaroslav Tykhiy 			goto got_oob;
2145ea022d16SRodney W. Grimes 		fflush(outstr);
2146ea022d16SRodney W. Grimes 		if (ferror(instr))
2147ea022d16SRodney W. Grimes 			goto data_err;
2148ea022d16SRodney W. Grimes 		if (ferror(outstr))
2149ea022d16SRodney W. Grimes 			goto file_err;
2150ea022d16SRodney W. Grimes 		transflag = 0;
2151ea022d16SRodney W. Grimes 		if (bare_lfs) {
2152ea022d16SRodney W. Grimes 			lreply(226,
2153ea022d16SRodney W. Grimes 		"WARNING! %d bare linefeeds received in ASCII mode",
2154ea022d16SRodney W. Grimes 			    bare_lfs);
2155ea022d16SRodney W. Grimes 		(void)printf("   File may not have transferred correctly.\r\n");
2156ea022d16SRodney W. Grimes 		}
2157ea022d16SRodney W. Grimes 		return (0);
2158ea022d16SRodney W. Grimes 	default:
2159ea022d16SRodney W. Grimes 		reply(550, "Unimplemented TYPE %d in receive_data", type);
2160ea022d16SRodney W. Grimes 		transflag = 0;
2161ea022d16SRodney W. Grimes 		return (-1);
2162ea022d16SRodney W. Grimes 	}
2163ea022d16SRodney W. Grimes 
2164ea022d16SRodney W. Grimes data_err:
2165ea022d16SRodney W. Grimes 	transflag = 0;
2166ea022d16SRodney W. Grimes 	perror_reply(426, "Data Connection");
2167ea022d16SRodney W. Grimes 	return (-1);
2168ea022d16SRodney W. Grimes 
2169ea022d16SRodney W. Grimes file_err:
2170ea022d16SRodney W. Grimes 	transflag = 0;
2171ea022d16SRodney W. Grimes 	perror_reply(452, "Error writing file");
2172ea022d16SRodney W. Grimes 	return (-1);
21734b82fc95SYaroslav Tykhiy 
21744b82fc95SYaroslav Tykhiy got_oob:
21754b82fc95SYaroslav Tykhiy 	myoob();
21764b82fc95SYaroslav Tykhiy 	recvurg = 0;
21774b82fc95SYaroslav Tykhiy 	transflag = 0;
21784b82fc95SYaroslav Tykhiy 	return (-1);
2179ea022d16SRodney W. Grimes }
2180ea022d16SRodney W. Grimes 
2181ea022d16SRodney W. Grimes void
2182e4bc453cSWarner Losh statfilecmd(char *filename)
2183ea022d16SRodney W. Grimes {
2184ea022d16SRodney W. Grimes 	FILE *fin;
2185f8a581a0SYaroslav Tykhiy 	int atstart;
2186ea022d16SRodney W. Grimes 	int c;
2187ea022d16SRodney W. Grimes 	char line[LINE_MAX];
2188ea022d16SRodney W. Grimes 
2189af85d782SDavid Nugent 	(void)snprintf(line, sizeof(line), _PATH_LS " -lgA %s", filename);
2190ea022d16SRodney W. Grimes 	fin = ftpd_popen(line, "r");
2191ea022d16SRodney W. Grimes 	lreply(211, "status of %s:", filename);
2192f8a581a0SYaroslav Tykhiy 	atstart = 1;
2193ea022d16SRodney W. Grimes 	while ((c = getc(fin)) != EOF) {
2194ea022d16SRodney W. Grimes 		if (c == '\n') {
2195ea022d16SRodney W. Grimes 			if (ferror(stdout)){
2196ea022d16SRodney W. Grimes 				perror_reply(421, "control connection");
2197ea022d16SRodney W. Grimes 				(void) ftpd_pclose(fin);
2198ea022d16SRodney W. Grimes 				dologout(1);
2199ea022d16SRodney W. Grimes 				/* NOTREACHED */
2200ea022d16SRodney W. Grimes 			}
2201ea022d16SRodney W. Grimes 			if (ferror(fin)) {
2202ea022d16SRodney W. Grimes 				perror_reply(551, filename);
2203ea022d16SRodney W. Grimes 				(void) ftpd_pclose(fin);
2204ea022d16SRodney W. Grimes 				return;
2205ea022d16SRodney W. Grimes 			}
2206ea022d16SRodney W. Grimes 			(void) putc('\r', stdout);
2207ea022d16SRodney W. Grimes 		}
2208f8a581a0SYaroslav Tykhiy 		/*
2209f8a581a0SYaroslav Tykhiy 		 * RFC 959 says neutral text should be prepended before
2210f8a581a0SYaroslav Tykhiy 		 * a leading 3-digit number followed by whitespace, but
2211f8a581a0SYaroslav Tykhiy 		 * many ftp clients can be confused by any leading digits,
2212f8a581a0SYaroslav Tykhiy 		 * as a matter of fact.
2213f8a581a0SYaroslav Tykhiy 		 */
2214f8a581a0SYaroslav Tykhiy 		if (atstart && isdigit(c))
2215f8a581a0SYaroslav Tykhiy 			(void) putc(' ', stdout);
2216ea022d16SRodney W. Grimes 		(void) putc(c, stdout);
2217f8a581a0SYaroslav Tykhiy 		atstart = (c == '\n');
2218ea022d16SRodney W. Grimes 	}
2219ea022d16SRodney W. Grimes 	(void) ftpd_pclose(fin);
2220ea022d16SRodney W. Grimes 	reply(211, "End of Status");
2221ea022d16SRodney W. Grimes }
2222ea022d16SRodney W. Grimes 
2223ea022d16SRodney W. Grimes void
2224e4bc453cSWarner Losh statcmd(void)
2225ea022d16SRodney W. Grimes {
22264dd8b5abSYoshinobu Inoue 	union sockunion *su;
2227ea022d16SRodney W. Grimes 	u_char *a, *p;
2228b0f06defSHajimu UMEMOTO 	char hname[NI_MAXHOST];
22294dd8b5abSYoshinobu Inoue 	int ispassive;
2230ea022d16SRodney W. Grimes 
2231a23f61bcSYaroslav Tykhiy 	lreply(211, "%s FTP server status:", hostname);
2232ea022d16SRodney W. Grimes 	printf("     %s\r\n", version);
2233ea022d16SRodney W. Grimes 	printf("     Connected to %s", remotehost);
22344dd8b5abSYoshinobu Inoue 	if (!getnameinfo((struct sockaddr *)&his_addr, his_addr.su_len,
2235b0f06defSHajimu UMEMOTO 			 hname, sizeof(hname) - 1, NULL, 0, NI_NUMERICHOST)) {
22364dd8b5abSYoshinobu Inoue 		if (strcmp(hname, remotehost) != 0)
22374dd8b5abSYoshinobu Inoue 			printf(" (%s)", hname);
22384dd8b5abSYoshinobu Inoue 	}
2239ea022d16SRodney W. Grimes 	printf("\r\n");
2240ea022d16SRodney W. Grimes 	if (logged_in) {
2241ea022d16SRodney W. Grimes 		if (guest)
2242ea022d16SRodney W. Grimes 			printf("     Logged in anonymously\r\n");
2243ea022d16SRodney W. Grimes 		else
2244ea022d16SRodney W. Grimes 			printf("     Logged in as %s\r\n", pw->pw_name);
2245ea022d16SRodney W. Grimes 	} else if (askpasswd)
2246ea022d16SRodney W. Grimes 		printf("     Waiting for password\r\n");
2247ea022d16SRodney W. Grimes 	else
2248ea022d16SRodney W. Grimes 		printf("     Waiting for user name\r\n");
2249ea022d16SRodney W. Grimes 	printf("     TYPE: %s", typenames[type]);
2250ea022d16SRodney W. Grimes 	if (type == TYPE_A || type == TYPE_E)
2251ea022d16SRodney W. Grimes 		printf(", FORM: %s", formnames[form]);
2252ea022d16SRodney W. Grimes 	if (type == TYPE_L)
225389fdc4e1SMike Barcroft #if CHAR_BIT == 8
225489fdc4e1SMike Barcroft 		printf(" %d", CHAR_BIT);
2255ea022d16SRodney W. Grimes #else
2256ea022d16SRodney W. Grimes 		printf(" %d", bytesize);	/* need definition! */
2257ea022d16SRodney W. Grimes #endif
2258ea022d16SRodney W. Grimes 	printf("; STRUcture: %s; transfer MODE: %s\r\n",
2259ea022d16SRodney W. Grimes 	    strunames[stru], modenames[mode]);
2260ea022d16SRodney W. Grimes 	if (data != -1)
2261ea022d16SRodney W. Grimes 		printf("     Data connection open\r\n");
2262ea022d16SRodney W. Grimes 	else if (pdata != -1) {
22634dd8b5abSYoshinobu Inoue 		ispassive = 1;
22644dd8b5abSYoshinobu Inoue 		su = &pasv_addr;
2265ea022d16SRodney W. Grimes 		goto printaddr;
2266ea022d16SRodney W. Grimes 	} else if (usedefault == 0) {
22674dd8b5abSYoshinobu Inoue 		ispassive = 0;
22684dd8b5abSYoshinobu Inoue 		su = &data_dest;
2269ea022d16SRodney W. Grimes printaddr:
2270ea022d16SRodney W. Grimes #define UC(b) (((int) b) & 0xff)
22714dd8b5abSYoshinobu Inoue 		if (epsvall) {
22724dd8b5abSYoshinobu Inoue 			printf("     EPSV only mode (EPSV ALL)\r\n");
22734dd8b5abSYoshinobu Inoue 			goto epsvonly;
22744dd8b5abSYoshinobu Inoue 		}
22754dd8b5abSYoshinobu Inoue 
22764dd8b5abSYoshinobu Inoue 		/* PORT/PASV */
22774dd8b5abSYoshinobu Inoue 		if (su->su_family == AF_INET) {
22784dd8b5abSYoshinobu Inoue 			a = (u_char *) &su->su_sin.sin_addr;
22794dd8b5abSYoshinobu Inoue 			p = (u_char *) &su->su_sin.sin_port;
22804dd8b5abSYoshinobu Inoue 			printf("     %s (%d,%d,%d,%d,%d,%d)\r\n",
22814dd8b5abSYoshinobu Inoue 				ispassive ? "PASV" : "PORT",
22824dd8b5abSYoshinobu Inoue 				UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
22834dd8b5abSYoshinobu Inoue 				UC(p[0]), UC(p[1]));
22844dd8b5abSYoshinobu Inoue 		}
22854dd8b5abSYoshinobu Inoue 
22864dd8b5abSYoshinobu Inoue 		/* LPRT/LPSV */
22874dd8b5abSYoshinobu Inoue 	    {
22884dd8b5abSYoshinobu Inoue 		int alen, af, i;
22894dd8b5abSYoshinobu Inoue 
22904dd8b5abSYoshinobu Inoue 		switch (su->su_family) {
22914dd8b5abSYoshinobu Inoue 		case AF_INET:
22924dd8b5abSYoshinobu Inoue 			a = (u_char *) &su->su_sin.sin_addr;
22934dd8b5abSYoshinobu Inoue 			p = (u_char *) &su->su_sin.sin_port;
22944dd8b5abSYoshinobu Inoue 			alen = sizeof(su->su_sin.sin_addr);
22954dd8b5abSYoshinobu Inoue 			af = 4;
22964dd8b5abSYoshinobu Inoue 			break;
22974dd8b5abSYoshinobu Inoue 		case AF_INET6:
22984dd8b5abSYoshinobu Inoue 			a = (u_char *) &su->su_sin6.sin6_addr;
22994dd8b5abSYoshinobu Inoue 			p = (u_char *) &su->su_sin6.sin6_port;
23004dd8b5abSYoshinobu Inoue 			alen = sizeof(su->su_sin6.sin6_addr);
23014dd8b5abSYoshinobu Inoue 			af = 6;
23024dd8b5abSYoshinobu Inoue 			break;
23034dd8b5abSYoshinobu Inoue 		default:
23044dd8b5abSYoshinobu Inoue 			af = 0;
23054dd8b5abSYoshinobu Inoue 			break;
23064dd8b5abSYoshinobu Inoue 		}
23074dd8b5abSYoshinobu Inoue 		if (af) {
23084dd8b5abSYoshinobu Inoue 			printf("     %s (%d,%d,", ispassive ? "LPSV" : "LPRT",
23094dd8b5abSYoshinobu Inoue 				af, alen);
23104dd8b5abSYoshinobu Inoue 			for (i = 0; i < alen; i++)
23114dd8b5abSYoshinobu Inoue 				printf("%d,", UC(a[i]));
23124dd8b5abSYoshinobu Inoue 			printf("%d,%d,%d)\r\n", 2, UC(p[0]), UC(p[1]));
23134dd8b5abSYoshinobu Inoue 		}
23144dd8b5abSYoshinobu Inoue 	    }
23154dd8b5abSYoshinobu Inoue 
23164dd8b5abSYoshinobu Inoue epsvonly:;
23174dd8b5abSYoshinobu Inoue 		/* EPRT/EPSV */
23184dd8b5abSYoshinobu Inoue 	    {
23194dd8b5abSYoshinobu Inoue 		int af;
23204dd8b5abSYoshinobu Inoue 
23214dd8b5abSYoshinobu Inoue 		switch (su->su_family) {
23224dd8b5abSYoshinobu Inoue 		case AF_INET:
23234dd8b5abSYoshinobu Inoue 			af = 1;
23244dd8b5abSYoshinobu Inoue 			break;
23254dd8b5abSYoshinobu Inoue 		case AF_INET6:
23264dd8b5abSYoshinobu Inoue 			af = 2;
23274dd8b5abSYoshinobu Inoue 			break;
23284dd8b5abSYoshinobu Inoue 		default:
23294dd8b5abSYoshinobu Inoue 			af = 0;
23304dd8b5abSYoshinobu Inoue 			break;
23314dd8b5abSYoshinobu Inoue 		}
23324dd8b5abSYoshinobu Inoue 		if (af) {
2333b0f06defSHajimu UMEMOTO 			union sockunion tmp;
2334b0f06defSHajimu UMEMOTO 
2335b0f06defSHajimu UMEMOTO 			tmp = *su;
2336b0f06defSHajimu UMEMOTO 			if (tmp.su_family == AF_INET6)
2337b0f06defSHajimu UMEMOTO 				tmp.su_sin6.sin6_scope_id = 0;
2338b0f06defSHajimu UMEMOTO 			if (!getnameinfo((struct sockaddr *)&tmp, tmp.su_len,
23394dd8b5abSYoshinobu Inoue 					hname, sizeof(hname) - 1, NULL, 0,
23404dd8b5abSYoshinobu Inoue 					NI_NUMERICHOST)) {
23414dd8b5abSYoshinobu Inoue 				printf("     %s |%d|%s|%d|\r\n",
23424dd8b5abSYoshinobu Inoue 					ispassive ? "EPSV" : "EPRT",
2343b0f06defSHajimu UMEMOTO 					af, hname, htons(tmp.su_port));
23444dd8b5abSYoshinobu Inoue 			}
23454dd8b5abSYoshinobu Inoue 		}
23464dd8b5abSYoshinobu Inoue 	    }
2347ea022d16SRodney W. Grimes #undef UC
2348ea022d16SRodney W. Grimes 	} else
2349ea022d16SRodney W. Grimes 		printf("     No data connection\r\n");
2350ea022d16SRodney W. Grimes 	reply(211, "End of status");
2351ea022d16SRodney W. Grimes }
2352ea022d16SRodney W. Grimes 
2353ea022d16SRodney W. Grimes void
2354e4bc453cSWarner Losh fatalerror(char *s)
2355ea022d16SRodney W. Grimes {
2356ea022d16SRodney W. Grimes 
2357ea022d16SRodney W. Grimes 	reply(451, "Error in server: %s\n", s);
2358ea022d16SRodney W. Grimes 	reply(221, "Closing connection due to server error.");
2359ea022d16SRodney W. Grimes 	dologout(0);
2360ea022d16SRodney W. Grimes 	/* NOTREACHED */
2361ea022d16SRodney W. Grimes }
2362ea022d16SRodney W. Grimes 
2363ea022d16SRodney W. Grimes void
2364ea022d16SRodney W. Grimes reply(int n, const char *fmt, ...)
2365ea022d16SRodney W. Grimes {
2366ea022d16SRodney W. Grimes 	va_list ap;
2367e4bc453cSWarner Losh 
2368ea022d16SRodney W. Grimes 	va_start(ap, fmt);
2369ea022d16SRodney W. Grimes 	(void)printf("%d ", n);
2370ea022d16SRodney W. Grimes 	(void)vprintf(fmt, ap);
2371ea022d16SRodney W. Grimes 	(void)printf("\r\n");
2372ea022d16SRodney W. Grimes 	(void)fflush(stdout);
2373618b0bbaSMark Murray 	if (ftpdebug) {
2374ea022d16SRodney W. Grimes 		syslog(LOG_DEBUG, "<--- %d ", n);
2375ea022d16SRodney W. Grimes 		vsyslog(LOG_DEBUG, fmt, ap);
2376ea022d16SRodney W. Grimes 	}
2377ea022d16SRodney W. Grimes }
2378ea022d16SRodney W. Grimes 
2379ea022d16SRodney W. Grimes void
2380ea022d16SRodney W. Grimes lreply(int n, const char *fmt, ...)
2381ea022d16SRodney W. Grimes {
2382ea022d16SRodney W. Grimes 	va_list ap;
2383e4bc453cSWarner Losh 
2384ea022d16SRodney W. Grimes 	va_start(ap, fmt);
2385ea022d16SRodney W. Grimes 	(void)printf("%d- ", n);
2386ea022d16SRodney W. Grimes 	(void)vprintf(fmt, ap);
2387ea022d16SRodney W. Grimes 	(void)printf("\r\n");
2388ea022d16SRodney W. Grimes 	(void)fflush(stdout);
2389618b0bbaSMark Murray 	if (ftpdebug) {
2390ea022d16SRodney W. Grimes 		syslog(LOG_DEBUG, "<--- %d- ", n);
2391ea022d16SRodney W. Grimes 		vsyslog(LOG_DEBUG, fmt, ap);
2392ea022d16SRodney W. Grimes 	}
2393ea022d16SRodney W. Grimes }
2394ea022d16SRodney W. Grimes 
2395ea022d16SRodney W. Grimes static void
2396e4bc453cSWarner Losh ack(char *s)
2397ea022d16SRodney W. Grimes {
2398ea022d16SRodney W. Grimes 
2399ea022d16SRodney W. Grimes 	reply(250, "%s command successful.", s);
2400ea022d16SRodney W. Grimes }
2401ea022d16SRodney W. Grimes 
2402ea022d16SRodney W. Grimes void
2403e4bc453cSWarner Losh nack(char *s)
2404ea022d16SRodney W. Grimes {
2405ea022d16SRodney W. Grimes 
2406ea022d16SRodney W. Grimes 	reply(502, "%s command not implemented.", s);
2407ea022d16SRodney W. Grimes }
2408ea022d16SRodney W. Grimes 
2409ea022d16SRodney W. Grimes /* ARGSUSED */
2410ea022d16SRodney W. Grimes void
2411e4bc453cSWarner Losh yyerror(char *s)
2412ea022d16SRodney W. Grimes {
2413ea022d16SRodney W. Grimes 	char *cp;
2414ea022d16SRodney W. Grimes 
24159aca17cbSMark Murray 	if ((cp = strchr(cbuf,'\n')))
2416ea022d16SRodney W. Grimes 		*cp = '\0';
2417ea022d16SRodney W. Grimes 	reply(500, "'%s': command not understood.", cbuf);
2418ea022d16SRodney W. Grimes }
2419ea022d16SRodney W. Grimes 
2420ea022d16SRodney W. Grimes void
2421e4bc453cSWarner Losh delete(char *name)
2422ea022d16SRodney W. Grimes {
2423ea022d16SRodney W. Grimes 	struct stat st;
2424ea022d16SRodney W. Grimes 
2425ea022d16SRodney W. Grimes 	LOGCMD("delete", name);
24261b0e12d7SYaroslav Tykhiy 	if (lstat(name, &st) < 0) {
2427ea022d16SRodney W. Grimes 		perror_reply(550, name);
2428ea022d16SRodney W. Grimes 		return;
2429ea022d16SRodney W. Grimes 	}
2430ea022d16SRodney W. Grimes 	if ((st.st_mode&S_IFMT) == S_IFDIR) {
2431ea022d16SRodney W. Grimes 		if (rmdir(name) < 0) {
2432ea022d16SRodney W. Grimes 			perror_reply(550, name);
2433ea022d16SRodney W. Grimes 			return;
2434ea022d16SRodney W. Grimes 		}
2435ea022d16SRodney W. Grimes 		goto done;
2436ea022d16SRodney W. Grimes 	}
2437ea022d16SRodney W. Grimes 	if (unlink(name) < 0) {
2438ea022d16SRodney W. Grimes 		perror_reply(550, name);
2439ea022d16SRodney W. Grimes 		return;
2440ea022d16SRodney W. Grimes 	}
2441ea022d16SRodney W. Grimes done:
2442ea022d16SRodney W. Grimes 	ack("DELE");
2443ea022d16SRodney W. Grimes }
2444ea022d16SRodney W. Grimes 
2445ea022d16SRodney W. Grimes void
2446e4bc453cSWarner Losh cwd(char *path)
2447ea022d16SRodney W. Grimes {
2448ea022d16SRodney W. Grimes 
2449ea022d16SRodney W. Grimes 	if (chdir(path) < 0)
2450ea022d16SRodney W. Grimes 		perror_reply(550, path);
2451ea022d16SRodney W. Grimes 	else
2452ea022d16SRodney W. Grimes 		ack("CWD");
2453ea022d16SRodney W. Grimes }
2454ea022d16SRodney W. Grimes 
2455ea022d16SRodney W. Grimes void
2456e4bc453cSWarner Losh makedir(char *name)
2457ea022d16SRodney W. Grimes {
24582b748987SYaroslav Tykhiy 	char *s;
2459ea022d16SRodney W. Grimes 
2460ea022d16SRodney W. Grimes 	LOGCMD("mkdir", name);
2461d186bb12SMatthew N. Dodd 	if (guest && noguestmkd)
2462d186bb12SMatthew N. Dodd 		reply(550, "%s: permission denied", name);
2463d186bb12SMatthew N. Dodd 	else if (mkdir(name, 0777) < 0)
2464ea022d16SRodney W. Grimes 		perror_reply(550, name);
24652b748987SYaroslav Tykhiy 	else {
24662b748987SYaroslav Tykhiy 		if ((s = doublequote(name)) == NULL)
24672b748987SYaroslav Tykhiy 			fatalerror("Ran out of memory.");
24682b748987SYaroslav Tykhiy 		reply(257, "\"%s\" directory created.", s);
24692b748987SYaroslav Tykhiy 		free(s);
24702b748987SYaroslav Tykhiy 	}
2471ea022d16SRodney W. Grimes }
2472ea022d16SRodney W. Grimes 
2473ea022d16SRodney W. Grimes void
2474e4bc453cSWarner Losh removedir(char *name)
2475ea022d16SRodney W. Grimes {
2476ea022d16SRodney W. Grimes 
2477ea022d16SRodney W. Grimes 	LOGCMD("rmdir", name);
2478ea022d16SRodney W. Grimes 	if (rmdir(name) < 0)
2479ea022d16SRodney W. Grimes 		perror_reply(550, name);
2480ea022d16SRodney W. Grimes 	else
2481ea022d16SRodney W. Grimes 		ack("RMD");
2482ea022d16SRodney W. Grimes }
2483ea022d16SRodney W. Grimes 
2484ea022d16SRodney W. Grimes void
2485e4bc453cSWarner Losh pwd(void)
2486ea022d16SRodney W. Grimes {
2487e4648f05SYaroslav Tykhiy 	char *s, path[MAXPATHLEN + 1];
2488ea022d16SRodney W. Grimes 
2489ea022d16SRodney W. Grimes 	if (getwd(path) == (char *)NULL)
2490ea022d16SRodney W. Grimes 		reply(550, "%s.", path);
2491e4648f05SYaroslav Tykhiy 	else {
2492e4648f05SYaroslav Tykhiy 		if ((s = doublequote(path)) == NULL)
2493e4648f05SYaroslav Tykhiy 			fatalerror("Ran out of memory.");
2494e4648f05SYaroslav Tykhiy 		reply(257, "\"%s\" is current directory.", s);
2495e4648f05SYaroslav Tykhiy 		free(s);
2496e4648f05SYaroslav Tykhiy 	}
2497ea022d16SRodney W. Grimes }
2498ea022d16SRodney W. Grimes 
2499ea022d16SRodney W. Grimes char *
2500e4bc453cSWarner Losh renamefrom(char *name)
2501ea022d16SRodney W. Grimes {
2502ea022d16SRodney W. Grimes 	struct stat st;
2503ea022d16SRodney W. Grimes 
25041b0e12d7SYaroslav Tykhiy 	if (lstat(name, &st) < 0) {
2505ea022d16SRodney W. Grimes 		perror_reply(550, name);
2506ea022d16SRodney W. Grimes 		return ((char *)0);
2507ea022d16SRodney W. Grimes 	}
2508ea022d16SRodney W. Grimes 	reply(350, "File exists, ready for destination name");
2509ea022d16SRodney W. Grimes 	return (name);
2510ea022d16SRodney W. Grimes }
2511ea022d16SRodney W. Grimes 
2512ea022d16SRodney W. Grimes void
2513e4bc453cSWarner Losh renamecmd(char *from, char *to)
2514ea022d16SRodney W. Grimes {
251561f891a6SPaul Traina 	struct stat st;
2516ea022d16SRodney W. Grimes 
2517ea022d16SRodney W. Grimes 	LOGCMD2("rename", from, to);
251861f891a6SPaul Traina 
251961f891a6SPaul Traina 	if (guest && (stat(to, &st) == 0)) {
252061f891a6SPaul Traina 		reply(550, "%s: permission denied", to);
252161f891a6SPaul Traina 		return;
252261f891a6SPaul Traina 	}
252361f891a6SPaul Traina 
2524ea022d16SRodney W. Grimes 	if (rename(from, to) < 0)
2525ea022d16SRodney W. Grimes 		perror_reply(550, "rename");
2526ea022d16SRodney W. Grimes 	else
2527ea022d16SRodney W. Grimes 		ack("RNTO");
2528ea022d16SRodney W. Grimes }
2529ea022d16SRodney W. Grimes 
2530ea022d16SRodney W. Grimes static void
2531e4bc453cSWarner Losh dolog(struct sockaddr *who)
2532ea022d16SRodney W. Grimes {
25334dd8b5abSYoshinobu Inoue 	int error;
25344dd8b5abSYoshinobu Inoue 
25354dd8b5abSYoshinobu Inoue 	realhostname_sa(remotehost, sizeof(remotehost) - 1, who, who->sa_len);
2536ea022d16SRodney W. Grimes 
2537ea022d16SRodney W. Grimes #ifdef SETPROCTITLE
2538ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
2539ea4e54b9SDavid Nugent 	if (thishost != firsthost)
2540ea4e54b9SDavid Nugent 		snprintf(proctitle, sizeof(proctitle), "%s: connected (to %s)",
2541ea4e54b9SDavid Nugent 			 remotehost, hostname);
2542ea4e54b9SDavid Nugent 	else
2543ea4e54b9SDavid Nugent #endif
2544ea4e54b9SDavid Nugent 		snprintf(proctitle, sizeof(proctitle), "%s: connected",
2545ea4e54b9SDavid Nugent 			 remotehost);
2546b63e1fe2SPeter Wemm 	setproctitle("%s", proctitle);
2547ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */
2548ea022d16SRodney W. Grimes 
2549ea4e54b9SDavid Nugent 	if (logging) {
2550ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
2551ea4e54b9SDavid Nugent 		if (thishost != firsthost)
2552ea4e54b9SDavid Nugent 			syslog(LOG_INFO, "connection from %s (to %s)",
2553ea4e54b9SDavid Nugent 			       remotehost, hostname);
2554ea4e54b9SDavid Nugent 		else
2555ea4e54b9SDavid Nugent #endif
25564dd8b5abSYoshinobu Inoue 		{
25574dd8b5abSYoshinobu Inoue 			char	who_name[MAXHOSTNAMELEN];
25584dd8b5abSYoshinobu Inoue 
25594dd8b5abSYoshinobu Inoue 			error = getnameinfo(who, who->sa_len,
25604dd8b5abSYoshinobu Inoue 					    who_name, sizeof(who_name) - 1,
2561b0f06defSHajimu UMEMOTO 					    NULL, 0, NI_NUMERICHOST);
2562f5c57d05SEivind Eklund 			syslog(LOG_INFO, "connection from %s (%s)", remotehost,
25634dd8b5abSYoshinobu Inoue 			       error == 0 ? who_name : "");
25644dd8b5abSYoshinobu Inoue 		}
2565ea022d16SRodney W. Grimes 	}
2566ea4e54b9SDavid Nugent }
2567ea022d16SRodney W. Grimes 
2568ea022d16SRodney W. Grimes /*
2569ea022d16SRodney W. Grimes  * Record logout in wtmp file
2570ea022d16SRodney W. Grimes  * and exit with supplied status.
2571ea022d16SRodney W. Grimes  */
2572ea022d16SRodney W. Grimes void
2573e4bc453cSWarner Losh dologout(int status)
2574ea022d16SRodney W. Grimes {
25750b4df2eeSDavid Greenman 	/*
25760b4df2eeSDavid Greenman 	 * Prevent reception of SIGURG from resulting in a resumption
25770b4df2eeSDavid Greenman 	 * back to the main program loop.
25780b4df2eeSDavid Greenman 	 */
25790b4df2eeSDavid Greenman 	transflag = 0;
2580ea022d16SRodney W. Grimes 
25815d7e0128SYaroslav Tykhiy 	if (logged_in && dowtmp) {
2582ea022d16SRodney W. Grimes 		(void) seteuid((uid_t)0);
258346948173SHajimu UMEMOTO 		ftpd_logwtmp(ttyline, "", NULL);
2584ea022d16SRodney W. Grimes 	}
2585ea022d16SRodney W. Grimes 	/* beware of flushing buffers after a SIGPIPE */
2586ea022d16SRodney W. Grimes 	_exit(status);
2587ea022d16SRodney W. Grimes }
2588ea022d16SRodney W. Grimes 
2589ea022d16SRodney W. Grimes static void
2590e4bc453cSWarner Losh sigurg(int signo)
2591ea022d16SRodney W. Grimes {
25924b82fc95SYaroslav Tykhiy 
25934b82fc95SYaroslav Tykhiy 	recvurg = 1;
25944b82fc95SYaroslav Tykhiy }
25954b82fc95SYaroslav Tykhiy 
25964b82fc95SYaroslav Tykhiy static void
2597e4bc453cSWarner Losh myoob(void)
25984b82fc95SYaroslav Tykhiy {
2599ea022d16SRodney W. Grimes 	char *cp;
2600ea022d16SRodney W. Grimes 
2601ea022d16SRodney W. Grimes 	/* only process if transfer occurring */
2602ea022d16SRodney W. Grimes 	if (!transflag)
2603ea022d16SRodney W. Grimes 		return;
2604ea022d16SRodney W. Grimes 	cp = tmpline;
2605ea022d16SRodney W. Grimes 	if (getline(cp, 7, stdin) == NULL) {
2606ea022d16SRodney W. Grimes 		reply(221, "You could at least say goodbye.");
2607ea022d16SRodney W. Grimes 		dologout(0);
2608ea022d16SRodney W. Grimes 	}
2609ea022d16SRodney W. Grimes 	upper(cp);
2610ea022d16SRodney W. Grimes 	if (strcmp(cp, "ABOR\r\n") == 0) {
2611ea022d16SRodney W. Grimes 		tmpline[0] = '\0';
2612ea022d16SRodney W. Grimes 		reply(426, "Transfer aborted. Data connection closed.");
2613ea022d16SRodney W. Grimes 		reply(226, "Abort successful");
2614ea022d16SRodney W. Grimes 	}
2615ea022d16SRodney W. Grimes 	if (strcmp(cp, "STAT\r\n") == 0) {
26169db4bbf3SMichael Haro 		tmpline[0] = '\0';
2617ea022d16SRodney W. Grimes 		if (file_size != (off_t) -1)
2618ea022d16SRodney W. Grimes 			reply(213, "Status: %qd of %qd bytes transferred",
2619ea022d16SRodney W. Grimes 			    byte_count, file_size);
2620ea022d16SRodney W. Grimes 		else
2621ea022d16SRodney W. Grimes 			reply(213, "Status: %qd bytes transferred", byte_count);
2622ea022d16SRodney W. Grimes 	}
2623ea022d16SRodney W. Grimes }
2624ea022d16SRodney W. Grimes 
2625ea022d16SRodney W. Grimes /*
2626ea022d16SRodney W. Grimes  * Note: a response of 425 is not mentioned as a possible response to
2627ea022d16SRodney W. Grimes  *	the PASV command in RFC959. However, it has been blessed as
2628ea022d16SRodney W. Grimes  *	a legitimate response by Jon Postel in a telephone conversation
2629ea022d16SRodney W. Grimes  *	with Rick Adams on 25 Jan 89.
2630ea022d16SRodney W. Grimes  */
2631ea022d16SRodney W. Grimes void
2632e4bc453cSWarner Losh passive(void)
2633ea022d16SRodney W. Grimes {
26348af7c9a3SYaroslav Tykhiy 	int len, on;
2635ea022d16SRodney W. Grimes 	char *p, *a;
2636ea022d16SRodney W. Grimes 
263761f891a6SPaul Traina 	if (pdata >= 0)		/* close old port if one set */
263861f891a6SPaul Traina 		close(pdata);
263961f891a6SPaul Traina 
26404dd8b5abSYoshinobu Inoue 	pdata = socket(ctrl_addr.su_family, SOCK_STREAM, 0);
2641ea022d16SRodney W. Grimes 	if (pdata < 0) {
2642ea022d16SRodney W. Grimes 		perror_reply(425, "Can't open passive connection");
2643ea022d16SRodney W. Grimes 		return;
2644ea022d16SRodney W. Grimes 	}
26458af7c9a3SYaroslav Tykhiy 	on = 1;
26468af7c9a3SYaroslav Tykhiy 	if (setsockopt(pdata, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
26478af7c9a3SYaroslav Tykhiy 		syslog(LOG_WARNING, "pdata setsockopt (SO_REUSEADDR): %m");
26484c450ad7SPaul Traina 
26494c450ad7SPaul Traina 	(void) seteuid((uid_t)0);
265061f891a6SPaul Traina 
2651dacc9752SPaul Traina #ifdef IP_PORTRANGE
26524dd8b5abSYoshinobu Inoue 	if (ctrl_addr.su_family == AF_INET) {
26538af7c9a3SYaroslav Tykhiy 	    on = restricted_data_ports ? IP_PORTRANGE_HIGH
2654dacc9752SPaul Traina 				       : IP_PORTRANGE_DEFAULT;
2655dacc9752SPaul Traina 
265640e9d39eSPeter Wemm 	    if (setsockopt(pdata, IPPROTO_IP, IP_PORTRANGE,
265757d4ef07SYaroslav Tykhiy 			    &on, sizeof(on)) < 0)
26584c450ad7SPaul Traina 		    goto pasv_error;
26594c450ad7SPaul Traina 	}
2660dacc9752SPaul Traina #endif
26612db39860SNick Sayer #ifdef IPV6_PORTRANGE
26622db39860SNick Sayer 	if (ctrl_addr.su_family == AF_INET6) {
26638af7c9a3SYaroslav Tykhiy 	    on = restricted_data_ports ? IPV6_PORTRANGE_HIGH
26642db39860SNick Sayer 				       : IPV6_PORTRANGE_DEFAULT;
26652db39860SNick Sayer 
26662db39860SNick Sayer 	    if (setsockopt(pdata, IPPROTO_IPV6, IPV6_PORTRANGE,
266757d4ef07SYaroslav Tykhiy 			    &on, sizeof(on)) < 0)
26682db39860SNick Sayer 		    goto pasv_error;
26692db39860SNick Sayer 	}
26702db39860SNick Sayer #endif
267140e9d39eSPeter Wemm 
2672ea022d16SRodney W. Grimes 	pasv_addr = ctrl_addr;
26734dd8b5abSYoshinobu Inoue 	pasv_addr.su_port = 0;
26744dd8b5abSYoshinobu Inoue 	if (bind(pdata, (struct sockaddr *)&pasv_addr, pasv_addr.su_len) < 0)
2675ea022d16SRodney W. Grimes 		goto pasv_error;
267661f891a6SPaul Traina 
2677ea022d16SRodney W. Grimes 	(void) seteuid((uid_t)pw->pw_uid);
26784c450ad7SPaul Traina 
2679ea022d16SRodney W. Grimes 	len = sizeof(pasv_addr);
2680ea022d16SRodney W. Grimes 	if (getsockname(pdata, (struct sockaddr *) &pasv_addr, &len) < 0)
2681ea022d16SRodney W. Grimes 		goto pasv_error;
2682ea022d16SRodney W. Grimes 	if (listen(pdata, 1) < 0)
2683ea022d16SRodney W. Grimes 		goto pasv_error;
26844dd8b5abSYoshinobu Inoue 	if (pasv_addr.su_family == AF_INET)
26854dd8b5abSYoshinobu Inoue 		a = (char *) &pasv_addr.su_sin.sin_addr;
26864dd8b5abSYoshinobu Inoue 	else if (pasv_addr.su_family == AF_INET6 &&
26874dd8b5abSYoshinobu Inoue 		 IN6_IS_ADDR_V4MAPPED(&pasv_addr.su_sin6.sin6_addr))
26884dd8b5abSYoshinobu Inoue 		a = (char *) &pasv_addr.su_sin6.sin6_addr.s6_addr[12];
26894dd8b5abSYoshinobu Inoue 	else
26904dd8b5abSYoshinobu Inoue 		goto pasv_error;
26914dd8b5abSYoshinobu Inoue 
26924dd8b5abSYoshinobu Inoue 	p = (char *) &pasv_addr.su_port;
2693ea022d16SRodney W. Grimes 
2694ea022d16SRodney W. Grimes #define UC(b) (((int) b) & 0xff)
2695ea022d16SRodney W. Grimes 
2696ea022d16SRodney W. Grimes 	reply(227, "Entering Passive Mode (%d,%d,%d,%d,%d,%d)", UC(a[0]),
2697ea022d16SRodney W. Grimes 		UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1]));
2698ea022d16SRodney W. Grimes 	return;
2699ea022d16SRodney W. Grimes 
2700ea022d16SRodney W. Grimes pasv_error:
270161f891a6SPaul Traina 	(void) seteuid((uid_t)pw->pw_uid);
2702ea022d16SRodney W. Grimes 	(void) close(pdata);
2703ea022d16SRodney W. Grimes 	pdata = -1;
2704ea022d16SRodney W. Grimes 	perror_reply(425, "Can't open passive connection");
2705ea022d16SRodney W. Grimes 	return;
2706ea022d16SRodney W. Grimes }
2707ea022d16SRodney W. Grimes 
2708ea022d16SRodney W. Grimes /*
27094dd8b5abSYoshinobu Inoue  * Long Passive defined in RFC 1639.
27104dd8b5abSYoshinobu Inoue  *     228 Entering Long Passive Mode
27114dd8b5abSYoshinobu Inoue  *         (af, hal, h1, h2, h3,..., pal, p1, p2...)
27124dd8b5abSYoshinobu Inoue  */
27134dd8b5abSYoshinobu Inoue 
27144dd8b5abSYoshinobu Inoue void
2715e4bc453cSWarner Losh long_passive(char *cmd, int pf)
27164dd8b5abSYoshinobu Inoue {
27178af7c9a3SYaroslav Tykhiy 	int len, on;
27184dd8b5abSYoshinobu Inoue 	char *p, *a;
27194dd8b5abSYoshinobu Inoue 
27204dd8b5abSYoshinobu Inoue 	if (pdata >= 0)		/* close old port if one set */
27214dd8b5abSYoshinobu Inoue 		close(pdata);
27224dd8b5abSYoshinobu Inoue 
27234dd8b5abSYoshinobu Inoue 	if (pf != PF_UNSPEC) {
27244dd8b5abSYoshinobu Inoue 		if (ctrl_addr.su_family != pf) {
27254dd8b5abSYoshinobu Inoue 			switch (ctrl_addr.su_family) {
27264dd8b5abSYoshinobu Inoue 			case AF_INET:
27274dd8b5abSYoshinobu Inoue 				pf = 1;
27284dd8b5abSYoshinobu Inoue 				break;
27294dd8b5abSYoshinobu Inoue 			case AF_INET6:
27304dd8b5abSYoshinobu Inoue 				pf = 2;
27314dd8b5abSYoshinobu Inoue 				break;
27324dd8b5abSYoshinobu Inoue 			default:
27334dd8b5abSYoshinobu Inoue 				pf = 0;
27344dd8b5abSYoshinobu Inoue 				break;
27354dd8b5abSYoshinobu Inoue 			}
27364dd8b5abSYoshinobu Inoue 			/*
27374dd8b5abSYoshinobu Inoue 			 * XXX
27384dd8b5abSYoshinobu Inoue 			 * only EPRT/EPSV ready clients will understand this
27394dd8b5abSYoshinobu Inoue 			 */
27404dd8b5abSYoshinobu Inoue 			if (strcmp(cmd, "EPSV") == 0 && pf) {
27414dd8b5abSYoshinobu Inoue 				reply(522, "Network protocol mismatch, "
27424dd8b5abSYoshinobu Inoue 					"use (%d)", pf);
27434dd8b5abSYoshinobu Inoue 			} else
27444dd8b5abSYoshinobu Inoue 				reply(501, "Network protocol mismatch"); /*XXX*/
27454dd8b5abSYoshinobu Inoue 
27464dd8b5abSYoshinobu Inoue 			return;
27474dd8b5abSYoshinobu Inoue 		}
27484dd8b5abSYoshinobu Inoue 	}
27494dd8b5abSYoshinobu Inoue 
27504dd8b5abSYoshinobu Inoue 	pdata = socket(ctrl_addr.su_family, SOCK_STREAM, 0);
27514dd8b5abSYoshinobu Inoue 	if (pdata < 0) {
27524dd8b5abSYoshinobu Inoue 		perror_reply(425, "Can't open passive connection");
27534dd8b5abSYoshinobu Inoue 		return;
27544dd8b5abSYoshinobu Inoue 	}
27558af7c9a3SYaroslav Tykhiy 	on = 1;
27568af7c9a3SYaroslav Tykhiy 	if (setsockopt(pdata, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
27578af7c9a3SYaroslav Tykhiy 		syslog(LOG_WARNING, "pdata setsockopt (SO_REUSEADDR): %m");
27584dd8b5abSYoshinobu Inoue 
27594dd8b5abSYoshinobu Inoue 	(void) seteuid((uid_t)0);
27604dd8b5abSYoshinobu Inoue 
27614dd8b5abSYoshinobu Inoue 	pasv_addr = ctrl_addr;
27624dd8b5abSYoshinobu Inoue 	pasv_addr.su_port = 0;
27634dd8b5abSYoshinobu Inoue 	len = pasv_addr.su_len;
27644dd8b5abSYoshinobu Inoue 
27652db39860SNick Sayer #ifdef IP_PORTRANGE
27662db39860SNick Sayer 	if (ctrl_addr.su_family == AF_INET) {
27678af7c9a3SYaroslav Tykhiy 	    on = restricted_data_ports ? IP_PORTRANGE_HIGH
27682db39860SNick Sayer 				       : IP_PORTRANGE_DEFAULT;
27692db39860SNick Sayer 
27702db39860SNick Sayer 	    if (setsockopt(pdata, IPPROTO_IP, IP_PORTRANGE,
277157d4ef07SYaroslav Tykhiy 			    &on, sizeof(on)) < 0)
27722db39860SNick Sayer 		    goto pasv_error;
27732db39860SNick Sayer 	}
27742db39860SNick Sayer #endif
27752db39860SNick Sayer #ifdef IPV6_PORTRANGE
27762db39860SNick Sayer 	if (ctrl_addr.su_family == AF_INET6) {
27778af7c9a3SYaroslav Tykhiy 	    on = restricted_data_ports ? IPV6_PORTRANGE_HIGH
27782db39860SNick Sayer 				       : IPV6_PORTRANGE_DEFAULT;
27792db39860SNick Sayer 
27802db39860SNick Sayer 	    if (setsockopt(pdata, IPPROTO_IPV6, IPV6_PORTRANGE,
278157d4ef07SYaroslav Tykhiy 			    &on, sizeof(on)) < 0)
27822db39860SNick Sayer 		    goto pasv_error;
27832db39860SNick Sayer 	}
27842db39860SNick Sayer #endif
27852db39860SNick Sayer 
27864dd8b5abSYoshinobu Inoue 	if (bind(pdata, (struct sockaddr *)&pasv_addr, len) < 0)
27874dd8b5abSYoshinobu Inoue 		goto pasv_error;
27884dd8b5abSYoshinobu Inoue 
27894dd8b5abSYoshinobu Inoue 	(void) seteuid((uid_t)pw->pw_uid);
27904dd8b5abSYoshinobu Inoue 
27914dd8b5abSYoshinobu Inoue 	if (getsockname(pdata, (struct sockaddr *) &pasv_addr, &len) < 0)
27924dd8b5abSYoshinobu Inoue 		goto pasv_error;
27934dd8b5abSYoshinobu Inoue 	if (listen(pdata, 1) < 0)
27944dd8b5abSYoshinobu Inoue 		goto pasv_error;
27954dd8b5abSYoshinobu Inoue 
27964dd8b5abSYoshinobu Inoue #define UC(b) (((int) b) & 0xff)
27974dd8b5abSYoshinobu Inoue 
27984dd8b5abSYoshinobu Inoue 	if (strcmp(cmd, "LPSV") == 0) {
27994dd8b5abSYoshinobu Inoue 		p = (char *)&pasv_addr.su_port;
28004dd8b5abSYoshinobu Inoue 		switch (pasv_addr.su_family) {
28014dd8b5abSYoshinobu Inoue 		case AF_INET:
28024dd8b5abSYoshinobu Inoue 			a = (char *) &pasv_addr.su_sin.sin_addr;
28034dd8b5abSYoshinobu Inoue 		v4_reply:
28044dd8b5abSYoshinobu Inoue 			reply(228,
28054dd8b5abSYoshinobu Inoue "Entering Long Passive Mode (%d,%d,%d,%d,%d,%d,%d,%d,%d)",
28064dd8b5abSYoshinobu Inoue 			      4, 4, UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
28074dd8b5abSYoshinobu Inoue 			      2, UC(p[0]), UC(p[1]));
28084dd8b5abSYoshinobu Inoue 			return;
28094dd8b5abSYoshinobu Inoue 		case AF_INET6:
28104dd8b5abSYoshinobu Inoue 			if (IN6_IS_ADDR_V4MAPPED(&pasv_addr.su_sin6.sin6_addr)) {
28114dd8b5abSYoshinobu Inoue 				a = (char *) &pasv_addr.su_sin6.sin6_addr.s6_addr[12];
28124dd8b5abSYoshinobu Inoue 				goto v4_reply;
28134dd8b5abSYoshinobu Inoue 			}
28144dd8b5abSYoshinobu Inoue 			a = (char *) &pasv_addr.su_sin6.sin6_addr;
28154dd8b5abSYoshinobu Inoue 			reply(228,
28164dd8b5abSYoshinobu Inoue "Entering Long Passive Mode "
28174dd8b5abSYoshinobu Inoue "(%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d)",
28184dd8b5abSYoshinobu Inoue 			      6, 16, UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
28194dd8b5abSYoshinobu Inoue 			      UC(a[4]), UC(a[5]), UC(a[6]), UC(a[7]),
28204dd8b5abSYoshinobu Inoue 			      UC(a[8]), UC(a[9]), UC(a[10]), UC(a[11]),
28214dd8b5abSYoshinobu Inoue 			      UC(a[12]), UC(a[13]), UC(a[14]), UC(a[15]),
28224dd8b5abSYoshinobu Inoue 			      2, UC(p[0]), UC(p[1]));
28234dd8b5abSYoshinobu Inoue 			return;
28244dd8b5abSYoshinobu Inoue 		}
28254dd8b5abSYoshinobu Inoue 	} else if (strcmp(cmd, "EPSV") == 0) {
28264dd8b5abSYoshinobu Inoue 		switch (pasv_addr.su_family) {
28274dd8b5abSYoshinobu Inoue 		case AF_INET:
28284dd8b5abSYoshinobu Inoue 		case AF_INET6:
28294dd8b5abSYoshinobu Inoue 			reply(229, "Entering Extended Passive Mode (|||%d|)",
28304dd8b5abSYoshinobu Inoue 				ntohs(pasv_addr.su_port));
28314dd8b5abSYoshinobu Inoue 			return;
28324dd8b5abSYoshinobu Inoue 		}
28334dd8b5abSYoshinobu Inoue 	} else {
28344dd8b5abSYoshinobu Inoue 		/* more proper error code? */
28354dd8b5abSYoshinobu Inoue 	}
28364dd8b5abSYoshinobu Inoue 
28374dd8b5abSYoshinobu Inoue pasv_error:
28384dd8b5abSYoshinobu Inoue 	(void) seteuid((uid_t)pw->pw_uid);
28394dd8b5abSYoshinobu Inoue 	(void) close(pdata);
28404dd8b5abSYoshinobu Inoue 	pdata = -1;
28414dd8b5abSYoshinobu Inoue 	perror_reply(425, "Can't open passive connection");
28424dd8b5abSYoshinobu Inoue 	return;
28434dd8b5abSYoshinobu Inoue }
28444dd8b5abSYoshinobu Inoue 
28454dd8b5abSYoshinobu Inoue /*
2846a117c345SYaroslav Tykhiy  * Generate unique name for file with basename "local"
2847a117c345SYaroslav Tykhiy  * and open the file in order to avoid possible races.
2848a117c345SYaroslav Tykhiy  * Try "local" first, then "local.1", "local.2" etc, up to "local.99".
2849a117c345SYaroslav Tykhiy  * Return descriptor to the file, set "name" to its name.
2850a117c345SYaroslav Tykhiy  *
2851ea022d16SRodney W. Grimes  * Generates failure reply on error.
2852ea022d16SRodney W. Grimes  */
2853a117c345SYaroslav Tykhiy static int
2854a117c345SYaroslav Tykhiy guniquefd(char *local, char **name)
2855ea022d16SRodney W. Grimes {
2856ea022d16SRodney W. Grimes 	static char new[MAXPATHLEN];
2857ea022d16SRodney W. Grimes 	struct stat st;
2858ea022d16SRodney W. Grimes 	char *cp;
2859a117c345SYaroslav Tykhiy 	int count;
2860a117c345SYaroslav Tykhiy 	int fd;
2861ea022d16SRodney W. Grimes 
2862ea022d16SRodney W. Grimes 	cp = strrchr(local, '/');
2863ea022d16SRodney W. Grimes 	if (cp)
2864ea022d16SRodney W. Grimes 		*cp = '\0';
2865ea022d16SRodney W. Grimes 	if (stat(cp ? local : ".", &st) < 0) {
2866ea022d16SRodney W. Grimes 		perror_reply(553, cp ? local : ".");
2867a117c345SYaroslav Tykhiy 		return (-1);
2868ea022d16SRodney W. Grimes 	}
2869a117c345SYaroslav Tykhiy 	if (cp) {
2870a117c345SYaroslav Tykhiy 		/*
2871a117c345SYaroslav Tykhiy 		 * Let not overwrite dirname with counter suffix.
2872a117c345SYaroslav Tykhiy 		 * -4 is for /nn\0
2873a117c345SYaroslav Tykhiy 		 * In this extreme case dot won't be put in front of suffix.
2874a117c345SYaroslav Tykhiy 		 */
2875a117c345SYaroslav Tykhiy 		if (strlen(local) > sizeof(new) - 4) {
2876a117c345SYaroslav Tykhiy 			reply(553, "Pathname too long");
2877a117c345SYaroslav Tykhiy 			return (-1);
2878a117c345SYaroslav Tykhiy 		}
2879ea022d16SRodney W. Grimes 		*cp = '/';
2880a117c345SYaroslav Tykhiy 	}
2881e760ef2cSWarner Losh 	/* -4 is for the .nn<null> we put on the end below */
2882e760ef2cSWarner Losh 	(void) snprintf(new, sizeof(new) - 4, "%s", local);
2883ea022d16SRodney W. Grimes 	cp = new + strlen(new);
2884a117c345SYaroslav Tykhiy 	/*
2885a117c345SYaroslav Tykhiy 	 * Don't generate dotfile unless requested explicitly.
2886a117c345SYaroslav Tykhiy 	 * This covers the case when basename gets truncated off
2887a117c345SYaroslav Tykhiy 	 * by buffer size.
2888a117c345SYaroslav Tykhiy 	 */
2889a117c345SYaroslav Tykhiy 	if (cp > new && cp[-1] != '/')
2890ea022d16SRodney W. Grimes 		*cp++ = '.';
2891a117c345SYaroslav Tykhiy 	for (count = 0; count < 100; count++) {
2892a117c345SYaroslav Tykhiy 		/* At count 0 try unmodified name */
2893a117c345SYaroslav Tykhiy 		if (count)
2894ea022d16SRodney W. Grimes 			(void)sprintf(cp, "%d", count);
2895a117c345SYaroslav Tykhiy 		if ((fd = open(count ? new : local,
2896a117c345SYaroslav Tykhiy 		    O_RDWR | O_CREAT | O_EXCL, 0666)) >= 0) {
2897a117c345SYaroslav Tykhiy 			*name = count ? new : local;
2898a117c345SYaroslav Tykhiy 			return (fd);
2899a117c345SYaroslav Tykhiy 		}
2900ea022d16SRodney W. Grimes 	}
2901ea022d16SRodney W. Grimes 	reply(452, "Unique file name cannot be created.");
2902a117c345SYaroslav Tykhiy 	return (-1);
2903ea022d16SRodney W. Grimes }
2904ea022d16SRodney W. Grimes 
2905ea022d16SRodney W. Grimes /*
2906ea022d16SRodney W. Grimes  * Format and send reply containing system error number.
2907ea022d16SRodney W. Grimes  */
2908ea022d16SRodney W. Grimes void
2909e4bc453cSWarner Losh perror_reply(int code, char *string)
2910ea022d16SRodney W. Grimes {
2911ea022d16SRodney W. Grimes 
2912ea022d16SRodney W. Grimes 	reply(code, "%s: %s.", string, strerror(errno));
2913ea022d16SRodney W. Grimes }
2914ea022d16SRodney W. Grimes 
2915ea022d16SRodney W. Grimes static char *onefile[] = {
2916ea022d16SRodney W. Grimes 	"",
2917ea022d16SRodney W. Grimes 	0
2918ea022d16SRodney W. Grimes };
2919ea022d16SRodney W. Grimes 
2920ea022d16SRodney W. Grimes void
2921e4bc453cSWarner Losh send_file_list(char *whichf)
2922ea022d16SRodney W. Grimes {
2923ea022d16SRodney W. Grimes 	struct stat st;
2924ea022d16SRodney W. Grimes 	DIR *dirp = NULL;
2925ea022d16SRodney W. Grimes 	struct dirent *dir;
2926ea022d16SRodney W. Grimes 	FILE *dout = NULL;
2927ea022d16SRodney W. Grimes 	char **dirlist, *dirname;
2928ea022d16SRodney W. Grimes 	int simple = 0;
2929ea022d16SRodney W. Grimes 	int freeglob = 0;
2930ea022d16SRodney W. Grimes 	glob_t gl;
2931ea022d16SRodney W. Grimes 
2932ea022d16SRodney W. Grimes 	if (strpbrk(whichf, "~{[*?") != NULL) {
293312da320bSMike Heffner 		int flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_TILDE;
2934ea022d16SRodney W. Grimes 
2935ea022d16SRodney W. Grimes 		memset(&gl, 0, sizeof(gl));
29366d10cb2fSJonathan Lemon 		gl.gl_matchc = MAXGLOBARGS;
293775dc5f1aSMike Heffner 		flags |= GLOB_LIMIT;
2938ea022d16SRodney W. Grimes 		freeglob = 1;
2939ea022d16SRodney W. Grimes 		if (glob(whichf, flags, 0, &gl)) {
2940ea022d16SRodney W. Grimes 			reply(550, "not found");
2941ea022d16SRodney W. Grimes 			goto out;
2942ea022d16SRodney W. Grimes 		} else if (gl.gl_pathc == 0) {
2943ea022d16SRodney W. Grimes 			errno = ENOENT;
2944ea022d16SRodney W. Grimes 			perror_reply(550, whichf);
2945ea022d16SRodney W. Grimes 			goto out;
2946ea022d16SRodney W. Grimes 		}
2947ea022d16SRodney W. Grimes 		dirlist = gl.gl_pathv;
2948ea022d16SRodney W. Grimes 	} else {
2949ea022d16SRodney W. Grimes 		onefile[0] = whichf;
2950ea022d16SRodney W. Grimes 		dirlist = onefile;
2951ea022d16SRodney W. Grimes 		simple = 1;
2952ea022d16SRodney W. Grimes 	}
2953ea022d16SRodney W. Grimes 
29549aca17cbSMark Murray 	while ((dirname = *dirlist++)) {
2955ea022d16SRodney W. Grimes 		if (stat(dirname, &st) < 0) {
2956ea022d16SRodney W. Grimes 			/*
2957ea022d16SRodney W. Grimes 			 * If user typed "ls -l", etc, and the client
2958ea022d16SRodney W. Grimes 			 * used NLST, do what the user meant.
2959ea022d16SRodney W. Grimes 			 */
2960ea022d16SRodney W. Grimes 			if (dirname[0] == '-' && *dirlist == NULL &&
2961ea022d16SRodney W. Grimes 			    transflag == 0) {
2962af85d782SDavid Nugent 				retrieve(_PATH_LS " %s", dirname);
2963ea022d16SRodney W. Grimes 				goto out;
2964ea022d16SRodney W. Grimes 			}
2965ea022d16SRodney W. Grimes 			perror_reply(550, whichf);
2966ea022d16SRodney W. Grimes 			if (dout != NULL) {
2967ea022d16SRodney W. Grimes 				(void) fclose(dout);
2968ea022d16SRodney W. Grimes 				transflag = 0;
2969ea022d16SRodney W. Grimes 				data = -1;
2970ea022d16SRodney W. Grimes 				pdata = -1;
2971ea022d16SRodney W. Grimes 			}
2972ea022d16SRodney W. Grimes 			goto out;
2973ea022d16SRodney W. Grimes 		}
2974ea022d16SRodney W. Grimes 
2975ea022d16SRodney W. Grimes 		if (S_ISREG(st.st_mode)) {
2976ea022d16SRodney W. Grimes 			if (dout == NULL) {
2977ea022d16SRodney W. Grimes 				dout = dataconn("file list", (off_t)-1, "w");
2978ea022d16SRodney W. Grimes 				if (dout == NULL)
2979ea022d16SRodney W. Grimes 					goto out;
2980ea022d16SRodney W. Grimes 				transflag++;
2981ea022d16SRodney W. Grimes 			}
2982ea022d16SRodney W. Grimes 			fprintf(dout, "%s%s\n", dirname,
2983ea022d16SRodney W. Grimes 				type == TYPE_A ? "\r" : "");
2984ea022d16SRodney W. Grimes 			byte_count += strlen(dirname) + 1;
2985ea022d16SRodney W. Grimes 			continue;
2986ea022d16SRodney W. Grimes 		} else if (!S_ISDIR(st.st_mode))
2987ea022d16SRodney W. Grimes 			continue;
2988ea022d16SRodney W. Grimes 
2989ea022d16SRodney W. Grimes 		if ((dirp = opendir(dirname)) == NULL)
2990ea022d16SRodney W. Grimes 			continue;
2991ea022d16SRodney W. Grimes 
2992ea022d16SRodney W. Grimes 		while ((dir = readdir(dirp)) != NULL) {
2993ea022d16SRodney W. Grimes 			char nbuf[MAXPATHLEN];
2994ea022d16SRodney W. Grimes 
29954b82fc95SYaroslav Tykhiy 			if (recvurg) {
29964b82fc95SYaroslav Tykhiy 				myoob();
29974b82fc95SYaroslav Tykhiy 				recvurg = 0;
29984b82fc95SYaroslav Tykhiy 				transflag = 0;
29994b82fc95SYaroslav Tykhiy 				goto out;
30004b82fc95SYaroslav Tykhiy 			}
30014b82fc95SYaroslav Tykhiy 
3002ea022d16SRodney W. Grimes 			if (dir->d_name[0] == '.' && dir->d_namlen == 1)
3003ea022d16SRodney W. Grimes 				continue;
3004ea022d16SRodney W. Grimes 			if (dir->d_name[0] == '.' && dir->d_name[1] == '.' &&
3005ea022d16SRodney W. Grimes 			    dir->d_namlen == 2)
3006ea022d16SRodney W. Grimes 				continue;
3007ea022d16SRodney W. Grimes 
3008e760ef2cSWarner Losh 			snprintf(nbuf, sizeof(nbuf),
3009e760ef2cSWarner Losh 				"%s/%s", dirname, dir->d_name);
3010ea022d16SRodney W. Grimes 
3011ea022d16SRodney W. Grimes 			/*
3012ea022d16SRodney W. Grimes 			 * We have to do a stat to insure it's
3013ea022d16SRodney W. Grimes 			 * not a directory or special file.
3014ea022d16SRodney W. Grimes 			 */
3015ea022d16SRodney W. Grimes 			if (simple || (stat(nbuf, &st) == 0 &&
3016ea022d16SRodney W. Grimes 			    S_ISREG(st.st_mode))) {
3017ea022d16SRodney W. Grimes 				if (dout == NULL) {
3018ea022d16SRodney W. Grimes 					dout = dataconn("file list", (off_t)-1,
3019ea022d16SRodney W. Grimes 						"w");
3020ea022d16SRodney W. Grimes 					if (dout == NULL)
3021ea022d16SRodney W. Grimes 						goto out;
3022ea022d16SRodney W. Grimes 					transflag++;
3023ea022d16SRodney W. Grimes 				}
3024ea022d16SRodney W. Grimes 				if (nbuf[0] == '.' && nbuf[1] == '/')
3025ea022d16SRodney W. Grimes 					fprintf(dout, "%s%s\n", &nbuf[2],
3026ea022d16SRodney W. Grimes 						type == TYPE_A ? "\r" : "");
3027ea022d16SRodney W. Grimes 				else
3028ea022d16SRodney W. Grimes 					fprintf(dout, "%s%s\n", nbuf,
3029ea022d16SRodney W. Grimes 						type == TYPE_A ? "\r" : "");
3030ea022d16SRodney W. Grimes 				byte_count += strlen(nbuf) + 1;
3031ea022d16SRodney W. Grimes 			}
3032ea022d16SRodney W. Grimes 		}
3033ea022d16SRodney W. Grimes 		(void) closedir(dirp);
3034ea022d16SRodney W. Grimes 	}
3035ea022d16SRodney W. Grimes 
3036ea022d16SRodney W. Grimes 	if (dout == NULL)
3037ea022d16SRodney W. Grimes 		reply(550, "No files found.");
3038ea022d16SRodney W. Grimes 	else if (ferror(dout) != 0)
3039ea022d16SRodney W. Grimes 		perror_reply(550, "Data connection");
3040ea022d16SRodney W. Grimes 	else
3041ea022d16SRodney W. Grimes 		reply(226, "Transfer complete.");
3042ea022d16SRodney W. Grimes 
3043ea022d16SRodney W. Grimes 	transflag = 0;
3044ea022d16SRodney W. Grimes 	if (dout != NULL)
3045ea022d16SRodney W. Grimes 		(void) fclose(dout);
3046ea022d16SRodney W. Grimes 	data = -1;
3047ea022d16SRodney W. Grimes 	pdata = -1;
3048ea022d16SRodney W. Grimes out:
3049ea022d16SRodney W. Grimes 	if (freeglob) {
3050ea022d16SRodney W. Grimes 		freeglob = 0;
3051ea022d16SRodney W. Grimes 		globfree(&gl);
3052ea022d16SRodney W. Grimes 	}
3053ea022d16SRodney W. Grimes }
3054ea022d16SRodney W. Grimes 
3055cf09a206SDavid Greenman void
3056e4bc453cSWarner Losh reapchild(int signo)
3057cf09a206SDavid Greenman {
3058cf09a206SDavid Greenman 	while (wait3(NULL, WNOHANG, NULL) > 0);
3059cf09a206SDavid Greenman }
3060cf09a206SDavid Greenman 
3061b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE
3062ea022d16SRodney W. Grimes /*
3063ea022d16SRodney W. Grimes  * Clobber argv so ps will show what we're doing.  (Stolen from sendmail.)
3064ea022d16SRodney W. Grimes  * Warning, since this is usually started from inetd.conf, it often doesn't
3065ea022d16SRodney W. Grimes  * have much of an environment or arglist to overwrite.
3066ea022d16SRodney W. Grimes  */
3067ea022d16SRodney W. Grimes void
3068ea022d16SRodney W. Grimes setproctitle(const char *fmt, ...)
3069ea022d16SRodney W. Grimes {
3070ea022d16SRodney W. Grimes 	int i;
3071ea022d16SRodney W. Grimes 	va_list ap;
3072ea022d16SRodney W. Grimes 	char *p, *bp, ch;
3073ea022d16SRodney W. Grimes 	char buf[LINE_MAX];
3074ea022d16SRodney W. Grimes 
3075ea022d16SRodney W. Grimes 	va_start(ap, fmt);
3076ea022d16SRodney W. Grimes 	(void)vsnprintf(buf, sizeof(buf), fmt, ap);
3077ea022d16SRodney W. Grimes 
3078ea022d16SRodney W. Grimes 	/* make ps print our process name */
3079ea022d16SRodney W. Grimes 	p = Argv[0];
3080ea022d16SRodney W. Grimes 	*p++ = '-';
3081ea022d16SRodney W. Grimes 
3082ea022d16SRodney W. Grimes 	i = strlen(buf);
3083ea022d16SRodney W. Grimes 	if (i > LastArgv - p - 2) {
3084ea022d16SRodney W. Grimes 		i = LastArgv - p - 2;
3085ea022d16SRodney W. Grimes 		buf[i] = '\0';
3086ea022d16SRodney W. Grimes 	}
3087ea022d16SRodney W. Grimes 	bp = buf;
3088ea022d16SRodney W. Grimes 	while (ch = *bp++)
3089ea022d16SRodney W. Grimes 		if (ch != '\n' && ch != '\r')
3090ea022d16SRodney W. Grimes 			*p++ = ch;
3091ea022d16SRodney W. Grimes 	while (p < LastArgv)
3092ea022d16SRodney W. Grimes 		*p++ = ' ';
3093ea022d16SRodney W. Grimes }
3094b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */
30953eb568f2SGuido van Rooij 
309661f891a6SPaul Traina static void
3097e4bc453cSWarner Losh logxfer(char *name, off_t size, time_t start)
30983eb568f2SGuido van Rooij {
30993eb568f2SGuido van Rooij 	char buf[1024];
31003eb568f2SGuido van Rooij 	char path[MAXPATHLEN + 1];
3101158a00b2SJohn Birrell 	time_t now;
31023eb568f2SGuido van Rooij 
31033eb568f2SGuido van Rooij 	if (statfd >= 0 && getwd(path) != NULL) {
31043eb568f2SGuido van Rooij 		time(&now);
3105e4a71114SAndrey A. Chernov 		snprintf(buf, sizeof(buf), "%.20s!%s!%s!%s/%s!%qd!%ld\n",
31063eb568f2SGuido van Rooij 			ctime(&now)+4, ident, remotehost,
3107e4a71114SAndrey A. Chernov 			path, name, (long long)size,
3108e4a71114SAndrey A. Chernov 			(long)(now - start + (now == start)));
31093eb568f2SGuido van Rooij 		write(statfd, buf, strlen(buf));
31103eb568f2SGuido van Rooij 	}
31113eb568f2SGuido van Rooij }
3112e4648f05SYaroslav Tykhiy 
3113e4648f05SYaroslav Tykhiy static char *
3114e4648f05SYaroslav Tykhiy doublequote(char *s)
3115e4648f05SYaroslav Tykhiy {
3116e4648f05SYaroslav Tykhiy 	int n;
3117e4648f05SYaroslav Tykhiy 	char *p, *s2;
3118e4648f05SYaroslav Tykhiy 
3119e4648f05SYaroslav Tykhiy 	for (p = s, n = 0; *p; p++)
3120e4648f05SYaroslav Tykhiy 		if (*p == '"')
3121e4648f05SYaroslav Tykhiy 			n++;
3122e4648f05SYaroslav Tykhiy 
3123e4648f05SYaroslav Tykhiy 	if ((s2 = malloc(p - s + n + 1)) == NULL)
3124e4648f05SYaroslav Tykhiy 		return (NULL);
3125e4648f05SYaroslav Tykhiy 
3126e4648f05SYaroslav Tykhiy 	for (p = s2; *s; s++, p++) {
3127e4648f05SYaroslav Tykhiy 		if ((*p = *s) == '"')
3128e4648f05SYaroslav Tykhiy 			*(++p) = '"';
3129e4648f05SYaroslav Tykhiy 	}
3130e4648f05SYaroslav Tykhiy 	*p = '\0';
3131e4648f05SYaroslav Tykhiy 
3132e4648f05SYaroslav Tykhiy 	return (s2);
3133e4648f05SYaroslav Tykhiy }
3134