BSD 4_3_Reno release
[unix-history] / usr / src / libexec / ftpd / ftpd.c
index 5e2c33e..1c5f751 100644 (file)
@@ -1,28 +1,30 @@
 /*
 /*
- * Copyright (c) 1985, 1988 Regents of the University of California.
+ * Copyright (c) 1985, 1988, 1990 Regents of the University of California.
  * All rights reserved.
  *
  * All rights reserved.
  *
- * Redistribution and use in source and binary forms are permitted
- * provided that the above copyright notice and this paragraph are
- * duplicated in all such forms and that any documentation,
- * advertising materials, and other materials related to such
- * distribution and use acknowledge that the software was developed
- * by the University of California, Berkeley.  The name of the
- * University may not 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 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.
  */
 
 #ifndef lint
 char copyright[] =
  */
 
 #ifndef lint
 char copyright[] =
-"@(#) Copyright (c) 1985, 1988 Regents of the University of California.\n\
+"@(#) Copyright (c) 1985, 1988, 1990 Regents of the University of California.\n\
  All rights reserved.\n";
 #endif /* not lint */
 
 #ifndef lint
  All rights reserved.\n";
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)ftpd.c     5.33    (Berkeley) %G%";
+static char sccsid[] = "@(#)ftpd.c     5.37 (Berkeley) 6/27/90";
 #endif /* not lint */
 
 /*
 #endif /* not lint */
 
 /*
@@ -37,6 +39,8 @@ static char sccsid[] = "@(#)ftpd.c    5.33    (Berkeley) %G%";
 #include <sys/dir.h>
 
 #include <netinet/in.h>
 #include <sys/dir.h>
 
 #include <netinet/in.h>
+#include <netinet/in_systm.h>
+#include <netinet/ip.h>
 
 #define        FTP_NAMES
 #include <arpa/ftp.h>
 
 #define        FTP_NAMES
 #include <arpa/ftp.h>
@@ -61,8 +65,6 @@ static char sccsid[] = "@(#)ftpd.c    5.33    (Berkeley) %G%";
  * Commonly used to disallow uucp.
  */
 extern int errno;
  * Commonly used to disallow uucp.
  */
 extern int errno;
-extern char *sys_errlist[];
-extern int sys_nerr;
 extern char *crypt();
 extern char version[];
 extern char *home;             /* pointer to home directory for glob */
 extern char *crypt();
 extern char version[];
 extern char *home;             /* pointer to home directory for glob */
@@ -70,6 +72,7 @@ extern        FILE *ftpd_popen(), *fopen(), *freopen();
 extern int  ftpd_pclose(), fclose();
 extern char *getline();
 extern char cbuf[];
 extern int  ftpd_pclose(), fclose();
 extern char *getline();
 extern char cbuf[];
+extern off_t restart_point;
 
 struct sockaddr_in ctrl_addr;
 struct sockaddr_in data_source;
 
 struct sockaddr_in ctrl_addr;
 struct sockaddr_in data_source;
@@ -130,7 +133,7 @@ main(argc, argv, envp)
        char *argv[];
        char **envp;
 {
        char *argv[];
        char **envp;
 {
-       int addrlen, on = 1;
+       int addrlen, on = 1, tos;
        char *cp;
 
        addrlen = sizeof (his_addr);
        char *cp;
 
        addrlen = sizeof (his_addr);
@@ -143,6 +146,11 @@ main(argc, argv, envp)
                syslog(LOG_ERR, "getsockname (%s): %m",argv[0]);
                exit(1);
        }
                syslog(LOG_ERR, "getsockname (%s): %m",argv[0]);
                exit(1);
        }
+#ifdef IP_TOS
+       tos = IPTOS_LOWDELAY;
+       if (setsockopt(0, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(int)) < 0)
+               syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
+#endif
        data_source.sin_port = htons(ntohs(ctrl_addr.sin_port) - 1);
        debug = 0;
        openlog("ftpd", LOG_PID, LOG_DAEMON);
        data_source.sin_port = htons(ntohs(ctrl_addr.sin_port) - 1);
        debug = 0;
        openlog("ftpd", LOG_PID, LOG_DAEMON);
