BSD 4_3_Tahoe release
[unix-history] / usr / src / usr.bin / tip / tip.c
index 973280b..019f29e 100644 (file)
@@ -1,50 +1,25 @@
-/*     tip.c   4.1     81/05/09        */
 /*
 /*
- * tip - Unix link to other systems
- *  tip [-v] [-speed] system-name
- *
- * Uses remote file for system descriptions.
- * Current commands (escapes):
- *
- *     ~!      fork a shell on the local machine
- *     ~c      change working directory on local machine
- *     ~^D     exit tip
- *     ~<      fetch file from remote system
- *     ~>      send file to remote system
- *     ~t      take a file from a remote UNIX (uses cat & echo)
- *     ~p      send a file to a remote UNIX (uses cat)
- *     ~|      fetch file from remote system and pipe it to
- *              a local process
- *     ~%      fork and wait for a program which inherits file
- *              descriptors 3 & 4 attached to the remote machine
- *              (optional by CONNECT define)
- *     ~s      set or show variable
- *     ~?      give a help summary
- *
- * Samuel J. Leffler   1-18-81
- *
- * sjl                 2-11-81
- * add auto-dial stuff for the BIZCOMP
- *
- * sjl                 2-14-81
- * cleaned up auto-dialer stuff and added variables
- *
- * sjl                 2-19-81
- * handle quit and interrupt during calls
- *
- * sjl                 3-8-81
- * made to pass lint
- *
- * sjl                 4-11-81
- * mods to handle both FIOCAPACITY and FIONREAD in biz.c
- *
- * sjl                 4-17-81
- * added take and put, made piping stuff work
- * honor uucp locks
- * rewrite remote file stuff for DN-11 like acu's and just to clean
- *   it up
+ * Copyright (c) 1983 Regents of the University of California.
+ * All rights reserved.  The Berkeley software License Agreement
+ * specifies the terms and conditions for redistribution.
  */
 
  */
 
+#ifndef lint
+char copyright[] =
+"@(#) Copyright (c) 1983 Regents of the University of California.\n\
+ All rights reserved.\n";
+#endif not lint
+
+#ifndef lint
+static char sccsid[] = "@(#)tip.c      5.6 (Berkeley) 10/22/87";
+#endif not lint
+
+/*
+ * tip - UNIX link to other systems
+ *  tip [-v] [-speed] system-name
+ * or
+ *  cu phone-number [-s speed] [-l line] [-a acu]
+ */
 #include "tip.h"
 
 /*
 #include "tip.h"
 
 /*
@@ -55,19 +30,30 @@ int bauds[] = {
        1200, 1800, 2400, 4800, 9600, 19200, -1
 };
 
        1200, 1800, 2400, 4800, 9600, 19200, -1
 };
 
+int    disc = OTTYDISC;                /* tip normally runs this way */
 int    intprompt();
 int    timeout();
 int    intprompt();
 int    timeout();
-static int cleanup();
+int    cleanup();
+char   *sname();
+char   PNbuf[256];                     /* This limits the size of a number */
 
 main(argc, argv)
 
 main(argc, argv)
