tip.c revision 29574
1/* 2 * Copyright (c) 1983, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34#ifndef lint 35static const char copyright[] = 36"@(#) Copyright (c) 1983, 1993\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38#endif /* not lint */ 39 40#ifndef lint 41#if 0 42static char sccsid[] = "@(#)tip.c 8.1 (Berkeley) 6/6/93"; 43#endif 44static const char rcsid[] = 45 "$Id: tip.c,v 1.6 1997/08/26 10:25:19 eivind Exp $"; 46#endif /* not lint */ 47 48/* 49 Forward declarations 50*/ 51void ttysetup (int speed); 52 53/* 54 * tip - UNIX link to other systems 55 * tip [-v] [-speed] system-name 56 * or 57 * cu phone-number [-s speed] [-l line] [-a acu] 58 */ 59 60#include <err.h> 61#include <sys/types.h> 62#include <libutil.h> 63#include "tipconf.h" 64#include "tip.h" 65#include "pathnames.h" 66 67/* 68 * Baud rate mapping table 69 */ 70#if !HAVE_TERMIOS 71CONST int bauds[] = { 72 0, 50, 75, 110, 134, 150, 200, 300, 600, 73 1200, 1800, 2400, 4800, 9600, 19200, 38400, 57600, 115200, -1 74}; 75#endif 76 77#if !HAVE_TERMIOS 78int disc = OTTYDISC; /* tip normally runs this way */ 79#endif 80 81void intprompt(); 82void timeout(); 83void cleanup(); 84void tipdone(); 85char *sname(); 86char PNbuf[256]; /* This limits the size of a number */ 87 88static void usage __P((void)); 89void setparity __P((char *)); 90void pwrite __P((int, char *, int)); 91char escape __P((void)); 92void tipin __P((void)); 93int prompt __P((char *, char *, size_t)); 94void unraw __P((void)); 95void shell_uid __P((void)); 96void daemon_uid __P((void)); 97void user_uid __P((void)); 98int speed __P((int)); 99 100void 101main(argc, argv) 102 char *argv[]; 103{ 104 char *system = NOSTR; 105 register int i; 106 register char *p; 107 char sbuf[12]; 108 109 gid = getgid(); 110 egid = getegid(); 111 uid = getuid(); 112 euid = geteuid(); 113 114#if INCLUDE_CU_INTERFACE 115 if (equal(sname(argv[0]), "cu")) { 116 cumode = 1; 117 cumain(argc, argv); 118 goto cucommon; 119 } 120#endif /* INCLUDE_CU_INTERFACE */ 121 122 if (argc > 4) 123 usage(); 124 if (!isatty(0)) 125 errx(1, "must be interactive"); 126 127 for (; argc > 1; argv++, argc--) { 128 if (argv[1][0] != '-') 129 system = argv[1]; 130 else switch (argv[1][1]) { 131 132 case 'v': 133 vflag++; 134 break; 135 136 case '0': case '1': case '2': case '3': case '4': 137 case '5': case '6': case '7': case '8': case '9': 138 BR = atoi(&argv[1][1]); 139 break; 140 141 default: 142 warnx("%s, unknown option", argv[1]); 143 break; 144 } 145 } 146 147 if (system == NOSTR) 148 goto notnumber; 149 if (isalpha(*system)) 150 goto notnumber; 151 /* 152 * System name is really a phone number... 153 * Copy the number then stomp on the original (in case the number 154 * is private, we don't want 'ps' or 'w' to find it). 155 */ 156 if (strlen(system) > sizeof PNbuf - 1) 157 errx(1, "phone number too long (max = %d bytes)", sizeof PNbuf - 1); 158 strncpy( PNbuf, system, sizeof PNbuf - 1 ); 159 for (p = system; *p; p++) 160 *p = '\0'; 161 PN = PNbuf; 162 (void)sprintf(sbuf, "tip%ld", BR); 163 system = sbuf; 164 165notnumber: 166 (void)signal(SIGINT, cleanup); 167 (void)signal(SIGQUIT, cleanup); 168 (void)signal(SIGHUP, cleanup); 169 (void)signal(SIGTERM, cleanup); 170 (void)signal(SIGUSR1, tipdone); 171 172 if ((i = hunt(system)) == 0) { 173 printf("all ports busy\n"); 174 exit(3); 175 } 176 if (i == -1) { 177 printf("link down\n"); 178 (void)uu_unlock(uucplock); 179 exit(3); 180 } 181 setbuf(stdout, NULL); 182 loginit(); 183 184 /* 185 * Kludge, their's no easy way to get the initialization 186 * in the right order, so force it here 187 */ 188 if ((PH = getenv("PHONES")) == NOSTR) 189 PH = _PATH_PHONES; 190 vinit(); /* init variables */ 191 setparity("even"); /* set the parity table */ 192 if ((i = speed(number(value(BAUDRATE)))) == 0) { 193 printf("tip: bad baud rate %d\n", number(value(BAUDRATE))); 194 (void)uu_unlock(uucplock); 195 exit(3); 196 } 197 198 /* 199 * Now that we have the logfile and the ACU open 200 * return to the real uid and gid. These things will 201 * be closed on exit. Swap real and effective uid's 202 * so we can get the original permissions back 203 * for removing the uucp lock. 204 */ 205 user_uid(); 206 207 /* 208 * Hardwired connections require the 209 * line speed set before they make any transmissions 210 * (this is particularly true of things like a DF03-AC) 211 */ 212 if (HW) 213 ttysetup(i); 214 if ((p = connect())) { 215 printf("\07%s\n[EOT]\n", p); 216 daemon_uid(); 217 (void)uu_unlock(uucplock); 218 exit(1); 219 } 220 if (!HW) 221 ttysetup(i); 222/* cucommon:*/ 223 /* 224 * From here down the code is shared with 225 * the "cu" version of tip. 226 */ 227 228#if HAVE_TERMIOS 229 tcgetattr (0, &otermios); 230 ctermios = otermios; 231#ifndef _POSIX_SOURCE 232 ctermios.c_iflag = (IMAXBEL|IXANY|ISTRIP|IXON|BRKINT); 233 ctermios.c_lflag = (PENDIN|IEXTEN|ISIG|ECHOCTL|ECHOE|ECHOKE); 234#else 235 ctermios.c_iflag = (ISTRIP|IXON|BRKINT); 236 ctermios.c_lflag = (PENDIN|IEXTEN|ISIG|ECHOE); 237#endif 238 ctermios.c_cflag = (CLOCAL|HUPCL|CREAD|CS8); 239 ctermios.c_cc[VINTR] = ctermios.c_cc[VQUIT] = -1; 240 ctermios.c_cc[VSUSP] = ctermios.c_cc[VDSUSP] = ctermios.c_cc[VDISCARD] = 241 ctermios.c_cc[VLNEXT] = -1; 242#else /* HAVE_TERMIOS */ 243 ioctl(0, TIOCGETP, (char *)&defarg); 244 ioctl(0, TIOCGETC, (char *)&defchars); 245 ioctl(0, TIOCGLTC, (char *)&deflchars); 246 ioctl(0, TIOCGETD, (char *)&odisc); 247 arg = defarg; 248 arg.sg_flags = ANYP | CBREAK; 249 tchars = defchars; 250 tchars.t_intrc = tchars.t_quitc = -1; 251 ltchars = deflchars; 252 ltchars.t_suspc = ltchars.t_dsuspc = ltchars.t_flushc 253 = ltchars.t_lnextc = -1; 254#endif /* HAVE_TERMIOS */ 255 raw(); 256 257 pipe(fildes); pipe(repdes); 258 (void)signal(SIGALRM, timeout); 259 260 /* 261 * Everything's set up now: 262 * connection established (hardwired or dialup) 263 * line conditioned (baud rate, mode, etc.) 264 * internal data structures (variables) 265 * so, fork one process for local side and one for remote. 266 */ 267 printf(cumode ? "Connected\r\n" : "\07connected\r\n"); 268 269 if (LI != NOSTR && tiplink (LI, 0) != 0) { 270 tipabort ("login failed"); 271 } 272 273 if ((pid = fork())) 274 tipin(); 275 else 276 tipout(); 277 /*NOTREACHED*/ 278} 279 280static void 281usage() 282{ 283 fprintf(stderr, "usage: tip [-v] [-speed] [system-name]\n"); 284 exit(1); 285} 286 287void 288cleanup() 289{ 290 291 daemon_uid(); 292 (void)uu_unlock(uucplock); 293#if !HAVE_TERMIOS 294 if (odisc) 295 ioctl(0, TIOCSETD, (char *)&odisc); 296#endif 297 exit(0); 298} 299 300void 301tipdone() 302{ 303 tipabort("Hangup."); 304} 305/* 306 * Muck with user ID's. We are setuid to the owner of the lock 307 * directory when we start. user_uid() reverses real and effective 308 * ID's after startup, to run with the user's permissions. 309 * daemon_uid() switches back to the privileged uid for unlocking. 310 * Finally, to avoid running a shell with the wrong real uid, 311 * shell_uid() sets real and effective uid's to the user's real ID. 312 */ 313static int uidswapped; 314 315void 316user_uid() 317{ 318 if (uidswapped == 0) { 319 seteuid(uid); 320 uidswapped = 1; 321 } 322} 323 324void 325daemon_uid() 326{ 327 if (uidswapped) { 328 seteuid(euid); 329 uidswapped = 0; 330 } 331} 332 333void 334shell_uid() 335{ 336 seteuid(uid); 337} 338 339/* 340 * put the controlling keyboard into raw mode 341 */ 342void 343raw () 344{ 345#if HAVE_TERMIOS 346 tcsetattr (0, TCSANOW, &ctermios); 347#else /* HAVE_TERMIOS */ 348 349 ioctl(0, TIOCSETP, &arg); 350 ioctl(0, TIOCSETC, &tchars); 351 ioctl(0, TIOCSLTC, <chars); 352 ioctl(0, TIOCSETD, (char *)&disc); 353#endif /* HAVE_TERMIOS */ 354} 355 356 357/* 358 * return keyboard to normal mode 359 */ 360void 361unraw() 362{ 363#if HAVE_TERMIOS 364 tcsetattr (0, TCSANOW, &otermios); 365#else /* HAVE_TERMIOS */ 366 367 ioctl(0, TIOCSETD, (char *)&odisc); 368 ioctl(0, TIOCSETP, (char *)&defarg); 369 ioctl(0, TIOCSETC, (char *)&defchars); 370 ioctl(0, TIOCSLTC, (char *)&deflchars); 371#endif /* HAVE_TERMIOS */ 372} 373 374static jmp_buf promptbuf; 375 376/* 377 * Print string ``s'', then read a string 378 * in from the terminal. Handles signals & allows use of 379 * normal erase and kill characters. 380 */ 381int 382prompt(s, p, sz) 383 char *s; 384 register char *p; 385 size_t sz; 386{ 387 register char *b = p; 388 sig_t oint, oquit; 389 390 stoprompt = 0; 391 oint = signal(SIGINT, intprompt); 392 oquit = signal(SIGQUIT, SIG_IGN); 393 unraw(); 394 printf("%s", s); 395 if (setjmp(promptbuf) == 0) 396 while ((*p = getchar()) != EOF && *p != '\n' && --sz > 0) 397 p++; 398 *p = '\0'; 399 400 raw(); 401 (void)signal(SIGINT, oint); 402 (void)signal(SIGQUIT, oquit); 403 return (stoprompt || p == b); 404} 405 406/* 407 * Interrupt service routine during prompting 408 */ 409void 410intprompt() 411{ 412 413 (void)signal(SIGINT, SIG_IGN); 414 stoprompt = 1; 415 printf("\r\n"); 416 longjmp(promptbuf, 1); 417} 418 419/* 420 * ****TIPIN TIPIN**** 421 */ 422void 423tipin() 424{ 425 int i; 426 char gch, bol = 1; 427 428 /* 429 * Kinda klugey here... 430 * check for scripting being turned on from the .tiprc file, 431 * but be careful about just using setscript(), as we may 432 * send a SIGEMT before tipout has a chance to set up catching 433 * it; so wait a second, then setscript() 434 */ 435 if (boolean(value(SCRIPT))) { 436 sleep(1); 437 setscript(); 438 } 439 440 while (1) { 441 i = getchar(); 442 if (i == EOF) 443 break; 444 gch = i&0177; 445 if ((gch == character(value(ESCAPE))) && bol) { 446 if (!(gch = escape())) 447 continue; 448 } else if (!cumode && gch == character(value(RAISECHAR))) { 449 boolean(value(RAISE)) = !boolean(value(RAISE)); 450 continue; 451 } else if (gch == '\r') { 452 bol = 1; 453 pwrite(FD, &gch, 1); 454 if (boolean(value(HALFDUPLEX))) 455 printf("\r\n"); 456 continue; 457 } else if (!cumode && gch == character(value(FORCE))) { 458 i = getchar(); 459 if (i == EOF) 460 break; 461 gch = i & 0177; 462 } 463 bol = any(gch, value(EOL)); 464 if (boolean(value(RAISE)) && islower(gch)) 465 gch = toupper(gch); 466 pwrite(FD, &gch, 1); 467 if (boolean(value(HALFDUPLEX))) 468 printf("%c", gch); 469 } 470} 471 472extern esctable_t etable[]; 473 474/* 475 * Escape handler -- 476 * called on recognition of ``escapec'' at the beginning of a line 477 */ 478char 479escape() 480{ 481 register char gch; 482 register esctable_t *p; 483 char c = character(value(ESCAPE)); 484 int i; 485 486 i = getchar(); 487 if (i == EOF) 488 return 0; 489 gch = (i&0177); 490 for (p = etable; p->e_char; p++) 491 if (p->e_char == gch) { 492 if ((p->e_flags&PRIV) && uid) 493 continue; 494 printf("%s", ctrl(c)); 495 (*p->e_func)(gch); 496 return (0); 497 } 498 /* ESCAPE ESCAPE forces ESCAPE */ 499 if (c != gch) 500 pwrite(FD, &c, 1); 501 return (gch); 502} 503 504int 505speed(n) 506 int n; 507{ 508#if HAVE_TERMIOS 509 return (n); 510#else 511 register CONST int *p; 512 513 for (p = bauds; *p != -1; p++) 514 if (*p == n) 515 return (p - bauds); 516 return (NULL); 517#endif 518} 519 520int 521any(c, p) 522 register char c, *p; 523{ 524 while (p && *p) 525 if (*p++ == c) 526 return (1); 527 return (0); 528} 529 530int 531size(s) 532 register char *s; 533{ 534 register int i = 0; 535 536 while (s && *s++) 537 i++; 538 return (i); 539} 540 541char * 542interp(s) 543 register char *s; 544{ 545 static char buf[256]; 546 register char *p = buf, c, *q; 547 548 while ((c = *s++)) { 549 for (q = "\nn\rr\tt\ff\033E\bb"; *q; q++) 550 if (*q++ == c) { 551 *p++ = '\\'; *p++ = *q; 552 goto next; 553 } 554 if (c < 040) { 555 *p++ = '^'; *p++ = c + 'A'-1; 556 } else if (c == 0177) { 557 *p++ = '^'; *p++ = '?'; 558 } else 559 *p++ = c; 560 next: 561 ; 562 } 563 *p = '\0'; 564 return (buf); 565} 566 567char * 568ctrl(c) 569 char c; 570{ 571 static char s[3]; 572 573 if (c < 040 || c == 0177) { 574 s[0] = '^'; 575 s[1] = c == 0177 ? '?' : c+'A'-1; 576 s[2] = '\0'; 577 } else { 578 s[0] = c; 579 s[1] = '\0'; 580 } 581 return (s); 582} 583 584/* 585 * Help command 586 */ 587void 588help(c) 589 char c; 590{ 591 register esctable_t *p; 592 593 printf("%c\r\n", c); 594 for (p = etable; p->e_char; p++) { 595 if ((p->e_flags&PRIV) && uid) 596 continue; 597 printf("%2s", ctrl(character(value(ESCAPE)))); 598 printf("%-2s %c %s\r\n", ctrl(p->e_char), 599 p->e_flags&EXP ? '*': ' ', p->e_help); 600 } 601} 602 603/* 604 * Set up the "remote" tty's state 605 */ 606void 607ttysetup (int speed) 608{ 609#if HAVE_TERMIOS 610 struct termios termios; 611 tcgetattr (FD, &termios); 612 if (boolean(value(TAND))) 613 termios.c_iflag = IXOFF; 614 else 615 termios.c_iflag = 0; 616#ifndef _POSIX_SOURCE 617 termios.c_lflag = (PENDIN|ECHOKE|ECHOE); 618#else 619 termios.c_lflag = (PENDIN|ECHOE); 620#endif 621 termios.c_cflag = (CLOCAL|HUPCL|CREAD|CS8); 622 termios.c_ispeed = termios.c_ospeed = speed; 623 tcsetattr (FD, TCSANOW, &termios); 624#else /* HAVE_TERMIOS */ 625 unsigned bits = LDECCTQ; 626 627 arg.sg_ispeed = arg.sg_ospeed = speed; 628 arg.sg_flags = RAW; 629 if (boolean(value(TAND))) 630 arg.sg_flags |= TANDEM; 631 ioctl(FD, TIOCSETP, (char *)&arg); 632 ioctl(FD, TIOCLBIS, (char *)&bits); 633#endif /* HAVE_TERMIOS */ 634} 635 636/* 637 * Return "simple" name from a file name, 638 * strip leading directories. 639 */ 640char * 641sname(s) 642 register char *s; 643{ 644 register char *p = s; 645 646 while (*s) 647 if (*s++ == '/') 648 p = s; 649 return (p); 650} 651 652static char partab[0200]; 653static int bits8; 654 655/* 656 * Do a write to the remote machine with the correct parity. 657 * We are doing 8 bit wide output, so we just generate a character 658 * with the right parity and output it. 659 */ 660void 661pwrite(fd, buf, n) 662 int fd; 663 char *buf; 664 register int n; 665{ 666 register int i; 667 register char *bp; 668 extern int errno; 669 670 bp = buf; 671 if (bits8 == 0) 672 for (i = 0; i < n; i++) { 673 *bp = partab[(*bp) & 0177]; 674 bp++; 675 } 676 if (write(fd, buf, n) < 0) { 677 if (errno == EIO) 678 tipabort("Lost carrier."); 679 if (errno == ENODEV) 680 tipabort("tty not available."); 681 tipabort("Something wrong..."); 682 } 683} 684 685/* 686 * Build a parity table with appropriate high-order bit. 687 */ 688void 689setparity(defparity) 690 char *defparity; 691{ 692 register int i, flip, clr, set; 693 char *parity; 694 extern char evenpartab[]; 695 696 if (value(PARITY) == NOSTR) 697 value(PARITY) = defparity; 698 parity = value(PARITY); 699 if (equal(parity, "none")) { 700 bits8 = 1; 701 return; 702 } 703 bits8 = 0; 704 flip = 0; 705 clr = 0377; 706 set = 0; 707 if (equal(parity, "odd")) 708 flip = 0200; /* reverse bit 7 */ 709 else if (equal(parity, "zero")) 710 clr = 0177; /* turn off bit 7 */ 711 else if (equal(parity, "one")) 712 set = 0200; /* turn on bit 7 */ 713 else if (!equal(parity, "even")) { 714 (void) fprintf(stderr, "%s: unknown parity value\r\n", parity); 715 (void) fflush(stderr); 716 } 717 for (i = 0; i < 0200; i++) 718 partab[i] = (evenpartab[i] ^ flip) | (set & clr); 719} 720