ftpcmd.y revision 101806
11592Srgrimes/*
21592Srgrimes * Copyright (c) 1985, 1988, 1993, 1994
31592Srgrimes *	The Regents of the University of California.  All rights reserved.
41592Srgrimes *
51592Srgrimes * Redistribution and use in source and binary forms, with or without
61592Srgrimes * modification, are permitted provided that the following conditions
71592Srgrimes * are met:
81592Srgrimes * 1. Redistributions of source code must retain the above copyright
91592Srgrimes *    notice, this list of conditions and the following disclaimer.
101592Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111592Srgrimes *    notice, this list of conditions and the following disclaimer in the
121592Srgrimes *    documentation and/or other materials provided with the distribution.
131592Srgrimes * 3. All advertising materials mentioning features or use of this software
141592Srgrimes *    must display the following acknowledgement:
151592Srgrimes *	This product includes software developed by the University of
161592Srgrimes *	California, Berkeley and its contributors.
171592Srgrimes * 4. Neither the name of the University nor the names of its contributors
181592Srgrimes *    may be used to endorse or promote products derived from this software
191592Srgrimes *    without specific prior written permission.
201592Srgrimes *
211592Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221592Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231592Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241592Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251592Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261592Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271592Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281592Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291592Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301592Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311592Srgrimes * SUCH DAMAGE.
321592Srgrimes *
331592Srgrimes *	@(#)ftpcmd.y	8.3 (Berkeley) 4/6/94
341592Srgrimes */
351592Srgrimes
361592Srgrimes/*
371592Srgrimes * Grammar for FTP commands.
381592Srgrimes * See RFC 959.
391592Srgrimes */
401592Srgrimes
411592Srgrimes%{
421592Srgrimes
431592Srgrimes#ifndef lint
4431329Scharnier#if 0
451592Srgrimesstatic char sccsid[] = "@(#)ftpcmd.y	8.3 (Berkeley) 4/6/94";
4631329Scharnier#endif
4731329Scharnierstatic const char rcsid[] =
4850476Speter  "$FreeBSD: head/libexec/ftpd/ftpcmd.y 101806 2002-08-13 13:56:42Z yar $";
491592Srgrimes#endif /* not lint */
501592Srgrimes
511592Srgrimes#include <sys/param.h>
521592Srgrimes#include <sys/socket.h>
531592Srgrimes#include <sys/stat.h>
541592Srgrimes
551592Srgrimes#include <netinet/in.h>
561592Srgrimes#include <arpa/ftp.h>
571592Srgrimes
581592Srgrimes#include <ctype.h>
591592Srgrimes#include <errno.h>
601592Srgrimes#include <glob.h>
6192090Smaxim#include <libutil.h>
6292272Smaxim#include <limits.h>
6392090Smaxim#include <md5.h>
6456668Sshin#include <netdb.h>
651592Srgrimes#include <pwd.h>
661592Srgrimes#include <signal.h>
671592Srgrimes#include <stdio.h>
681592Srgrimes#include <stdlib.h>
691592Srgrimes#include <string.h>
701592Srgrimes#include <syslog.h>
711592Srgrimes#include <time.h>
721592Srgrimes#include <unistd.h>
731592Srgrimes
741592Srgrimes#include "extern.h"
751592Srgrimes
7656668Sshinextern	union sockunion data_dest, his_addr;
771592Srgrimesextern	int logged_in;
781592Srgrimesextern	struct passwd *pw;
791592Srgrimesextern	int guest;
8017435Spstextern 	int paranoid;
811592Srgrimesextern	int logging;
821592Srgrimesextern	int type;
831592Srgrimesextern	int form;
8476096Smarkmextern	int ftpdebug;
851592Srgrimesextern	int timeout;
861592Srgrimesextern	int maxtimeout;
871592Srgrimesextern  int pdata;
8827650Sdavidnextern	char *hostname;
8927650Sdavidnextern	char remotehost[];
901592Srgrimesextern	char proctitle[];
911592Srgrimesextern	int usedefault;
921592Srgrimesextern  int transflag;
931592Srgrimesextern  char tmpline[];
9470102Sphkextern	int readonly;
9570102Sphkextern	int noepsv;
9682460Snikextern	int noretr;
9782796Ssheldonhextern	int noguestretr;
98100684Syarextern	char *typenames[]; /* defined in <arpa/ftp.h> included from ftpd.c */
991592Srgrimes
1001592Srgrimesoff_t	restart_point;
1011592Srgrimes
1021592Srgrimesstatic	int cmd_type;
1031592Srgrimesstatic	int cmd_form;
1041592Srgrimesstatic	int cmd_bytesz;
10589935Syarstatic	int state;
1061592Srgrimeschar	cbuf[512];
10788935Sdwmalonechar	*fromname = (char *) 0;
1081592Srgrimes
10956668Sshinextern int epsvall;
11056668Sshin
1111592Srgrimes%}
1121592Srgrimes
1131592Srgrimes%union {
11492272Smaxim	struct {
11592272Smaxim		off_t	o;
11692272Smaxim		int	i;
11792272Smaxim	} u;
1181592Srgrimes	char   *s;
1191592Srgrimes}
1201592Srgrimes
1211592Srgrimes%token
1221592Srgrimes	A	B	C	E	F	I
1231592Srgrimes	L	N	P	R	S	T
12456668Sshin	ALL
1251592Srgrimes
1261592Srgrimes	SP	CRLF	COMMA
1271592Srgrimes
1281592Srgrimes	USER	PASS	ACCT	REIN	QUIT	PORT
1291592Srgrimes	PASV	TYPE	STRU	MODE	RETR	STOR
1301592Srgrimes	APPE	MLFL	MAIL	MSND	MSOM	MSAM
1311592Srgrimes	MRSQ	MRCP	ALLO	REST	RNFR	RNTO
1321592Srgrimes	ABOR	DELE	CWD	LIST	NLST	SITE
1331592Srgrimes	STAT	HELP	NOOP	MKD	RMD	PWD
1341592Srgrimes	CDUP	STOU	SMNT	SYST	SIZE	MDTM
13556668Sshin	LPRT	LPSV	EPRT	EPSV
1361592Srgrimes
13775535Sphk	UMASK	IDLE	CHMOD	MDFIVE
1381592Srgrimes
1391592Srgrimes	LEXERR
1401592Srgrimes
1411592Srgrimes%token	<s> STRING
14292272Smaxim%token	<u> NUMBER
1431592Srgrimes
14492272Smaxim%type	<u.i> check_login octal_number byte_size
14592272Smaxim%type	<u.i> check_login_ro check_login_epsv
14692272Smaxim%type	<u.i> struct_code mode_code type_code form_code
14775567Speter%type	<s> pathstring pathname password username
14856668Sshin%type	<s> ALL
1491592Srgrimes
1501592Srgrimes%start	cmd_list
1511592Srgrimes
1521592Srgrimes%%
1531592Srgrimes
1541592Srgrimescmd_list
1551592Srgrimes	: /* empty */
1561592Srgrimes	| cmd_list cmd
1571592Srgrimes		{
15888935Sdwmalone			if (fromname)
15988935Sdwmalone				free(fromname);
1601592Srgrimes			fromname = (char *) 0;
1611592Srgrimes			restart_point = (off_t) 0;
1621592Srgrimes		}
1631592Srgrimes	| cmd_list rcmd
1641592Srgrimes	;
1651592Srgrimes
1661592Srgrimescmd
1671592Srgrimes	: USER SP username CRLF
1681592Srgrimes		{
1691592Srgrimes			user($3);
1701592Srgrimes			free($3);
1711592Srgrimes		}
1721592Srgrimes	| PASS SP password CRLF
1731592Srgrimes		{
1741592Srgrimes			pass($3);
1751592Srgrimes			free($3);
1761592Srgrimes		}
17775556Sgreen	| PASS CRLF
17875556Sgreen		{
17975556Sgreen			pass("");
18075556Sgreen		}
18117433Spst	| PORT check_login SP host_port CRLF
1821592Srgrimes		{
18356668Sshin			if (epsvall) {
18456668Sshin				reply(501, "no PORT allowed after EPSV ALL");
18556668Sshin				goto port_done;
18656668Sshin			}
18756668Sshin			if (!$2)
18856668Sshin				goto port_done;
18956668Sshin			if (port_check("PORT") == 1)
19056668Sshin				goto port_done;
19156668Sshin#ifdef INET6
19256668Sshin			if ((his_addr.su_family != AF_INET6 ||
19356668Sshin			     !IN6_IS_ADDR_V4MAPPED(&his_addr.su_sin6.sin6_addr))) {
19456668Sshin				/* shoud never happen */
19556668Sshin				usedefault = 1;
19656668Sshin				reply(500, "Invalid address rejected.");
19756668Sshin				goto port_done;
19856668Sshin			}
19956668Sshin			port_check_v6("pcmd");
20056668Sshin#endif
20156668Sshin		port_done:
20256668Sshin		}
20356668Sshin	| LPRT check_login SP host_long_port CRLF
20456668Sshin		{
20556668Sshin			if (epsvall) {
20656668Sshin				reply(501, "no LPRT allowed after EPSV ALL");
20756668Sshin				goto lprt_done;
20856668Sshin			}
20956668Sshin			if (!$2)
21056668Sshin				goto lprt_done;
21156668Sshin			if (port_check("LPRT") == 1)
21256668Sshin				goto lprt_done;
21356668Sshin#ifdef INET6
21456668Sshin			if (his_addr.su_family != AF_INET6) {
21556668Sshin				usedefault = 1;
21656668Sshin				reply(500, "Invalid address rejected.");
21756668Sshin				goto lprt_done;
21856668Sshin			}
21956668Sshin			if (port_check_v6("LPRT") == 1)
22056668Sshin				goto lprt_done;
22156668Sshin#endif
22256668Sshin		lprt_done:
22356668Sshin		}
22456668Sshin	| EPRT check_login SP STRING CRLF
22556668Sshin		{
22656668Sshin			char delim;
22756668Sshin			char *tmp = NULL;
22856668Sshin			char *p, *q;
22956668Sshin			char *result[3];
23056668Sshin			struct addrinfo hints;
23156668Sshin			struct addrinfo *res;
23256668Sshin			int i;
23356668Sshin
23456668Sshin			if (epsvall) {
23556668Sshin				reply(501, "no EPRT allowed after EPSV ALL");
23656668Sshin				goto eprt_done;
23756668Sshin			}
23856668Sshin			if (!$2)
23956668Sshin				goto eprt_done;
24056668Sshin
24156668Sshin			memset(&data_dest, 0, sizeof(data_dest));
24256668Sshin			tmp = strdup($4);
24376096Smarkm			if (ftpdebug)
24456668Sshin				syslog(LOG_DEBUG, "%s", tmp);
24556668Sshin			if (!tmp) {
24676096Smarkm				fatalerror("not enough core");
24756668Sshin				/*NOTREACHED*/
24856668Sshin			}
24956668Sshin			p = tmp;
25056668Sshin			delim = p[0];
25156668Sshin			p++;
25256668Sshin			memset(result, 0, sizeof(result));
25356668Sshin			for (i = 0; i < 3; i++) {
25456668Sshin				q = strchr(p, delim);
25556668Sshin				if (!q || *q != delim) {
25656668Sshin		parsefail:
25756668Sshin					reply(500,
25856668Sshin						"Invalid argument, rejected.");
25956668Sshin					if (tmp)
26056668Sshin						free(tmp);
26117433Spst					usedefault = 1;
26256668Sshin					goto eprt_done;
26317433Spst				}
26456668Sshin				*q++ = '\0';
26556668Sshin				result[i] = p;
26676096Smarkm				if (ftpdebug)
26756668Sshin					syslog(LOG_DEBUG, "%d: %s", i, p);
26856668Sshin				p = q;
2691592Srgrimes			}
27056668Sshin
27156668Sshin			/* some more sanity check */
27256668Sshin			p = result[0];
27356668Sshin			while (*p) {
27456668Sshin				if (!isdigit(*p))
27556668Sshin					goto parsefail;
27656668Sshin				p++;
27756668Sshin			}
27856668Sshin			p = result[2];
27956668Sshin			while (*p) {
28056668Sshin				if (!isdigit(*p))
28156668Sshin					goto parsefail;
28256668Sshin				p++;
28356668Sshin			}
28456668Sshin
28556668Sshin			/* grab address */
28656668Sshin			memset(&hints, 0, sizeof(hints));
28756668Sshin			if (atoi(result[0]) == 1)
28856668Sshin				hints.ai_family = PF_INET;
28956668Sshin#ifdef INET6
29056668Sshin			else if (atoi(result[0]) == 2)
29156668Sshin				hints.ai_family = PF_INET6;
29256668Sshin#endif
29356668Sshin			else
29456668Sshin				hints.ai_family = PF_UNSPEC;	/*XXX*/
29556668Sshin			hints.ai_socktype = SOCK_STREAM;
29656668Sshin			i = getaddrinfo(result[1], result[2], &hints, &res);
29756668Sshin			if (i)
29856668Sshin				goto parsefail;
29956668Sshin			memcpy(&data_dest, res->ai_addr, res->ai_addrlen);
30056668Sshin#ifdef INET6
30156668Sshin			if (his_addr.su_family == AF_INET6
30256668Sshin			    && data_dest.su_family == AF_INET6) {
30356668Sshin				/* XXX more sanity checks! */
30456668Sshin				data_dest.su_sin6.sin6_scope_id =
30556668Sshin					his_addr.su_sin6.sin6_scope_id;
30656668Sshin			}
30756668Sshin#endif
30856668Sshin			free(tmp);
30956668Sshin			tmp = NULL;
31056668Sshin
31156668Sshin			if (port_check("EPRT") == 1)
31256668Sshin				goto eprt_done;
31356668Sshin#ifdef INET6
31456668Sshin			if (his_addr.su_family != AF_INET6) {
31556668Sshin				usedefault = 1;
31656668Sshin				reply(500, "Invalid address rejected.");
31756668Sshin				goto eprt_done;
31856668Sshin			}
31956668Sshin			if (port_check_v6("EPRT") == 1)
32056668Sshin				goto eprt_done;
32156668Sshin#endif
32288935Sdwmalone		eprt_done:
32388935Sdwmalone			free($4);
3241592Srgrimes		}
32517433Spst	| PASV check_login CRLF
3261592Srgrimes		{
32756668Sshin			if (epsvall)
32856668Sshin				reply(501, "no PASV allowed after EPSV ALL");
32956668Sshin			else if ($2)
33017433Spst				passive();
3311592Srgrimes		}
33256668Sshin	| LPSV check_login CRLF
33356668Sshin		{
33456668Sshin			if (epsvall)
33556668Sshin				reply(501, "no LPSV allowed after EPSV ALL");
33656668Sshin			else if ($2)
33756668Sshin				long_passive("LPSV", PF_UNSPEC);
33856668Sshin		}
33970102Sphk	| EPSV check_login_epsv SP NUMBER CRLF
34056668Sshin		{
34156668Sshin			if ($2) {
34256668Sshin				int pf;
34392272Smaxim				switch ($4.i) {
34456668Sshin				case 1:
34556668Sshin					pf = PF_INET;
34656668Sshin					break;
34756668Sshin#ifdef INET6
34856668Sshin				case 2:
34956668Sshin					pf = PF_INET6;
35056668Sshin					break;
35156668Sshin#endif
35256668Sshin				default:
35356668Sshin					pf = -1;	/*junk value*/
35456668Sshin					break;
35556668Sshin				}
35656668Sshin				long_passive("EPSV", pf);
35756668Sshin			}
35856668Sshin		}
35970102Sphk	| EPSV check_login_epsv SP ALL CRLF
36056668Sshin		{
36156668Sshin			if ($2) {
36256668Sshin				reply(200,
36356668Sshin				      "EPSV ALL command successful.");
36456668Sshin				epsvall++;
36556668Sshin			}
36656668Sshin		}
36770102Sphk	| EPSV check_login_epsv CRLF
36856668Sshin		{
36956668Sshin			if ($2)
37056668Sshin				long_passive("EPSV", PF_UNSPEC);
37156668Sshin		}
37271278Sjedgar	| TYPE check_login SP type_code CRLF
3731592Srgrimes		{
37471278Sjedgar			if ($2) {
37571278Sjedgar				switch (cmd_type) {
3761592Srgrimes
37771278Sjedgar				case TYPE_A:
37871278Sjedgar					if (cmd_form == FORM_N) {
37971278Sjedgar						reply(200, "Type set to A.");
38071278Sjedgar						type = cmd_type;
38171278Sjedgar						form = cmd_form;
38271278Sjedgar					} else
38371278Sjedgar						reply(504, "Form must be N.");
38471278Sjedgar					break;
3851592Srgrimes
38671278Sjedgar				case TYPE_E:
38771278Sjedgar					reply(504, "Type E not implemented.");
38871278Sjedgar					break;
3891592Srgrimes
39071278Sjedgar				case TYPE_I:
39171278Sjedgar					reply(200, "Type set to I.");
39271278Sjedgar					type = cmd_type;
39371278Sjedgar					break;
3941592Srgrimes
39571278Sjedgar				case TYPE_L:
3961592Srgrimes#if NBBY == 8
39771278Sjedgar					if (cmd_bytesz == 8) {
39871278Sjedgar						reply(200,
39971278Sjedgar						    "Type set to L (byte size 8).");
40071278Sjedgar						type = cmd_type;
40171278Sjedgar					} else
40271278Sjedgar						reply(504, "Byte size must be 8.");
4031592Srgrimes#else /* NBBY == 8 */
40471278Sjedgar					UNIMPLEMENTED for NBBY != 8
4051592Srgrimes#endif /* NBBY == 8 */
40671278Sjedgar				}
4071592Srgrimes			}
4081592Srgrimes		}
40971278Sjedgar	| STRU check_login SP struct_code CRLF
4101592Srgrimes		{
41171278Sjedgar			if ($2) {
41271278Sjedgar				switch ($4) {
4131592Srgrimes
41471278Sjedgar				case STRU_F:
41571278Sjedgar					reply(200, "STRU F ok.");
41671278Sjedgar					break;
4171592Srgrimes
41871278Sjedgar				default:
41971278Sjedgar					reply(504, "Unimplemented STRU type.");
42071278Sjedgar				}
4211592Srgrimes			}
4221592Srgrimes		}
42371278Sjedgar	| MODE check_login SP mode_code CRLF
4241592Srgrimes		{
42571278Sjedgar			if ($2) {
42671278Sjedgar				switch ($4) {
4271592Srgrimes
42871278Sjedgar				case MODE_S:
42971278Sjedgar					reply(200, "MODE S ok.");
43071278Sjedgar					break;
43171278Sjedgar
43271278Sjedgar				default:
43371278Sjedgar					reply(502, "Unimplemented MODE type.");
43471278Sjedgar				}
4351592Srgrimes			}
4361592Srgrimes		}
43771278Sjedgar	| ALLO check_login SP NUMBER CRLF
4381592Srgrimes		{
43971278Sjedgar			if ($2) {
44071278Sjedgar				reply(202, "ALLO command ignored.");
44171278Sjedgar			}
4421592Srgrimes		}
44371278Sjedgar	| ALLO check_login SP NUMBER SP R SP NUMBER CRLF
4441592Srgrimes		{
44571278Sjedgar			if ($2) {
44671278Sjedgar				reply(202, "ALLO command ignored.");
44771278Sjedgar			}
4481592Srgrimes		}
4491592Srgrimes	| RETR check_login SP pathname CRLF
4501592Srgrimes		{
45182796Ssheldonh			if (noretr || (guest && noguestretr))
45282460Snik				reply(500, "RETR command is disabled");
45382460Snik			else if ($2 && $4 != NULL)
4541592Srgrimes				retrieve((char *) 0, $4);
45582460Snik
4561592Srgrimes			if ($4 != NULL)
4571592Srgrimes				free($4);
4581592Srgrimes		}
45970102Sphk	| STOR check_login_ro SP pathname CRLF
4601592Srgrimes		{
4611592Srgrimes			if ($2 && $4 != NULL)
4621592Srgrimes				store($4, "w", 0);
4631592Srgrimes			if ($4 != NULL)
4641592Srgrimes				free($4);
4651592Srgrimes		}
46670102Sphk	| APPE check_login_ro SP pathname CRLF
4671592Srgrimes		{
4681592Srgrimes			if ($2 && $4 != NULL)
4691592Srgrimes				store($4, "a", 0);
4701592Srgrimes			if ($4 != NULL)
4711592Srgrimes				free($4);
4721592Srgrimes		}
4731592Srgrimes	| NLST check_login CRLF
4741592Srgrimes		{
4751592Srgrimes			if ($2)
4761592Srgrimes				send_file_list(".");
4771592Srgrimes		}
478101395Syar	| NLST check_login SP pathstring CRLF
4791592Srgrimes		{
480101395Syar			if ($2)
4811592Srgrimes				send_file_list($4);
482101395Syar			free($4);
4831592Srgrimes		}
4841592Srgrimes	| LIST check_login CRLF
4851592Srgrimes		{
4861592Srgrimes			if ($2)
4871592Srgrimes				retrieve("/bin/ls -lgA", "");
4881592Srgrimes		}
48975567Speter	| LIST check_login SP pathstring CRLF
4901592Srgrimes		{
491101395Syar			if ($2)
4921592Srgrimes				retrieve("/bin/ls -lgA %s", $4);
493101395Syar			free($4);
4941592Srgrimes		}
4951592Srgrimes	| STAT check_login SP pathname CRLF
4961592Srgrimes		{
4971592Srgrimes			if ($2 && $4 != NULL)
4981592Srgrimes				statfilecmd($4);
4991592Srgrimes			if ($4 != NULL)
5001592Srgrimes				free($4);
5011592Srgrimes		}
50271278Sjedgar	| STAT check_login CRLF
5031592Srgrimes		{
50471278Sjedgar			if ($2) {
50571278Sjedgar				statcmd();
50671278Sjedgar			}
5071592Srgrimes		}
50870102Sphk	| DELE check_login_ro SP pathname CRLF
5091592Srgrimes		{
5101592Srgrimes			if ($2 && $4 != NULL)
5111592Srgrimes				delete($4);
5121592Srgrimes			if ($4 != NULL)
5131592Srgrimes				free($4);
5141592Srgrimes		}
51570102Sphk	| RNTO check_login_ro SP pathname CRLF
5161592Srgrimes		{
517101379Syar			if ($2 && $4 != NULL) {
51817433Spst				if (fromname) {
51917433Spst					renamecmd(fromname, $4);
52017433Spst					free(fromname);
52117433Spst					fromname = (char *) 0;
52217433Spst				} else {
52317433Spst					reply(503, "Bad sequence of commands.");
52417433Spst				}
5251592Srgrimes			}
526101379Syar			if ($4 != NULL)
527101379Syar				free($4);
5281592Srgrimes		}
52971278Sjedgar	| ABOR check_login CRLF
5301592Srgrimes		{
53171278Sjedgar			if ($2)
53271278Sjedgar				reply(225, "ABOR command successful.");
5331592Srgrimes		}
5341592Srgrimes	| CWD check_login CRLF
5351592Srgrimes		{
53669234Sdanny			if ($2) {
53769234Sdanny				if (guest)
53869234Sdanny					cwd("/");
53969234Sdanny				else
54069234Sdanny					cwd(pw->pw_dir);
54169234Sdanny			}
5421592Srgrimes		}
5431592Srgrimes	| CWD check_login SP pathname CRLF
5441592Srgrimes		{
5451592Srgrimes			if ($2 && $4 != NULL)
5461592Srgrimes				cwd($4);
5471592Srgrimes			if ($4 != NULL)
5481592Srgrimes				free($4);
5491592Srgrimes		}
5501592Srgrimes	| HELP CRLF
5511592Srgrimes		{
5521592Srgrimes			help(cmdtab, (char *) 0);
5531592Srgrimes		}
5541592Srgrimes	| HELP SP STRING CRLF
5551592Srgrimes		{
5561592Srgrimes			char *cp = $3;
5571592Srgrimes
5581592Srgrimes			if (strncasecmp(cp, "SITE", 4) == 0) {
5591592Srgrimes				cp = $3 + 4;
5601592Srgrimes				if (*cp == ' ')
5611592Srgrimes					cp++;
5621592Srgrimes				if (*cp)
5631592Srgrimes					help(sitetab, cp);
5641592Srgrimes				else
5651592Srgrimes					help(sitetab, (char *) 0);
5661592Srgrimes			} else
5671592Srgrimes				help(cmdtab, $3);
56888935Sdwmalone			free($3);
5691592Srgrimes		}
5701592Srgrimes	| NOOP CRLF
5711592Srgrimes		{
5721592Srgrimes			reply(200, "NOOP command successful.");
5731592Srgrimes		}
57470102Sphk	| MKD check_login_ro SP pathname CRLF
5751592Srgrimes		{
5761592Srgrimes			if ($2 && $4 != NULL)
5771592Srgrimes				makedir($4);
5781592Srgrimes			if ($4 != NULL)
5791592Srgrimes				free($4);
5801592Srgrimes		}
58170102Sphk	| RMD check_login_ro SP pathname CRLF
5821592Srgrimes		{
5831592Srgrimes			if ($2 && $4 != NULL)
5841592Srgrimes				removedir($4);
5851592Srgrimes			if ($4 != NULL)
5861592Srgrimes				free($4);
5871592Srgrimes		}
5881592Srgrimes	| PWD check_login CRLF
5891592Srgrimes		{
5901592Srgrimes			if ($2)
5911592Srgrimes				pwd();
5921592Srgrimes		}
5931592Srgrimes	| CDUP check_login CRLF
5941592Srgrimes		{
5951592Srgrimes			if ($2)
5961592Srgrimes				cwd("..");
5971592Srgrimes		}
5981592Srgrimes	| SITE SP HELP CRLF
5991592Srgrimes		{
6001592Srgrimes			help(sitetab, (char *) 0);
6011592Srgrimes		}
6021592Srgrimes	| SITE SP HELP SP STRING CRLF
6031592Srgrimes		{
6041592Srgrimes			help(sitetab, $5);
60588935Sdwmalone			free($5);
6061592Srgrimes		}
60775535Sphk	| SITE SP MDFIVE check_login SP pathname CRLF
60875535Sphk		{
60975535Sphk			char p[64], *q;
61075535Sphk
611101379Syar			if ($4 && $6) {
61275535Sphk				q = MD5File($6, p);
61375535Sphk				if (q != NULL)
61475535Sphk					reply(200, "MD5(%s) = %s", $6, p);
61575535Sphk				else
61675535Sphk					perror_reply(550, $6);
61775535Sphk			}
61888935Sdwmalone			if ($6)
61988935Sdwmalone				free($6);
62075535Sphk		}
6211592Srgrimes	| SITE SP UMASK check_login CRLF
6221592Srgrimes		{
6231592Srgrimes			int oldmask;
6241592Srgrimes
6251592Srgrimes			if ($4) {
6261592Srgrimes				oldmask = umask(0);
6271592Srgrimes				(void) umask(oldmask);
6281592Srgrimes				reply(200, "Current UMASK is %03o", oldmask);
6291592Srgrimes			}
6301592Srgrimes		}
6311592Srgrimes	| SITE SP UMASK check_login SP octal_number CRLF
6321592Srgrimes		{
6331592Srgrimes			int oldmask;
6341592Srgrimes
6351592Srgrimes			if ($4) {
6361592Srgrimes				if (($6 == -1) || ($6 > 0777)) {
6371592Srgrimes					reply(501, "Bad UMASK value");
6381592Srgrimes				} else {
6391592Srgrimes					oldmask = umask($6);
6401592Srgrimes					reply(200,
6411592Srgrimes					    "UMASK set to %03o (was %03o)",
6421592Srgrimes					    $6, oldmask);
6431592Srgrimes				}
6441592Srgrimes			}
6451592Srgrimes		}
64670102Sphk	| SITE SP CHMOD check_login_ro SP octal_number SP pathname CRLF
6471592Srgrimes		{
6481592Srgrimes			if ($4 && ($8 != NULL)) {
649101378Syar				if (($6 == -1 ) || ($6 > 0777))
650101378Syar					reply(501, "Bad mode value");
6511592Srgrimes				else if (chmod($8, $6) < 0)
6521592Srgrimes					perror_reply(550, $8);
6531592Srgrimes				else
6541592Srgrimes					reply(200, "CHMOD command successful.");
6551592Srgrimes			}
6561592Srgrimes			if ($8 != NULL)
6571592Srgrimes				free($8);
6581592Srgrimes		}
65971278Sjedgar	| SITE SP check_login IDLE CRLF
6601592Srgrimes		{
66171278Sjedgar			if ($3)
66271278Sjedgar				reply(200,
66371278Sjedgar			    	    "Current IDLE time limit is %d seconds; max %d",
66471278Sjedgar				    timeout, maxtimeout);
6651592Srgrimes		}
66671278Sjedgar	| SITE SP check_login IDLE SP NUMBER CRLF
6671592Srgrimes		{
66871278Sjedgar			if ($3) {
66992272Smaxim				if ($6.i < 30 || $6.i > maxtimeout) {
67071278Sjedgar					reply(501,
67171278Sjedgar					    "Maximum IDLE time must be between 30 and %d seconds",
67271278Sjedgar					    maxtimeout);
67371278Sjedgar				} else {
67492272Smaxim					timeout = $6.i;
67571278Sjedgar					(void) alarm((unsigned) timeout);
67671278Sjedgar					reply(200,
67771278Sjedgar					    "Maximum IDLE time set to %d seconds",
67871278Sjedgar					    timeout);
67971278Sjedgar				}
6801592Srgrimes			}
6811592Srgrimes		}
68270102Sphk	| STOU check_login_ro SP pathname CRLF
6831592Srgrimes		{
6841592Srgrimes			if ($2 && $4 != NULL)
6851592Srgrimes				store($4, "w", 1);
6861592Srgrimes			if ($4 != NULL)
6871592Srgrimes				free($4);
6881592Srgrimes		}
68971278Sjedgar	| SYST check_login CRLF
6901592Srgrimes		{
69171278Sjedgar			if ($2)
6921592Srgrimes#ifdef unix
6931592Srgrimes#ifdef BSD
6941592Srgrimes			reply(215, "UNIX Type: L%d Version: BSD-%d",
6951592Srgrimes				NBBY, BSD);
6961592Srgrimes#else /* BSD */
6971592Srgrimes			reply(215, "UNIX Type: L%d", NBBY);
6981592Srgrimes#endif /* BSD */
6991592Srgrimes#else /* unix */
7001592Srgrimes			reply(215, "UNKNOWN Type: L%d", NBBY);
7011592Srgrimes#endif /* unix */
7021592Srgrimes		}
7031592Srgrimes
7041592Srgrimes		/*
7051592Srgrimes		 * SIZE is not in RFC959, but Postel has blessed it and
7061592Srgrimes		 * it will be in the updated RFC.
7071592Srgrimes		 *
7081592Srgrimes		 * Return size of file in a format suitable for
7091592Srgrimes		 * using with RESTART (we just count bytes).
7101592Srgrimes		 */
7111592Srgrimes	| SIZE check_login SP pathname CRLF
7121592Srgrimes		{
7131592Srgrimes			if ($2 && $4 != NULL)
7141592Srgrimes				sizecmd($4);
7151592Srgrimes			if ($4 != NULL)
7161592Srgrimes				free($4);
7171592Srgrimes		}
7181592Srgrimes
7191592Srgrimes		/*
7201592Srgrimes		 * MDTM is not in RFC959, but Postel has blessed it and
7211592Srgrimes		 * it will be in the updated RFC.
7221592Srgrimes		 *
7231592Srgrimes		 * Return modification time of file as an ISO 3307
7241592Srgrimes		 * style time. E.g. YYYYMMDDHHMMSS or YYYYMMDDHHMMSS.xxx
7251592Srgrimes		 * where xxx is the fractional second (of any precision,
7261592Srgrimes		 * not necessarily 3 digits)
7271592Srgrimes		 */
7281592Srgrimes	| MDTM check_login SP pathname CRLF
7291592Srgrimes		{
7301592Srgrimes			if ($2 && $4 != NULL) {
7311592Srgrimes				struct stat stbuf;
7321592Srgrimes				if (stat($4, &stbuf) < 0)
7331592Srgrimes					reply(550, "%s: %s",
7341592Srgrimes					    $4, strerror(errno));
7351592Srgrimes				else if (!S_ISREG(stbuf.st_mode)) {
7361592Srgrimes					reply(550, "%s: not a plain file.", $4);
7371592Srgrimes				} else {
7381592Srgrimes					struct tm *t;
7391592Srgrimes					t = gmtime(&stbuf.st_mtime);
7401592Srgrimes					reply(213,
74117435Spst					    "%04d%02d%02d%02d%02d%02d",
74217435Spst					    1900 + t->tm_year,
74317435Spst					    t->tm_mon+1, t->tm_mday,
7441592Srgrimes					    t->tm_hour, t->tm_min, t->tm_sec);
7451592Srgrimes				}
7461592Srgrimes			}
7471592Srgrimes			if ($4 != NULL)
7481592Srgrimes				free($4);
7491592Srgrimes		}
7501592Srgrimes	| QUIT CRLF
7511592Srgrimes		{
7521592Srgrimes			reply(221, "Goodbye.");
7531592Srgrimes			dologout(0);
7541592Srgrimes		}
75589935Syar	| error
7561592Srgrimes		{
75789935Syar			yyclearin;		/* discard lookahead data */
75889935Syar			yyerrok;		/* clear error condition */
75989935Syar			state = 0;		/* reset lexer state */
7601592Srgrimes		}
7611592Srgrimes	;
7621592Srgrimesrcmd
76370102Sphk	: RNFR check_login_ro SP pathname CRLF
7641592Srgrimes		{
7651592Srgrimes			restart_point = (off_t) 0;
7661592Srgrimes			if ($2 && $4) {
76788935Sdwmalone				if (fromname)
76888935Sdwmalone					free(fromname);
76988935Sdwmalone				fromname = (char *) 0;
77088935Sdwmalone				if (renamefrom($4))
77188935Sdwmalone					fromname = $4;
77288935Sdwmalone				else
7731592Srgrimes					free($4);
77488935Sdwmalone			} else if ($4) {
77588935Sdwmalone				free($4);
7761592Srgrimes			}
7771592Srgrimes		}
77892272Smaxim	| REST check_login SP NUMBER CRLF
7791592Srgrimes		{
78071278Sjedgar			if ($2) {
78188935Sdwmalone				if (fromname)
78288935Sdwmalone					free(fromname);
78371278Sjedgar				fromname = (char *) 0;
78492272Smaxim				restart_point = $4.o;
78592272Smaxim				reply(350, "Restarting at %llu. %s",
78671278Sjedgar				    restart_point,
78771278Sjedgar				    "Send STORE or RETRIEVE to initiate transfer.");
78871278Sjedgar			}
7891592Srgrimes		}
7901592Srgrimes	;
7911592Srgrimes
7921592Srgrimesusername
7931592Srgrimes	: STRING
7941592Srgrimes	;
7951592Srgrimes
7961592Srgrimespassword
7971592Srgrimes	: /* empty */
7981592Srgrimes		{
7991592Srgrimes			$$ = (char *)calloc(1, sizeof(char));
8001592Srgrimes		}
8011592Srgrimes	| STRING
8021592Srgrimes	;
8031592Srgrimes
8041592Srgrimesbyte_size
8051592Srgrimes	: NUMBER
80692272Smaxim		{
80792272Smaxim			$$ = $1.i;
80892272Smaxim		}
8091592Srgrimes	;
8101592Srgrimes
8111592Srgrimeshost_port
8121592Srgrimes	: NUMBER COMMA NUMBER COMMA NUMBER COMMA NUMBER COMMA
8131592Srgrimes		NUMBER COMMA NUMBER
8141592Srgrimes		{
8151592Srgrimes			char *a, *p;
8161592Srgrimes
81756668Sshin			data_dest.su_len = sizeof(struct sockaddr_in);
81856668Sshin			data_dest.su_family = AF_INET;
81956668Sshin			p = (char *)&data_dest.su_sin.sin_port;
82092272Smaxim			p[0] = $9.i; p[1] = $11.i;
82156668Sshin			a = (char *)&data_dest.su_sin.sin_addr;
82292272Smaxim			a[0] = $1.i; a[1] = $3.i; a[2] = $5.i; a[3] = $7.i;
8231592Srgrimes		}
8241592Srgrimes	;
8251592Srgrimes
82656668Sshinhost_long_port
82756668Sshin	: NUMBER COMMA NUMBER COMMA NUMBER COMMA NUMBER COMMA
82856668Sshin		NUMBER COMMA NUMBER COMMA NUMBER COMMA NUMBER COMMA
82956668Sshin		NUMBER COMMA NUMBER COMMA NUMBER COMMA NUMBER COMMA
83056668Sshin		NUMBER COMMA NUMBER COMMA NUMBER COMMA NUMBER COMMA
83156668Sshin		NUMBER COMMA NUMBER COMMA NUMBER COMMA NUMBER COMMA
83256668Sshin		NUMBER
83356668Sshin		{
83456668Sshin			char *a, *p;
83556668Sshin
83656668Sshin			memset(&data_dest, 0, sizeof(data_dest));
83756668Sshin			data_dest.su_len = sizeof(struct sockaddr_in6);
83856668Sshin			data_dest.su_family = AF_INET6;
83956668Sshin			p = (char *)&data_dest.su_port;
84092272Smaxim			p[0] = $39.i; p[1] = $41.i;
84156668Sshin			a = (char *)&data_dest.su_sin6.sin6_addr;
84292272Smaxim			a[0] = $5.i; a[1] = $7.i; a[2] = $9.i; a[3] = $11.i;
84392272Smaxim			a[4] = $13.i; a[5] = $15.i; a[6] = $17.i; a[7] = $19.i;
84492272Smaxim			a[8] = $21.i; a[9] = $23.i; a[10] = $25.i; a[11] = $27.i;
84592272Smaxim			a[12] = $29.i; a[13] = $31.i; a[14] = $33.i; a[15] = $35.i;
84656668Sshin			if (his_addr.su_family == AF_INET6) {
84756668Sshin				/* XXX more sanity checks! */
84856668Sshin				data_dest.su_sin6.sin6_scope_id =
84956668Sshin					his_addr.su_sin6.sin6_scope_id;
85056668Sshin			}
85192272Smaxim			if ($1.i != 6 || $3.i != 16 || $37.i != 2)
85256668Sshin				memset(&data_dest, 0, sizeof(data_dest));
85356668Sshin		}
85456668Sshin	| NUMBER COMMA NUMBER COMMA NUMBER COMMA NUMBER COMMA
85556668Sshin		NUMBER COMMA NUMBER COMMA NUMBER COMMA NUMBER COMMA
85656668Sshin		NUMBER
85756668Sshin		{
85856668Sshin			char *a, *p;
85956668Sshin
86056668Sshin			memset(&data_dest, 0, sizeof(data_dest));
86156668Sshin			data_dest.su_sin.sin_len = sizeof(struct sockaddr_in);
86256668Sshin			data_dest.su_family = AF_INET;
86356668Sshin			p = (char *)&data_dest.su_port;
86492272Smaxim			p[0] = $15.i; p[1] = $17.i;
86556668Sshin			a = (char *)&data_dest.su_sin.sin_addr;
86692272Smaxim			a[0] =  $5.i; a[1] = $7.i; a[2] = $9.i; a[3] = $11.i;
86792272Smaxim			if ($1.i != 4 || $3.i != 4 || $13.i != 2)
86856668Sshin				memset(&data_dest, 0, sizeof(data_dest));
86956668Sshin		}
87056668Sshin	;
87156668Sshin
8721592Srgrimesform_code
8731592Srgrimes	: N
8741592Srgrimes		{
8751592Srgrimes			$$ = FORM_N;
8761592Srgrimes		}
8771592Srgrimes	| T
8781592Srgrimes		{
8791592Srgrimes			$$ = FORM_T;
8801592Srgrimes		}
8811592Srgrimes	| C
8821592Srgrimes		{
8831592Srgrimes			$$ = FORM_C;
8841592Srgrimes		}
8851592Srgrimes	;
8861592Srgrimes
8871592Srgrimestype_code
8881592Srgrimes	: A
8891592Srgrimes		{
8901592Srgrimes			cmd_type = TYPE_A;
8911592Srgrimes			cmd_form = FORM_N;
8921592Srgrimes		}
8931592Srgrimes	| A SP form_code
8941592Srgrimes		{
8951592Srgrimes			cmd_type = TYPE_A;
8961592Srgrimes			cmd_form = $3;
8971592Srgrimes		}
8981592Srgrimes	| E
8991592Srgrimes		{
9001592Srgrimes			cmd_type = TYPE_E;
9011592Srgrimes			cmd_form = FORM_N;
9021592Srgrimes		}
9031592Srgrimes	| E SP form_code
9041592Srgrimes		{
9051592Srgrimes			cmd_type = TYPE_E;
9061592Srgrimes			cmd_form = $3;
9071592Srgrimes		}
9081592Srgrimes	| I
9091592Srgrimes		{
9101592Srgrimes			cmd_type = TYPE_I;
9111592Srgrimes		}
9121592Srgrimes	| L
9131592Srgrimes		{
9141592Srgrimes			cmd_type = TYPE_L;
9151592Srgrimes			cmd_bytesz = NBBY;
9161592Srgrimes		}
9171592Srgrimes	| L SP byte_size
9181592Srgrimes		{
9191592Srgrimes			cmd_type = TYPE_L;
9201592Srgrimes			cmd_bytesz = $3;
9211592Srgrimes		}
9221592Srgrimes		/* this is for a bug in the BBN ftp */
9231592Srgrimes	| L byte_size
9241592Srgrimes		{
9251592Srgrimes			cmd_type = TYPE_L;
9261592Srgrimes			cmd_bytesz = $2;
9271592Srgrimes		}
9281592Srgrimes	;
9291592Srgrimes
9301592Srgrimesstruct_code
9311592Srgrimes	: F
9321592Srgrimes		{
9331592Srgrimes			$$ = STRU_F;
9341592Srgrimes		}
9351592Srgrimes	| R
9361592Srgrimes		{
9371592Srgrimes			$$ = STRU_R;
9381592Srgrimes		}
9391592Srgrimes	| P
9401592Srgrimes		{
9411592Srgrimes			$$ = STRU_P;
9421592Srgrimes		}
9431592Srgrimes	;
9441592Srgrimes
9451592Srgrimesmode_code
9461592Srgrimes	: S
9471592Srgrimes		{
9481592Srgrimes			$$ = MODE_S;
9491592Srgrimes		}
9501592Srgrimes	| B
9511592Srgrimes		{
9521592Srgrimes			$$ = MODE_B;
9531592Srgrimes		}
9541592Srgrimes	| C
9551592Srgrimes		{
9561592Srgrimes			$$ = MODE_C;
9571592Srgrimes		}
9581592Srgrimes	;
9591592Srgrimes
9601592Srgrimespathname
9611592Srgrimes	: pathstring
9621592Srgrimes		{
9631592Srgrimes			/*
9641592Srgrimes			 * Problem: this production is used for all pathname
9651592Srgrimes			 * processing, but only gives a 550 error reply.
9661592Srgrimes			 * This is a valid reply in some cases but not in others.
9671592Srgrimes			 */
96875567Speter			if (logged_in && $1) {
9691592Srgrimes				glob_t gl;
9701592Srgrimes				int flags =
971100222Smikeh				 GLOB_BRACE|GLOB_NOCHECK|GLOB_TILDE;
9721592Srgrimes
9731592Srgrimes				memset(&gl, 0, sizeof(gl));
97475560Sjedgar				flags |= GLOB_MAXPATH;
97575560Sjedgar				gl.gl_matchc = MAXGLOBARGS;
9761592Srgrimes				if (glob($1, flags, NULL, &gl) ||
9771592Srgrimes				    gl.gl_pathc == 0) {
978101380Syar					reply(550, "wildcard expansion error");
9791592Srgrimes					$$ = NULL;
98075567Speter				} else if (gl.gl_pathc > 1) {
98175567Speter					reply(550, "ambiguous");
98275567Speter					$$ = NULL;
9831592Srgrimes				} else {
9841592Srgrimes					$$ = strdup(gl.gl_pathv[0]);
9851592Srgrimes				}
9861592Srgrimes				globfree(&gl);
9871592Srgrimes				free($1);
9881592Srgrimes			} else
9891592Srgrimes				$$ = $1;
9901592Srgrimes		}
9911592Srgrimes	;
9921592Srgrimes
9931592Srgrimespathstring
9941592Srgrimes	: STRING
9951592Srgrimes	;
9961592Srgrimes
9971592Srgrimesoctal_number
9981592Srgrimes	: NUMBER
9991592Srgrimes		{
10001592Srgrimes			int ret, dec, multby, digit;
10011592Srgrimes
10021592Srgrimes			/*
10031592Srgrimes			 * Convert a number that was read as decimal number
10041592Srgrimes			 * to what it would be if it had been read as octal.
10051592Srgrimes			 */
100692272Smaxim			dec = $1.i;
10071592Srgrimes			multby = 1;
10081592Srgrimes			ret = 0;
10091592Srgrimes			while (dec) {
10101592Srgrimes				digit = dec%10;
10111592Srgrimes				if (digit > 7) {
10121592Srgrimes					ret = -1;
10131592Srgrimes					break;
10141592Srgrimes				}
10151592Srgrimes				ret += digit * multby;
10161592Srgrimes				multby *= 8;
10171592Srgrimes				dec /= 10;
10181592Srgrimes			}
10191592Srgrimes			$$ = ret;
10201592Srgrimes		}
10211592Srgrimes	;
10221592Srgrimes
10231592Srgrimes
10241592Srgrimescheck_login
10251592Srgrimes	: /* empty */
10261592Srgrimes		{
102770102Sphk		$$ = check_login1();
10281592Srgrimes		}
10291592Srgrimes	;
10301592Srgrimes
103170102Sphkcheck_login_epsv
103270102Sphk	: /* empty */
103370102Sphk		{
103470102Sphk		if (noepsv) {
103570102Sphk			reply(500, "EPSV command disabled");
103670102Sphk			$$ = 0;
103770102Sphk		}
103870102Sphk		else
103970102Sphk			$$ = check_login1();
104070102Sphk		}
104170102Sphk	;
104270102Sphk
104370102Sphkcheck_login_ro
104470102Sphk	: /* empty */
104570102Sphk		{
104670102Sphk		if (readonly) {
104772710Sdes			reply(550, "Permission denied.");
104870102Sphk			$$ = 0;
104970102Sphk		}
105070102Sphk		else
105170102Sphk			$$ = check_login1();
105270102Sphk		}
105370102Sphk	;
105470102Sphk
10551592Srgrimes%%
10561592Srgrimes
10571592Srgrimes#define	CMD	0	/* beginning of command */
10581592Srgrimes#define	ARGS	1	/* expect miscellaneous arguments */
10591592Srgrimes#define	STR1	2	/* expect SP followed by STRING */
10601592Srgrimes#define	STR2	3	/* expect STRING */
10611592Srgrimes#define	OSTR	4	/* optional SP then STRING */
106275556Sgreen#define	ZSTR1	5	/* optional SP then optional STRING */
10631592Srgrimes#define	ZSTR2	6	/* optional STRING after SP */
10641592Srgrimes#define	SITECMD	7	/* SITE command */
10651592Srgrimes#define	NSTR	8	/* Number followed by a string */
10661592Srgrimes
106775560Sjedgar#define	MAXGLOBARGS	1000
106875560Sjedgar
1069101034Syar#define	MAXASIZE	10240	/* Deny ASCII SIZE on files larger than that */
1070101034Syar
10711592Srgrimesstruct tab {
10721592Srgrimes	char	*name;
10731592Srgrimes	short	token;
10741592Srgrimes	short	state;
10751592Srgrimes	short	implemented;	/* 1 if command is implemented */
10761592Srgrimes	char	*help;
10771592Srgrimes};
10781592Srgrimes
10791592Srgrimesstruct tab cmdtab[] = {		/* In order defined in RFC 765 */
10801592Srgrimes	{ "USER", USER, STR1, 1,	"<sp> username" },
108175556Sgreen	{ "PASS", PASS, ZSTR1, 1,	"[<sp> [password]]" },
10821592Srgrimes	{ "ACCT", ACCT, STR1, 0,	"(specify account)" },
10831592Srgrimes	{ "SMNT", SMNT, ARGS, 0,	"(structure mount)" },
10841592Srgrimes	{ "REIN", REIN, ARGS, 0,	"(reinitialize server state)" },
10851592Srgrimes	{ "QUIT", QUIT, ARGS, 1,	"(terminate service)", },
1086101806Syar	{ "PORT", PORT, ARGS, 1,	"<sp> b0, b1, b2, b3, b4, b5" },
108756668Sshin	{ "LPRT", LPRT, ARGS, 1,	"<sp> af, hal, h1, h2, h3,..., pal, p1, p2..." },
108856668Sshin	{ "EPRT", EPRT, STR1, 1,	"<sp> |af|addr|port|" },
10891592Srgrimes	{ "PASV", PASV, ARGS, 1,	"(set server in passive mode)" },
109056668Sshin	{ "LPSV", LPSV, ARGS, 1,	"(set server in passive mode)" },
109156668Sshin	{ "EPSV", EPSV, ARGS, 1,	"[<sp> af|ALL]" },
1092101806Syar	{ "TYPE", TYPE, ARGS, 1,	"<sp> { A | E | I | L }" },
10931592Srgrimes	{ "STRU", STRU, ARGS, 1,	"(specify file structure)" },
10941592Srgrimes	{ "MODE", MODE, ARGS, 1,	"(specify transfer mode)" },
10951592Srgrimes	{ "RETR", RETR, STR1, 1,	"<sp> file-name" },
10961592Srgrimes	{ "STOR", STOR, STR1, 1,	"<sp> file-name" },
10971592Srgrimes	{ "APPE", APPE, STR1, 1,	"<sp> file-name" },
10981592Srgrimes	{ "MLFL", MLFL, OSTR, 0,	"(mail file)" },
10991592Srgrimes	{ "MAIL", MAIL, OSTR, 0,	"(mail to user)" },
11001592Srgrimes	{ "MSND", MSND, OSTR, 0,	"(mail send to terminal)" },
11011592Srgrimes	{ "MSOM", MSOM, OSTR, 0,	"(mail send to terminal or mailbox)" },
11021592Srgrimes	{ "MSAM", MSAM, OSTR, 0,	"(mail send to terminal and mailbox)" },
11031592Srgrimes	{ "MRSQ", MRSQ, OSTR, 0,	"(mail recipient scheme question)" },
11041592Srgrimes	{ "MRCP", MRCP, STR1, 0,	"(mail recipient)" },
11051592Srgrimes	{ "ALLO", ALLO, ARGS, 1,	"allocate storage (vacuously)" },
11061592Srgrimes	{ "REST", REST, ARGS, 1,	"<sp> offset (restart command)" },
11071592Srgrimes	{ "RNFR", RNFR, STR1, 1,	"<sp> file-name" },
11081592Srgrimes	{ "RNTO", RNTO, STR1, 1,	"<sp> file-name" },
11091592Srgrimes	{ "ABOR", ABOR, ARGS, 1,	"(abort operation)" },
11101592Srgrimes	{ "DELE", DELE, STR1, 1,	"<sp> file-name" },
11111592Srgrimes	{ "CWD",  CWD,  OSTR, 1,	"[ <sp> directory-name ]" },
11121592Srgrimes	{ "XCWD", CWD,	OSTR, 1,	"[ <sp> directory-name ]" },
11131592Srgrimes	{ "LIST", LIST, OSTR, 1,	"[ <sp> path-name ]" },
11141592Srgrimes	{ "NLST", NLST, OSTR, 1,	"[ <sp> path-name ]" },
11151592Srgrimes	{ "SITE", SITE, SITECMD, 1,	"site-cmd [ <sp> arguments ]" },
11161592Srgrimes	{ "SYST", SYST, ARGS, 1,	"(get type of operating system)" },
11171592Srgrimes	{ "STAT", STAT, OSTR, 1,	"[ <sp> path-name ]" },
11181592Srgrimes	{ "HELP", HELP, OSTR, 1,	"[ <sp> <string> ]" },
11191592Srgrimes	{ "NOOP", NOOP, ARGS, 1,	"" },
11201592Srgrimes	{ "MKD",  MKD,  STR1, 1,	"<sp> path-name" },
11211592Srgrimes	{ "XMKD", MKD,  STR1, 1,	"<sp> path-name" },
11221592Srgrimes	{ "RMD",  RMD,  STR1, 1,	"<sp> path-name" },
11231592Srgrimes	{ "XRMD", RMD,  STR1, 1,	"<sp> path-name" },
11241592Srgrimes	{ "PWD",  PWD,  ARGS, 1,	"(return current directory)" },
11251592Srgrimes	{ "XPWD", PWD,  ARGS, 1,	"(return current directory)" },
11261592Srgrimes	{ "CDUP", CDUP, ARGS, 1,	"(change to parent directory)" },
11271592Srgrimes	{ "XCUP", CDUP, ARGS, 1,	"(change to parent directory)" },
11281592Srgrimes	{ "STOU", STOU, STR1, 1,	"<sp> file-name" },
11291592Srgrimes	{ "SIZE", SIZE, OSTR, 1,	"<sp> path-name" },
11301592Srgrimes	{ "MDTM", MDTM, OSTR, 1,	"<sp> path-name" },
11311592Srgrimes	{ NULL,   0,    0,    0,	0 }
11321592Srgrimes};
11331592Srgrimes
11341592Srgrimesstruct tab sitetab[] = {
113575535Sphk	{ "MD5", MDFIVE, STR1, 1,	"[ <sp> file-name ]" },
11361592Srgrimes	{ "UMASK", UMASK, ARGS, 1,	"[ <sp> umask ]" },
11371592Srgrimes	{ "IDLE", IDLE, ARGS, 1,	"[ <sp> maximum-idle-time ]" },
11381592Srgrimes	{ "CHMOD", CHMOD, NSTR, 1,	"<sp> mode <sp> file-name" },
11391592Srgrimes	{ "HELP", HELP, OSTR, 1,	"[ <sp> <string> ]" },
11401592Srgrimes	{ NULL,   0,    0,    0,	0 }
11411592Srgrimes};
11421592Srgrimes
114390148Simpstatic char	*copy(char *);
114490148Simpstatic void	 help(struct tab *, char *);
11451592Srgrimesstatic struct tab *
114690148Simp		 lookup(struct tab *, char *);
114790148Simpstatic int	 port_check(const char *);
114890148Simpstatic int	 port_check_v6(const char *);
114990148Simpstatic void	 sizecmd(char *);
115090148Simpstatic void	 toolong(int);
115190148Simpstatic void	 v4map_data_dest(void);
115290148Simpstatic int	 yylex(void);
11531592Srgrimes
11541592Srgrimesstatic struct tab *
115590148Simplookup(struct tab *p, char *cmd)
11561592Srgrimes{
11571592Srgrimes
11581592Srgrimes	for (; p->name != NULL; p++)
11591592Srgrimes		if (strcmp(cmd, p->name) == 0)
11601592Srgrimes			return (p);
11611592Srgrimes	return (0);
11621592Srgrimes}
11631592Srgrimes
11641592Srgrimes#include <arpa/telnet.h>
11651592Srgrimes
11661592Srgrimes/*
11671592Srgrimes * getline - a hacked up version of fgets to ignore TELNET escape codes.
11681592Srgrimes */
11691592Srgrimeschar *
117090148Simpgetline(char *s, int n, FILE *iop)
11711592Srgrimes{
11721592Srgrimes	int c;
11731592Srgrimes	register char *cs;
11741592Srgrimes
11751592Srgrimes	cs = s;
11761592Srgrimes/* tmpline may contain saved command from urgent mode interruption */
11771592Srgrimes	for (c = 0; tmpline[c] != '\0' && --n > 0; ++c) {
11781592Srgrimes		*cs++ = tmpline[c];
11791592Srgrimes		if (tmpline[c] == '\n') {
11801592Srgrimes			*cs++ = '\0';
118176096Smarkm			if (ftpdebug)
11821592Srgrimes				syslog(LOG_DEBUG, "command: %s", s);
11831592Srgrimes			tmpline[0] = '\0';
11841592Srgrimes			return(s);
11851592Srgrimes		}
11861592Srgrimes		if (c == 0)
11871592Srgrimes			tmpline[0] = '\0';
11881592Srgrimes	}
11891592Srgrimes	while ((c = getc(iop)) != EOF) {
11901592Srgrimes		c &= 0377;
11911592Srgrimes		if (c == IAC) {
11921592Srgrimes		    if ((c = getc(iop)) != EOF) {
11931592Srgrimes			c &= 0377;
11941592Srgrimes			switch (c) {
11951592Srgrimes			case WILL:
11961592Srgrimes			case WONT:
11971592Srgrimes				c = getc(iop);
11981592Srgrimes				printf("%c%c%c", IAC, DONT, 0377&c);
11991592Srgrimes				(void) fflush(stdout);
12001592Srgrimes				continue;
12011592Srgrimes			case DO:
12021592Srgrimes			case DONT:
12031592Srgrimes				c = getc(iop);
12041592Srgrimes				printf("%c%c%c", IAC, WONT, 0377&c);
12051592Srgrimes				(void) fflush(stdout);
12061592Srgrimes				continue;
12071592Srgrimes			case IAC:
12081592Srgrimes				break;
12091592Srgrimes			default:
12101592Srgrimes				continue;	/* ignore command */
12111592Srgrimes			}
12121592Srgrimes		    }
12131592Srgrimes		}
12141592Srgrimes		*cs++ = c;
12151592Srgrimes		if (--n <= 0 || c == '\n')
12161592Srgrimes			break;
12171592Srgrimes	}
12181592Srgrimes	if (c == EOF && cs == s)
12191592Srgrimes		return (NULL);
12201592Srgrimes	*cs++ = '\0';
122176096Smarkm	if (ftpdebug) {
12221592Srgrimes		if (!guest && strncasecmp("pass ", s, 5) == 0) {
12231592Srgrimes			/* Don't syslog passwords */
12241592Srgrimes			syslog(LOG_DEBUG, "command: %.5s ???", s);
12251592Srgrimes		} else {
12261592Srgrimes			register char *cp;
12271592Srgrimes			register int len;
12281592Srgrimes
12291592Srgrimes			/* Don't syslog trailing CR-LF */
12301592Srgrimes			len = strlen(s);
12311592Srgrimes			cp = s + len - 1;
12321592Srgrimes			while (cp >= s && (*cp == '\n' || *cp == '\r')) {
12331592Srgrimes				--cp;
12341592Srgrimes				--len;
12351592Srgrimes			}
12361592Srgrimes			syslog(LOG_DEBUG, "command: %.*s", len, s);
12371592Srgrimes		}
12381592Srgrimes	}
12391592Srgrimes	return (s);
12401592Srgrimes}
12411592Srgrimes
12421592Srgrimesstatic void
124390148Simptoolong(int signo)
12441592Srgrimes{
12451592Srgrimes
12461592Srgrimes	reply(421,
12471592Srgrimes	    "Timeout (%d seconds): closing control connection.", timeout);
12481592Srgrimes	if (logging)
12491592Srgrimes		syslog(LOG_INFO, "User %s timed out after %d seconds",
12501592Srgrimes		    (pw ? pw -> pw_name : "unknown"), timeout);
12511592Srgrimes	dologout(1);
12521592Srgrimes}
12531592Srgrimes
12541592Srgrimesstatic int
125590148Simpyylex(void)
12561592Srgrimes{
125789935Syar	static int cpos;
12581592Srgrimes	char *cp, *cp2;
12591592Srgrimes	struct tab *p;
12601592Srgrimes	int n;
12611592Srgrimes	char c;
12621592Srgrimes
12631592Srgrimes	for (;;) {
12641592Srgrimes		switch (state) {
12651592Srgrimes
12661592Srgrimes		case CMD:
12671592Srgrimes			(void) signal(SIGALRM, toolong);
12681592Srgrimes			(void) alarm((unsigned) timeout);
12691592Srgrimes			if (getline(cbuf, sizeof(cbuf)-1, stdin) == NULL) {
12701592Srgrimes				reply(221, "You could at least say goodbye.");
12711592Srgrimes				dologout(0);
12721592Srgrimes			}
12731592Srgrimes			(void) alarm(0);
12741592Srgrimes#ifdef SETPROCTITLE
127529574Sphk			if (strncasecmp(cbuf, "PASS", 4) != 0)
12761592Srgrimes				setproctitle("%s: %s", proctitle, cbuf);
12771592Srgrimes#endif /* SETPROCTITLE */
12781592Srgrimes			if ((cp = strchr(cbuf, '\r'))) {
12791592Srgrimes				*cp++ = '\n';
12801592Srgrimes				*cp = '\0';
12811592Srgrimes			}
12821592Srgrimes			if ((cp = strpbrk(cbuf, " \n")))
12831592Srgrimes				cpos = cp - cbuf;
12841592Srgrimes			if (cpos == 0)
12851592Srgrimes				cpos = 4;
12861592Srgrimes			c = cbuf[cpos];
12871592Srgrimes			cbuf[cpos] = '\0';
12881592Srgrimes			upper(cbuf);
12891592Srgrimes			p = lookup(cmdtab, cbuf);
12901592Srgrimes			cbuf[cpos] = c;
12913776Spst			if (p != 0) {
12921592Srgrimes				if (p->implemented == 0) {
12931592Srgrimes					nack(p->name);
129489935Syar					return (LEXERR);
12951592Srgrimes				}
12961592Srgrimes				state = p->state;
12971592Srgrimes				yylval.s = p->name;
12981592Srgrimes				return (p->token);
12991592Srgrimes			}
13001592Srgrimes			break;
13011592Srgrimes
13021592Srgrimes		case SITECMD:
13031592Srgrimes			if (cbuf[cpos] == ' ') {
13041592Srgrimes				cpos++;
13051592Srgrimes				return (SP);
13061592Srgrimes			}
13071592Srgrimes			cp = &cbuf[cpos];
13081592Srgrimes			if ((cp2 = strpbrk(cp, " \n")))
13091592Srgrimes				cpos = cp2 - cbuf;
13101592Srgrimes			c = cbuf[cpos];
13111592Srgrimes			cbuf[cpos] = '\0';
13121592Srgrimes			upper(cp);
13131592Srgrimes			p = lookup(sitetab, cp);
13141592Srgrimes			cbuf[cpos] = c;
13153777Spst			if (guest == 0 && p != 0) {
13161592Srgrimes				if (p->implemented == 0) {
13171592Srgrimes					state = CMD;
13181592Srgrimes					nack(p->name);
131989935Syar					return (LEXERR);
13201592Srgrimes				}
13211592Srgrimes				state = p->state;
13221592Srgrimes				yylval.s = p->name;
13231592Srgrimes				return (p->token);
13241592Srgrimes			}
13251592Srgrimes			state = CMD;
13261592Srgrimes			break;
13271592Srgrimes
132875556Sgreen		case ZSTR1:
13291592Srgrimes		case OSTR:
13301592Srgrimes			if (cbuf[cpos] == '\n') {
13311592Srgrimes				state = CMD;
13321592Srgrimes				return (CRLF);
13331592Srgrimes			}
13341592Srgrimes			/* FALLTHROUGH */
13351592Srgrimes
13361592Srgrimes		case STR1:
13371592Srgrimes		dostr1:
13381592Srgrimes			if (cbuf[cpos] == ' ') {
13391592Srgrimes				cpos++;
134051979Salfred				state = state == OSTR ? STR2 : state+1;
13411592Srgrimes				return (SP);
13421592Srgrimes			}
13431592Srgrimes			break;
13441592Srgrimes
13451592Srgrimes		case ZSTR2:
13461592Srgrimes			if (cbuf[cpos] == '\n') {
13471592Srgrimes				state = CMD;
13481592Srgrimes				return (CRLF);
13491592Srgrimes			}
13501592Srgrimes			/* FALLTHROUGH */
13511592Srgrimes
13521592Srgrimes		case STR2:
13531592Srgrimes			cp = &cbuf[cpos];
13541592Srgrimes			n = strlen(cp);
13551592Srgrimes			cpos += n - 1;
13561592Srgrimes			/*
13571592Srgrimes			 * Make sure the string is nonempty and \n terminated.
13581592Srgrimes			 */
13591592Srgrimes			if (n > 1 && cbuf[cpos] == '\n') {
13601592Srgrimes				cbuf[cpos] = '\0';
13611592Srgrimes				yylval.s = copy(cp);
13621592Srgrimes				cbuf[cpos] = '\n';
13631592Srgrimes				state = ARGS;
13641592Srgrimes				return (STRING);
13651592Srgrimes			}
13661592Srgrimes			break;
13671592Srgrimes
13681592Srgrimes		case NSTR:
13691592Srgrimes			if (cbuf[cpos] == ' ') {
13701592Srgrimes				cpos++;
13711592Srgrimes				return (SP);
13721592Srgrimes			}
13731592Srgrimes			if (isdigit(cbuf[cpos])) {
13741592Srgrimes				cp = &cbuf[cpos];
13751592Srgrimes				while (isdigit(cbuf[++cpos]))
13761592Srgrimes					;
13771592Srgrimes				c = cbuf[cpos];
13781592Srgrimes				cbuf[cpos] = '\0';
137992272Smaxim				yylval.u.i = atoi(cp);
13801592Srgrimes				cbuf[cpos] = c;
13811592Srgrimes				state = STR1;
13821592Srgrimes				return (NUMBER);
13831592Srgrimes			}
13841592Srgrimes			state = STR1;
13851592Srgrimes			goto dostr1;
13861592Srgrimes
13871592Srgrimes		case ARGS:
13881592Srgrimes			if (isdigit(cbuf[cpos])) {
13891592Srgrimes				cp = &cbuf[cpos];
13901592Srgrimes				while (isdigit(cbuf[++cpos]))
13911592Srgrimes					;
13921592Srgrimes				c = cbuf[cpos];
13931592Srgrimes				cbuf[cpos] = '\0';
139492272Smaxim				yylval.u.i = atoi(cp);
139592272Smaxim				yylval.u.o = strtoull(cp, (char **)NULL, 10);
13961592Srgrimes				cbuf[cpos] = c;
13971592Srgrimes				return (NUMBER);
13981592Srgrimes			}
139956668Sshin			if (strncasecmp(&cbuf[cpos], "ALL", 3) == 0
140056668Sshin			 && !isalnum(cbuf[cpos + 3])) {
140156668Sshin				cpos += 3;
140256668Sshin				return ALL;
140356668Sshin			}
14041592Srgrimes			switch (cbuf[cpos++]) {
14051592Srgrimes
14061592Srgrimes			case '\n':
14071592Srgrimes				state = CMD;
14081592Srgrimes				return (CRLF);
14091592Srgrimes
14101592Srgrimes			case ' ':
14111592Srgrimes				return (SP);
14121592Srgrimes
14131592Srgrimes			case ',':
14141592Srgrimes				return (COMMA);
14151592Srgrimes
14161592Srgrimes			case 'A':
14171592Srgrimes			case 'a':
14181592Srgrimes				return (A);
14191592Srgrimes
14201592Srgrimes			case 'B':
14211592Srgrimes			case 'b':
14221592Srgrimes				return (B);
14231592Srgrimes
14241592Srgrimes			case 'C':
14251592Srgrimes			case 'c':
14261592Srgrimes				return (C);
14271592Srgrimes
14281592Srgrimes			case 'E':
14291592Srgrimes			case 'e':
14301592Srgrimes				return (E);
14311592Srgrimes
14321592Srgrimes			case 'F':
14331592Srgrimes			case 'f':
14341592Srgrimes				return (F);
14351592Srgrimes
14361592Srgrimes			case 'I':
14371592Srgrimes			case 'i':
14381592Srgrimes				return (I);
14391592Srgrimes
14401592Srgrimes			case 'L':
14411592Srgrimes			case 'l':
14421592Srgrimes				return (L);
14431592Srgrimes
14441592Srgrimes			case 'N':
14451592Srgrimes			case 'n':
14461592Srgrimes				return (N);
14471592Srgrimes
14481592Srgrimes			case 'P':
14491592Srgrimes			case 'p':
14501592Srgrimes				return (P);
14511592Srgrimes
14521592Srgrimes			case 'R':
14531592Srgrimes			case 'r':
14541592Srgrimes				return (R);
14551592Srgrimes
14561592Srgrimes			case 'S':
14571592Srgrimes			case 's':
14581592Srgrimes				return (S);
14591592Srgrimes
14601592Srgrimes			case 'T':
14611592Srgrimes			case 't':
14621592Srgrimes				return (T);
14631592Srgrimes
14641592Srgrimes			}
14651592Srgrimes			break;
14661592Srgrimes
14671592Srgrimes		default:
146876096Smarkm			fatalerror("Unknown state in scanner.");
14691592Srgrimes		}
14701592Srgrimes		state = CMD;
147189935Syar		return (LEXERR);
14721592Srgrimes	}
14731592Srgrimes}
14741592Srgrimes
14751592Srgrimesvoid
147690148Simpupper(char *s)
14771592Srgrimes{
14781592Srgrimes	while (*s != '\0') {
14791592Srgrimes		if (islower(*s))
14801592Srgrimes			*s = toupper(*s);
14811592Srgrimes		s++;
14821592Srgrimes	}
14831592Srgrimes}
14841592Srgrimes
14851592Srgrimesstatic char *
148690148Simpcopy(char *s)
14871592Srgrimes{
14881592Srgrimes	char *p;
14891592Srgrimes
14901592Srgrimes	p = malloc((unsigned) strlen(s) + 1);
14911592Srgrimes	if (p == NULL)
149276096Smarkm		fatalerror("Ran out of memory.");
14931592Srgrimes	(void) strcpy(p, s);
14941592Srgrimes	return (p);
14951592Srgrimes}
14961592Srgrimes
14971592Srgrimesstatic void
149890148Simphelp(struct tab *ctab, char *s)
14991592Srgrimes{
15001592Srgrimes	struct tab *c;
15011592Srgrimes	int width, NCMDS;
15021592Srgrimes	char *type;
15031592Srgrimes
15041592Srgrimes	if (ctab == sitetab)
15051592Srgrimes		type = "SITE ";
15061592Srgrimes	else
15071592Srgrimes		type = "";
15081592Srgrimes	width = 0, NCMDS = 0;
15091592Srgrimes	for (c = ctab; c->name != NULL; c++) {
15101592Srgrimes		int len = strlen(c->name);
15111592Srgrimes
15121592Srgrimes		if (len > width)
15131592Srgrimes			width = len;
15141592Srgrimes		NCMDS++;
15151592Srgrimes	}
15161592Srgrimes	width = (width + 8) &~ 7;
15171592Srgrimes	if (s == 0) {
15181592Srgrimes		int i, j, w;
15191592Srgrimes		int columns, lines;
15201592Srgrimes
15211592Srgrimes		lreply(214, "The following %scommands are recognized %s.",
15221592Srgrimes		    type, "(* =>'s unimplemented)");
15231592Srgrimes		columns = 76 / width;
15241592Srgrimes		if (columns == 0)
15251592Srgrimes			columns = 1;
15261592Srgrimes		lines = (NCMDS + columns - 1) / columns;
15271592Srgrimes		for (i = 0; i < lines; i++) {
15281592Srgrimes			printf("   ");
15291592Srgrimes			for (j = 0; j < columns; j++) {
15301592Srgrimes				c = ctab + j * lines + i;
15311592Srgrimes				printf("%s%c", c->name,
15321592Srgrimes					c->implemented ? ' ' : '*');
15331592Srgrimes				if (c + lines >= &ctab[NCMDS])
15341592Srgrimes					break;
15351592Srgrimes				w = strlen(c->name) + 1;
15361592Srgrimes				while (w < width) {
15371592Srgrimes					putchar(' ');
15381592Srgrimes					w++;
15391592Srgrimes				}
15401592Srgrimes			}
15411592Srgrimes			printf("\r\n");
15421592Srgrimes		}
15431592Srgrimes		(void) fflush(stdout);
15441592Srgrimes		reply(214, "Direct comments to ftp-bugs@%s.", hostname);
15451592Srgrimes		return;
15461592Srgrimes	}
15471592Srgrimes	upper(s);
15481592Srgrimes	c = lookup(ctab, s);
15491592Srgrimes	if (c == (struct tab *)0) {
15501592Srgrimes		reply(502, "Unknown command %s.", s);
15511592Srgrimes		return;
15521592Srgrimes	}
15531592Srgrimes	if (c->implemented)
15541592Srgrimes		reply(214, "Syntax: %s%s %s", type, c->name, c->help);
15551592Srgrimes	else
15561592Srgrimes		reply(214, "%s%-*s\t%s; unimplemented.", type, width,
15571592Srgrimes		    c->name, c->help);
15581592Srgrimes}
15591592Srgrimes
15601592Srgrimesstatic void
156190148Simpsizecmd(char *filename)
15621592Srgrimes{
15631592Srgrimes	switch (type) {
15641592Srgrimes	case TYPE_L:
15651592Srgrimes	case TYPE_I: {
15661592Srgrimes		struct stat stbuf;
156763350Sdes		if (stat(filename, &stbuf) < 0)
156863350Sdes			perror_reply(550, filename);
156963350Sdes		else if (!S_ISREG(stbuf.st_mode))
15701592Srgrimes			reply(550, "%s: not a plain file.", filename);
15711592Srgrimes		else
15721592Srgrimes			reply(213, "%qu", stbuf.st_size);
15731592Srgrimes		break; }
15741592Srgrimes	case TYPE_A: {
15751592Srgrimes		FILE *fin;
15761592Srgrimes		int c;
15771592Srgrimes		off_t count;
15781592Srgrimes		struct stat stbuf;
15791592Srgrimes		fin = fopen(filename, "r");
15801592Srgrimes		if (fin == NULL) {
15811592Srgrimes			perror_reply(550, filename);
15821592Srgrimes			return;
15831592Srgrimes		}
158463350Sdes		if (fstat(fileno(fin), &stbuf) < 0) {
158563350Sdes			perror_reply(550, filename);
158663350Sdes			(void) fclose(fin);
158763350Sdes			return;
158863350Sdes		} else if (!S_ISREG(stbuf.st_mode)) {
15891592Srgrimes			reply(550, "%s: not a plain file.", filename);
15901592Srgrimes			(void) fclose(fin);
15911592Srgrimes			return;
1592101034Syar		} else if (stbuf.st_size > MAXASIZE) {
1593101034Syar			reply(550, "%s: too large for type A SIZE.", filename);
1594101034Syar			(void) fclose(fin);
1595101034Syar			return;
15961592Srgrimes		}
15971592Srgrimes
15981592Srgrimes		count = 0;
15991592Srgrimes		while((c=getc(fin)) != EOF) {
16001592Srgrimes			if (c == '\n')	/* will get expanded to \r\n */
16011592Srgrimes				count++;
16021592Srgrimes			count++;
16031592Srgrimes		}
16041592Srgrimes		(void) fclose(fin);
16051592Srgrimes
16061592Srgrimes		reply(213, "%qd", count);
16071592Srgrimes		break; }
16081592Srgrimes	default:
1609100684Syar		reply(504, "SIZE not implemented for type %s.",
1610100684Syar		           typenames[type]);
16111592Srgrimes	}
16121592Srgrimes}
161356668Sshin
161456668Sshin/* Return 1, if port check is done. Return 0, if not yet. */
161556668Sshinstatic int
161690148Simpport_check(const char *pcmd)
161756668Sshin{
161856668Sshin	if (his_addr.su_family == AF_INET) {
161956668Sshin		if (data_dest.su_family != AF_INET) {
162056668Sshin			usedefault = 1;
162156668Sshin			reply(500, "Invalid address rejected.");
162256668Sshin			return 1;
162356668Sshin		}
162456668Sshin		if (paranoid &&
162556668Sshin		    ((ntohs(data_dest.su_port) < IPPORT_RESERVED) ||
162656668Sshin		     memcmp(&data_dest.su_sin.sin_addr,
162756668Sshin			    &his_addr.su_sin.sin_addr,
162856668Sshin			    sizeof(data_dest.su_sin.sin_addr)))) {
162956668Sshin			usedefault = 1;
163056668Sshin			reply(500, "Illegal PORT range rejected.");
163156668Sshin		} else {
163256668Sshin			usedefault = 0;
163356668Sshin			if (pdata >= 0) {
163456668Sshin				(void) close(pdata);
163556668Sshin				pdata = -1;
163656668Sshin			}
163756668Sshin			reply(200, "%s command successful.", pcmd);
163856668Sshin		}
163956668Sshin		return 1;
164056668Sshin	}
164156668Sshin	return 0;
164256668Sshin}
164356668Sshin
164470102Sphkstatic int
164590148Simpcheck_login1(void)
164670102Sphk{
164770102Sphk	if (logged_in)
164870102Sphk		return 1;
164970102Sphk	else {
165070102Sphk		reply(530, "Please login with USER and PASS.");
165170102Sphk		return 0;
165270102Sphk	}
165370102Sphk}
165470102Sphk
165556668Sshin#ifdef INET6
165656668Sshin/* Return 1, if port check is done. Return 0, if not yet. */
165756668Sshinstatic int
165890148Simpport_check_v6(const char *pcmd)
165956668Sshin{
166056668Sshin	if (his_addr.su_family == AF_INET6) {
166156668Sshin		if (IN6_IS_ADDR_V4MAPPED(&his_addr.su_sin6.sin6_addr))
166256668Sshin			/* Convert data_dest into v4 mapped sockaddr.*/
166356668Sshin			v4map_data_dest();
166456668Sshin		if (data_dest.su_family != AF_INET6) {
166556668Sshin			usedefault = 1;
166656668Sshin			reply(500, "Invalid address rejected.");
166756668Sshin			return 1;
166856668Sshin		}
166956668Sshin		if (paranoid &&
167056668Sshin		    ((ntohs(data_dest.su_port) < IPPORT_RESERVED) ||
167156668Sshin		     memcmp(&data_dest.su_sin6.sin6_addr,
167256668Sshin			    &his_addr.su_sin6.sin6_addr,
167356668Sshin			    sizeof(data_dest.su_sin6.sin6_addr)))) {
167456668Sshin			usedefault = 1;
167556668Sshin			reply(500, "Illegal PORT range rejected.");
167656668Sshin		} else {
167756668Sshin			usedefault = 0;
167856668Sshin			if (pdata >= 0) {
167956668Sshin				(void) close(pdata);
168056668Sshin				pdata = -1;
168156668Sshin			}
168256668Sshin			reply(200, "%s command successful.", pcmd);
168356668Sshin		}
168456668Sshin		return 1;
168556668Sshin	}
168656668Sshin	return 0;
168756668Sshin}
168856668Sshin
168956668Sshinstatic void
169090148Simpv4map_data_dest(void)
169156668Sshin{
169256668Sshin	struct in_addr savedaddr;
169356668Sshin	int savedport;
169456668Sshin
169556668Sshin	if (data_dest.su_family != AF_INET) {
169656668Sshin		usedefault = 1;
169756668Sshin		reply(500, "Invalid address rejected.");
169856668Sshin		return;
169956668Sshin	}
170056668Sshin
170156668Sshin	savedaddr = data_dest.su_sin.sin_addr;
170256668Sshin	savedport = data_dest.su_port;
170356668Sshin
170456668Sshin	memset(&data_dest, 0, sizeof(data_dest));
170556668Sshin	data_dest.su_sin6.sin6_len = sizeof(struct sockaddr_in6);
170656668Sshin	data_dest.su_sin6.sin6_family = AF_INET6;
170756668Sshin	data_dest.su_sin6.sin6_port = savedport;
170856668Sshin	memset((caddr_t)&data_dest.su_sin6.sin6_addr.s6_addr[10], 0xff, 2);
170956668Sshin	memcpy((caddr_t)&data_dest.su_sin6.sin6_addr.s6_addr[12],
171056668Sshin	       (caddr_t)&savedaddr, sizeof(savedaddr));
171156668Sshin}
171256668Sshin#endif
1713