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