119806Sdist /* 219806Sdist * Copyright (c) 1983 Regents of the University of California. 319806Sdist * All rights reserved. The Berkeley software License Agreement 419806Sdist * specifies the terms and conditions for redistribution. 519806Sdist */ 619806Sdist 713279Ssam #ifndef lint 819806Sdist char copyright[] = 919806Sdist "@(#) Copyright (c) 1983 Regents of the University of California.\n\ 1019806Sdist All rights reserved.\n"; 1119806Sdist #endif not lint 125136Ssam 1319806Sdist #ifndef lint 14*35460Sbostic static char sccsid[] = "@(#)tip.c 5.7 (Berkeley) 09/02/88"; 1519806Sdist #endif not lint 1619806Sdist 173696Sroot /* 185136Ssam * tip - UNIX link to other systems 193696Sroot * tip [-v] [-speed] system-name 205136Ssam * or 215136Ssam * cu phone-number [-s speed] [-l line] [-a acu] 223696Sroot */ 233696Sroot #include "tip.h" 243696Sroot 253696Sroot /* 263696Sroot * Baud rate mapping table 273696Sroot */ 283696Sroot int bauds[] = { 293696Sroot 0, 50, 75, 110, 134, 150, 200, 300, 600, 303696Sroot 1200, 1800, 2400, 4800, 9600, 19200, -1 313696Sroot }; 323696Sroot 334004Ssam int disc = OTTYDISC; /* tip normally runs this way */ 343696Sroot int intprompt(); 353696Sroot int timeout(); 365136Ssam int cleanup(); 375257Sshannon char *sname(); 3825550Sdonn char PNbuf[256]; /* This limits the size of a number */ 393696Sroot 403696Sroot main(argc, argv) 414962Ssam char *argv[]; 423696Sroot { 433696Sroot char *system = NOSTR; 443696Sroot register int i; 455257Sshannon register char *p; 465257Sshannon char sbuf[12]; 473696Sroot 4830458Skarels gid = getgid(); 4930458Skarels egid = getegid(); 5030458Skarels uid = getuid(); 5130458Skarels euid = geteuid(); 525257Sshannon if (equal(sname(argv[0]), "cu")) { 5325905Skarels cumode = 1; 545136Ssam cumain(argc, argv); 555136Ssam goto cucommon; 565136Ssam } 575136Ssam 583696Sroot if (argc > 4) { 593696Sroot fprintf(stderr, "usage: tip [-v] [-speed] [system-name]\n"); 603696Sroot exit(1); 613696Sroot } 623696Sroot if (!isatty(0)) { 633696Sroot fprintf(stderr, "tip: must be interactive\n"); 643696Sroot exit(1); 653696Sroot } 665257Sshannon 675257Sshannon for (; argc > 1; argv++, argc--) { 685257Sshannon if (argv[1][0] != '-') 695257Sshannon system = argv[1]; 705257Sshannon else switch (argv[1][1]) { 715257Sshannon 725257Sshannon case 'v': 735257Sshannon vflag++; 745257Sshannon break; 755257Sshannon 765257Sshannon case '0': case '1': case '2': case '3': case '4': 775257Sshannon case '5': case '6': case '7': case '8': case '9': 785257Sshannon BR = atoi(&argv[1][1]); 795257Sshannon break; 805257Sshannon 815257Sshannon default: 825257Sshannon fprintf(stderr, "tip: %s, unknown option\n", argv[1]); 835257Sshannon break; 845257Sshannon } 855257Sshannon } 865257Sshannon 877593Sshannon if (system == NOSTR) 887593Sshannon goto notnumber; 8927004Sdonn if (isalpha(*system)) 9027004Sdonn goto notnumber; 9125550Sdonn /* 9225550Sdonn * System name is really a phone number... 9325550Sdonn * Copy the number then stomp on the original (in case the number 9425550Sdonn * is private, we don't want 'ps' or 'w' to find it). 9525550Sdonn */ 9625550Sdonn if (strlen(system) > sizeof PNbuf - 1) { 9725550Sdonn fprintf(stderr, "tip: phone number too long (max = %d bytes)\n", 9825550Sdonn sizeof PNbuf - 1); 9925550Sdonn exit(1); 10025550Sdonn } 10125550Sdonn strncpy( PNbuf, system, sizeof PNbuf - 1 ); 10225550Sdonn for (p = system; *p; p++) 10325550Sdonn *p = '\0'; 10425550Sdonn PN = PNbuf; 10532513Sbostic (void)sprintf(sbuf, "tip%d", BR); 10632513Sbostic system = sbuf; 1075257Sshannon 1085257Sshannon notnumber: 1093696Sroot signal(SIGINT, cleanup); 1103696Sroot signal(SIGQUIT, cleanup); 1113696Sroot signal(SIGHUP, cleanup); 1123696Sroot signal(SIGTERM, cleanup); 1133898Ssam 1143696Sroot if ((i = hunt(system)) == 0) { 1153696Sroot printf("all ports busy\n"); 1163696Sroot exit(3); 1173696Sroot } 1183696Sroot if (i == -1) { 1193696Sroot printf("link down\n"); 120*35460Sbostic (void)uu_unlock(uucplock); 1213696Sroot exit(3); 1223696Sroot } 1233719Ssam setbuf(stdout, NULL); 1243696Sroot loginit(); 1255257Sshannon 1263696Sroot /* 1273696Sroot * Kludge, their's no easy way to get the initialization 1283696Sroot * in the right order, so force it here 1293696Sroot */ 1303696Sroot if ((PH = getenv("PHONES")) == NOSTR) 1313696Sroot PH = "/etc/phones"; 1323696Sroot vinit(); /* init variables */ 13313430Ssam setparity("even"); /* set the parity table */ 1344004Ssam if ((i = speed(number(value(BAUDRATE)))) == NULL) { 1353696Sroot printf("tip: bad baud rate %d\n", number(value(BAUDRATE))); 136*35460Sbostic (void)uu_unlock(uucplock); 1373696Sroot exit(3); 1383696Sroot } 1393898Ssam 1404004Ssam /* 14125905Skarels * Now that we have the logfile and the ACU open 14225905Skarels * return to the real uid and gid. These things will 14325905Skarels * be closed on exit. Swap real and effective uid's 14425905Skarels * so we can get the original permissions back 14525905Skarels * for removing the uucp lock. 14625905Skarels */ 14730458Skarels user_uid(); 14825905Skarels 14925905Skarels /* 1504004Ssam * Hardwired connections require the 1514004Ssam * line speed set before they make any transmissions 1524004Ssam * (this is particularly true of things like a DF03-AC) 1534004Ssam */ 1544004Ssam if (HW) 1554004Ssam ttysetup(i); 1563898Ssam if (p = connect()) { 1573898Ssam printf("\07%s\n[EOT]\n", p); 15830458Skarels daemon_uid(); 159*35460Sbostic (void)uu_unlock(uucplock); 1603898Ssam exit(1); 1613898Ssam } 1624004Ssam if (!HW) 1634004Ssam ttysetup(i); 1645136Ssam cucommon: 1653696Sroot /* 1665136Ssam * From here down the code is shared with 1675136Ssam * the "cu" version of tip. 1683696Sroot */ 16925905Skarels 1704004Ssam ioctl(0, TIOCGETP, (char *)&defarg); 1714004Ssam ioctl(0, TIOCGETC, (char *)&defchars); 17212478Sroot ioctl(0, TIOCGLTC, (char *)&deflchars); 1734004Ssam ioctl(0, TIOCGETD, (char *)&odisc); 1743696Sroot arg = defarg; 1753696Sroot arg.sg_flags = ANYP | CBREAK; 1763696Sroot tchars = defchars; 1773696Sroot tchars.t_intrc = tchars.t_quitc = -1; 17812478Sroot ltchars = deflchars; 17912478Sroot ltchars.t_suspc = ltchars.t_dsuspc = ltchars.t_flushc 18012478Sroot = ltchars.t_lnextc = -1; 1813696Sroot raw(); 1824004Ssam 1833696Sroot pipe(fildes); pipe(repdes); 1843696Sroot signal(SIGALRM, timeout); 1853898Ssam 1863898Ssam /* 1873898Ssam * Everything's set up now: 18817236Shelge * connection established (hardwired or dialup) 1893898Ssam * line conditioned (baud rate, mode, etc.) 1903898Ssam * internal data structures (variables) 1913898Ssam * so, fork one process for local side and one for remote. 1923898Ssam */ 1935136Ssam printf(cumode ? "Connected\r\n" : "\07connected\r\n"); 1943696Sroot if (pid = fork()) 1953696Sroot tipin(); 1963696Sroot else 1973696Sroot tipout(); 1983696Sroot /*NOTREACHED*/ 1993696Sroot } 2003696Sroot 2013696Sroot cleanup() 2023696Sroot { 20313279Ssam 20430458Skarels daemon_uid(); 205*35460Sbostic (void)uu_unlock(uucplock); 2063898Ssam if (odisc) 2073898Ssam ioctl(0, TIOCSETD, (char *)&odisc); 2083696Sroot exit(0); 2093696Sroot } 2103696Sroot 2113696Sroot /* 21230458Skarels * Muck with user ID's. We are setuid to the owner of the lock 21330458Skarels * directory when we start. user_uid() reverses real and effective 21430458Skarels * ID's after startup, to run with the user's permissions. 21530458Skarels * daemon_uid() switches back to the privileged uid for unlocking. 21630458Skarels * Finally, to avoid running a shell with the wrong real uid, 21730458Skarels * shell_uid() sets real and effective uid's to the user's real ID. 21830458Skarels */ 21930458Skarels static int uidswapped; 22030458Skarels 22130458Skarels user_uid() 22230458Skarels { 22330458Skarels if (uidswapped == 0) { 22430458Skarels setregid(egid, gid); 22530458Skarels setreuid(euid, uid); 22630458Skarels uidswapped = 1; 22730458Skarels } 22830458Skarels } 22930458Skarels 23030458Skarels daemon_uid() 23130458Skarels { 23230458Skarels 23330458Skarels if (uidswapped) { 23430458Skarels setreuid(uid, euid); 23530458Skarels setregid(gid, egid); 23630458Skarels uidswapped = 0; 23730458Skarels } 23830458Skarels } 23930458Skarels 24030458Skarels shell_uid() 24130458Skarels { 24230458Skarels 24330458Skarels setreuid(uid, uid); 24430458Skarels setregid(gid, gid); 24530458Skarels } 24630458Skarels 24730458Skarels /* 2483696Sroot * put the controlling keyboard into raw mode 2493696Sroot */ 2503696Sroot raw() 2513696Sroot { 25213279Ssam 2533696Sroot ioctl(0, TIOCSETP, &arg); 2543696Sroot ioctl(0, TIOCSETC, &tchars); 25512478Sroot ioctl(0, TIOCSLTC, <chars); 2564004Ssam ioctl(0, TIOCSETD, (char *)&disc); 2573696Sroot } 2583696Sroot 2593696Sroot 2603696Sroot /* 2613696Sroot * return keyboard to normal mode 2623696Sroot */ 2633696Sroot unraw() 2643696Sroot { 26513279Ssam 2664004Ssam ioctl(0, TIOCSETD, (char *)&odisc); 2674004Ssam ioctl(0, TIOCSETP, (char *)&defarg); 2684004Ssam ioctl(0, TIOCSETC, (char *)&defchars); 26912478Sroot ioctl(0, TIOCSLTC, (char *)&deflchars); 2703696Sroot } 2713696Sroot 27213279Ssam static jmp_buf promptbuf; 27313279Ssam 2743696Sroot /* 2753696Sroot * Print string ``s'', then read a string 2763696Sroot * in from the terminal. Handles signals & allows use of 2773696Sroot * normal erase and kill characters. 2783696Sroot */ 2793696Sroot prompt(s, p) 2803696Sroot char *s; 2813696Sroot register char *p; 2823696Sroot { 2833696Sroot register char *b = p; 28413279Ssam int (*oint)(), (*oquit)(); 2853696Sroot 2863696Sroot stoprompt = 0; 28713279Ssam oint = signal(SIGINT, intprompt); 28813279Ssam oint = signal(SIGQUIT, SIG_IGN); 2893696Sroot unraw(); 2903696Sroot printf("%s", s); 29113279Ssam if (setjmp(promptbuf) == 0) 29213279Ssam while ((*p = getchar()) != EOF && *p != '\n') 29313279Ssam p++; 2943696Sroot *p = '\0'; 29513279Ssam 2963696Sroot raw(); 29713279Ssam signal(SIGINT, oint); 29813279Ssam signal(SIGQUIT, oint); 29913279Ssam return (stoprompt || p == b); 3003696Sroot } 3013696Sroot 3023696Sroot /* 3033696Sroot * Interrupt service routine during prompting 3043696Sroot */ 3053696Sroot intprompt() 3063696Sroot { 30713279Ssam 3083696Sroot signal(SIGINT, SIG_IGN); 3093696Sroot stoprompt = 1; 3103696Sroot printf("\r\n"); 31113279Ssam longjmp(promptbuf, 1); 3123696Sroot } 3133696Sroot 3143696Sroot /* 3153696Sroot * ****TIPIN TIPIN**** 3163696Sroot */ 3173696Sroot tipin() 3183696Sroot { 3193696Sroot char gch, bol = 1; 3203696Sroot 3213796Ssam /* 3223796Ssam * Kinda klugey here... 3233796Ssam * check for scripting being turned on from the .tiprc file, 3243796Ssam * but be careful about just using setscript(), as we may 3253796Ssam * send a SIGEMT before tipout has a chance to set up catching 3263796Ssam * it; so wait a second, then setscript() 3273796Ssam */ 3283796Ssam if (boolean(value(SCRIPT))) { 3293796Ssam sleep(1); 3303796Ssam setscript(); 3313796Ssam } 3323796Ssam 3333696Sroot while (1) { 3343696Sroot gch = getchar()&0177; 3353696Sroot if ((gch == character(value(ESCAPE))) && bol) { 3363696Sroot if (!(gch = escape())) 3373696Sroot continue; 3385136Ssam } else if (!cumode && gch == character(value(RAISECHAR))) { 3393696Sroot boolean(value(RAISE)) = !boolean(value(RAISE)); 3403696Sroot continue; 3413696Sroot } else if (gch == '\r') { 3423696Sroot bol = 1; 34313139Sralph pwrite(FD, &gch, 1); 34413139Sralph if (boolean(value(HALFDUPLEX))) 34513139Sralph printf("\r\n"); 3463696Sroot continue; 3475136Ssam } else if (!cumode && gch == character(value(FORCE))) 3483696Sroot gch = getchar()&0177; 3493696Sroot bol = any(gch, value(EOL)); 3503696Sroot if (boolean(value(RAISE)) && islower(gch)) 35113139Sralph gch = toupper(gch); 35213139Sralph pwrite(FD, &gch, 1); 35313139Sralph if (boolean(value(HALFDUPLEX))) 35413139Sralph printf("%c", gch); 3553696Sroot } 3563696Sroot } 3573696Sroot 3583696Sroot /* 3593696Sroot * Escape handler -- 3603696Sroot * called on recognition of ``escapec'' at the beginning of a line 3613696Sroot */ 3623696Sroot escape() 3633696Sroot { 3643696Sroot register char gch; 3653696Sroot register esctable_t *p; 3663696Sroot char c = character(value(ESCAPE)); 3673696Sroot extern esctable_t etable[]; 3683696Sroot 3693696Sroot gch = (getchar()&0177); 3703696Sroot for (p = etable; p->e_char; p++) 3713696Sroot if (p->e_char == gch) { 37230458Skarels if ((p->e_flags&PRIV) && uid) 3733696Sroot continue; 3743696Sroot printf("%s", ctrl(c)); 3753696Sroot (*p->e_func)(gch); 37613279Ssam return (0); 3773696Sroot } 3784146Ssam /* ESCAPE ESCAPE forces ESCAPE */ 3794146Ssam if (c != gch) 38013139Sralph pwrite(FD, &c, 1); 38113279Ssam return (gch); 3823696Sroot } 3833696Sroot 3843696Sroot speed(n) 38513279Ssam int n; 3863696Sroot { 3873696Sroot register int *p; 3883696Sroot 3893696Sroot for (p = bauds; *p != -1; p++) 3903696Sroot if (*p == n) 39113279Ssam return (p - bauds); 39213279Ssam return (NULL); 3933696Sroot } 3943696Sroot 3953696Sroot any(c, p) 3963696Sroot register char c, *p; 3973696Sroot { 39813279Ssam while (p && *p) 3993696Sroot if (*p++ == c) 40013279Ssam return (1); 40113279Ssam return (0); 4023696Sroot } 4033696Sroot 4043696Sroot size(s) 4053696Sroot register char *s; 4063696Sroot { 40713279Ssam register int i = 0; 4083696Sroot 40913279Ssam while (s && *s++) 41013279Ssam i++; 41113279Ssam return (i); 4123696Sroot } 4133696Sroot 4143696Sroot char * 4153696Sroot interp(s) 4163696Sroot register char *s; 4173696Sroot { 4183696Sroot static char buf[256]; 4193696Sroot register char *p = buf, c, *q; 4203696Sroot 4213696Sroot while (c = *s++) { 4223696Sroot for (q = "\nn\rr\tt\ff\033E\bb"; *q; q++) 4233696Sroot if (*q++ == c) { 4243696Sroot *p++ = '\\'; *p++ = *q; 4253696Sroot goto next; 4263696Sroot } 4273696Sroot if (c < 040) { 4283696Sroot *p++ = '^'; *p++ = c + 'A'-1; 4293696Sroot } else if (c == 0177) { 4303696Sroot *p++ = '^'; *p++ = '?'; 4313696Sroot } else 4323696Sroot *p++ = c; 4333696Sroot next: 4343696Sroot ; 4353696Sroot } 4363696Sroot *p = '\0'; 43713279Ssam return (buf); 4383696Sroot } 4393696Sroot 4403696Sroot char * 4413696Sroot ctrl(c) 4423696Sroot char c; 4433696Sroot { 4443696Sroot static char s[3]; 4453696Sroot 4463696Sroot if (c < 040 || c == 0177) { 4473696Sroot s[0] = '^'; 4483696Sroot s[1] = c == 0177 ? '?' : c+'A'-1; 4493696Sroot s[2] = '\0'; 4503696Sroot } else { 4513696Sroot s[0] = c; 4523696Sroot s[1] = '\0'; 4533696Sroot } 45413279Ssam return (s); 4553696Sroot } 4563696Sroot 4573696Sroot /* 4583696Sroot * Help command 4593696Sroot */ 4603696Sroot help(c) 4613696Sroot char c; 4623696Sroot { 4633696Sroot register esctable_t *p; 4643696Sroot extern esctable_t etable[]; 4653696Sroot 4663696Sroot printf("%c\r\n", c); 4673696Sroot for (p = etable; p->e_char; p++) { 46830458Skarels if ((p->e_flags&PRIV) && uid) 4693696Sroot continue; 4703696Sroot printf("%2s", ctrl(character(value(ESCAPE)))); 4713696Sroot printf("%-2s %c %s\r\n", ctrl(p->e_char), 4723696Sroot p->e_flags&EXP ? '*': ' ', p->e_help); 4733696Sroot } 4743696Sroot } 4754004Ssam 4764004Ssam /* 4774004Ssam * Set up the "remote" tty's state 4784004Ssam */ 4794004Ssam ttysetup(speed) 48013279Ssam int speed; 4814004Ssam { 4824004Ssam unsigned bits = LDECCTQ; 4834004Ssam 4844004Ssam arg.sg_ispeed = arg.sg_ospeed = speed; 48513279Ssam arg.sg_flags = RAW; 48613139Sralph if (boolean(value(TAND))) 48713279Ssam arg.sg_flags |= TANDEM; 4884004Ssam ioctl(FD, TIOCSETP, (char *)&arg); 4894004Ssam ioctl(FD, TIOCLBIS, (char *)&bits); 4904004Ssam } 4915257Sshannon 4925257Sshannon /* 4935257Sshannon * Return "simple" name from a file name, 4945257Sshannon * strip leading directories. 4955257Sshannon */ 4965257Sshannon char * 4975257Sshannon sname(s) 4985257Sshannon register char *s; 4995257Sshannon { 5005257Sshannon register char *p = s; 5015257Sshannon 5025257Sshannon while (*s) 5035257Sshannon if (*s++ == '/') 5045257Sshannon p = s; 5055257Sshannon return (p); 5065257Sshannon } 50713139Sralph 50813139Sralph static char partab[0200]; 50913139Sralph 51013139Sralph /* 51113279Ssam * Do a write to the remote machine with the correct parity. 51213279Ssam * We are doing 8 bit wide output, so we just generate a character 51313139Sralph * with the right parity and output it. 51413139Sralph */ 51513139Sralph pwrite(fd, buf, n) 51613139Sralph int fd; 51713139Sralph char *buf; 51813139Sralph register int n; 51913139Sralph { 52013139Sralph register int i; 52113279Ssam register char *bp; 52215188Ssam extern int errno; 52313139Sralph 52413279Ssam bp = buf; 52513279Ssam for (i = 0; i < n; i++) { 52613139Sralph *bp = partab[(*bp) & 0177]; 52713279Ssam bp++; 52813139Sralph } 52915188Ssam if (write(fd, buf, n) < 0) { 53015188Ssam if (errno == EIO) 53115188Ssam abort("Lost carrier."); 53215188Ssam /* this is questionable */ 53315188Ssam perror("write"); 53415188Ssam } 53513139Sralph } 53613139Sralph 53713139Sralph /* 53813279Ssam * Build a parity table with appropriate high-order bit. 53913139Sralph */ 54013430Ssam setparity(defparity) 54113430Ssam char *defparity; 54213139Sralph { 54313279Ssam register int i; 54413139Sralph char *parity; 54513279Ssam extern char evenpartab[]; 54613139Sralph 54713139Sralph if (value(PARITY) == NOSTR) 54813430Ssam value(PARITY) = defparity; 54913139Sralph parity = value(PARITY); 55013139Sralph for (i = 0; i < 0200; i++) 55113279Ssam partab[i] = evenpartab[i]; 55213279Ssam if (equal(parity, "even")) 55313279Ssam return; 55413139Sralph if (equal(parity, "odd")) { 55513139Sralph for (i = 0; i < 0200; i++) 55613139Sralph partab[i] ^= 0200; /* reverse bit 7 */ 55713279Ssam return; 55813139Sralph } 55913279Ssam if (equal(parity, "none") || equal(parity, "zero")) { 56013139Sralph for (i = 0; i < 0200; i++) 56113139Sralph partab[i] &= ~0200; /* turn off bit 7 */ 56213279Ssam return; 56313139Sralph } 56413279Ssam if (equal(parity, "one")) { 56513139Sralph for (i = 0; i < 0200; i++) 56613139Sralph partab[i] |= 0200; /* turn on bit 7 */ 56713279Ssam return; 56813139Sralph } 56913279Ssam fprintf(stderr, "%s: unknown parity value\n", PA); 57013279Ssam fflush(stderr); 57113139Sralph } 572