Annotation of src/usr.bin/tip/tip.c, Revision 1.15
1.15 ! millert 1: /* $OpenBSD: tip.c,v 1.14 2001/09/26 06:07:28 pvalchev Exp $ */
1.5 millert 2: /* $NetBSD: tip.c,v 1.13 1997/04/20 00:03:05 mellon Exp $ */
1.1 deraadt 3:
4: /*
5: * Copyright (c) 1983, 1993
6: * The Regents of the University of California. All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
16: * 3. All advertising materials mentioning features or use of this software
17: * must display the following acknowledgement:
18: * This product includes software developed by the University of
19: * California, Berkeley and its contributors.
20: * 4. Neither the name of the University nor the names of its contributors
21: * may be used to endorse or promote products derived from this software
22: * without specific prior written permission.
23: *
24: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34: * SUCH DAMAGE.
35: */
36:
37: #ifndef lint
38: static char copyright[] =
39: "@(#) Copyright (c) 1983, 1993\n\
40: The Regents of the University of California. All rights reserved.\n";
41: #endif /* not lint */
42:
43: #ifndef lint
44: #if 0
45: static char sccsid[] = "@(#)tip.c 8.1 (Berkeley) 6/6/93";
46: #endif
1.15 ! millert 47: static char rcsid[] = "$OpenBSD: tip.c,v 1.14 2001/09/26 06:07:28 pvalchev Exp $";
1.1 deraadt 48: #endif /* not lint */
49:
50: /*
51: * tip - UNIX link to other systems
52: * tip [-v] [-speed] system-name
53: * or
54: * cu phone-number [-s speed] [-l line] [-a acu]
55: */
56: #include "tip.h"
57: #include "pathnames.h"
58:
59: /*
60: * Baud rate mapping table
61: */
1.2 deraadt 62: int rates[] = {
1.1 deraadt 63: 0, 50, 75, 110, 134, 150, 200, 300, 600,
64: 1200, 1800, 2400, 4800, 9600, 19200, 38400, 57600, 115200, -1
65: };
66:
1.2 deraadt 67: int disc = TTYDISC; /* tip normally runs this way */
1.1 deraadt 68: void intprompt();
69: void timeout();
70: void cleanup();
71: char PNbuf[256]; /* This limits the size of a number */
72:
1.8 deraadt 73: int
1.1 deraadt 74: main(argc, argv)
1.8 deraadt 75: int argc;
1.1 deraadt 76: char *argv[];
77: {
78: char *system = NOSTR;
1.15 ! millert 79: int i;
! 80: char *p;
1.1 deraadt 81: char sbuf[12];
82:
83: gid = getgid();
84: egid = getegid();
85: uid = getuid();
86: euid = geteuid();
1.13 millert 87: if (equal(__progname, "cu")) {
1.1 deraadt 88: cumode = 1;
89: cumain(argc, argv);
90: goto cucommon;
91: }
92:
93: if (argc > 4) {
94: fprintf(stderr, "usage: tip [-v] [-speed] [system-name]\n");
95: exit(1);
96: }
97: if (!isatty(0)) {
1.13 millert 98: fprintf(stderr, "%s: must be interactive\n", __progname);
1.1 deraadt 99: exit(1);
100: }
101:
102: for (; argc > 1; argv++, argc--) {
103: if (argv[1][0] != '-')
104: system = argv[1];
105: else switch (argv[1][1]) {
106:
107: case 'v':
108: vflag++;
109: break;
110:
1.9 todd 111: case 'n':
112: noesc++;
113: break;
114:
1.1 deraadt 115: case '0': case '1': case '2': case '3': case '4':
116: case '5': case '6': case '7': case '8': case '9':
117: BR = atoi(&argv[1][1]);
118: break;
119:
120: default:
1.13 millert 121: fprintf(stderr, "%s: %s, unknown option\n", __progname,
122: argv[1]);
1.1 deraadt 123: break;
124: }
125: }
126:
127: if (system == NOSTR)
128: goto notnumber;
129: if (isalpha(*system))
130: goto notnumber;
131: /*
132: * System name is really a phone number...
133: * Copy the number then stomp on the original (in case the number
134: * is private, we don't want 'ps' or 'w' to find it).
135: */
136: if (strlen(system) > sizeof PNbuf - 1) {
1.13 millert 137: fprintf(stderr, "%s: phone number too long (max = %d bytes)\n",
1.14 pvalchev 138: __progname, (int)sizeof(PNbuf) - 1);
1.1 deraadt 139: exit(1);
140: }
141: strncpy( PNbuf, system, sizeof PNbuf - 1 );
142: for (p = system; *p; p++)
143: *p = '\0';
144: PN = PNbuf;
1.8 deraadt 145: (void)snprintf(sbuf, sizeof(sbuf), "tip%ld", BR);
1.1 deraadt 146: system = sbuf;
147:
148: notnumber:
149: (void)signal(SIGINT, cleanup);
150: (void)signal(SIGQUIT, cleanup);
151: (void)signal(SIGHUP, cleanup);
152: (void)signal(SIGTERM, cleanup);
153:
154: if ((i = hunt(system)) == 0) {
155: printf("all ports busy\n");
156: exit(3);
157: }
158: if (i == -1) {
159: printf("link down\n");
160: (void)uu_unlock(uucplock);
161: exit(3);
162: }
163: setbuf(stdout, NULL);
164: loginit();
165:
166: /*
167: * Now that we have the logfile and the ACU open
168: * return to the real uid and gid. These things will
169: * be closed on exit. Swap real and effective uid's
170: * so we can get the original permissions back
171: * for removing the uucp lock.
172: */
173: user_uid();
174:
175: /*
176: * Kludge, their's no easy way to get the initialization
177: * in the right order, so force it here
178: */
179: if ((PH = getenv("PHONES")) == NOSTR)
180: PH = _PATH_PHONES;
181: vinit(); /* init variables */
1.12 millert 182: setparity("none"); /* set the parity table */
1.2 deraadt 183: if ((i = speed(number(value(BAUDRATE)))) == 0) {
1.13 millert 184: printf("%s: bad baud rate %ld\n", __progname,
185: number(value(BAUDRATE)));
1.1 deraadt 186: daemon_uid();
187: (void)uu_unlock(uucplock);
188: exit(3);
189: }
190:
191: /*
192: * Hardwired connections require the
193: * line speed set before they make any transmissions
194: * (this is particularly true of things like a DF03-AC)
195: */
196: if (HW)
197: ttysetup(i);
1.8 deraadt 198: if ((p = connect())) {
1.1 deraadt 199: printf("\07%s\n[EOT]\n", p);
200: daemon_uid();
201: (void)uu_unlock(uucplock);
202: exit(1);
203: }
204: if (!HW)
205: ttysetup(i);
206: cucommon:
207: /*
208: * From here down the code is shared with
209: * the "cu" version of tip.
210: */
1.10 jason 211:
212: i = fcntl(FD, F_GETFL);
213: if (i == -1) {
214: perror("fcntl");
215: cleanup();
216: }
217: i = fcntl(FD, F_SETFL, i & ~O_NONBLOCK);
218: if (i == -1) {
219: perror("fcntl");
220: cleanup();
221: }
1.1 deraadt 222:
1.2 deraadt 223: tcgetattr(0, &defterm);
224: term = defterm;
225: term.c_lflag &= ~(ICANON|IEXTEN|ECHO);
226: term.c_iflag &= ~(INPCK|ICRNL);
227: term.c_oflag &= ~OPOST;
228: term.c_cc[VMIN] = 1;
229: term.c_cc[VTIME] = 0;
230: defchars = term;
231: term.c_cc[VINTR] = term.c_cc[VQUIT] = term.c_cc[VSUSP] =
232: term.c_cc[VDSUSP] = term.c_cc[VDISCARD] =
233: term.c_cc[VLNEXT] = _POSIX_VDISABLE;
1.1 deraadt 234: raw();
235:
236: pipe(fildes); pipe(repdes);
237: (void)signal(SIGALRM, timeout);
238:
239: /*
240: * Everything's set up now:
241: * connection established (hardwired or dialup)
242: * line conditioned (baud rate, mode, etc.)
243: * internal data structures (variables)
244: * so, fork one process for local side and one for remote.
245: */
246: printf(cumode ? "Connected\r\n" : "\07connected\r\n");
1.8 deraadt 247: if ((pid = fork()))
1.1 deraadt 248: tipin();
249: else
250: tipout();
251: /*NOTREACHED*/
1.8 deraadt 252: exit(0);
1.1 deraadt 253: }
254:
255: void
256: cleanup()
257: {
258:
259: daemon_uid();
260: (void)uu_unlock(uucplock);
261: if (odisc)
262: ioctl(0, TIOCSETD, (char *)&odisc);
263: exit(0);
264: }
265:
266: /*
267: * Muck with user ID's. We are setuid to the owner of the lock
268: * directory when we start. user_uid() reverses real and effective
269: * ID's after startup, to run with the user's permissions.
270: * daemon_uid() switches back to the privileged uid for unlocking.
271: * Finally, to avoid running a shell with the wrong real uid,
272: * shell_uid() sets real and effective uid's to the user's real ID.
273: */
274: static int uidswapped;
275:
1.8 deraadt 276: void
1.1 deraadt 277: user_uid()
278: {
279: if (uidswapped == 0) {
280: seteuid(uid);
281: uidswapped = 1;
282: }
283: }
284:
1.8 deraadt 285: void
1.1 deraadt 286: daemon_uid()
287: {
288:
289: if (uidswapped) {
290: seteuid(euid);
291: uidswapped = 0;
292: }
293: }
294:
1.8 deraadt 295: void
1.1 deraadt 296: shell_uid()
297: {
1.7 deraadt 298: setegid(gid);
1.1 deraadt 299: seteuid(uid);
300: }
301:
302: /*
303: * put the controlling keyboard into raw mode
304: */
1.8 deraadt 305: void
1.1 deraadt 306: raw()
307: {
1.2 deraadt 308: tcsetattr(0, TCSADRAIN, &term);
1.1 deraadt 309: }
310:
311:
312: /*
313: * return keyboard to normal mode
314: */
1.8 deraadt 315: void
1.1 deraadt 316: unraw()
317: {
1.2 deraadt 318: tcsetattr(0, TCSADRAIN, &defterm);
1.1 deraadt 319: }
320:
321: static jmp_buf promptbuf;
322:
323: /*
324: * Print string ``s'', then read a string
325: * in from the terminal. Handles signals & allows use of
326: * normal erase and kill characters.
327: */
1.8 deraadt 328: int
1.6 millert 329: prompt(s, p, sz)
1.1 deraadt 330: char *s;
1.8 deraadt 331: char *p;
1.6 millert 332: size_t sz;
1.1 deraadt 333: {
1.15 ! millert 334: int c;
! 335: char *b = p;
1.1 deraadt 336: sig_t oint, oquit;
337:
338: stoprompt = 0;
339: oint = signal(SIGINT, intprompt);
340: oquit = signal(SIGQUIT, SIG_IGN);
341: unraw();
342: printf("%s", s);
343: if (setjmp(promptbuf) == 0)
1.6 millert 344: while ((c = getchar()) != EOF && (*p = c) != '\n' && --sz > 0)
1.1 deraadt 345: p++;
346: *p = '\0';
347:
348: raw();
349: (void)signal(SIGINT, oint);
350: (void)signal(SIGQUIT, oquit);
351: return (stoprompt || p == b);
352: }
353:
354: /*
355: * Interrupt service routine during prompting
356: */
357: void
358: intprompt()
359: {
360:
361: (void)signal(SIGINT, SIG_IGN);
362: stoprompt = 1;
363: printf("\r\n");
364: longjmp(promptbuf, 1);
365: }
366:
367: /*
368: * ****TIPIN TIPIN****
369: */
1.8 deraadt 370: void
1.1 deraadt 371: tipin()
372: {
373: char gch, bol = 1;
374:
375: /*
376: * Kinda klugey here...
377: * check for scripting being turned on from the .tiprc file,
378: * but be careful about just using setscript(), as we may
379: * send a SIGEMT before tipout has a chance to set up catching
380: * it; so wait a second, then setscript()
381: */
382: if (boolean(value(SCRIPT))) {
383: sleep(1);
384: setscript();
385: }
386:
387: while (1) {
1.2 deraadt 388: gch = getchar()&STRIP_PAR;
1.1 deraadt 389: if ((gch == character(value(ESCAPE))) && bol) {
1.9 todd 390: if (!noesc) {
391: if (!(gch = escape()))
392: continue;
393: }
1.1 deraadt 394: } else if (!cumode && gch == character(value(RAISECHAR))) {
1.4 millert 395: setboolean(value(RAISE), !boolean(value(RAISE)));
1.1 deraadt 396: continue;
397: } else if (gch == '\r') {
398: bol = 1;
1.11 deraadt 399: parwrite(FD, &gch, 1);
1.1 deraadt 400: if (boolean(value(HALFDUPLEX)))
401: printf("\r\n");
402: continue;
403: } else if (!cumode && gch == character(value(FORCE)))
1.2 deraadt 404: gch = getchar()&STRIP_PAR;
1.1 deraadt 405: bol = any(gch, value(EOL));
406: if (boolean(value(RAISE)) && islower(gch))
407: gch = toupper(gch);
1.11 deraadt 408: parwrite(FD, &gch, 1);
1.1 deraadt 409: if (boolean(value(HALFDUPLEX)))
410: printf("%c", gch);
411: }
412: }
413:
414: extern esctable_t etable[];
415:
416: /*
417: * Escape handler --
418: * called on recognition of ``escapec'' at the beginning of a line
419: */
1.8 deraadt 420: int
1.1 deraadt 421: escape()
422: {
1.15 ! millert 423: char gch;
! 424: esctable_t *p;
1.1 deraadt 425: char c = character(value(ESCAPE));
426:
1.2 deraadt 427: gch = (getchar()&STRIP_PAR);
1.1 deraadt 428: for (p = etable; p->e_char; p++)
429: if (p->e_char == gch) {
430: if ((p->e_flags&PRIV) && uid)
431: continue;
432: printf("%s", ctrl(c));
433: (*p->e_func)(gch);
434: return (0);
435: }
436: /* ESCAPE ESCAPE forces ESCAPE */
437: if (c != gch)
1.11 deraadt 438: parwrite(FD, &c, 1);
1.1 deraadt 439: return (gch);
440: }
441:
1.8 deraadt 442: int
1.1 deraadt 443: speed(n)
444: int n;
445: {
1.15 ! millert 446: int *p;
1.1 deraadt 447:
1.2 deraadt 448: for (p = rates; *p != -1; p++)
1.1 deraadt 449: if (*p == n)
1.2 deraadt 450: return n;
451: return 0;
1.1 deraadt 452: }
453:
1.8 deraadt 454: int
455: any(cc, p)
1.15 ! millert 456: int cc;
1.8 deraadt 457: char *p;
1.1 deraadt 458: {
1.8 deraadt 459: char c = cc;
1.1 deraadt 460: while (p && *p)
461: if (*p++ == c)
462: return (1);
463: return (0);
464: }
465:
1.8 deraadt 466: int
1.1 deraadt 467: size(s)
1.15 ! millert 468: char *s;
1.1 deraadt 469: {
1.15 ! millert 470: int i = 0;
1.1 deraadt 471:
472: while (s && *s++)
473: i++;
474: return (i);
475: }
476:
477: char *
478: interp(s)
1.15 ! millert 479: char *s;
1.1 deraadt 480: {
481: static char buf[256];
1.15 ! millert 482: char *p = buf, c, *q;
1.1 deraadt 483:
1.8 deraadt 484: while ((c = *s++)) {
1.1 deraadt 485: for (q = "\nn\rr\tt\ff\033E\bb"; *q; q++)
486: if (*q++ == c) {
487: *p++ = '\\'; *p++ = *q;
488: goto next;
489: }
490: if (c < 040) {
491: *p++ = '^'; *p++ = c + 'A'-1;
492: } else if (c == 0177) {
493: *p++ = '^'; *p++ = '?';
494: } else
495: *p++ = c;
496: next:
497: ;
498: }
499: *p = '\0';
500: return (buf);
501: }
502:
503: char *
504: ctrl(c)
505: char c;
506: {
507: static char s[3];
508:
509: if (c < 040 || c == 0177) {
510: s[0] = '^';
511: s[1] = c == 0177 ? '?' : c+'A'-1;
512: s[2] = '\0';
513: } else {
514: s[0] = c;
515: s[1] = '\0';
516: }
517: return (s);
518: }
519:
520: /*
521: * Help command
522: */
1.8 deraadt 523: void
1.1 deraadt 524: help(c)
525: char c;
526: {
1.15 ! millert 527: esctable_t *p;
1.1 deraadt 528:
529: printf("%c\r\n", c);
530: for (p = etable; p->e_char; p++) {
531: if ((p->e_flags&PRIV) && uid)
532: continue;
533: printf("%2s", ctrl(character(value(ESCAPE))));
534: printf("%-2s %c %s\r\n", ctrl(p->e_char),
535: p->e_flags&EXP ? '*': ' ', p->e_help);
536: }
537: }
538:
539: /*
540: * Set up the "remote" tty's state
541: */
1.8 deraadt 542: void
1.1 deraadt 543: ttysetup(speed)
544: int speed;
545: {
1.2 deraadt 546: struct termios cntrl;
1.1 deraadt 547:
1.2 deraadt 548: tcgetattr(FD, &cntrl);
549: cfsetospeed(&cntrl, speed);
550: cfsetispeed(&cntrl, speed);
551: cntrl.c_cflag &= ~(CSIZE|PARENB);
552: cntrl.c_cflag |= CS8;
1.5 millert 553: if (boolean(value(DC)))
554: cntrl.c_cflag |= CLOCAL;
1.2 deraadt 555: cntrl.c_iflag &= ~(ISTRIP|ICRNL);
556: cntrl.c_oflag &= ~OPOST;
557: cntrl.c_lflag &= ~(ICANON|ISIG|IEXTEN|ECHO);
558: cntrl.c_cc[VMIN] = 1;
559: cntrl.c_cc[VTIME] = 0;
1.1 deraadt 560: if (boolean(value(TAND)))
1.2 deraadt 561: cntrl.c_iflag |= IXOFF;
562: tcsetattr(FD, TCSAFLUSH, &cntrl);
1.1 deraadt 563: }
564:
565: static char partab[0200];
566:
567: /*
568: * Do a write to the remote machine with the correct parity.
569: * We are doing 8 bit wide output, so we just generate a character
570: * with the right parity and output it.
571: */
1.8 deraadt 572: void
1.11 deraadt 573: parwrite(fd, buf, n)
1.1 deraadt 574: int fd;
575: char *buf;
1.15 ! millert 576: int n;
1.1 deraadt 577: {
1.15 ! millert 578: int i;
! 579: char *bp;
1.1 deraadt 580:
581: bp = buf;
582: if (bits8 == 0)
583: for (i = 0; i < n; i++) {
584: *bp = partab[(*bp) & 0177];
585: bp++;
586: }
587: if (write(fd, buf, n) < 0) {
588: if (errno == EIO)
589: tipabort("Lost carrier.");
590: /* this is questionable */
591: perror("write");
592: }
593: }
594:
595: /*
596: * Build a parity table with appropriate high-order bit.
597: */
1.8 deraadt 598: void
1.1 deraadt 599: setparity(defparity)
600: char *defparity;
601: {
1.15 ! millert 602: int i, flip, clr, set;
1.1 deraadt 603: char *parity;
1.4 millert 604: extern const unsigned char evenpartab[];
1.1 deraadt 605:
606: if (value(PARITY) == NOSTR)
607: value(PARITY) = defparity;
608: parity = value(PARITY);
609: if (equal(parity, "none")) {
610: bits8 = 1;
611: return;
612: }
613: bits8 = 0;
614: flip = 0;
615: clr = 0377;
616: set = 0;
617: if (equal(parity, "odd"))
618: flip = 0200; /* reverse bit 7 */
619: else if (equal(parity, "zero"))
620: clr = 0177; /* turn off bit 7 */
621: else if (equal(parity, "one"))
622: set = 0200; /* turn on bit 7 */
623: else if (!equal(parity, "even")) {
624: (void) fprintf(stderr, "%s: unknown parity value\r\n", parity);
625: (void) fflush(stderr);
626: }
627: for (i = 0; i < 0200; i++)
628: partab[i] = (evenpartab[i] ^ flip | set) & clr;
629: }