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