tip.c revision 50477
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 "$FreeBSD: head/usr.bin/tip/tip/tip.c 50477 1999-08-28 01:08:13Z peter $"; 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 xpwrite __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)snprintf(sbuf, sizeof(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 setegid(gid); 337 seteuid(uid); 338} 339 340/* 341 * put the controlling keyboard into raw mode 342 */ 343void 344raw () 345{ 346#if HAVE_TERMIOS 347 tcsetattr (0, TCSANOW, &ctermios); 348#else /* HAVE_TERMIOS */ 349 350 ioctl(0, TIOCSETP, &arg); 351 ioctl(0, TIOCSETC, &tchars); 352 ioctl(0, TIOCSLTC, <chars); 353 ioctl(0, TIOCSETD, (char *)&disc); 354#endif /* HAVE_TERMIOS */ 355} 356 357 358/* 359 * return keyboard to normal mode 360 */ 361void 362unraw() 363{ 364#if HAVE_TERMIOS 365 tcsetattr (0, TCSANOW, &otermios); 366#else /* HAVE_TERMIOS */ 367 368 ioctl(0, TIOCSETD, (char *)&odisc); 369 ioctl(0, TIOCSETP, (char *)&defarg); 370 ioctl(0, TIOCSETC, (char *)&defchars); 371 ioctl(0, TIOCSLTC, (char *)&deflchars); 372#endif /* HAVE_TERMIOS */ 373} 374 375static jmp_buf promptbuf; 376 377/* 378 * Print string ``s'', then read a string 379 * in from the terminal. Handles signals & allows use of 380 * normal erase and kill characters. 381 */ 382int 383prompt(s, p, sz) 384 char *s; 385 register char *p; 386 size_t sz; 387{ 388 register char *b = p; 389 sig_t oint, oquit; 390 391 stoprompt = 0; 392 oint = signal(SIGINT, intprompt); 393 oquit = signal(SIGQUIT, SIG_IGN); 394 unraw(); 395 printf("%s", s); 396 if (setjmp(promptbuf) == 0) 397 while ((*p = getchar()) != EOF && *p != '\n' && --sz > 0) 398 p++; 399 *p = '\0'; 400 401 raw(); 402 (void)signal(SIGINT, oint); 403 (void)signal(SIGQUIT, oquit); 404 return (stoprompt || p == b); 405} 406 407/* 408 * Interrupt service routine during prompting 409 */ 410void 411intprompt() 412{ 413 414 (void)signal(SIGINT, SIG_IGN); 415 stoprompt = 1; 416 printf("\r\n"); 417 longjmp(promptbuf, 1); 418} 419 420/* 421 * ****TIPIN TIPIN**** 422 */ 423void 424tipin() 425{ 426 int i; 427 char gch, bol = 1; 428 429 /* 430 * Kinda klugey here... 431 * check for scripting being turned on from the .tiprc file, 432 * but be careful about just using setscript(), as we may 433 * send a SIGEMT before tipout has a chance to set up catching 434 * it; so wait a second, then setscript() 435 */ 436 if (boolean(value(SCRIPT))) { 437 sleep(1); 438 setscript(); 439 } 440 441 while (1) { 442 i = getchar(); 443 if (i == EOF) 444 break; 445 gch = i&0177; 446 if ((gch == character(value(ESCAPE))) && bol) { 447 if (!(gch = escape())) 448 continue; 449 } else if (!cumode && gch == character(value(RAISECHAR))) { 450 boolean(value(RAISE)) = !boolean(value(RAISE)); 451 continue; 452 } else if (gch == '\r') { 453 bol = 1; 454 xpwrite(FD, &gch, 1); 455 if (boolean(value(HALFDUPLEX))) 456 printf("\r\n"); 457 continue; 458 } else if (!cumode && gch == character(value(FORCE))) { 459 i = getchar(); 460 if (i == EOF) 461 break; 462 gch = i & 0177; 463 } 464 bol = any(gch, value(EOL)); 465 if (boolean(value(RAISE)) && islower(gch)) 466 gch = toupper(gch); 467 xpwrite(FD, &gch, 1); 468 if (boolean(value(HALFDUPLEX))) 469 printf("%c", gch); 470 } 471} 472 473extern esctable_t etable[]; 474 475/* 476 * Escape handler -- 477 * called on recognition of ``escapec'' at the beginning of a line 478 */ 479char 480escape() 481{ 482 register char gch; 483 register esctable_t *p; 484 char c = character(value(ESCAPE)); 485 int i; 486 487 i = getchar(); 488 if (i == EOF) 489 return 0; 490 gch = (i&0177); 491 for (p = etable; p->e_char; p++) 492 if (p->e_char == gch) { 493 if ((p->e_flags&PRIV) && uid) 494 continue; 495 printf("%s", ctrl(c)); 496 (*p->e_func)(gch); 497 return (0); 498 } 499 /* ESCAPE ESCAPE forces ESCAPE */ 500 if (c != gch) 501 xpwrite(FD, &c, 1); 502 return (gch); 503} 504 505int 506speed(n) 507 int n; 508{ 509#if HAVE_TERMIOS 510 return (n); 511#else 512 register CONST int *p; 513 514 for (p = bauds; *p != -1; p++) 515 if (*p == n) 516 return (p - bauds); 517 return (NULL); 518#endif 519} 520 521int 522any(c, p) 523 register char c, *p; 524{ 525 while (p && *p) 526 if (*p++ == c) 527 return (1); 528 return (0); 529} 530 531int 532size(s) 533 register char *s; 534{ 535 register int i = 0; 536 537 while (s && *s++) 538 i++; 539 return (i); 540} 541 542char * 543interp(s) 544 register char *s; 545{ 546 static char buf[256]; 547 register char *p = buf, c, *q; 548 549 while ((c = *s++)) { 550 for (q = "\nn\rr\tt\ff\033E\bb"; *q; q++) 551 if (*q++ == c) { 552 *p++ = '\\'; *p++ = *q; 553 goto next; 554 } 555 if (c < 040) { 556 *p++ = '^'; *p++ = c + 'A'-1; 557 } else if (c == 0177) { 558 *p++ = '^'; *p++ = '?'; 559 } else 560 *p++ = c; 561 next: 562 ; 563 } 564 *p = '\0'; 565 return (buf); 566} 567 568char * 569ctrl(c) 570 char c; 571{ 572 static char s[3]; 573 574 if (c < 040 || c == 0177) { 575 s[0] = '^'; 576 s[1] = c == 0177 ? '?' : c+'A'-1; 577 s[2] = '\0'; 578 } else { 579 s[0] = c; 580 s[1] = '\0'; 581 } 582 return (s); 583} 584 585/* 586 * Help command 587 */ 588void 589help(c) 590 char c; 591{ 592 register esctable_t *p; 593 594 printf("%c\r\n", c); 595 for (p = etable; p->e_char; p++) { 596 if ((p->e_flags&PRIV) && uid) 597 continue; 598 printf("%2s", ctrl(character(value(ESCAPE)))); 599 printf("%-2s %c %s\r\n", ctrl(p->e_char), 600 p->e_flags&EXP ? '*': ' ', p->e_help); 601 } 602} 603 604/* 605 * Set up the "remote" tty's state 606 */ 607void 608ttysetup (int speed) 609{ 610#if HAVE_TERMIOS 611 struct termios termios; 612 tcgetattr (FD, &termios); 613 if (boolean(value(TAND))) 614 termios.c_iflag = IXOFF; 615 else 616 termios.c_iflag = 0; 617#ifndef _POSIX_SOURCE 618 termios.c_lflag = (PENDIN|ECHOKE|ECHOE); 619#else 620 termios.c_lflag = (PENDIN|ECHOE); 621#endif 622 termios.c_cflag = (CLOCAL|HUPCL|CREAD|CS8); 623 termios.c_ispeed = termios.c_ospeed = speed; 624 tcsetattr (FD, TCSANOW, &termios); 625#else /* HAVE_TERMIOS */ 626 unsigned bits = LDECCTQ; 627 628 arg.sg_ispeed = arg.sg_ospeed = speed; 629 arg.sg_flags = RAW; 630 if (boolean(value(TAND))) 631 arg.sg_flags |= TANDEM; 632 ioctl(FD, TIOCSETP, (char *)&arg); 633 ioctl(FD, TIOCLBIS, (char *)&bits); 634#endif /* HAVE_TERMIOS */ 635} 636 637/* 638 * Return "simple" name from a file name, 639 * strip leading directories. 640 */ 641char * 642sname(s) 643 register char *s; 644{ 645 register char *p = s; 646 647 while (*s) 648 if (*s++ == '/') 649 p = s; 650 return (p); 651} 652 653static char partab[0200]; 654static int bits8; 655 656/* 657 * Do a write to the remote machine with the correct parity. 658 * We are doing 8 bit wide output, so we just generate a character 659 * with the right parity and output it. 660 */ 661void 662xpwrite(fd, buf, n) 663 int fd; 664 char *buf; 665 register int n; 666{ 667 register int i; 668 register char *bp; 669 extern int errno; 670 671 bp = buf; 672 if (bits8 == 0) 673 for (i = 0; i < n; i++) { 674 *bp = partab[(*bp) & 0177]; 675 bp++; 676 } 677 if (write(fd, buf, n) < 0) { 678 if (errno == EIO) 679 tipabort("Lost carrier."); 680 if (errno == ENODEV) 681 tipabort("tty not available."); 682 tipabort("Something wrong..."); 683 } 684} 685 686/* 687 * Build a parity table with appropriate high-order bit. 688 */ 689void 690setparity(defparity) 691 char *defparity; 692{ 693 register int i, flip, clr, set; 694 char *parity; 695 extern char evenpartab[]; 696 697 if (value(PARITY) == NOSTR) 698 value(PARITY) = defparity; 699 parity = value(PARITY); 700 if (equal(parity, "none")) { 701 bits8 = 1; 702 return; 703 } 704 bits8 = 0; 705 flip = 0; 706 clr = 0377; 707 set = 0; 708 if (equal(parity, "odd")) 709 flip = 0200; /* reverse bit 7 */ 710 else if (equal(parity, "zero")) 711 clr = 0177; /* turn off bit 7 */ 712 else if (equal(parity, "one")) 713 set = 0200; /* turn on bit 7 */ 714 else if (!equal(parity, "even")) { 715 (void) fprintf(stderr, "%s: unknown parity value\r\n", parity); 716 (void) fflush(stderr); 717 } 718 for (i = 0; i < 0200; i++) 719 partab[i] = (evenpartab[i] ^ flip) | (set & clr); 720} 721