setgid to group "write" so that terminals need not be world writable
[unix-history] / usr / src / usr.bin / rlogin / rlogin.c
index bae9b45..68e4ace 100644 (file)
@@ -11,13 +11,15 @@ char copyright[] =
 #endif not lint
 
 #ifndef lint
 #endif not lint
 
 #ifndef lint
-static char sccsid[] = "@(#)rlogin.c   5.2 (Berkeley) %G%";
+static char sccsid[] = "@(#)rlogin.c   5.9 (Berkeley) %G%";
 #endif not lint
 
 /*
  * rlogin - remote login
  */
 #include <sys/types.h>
 #endif not lint
 
 /*
  * rlogin - remote login
  */
 #include <sys/types.h>
+#include <sys/errno.h>
+#include <sys/file.h>
 #include <sys/socket.h>
 #include <sys/wait.h>
 
 #include <sys/socket.h>
 #include <sys/wait.h>
 
@@ -28,8 +30,12 @@ static char sccsid[] = "@(#)rlogin.c 5.2 (Berkeley) %G%";
 #include <errno.h>
 #include <pwd.h>
 #include <signal.h>
 #include <errno.h>
 #include <pwd.h>
 #include <signal.h>
-#include <netdb.h>
 #include <setjmp.h>
 #include <setjmp.h>
+#include <netdb.h>
+
+# ifndef TIOCPKT_WINDOW
+# define TIOCPKT_WINDOW 0x80
+# endif TIOCPKT_WINDOW
 
 char   *index(), *rindex(), *malloc(), *getenv();
 struct passwd *getpwuid();
 
 char   *index(), *rindex(), *malloc(), *getenv();
 struct passwd *getpwuid();
@@ -44,10 +50,9 @@ char *speeds[] =
 char   term[256] = "network";
 extern int errno;
 int    lostpeer();
 char   term[256] = "network";
 extern int errno;
 int    lostpeer();
-int    nosigwin;
-jmp_buf        winsizechanged;
+int    dosigwinch = 0;
 struct winsize winsize;
 struct winsize winsize;
-int    sigwinch();
+int    sigwinch(), oob();
 
 main(argc, argv)
        int argc;
 
 main(argc, argv)
        int argc;
@@ -57,7 +62,7 @@ main(argc, argv)
        struct sgttyb ttyb;
        struct passwd *pwd;
        struct servent *sp;
        struct sgttyb ttyb;
        struct passwd *pwd;
        struct servent *sp;
-       int uid, options = 0;
+       int uid, options = 0, oldmask;
        int on = 1;
 
        host = rindex(argv[0], '/');
        int on = 1;
 
        host = rindex(argv[0], '/');
@@ -96,11 +101,6 @@ another:
                argv++, argc--;
                goto another;
        }
                argv++, argc--;
                goto another;
        }
-       if (argc > 0 && !strcmp(*argv, "-w")) {
-               nosigwin++;
-               argv++, argc--;
-               goto another;
-       }
        if (host == 0)
                goto usage;
        if (argc > 0)
        if (host == 0)
                goto usage;
        if (argc > 0)
@@ -122,12 +122,10 @@ another:
                strcat(term, "/");
                strcat(term, speeds[ttyb.sg_ospeed]);
        }
                strcat(term, "/");
                strcat(term, speeds[ttyb.sg_ospeed]);
        }
-       if (!nosigwin && ioctl(0, TIOCGWINSZ, &winsize) == 0) {
-               cp = index(term, '\0');
-               sprintf(cp, "/%u,%u,%u,%u", winsize.ws_row, winsize.ws_col,
-                   winsize.ws_xpixel, winsize.ws_ypixel);
-       }
+       (void) ioctl(0, TIOCGWINSZ, &winsize);
        signal(SIGPIPE, lostpeer);
        signal(SIGPIPE, lostpeer);
+       signal(SIGURG, oob);
+       oldmask = sigblock(sigmask(SIGURG));
         rem = rcmd(&host, sp->s_port, pwd->pw_name,
            name ? name : pwd->pw_name, term, 0);
         if (rem < 0)
         rem = rcmd(&host, sp->s_port, pwd->pw_name,
            name ? name : pwd->pw_name, term, 0);
         if (rem < 0)
@@ -140,11 +138,11 @@ another:
                perror("rlogin: setuid");
                exit(1);
        }
                perror("rlogin: setuid");
                exit(1);
        }
