Annotation of src/usr.bin/tip/tip.c, Revision 1.11
1.11 ! deraadt 1: /* $OpenBSD: tip.c,v 1.10 2000/01/09 05:38:47 jason 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.11 ! deraadt 47: static char rcsid[] = "$OpenBSD: tip.c,v 1.10 2000/01/09 05:38:47 jason 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 *sname();
72: char PNbuf[256]; /* This limits the size of a number */
73:
1.8 deraadt 74: int
1.1 deraadt 75: main(argc, argv)
1.8 deraadt 76: int argc;
1.1 deraadt 77: char *argv[];
78: {
79: char *system = NOSTR;
80: register int i;
81: register char *p;
82: char sbuf[12];
83:
84: gid = getgid();
85: egid = getegid();
86: uid = getuid();
87: euid = geteuid();
88: if (equal(sname(argv[0]), "cu")) {
89: cumode = 1;
90: cumain(argc, argv);
91: goto cucommon;
92: }
93:
94: if (argc > 4) {
95: fprintf(stderr, "usage: tip [-v] [-speed] [system-name]\n");
96: exit(1);
97: }
98: if (!isatty(0)) {
99: fprintf(stderr, "tip: must be interactive\n");
100: exit(1);
101: }
102:
103: for (; argc > 1; argv++, argc--) {
104: if (argv[1][0] != '-')
105: system = argv[1];
106: else switch (argv[1][1]) {
107:
108: case 'v':
109: vflag++;
110: break;
111:
1.9 todd 112: case 'n':
113: noesc++;
114: break;
115:
1.1 deraadt 116: case '0': case '1': case '2': case '3': case '4':
117: case '5': case '6': case '7': case '8': case '9':
118: BR = atoi(&argv[1][1]);
119: break;
120:
121: default:
122: fprintf(stderr, "tip: %s, unknown option\n", argv[1]);
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) {
137: fprintf(stderr, "tip: phone number too long (max = %d bytes)\n",
138: sizeof PNbuf - 1);
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 */
182: setparity("even"); /* set the parity table */
1.2 deraadt 183: if ((i = speed(number(value(BAUDRATE)))) == 0) {
1.8 deraadt 184: printf("tip: bad baud rate %ld\n", number(value(BAUDRATE)));
1.1 deraadt 185: daemon_uid();
186: (void)uu_unlock(uucplock);
187: exit(3);
188: }
189:
190: /*
191: * Hardwired connections require the
192: * line speed set before they make any transmissions
193: * (this is particularly true of things like a DF03-AC)
194: */
195: if (HW)
196: ttysetup(i);
1.8 deraadt 197: if ((p = connect())) {
1.1 deraadt 198: printf("\07%s\n[EOT]\n", p);
199: daemon_uid();
200: (void)uu_unlock(uucplock);
201: exit(1);
202: }
203: if (!HW)
204: ttysetup(i);
205: cucommon:
206: /*
207: * From here down the code is shared with
208: * the "cu" version of tip.
209: */
1.10 jason 210:
211: i = fcntl(FD, F_GETFL);
212: if (i == -1) {
213: perror("fcntl");
214: cleanup();
215: }
216: i = fcntl(FD, F_SETFL, i & ~O_NONBLOCK);
217: if (i == -1) {
218: perror("fcntl");
219: cleanup();
220: }
1.1 deraadt 221:
1.2 deraadt 222: tcgetattr(0, &defterm);
223: term = defterm;
224: term.c_lflag &= ~(ICANON|IEXTEN|ECHO);
225: term.c_iflag &= ~(INPCK|ICRNL);
226: term.c_oflag &= ~OPOST;
227: term.c_cc[VMIN] = 1;
228: term.c_cc[VTIME] = 0;
229: defchars = term;
230: term.c_cc[VINTR] = term.c_cc[VQUIT] = term.c_cc[VSUSP] =
231: term.c_cc[VDSUSP] = term.c_cc[VDISCARD] =
232: term.c_cc[VLNEXT] = _POSIX_VDISABLE;
1.1 deraadt 233: raw();
234:
235: pipe(fildes); pipe(repdes);
236: (void)signal(SIGALRM, timeout);
237:
238: /*
239: * Everything's set up now:
240: * connection established (hardwired or dialup)
241: * line conditioned (baud rate, mode, etc.)
242: * internal data structures (variables)
243: * so, fork one process for local side and one for remote.
244: */
245: printf(cumode ? "Connected\r\n" : "\07connected\r\n");
1.8 deraadt 246: if ((pid = fork()))
1.1 deraadt 247: tipin();
248: else
249: tipout();
250: /*NOTREACHED*/
1.8 deraadt 251: exit(0);
1.1 deraadt 252: }
253:
254: void
255: cleanup()
256: {
257:
258: daemon_uid();
259: (void)uu_unlock(uucplock);
260: if (odisc)
261: ioctl(0, TIOCSETD, (char *)&odisc);
262: exit(0);
263: }
264:
265: /*
266: * Muck with user ID's. We are setuid to the owner of the lock
267: * directory when we start. user_uid() reverses real and effective
268: * ID's after startup, to run with the user's permissions.
269: * daemon_uid() switches back to the privileged uid for unlocking.
270: * Finally, to avoid running a shell with the wrong real uid,
271: * shell_uid() sets real and effective uid's to the user's real ID.
272: */
273: static int uidswapped;
274:
1.8 deraadt 275: void
1.1 deraadt 276: user_uid()
277: {
278: if (uidswapped == 0) {
279: seteuid(uid);
280: uidswapped = 1;
281: }
282: }
283:
1.8 deraadt 284: void
1.1 deraadt 285: daemon_uid()
286: {
287:
288: if (uidswapped) {
289: seteuid(euid);
290: uidswapped = 0;
291: }
292: }
293:
1.8 deraadt 294: void
1.1 deraadt 295: shell_uid()
296: {
1.7 deraadt 297: setegid(gid);
1.1 deraadt 298: seteuid(uid);
299: }
300:
301: /*
302: * put the controlling keyboard into raw mode
303: */
1.8 deraadt 304: void
1.1 deraadt 305: raw()
306: {
1.2 deraadt 307: tcsetattr(0, TCSADRAIN, &term);
1.1 deraadt 308: }
309:
310:
311: /*
312: * return keyboard to normal mode
313: */
1.8 deraadt 314: void
1.1 deraadt 315: unraw()
316: {
1.2 deraadt 317: tcsetattr(0, TCSADRAIN, &defterm);
1.1 deraadt 318: }
319:
320: static jmp_buf promptbuf;
321:
322: /*
323: * Print string ``s'', then read a string
324: * in from the terminal. Handles signals & allows use of
325: * normal erase and kill characters.
326: */
1.8 deraadt 327: int
1.6 millert 328: prompt(s, p, sz)
1.1 deraadt 329: char *s;
1.8 deraadt 330: char *p;
1.6 millert 331: size_t sz;
1.1 deraadt 332: {
1.5 millert 333: register int c;
1.1 deraadt 334: register char *b = p;
335: sig_t oint, oquit;
336:
337: stoprompt = 0;
338: oint = signal(SIGINT, intprompt);
339: oquit = signal(SIGQUIT, SIG_IGN);
340: unraw();
341: printf("%s", s);
342: if (setjmp(promptbuf) == 0)
1.6 millert 343: while ((c = getchar()) != EOF && (*p = c) != '\n' && --sz > 0)
1.1 deraadt 344: p++;
345: *p = '\0';
346:
347: raw();
348: (void)signal(SIGINT, oint);
349: (void)signal(SIGQUIT, oquit);
350: return (stoprompt || p == b);
351: }
352:
353: /*
354: * Interrupt service routine during prompting
355: */
356: void
357: intprompt()
358: {
359:
360: (void)signal(SIGINT, SIG_IGN);
361: stoprompt = 1;
362: printf("\r\n");
363: longjmp(promptbuf, 1);
364: }
365:
366: /*
367: * ****TIPIN TIPIN****
368: */
1.8 deraadt 369: void
1.1 deraadt 370: tipin()
371: {
372: char gch, bol = 1;
373:
374: /*
375: * Kinda klugey here...
376: * check for scripting being turned on from the .tiprc file,
377: * but be careful about just using setscript(), as we may
378: * send a SIGEMT before tipout has a chance to set up catching
379: * it; so wait a second, then setscript()
380: */
381: if (boolean(value(SCRIPT))) {
382: sleep(1);
383: setscript();
384: }
385:
386: while (1) {
1.2 deraadt 387: gch = getchar()&STRIP_PAR;
1.1 deraadt 388: if ((gch == character(value(ESCAPE))) && bol) {
1.9 todd 389: if (!noesc) {
390: if (!(gch = escape()))
391: continue;
392: }
1.1 deraadt 393: } else if (!cumode && gch == character(value(RAISECHAR))) {
1.4 millert 394: setboolean(value(RAISE), !boolean(value(RAISE)));
1.1 deraadt 395: continue;
396: } else if (gch == '\r') {
397: bol = 1;
1.11 ! deraadt 398: parwrite(FD, &gch, 1);
1.1 deraadt 399: if (boolean(value(HALFDUPLEX)))
400: printf("\r\n");
401: continue;
402: } else if (!cumode && gch == character(value(FORCE)))
1.2 deraadt 403: gch = getchar()&STRIP_PAR;
1.1 deraadt 404: bol = any(gch, value(EOL));
405: if (boolean(value(RAISE)) && islower(gch))
406: gch = toupper(gch);
1.11 ! deraadt 407: parwrite(FD, &gch, 1);
1.1 deraadt 408: if (boolean(value(HALFDUPLEX)))
409: printf("%c", gch);
410: }
411: }
412:
413: extern esctable_t etable[];
414:
415: /*
416: * Escape handler --
417: * called on recognition of ``escapec'' at the beginning of a line
418: */
1.8 deraadt 419: int
1.1 deraadt 420: escape()
421: {
422: register char gch;
423: register esctable_t *p;
424: char c = character(value(ESCAPE));
425:
1.2 deraadt 426: gch = (getchar()&STRIP_PAR);
1.1 deraadt 427: for (p = etable; p->e_char; p++)
428: if (p->e_char == gch) {
429: if ((p->e_flags&PRIV) && uid)
430: continue;
431: printf("%s", ctrl(c));
432: (*p->e_func)(gch);
433: return (0);
434: }
435: /* ESCAPE ESCAPE forces ESCAPE */
436: if (c != gch)
1.11 ! deraadt 437: parwrite(FD, &c, 1);
1.1 deraadt 438: return (gch);
439: }
440:
1.8 deraadt 441: int
1.1 deraadt 442: speed(n)
443: int n;
444: {
445: register int *p;
446:
1.2 deraadt 447: for (p = rates; *p != -1; p++)
1.1 deraadt 448: if (*p == n)
1.2 deraadt 449: return n;
450: return 0;
1.1 deraadt 451: }
452:
1.8 deraadt 453: int
454: any(cc, p)
455: register int cc;
456: char *p;
1.1 deraadt 457: {
1.8 deraadt 458: char c = cc;
1.1 deraadt 459: while (p && *p)
460: if (*p++ == c)
461: return (1);
462: return (0);
463: }
464:
1.8 deraadt 465: int
1.1 deraadt 466: size(s)
467: register char *s;
468: {
469: register int i = 0;
470:
471: while (s && *s++)
472: i++;
473: return (i);
474: }
475:
476: char *
477: interp(s)
478: register char *s;
479: {
480: static char buf[256];
481: register char *p = buf, c, *q;
482:
1.8 deraadt 483: while ((c = *s++)) {
1.1 deraadt 484: for (q = "\nn\rr\tt\ff\033E\bb"; *q; q++)
485: if (*q++ == c) {
486: *p++ = '\\'; *p++ = *q;
487: goto next;
488: }
489: if (c < 040) {
490: *p++ = '^'; *p++ = c + 'A'-1;
491: } else if (c == 0177) {
492: *p++ = '^'; *p++ = '?';
493: } else
494: *p++ = c;
495: next:
496: ;
497: }
498: *p = '\0';
499: return (buf);
500: }
501:
502: char *
503: ctrl(c)
504: char c;
505: {
506: static char s[3];
507:
508: if (c < 040 || c == 0177) {
509: s[0] = '^';
510: s[1] = c == 0177 ? '?' : c+'A'-1;
511: s[2] = '\0';
512: } else {
513: s[0] = c;
514: s[1] = '\0';
515: }
516: return (s);
517: }
518:
519: /*
520: * Help command
521: */
1.8 deraadt 522: void
1.1 deraadt 523: help(c)
524: char c;
525: {
526: register esctable_t *p;
527:
528: printf("%c\r\n", c);
529: for (p = etable; p->e_char; p++) {
530: if ((p->e_flags&PRIV) && uid)
531: continue;
532: printf("%2s", ctrl(character(value(ESCAPE))));
533: printf("%-2s %c %s\r\n", ctrl(p->e_char),
534: p->e_flags&EXP ? '*': ' ', p->e_help);
535: }
536: }
537:
538: /*
539: * Set up the "remote" tty's state
540: */
1.8 deraadt 541: void
1.1 deraadt 542: ttysetup(speed)
543: int speed;
544: {
1.2 deraadt 545: struct termios cntrl;
1.1 deraadt 546:
1.2 deraadt 547: tcgetattr(FD, &cntrl);
548: cfsetospeed(&cntrl, speed);
549: cfsetispeed(&cntrl, speed);
550: cntrl.c_cflag &= ~(CSIZE|PARENB);
551: cntrl.c_cflag |= CS8;
1.5 millert 552: if (boolean(value(DC)))
553: cntrl.c_cflag |= CLOCAL;
1.2 deraadt 554: cntrl.c_iflag &= ~(ISTRIP|ICRNL);
555: cntrl.c_oflag &= ~OPOST;
556: cntrl.c_lflag &= ~(ICANON|ISIG|IEXTEN|ECHO);
557: cntrl.c_cc[VMIN] = 1;
558: cntrl.c_cc[VTIME] = 0;
1.1 deraadt 559: if (boolean(value(TAND)))
1.2 deraadt 560: cntrl.c_iflag |= IXOFF;
561: tcsetattr(FD, TCSAFLUSH, &cntrl);
1.1 deraadt 562: }
563:
564: /*
565: * Return "simple" name from a file name,
566: * strip leading directories.
567: */
568: char *
569: sname(s)
570: register char *s;
571: {
572: register char *p = s;
573:
574: while (*s)
575: if (*s++ == '/')
576: p = s;
577: return (p);
578: }
579:
580: static char partab[0200];
581:
582: /*
583: * Do a write to the remote machine with the correct parity.
584: * We are doing 8 bit wide output, so we just generate a character
585: * with the right parity and output it.
586: */
1.8 deraadt 587: void
1.11 ! deraadt 588: parwrite(fd, buf, n)
1.1 deraadt 589: int fd;
590: char *buf;
591: register int n;
592: {
593: register int i;
594: register char *bp;
595: extern int errno;
596:
597: bp = buf;
598: if (bits8 == 0)
599: for (i = 0; i < n; i++) {
600: *bp = partab[(*bp) & 0177];
601: bp++;
602: }
603: if (write(fd, buf, n) < 0) {
604: if (errno == EIO)
605: tipabort("Lost carrier.");
606: /* this is questionable */
607: perror("write");
608: }
609: }
610:
611: /*
612: * Build a parity table with appropriate high-order bit.
613: */
1.8 deraadt 614: void
1.1 deraadt 615: setparity(defparity)
616: char *defparity;
617: {
618: register int i, flip, clr, set;
619: char *parity;
1.4 millert 620: extern const unsigned char evenpartab[];
1.1 deraadt 621:
622: if (value(PARITY) == NOSTR)
623: value(PARITY) = defparity;
624: parity = value(PARITY);
625: if (equal(parity, "none")) {
626: bits8 = 1;
627: return;
628: }
629: bits8 = 0;
630: flip = 0;
631: clr = 0377;
632: set = 0;
633: if (equal(parity, "odd"))
634: flip = 0200; /* reverse bit 7 */
635: else if (equal(parity, "zero"))
636: clr = 0177; /* turn off bit 7 */
637: else if (equal(parity, "one"))
638: set = 0200; /* turn on bit 7 */
639: else if (!equal(parity, "even")) {
640: (void) fprintf(stderr, "%s: unknown parity value\r\n", parity);
641: (void) fflush(stderr);
642: }
643: for (i = 0; i < 0200; i++)
644: partab[i] = (evenpartab[i] ^ flip | set) & clr;
645: }