BSD 4_3_Reno release
[unix-history] / usr / src / libexec / ftpd / ftpd.c
index 409a50c..1c5f751 100644 (file)
@@ -1,18 +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.
  *
- * %sccs.include.redist.c%
+ * 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.36    (Berkeley) %G%";
+static char sccsid[] = "@(#)ftpd.c     5.37 (Berkeley) 6/27/90";
 #endif /* not lint */
 
 /*
 #endif /* not lint */
 
 /*
@@ -27,6 +39,8 @@ static char sccsid[] = "@(#)ftpd.c    5.36    (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>
@@ -58,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;
@@ -118,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);
@@ -131,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);
@@ -512,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;
@@ -536,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;
@@ -586,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);
@@ -601,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;
@@ -622,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));