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