automatic aggregate for pcc
[unix-history] / usr / src / libexec / tftpd / tftpd.c
index 2087b83..1cea861 100644 (file)
@@ -2,17 +2,7 @@
  * Copyright (c) 1983 Regents of the University of California.
  * All rights reserved.
  *
  * Copyright (c) 1983 Regents of the University of California.
  * 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 MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ * %sccs.include.redist.c%
  */
 
 #ifndef lint
  */
 
 #ifndef lint
@@ -22,7 +12,7 @@ char copyright[] =
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)tftpd.c    5.8 (Berkeley) %G%";
+static char sccsid[] = "@(#)tftpd.c    5.13 (Berkeley) %G%";
 #endif /* not lint */
 
 /*
 #endif /* not lint */
 
 /*
@@ -32,22 +22,23 @@ static char sccsid[] = "@(#)tftpd.c 5.8 (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
 
@@ -63,12 +54,19 @@ 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;
 
        int on = 1;
 
+       ac--; av++;
+       while (ac-- > 0 && n < MAXARG)
+               dirs[n++] = *av++;
        openlog("tftpd", LOG_PID, LOG_DAEMON);
        if (ioctl(0, FIONBIO, &on) < 0) {
                syslog(LOG_ERR, "ioctl(FIONBIO): %m\n");
        openlog("tftpd", LOG_PID, LOG_DAEMON);
        if (ioctl(0, FIONBIO, &on) < 0) {
                syslog(LOG_ERR, "ioctl(FIONBIO): %m\n");
@@ -76,7 +74,7 @@ main()
        }
        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) {
                syslog(LOG_ERR, "recvfrom: %m\n");
                exit(1);
        if (n < 0) {
                syslog(LOG_ERR, "recvfrom: %m\n");
                exit(1);
@@ -115,7 +113,7 @@ main()
                                 */
                                j = sizeof from;
                                i = recvfrom(0, buf, sizeof (buf), 0,
                                 */
                                j = sizeof from;
                                i = recvfrom(0, buf, sizeof (buf), 0,
-                                   (caddr_t)&from, &j);
+                                   (struct sockaddr *)&from, &j);
                                if (i > 0) {
                                        n = i;
                                        fromlen = j;
                                if (i > 0) {
                                        n = i;
                                        fromlen = j;
@@ -140,11 +138,11 @@ main()
                syslog(LOG_ERR, "socket: %m\n");
                exit(1);
        }
                syslog(LOG_ERR, "socket: %m\n");
                exit(1);
        }
-       if (bind(peer, (caddr_t)&sin, sizeof (sin)) < 0) {
+       if (bind(peer, (struct sockaddr *)&sin, sizeof (sin)) < 0) {
                syslog(LOG_ERR, "bind: %m\n");
                exit(1);
        }
                syslog(LOG_ERR, "bind: %m\n");
                exit(1);
        }
-       if (connect(peer, (caddr_t)&from, sizeof(from)) < 0) {
+       if (connect(peer, (struct sockaddr *)&from, sizeof(from)) < 0) {
                syslog(LOG_ERR, "connect: %m\n");
                exit(1);
        }
                syslog(LOG_ERR, "connect: %m\n");
                exit(1);
        }
@@ -231,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.
  */
@@ -240,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) {
@@ -265,6 +278,7 @@ validate_access(filename, mode)
 int    timeout;
 jmp_buf        timeoutbuf;
 
 int    timeout;
 jmp_buf        timeoutbuf;
 
+void
 timer()
 {
 
 timer()
 {
 
@@ -336,6 +350,7 @@ abort:
        (void) fclose(file);
 }
 
        (void) fclose(file);
 }
 
+void
 justquit()
 {
        exit(0);
 justquit()
 {
        exit(0);
@@ -444,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);
@@ -453,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);