tip.c revision 331722
111406Sbde/* $OpenBSD: tip.c,v 1.30 2006/08/18 03:06:18 jason Exp $ */ 211406Sbde/* $NetBSD: tip.c,v 1.13 1997/04/20 00:03:05 mellon Exp $ */ 311406Sbde 411406Sbde/* 511406Sbde * Copyright (c) 1983, 1993 611406Sbde * The Regents of the University of California. All rights reserved. 711406Sbde * 811406Sbde * Redistribution and use in source and binary forms, with or without 911406Sbde * modification, are permitted provided that the following conditions 1011406Sbde * are met: 1111406Sbde * 1. Redistributions of source code must retain the above copyright 1211406Sbde * notice, this list of conditions and the following disclaimer. 1311406Sbde * 2. Redistributions in binary form must reproduce the above copyright 1411406Sbde * notice, this list of conditions and the following disclaimer in the 1511406Sbde * documentation and/or other materials provided with the distribution. 1611406Sbde * 3. Neither the name of the University nor the names of its contributors 1711406Sbde * may be used to endorse or promote products derived from this software 1811406Sbde * without specific prior written permission. 1911406Sbde * 2011406Sbde * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2111406Sbde * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2211406Sbde * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2311406Sbde * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2411406Sbde * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2511406Sbde * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2611406Sbde * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2711406Sbde * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2811406Sbde * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2911406Sbde * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3011406Sbde * SUCH DAMAGE. 3111406Sbde */ 3211406Sbde 3311406Sbde#include <sys/cdefs.h> 3411406Sbde__FBSDID("$FreeBSD: stable/11/usr.bin/tip/tip/tip.c 331722 2018-03-29 02:50:57Z eadler $"); 3511406Sbde 3611406Sbde#ifndef lint 3711406Sbdestatic const char copyright[] = 3850476Speter"@(#) Copyright (c) 1983, 1993\n\ 3911406Sbde The Regents of the University of California. All rights reserved.\n"; 40129614Sbde#endif /* not lint */ 41129619Sbde 4279538Sru#ifndef lint 4311406Sbde#if 0 4411406Sbdestatic char sccsid[] = "@(#)tip.c 8.1 (Berkeley) 6/6/93"; 4523354Sbdestatic const char rcsid[] = "$OpenBSD: tip.c,v 1.30 2006/08/18 03:06:18 jason Exp $"; 4611406Sbde#endif 4799810Sbde#endif /* not lint */ 48129614Sbde 49101908Sru/* 50129614Sbde * tip - UNIX link to other systems 51101908Sru * tip [-v] [-speed] system-name 52129614Sbde * or 5384877Syokota * cu phone-number [-s speed] [-l line] [-a acu] 5484877Syokota */ 5584877Syokota#include "tip.h" 5684877Syokota#include "pathnames.h" 5784877Syokota 58101908Sruint disc = TTYDISC; /* tip normally runs this way */ 5984877Syokotachar PNbuf[256]; /* This limits the size of a number */ 60129614Sbde 61101908Srustatic void intprompt(int); 62129614Sbdestatic void tipin(void); 63101908Srustatic int escape(void); 64129614Sbde 6599810Sbdeint 6684877Syokotamain(int argc, char *argv[]) 6784877Syokota{ 6884877Syokota char *sys = NOSTR, sbuf[12], *p; 6984877Syokota int i; 7084877Syokota 7184877Syokota /* XXX preserve previous braindamaged behavior */ 7284877Syokota setboolean(value(DC), TRUE); 7384877Syokota 74101908Sru gid = getgid(); 7584877Syokota egid = getegid(); 76129614Sbde uid = getuid(); 77129614Sbde euid = geteuid(); 78129617Sbde if (equal(__progname, "cu")) { 79129614Sbde cumode = 1; 80129614Sbde cumain(argc, argv); 81129614Sbde goto cucommon; 82129614Sbde } 83129614Sbde 84129614Sbde if (argc > 4) { 85129614Sbde fprintf(stderr, "usage: tip [-v] [-speed] [system-name]\n"); 8611406Sbde exit(1); 87101908Sru } 8823354Sbde if (!isatty(0)) { 8923354Sbde fprintf(stderr, "%s: must be interactive\n", __progname); 9023354Sbde exit(1); 9123354Sbde } 9223354Sbde 9322148Smpp for (; argc > 1; argv++, argc--) { 9411406Sbde if (argv[1][0] != '-') 9511406Sbde sys = argv[1]; 9668962Sru else switch (argv[1][1]) { 9723354Sbde 9811406Sbde case 'v': 9911406Sbde vflag++; 10011406Sbde break; 10111406Sbde 10211406Sbde case 'n': 10311406Sbde noesc++; 10423354Sbde break; 10523354Sbde 10611406Sbde case '0': case '1': case '2': case '3': case '4': 10711406Sbde case '5': case '6': case '7': case '8': case '9': 10811406Sbde BR = atoi(&argv[1][1]); 10911406Sbde break; 11011406Sbde 11111406Sbde default: 11211406Sbde fprintf(stderr, "%s: %s, unknown option\n", __progname, 11311406Sbde argv[1]); 11411406Sbde break; 11511406Sbde } 11611406Sbde } 11711406Sbde 11811406Sbde if (sys == NOSTR) 11911406Sbde goto notnumber; 12011406Sbde if (isalpha(*sys)) 12111406Sbde goto notnumber; 12211406Sbde /* 12368962Sru * System name is really a phone number... 12411406Sbde * Copy the number then stomp on the original (in case the number 12511406Sbde * is private, we don't want 'ps' or 'w' to find it). 12611406Sbde */ 12711406Sbde if (strlen(sys) > sizeof PNbuf - 1) { 12811406Sbde fprintf(stderr, "%s: phone number too long (max = %d bytes)\n", 12911406Sbde __progname, (int)sizeof(PNbuf) - 1); 13011406Sbde exit(1); 13111406Sbde } 13211406Sbde strlcpy(PNbuf, sys, sizeof PNbuf - 1); 13311406Sbde for (p = sys; *p; p++) 13411406Sbde *p = '\0'; 13511406Sbde PN = PNbuf; 13611406Sbde (void)snprintf(sbuf, sizeof(sbuf), "tip%ld", BR); 13711406Sbde sys = sbuf; 13811406Sbde 13911406Sbdenotnumber: 14011406Sbde (void)signal(SIGINT, cleanup); 14168962Sru (void)signal(SIGQUIT, cleanup); 14211406Sbde (void)signal(SIGHUP, cleanup); 14311406Sbde (void)signal(SIGTERM, cleanup); 14411406Sbde (void)signal(SIGCHLD, SIG_DFL); 14511406Sbde 14611406Sbde if ((i = hunt(sys)) == 0) { 14711406Sbde printf("all ports busy\n"); 14811406Sbde exit(3); 14911406Sbde } 15011406Sbde if (i == -1) { 15111406Sbde printf("link down\n"); 15211406Sbde (void)uu_unlock(uucplock); 15311406Sbde exit(3); 15411406Sbde } 15522148Smpp setbuf(stdout, NULL); 15611406Sbde loginit(); 157131530Sru 158131530Sru /* 15911406Sbde * Now that we have the logfile and the ACU open 16022300Smpp * return to the real uid and gid. These things will 161131530Sru * be closed on exit. Swap real and effective uid's 162131530Sru * so we can get the original permissions back 16311406Sbde * for removing the uucp lock. 16411406Sbde */ 16522148Smpp user_uid(); 16611406Sbde 16711406Sbde /* 16811406Sbde * Kludge, their's no easy way to get the initialization 16911406Sbde * in the right order, so force it here 17011406Sbde */ 17111406Sbde if ((PH = getenv("PHONES")) == NOSTR) 17211406Sbde PH = _PATH_PHONES; 17311406Sbde vinit(); /* init variables */ 174141846Sru setparity("none"); /* set the parity table */ 175131530Sru 176131530Sru /* 177141846Sru * Hardwired connections require the 178141846Sru * line speed set before they make any transmissions 179131530Sru * (this is particularly true of things like a DF03-AC) 180131530Sru */ 18111406Sbde if (HW && ttysetup(number(value(BAUDRATE)))) { 182131530Sru fprintf(stderr, "%s: bad baud rate %ld\n", __progname, 183131530Sru number(value(BAUDRATE))); 18411406Sbde daemon_uid(); 18511406Sbde (void)uu_unlock(uucplock); 18611406Sbde exit(3); 18747903Sbde } 18847903Sbde if ((p = con())) { 18947903Sbde printf("\07%s\n[EOT]\n", p); 19047903Sbde daemon_uid(); 19147903Sbde (void)uu_unlock(uucplock); 19247903Sbde exit(1); 19347903Sbde } 19447903Sbde if (!HW && ttysetup(number(value(BAUDRATE)))) { 19547903Sbde fprintf(stderr, "%s: bad baud rate %ld\n", __progname, 19647903Sbde number(value(BAUDRATE))); 19747903Sbde daemon_uid(); 19811406Sbde (void)uu_unlock(uucplock); 19923354Sbde exit(3); 20023354Sbde } 20123354Sbdecucommon: 20211406Sbde /* 20323354Sbde * From here down the code is shared with 20423354Sbde * the "cu" version of tip. 20511406Sbde */ 20622148Smpp 20711406Sbde i = fcntl(FD, F_GETFL); 20823354Sbde if (i == -1) { 20911406Sbde perror("fcntl"); 21023354Sbde cleanup(0); 21123354Sbde } 21211406Sbde i = fcntl(FD, F_SETFL, i & ~O_NONBLOCK); 21311406Sbde if (i == -1) { 21422148Smpp perror("fcntl"); 21511406Sbde cleanup(0); 21611406Sbde } 21711406Sbde 21811406Sbde tcgetattr(0, &defterm); 21911406Sbde gotdefterm = 1; 22023354Sbde term = defterm; 22123354Sbde term.c_lflag &= ~(ICANON|IEXTEN|ECHO); 22223354Sbde term.c_iflag &= ~(INPCK|ICRNL); 22323354Sbde term.c_oflag &= ~OPOST; 22423354Sbde term.c_cc[VMIN] = 1; 22511406Sbde term.c_cc[VTIME] = 0; 22611406Sbde defchars = term; 22711406Sbde term.c_cc[VINTR] = term.c_cc[VQUIT] = term.c_cc[VSUSP] = 22811406Sbde term.c_cc[VDSUSP] = term.c_cc[VDISCARD] = 22911406Sbde term.c_cc[VLNEXT] = _POSIX_VDISABLE; 23011406Sbde raw(); 23111406Sbde 23211406Sbde pipe(fildes); pipe(repdes); 23311406Sbde (void)signal(SIGALRM, timeout); 23411406Sbde 23511406Sbde if (value(LINEDISC) != TTYDISC) { 23611406Sbde int ld = (int)(intptr_t)value(LINEDISC); 23711406Sbde ioctl(FD, TIOCSETD, &ld); 23811406Sbde } 23911406Sbde 24011406Sbde /* 24111406Sbde * Everything's set up now: 24211406Sbde * connection established (hardwired or dialup) 24311406Sbde * line conditioned (baud rate, mode, etc.) 24411406Sbde * internal data structures (variables) 24511406Sbde * so, fork one process for local side and one for remote. 24620920Swosch */ 24720920Swosch printf(cumode ? "Connected\r\n" : "\07connected\r\n"); 24811406Sbde tipin_pid = getpid(); 24923354Sbde if ((tipout_pid = fork())) 25023354Sbde tipin(); 25111406Sbde else 25211406Sbde tipout(); 25311406Sbde /*NOTREACHED*/ 25411406Sbde exit(0); 25511406Sbde} 25681590Sru 25781590Sruvoid 25811406Sbdecleanup(int signo) 25911406Sbde{ 26011406Sbde daemon_uid(); 26111406Sbde (void)uu_unlock(uucplock); 26211406Sbde if (odisc) 263 ioctl(0, TIOCSETD, &odisc); 264 unraw(); 265 if (signo && tipout_pid) { 266 kill(tipout_pid, signo); 267 wait(NULL); 268 } 269 exit(0); 270} 271 272/* 273 * Muck with user ID's. We are setuid to the owner of the lock 274 * directory when we start. user_uid() reverses real and effective 275 * ID's after startup, to run with the user's permissions. 276 * daemon_uid() switches back to the privileged uid for unlocking. 277 * Finally, to avoid running a shell with the wrong real uid, 278 * shell_uid() sets real and effective uid's to the user's real ID. 279 */ 280static int uidswapped; 281 282void 283user_uid(void) 284{ 285 if (uidswapped == 0) { 286 seteuid(uid); 287 uidswapped = 1; 288 } 289} 290 291void 292daemon_uid(void) 293{ 294 295 if (uidswapped) { 296 seteuid(euid); 297 uidswapped = 0; 298 } 299} 300 301void 302shell_uid(void) 303{ 304 setegid(gid); 305 seteuid(uid); 306} 307 308/* 309 * put the controlling keyboard into raw mode 310 */ 311void 312raw(void) 313{ 314 tcsetattr(0, TCSADRAIN, &term); 315} 316 317 318/* 319 * return keyboard to normal mode 320 */ 321void 322unraw(void) 323{ 324 if (gotdefterm) 325 tcsetattr(0, TCSADRAIN, &defterm); 326} 327 328/* 329 * give up exclusive tty access 330 */ 331void 332unexcl() 333{ 334 ioctl(FD, TIOCNXCL, 0); 335} 336 337static jmp_buf promptbuf; 338 339/* 340 * Print string ``s'', then read a string 341 * in from the terminal. Handles signals & allows use of 342 * normal erase and kill characters. 343 */ 344int 345prompt(char *s, char *p, size_t sz) 346{ 347 int c; 348 char *b = p; 349 sig_t oint, oquit; 350 351 stoprompt = 0; 352 oint = signal(SIGINT, intprompt); 353 oquit = signal(SIGQUIT, SIG_IGN); 354 unraw(); 355 printf("%s", s); 356 if (setjmp(promptbuf) == 0) 357 while ((c = getchar()) != EOF && (*p = c) != '\n' && --sz > 0) 358 p++; 359 *p = '\0'; 360 361 raw(); 362 (void)signal(SIGINT, oint); 363 (void)signal(SIGQUIT, oquit); 364 return (stoprompt || p == b); 365} 366 367/* 368 * Interrupt service routine during prompting 369 */ 370/*ARGSUSED*/ 371static void 372intprompt(int signo) 373{ 374 (void)signal(SIGINT, SIG_IGN); 375 stoprompt = 1; 376 printf("\r\n"); 377 longjmp(promptbuf, 1); 378} 379 380/* 381 * ****TIPIN TIPIN**** 382 */ 383static void 384tipin(void) 385{ 386 int bol = 1; 387 int gch; 388 char ch; 389 390 /* 391 * Kinda klugey here... 392 * check for scripting being turned on from the .tiprc file, 393 * but be careful about just using setscript(), as we may 394 * send a SIGEMT before tipout has a chance to set up catching 395 * it; so wait a second, then setscript() 396 */ 397 if (boolean(value(SCRIPT))) { 398 sleep(1); 399 setscript(); 400 } 401 402 while (1) { 403 gch = getchar()&STRIP_PAR; 404 /* XXX does not check for EOF */ 405 if ((gch == character(value(ESCAPE))) && bol) { 406 if (!noesc) { 407 if (!(gch = escape())) 408 continue; 409 } 410 } else if (!cumode && gch == character(value(RAISECHAR))) { 411 setboolean(value(RAISE), !boolean(value(RAISE))); 412 continue; 413 } else if (gch == '\r') { 414 bol = 1; 415 ch = gch; 416 parwrite(FD, &ch, 1); 417 if (boolean(value(HALFDUPLEX))) 418 printf("\r\n"); 419 continue; 420 } else if (!cumode && 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 ch = gch; 426 parwrite(FD, &ch, 1); 427 if (boolean(value(HALFDUPLEX))) 428 printf("%c", ch); 429 } 430} 431 432extern esctable_t etable[]; 433 434/* 435 * Escape handler -- 436 * called on recognition of ``escapec'' at the beginning of a line 437 */ 438static int 439escape(void) 440{ 441 int gch; 442 esctable_t *p; 443 char c = character(value(ESCAPE)); 444 445 gch = (getchar()&STRIP_PAR); 446 /* XXX does not check for EOF */ 447 for (p = etable; p->e_char; p++) 448 if (p->e_char == gch) { 449 if ((p->e_flags&PRIV) && uid) 450 continue; 451 printf("%s", ctrl(c)); 452 (*p->e_func)(gch); 453 return (0); 454 } 455 /* ESCAPE ESCAPE forces ESCAPE */ 456 if (c != gch) 457 parwrite(FD, &c, 1); 458 return (gch); 459} 460 461int 462any(int cc, char *p) 463{ 464 char c = cc; 465 while (p && *p) 466 if (*p++ == c) 467 return (1); 468 return (0); 469} 470 471size_t 472size(char *s) 473{ 474 size_t i = 0; 475 476 while (s && *s++) 477 i++; 478 return (i); 479} 480 481char * 482interp(char *s) 483{ 484 static char buf[256]; 485 char *p = buf, c, *q; 486 487 while ((c = *s++)) { 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 506char * 507ctrl(char c) 508{ 509 static char s[3]; 510 511 if (c < 040 || c == 0177) { 512 s[0] = '^'; 513 s[1] = c == 0177 ? '?' : c+'A'-1; 514 s[2] = '\0'; 515 } else { 516 s[0] = c; 517 s[1] = '\0'; 518 } 519 return (s); 520} 521 522/* 523 * Help command 524 */ 525void 526help(int c) 527{ 528 esctable_t *p; 529 530 printf("%c\r\n", c); 531 for (p = etable; p->e_char; p++) { 532 if ((p->e_flags&PRIV) && uid) 533 continue; 534 printf("%2s", ctrl(character(value(ESCAPE)))); 535 printf("%-2s %c %s\r\n", ctrl(p->e_char), 536 p->e_flags&EXP ? '*': ' ', p->e_help); 537 } 538} 539 540/* 541 * Set up the "remote" tty's state 542 */ 543int 544ttysetup(int speed) 545{ 546 struct termios cntrl; 547 548 if (tcgetattr(FD, &cntrl)) 549 return (-1); 550 cfsetspeed(&cntrl, speed); 551 cntrl.c_cflag &= ~(CSIZE|PARENB); 552 cntrl.c_cflag |= CS8; 553 if (boolean(value(DC))) 554 cntrl.c_cflag |= CLOCAL; 555 if (boolean(value(HARDWAREFLOW))) 556 cntrl.c_cflag |= CRTSCTS; 557 cntrl.c_iflag &= ~(ISTRIP|ICRNL); 558 cntrl.c_oflag &= ~OPOST; 559 cntrl.c_lflag &= ~(ICANON|ISIG|IEXTEN|ECHO); 560 cntrl.c_cc[VMIN] = 1; 561 cntrl.c_cc[VTIME] = 0; 562 if (boolean(value(TAND))) 563 cntrl.c_iflag |= IXOFF; 564 return (tcsetattr(FD, TCSAFLUSH, &cntrl)); 565} 566 567static char partab[0200]; 568 569/* 570 * Do a write to the remote machine with the correct parity. 571 * We are doing 8 bit wide output, so we just generate a character 572 * with the right parity and output it. 573 */ 574void 575parwrite(int fd, char *buf, size_t n) 576{ 577 size_t i; 578 char *bp; 579 580 bp = buf; 581 if (bits8 == 0) 582 for (i = 0; i < n; i++) { 583 *bp = partab[(*bp) & 0177]; 584 bp++; 585 } 586 if (write(fd, buf, n) < 0) { 587 if (errno == EIO || errno == ENXIO) 588 tipabort("Lost carrier."); 589 /* this is questionable */ 590 perror("write"); 591 } 592} 593 594/* 595 * Build a parity table with appropriate high-order bit. 596 */ 597void 598setparity(char *defparity) 599{ 600 int i, flip, clr, set; 601 char *parity; 602 extern const unsigned char evenpartab[]; 603 604 if (value(PARITY) == NOSTR) 605 value(PARITY) = defparity; 606 parity = value(PARITY); 607 if (equal(parity, "none")) { 608 bits8 = 1; 609 return; 610 } 611 bits8 = 0; 612 flip = 0; 613 clr = 0377; 614 set = 0; 615 if (equal(parity, "odd")) 616 flip = 0200; /* reverse bit 7 */ 617 else if (equal(parity, "zero")) 618 clr = 0177; /* turn off bit 7 */ 619 else if (equal(parity, "one")) 620 set = 0200; /* turn on bit 7 */ 621 else if (!equal(parity, "even")) { 622 (void) fprintf(stderr, "%s: unknown parity value\r\n", parity); 623 (void) fflush(stderr); 624 } 625 for (i = 0; i < 0200; i++) 626 partab[i] = ((evenpartab[i] ^ flip) | set) & clr; 627} 628