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