BSD 4_4 release
[unix-history] / usr / src / libexec / ftpd / ftpcmd.y
index 5025b48..f0a4bde 100644 (file)
@@ -1,22 +1,36 @@
 /*
 /*
- * Copyright (c) 1985, 1988 Regents of the University of California.
- * All rights reserved.
+ * Copyright (c) 1985, 1988, 1993
+ *     The Regents of the University of California.  All rights reserved.
  *
  *
- * Redistribution and use in source and binary forms are permitted
- * provided that: (1) source distributions retain this entire copyright
- * notice and comment, and (2) distributions including binaries display
- * the following acknowledgement:  ``This product includes software
- * developed by the University of California, Berkeley and its contributors''
- * in the documentation or other materials provided with the distribution
- * and in all advertising materials mentioning features or use of this
- * software. Neither the name of the University nor the names of its
- * contributors may be used to endorse or promote products derived
- * from this software without specific prior written permission.
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
  *
  *
- *     @(#)ftpcmd.y    5.23 (Berkeley) 6/1/90
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ *     @(#)ftpcmd.y    8.1 (Berkeley) 6/4/93
  */
 
 /*
  */
 
 /*
 %{
 
 #ifndef lint
 %{
 
 #ifndef lint
-static char sccsid[] = "@(#)ftpcmd.y   5.23 (Berkeley) 6/1/90";
+static char sccsid[] = "@(#)ftpcmd.y   8.1 (Berkeley) 6/4/93";
 #endif /* not lint */
 
 #include <sys/param.h>
 #include <sys/socket.h>
 #endif /* not lint */
 
 #include <sys/param.h>
 #include <sys/socket.h>
+#include <sys/stat.h>
 
 #include <netinet/in.h>
 
 #include <netinet/in.h>
-
 #include <arpa/ftp.h>
 
 #include <arpa/ftp.h>
 
-#include <stdio.h>
 #include <signal.h>
 #include <signal.h>
-#include <ctype.h>
-#include <pwd.h>
 #include <setjmp.h>
 #include <syslog.h>
 #include <setjmp.h>
 #include <syslog.h>
-#include <sys/stat.h>
 #include <time.h>
 #include <time.h>
+#include <pwd.h>
+#include <errno.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <ctype.h>
+#include <stdlib.h>
+#include <string.h>
+#include "extern.h"
 
 extern struct sockaddr_in data_dest;
 extern int logged_in;
 
 extern struct sockaddr_in data_dest;
 extern int logged_in;
@@ -63,7 +81,6 @@ extern        char *globerr;
 extern int usedefault;
 extern  int transflag;
 extern  char tmpline[];
 extern int usedefault;
 extern  int transflag;
 extern  char tmpline[];
-char   **glob();
 
 off_t  restart_point;
 
 
 off_t  restart_point;
 
@@ -73,7 +90,6 @@ static        int cmd_bytesz;
 char   cbuf[512];
 char   *fromname;
 
 char   cbuf[512];
 char   *fromname;
 
-char   *index();
 %}
 
 %token
 %}
 
 %token
