Debian Codegraph @

telnetd/termstat.c

raw
1 /*
2 Copyright (C) 1993-2026 Free Software Foundation, Inc.
3
4 This file is part of GNU Inetutils.
5
6 GNU Inetutils is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or (at
9 your option) any later version.
10
11 GNU Inetutils is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see `http://www.gnu.org/licenses/'. */
18
19 /*
20 * Copyright (c) 1989, 1993
21 * The Regents of the University of California. All rights reserved.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. Neither the name of the University nor the names of its contributors
32 * may be used to endorse or promote products derived from this software
33 * without specific prior written permission.
34 *
35 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
36 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
39 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 * SUCH DAMAGE.
46 */
47
48 #include <config.h>
49
50 #include "telnetd.h"
51
52 #include <fcntl.h> /* Solaris */
53
54 /*
55 * local variables
56 */
57 int def_tspeed = -1, def_rspeed = -1;
58 #ifdef TIOCSWINSZ
59 int def_row = 0, def_col = 0;
60 #endif
61 static int _terminit = 0;
62
63 #if defined CRAY2 && defined UNICOS5
64 int newmap = 1; /* nonzero if \n maps to ^M^J */
65 #endif
66
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 (void)
145 {
146 void netflush (void);
147 int need_will_echo = 0;
148
149 #if defined CRAY2 && defined UNICOS5
150 /*
151 * Keep track of that ol' CR/NL mapping while we're in the
152 * neighborhood.
153 */
154 newmap = tty_isnewmap ();
155 #endif /* defined(CRAY2) && defined(UNICOS5) */
156
157 /*
158 * Check for state of BINARY options.
159 */
160 if (tty_isbinaryin ())
161 {
162 if (his_want_state_is_wont (TELOPT_BINARY))
163 send_do (TELOPT_BINARY, 1);
164 }
165 else
166 {
167 if (his_want_state_is_will (TELOPT_BINARY))
168 send_dont (TELOPT_BINARY, 1);
169 }
170
171 if (tty_isbinaryout ())
172 {
173 if (my_want_state_is_wont (TELOPT_BINARY))
174 send_will (TELOPT_BINARY, 1);
175 }
176 else
177 {
178 if (my_want_state_is_will (TELOPT_BINARY))
179 send_wont (TELOPT_BINARY, 1);
180 }
181
182 /*
183 * Check for changes to flow control if client supports it.
184 */
185 flowstat ();
186
187 /*
188 * Check linemode on/off state
189 */
190 uselinemode = tty_linemode ();
191
192 /*
193 * If alwayslinemode is on, and pty is changing to turn it off, then
194 * force linemode back on.
195 */
196 if (alwayslinemode && linemode && !uselinemode)
197 {
198 uselinemode = 1;
199 tty_setlinemode (uselinemode);
200 }
201
202 #ifdef ENCRYPTION
203 /*
204 * If the terminal is not echoing, but editing is enabled,
205 * something like password input is going to happen, so
206 * if we the other side is not currently sending encrypted
207 * data, ask the other side to start encrypting.
208 */
209 if (his_state_is_will (TELOPT_ENCRYPT))
210 {
211 static int enc_passwd = 0;
212 if (uselinemode && !tty_isecho () && tty_isediting ()
213 && (enc_passwd == 0) && !decrypt_input)
214 {
215 encrypt_send_request_start ();
216 enc_passwd = 1;
217 }
218 else if (enc_passwd)
219 {
220 encrypt_send_request_end ();
221 enc_passwd = 0;
222 }
223 }
224 #endif /* ENCRYPTION */
225
226 /*
227 * Do echo mode handling as soon as we know what the
228 * linemode is going to be.
229 * If the pty has echo turned off, then tell the client that
230 * the server will echo. If echo is on, then the server
231 * will echo if in character mode, but in linemode the
232 * client should do local echoing. The state machine will
233 * not send anything if it is unnecessary, so don't worry
234 * about that here.
235 *
236 * If we need to send the WILL ECHO (because echo is off),
237 * then delay that until after we have changed the MODE.
238 * This way, when the user is turning off both editing
239 * and echo, the client will get editing turned off first.
240 * This keeps the client from going into encryption mode
241 * and then right back out if it is doing auto-encryption
242 * when passwords are being typed.
243 */
244 if (uselinemode)
245 {
246 if (tty_isecho ())
247 send_wont (TELOPT_ECHO, 1);
248 else
249 need_will_echo = 1;
250 if (lmodetype == KLUDGE_OK)
251 lmodetype = KLUDGE_LINEMODE;
252 }
253
254 /*
255 * If linemode is being turned off, send appropriate
256 * command and then we're all done.
257 */
258 if (!uselinemode && linemode)
259 {
260 if (lmodetype == REAL_LINEMODE)
261 {
262 send_dont (TELOPT_LINEMODE, 1);
263 }
264 else if (lmodetype == KLUDGE_LINEMODE)
265 send_will (TELOPT_SGA, 1);
266 send_will (TELOPT_ECHO, 1);
267 linemode = uselinemode;
268 goto done;
269 }
270
271 /*
272 * If using real linemode check edit modes for possible later use.
273 * If we are in kludge linemode, do the SGA negotiation.
274 */
275 if (lmodetype == REAL_LINEMODE)
276 {
277 useeditmode = 0;
278 if (tty_isediting ())
279 useeditmode |= MODE_EDIT;
280 if (tty_istrapsig ())
281 useeditmode |= MODE_TRAPSIG;
282 if (tty_issofttab ())
283 useeditmode |= MODE_SOFT_TAB;
284 if (tty_islitecho ())
285 useeditmode |= MODE_LIT_ECHO;
286 }
287 else if (lmodetype == KLUDGE_LINEMODE)
288 {
289 if (tty_isediting () && uselinemode)
290 send_wont (TELOPT_SGA, 1);
291 else
292 send_will (TELOPT_SGA, 1);
293 }
294
295 /*
296 * Negotiate linemode on if pty state has changed to turn it on.
297 * Send appropriate command and send along edit mode, then all done.
298 */
299 if (uselinemode && !linemode)
300 {
301 if (lmodetype == KLUDGE_LINEMODE)
302 {
303 send_wont (TELOPT_SGA, 1);
304 }
305 else if (lmodetype == REAL_LINEMODE)
306 {
307 char data[8];
308
309 send_do (TELOPT_LINEMODE, 1);
310 /* send along edit modes */
311 sprintf (data, "%c%c%c%c%c%c%c",
312 IAC, SB, TELOPT_LINEMODE, LM_MODE, useeditmode, IAC, SE);
313 net_output_datalen (data, sizeof (data));
314
315 editmode = useeditmode;
316 }
317 linemode = uselinemode;
318 goto done;
319 }
320
321 /*
322 * None of what follows is of any value if not using
323 * real linemode.
324 */
325 if (lmodetype < REAL_LINEMODE)
326 goto done;
327
328 if (linemode && his_state_is_will (TELOPT_LINEMODE))
329 {
330 /*
331 * If edit mode changed, send edit mode.
332 */
333 if (useeditmode != editmode)
334 {
335 /*
336 * Send along appropriate edit mode mask.
337 */
338 char data[8];
339
340 sprintf (data, "%c%c%c%c%c%c%c",
341 IAC, SB, TELOPT_LINEMODE, LM_MODE, useeditmode, IAC, SE);
342 net_output_datalen (data, sizeof (data));
343
344 editmode = useeditmode;
345 }
346
347
348 /*
349 * Check for changes to special characters in use.
350 */
351 start_slc (0);
352 check_slc ();
353 end_slc (0);
354 }
355
356 done:
357 if (need_will_echo)
358 send_will (TELOPT_ECHO, 1);
359 /*
360 * Some things should be deferred until after the pty state has
361 * been set by the local process. Do those things that have been
362 * deferred now. This only happens once.
363 */
364 if (_terminit == 0)
365 {
366 _terminit = 1;
367 defer_terminit ();
368 }
369
370 netflush ();
371 set_termbuf ();
372 return;
373
374 } /* end of localstat */
375
376 /*
377 * flowstat
378 *
379 * Check for changes to flow control
380 */
381 void
382 flowstat (void)
383 {
384 if (his_state_is_will (TELOPT_LFLOW))
385 {
386 char data[7];
387
388 if (tty_flowmode () != flowmode)
389 {
390 flowmode = tty_flowmode ();
391 sprintf (data, "%c%c%c%c%c%c",
392 IAC, SB, TELOPT_LFLOW,
393 flowmode ? LFLOW_ON : LFLOW_OFF, IAC, SE);
394 net_output_datalen (data, sizeof (data));
395 }
396 if (tty_restartany () != restartany)
397 {
398 restartany = tty_restartany ();
399 sprintf (data, "%c%c%c%c%c%c",
400 IAC, SB, TELOPT_LFLOW,
401 restartany ? LFLOW_RESTART_ANY : LFLOW_RESTART_XON,
402 IAC, SE);
403 net_output_datalen (data, sizeof (data));
404 }
405 }
406 }
407
408 /*
409 * clientstat
410 *
411 * Process linemode related requests from the client.
412 * Client can request a change to only one of linemode, editmode or slc's
413 * at a time, and if using kludge linemode, then only linemode may be
414 * affected.
415 */
416 void
417 clientstat (int code, int parm1, int parm2)
418 {
419 void netflush (void);
420
421 /*
422 * Get a copy of terminal characteristics.
423 */
424 init_termbuf ();
425
426 /*
427 * Process request from client. code tells what it is.
428 */
429 switch (code)
430 {
431 case TELOPT_LINEMODE:
432 /*
433 * Don't do anything unless client is asking us to change
434 * modes.
435 */
436 uselinemode = (parm1 == WILL);
437 if (uselinemode != linemode)
438 {
439 /*
440 * If using kludge linemode, make sure that
441 * we can do what the client asks.
442 * We cannot turn off linemode if alwayslinemode
443 * and the ICANON bit is set.
444 */
445 if (lmodetype == KLUDGE_LINEMODE)
446 {
447 if (alwayslinemode && tty_isediting ())
448 {
449 uselinemode = 1;
450 }
451 }
452
453 /*
454 * Quit now if we can't do it.
455 */
456 if (uselinemode == linemode)
457 return;
458
459 /*
460 * If using real linemode and linemode is being
461 * turned on, send along the edit mode mask.
462 */
463 if (lmodetype == REAL_LINEMODE && uselinemode)
464 if (uselinemode)
465 {
466 char data[8];
467
468 useeditmode = 0;
469 if (tty_isediting ())
470 useeditmode |= MODE_EDIT;
471 if (tty_istrapsig ())
472 useeditmode |= MODE_TRAPSIG;
473 if (tty_issofttab ())
474 useeditmode |= MODE_SOFT_TAB;
475 if (tty_islitecho ())
476 useeditmode |= MODE_LIT_ECHO;
477
478 sprintf (data, "%c%c%c%c%c%c%c",
479 IAC, SB, TELOPT_LINEMODE,
480 LM_MODE, useeditmode, IAC, SE);
481 net_output_datalen (data, sizeof (data));
482
483 editmode = useeditmode;
484 }
485
486
487 tty_setlinemode (uselinemode);
488
489 linemode = uselinemode;
490
491 if (!linemode)
492 send_will (TELOPT_ECHO, 1);
493 }
494 break;
495
496 case LM_MODE:
497 {
498 int ack, changed;
499
500 /*
501 * Client has sent along a mode mask. If it agrees with
502 * what we are currently doing, ignore it; if not, it could
503 * be viewed as a request to change. Note that the server
504 * will change to the modes in an ack if it is different from
505 * what we currently have, but we will not ack the ack.
506 */
507 useeditmode &= MODE_MASK;
508 ack = (useeditmode & MODE_ACK);
509 useeditmode &= ~MODE_ACK;
510
511 if ((changed = (useeditmode ^ editmode)))
512 {
513 /*
514 * This check is for a timing problem. If the
515 * state of the tty has changed (due to the user
516 * application) we need to process that info
517 * before we write in the state contained in the
518 * ack!!! This gets out the new MODE request,
519 * and when the ack to that command comes back
520 * we'll set it and be in the right mode.
521 */
522 if (ack)
523 localstat ();
524 if (changed & MODE_EDIT)
525 tty_setedit (useeditmode & MODE_EDIT);
526
527 if (changed & MODE_TRAPSIG)
528 tty_setsig (useeditmode & MODE_TRAPSIG);
529
530 if (changed & MODE_SOFT_TAB)
531 tty_setsofttab (useeditmode & MODE_SOFT_TAB);
532
533 if (changed & MODE_LIT_ECHO)
534 tty_setlitecho (useeditmode & MODE_LIT_ECHO);
535
536 set_termbuf ();
537
538 if (!ack)
539 {
540 char data[8];
541
542 sprintf (data, "%c%c%c%c%c%c%c",
543 IAC, SB, TELOPT_LINEMODE,
544 LM_MODE, useeditmode | MODE_ACK, IAC, SE);
545 net_output_datalen (data, sizeof (data));
546 }
547
548 editmode = useeditmode;
549 }
550
551 break;
552
553 } /* end of case LM_MODE */
554
555 case TELOPT_NAWS:
556 #ifdef TIOCSWINSZ
557 {
558 struct winsize ws;
559
560 def_col = parm1;
561 def_row = parm2;
562 /*
563 * Defer changing window size until after terminal is
564 * initialized.
565 */
566 if (terminit () == 0)
567 return;
568
569 /*
570 * Change window size as requested by client.
571 */
572
573 ws.ws_col = parm1;
574 ws.ws_row = parm2;
575
576 # if !defined SOLARIS && !defined SOLARIS10
577 ioctl (pty, TIOCSWINSZ, (char *) &ws);
578 # else/* SOLARIS || SOLARIS10 */
579 {
580 int tty = pty;
581 char *name = ptsname (pty);
582
583 if (name)
584 tty = open (name, O_RDWR | O_NONBLOCK | O_NOCTTY);
585
586 ioctl (tty, TIOCSWINSZ, (char *) &ws);
587
588 if (name)
589 close (tty);
590 }
591 # endif/* SOLARIS || SOLARIS10 */
592 }
593 #endif /* TIOCSWINSZ */
594
595 break;
596
597 case TELOPT_TSPEED:
598 {
599 def_tspeed = parm1;
600 def_rspeed = parm2;
601 /*
602 * Defer changing the terminal speed.
603 */
604 if (terminit () == 0)
605 return;
606 /*
607 * Change terminal speed as requested by client.
608 * We set the receive speed first, so that if we can't
609 * store separate receive and transmit speeds, the transmit
610 * speed will take precedence.
611 */
612 tty_rspeed (parm2);
613 tty_tspeed (parm1);
614 set_termbuf ();
615
616 break;
617
618 } /* end of case TELOPT_TSPEED */
619
620 default:
621 /* What? */
622 break;
623 } /* end of switch */
624
625 #if defined CRAY2 && defined UNICOS5
626 /*
627 * Just in case of the likely event that we changed the pty state.
628 */
629 rcv_ioctl ();
630 #endif /* defined(CRAY2) && defined(UNICOS5) */
631
632 netflush ();
633
634 } /* end of clientstat */
635
636 #if defined CRAY2 && defined UNICOS5
637 void
638 termstat (void)
639 {
640 needtermstat = 1;
641 }
642
643 void
644 _termstat (void)
645 {
646 needtermstat = 0;
647 init_termbuf ();
648 localstat ();
649 rcv_ioctl ();
650 }
651 #endif /* defined(CRAY2) && defined(UNICOS5) */
652
653 /*
654 * defer_terminit
655 *
656 * Some things should not be done until after the login process has started
657 * and all the pty modes are set to what they are supposed to be. This
658 * function is called when the pty state has been processed for the first time.
659 * It calls other functions that do things that were deferred in each module.
660 */
661 void
662 defer_terminit (void)
663 {
664
665 /*
666 * local stuff that got deferred.
667 */
668 if (def_tspeed != -1)
669 {
670 clientstat (TELOPT_TSPEED, def_tspeed, def_rspeed);
671 def_tspeed = def_rspeed = 0;
672 }
673
674 #ifdef TIOCSWINSZ
675 if (def_col || def_row)
676 {
677 struct winsize ws;
678
679 memset ((char *) &ws, 0, sizeof (ws));
680 ws.ws_col = def_col;
681 ws.ws_row = def_row;
682
683 # if !defined SOLARIS && !defined SOLARIS10
684 ioctl (pty, TIOCSWINSZ, (char *) &ws);
685 # else/* SOLARIS || SOLARIS10 */
686 {
687 int tty = pty;
688 char *name = ptsname (pty);
689
690 if (name)
691 tty = open (name, O_RDWR | O_NONBLOCK | O_NOCTTY);
692
693 ioctl (tty, TIOCSWINSZ, (char *) &ws);
694
695 if (name)
696 close (tty);
697 }
698 # endif/* SOLARIS || SOLARIS10 */
699 }
700 #endif /* TIOCSWINSZ */
701
702 /*
703 * The only other module that currently defers anything.
704 */
705 deferslc ();
706
707 } /* end of defer_terminit */
708
709 /*
710 * terminit
711 *
712 * Returns true if the pty state has been processed yet.
713 */
714 int
715 terminit (void)
716 {
717 return (_terminit);
718
719 } /* end of terminit */