xref: /csrg-svn/libexec/ftpd/ftpcmd.y (revision 26494)
110276Ssam /*
226045Sminshall  * Copyright (c) 1985 Regents of the University of California.
322501Sdist  * All rights reserved.  The Berkeley software License Agreement
422501Sdist  * specifies the terms and conditions for redistribution.
522501Sdist  */
622501Sdist 
722501Sdist /*
810276Ssam  * Grammar for FTP commands.
910276Ssam  * See RFC 765.
1010276Ssam  */
1110276Ssam 
1210276Ssam %{
1310276Ssam 
1410276Ssam #ifndef lint
15*26494Sminshall static	char sccsid[] = "@(#)ftpcmd.y	5.4 (Berkeley) 03/07/86";
1610276Ssam #endif
1710276Ssam 
1810276Ssam #include <sys/types.h>
1910276Ssam #include <sys/socket.h>
2010276Ssam 
2110276Ssam #include <netinet/in.h>
2210276Ssam 
2313033Ssam #include <arpa/ftp.h>
2413033Ssam 
2510276Ssam #include <stdio.h>
2611652Ssam #include <signal.h>
2710276Ssam #include <ctype.h>
2810276Ssam #include <pwd.h>
2910276Ssam #include <setjmp.h>
30*26494Sminshall #include <syslog.h>
3110276Ssam 
3210276Ssam extern	struct sockaddr_in data_dest;
3310276Ssam extern	int logged_in;
3410276Ssam extern	struct passwd *pw;
3510276Ssam extern	int guest;
3610276Ssam extern	int logging;
3710276Ssam extern	int type;
3810276Ssam extern	int form;
3910276Ssam extern	int debug;
4011652Ssam extern	int timeout;
4126045Sminshall extern  int pdata;
4210276Ssam extern	char hostname[];
4310276Ssam extern	char *globerr;
4410320Ssam extern	int usedefault;
4526045Sminshall extern	int unique;
4626045Sminshall extern  int transflag;
4726045Sminshall extern  char tmpline[];
4810276Ssam char	**glob();
4910276Ssam 
5010276Ssam static	int cmd_type;
5110276Ssam static	int cmd_form;
5210276Ssam static	int cmd_bytesz;
5326045Sminshall char cbuf[512];
5410276Ssam 
5510276Ssam char	*index();
5610276Ssam %}
5710276Ssam 
5810276Ssam %token
5910276Ssam 	A	B	C	E	F	I
6010276Ssam 	L	N	P	R	S	T
6110276Ssam 
6210276Ssam 	SP	CRLF	COMMA	STRING	NUMBER
6310276Ssam 
6410276Ssam 	USER	PASS	ACCT	REIN	QUIT	PORT
6510276Ssam 	PASV	TYPE	STRU	MODE	RETR	STOR
6610276Ssam 	APPE	MLFL	MAIL	MSND	MSOM	MSAM
6710276Ssam 	MRSQ	MRCP	ALLO	REST	RNFR	RNTO
6810276Ssam 	ABOR	DELE	CWD	LIST	NLST	SITE
6910276Ssam 	STAT	HELP	NOOP	XMKD	XRMD	XPWD
7026045Sminshall 	XCUP	STOU
7110276Ssam 
7210276Ssam 	LEXERR
7310276Ssam 
7410276Ssam %start	cmd_list
7510276Ssam 
7610276Ssam %%
7710276Ssam 
7810276Ssam cmd_list:	/* empty */
7910276Ssam 	|	cmd_list cmd
8010276Ssam 	;
8110276Ssam 
8210276Ssam cmd:		USER SP username CRLF
8310276Ssam 		= {
8410276Ssam 			extern struct passwd *getpwnam();
8510276Ssam 
8626045Sminshall 			logged_in = 0;
87*26494Sminshall 			if (strcmp((char *) $3, "ftp") == 0 ||
88*26494Sminshall 			  strcmp((char *) $3, "anonymous") == 0) {
8910324Ssam 				if ((pw = getpwnam("ftp")) != NULL) {
9010324Ssam 					guest = 1;
9110324Ssam 					reply(331,
9210276Ssam 				  "Guest login ok, send ident as password.");
9310324Ssam 				}
9426045Sminshall 				else {
9526045Sminshall 					reply(530, "User %s unknown.", $3);
9626045Sminshall 				}
97*26494Sminshall 			} else if (checkuser((char *) $3)) {
9810276Ssam 				guest = 0;
99*26494Sminshall 				pw = getpwnam((char *) $3);
10026045Sminshall 				if (pw == NULL) {
10126045Sminshall 					reply(530, "User %s unknown.", $3);
10226045Sminshall 				}
10326045Sminshall 				else {
10426045Sminshall 				    reply(331, "Password required for %s.", $3);
10526045Sminshall 				}
10610276Ssam 			}
107*26494Sminshall 			free((char *) $3);
10810276Ssam 		}
10910276Ssam 	|	PASS SP password CRLF
11010276Ssam 		= {
111*26494Sminshall 			pass((char *) $3);
112*26494Sminshall 			free((char *) $3);
11310276Ssam 		}
11410276Ssam 	|	PORT SP host_port CRLF
11510276Ssam 		= {
11610320Ssam 			usedefault = 0;
11726045Sminshall 			if (pdata > 0) {
11826045Sminshall 				(void) close(pdata);
11926045Sminshall 			}
12026045Sminshall 			pdata = -1;
121*26494Sminshall 			ack((char *) $1);
12210276Ssam 		}
12326045Sminshall 	|	PASV CRLF
12426045Sminshall 		= {
12526045Sminshall 			passive();
12626045Sminshall 		}
12710276Ssam 	|	TYPE SP type_code CRLF
12810276Ssam 		= {
12910276Ssam 			switch (cmd_type) {
13010276Ssam 
13110276Ssam 			case TYPE_A:
13210276Ssam 				if (cmd_form == FORM_N) {
13310276Ssam 					reply(200, "Type set to A.");
13410276Ssam 					type = cmd_type;
13510276Ssam 					form = cmd_form;
13610276Ssam 				} else
13710276Ssam 					reply(504, "Form must be N.");
13810276Ssam 				break;
13910276Ssam 
14010276Ssam 			case TYPE_E:
14110276Ssam 				reply(504, "Type E not implemented.");
14210276Ssam 				break;
14310276Ssam 
14410276Ssam 			case TYPE_I:
14510276Ssam 				reply(200, "Type set to I.");
14610276Ssam 				type = cmd_type;
14710276Ssam 				break;
14810276Ssam 
14910276Ssam 			case TYPE_L:
15010276Ssam 				if (cmd_bytesz == 8) {
15110276Ssam 					reply(200,
15210276Ssam 					    "Type set to L (byte size 8).");
15310276Ssam 					type = cmd_type;
15410276Ssam 				} else
15510276Ssam 					reply(504, "Byte size must be 8.");
15610276Ssam 			}
15710276Ssam 		}
15810276Ssam 	|	STRU SP struct_code CRLF
15910276Ssam 		= {
16010276Ssam 			switch ($3) {
16110276Ssam 
16210276Ssam 			case STRU_F:
16310276Ssam 				reply(200, "STRU F ok.");
16410276Ssam 				break;
16510276Ssam 
16610276Ssam 			default:
16710276Ssam 				reply(502, "Unimplemented STRU type.");
16810276Ssam 			}
16910276Ssam 		}
17010276Ssam 	|	MODE SP mode_code CRLF
17110276Ssam 		= {
17210276Ssam 			switch ($3) {
17310276Ssam 
17410276Ssam 			case MODE_S:
17510276Ssam 				reply(200, "MODE S ok.");
17610276Ssam 				break;
17710276Ssam 
17810276Ssam 			default:
17910276Ssam 				reply(502, "Unimplemented MODE type.");
18010276Ssam 			}
18110276Ssam 		}
18210276Ssam 	|	ALLO SP NUMBER CRLF
18310276Ssam 		= {
184*26494Sminshall 			ack((char *) $1);
18510276Ssam 		}
18610276Ssam 	|	RETR check_login SP pathname CRLF
18710276Ssam 		= {
18810302Ssam 			if ($2 && $4 != NULL)
189*26494Sminshall 				retrieve((char *) 0, (char *) $4);
19010302Ssam 			if ($4 != NULL)
191*26494Sminshall 				free((char *) $4);
19210276Ssam 		}
19310276Ssam 	|	STOR check_login SP pathname CRLF
19410276Ssam 		= {
19510302Ssam 			if ($2 && $4 != NULL)
196*26494Sminshall 				store((char *) $4, "w");
19710302Ssam 			if ($4 != NULL)
198*26494Sminshall 				free((char *) $4);
19910276Ssam 		}
20010276Ssam 	|	APPE check_login SP pathname CRLF
20110276Ssam 		= {
20210302Ssam 			if ($2 && $4 != NULL)
203*26494Sminshall 				store((char *) $4, "a");
20410302Ssam 			if ($4 != NULL)
205*26494Sminshall 				free((char *) $4);
20610276Ssam 		}
20710276Ssam 	|	NLST check_login CRLF
20810276Ssam 		= {
20910276Ssam 			if ($2)
21011217Ssam 				retrieve("/bin/ls", "");
21110276Ssam 		}
21210276Ssam 	|	NLST check_login SP pathname CRLF
21310276Ssam 		= {
21410302Ssam 			if ($2 && $4 != NULL)
215*26494Sminshall 				retrieve("/bin/ls %s", (char *) $4);
21610302Ssam 			if ($4 != NULL)
217*26494Sminshall 				free((char *) $4);
21810276Ssam 		}
21910276Ssam 	|	LIST check_login CRLF
22010276Ssam 		= {
22110276Ssam 			if ($2)
22210318Ssam 				retrieve("/bin/ls -lg", "");
22310276Ssam 		}
22410276Ssam 	|	LIST check_login SP pathname CRLF
22510276Ssam 		= {
22610302Ssam 			if ($2 && $4 != NULL)
227*26494Sminshall 				retrieve("/bin/ls -lg %s", (char *) $4);
22810302Ssam 			if ($4 != NULL)
229*26494Sminshall 				free((char *) $4);
23010276Ssam 		}
23110276Ssam 	|	DELE check_login SP pathname CRLF
23210276Ssam 		= {
23310302Ssam 			if ($2 && $4 != NULL)
234*26494Sminshall 				delete((char *) $4);
23510302Ssam 			if ($4 != NULL)
236*26494Sminshall 				free((char *) $4);
23710276Ssam 		}
23826045Sminshall 	|	ABOR CRLF
23926045Sminshall 		= {
240*26494Sminshall 			ack((char *) $1);
24126045Sminshall 		}
24210276Ssam 	|	CWD check_login CRLF
24310276Ssam 		= {
24410276Ssam 			if ($2)
24510276Ssam 				cwd(pw->pw_dir);
24610276Ssam 		}
24710276Ssam 	|	CWD check_login SP pathname CRLF
24810276Ssam 		= {
24910302Ssam 			if ($2 && $4 != NULL)
250*26494Sminshall 				cwd((char *) $4);
25110302Ssam 			if ($4 != NULL)
252*26494Sminshall 				free((char *) $4);
25310276Ssam 		}
25410276Ssam 	|	rename_cmd
25510276Ssam 	|	HELP CRLF
25610276Ssam 		= {
257*26494Sminshall 			help((char *) 0);
25810276Ssam 		}
25910276Ssam 	|	HELP SP STRING CRLF
26010276Ssam 		= {
261*26494Sminshall 			help((char *) $3);
26210276Ssam 		}
26310276Ssam 	|	NOOP CRLF
26410276Ssam 		= {
265*26494Sminshall 			ack((char *) $1);
26610276Ssam 		}
26710276Ssam 	|	XMKD check_login SP pathname CRLF
26810276Ssam 		= {
26910302Ssam 			if ($2 && $4 != NULL)
270*26494Sminshall 				makedir((char *) $4);
27110302Ssam 			if ($4 != NULL)
272*26494Sminshall 				free((char *) $4);
27310276Ssam 		}
27410276Ssam 	|	XRMD check_login SP pathname CRLF
27510276Ssam 		= {
27610302Ssam 			if ($2 && $4 != NULL)
277*26494Sminshall 				removedir((char *) $4);
27810302Ssam 			if ($4 != NULL)
279*26494Sminshall 				free((char *) $4);
28010276Ssam 		}
28110276Ssam 	|	XPWD check_login CRLF
28210276Ssam 		= {
28310276Ssam 			if ($2)
28410302Ssam 				pwd();
28510276Ssam 		}
28610276Ssam 	|	XCUP check_login CRLF
28710276Ssam 		= {
28810276Ssam 			if ($2)
28910276Ssam 				cwd("..");
29010276Ssam 		}
29126045Sminshall 	|	STOU check_login SP pathname CRLF
29226045Sminshall 		= {
29326045Sminshall 			if ($2 && $4 != NULL) {
29426045Sminshall 				unique++;
295*26494Sminshall 				store((char *) $4, "w");
29626045Sminshall 				unique = 0;
29726045Sminshall 			}
29826045Sminshall 			if ($4 != NULL)
299*26494Sminshall 				free((char *) $4);
30026045Sminshall 		}
30110276Ssam 	|	QUIT CRLF
30210276Ssam 		= {
30310276Ssam 			reply(221, "Goodbye.");
30413246Ssam 			dologout(0);
30510276Ssam 		}
30610276Ssam 	|	error CRLF
30710276Ssam 		= {
30810276Ssam 			yyerrok;
30910276Ssam 		}
31010276Ssam 	;
31110276Ssam 
31210276Ssam username:	STRING
31310276Ssam 	;
31410276Ssam 
31510276Ssam password:	STRING
31610276Ssam 	;
31710276Ssam 
31810276Ssam byte_size:	NUMBER
31910276Ssam 	;
32010276Ssam 
32110276Ssam host_port:	NUMBER COMMA NUMBER COMMA NUMBER COMMA NUMBER COMMA
32210276Ssam 		NUMBER COMMA NUMBER
32310276Ssam 		= {
32410276Ssam 			register char *a, *p;
32510276Ssam 
32610276Ssam 			a = (char *)&data_dest.sin_addr;
32710276Ssam 			a[0] = $1; a[1] = $3; a[2] = $5; a[3] = $7;
32810276Ssam 			p = (char *)&data_dest.sin_port;
32910276Ssam 			p[0] = $9; p[1] = $11;
33010324Ssam 			data_dest.sin_family = AF_INET;
33110276Ssam 		}
33210276Ssam 	;
33310276Ssam 
33410276Ssam form_code:	N
33510276Ssam 	= {
33610276Ssam 		$$ = FORM_N;
33710276Ssam 	}
33810276Ssam 	|	T
33910276Ssam 	= {
34010276Ssam 		$$ = FORM_T;
34110276Ssam 	}
34210276Ssam 	|	C
34310276Ssam 	= {
34410276Ssam 		$$ = FORM_C;
34510276Ssam 	}
34610276Ssam 	;
34710276Ssam 
34810276Ssam type_code:	A
34910276Ssam 	= {
35010276Ssam 		cmd_type = TYPE_A;
35110276Ssam 		cmd_form = FORM_N;
35210276Ssam 	}
35310276Ssam 	|	A SP form_code
35410276Ssam 	= {
35510276Ssam 		cmd_type = TYPE_A;
35610276Ssam 		cmd_form = $3;
35710276Ssam 	}
35810276Ssam 	|	E
35910276Ssam 	= {
36010276Ssam 		cmd_type = TYPE_E;
36110276Ssam 		cmd_form = FORM_N;
36210276Ssam 	}
36310276Ssam 	|	E SP form_code
36410276Ssam 	= {
36510276Ssam 		cmd_type = TYPE_E;
36610276Ssam 		cmd_form = $3;
36710276Ssam 	}
36810276Ssam 	|	I
36910276Ssam 	= {
37010276Ssam 		cmd_type = TYPE_I;
37110276Ssam 	}
37210276Ssam 	|	L
37310276Ssam 	= {
37410276Ssam 		cmd_type = TYPE_L;
37510276Ssam 		cmd_bytesz = 8;
37610276Ssam 	}
37710276Ssam 	|	L SP byte_size
37810276Ssam 	= {
37910276Ssam 		cmd_type = TYPE_L;
38010276Ssam 		cmd_bytesz = $3;
38110276Ssam 	}
38210276Ssam 	/* this is for a bug in the BBN ftp */
38310276Ssam 	|	L byte_size
38410276Ssam 	= {
38510276Ssam 		cmd_type = TYPE_L;
38610276Ssam 		cmd_bytesz = $2;
38710276Ssam 	}
38810276Ssam 	;
38910276Ssam 
39010276Ssam struct_code:	F
39110276Ssam 	= {
39210276Ssam 		$$ = STRU_F;
39310276Ssam 	}
39410276Ssam 	|	R
39510276Ssam 	= {
39610276Ssam 		$$ = STRU_R;
39710276Ssam 	}
39810276Ssam 	|	P
39910276Ssam 	= {
40010276Ssam 		$$ = STRU_P;
40110276Ssam 	}
40210276Ssam 	;
40310276Ssam 
40410276Ssam mode_code:	S
40510276Ssam 	= {
40610276Ssam 		$$ = MODE_S;
40710276Ssam 	}
40810276Ssam 	|	B
40910276Ssam 	= {
41010276Ssam 		$$ = MODE_B;
41110276Ssam 	}
41210276Ssam 	|	C
41310276Ssam 	= {
41410276Ssam 		$$ = MODE_C;
41510276Ssam 	}
41610276Ssam 	;
41710276Ssam 
41810276Ssam pathname:	pathstring
41910276Ssam 	= {
420*26494Sminshall 		if ($1 && strncmp((char *) $1, "~", 1) == 0) {
421*26494Sminshall 			$$ = (int)*glob((char *) $1);
42210302Ssam 			if (globerr != NULL) {
42310276Ssam 				reply(550, globerr);
42410302Ssam 				$$ = NULL;
42510302Ssam 			}
426*26494Sminshall 			free((char *) $1);
42710276Ssam 		} else
42810276Ssam 			$$ = $1;
42910276Ssam 	}
43010276Ssam 	;
43110276Ssam 
43210276Ssam pathstring:	STRING
43310276Ssam 	;
43410276Ssam 
43510276Ssam rename_cmd:	rename_from rename_to
43610276Ssam 	= {
43710276Ssam 		if ($1 && $2)
438*26494Sminshall 			renamecmd((char *) $1, (char *) $2);
43910276Ssam 		else
44010276Ssam 			reply(503, "Bad sequence of commands.");
44110276Ssam 		if ($1)
442*26494Sminshall 			free((char *) $1);
44310276Ssam 		if ($2)
444*26494Sminshall 			free((char *) $2);
44510276Ssam 	}
44610276Ssam 	;
44710276Ssam 
44810276Ssam rename_from:	RNFR check_login SP pathname CRLF
44910276Ssam 	= {
45010276Ssam 		char *from = 0, *renamefrom();
45110276Ssam 
45210302Ssam 		if ($2 && $4)
453*26494Sminshall 			from = renamefrom((char *) $4);
45410302Ssam 		if (from == 0 && $4)
455*26494Sminshall 			free((char *) $4);
45610276Ssam 		$$ = (int)from;
45710276Ssam 	}
45810276Ssam 	;
45910276Ssam 
46010276Ssam rename_to:	RNTO SP pathname CRLF
46110276Ssam 	= {
46210276Ssam 		$$ = $3;
46310276Ssam 	}
46410276Ssam 	;
46510276Ssam 
46610276Ssam check_login:	/* empty */
46710276Ssam 	= {
46810276Ssam 		if (logged_in)
46910276Ssam 			$$ = 1;
47010276Ssam 		else {
47110276Ssam 			reply(530, "Please login with USER and PASS.");
47210276Ssam 			$$ = 0;
47310276Ssam 		}
47410276Ssam 	}
47510276Ssam 	;
47610276Ssam 
47710276Ssam %%
47810276Ssam 
47910276Ssam extern jmp_buf errcatch;
48010276Ssam 
48110276Ssam #define	CMD	0	/* beginning of command */
48210276Ssam #define	ARGS	1	/* expect miscellaneous arguments */
48310276Ssam #define	STR1	2	/* expect SP followed by STRING */
48410276Ssam #define	STR2	3	/* expect STRING */
48510276Ssam #define	OSTR	4	/* optional STRING */
48610276Ssam 
48710276Ssam struct tab {
48810276Ssam 	char	*name;
48910276Ssam 	short	token;
49010276Ssam 	short	state;
49110276Ssam 	short	implemented;	/* 1 if command is implemented */
49210276Ssam 	char	*help;
49310276Ssam };
49410276Ssam 
49510276Ssam struct tab cmdtab[] = {		/* In order defined in RFC 765 */
49610276Ssam 	{ "USER", USER, STR1, 1,	"<sp> username" },
49710276Ssam 	{ "PASS", PASS, STR1, 1,	"<sp> password" },
49810276Ssam 	{ "ACCT", ACCT, STR1, 0,	"(specify account)" },
49910276Ssam 	{ "REIN", REIN, ARGS, 0,	"(reinitialize server state)" },
50010276Ssam 	{ "QUIT", QUIT, ARGS, 1,	"(terminate service)", },
50110276Ssam 	{ "PORT", PORT, ARGS, 1,	"<sp> b0, b1, b2, b3, b4" },
50226045Sminshall 	{ "PASV", PASV, ARGS, 1,	"(set server in passive mode)" },
50310276Ssam 	{ "TYPE", TYPE, ARGS, 1,	"<sp> [ A | E | I | L ]" },
50410276Ssam 	{ "STRU", STRU, ARGS, 1,	"(specify file structure)" },
50510276Ssam 	{ "MODE", MODE, ARGS, 1,	"(specify transfer mode)" },
50610276Ssam 	{ "RETR", RETR, STR1, 1,	"<sp> file-name" },
50710276Ssam 	{ "STOR", STOR, STR1, 1,	"<sp> file-name" },
50810276Ssam 	{ "APPE", APPE, STR1, 1,	"<sp> file-name" },
50910276Ssam 	{ "MLFL", MLFL, OSTR, 0,	"(mail file)" },
51010276Ssam 	{ "MAIL", MAIL, OSTR, 0,	"(mail to user)" },
51110276Ssam 	{ "MSND", MSND, OSTR, 0,	"(mail send to terminal)" },
51210276Ssam 	{ "MSOM", MSOM, OSTR, 0,	"(mail send to terminal or mailbox)" },
51310276Ssam 	{ "MSAM", MSAM, OSTR, 0,	"(mail send to terminal and mailbox)" },
51410276Ssam 	{ "MRSQ", MRSQ, OSTR, 0,	"(mail recipient scheme question)" },
51510276Ssam 	{ "MRCP", MRCP, STR1, 0,	"(mail recipient)" },
51610276Ssam 	{ "ALLO", ALLO, ARGS, 1,	"allocate storage (vacuously)" },
51710276Ssam 	{ "REST", REST, STR1, 0,	"(restart command)" },
51810276Ssam 	{ "RNFR", RNFR, STR1, 1,	"<sp> file-name" },
51910276Ssam 	{ "RNTO", RNTO, STR1, 1,	"<sp> file-name" },
52026045Sminshall 	{ "ABOR", ABOR, ARGS, 1,	"(abort operation)" },
52110276Ssam 	{ "DELE", DELE, STR1, 1,	"<sp> file-name" },
52210276Ssam 	{ "CWD",  CWD,  OSTR, 1,	"[ <sp> directory-name]" },
52310276Ssam 	{ "XCWD", CWD,	OSTR, 1,	"[ <sp> directory-name ]" },
52410276Ssam 	{ "LIST", LIST, OSTR, 1,	"[ <sp> path-name ]" },
52510276Ssam 	{ "NLST", NLST, OSTR, 1,	"[ <sp> path-name ]" },
52610276Ssam 	{ "SITE", SITE, STR1, 0,	"(get site parameters)" },
52710276Ssam 	{ "STAT", STAT, OSTR, 0,	"(get server status)" },
52810276Ssam 	{ "HELP", HELP, OSTR, 1,	"[ <sp> <string> ]" },
52910276Ssam 	{ "NOOP", NOOP, ARGS, 1,	"" },
53026045Sminshall 	{ "MKD",  XMKD, STR1, 1,	"<sp> path-name" },
53110276Ssam 	{ "XMKD", XMKD, STR1, 1,	"<sp> path-name" },
53226045Sminshall 	{ "RMD",  XRMD, STR1, 1,	"<sp> path-name" },
53310276Ssam 	{ "XRMD", XRMD, STR1, 1,	"<sp> path-name" },
53426045Sminshall 	{ "PWD",  XPWD, ARGS, 1,	"(return current directory)" },
53510276Ssam 	{ "XPWD", XPWD, ARGS, 1,	"(return current directory)" },
53626045Sminshall 	{ "CDUP", XCUP, ARGS, 1,	"(change to parent directory)" },
53710276Ssam 	{ "XCUP", XCUP, ARGS, 1,	"(change to parent directory)" },
53826045Sminshall 	{ "STOU", STOU, STR1, 1,	"<sp> file-name" },
53910276Ssam 	{ NULL,   0,    0,    0,	0 }
54010276Ssam };
54110276Ssam 
54210276Ssam struct tab *
54310276Ssam lookup(cmd)
54410276Ssam 	char *cmd;
54510276Ssam {
54610276Ssam 	register struct tab *p;
54710276Ssam 
54810276Ssam 	for (p = cmdtab; p->name != NULL; p++)
54910276Ssam 		if (strcmp(cmd, p->name) == 0)
55010276Ssam 			return (p);
55110276Ssam 	return (0);
55210276Ssam }
55310276Ssam 
55413033Ssam #include <arpa/telnet.h>
55510276Ssam 
55610276Ssam /*
55710276Ssam  * getline - a hacked up version of fgets to ignore TELNET escape codes.
55810276Ssam  */
55910276Ssam char *
56010276Ssam getline(s, n, iop)
56110276Ssam 	char *s;
56210276Ssam 	register FILE *iop;
56310276Ssam {
56410276Ssam 	register c;
565*26494Sminshall 	register char *cs;
566*26494Sminshall 	char ch;
56710276Ssam 
56810276Ssam 	cs = s;
56926045Sminshall 	for (c = 0; tmpline[c] != '\0' && --n > 0; ++c) {
57026045Sminshall 		*cs++ = tmpline[c];
57126045Sminshall 		if (tmpline[c] == '\n') {
57226045Sminshall 			*cs++ = '\0';
57326045Sminshall 			if (debug) {
574*26494Sminshall 				syslog(LOG_DEBUG, "FTPD: command: %s", s);
57526045Sminshall 			}
57626045Sminshall 			tmpline[0] = '\0';
57726045Sminshall 			return(s);
57826045Sminshall 		}
57926045Sminshall 		if (c == 0) {
58026045Sminshall 			tmpline[0] = '\0';
58126045Sminshall 		}
58226045Sminshall 	}
58326045Sminshall 	while (--n > 0 && read(fileno(iop),&ch,1) >= 0) {
58426045Sminshall 		c = 0377 & ch;
58510276Ssam 		while (c == IAC) {
586*26494Sminshall 			(void) read(fileno(iop),&ch,1);	/* skip command */
587*26494Sminshall 			(void) read(fileno(iop),&ch,1);	/* try next char */
58826045Sminshall 			c = 0377 & ch;
58910276Ssam 		}
59010276Ssam 		*cs++ = c;
59110276Ssam 		if (c=='\n')
59210276Ssam 			break;
59310276Ssam 	}
59410276Ssam 	if (c < 0 && cs == s)
59518303Sralph 		return (NULL);
59610276Ssam 	*cs++ = '\0';
59711652Ssam 	if (debug) {
598*26494Sminshall 		syslog(LOG_DEBUG, "FTPD: command: %s", s);
59911652Ssam 	}
60010276Ssam 	return (s);
60110276Ssam }
60210276Ssam 
60311652Ssam static int
60411652Ssam toolong()
60511652Ssam {
606*26494Sminshall 	time_t now;
60711652Ssam 	extern char *ctime();
608*26494Sminshall 	extern time_t time();
60911652Ssam 
61011652Ssam 	reply(421,
61111652Ssam 	  "Timeout (%d seconds): closing control connection.", timeout);
612*26494Sminshall 	(void) time(&now);
61311652Ssam 	if (logging) {
614*26494Sminshall 		syslog(LOG_INFO,
61511652Ssam 			"FTPD: User %s timed out after %d seconds at %s",
61611652Ssam 			(pw ? pw -> pw_name : "unknown"), timeout, ctime(&now));
61711652Ssam 	}
61813246Ssam 	dologout(1);
61911652Ssam }
62011652Ssam 
62110276Ssam yylex()
62210276Ssam {
62310276Ssam 	static int cpos, state;
62410276Ssam 	register char *cp;
62510276Ssam 	register struct tab *p;
62610276Ssam 	int n;
62710276Ssam 	char c;
62810276Ssam 
62910276Ssam 	for (;;) {
63010276Ssam 		switch (state) {
63110276Ssam 
63210276Ssam 		case CMD:
633*26494Sminshall 			(void) signal(SIGALRM, toolong);
634*26494Sminshall 			(void) alarm((unsigned) timeout);
63510276Ssam 			if (getline(cbuf, sizeof(cbuf)-1, stdin) == NULL) {
63610276Ssam 				reply(221, "You could at least say goodbye.");
63713246Ssam 				dologout(0);
63810276Ssam 			}
639*26494Sminshall 			(void) alarm(0);
64010276Ssam 			if (index(cbuf, '\r')) {
64110276Ssam 				cp = index(cbuf, '\r');
64210276Ssam 				cp[0] = '\n'; cp[1] = 0;
64310276Ssam 			}
64410276Ssam 			if (index(cbuf, ' '))
64510276Ssam 				cpos = index(cbuf, ' ') - cbuf;
64610276Ssam 			else
64726045Sminshall 				cpos = index(cbuf, '\n') - cbuf;
64826045Sminshall 			if (cpos == 0) {
64910276Ssam 				cpos = 4;
65026045Sminshall 			}
65110276Ssam 			c = cbuf[cpos];
65210276Ssam 			cbuf[cpos] = '\0';
65310276Ssam 			upper(cbuf);
65410276Ssam 			p = lookup(cbuf);
65510276Ssam 			cbuf[cpos] = c;
65610276Ssam 			if (p != 0) {
65710276Ssam 				if (p->implemented == 0) {
65810276Ssam 					nack(p->name);
659*26494Sminshall 					longjmp(errcatch,0);
66010276Ssam 					/* NOTREACHED */
66110276Ssam 				}
66210276Ssam 				state = p->state;
66310276Ssam 				yylval = (int) p->name;
66410276Ssam 				return (p->token);
66510276Ssam 			}
66610276Ssam 			break;
66710276Ssam 
66810276Ssam 		case OSTR:
66910276Ssam 			if (cbuf[cpos] == '\n') {
67010276Ssam 				state = CMD;
67110276Ssam 				return (CRLF);
67210276Ssam 			}
67310276Ssam 			/* FALL THRU */
67410276Ssam 
67510276Ssam 		case STR1:
67610276Ssam 			if (cbuf[cpos] == ' ') {
67710276Ssam 				cpos++;
67810276Ssam 				state = STR2;
67910276Ssam 				return (SP);
68010276Ssam 			}
68110276Ssam 			break;
68210276Ssam 
68310276Ssam 		case STR2:
68410276Ssam 			cp = &cbuf[cpos];
68510276Ssam 			n = strlen(cp);
68610276Ssam 			cpos += n - 1;
68710276Ssam 			/*
68810276Ssam 			 * Make sure the string is nonempty and \n terminated.
68910276Ssam 			 */
69010276Ssam 			if (n > 1 && cbuf[cpos] == '\n') {
69110276Ssam 				cbuf[cpos] = '\0';
69210276Ssam 				yylval = copy(cp);
69310276Ssam 				cbuf[cpos] = '\n';
69410276Ssam 				state = ARGS;
69510276Ssam 				return (STRING);
69610276Ssam 			}
69710276Ssam 			break;
69810276Ssam 
69910276Ssam 		case ARGS:
70010276Ssam 			if (isdigit(cbuf[cpos])) {
70110276Ssam 				cp = &cbuf[cpos];
70210276Ssam 				while (isdigit(cbuf[++cpos]))
70310276Ssam 					;
70410276Ssam 				c = cbuf[cpos];
70510276Ssam 				cbuf[cpos] = '\0';
70610276Ssam 				yylval = atoi(cp);
70710276Ssam 				cbuf[cpos] = c;
70810276Ssam 				return (NUMBER);
70910276Ssam 			}
71010276Ssam 			switch (cbuf[cpos++]) {
71110276Ssam 
71210276Ssam 			case '\n':
71310276Ssam 				state = CMD;
71410276Ssam 				return (CRLF);
71510276Ssam 
71610276Ssam 			case ' ':
71710276Ssam 				return (SP);
71810276Ssam 
71910276Ssam 			case ',':
72010276Ssam 				return (COMMA);
72110276Ssam 
72210276Ssam 			case 'A':
72310276Ssam 			case 'a':
72410276Ssam 				return (A);
72510276Ssam 
72610276Ssam 			case 'B':
72710276Ssam 			case 'b':
72810276Ssam 				return (B);
72910276Ssam 
73010276Ssam 			case 'C':
73110276Ssam 			case 'c':
73210276Ssam 				return (C);
73310276Ssam 
73410276Ssam 			case 'E':
73510276Ssam 			case 'e':
73610276Ssam 				return (E);
73710276Ssam 
73810276Ssam 			case 'F':
73910276Ssam 			case 'f':
74010276Ssam 				return (F);
74110276Ssam 
74210276Ssam 			case 'I':
74310276Ssam 			case 'i':
74410276Ssam 				return (I);
74510276Ssam 
74610276Ssam 			case 'L':
74710276Ssam 			case 'l':
74810276Ssam 				return (L);
74910276Ssam 
75010276Ssam 			case 'N':
75110276Ssam 			case 'n':
75210276Ssam 				return (N);
75310276Ssam 
75410276Ssam 			case 'P':
75510276Ssam 			case 'p':
75610276Ssam 				return (P);
75710276Ssam 
75810276Ssam 			case 'R':
75910276Ssam 			case 'r':
76010276Ssam 				return (R);
76110276Ssam 
76210276Ssam 			case 'S':
76310276Ssam 			case 's':
76410276Ssam 				return (S);
76510276Ssam 
76610276Ssam 			case 'T':
76710276Ssam 			case 't':
76810276Ssam 				return (T);
76910276Ssam 
77010276Ssam 			}
77110276Ssam 			break;
77210276Ssam 
77310276Ssam 		default:
77410276Ssam 			fatal("Unknown state in scanner.");
77510276Ssam 		}
776*26494Sminshall 		yyerror((char *) 0);
77710276Ssam 		state = CMD;
778*26494Sminshall 		longjmp(errcatch,0);
77910276Ssam 	}
78010276Ssam }
78110276Ssam 
78210276Ssam upper(s)
78310276Ssam 	char *s;
78410276Ssam {
78510276Ssam 	while (*s != '\0') {
78610276Ssam 		if (islower(*s))
78710276Ssam 			*s = toupper(*s);
78810276Ssam 		s++;
78910276Ssam 	}
79010276Ssam }
79110276Ssam 
79210276Ssam copy(s)
79310276Ssam 	char *s;
79410276Ssam {
79510276Ssam 	char *p;
796*26494Sminshall 	extern char *malloc(), *strcpy();
79710276Ssam 
798*26494Sminshall 	p = malloc((unsigned) strlen(s) + 1);
79910276Ssam 	if (p == NULL)
80010276Ssam 		fatal("Ran out of memory.");
801*26494Sminshall 	(void) strcpy(p, s);
80210276Ssam 	return ((int)p);
80310276Ssam }
80410276Ssam 
80510276Ssam help(s)
80610276Ssam 	char *s;
80710276Ssam {
80810276Ssam 	register struct tab *c;
80910276Ssam 	register int width, NCMDS;
81010276Ssam 
81110276Ssam 	width = 0, NCMDS = 0;
81210276Ssam 	for (c = cmdtab; c->name != NULL; c++) {
81310276Ssam 		int len = strlen(c->name);
81410276Ssam 
81510276Ssam 		if (c->implemented == 0)
81610276Ssam 			len++;
81710276Ssam 		if (len > width)
81810276Ssam 			width = len;
81910276Ssam 		NCMDS++;
82010276Ssam 	}
82110276Ssam 	width = (width + 8) &~ 7;
82210276Ssam 	if (s == 0) {
82310276Ssam 		register int i, j, w;
82410276Ssam 		int columns, lines;
82510276Ssam 
82610276Ssam 		lreply(214,
82710276Ssam 	  "The following commands are recognized (* =>'s unimplemented).");
82810276Ssam 		columns = 76 / width;
82910276Ssam 		if (columns == 0)
83010276Ssam 			columns = 1;
83110276Ssam 		lines = (NCMDS + columns - 1) / columns;
83210276Ssam 		for (i = 0; i < lines; i++) {
83310276Ssam 			printf("    ");
83410276Ssam 			for (j = 0; j < columns; j++) {
83510276Ssam 				c = cmdtab + j * lines + i;
83610276Ssam 				printf("%s%c", c->name,
83710276Ssam 					c->implemented ? ' ' : '*');
83810302Ssam 				if (c + lines >= &cmdtab[NCMDS])
83910276Ssam 					break;
84010276Ssam 				w = strlen(c->name);
84110276Ssam 				while (w < width) {
84210276Ssam 					putchar(' ');
84310276Ssam 					w++;
84410276Ssam 				}
84510276Ssam 			}
84610276Ssam 			printf("\r\n");
84710276Ssam 		}
848*26494Sminshall 		(void) fflush(stdout);
84910276Ssam 		reply(214, "Direct comments to ftp-bugs@%s.", hostname);
85010276Ssam 		return;
85110276Ssam 	}
85210276Ssam 	upper(s);
85310276Ssam 	c = lookup(s);
85410276Ssam 	if (c == (struct tab *)0) {
85510276Ssam 		reply(504, "Unknown command %s.", s);
85610276Ssam 		return;
85710276Ssam 	}
85810276Ssam 	if (c->implemented)
85910276Ssam 		reply(214, "Syntax: %s %s", c->name, c->help);
86010276Ssam 	else
86110276Ssam 		reply(214, "%-*s\t%s; unimplemented.", width, c->name, c->help);
86210276Ssam }
863