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