fixed order of tgoto args, zapped reliance on former isprint bug
[unix-history] / usr / src / usr.bin / tip / tip.c
index 4e168f2..e36593a 100644 (file)
@@ -1,4 +1,29 @@
-/*     tip.c   4.14    83/06/15        */
+/*
+ * Copyright (c) 1983 The 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.
+ */
+
+#ifndef lint
+char copyright[] =
+"@(#) Copyright (c) 1983 The Regents of the University of California.\n\
+ All rights reserved.\n";
+#endif /* not lint */
+
+#ifndef lint
+static char sccsid[] = "@(#)tip.c      5.8 (Berkeley) %G%";
+#endif /* not lint */
 
 /*
  * tip - UNIX link to other systems
 
 /*
  * tip - UNIX link to other systems
@@ -8,8 +33,6 @@
  */
 #include "tip.h"
 
  */
 #include "tip.h"
 
-static char *sccsid = "@(#)tip.c       4.14 %G%";
-
 /*
  * Baud rate mapping table
  */
 /*
  * Baud rate mapping table
  */
@@ -18,15 +41,12 @@ int bauds[] = {
        1200, 1800, 2400, 4800, 9600, 19200, -1
 };
 
        1200, 1800, 2400, 4800, 9600, 19200, -1
 };
 
-#ifdef VMUNIX
 int    disc = OTTYDISC;                /* tip normally runs this way */
 int    disc = OTTYDISC;                /* tip normally runs this way */
-#endif
-
 int    intprompt();
 int    timeout();
 int    cleanup();
 char   *sname();
 int    intprompt();
 int    timeout();
 int    cleanup();
 char   *sname();
-extern char *sprintf();
+char   PNbuf[256];                     /* This limits the size of a number */
 
 main(argc, argv)
        char *argv[];
 
 main(argc, argv)
        char *argv[];
@@ -36,9 +56,13 @@ main(argc, argv)
        register char *p;
        char sbuf[12];
 
        register char *p;
        char sbuf[12];
 
+       gid = getgid();
+       egid = getegid();
+       uid = getuid();
+       euid = geteuid();
        if (equal(sname(argv[0]), "cu")) {
        if (equal(sname(argv[0]), "cu")) {
-               cumain(argc, argv);
                cumode = 1;
                cumode = 1;
+               cumain(argc, argv);
                goto cucommon;
        }
 
                goto cucommon;
        }
 
@@ -73,11 +97,24 @@ main(argc, argv)
 
        if (system == NOSTR)
                goto notnumber;
 
        if (system == NOSTR)
                goto notnumber;
+       if (isalpha(*system))
+               goto notnumber;
+       /*
+        * System name is really a phone number...
+        * Copy the number then stomp on the original (in case the number
+        *      is private, we don't want 'ps' or 'w' to find it).
+        */
+       if (strlen(system) > sizeof PNbuf - 1) {
+               fprintf(stderr, "tip: phone number too long (max = %d bytes)\n",
+                       sizeof PNbuf - 1);
+               exit(1);
+       }
+       strncpy( PNbuf, system, sizeof PNbuf - 1 );
        for (p = system; *p; p++)
        for (p = system; *p; p++)
-               if (isalpha(*p))
-                       goto notnumber;
-       PN = system;            /* system name is really a phone number */
-       system = sprintf(sbuf, "tip%d", BR);
+               *p = '\0';
+       PN = PNbuf;
+       (void)sprintf(sbuf, "tip%d", BR);
+       system = sbuf;
 
 notnumber:
        signal(SIGINT, cleanup);
 
 notnumber:
        signal(SIGINT, cleanup);
@@ -91,20 +128,11 @@ notnumber:
        }
        if (i == -1) {
                printf("link down\n");
        }
        if (i == -1) {
                printf("link down\n");
-               delock(uucplock);
+               (void)uu_unlock(uucplock);
                exit(3);
        }
        setbuf(stdout, NULL);
        loginit();
                exit(3);
        }
        setbuf(stdout, NULL);
        loginit();
