home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / UNIX / Networking / wu-ftpd-2.4.2b13-MIHS / src / ftpcmd.y < prev    next >
Encoding:
Text File  |  1997-03-03  |  44.4 KB  |  1,628 lines

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