missing arg to accept and make diagnostics consistent
[unix-history] / usr / src / libexec / telnetd / telnetd.c
index 477fd79..a86f912 100644 (file)
@@ -1,21 +1,24 @@
 #ifndef lint
 #ifndef lint
-static char sccsid[] = "@(#)telnetd.c  4.6 82/03/31";
+static char sccsid[] = "@(#)telnetd.c  4.11 82/11/15";
 #endif
 
 /*
  * Stripped-down telnet server.
  */
 #endif
 
 /*
  * Stripped-down telnet server.
  */
+#include <sys/types.h>
+#include <sys/socket.h>
+
+#include <netinet/in.h>
+
 #include <stdio.h>
 #include <signal.h>
 #include <errno.h>
 #include <sgtty.h>
 #include <wait.h>
 #include <stdio.h>
 #include <signal.h>
 #include <errno.h>
 #include <sgtty.h>
 #include <wait.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <net/in.h>
+#include <netdb.h>
+
 #include "telnet.h"
 
 #include "telnet.h"
 
-#define        INFINITY        10000000
 #define        BELL            '\07'
 
 char   hisopts[256];
 #define        BELL            '\07'
 
 char   hisopts[256];
@@ -40,39 +43,71 @@ int inter;
 extern int errno;
 char   line[] = "/dev/ptyp0";
 
 extern int errno;
 char   line[] = "/dev/ptyp0";
 
-struct sockaddr_in sin = { AF_INET, IPPORT_TELNET };
-int    options = SO_ACCEPTCONN|SO_KEEPALIVE;
+struct sockaddr_in sin = { AF_INET };
 
 main(argc, argv)
        char *argv[];
 {
        int s, pid;
        union wait status;
 
 main(argc, argv)
        char *argv[];
 {
        int s, pid;
        union wait status;
+       struct servent *sp;
 
 
+       sp = getservbyname("telnet", "tcp");
+       if (sp == 0) {
+               fprintf(stderr, "telnetd: tcp/telnet: unknown service\n");
+               exit(1);
+       }
+       sin.sin_port = sp->s_port;
        argc--, argv++;
        argc--, argv++;
-       if (argc > 0 && !strcmp(argv[0], "-d"))
-               options |= SO_DEBUG;
-#if vax || pdp11
-       sin.sin_port = htons(sin.sin_port);
+       if (argc > 0) {
+               sin.sin_port = atoi(*argv);
+               if (sin.sin_port <= 0) {
+                       fprintf(stderr, "telnetd: %s: bad port #\n", *argv);
+                       exit(1);
+               }
+       }
+       sin.sin_port = htons((u_short)sin.sin_port);
+#ifndef DEBUG
+       if (fork())
+               exit(0);
+       for (s = 0; s < 10; s++)
+               (void) close(s);
+       (void) open("/", 0);
+       (void) dup2(0, 1);
+       (void) dup2(0, 2);
+       { int tt = open("/dev/tty", 2);
+         if (tt > 0) {
+               ioctl(tt, TIOCNOTTY, 0);
+               close(tt);
+         }
+       }
 #endif
 #endif
+again:
+       s = socket(0, SOCK_STREAM, 0, 0);
+       if (s < 0) {
+               perror("telnetd: socket");;
+               sleep(5);
+               goto again;
+       }
+       while (bind(s, (caddr_t)&sin, sizeof (sin), 0) < 0) {
+               perror("telnetd: bind");
+               sleep(5);
+       }
+       listen(s, 10);
        for (;;) {
        for (;;) {
-               errno = 0;
-               if ((s = socket(SOCK_STREAM, 0, &sin, options)) < 0) {
-                       perror("socket");
-                       sleep(5);
-                       continue;
-               }
-               if (accept(s, 0) < 0) {
-                       perror("accept");
-                       close(s);
+               int s2;
+
+               s2 = accept(s, (caddr_t)0, 0, 0);
+               if (s2 < 0) {
+                       perror("telnetd: accept");
                        sleep(1);
                        continue;
                }
                if ((pid = fork()) < 0)
                        printf("Out of processes\n");
                else if (pid == 0)
                        sleep(1);
                        continue;
                }
                if ((pid = fork()) < 0)
                        printf("Out of processes\n");
                else if (pid == 0)
-                       doit(s);
-               close(s);
+                       doit(s2);
+               close(s2);
                while (wait3(status, WNOHANG, 0) > 0)
                        continue;
        }
                while (wait3(status, WNOHANG, 0) > 0)
                        continue;
        }
@@ -96,9 +131,8 @@ doit(f)
                if (p > 0)
                        goto gotpty;
        }
                if (p > 0)
                        goto gotpty;
        }
-       dup2(f, 1);
-       printf("All network ports in use.\n");
-       exit(1);
+       fatal(f, "All network ports in use");
+       /*NOTREACHED*/
 gotpty:
        dup2(f, 0);
        cp[strlen("/dev/")] = 't';
 gotpty:
        dup2(f, 0);
        cp[strlen("/dev/")] = 't';
