119806Sdist /* 235464Sbostic * Copyright (c) 1983 The Regents of the University of California. 335464Sbostic * All rights reserved. 435464Sbostic * 535464Sbostic * Redistribution and use in source and binary forms are permitted 635464Sbostic * provided that the above copyright notice and this paragraph are 735464Sbostic * duplicated in all such forms and that any documentation, 835464Sbostic * advertising materials, and other materials related to such 935464Sbostic * distribution and use acknowledge that the software was developed 1035464Sbostic * by the University of California, Berkeley. The name of the 1135464Sbostic * University may not be used to endorse or promote products derived 1235464Sbostic * from this software without specific prior written permission. 1335464Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1435464Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1535464Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1619806Sdist */ 1719806Sdist 1813279Ssam #ifndef lint 1919806Sdist char copyright[] = 2035464Sbostic "@(#) Copyright (c) 1983 The Regents of the University of California.\n\ 2119806Sdist All rights reserved.\n"; 2235464Sbostic #endif /* not lint */ 235136Ssam 2419806Sdist #ifndef lint 25*37857Sbostic static char sccsid[] = "@(#)tip.c 5.9 (Berkeley) 05/11/89"; 2635464Sbostic #endif /* not lint */ 2719806Sdist 283696Sroot /* 295136Ssam * tip - UNIX link to other systems 303696Sroot * tip [-v] [-speed] system-name 315136Ssam * or 325136Ssam * cu phone-number [-s speed] [-l line] [-a acu] 333696Sroot */ 343696Sroot #include "tip.h" 35*37857Sbostic #include "pathnames.h" 363696Sroot 373696Sroot /* 383696Sroot * Baud rate mapping table 393696Sroot */ 403696Sroot int bauds[] = { 413696Sroot 0, 50, 75, 110, 134, 150, 200, 300, 600, 423696Sroot 1200, 1800, 2400, 4800, 9600, 19200, -1 433696Sroot }; 443696Sroot 454004Ssam int disc = OTTYDISC; /* tip normally runs this way */ 463696Sroot int intprompt(); 473696Sroot int timeout(); 485136Ssam int cleanup(); 495257Sshannon char *sname(); 5025550Sdonn char PNbuf[256]; /* This limits the size of a number */ 513696Sroot 523696Sroot main(argc, argv) 534962Ssam char *argv[]; 543696Sroot { 553696Sroot char *system = NOSTR; 563696Sroot register int i; 575257Sshannon register char *p; 585257Sshannon char sbuf[12]; 593696Sroot 6030458Skarels gid = getgid(); 6130458Skarels egid = getegid(); 6230458Skarels uid = getuid(); 6330458Skarels euid = geteuid(); 645257Sshannon if (equal(sname(argv[0]), "cu")) { 6525905Skarels cumode = 1; 665136Ssam cumain(argc, argv); 675136Ssam goto cucommon; 685136Ssam } 695136Ssam 703696Sroot if (argc > 4) { 713696Sroot fprintf(stderr, "usage: tip [-v] [-speed] [system-name]\n"); 723696Sroot exit(1); 733696Sroot } 743696Sroot if (!isatty(0)) { 753696Sroot fprintf(stderr, "tip: must be interactive\n"); 763696Sroot exit(1); 773696Sroot } 785257Sshannon 795257Sshannon for (; argc > 1; argv++, argc--) { 805257Sshannon if (argv[1][0] != '-') 815257Sshannon system = argv[1]; 825257Sshannon else switch (argv[1][1]) { 835257Sshannon 845257Sshannon case 'v': 855257Sshannon vflag++; 865257Sshannon break; 875257Sshannon 885257Sshannon case '0': case '1': case '2': case '3': case '4': 895257Sshannon case '5': case '6': case '7': case '8': case '9': 905257Sshannon BR = atoi(&argv[1][1]); 915257Sshannon break; 925257Sshannon 935257Sshannon default: 945257Sshannon fprintf(stderr, "tip: %s, unknown option\n", argv[1]); 955257Sshannon break; 965257Sshannon } 975257Sshannon } 985257Sshannon 997593Sshannon if (system == NOSTR) 1007593Sshannon goto notnumber; 10127004Sdonn if (isalpha(*system)) 10227004Sdonn goto notnumber; 10325550Sdonn /* 10425550Sdonn * System name is really a phone number... 10525550Sdonn * Copy the number then stomp on the original (in case the number 10625550Sdonn * is private, we don't want 'ps' or 'w' to find it). 10725550Sdonn */ 10825550Sdonn if (strlen(system) > sizeof PNbuf - 1) { 10925550Sdonn fprintf(stderr, "tip: phone number too long (max = %d bytes)\n", 11025550Sdonn sizeof PNbuf - 1); 11125550Sdonn exit(1); 11225550Sdonn } 11325550Sdonn strncpy( PNbuf, system, sizeof PNbuf - 1 ); 11425550Sdonn for (p = system; *p; p++) 11525550Sdonn *p = '\0'; 11625550Sdonn PN = PNbuf; 11732513Sbostic (void)sprintf(sbuf, "tip%d", BR); 11832513Sbostic system = sbuf; 1195257Sshannon 1205257Sshannon notnumber: 1213696Sroot signal(SIGINT, cleanup); 1223696Sroot signal(SIGQUIT, cleanup); 1233696Sroot signal(SIGHUP, cleanup); 1243696Sroot signal(SIGTERM, cleanup); 1253898Ssam 1263696Sroot if ((i = hunt(system)) == 0) { 1273696Sroot printf("all ports busy\n"); 1283696Sroot exit(3); 1293696Sroot } 1303696Sroot if (i == -1) { 1313696Sroot printf("link down\n"); 13235460Sbostic (void)uu_unlock(uucplock); 1333696Sroot exit(3); 1343696Sroot } 1353719Ssam setbuf(stdout, NULL); 1363696Sroot loginit(); 1375257Sshannon 1383696Sroot /* 1393696Sroot * Kludge, their's no easy way to get the initialization 1403696Sroot * in the right order, so force it here 1413696Sroot */ 1423696Sroot if ((PH = getenv("PHONES")) == NOSTR) 143*37857Sbostic PH = _PATH_PHONES; 1443696Sroot vinit(); /* init variables */ 14513430Ssam setparity("even"); /* set the parity table */ 1464004Ssam if ((i = speed(number(value(BAUDRATE)))) == NULL) { 1473696Sroot printf("tip: bad baud rate %d\n", number(value(BAUDRATE))); 14835460Sbostic (void)uu_unlock(uucplock); 1493696Sroot exit(3); 1503696Sroot } 1513898Ssam 1524004Ssam /* 15325905Skarels * Now that we have the logfile and the ACU open 15425905Skarels * return to the real uid and gid. These things will 15525905Skarels * be closed on exit. Swap real and effective uid's 15625905Skarels * so we can get the original permissions back 15725905Skarels * for removing the uucp lock. 15825905Skarels */ 15930458Skarels user_uid(); 16025905Skarels 16125905Skarels /* 1624004Ssam * Hardwired connections require the 1634004Ssam * line speed set before they make any transmissions 1644004Ssam * (this is particularly true of things like a DF03-AC) 1654004Ssam */ 1664004Ssam if (HW) 1674004Ssam ttysetup(i); 1683898Ssam if (p = connect()) { 1693898Ssam printf("\07%s\n[EOT]\n", p); 17030458Skarels daemon_uid(); 17135460Sbostic (void)uu_unlock(uucplock); 1723898Ssam exit(1); 1733898Ssam } 1744004Ssam if (!HW) 1754004Ssam ttysetup(i); 1765136Ssam cucommon: 1773696Sroot /* 1785136Ssam * From here down the code is shared with 1795136Ssam * the "cu" version of tip. 1803696Sroot */ 18125905Skarels 1824004Ssam ioctl(0, TIOCGETP, (char *)&defarg); 1834004Ssam ioctl(0, TIOCGETC, (char *)&defchars); 18412478Sroot ioctl(0, TIOCGLTC, (char *)&deflchars); 1854004Ssam ioctl(0, TIOCGETD, (char *)&odisc); 1863696Sroot arg = defarg; 1873696Sroot arg.sg_flags = ANYP | CBREAK; 1883696Sroot tchars = defchars; 1893696Sroot tchars.t_intrc = tchars.t_quitc = -1; 19012478Sroot ltchars = deflchars; 19112478Sroot ltchars.t_suspc = ltchars.t_dsuspc = ltchars.t_flushc 19212478Sroot = ltchars.t_lnextc = -1; 1933696Sroot raw(); 1944004Ssam 1953696Sroot pipe(fildes); pipe(repdes); 1963696Sroot signal(SIGALRM, timeout); 1973898Ssam 1983898Ssam /* 1993898Ssam * Everything's set up now: 20017236Shelge * connection established (hardwired or dialup) 2013898Ssam * line conditioned (baud rate, mode, etc.) 2023898Ssam * internal data structures (variables) 2033898Ssam * so, fork one process for local side and one for remote. 2043898Ssam */ 2055136Ssam printf(cumode ? "Connected\r\n" : "\07connected\r\n"); 2063696Sroot if (pid = fork()) 2073696Sroot tipin(); 2083696Sroot else 2093696Sroot tipout(); 2103696Sroot /*NOTREACHED*/ 2113696Sroot } 2123696Sroot 2133696Sroot cleanup() 2143696Sroot { 21513279Ssam 21630458Skarels daemon_uid(); 21735460Sbostic (void)uu_unlock(uucplock); 2183898Ssam if (odisc) 2193898Ssam ioctl(0, TIOCSETD, (char *)&odisc); 2203696Sroot exit(0); 2213696Sroot } 2223696Sroot 2233696Sroot /* 22430458Skarels * Muck with user ID's. We are setuid to the owner of the lock 22530458Skarels * directory when we start. user_uid() reverses real and effective 22630458Skarels * ID's after startup, to run with the user's permissions. 22730458Skarels * daemon_uid() switches back to the privileged uid for unlocking. 22830458Skarels * Finally, to avoid running a shell with the wrong real uid, 22930458Skarels * shell_uid() sets real and effective uid's to the user's real ID. 23030458Skarels */ 23130458Skarels static int uidswapped; 23230458Skarels 23330458Skarels user_uid() 23430458Skarels { 23530458Skarels if (uidswapped == 0) { 23630458Skarels setregid(egid, gid); 23730458Skarels setreuid(euid, uid); 23830458Skarels uidswapped = 1; 23930458Skarels } 24030458Skarels } 24130458Skarels 24230458Skarels daemon_uid() 24330458Skarels { 24430458Skarels 24530458Skarels if (uidswapped) { 24630458Skarels setreuid(uid, euid); 24730458Skarels setregid(gid, egid); 24830458Skarels uidswapped = 0; 24930458Skarels } 25030458Skarels } 25130458Skarels 25230458Skarels shell_uid() 25330458Skarels { 25430458Skarels 25530458Skarels setreuid(uid, uid); 25630458Skarels setregid(gid, gid); 25730458Skarels } 25830458Skarels 25930458Skarels /* 2603696Sroot * put the controlling keyboard into raw mode 2613696Sroot */ 2623696Sroot raw() 2633696Sroot { 26413279Ssam 2653696Sroot ioctl(0, TIOCSETP, &arg); 2663696Sroot ioctl(0, TIOCSETC, &tchars); 26712478Sroot ioctl(0, TIOCSLTC, <chars); 2684004Ssam ioctl(0, TIOCSETD, (char *)&disc); 2693696Sroot } 2703696Sroot 2713696Sroot 2723696Sroot /* 2733696Sroot * return keyboard to normal mode 2743696Sroot */ 2753696Sroot unraw() 2763696Sroot { 27713279Ssam 2784004Ssam ioctl(0, TIOCSETD, (char *)&odisc); 2794004Ssam ioctl(0, TIOCSETP, (char *)&defarg); 2804004Ssam ioctl(0, TIOCSETC, (char *)&defchars); 28112478Sroot ioctl(0, TIOCSLTC, (char *)&deflchars); 2823696Sroot } 2833696Sroot 28413279Ssam static jmp_buf promptbuf; 28513279Ssam 2863696Sroot /* 2873696Sroot * Print string ``s'', then read a string 2883696Sroot * in from the terminal. Handles signals & allows use of 2893696Sroot * normal erase and kill characters. 2903696Sroot */ 2913696Sroot prompt(s, p) 2923696Sroot char *s; 2933696Sroot register char *p; 2943696Sroot { 2953696Sroot register char *b = p; 29613279Ssam int (*oint)(), (*oquit)(); 2973696Sroot 2983696Sroot stoprompt = 0; 29913279Ssam oint = signal(SIGINT, intprompt); 30013279Ssam oint = signal(SIGQUIT, SIG_IGN); 3013696Sroot unraw(); 3023696Sroot printf("%s", s); 30313279Ssam if (setjmp(promptbuf) == 0) 30413279Ssam while ((*p = getchar()) != EOF && *p != '\n') 30513279Ssam p++; 3063696Sroot *p = '\0'; 30713279Ssam 3083696Sroot raw(); 30913279Ssam signal(SIGINT, oint); 31013279Ssam signal(SIGQUIT, oint); 31113279Ssam return (stoprompt || p == b); 3123696Sroot } 3133696Sroot 3143696Sroot /* 3153696Sroot * Interrupt service routine during prompting 3163696Sroot */ 3173696Sroot intprompt() 3183696Sroot { 31913279Ssam 3203696Sroot signal(SIGINT, SIG_IGN); 3213696Sroot stoprompt = 1; 3223696Sroot printf("\r\n"); 32313279Ssam longjmp(promptbuf, 1); 3243696Sroot } 3253696Sroot 3263696Sroot /* 3273696Sroot * ****TIPIN TIPIN**** 3283696Sroot */ 3293696Sroot tipin() 3303696Sroot { 3313696Sroot char gch, bol = 1; 3323696Sroot 3333796Ssam /* 3343796Ssam * Kinda klugey here... 3353796Ssam * check for scripting being turned on from the .tiprc file, 3363796Ssam * but be careful about just using setscript(), as we may 3373796Ssam * send a SIGEMT before tipout has a chance to set up catching 3383796Ssam * it; so wait a second, then setscript() 3393796Ssam */ 3403796Ssam if (boolean(value(SCRIPT))) { 3413796Ssam sleep(1); 3423796Ssam setscript(); 3433796Ssam } 3443796Ssam 3453696Sroot while (1) { 3463696Sroot gch = getchar()&0177; 3473696Sroot if ((gch == character(value(ESCAPE))) && bol) { 3483696Sroot if (!(gch = escape())) 3493696Sroot continue; 3505136Ssam } else if (!cumode && gch == character(value(RAISECHAR))) { 3513696Sroot boolean(value(RAISE)) = !boolean(value(RAISE)); 3523696Sroot continue; 3533696Sroot } else if (gch == '\r') { 3543696Sroot bol = 1; 35513139Sralph pwrite(FD, &gch, 1); 35613139Sralph if (boolean(value(HALFDUPLEX))) 35713139Sralph printf("\r\n"); 3583696Sroot continue; 3595136Ssam } else if (!cumode && gch == character(value(FORCE))) 3603696Sroot gch = getchar()&0177; 3613696Sroot bol = any(gch, value(EOL)); 3623696Sroot if (boolean(value(RAISE)) && islower(gch)) 36313139Sralph gch = toupper(gch); 36413139Sralph pwrite(FD, &gch, 1); 36513139Sralph if (boolean(value(HALFDUPLEX))) 36613139Sralph printf("%c", gch); 3673696Sroot } 3683696Sroot } 3693696Sroot 3703696Sroot /* 3713696Sroot * Escape handler -- 3723696Sroot * called on recognition of ``escapec'' at the beginning of a line 3733696Sroot */ 3743696Sroot escape() 3753696Sroot { 3763696Sroot register char gch; 3773696Sroot register esctable_t *p; 3783696Sroot char c = character(value(ESCAPE)); 3793696Sroot extern esctable_t etable[]; 3803696Sroot 3813696Sroot gch = (getchar()&0177); 3823696Sroot for (p = etable; p->e_char; p++) 3833696Sroot if (p->e_char == gch) { 38430458Skarels if ((p->e_flags&PRIV) && uid) 3853696Sroot continue; 3863696Sroot printf("%s", ctrl(c)); 3873696Sroot (*p->e_func)(gch); 38813279Ssam return (0); 3893696Sroot } 3904146Ssam /* ESCAPE ESCAPE forces ESCAPE */ 3914146Ssam if (c != gch) 39213139Sralph pwrite(FD, &c, 1); 39313279Ssam return (gch); 3943696Sroot } 3953696Sroot 3963696Sroot speed(n) 39713279Ssam int n; 3983696Sroot { 3993696Sroot register int *p; 4003696Sroot 4013696Sroot for (p = bauds; *p != -1; p++) 4023696Sroot if (*p == n) 40313279Ssam return (p - bauds); 40413279Ssam return (NULL); 4053696Sroot } 4063696Sroot 4073696Sroot any(c, p) 4083696Sroot register char c, *p; 4093696Sroot { 41013279Ssam while (p && *p) 4113696Sroot if (*p++ == c) 41213279Ssam return (1); 41313279Ssam return (0); 4143696Sroot } 4153696Sroot 4163696Sroot size(s) 4173696Sroot register char *s; 4183696Sroot { 41913279Ssam register int i = 0; 4203696Sroot 42113279Ssam while (s && *s++) 42213279Ssam i++; 42313279Ssam return (i); 4243696Sroot } 4253696Sroot 4263696Sroot char * 4273696Sroot interp(s) 4283696Sroot register char *s; 4293696Sroot { 4303696Sroot static char buf[256]; 4313696Sroot register char *p = buf, c, *q; 4323696Sroot 4333696Sroot while (c = *s++) { 4343696Sroot for (q = "\nn\rr\tt\ff\033E\bb"; *q; q++) 4353696Sroot if (*q++ == c) { 4363696Sroot *p++ = '\\'; *p++ = *q; 4373696Sroot goto next; 4383696Sroot } 4393696Sroot if (c < 040) { 4403696Sroot *p++ = '^'; *p++ = c + 'A'-1; 4413696Sroot } else if (c == 0177) { 4423696Sroot *p++ = '^'; *p++ = '?'; 4433696Sroot } else 4443696Sroot *p++ = c; 4453696Sroot next: 4463696Sroot ; 4473696Sroot } 4483696Sroot *p = '\0'; 44913279Ssam return (buf); 4503696Sroot } 4513696Sroot 4523696Sroot char * 4533696Sroot ctrl(c) 4543696Sroot char c; 4553696Sroot { 4563696Sroot static char s[3]; 4573696Sroot 4583696Sroot if (c < 040 || c == 0177) { 4593696Sroot s[0] = '^'; 4603696Sroot s[1] = c == 0177 ? '?' : c+'A'-1; 4613696Sroot s[2] = '\0'; 4623696Sroot } else { 4633696Sroot s[0] = c; 4643696Sroot s[1] = '\0'; 4653696Sroot } 46613279Ssam return (s); 4673696Sroot } 4683696Sroot 4693696Sroot /* 4703696Sroot * Help command 4713696Sroot */ 4723696Sroot help(c) 4733696Sroot char c; 4743696Sroot { 4753696Sroot register esctable_t *p; 4763696Sroot extern esctable_t etable[]; 4773696Sroot 4783696Sroot printf("%c\r\n", c); 4793696Sroot for (p = etable; p->e_char; p++) { 48030458Skarels if ((p->e_flags&PRIV) && uid) 4813696Sroot continue; 4823696Sroot printf("%2s", ctrl(character(value(ESCAPE)))); 4833696Sroot printf("%-2s %c %s\r\n", ctrl(p->e_char), 4843696Sroot p->e_flags&EXP ? '*': ' ', p->e_help); 4853696Sroot } 4863696Sroot } 4874004Ssam 4884004Ssam /* 4894004Ssam * Set up the "remote" tty's state 4904004Ssam */ 4914004Ssam ttysetup(speed) 49213279Ssam int speed; 4934004Ssam { 4944004Ssam unsigned bits = LDECCTQ; 4954004Ssam 4964004Ssam arg.sg_ispeed = arg.sg_ospeed = speed; 49713279Ssam arg.sg_flags = RAW; 49813139Sralph if (boolean(value(TAND))) 49913279Ssam arg.sg_flags |= TANDEM; 5004004Ssam ioctl(FD, TIOCSETP, (char *)&arg); 5014004Ssam ioctl(FD, TIOCLBIS, (char *)&bits); 5024004Ssam } 5035257Sshannon 5045257Sshannon /* 5055257Sshannon * Return "simple" name from a file name, 5065257Sshannon * strip leading directories. 5075257Sshannon */ 5085257Sshannon char * 5095257Sshannon sname(s) 5105257Sshannon register char *s; 5115257Sshannon { 5125257Sshannon register char *p = s; 5135257Sshannon 5145257Sshannon while (*s) 5155257Sshannon if (*s++ == '/') 5165257Sshannon p = s; 5175257Sshannon return (p); 5185257Sshannon } 51913139Sralph 52013139Sralph static char partab[0200]; 52113139Sralph 52213139Sralph /* 52313279Ssam * Do a write to the remote machine with the correct parity. 52413279Ssam * We are doing 8 bit wide output, so we just generate a character 52513139Sralph * with the right parity and output it. 52613139Sralph */ 52713139Sralph pwrite(fd, buf, n) 52813139Sralph int fd; 52913139Sralph char *buf; 53013139Sralph register int n; 53113139Sralph { 53213139Sralph register int i; 53313279Ssam register char *bp; 53415188Ssam extern int errno; 53513139Sralph 53613279Ssam bp = buf; 53713279Ssam for (i = 0; i < n; i++) { 53813139Sralph *bp = partab[(*bp) & 0177]; 53913279Ssam bp++; 54013139Sralph } 54115188Ssam if (write(fd, buf, n) < 0) { 54215188Ssam if (errno == EIO) 54315188Ssam abort("Lost carrier."); 54415188Ssam /* this is questionable */ 54515188Ssam perror("write"); 54615188Ssam } 54713139Sralph } 54813139Sralph 54913139Sralph /* 55013279Ssam * Build a parity table with appropriate high-order bit. 55113139Sralph */ 55213430Ssam setparity(defparity) 55313430Ssam char *defparity; 55413139Sralph { 55513279Ssam register int i; 55613139Sralph char *parity; 55713279Ssam extern char evenpartab[]; 55813139Sralph 55913139Sralph if (value(PARITY) == NOSTR) 56013430Ssam value(PARITY) = defparity; 56113139Sralph parity = value(PARITY); 56213139Sralph for (i = 0; i < 0200; i++) 56313279Ssam partab[i] = evenpartab[i]; 56413279Ssam if (equal(parity, "even")) 56513279Ssam return; 56613139Sralph if (equal(parity, "odd")) { 56713139Sralph for (i = 0; i < 0200; i++) 56813139Sralph partab[i] ^= 0200; /* reverse bit 7 */ 56913279Ssam return; 57013139Sralph } 57113279Ssam if (equal(parity, "none") || equal(parity, "zero")) { 57213139Sralph for (i = 0; i < 0200; i++) 57313139Sralph partab[i] &= ~0200; /* turn off bit 7 */ 57413279Ssam return; 57513139Sralph } 57613279Ssam if (equal(parity, "one")) { 57713139Sralph for (i = 0; i < 0200; i++) 57813139Sralph partab[i] |= 0200; /* turn on bit 7 */ 57913279Ssam return; 58013139Sralph } 58113279Ssam fprintf(stderr, "%s: unknown parity value\n", PA); 58213279Ssam fflush(stderr); 58313139Sralph } 584