-       doit();
+       doit(oldmask);
        /*NOTREACHED*/
 usage:
        fprintf(stderr,
        /*NOTREACHED*/
 usage:
        fprintf(stderr,
-           "usage: rlogin host [ -ex ] [ -l username ] [ -8 ] [ -L ] [ -w ]\n");
+           "usage: rlogin host [ -ex ] [ -l username ] [ -8 ] [ -L ]\n");
        exit(1);
 }
 
        exit(1);
 }
 
@@ -152,6 +150,7 @@ usage:
 
 int    child;
 int    catchild();
 
 int    child;
 int    catchild();
+int    writeroob();
 
 int    defflags, tabflag;
 int    deflflags;
 
 int    defflags, tabflag;
 int    deflflags;
@@ -161,7 +160,7 @@ struct      ltchars defltc;
 struct tchars notc =   { -1, -1, -1, -1, -1, -1 };
 struct ltchars noltc = { -1, -1, -1, -1, -1, -1 };
 
 struct tchars notc =   { -1, -1, -1, -1, -1, -1 };
 struct ltchars noltc = { -1, -1, -1, -1, -1, -1 };
 
-doit()
+doit(oldmask)
 {
        int exit();
        struct sgttyb sb;
 {
        int exit();
        struct sgttyb sb;
@@ -177,37 +176,55 @@ doit()
        notc.t_startc = deftc.t_startc;
        notc.t_stopc = deftc.t_stopc;
        ioctl(0, TIOCGLTC, (char *)&defltc);
        notc.t_startc = deftc.t_startc;
        notc.t_stopc = deftc.t_stopc;
        ioctl(0, TIOCGLTC, (char *)&defltc);
-       signal(SIGINT, exit);
+       signal(SIGINT, SIG_IGN);
        signal(SIGHUP, exit);
        signal(SIGQUIT, exit);
        child = fork();
        if (child == -1) {
                perror("rlogin: fork");
        signal(SIGHUP, exit);
        signal(SIGQUIT, exit);
        child = fork();
        if (child == -1) {
                perror("rlogin: fork");
-               done();
+               done(1);
        }
        }
-       signal(SIGINT, SIG_IGN);
-       mode(1);
        if (child == 0) {
        if (child == 0) {
-               reader();
+               mode(1);
+               sigsetmask(oldmask);
+               if (reader() == 0) {
+                       prf("Connection closed.");
+                       exit(0);
+               }
                sleep(1);
                prf("\007Connection closed.");
                exit(3);
        }
                sleep(1);
                prf("\007Connection closed.");
                exit(3);
        }
+       signal(SIGURG, writeroob);
+       sigsetmask(oldmask);
        signal(SIGCHLD, catchild);
        signal(SIGCHLD, catchild);
-       if (!nosigwin)
-               signal(SIGWINCH, sigwinch);
        writer();
        prf("Closed connection.");
        writer();
        prf("Closed connection.");
-       done();
+       done(0);
 }
 
 }
 
-done()
+done(status)
+       int status;
 {
 
        mode(0);
        if (child > 0 && kill(child, SIGKILL) >= 0)
                wait((int *)0);
 {
 
        mode(0);
        if (child > 0 && kill(child, SIGKILL) >= 0)
                wait((int *)0);
-       exit(0);
+       exit(status);
+}
+
+/*
+ * This is called when the reader process gets the out-of-band (urgent)
+ * request to turn on the window-changing protocol.
+ */
+writeroob()
+{
+
+       if (dosigwinch == 0) {
+               sendwindow();
+               signal(SIGWINCH, sigwinch);
+       }
+       dosigwinch = 1;
 }
 
 catchild()
 }
 
 catchild()
@@ -223,7 +240,7 @@ again:
         * if the child (reader) dies, just quit
         */
        if (pid < 0 || pid == child && !WIFSTOPPED(status))
         * if the child (reader) dies, just quit
         */
        if (pid < 0 || pid == child && !WIFSTOPPED(status))
-               done();
+               done(status.w_termsig | status.w_retcode);
        goto again;
 }
 
        goto again;
 }
 
