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