BSD 4_3_Reno release
[unix-history] / usr / src / libexec / tftpd / tftpd.c
index 8a99725..6e1bd60 100644 (file)
@@ -1,19 +1,31 @@
 /*
  * Copyright (c) 1983 Regents of the University of California.
 /*
  * Copyright (c) 1983 Regents of the University of California.
- * All rights reserved.  The Berkeley software License Agreement
- * specifies the terms and conditions for redistribution.
+ * 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.
  */
 
 #ifndef lint
 char copyright[] =
 "@(#) Copyright (c) 1983 Regents of the University of California.\n\
  All rights reserved.\n";
  */
 
 #ifndef lint
 char copyright[] =
 "@(#) Copyright (c) 1983 Regents of the University of California.\n\
  All rights reserved.\n";
-#endif not lint
+#endif /* not lint */
 
 #ifndef lint
 
 #ifndef lint
-static char sccsid[] = "@(#)tftpd.c    5.5 (Berkeley) %G%";
-#endif not lint
-
+static char sccsid[] = "@(#)tftpd.c    5.12 (Berkeley) 6/1/90";
+#endif /* not lint */
 
 /*
  * Trivial file transfer protocol server.
 
 /*
  * Trivial file transfer protocol server.
@@ -26,18 +38,19 @@ static char sccsid[] = "@(#)tftpd.c 5.5 (Berkeley) %G%";
 #include <sys/ioctl.h>
 #include <sys/wait.h>
 #include <sys/stat.h>
 #include <sys/ioctl.h>
 #include <sys/wait.h>
 #include <sys/stat.h>
+#include <sys/signal.h>
 
 #include <netinet/in.h>
 
 #include <arpa/tftp.h>
 
 
 #include <netinet/in.h>
 
 #include <arpa/tftp.h>
 
-#include <signal.h>
+#include <netdb.h>
+#include <setjmp.h>
 #include <stdio.h>
 #include <errno.h>
 #include <ctype.h>
 #include <stdio.h>
 #include <errno.h>
 #include <ctype.h>
-#include <netdb.h>
-#include <setjmp.h>
 #include <syslog.h>
 #include <syslog.h>
+#include <string.h>
 
 #define        TIMEOUT         5
 
 
 #define        TIMEOUT         5
 
@@ -53,35 +66,96 @@ char        ackbuf[PKTSIZE];
 struct sockaddr_in from;
 int    fromlen;
 
 struct sockaddr_in from;
 int    fromlen;
 
-main()
+#define MAXARG 4
+char   *dirs[MAXARG+1];
+
+main(ac, av)
+       char **av;
 {
        register struct tftphdr *tp;
 {
        register struct tftphdr *tp;
-       register int n;
+       register int n = 0;
+       int on = 1;
 
 
+       ac--; av++;
+       while (ac-- > 0 && n < MAXARG)
+               dirs[n++] = *av++;
        openlog("tftpd", LOG_PID, LOG_DAEMON);
        openlog("tftpd", LOG_PID, LOG_DAEMON);
-       alarm(10);
+       if (ioctl(0, FIONBIO, &on) < 0) {
+               syslog(LOG_ERR, "ioctl(FIONBIO): %m\n");
+               exit(1);
+       }
        fromlen = sizeof (from);
        n = recvfrom(0, buf, sizeof (buf), 0,
            (caddr_t)&from, &fromlen);
        if (n < 0) {
        fromlen = sizeof (from);
        n = recvfrom(0, buf, sizeof (buf), 0,
            (caddr_t)&from, &fromlen);
        if (n < 0) {
-               perror("tftpd: recvfrom");
+               syslog(LOG_ERR, "recvfrom: %m\n");
                exit(1);
        }
                exit(1);
        }
+       /*
+        * Now that we have read the message out of the UDP
+        * socket, we fork and exit.  Thus, inetd will go back
+        * to listening to the tftp port, and the next request
+        * to come in will start up a new instance of tftpd.
+        *
+        * We do this so that inetd can run tftpd in "wait" mode.
+        * The problem with tftpd running in "nowait" mode is that
+        * inetd may get one or more successful "selects" on the
+        * tftp port before we do our receive, so more than one
+        * instance of tftpd may be started up.  Worse, if tftpd
+        * break before doing the above "recvfrom", inetd would
+        * spawn endless instances, clogging the system.
+        */
+       {
+               int pid;
+               int i, j;
+
+               for (i = 1; i < 20; i++) {
+                   pid = fork();
+                   if (pid < 0) {
+                               sleep(i);
+                               /*
+                                * flush out to most recently sent request.
+                                *
+                                * This may drop some request, but those
+                                * will be resent by the clients when
+                                * they timeout.  The positive effect of
+                                * this flush is to (try to) prevent more
+                                * than one tftpd being started up to service
+                                * a single request from a single client.
+                                */
+                               j = sizeof from;
+                               i = recvfrom(0, buf, sizeof (buf), 0,
+                                   (caddr_t)&from, &j);
+                               if (i > 0) {
+                                       n = i;
+                                       fromlen = j;
+                               }
+                   } else {
+                               break;
+                   }
+               }
+               if (pid < 0) {
+                       syslog(LOG_ERR, "fork: %m\n");
+                       exit(1);
+               } else if (pid != 0) {
+                       exit(0);
+               }
+       }
        from.sin_family = AF_INET;
        alarm(0);
        close(0);
        close(1);
        peer = socket(AF_INET, SOCK_DGRAM, 0);
        if (peer < 0) {
        from.sin_family = AF_INET;
        alarm(0);
        close(0);
        close(1);
        peer = socket(AF_INET, SOCK_DGRAM, 0);
        if (peer < 0) {
-               syslog(LOG_ERR, "socket: %m");
+               syslog(LOG_ERR, "socket: %m\n");
                exit(1);
        }
        if (bind(peer, (caddr_t)&sin, sizeof (sin)) < 0) {
                exit(1);
        }
        if (bind(peer, (caddr_t)&sin, sizeof (sin)) < 0) {
-               syslog(LOG_ERR, "bind: %m");
+               syslog(LOG_ERR, "bind: %m\n");
                exit(1);
        }
        if (connect(peer, (caddr_t)&from, sizeof(from)) < 0) {
                exit(1);
        }
        if (connect(peer, (caddr_t)&from, sizeof(from)) < 0) {
-               syslog(LOG_ERR, "connect: %m");
+               syslog(LOG_ERR, "connect: %m\n");
                exit(1);
        }
        tp = (struct tftphdr *)buf;
                exit(1);
        }
        tp = (struct tftphdr *)buf;
