11573Srgrimes/* $OpenBSD: tip.c,v 1.30 2006/08/18 03:06:18 jason Exp $ */ 21573Srgrimes/* $NetBSD: tip.c,v 1.13 1997/04/20 00:03:05 mellon Exp $ */ 31573Srgrimes 41573Srgrimes/* 51573Srgrimes * Copyright (c) 1983, 1993 61573Srgrimes * The Regents of the University of California. All rights reserved. 71573Srgrimes * 81573Srgrimes * Redistribution and use in source and binary forms, with or without 91573Srgrimes * modification, are permitted provided that the following conditions 101573Srgrimes * are met: 111573Srgrimes * 1. Redistributions of source code must retain the above copyright 121573Srgrimes * notice, this list of conditions and the following disclaimer. 131573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141573Srgrimes * notice, this list of conditions and the following disclaimer in the 151573Srgrimes * documentation and/or other materials provided with the distribution. 161573Srgrimes * 3. Neither the name of the University nor the names of its contributors 171573Srgrimes * may be used to endorse or promote products derived from this software 181573Srgrimes * without specific prior written permission. 191573Srgrimes * 201573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 211573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 221573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 231573Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 241573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 251573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 261573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 271573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 281573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 291573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 301573Srgrimes * SUCH DAMAGE. 311573Srgrimes */ 321573Srgrimes 331573Srgrimes#include <sys/cdefs.h> 341573Srgrimes__FBSDID("$FreeBSD: stable/11/usr.bin/tip/tip/tip.c 359763 2020-04-10 00:27:19Z kevans $"); 351573Srgrimes 361573Srgrimes#ifndef lint 371573Srgrimesstatic const char copyright[] = 381573Srgrimes"@(#) Copyright (c) 1983, 1993\n\ 391573Srgrimes The Regents of the University of California. All rights reserved.\n"; 401573Srgrimes#endif /* not lint */ 411573Srgrimes 421573Srgrimes#ifndef lint 431573Srgrimes#if 0 441573Srgrimesstatic char sccsid[] = "@(#)tip.c 8.1 (Berkeley) 6/6/93"; 451573Srgrimesstatic const char rcsid[] = "$OpenBSD: tip.c,v 1.30 2006/08/18 03:06:18 jason Exp $"; 461573Srgrimes#endif 471573Srgrimes#endif /* not lint */ 481573Srgrimes 491573Srgrimes/* 501573Srgrimes * tip - UNIX link to other systems 511573Srgrimes * tip [-v] [-speed] system-name 521573Srgrimes * or 531573Srgrimes * cu phone-number [-s speed] [-l line] [-a acu] 541573Srgrimes */ 551573Srgrimes#define EXTERN 561573Srgrimes#include "tip.h" 571573Srgrimes#include "pathnames.h" 581573Srgrimes 591573Srgrimesint disc = TTYDISC; /* tip normally runs this way */ 601573Srgrimeschar PNbuf[256]; /* This limits the size of a number */ 611573Srgrimes 621573Srgrimesstatic void intprompt(int); 631573Srgrimesstatic void tipin(void); 641573Srgrimesstatic int escape(void); 651573Srgrimes 661573Srgrimesint 671573Srgrimesmain(int argc, char *argv[]) 681573Srgrimes{ 691573Srgrimes char *sys = NOSTR, sbuf[12], *p; 701573Srgrimes int i; 711573Srgrimes 721573Srgrimes /* XXX preserve previous braindamaged behavior */ 731573Srgrimes setboolean(value(DC), TRUE); 741573Srgrimes 751573Srgrimes gid = getgid(); 761573Srgrimes egid = getegid(); 771573Srgrimes uid = getuid(); 781573Srgrimes euid = geteuid(); 791573Srgrimes if (equal(__progname, "cu")) { 801573Srgrimes cumode = 1; 811573Srgrimes cumain(argc, argv); 821573Srgrimes goto cucommon; 831573Srgrimes } 841573Srgrimes 851573Srgrimes if (argc > 4) { 861573Srgrimes fprintf(stderr, "usage: tip [-v] [-speed] [system-name]\n"); 871573Srgrimes exit(1); 881573Srgrimes } 891573Srgrimes if (!isatty(0)) { 901573Srgrimes fprintf(stderr, "%s: must be interactive\n", __progname); 911573Srgrimes exit(1); 921573Srgrimes } 931573Srgrimes 941573Srgrimes for (; argc > 1; argv++, argc--) { 951573Srgrimes if (argv[1][0] != '-') 961573Srgrimes sys = argv[1]; 971573Srgrimes else switch (argv[1][1]) { 981573Srgrimes 991573Srgrimes case 'v': 1001573Srgrimes vflag++; 1011573Srgrimes break; 1021573Srgrimes 1031573Srgrimes case 'n': 1041573Srgrimes noesc++; 1051573Srgrimes break; 1061573Srgrimes 1071573Srgrimes case '0': case '1': case '2': case '3': case '4': 1081573Srgrimes case '5': case '6': case '7': case '8': case '9': 1091573Srgrimes BR = atoi(&argv[1][1]); 1101573Srgrimes break; 1111573Srgrimes 1121573Srgrimes default: 1131573Srgrimes fprintf(stderr, "%s: %s, unknown option\n", __progname, 1141573Srgrimes argv[1]); 1151573Srgrimes break; 1161573Srgrimes } 1171573Srgrimes } 1181573Srgrimes 1191573Srgrimes if (sys == NOSTR) 1201573Srgrimes goto notnumber; 1211573Srgrimes if (isalpha(*sys)) 1221573Srgrimes goto notnumber; 1231573Srgrimes /* 1241573Srgrimes * System name is really a phone number... 1251573Srgrimes * Copy the number then stomp on the original (in case the number 1261573Srgrimes * is private, we don't want 'ps' or 'w' to find it). 1271573Srgrimes */ 1281573Srgrimes if (strlen(sys) > sizeof PNbuf - 1) { 1291573Srgrimes fprintf(stderr, "%s: phone number too long (max = %d bytes)\n", 1301573Srgrimes __progname, (int)sizeof(PNbuf) - 1); 1311573Srgrimes exit(1); 1321573Srgrimes } 1331573Srgrimes strlcpy(PNbuf, sys, sizeof PNbuf - 1); 1341573Srgrimes for (p = sys; *p; p++) 1351573Srgrimes *p = '\0'; 1361573Srgrimes PN = PNbuf; 1371573Srgrimes (void)snprintf(sbuf, sizeof(sbuf), "tip%ld", BR); 138 sys = sbuf; 139 140notnumber: 141 (void)signal(SIGINT, cleanup); 142 (void)signal(SIGQUIT, cleanup); 143 (void)signal(SIGHUP, cleanup); 144 (void)signal(SIGTERM, cleanup); 145 (void)signal(SIGCHLD, SIG_DFL); 146 147 if ((i = hunt(sys)) == 0) { 148 printf("all ports busy\n"); 149 exit(3); 150 } 151 if (i == -1) { 152 printf("link down\n"); 153 (void)uu_unlock(uucplock); 154 exit(3); 155 } 156 setbuf(stdout, NULL); 157 loginit(); 158 159 /* 160 * Now that we have the logfile and the ACU open 161 * return to the real uid and gid. These things will 162 * be closed on exit. Swap real and effective uid's 163 * so we can get the original permissions back 164 * for removing the uucp lock. 165 */ 166 user_uid(); 167 168 /* 169 * Kludge, their's no easy way to get the initialization 170 * in the right order, so force it here 171 */ 172 if ((PH = getenv("PHONES")) == NOSTR) 173 PH = _PATH_PHONES; 174 vinit(); /* init variables */ 175 setparity("none"); /* set the parity table */ 176 177 /* 178 * Hardwired connections require the 179 * line speed set before they make any transmissions 180 * (this is particularly true of things like a DF03-AC) 181 */ 182 if (HW && ttysetup(number(value(BAUDRATE)))) { 183 fprintf(stderr, "%s: bad baud rate %ld\n", __progname, 184 number(value(BAUDRATE))); 185 daemon_uid(); 186 (void)uu_unlock(uucplock); 187 exit(3); 188 } 189 if ((p = con())) { 190 printf("\07%s\n[EOT]\n", p); 191 daemon_uid(); 192 (void)uu_unlock(uucplock); 193 exit(1); 194 } 195 if (!HW && ttysetup(number(value(BAUDRATE)))) { 196 fprintf(stderr, "%s: bad baud rate %ld\n", __progname, 197 number(value(BAUDRATE))); 198 daemon_uid(); 199 (void)uu_unlock(uucplock); 200 exit(3); 201 } 202cucommon: 203 /* 204 * From here down the code is shared with 205 * the "cu" version of tip. 206 */ 207 208 i = fcntl(FD, F_GETFL); 209 if (i == -1) { 210 perror("fcntl"); 211 cleanup(0); 212 } 213 i = fcntl(FD, F_SETFL, i & ~O_NONBLOCK); 214 if (i == -1) { 215 perror("fcntl"); 216 cleanup(0); 217 } 218 219 tcgetattr(0, &defterm); 220 gotdefterm = 1; 221 term = defterm; 222 term.c_lflag &= ~(ICANON|IEXTEN|ECHO); 223 term.c_iflag &= ~(INPCK|ICRNL); 224 term.c_oflag &= ~OPOST; 225 term.c_cc[VMIN] = 1; 226 term.c_cc[VTIME] = 0; 227 defchars = term; 228 term.c_cc[VINTR] = term.c_cc[VQUIT] = term.c_cc[VSUSP] = 229 term.c_cc[VDSUSP] = term.c_cc[VDISCARD] = 230 term.c_cc[VLNEXT] = _POSIX_VDISABLE; 231 raw(); 232 233 pipe(fildes); pipe(repdes); 234 (void)signal(SIGALRM, timeout); 235 236 if (value(LINEDISC) != TTYDISC) { 237 int ld = (int)(intptr_t)value(LINEDISC); 238 ioctl(FD, TIOCSETD, &ld); 239 } 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 tipin_pid = getpid(); 250 if ((tipout_pid = fork())) 251 tipin(); 252 else 253 tipout(); 254 exit(0); 255} 256 257void 258cleanup(int signo) 259{ 260 daemon_uid(); 261 (void)uu_unlock(uucplock); 262 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(); 404 if (gch == EOF) 405 return; 406 gch = gch & STRIP_PAR; 407 if ((gch == character(value(ESCAPE))) && bol) { 408 if (!noesc) { 409 gch = escape(); 410 if (gch == EOF) 411 return; 412 if (gch == 0) 413 continue; 414 } 415 } else if (!cumode && gch == character(value(RAISECHAR))) { 416 setboolean(value(RAISE), !boolean(value(RAISE))); 417 continue; 418 } else if (gch == '\r') { 419 bol = 1; 420 ch = gch; 421 parwrite(FD, &ch, 1); 422 if (boolean(value(HALFDUPLEX))) 423 printf("\r\n"); 424 continue; 425 } else if (!cumode && gch == character(value(FORCE))) { 426 gch = getchar(); 427 if (gch == EOF) 428 return; 429 gch = gch & STRIP_PAR; 430 } 431 bol = any(gch, value(EOL)); 432 if (boolean(value(RAISE)) && islower(gch)) 433 gch = toupper(gch); 434 ch = gch; 435 parwrite(FD, &ch, 1); 436 if (boolean(value(HALFDUPLEX))) 437 printf("%c", ch); 438 } 439} 440 441extern esctable_t etable[]; 442 443/* 444 * Escape handler -- 445 * called on recognition of ``escapec'' at the beginning of a line 446 */ 447static int 448escape(void) 449{ 450 int gch; 451 esctable_t *p; 452 char c = character(value(ESCAPE)); 453 454 gch = getchar(); 455 if (gch == EOF) 456 return (EOF); 457 gch = gch & STRIP_PAR; 458 for (p = etable; p->e_char; p++) 459 if (p->e_char == gch) { 460 if ((p->e_flags&PRIV) && uid) 461 continue; 462 printf("%s", ctrl(c)); 463 (*p->e_func)(gch); 464 return (0); 465 } 466 /* ESCAPE ESCAPE forces ESCAPE */ 467 if (c != gch) 468 parwrite(FD, &c, 1); 469 return (gch); 470} 471 472int 473any(int cc, char *p) 474{ 475 char c = cc; 476 while (p && *p) 477 if (*p++ == c) 478 return (1); 479 return (0); 480} 481 482size_t 483size(char *s) 484{ 485 size_t i = 0; 486 487 while (s && *s++) 488 i++; 489 return (i); 490} 491 492char * 493interp(char *s) 494{ 495 static char buf[256]; 496 char *p = buf, c, *q; 497 498 while ((c = *s++)) { 499 for (q = "\nn\rr\tt\ff\033E\bb"; *q; q++) 500 if (*q++ == c) { 501 *p++ = '\\'; *p++ = *q; 502 goto next; 503 } 504 if (c < 040) { 505 *p++ = '^'; *p++ = c + 'A'-1; 506 } else if (c == 0177) { 507 *p++ = '^'; *p++ = '?'; 508 } else 509 *p++ = c; 510 next: 511 ; 512 } 513 *p = '\0'; 514 return (buf); 515} 516 517char * 518ctrl(char c) 519{ 520 static char s[3]; 521 522 if (c < 040 || c == 0177) { 523 s[0] = '^'; 524 s[1] = c == 0177 ? '?' : c+'A'-1; 525 s[2] = '\0'; 526 } else { 527 s[0] = c; 528 s[1] = '\0'; 529 } 530 return (s); 531} 532 533/* 534 * Help command 535 */ 536void 537help(int c) 538{ 539 esctable_t *p; 540 541 printf("%c\r\n", c); 542 for (p = etable; p->e_char; p++) { 543 if ((p->e_flags&PRIV) && uid) 544 continue; 545 printf("%2s", ctrl(character(value(ESCAPE)))); 546 printf("%-2s %c %s\r\n", ctrl(p->e_char), 547 p->e_flags&EXP ? '*': ' ', p->e_help); 548 } 549} 550 551/* 552 * Set up the "remote" tty's state 553 */ 554int 555ttysetup(int speed) 556{ 557 struct termios cntrl; 558 559 if (tcgetattr(FD, &cntrl)) 560 return (-1); 561 cfsetspeed(&cntrl, speed); 562 cntrl.c_cflag &= ~(CSIZE|PARENB); 563 cntrl.c_cflag |= CS8; 564 if (boolean(value(DC))) 565 cntrl.c_cflag |= CLOCAL; 566 if (boolean(value(HARDWAREFLOW))) 567 cntrl.c_cflag |= CRTSCTS; 568 cntrl.c_iflag &= ~(ISTRIP|ICRNL); 569 cntrl.c_oflag &= ~OPOST; 570 cntrl.c_lflag &= ~(ICANON|ISIG|IEXTEN|ECHO); 571 cntrl.c_cc[VMIN] = 1; 572 cntrl.c_cc[VTIME] = 0; 573 if (boolean(value(TAND))) 574 cntrl.c_iflag |= IXOFF; 575 return (tcsetattr(FD, TCSAFLUSH, &cntrl)); 576} 577 578static char partab[0200]; 579 580/* 581 * Do a write to the remote machine with the correct parity. 582 * We are doing 8 bit wide output, so we just generate a character 583 * with the right parity and output it. 584 */ 585void 586parwrite(int fd, char *buf, size_t n) 587{ 588 size_t i; 589 char *bp; 590 591 bp = buf; 592 if (bits8 == 0) 593 for (i = 0; i < n; i++) { 594 *bp = partab[(*bp) & 0177]; 595 bp++; 596 } 597 if (write(fd, buf, n) < 0) { 598 if (errno == EIO || errno == ENXIO) 599 tipabort("Lost carrier."); 600 /* this is questionable */ 601 perror("write"); 602 } 603} 604 605/* 606 * Build a parity table with appropriate high-order bit. 607 */ 608void 609setparity(char *defparity) 610{ 611 int i, flip, clr, set; 612 char *parity; 613 extern const unsigned char evenpartab[]; 614 615 if (value(PARITY) == NOSTR) 616 value(PARITY) = defparity; 617 parity = value(PARITY); 618 if (equal(parity, "none")) { 619 bits8 = 1; 620 return; 621 } 622 bits8 = 0; 623 flip = 0; 624 clr = 0377; 625 set = 0; 626 if (equal(parity, "odd")) 627 flip = 0200; /* reverse bit 7 */ 628 else if (equal(parity, "zero")) 629 clr = 0177; /* turn off bit 7 */ 630 else if (equal(parity, "one")) 631 set = 0200; /* turn on bit 7 */ 632 else if (!equal(parity, "even")) { 633 (void) fprintf(stderr, "%s: unknown parity value\r\n", parity); 634 (void) fflush(stderr); 635 } 636 for (i = 0; i < 0200; i++) 637 partab[i] = ((evenpartab[i] ^ flip) | set) & clr; 638} 639