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