@@ -108,22 +142,16 @@ gotpty:
                close(t);
        }
        t = open(cp, 2);
                close(t);
        }
        t = open(cp, 2);
-       if (t < 0) {
-               dup2(f, 2);
-               perror(cp);
-               exit(1);
-       }
+       if (t < 0)
+               fatalperror(f, cp, errno);
        ioctl(t, TIOCGETP, &b);
        b.sg_flags = CRMOD|XTABS|ANYP;
        ioctl(t, TIOCSETP, &b);
        ioctl(p, TIOCGETP, &b);
        ioctl(t, TIOCGETP, &b);
        b.sg_flags = CRMOD|XTABS|ANYP;
        ioctl(t, TIOCSETP, &b);
        ioctl(p, TIOCGETP, &b);
-       b.sg_flags &= ~ECHO;            /* not until remote says to */
+       b.sg_flags &= ~ECHO;
        ioctl(p, TIOCSETP, &b);
        ioctl(p, TIOCSETP, &b);
-       if ((i = fork()) < 0) {
-               dup2(f, 2);
-               perror("fork");
-               exit(1);
-       }
+       if ((i = fork()) < 0)
+               fatalperror(f, "fork", errno);
        if (i)
                telnet(f, p);
        close(f);
        if (i)
                telnet(f, p);
        close(f);
@@ -133,10 +161,33 @@ gotpty:
        dup2(t, 2);
        close(t);
        execl("/bin/login", "telnet-login", 0);
        dup2(t, 2);
        close(t);
        execl("/bin/login", "telnet-login", 0);
-       perror("/bin/login");
+       fatalperror(f, "/bin/login", errno);
+       /*NOTREACHED*/
+}
+
+fatal(f, msg)
+       int f;
+       char *msg;
+{
+       char buf[BUFSIZ];
+
+       (void) sprintf(buf, "telnetd: %s.\n", msg);
+       (void) write(f, buf, strlen(buf));
        exit(1);
 }
 
        exit(1);
 }
 
+fatalperror(f, msg, errno)
+       int f;
+       char *msg;
+       int errno;
+{
+       char buf[BUFSIZ];
+       extern char *sys_errlist[];
+
+       (void) sprintf(buf, "%s: %s", msg, sys_errlist[errno]);
+       fatal(f, buf);
+}
+
 /*
  * Main loop.  Select from pty and network, and
  * hand data to telnet receiver finite state machine.
 /*
  * Main loop.  Select from pty and network, and
  * hand data to telnet receiver finite state machine.
@@ -151,6 +202,11 @@ telnet(f, p)
        signal(SIGTSTP, SIG_IGN);
        sigset(SIGCHLD, cleanup);
 
        signal(SIGTSTP, SIG_IGN);
        sigset(SIGCHLD, cleanup);
 
+       /*
+        * Request to do remote echo.
+        */
+       dooption(TELOPT_ECHO);
+       myopts[TELOPT_ECHO] = 1;
        for (;;) {
                int ibits = 0, obits = 0;
                register int c;
        for (;;) {
                int ibits = 0, obits = 0;
                register int c;
@@ -169,7 +225,7 @@ telnet(f, p)
                        ibits |= (1 << f);
                if (ncc < 0 && pcc < 0)
                        break;
                        ibits |= (1 << f);
                if (ncc < 0 && pcc < 0)
                        break;
-               select(32, &ibits, &obits, INFINITY);
+               select(16, &ibits, &obits, 0, 0);
                if (ibits == 0 && obits == 0) {
                        sleep(5);
                        continue;
                if (ibits == 0 && obits == 0) {
                        sleep(5);
                        continue;
@@ -354,13 +410,13 @@ telrcv()
                        if (myopts[c]) {
                                myopts[c] = 0;
                                sprintf(nfrontp, wont, c);
                        if (myopts[c]) {
                                myopts[c] = 0;
                                sprintf(nfrontp, wont, c);
-                               nfrontp += sizeof(wont) - 2;
+                               nfrontp += sizeof (wont) - 2;
                        }
                        state = TS_DATA;
                        continue;
 
                default:
                        }
                        state = TS_DATA;
                        continue;
 
                default:
-                       printf("netser: panic state=%d\n", state);
+                       printf("telnetd: panic state=%d\n", state);
                        exit(1);
                }
        }
                        exit(1);
                }
        }
@@ -396,7 +452,7 @@ willoption(option)
                break;
        }
        sprintf(nfrontp, fmt, option);
                break;
        }
        sprintf(nfrontp, fmt, option);
-       nfrontp += sizeof(dont) - 2;
+       nfrontp += sizeof (dont) - 2;
 }
 
 wontoption(option)
 }
 
 wontoption(option)
