1 /* $NetBSD: termstat.c,v 1.11 2001/08/24 00:14:04 wiz Exp $ */ 2 3 /* 4 * Copyright (c) 1989, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/cdefs.h> 37 #ifndef lint 38 #if 0 39 static char sccsid[] = "@(#)termstat.c 8.2 (Berkeley) 5/30/95"; 40 #else 41 __RCSID("$NetBSD: termstat.c,v 1.11 2001/08/24 00:14:04 wiz Exp $"); 42 #endif 43 #endif /* not lint */ 44 45 #include "telnetd.h" 46 47 #if defined(ENCRYPTION) 48 #include <libtelnet/encrypt.h> 49 #endif 50 51 /* 52 * local variables 53 */ 54 int def_tspeed = -1, def_rspeed = -1; 55 #ifdef TIOCSWINSZ 56 int def_row = 0, def_col = 0; 57 #endif 58 #ifdef LINEMODE 59 static int _terminit = 0; 60 #endif /* LINEMODE */ 61 62 63 #ifdef LINEMODE 64 /* 65 * localstat 66 * 67 * This function handles all management of linemode. 68 * 69 * Linemode allows the client to do the local editing of data 70 * and send only complete lines to the server. Linemode state is 71 * based on the state of the pty driver. If the pty is set for 72 * external processing, then we can use linemode. Further, if we 73 * can use real linemode, then we can look at the edit control bits 74 * in the pty to determine what editing the client should do. 75 * 76 * Linemode support uses the following state flags to keep track of 77 * current and desired linemode state. 78 * alwayslinemode : true if -l was specified on the telnetd 79 * command line. It means to have linemode on as much as 80 * possible. 81 * 82 * lmodetype: signifies whether the client can 83 * handle real linemode, or if use of kludgeomatic linemode 84 * is preferred. It will be set to one of the following: 85 * REAL_LINEMODE : use linemode option 86 * NO_KLUDGE : don't initiate kludge linemode. 87 * KLUDGE_LINEMODE : use kludge linemode 88 * NO_LINEMODE : client is ignorant of linemode 89 * 90 * linemode, uselinemode : linemode is true if linemode 91 * is currently on, uselinemode is the state that we wish 92 * to be in. If another function wishes to turn linemode 93 * on or off, it sets or clears uselinemode. 94 * 95 * editmode, useeditmode : like linemode/uselinemode, but 96 * these contain the edit mode states (edit and trapsig). 97 * 98 * The state variables correspond to some of the state information 99 * in the pty. 100 * linemode: 101 * In real linemode, this corresponds to whether the pty 102 * expects external processing of incoming data. 103 * In kludge linemode, this more closely corresponds to the 104 * whether normal processing is on or not. (ICANON in 105 * system V, or COOKED mode in BSD.) 106 * If the -l option was specified (alwayslinemode), then 107 * an attempt is made to force external processing on at 108 * all times. 109 * 110 * The following heuristics are applied to determine linemode 111 * handling within the server. 112 * 1) Early on in starting up the server, an attempt is made 113 * to negotiate the linemode option. If this succeeds 114 * then lmodetype is set to REAL_LINEMODE and all linemode 115 * processing occurs in the context of the linemode option. 116 * 2) If the attempt to negotiate the linemode option failed, 117 * and the "-k" (don't initiate kludge linemode) isn't set, 118 * then we try to use kludge linemode. We test for this 119 * capability by sending "do Timing Mark". If a positive 120 * response comes back, then we assume that the client 121 * understands kludge linemode (ech!) and the 122 * lmodetype flag is set to KLUDGE_LINEMODE. 123 * 3) Otherwise, linemode is not supported at all and 124 * lmodetype remains set to NO_LINEMODE (which happens 125 * to be 0 for convenience). 126 * 4) At any time a command arrives that implies a higher 127 * state of linemode support in the client, we move to that 128 * linemode support. 129 * 130 * A short explanation of kludge linemode is in order here. 131 * 1) The heuristic to determine support for kludge linemode 132 * is to send a do timing mark. We assume that a client 133 * that supports timing marks also supports kludge linemode. 134 * A risky proposition at best. 135 * 2) Further negotiation of linemode is done by changing the 136 * the server's state regarding SGA. If server will SGA, 137 * then linemode is off, if server won't SGA, then linemode 138 * is on. 139 */ 140 void 141 localstat() 142 { 143 int need_will_echo = 0; 144 145 146 /* 147 * Check for state of BINARY options. 148 */ 149 if (tty_isbinaryin()) { 150 if (his_want_state_is_wont(TELOPT_BINARY)) 151 send_do(TELOPT_BINARY, 1); 152 } else { 153 if (his_want_state_is_will(TELOPT_BINARY)) 154 send_dont(TELOPT_BINARY, 1); 155 } 156 157 if (tty_isbinaryout()) { 158 if (my_want_state_is_wont(TELOPT_BINARY)) 159 send_will(TELOPT_BINARY, 1); 160 } else { 161 if (my_want_state_is_will(TELOPT_BINARY)) 162 send_wont(TELOPT_BINARY, 1); 163 } 164 165 /* 166 * Check for changes to flow control if client supports it. 167 */ 168 flowstat(); 169 170 /* 171 * Check linemode on/off state 172 */ 173 uselinemode = tty_linemode(); 174 175 /* 176 * If alwayslinemode is on, and pty is changing to turn it off, then 177 * force linemode back on. 178 */ 179 if (alwayslinemode && linemode && !uselinemode) { 180 uselinemode = 1; 181 tty_setlinemode(uselinemode); 182 } 183 184 #ifdef ENCRYPTION 185 /* 186 * If the terminal is not echoing, but editing is enabled, 187 * something like password input is going to happen, so 188 * if we the other side is not currently sending encrypted 189 * data, ask the other side to start encrypting. 190 */ 191 if (his_state_is_will(TELOPT_ENCRYPT)) { 192 static int enc_passwd = 0; 193 if (uselinemode && !tty_isecho() && tty_isediting() 194 && (enc_passwd == 0) && !decrypt_input) { 195 encrypt_send_request_start(); 196 enc_passwd = 1; 197 } else if (enc_passwd) { 198 encrypt_send_request_end(); 199 enc_passwd = 0; 200 } 201 } 202 #endif /* ENCRYPTION */ 203 204 /* 205 * Do echo mode handling as soon as we know what the 206 * linemode is going to be. 207 * If the pty has echo turned off, then tell the client that 208 * the server will echo. If echo is on, then the server 209 * will echo if in character mode, but in linemode the 210 * client should do local echoing. The state machine will 211 * not send anything if it is unnecessary, so don't worry 212 * about that here. 213 * 214 * If we need to send the WILL ECHO (because echo is off), 215 * then delay that until after we have changed the MODE. 216 * This way, when the user is turning off both editing 217 * and echo, the client will get editing turned off first. 218 * This keeps the client from going into encryption mode 219 * and then right back out if it is doing auto-encryption 220 * when passwords are being typed. 221 */ 222 if (uselinemode) { 223 if (tty_isecho()) 224 send_wont(TELOPT_ECHO, 1); 225 else 226 need_will_echo = 1; 227 #ifdef KLUDGELINEMODE 228 if (lmodetype == KLUDGE_OK) 229 lmodetype = KLUDGE_LINEMODE; 230 #endif 231 } 232 233 /* 234 * If linemode is being turned off, send appropriate 235 * command and then we're all done. 236 */ 237 if (!uselinemode && linemode) { 238 # ifdef KLUDGELINEMODE 239 if (lmodetype == REAL_LINEMODE) { 240 # endif /* KLUDGELINEMODE */ 241 send_dont(TELOPT_LINEMODE, 1); 242 # ifdef KLUDGELINEMODE 243 } else if (lmodetype == KLUDGE_LINEMODE) 244 send_will(TELOPT_SGA, 1); 245 # endif /* KLUDGELINEMODE */ 246 send_will(TELOPT_ECHO, 1); 247 linemode = uselinemode; 248 goto done; 249 } 250 251 # ifdef KLUDGELINEMODE 252 /* 253 * If using real linemode check edit modes for possible later use. 254 * If we are in kludge linemode, do the SGA negotiation. 255 */ 256 if (lmodetype == REAL_LINEMODE) { 257 # endif /* KLUDGELINEMODE */ 258 useeditmode = 0; 259 if (tty_isediting()) 260 useeditmode |= MODE_EDIT; 261 if (tty_istrapsig()) 262 useeditmode |= MODE_TRAPSIG; 263 if (tty_issofttab()) 264 useeditmode |= MODE_SOFT_TAB; 265 if (tty_islitecho()) 266 useeditmode |= MODE_LIT_ECHO; 267 # ifdef KLUDGELINEMODE 268 } else if (lmodetype == KLUDGE_LINEMODE) { 269 if (tty_isediting() && uselinemode) 270 send_wont(TELOPT_SGA, 1); 271 else 272 send_will(TELOPT_SGA, 1); 273 } 274 # endif /* KLUDGELINEMODE */ 275 276 /* 277 * Negotiate linemode on if pty state has changed to turn it on. 278 * Send appropriate command and send along edit mode, then all done. 279 */ 280 if (uselinemode && !linemode) { 281 # ifdef KLUDGELINEMODE 282 if (lmodetype == KLUDGE_LINEMODE) { 283 send_wont(TELOPT_SGA, 1); 284 } else if (lmodetype == REAL_LINEMODE) { 285 # endif /* KLUDGELINEMODE */ 286 send_do(TELOPT_LINEMODE, 1); 287 /* send along edit modes */ 288 (void) output_data("%c%c%c%c%c%c%c", IAC, SB, 289 TELOPT_LINEMODE, LM_MODE, useeditmode, 290 IAC, SE); 291 editmode = useeditmode; 292 # ifdef KLUDGELINEMODE 293 } 294 # endif /* KLUDGELINEMODE */ 295 linemode = uselinemode; 296 goto done; 297 } 298 299 # ifdef KLUDGELINEMODE 300 /* 301 * None of what follows is of any value if not using 302 * real linemode. 303 */ 304 if (lmodetype < REAL_LINEMODE) 305 goto done; 306 # endif /* KLUDGELINEMODE */ 307 308 if (linemode && his_state_is_will(TELOPT_LINEMODE)) { 309 /* 310 * If edit mode changed, send edit mode. 311 */ 312 if (useeditmode != editmode) { 313 /* 314 * Send along appropriate edit mode mask. 315 */ 316 (void) output_data("%c%c%c%c%c%c%c", IAC, SB, 317 TELOPT_LINEMODE, LM_MODE, useeditmode, 318 IAC, SE); 319 editmode = useeditmode; 320 } 321 322 323 /* 324 * Check for changes to special characters in use. 325 */ 326 start_slc(0); 327 check_slc(); 328 (void) end_slc(0); 329 } 330 331 done: 332 if (need_will_echo) 333 send_will(TELOPT_ECHO, 1); 334 /* 335 * Some things should be deferred until after the pty state has 336 * been set by the local process. Do those things that have been 337 * deferred now. This only happens once. 338 */ 339 if (_terminit == 0) { 340 _terminit = 1; 341 defer_terminit(); 342 } 343 344 netflush(); 345 set_termbuf(); 346 return; 347 348 } /* end of localstat */ 349 #endif /* LINEMODE */ 350 351 /* 352 * flowstat 353 * 354 * Check for changes to flow control 355 */ 356 void 357 flowstat() 358 { 359 if (his_state_is_will(TELOPT_LFLOW)) { 360 if (tty_flowmode() != flowmode) { 361 flowmode = tty_flowmode(); 362 (void) output_data("%c%c%c%c%c%c", 363 IAC, SB, TELOPT_LFLOW, 364 flowmode ? LFLOW_ON : LFLOW_OFF, 365 IAC, SE); 366 } 367 if (tty_restartany() != restartany) { 368 restartany = tty_restartany(); 369 (void) output_data("%c%c%c%c%c%c", 370 IAC, SB, TELOPT_LFLOW, 371 restartany ? LFLOW_RESTART_ANY 372 : LFLOW_RESTART_XON, 373 IAC, SE); 374 } 375 } 376 } 377 378 /* 379 * clientstat 380 * 381 * Process linemode related requests from the client. 382 * Client can request a change to only one of linemode, editmode or slc's 383 * at a time, and if using kludge linemode, then only linemode may be 384 * affected. 385 */ 386 void 387 clientstat(code, parm1, parm2) 388 register int code, parm1, parm2; 389 { 390 391 /* 392 * Get a copy of terminal characteristics. 393 */ 394 init_termbuf(); 395 396 /* 397 * Process request from client. code tells what it is. 398 */ 399 switch (code) { 400 #ifdef LINEMODE 401 case TELOPT_LINEMODE: 402 /* 403 * Don't do anything unless client is asking us to change 404 * modes. 405 */ 406 uselinemode = (parm1 == WILL); 407 if (uselinemode != linemode) { 408 # ifdef KLUDGELINEMODE 409 /* 410 * If using kludge linemode, make sure that 411 * we can do what the client asks. 412 * We can not turn off linemode if alwayslinemode 413 * and the ICANON bit is set. 414 */ 415 if (lmodetype == KLUDGE_LINEMODE) { 416 if (alwayslinemode && tty_isediting()) { 417 uselinemode = 1; 418 } 419 } 420 421 /* 422 * Quit now if we can't do it. 423 */ 424 if (uselinemode == linemode) 425 return; 426 427 /* 428 * If using real linemode and linemode is being 429 * turned on, send along the edit mode mask. 430 */ 431 if (lmodetype == REAL_LINEMODE && uselinemode) 432 # else /* KLUDGELINEMODE */ 433 if (uselinemode) 434 # endif /* KLUDGELINEMODE */ 435 { 436 useeditmode = 0; 437 if (tty_isediting()) 438 useeditmode |= MODE_EDIT; 439 if (tty_istrapsig()) 440 useeditmode |= MODE_TRAPSIG; 441 if (tty_issofttab()) 442 useeditmode |= MODE_SOFT_TAB; 443 if (tty_islitecho()) 444 useeditmode |= MODE_LIT_ECHO; 445 (void) output_data("%c%c%c%c%c%c%c", IAC, 446 SB, TELOPT_LINEMODE, LM_MODE, 447 useeditmode, IAC, SE); 448 editmode = useeditmode; 449 } 450 451 452 tty_setlinemode(uselinemode); 453 454 linemode = uselinemode; 455 456 if (!linemode) 457 send_will(TELOPT_ECHO, 1); 458 } 459 break; 460 461 case LM_MODE: 462 { 463 register int ack, changed; 464 465 /* 466 * Client has sent along a mode mask. If it agrees with 467 * what we are currently doing, ignore it; if not, it could 468 * be viewed as a request to change. Note that the server 469 * will change to the modes in an ack if it is different from 470 * what we currently have, but we will not ack the ack. 471 */ 472 useeditmode &= MODE_MASK; 473 ack = (useeditmode & MODE_ACK); 474 useeditmode &= ~MODE_ACK; 475 476 if ((changed = (useeditmode ^ editmode))) { 477 /* 478 * This check is for a timing problem. If the 479 * state of the tty has changed (due to the user 480 * application) we need to process that info 481 * before we write in the state contained in the 482 * ack!!! This gets out the new MODE request, 483 * and when the ack to that command comes back 484 * we'll set it and be in the right mode. 485 */ 486 if (ack) 487 localstat(); 488 if (changed & MODE_EDIT) 489 tty_setedit(useeditmode & MODE_EDIT); 490 491 if (changed & MODE_TRAPSIG) 492 tty_setsig(useeditmode & MODE_TRAPSIG); 493 494 if (changed & MODE_SOFT_TAB) 495 tty_setsofttab(useeditmode & MODE_SOFT_TAB); 496 497 if (changed & MODE_LIT_ECHO) 498 tty_setlitecho(useeditmode & MODE_LIT_ECHO); 499 500 set_termbuf(); 501 502 if (!ack) { 503 (void) output_data("%c%c%c%c%c%c%c", IAC, 504 SB, TELOPT_LINEMODE, LM_MODE, 505 useeditmode|MODE_ACK, 506 IAC, SE); 507 } 508 509 editmode = useeditmode; 510 } 511 512 break; 513 514 } /* end of case LM_MODE */ 515 #endif /* LINEMODE */ 516 517 case TELOPT_NAWS: 518 #ifdef TIOCSWINSZ 519 { 520 struct winsize ws; 521 522 def_col = parm1; 523 def_row = parm2; 524 #ifdef LINEMODE 525 /* 526 * Defer changing window size until after terminal is 527 * initialized. 528 */ 529 if (terminit() == 0) 530 return; 531 #endif /* LINEMODE */ 532 533 /* 534 * Change window size as requested by client. 535 */ 536 537 ws.ws_col = parm1; 538 ws.ws_row = parm2; 539 (void) ioctl(pty, TIOCSWINSZ, (char *)&ws); 540 } 541 #endif /* TIOCSWINSZ */ 542 543 break; 544 545 case TELOPT_TSPEED: 546 { 547 def_tspeed = parm1; 548 def_rspeed = parm2; 549 #ifdef LINEMODE 550 /* 551 * Defer changing the terminal speed. 552 */ 553 if (terminit() == 0) 554 return; 555 #endif /* LINEMODE */ 556 /* 557 * Change terminal speed as requested by client. 558 * We set the receive speed first, so that if we can't 559 * store separate receive and transmit speeds, the transmit 560 * speed will take precedence. 561 */ 562 tty_rspeed(parm2); 563 tty_tspeed(parm1); 564 set_termbuf(); 565 566 break; 567 568 } /* end of case TELOPT_TSPEED */ 569 570 default: 571 /* What? */ 572 break; 573 } /* end of switch */ 574 575 576 netflush(); 577 578 } /* end of clientstat */ 579 580 581 #ifdef LINEMODE 582 /* 583 * defer_terminit 584 * 585 * Some things should not be done until after the login process has started 586 * and all the pty modes are set to what they are supposed to be. This 587 * function is called when the pty state has been processed for the first time. 588 * It calls other functions that do things that were deferred in each module. 589 */ 590 void 591 defer_terminit() 592 { 593 594 /* 595 * local stuff that got deferred. 596 */ 597 if (def_tspeed != -1) { 598 clientstat(TELOPT_TSPEED, def_tspeed, def_rspeed); 599 def_tspeed = def_rspeed = 0; 600 } 601 602 #ifdef TIOCSWINSZ 603 if (def_col || def_row) { 604 struct winsize ws; 605 606 memset((char *)&ws, 0, sizeof(ws)); 607 ws.ws_col = def_col; 608 ws.ws_row = def_row; 609 (void) ioctl(pty, TIOCSWINSZ, (char *)&ws); 610 } 611 #endif 612 613 /* 614 * The only other module that currently defers anything. 615 */ 616 deferslc(); 617 618 } /* end of defer_terminit */ 619 620 /* 621 * terminit 622 * 623 * Returns true if the pty state has been processed yet. 624 */ 625 int 626 terminit() 627 { 628 return(_terminit); 629 630 } /* end of terminit */ 631 #endif /* LINEMODE */ 632