BSD 4_3_Net_2 release
[unix-history] / usr / src / libexec / ftpd / ftpd.c
index 1c5f751..2638e8d 100644 (file)
@@ -2,19 +2,33 @@
  * Copyright (c) 1985, 1988, 1990 Regents of the University of California.
  * All rights reserved.
  *
  * Copyright (c) 1985, 1988, 1990 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.
+ *
+ * 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.
  */
 
 #ifndef lint
  */
 
 #ifndef lint
@@ -24,7 +38,7 @@ char copyright[] =
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)ftpd.c     5.37 (Berkeley) 6/27/90";
+static char sccsid[] = "@(#)ftpd.c     5.40 (Berkeley) 7/2/91";
 #endif /* not lint */
 
 /*
 #endif /* not lint */
 
 /*
@@ -34,9 +48,7 @@ static char sccsid[] = "@(#)ftpd.c    5.37 (Berkeley) 6/27/90";
 #include <sys/stat.h>
 #include <sys/ioctl.h>
 #include <sys/socket.h>
 #include <sys/stat.h>
 #include <sys/ioctl.h>
 #include <sys/socket.h>
-#include <sys/file.h>
 #include <sys/wait.h>
 #include <sys/wait.h>
-#include <sys/dir.h>
 
 #include <netinet/in.h>
 #include <netinet/in_systm.h>
 
 #include <netinet/in.h>
 #include <netinet/in_systm.h>
@@ -47,16 +59,21 @@ static char sccsid[] = "@(#)ftpd.c  5.37 (Berkeley) 6/27/90";
 #include <arpa/inet.h>
 #include <arpa/telnet.h>
 
 #include <arpa/inet.h>
 #include <arpa/telnet.h>
 
-#include <ctype.h>
-#include <stdio.h>
 #include <signal.h>
 #include <signal.h>
+#include <dirent.h>
+#include <fcntl.h>
+#include <time.h>
 #include <pwd.h>
 #include <setjmp.h>
 #include <netdb.h>
 #include <errno.h>
 #include <pwd.h>
 #include <setjmp.h>
 #include <netdb.h>
 #include <errno.h>
-#include <string.h>
 #include <syslog.h>
 #include <varargs.h>
 #include <syslog.h>
 #include <varargs.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <ctype.h>
+#include <stdlib.h>
+#include <string.h>
 #include "pathnames.h"
 
 /*
 #include "pathnames.h"
 
 /*
@@ -118,8 +135,7 @@ char        remotehost[MAXHOSTNAMELEN];
 int    swaitmax = SWAITMAX;
 int    swaitint = SWAITINT;
 
 int    swaitmax = SWAITMAX;
 int    swaitint = SWAITINT;
 
-int    lostconn();
-int    myoob();
+void   lostconn(), myoob();
 FILE   *getdatasock(), *dataconn();
 
 #ifdef SETPROCTITLE
 FILE   *getdatasock(), *dataconn();
 
 #ifdef SETPROCTITLE
@@ -136,6 +152,11 @@ main(argc, argv, envp)
        int addrlen, on = 1, tos;
        char *cp;
 
        int addrlen, on = 1, tos;
        char *cp;
 
+       /*
+        * LOG_NDELAY sets up the logging connection immediately,
+        * necessary for anonymous ftp's that chroot and can't do it later.
+        */
+       openlog("ftpd", LOG_PID | LOG_NDELAY, LOG_DAEMON);
        addrlen = sizeof (his_addr);
        if (getpeername(0, (struct sockaddr *)&his_addr, &addrlen) < 0) {
                syslog(LOG_ERR, "getpeername (%s): %m",argv[0]);
        addrlen = sizeof (his_addr);
        if (getpeername(0, (struct sockaddr *)&his_addr, &addrlen) < 0) {
                syslog(LOG_ERR, "getpeername (%s): %m",argv[0]);
@@ -153,7 +174,6 @@ main(argc, argv, envp)
 #endif
        data_source.sin_port = htons(ntohs(ctrl_addr.sin_port) - 1);
        debug = 0;
 #endif
        data_source.sin_port = htons(ntohs(ctrl_addr.sin_port) - 1);
        debug = 0;
-       openlog("ftpd", LOG_PID, LOG_DAEMON);
 #ifdef SETPROCTITLE
        /*
         *  Save start and extent of argv for setproctitle.
 #ifdef SETPROCTITLE
        /*
         *  Save start and extent of argv for setproctitle.
@@ -247,9 +267,9 @@ nextopt:
        /* NOTREACHED */
 }
 
        /* NOTREACHED */
 }
 
+void
 lostconn()
 {
 lostconn()
 {
-
        if (debug)
                syslog(LOG_DEBUG, "lost connection");
        dologout(-1);
        if (debug)
                syslog(LOG_DEBUG, "lost connection");
        dologout(-1);
@@ -264,7 +284,6 @@ char *
 sgetsave(s)
        char *s;
 {
 sgetsave(s)
        char *s;
 {
-       char *malloc();
        char *new = malloc((unsigned) strlen(s) + 1);
 
        if (new == NULL) {
        char *new = malloc((unsigned) strlen(s) + 1);
 
        if (new == NULL) {
@@ -636,10 +655,10 @@ getdatasock(mode)
 
        if (data >= 0)
                return (fdopen(data, mode));
 
        if (data >= 0)
                return (fdopen(data, mode));
+       (void) seteuid((uid_t)0);
        s = socket(AF_INET, SOCK_STREAM, 0);
        if (s < 0)
        s = socket(AF_INET, SOCK_STREAM, 0);
        if (s < 0)
-               return (NULL);
-       (void) seteuid((uid_t)0);
+               goto bad;
        if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
            (char *) &on, sizeof (on)) < 0)
                goto bad;
        if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
            (char *) &on, sizeof (on)) < 0)
                goto bad;
@@ -1172,6 +1191,7 @@ dologout(status)
        _exit(status);
 }
 
        _exit(status);
 }
 
+void
 myoob()
 {
        char *cp;
 myoob()
 {
        char *cp;
@@ -1299,17 +1319,17 @@ send_file_list(whichfiles)
 {
        struct stat st;
        DIR *dirp = NULL;
 {
        struct stat st;
        DIR *dirp = NULL;
-       struct direct *dir;
+       struct dirent *dir;
        FILE *dout = NULL;
        register char **dirlist, *dirname;
        int simple = 0;
        char *strpbrk();
 
        if (strpbrk(whichfiles, "~{[*?") != NULL) {
        FILE *dout = NULL;
        register char **dirlist, *dirname;
        int simple = 0;
        char *strpbrk();
 
        if (strpbrk(whichfiles, "~{[*?") != NULL) {
-               extern char **glob(), *globerr;
+               extern char **ftpglob(), *globerr;
 
                globerr = NULL;
 
                globerr = NULL;
-               dirlist = glob(whichfiles);
+               dirlist = ftpglob(whichfiles);
                if (globerr != NULL) {
                        reply(550, globerr);
                        return;
                if (globerr != NULL) {
                        reply(550, globerr);
                        return;