@@ -424,7 +480,7 @@ wontoption(option)
                fmt = dont;
        }
        sprintf(nfrontp, fmt, option);
                fmt = dont;
        }
        sprintf(nfrontp, fmt, option);
-       nfrontp += sizeof(doopt) - 2;
+       nfrontp += sizeof (doopt) - 2;
 }
 
 dooption(option)
 }
 
 dooption(option)
@@ -456,7 +512,7 @@ dooption(option)
                break;
        }
        sprintf(nfrontp, fmt, option);
                break;
        }
        sprintf(nfrontp, fmt, option);
-       nfrontp += sizeof(doopt) - 2;
+       nfrontp += sizeof (doopt) - 2;
 }
 
 mode(on, off)
 }
 
 mode(on, off)
@@ -497,8 +553,8 @@ ptyflush()
 
        if ((n = pfrontp - pbackp) > 0)
                n = write(pty, pbackp, n);
 
        if ((n = pfrontp - pbackp) > 0)
                n = write(pty, pbackp, n);
-       if (n < 0 && errno == EWOULDBLOCK)
-               n = 0;
+       if (n < 0)
+               return;
        pbackp += n;
        if (pbackp == pfrontp)
                pbackp = pfrontp = ptyobuf;
        pbackp += n;
        if (pbackp == pfrontp)
                pbackp = pfrontp = ptyobuf;
@@ -510,8 +566,12 @@ netflush()
 
        if ((n = nfrontp - nbackp) > 0)
                n = write(net, nbackp, n);
 
        if ((n = nfrontp - nbackp) > 0)
                n = write(net, nbackp, n);
-       if (n < 0 && errno == EWOULDBLOCK)
-               n = 0;
+       if (n < 0) {
+               if (errno == EWOULDBLOCK)
+                       return;
+               /* should blow this guy away... */
+               return;
+       }
        nbackp += n;
        if (nbackp == nfrontp)
                nbackp = nfrontp = netobuf;
        nbackp += n;
        if (nbackp == nfrontp)
                nbackp = nfrontp = netobuf;
@@ -522,7 +582,9 @@ cleanup()
        int how = 2;
 
        rmut();
        int how = 2;
 
        rmut();
+#ifdef notdef
        vhangup();
        vhangup();
+#endif
        ioctl(net, SIOCDONE, &how);
        kill(0, SIGKILL);
        exit(1);
        ioctl(net, SIOCDONE, &how);
        kill(0, SIGKILL);
        exit(1);
@@ -533,8 +595,8 @@ cleanup()
 struct utmp wtmp;
 char   wtmpf[] = "/usr/adm/wtmp";
 char   utmp[] = "/etc/utmp";
 struct utmp wtmp;
 char   wtmpf[] = "/usr/adm/wtmp";
 char   utmp[] = "/etc/utmp";
-#define SCPYN(a, b)    strncpy(a, b, sizeof(a))
-#define SCMPN(a, b)    strncmp(a, b, sizeof(a))
+#define SCPYN(a, b)    strncpy(a, b, sizeof (a))
+#define SCMPN(a, b)    strncmp(a, b, sizeof (a))
 
 rmut()
 {
 
 rmut()
 {
@@ -543,13 +605,13 @@ rmut()
 
        f = open(utmp, 2);
        if (f >= 0) {
 
        f = open(utmp, 2);
        if (f >= 0) {
-               while(read(f, (char *)&wtmp, sizeof(wtmp)) == sizeof(wtmp)) {
+               while(read(f, (char *)&wtmp, sizeof (wtmp)) == sizeof (wtmp)) {
                        if (SCMPN(wtmp.ut_line, line+5) || wtmp.ut_name[0]==0)
                                continue;
                        if (SCMPN(wtmp.ut_line, line+5) || wtmp.ut_name[0]==0)
                                continue;
-                       lseek(f, -(long)sizeof(wtmp), 1);
+                       lseek(f, -(long)sizeof (wtmp), 1);
                        SCPYN(wtmp.ut_name, "");
                        time(&wtmp.ut_time);
                        SCPYN(wtmp.ut_name, "");
                        time(&wtmp.ut_time);
-                       write(f, (char *)&wtmp, sizeof(wtmp));
+                       write(f, (char *)&wtmp, sizeof (wtmp));
                        found++;
                }
                close(f);
                        found++;
                }
                close(f);
@@ -561,7 +623,7 @@ rmut()
                        SCPYN(wtmp.ut_name, "");
                        time(&wtmp.ut_time);
                        lseek(f, (long)0, 2);
                        SCPYN(wtmp.ut_name, "");
                        time(&wtmp.ut_time);
                        lseek(f, (long)0, 2);
-                       write(f, (char *)&wtmp, sizeof(wtmp));
+                       write(f, (char *)&wtmp, sizeof (wtmp));
                        close(f);
                }
        }
                        close(f);
                }
        }