-       /*
-        * Now that we have the logfile and the ACU open
-        *  return to the real uid and gid.  These things will
-        *  be closed on exit.  Note that we can't run as root,
-        *  because locking mechanism on the tty and the accounting
-        *  will be bypassed.
-        */
-       setgid(getgid());
-       setuid(getuid());
 
        /*
         * Kludge, their's no easy way to get the initialization
 
        /*
         * Kludge, their's no easy way to get the initialization
@@ -113,13 +141,22 @@ notnumber:
        if ((PH = getenv("PHONES")) == NOSTR)
                PH = "/etc/phones";
        vinit();                                /* init variables */
        if ((PH = getenv("PHONES")) == NOSTR)
                PH = "/etc/phones";
        vinit();                                /* init variables */
-       setparity();                            /* set the parity table */
+       setparity("even");                      /* set the parity table */
        if ((i = speed(number(value(BAUDRATE)))) == NULL) {
                printf("tip: bad baud rate %d\n", number(value(BAUDRATE)));
        if ((i = speed(number(value(BAUDRATE)))) == NULL) {
                printf("tip: bad baud rate %d\n", number(value(BAUDRATE)));
-               delock(uucplock);
+               (void)uu_unlock(uucplock);
                exit(3);
        }
 
                exit(3);
        }
 
+       /*
+        * Now that we have the logfile and the ACU open
+        *  return to the real uid and gid.  These things will
+        *  be closed on exit.  Swap real and effective uid's
+        *  so we can get the original permissions back
+        *  for removing the uucp lock.
+        */
+       user_uid();
+
        /*
         * Hardwired connections require the
         *  line speed set before they make any transmissions
        /*
         * Hardwired connections require the
         *  line speed set before they make any transmissions
@@ -129,7 +166,8 @@ notnumber:
                ttysetup(i);
        if (p = connect()) {
                printf("\07%s\n[EOT]\n", p);
                ttysetup(i);
        if (p = connect()) {
                printf("\07%s\n[EOT]\n", p);
-               delock(uucplock);
+               daemon_uid();
+               (void)uu_unlock(uucplock);
                exit(1);
        }
        if (!HW)
                exit(1);
        }
        if (!HW)
@@ -139,12 +177,11 @@ cucommon:
         * From here down the code is shared with
         * the "cu" version of tip.
         */
         * From here down the code is shared with
         * the "cu" version of tip.
         */
+
        ioctl(0, TIOCGETP, (char *)&defarg);
        ioctl(0, TIOCGETC, (char *)&defchars);
        ioctl(0, TIOCGLTC, (char *)&deflchars);
        ioctl(0, TIOCGETP, (char *)&defarg);
        ioctl(0, TIOCGETC, (char *)&defchars);
        ioctl(0, TIOCGLTC, (char *)&deflchars);
-#ifdef VMUNIX
        ioctl(0, TIOCGETD, (char *)&odisc);
        ioctl(0, TIOCGETD, (char *)&odisc);
-#endif
        arg = defarg;
        arg.sg_flags = ANYP | CBREAK;
        tchars = defchars;
        arg = defarg;
        arg.sg_flags = ANYP | CBREAK;
        tchars = defchars;
