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*27004Sdonn static char sccsid[] = "@(#)tip.c 5.4 (Berkeley) 04/03/86"; 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 */ 395257Sshannon extern char *sprintf(); 403696Sroot 413696Sroot main(argc, argv) 424962Ssam char *argv[]; 433696Sroot { 443696Sroot char *system = NOSTR; 453696Sroot register int i; 465257Sshannon register char *p; 475257Sshannon char sbuf[12]; 483696Sroot 495257Sshannon if (equal(sname(argv[0]), "cu")) { 5025905Skarels cumode = 1; 515136Ssam cumain(argc, argv); 525136Ssam goto cucommon; 535136Ssam } 545136Ssam 553696Sroot if (argc > 4) { 563696Sroot fprintf(stderr, "usage: tip [-v] [-speed] [system-name]\n"); 573696Sroot exit(1); 583696Sroot } 593696Sroot if (!isatty(0)) { 603696Sroot fprintf(stderr, "tip: must be interactive\n"); 613696Sroot exit(1); 623696Sroot } 635257Sshannon 645257Sshannon for (; argc > 1; argv++, argc--) { 655257Sshannon if (argv[1][0] != '-') 665257Sshannon system = argv[1]; 675257Sshannon else switch (argv[1][1]) { 685257Sshannon 695257Sshannon case 'v': 705257Sshannon vflag++; 715257Sshannon break; 725257Sshannon 735257Sshannon case '0': case '1': case '2': case '3': case '4': 745257Sshannon case '5': case '6': case '7': case '8': case '9': 755257Sshannon BR = atoi(&argv[1][1]); 765257Sshannon break; 775257Sshannon 785257Sshannon default: 795257Sshannon fprintf(stderr, "tip: %s, unknown option\n", argv[1]); 805257Sshannon break; 815257Sshannon } 825257Sshannon } 835257Sshannon 847593Sshannon if (system == NOSTR) 857593Sshannon goto notnumber; 86*27004Sdonn if (isalpha(*system)) 87*27004Sdonn goto notnumber; 8825550Sdonn /* 8925550Sdonn * System name is really a phone number... 9025550Sdonn * Copy the number then stomp on the original (in case the number 9125550Sdonn * is private, we don't want 'ps' or 'w' to find it). 9225550Sdonn */ 9325550Sdonn if (strlen(system) > sizeof PNbuf - 1) { 9425550Sdonn fprintf(stderr, "tip: phone number too long (max = %d bytes)\n", 9525550Sdonn sizeof PNbuf - 1); 9625550Sdonn exit(1); 9725550Sdonn } 9825550Sdonn strncpy( PNbuf, system, sizeof PNbuf - 1 ); 9925550Sdonn for (p = system; *p; p++) 10025550Sdonn *p = '\0'; 10125550Sdonn PN = PNbuf; 1025257Sshannon system = sprintf(sbuf, "tip%d", BR); 1035257Sshannon 1045257Sshannon notnumber: 1053696Sroot signal(SIGINT, cleanup); 1063696Sroot signal(SIGQUIT, cleanup); 1073696Sroot signal(SIGHUP, cleanup); 1083696Sroot signal(SIGTERM, cleanup); 1093898Ssam 1103696Sroot if ((i = hunt(system)) == 0) { 1113696Sroot printf("all ports busy\n"); 1123696Sroot exit(3); 1133696Sroot } 1143696Sroot if (i == -1) { 1153696Sroot printf("link down\n"); 1164232Ssam delock(uucplock); 1173696Sroot exit(3); 1183696Sroot } 1193719Ssam setbuf(stdout, NULL); 1203696Sroot loginit(); 1215257Sshannon 1223696Sroot /* 1233696Sroot * Kludge, their's no easy way to get the initialization 1243696Sroot * in the right order, so force it here 1253696Sroot */ 1263696Sroot if ((PH = getenv("PHONES")) == NOSTR) 1273696Sroot PH = "/etc/phones"; 1283696Sroot vinit(); /* init variables */ 12913430Ssam setparity("even"); /* set the parity table */ 1304004Ssam if ((i = speed(number(value(BAUDRATE)))) == NULL) { 1313696Sroot printf("tip: bad baud rate %d\n", number(value(BAUDRATE))); 1323696Sroot delock(uucplock); 1333696Sroot exit(3); 1343696Sroot } 1353898Ssam 1364004Ssam /* 13725905Skarels * Now that we have the logfile and the ACU open 13825905Skarels * return to the real uid and gid. These things will 13925905Skarels * be closed on exit. Swap real and effective uid's 14025905Skarels * so we can get the original permissions back 14125905Skarels * for removing the uucp lock. 14225905Skarels */ 14325905Skarels gid = getgid(); 14425905Skarels egid = getegid(); 14525905Skarels uid = getuid(); 14625905Skarels euid = geteuid(); 14725905Skarels setregid(egid, gid); 14825905Skarels setreuid(euid, uid); 14925905Skarels 15025905Skarels /* 1514004Ssam * Hardwired connections require the 1524004Ssam * line speed set before they make any transmissions 1534004Ssam * (this is particularly true of things like a DF03-AC) 1544004Ssam */ 1554004Ssam if (HW) 1564004Ssam ttysetup(i); 1573898Ssam if (p = connect()) { 1583898Ssam printf("\07%s\n[EOT]\n", p); 15925905Skarels setreuid(uid, euid); 16025905Skarels setregid(gid, egid); 1613898Ssam delock(uucplock); 1623898Ssam exit(1); 1633898Ssam } 1644004Ssam if (!HW) 1654004Ssam ttysetup(i); 1665136Ssam cucommon: 1673696Sroot /* 1685136Ssam * From here down the code is shared with 1695136Ssam * the "cu" version of tip. 1703696Sroot */ 17125905Skarels 1724004Ssam ioctl(0, TIOCGETP, (char *)&defarg); 1734004Ssam ioctl(0, TIOCGETC, (char *)&defchars); 17412478Sroot ioctl(0, TIOCGLTC, (char *)&deflchars); 1754004Ssam ioctl(0, TIOCGETD, (char *)&odisc); 1763696Sroot arg = defarg; 1773696Sroot arg.sg_flags = ANYP | CBREAK; 1783696Sroot tchars = defchars; 1793696Sroot tchars.t_intrc = tchars.t_quitc = -1; 18012478Sroot ltchars = deflchars; 18112478Sroot ltchars.t_suspc = ltchars.t_dsuspc = ltchars.t_flushc 18212478Sroot = ltchars.t_lnextc = -1; 1833696Sroot raw(); 1844004Ssam 1853696Sroot pipe(fildes); pipe(repdes); 1863696Sroot signal(SIGALRM, timeout); 1873898Ssam 1883898Ssam /* 1893898Ssam * Everything's set up now: 19017236Shelge * connection established (hardwired or dialup) 1913898Ssam * line conditioned (baud rate, mode, etc.) 1923898Ssam * internal data structures (variables) 1933898Ssam * so, fork one process for local side and one for remote. 1943898Ssam */ 1955136Ssam printf(cumode ? "Connected\r\n" : "\07connected\r\n"); 1963696Sroot if (pid = fork()) 1973696Sroot tipin(); 1983696Sroot else 1993696Sroot tipout(); 2003696Sroot /*NOTREACHED*/ 2013696Sroot } 2023696Sroot 2033696Sroot cleanup() 2043696Sroot { 20513279Ssam 20625905Skarels if (uid != getuid()) { 20725905Skarels setreuid(uid, euid); 20825905Skarels setregid(gid, egid); 20925905Skarels } 2103696Sroot delock(uucplock); 2113898Ssam if (odisc) 2123898Ssam ioctl(0, TIOCSETD, (char *)&odisc); 2133696Sroot exit(0); 2143696Sroot } 2153696Sroot 2163696Sroot /* 2173696Sroot * put the controlling keyboard into raw mode 2183696Sroot */ 2193696Sroot raw() 2203696Sroot { 22113279Ssam 2223696Sroot ioctl(0, TIOCSETP, &arg); 2233696Sroot ioctl(0, TIOCSETC, &tchars); 22412478Sroot ioctl(0, TIOCSLTC, <chars); 2254004Ssam ioctl(0, TIOCSETD, (char *)&disc); 2263696Sroot } 2273696Sroot 2283696Sroot 2293696Sroot /* 2303696Sroot * return keyboard to normal mode 2313696Sroot */ 2323696Sroot unraw() 2333696Sroot { 23413279Ssam 2354004Ssam ioctl(0, TIOCSETD, (char *)&odisc); 2364004Ssam ioctl(0, TIOCSETP, (char *)&defarg); 2374004Ssam ioctl(0, TIOCSETC, (char *)&defchars); 23812478Sroot ioctl(0, TIOCSLTC, (char *)&deflchars); 2393696Sroot } 2403696Sroot 24113279Ssam static jmp_buf promptbuf; 24213279Ssam 2433696Sroot /* 2443696Sroot * Print string ``s'', then read a string 2453696Sroot * in from the terminal. Handles signals & allows use of 2463696Sroot * normal erase and kill characters. 2473696Sroot */ 2483696Sroot prompt(s, p) 2493696Sroot char *s; 2503696Sroot register char *p; 2513696Sroot { 2523696Sroot register char *b = p; 25313279Ssam int (*oint)(), (*oquit)(); 2543696Sroot 2553696Sroot stoprompt = 0; 25613279Ssam oint = signal(SIGINT, intprompt); 25713279Ssam oint = signal(SIGQUIT, SIG_IGN); 2583696Sroot unraw(); 2593696Sroot printf("%s", s); 26013279Ssam if (setjmp(promptbuf) == 0) 26113279Ssam while ((*p = getchar()) != EOF && *p != '\n') 26213279Ssam p++; 2633696Sroot *p = '\0'; 26413279Ssam 2653696Sroot raw(); 26613279Ssam signal(SIGINT, oint); 26713279Ssam signal(SIGQUIT, oint); 26813279Ssam return (stoprompt || p == b); 2693696Sroot } 2703696Sroot 2713696Sroot /* 2723696Sroot * Interrupt service routine during prompting 2733696Sroot */ 2743696Sroot intprompt() 2753696Sroot { 27613279Ssam 2773696Sroot signal(SIGINT, SIG_IGN); 2783696Sroot stoprompt = 1; 2793696Sroot printf("\r\n"); 28013279Ssam longjmp(promptbuf, 1); 2813696Sroot } 2823696Sroot 2833696Sroot /* 2843696Sroot * ****TIPIN TIPIN**** 2853696Sroot */ 2863696Sroot tipin() 2873696Sroot { 2883696Sroot char gch, bol = 1; 2893696Sroot 2903796Ssam /* 2913796Ssam * Kinda klugey here... 2923796Ssam * check for scripting being turned on from the .tiprc file, 2933796Ssam * but be careful about just using setscript(), as we may 2943796Ssam * send a SIGEMT before tipout has a chance to set up catching 2953796Ssam * it; so wait a second, then setscript() 2963796Ssam */ 2973796Ssam if (boolean(value(SCRIPT))) { 2983796Ssam sleep(1); 2993796Ssam setscript(); 3003796Ssam } 3013796Ssam 3023696Sroot while (1) { 3033696Sroot gch = getchar()&0177; 3043696Sroot if ((gch == character(value(ESCAPE))) && bol) { 3053696Sroot if (!(gch = escape())) 3063696Sroot continue; 3075136Ssam } else if (!cumode && gch == character(value(RAISECHAR))) { 3083696Sroot boolean(value(RAISE)) = !boolean(value(RAISE)); 3093696Sroot continue; 3103696Sroot } else if (gch == '\r') { 3113696Sroot bol = 1; 31213139Sralph pwrite(FD, &gch, 1); 31313139Sralph if (boolean(value(HALFDUPLEX))) 31413139Sralph printf("\r\n"); 3153696Sroot continue; 3165136Ssam } else if (!cumode && gch == character(value(FORCE))) 3173696Sroot gch = getchar()&0177; 3183696Sroot bol = any(gch, value(EOL)); 3193696Sroot if (boolean(value(RAISE)) && islower(gch)) 32013139Sralph gch = toupper(gch); 32113139Sralph pwrite(FD, &gch, 1); 32213139Sralph if (boolean(value(HALFDUPLEX))) 32313139Sralph printf("%c", gch); 3243696Sroot } 3253696Sroot } 3263696Sroot 3273696Sroot /* 3283696Sroot * Escape handler -- 3293696Sroot * called on recognition of ``escapec'' at the beginning of a line 3303696Sroot */ 3313696Sroot escape() 3323696Sroot { 3333696Sroot register char gch; 3343696Sroot register esctable_t *p; 3353696Sroot char c = character(value(ESCAPE)); 3363696Sroot extern esctable_t etable[]; 3373696Sroot 3383696Sroot gch = (getchar()&0177); 3393696Sroot for (p = etable; p->e_char; p++) 3403696Sroot if (p->e_char == gch) { 3413696Sroot if ((p->e_flags&PRIV) && getuid()) 3423696Sroot continue; 3433696Sroot printf("%s", ctrl(c)); 3443696Sroot (*p->e_func)(gch); 34513279Ssam return (0); 3463696Sroot } 3474146Ssam /* ESCAPE ESCAPE forces ESCAPE */ 3484146Ssam if (c != gch) 34913139Sralph pwrite(FD, &c, 1); 35013279Ssam return (gch); 3513696Sroot } 3523696Sroot 3533696Sroot speed(n) 35413279Ssam int n; 3553696Sroot { 3563696Sroot register int *p; 3573696Sroot 3583696Sroot for (p = bauds; *p != -1; p++) 3593696Sroot if (*p == n) 36013279Ssam return (p - bauds); 36113279Ssam return (NULL); 3623696Sroot } 3633696Sroot 3643696Sroot any(c, p) 3653696Sroot register char c, *p; 3663696Sroot { 36713279Ssam while (p && *p) 3683696Sroot if (*p++ == c) 36913279Ssam return (1); 37013279Ssam return (0); 3713696Sroot } 3723696Sroot 3733696Sroot size(s) 3743696Sroot register char *s; 3753696Sroot { 37613279Ssam register int i = 0; 3773696Sroot 37813279Ssam while (s && *s++) 37913279Ssam i++; 38013279Ssam return (i); 3813696Sroot } 3823696Sroot 3833696Sroot char * 3843696Sroot interp(s) 3853696Sroot register char *s; 3863696Sroot { 3873696Sroot static char buf[256]; 3883696Sroot register char *p = buf, c, *q; 3893696Sroot 3903696Sroot while (c = *s++) { 3913696Sroot for (q = "\nn\rr\tt\ff\033E\bb"; *q; q++) 3923696Sroot if (*q++ == c) { 3933696Sroot *p++ = '\\'; *p++ = *q; 3943696Sroot goto next; 3953696Sroot } 3963696Sroot if (c < 040) { 3973696Sroot *p++ = '^'; *p++ = c + 'A'-1; 3983696Sroot } else if (c == 0177) { 3993696Sroot *p++ = '^'; *p++ = '?'; 4003696Sroot } else 4013696Sroot *p++ = c; 4023696Sroot next: 4033696Sroot ; 4043696Sroot } 4053696Sroot *p = '\0'; 40613279Ssam return (buf); 4073696Sroot } 4083696Sroot 4093696Sroot char * 4103696Sroot ctrl(c) 4113696Sroot char c; 4123696Sroot { 4133696Sroot static char s[3]; 4143696Sroot 4153696Sroot if (c < 040 || c == 0177) { 4163696Sroot s[0] = '^'; 4173696Sroot s[1] = c == 0177 ? '?' : c+'A'-1; 4183696Sroot s[2] = '\0'; 4193696Sroot } else { 4203696Sroot s[0] = c; 4213696Sroot s[1] = '\0'; 4223696Sroot } 42313279Ssam return (s); 4243696Sroot } 4253696Sroot 4263696Sroot /* 4273696Sroot * Help command 4283696Sroot */ 4293696Sroot help(c) 4303696Sroot char c; 4313696Sroot { 4323696Sroot register esctable_t *p; 4333696Sroot extern esctable_t etable[]; 4343696Sroot 4353696Sroot printf("%c\r\n", c); 4363696Sroot for (p = etable; p->e_char; p++) { 4373696Sroot if ((p->e_flags&PRIV) && getuid()) 4383696Sroot continue; 4393696Sroot printf("%2s", ctrl(character(value(ESCAPE)))); 4403696Sroot printf("%-2s %c %s\r\n", ctrl(p->e_char), 4413696Sroot p->e_flags&EXP ? '*': ' ', p->e_help); 4423696Sroot } 4433696Sroot } 4444004Ssam 4454004Ssam /* 4464004Ssam * Set up the "remote" tty's state 4474004Ssam */ 4484004Ssam ttysetup(speed) 44913279Ssam int speed; 4504004Ssam { 4514004Ssam unsigned bits = LDECCTQ; 4524004Ssam 4534004Ssam arg.sg_ispeed = arg.sg_ospeed = speed; 45413279Ssam arg.sg_flags = RAW; 45513139Sralph if (boolean(value(TAND))) 45613279Ssam arg.sg_flags |= TANDEM; 4574004Ssam ioctl(FD, TIOCSETP, (char *)&arg); 4584004Ssam ioctl(FD, TIOCLBIS, (char *)&bits); 4594004Ssam } 4605257Sshannon 4615257Sshannon /* 4625257Sshannon * Return "simple" name from a file name, 4635257Sshannon * strip leading directories. 4645257Sshannon */ 4655257Sshannon char * 4665257Sshannon sname(s) 4675257Sshannon register char *s; 4685257Sshannon { 4695257Sshannon register char *p = s; 4705257Sshannon 4715257Sshannon while (*s) 4725257Sshannon if (*s++ == '/') 4735257Sshannon p = s; 4745257Sshannon return (p); 4755257Sshannon } 47613139Sralph 47713139Sralph static char partab[0200]; 47813139Sralph 47913139Sralph /* 48013279Ssam * Do a write to the remote machine with the correct parity. 48113279Ssam * We are doing 8 bit wide output, so we just generate a character 48213139Sralph * with the right parity and output it. 48313139Sralph */ 48413139Sralph pwrite(fd, buf, n) 48513139Sralph int fd; 48613139Sralph char *buf; 48713139Sralph register int n; 48813139Sralph { 48913139Sralph register int i; 49013279Ssam register char *bp; 49115188Ssam extern int errno; 49213139Sralph 49313279Ssam bp = buf; 49413279Ssam for (i = 0; i < n; i++) { 49513139Sralph *bp = partab[(*bp) & 0177]; 49613279Ssam bp++; 49713139Sralph } 49815188Ssam if (write(fd, buf, n) < 0) { 49915188Ssam if (errno == EIO) 50015188Ssam abort("Lost carrier."); 50115188Ssam /* this is questionable */ 50215188Ssam perror("write"); 50315188Ssam } 50413139Sralph } 50513139Sralph 50613139Sralph /* 50713279Ssam * Build a parity table with appropriate high-order bit. 50813139Sralph */ 50913430Ssam setparity(defparity) 51013430Ssam char *defparity; 51113139Sralph { 51213279Ssam register int i; 51313139Sralph char *parity; 51413279Ssam extern char evenpartab[]; 51513139Sralph 51613139Sralph if (value(PARITY) == NOSTR) 51713430Ssam value(PARITY) = defparity; 51813139Sralph parity = value(PARITY); 51913139Sralph for (i = 0; i < 0200; i++) 52013279Ssam partab[i] = evenpartab[i]; 52113279Ssam if (equal(parity, "even")) 52213279Ssam return; 52313139Sralph if (equal(parity, "odd")) { 52413139Sralph for (i = 0; i < 0200; i++) 52513139Sralph partab[i] ^= 0200; /* reverse bit 7 */ 52613279Ssam return; 52713139Sralph } 52813279Ssam if (equal(parity, "none") || equal(parity, "zero")) { 52913139Sralph for (i = 0; i < 0200; i++) 53013139Sralph partab[i] &= ~0200; /* turn off bit 7 */ 53113279Ssam return; 53213139Sralph } 53313279Ssam if (equal(parity, "one")) { 53413139Sralph for (i = 0; i < 0200; i++) 53513139Sralph partab[i] |= 0200; /* turn on bit 7 */ 53613279Ssam return; 53713139Sralph } 53813279Ssam fprintf(stderr, "%s: unknown parity value\n", PA); 53913279Ssam fflush(stderr); 54013139Sralph } 541