Annotation of src/usr.bin/tip/tip.c, Revision 1.26
1.26 ! moritz 1: /* $OpenBSD: tip.c,v 1.25 2006/03/16 19:32:46 deraadt 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.26 ! moritz 43: static const char rcsid[] = "$OpenBSD: tip.c,v 1.25 2006/03/16 19:32:46 deraadt 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.25 deraadt 65: char *system = 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] != '-')
89: system = argv[1];
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:
112: if (system == NOSTR)
113: goto notnumber;
114: if (isalpha(*system))
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: */
121: if (strlen(system) > 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: }
126: strncpy( PNbuf, system, sizeof PNbuf - 1 );
127: for (p = system; *p; p++)
128: *p = '\0';
129: PN = PNbuf;
1.8 deraadt 130: (void)snprintf(sbuf, sizeof(sbuf), "tip%ld", BR);
1.1 deraadt 131: system = sbuf;
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:
140: if ((i = hunt(system)) == 0) {
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)
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: */
1.26 ! moritz 350: static void
! 351: intprompt(int signo)
1.1 deraadt 352: {
353: (void)signal(SIGINT, SIG_IGN);
354: stoprompt = 1;
355: printf("\r\n");
356: longjmp(promptbuf, 1);
357: }
358:
359: /*
360: * ****TIPIN TIPIN****
361: */
1.26 ! moritz 362: static void
1.25 deraadt 363: tipin(void)
1.1 deraadt 364: {
1.16 deraadt 365: char bol = 1;
366: int gch;
1.17 deraadt 367: char ch;
1.1 deraadt 368:
369: /*
370: * Kinda klugey here...
371: * check for scripting being turned on from the .tiprc file,
372: * but be careful about just using setscript(), as we may
373: * send a SIGEMT before tipout has a chance to set up catching
374: * it; so wait a second, then setscript()
375: */
376: if (boolean(value(SCRIPT))) {
377: sleep(1);
378: setscript();
379: }
380:
381: while (1) {
1.2 deraadt 382: gch = getchar()&STRIP_PAR;
1.16 deraadt 383: /* XXX does not check for EOF */
1.1 deraadt 384: if ((gch == character(value(ESCAPE))) && bol) {
1.9 todd 385: if (!noesc) {
386: if (!(gch = escape()))
387: continue;
388: }
1.1 deraadt 389: } else if (!cumode && gch == character(value(RAISECHAR))) {
1.4 millert 390: setboolean(value(RAISE), !boolean(value(RAISE)));
1.1 deraadt 391: continue;
392: } else if (gch == '\r') {
393: bol = 1;
1.17 deraadt 394: ch = gch;
395: parwrite(FD, &ch, 1);
1.1 deraadt 396: if (boolean(value(HALFDUPLEX)))
397: printf("\r\n");
398: continue;
399: } else if (!cumode && gch == character(value(FORCE)))
1.2 deraadt 400: gch = getchar()&STRIP_PAR;
1.1 deraadt 401: bol = any(gch, value(EOL));
402: if (boolean(value(RAISE)) && islower(gch))
403: gch = toupper(gch);
1.17 deraadt 404: ch = gch;
405: parwrite(FD, &ch, 1);
1.1 deraadt 406: if (boolean(value(HALFDUPLEX)))
1.17 deraadt 407: printf("%c", ch);
1.1 deraadt 408: }
409: }
410:
411: extern esctable_t etable[];
412:
413: /*
414: * Escape handler --
415: * called on recognition of ``escapec'' at the beginning of a line
416: */
1.26 ! moritz 417: static int
1.25 deraadt 418: escape(void)
1.1 deraadt 419: {
1.16 deraadt 420: int gch;
1.15 millert 421: esctable_t *p;
1.1 deraadt 422: char c = character(value(ESCAPE));
423:
1.2 deraadt 424: gch = (getchar()&STRIP_PAR);
1.16 deraadt 425: /* XXX does not check for EOF */
1.1 deraadt 426: for (p = etable; p->e_char; p++)
427: if (p->e_char == gch) {
428: if ((p->e_flags&PRIV) && uid)
429: continue;
430: printf("%s", ctrl(c));
431: (*p->e_func)(gch);
432: return (0);
433: }
434: /* ESCAPE ESCAPE forces ESCAPE */
435: if (c != gch)
1.11 deraadt 436: parwrite(FD, &c, 1);
1.1 deraadt 437: return (gch);
438: }
439:
1.8 deraadt 440: int
1.25 deraadt 441: any(int cc, char *p)
1.1 deraadt 442: {
1.8 deraadt 443: char c = cc;
1.1 deraadt 444: while (p && *p)
445: if (*p++ == c)
446: return (1);
447: return (0);
448: }
449:
1.8 deraadt 450: int
1.25 deraadt 451: size(char *s)
1.1 deraadt 452: {
1.15 millert 453: int i = 0;
1.1 deraadt 454:
455: while (s && *s++)
456: i++;
457: return (i);
458: }
459:
460: char *
1.25 deraadt 461: interp(char *s)
1.1 deraadt 462: {
463: static char buf[256];
1.15 millert 464: char *p = buf, c, *q;
1.1 deraadt 465:
1.8 deraadt 466: while ((c = *s++)) {
1.1 deraadt 467: for (q = "\nn\rr\tt\ff\033E\bb"; *q; q++)
468: if (*q++ == c) {
469: *p++ = '\\'; *p++ = *q;
470: goto next;
471: }
472: if (c < 040) {
473: *p++ = '^'; *p++ = c + 'A'-1;
474: } else if (c == 0177) {
475: *p++ = '^'; *p++ = '?';
476: } else
477: *p++ = c;
478: next:
479: ;
480: }
481: *p = '\0';
482: return (buf);
483: }
484:
485: char *
1.25 deraadt 486: ctrl(char c)
1.1 deraadt 487: {
488: static char s[3];
489:
490: if (c < 040 || c == 0177) {
491: s[0] = '^';
492: s[1] = c == 0177 ? '?' : c+'A'-1;
493: s[2] = '\0';
494: } else {
495: s[0] = c;
496: s[1] = '\0';
497: }
498: return (s);
499: }
500:
501: /*
502: * Help command
503: */
1.8 deraadt 504: void
1.26 ! moritz 505: help(int c)
1.1 deraadt 506: {
1.15 millert 507: esctable_t *p;
1.1 deraadt 508:
509: printf("%c\r\n", c);
510: for (p = etable; p->e_char; p++) {
511: if ((p->e_flags&PRIV) && uid)
512: continue;
513: printf("%2s", ctrl(character(value(ESCAPE))));
514: printf("%-2s %c %s\r\n", ctrl(p->e_char),
515: p->e_flags&EXP ? '*': ' ', p->e_help);
516: }
517: }
518:
519: /*
520: * Set up the "remote" tty's state
521: */
1.19 millert 522: int
1.25 deraadt 523: ttysetup(int speed)
1.1 deraadt 524: {
1.2 deraadt 525: struct termios cntrl;
1.1 deraadt 526:
1.19 millert 527: if (tcgetattr(FD, &cntrl))
528: return (-1);
529: cfsetspeed(&cntrl, speed);
1.2 deraadt 530: cntrl.c_cflag &= ~(CSIZE|PARENB);
531: cntrl.c_cflag |= CS8;
1.5 millert 532: if (boolean(value(DC)))
533: cntrl.c_cflag |= CLOCAL;
1.21 millert 534: if (boolean(value(HARDWAREFLOW)))
1.25 deraadt 535: cntrl.c_cflag |= CRTSCTS;
1.2 deraadt 536: cntrl.c_iflag &= ~(ISTRIP|ICRNL);
537: cntrl.c_oflag &= ~OPOST;
538: cntrl.c_lflag &= ~(ICANON|ISIG|IEXTEN|ECHO);
539: cntrl.c_cc[VMIN] = 1;
540: cntrl.c_cc[VTIME] = 0;
1.1 deraadt 541: if (boolean(value(TAND)))
1.2 deraadt 542: cntrl.c_iflag |= IXOFF;
1.19 millert 543: return (tcsetattr(FD, TCSAFLUSH, &cntrl));
1.1 deraadt 544: }
545:
546: static char partab[0200];
547:
548: /*
549: * Do a write to the remote machine with the correct parity.
550: * We are doing 8 bit wide output, so we just generate a character
551: * with the right parity and output it.
552: */
1.8 deraadt 553: void
1.25 deraadt 554: parwrite(int fd, char *buf, int n)
1.1 deraadt 555: {
1.15 millert 556: int i;
557: char *bp;
1.1 deraadt 558:
559: bp = buf;
560: if (bits8 == 0)
561: for (i = 0; i < n; i++) {
562: *bp = partab[(*bp) & 0177];
563: bp++;
564: }
565: if (write(fd, buf, n) < 0) {
566: if (errno == EIO)
567: tipabort("Lost carrier.");
568: /* this is questionable */
569: perror("write");
570: }
571: }
572:
573: /*
574: * Build a parity table with appropriate high-order bit.
575: */
1.8 deraadt 576: void
1.25 deraadt 577: setparity(char *defparity)
1.1 deraadt 578: {
1.15 millert 579: int i, flip, clr, set;
1.1 deraadt 580: char *parity;
1.4 millert 581: extern const unsigned char evenpartab[];
1.1 deraadt 582:
583: if (value(PARITY) == NOSTR)
584: value(PARITY) = defparity;
585: parity = value(PARITY);
586: if (equal(parity, "none")) {
587: bits8 = 1;
588: return;
589: }
590: bits8 = 0;
591: flip = 0;
592: clr = 0377;
593: set = 0;
594: if (equal(parity, "odd"))
595: flip = 0200; /* reverse bit 7 */
596: else if (equal(parity, "zero"))
597: clr = 0177; /* turn off bit 7 */
598: else if (equal(parity, "one"))
599: set = 0200; /* turn on bit 7 */
600: else if (!equal(parity, "even")) {
601: (void) fprintf(stderr, "%s: unknown parity value\r\n", parity);
602: (void) fflush(stderr);
603: }
604: for (i = 0; i < 0200; i++)
1.18 hugh 605: partab[i] = ((evenpartab[i] ^ flip) | set) & clr;
1.1 deraadt 606: }