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