@@ -159,7 +196,7 @@ cucommon:
 
        /*
         * Everything's set up now:
 
        /*
         * Everything's set up now:
-        *      connection established (hardwired or diaulup)
+        *      connection established (hardwired or dialup)
         *      line conditioned (baud rate, mode, etc.)
         *      internal data structures (variables)
         * so, fork one process for local side and one for remote.
         *      line conditioned (baud rate, mode, etc.)
         *      internal data structures (variables)
         * so, fork one process for local side and one for remote.
@@ -174,25 +211,60 @@ cucommon:
 
 cleanup()
 {
 
 cleanup()
 {
-       delock(uucplock);
-#ifdef VMUNIX
+
+       daemon_uid();
+       (void)uu_unlock(uucplock);
        if (odisc)
                ioctl(0, TIOCSETD, (char *)&odisc);
        if (odisc)
                ioctl(0, TIOCSETD, (char *)&odisc);
-#endif
        exit(0);
 }
 
        exit(0);
 }
 
+/*
+ * Muck with user ID's.  We are setuid to the owner of the lock
+ * directory when we start.  user_uid() reverses real and effective
+ * ID's after startup, to run with the user's permissions.
+ * daemon_uid() switches back to the privileged uid for unlocking.
+ * Finally, to avoid running a shell with the wrong real uid,
+ * shell_uid() sets real and effective uid's to the user's real ID.
+ */
+static int uidswapped;
+
+user_uid()
+{
+       if (uidswapped == 0) {
+               setregid(egid, gid);
+               setreuid(euid, uid);
+               uidswapped = 1;
+       }
+}
+
+daemon_uid()
+{
+
+       if (uidswapped) {
+               setreuid(uid, euid);
+               setregid(gid, egid);
+               uidswapped = 0;
+       }
+}
+
+shell_uid()
+{
+
+       setreuid(uid, uid);
+       setregid(gid, gid);
+}
+
 /*
  * put the controlling keyboard into raw mode
  */
 raw()
 {
 /*
  * put the controlling keyboard into raw mode
  */
 raw()
 {
+
        ioctl(0, TIOCSETP, &arg);
        ioctl(0, TIOCSETC, &tchars);
        ioctl(0, TIOCSLTC, &ltchars);
        ioctl(0, TIOCSETP, &arg);
        ioctl(0, TIOCSETC, &tchars);
        ioctl(0, TIOCSLTC, &ltchars);
-#ifdef VMUNIX
        ioctl(0, TIOCSETD, (char *)&disc);
        ioctl(0, TIOCSETD, (char *)&disc);
-#endif
 }
 
 
 }
 
 
@@ -201,14 +273,15 @@ raw()
  */
 unraw()
 {
  */
 unraw()
 {
-#ifdef VMUNIX
+
        ioctl(0, TIOCSETD, (char *)&odisc);
        ioctl(0, TIOCSETD, (char *)&odisc);
-#endif
        ioctl(0, TIOCSETP, (char *)&defarg);
        ioctl(0, TIOCSETC, (char *)&defchars);
        ioctl(0, TIOCSLTC, (char *)&deflchars);
 }
 
        ioctl(0, TIOCSETP, (char *)&defarg);
        ioctl(0, TIOCSETC, (char *)&defchars);
        ioctl(0, TIOCSLTC, (char *)&deflchars);
 }
 
+static jmp_buf promptbuf;
+
 /*
  * Print string ``s'', then read a string
  *  in from the terminal.  Handles signals & allows use of
 /*
  * Print string ``s'', then read a string
  *  in from the terminal.  Handles signals & allows use of
@@ -219,23 +292,22 @@ prompt(s, p)
        register char *p;
 {
        register char *b = p;
        register char *p;
 {
        register char *b = p;
+       int (*oint)(), (*oquit)();
 
        stoprompt = 0;
 
        stoprompt = 0;
-       signal(SIGINT, intprompt);
-       signal(SIGQUIT, SIG_IGN);
+       oint = signal(SIGINT, intprompt);
+       oint = signal(SIGQUIT, SIG_IGN);
        unraw();
        printf("%s", s);
        unraw();
        printf("%s", s);
-       while ((*p = getchar()) != EOF && *p != '\n') {
-               if (stoprompt)
-                       goto pbreak;
-               p++;
-       }
+       if (setjmp(promptbuf) == 0)
+               while ((*p = getchar()) != EOF && *p != '\n')
+                       p++;
        *p = '\0';
        *p = '\0';
-pbreak:
+
        raw();
        raw();
-       signal(SIGINT, SIG_DFL);
-       signal(SIGQUIT,SIG_DFL);
-       return(stoprompt || p == b);
+       signal(SIGINT, oint);
+       signal(SIGQUIT, oint);
+       return (stoprompt || p == b);
 }
 
 /*
 }
 
 /*
@@ -243,9 +315,11 @@ pbreak:
  */
 intprompt()
 {
  */
 intprompt()
 {
+
        signal(SIGINT, SIG_IGN);
        stoprompt = 1;
        printf("\r\n");
        signal(SIGINT, SIG_IGN);
        stoprompt = 1;
        printf("\r\n");
+       longjmp(promptbuf, 1);
 }
 
 /*
 }
 
 /*
@@ -306,45 +380,46 @@ escape()
        gch = (getchar()&0177);
        for (p = etable; p->e_char; p++)
                if (p->e_char == gch) {
        gch = (getchar()&0177);
        for (p = etable; p->e_char; p++)
                if (p->e_char == gch) {
-                       if ((p->e_flags&PRIV) && getuid())
+                       if ((p->e_flags&PRIV) && uid)
                                continue;
                        printf("%s", ctrl(c));
                        (*p->e_func)(gch);
                                continue;
                        printf("%s", ctrl(c));
                        (*p->e_func)(gch);
-                       return(0);
+                       return (0);
                }
        /* ESCAPE ESCAPE forces ESCAPE */
        if (c != gch)
                pwrite(FD, &c, 1);
                }
        /* ESCAPE ESCAPE forces ESCAPE */
        if (c != gch)
                pwrite(FD, &c, 1);
