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