Home | History | Annotate | Line # | Download | only in ftpd
ftpcmd.y revision 1.32
      1  1.32    itojun /*	$NetBSD: ftpcmd.y,v 1.32 1999/07/02 05:52:14 itojun Exp $	*/
      2   1.5       cgd 
      3   1.1       cgd /*
      4   1.4   deraadt  * Copyright (c) 1985, 1988, 1993, 1994
      5   1.4   deraadt  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16   1.1       cgd  *    must display the following acknowledgement:
     17   1.1       cgd  *	This product includes software developed by the University of
     18   1.1       cgd  *	California, Berkeley and its contributors.
     19   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     20   1.1       cgd  *    may be used to endorse or promote products derived from this software
     21   1.1       cgd  *    without specific prior written permission.
     22   1.1       cgd  *
     23   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1       cgd  * SUCH DAMAGE.
     34   1.1       cgd  *
     35   1.4   deraadt  *	@(#)ftpcmd.y	8.3 (Berkeley) 4/6/94
     36   1.1       cgd  */
     37   1.1       cgd 
     38   1.1       cgd /*
     39   1.1       cgd  * Grammar for FTP commands.
     40   1.1       cgd  * See RFC 959.
     41   1.1       cgd  */
     42   1.1       cgd 
     43   1.1       cgd %{
     44  1.13  christos #include <sys/cdefs.h>
     45   1.1       cgd 
     46   1.1       cgd #ifndef lint
     47   1.5       cgd #if 0
     48   1.4   deraadt static char sccsid[] = "@(#)ftpcmd.y	8.3 (Berkeley) 4/6/94";
     49   1.5       cgd #else
     50  1.32    itojun __RCSID("$NetBSD: ftpcmd.y,v 1.32 1999/07/02 05:52:14 itojun Exp $");
     51   1.5       cgd #endif
     52   1.1       cgd #endif /* not lint */
     53   1.1       cgd 
     54   1.1       cgd #include <sys/param.h>
     55   1.1       cgd #include <sys/socket.h>
     56   1.1       cgd #include <sys/stat.h>
     57   1.4   deraadt 
     58   1.1       cgd #include <netinet/in.h>
     59   1.1       cgd #include <arpa/ftp.h>
     60  1.15       mrg #include <arpa/inet.h>
     61   1.4   deraadt 
     62   1.4   deraadt #include <ctype.h>
     63   1.4   deraadt #include <errno.h>
     64   1.4   deraadt #include <glob.h>
     65   1.4   deraadt #include <pwd.h>
     66   1.4   deraadt #include <setjmp.h>
     67   1.1       cgd #include <signal.h>
     68   1.4   deraadt #include <stdio.h>
     69   1.4   deraadt #include <stdlib.h>
     70   1.4   deraadt #include <string.h>
     71   1.1       cgd #include <syslog.h>
     72   1.1       cgd #include <time.h>
     73  1.18     lukem #include <tzfile.h>
     74   1.1       cgd #include <unistd.h>
     75  1.32    itojun #include <netdb.h>
     76  1.26  explorer 
     77  1.26  explorer #ifdef KERBEROS5
     78  1.26  explorer #include <krb5.h>
     79  1.26  explorer #endif
     80   1.4   deraadt 
     81   1.4   deraadt #include "extern.h"
     82   1.1       cgd 
     83   1.1       cgd off_t	restart_point;
     84   1.1       cgd 
     85   1.1       cgd static	int cmd_type;
     86   1.1       cgd static	int cmd_form;
     87   1.1       cgd static	int cmd_bytesz;
     88   1.1       cgd char	cbuf[512];
     89   1.1       cgd char	*fromname;
     90  1.22     lukem int	hasyyerrored;
     91   1.1       cgd 
     92  1.24     lukem extern	jmp_buf		errcatch;
     93  1.24     lukem 
     94   1.1       cgd %}
     95   1.1       cgd 
     96   1.4   deraadt %union {
     97   1.4   deraadt 	int	i;
     98   1.4   deraadt 	char   *s;
     99   1.4   deraadt }
    100   1.4   deraadt 
    101   1.1       cgd %token
    102   1.1       cgd 	A	B	C	E	F	I
    103   1.1       cgd 	L	N	P	R	S	T
    104  1.32    itojun 	ALL
    105   1.1       cgd 
    106   1.4   deraadt 	SP	CRLF	COMMA
    107   1.1       cgd 
    108  1.23     lukem 	USER	PASS	ACCT	CWD	CDUP	SMNT
    109  1.23     lukem 	QUIT	REIN	PORT	PASV	TYPE	STRU
    110  1.23     lukem 	MODE	RETR	STOR	STOU	APPE	ALLO
    111  1.23     lukem 	REST	RNFR	RNTO	ABOR	DELE	RMD
    112  1.23     lukem 	MKD	PWD	LIST	NLST	SITE	SYST
    113  1.23     lukem 	STAT	HELP	NOOP
    114  1.23     lukem 
    115  1.25     lukem 	AUTH	ADAT	PROT	PBSZ	CCC	MIC
    116  1.25     lukem 	CONF	ENC
    117  1.25     lukem 
    118  1.23     lukem 	FEAT	OPTS
    119  1.21     lukem 
    120  1.21     lukem 	SIZE	MDTM
    121   1.1       cgd 
    122  1.32    itojun 	LPRT	LPSV	EPRT	EPSV
    123  1.32    itojun 
    124  1.23     lukem 	MAIL	MLFL	MRCP	MRSQ	MSAM	MSND
    125  1.23     lukem 	MSOM
    126  1.23     lukem 
    127   1.1       cgd 	UMASK	IDLE	CHMOD
    128   1.1       cgd 
    129   1.1       cgd 	LEXERR
    130   1.1       cgd 
    131   1.4   deraadt %token	<s> STRING
    132  1.32    itojun %token	<s> ALL
    133   1.4   deraadt %token	<i> NUMBER
    134   1.4   deraadt 
    135  1.12     lukem %type	<i> check_login check_modify octal_number byte_size
    136  1.25     lukem %type	<i> struct_code mode_code type_code form_code decimal_integer
    137   1.4   deraadt %type	<s> pathstring pathname password username
    138  1.25     lukem %type	<s> mechanism_name base64data prot_code
    139   1.4   deraadt 
    140   1.1       cgd %start	cmd_list
    141   1.1       cgd 
    142   1.1       cgd %%
    143   1.1       cgd 
    144   1.4   deraadt cmd_list
    145   1.4   deraadt 	: /* empty */
    146  1.23     lukem 
    147   1.4   deraadt 	| cmd_list cmd
    148   1.4   deraadt 		{
    149  1.22     lukem 			fromname = NULL;
    150   1.1       cgd 			restart_point = (off_t) 0;
    151   1.1       cgd 		}
    152  1.23     lukem 
    153   1.4   deraadt 	| cmd_list rcmd
    154  1.23     lukem 
    155   1.1       cgd 	;
    156   1.1       cgd 
    157   1.4   deraadt cmd
    158  1.23     lukem 						/* RFC 959 */
    159   1.4   deraadt 	: USER SP username CRLF
    160   1.4   deraadt 		{
    161   1.4   deraadt 			user($3);
    162   1.4   deraadt 			free($3);
    163   1.4   deraadt 		}
    164  1.23     lukem 
    165   1.4   deraadt 	| PASS SP password CRLF
    166   1.4   deraadt 		{
    167   1.4   deraadt 			pass($3);
    168   1.4   deraadt 			free($3);
    169   1.1       cgd 		}
    170  1.23     lukem 
    171  1.23     lukem 	| CWD check_login CRLF
    172  1.23     lukem 		{
    173  1.23     lukem 			if ($2)
    174  1.23     lukem 				cwd(pw->pw_dir);
    175  1.23     lukem 		}
    176  1.23     lukem 
    177  1.23     lukem 	| CWD check_login SP pathname CRLF
    178  1.23     lukem 		{
    179  1.23     lukem 			if ($2 && $4 != NULL)
    180  1.23     lukem 				cwd($4);
    181  1.23     lukem 			if ($4 != NULL)
    182  1.23     lukem 				free($4);
    183  1.23     lukem 		}
    184  1.23     lukem 
    185  1.23     lukem 	| CDUP check_login CRLF
    186  1.23     lukem 		{
    187  1.23     lukem 			if ($2)
    188  1.23     lukem 				cwd("..");
    189  1.23     lukem 		}
    190  1.23     lukem 
    191  1.23     lukem 	| QUIT CRLF
    192  1.23     lukem 		{
    193  1.27     lukem 			if (logged_in) {
    194  1.28     lukem 				lreply(221, "");
    195  1.28     lukem 				lreply(0,
    196  1.27     lukem 	    "Data traffic for this session was %qd byte%s in %qd file%s.",
    197  1.30      ross 				    (qdfmt_t)total_data, PLURAL(total_data),
    198  1.30      ross 				    (qdfmt_t)total_files, PLURAL(total_files));
    199  1.28     lukem 				lreply(0,
    200  1.27     lukem 	    "Total traffic for this session was %qd byte%s in %qd transfer%s.",
    201  1.30      ross 				    (qdfmt_t)total_bytes, PLURAL(total_bytes),
    202  1.30      ross 				    (qdfmt_t)total_xfers, PLURAL(total_xfers));
    203  1.31     lukem 			}
    204  1.31     lukem 			reply(221,
    205  1.31     lukem 			    "Thank you for using the FTP service on %s.",
    206  1.31     lukem 			    hostname);
    207  1.31     lukem 			if (logged_in) {
    208  1.27     lukem 				syslog(LOG_INFO,
    209  1.27     lukem 				    "Data traffic: %qd byte%s in %qd file%s",
    210  1.30      ross 				    (qdfmt_t)total_data, PLURAL(total_data),
    211  1.30      ross 				    (qdfmt_t)total_files, PLURAL(total_files));
    212  1.27     lukem 				syslog(LOG_INFO,
    213  1.27     lukem 				  "Total traffic: %qd byte%s in %qd transfer%s",
    214  1.30      ross 				    (qdfmt_t)total_bytes, PLURAL(total_bytes),
    215  1.30      ross 				    (qdfmt_t)total_xfers, PLURAL(total_xfers));
    216  1.27     lukem 			}
    217  1.31     lukem 
    218  1.23     lukem 			dologout(0);
    219  1.23     lukem 		}
    220  1.23     lukem 
    221  1.15       mrg 	| PORT check_login SP host_port CRLF
    222   1.4   deraadt 		{
    223  1.22     lukem 			if ($2) {
    224  1.22     lukem 					/* be paranoid, if told so */
    225  1.16       mrg 			if (curclass.checkportcmd &&
    226  1.32    itojun 			    ((ntohs(data_dest.su_port) < IPPORT_RESERVED) ||
    227  1.32    itojun 			    memcmp(&data_dest.su_sin.sin_addr,
    228  1.32    itojun 			    &his_addr.su_sin.sin_addr,
    229  1.32    itojun 			    sizeof(data_dest.su_sin.sin_addr)) != 0)) {
    230  1.22     lukem 				reply(500,
    231  1.22     lukem 				    "Illegal PORT command rejected");
    232  1.32    itojun 			} else if (epsvall) {
    233  1.32    itojun 				reply(501, "PORT disallowed after EPSV ALL");
    234  1.22     lukem 			} else {
    235  1.22     lukem 				usedefault = 0;
    236  1.22     lukem 				if (pdata >= 0) {
    237  1.22     lukem 					(void) close(pdata);
    238  1.22     lukem 					pdata = -1;
    239  1.22     lukem 				}
    240  1.22     lukem 				reply(200, "PORT command successful.");
    241  1.15       mrg 			}
    242  1.32    itojun 
    243  1.32    itojun 			}
    244  1.32    itojun 		}
    245  1.32    itojun 
    246  1.32    itojun 	| LPRT check_login SP host_long_port CRLF
    247  1.32    itojun 		{
    248  1.32    itojun 			/* be paranoid, if told so */
    249  1.32    itojun 			if (curclass.checkportcmd &&
    250  1.32    itojun 			    ((ntohs(data_dest.su_port) <
    251  1.32    itojun 			      IPPORT_RESERVED) ||
    252  1.32    itojun 			    memcmp(&data_dest.su_sin6.sin6_addr,
    253  1.32    itojun 				   &his_addr.su_sin6.sin6_addr,
    254  1.32    itojun 			    sizeof(data_dest.su_sin6.sin6_addr)) != 0)) {
    255  1.32    itojun 				reply(500, "Illegal LPRT command rejected");
    256  1.32    itojun 				return (NULL);
    257  1.32    itojun 			}
    258  1.32    itojun 			if (epsvall)
    259  1.32    itojun 				reply(501, "LPRT disallowed after EPSV ALL");
    260  1.32    itojun 			else {
    261  1.32    itojun 				usedefault = 0;
    262  1.32    itojun 				if (pdata >= 0) {
    263  1.32    itojun 					(void) close(pdata);
    264  1.32    itojun 					pdata = -1;
    265  1.32    itojun 				}
    266  1.32    itojun 				reply(200, "LPRT command successful.");
    267   1.1       cgd 			}
    268   1.1       cgd 		}
    269  1.23     lukem 
    270  1.32    itojun 	| EPRT check_login SP STRING CRLF
    271  1.32    itojun 		{
    272  1.32    itojun 			char *tmp = NULL;
    273  1.32    itojun 			char *result[3];
    274  1.32    itojun 			char *p, *q;
    275  1.32    itojun 			char delim;
    276  1.32    itojun 			struct addrinfo hints;
    277  1.32    itojun 			struct addrinfo *res;
    278  1.32    itojun 			int i;
    279  1.32    itojun 
    280  1.32    itojun 			if (epsvall) {
    281  1.32    itojun 				reply(501, "EPRT disallowed after EPSV ALL");
    282  1.32    itojun 				goto eprt_done;
    283  1.32    itojun 			}
    284  1.32    itojun 			usedefault = 0;
    285  1.32    itojun 			if (pdata >= 0) {
    286  1.32    itojun 				(void) close(pdata);
    287  1.32    itojun 				pdata = -1;
    288  1.32    itojun 			}
    289  1.32    itojun 
    290  1.32    itojun 			/*XXX checks for login */
    291  1.32    itojun 
    292  1.32    itojun 			tmp = strdup($4);
    293  1.32    itojun 			if (!tmp) {
    294  1.32    itojun 				fatal("not enough core.");
    295  1.32    itojun 				/*NOTREACHED*/
    296  1.32    itojun 			}
    297  1.32    itojun 			p = tmp;
    298  1.32    itojun 			delim = p[0];
    299  1.32    itojun 			p++;
    300  1.32    itojun 			memset(result, 0, sizeof(result));
    301  1.32    itojun 			for (i = 0; i < 3; i++) {
    302  1.32    itojun 				q = strchr(p, delim);
    303  1.32    itojun 				if (!q || *q != delim) {
    304  1.32    itojun 		parsefail:
    305  1.32    itojun 					reply(500, "Invalid argument, rejected.");
    306  1.32    itojun 					if (tmp)
    307  1.32    itojun 						free(tmp);
    308  1.32    itojun 					usedefault = 1;
    309  1.32    itojun 					goto eprt_done;
    310  1.32    itojun 				}
    311  1.32    itojun 				*q++ = '\0';
    312  1.32    itojun 				result[i] = p;
    313  1.32    itojun 				p = q;
    314  1.32    itojun 			}
    315  1.32    itojun 
    316  1.32    itojun 			/* some more sanity check */
    317  1.32    itojun 			p = result[0];
    318  1.32    itojun 			while (*p) {
    319  1.32    itojun 				if (!isdigit(*p))
    320  1.32    itojun 					goto parsefail;
    321  1.32    itojun 				p++;
    322  1.32    itojun 			}
    323  1.32    itojun 			p = result[2];
    324  1.32    itojun 			while (*p) {
    325  1.32    itojun 				if (!isdigit(*p))
    326  1.32    itojun 					goto parsefail;
    327  1.32    itojun 				p++;
    328  1.32    itojun 			}
    329  1.32    itojun 
    330  1.32    itojun 			memset(&hints, 0, sizeof(hints));
    331  1.32    itojun 			if (atoi(result[0]) == 1)
    332  1.32    itojun 				hints.ai_family = PF_INET;
    333  1.32    itojun 			if (atoi(result[0]) == 2)
    334  1.32    itojun 				hints.ai_family = PF_INET6;
    335  1.32    itojun 			else
    336  1.32    itojun 				hints.ai_family = PF_UNSPEC;	/*XXX*/
    337  1.32    itojun 			hints.ai_socktype = SOCK_STREAM;
    338  1.32    itojun 			if (getaddrinfo(result[1], result[2], &hints, &res))
    339  1.32    itojun 				goto parsefail;
    340  1.32    itojun 			memcpy(&data_dest, res->ai_addr, res->ai_addrlen);
    341  1.32    itojun 			/* be paranoid, if told so */
    342  1.32    itojun 			if (curclass.checkportcmd) {
    343  1.32    itojun 				int fail;
    344  1.32    itojun 				fail = 0;
    345  1.32    itojun 				if (ntohs(data_dest.su_port) < IPPORT_RESERVED)
    346  1.32    itojun 					fail++;
    347  1.32    itojun 				if (data_dest.su_family != his_addr.su_family)
    348  1.32    itojun 					fail++;
    349  1.32    itojun 				if (data_dest.su_len != his_addr.su_len)
    350  1.32    itojun 					fail++;
    351  1.32    itojun 				switch (data_dest.su_family) {
    352  1.32    itojun 				case AF_INET:
    353  1.32    itojun 					fail += memcmp(&data_dest.su_sin.sin_addr,
    354  1.32    itojun 					    &his_addr.su_sin.sin_addr,
    355  1.32    itojun 					    sizeof(data_dest.su_sin.sin_addr));
    356  1.32    itojun 					break;
    357  1.32    itojun 				case AF_INET6:
    358  1.32    itojun 					fail += memcmp(&data_dest.su_sin6.sin6_addr,
    359  1.32    itojun 					    &his_addr.su_sin6.sin6_addr,
    360  1.32    itojun 					    sizeof(data_dest.su_sin6.sin6_addr));
    361  1.32    itojun 					break;
    362  1.32    itojun 				default:
    363  1.32    itojun 					fail++;
    364  1.32    itojun 				}
    365  1.32    itojun 				if (fail) {
    366  1.32    itojun 					reply(500,
    367  1.32    itojun 					    "Illegal EPRT command rejected");
    368  1.32    itojun 					return (NULL);
    369  1.32    itojun 				}
    370  1.32    itojun 			}
    371  1.32    itojun 			free(tmp);
    372  1.32    itojun 			tmp = NULL;
    373  1.32    itojun 
    374  1.32    itojun 			reply(200, "EPRT command successful.");
    375  1.32    itojun 		eprt_done:;
    376  1.32    itojun 		}
    377  1.32    itojun 
    378  1.20        tv 	| PASV check_login CRLF
    379   1.4   deraadt 		{
    380  1.20        tv 			if (curclass.passive) {
    381  1.20        tv 				passive();
    382  1.20        tv 			} else {
    383  1.20        tv 				reply(500, "PASV mode not available.");
    384  1.20        tv 			}
    385   1.1       cgd 		}
    386  1.23     lukem 
    387  1.32    itojun 	| LPSV CRLF
    388  1.32    itojun 		{
    389  1.32    itojun 			if (epsvall)
    390  1.32    itojun 				reply(501, "LPSV disallowed after EPSV ALL");
    391  1.32    itojun 			else
    392  1.32    itojun 				long_passive("LPSV", AF_INET6);
    393  1.32    itojun 		}
    394  1.32    itojun 
    395  1.32    itojun 	| EPSV SP NUMBER CRLF
    396  1.32    itojun 		{
    397  1.32    itojun 			int pf;
    398  1.32    itojun 			switch ($3) {
    399  1.32    itojun 			case 1:
    400  1.32    itojun 				pf = PF_INET;
    401  1.32    itojun 				break;
    402  1.32    itojun 			case 2:
    403  1.32    itojun 				pf = PF_INET6;
    404  1.32    itojun 				break;
    405  1.32    itojun 			default:
    406  1.32    itojun 				pf = -1;	/*junk*/
    407  1.32    itojun 				break;
    408  1.32    itojun 			}
    409  1.32    itojun 			long_passive("EPSV", pf);
    410  1.32    itojun 		}
    411  1.32    itojun 
    412  1.32    itojun 	| EPSV SP ALL CRLF
    413  1.32    itojun 		{
    414  1.32    itojun 			if (!logged_in) {
    415  1.32    itojun 				syslog(LOG_NOTICE, "long passive but not logged in");
    416  1.32    itojun 				reply(503, "Login with USER first.");
    417  1.32    itojun 			} else {
    418  1.32    itojun 				reply(200, "EPSV ALL command successful.");
    419  1.32    itojun 				epsvall++;
    420  1.32    itojun 			}
    421  1.32    itojun 		}
    422  1.32    itojun 
    423  1.32    itojun 	| EPSV CRLF
    424  1.32    itojun 		{
    425  1.32    itojun 			long_passive("EPSV", PF_UNSPEC);
    426  1.32    itojun 		}
    427  1.32    itojun 
    428   1.4   deraadt 	| TYPE SP type_code CRLF
    429   1.4   deraadt 		{
    430   1.1       cgd 			switch (cmd_type) {
    431   1.1       cgd 
    432   1.1       cgd 			case TYPE_A:
    433   1.1       cgd 				if (cmd_form == FORM_N) {
    434   1.1       cgd 					reply(200, "Type set to A.");
    435   1.1       cgd 					type = cmd_type;
    436   1.1       cgd 					form = cmd_form;
    437   1.1       cgd 				} else
    438   1.1       cgd 					reply(504, "Form must be N.");
    439   1.1       cgd 				break;
    440   1.1       cgd 
    441   1.1       cgd 			case TYPE_E:
    442   1.1       cgd 				reply(504, "Type E not implemented.");
    443   1.1       cgd 				break;
    444   1.1       cgd 
    445   1.1       cgd 			case TYPE_I:
    446   1.1       cgd 				reply(200, "Type set to I.");
    447   1.1       cgd 				type = cmd_type;
    448   1.1       cgd 				break;
    449   1.1       cgd 
    450   1.1       cgd 			case TYPE_L:
    451   1.1       cgd #if NBBY == 8
    452   1.1       cgd 				if (cmd_bytesz == 8) {
    453   1.1       cgd 					reply(200,
    454   1.1       cgd 					    "Type set to L (byte size 8).");
    455   1.1       cgd 					type = cmd_type;
    456   1.1       cgd 				} else
    457   1.1       cgd 					reply(504, "Byte size must be 8.");
    458   1.1       cgd #else /* NBBY == 8 */
    459   1.1       cgd 				UNIMPLEMENTED for NBBY != 8
    460   1.1       cgd #endif /* NBBY == 8 */
    461   1.1       cgd 			}
    462   1.1       cgd 		}
    463  1.23     lukem 
    464   1.4   deraadt 	| STRU SP struct_code CRLF
    465   1.4   deraadt 		{
    466   1.1       cgd 			switch ($3) {
    467   1.1       cgd 
    468   1.1       cgd 			case STRU_F:
    469   1.1       cgd 				reply(200, "STRU F ok.");
    470   1.1       cgd 				break;
    471   1.1       cgd 
    472   1.1       cgd 			default:
    473   1.1       cgd 				reply(504, "Unimplemented STRU type.");
    474   1.1       cgd 			}
    475   1.1       cgd 		}
    476  1.23     lukem 
    477   1.4   deraadt 	| MODE SP mode_code CRLF
    478   1.4   deraadt 		{
    479   1.1       cgd 			switch ($3) {
    480   1.1       cgd 
    481   1.1       cgd 			case MODE_S:
    482   1.1       cgd 				reply(200, "MODE S ok.");
    483   1.1       cgd 				break;
    484   1.1       cgd 
    485   1.1       cgd 			default:
    486   1.1       cgd 				reply(502, "Unimplemented MODE type.");
    487   1.1       cgd 			}
    488   1.1       cgd 		}
    489  1.23     lukem 
    490   1.4   deraadt 	| RETR check_login SP pathname CRLF
    491   1.4   deraadt 		{
    492   1.1       cgd 			if ($2 && $4 != NULL)
    493  1.22     lukem 				retrieve(NULL, $4);
    494   1.1       cgd 			if ($4 != NULL)
    495   1.4   deraadt 				free($4);
    496   1.1       cgd 		}
    497  1.23     lukem 
    498   1.4   deraadt 	| STOR check_login SP pathname CRLF
    499   1.4   deraadt 		{
    500   1.1       cgd 			if ($2 && $4 != NULL)
    501   1.4   deraadt 				store($4, "w", 0);
    502   1.1       cgd 			if ($4 != NULL)
    503   1.4   deraadt 				free($4);
    504   1.1       cgd 		}
    505  1.23     lukem 
    506  1.23     lukem 	| STOU check_login SP pathname CRLF
    507   1.4   deraadt 		{
    508   1.1       cgd 			if ($2 && $4 != NULL)
    509  1.23     lukem 				store($4, "w", 1);
    510   1.1       cgd 			if ($4 != NULL)
    511   1.4   deraadt 				free($4);
    512   1.1       cgd 		}
    513  1.23     lukem 
    514  1.23     lukem 	| APPE check_login SP pathname CRLF
    515   1.4   deraadt 		{
    516   1.4   deraadt 			if ($2 && $4 != NULL)
    517  1.23     lukem 				store($4, "a", 0);
    518   1.1       cgd 			if ($4 != NULL)
    519   1.4   deraadt 				free($4);
    520   1.1       cgd 		}
    521  1.23     lukem 
    522  1.23     lukem 	| ALLO SP NUMBER CRLF
    523   1.4   deraadt 		{
    524  1.23     lukem 			reply(202, "ALLO command ignored.");
    525   1.1       cgd 		}
    526  1.23     lukem 
    527  1.23     lukem 	| ALLO SP NUMBER SP R SP NUMBER CRLF
    528   1.4   deraadt 		{
    529  1.23     lukem 			reply(202, "ALLO command ignored.");
    530   1.1       cgd 		}
    531  1.23     lukem 
    532   1.4   deraadt 	| RNTO SP pathname CRLF
    533   1.4   deraadt 		{
    534   1.1       cgd 			if (fromname) {
    535   1.4   deraadt 				renamecmd(fromname, $3);
    536   1.1       cgd 				free(fromname);
    537  1.22     lukem 				fromname = NULL;
    538   1.1       cgd 			} else {
    539   1.1       cgd 				reply(503, "Bad sequence of commands.");
    540   1.1       cgd 			}
    541   1.4   deraadt 			free($3);
    542   1.1       cgd 		}
    543  1.23     lukem 
    544   1.4   deraadt 	| ABOR CRLF
    545   1.4   deraadt 		{
    546   1.1       cgd 			reply(225, "ABOR command successful.");
    547   1.1       cgd 		}
    548  1.23     lukem 
    549  1.23     lukem 	| DELE check_modify SP pathname CRLF
    550   1.4   deraadt 		{
    551  1.23     lukem 			if ($2 && $4 != NULL)
    552  1.23     lukem 				delete($4);
    553  1.23     lukem 			if ($4 != NULL)
    554  1.23     lukem 				free($4);
    555   1.1       cgd 		}
    556  1.23     lukem 
    557  1.23     lukem 	| RMD check_modify SP pathname CRLF
    558   1.4   deraadt 		{
    559   1.1       cgd 			if ($2 && $4 != NULL)
    560  1.23     lukem 				removedir($4);
    561   1.1       cgd 			if ($4 != NULL)
    562   1.4   deraadt 				free($4);
    563   1.1       cgd 		}
    564   1.1       cgd 
    565  1.12     lukem 	| MKD check_modify SP pathname CRLF
    566   1.4   deraadt 		{
    567   1.1       cgd 			if ($2 && $4 != NULL)
    568   1.4   deraadt 				makedir($4);
    569   1.1       cgd 			if ($4 != NULL)
    570   1.4   deraadt 				free($4);
    571   1.1       cgd 		}
    572  1.23     lukem 
    573  1.23     lukem 	| PWD check_login CRLF
    574  1.23     lukem 		{
    575  1.23     lukem 			if ($2)
    576  1.23     lukem 				pwd();
    577  1.23     lukem 		}
    578  1.23     lukem 
    579  1.23     lukem 	| LIST check_login CRLF
    580  1.23     lukem 		{
    581  1.23     lukem 			if ($2)
    582  1.23     lukem 				retrieve("/bin/ls -lgA", "");
    583  1.23     lukem 		}
    584  1.23     lukem 
    585  1.23     lukem 	| LIST check_login SP pathname CRLF
    586   1.4   deraadt 		{
    587   1.1       cgd 			if ($2 && $4 != NULL)
    588  1.23     lukem 				retrieve("/bin/ls -lgA %s", $4);
    589   1.1       cgd 			if ($4 != NULL)
    590   1.4   deraadt 				free($4);
    591   1.1       cgd 		}
    592  1.23     lukem 
    593  1.23     lukem 	| NLST check_login CRLF
    594   1.4   deraadt 		{
    595   1.1       cgd 			if ($2)
    596  1.23     lukem 				send_file_list(".");
    597   1.1       cgd 		}
    598  1.23     lukem 
    599  1.23     lukem 	| NLST check_login SP STRING CRLF
    600   1.4   deraadt 		{
    601  1.23     lukem 			if ($2 && $4 != NULL)
    602  1.23     lukem 				send_file_list($4);
    603  1.23     lukem 			if ($4 != NULL)
    604  1.23     lukem 				free($4);
    605   1.1       cgd 		}
    606  1.23     lukem 
    607   1.4   deraadt 	| SITE SP HELP CRLF
    608   1.4   deraadt 		{
    609  1.22     lukem 			help(sitetab, NULL);
    610   1.1       cgd 		}
    611  1.23     lukem 
    612   1.4   deraadt 	| SITE SP HELP SP STRING CRLF
    613   1.4   deraadt 		{
    614   1.4   deraadt 			help(sitetab, $5);
    615   1.1       cgd 		}
    616  1.23     lukem 
    617   1.4   deraadt 	| SITE SP UMASK check_login CRLF
    618   1.4   deraadt 		{
    619   1.1       cgd 			int oldmask;
    620   1.1       cgd 
    621   1.1       cgd 			if ($4) {
    622   1.1       cgd 				oldmask = umask(0);
    623   1.1       cgd 				(void) umask(oldmask);
    624   1.1       cgd 				reply(200, "Current UMASK is %03o", oldmask);
    625   1.1       cgd 			}
    626   1.1       cgd 		}
    627  1.23     lukem 
    628  1.12     lukem 	| SITE SP UMASK check_modify SP octal_number CRLF
    629   1.4   deraadt 		{
    630   1.1       cgd 			int oldmask;
    631   1.1       cgd 
    632   1.1       cgd 			if ($4) {
    633   1.1       cgd 				if (($6 == -1) || ($6 > 0777)) {
    634   1.1       cgd 					reply(501, "Bad UMASK value");
    635   1.1       cgd 				} else {
    636   1.1       cgd 					oldmask = umask($6);
    637   1.1       cgd 					reply(200,
    638   1.1       cgd 					    "UMASK set to %03o (was %03o)",
    639   1.1       cgd 					    $6, oldmask);
    640   1.1       cgd 				}
    641   1.1       cgd 			}
    642   1.1       cgd 		}
    643  1.23     lukem 
    644  1.12     lukem 	| SITE SP CHMOD check_modify SP octal_number SP pathname CRLF
    645   1.4   deraadt 		{
    646   1.1       cgd 			if ($4 && ($8 != NULL)) {
    647   1.1       cgd 				if ($6 > 0777)
    648   1.1       cgd 					reply(501,
    649   1.1       cgd 				"CHMOD: Mode value must be between 0 and 0777");
    650   1.4   deraadt 				else if (chmod($8, $6) < 0)
    651   1.4   deraadt 					perror_reply(550, $8);
    652   1.1       cgd 				else
    653   1.1       cgd 					reply(200, "CHMOD command successful.");
    654   1.1       cgd 			}
    655   1.1       cgd 			if ($8 != NULL)
    656   1.4   deraadt 				free($8);
    657   1.1       cgd 		}
    658  1.23     lukem 
    659   1.4   deraadt 	| SITE SP IDLE CRLF
    660   1.4   deraadt 		{
    661   1.1       cgd 			reply(200,
    662   1.1       cgd 			    "Current IDLE time limit is %d seconds; max %d",
    663  1.12     lukem 				curclass.timeout, curclass.maxtimeout);
    664   1.1       cgd 		}
    665  1.23     lukem 
    666   1.4   deraadt 	| SITE SP IDLE SP NUMBER CRLF
    667   1.4   deraadt 		{
    668  1.12     lukem 			if ($5 < 30 || $5 > curclass.maxtimeout) {
    669   1.1       cgd 				reply(501,
    670  1.12     lukem 			"IDLE time limit must be between 30 and %d seconds",
    671  1.12     lukem 				    curclass.maxtimeout);
    672   1.1       cgd 			} else {
    673  1.12     lukem 				curclass.timeout = $5;
    674  1.12     lukem 				(void) alarm(curclass.timeout);
    675   1.1       cgd 				reply(200,
    676  1.12     lukem 				    "IDLE time limit set to %d seconds",
    677  1.12     lukem 				    curclass.timeout);
    678   1.1       cgd 			}
    679   1.1       cgd 		}
    680  1.23     lukem 
    681  1.23     lukem 	| SYST CRLF
    682  1.23     lukem 		{
    683  1.23     lukem 			reply(215, "UNIX Type: L%d %s", NBBY, version);
    684  1.23     lukem 		}
    685  1.23     lukem 
    686  1.23     lukem 	| STAT check_login SP pathname CRLF
    687   1.4   deraadt 		{
    688   1.1       cgd 			if ($2 && $4 != NULL)
    689  1.23     lukem 				statfilecmd($4);
    690   1.1       cgd 			if ($4 != NULL)
    691   1.4   deraadt 				free($4);
    692   1.1       cgd 		}
    693  1.23     lukem 
    694  1.23     lukem 	| STAT CRLF
    695  1.23     lukem 		{
    696  1.23     lukem 			statcmd();
    697  1.23     lukem 		}
    698  1.23     lukem 
    699  1.23     lukem 	| HELP CRLF
    700  1.23     lukem 		{
    701  1.23     lukem 			help(cmdtab, NULL);
    702  1.23     lukem 		}
    703  1.23     lukem 
    704  1.23     lukem 	| HELP SP STRING CRLF
    705  1.23     lukem 		{
    706  1.23     lukem 			char *cp = $3;
    707  1.23     lukem 
    708  1.23     lukem 			if (strncasecmp(cp, "SITE", 4) == 0) {
    709  1.23     lukem 				cp = $3 + 4;
    710  1.23     lukem 				if (*cp == ' ')
    711  1.23     lukem 					cp++;
    712  1.23     lukem 				if (*cp)
    713  1.23     lukem 					help(sitetab, cp);
    714  1.23     lukem 				else
    715  1.23     lukem 					help(sitetab, NULL);
    716  1.23     lukem 			} else
    717  1.23     lukem 				help(cmdtab, $3);
    718  1.23     lukem 		}
    719  1.23     lukem 
    720  1.23     lukem 	| NOOP CRLF
    721  1.23     lukem 		{
    722  1.23     lukem 			reply(200, "NOOP command successful.");
    723  1.23     lukem 		}
    724  1.23     lukem 
    725  1.25     lukem 						/* RFC 2228 */
    726  1.25     lukem 	| AUTH SP mechanism_name CRLF
    727  1.25     lukem 		{
    728  1.25     lukem 			reply(502, "RFC 2228 authentication not implemented.");
    729  1.25     lukem 		}
    730  1.25     lukem 
    731  1.25     lukem 	| ADAT SP base64data CRLF
    732  1.25     lukem 		{
    733  1.25     lukem 			reply(503,
    734  1.25     lukem 			    "Please set authentication state with AUTH.");
    735  1.25     lukem 		}
    736  1.25     lukem 
    737  1.25     lukem 	| PROT SP prot_code CRLF
    738  1.25     lukem 		{
    739  1.25     lukem 			reply(503,
    740  1.25     lukem 			    "Please set protection buffer size with PBSZ.");
    741  1.25     lukem 		}
    742  1.25     lukem 
    743  1.25     lukem 	| PBSZ SP decimal_integer CRLF
    744  1.25     lukem 		{
    745  1.25     lukem 			reply(503,
    746  1.25     lukem 			    "Please set authentication state with AUTH.");
    747  1.25     lukem 		}
    748  1.25     lukem 
    749  1.25     lukem 	| CCC CRLF
    750  1.25     lukem 		{
    751  1.25     lukem 			reply(533, "No protection enabled.");
    752  1.25     lukem 		}
    753  1.25     lukem 
    754  1.25     lukem 	| MIC SP base64data CRLF
    755  1.25     lukem 		{
    756  1.25     lukem 			reply(502, "RFC 2228 authentication not implemented.");
    757  1.25     lukem 		}
    758  1.25     lukem 
    759  1.25     lukem 	| CONF SP base64data CRLF
    760  1.25     lukem 		{
    761  1.25     lukem 			reply(502, "RFC 2228 authentication not implemented.");
    762  1.25     lukem 		}
    763  1.25     lukem 
    764  1.25     lukem 	| ENC SP base64data CRLF
    765  1.25     lukem 		{
    766  1.25     lukem 			reply(502, "RFC 2228 authentication not implemented.");
    767  1.25     lukem 		}
    768  1.25     lukem 
    769  1.23     lukem 						/* RFC 2389 */
    770  1.23     lukem 	| FEAT CRLF
    771  1.23     lukem 		{
    772  1.23     lukem 			lreply(211, "Features supported");
    773  1.27     lukem 			lreply(-1,  " MDTM");
    774  1.27     lukem 			lreply(-1,  " REST STREAM");
    775  1.27     lukem 			lreply(-1,  " SIZE");
    776  1.27     lukem 			reply(211,  "End");
    777  1.23     lukem 		}
    778  1.23     lukem 
    779  1.23     lukem 	| OPTS SP STRING CRLF
    780   1.4   deraadt 		{
    781  1.23     lukem 
    782  1.23     lukem 			opts($3);
    783   1.1       cgd 		}
    784   1.1       cgd 
    785  1.23     lukem 
    786  1.23     lukem 						/* BSD extensions */
    787  1.23     lukem 
    788   1.1       cgd 		/*
    789  1.21     lukem 		 * SIZE is not in RFC 959, but Postel has blessed it and
    790   1.1       cgd 		 * it will be in the updated RFC.
    791   1.1       cgd 		 *
    792   1.1       cgd 		 * Return size of file in a format suitable for
    793   1.1       cgd 		 * using with RESTART (we just count bytes).
    794   1.1       cgd 		 */
    795   1.4   deraadt 	| SIZE check_login SP pathname CRLF
    796   1.4   deraadt 		{
    797   1.1       cgd 			if ($2 && $4 != NULL)
    798   1.4   deraadt 				sizecmd($4);
    799   1.1       cgd 			if ($4 != NULL)
    800   1.4   deraadt 				free($4);
    801   1.1       cgd 		}
    802   1.1       cgd 
    803   1.1       cgd 		/*
    804  1.21     lukem 		 * MDTM is not in RFC 959, but Postel has blessed it and
    805   1.1       cgd 		 * it will be in the updated RFC.
    806   1.1       cgd 		 *
    807   1.1       cgd 		 * Return modification time of file as an ISO 3307
    808   1.1       cgd 		 * style time. E.g. YYYYMMDDHHMMSS or YYYYMMDDHHMMSS.xxx
    809   1.1       cgd 		 * where xxx is the fractional second (of any precision,
    810   1.1       cgd 		 * not necessarily 3 digits)
    811   1.1       cgd 		 */
    812   1.4   deraadt 	| MDTM check_login SP pathname CRLF
    813   1.4   deraadt 		{
    814   1.1       cgd 			if ($2 && $4 != NULL) {
    815   1.1       cgd 				struct stat stbuf;
    816   1.4   deraadt 				if (stat($4, &stbuf) < 0)
    817  1.22     lukem 					perror_reply(550, $4);
    818   1.4   deraadt 				else if (!S_ISREG(stbuf.st_mode)) {
    819   1.4   deraadt 					reply(550, "%s: not a plain file.", $4);
    820   1.1       cgd 				} else {
    821   1.4   deraadt 					struct tm *t;
    822   1.1       cgd 					t = gmtime(&stbuf.st_mtime);
    823   1.1       cgd 					reply(213,
    824   1.7       jtc 					    "%04d%02d%02d%02d%02d%02d",
    825  1.18     lukem 					    TM_YEAR_BASE + t->tm_year,
    826   1.7       jtc 					    t->tm_mon+1, t->tm_mday,
    827   1.1       cgd 					    t->tm_hour, t->tm_min, t->tm_sec);
    828   1.1       cgd 				}
    829   1.1       cgd 			}
    830   1.1       cgd 			if ($4 != NULL)
    831   1.4   deraadt 				free($4);
    832   1.1       cgd 		}
    833  1.23     lukem 
    834   1.4   deraadt 	| error CRLF
    835   1.4   deraadt 		{
    836   1.1       cgd 			yyerrok;
    837   1.1       cgd 		}
    838   1.1       cgd 	;
    839  1.21     lukem 
    840   1.4   deraadt rcmd
    841  1.23     lukem 	: REST SP byte_size CRLF
    842  1.23     lukem 		{
    843  1.23     lukem 			fromname = NULL;
    844  1.23     lukem 			restart_point = $3;	/* XXX $3 is only "int" */
    845  1.30      ross 			reply(350, "Restarting at %qd. %s",
    846  1.30      ross 			    (qdfmt_t)restart_point,
    847  1.23     lukem 			    "Send STORE or RETRIEVE to initiate transfer.");
    848  1.23     lukem 		}
    849  1.23     lukem 	| RNFR check_modify SP pathname CRLF
    850   1.4   deraadt 		{
    851   1.1       cgd 			restart_point = (off_t) 0;
    852   1.1       cgd 			if ($2 && $4) {
    853   1.4   deraadt 				fromname = renamefrom($4);
    854  1.22     lukem 				if (fromname == NULL && $4) {
    855   1.4   deraadt 					free($4);
    856   1.1       cgd 				}
    857   1.1       cgd 			}
    858   1.1       cgd 		}
    859   1.1       cgd 	;
    860   1.4   deraadt 
    861   1.4   deraadt username
    862   1.4   deraadt 	: STRING
    863   1.1       cgd 	;
    864   1.1       cgd 
    865   1.4   deraadt password
    866   1.4   deraadt 	: /* empty */
    867   1.4   deraadt 		{
    868   1.4   deraadt 			$$ = (char *)calloc(1, sizeof(char));
    869   1.1       cgd 		}
    870  1.23     lukem 
    871   1.4   deraadt 	| STRING
    872   1.1       cgd 	;
    873   1.1       cgd 
    874   1.4   deraadt byte_size
    875   1.4   deraadt 	: NUMBER
    876   1.1       cgd 	;
    877   1.1       cgd 
    878   1.4   deraadt host_port
    879   1.4   deraadt 	: NUMBER COMMA NUMBER COMMA NUMBER COMMA NUMBER COMMA
    880   1.1       cgd 		NUMBER COMMA NUMBER
    881   1.4   deraadt 		{
    882   1.4   deraadt 			char *a, *p;
    883   1.1       cgd 
    884  1.32    itojun 			data_dest.su_len = sizeof(struct sockaddr_in);
    885  1.32    itojun 			data_dest.su_family = AF_INET;
    886  1.32    itojun 			p = (char *)&data_dest.su_sin.sin_port;
    887   1.6   mycroft 			p[0] = $9; p[1] = $11;
    888  1.32    itojun 			a = (char *)&data_dest.su_sin.sin_addr;
    889   1.1       cgd 			a[0] = $1; a[1] = $3; a[2] = $5; a[3] = $7;
    890   1.1       cgd 		}
    891   1.1       cgd 	;
    892   1.1       cgd 
    893  1.32    itojun host_long_port
    894  1.32    itojun 	: NUMBER COMMA NUMBER COMMA NUMBER COMMA NUMBER COMMA
    895  1.32    itojun 		NUMBER COMMA NUMBER COMMA NUMBER COMMA NUMBER COMMA
    896  1.32    itojun 		NUMBER COMMA NUMBER COMMA NUMBER COMMA NUMBER COMMA
    897  1.32    itojun 		NUMBER COMMA NUMBER COMMA NUMBER COMMA NUMBER COMMA
    898  1.32    itojun 		NUMBER COMMA NUMBER COMMA NUMBER COMMA NUMBER COMMA
    899  1.32    itojun 		NUMBER
    900  1.32    itojun 		{
    901  1.32    itojun 			char *a, *p;
    902  1.32    itojun 
    903  1.32    itojun 			data_dest.su_sin6.sin6_len =
    904  1.32    itojun 				sizeof(struct sockaddr_in6);
    905  1.32    itojun 			data_dest.su_family = AF_INET6;
    906  1.32    itojun 			p = (char *)&data_dest.su_port;
    907  1.32    itojun 			p[0] = $39; p[1] = $41;
    908  1.32    itojun 			a = (char *)&data_dest.su_sin6.sin6_addr;
    909  1.32    itojun 			 a[0] =  $5;  a[1] =  $7;  a[2] =  $9;  a[3] = $11;
    910  1.32    itojun 			 a[4] = $13;  a[5] = $15;  a[6] = $17;  a[7] = $19;
    911  1.32    itojun 			 a[8] = $21;  a[9] = $23; a[10] = $25; a[11] = $27;
    912  1.32    itojun 			a[12] = $29; a[13] = $31; a[14] = $33; a[15] = $35;
    913  1.32    itojun 		}
    914  1.32    itojun 	;
    915  1.32    itojun 
    916   1.4   deraadt form_code
    917   1.4   deraadt 	: N
    918   1.4   deraadt 		{
    919   1.4   deraadt 			$$ = FORM_N;
    920   1.4   deraadt 		}
    921  1.23     lukem 
    922   1.4   deraadt 	| T
    923   1.4   deraadt 		{
    924   1.4   deraadt 			$$ = FORM_T;
    925   1.4   deraadt 		}
    926  1.23     lukem 
    927   1.4   deraadt 	| C
    928   1.4   deraadt 		{
    929   1.4   deraadt 			$$ = FORM_C;
    930   1.4   deraadt 		}
    931   1.1       cgd 	;
    932   1.1       cgd 
    933   1.4   deraadt type_code
    934   1.4   deraadt 	: A
    935   1.4   deraadt 		{
    936   1.4   deraadt 			cmd_type = TYPE_A;
    937   1.4   deraadt 			cmd_form = FORM_N;
    938   1.4   deraadt 		}
    939  1.23     lukem 
    940   1.4   deraadt 	| A SP form_code
    941   1.4   deraadt 		{
    942   1.4   deraadt 			cmd_type = TYPE_A;
    943   1.4   deraadt 			cmd_form = $3;
    944   1.4   deraadt 		}
    945  1.23     lukem 
    946   1.4   deraadt 	| E
    947   1.4   deraadt 		{
    948   1.4   deraadt 			cmd_type = TYPE_E;
    949   1.4   deraadt 			cmd_form = FORM_N;
    950   1.4   deraadt 		}
    951  1.23     lukem 
    952   1.4   deraadt 	| E SP form_code
    953   1.4   deraadt 		{
    954   1.4   deraadt 			cmd_type = TYPE_E;
    955   1.4   deraadt 			cmd_form = $3;
    956   1.4   deraadt 		}
    957  1.23     lukem 
    958   1.4   deraadt 	| I
    959   1.4   deraadt 		{
    960   1.4   deraadt 			cmd_type = TYPE_I;
    961   1.4   deraadt 		}
    962  1.23     lukem 
    963   1.4   deraadt 	| L
    964   1.4   deraadt 		{
    965   1.4   deraadt 			cmd_type = TYPE_L;
    966   1.4   deraadt 			cmd_bytesz = NBBY;
    967   1.4   deraadt 		}
    968  1.23     lukem 
    969   1.4   deraadt 	| L SP byte_size
    970   1.4   deraadt 		{
    971   1.4   deraadt 			cmd_type = TYPE_L;
    972   1.4   deraadt 			cmd_bytesz = $3;
    973   1.4   deraadt 		}
    974  1.23     lukem 
    975   1.4   deraadt 		/* this is for a bug in the BBN ftp */
    976   1.4   deraadt 	| L byte_size
    977   1.4   deraadt 		{
    978   1.4   deraadt 			cmd_type = TYPE_L;
    979   1.4   deraadt 			cmd_bytesz = $2;
    980   1.4   deraadt 		}
    981   1.1       cgd 	;
    982   1.1       cgd 
    983   1.4   deraadt struct_code
    984   1.4   deraadt 	: F
    985   1.4   deraadt 		{
    986   1.4   deraadt 			$$ = STRU_F;
    987   1.4   deraadt 		}
    988  1.23     lukem 
    989   1.4   deraadt 	| R
    990   1.4   deraadt 		{
    991   1.4   deraadt 			$$ = STRU_R;
    992   1.4   deraadt 		}
    993  1.23     lukem 
    994   1.4   deraadt 	| P
    995   1.4   deraadt 		{
    996   1.4   deraadt 			$$ = STRU_P;
    997   1.4   deraadt 		}
    998   1.1       cgd 	;
    999   1.1       cgd 
   1000   1.4   deraadt mode_code
   1001   1.4   deraadt 	: S
   1002   1.4   deraadt 		{
   1003   1.4   deraadt 			$$ = MODE_S;
   1004   1.4   deraadt 		}
   1005  1.23     lukem 
   1006   1.4   deraadt 	| B
   1007   1.4   deraadt 		{
   1008   1.4   deraadt 			$$ = MODE_B;
   1009   1.4   deraadt 		}
   1010  1.23     lukem 
   1011   1.4   deraadt 	| C
   1012   1.4   deraadt 		{
   1013   1.4   deraadt 			$$ = MODE_C;
   1014   1.4   deraadt 		}
   1015   1.1       cgd 	;
   1016   1.1       cgd 
   1017   1.4   deraadt pathname
   1018   1.4   deraadt 	: pathstring
   1019   1.4   deraadt 		{
   1020   1.4   deraadt 			/*
   1021   1.4   deraadt 			 * Problem: this production is used for all pathname
   1022   1.4   deraadt 			 * processing, but only gives a 550 error reply.
   1023   1.9     lukem 			 * This is a valid reply in some cases but not in
   1024   1.9     lukem 			 * others.
   1025   1.4   deraadt 			 */
   1026   1.4   deraadt 			if (logged_in && $1 && *$1 == '~') {
   1027   1.4   deraadt 				glob_t gl;
   1028   1.4   deraadt 				int flags =
   1029  1.19    kleink 				 GLOB_BRACE|GLOB_NOCHECK|GLOB_TILDE;
   1030   1.4   deraadt 
   1031   1.9     lukem 				if ($1[1] == '\0')
   1032  1.23     lukem 					$$ = xstrdup(pw->pw_dir);
   1033   1.9     lukem 				else {
   1034   1.9     lukem 					memset(&gl, 0, sizeof(gl));
   1035   1.9     lukem 					if (glob($1, flags, NULL, &gl) ||
   1036   1.9     lukem 					    gl.gl_pathc == 0) {
   1037   1.9     lukem 						reply(550, "not found");
   1038   1.9     lukem 						$$ = NULL;
   1039   1.9     lukem 					} else
   1040  1.23     lukem 						$$ = xstrdup(gl.gl_pathv[0]);
   1041   1.9     lukem 					globfree(&gl);
   1042   1.4   deraadt 				}
   1043   1.4   deraadt 				free($1);
   1044   1.4   deraadt 			} else
   1045   1.4   deraadt 				$$ = $1;
   1046   1.4   deraadt 		}
   1047   1.1       cgd 	;
   1048   1.1       cgd 
   1049   1.4   deraadt pathstring
   1050   1.4   deraadt 	: STRING
   1051   1.1       cgd 	;
   1052   1.1       cgd 
   1053   1.4   deraadt octal_number
   1054   1.4   deraadt 	: NUMBER
   1055   1.4   deraadt 		{
   1056   1.4   deraadt 			int ret, dec, multby, digit;
   1057   1.1       cgd 
   1058   1.4   deraadt 			/*
   1059   1.4   deraadt 			 * Convert a number that was read as decimal number
   1060   1.4   deraadt 			 * to what it would be if it had been read as octal.
   1061   1.4   deraadt 			 */
   1062   1.4   deraadt 			dec = $1;
   1063   1.4   deraadt 			multby = 1;
   1064   1.4   deraadt 			ret = 0;
   1065   1.4   deraadt 			while (dec) {
   1066   1.4   deraadt 				digit = dec%10;
   1067   1.4   deraadt 				if (digit > 7) {
   1068   1.4   deraadt 					ret = -1;
   1069   1.4   deraadt 					break;
   1070   1.4   deraadt 				}
   1071   1.4   deraadt 				ret += digit * multby;
   1072   1.4   deraadt 				multby *= 8;
   1073   1.4   deraadt 				dec /= 10;
   1074   1.1       cgd 			}
   1075   1.4   deraadt 			$$ = ret;
   1076   1.1       cgd 		}
   1077   1.1       cgd 	;
   1078   1.1       cgd 
   1079  1.25     lukem mechanism_name
   1080  1.25     lukem 	: STRING
   1081  1.25     lukem 	;
   1082  1.25     lukem 
   1083  1.25     lukem base64data
   1084  1.25     lukem 	: STRING
   1085  1.25     lukem 	;
   1086  1.25     lukem 
   1087  1.25     lukem prot_code
   1088  1.25     lukem 	: STRING
   1089  1.25     lukem 	;
   1090  1.25     lukem 
   1091  1.25     lukem decimal_integer
   1092  1.25     lukem 	: NUMBER
   1093  1.25     lukem 	;
   1094  1.25     lukem 
   1095   1.4   deraadt check_login
   1096   1.4   deraadt 	: /* empty */
   1097   1.4   deraadt 		{
   1098   1.4   deraadt 			if (logged_in)
   1099   1.4   deraadt 				$$ = 1;
   1100   1.4   deraadt 			else {
   1101   1.4   deraadt 				reply(530, "Please login with USER and PASS.");
   1102   1.4   deraadt 				$$ = 0;
   1103  1.22     lukem 				hasyyerrored = 1;
   1104   1.4   deraadt 			}
   1105   1.1       cgd 		}
   1106   1.1       cgd 	;
   1107  1.21     lukem 
   1108  1.12     lukem check_modify
   1109   1.8       cjs 	: /* empty */
   1110   1.8       cjs 		{
   1111  1.22     lukem 			if (logged_in) {
   1112  1.22     lukem 				if (curclass.modify)
   1113  1.12     lukem 					$$ = 1;
   1114  1.22     lukem 				else {
   1115   1.8       cjs 					reply(502,
   1116  1.12     lukem 					"No permission to use this command.");
   1117   1.8       cjs 					$$ = 0;
   1118  1.22     lukem 					hasyyerrored = 1;
   1119  1.14   hannken 				}
   1120   1.8       cjs 			} else {
   1121   1.8       cjs 				reply(530, "Please login with USER and PASS.");
   1122   1.8       cjs 				$$ = 0;
   1123  1.22     lukem 				hasyyerrored = 1;
   1124   1.8       cjs 			}
   1125   1.8       cjs 		}
   1126   1.1       cgd 
   1127   1.1       cgd %%
   1128   1.1       cgd 
   1129   1.1       cgd #define	CMD	0	/* beginning of command */
   1130   1.1       cgd #define	ARGS	1	/* expect miscellaneous arguments */
   1131   1.1       cgd #define	STR1	2	/* expect SP followed by STRING */
   1132   1.1       cgd #define	STR2	3	/* expect STRING */
   1133   1.1       cgd #define	OSTR	4	/* optional SP then STRING */
   1134   1.1       cgd #define	ZSTR1	5	/* SP then optional STRING */
   1135   1.1       cgd #define	ZSTR2	6	/* optional STRING after SP */
   1136   1.1       cgd #define	SITECMD	7	/* SITE command */
   1137   1.1       cgd #define	NSTR	8	/* Number followed by a string */
   1138  1.21     lukem #define NOARGS	9	/* No arguments allowed */
   1139   1.1       cgd 
   1140   1.1       cgd struct tab {
   1141   1.1       cgd 	char	*name;
   1142  1.23     lukem 	short	 token;
   1143  1.23     lukem 	short	 state;
   1144  1.23     lukem 	short	 implemented;	/* 1 if command is implemented */
   1145  1.23     lukem 	short	 hasopts;	/* 1 if command takes options */
   1146   1.1       cgd 	char	*help;
   1147  1.23     lukem 	char	*options;
   1148   1.1       cgd };
   1149   1.1       cgd 
   1150  1.21     lukem struct tab cmdtab[] = {
   1151  1.21     lukem 				/* From RFC 959, in order defined (5.3.1) */
   1152  1.23     lukem 	{ "USER", USER, STR1,	1, 0,	"<sp> username" },
   1153  1.23     lukem 	{ "PASS", PASS, ZSTR1,	1, 0,	"<sp> password" },
   1154  1.23     lukem 	{ "ACCT", ACCT, STR1,	0, 0,	"(specify account)" },
   1155  1.23     lukem 	{ "CWD",  CWD,  OSTR,	1, 0,	"[ <sp> directory-name ]" },
   1156  1.23     lukem 	{ "CDUP", CDUP, NOARGS,	1, 0,	"(change to parent directory)" },
   1157  1.23     lukem 	{ "SMNT", SMNT, ARGS,	0, 0,	"(structure mount)" },
   1158  1.23     lukem 	{ "QUIT", QUIT, NOARGS,	1, 0,	"(terminate service)", },
   1159  1.23     lukem 	{ "REIN", REIN, NOARGS,	0, 0,	"(reinitialize server state)" },
   1160  1.23     lukem 	{ "PORT", PORT, ARGS,	1, 0,	"<sp> b0, b1, b2, b3, b4" },
   1161  1.32    itojun 	{ "LPRT", LPRT, ARGS,	1, 0,	"<sp> af, hal, h1, h2, h3,..., pal, p1, p2..." },
   1162  1.32    itojun 	{ "EPRT", EPRT, STR1,	1, 0,	"<sp> |af|addr|port|" },
   1163  1.23     lukem 	{ "PASV", PASV, NOARGS,	1, 0,	"(set server in passive mode)" },
   1164  1.32    itojun 	{ "LPSV", LPSV, ARGS,	1, 0,	"(set server in passive mode)" },
   1165  1.32    itojun 	{ "EPSV", EPSV, ARGS,	1, 0,	"[<sp> af|ALL]" },
   1166  1.23     lukem 	{ "TYPE", TYPE, ARGS,	1, 0,	"<sp> [ A | E | I | L ]" },
   1167  1.23     lukem 	{ "STRU", STRU, ARGS,	1, 0,	"(specify file structure)" },
   1168  1.23     lukem 	{ "MODE", MODE, ARGS,	1, 0,	"(specify transfer mode)" },
   1169  1.23     lukem 	{ "RETR", RETR, STR1,	1, 0,	"<sp> file-name" },
   1170  1.23     lukem 	{ "STOR", STOR, STR1,	1, 0,	"<sp> file-name" },
   1171  1.23     lukem 	{ "STOU", STOU, STR1,	1, 0,	"<sp> file-name" },
   1172  1.23     lukem 	{ "APPE", APPE, STR1,	1, 0,	"<sp> file-name" },
   1173  1.23     lukem 	{ "ALLO", ALLO, ARGS,	1, 0,	"allocate storage (vacuously)" },
   1174  1.23     lukem 	{ "REST", REST, ARGS,	1, 0,	"<sp> offset (restart command)" },
   1175  1.23     lukem 	{ "RNFR", RNFR, STR1,	1, 0,	"<sp> file-name" },
   1176  1.23     lukem 	{ "RNTO", RNTO, STR1,	1, 0,	"<sp> file-name" },
   1177  1.23     lukem 	{ "ABOR", ABOR, NOARGS,	1, 0,	"(abort operation)" },
   1178  1.23     lukem 	{ "DELE", DELE, STR1,	1, 0,	"<sp> file-name" },
   1179  1.23     lukem 	{ "RMD",  RMD,  STR1,	1, 0,	"<sp> path-name" },
   1180  1.23     lukem 	{ "MKD",  MKD,  STR1,	1, 0,	"<sp> path-name" },
   1181  1.23     lukem 	{ "PWD",  PWD,  NOARGS,	1, 0,	"(return current directory)" },
   1182  1.23     lukem 	{ "LIST", LIST, OSTR,	1, 0,	"[ <sp> path-name ]" },
   1183  1.23     lukem 	{ "NLST", NLST, OSTR,	1, 0,	"[ <sp> path-name ]" },
   1184  1.23     lukem 	{ "SITE", SITE, SITECMD, 1, 0,	"site-cmd [ <sp> arguments ]" },
   1185  1.23     lukem 	{ "SYST", SYST, NOARGS,	1, 0,	"(get type of operating system)" },
   1186  1.23     lukem 	{ "STAT", STAT, OSTR,	1, 0,	"[ <sp> path-name ]" },
   1187  1.23     lukem 	{ "HELP", HELP, OSTR,	1, 0,	"[ <sp> <string> ]" },
   1188  1.23     lukem 	{ "NOOP", NOOP, NOARGS,	1, 1,	"" },
   1189  1.23     lukem 
   1190  1.25     lukem 				/* From RFC 2228, in order defined */
   1191  1.25     lukem 	{ "AUTH", AUTH, STR1,	1, 0,	"<sp> mechanism-name" },
   1192  1.25     lukem 	{ "ADAT", ADAT, STR1,	1, 0,	"<sp> base-64-data" },
   1193  1.25     lukem 	{ "PROT", PROT, STR1,	1, 0,	"<sp> prot-code" },
   1194  1.25     lukem 	{ "PBSZ", PBSZ, ARGS,	1, 0,	"<sp> decimal-integer" },
   1195  1.25     lukem 	{ "CCC",  CCC,  NOARGS,	1, 0,	"(Disable data protection)" },
   1196  1.25     lukem 	{ "MIC",  MIC,  STR1,	1, 0,	"<sp> base64data" },
   1197  1.25     lukem 	{ "CONF", CONF, STR1,	1, 0,	"<sp> base64data" },
   1198  1.25     lukem 	{ "ENC",  ENC,  STR1,	1, 0,	"<sp> base64data" },
   1199  1.25     lukem 
   1200  1.25     lukem 				/* From RFC 2389, in order defined */
   1201  1.23     lukem 	{ "FEAT", FEAT, NOARGS,	1, 0,	"(display extended features)" },
   1202  1.23     lukem 	{ "OPTS", OPTS, STR1,	1, 0,	"<sp> command [ <sp> options ]" },
   1203  1.23     lukem 
   1204  1.23     lukem 				/* Non standardized extensions */
   1205  1.23     lukem 	{ "SIZE", SIZE, OSTR,	1, 0,	"<sp> path-name" },
   1206  1.23     lukem 	{ "MDTM", MDTM, OSTR,	1, 0,	"<sp> path-name" },
   1207  1.21     lukem 
   1208  1.21     lukem 				/* obsolete commands */
   1209  1.23     lukem 	{ "MAIL", MAIL, OSTR,	0, 0,	"(mail to user)" },
   1210  1.23     lukem 	{ "MLFL", MLFL, OSTR,	0, 0,	"(mail file)" },
   1211  1.23     lukem 	{ "MRCP", MRCP, STR1,	0, 0,	"(mail recipient)" },
   1212  1.23     lukem 	{ "MRSQ", MRSQ, OSTR,	0, 0,	"(mail recipient scheme question)" },
   1213  1.23     lukem 	{ "MSAM", MSAM, OSTR,	0, 0,	"(mail send to terminal and mailbox)" },
   1214  1.23     lukem 	{ "MSND", MSND, OSTR,	0, 0,	"(mail send to terminal)" },
   1215  1.23     lukem 	{ "MSOM", MSOM, OSTR,	0, 0,	"(mail send to terminal or mailbox)" },
   1216  1.23     lukem 	{ "XCUP", CDUP, NOARGS,	1, 0,	"(change to parent directory)" },
   1217  1.23     lukem 	{ "XCWD", CWD,	OSTR,	1, 0,	"[ <sp> directory-name ]" },
   1218  1.23     lukem 	{ "XMKD", MKD,  STR1,	1, 0,	"<sp> path-name" },
   1219  1.23     lukem 	{ "XPWD", PWD,  NOARGS,	1, 0,	"(return current directory)" },
   1220  1.23     lukem 	{ "XRMD", RMD,  STR1,	1, 0,	"<sp> path-name" },
   1221  1.21     lukem 
   1222  1.23     lukem 	{ NULL,   0,    0,	0, 0,	0 }
   1223   1.1       cgd };
   1224   1.1       cgd 
   1225   1.1       cgd struct tab sitetab[] = {
   1226  1.23     lukem 	{ "UMASK", UMASK, ARGS,	1, 0,	"[ <sp> umask ]" },
   1227  1.23     lukem 	{ "IDLE",  IDLE,  ARGS,	1, 0,	"[ <sp> maximum-idle-time ]" },
   1228  1.23     lukem 	{ "CHMOD", CHMOD, NSTR,	1, 0,	"<sp> mode <sp> file-name" },
   1229  1.23     lukem 	{ "HELP",  HELP,  OSTR,	1, 0,	"[ <sp> <string> ]" },
   1230  1.23     lukem 	{ NULL,    0,     0,	0, 0,	0 }
   1231   1.1       cgd };
   1232   1.1       cgd 
   1233  1.23     lukem static	void	 	help __P((struct tab *, char *));
   1234  1.23     lukem static	struct tab     *lookup __P((struct tab *, const char *));
   1235  1.23     lukem static	void		opts __P((const char *));
   1236  1.23     lukem static	void		sizecmd __P((char *));
   1237  1.23     lukem static	void		toolong __P((int));
   1238  1.23     lukem static	int		yylex __P((void));
   1239   1.4   deraadt 
   1240  1.32    itojun extern int epsvall;
   1241  1.32    itojun 
   1242   1.4   deraadt static struct tab *
   1243   1.1       cgd lookup(p, cmd)
   1244   1.4   deraadt 	struct tab *p;
   1245  1.23     lukem 	const char *cmd;
   1246   1.1       cgd {
   1247   1.1       cgd 
   1248   1.1       cgd 	for (; p->name != NULL; p++)
   1249  1.23     lukem 		if (strcasecmp(cmd, p->name) == 0)
   1250   1.1       cgd 			return (p);
   1251   1.1       cgd 	return (0);
   1252   1.1       cgd }
   1253   1.1       cgd 
   1254   1.1       cgd #include <arpa/telnet.h>
   1255   1.1       cgd 
   1256   1.1       cgd /*
   1257   1.1       cgd  * getline - a hacked up version of fgets to ignore TELNET escape codes.
   1258   1.1       cgd  */
   1259   1.1       cgd char *
   1260   1.1       cgd getline(s, n, iop)
   1261   1.1       cgd 	char *s;
   1262   1.4   deraadt 	int n;
   1263   1.4   deraadt 	FILE *iop;
   1264   1.1       cgd {
   1265  1.27     lukem 	off_t b;
   1266   1.4   deraadt 	int c;
   1267  1.21     lukem 	char *cs;
   1268   1.1       cgd 
   1269   1.1       cgd 	cs = s;
   1270   1.1       cgd /* tmpline may contain saved command from urgent mode interruption */
   1271   1.1       cgd 	for (c = 0; tmpline[c] != '\0' && --n > 0; ++c) {
   1272   1.1       cgd 		*cs++ = tmpline[c];
   1273   1.1       cgd 		if (tmpline[c] == '\n') {
   1274   1.1       cgd 			*cs++ = '\0';
   1275   1.1       cgd 			if (debug)
   1276   1.1       cgd 				syslog(LOG_DEBUG, "command: %s", s);
   1277   1.1       cgd 			tmpline[0] = '\0';
   1278   1.1       cgd 			return(s);
   1279   1.1       cgd 		}
   1280   1.1       cgd 		if (c == 0)
   1281   1.1       cgd 			tmpline[0] = '\0';
   1282   1.1       cgd 	}
   1283   1.1       cgd 	while ((c = getc(iop)) != EOF) {
   1284  1.27     lukem 		total_bytes++;
   1285  1.27     lukem 		total_bytes_in++;
   1286   1.1       cgd 		c &= 0377;
   1287   1.1       cgd 		if (c == IAC) {
   1288   1.1       cgd 		    if ((c = getc(iop)) != EOF) {
   1289  1.27     lukem 			total_bytes++;
   1290  1.27     lukem 			total_bytes_in++;
   1291   1.1       cgd 			c &= 0377;
   1292   1.1       cgd 			switch (c) {
   1293   1.1       cgd 			case WILL:
   1294   1.1       cgd 			case WONT:
   1295   1.1       cgd 				c = getc(iop);
   1296  1.27     lukem 				total_bytes++;
   1297  1.27     lukem 				total_bytes_in++;
   1298  1.27     lukem 				b = printf("%c%c%c", IAC, DONT, 0377&c);
   1299  1.27     lukem 				total_bytes += b;
   1300  1.27     lukem 				total_bytes_out += b;
   1301   1.1       cgd 				(void) fflush(stdout);
   1302   1.1       cgd 				continue;
   1303   1.1       cgd 			case DO:
   1304   1.1       cgd 			case DONT:
   1305   1.1       cgd 				c = getc(iop);
   1306  1.27     lukem 				total_bytes++;
   1307  1.27     lukem 				total_bytes_in++;
   1308  1.27     lukem 				b = printf("%c%c%c", IAC, WONT, 0377&c);
   1309  1.27     lukem 				total_bytes += b;
   1310  1.27     lukem 				total_bytes_out += b;
   1311   1.1       cgd 				(void) fflush(stdout);
   1312   1.1       cgd 				continue;
   1313   1.1       cgd 			case IAC:
   1314   1.1       cgd 				break;
   1315   1.1       cgd 			default:
   1316   1.1       cgd 				continue;	/* ignore command */
   1317   1.1       cgd 			}
   1318   1.1       cgd 		    }
   1319   1.1       cgd 		}
   1320   1.1       cgd 		*cs++ = c;
   1321   1.1       cgd 		if (--n <= 0 || c == '\n')
   1322   1.1       cgd 			break;
   1323   1.1       cgd 	}
   1324   1.1       cgd 	if (c == EOF && cs == s)
   1325   1.1       cgd 		return (NULL);
   1326   1.1       cgd 	*cs++ = '\0';
   1327   1.4   deraadt 	if (debug) {
   1328   1.4   deraadt 		if (!guest && strncasecmp("pass ", s, 5) == 0) {
   1329   1.4   deraadt 			/* Don't syslog passwords */
   1330   1.4   deraadt 			syslog(LOG_DEBUG, "command: %.5s ???", s);
   1331   1.4   deraadt 		} else {
   1332  1.21     lukem 			char *cp;
   1333  1.21     lukem 			int len;
   1334   1.4   deraadt 
   1335   1.4   deraadt 			/* Don't syslog trailing CR-LF */
   1336   1.4   deraadt 			len = strlen(s);
   1337   1.4   deraadt 			cp = s + len - 1;
   1338   1.4   deraadt 			while (cp >= s && (*cp == '\n' || *cp == '\r')) {
   1339   1.4   deraadt 				--cp;
   1340   1.4   deraadt 				--len;
   1341   1.4   deraadt 			}
   1342   1.4   deraadt 			syslog(LOG_DEBUG, "command: %.*s", len, s);
   1343   1.4   deraadt 		}
   1344   1.4   deraadt 	}
   1345   1.1       cgd 	return (s);
   1346   1.1       cgd }
   1347   1.1       cgd 
   1348   1.1       cgd static void
   1349   1.4   deraadt toolong(signo)
   1350   1.4   deraadt 	int signo;
   1351   1.1       cgd {
   1352   1.1       cgd 
   1353   1.1       cgd 	reply(421,
   1354  1.12     lukem 	    "Timeout (%d seconds): closing control connection.",
   1355  1.12     lukem 	    curclass.timeout);
   1356   1.4   deraadt 	if (logging)
   1357   1.4   deraadt 		syslog(LOG_INFO, "User %s timed out after %d seconds",
   1358  1.12     lukem 		    (pw ? pw -> pw_name : "unknown"), curclass.timeout);
   1359   1.1       cgd 	dologout(1);
   1360   1.1       cgd }
   1361   1.1       cgd 
   1362   1.4   deraadt static int
   1363   1.1       cgd yylex()
   1364   1.1       cgd {
   1365   1.1       cgd 	static int cpos, state;
   1366   1.4   deraadt 	char *cp, *cp2;
   1367   1.4   deraadt 	struct tab *p;
   1368  1.22     lukem 	int n;
   1369   1.4   deraadt 	char c;
   1370   1.1       cgd 
   1371  1.23     lukem 	switch (state) {
   1372   1.1       cgd 
   1373  1.23     lukem 	case CMD:
   1374  1.23     lukem 		hasyyerrored = 0;
   1375  1.23     lukem 		(void) signal(SIGALRM, toolong);
   1376  1.23     lukem 		(void) alarm(curclass.timeout);
   1377  1.23     lukem 		if (getline(cbuf, sizeof(cbuf)-1, stdin) == NULL) {
   1378  1.23     lukem 			reply(221, "You could at least say goodbye.");
   1379  1.23     lukem 			dologout(0);
   1380  1.23     lukem 		}
   1381  1.23     lukem 		(void) alarm(0);
   1382   1.3       cgd #ifdef HASSETPROCTITLE
   1383  1.23     lukem 		if (strncasecmp(cbuf, "PASS", 4) != 0)
   1384  1.23     lukem 			setproctitle("%s: %s", proctitle, cbuf);
   1385   1.3       cgd #endif /* HASSETPROCTITLE */
   1386  1.23     lukem 		if ((cp = strchr(cbuf, '\r'))) {
   1387  1.23     lukem 			*cp++ = '\n';
   1388  1.23     lukem 			*cp = '\0';
   1389  1.23     lukem 		}
   1390  1.23     lukem 		if ((cp = strpbrk(cbuf, " \n")))
   1391  1.23     lukem 			cpos = cp - cbuf;
   1392  1.23     lukem 		if (cpos == 0)
   1393  1.23     lukem 			cpos = 4;
   1394  1.23     lukem 		c = cbuf[cpos];
   1395  1.23     lukem 		cbuf[cpos] = '\0';
   1396  1.23     lukem 		p = lookup(cmdtab, cbuf);
   1397  1.23     lukem 		cbuf[cpos] = c;
   1398  1.23     lukem 		if (p != NULL) {
   1399  1.23     lukem 			if (p->implemented == 0) {
   1400  1.23     lukem 				reply(502, "%s command not implemented.",
   1401  1.23     lukem 				    p->name);
   1402  1.23     lukem 				hasyyerrored = 1;
   1403  1.23     lukem 				break;
   1404  1.23     lukem 			}
   1405  1.23     lukem 			state = p->state;
   1406  1.23     lukem 			yylval.s = p->name;
   1407  1.23     lukem 			return (p->token);
   1408  1.23     lukem 		}
   1409  1.23     lukem 		break;
   1410  1.23     lukem 
   1411  1.23     lukem 	case SITECMD:
   1412  1.23     lukem 		if (cbuf[cpos] == ' ') {
   1413  1.23     lukem 			cpos++;
   1414  1.23     lukem 			return (SP);
   1415  1.23     lukem 		}
   1416  1.23     lukem 		cp = &cbuf[cpos];
   1417  1.23     lukem 		if ((cp2 = strpbrk(cp, " \n")))
   1418  1.23     lukem 			cpos = cp2 - cbuf;
   1419  1.23     lukem 		c = cbuf[cpos];
   1420  1.23     lukem 		cbuf[cpos] = '\0';
   1421  1.23     lukem 		p = lookup(sitetab, cp);
   1422  1.23     lukem 		cbuf[cpos] = c;
   1423  1.23     lukem 		if (p != NULL) {
   1424  1.23     lukem 			if (p->implemented == 0) {
   1425  1.23     lukem 				reply(502, "SITE %s command not implemented.",
   1426  1.23     lukem 				    p->name);
   1427  1.23     lukem 				hasyyerrored = 1;
   1428  1.23     lukem 				break;
   1429  1.23     lukem 			}
   1430  1.23     lukem 			state = p->state;
   1431  1.23     lukem 			yylval.s = p->name;
   1432  1.23     lukem 			return (p->token);
   1433  1.23     lukem 		}
   1434  1.23     lukem 		break;
   1435  1.23     lukem 
   1436  1.23     lukem 	case OSTR:
   1437  1.23     lukem 		if (cbuf[cpos] == '\n') {
   1438  1.23     lukem 			state = CMD;
   1439  1.23     lukem 			return (CRLF);
   1440  1.23     lukem 		}
   1441  1.23     lukem 		/* FALLTHROUGH */
   1442  1.23     lukem 
   1443  1.23     lukem 	case STR1:
   1444  1.23     lukem 	case ZSTR1:
   1445  1.23     lukem 	dostr1:
   1446  1.23     lukem 		if (cbuf[cpos] == ' ') {
   1447  1.23     lukem 			cpos++;
   1448  1.23     lukem 			state = state == OSTR ? STR2 : ++state;
   1449  1.23     lukem 			return (SP);
   1450  1.23     lukem 		}
   1451  1.23     lukem 		break;
   1452  1.23     lukem 
   1453  1.23     lukem 	case ZSTR2:
   1454  1.23     lukem 		if (cbuf[cpos] == '\n') {
   1455  1.23     lukem 			state = CMD;
   1456  1.23     lukem 			return (CRLF);
   1457  1.23     lukem 		}
   1458  1.23     lukem 		/* FALLTHROUGH */
   1459  1.23     lukem 
   1460  1.23     lukem 	case STR2:
   1461  1.23     lukem 		cp = &cbuf[cpos];
   1462  1.23     lukem 		n = strlen(cp);
   1463  1.23     lukem 		cpos += n - 1;
   1464  1.23     lukem 		/*
   1465  1.23     lukem 		 * Make sure the string is nonempty and \n terminated.
   1466  1.23     lukem 		 */
   1467  1.23     lukem 		if (n > 1 && cbuf[cpos] == '\n') {
   1468  1.23     lukem 			cbuf[cpos] = '\0';
   1469  1.23     lukem 			yylval.s = xstrdup(cp);
   1470  1.23     lukem 			cbuf[cpos] = '\n';
   1471  1.23     lukem 			state = ARGS;
   1472  1.23     lukem 			return (STRING);
   1473  1.23     lukem 		}
   1474  1.23     lukem 		break;
   1475  1.23     lukem 
   1476  1.23     lukem 	case NSTR:
   1477  1.23     lukem 		if (cbuf[cpos] == ' ') {
   1478  1.23     lukem 			cpos++;
   1479  1.23     lukem 			return (SP);
   1480  1.23     lukem 		}
   1481  1.23     lukem 		if (isdigit(cbuf[cpos])) {
   1482  1.23     lukem 			cp = &cbuf[cpos];
   1483  1.23     lukem 			while (isdigit(cbuf[++cpos]))
   1484  1.23     lukem 				;
   1485   1.1       cgd 			c = cbuf[cpos];
   1486   1.1       cgd 			cbuf[cpos] = '\0';
   1487  1.23     lukem 			yylval.i = atoi(cp);
   1488   1.1       cgd 			cbuf[cpos] = c;
   1489  1.23     lukem 			state = STR1;
   1490  1.23     lukem 			return (NUMBER);
   1491  1.23     lukem 		}
   1492  1.23     lukem 		state = STR1;
   1493  1.23     lukem 		goto dostr1;
   1494   1.1       cgd 
   1495  1.23     lukem 	case ARGS:
   1496  1.23     lukem 		if (isdigit(cbuf[cpos])) {
   1497   1.1       cgd 			cp = &cbuf[cpos];
   1498  1.23     lukem 			while (isdigit(cbuf[++cpos]))
   1499  1.23     lukem 				;
   1500   1.1       cgd 			c = cbuf[cpos];
   1501   1.1       cgd 			cbuf[cpos] = '\0';
   1502  1.23     lukem 			yylval.i = atoi(cp);
   1503   1.1       cgd 			cbuf[cpos] = c;
   1504  1.23     lukem 			return (NUMBER);
   1505  1.32    itojun 		}
   1506  1.32    itojun 		if (strncasecmp(&cbuf[cpos], "ALL", 3) == 0
   1507  1.32    itojun 		 && !isalnum(cbuf[cpos + 3])) {
   1508  1.32    itojun 			yylval.s = xstrdup("ALL");
   1509  1.32    itojun 			cpos += 3;
   1510  1.32    itojun 			return ALL;
   1511  1.23     lukem 		}
   1512  1.23     lukem 		switch (cbuf[cpos++]) {
   1513  1.23     lukem 
   1514  1.23     lukem 		case '\n':
   1515   1.1       cgd 			state = CMD;
   1516  1.23     lukem 			return (CRLF);
   1517  1.23     lukem 
   1518  1.23     lukem 		case ' ':
   1519  1.23     lukem 			return (SP);
   1520  1.23     lukem 
   1521  1.23     lukem 		case ',':
   1522  1.23     lukem 			return (COMMA);
   1523  1.23     lukem 
   1524  1.23     lukem 		case 'A':
   1525  1.23     lukem 		case 'a':
   1526  1.23     lukem 			return (A);
   1527  1.23     lukem 
   1528  1.23     lukem 		case 'B':
   1529  1.23     lukem 		case 'b':
   1530  1.23     lukem 			return (B);
   1531  1.23     lukem 
   1532  1.23     lukem 		case 'C':
   1533  1.23     lukem 		case 'c':
   1534  1.23     lukem 			return (C);
   1535  1.23     lukem 
   1536  1.23     lukem 		case 'E':
   1537  1.23     lukem 		case 'e':
   1538  1.23     lukem 			return (E);
   1539  1.23     lukem 
   1540  1.23     lukem 		case 'F':
   1541  1.23     lukem 		case 'f':
   1542  1.23     lukem 			return (F);
   1543  1.23     lukem 
   1544  1.23     lukem 		case 'I':
   1545  1.23     lukem 		case 'i':
   1546  1.23     lukem 			return (I);
   1547   1.1       cgd 
   1548  1.23     lukem 		case 'L':
   1549  1.23     lukem 		case 'l':
   1550  1.23     lukem 			return (L);
   1551   1.1       cgd 
   1552  1.23     lukem 		case 'N':
   1553  1.23     lukem 		case 'n':
   1554  1.23     lukem 			return (N);
   1555   1.1       cgd 
   1556  1.23     lukem 		case 'P':
   1557  1.23     lukem 		case 'p':
   1558  1.23     lukem 			return (P);
   1559   1.1       cgd 
   1560  1.23     lukem 		case 'R':
   1561  1.23     lukem 		case 'r':
   1562  1.23     lukem 			return (R);
   1563   1.1       cgd 
   1564  1.23     lukem 		case 'S':
   1565  1.23     lukem 		case 's':
   1566  1.23     lukem 			return (S);
   1567   1.1       cgd 
   1568  1.23     lukem 		case 'T':
   1569  1.23     lukem 		case 't':
   1570  1.23     lukem 			return (T);
   1571   1.1       cgd 
   1572  1.23     lukem 		}
   1573  1.23     lukem 		break;
   1574  1.21     lukem 
   1575  1.23     lukem 	case NOARGS:
   1576  1.23     lukem 		if (cbuf[cpos] == '\n') {
   1577  1.23     lukem 			state = CMD;
   1578  1.23     lukem 			return (CRLF);
   1579   1.1       cgd 		}
   1580  1.23     lukem 		c = cbuf[cpos];
   1581  1.23     lukem 		cbuf[cpos] = '\0';
   1582  1.23     lukem 		reply(501, "'%s' command does not take any arguments.", cbuf);
   1583  1.23     lukem 		hasyyerrored = 1;
   1584  1.23     lukem 		cbuf[cpos] = c;
   1585  1.23     lukem 		break;
   1586  1.23     lukem 
   1587  1.23     lukem 	default:
   1588  1.23     lukem 		fatal("Unknown state in scanner.");
   1589   1.1       cgd 	}
   1590  1.23     lukem 	yyerror(NULL);
   1591  1.23     lukem 	state = CMD;
   1592  1.23     lukem 	longjmp(errcatch, 0);
   1593  1.23     lukem 	/* NOTREACHED */
   1594   1.1       cgd }
   1595   1.1       cgd 
   1596  1.22     lukem /* ARGSUSED */
   1597  1.22     lukem void
   1598  1.22     lukem yyerror(s)
   1599  1.22     lukem 	char *s;
   1600  1.22     lukem {
   1601  1.22     lukem 	char *cp;
   1602  1.22     lukem 
   1603  1.22     lukem 	if (hasyyerrored)
   1604  1.22     lukem 		return;
   1605  1.22     lukem 	if ((cp = strchr(cbuf,'\n')) != NULL)
   1606  1.22     lukem 		*cp = '\0';
   1607  1.22     lukem 	reply(500, "'%s': command not understood.", cbuf);
   1608  1.22     lukem 	hasyyerrored = 1;
   1609  1.22     lukem }
   1610  1.22     lukem 
   1611   1.4   deraadt static void
   1612   1.1       cgd help(ctab, s)
   1613   1.1       cgd 	struct tab *ctab;
   1614   1.1       cgd 	char *s;
   1615   1.1       cgd {
   1616   1.4   deraadt 	struct tab *c;
   1617   1.4   deraadt 	int width, NCMDS;
   1618  1.27     lukem 	off_t b;
   1619   1.1       cgd 	char *type;
   1620   1.1       cgd 
   1621   1.1       cgd 	if (ctab == sitetab)
   1622   1.1       cgd 		type = "SITE ";
   1623   1.1       cgd 	else
   1624   1.1       cgd 		type = "";
   1625   1.1       cgd 	width = 0, NCMDS = 0;
   1626   1.1       cgd 	for (c = ctab; c->name != NULL; c++) {
   1627   1.1       cgd 		int len = strlen(c->name);
   1628   1.1       cgd 
   1629   1.1       cgd 		if (len > width)
   1630   1.1       cgd 			width = len;
   1631   1.1       cgd 		NCMDS++;
   1632   1.1       cgd 	}
   1633   1.1       cgd 	width = (width + 8) &~ 7;
   1634   1.1       cgd 	if (s == 0) {
   1635   1.4   deraadt 		int i, j, w;
   1636   1.1       cgd 		int columns, lines;
   1637   1.1       cgd 
   1638  1.28     lukem 		lreply(214, "");
   1639  1.28     lukem 		lreply(0, "The following %scommands are recognized.", type);
   1640  1.27     lukem 		lreply(0, "(`-' = not implemented, `+' = supports options)");
   1641   1.1       cgd 		columns = 76 / width;
   1642   1.1       cgd 		if (columns == 0)
   1643   1.1       cgd 			columns = 1;
   1644   1.1       cgd 		lines = (NCMDS + columns - 1) / columns;
   1645   1.1       cgd 		for (i = 0; i < lines; i++) {
   1646  1.28     lukem 			b = printf("    ");
   1647  1.27     lukem 			total_bytes += b;
   1648  1.27     lukem 			total_bytes_out += b;
   1649   1.1       cgd 			for (j = 0; j < columns; j++) {
   1650   1.1       cgd 				c = ctab + j * lines + i;
   1651  1.27     lukem 				b = printf("%s", c->name);
   1652  1.27     lukem 				total_bytes += b;
   1653  1.27     lukem 				total_bytes_out += b;
   1654  1.23     lukem 				w = strlen(c->name);
   1655  1.23     lukem 				if (! c->implemented) {
   1656  1.24     lukem 					putchar('-');
   1657  1.28     lukem 					total_bytes++;
   1658  1.28     lukem 					total_bytes_out++;
   1659  1.23     lukem 					w++;
   1660  1.23     lukem 				}
   1661  1.23     lukem 				if (c->hasopts) {
   1662  1.23     lukem 					putchar('+');
   1663  1.28     lukem 					total_bytes++;
   1664  1.28     lukem 					total_bytes_out++;
   1665  1.23     lukem 					w++;
   1666  1.23     lukem 				}
   1667   1.1       cgd 				if (c + lines >= &ctab[NCMDS])
   1668   1.1       cgd 					break;
   1669   1.1       cgd 				while (w < width) {
   1670   1.1       cgd 					putchar(' ');
   1671  1.28     lukem 					total_bytes++;
   1672  1.28     lukem 					total_bytes_out++;
   1673   1.1       cgd 					w++;
   1674   1.1       cgd 				}
   1675   1.1       cgd 			}
   1676  1.27     lukem 			b = printf("\r\n");
   1677  1.27     lukem 			total_bytes += b;
   1678  1.27     lukem 			total_bytes_out += b;
   1679   1.1       cgd 		}
   1680   1.1       cgd 		(void) fflush(stdout);
   1681   1.1       cgd 		reply(214, "Direct comments to ftp-bugs@%s.", hostname);
   1682   1.1       cgd 		return;
   1683   1.1       cgd 	}
   1684   1.1       cgd 	c = lookup(ctab, s);
   1685   1.1       cgd 	if (c == (struct tab *)0) {
   1686   1.1       cgd 		reply(502, "Unknown command %s.", s);
   1687   1.1       cgd 		return;
   1688   1.1       cgd 	}
   1689   1.1       cgd 	if (c->implemented)
   1690   1.1       cgd 		reply(214, "Syntax: %s%s %s", type, c->name, c->help);
   1691   1.1       cgd 	else
   1692  1.23     lukem 		reply(214, "%s%-*s\t%s; not implemented.", type, width,
   1693   1.1       cgd 		    c->name, c->help);
   1694   1.1       cgd }
   1695   1.1       cgd 
   1696   1.4   deraadt static void
   1697   1.1       cgd sizecmd(filename)
   1698   1.4   deraadt 	char *filename;
   1699   1.1       cgd {
   1700   1.1       cgd 	switch (type) {
   1701   1.1       cgd 	case TYPE_L:
   1702   1.1       cgd 	case TYPE_I: {
   1703   1.1       cgd 		struct stat stbuf;
   1704   1.4   deraadt 		if (stat(filename, &stbuf) < 0 || !S_ISREG(stbuf.st_mode))
   1705   1.1       cgd 			reply(550, "%s: not a plain file.", filename);
   1706   1.1       cgd 		else
   1707  1.30      ross 			reply(213, "%qu", (qufmt_t)stbuf.st_size);
   1708   1.4   deraadt 		break; }
   1709   1.1       cgd 	case TYPE_A: {
   1710   1.1       cgd 		FILE *fin;
   1711   1.4   deraadt 		int c;
   1712   1.4   deraadt 		off_t count;
   1713   1.1       cgd 		struct stat stbuf;
   1714   1.1       cgd 		fin = fopen(filename, "r");
   1715   1.1       cgd 		if (fin == NULL) {
   1716   1.1       cgd 			perror_reply(550, filename);
   1717   1.1       cgd 			return;
   1718   1.1       cgd 		}
   1719   1.4   deraadt 		if (fstat(fileno(fin), &stbuf) < 0 || !S_ISREG(stbuf.st_mode)) {
   1720   1.1       cgd 			reply(550, "%s: not a plain file.", filename);
   1721   1.1       cgd 			(void) fclose(fin);
   1722   1.1       cgd 			return;
   1723   1.1       cgd 		}
   1724   1.1       cgd 
   1725   1.1       cgd 		count = 0;
   1726   1.1       cgd 		while((c=getc(fin)) != EOF) {
   1727   1.1       cgd 			if (c == '\n')	/* will get expanded to \r\n */
   1728   1.1       cgd 				count++;
   1729   1.1       cgd 			count++;
   1730   1.1       cgd 		}
   1731   1.1       cgd 		(void) fclose(fin);
   1732   1.1       cgd 
   1733  1.30      ross 		reply(213, "%qd", (qdfmt_t)count);
   1734   1.4   deraadt 		break; }
   1735   1.1       cgd 	default:
   1736   1.1       cgd 		reply(504, "SIZE not implemented for Type %c.", "?AEIL"[type]);
   1737   1.1       cgd 	}
   1738   1.1       cgd }
   1739  1.22     lukem 
   1740  1.23     lukem static void
   1741  1.23     lukem opts(command)
   1742  1.23     lukem 	const char *command;
   1743  1.23     lukem {
   1744  1.23     lukem 	struct tab *c;
   1745  1.23     lukem 	char *ep;
   1746  1.23     lukem 
   1747  1.23     lukem 	if ((ep = strchr(command, ' ')) != NULL)
   1748  1.23     lukem 		*ep++ = '\0';
   1749  1.23     lukem 	c = lookup(cmdtab, command);
   1750  1.23     lukem 	if (c == NULL) {
   1751  1.23     lukem 		reply(502, "Unknown command %s.", command);
   1752  1.23     lukem 		return;
   1753  1.23     lukem 	}
   1754  1.23     lukem 	if (c->implemented == 0) {
   1755  1.23     lukem 		reply(502, "%s command not implemented.", c->name);
   1756  1.23     lukem 		return;
   1757  1.23     lukem 	}
   1758  1.23     lukem 	if (c->hasopts == 0) {
   1759  1.23     lukem 		reply(501, "%s command does not support persistent options.",
   1760  1.23     lukem 		    c->name);
   1761  1.23     lukem 		return;
   1762  1.23     lukem 	}
   1763  1.23     lukem 
   1764  1.23     lukem 	if (ep != NULL && *ep != '\0') {
   1765  1.23     lukem 		if (c->options != NULL)
   1766  1.23     lukem 			free(c->options);
   1767  1.23     lukem 		c->options = xstrdup(ep);
   1768  1.23     lukem 	}
   1769  1.23     lukem 	if (c->options != NULL)
   1770  1.23     lukem 		reply(200, "Options for %s are '%s'.", c->name, c->options);
   1771  1.23     lukem 	else
   1772  1.23     lukem 		reply(200, "No options defined for %s.", c->name);
   1773  1.23     lukem }
   1774