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