@@ -235,48 +252,18 @@ again:
  */
 writer()
 {
  */
 writer()
 {
-       char obuf[600], c;
-       register char *op;
+       char c;
        register n;
        register n;
+       register bol = 1;               /* beginning of line */
+       register local = 0;
 
 
-       /*
-        * Handle SIGWINCH's with in-band signaling of new
-        * window size.  It seems reasonable that we flush
-        * pending input and not force out of band signal
-        * as this most likely will occur from an input device
-        * other than the keyboard (e.g. a mouse).
-        *
-        * The hack of using 0377 to signal an in-band signal
-        * is pretty bad, but otherwise we'd be forced to
-        * either get complicated (use MSG_OOB) or go to a
-        * serious (telnet-style) protocol.
-        */
-       if (setjmp(winsizechanged)) {
-               struct winsize *wp = (struct winsize *)(obuf+4);
-
-               obuf[0] = 0377;                 /* XXX */
-               obuf[1] = 0377;                 /* XXX */
-               obuf[2] = 's';                  /* XXX */
-               obuf[3] = 's';                  /* XXX */
-               wp->ws_row = htons(winsize.ws_row);
-               wp->ws_col = htons(winsize.ws_col);
-               wp->ws_xpixel = htons(winsize.ws_xpixel);
-               wp->ws_ypixel = htons(winsize.ws_ypixel);
-               (void) write(rem, obuf, 4+sizeof (*wp));
-       }
-top:
-       op = obuf;
        for (;;) {
        for (;;) {
-               int local;
-
                n = read(0, &c, 1);
                if (n <= 0) {
                        if (n < 0 && errno == EINTR)
                                continue;
                        break;
                }
                n = read(0, &c, 1);
                if (n <= 0) {
                        if (n < 0 && errno == EINTR)
                                continue;
                        break;
                }
-               if (!eight)
-                       c &= 0177;
                /*
                 * If we're at the beginning of the line
                 * and recognize a command character, then
                /*
                 * If we're at the beginning of the line
                 * and recognize a command character, then
@@ -285,56 +272,62 @@ top:
                 * character is doubled, this acts as a 
                 * force and local echo is suppressed.
                 */
                 * character is doubled, this acts as a 
                 * force and local echo is suppressed.
                 */
-               if (op == obuf)
-                       local = (c == cmdchar);
-               if (op == obuf + 1 && *obuf == cmdchar)
-                       local = (c != cmdchar);
-               if (!local) {
-                       if (write(rem, &c, 1) == 0) {
-                               prf("line gone");
-                               return;
+               if (bol) {
+                       bol = 0;
+                       if (c == cmdchar) {
+                               bol = 0;
+                               local = 1;
+                               continue;
                        }
                        }
-                       if (!eight)
-                               c &= 0177;
-               } else {
-                       if (c == '\r' || c == '\n') {
-                               char cmdc = obuf[1];
-
-                               if (cmdc == '.' || cmdc == deftc.t_eofc) {
-                                       write(0, CRLF, sizeof(CRLF));
-                                       return;
-                               }
-                               if (cmdc == defltc.t_suspc ||
-                                   cmdc == defltc.t_dsuspc) {
-                                       stop(cmdc);
-                                       goto top;
-                               }
-                               *op++ = c;
-                               write(rem, obuf, op - obuf);
-                               goto top;
+               } else if (local) {
+                       local = 0;
+                       if (c == '.' || c == deftc.t_eofc) {
+                               echo(c);
+                               break;
                        }
                        }
-                       write(1, &c, 1);
+                       if (c == defltc.t_suspc || c == defltc.t_dsuspc) {
+                               bol = 1;
+                               echo(c);
+                               stop(c);
+                               continue;
+                       }
+                       if (c != cmdchar)
+                               write(rem, &cmdchar, 1);
                }
                }
-               *op++ = c;
-               if (c == deferase) {
-                       op -= 2; 
-                       if (op < obuf)
-                               goto top;
+               if (write(rem, &c, 1) == 0) {
+                       prf("line gone");
+                       break;
                }
                }
-               if (c == defkill || c == deftc.t_eofc ||
-                   c == '\r' || c == '\n')
-                       goto top;
-               if (op >= &obuf[sizeof (obuf)])
-                       op--;
+               bol = c == defkill || c == deftc.t_eofc ||
+                   c == deftc.t_intrc || c == defltc.t_suspc ||
+                   c == '\r' || c == '\n';
        }
 }
 
        }
 }
 