@@ -304,17 +312,14 @@ int askpasswd;                    /* had user command, ask for passwd */
 
 /*
  * USER command.
 
 /*
  * USER command.
- * Sets global passwd pointer pw if named account exists
- * and is acceptable; sets askpasswd if a PASS command is
- * expected. If logged in previously, need to reset state.
- * If name is "ftp" or "anonymous", the name is not in /etc/ftpusers,
- * and ftp account exists, set guest and pw, then just return.
- * If account doesn't exist, ask for passwd anyway.
- * Otherwise, check user requesting login privileges.
- * Disallow anyone who does not have a standard
- * shell as returned by getusershell().
- * Disallow anyone mentioned in the file _PATH_FTPUSERS
- * to allow people such as root and uucp to be avoided.
+ * Sets global passwd pointer pw if named account exists and is acceptable;
+ * sets askpasswd if a PASS command is expected.  If logged in previously,
+ * need to reset state.  If name is "ftp" or "anonymous", the name is not in
+ * _PATH_FTPUSERS, and ftp account exists, set guest and pw, then just return.
+ * If account doesn't exist, ask for passwd anyway.  Otherwise, check user
+ * requesting login privileges.  Disallow anyone who does not have a standard
+ * shell as returned by getusershell().  Disallow anyone mentioned in the file
+ * _PATH_FTPUSERS to allow people such as root and uucp to be avoided.
  */
 user(name)
        char *name;
  */
 user(name)
        char *name;
@@ -376,16 +381,19 @@ user(name)
 checkuser(name)
        char *name;
 {
 checkuser(name)
        char *name;
 {
-       FILE *fd;
-       char line[BUFSIZ], *cp;
+       register FILE *fd;
+       register char *p;
+       char line[BUFSIZ];
 
        if ((fd = fopen(_PATH_FTPUSERS, "r")) != NULL) {
 
        if ((fd = fopen(_PATH_FTPUSERS, "r")) != NULL) {
-               while (fgets(line, sizeof (line), fd) != NULL) {
-                       if ((cp = index(line, '\n')) != NULL)
-                               *cp = '\0';
-                       if (strcmp(line, name) == 0)
-                               return (1);
-               }
+               while (fgets(line, sizeof(line), fd) != NULL)
+                       if ((p = index(line, '\n')) != NULL) {
+                               *p = '\0';
+                               if (line[0] == '#')
+                                       continue;
+                               if (strcmp(line, name) == 0)
+                                       return (1);
+                       }
                (void) fclose(fd);
        }
        return (0);
                (void) fclose(fd);
        }
        return (0);
@@ -524,6 +532,25 @@ retrieve(cmd, name)
                reply(550, "%s: not a plain file.", name);
                goto done;
        }
                reply(550, "%s: not a plain file.", name);
                goto done;
        }
+       if (restart_point) {
+               if (type == TYPE_A) {
+                       register int i, n, c;
+
+                       n = restart_point;
+                       i = 0;
+                       while (i++ < n) {
+                               if ((c=getc(fin)) == EOF) {
+                                       perror_reply(550, name);
+                                       goto done;
+                               }
+                               if (c == '\n')
+                                       i++;
+                       }       
+               } else if (lseek(fileno(fin), restart_point, L_SET) < 0) {
+                       perror_reply(550, name);
+                       goto done;
+               }
+       }
        dout = dataconn(name, st.st_size, "w");
        if (dout == NULL)
                goto done;
        dout = dataconn(name, st.st_size, "w");
        if (dout == NULL)
                goto done;
@@ -548,12 +575,42 @@ store(name, mode, unique)
            (name = gunique(name)) == NULL)
                return;
 
            (name = gunique(name)) == NULL)
                return;
 