@@ -167,6 +241,9 @@ FILE *file;
  * have no uid or gid, for now require
  * file to exist and be publicly
  * readable/writable.
  * have no uid or gid, for now require
  * file to exist and be publicly
  * readable/writable.
+ * If we were invoked with arguments
+ * from inetd then the file must also be
+ * in one of the given directory prefixes.
  * Note also, full path name must be
  * given as we have no login directory.
  */
  * Note also, full path name must be
  * given as we have no login directory.
  */
@@ -176,9 +253,21 @@ validate_access(filename, mode)
 {
        struct stat stbuf;
        int     fd;
 {
        struct stat stbuf;
        int     fd;
+       char *cp, **dirp;
 
        if (*filename != '/')
                return (EACCESS);
 
        if (*filename != '/')
                return (EACCESS);
+       /*
+        * prevent tricksters from getting around the directory restrictions
+        */
+       for (cp = filename + 1; *cp; cp++)
+               if(*cp == '.' && strncmp(cp-1, "/../", 4) == 0)
+                       return(EACCESS);
+       for (dirp = dirs; *dirp; dirp++)
+               if (strncmp(filename, *dirp, strlen(*dirp)) == 0)
+                       break;
+       if (*dirp==0 && dirp!=dirs)
+               return (EACCESS);
        if (stat(filename, &stbuf) < 0)
                return (errno == ENOENT ? ENOTFOUND : EACCESS);
        if (mode == RRQ) {
        if (stat(filename, &stbuf) < 0)
                return (errno == ENOENT ? ENOTFOUND : EACCESS);
        if (mode == RRQ) {
@@ -236,7 +325,7 @@ sendfile(pf)
 
 send_data:
                if (send(peer, dp, size + 4, 0) != size + 4) {
 
 send_data:
                if (send(peer, dp, size + 4, 0) != size + 4) {
-                       perror("tftpd: write");
+                       syslog(LOG_ERR, "tftpd: write: %m\n");
                        goto abort;
                }
                read_ahead(file, pf->f_convert);
                        goto abort;
                }
                read_ahead(file, pf->f_convert);
@@ -245,7 +334,7 @@ send_data:
                        n = recv(peer, ackbuf, sizeof (ackbuf), 0);
                        alarm(0);
                        if (n < 0) {
                        n = recv(peer, ackbuf, sizeof (ackbuf), 0);
                        alarm(0);
                        if (n < 0) {
-                               perror("tftpd: read");
+                               syslog(LOG_ERR, "tftpd: read: %m\n");
                                goto abort;
                        }
                        ap->th_opcode = ntohs((u_short)ap->th_opcode);
                                goto abort;
                        }
                        ap->th_opcode = ntohs((u_short)ap->th_opcode);
@@ -299,7 +388,7 @@ recvfile(pf)
                (void) setjmp(timeoutbuf);
 send_ack:
                if (send(peer, ackbuf, 4, 0) != 4) {
                (void) setjmp(timeoutbuf);
 send_ack:
                if (send(peer, ackbuf, 4, 0) != 4) {
-                       perror("tftpd: write");
+                       syslog(LOG_ERR, "tftpd: write: %m\n");
                        goto abort;
                }
                write_behind(file, pf->f_convert);
                        goto abort;
                }
                write_behind(file, pf->f_convert);
@@ -308,7 +397,7 @@ send_ack:
                        n = recv(peer, dp, PKTSIZE, 0);
                        alarm(0);
                        if (n < 0) {            /* really? */
                        n = recv(peer, dp, PKTSIZE, 0);
                        alarm(0);
                        if (n < 0) {            /* really? */
-                               perror("tftpd: read");
+                               syslog(LOG_ERR, "tftpd: read: %m\n");
                                goto abort;
                        }
                        dp->th_opcode = ntohs((u_short)dp->th_opcode);
                                goto abort;
                        }
                        dp->th_opcode = ntohs((u_short)dp->th_opcode);
@@ -380,7 +469,6 @@ nak(error)
        register struct tftphdr *tp;
        int length;
        register struct errmsg *pe;
        register struct tftphdr *tp;
        int length;
        register struct errmsg *pe;
-       extern char *sys_errlist[];
 
        tp = (struct tftphdr *)buf;
        tp->th_opcode = htons((u_short)ERROR);
 
        tp = (struct tftphdr *)buf;
        tp->th_opcode = htons((u_short)ERROR);
@@ -389,7 +477,7 @@ nak(error)
                if (pe->e_code == error)
                        break;
        if (pe->e_code < 0) {
                if (pe->e_code == error)
                        break;
        if (pe->e_code < 0) {
-               pe->e_msg = sys_errlist[error - 100];
+               pe->e_msg = strerror(error - 100);
                tp->th_code = EUNDEF;   /* set 'undef' errorcode */
        }
        strcpy(tp->th_msg, pe->e_msg);
                tp->th_code = EUNDEF;   /* set 'undef' errorcode */
        }
        strcpy(tp->th_msg, pe->e_msg);
@@ -397,5 +485,5 @@ nak(error)
        tp->th_msg[length] = '\0';
        length += 5;
        if (send(peer, buf, length, 0) != length)
        tp->th_msg[length] = '\0';
        length += 5;
        if (send(peer, buf, length, 0) != length)
-               perror("nak");
+               syslog(LOG_ERR, "nak: %m\n");
 }
 }