Annotation of src/usr.bin/tip/tip.c, Revision 1.16
1.16 ! deraadt 1: /* $OpenBSD: tip.c,v 1.15 2001/10/24 18:38:58 millert 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.16 ! deraadt 47: static char rcsid[] = "$OpenBSD: tip.c,v 1.15 2001/10/24 18:38:58 millert 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: {
1.16 ! deraadt 373: char bol = 1;
! 374: int gch;
1.1 deraadt 375:
376: /*
377: * Kinda klugey here...
378: * check for scripting being turned on from the .tiprc file,
379: * but be careful about just using setscript(), as we may
380: * send a SIGEMT before tipout has a chance to set up catching
381: * it; so wait a second, then setscript()
382: */
383: if (boolean(value(SCRIPT))) {
384: sleep(1);
385: setscript();
386: }
387:
388: while (1) {
1.2 deraadt 389: gch = getchar()&STRIP_PAR;
1.16 ! deraadt 390: /* XXX does not check for EOF */
1.1 deraadt 391: if ((gch == character(value(ESCAPE))) && bol) {
1.9 todd 392: if (!noesc) {
393: if (!(gch = escape()))
394: continue;
395: }
1.1 deraadt 396: } else if (!cumode && gch == character(value(RAISECHAR))) {
1.4 millert 397: setboolean(value(RAISE), !boolean(value(RAISE)));
1.1 deraadt 398: continue;
399: } else if (gch == '\r') {
400: bol = 1;
1.11 deraadt 401: parwrite(FD, &gch, 1);
1.1 deraadt 402: if (boolean(value(HALFDUPLEX)))
403: printf("\r\n");
404: continue;
405: } else if (!cumode && gch == character(value(FORCE)))
1.2 deraadt 406: gch = getchar()&STRIP_PAR;
1.1 deraadt 407: bol = any(gch, value(EOL));
408: if (boolean(value(RAISE)) && islower(gch))
409: gch = toupper(gch);
1.11 deraadt 410: parwrite(FD, &gch, 1);
1.1 deraadt 411: if (boolean(value(HALFDUPLEX)))
412: printf("%c", gch);
413: }
414: }
415:
416: extern esctable_t etable[];
417:
418: /*
419: * Escape handler --
420: * called on recognition of ``escapec'' at the beginning of a line
421: */
1.8 deraadt 422: int
1.1 deraadt 423: escape()
424: {
1.16 ! deraadt 425: int gch;
1.15 millert 426: esctable_t *p;
1.1 deraadt 427: char c = character(value(ESCAPE));
428:
1.2 deraadt 429: gch = (getchar()&STRIP_PAR);
1.16 ! deraadt 430: /* XXX does not check for EOF */
1.1 deraadt 431: for (p = etable; p->e_char; p++)
432: if (p->e_char == gch) {
433: if ((p->e_flags&PRIV) && uid)
434: continue;
435: printf("%s", ctrl(c));
436: (*p->e_func)(gch);
437: return (0);
438: }
439: /* ESCAPE ESCAPE forces ESCAPE */
440: if (c != gch)
1.11 deraadt 441: parwrite(FD, &c, 1);
1.1 deraadt 442: return (gch);
443: }
444:
1.8 deraadt 445: int
1.1 deraadt 446: speed(n)
447: int n;
448: {
1.15 millert 449: int *p;
1.1 deraadt 450:
1.2 deraadt 451: for (p = rates; *p != -1; p++)
1.1 deraadt 452: if (*p == n)
1.2 deraadt 453: return n;
454: return 0;
1.1 deraadt 455: }
456:
1.8 deraadt 457: int
458: any(cc, p)
1.15 millert 459: int cc;
1.8 deraadt 460: char *p;
1.1 deraadt 461: {
1.8 deraadt 462: char c = cc;
1.1 deraadt 463: while (p && *p)
464: if (*p++ == c)
465: return (1);
466: return (0);
467: }
468:
1.8 deraadt 469: int
1.1 deraadt 470: size(s)
1.15 millert 471: char *s;
1.1 deraadt 472: {
1.15 millert 473: int i = 0;
1.1 deraadt 474:
475: while (s && *s++)
476: i++;
477: return (i);
478: }
479:
480: char *
481: interp(s)
1.15 millert 482: char *s;
1.1 deraadt 483: {
484: static char buf[256];
1.15 millert 485: char *p = buf, c, *q;
1.1 deraadt 486:
1.8 deraadt 487: while ((c = *s++)) {
1.1 deraadt 488: for (q = "\nn\rr\tt\ff\033E\bb"; *q; q++)
489: if (*q++ == c) {
490: *p++ = '\\'; *p++ = *q;
491: goto next;
492: }
493: if (c < 040) {
494: *p++ = '^'; *p++ = c + 'A'-1;
495: } else if (c == 0177) {
496: *p++ = '^'; *p++ = '?';
497: } else
498: *p++ = c;
499: next:
500: ;
501: }
502: *p = '\0';
503: return (buf);
504: }
505:
506: char *
507: ctrl(c)
508: char c;
509: {
510: static char s[3];
511:
512: if (c < 040 || c == 0177) {
513: s[0] = '^';
514: s[1] = c == 0177 ? '?' : c+'A'-1;
515: s[2] = '\0';
516: } else {
517: s[0] = c;
518: s[1] = '\0';
519: }
520: return (s);
521: }
522:
523: /*
524: * Help command
525: */
1.8 deraadt 526: void
1.1 deraadt 527: help(c)
528: char c;
529: {
1.15 millert 530: esctable_t *p;
1.1 deraadt 531:
532: printf("%c\r\n", c);
533: for (p = etable; p->e_char; p++) {
534: if ((p->e_flags&PRIV) && uid)
535: continue;
536: printf("%2s", ctrl(character(value(ESCAPE))));
537: printf("%-2s %c %s\r\n", ctrl(p->e_char),
538: p->e_flags&EXP ? '*': ' ', p->e_help);
539: }
540: }
541:
542: /*
543: * Set up the "remote" tty's state
544: */
1.8 deraadt 545: void
1.1 deraadt 546: ttysetup(speed)
547: int speed;
548: {
1.2 deraadt 549: struct termios cntrl;
1.1 deraadt 550:
1.2 deraadt 551: tcgetattr(FD, &cntrl);
552: cfsetospeed(&cntrl, speed);
553: cfsetispeed(&cntrl, speed);
554: cntrl.c_cflag &= ~(CSIZE|PARENB);
555: cntrl.c_cflag |= CS8;
1.5 millert 556: if (boolean(value(DC)))
557: cntrl.c_cflag |= CLOCAL;
1.2 deraadt 558: cntrl.c_iflag &= ~(ISTRIP|ICRNL);
559: cntrl.c_oflag &= ~OPOST;
560: cntrl.c_lflag &= ~(ICANON|ISIG|IEXTEN|ECHO);
561: cntrl.c_cc[VMIN] = 1;
562: cntrl.c_cc[VTIME] = 0;
1.1 deraadt 563: if (boolean(value(TAND)))
1.2 deraadt 564: cntrl.c_iflag |= IXOFF;
565: tcsetattr(FD, TCSAFLUSH, &cntrl);
1.1 deraadt 566: }
567:
568: static char partab[0200];
569:
570: /*
571: * Do a write to the remote machine with the correct parity.
572: * We are doing 8 bit wide output, so we just generate a character
573: * with the right parity and output it.
574: */
1.8 deraadt 575: void
1.11 deraadt 576: parwrite(fd, buf, n)
1.1 deraadt 577: int fd;
578: char *buf;
1.15 millert 579: int n;
1.1 deraadt 580: {
1.15 millert 581: int i;
582: char *bp;
1.1 deraadt 583:
584: bp = buf;
585: if (bits8 == 0)
586: for (i = 0; i < n; i++) {
587: *bp = partab[(*bp) & 0177];
588: bp++;
589: }
590: if (write(fd, buf, n) < 0) {
591: if (errno == EIO)
592: tipabort("Lost carrier.");
593: /* this is questionable */
594: perror("write");
595: }
596: }
597:
598: /*
599: * Build a parity table with appropriate high-order bit.
600: */
1.8 deraadt 601: void
1.1 deraadt 602: setparity(defparity)
603: char *defparity;
604: {
1.15 millert 605: int i, flip, clr, set;
1.1 deraadt 606: char *parity;
1.4 millert 607: extern const unsigned char evenpartab[];
1.1 deraadt 608:
609: if (value(PARITY) == NOSTR)
610: value(PARITY) = defparity;
611: parity = value(PARITY);
612: if (equal(parity, "none")) {
613: bits8 = 1;
614: return;
615: }
616: bits8 = 0;
617: flip = 0;
618: clr = 0377;
619: set = 0;
620: if (equal(parity, "odd"))
621: flip = 0200; /* reverse bit 7 */
622: else if (equal(parity, "zero"))
623: clr = 0177; /* turn off bit 7 */
624: else if (equal(parity, "one"))
625: set = 0200; /* turn on bit 7 */
626: else if (!equal(parity, "even")) {
627: (void) fprintf(stderr, "%s: unknown parity value\r\n", parity);
628: (void) fflush(stderr);
629: }
630: for (i = 0; i < 0200; i++)
631: partab[i] = (evenpartab[i] ^ flip | set) & clr;
632: }