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