@@ -225,7 +241,7 @@ cmd:                USER SP username CRLF
                }
        |       NLST check_login SP STRING CRLF
                = {
                }
        |       NLST check_login SP STRING CRLF
                = {
-                       if ($2 && $4 != NULL) 
+                       if ($2 && $4 != NULL)
                                send_file_list((char *) $4);
                        if ($4 != NULL)
                                free((char *) $4);
                                send_file_list((char *) $4);
                        if ($4 != NULL)
                                free((char *) $4);
@@ -451,13 +467,13 @@ cmd:              USER SP username CRLF
                        if ($2 && $4 != NULL) {
                                struct stat stbuf;
                                if (stat((char *) $4, &stbuf) < 0)
                        if ($2 && $4 != NULL) {
                                struct stat stbuf;
                                if (stat((char *) $4, &stbuf) < 0)
-                                       perror_reply(550, "%s", (char *) $4);
+                                       reply(550, "%s: %s",
+                                           (char *)$4, strerror(errno));
                                else if ((stbuf.st_mode&S_IFMT) != S_IFREG) {
                                        reply(550, "%s: not a plain file.",
                                                (char *) $4);
                                } else {
                                        register struct tm *t;
                                else if ((stbuf.st_mode&S_IFMT) != S_IFREG) {
                                        reply(550, "%s: not a plain file.",
                                                (char *) $4);
                                } else {
                                        register struct tm *t;
-                                       struct tm *gmtime();
                                        t = gmtime(&stbuf.st_mtime);
                                        reply(213,
                                            "19%02d%02d%02d%02d%02d%02d",
                                        t = gmtime(&stbuf.st_mtime);
                                        reply(213,
                                            "19%02d%02d%02d%02d%02d%02d",
@@ -492,15 +508,13 @@ rcmd:             RNFR check_login SP pathname CRLF
                }
        |       REST SP byte_size CRLF
                = {
                }
        |       REST SP byte_size CRLF
                = {
-                       long atol();
-
                        fromname = (char *) 0;
                        restart_point = $3;
                        reply(350, "Restarting at %ld. %s", restart_point,
                            "Send STORE or RETRIEVE to initiate transfer.");
                }
        ;
                        fromname = (char *) 0;
                        restart_point = $3;
                        reply(350, "Restarting at %ld. %s", restart_point,
                            "Send STORE or RETRIEVE to initiate transfer.");
                }
        ;
-               
+
 username:      STRING
        ;
 
 username:      STRING
        ;
 
@@ -514,7 +528,7 @@ password:   /* empty */
 byte_size:     NUMBER
        ;
 
 byte_size:     NUMBER
        ;
 
-host_port:     NUMBER COMMA NUMBER COMMA NUMBER COMMA NUMBER COMMA 
+host_port:     NUMBER COMMA NUMBER COMMA NUMBER COMMA NUMBER COMMA
                NUMBER COMMA NUMBER
                = {
                        register char *a, *p;
                NUMBER COMMA NUMBER
                = {
                        register char *a, *p;
@@ -619,7 +633,7 @@ pathname:   pathstring
                 * This is a valid reply in some cases but not in others.
                 */
                if (logged_in && $1 && strncmp((char *) $1, "~", 1) == 0) {
                 * This is a valid reply in some cases but not in others.
                 */
                if (logged_in && $1 && strncmp((char *) $1, "~", 1) == 0) {
-                       *(char **)&($$) = *glob((char *) $1);
+                       *(char **)&($$) = *ftpglob((char *) $1);
                        if (globerr != NULL) {
                                reply(550, globerr);
                                $$ = NULL;
                        if (globerr != NULL) {
                                reply(550, globerr);
                                $$ = NULL;
@@ -714,7 +728,7 @@ struct tab cmdtab[] = {             /* In order defined in RFC 765 */
        { "MRSQ", MRSQ, OSTR, 0,        "(mail recipient scheme question)" },
        { "MRCP", MRCP, STR1, 0,        "(mail recipient)" },
        { "ALLO", ALLO, ARGS, 1,        "allocate storage (vacuously)" },
        { "MRSQ", MRSQ, OSTR, 0,        "(mail recipient scheme question)" },
        { "MRCP", MRCP, STR1, 0,        "(mail recipient)" },
        { "ALLO", ALLO, ARGS, 1,        "allocate storage (vacuously)" },
-       { "REST", REST, ARGS, 1,        "(restart command)" },
+       { "REST", REST, ARGS, 1,        "<sp> offset (restart command)" },
        { "RNFR", RNFR, STR1, 1,        "<sp> file-name" },
        { "RNTO", RNTO, STR1, 1,        "<sp> file-name" },
        { "ABOR", ABOR, ARGS, 1,        "(abort operation)" },
        { "RNFR", RNFR, STR1, 1,        "<sp> file-name" },
        { "RNTO", RNTO, STR1, 1,        "<sp> file-name" },
        { "ABOR", ABOR, ARGS, 1,        "(abort operation)" },
@@ -750,7 +764,15 @@ struct tab sitetab[] = {
        { NULL,   0,    0,    0,        0 }
 };
 
        { NULL,   0,    0,    0,        0 }
 };
 
-struct tab *
+static char    *copy __P((char *));
+static void     help __P((struct tab *, char *));
+static struct tab *
+                lookup __P((struct tab *, char *));
+static void     sizecmd __P((char *));
+static void     toolong __P((int));
+static int      yylex __P((void));
+
+static struct tab *
 lookup(p, cmd)
        register struct tab *p;
        char *cmd;
 lookup(p, cmd)
        register struct tab *p;
        char *cmd;
@@ -770,6 +792,7 @@ lookup(p, cmd)
 char *
 getline(s, n, iop)
        char *s;
 char *
 getline(s, n, iop)
        char *s;
+       int n;
        register FILE *iop;
 {
        register c;
        register FILE *iop;
 {
        register c;
@@ -821,37 +844,48 @@ getline(s, n, iop)
        if (c == EOF && cs == s)
                return (NULL);
        *cs++ = '\0';
        if (c == EOF && cs == s)
                return (NULL);
        *cs++ = '\0';
-       if (debug)
-               syslog(LOG_DEBUG, "command: %s", s);
+       if (debug) {
+               if (!guest && strncasecmp("pass ", s, 5) == 0) {
+                       /* Don't syslog passwords */
+                       syslog(LOG_DEBUG, "command: %.5s ???", s);
+               } else {
+                       register char *cp;
+                       register int len;
+
+                       /* Don't syslog trailing CR-LF */
+                       len = strlen(s);
+                       cp = s + len - 1;
+                       while (cp >= s && (*cp == '\n' || *cp == '\r')) {
+                               --cp;
+                               --len;
+                       }
+                       syslog(LOG_DEBUG, "command: %.*s", len, s);
+               }
+       }
        return (s);
 }
 
        return (s);
 }
 
-static int
-toolong()
+static void
+toolong(signo)
+       int signo;
 {
 {
-       time_t now;
-       extern char *ctime();
-       extern time_t time();
 
        reply(421,
 
        reply(421,
-         "Timeout (%d seconds): closing control connection.", timeout);
-       (void) time(&now);
-       if (logging) {
-               syslog(LOG_INFO,
-                       "User %s timed out after %d seconds at %s",
-                       (pw ? pw -> pw_name : "unknown"), timeout, ctime(&now));
-       }
+           "Timeout (%d seconds): closing control connection.", timeout);
+       if (logging)
+               syslog(LOG_INFO, "User %s timed out after %d seconds",
+                   (pw ? pw -> pw_name : "unknown"), timeout);
        dologout(1);
 }
 
        dologout(1);
 }
 
+static int
 yylex()
 {
        static int cpos, state;
        register char *cp, *cp2;
        register struct tab *p;
        int n;
 yylex()
 {
        static int cpos, state;
        register char *cp, *cp2;
        register struct tab *p;
        int n;
-       char c, *strpbrk();
-       char *copy();
+       char c;
 
        for (;;) {
                switch (state) {
 
        for (;;) {
                switch (state) {
@@ -868,7 +902,7 @@ yylex()
                        if (strncasecmp(cbuf, "PASS", 4) != NULL)
                                setproctitle("%s: %s", proctitle, cbuf);
 #endif /* SETPROCTITLE */
                        if (strncasecmp(cbuf, "PASS", 4) != NULL)
                                setproctitle("%s: %s", proctitle, cbuf);
 #endif /* SETPROCTITLE */
-                       if ((cp = index(cbuf, '\r'))) {
+                       if ((cp = strchr(cbuf, '\r'))) {
                                *cp++ = '\n';
                                *cp = '\0';
                        }
                                *cp++ = '\n';
                                *cp = '\0';
                        }
@@ -1062,6 +1096,7 @@ yylex()
        }
 }
 
        }
 }
 
+void
 upper(s)
        register char *s;
 {
 upper(s)
        register char *s;
 {
@@ -1072,12 +1107,11 @@ upper(s)
        }
 }
 
        }
 }
 
-char *
+static char *
 copy(s)
        char *s;
 {
        char *p;
 copy(s)
        char *s;
 {
        char *p;
-       extern char *malloc(), *strcpy();
 
        p = malloc((unsigned) strlen(s) + 1);
        if (p == NULL)
 
        p = malloc((unsigned) strlen(s) + 1);
        if (p == NULL)
@@ -1086,6 +1120,7 @@ copy(s)
        return (p);
 }
 
        return (p);
 }
 
+static void
 help(ctab, s)
        struct tab *ctab;
        char *s;
 help(ctab, s)
        struct tab *ctab;
        char *s;
@@ -1150,8 +1185,9 @@ help(ctab, s)
                    c->name, c->help);
 }
 
                    c->name, c->help);
 }
 
+static void
 sizecmd(filename)
 sizecmd(filename)
-char *filename;
+       char *filename;
 {
        switch (type) {
        case TYPE_L:
 {
        switch (type) {
        case TYPE_L: