fixed exclusion from group 0 (from jrs@purue.arpa)
[unix-history] / usr / src / libexec / telnetd / telnetd.c
index 1015310..d2db5ef 100644 (file)
@@ -1,5 +1,5 @@
 #ifndef lint
 #ifndef lint
-static char sccsid[] = "@(#)telnetd.c  4.19 83/05/03";
+static char sccsid[] = "@(#)telnetd.c  4.27 (Berkeley) %G%";
 #endif
 
 /*
 #endif
 
 /*
@@ -7,6 +7,7 @@ static char sccsid[] = "@(#)telnetd.c   4.19 83/05/03";
  */
 #include <sys/types.h>
 #include <sys/socket.h>
  */
 #include <sys/types.h>
 #include <sys/socket.h>
+#include <sys/wait.h>
 
 #include <netinet/in.h>
 
 
 #include <netinet/in.h>
 
@@ -16,10 +17,10 @@ static char sccsid[] = "@(#)telnetd.c       4.19 83/05/03";
 #include <signal.h>
 #include <errno.h>
 #include <sgtty.h>
 #include <signal.h>
 #include <errno.h>
 #include <sgtty.h>
-#include <wait.h>
 #include <netdb.h>
 
 #include <netdb.h>
 
-#define        BELL            '\07'
+#define        BELL    '\07'
+#define BANNER "\r\n\r\n4.2 BSD UNIX (%s)\r\n\r\r\n\r%s"
 
 char   hisopts[256];
 char   myopts[256];
 
 char   hisopts[256];
 char   myopts[256];
@@ -40,110 +41,43 @@ int        pcc, ncc;
 
 int    pty, net;
 int    inter;
 
 int    pty, net;
 int    inter;
-int    reapchild();
+extern char **environ;
 extern int errno;
 char   line[] = "/dev/ptyp0";
 
 extern int errno;
 char   line[] = "/dev/ptyp0";
 
-struct sockaddr_in sin = { AF_INET };
-
 main(argc, argv)
        char *argv[];
 {
 main(argc, argv)
        char *argv[];
 {
-       int s, pid, options;
-       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++;
-       if (argc > 0 && !strcmp(*argv, "-d")) {
-               options |= SO_DEBUG;
-               argc--, argv++;
-       }
-       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);
+       struct sockaddr_in from;
+       int fromlen;
+
+       fromlen = sizeof (from);
+       if (getpeername(0, &from, &fromlen) < 0) {
+               fprintf(stderr, "%s: ", argv[0]);
+               perror("getpeername");
+               _exit(1);
        }
        }
-#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
-again:
-       s = socket(AF_INET, SOCK_STREAM, 0, 0);
-       if (s < 0) {
-               perror("telnetd: socket");;
-               sleep(5);
-               goto again;
-       }
-       if (options & SO_DEBUG)
-               if (setsockopt(s, SOL_SOCKET, SO_DEBUG, 0, 0) < 0)
-                       perror("telnetd: setsockopt (SO_DEBUG)");
-       if (setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, 0, 0) < 0)
-               perror("telnetd: setsockopt (SO_KEEPALIVE)");
-       while (bind(s, (caddr_t)&sin, sizeof (sin), 0) < 0) {
-               perror("telnetd: bind");
-               sleep(5);
-       }
-       sigset(SIGCHLD, reapchild);
-       listen(s, 10);
-       for (;;) {
-               int s2;
-
-               s2 = accept(s, (caddr_t)0, 0, 0);
-               if (s2 < 0) {
-                       if (errno == EINTR)
-                               continue;
-                       perror("telnetd: accept");
-                       sleep(1);
-                       continue;
-               }
-               if ((pid = fork()) < 0)
-                       printf("Out of processes\n");
-               else if (pid == 0) {
-                       signal(SIGCHLD, SIG_IGN);
-                       doit(s2);
-               }
-               close(s2);
+       if (setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, 0, 0) < 0) {
+               fprintf(stderr, "%s: ", argv[0]);
+               perror("setsockopt (SO_KEEPALIVE)");
        }
        }
-       /*NOTREACHED*/
-}
-
-reapchild()
-{
-       union wait status;
-
-       while (wait3(&status, WNOHANG, 0) > 0)
-               ;
+       doit(0, &from);
 }
 
 }
 
+char   *envinit[] = { "TERM=network", 0 };
 int    cleanup();
 
 /*
  * Get a pty, scan input lines.
  */
 int    cleanup();
 
 /*
  * Get a pty, scan input lines.
  */
