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