-       return(gch);
+       return (gch);
 }
 
 speed(n)
 }
 
 speed(n)
+       int n;
 {
        register int *p;
 
        for (p = bauds; *p != -1;  p++)
                if (*p == n)
 {
        register int *p;
 
        for (p = bauds; *p != -1;  p++)
                if (*p == n)
-                       return(p-bauds);
-       return(NULL);
+                       return (p - bauds);
+       return (NULL);
 }
 
 any(c, p)
        register char c, *p;
 {
 }
 
 any(c, p)
        register char c, *p;
 {
-       if (p)
-       while (*p)
+       while (p && *p)
                if (*p++ == c)
                if (*p++ == c)
-                       return(1);
-       return(0);
+                       return (1);
+       return (0);
 }
 
 size(s)
        register char   *s;
 {
 }
 
 size(s)
        register char   *s;
 {
-       register int    i = 0;
+       register int i = 0;
 
 
-       while (*s++) i++;
-       return(i);
+       while (s && *s++)
+               i++;
+       return (i);
 }
 
 char *
 }
 
 char *
@@ -370,7 +445,7 @@ interp(s)
                ;
        }
        *p = '\0';
                ;
        }
        *p = '\0';
-       return(buf);
+       return (buf);
 }
 
 char *
 }
 
 char *
@@ -387,7 +462,7 @@ ctrl(c)
                s[0] = c;
                s[1] = '\0';
        }
                s[0] = c;
                s[1] = '\0';
        }
-       return(s);
+       return (s);
 }
 
 /*
 }
 
 /*
@@ -401,7 +476,7 @@ help(c)
 
        printf("%c\r\n", c);
        for (p = etable; p->e_char; p++) {
 
        printf("%c\r\n", c);
        for (p = etable; p->e_char; p++) {
-               if ((p->e_flags&PRIV) && getuid())
+               if ((p->e_flags&PRIV) && uid)
                        continue;
                printf("%2s", ctrl(character(value(ESCAPE))));
                printf("%-2s %c   %s\r\n", ctrl(p->e_char),
                        continue;
                printf("%2s", ctrl(character(value(ESCAPE))));
                printf("%-2s %c   %s\r\n", ctrl(p->e_char),
@@ -413,20 +488,16 @@ help(c)
  * Set up the "remote" tty's state
  */
 ttysetup(speed)
  * Set up the "remote" tty's state
  */
 ttysetup(speed)