-doit(f)
+doit(f, who)
+       int f;
+       struct sockaddr_in *who;
 {
 {
-       char *cp = line;
+       char *cp = line, *host, *ntoa();
        int i, p, cc, t;
        struct sgttyb b;
        int i, p, cc, t;
        struct sgttyb b;
+       struct hostent *hp;
 
        for (i = 0; i < 16; i++) {
                cp[strlen("/dev/ptyp")] = "0123456789abcdef"[i];
 
        for (i = 0; i < 16; i++) {
                cp[strlen("/dev/ptyp")] = "0123456789abcdef"[i];
@@ -170,6 +104,12 @@ gotpty:
        ioctl(p, TIOCGETP, &b);
        b.sg_flags &= ~ECHO;
        ioctl(p, TIOCSETP, &b);
        ioctl(p, TIOCGETP, &b);
        b.sg_flags &= ~ECHO;
        ioctl(p, TIOCSETP, &b);
+       hp = gethostbyaddr(&who->sin_addr, sizeof (struct in_addr),
+               who->sin_family);
+       if (hp)
+               host = hp->h_name;
+       else
+               host = ntoa(who->sin_addr);
        if ((i = fork()) < 0)
                fatalperror(f, "fork", errno);
        if (i)
        if ((i = fork()) < 0)
                fatalperror(f, "fork", errno);
        if (i)
@@ -180,7 +120,8 @@ gotpty:
        dup2(t, 1);
        dup2(t, 2);
        close(t);
        dup2(t, 1);
        dup2(t, 2);
        close(t);
-       execl("/bin/login", "telnet-login", 0);
+       environ = envinit;
+       execl("/bin/login", "login", "-h", host, 0);
        fatalperror(f, "/bin/login", errno);
        /*NOTREACHED*/
 }
        fatalperror(f, "/bin/login", errno);
        /*NOTREACHED*/
 }
@@ -215,18 +156,25 @@ fatalperror(f, msg, errno)
 telnet(f, p)
 {
        int on = 1;
 telnet(f, p)
 {
        int on = 1;
+       char hostname[32];
 
        net = f, pty = p;
        ioctl(f, FIONBIO, &on);
        ioctl(p, FIONBIO, &on);
        signal(SIGTSTP, SIG_IGN);
 
        net = f, pty = p;
        ioctl(f, FIONBIO, &on);
        ioctl(p, FIONBIO, &on);
        signal(SIGTSTP, SIG_IGN);
-       sigset(SIGCHLD, cleanup);
+       signal(SIGCHLD, cleanup);
 
        /*
         * Request to do remote echo.
         */
        dooption(TELOPT_ECHO);
        myopts[TELOPT_ECHO] = 1;
 
        /*
         * Request to do remote echo.
         */
        dooption(TELOPT_ECHO);
        myopts[TELOPT_ECHO] = 1;
+       /*
+        * Show banner that getty never gave.
+        */
+       gethostname(hostname, sizeof (hostname));
+       sprintf(nfrontp, BANNER, hostname, "");
+       nfrontp += strlen(nfrontp);
        for (;;) {
                int ibits = 0, obits = 0;
                register int c;
        for (;;) {
                int ibits = 0, obits = 0;
                register int c;
@@ -627,6 +575,7 @@ rmut()
                                continue;
                        lseek(f, -(long)sizeof (wtmp), 1);
                        SCPYN(wtmp.ut_name, "");
                                continue;
                        lseek(f, -(long)sizeof (wtmp), 1);
                        SCPYN(wtmp.ut_name, "");
+                       SCPYN(wtmp.ut_host, "");
                        time(&wtmp.ut_time);
                        write(f, (char *)&wtmp, sizeof (wtmp));
                        found++;
                        time(&wtmp.ut_time);
                        write(f, (char *)&wtmp, sizeof (wtmp));
                        found++;
@@ -638,6 +587,7 @@ rmut()
                if (f >= 0) {
                        SCPYN(wtmp.ut_line, line+5);
                        SCPYN(wtmp.ut_name, "");
                if (f >= 0) {
                        SCPYN(wtmp.ut_line, line+5);
                        SCPYN(wtmp.ut_name, "");
+                       SCPYN(wtmp.ut_host, "");
                        time(&wtmp.ut_time);
                        lseek(f, (long)0, 2);
                        write(f, (char *)&wtmp, sizeof (wtmp));
                        time(&wtmp.ut_time);
                        lseek(f, (long)0, 2);
                        write(f, (char *)&wtmp, sizeof (wtmp));
@@ -650,3 +600,20 @@ rmut()
        chmod(line, 0666);
        chown(line, 0, 0);
 }
        chmod(line, 0666);
        chown(line, 0, 0);
 }
+
+/*
+ * Convert network-format internet address
+ * to base 256 d.d.d.d representation.
+ */
+char *
+ntoa(in)
+       struct in_addr in;
+{
+       static char b[18];
+       register char *p;
+
+       p = (char *)&in;
+#define        UC(b)   (((int)b)&0xff)
+       sprintf(b, "%d.%d.%d.%d", UC(p[0]), UC(p[1]), UC(p[2]), UC(p[3]));
+       return (b);
+}