use syslog for error messages
[unix-history] / usr / src / libexec / tftpd / tftpd.c
index 06b8677..185ba09 100644 (file)
@@ -1,86 +1,77 @@
-/*     tftpd.c 4.1     82/08/16        */
+#ifndef lint
+static char sccsid[] = "@(#)tftpd.c    4.14 (Berkeley) %G%";
+#endif
 
 /*
  * Trivial file transfer protocol server.
  */
 #include <sys/types.h>
 
 /*
  * Trivial file transfer protocol server.
  */
 #include <sys/types.h>
-#include <net/in.h>
 #include <sys/socket.h>
 #include <sys/socket.h>
-#include <signal.h>
 #include <sys/ioctl.h>
 #include <sys/ioctl.h>
-#include <stat.h>
+#include <sys/wait.h>
+#include <sys/stat.h>
+
+#include <netinet/in.h>
+
+#include <arpa/tftp.h>
+
+#include <signal.h>
 #include <stdio.h>
 #include <stdio.h>
-#include <wait.h>
 #include <errno.h>
 #include <ctype.h>
 #include <errno.h>
 #include <ctype.h>
-#include "tftp.h"
+#include <netdb.h>
+#include <setjmp.h>
+#include <syslog.h>
 
 
-#define        DEBUG           1
+#define        TIMEOUT         5
 
 extern int errno;
 
 extern int errno;
-struct sockaddr_in sin = { AF_INET, IPPORT_TFTP };
-int    f;
-int    options;
+struct sockaddr_in sin = { AF_INET };
+int    peer;
+int    rexmtval = TIMEOUT;
+int    maxtimeout = 5*TIMEOUT;
 char   buf[BUFSIZ];
 char   buf[BUFSIZ];
+struct sockaddr_in from;
+int    fromlen;
 
 
-main(argc, argv)
-       char *argv[];
+main()
 {
 {
-       union wait status;
-       struct sockaddr_in from;
        register struct tftphdr *tp;
        register int n;
 
        register struct tftphdr *tp;
        register int n;
 
-#ifndef DEBUG
-       if (fork())
-               exit(0);
-       for (f = 0; f < 10; f++)
-               (void) close(f);
-       (void) open("/", 0);
-       (void) dup2(0, 1);
-       (void) dup2(0, 2);
-       { int t = open("/dev/tty", 2);
-         if (t >= 0) {
-               ioctl(t, TIOCNOTTY, (char *)0);
-               (void) close(t);
-         }
+       alarm(10);
+       fromlen = sizeof (from);
+       n = recvfrom(0, buf, sizeof (buf), 0,
+           (caddr_t)&from, &fromlen);
+       if (n < 0) {
+               perror("tftpd: recvfrom");
+               exit(1);
        }
        }
-#endif
-#if vax || pdp11
-       sin.sin_port = htons(sin.sin_port);
-#endif
-       argc--, argv++;
-       if (argc > 0 && !strcmp(argv[0], "-d"))
-               options |= SO_DEBUG;
-       for (;;) {
-               errno = 0;
-               f = socket(SOCK_DGRAM, 0, &sin, options);
-               if (f < 0) {
-                       perror("socket");
-                       sleep(5);
-                       continue;
-               }
-again:
-               n = receive(f, &from, buf, sizeof (buf));
-               if (n <= 0) {
-                       if (n < 0)
-                               perror("receive");
-                       goto again;
-               }
-               tp = (struct tftphdr *)buf;
-#if vax || pdp11
-               tp->th_opcode = ntohs(tp->th_opcode);
-#endif
-               if (tp->th_opcode == RRQ || tp->th_opcode == WRQ)
-                       if (fork() == 0)
-                               tftp(&from, tp, n);
-               (void) close(f);
-#ifdef notdef
-               while (wait3(status, WNOHANG, 0) > 0)
-#else
-               while (wait3(status, 0, 0) > 0)
-                       continue;
+       from.sin_family = AF_INET;
+       alarm(0);
+       close(0);
+       close(1);
+       peer = socket(AF_INET, SOCK_DGRAM, 0);
+       if (peer < 0) {
+               openlog("tftpd", LOG_PID, 0);
+               syslog(LOG_ERR, "socket: %m");
+               exit(1);
        }
        }
+       if (bind(peer, (caddr_t)&sin, sizeof (sin)) < 0) {
+               openlog("tftpd", LOG_PID, 0);
+               syslog(LOG_ERR, "bind: %m");
+               exit(1);
+       }
+       if (connect(peer, (caddr_t)&from, sizeof(from)) < 0) {
+               openlog("tftpd", LOG_PID, 0);
+               syslog(LOG_ERR, "connect: %m");
+               exit(1);
+       }
+       tp = (struct tftphdr *)buf;
+       tp->th_opcode = ntohs(tp->th_opcode);
+       if (tp->th_opcode == RRQ || tp->th_opcode == WRQ)
+               tftp(tp, n);
+       exit(1);
 }
 
 int    validate_access();
 }
 
 int    validate_access();
