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