1 /* 2 * Copyright (c) 1989, 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. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#)telnetd.c 8.4 (Berkeley) 5/30/95 30 * $FreeBSD: src/crypto/telnet/telnetd/telnetd.c,v 1.11.2.5 2002/04/13 10:59:09 markm Exp $ 31 */ 32 33 #include "telnetd.h" 34 #include "pathnames.h" 35 36 #include <sys/mman.h> 37 #include <err.h> 38 #include <libutil.h> 39 #include <paths.h> 40 #include <termcap.h> 41 42 #include <arpa/inet.h> 43 44 #ifdef AUTHENTICATION 45 #include <libtelnet/auth.h> 46 int auth_level = 0; 47 #endif 48 #ifdef ENCRYPTION 49 #include <libtelnet/encrypt.h> 50 #endif 51 #include <libtelnet/misc.h> 52 53 char remote_hostname[MAXHOSTNAMELEN]; 54 int registerd_host_only = 0; 55 56 57 /* 58 * I/O data buffers, 59 * pointers, and counters. 60 */ 61 char ptyibuf[BUFSIZ], *ptyip = ptyibuf; 62 char ptyibuf2[BUFSIZ]; 63 64 int readstream(int, char *, int); 65 void doit(struct sockaddr *) __dead2; 66 int terminaltypeok(char *); 67 68 int hostinfo = 1; /* do we print login banner? */ 69 70 static int debug = 0; 71 int keepalive = 1; 72 const char *altlogin; 73 74 void startslave(char *, int, char *); 75 extern void usage(void) __dead2; 76 static void _gettermname(void); 77 78 /* 79 * The string to pass to getopt(). We do it this way so 80 * that only the actual options that we support will be 81 * passed off to getopt(). 82 */ 83 char valid_opts[] = { 84 'd', ':', 'h', 'k', 'n', 'p', ':', 'S', ':', 'u', ':', 'U', 85 '4', '6', 86 #ifdef AUTHENTICATION 87 'a', ':', 'X', ':', 88 #endif 89 #ifdef BFTPDAEMON 90 'B', 91 #endif 92 #ifdef DIAGNOSTICS 93 'D', ':', 94 #endif 95 #ifdef ENCRYPTION 96 'e', ':', 97 #endif 98 #ifdef LINEMODE 99 'l', 100 #endif 101 '\0' 102 }; 103 104 int family = AF_INET; 105 106 #ifndef MAXHOSTNAMELEN 107 #define MAXHOSTNAMELEN 256 108 #endif /* MAXHOSTNAMELEN */ 109 110 char *hostname; 111 char host_name[MAXHOSTNAMELEN]; 112 113 extern void telnet(int, int, char *) __dead2; 114 extern char *terminaltype; 115 116 int level; 117 char user_name[256]; 118 119 int 120 main(int argc, char *argv[]) 121 { 122 struct sockaddr_storage from; 123 int on = 1, fromlen; 124 int ch; 125 #if defined(IPPROTO_IP) && defined(IP_TOS) 126 int tos = -1; 127 #endif 128 129 pfrontp = pbackp = ptyobuf; 130 netip = netibuf; 131 nfrontp = nbackp = netobuf; 132 #ifdef ENCRYPTION 133 nclearto = 0; 134 #endif /* ENCRYPTION */ 135 136 /* 137 * This initialization causes linemode to default to a configuration 138 * that works on all telnet clients, including the FreeBSD client. 139 * This is not quite the same as the telnet client issuing a "mode 140 * character" command, but has most of the same benefits, and is 141 * preferable since some clients (like usofts) don't have the 142 * mode character command anyway and linemode breaks things. 143 * The most notable symptom of fix is that csh "set filec" operations 144 * like <ESC> (filename completion) and ^D (choices) keys now work 145 * in telnet sessions and can be used more than once on the same line. 146 * CR/LF handling is also corrected in some termio modes. This 147 * change resolves problem reports bin/771 and bin/1037. 148 */ 149 150 linemode=1; /*Default to mode that works on bulk of clients*/ 151 152 while ((ch = getopt(argc, argv, valid_opts)) != -1) { 153 switch(ch) { 154 155 #ifdef AUTHENTICATION 156 case 'a': 157 /* 158 * Check for required authentication level 159 */ 160 if (strcmp(optarg, "debug") == 0) { 161 extern int auth_debug_mode; 162 auth_debug_mode = 1; 163 } else if (strcasecmp(optarg, "none") == 0) { 164 auth_level = 0; 165 } else if (strcasecmp(optarg, "other") == 0) { 166 auth_level = AUTH_OTHER; 167 } else if (strcasecmp(optarg, "user") == 0) { 168 auth_level = AUTH_USER; 169 } else if (strcasecmp(optarg, "valid") == 0) { 170 auth_level = AUTH_VALID; 171 } else if (strcasecmp(optarg, "off") == 0) { 172 /* 173 * This hack turns off authentication 174 */ 175 auth_level = -1; 176 } else { 177 warnx("unknown authorization level for -a"); 178 } 179 break; 180 #endif /* AUTHENTICATION */ 181 182 #ifdef BFTPDAEMON 183 case 'B': 184 bftpd++; 185 break; 186 #endif /* BFTPDAEMON */ 187 188 case 'd': 189 if (strcmp(optarg, "ebug") == 0) { 190 debug++; 191 break; 192 } 193 usage(); 194 /* NOTREACHED */ 195 break; 196 197 #ifdef DIAGNOSTICS 198 case 'D': 199 /* 200 * Check for desired diagnostics capabilities. 201 */ 202 if (!strcmp(optarg, "report")) { 203 diagnostic |= TD_REPORT|TD_OPTIONS; 204 } else if (!strcmp(optarg, "exercise")) { 205 diagnostic |= TD_EXERCISE; 206 } else if (!strcmp(optarg, "netdata")) { 207 diagnostic |= TD_NETDATA; 208 } else if (!strcmp(optarg, "ptydata")) { 209 diagnostic |= TD_PTYDATA; 210 } else if (!strcmp(optarg, "options")) { 211 diagnostic |= TD_OPTIONS; 212 } else { 213 usage(); 214 /* NOT REACHED */ 215 } 216 break; 217 #endif /* DIAGNOSTICS */ 218 219 #ifdef ENCRYPTION 220 case 'e': 221 if (strcmp(optarg, "debug") == 0) { 222 extern int encrypt_debug_mode; 223 encrypt_debug_mode = 1; 224 break; 225 } 226 usage(); 227 /* NOTREACHED */ 228 break; 229 #endif /* ENCRYPTION */ 230 231 case 'h': 232 hostinfo = 0; 233 break; 234 235 #ifdef LINEMODE 236 case 'l': 237 alwayslinemode = 1; 238 break; 239 #endif /* LINEMODE */ 240 241 case 'k': 242 #if defined(LINEMODE) && defined(KLUDGELINEMODE) 243 lmodetype = NO_AUTOKLUDGE; 244 #else 245 /* ignore -k option if built without kludge linemode */ 246 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */ 247 break; 248 249 case 'n': 250 keepalive = 0; 251 break; 252 253 case 'p': 254 altlogin = optarg; 255 break; 256 257 case 'S': 258 #ifdef HAS_GETTOS 259 if ((tos = parsetos(optarg, "tcp")) < 0) 260 warnx("%s%s%s", 261 "bad TOS argument '", optarg, 262 "'; will try to use default TOS"); 263 #else 264 warnx("TOS option unavailable; -S flag not supported"); 265 #endif 266 break; 267 268 case 'u': 269 fprintf(stderr, "telnetd: -u option unneeded\n"); 270 break; 271 272 case 'U': 273 registerd_host_only = 1; 274 break; 275 276 #ifdef AUTHENTICATION 277 case 'X': 278 /* 279 * Check for invalid authentication types 280 */ 281 auth_disable_name(optarg); 282 break; 283 #endif /* AUTHENTICATION */ 284 285 case '4': 286 family = AF_INET; 287 break; 288 289 #ifdef INET6 290 case '6': 291 family = AF_INET6; 292 break; 293 #endif 294 295 default: 296 warnx("%c: unknown option", ch); 297 /* FALLTHROUGH */ 298 case '?': 299 usage(); 300 /* NOTREACHED */ 301 } 302 } 303 304 argc -= optind; 305 argv += optind; 306 307 if (debug) { 308 int s, ns, foo, error; 309 const char *service = "telnet"; 310 struct addrinfo hints, *res; 311 312 if (argc > 1) { 313 usage(); 314 /* NOT REACHED */ 315 } else if (argc == 1) 316 service = *argv; 317 318 memset(&hints, 0, sizeof(hints)); 319 hints.ai_flags = AI_PASSIVE; 320 hints.ai_family = family; 321 hints.ai_socktype = SOCK_STREAM; 322 hints.ai_protocol = 0; 323 error = getaddrinfo(NULL, service, &hints, &res); 324 325 if (error) { 326 errx(1, "tcp/%s: %s\n", service, gai_strerror(error)); 327 if (error == EAI_SYSTEM) 328 errx(1, "tcp/%s: %s\n", service, strerror(errno)); 329 usage(); 330 } 331 332 s = socket(res->ai_family, res->ai_socktype, res->ai_protocol); 333 if (s < 0) 334 err(1, "socket"); 335 (void) setsockopt(s, SOL_SOCKET, SO_REUSEADDR, 336 (char *)&on, sizeof(on)); 337 if (bind(s, res->ai_addr, res->ai_addrlen) < 0) 338 err(1, "bind"); 339 if (listen(s, 1) < 0) 340 err(1, "listen"); 341 foo = res->ai_addrlen; 342 ns = accept(s, res->ai_addr, &foo); 343 if (ns < 0) 344 err(1, "accept"); 345 (void) dup2(ns, 0); 346 (void) close(ns); 347 (void) close(s); 348 #ifdef convex 349 } else if (argc == 1) { 350 ; /* VOID*/ /* Just ignore the host/port name */ 351 #endif 352 } else if (argc > 0) { 353 usage(); 354 /* NOT REACHED */ 355 } 356 357 openlog("telnetd", LOG_PID, LOG_DAEMON); 358 fromlen = sizeof (from); 359 if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) { 360 warn("getpeername"); 361 _exit(1); 362 } 363 if (keepalive && 364 setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, 365 (char *)&on, sizeof (on)) < 0) { 366 syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m"); 367 } 368 369 #if defined(IPPROTO_IP) && defined(IP_TOS) 370 if (from.ss_family == AF_INET) { 371 # if defined(HAS_GETTOS) 372 struct tosent *tp; 373 if (tos < 0 && (tp = gettosbyname("telnet", "tcp"))) 374 tos = tp->t_tos; 375 # endif 376 if (tos < 0) 377 tos = 020; /* Low Delay bit */ 378 if (tos 379 && (setsockopt(0, IPPROTO_IP, IP_TOS, 380 (char *)&tos, sizeof(tos)) < 0) 381 && (errno != ENOPROTOOPT) ) 382 syslog(LOG_WARNING, "setsockopt (IP_TOS): %m"); 383 } 384 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */ 385 net = 0; 386 doit((struct sockaddr *)&from); 387 /* NOTREACHED */ 388 return(0); 389 } /* end of main */ 390 391 void 392 usage(void) 393 { 394 fprintf(stderr, "usage: telnetd"); 395 #ifdef AUTHENTICATION 396 fprintf(stderr, " [-a (debug|other|user|valid|off|none)]\n\t"); 397 #endif 398 #ifdef BFTPDAEMON 399 fprintf(stderr, " [-B]"); 400 #endif 401 fprintf(stderr, " [-debug]"); 402 #ifdef DIAGNOSTICS 403 fprintf(stderr, " [-D (options|report|exercise|netdata|ptydata)]\n\t"); 404 #endif 405 #ifdef AUTHENTICATION 406 fprintf(stderr, " [-edebug]"); 407 #endif 408 fprintf(stderr, " [-h]"); 409 #if defined(LINEMODE) && defined(KLUDGELINEMODE) 410 fprintf(stderr, " [-k]"); 411 #endif 412 #ifdef LINEMODE 413 fprintf(stderr, " [-l]"); 414 #endif 415 fprintf(stderr, " [-n]"); 416 fprintf(stderr, "\n\t"); 417 #ifdef HAS_GETTOS 418 fprintf(stderr, " [-S tos]"); 419 #endif 420 #ifdef AUTHENTICATION 421 fprintf(stderr, " [-X auth-type]"); 422 #endif 423 fprintf(stderr, " [-u utmp_hostname_length] [-U]"); 424 fprintf(stderr, " [port]\n"); 425 exit(1); 426 } 427 428 /* 429 * getterminaltype 430 * 431 * Ask the other end to send along its terminal type and speed. 432 * Output is the variable terminaltype filled in. 433 */ 434 static unsigned char ttytype_sbbuf[] = { 435 IAC, SB, TELOPT_TTYPE, TELQUAL_SEND, IAC, SE 436 }; 437 438 439 #ifndef AUTHENTICATION 440 #define undef2 __unused 441 #else 442 #define undef2 443 #endif 444 445 static int 446 getterminaltype(char *name undef2) 447 { 448 int retval = -1; 449 450 settimer(baseline); 451 #ifdef AUTHENTICATION 452 /* 453 * Handle the Authentication option before we do anything else. 454 */ 455 send_do(TELOPT_AUTHENTICATION, 1); 456 while (his_will_wont_is_changing(TELOPT_AUTHENTICATION)) 457 ttloop(); 458 if (his_state_is_will(TELOPT_AUTHENTICATION)) { 459 retval = auth_wait(name); 460 } 461 #endif 462 463 #ifdef ENCRYPTION 464 send_will(TELOPT_ENCRYPT, 1); 465 #endif /* ENCRYPTION */ 466 send_do(TELOPT_TTYPE, 1); 467 send_do(TELOPT_TSPEED, 1); 468 send_do(TELOPT_XDISPLOC, 1); 469 send_do(TELOPT_NEW_ENVIRON, 1); 470 send_do(TELOPT_OLD_ENVIRON, 1); 471 while ( 472 #ifdef ENCRYPTION 473 his_do_dont_is_changing(TELOPT_ENCRYPT) || 474 #endif /* ENCRYPTION */ 475 his_will_wont_is_changing(TELOPT_TTYPE) || 476 his_will_wont_is_changing(TELOPT_TSPEED) || 477 his_will_wont_is_changing(TELOPT_XDISPLOC) || 478 his_will_wont_is_changing(TELOPT_NEW_ENVIRON) || 479 his_will_wont_is_changing(TELOPT_OLD_ENVIRON)) { 480 ttloop(); 481 } 482 #ifdef ENCRYPTION 483 /* 484 * Wait for the negotiation of what type of encryption we can 485 * send with. If autoencrypt is not set, this will just return. 486 */ 487 if (his_state_is_will(TELOPT_ENCRYPT)) { 488 encrypt_wait(); 489 } 490 #endif /* ENCRYPTION */ 491 if (his_state_is_will(TELOPT_TSPEED)) { 492 static unsigned char sb[] = 493 { IAC, SB, TELOPT_TSPEED, TELQUAL_SEND, IAC, SE }; 494 495 output_datalen(sb, sizeof sb); 496 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2);); 497 } 498 if (his_state_is_will(TELOPT_XDISPLOC)) { 499 static unsigned char sb[] = 500 { IAC, SB, TELOPT_XDISPLOC, TELQUAL_SEND, IAC, SE }; 501 502 output_datalen(sb, sizeof sb); 503 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2);); 504 } 505 if (his_state_is_will(TELOPT_NEW_ENVIRON)) { 506 static unsigned char sb[] = 507 { IAC, SB, TELOPT_NEW_ENVIRON, TELQUAL_SEND, IAC, SE }; 508 509 output_datalen(sb, sizeof sb); 510 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2);); 511 } 512 else if (his_state_is_will(TELOPT_OLD_ENVIRON)) { 513 static unsigned char sb[] = 514 { IAC, SB, TELOPT_OLD_ENVIRON, TELQUAL_SEND, IAC, SE }; 515 516 output_datalen(sb, sizeof sb); 517 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2);); 518 } 519 if (his_state_is_will(TELOPT_TTYPE)) { 520 521 output_datalen(ttytype_sbbuf, sizeof ttytype_sbbuf); 522 DIAG(TD_OPTIONS, printsub('>', ttytype_sbbuf + 2, 523 sizeof ttytype_sbbuf - 2);); 524 } 525 if (his_state_is_will(TELOPT_TSPEED)) { 526 while (sequenceIs(tspeedsubopt, baseline)) 527 ttloop(); 528 } 529 if (his_state_is_will(TELOPT_XDISPLOC)) { 530 while (sequenceIs(xdisplocsubopt, baseline)) 531 ttloop(); 532 } 533 if (his_state_is_will(TELOPT_NEW_ENVIRON)) { 534 while (sequenceIs(environsubopt, baseline)) 535 ttloop(); 536 } 537 if (his_state_is_will(TELOPT_OLD_ENVIRON)) { 538 while (sequenceIs(oenvironsubopt, baseline)) 539 ttloop(); 540 } 541 if (his_state_is_will(TELOPT_TTYPE)) { 542 char first[256], last[256]; 543 544 while (sequenceIs(ttypesubopt, baseline)) 545 ttloop(); 546 547 /* 548 * If the other side has already disabled the option, then 549 * we have to just go with what we (might) have already gotten. 550 */ 551 if (his_state_is_will(TELOPT_TTYPE) && !terminaltypeok(terminaltype)) { 552 (void) strncpy(first, terminaltype, sizeof(first)-1); 553 first[sizeof(first)-1] = '\0'; 554 for(;;) { 555 /* 556 * Save the unknown name, and request the next name. 557 */ 558 (void) strncpy(last, terminaltype, sizeof(last)-1); 559 last[sizeof(last)-1] = '\0'; 560 _gettermname(); 561 if (terminaltypeok(terminaltype)) 562 break; 563 if ((strncmp(last, terminaltype, sizeof(last)) == 0) || 564 his_state_is_wont(TELOPT_TTYPE)) { 565 /* 566 * We've hit the end. If this is the same as 567 * the first name, just go with it. 568 */ 569 if (strncmp(first, terminaltype, sizeof(first)) == 0) 570 break; 571 /* 572 * Get the terminal name one more time, so that 573 * RFC1091 compliant telnets will cycle back to 574 * the start of the list. 575 */ 576 _gettermname(); 577 if (strncmp(first, terminaltype, sizeof(first)) != 0) { 578 (void) strncpy(terminaltype, first, TERMINAL_TYPE_SIZE-1); 579 terminaltype[TERMINAL_TYPE_SIZE-1] = '\0'; 580 } 581 break; 582 } 583 } 584 } 585 } 586 return(retval); 587 } /* end of getterminaltype */ 588 589 static void 590 _gettermname(void) 591 { 592 /* 593 * If the client turned off the option, 594 * we can't send another request, so we 595 * just return. 596 */ 597 if (his_state_is_wont(TELOPT_TTYPE)) 598 return; 599 settimer(baseline); 600 output_datalen(ttytype_sbbuf, sizeof ttytype_sbbuf); 601 DIAG(TD_OPTIONS, printsub('>', ttytype_sbbuf + 2, 602 sizeof ttytype_sbbuf - 2);); 603 while (sequenceIs(ttypesubopt, baseline)) 604 ttloop(); 605 } 606 607 int 608 terminaltypeok(char *s) 609 { 610 char buf[1024]; 611 612 if (terminaltype == NULL) 613 return(1); 614 615 /* 616 * tgetent() will return 1 if the type is known, and 617 * 0 if it is not known. If it returns -1, it couldn't 618 * open the database. But if we can't open the database, 619 * it won't help to say we failed, because we won't be 620 * able to verify anything else. So, we treat -1 like 1. 621 */ 622 if (tgetent(buf, s) == 0) 623 return(0); 624 return(1); 625 } 626 627 /* 628 * Get a pty, scan input lines. 629 */ 630 void 631 doit(struct sockaddr *who) 632 { 633 int ptynum; 634 635 /* 636 * Find an available pty to use. 637 */ 638 #ifndef convex 639 pty = getpty(&ptynum); 640 if (pty < 0) 641 fatalmsg(net, "All network ports in use"); 642 #else 643 for (;;) { 644 char *lp; 645 646 if ((lp = getpty()) == NULL) 647 fatalmsg(net, "Out of ptys"); 648 649 if ((pty = open(lp, 2)) >= 0) { 650 strlcpy(line,lp,sizeof(line)); 651 line[5] = 't'; 652 break; 653 } 654 } 655 #endif 656 657 /* get name of connected client */ 658 if (realhostname_sa(remote_hostname, sizeof(remote_hostname) - 1, 659 who, who->sa_len) == HOSTNAME_INVALIDADDR && registerd_host_only) 660 fatalmsg(net, "Couldn't resolve your address into a host name.\r\n\ 661 Please contact your net administrator"); 662 remote_hostname[sizeof(remote_hostname) - 1] = '\0'; 663 664 (void) gethostname(host_name, sizeof(host_name) - 1); 665 host_name[sizeof(host_name) - 1] = '\0'; 666 hostname = host_name; 667 668 #ifdef AUTHENTICATION 669 #ifdef ENCRYPTION 670 /* The above #ifdefs should actually be "or"'ed, not "and"'ed. 671 * This is a byproduct of needing "#ifdef" and not "#if defined()" 672 * for unifdef. XXX MarkM 673 */ 674 auth_encrypt_init(hostname, remote_hostname, "TELNETD", 1); 675 #endif 676 #endif 677 678 init_env(); 679 /* 680 * get terminal type. 681 */ 682 *user_name = 0; 683 level = getterminaltype(user_name); 684 if (setenv("TERM", terminaltype ? terminaltype : "network", 1) == -1) 685 syslog(LOG_ERR, "setenv: cannot set TERM=%s: %m", terminaltype ? terminaltype : "network"); 686 687 telnet(net, pty, remote_hostname); /* begin server process */ 688 689 /*NOTREACHED*/ 690 } /* end of doit */ 691 692 /* 693 * Main loop. Select from pty and network, and 694 * hand data to telnet receiver finite state machine. 695 */ 696 void 697 telnet(int f, int p, char *host) 698 { 699 int on = 1; 700 #define TABBUFSIZ 512 701 char defent[TABBUFSIZ]; 702 char defstrs[TABBUFSIZ]; 703 #undef TABBUFSIZ 704 char *HE; 705 char *HN; 706 char *IM; 707 int nfd; 708 709 /* 710 * Initialize the slc mapping table. 711 */ 712 get_slc_defaults(); 713 714 /* 715 * Do some tests where it is desireable to wait for a response. 716 * Rather than doing them slowly, one at a time, do them all 717 * at once. 718 */ 719 if (my_state_is_wont(TELOPT_SGA)) 720 send_will(TELOPT_SGA, 1); 721 /* 722 * Is the client side a 4.2 (NOT 4.3) system? We need to know this 723 * because 4.2 clients are unable to deal with TCP urgent data. 724 * 725 * To find out, we send out a "DO ECHO". If the remote system 726 * answers "WILL ECHO" it is probably a 4.2 client, and we note 727 * that fact ("WILL ECHO" ==> that the client will echo what 728 * WE, the server, sends it; it does NOT mean that the client will 729 * echo the terminal input). 730 */ 731 send_do(TELOPT_ECHO, 1); 732 733 #ifdef LINEMODE 734 if (his_state_is_wont(TELOPT_LINEMODE)) { 735 /* Query the peer for linemode support by trying to negotiate 736 * the linemode option. 737 */ 738 linemode = 0; 739 editmode = 0; 740 send_do(TELOPT_LINEMODE, 1); /* send do linemode */ 741 } 742 #endif /* LINEMODE */ 743 744 /* 745 * Send along a couple of other options that we wish to negotiate. 746 */ 747 send_do(TELOPT_NAWS, 1); 748 send_will(TELOPT_STATUS, 1); 749 flowmode = 1; /* default flow control state */ 750 restartany = -1; /* uninitialized... */ 751 send_do(TELOPT_LFLOW, 1); 752 753 /* 754 * Spin, waiting for a response from the DO ECHO. However, 755 * some REALLY DUMB telnets out there might not respond 756 * to the DO ECHO. So, we spin looking for NAWS, (most dumb 757 * telnets so far seem to respond with WONT for a DO that 758 * they don't understand...) because by the time we get the 759 * response, it will already have processed the DO ECHO. 760 * Kludge upon kludge. 761 */ 762 while (his_will_wont_is_changing(TELOPT_NAWS)) 763 ttloop(); 764 765 /* 766 * But... 767 * The client might have sent a WILL NAWS as part of its 768 * startup code; if so, we'll be here before we get the 769 * response to the DO ECHO. We'll make the assumption 770 * that any implementation that understands about NAWS 771 * is a modern enough implementation that it will respond 772 * to our DO ECHO request; hence we'll do another spin 773 * waiting for the ECHO option to settle down, which is 774 * what we wanted to do in the first place... 775 */ 776 if (his_want_state_is_will(TELOPT_ECHO) && 777 his_state_is_will(TELOPT_NAWS)) { 778 while (his_will_wont_is_changing(TELOPT_ECHO)) 779 ttloop(); 780 } 781 /* 782 * On the off chance that the telnet client is broken and does not 783 * respond to the DO ECHO we sent, (after all, we did send the 784 * DO NAWS negotiation after the DO ECHO, and we won't get here 785 * until a response to the DO NAWS comes back) simulate the 786 * receipt of a will echo. This will also send a WONT ECHO 787 * to the client, since we assume that the client failed to 788 * respond because it believes that it is already in DO ECHO 789 * mode, which we do not want. 790 */ 791 if (his_want_state_is_will(TELOPT_ECHO)) { 792 DIAG(TD_OPTIONS, output_data("td: simulating recv\r\n")); 793 willoption(TELOPT_ECHO); 794 } 795 796 /* 797 * Finally, to clean things up, we turn on our echo. This 798 * will break stupid 4.2 telnets out of local terminal echo. 799 */ 800 801 if (my_state_is_wont(TELOPT_ECHO)) 802 send_will(TELOPT_ECHO, 1); 803 804 /* 805 * Turn on packet mode 806 */ 807 (void) ioctl(p, TIOCPKT, (char *)&on); 808 809 #if defined(LINEMODE) && defined(KLUDGELINEMODE) 810 /* 811 * Continuing line mode support. If client does not support 812 * real linemode, attempt to negotiate kludge linemode by sending 813 * the do timing mark sequence. 814 */ 815 if (lmodetype < REAL_LINEMODE) 816 send_do(TELOPT_TM, 1); 817 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */ 818 819 /* 820 * Call telrcv() once to pick up anything received during 821 * terminal type negotiation, 4.2/4.3 determination, and 822 * linemode negotiation. 823 */ 824 telrcv(); 825 826 (void) ioctl(f, FIONBIO, (char *)&on); 827 (void) ioctl(p, FIONBIO, (char *)&on); 828 829 #if defined(SO_OOBINLINE) 830 (void) setsockopt(net, SOL_SOCKET, SO_OOBINLINE, 831 (char *)&on, sizeof on); 832 #endif /* defined(SO_OOBINLINE) */ 833 834 #ifdef SIGTSTP 835 (void) signal(SIGTSTP, SIG_IGN); 836 #endif 837 #ifdef SIGTTOU 838 /* 839 * Ignoring SIGTTOU keeps the kernel from blocking us 840 * in ttioct() in /sys/tty.c. 841 */ 842 (void) signal(SIGTTOU, SIG_IGN); 843 #endif 844 845 (void) signal(SIGCHLD, cleanup); 846 847 #ifdef TIOCNOTTY 848 { 849 int t; 850 t = open(_PATH_TTY, O_RDWR); 851 if (t >= 0) { 852 (void) ioctl(t, TIOCNOTTY, NULL); 853 (void) close(t); 854 } 855 } 856 #endif 857 858 /* 859 * Show banner that getty never gave. 860 * 861 * We put the banner in the pty input buffer. This way, it 862 * gets carriage return null processing, etc., just like all 863 * other pty --> client data. 864 */ 865 866 if (getent(defent, "default") == 1) { 867 char *cp=defstrs; 868 869 HE = Getstr("he", &cp); 870 HN = Getstr("hn", &cp); 871 IM = Getstr("im", &cp); 872 if (HN && *HN) 873 (void) strlcpy(host_name, HN, sizeof(host_name)); 874 if (IM == NULL) 875 IM = strdup(""); 876 } else { 877 IM = strdup(DEFAULT_IM); 878 HE = NULL; 879 } 880 edithost(HE, host_name); 881 if (hostinfo && *IM) 882 putf(IM, ptyibuf2); 883 884 if (pcc) 885 (void) strncat(ptyibuf2, ptyip, pcc+1); 886 ptyip = ptyibuf2; 887 pcc = strlen(ptyip); 888 #ifdef LINEMODE 889 /* 890 * Last check to make sure all our states are correct. 891 */ 892 init_termbuf(); 893 localstat(); 894 #endif /* LINEMODE */ 895 896 DIAG(TD_REPORT, output_data("td: Entering processing loop\r\n")); 897 898 /* 899 * Startup the login process on the slave side of the terminal 900 * now. We delay this until here to insure option negotiation 901 * is complete. 902 */ 903 startslave(host, level, user_name); 904 905 nfd = ((f > p) ? f : p) + 1; 906 for (;;) { 907 fd_set ibits, obits, xbits; 908 int c; 909 910 if (ncc < 0 && pcc < 0) 911 break; 912 913 FD_ZERO(&ibits); 914 FD_ZERO(&obits); 915 FD_ZERO(&xbits); 916 /* 917 * Never look for input if there's still 918 * stuff in the corresponding output buffer 919 */ 920 if (nfrontp - nbackp || pcc > 0) { 921 FD_SET(f, &obits); 922 } else { 923 FD_SET(p, &ibits); 924 } 925 if (pfrontp - pbackp || ncc > 0) { 926 FD_SET(p, &obits); 927 } else { 928 FD_SET(f, &ibits); 929 } 930 if (!SYNCHing) { 931 FD_SET(f, &xbits); 932 } 933 if ((c = select(nfd, &ibits, &obits, &xbits, NULL)) < 1) { 934 if (c == -1) { 935 if (errno == EINTR) { 936 continue; 937 } 938 } 939 sleep(5); 940 continue; 941 } 942 943 /* 944 * Any urgent data? 945 */ 946 if (FD_ISSET(net, &xbits)) { 947 SYNCHing = 1; 948 } 949 950 /* 951 * Something to read from the network... 952 */ 953 if (FD_ISSET(net, &ibits)) { 954 #if !defined(SO_OOBINLINE) 955 /* 956 * In 4.2 (and 4.3 beta) systems, the 957 * OOB indication and data handling in the kernel 958 * is such that if two separate TCP Urgent requests 959 * come in, one byte of TCP data will be overlaid. 960 * This is fatal for Telnet, but we try to live 961 * with it. 962 * 963 * In addition, in 4.2 (and...), a special protocol 964 * is needed to pick up the TCP Urgent data in 965 * the correct sequence. 966 * 967 * What we do is: if we think we are in urgent 968 * mode, we look to see if we are "at the mark". 969 * If we are, we do an OOB receive. If we run 970 * this twice, we will do the OOB receive twice, 971 * but the second will fail, since the second 972 * time we were "at the mark", but there wasn't 973 * any data there (the kernel doesn't reset 974 * "at the mark" until we do a normal read). 975 * Once we've read the OOB data, we go ahead 976 * and do normal reads. 977 * 978 * There is also another problem, which is that 979 * since the OOB byte we read doesn't put us 980 * out of OOB state, and since that byte is most 981 * likely the TELNET DM (data mark), we would 982 * stay in the TELNET SYNCH (SYNCHing) state. 983 * So, clocks to the rescue. If we've "just" 984 * received a DM, then we test for the 985 * presence of OOB data when the receive OOB 986 * fails (and AFTER we did the normal mode read 987 * to clear "at the mark"). 988 */ 989 if (SYNCHing) { 990 int atmark; 991 992 (void) ioctl(net, SIOCATMARK, (char *)&atmark); 993 if (atmark) { 994 ncc = recv(net, netibuf, sizeof (netibuf), MSG_OOB); 995 if ((ncc == -1) && (errno == EINVAL)) { 996 ncc = read(net, netibuf, sizeof (netibuf)); 997 if (sequenceIs(didnetreceive, gotDM)) { 998 SYNCHing = stilloob(net); 999 } 1000 } 1001 } else { 1002 ncc = read(net, netibuf, sizeof (netibuf)); 1003 } 1004 } else { 1005 ncc = read(net, netibuf, sizeof (netibuf)); 1006 } 1007 settimer(didnetreceive); 1008 #else /* !defined(SO_OOBINLINE)) */ 1009 ncc = read(net, netibuf, sizeof (netibuf)); 1010 #endif /* !defined(SO_OOBINLINE)) */ 1011 if (ncc < 0 && errno == EWOULDBLOCK) 1012 ncc = 0; 1013 else { 1014 if (ncc <= 0) { 1015 break; 1016 } 1017 netip = netibuf; 1018 } 1019 DIAG((TD_REPORT | TD_NETDATA), 1020 output_data("td: netread %d chars\r\n", ncc)); 1021 DIAG(TD_NETDATA, printdata("nd", netip, ncc)); 1022 } 1023 1024 /* 1025 * Something to read from the pty... 1026 */ 1027 if (FD_ISSET(p, &ibits)) { 1028 pcc = read(p, ptyibuf, BUFSIZ); 1029 /* 1030 * On some systems, if we try to read something 1031 * off the master side before the slave side is 1032 * opened, we get EIO. 1033 */ 1034 if (pcc < 0 && (errno == EWOULDBLOCK || 1035 #ifdef EAGAIN 1036 errno == EAGAIN || 1037 #endif 1038 errno == EIO)) { 1039 pcc = 0; 1040 } else { 1041 if (pcc <= 0) 1042 break; 1043 #ifdef LINEMODE 1044 /* 1045 * If ioctl from pty, pass it through net 1046 */ 1047 if (ptyibuf[0] & TIOCPKT_IOCTL) { 1048 copy_termbuf(ptyibuf+1, pcc-1); 1049 localstat(); 1050 pcc = 1; 1051 } 1052 #endif /* LINEMODE */ 1053 if (ptyibuf[0] & TIOCPKT_FLUSHWRITE) { 1054 netclear(); /* clear buffer back */ 1055 #ifndef NO_URGENT 1056 /* 1057 * There are client telnets on some 1058 * operating systems get screwed up 1059 * royally if we send them urgent 1060 * mode data. 1061 */ 1062 output_data("%c%c", IAC, DM); 1063 neturg = nfrontp-1; /* off by one XXX */ 1064 DIAG(TD_OPTIONS, 1065 printoption("td: send IAC", DM)); 1066 1067 #endif 1068 } 1069 if (his_state_is_will(TELOPT_LFLOW) && 1070 (ptyibuf[0] & 1071 (TIOCPKT_NOSTOP|TIOCPKT_DOSTOP))) { 1072 int newflow = 1073 ptyibuf[0] & TIOCPKT_DOSTOP ? 1 : 0; 1074 if (newflow != flowmode) { 1075 flowmode = newflow; 1076 output_data("%c%c%c%c%c%c", 1077 IAC, SB, TELOPT_LFLOW, 1078 flowmode ? LFLOW_ON 1079 : LFLOW_OFF, 1080 IAC, SE); 1081 DIAG(TD_OPTIONS, printsub('>', 1082 (unsigned char *)nfrontp-4, 1083 4);); 1084 } 1085 } 1086 pcc--; 1087 ptyip = ptyibuf+1; 1088 } 1089 } 1090 1091 while (pcc > 0) { 1092 if ((&netobuf[BUFSIZ] - nfrontp) < 2) 1093 break; 1094 c = *ptyip++ & 0377, pcc--; 1095 if (c == IAC) 1096 output_data("%c", c); 1097 output_data("%c", c); 1098 if ((c == '\r') && (my_state_is_wont(TELOPT_BINARY))) { 1099 if (pcc > 0 && ((*ptyip & 0377) == '\n')) { 1100 output_data("%c", *ptyip++ & 0377); 1101 pcc--; 1102 } else 1103 output_data("%c", '\0'); 1104 } 1105 } 1106 1107 if (FD_ISSET(f, &obits) && (nfrontp - nbackp) > 0) 1108 netflush(); 1109 if (ncc > 0) 1110 telrcv(); 1111 if (FD_ISSET(p, &obits) && (pfrontp - pbackp) > 0) 1112 ptyflush(); 1113 } 1114 cleanup(0); 1115 } /* end of telnet */ 1116 1117 #ifndef TCSIG 1118 # ifdef TIOCSIG 1119 # define TCSIG TIOCSIG 1120 # endif 1121 #endif 1122 1123 /* 1124 * Send interrupt to process on other side of pty. 1125 * If it is in raw mode, just write NULL; 1126 * otherwise, write intr char. 1127 */ 1128 void 1129 interrupt(void) 1130 { 1131 ptyflush(); /* half-hearted */ 1132 1133 #ifdef TCSIG 1134 (void) ioctl(pty, TCSIG, (char *)SIGINT); 1135 #else /* TCSIG */ 1136 init_termbuf(); 1137 *pfrontp++ = slctab[SLC_IP].sptr ? 1138 (unsigned char)*slctab[SLC_IP].sptr : '\177'; 1139 #endif /* TCSIG */ 1140 } 1141 1142 /* 1143 * Send quit to process on other side of pty. 1144 * If it is in raw mode, just write NULL; 1145 * otherwise, write quit char. 1146 */ 1147 void 1148 sendbrk(void) 1149 { 1150 ptyflush(); /* half-hearted */ 1151 #ifdef TCSIG 1152 (void) ioctl(pty, TCSIG, (char *)SIGQUIT); 1153 #else /* TCSIG */ 1154 init_termbuf(); 1155 *pfrontp++ = slctab[SLC_ABORT].sptr ? 1156 (unsigned char)*slctab[SLC_ABORT].sptr : '\034'; 1157 #endif /* TCSIG */ 1158 } 1159 1160 void 1161 sendsusp(void) 1162 { 1163 #ifdef SIGTSTP 1164 ptyflush(); /* half-hearted */ 1165 # ifdef TCSIG 1166 (void) ioctl(pty, TCSIG, (char *)SIGTSTP); 1167 # else /* TCSIG */ 1168 *pfrontp++ = slctab[SLC_SUSP].sptr ? 1169 (unsigned char)*slctab[SLC_SUSP].sptr : '\032'; 1170 # endif /* TCSIG */ 1171 #endif /* SIGTSTP */ 1172 } 1173 1174 /* 1175 * When we get an AYT, if ^T is enabled, use that. Otherwise, 1176 * just send back "[Yes]". 1177 */ 1178 void 1179 recv_ayt(void) 1180 { 1181 #if defined(SIGINFO) && defined(TCSIG) 1182 if (slctab[SLC_AYT].sptr && *slctab[SLC_AYT].sptr != _POSIX_VDISABLE) { 1183 (void) ioctl(pty, TCSIG, (char *)SIGINFO); 1184 return; 1185 } 1186 #endif 1187 output_data("\r\n[Yes]\r\n"); 1188 } 1189 1190 void 1191 doeof(void) 1192 { 1193 init_termbuf(); 1194 1195 #if defined(LINEMODE) && defined(USE_TERMIO) && (VEOF == VMIN) 1196 if (!tty_isediting()) { 1197 extern char oldeofc; 1198 *pfrontp++ = oldeofc; 1199 return; 1200 } 1201 #endif 1202 *pfrontp++ = slctab[SLC_EOF].sptr ? 1203 (unsigned char)*slctab[SLC_EOF].sptr : '\004'; 1204 } 1205