+       if (restart_point)
+               mode = "r+w";
        fout = fopen(name, mode);
        closefunc = fclose;
        if (fout == NULL) {
                perror_reply(553, name);
                return;
        }
        fout = fopen(name, mode);
        closefunc = fclose;
        if (fout == NULL) {
                perror_reply(553, name);
                return;
        }
+       if (restart_point) {
+               if (type == TYPE_A) {
+                       register int i, n, c;
+
+                       n = restart_point;
+                       i = 0;
+                       while (i++ < n) {
+                               if ((c=getc(fout)) == EOF) {
+                                       perror_reply(550, name);
+                                       goto done;
+                               }
+                               if (c == '\n')
+                                       i++;
+                       }       
+                       /*
+                        * We must do this seek to "current" position
+                        * because we are changing from reading to
+                        * writing.
+                        */
+                       if (fseek(fout, 0L, L_INCR) < 0) {
+                               perror_reply(550, name);
+                               goto done;
+                       }
+               } else if (lseek(fileno(fout), restart_point, L_SET) < 0) {
+                       perror_reply(550, name);
+                       goto done;
+               }
+       }
        din = dataconn(name, (off_t)-1, "r");
        if (din == NULL)
                goto done;
        din = dataconn(name, (off_t)-1, "r");
        if (din == NULL)
                goto done;
@@ -598,6 +655,11 @@ getdatasock(mode)
                sleep(tries);
        }
        (void) seteuid((uid_t)pw->pw_uid);
                sleep(tries);
        }
        (void) seteuid((uid_t)pw->pw_uid);
+#ifdef IP_TOS
+       on = IPTOS_THROUGHPUT;
+       if (setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0)
+               syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
+#endif
        return (fdopen(s, mode));
 bad:
        (void) seteuid((uid_t)pw->pw_uid);
        return (fdopen(s, mode));
 bad:
        (void) seteuid((uid_t)pw->pw_uid);
@@ -613,7 +675,7 @@ dataconn(name, size, mode)
 {
        char sizebuf[32];
        FILE *file;
 {
        char sizebuf[32];
        FILE *file;
-       int retry = 0;
+       int retry = 0, tos;
 
        file_size = size;
        byte_count = 0;
 
        file_size = size;
        byte_count = 0;
@@ -634,6 +696,11 @@ dataconn(name, size, mode)
                }
                (void) close(pdata);
                pdata = s;
                }
                (void) close(pdata);
                pdata = s;
+#ifdef IP_TOS
+               tos = IPTOS_LOWDELAY;
+               (void) setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&tos,
+                   sizeof(int));
+#endif
                reply(150, "Opening %s mode data connection for %s%s.",
                     type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf);
                return(fdopen(pdata, mode));
                reply(150, "Opening %s mode data connection for %s%s.",
                     type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf);
                return(fdopen(pdata, mode));
@@ -651,8 +718,7 @@ dataconn(name, size, mode)
        if (file == NULL) {
                reply(425, "Can't create data socket (%s,%d): %s.",
                    inet_ntoa(data_source.sin_addr),
        if (file == NULL) {
                reply(425, "Can't create data socket (%s,%d): %s.",
                    inet_ntoa(data_source.sin_addr),
-                   ntohs(data_source.sin_port),
-                   errno < sys_nerr ? sys_errlist[errno] : "unknown error");
+                   ntohs(data_source.sin_port), strerror(errno));
                return (NULL);
        }
        data = fileno(file);
                return (NULL);
        }
        data = fileno(file);
@@ -1220,10 +1286,7 @@ perror_reply(code, string)
        int code;
        char *string;
 {
        int code;
        char *string;
 {
-       if (errno < sys_nerr)
-               reply(code, "%s: %s.", string, sys_errlist[errno]);
-       else
-               reply(code, "%s: unknown error %d.", string, errno);
+       reply(code, "%s: %s.", string, strerror(errno));
 }
 
 static char *onefile[] = {
 }
 
 static char *onefile[] = {