-char *argv[];
+       char *argv[];
 {
        char *system = NOSTR;
        register int i;
 {
        char *system = NOSTR;
        register int i;
-#ifdef VMUNIX
-       int disc;
-#endif
-       char *p;
+       register char *p;
+       char sbuf[12];
+
+       gid = getgid();
+       egid = getegid();
+       uid = getuid();
+       euid = geteuid();
+       if (equal(sname(argv[0]), "cu")) {
+               cumode = 1;
+               cumain(argc, argv);
+               goto cucommon;
+       }
 
        if (argc > 4) {
                fprintf(stderr, "usage: tip [-v] [-speed] [system-name]\n");
 
        if (argc > 4) {
                fprintf(stderr, "usage: tip [-v] [-speed] [system-name]\n");
@@ -77,33 +63,66 @@ char *argv[];
                fprintf(stderr, "tip: must be interactive\n");
                exit(1);
        }
                fprintf(stderr, "tip: must be interactive\n");
                exit(1);
        }
-       if (argc > 1 && argv[argc-1][0] != '-')
-               system = argv[argc-1];          /* always last item */
+
+       for (; argc > 1; argv++, argc--) {
+               if (argv[1][0] != '-')
+                       system = argv[1];
+               else switch (argv[1][1]) {
+
+               case 'v':
+                       vflag++;
+                       break;
+
+               case '0': case '1': case '2': case '3': case '4':
+               case '5': case '6': case '7': case '8': case '9':
+                       BR = atoi(&argv[1][1]);
+                       break;
+
+               default:
+                       fprintf(stderr, "tip: %s, unknown option\n", argv[1]);
+                       break;
+               }
+       }
+
+       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++)
+               *p = '\0';
+       PN = PNbuf;
+       (void)sprintf(sbuf, "tip%d", BR);
+       system = sbuf;
+
+notnumber:
        signal(SIGINT, cleanup);
        signal(SIGQUIT, cleanup);
        signal(SIGHUP, cleanup);
        signal(SIGTERM, cleanup);
        signal(SIGINT, cleanup);
        signal(SIGQUIT, cleanup);
        signal(SIGHUP, cleanup);
        signal(SIGTERM, cleanup);
+
        if ((i = hunt(system)) == 0) {
                printf("all ports busy\n");
                exit(3);
        }
        if (i == -1) {
                printf("link down\n");
        if ((i = hunt(system)) == 0) {
                printf("all ports busy\n");
                exit(3);
        }
        if (i == -1) {
                printf("link down\n");
+               delock(uucplock);
                exit(3);
        }
                exit(3);
        }
+       setbuf(stdout, NULL);
        loginit();
        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.
-        */
-       setuid(getuid());
-       setgid(getgid());
-       for (i = 1; i < argc-1; i++)
-               if (equal(argv[i], "-v"))
-                       vflag++;
+
        /*
         * Kludge, their's no easy way to get the initialization
         *   in the right order, so force it here
        /*
         * Kludge, their's no easy way to get the initialization
         *   in the right order, so force it here
@@ -111,51 +130,67 @@ char *argv[];
        if ((PH = getenv("PHONES")) == NOSTR)
                PH = "/etc/phones";
        vinit();                                /* init variables */
        if ((PH = getenv("PHONES")) == NOSTR)
                PH = "/etc/phones";
        vinit();                                /* init variables */
-       for (i = 1; i < argc-1; i++)
-               if (argv[i][0] == '-' && argv[i][1] != 'v') {
-                       if (isnum(argv[i][1]))
-                               number(value(BAUDRATE)) = atoi(&argv[i][1]);
-                       else
-                               printf("%s: unknown option\n", argv[i]);
-               }
-       if ((arg.sg_ispeed = speed(number(value(BAUDRATE)))) == NULL) {
+       setparity("even");                      /* set the parity table */
+       if ((i = speed(number(value(BAUDRATE)))) == NULL) {
                printf("tip: bad baud rate %d\n", number(value(BAUDRATE)));
                delock(uucplock);
                exit(3);
        }
                printf("tip: bad baud rate %d\n", number(value(BAUDRATE)));
                delock(uucplock);
                exit(3);
        }
-       arg.sg_ospeed = arg.sg_ispeed;
+
        /*
        /*
-        * NOTE that remote side runs in TANDEM mode,
-        *  if the host doesn't honor X-ON/X-OFF with default
-        *  start/stop chars, the remote description must be
-        *  extended and tchars will have to be set up here.
-        * If the host doesn't honor TANDEM mode, then watch
-        *  out, as you'll get garbage.
+        * 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.
         */
         */
-       arg.sg_flags = RAW | TANDEM;
-       ioctl(FD, TIOCSETP, &arg);
-
-       ioctl(0, TIOCGETP, &defarg);    /* store initial status */
-       ioctl(0, TIOCGETC, &defchars);
-       arg = defarg;
-       arg.sg_flags = ANYP | CBREAK;
-       tchars = defchars;
-       tchars.t_intrc = tchars.t_quitc = -1;
+       user_uid();
 
 
+       /*
+        * Hardwired connections require the
+        *  line speed set before they make any transmissions
+        *  (this is particularly true of things like a DF03-AC)
+        */
+       if (HW)
+               ttysetup(i);
        if (p = connect()) {
                printf("\07%s\n[EOT]\n", p);
        if (p = connect()) {
                printf("\07%s\n[EOT]\n", p);
+               daemon_uid();
                delock(uucplock);
                exit(1);
        }
                delock(uucplock);
                exit(1);
        }
-       write(1, "\07connected\n", 11);
-       raw();
-#ifdef VMUNIX
+       if (!HW)
+               ttysetup(i);
+cucommon:
+       /*
+        * 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, TIOCGETD, (char *)&odisc);
        ioctl(0, TIOCGETD, (char *)&odisc);
-       disc = OTTYDISC;
-       ioctl(0, TIOCSETD, (char *)&disc);
-#endif
+       arg = defarg;
+       arg.sg_flags = ANYP | CBREAK;
+       tchars = defchars;
+       tchars.t_intrc = tchars.t_quitc = -1;
+       ltchars = deflchars;
+       ltchars.t_suspc = ltchars.t_dsuspc = ltchars.t_flushc
+               = ltchars.t_lnextc = -1;
+       raw();
+
        pipe(fildes); pipe(repdes);
        signal(SIGALRM, timeout);
        pipe(fildes); pipe(repdes);
        signal(SIGALRM, timeout);
+
+       /*
+        * Everything's set up now:
+        *      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.
+        */
+       printf(cumode ? "Connected\r\n" : "\07connected\r\n");
        if (pid = fork())
                tipin();
        else
        if (pid = fork())
                tipin();
        else
@@ -163,20 +198,62 @@ char *argv[];
        /*NOTREACHED*/
 }
 
        /*NOTREACHED*/
 }
 
