Annotation of src/usr.bin/tip/tip.c, Revision 1.27
1.27 ! deraadt 1: /* $OpenBSD: tip.c,v 1.26 2006/03/17 14:43:06 moritz 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.27 ! deraadt 43: static const char rcsid[] = "$OpenBSD: tip.c,v 1.26 2006/03/17 14:43:06 moritz 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);
228:
229: /*
230: * Everything's set up now:
231: * connection established (hardwired or dialup)
232: * line conditioned (baud rate, mode, etc.)
233: * internal data structures (variables)
234: * so, fork one process for local side and one for remote.
235: */
236: printf(cumode ? "Connected\r\n" : "\07connected\r\n");
1.23 deraadt 237: tipin_pid = getpid();
238: if ((tipout_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
1.23 deraadt 247: cleanup(int signo)
1.1 deraadt 248: {
249: daemon_uid();
250: (void)uu_unlock(uucplock);
251: if (odisc)
1.27 ! deraadt 252: ioctl(0, TIOCSETD, &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: */
1.27 ! deraadt 350: /*ARGSUSED*/
1.26 moritz 351: static void
352: intprompt(int signo)
1.1 deraadt 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.26 moritz 363: static void
1.25 deraadt 364: tipin(void)
1.1 deraadt 365: {
1.27 ! deraadt 366: int bol = 1;
1.16 deraadt 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.26 moritz 418: static 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.26 moritz 506: help(int 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.27 ! deraadt 555: parwrite(int fd, char *buf, size_t 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: }