added redist.man
[unix-history] / usr / src / libexec / tftpd / tftpd.c
index 49dc9d0..1cea861 100644 (file)
@@ -1,19 +1,19 @@
 /*
  * 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.
+ *
+ * %sccs.include.redist.c%
  */
 
 #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.3 (Berkeley) %G%";
-#endif not lint
-
+static char sccsid[] = "@(#)tftpd.c    5.13 (Berkeley) %G%";
+#endif /* not lint */
 
 /*
  * Trivial file transfer protocol server.
 
 /*
  * Trivial file transfer protocol server.
@@ -22,22 +22,23 @@ static char sccsid[] = "@(#)tftpd.c 5.3 (Berkeley) %G%";
  */
 
 #include <sys/types.h>
  */
 
 #include <sys/types.h>
-#include <sys/socket.h>
 #include <sys/ioctl.h>
 #include <sys/ioctl.h>
-#include <sys/wait.h>
 #include <sys/stat.h>
 #include <sys/stat.h>
+#include <signal.h>
+#include <fcntl.h>
 
 
+#include <sys/socket.h>
 #include <netinet/in.h>
 #include <netinet/in.h>
-
 #include <arpa/tftp.h>
 #include <arpa/tftp.h>
+#include <netdb.h>
 
 
-#include <signal.h>
+#include <setjmp.h>
+#include <syslog.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 <string.h>
+#include <stdlib.h>
 
 #define        TIMEOUT         5
 
 
 #define        TIMEOUT         5
 
@@ -53,35 +54,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,
        fromlen = sizeof (from);
        n = recvfrom(0, buf, sizeof (buf), 0,
-           (caddr_t)&from, &fromlen);
+           (struct sockaddr *)&from, &fromlen);
        if (n < 0) {
        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,
+                                   (struct sockaddr *)&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);
        }
                exit(1);
        }
-       if (bind(peer, (caddr_t)&sin, sizeof (sin)) < 0) {
-               syslog(LOG_ERR, "bind: %m");
+       if (bind(peer, (struct sockaddr *)&sin, sizeof (sin)) < 0) {
+               syslog(LOG_ERR, "bind: %m\n");
                exit(1);
        }
                exit(1);
        }
-       if (connect(peer, (caddr_t)&from, sizeof(from)) < 0) {
-               syslog(LOG_ERR, "connect: %m");
+       if (connect(peer, (struct sockaddr *)&from, sizeof(from)) < 0) {
+               syslog(LOG_ERR, "connect: %m\n");
                exit(1);
        }
        tp = (struct tftphdr *)buf;
                exit(1);
        }
        tp = (struct tftphdr *)buf;
@@ -167,6 +229,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 +241,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) {
@@ -201,6 +278,7 @@ validate_access(filename, mode)
 int    timeout;
 jmp_buf        timeoutbuf;
 
 int    timeout;
 jmp_buf        timeoutbuf;
 
+void
 timer()
 {
 
 timer()
 {
 
@@ -234,17 +312,18 @@ sendfile(pf)
                timeout = 0;
                (void) setjmp(timeoutbuf);
 
                timeout = 0;
                (void) setjmp(timeoutbuf);
 
+send_data:
                if (send(peer, dp, size + 4, 0) != size + 4) {
                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);
-               do {
+               for ( ; ; ) {
                        alarm(rexmtval);        /* read the ack */
                        n = recv(peer, ackbuf, sizeof (ackbuf), 0);
                        alarm(0);
                        if (n < 0) {
                        alarm(rexmtval);        /* read the ack */
                        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);
@@ -252,14 +331,26 @@ sendfile(pf)
 
                        if (ap->th_opcode == ERROR)
                                goto abort;
 
                        if (ap->th_opcode == ERROR)
                                goto abort;
+                       
+                       if (ap->th_opcode == ACK) {
+                               if (ap->th_block == block) {
+                                       break;
+                               }
+                               /* Re-synchronize with the other side */
+                               (void) synchnet(peer);
+                               if (ap->th_block == (block -1)) {
+                                       goto send_data;
+                               }
+                       }
 
 
-               }  while (ap->th_opcode != ACK || ap->th_block != block);
+               }
                block++;
        } while (size == SEGSIZE);
 abort:
        (void) fclose(file);
 }
 
                block++;
        } while (size == SEGSIZE);
 abort:
        (void) fclose(file);
 }
 
+void
 justquit()
 {
        exit(0);
 justquit()
 {
        exit(0);
@@ -287,7 +378,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);
@@ -296,7 +387,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);
@@ -307,6 +398,8 @@ send_ack:
                                if (dp->th_block == block) {
                                        break;   /* normal */
                                }
                                if (dp->th_block == block) {
                                        break;   /* normal */
                                }
+                               /* Re-synchronize with the other side */
+                               (void) synchnet(peer);
                                if (dp->th_block == (block-1))
                                        goto send_ack;          /* rexmit */
                        }
                                if (dp->th_block == (block-1))
                                        goto send_ack;          /* rexmit */
                        }
@@ -366,7 +459,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);
@@ -375,7 +467,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);
@@ -383,5 +475,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");
 }
 }