Annotation of src/usr.bin/tip/tip.c, Revision 1.20
1.20 ! millert 1: /* $OpenBSD: tip.c,v 1.19 2002/05/29 22:58:56 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.20 ! millert 43: static const char rcsid[] = "$OpenBSD: tip.c,v 1.19 2002/05/29 22:58:56 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);
215: term = defterm;
216: term.c_lflag &= ~(ICANON|IEXTEN|ECHO);
217: term.c_iflag &= ~(INPCK|ICRNL);
218: term.c_oflag &= ~OPOST;
219: term.c_cc[VMIN] = 1;
220: term.c_cc[VTIME] = 0;
221: defchars = term;
222: term.c_cc[VINTR] = term.c_cc[VQUIT] = term.c_cc[VSUSP] =
223: term.c_cc[VDSUSP] = term.c_cc[VDISCARD] =
224: term.c_cc[VLNEXT] = _POSIX_VDISABLE;
1.1 deraadt 225: raw();
226:
227: pipe(fildes); pipe(repdes);
228: (void)signal(SIGALRM, timeout);
229:
230: /*
231: * Everything's set up now:
232: * connection established (hardwired or dialup)
233: * line conditioned (baud rate, mode, etc.)
234: * internal data structures (variables)
235: * so, fork one process for local side and one for remote.
236: */
237: printf(cumode ? "Connected\r\n" : "\07connected\r\n");
1.8 deraadt 238: if ((pid = fork()))
1.1 deraadt 239: tipin();
240: else
241: tipout();
242: /*NOTREACHED*/
1.8 deraadt 243: exit(0);
1.1 deraadt 244: }
245:
246: void
247: cleanup()
248: {
249:
250: daemon_uid();
251: (void)uu_unlock(uucplock);
252: if (odisc)
253: ioctl(0, TIOCSETD, (char *)&odisc);
254: exit(0);
255: }
256:
257: /*
258: * Muck with user ID's. We are setuid to the owner of the lock
259: * directory when we start. user_uid() reverses real and effective
260: * ID's after startup, to run with the user's permissions.
261: * daemon_uid() switches back to the privileged uid for unlocking.
262: * Finally, to avoid running a shell with the wrong real uid,
263: * shell_uid() sets real and effective uid's to the user's real ID.
264: */
265: static int uidswapped;
266:
1.8 deraadt 267: void
1.1 deraadt 268: user_uid()
269: {
270: if (uidswapped == 0) {
271: seteuid(uid);
272: uidswapped = 1;
273: }
274: }
275:
1.8 deraadt 276: void
1.1 deraadt 277: daemon_uid()
278: {
279:
280: if (uidswapped) {
281: seteuid(euid);
282: uidswapped = 0;
283: }
284: }
285:
1.8 deraadt 286: void
1.1 deraadt 287: shell_uid()
288: {
1.7 deraadt 289: setegid(gid);
1.1 deraadt 290: seteuid(uid);
291: }
292:
293: /*
294: * put the controlling keyboard into raw mode
295: */
1.8 deraadt 296: void
1.1 deraadt 297: raw()
298: {
1.2 deraadt 299: tcsetattr(0, TCSADRAIN, &term);
1.1 deraadt 300: }
301:
302:
303: /*
304: * return keyboard to normal mode
305: */
1.8 deraadt 306: void
1.1 deraadt 307: unraw()
308: {
1.2 deraadt 309: tcsetattr(0, TCSADRAIN, &defterm);
1.1 deraadt 310: }
311:
312: static jmp_buf promptbuf;
313:
314: /*
315: * Print string ``s'', then read a string
316: * in from the terminal. Handles signals & allows use of
317: * normal erase and kill characters.
318: */
1.8 deraadt 319: int
1.6 millert 320: prompt(s, p, sz)
1.1 deraadt 321: char *s;
1.8 deraadt 322: char *p;
1.6 millert 323: size_t sz;
1.1 deraadt 324: {
1.15 millert 325: int c;
326: char *b = p;
1.1 deraadt 327: sig_t oint, oquit;
328:
329: stoprompt = 0;
330: oint = signal(SIGINT, intprompt);
331: oquit = signal(SIGQUIT, SIG_IGN);
332: unraw();
333: printf("%s", s);
334: if (setjmp(promptbuf) == 0)
1.6 millert 335: while ((c = getchar()) != EOF && (*p = c) != '\n' && --sz > 0)
1.1 deraadt 336: p++;
337: *p = '\0';
338:
339: raw();
340: (void)signal(SIGINT, oint);
341: (void)signal(SIGQUIT, oquit);
342: return (stoprompt || p == b);
343: }
344:
345: /*
346: * Interrupt service routine during prompting
347: */
348: void
349: intprompt()
350: {
351:
352: (void)signal(SIGINT, SIG_IGN);
353: stoprompt = 1;
354: printf("\r\n");
355: longjmp(promptbuf, 1);
356: }
357:
358: /*
359: * ****TIPIN TIPIN****
360: */
1.8 deraadt 361: void
1.1 deraadt 362: tipin()
363: {
1.16 deraadt 364: char bol = 1;
365: int gch;
1.17 deraadt 366: char ch;
1.1 deraadt 367:
368: /*
369: * Kinda klugey here...
370: * check for scripting being turned on from the .tiprc file,
371: * but be careful about just using setscript(), as we may
372: * send a SIGEMT before tipout has a chance to set up catching
373: * it; so wait a second, then setscript()
374: */
375: if (boolean(value(SCRIPT))) {
376: sleep(1);
377: setscript();
378: }
379:
380: while (1) {
1.2 deraadt 381: gch = getchar()&STRIP_PAR;
1.16 deraadt 382: /* XXX does not check for EOF */
1.1 deraadt 383: if ((gch == character(value(ESCAPE))) && bol) {
1.9 todd 384: if (!noesc) {
385: if (!(gch = escape()))
386: continue;
387: }
1.1 deraadt 388: } else if (!cumode && gch == character(value(RAISECHAR))) {
1.4 millert 389: setboolean(value(RAISE), !boolean(value(RAISE)));
1.1 deraadt 390: continue;
391: } else if (gch == '\r') {
392: bol = 1;
1.17 deraadt 393: ch = gch;
394: parwrite(FD, &ch, 1);
1.1 deraadt 395: if (boolean(value(HALFDUPLEX)))
396: printf("\r\n");
397: continue;
398: } else if (!cumode && gch == character(value(FORCE)))
1.2 deraadt 399: gch = getchar()&STRIP_PAR;
1.1 deraadt 400: bol = any(gch, value(EOL));
401: if (boolean(value(RAISE)) && islower(gch))
402: gch = toupper(gch);
1.17 deraadt 403: ch = gch;
404: parwrite(FD, &ch, 1);
1.1 deraadt 405: if (boolean(value(HALFDUPLEX)))
1.17 deraadt 406: printf("%c", ch);
1.1 deraadt 407: }
408: }
409:
410: extern esctable_t etable[];
411:
412: /*
413: * Escape handler --
414: * called on recognition of ``escapec'' at the beginning of a line
415: */
1.8 deraadt 416: int
1.1 deraadt 417: escape()
418: {
1.16 deraadt 419: int gch;
1.15 millert 420: esctable_t *p;
1.1 deraadt 421: char c = character(value(ESCAPE));
422:
1.2 deraadt 423: gch = (getchar()&STRIP_PAR);
1.16 deraadt 424: /* XXX does not check for EOF */
1.1 deraadt 425: for (p = etable; p->e_char; p++)
426: if (p->e_char == gch) {
427: if ((p->e_flags&PRIV) && uid)
428: continue;
429: printf("%s", ctrl(c));
430: (*p->e_func)(gch);
431: return (0);
432: }
433: /* ESCAPE ESCAPE forces ESCAPE */
434: if (c != gch)
1.11 deraadt 435: parwrite(FD, &c, 1);
1.1 deraadt 436: return (gch);
437: }
438:
1.8 deraadt 439: int
440: any(cc, p)
1.15 millert 441: int cc;
1.8 deraadt 442: char *p;
1.1 deraadt 443: {
1.8 deraadt 444: char c = cc;
1.1 deraadt 445: while (p && *p)
446: if (*p++ == c)
447: return (1);
448: return (0);
449: }
450:
1.8 deraadt 451: int
1.1 deraadt 452: size(s)
1.15 millert 453: char *s;
1.1 deraadt 454: {
1.15 millert 455: int i = 0;
1.1 deraadt 456:
457: while (s && *s++)
458: i++;
459: return (i);
460: }
461:
462: char *
463: interp(s)
1.15 millert 464: char *s;
1.1 deraadt 465: {
466: static char buf[256];
1.15 millert 467: char *p = buf, c, *q;
1.1 deraadt 468:
1.8 deraadt 469: while ((c = *s++)) {
1.1 deraadt 470: for (q = "\nn\rr\tt\ff\033E\bb"; *q; q++)
471: if (*q++ == c) {
472: *p++ = '\\'; *p++ = *q;
473: goto next;
474: }
475: if (c < 040) {
476: *p++ = '^'; *p++ = c + 'A'-1;
477: } else if (c == 0177) {
478: *p++ = '^'; *p++ = '?';
479: } else
480: *p++ = c;
481: next:
482: ;
483: }
484: *p = '\0';
485: return (buf);
486: }
487:
488: char *
489: ctrl(c)
490: char c;
491: {
492: static char s[3];
493:
494: if (c < 040 || c == 0177) {
495: s[0] = '^';
496: s[1] = c == 0177 ? '?' : c+'A'-1;
497: s[2] = '\0';
498: } else {
499: s[0] = c;
500: s[1] = '\0';
501: }
502: return (s);
503: }
504:
505: /*
506: * Help command
507: */
1.8 deraadt 508: void
1.1 deraadt 509: help(c)
510: char c;
511: {
1.15 millert 512: esctable_t *p;
1.1 deraadt 513:
514: printf("%c\r\n", c);
515: for (p = etable; p->e_char; p++) {
516: if ((p->e_flags&PRIV) && uid)
517: continue;
518: printf("%2s", ctrl(character(value(ESCAPE))));
519: printf("%-2s %c %s\r\n", ctrl(p->e_char),
520: p->e_flags&EXP ? '*': ' ', p->e_help);
521: }
522: }
523:
524: /*
525: * Set up the "remote" tty's state
526: */
1.19 millert 527: int
1.1 deraadt 528: ttysetup(speed)
529: int speed;
530: {
1.2 deraadt 531: struct termios cntrl;
1.1 deraadt 532:
1.19 millert 533: if (tcgetattr(FD, &cntrl))
534: return (-1);
535: cfsetspeed(&cntrl, speed);
1.2 deraadt 536: cntrl.c_cflag &= ~(CSIZE|PARENB);
537: cntrl.c_cflag |= CS8;
1.5 millert 538: if (boolean(value(DC)))
539: cntrl.c_cflag |= CLOCAL;
1.2 deraadt 540: cntrl.c_iflag &= ~(ISTRIP|ICRNL);
541: cntrl.c_oflag &= ~OPOST;
542: cntrl.c_lflag &= ~(ICANON|ISIG|IEXTEN|ECHO);
543: cntrl.c_cc[VMIN] = 1;
544: cntrl.c_cc[VTIME] = 0;
1.1 deraadt 545: if (boolean(value(TAND)))
1.2 deraadt 546: cntrl.c_iflag |= IXOFF;
1.19 millert 547: return (tcsetattr(FD, TCSAFLUSH, &cntrl));
1.1 deraadt 548: }
549:
550: static char partab[0200];
551:
552: /*
553: * Do a write to the remote machine with the correct parity.
554: * We are doing 8 bit wide output, so we just generate a character
555: * with the right parity and output it.
556: */
1.8 deraadt 557: void
1.11 deraadt 558: parwrite(fd, buf, n)
1.1 deraadt 559: int fd;
560: char *buf;
1.15 millert 561: int n;
1.1 deraadt 562: {
1.15 millert 563: int i;
564: char *bp;
1.1 deraadt 565:
566: bp = buf;
567: if (bits8 == 0)
568: for (i = 0; i < n; i++) {
569: *bp = partab[(*bp) & 0177];
570: bp++;
571: }
572: if (write(fd, buf, n) < 0) {
573: if (errno == EIO)
574: tipabort("Lost carrier.");
575: /* this is questionable */
576: perror("write");
577: }
578: }
579:
580: /*
581: * Build a parity table with appropriate high-order bit.
582: */
1.8 deraadt 583: void
1.1 deraadt 584: setparity(defparity)
585: char *defparity;
586: {
1.15 millert 587: int i, flip, clr, set;
1.1 deraadt 588: char *parity;
1.4 millert 589: extern const unsigned char evenpartab[];
1.1 deraadt 590:
591: if (value(PARITY) == NOSTR)
592: value(PARITY) = defparity;
593: parity = value(PARITY);
594: if (equal(parity, "none")) {
595: bits8 = 1;
596: return;
597: }
598: bits8 = 0;
599: flip = 0;
600: clr = 0377;
601: set = 0;
602: if (equal(parity, "odd"))
603: flip = 0200; /* reverse bit 7 */
604: else if (equal(parity, "zero"))
605: clr = 0177; /* turn off bit 7 */
606: else if (equal(parity, "one"))
607: set = 0200; /* turn on bit 7 */
608: else if (!equal(parity, "even")) {
609: (void) fprintf(stderr, "%s: unknown parity value\r\n", parity);
610: (void) fflush(stderr);
611: }
612: for (i = 0; i < 0200; i++)
1.18 hugh 613: partab[i] = ((evenpartab[i] ^ flip) | set) & clr;
1.1 deraadt 614: }