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