-static
 cleanup()
 {
 cleanup()
 {
+
+       daemon_uid();
        delock(uucplock);
        delock(uucplock);
+       if (odisc)
+               ioctl(0, TIOCSETD, (char *)&odisc);
        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, TIOCSETP, &arg);
        ioctl(0, TIOCSETC, &tchars);
+       ioctl(0, TIOCSLTC, &ltchars);
+       ioctl(0, TIOCSETD, (char *)&disc);
 }
 
 
 }
 
 
@@ -185,10 +262,15 @@ raw()
  */
 unraw()
 {
  */
 unraw()
 {
-       ioctl(0, TIOCSETP, &defarg);
-       ioctl(0, TIOCSETC, &defchars);
+
+       ioctl(0, TIOCSETD, (char *)&odisc);
+       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
@@ -199,23 +281,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);
 }
 
 /*
 }
 
 /*
@@ -223,9 +304,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);
 }
 
 /*
 }
 
 /*
@@ -235,27 +318,40 @@ tipin()
 {
        char gch, bol = 1;
 
 {
        char gch, bol = 1;
 
+       /*
+        * Kinda klugey here...
+        *   check for scripting being turned on from the .tiprc file,
+        *   but be careful about just using setscript(), as we may
+        *   send a SIGEMT before tipout has a chance to set up catching
+        *   it; so wait a second, then setscript()
+        */
+       if (boolean(value(SCRIPT))) {
+               sleep(1);
+               setscript();
+       }
+
        while (1) {
                gch = getchar()&0177;
                if ((gch == character(value(ESCAPE))) && bol) {
                        if (!(gch = escape()))
                                continue;
        while (1) {
                gch = getchar()&0177;
                if ((gch == character(value(ESCAPE))) && bol) {
                        if (!(gch = escape()))
                                continue;
-               } else if (gch == character(value(RAISECHAR))) {
+               } else if (!cumode && gch == character(value(RAISECHAR))) {
                        boolean(value(RAISE)) = !boolean(value(RAISE));
                        boolean(value(RAISE)) = !boolean(value(RAISE));
-                       printf("%s", ctrl(character(value(RAISECHAR))));
                        continue;
                } else if (gch == '\r') {
                        bol = 1;
                        continue;
                } else if (gch == '\r') {
                        bol = 1;
-                       write(FD, &gch, 1);
+                       pwrite(FD, &gch, 1);
+                       if (boolean(value(HALFDUPLEX)))
+                               printf("\r\n");
                        continue;
                        continue;
-               } else if (gch == character(value(FORCE))) {
-                       printf("%s", ctrl(character(value(FORCE))));
+               } else if (!cumode && gch == character(value(FORCE)))
                        gch = getchar()&0177;
                        gch = getchar()&0177;
-               }
                bol = any(gch, value(EOL));
                if (boolean(value(RAISE)) && islower(gch))
                bol = any(gch, value(EOL));
                if (boolean(value(RAISE)) && islower(gch))
-                       toupper(gch);
-               write(FD, &gch, 1);
+                       gch = toupper(gch);
+               pwrite(FD, &gch, 1);
+               if (boolean(value(HALFDUPLEX)))
+                       printf("%c", gch);
        }
 }
 
        }
 }
 