+       int speed;
 {
 {
-#ifdef VMUNIX
        unsigned bits = LDECCTQ;
        unsigned bits = LDECCTQ;
-#endif
 
        arg.sg_ispeed = arg.sg_ospeed = speed;
 
        arg.sg_ispeed = arg.sg_ospeed = speed;
+       arg.sg_flags = RAW;
        if (boolean(value(TAND)))
        if (boolean(value(TAND)))
-               arg.sg_flags = TANDEM|RAW;
-       else
-               arg.sg_flags = RAW;
+               arg.sg_flags |= TANDEM;
        ioctl(FD, TIOCSETP, (char *)&arg);
        ioctl(FD, TIOCSETP, (char *)&arg);
-#ifdef VMUNIX
        ioctl(FD, TIOCLBIS, (char *)&bits);
        ioctl(FD, TIOCLBIS, (char *)&bits);
-#endif
 }
 
 /*
 }
 
 /*
@@ -445,12 +516,11 @@ sname(s)
        return (p);
 }
 
        return (p);
 }
 
-extern char chartab[];
 static char partab[0200];
 
 /*
 static char partab[0200];
 
 /*
- * do a write to the remote machine with the correct parity
- * we are doing 8 bit wide output, so we just generate a character
+ * Do a write to the remote machine with the correct parity.
+ * We are doing 8 bit wide output, so we just generate a character
  * with the right parity and output it.
  */
 pwrite(fd, buf, n)
  * with the right parity and output it.
  */
 pwrite(fd, buf, n)
@@ -459,48 +529,54 @@ pwrite(fd, buf, n)
        register int n;
 {
        register int i;
        register int n;
 {
        register int i;
-       register char *bp = buf;
+       register char *bp;
+       extern int errno;
 
 
-       for (i = 0, bp = buf; i < n; i++, bp++) {
+       bp = buf;
+       for (i = 0; i < n; i++) {
                *bp = partab[(*bp) & 0177];
                *bp = partab[(*bp) & 0177];
+               bp++;
+       }
+       if (write(fd, buf, n) < 0) {
+               if (errno == EIO)
+                       abort("Lost carrier.");
+               /* this is questionable */
+               perror("write");
        }
        }
-       write(fd, buf, n);
 }
 
 /*
 }
 
 /*
- * build a parity table with the right high-order bit
- * copy an even-parity table and doctor it
+ * Build a parity table with appropriate high-order bit.
  */
  */
-setparity()
+setparity(defparity)
+       char *defparity;
 {
 {
-       int i;
+       register int i;
        char *parity;
        char *parity;
+       extern char evenpartab[];
 
        if (value(PARITY) == NOSTR)
 
        if (value(PARITY) == NOSTR)
-               value(PARITY) = "even";
-
+               value(PARITY) = defparity;
        parity = value(PARITY);
        parity = value(PARITY);
-
        for (i = 0; i < 0200; i++)
        for (i = 0; i < 0200; i++)
-               partab[i] = chartab[i];
-
+               partab[i] = evenpartab[i];
+       if (equal(parity, "even"))
+               return;
        if (equal(parity, "odd")) {
                for (i = 0; i < 0200; i++)
                        partab[i] ^= 0200;      /* reverse bit 7 */
        if (equal(parity, "odd")) {
                for (i = 0; i < 0200; i++)
                        partab[i] ^= 0200;      /* reverse bit 7 */
+               return;
        }
        }
-       else if (equal(parity, "none") || equal(parity, "zero")) {
+       if (equal(parity, "none") || equal(parity, "zero")) {
                for (i = 0; i < 0200; i++)
                        partab[i] &= ~0200;     /* turn off bit 7 */
                for (i = 0; i < 0200; i++)
                        partab[i] &= ~0200;     /* turn off bit 7 */
+               return;
        }
        }
-       else if (equal(parity, "one")) {
+       if (equal(parity, "one")) {
                for (i = 0; i < 0200; i++)
                        partab[i] |= 0200;      /* turn on bit 7 */
                for (i = 0; i < 0200; i++)
                        partab[i] |= 0200;      /* turn on bit 7 */
+               return;
        }
        }
-       else if (equal(parity, "even")) {
-               /* table is already even parity */
-       }
-       else {
-               fprintf(stderr, "parity value %s unknown\n", PA);
-               fflush(stderr);
-       }
+       fprintf(stderr, "%s: unknown parity value\n", PA);
+       fflush(stderr);
 }
 }