+echo(c)
+register char c;
+{
+       char buf[8];
+       register char *p = buf;
+
+       c &= 0177;
+       *p++ = cmdchar;
+       if (c < ' ') {
+               *p++ = '^';
+               *p++ = c + '@';
+       } else if (c == 0177) {
+               *p++ = '^';
+               *p++ = '?';
+       } else
+               *p++ = c;
+       *p++ = '\r';
+       *p++ = '\n';
+       write(1, buf, p - buf);
+}
+
 stop(cmdc)
        char cmdc;
 {
 stop(cmdc)
        char cmdc;
 {
-       struct winsize ws;
-
-       write(0, CRLF, sizeof(CRLF));
        mode(0);
        signal(SIGCHLD, SIG_IGN);
        kill(cmdc == defltc.t_suspc ? 0 : getpid(), SIGTSTP);
        mode(0);
        signal(SIGCHLD, SIG_IGN);
        kill(cmdc == defltc.t_suspc ? 0 : getpid(), SIGTSTP);
@@ -347,40 +340,130 @@ sigwinch()
 {
        struct winsize ws;
 
 {
        struct winsize ws;
 
-       if (!nosigwin && ioctl(0, TIOCGWINSZ, &ws) == 0 &&
+       if (dosigwinch && ioctl(0, TIOCGWINSZ, &ws) == 0 &&
            bcmp(&ws, &winsize, sizeof (ws))) {
                winsize = ws;
            bcmp(&ws, &winsize, sizeof (ws))) {
                winsize = ws;
-               longjmp(winsizechanged, 1);
+               sendwindow();
        }
 }
 
        }
 }
 
+/*
+ * Send the window size to the server via the magic escape
+ */
+sendwindow()
+{
+       char obuf[4 + sizeof (struct winsize)];
+       struct winsize *wp = (struct winsize *)(obuf+4);
+
+       obuf[0] = 0377;
+       obuf[1] = 0377;
+       obuf[2] = 's';
+       obuf[3] = 's';
+       wp->ws_row = htons(winsize.ws_row);
+       wp->ws_col = htons(winsize.ws_col);
+       wp->ws_xpixel = htons(winsize.ws_xpixel);
+       wp->ws_ypixel = htons(winsize.ws_ypixel);
+       (void) write(rem, obuf, sizeof(obuf));
+}
+
+/*
+ * reader: read from remote: line -> 1
+ */
+#define        READING 1
+#define        WRITING 2
+
+char   rcvbuf[8 * 1024];
+int    rcvcnt;
+int    rcvstate;
+jmp_buf        rcvtop;
+
 oob()
 {
 oob()
 {
-       int out = 1+1, atmark;
+       int out = FWRITE, atmark, n;
+       int rcvd = 0;
        char waste[BUFSIZ], mark;
        char waste[BUFSIZ], mark;
+       struct sgttyb sb;
 
 
-       ioctl(1, TIOCFLUSH, (char *)&out);
-       for (;;) {
-               if (ioctl(rem, SIOCATMARK, &atmark) < 0) {
-                       perror("ioctl");
-                       break;
-               }
-               if (atmark)
-                       break;
-               if (read(rem, waste, sizeof (waste)) <= 0)
-                       break;
+       while (recv(rem, &mark, 1, MSG_OOB) < 0)
+               switch (errno) {
+               
+               case EWOULDBLOCK:
+                       /*
+                        * Urgent data not here yet.
+                        * It may not be possible to send it yet
+                        * if we are blocked for output
+                        * and our input buffer is full.
+                        */
+                       if (rcvcnt < sizeof(rcvbuf)) {
+                               n = read(rem, rcvbuf + rcvcnt,
+                                       sizeof(rcvbuf) - rcvcnt);
+                               if (n <= 0)
+                                       return;
+                               rcvd += n;
+                       } else {
+                               n = read(rem, waste, sizeof(waste));
+                               if (n <= 0)
+                                       return;
+                       }
+                       continue;
+                               
+               default:
+                       return;
        }
        }
-       recv(rem, &mark, 1, MSG_OOB);
-       if (mark & TIOCPKT_NOSTOP) {
+       if (mark & TIOCPKT_WINDOW) {
+               /*
+                * Let server know about window size changes
+                */
+               kill(getppid(), SIGURG);
+       }
+       if (!eight && (mark & TIOCPKT_NOSTOP)) {
+               ioctl(0, TIOCGETP, (char *)&sb);
+               sb.sg_flags &= ~CBREAK;
+               sb.sg_flags |= RAW;
+               ioctl(0, TIOCSETN, (char *)&sb);
                notc.t_stopc = -1;
                notc.t_startc = -1;
                ioctl(0, TIOCSETC, (char *)&notc);
        }
                notc.t_stopc = -1;
                notc.t_startc = -1;
                ioctl(0, TIOCSETC, (char *)&notc);
        }
-       if (mark & TIOCPKT_DOSTOP) {
+       if (!eight && (mark & TIOCPKT_DOSTOP)) {
+               ioctl(0, TIOCGETP, (char *)&sb);
+               sb.sg_flags &= ~RAW;
+               sb.sg_flags |= CBREAK;
+               ioctl(0, TIOCSETN, (char *)&sb);
                notc.t_stopc = deftc.t_stopc;
                notc.t_startc = deftc.t_startc;
                ioctl(0, TIOCSETC, (char *)&notc);
        }
                notc.t_stopc = deftc.t_stopc;
                notc.t_startc = deftc.t_startc;
                ioctl(0, TIOCSETC, (char *)&notc);
        }
+       if (mark & TIOCPKT_FLUSHWRITE) {
+               ioctl(1, TIOCFLUSH, (char *)&out);
+               for (;;) {
+                       if (ioctl(rem, SIOCATMARK, &atmark) < 0) {
+                               perror("ioctl");
+                               break;
+                       }
+                       if (atmark)
+                               break;
+                       n = read(rem, waste, sizeof (waste));
+                       if (n <= 0)
+                               break;
+               }
+               /*
+                * Don't want any pending data to be output,
+                * so clear the recv buffer.
+                * If we were hanging on a write when interrupted,
+                * don't want it to restart.  If we were reading,
+                * restart anyway.
+                */
+               rcvcnt = 0;
+               longjmp(rcvtop, 1);
+       }
+       /*
+        * If we filled the receive buffer while a read was pending,
+        * longjmp to the top to restart appropriately.  Don't abort
+        * a pending write, however, or we won't know how much was written.
+        */
+       if (rcvd && rcvstate == READING)
+               longjmp(rcvtop, 1);
 }
 
 /*
 }
 
 /*
@@ -388,22 +471,35 @@ oob()
  */
 reader()
 {
  */
 reader()
 {
-       char rb[BUFSIZ];
-       register int cnt;
+       int pid = getpid();
+       int n, remaining;
+       char *bufp = rcvbuf;
 
 
-       signal(SIGURG, oob);
-       { int pid = -getpid();
-         ioctl(rem, SIOCSPGRP, (char *)&pid); }
+       signal(SIGTTOU, SIG_IGN);
+       fcntl(rem, F_SETOWN, pid);
+       (void) setjmp(rcvtop);
        for (;;) {
        for (;;) {
-               cnt = read(rem, rb, sizeof (rb));
-               if (cnt == 0)
-                       break;
-               if (cnt < 0) {
+               while ((remaining = rcvcnt - (bufp - rcvbuf)) > 0) {
+                       rcvstate = WRITING;
+                       n = write(1, bufp, remaining);
+                       if (n < 0) {
+                               if (errno != EINTR)
+                                       return (-1);
+                               continue;
+                       }
+                       bufp += n;
+               }
+               bufp = rcvbuf;
+               rcvcnt = 0;
+               rcvstate = READING;
+               rcvcnt = read(rem, rcvbuf, sizeof (rcvbuf));
+               if (rcvcnt == 0)
+                       return (0);
+               if (rcvcnt < 0) {
                        if (errno == EINTR)
                                continue;
                        if (errno == EINTR)
                                continue;
-                       break;
+                       return (-1);
                }
                }
-               write(1, rb, cnt);
        }
 }
 
        }
 }
 
@@ -451,10 +547,10 @@ mode(f)
 }
 
 /*VARARGS*/
 }
 
 /*VARARGS*/
-prf(f, a1, a2, a3)
+prf(f, a1, a2, a3, a4, a5)
        char *f;
 {
        char *f;
 {
-       fprintf(stderr, f, a1, a2, a3);
+       fprintf(stderr, f, a1, a2, a3, a4, a5);
        fprintf(stderr, CRLF);
 }
 
        fprintf(stderr, CRLF);
 }
 
@@ -462,5 +558,5 @@ lostpeer()
 {
        signal(SIGPIPE, SIG_IGN);
        prf("\007Connection closed.");
 {
        signal(SIGPIPE, SIG_IGN);
        prf("\007Connection closed.");
-       done();
+       done(1);
 }
 }