@@ -273,43 +369,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);
                }
                }
-
-       write(FD, &c, 1);
-       return(gch);
+       /* ESCAPE ESCAPE forces ESCAPE */
+       if (c != gch)
+               pwrite(FD, &c, 1);
+       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;
 {
-       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 *
@@ -335,7 +434,7 @@ interp(s)
                ;
        }
        *p = '\0';
                ;
        }
        *p = '\0';
-       return(buf);
+       return (buf);
 }
 
 char *
 }
 
 char *
@@ -352,7 +451,7 @@ ctrl(c)
                s[0] = c;
                s[1] = '\0';
        }
                s[0] = c;
                s[1] = '\0';
        }
-       return(s);
+       return (s);
 }
 
 /*
 }
 
 /*
@@ -366,10 +465,107 @@ 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),
                        p->e_flags&EXP ? '*': ' ', p->e_help);
        }
 }
                        continue;
                printf("%2s", ctrl(character(value(ESCAPE))));
                printf("%-2s %c   %s\r\n", ctrl(p->e_char),
                        p->e_flags&EXP ? '*': ' ', p->e_help);
        }
 }
+
+/*
+ * Set up the "remote" tty's state
+ */
+ttysetup(speed)
+       int speed;
+{
+       unsigned bits = LDECCTQ;
+
+       arg.sg_ispeed = arg.sg_ospeed = speed;
+       arg.sg_flags = RAW;
+       if (boolean(value(TAND)))
+               arg.sg_flags |= TANDEM;
+       ioctl(FD, TIOCSETP, (char *)&arg);
+       ioctl(FD, TIOCLBIS, (char *)&bits);
+}
+
+/*
+ * Return "simple" name from a file name,
+ * strip leading directories.
+ */
+char *
+sname(s)
+       register char *s;
+{
+       register char *p = s;
+
+       while (*s)
+               if (*s++ == '/')
+                       p = s;
+       return (p);
+}
+
+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
+ * with the right parity and output it.
+ */
+pwrite(fd, buf, n)
+       int fd;
+       char *buf;
+       register int n;
+{
+       register int i;
+       register char *bp;
+       extern int errno;
+
+       bp = buf;
+       for (i = 0; i < n; i++) {
+               *bp = partab[(*bp) & 0177];
+               bp++;
+       }
+       if (write(fd, buf, n) < 0) {
+               if (errno == EIO)
+                       abort("Lost carrier.");
+               /* this is questionable */
+               perror("write");
+       }
+}
+
+/*
+ * Build a parity table with appropriate high-order bit.
+ */
+setparity(defparity)
+       char *defparity;
+{
+       register int i;
+       char *parity;
+       extern char evenpartab[];
+
+       if (value(PARITY) == NOSTR)
+               value(PARITY) = defparity;
+       parity = value(PARITY);
+       for (i = 0; i < 0200; 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 */
+               return;
+       }
+       if (equal(parity, "none") || equal(parity, "zero")) {
+               for (i = 0; i < 0200; i++)
+                       partab[i] &= ~0200;     /* turn off bit 7 */
+               return;
+       }
+       if (equal(parity, "one")) {
+               for (i = 0; i < 0200; i++)
+                       partab[i] |= 0200;      /* turn on bit 7 */
+               return;
+       }
+       fprintf(stderr, "%s: unknown parity value\n", PA);
+       fflush(stderr);
+}