@@ -100,13 +91,10 @@ struct formats {
        { 0 }
 };
 
        { 0 }
 };
 
-int    fd;                     /* file being transferred */
-
 /*
  * Handle initial connection protocol.
  */
 /*
  * Handle initial connection protocol.
  */
-tftp(client, tp, size)
-       struct sockaddr_in *client;
+tftp(tp, size)
        struct tftphdr *tp;
        int size;
 {
        struct tftphdr *tp;
        int size;
 {
@@ -115,10 +103,6 @@ tftp(client, tp, size)
        register struct formats *pf;
        char *filename, *mode;
 
        register struct formats *pf;
        char *filename, *mode;
 
-       if (connect(f, client) < 0) {
-               perror("connect");
-               exit(1);
-       }
        filename = cp = tp->th_stuff;
 again:
        while (cp < buf + size) {
        filename = cp = tp->th_stuff;
 again:
        while (cp < buf + size) {
@@ -145,7 +129,7 @@ again:
                nak(EBADOP);
                exit(1);
        }
                nak(EBADOP);
                exit(1);
        }
-       ecode = (*pf->f_validate)(filename, client, tp->th_opcode);
+       ecode = (*pf->f_validate)(filename, tp->th_opcode);
        if (ecode) {
                nak(ecode);
                exit(1);
        if (ecode) {
                nak(ecode);
                exit(1);
@@ -157,6 +141,8 @@ again:
        exit(0);
 }
 
        exit(0);
 }
 
+int    fd;
+
 /*
  * Validate file access.  Since we
  * have no uid or gid, for now require
 /*
  * Validate file access.  Since we
  * have no uid or gid, for now require
@@ -165,9 +151,8 @@ again:
  * 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.
  */
-validate_access(file, client, mode)
+validate_access(file, mode)
        char *file;
        char *file;
-       struct sockaddr_in *client;
        int mode;
 {
        struct stat stbuf;
        int mode;
 {
        struct stat stbuf;
@@ -189,14 +174,16 @@ validate_access(file, client, mode)
        return (0);
 }
 
        return (0);
 }
 
-int timeout;
+int    timeout;
+jmp_buf        timeoutbuf;
 
 timer()
 {
 
 timer()
 {
-       timeout += TIMEOUT;
-       if (timeout >= MAXTIMEOUT)
+
+       timeout += rexmtval;
+       if (timeout >= maxtimeout)
                exit(1);
                exit(1);
-       alarm(TIMEOUT);
+       longjmp(timeoutbuf, 1);
 }
 
 /*
 }
 
 /*
@@ -208,46 +195,37 @@ sendfile(pf)
        register struct tftphdr *tp;
        register int block = 1, size, n;
 
        register struct tftphdr *tp;
        register int block = 1, size, n;
 
-       sigset(SIGALRM, timer);
+       signal(SIGALRM, timer);
        tp = (struct tftphdr *)buf;
        do {
                size = read(fd, tp->th_data, SEGSIZE);
                if (size < 0) {
                        nak(errno + 100);
        tp = (struct tftphdr *)buf;
        do {
                size = read(fd, tp->th_data, SEGSIZE);
                if (size < 0) {
                        nak(errno + 100);
-                       break;
+                       return;
                }
                tp->th_opcode = htons((u_short)DATA);
                tp->th_block = htons((u_short)block);
                timeout = 0;
                }
                tp->th_opcode = htons((u_short)DATA);
                tp->th_block = htons((u_short)block);
                timeout = 0;
-               alarm(TIMEOUT);
-rexmt:
-               if (write(f, buf, size + 4) != size + 4) {
-                       perror("send");
-                       break;
+               (void) setjmp(timeoutbuf);
+               if (send(peer, buf, size + 4, 0) != size + 4) {
+                       perror("tftpd: send");
+                       return;
                }
                }
-again:
-               n = read(f, buf, sizeof (buf));
-               if (n <= 0) {
-                       if (n == 0)
-                               goto again;
-                       if (errno == EINTR)
-                               goto rexmt;
+               do {
+                       alarm(rexmtval);
+                       n = recv(peer, buf, sizeof (buf), 0);
                        alarm(0);
                        alarm(0);
-                       perror("receive");
-                       break;
-               }
-               alarm(0);
-#if vax || pdp11
-               tp->th_opcode = ntohs(tp->th_opcode);
-               tp->th_block = ntohs(tp->th_block);
-#endif
-               if (tp->th_opcode == ERROR)
-                       break;
-               if (tp->th_opcode != ACK || tp->th_block != block)
-                       goto again;
+                       if (n < 0) {
+                               perror("tftpd: recv");
+                               return;
+                       }
+                       tp->th_opcode = ntohs((u_short)tp->th_opcode);
+                       tp->th_block = ntohs((u_short)tp->th_block);
+                       if (tp->th_opcode == ERROR)
+                               return;
+               } while (tp->th_opcode != ACK || tp->th_block != block);
                block++;
        } while (size == SEGSIZE);
                block++;
        } while (size == SEGSIZE);
-       (void) close(fd);
 }
 
 /*
 }
 
 /*
@@ -259,49 +237,41 @@ recvfile(pf)
        register struct tftphdr *tp;
        register int block = 0, n, size;
 
        register struct tftphdr *tp;
        register int block = 0, n, size;
 
-       sigset(SIGALRM, timer);
+       signal(SIGALRM, timer);
        tp = (struct tftphdr *)buf;
        do {
                timeout = 0;
        tp = (struct tftphdr *)buf;
        do {
                timeout = 0;
-               alarm(TIMEOUT);
                tp->th_opcode = htons((u_short)ACK);
                tp->th_block = htons((u_short)block);
                block++;
                tp->th_opcode = htons((u_short)ACK);
                tp->th_block = htons((u_short)block);
                block++;
-rexmt:
-               if (write(f, buf, 4) != 4) {
-                       perror("ack");
-                       break;
+               (void) setjmp(timeoutbuf);
+               if (send(peer, buf, 4, 0) != 4) {
+                       perror("tftpd: send");
+                       goto abort;
                }
                }
-again:
-               n = read(f, buf, sizeof (buf));
-               if (n <= 0) {
-                       if (n == 0)
-                               goto again;
-                       if (errno == EINTR)
-                               goto rexmt;
+               do {
+                       alarm(rexmtval);
+                       n = recv(peer, buf, sizeof (buf), 0);
                        alarm(0);
                        alarm(0);
-                       perror("receive");
-                       break;
-               }
-               alarm(0);
-#if vax || pdp11
-               tp->th_opcode = ntohs(tp->th_opcode);
-               tp->th_block = ntohs(tp->th_block);
-#endif
-               if (tp->th_opcode == ERROR)
-                       break;
-               if (tp->th_opcode != DATA || block != tp->th_block)
-                       goto again;
+                       if (n < 0) {
+                               perror("tftpd: recv");
+                               goto abort;
+                       }
+                       tp->th_opcode = ntohs((u_short)tp->th_opcode);
+                       tp->th_block = ntohs((u_short)tp->th_block);
+                       if (tp->th_opcode == ERROR)
+                               goto abort;
+               } while (tp->th_opcode != DATA || block != tp->th_block);
                size = write(fd, tp->th_data, n - 4);
                if (size < 0) {
                        nak(errno + 100);
                size = write(fd, tp->th_data, n - 4);
                if (size < 0) {
                        nak(errno + 100);
-                       break;
+                       goto abort;
                }
        } while (size == SEGSIZE);
                }
        } while (size == SEGSIZE);
+abort:
        tp->th_opcode = htons((u_short)ACK);
        tp->th_block = htons((u_short)(block));
        tp->th_opcode = htons((u_short)ACK);
        tp->th_block = htons((u_short)(block));
-       (void) write(f, buf, 4);
-       (void) close(fd);
+       (void) send(peer, buf, 4, 0);
 }
 
 struct errmsg {
 }
 
 struct errmsg {
@@ -345,6 +315,7 @@ nak(error)
        length = strlen(pe->e_msg);
        tp->th_msg[length] = '\0';
        length += 5;
        length = strlen(pe->e_msg);
        tp->th_msg[length] = '\0';
        length += 5;
-       if (write(f, buf, length) != length)
+       if (send(peer, buf, length, 0) != length)
                perror("nak");
                perror("nak");
+       exit(1);
 }
 }