]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - crypto/telnet/telnetd/termstat.c
This commit was generated by cvs2svn to compensate for changes in r58809,
[FreeBSD/FreeBSD.git] / crypto / telnet / telnetd / termstat.c
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 #ifndef lint
35 #if 0
36 static const char sccsid[] = "@(#)termstat.c    8.2 (Berkeley) 5/30/95";
37 #endif
38 static const char rcsid[] =
39   "$FreeBSD$";
40 #endif /* not lint */
41
42 #include "telnetd.h"
43
44 #if     defined(ENCRYPTION)
45 #include <libtelnet/encrypt.h>
46 #endif
47
48 /*
49  * local variables
50  */
51 int def_tspeed = -1, def_rspeed = -1;
52 #ifdef  TIOCSWINSZ
53 int def_row = 0, def_col = 0;
54 #endif
55 #ifdef  LINEMODE
56 static int _terminit = 0;
57 #endif  /* LINEMODE */
58
59 #if     defined(CRAY2) && defined(UNICOS5)
60 int     newmap = 1;     /* nonzero if \n maps to ^M^J */
61 #endif
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         void netflush();
144         int need_will_echo = 0;
145
146 #if     defined(CRAY2) && defined(UNICOS5)
147         /*
148          * Keep track of that ol' CR/NL mapping while we're in the
149          * neighborhood.
150          */
151         newmap = tty_isnewmap();
152 #endif  /* defined(CRAY2) && defined(UNICOS5) */
153
154         /*
155          * Check for changes to flow control if client supports it.
156          */
157         flowstat();
158
159         /*
160          * Check linemode on/off state
161          */
162         uselinemode = tty_linemode();
163
164         /*
165          * If alwayslinemode is on, and pty is changing to turn it off, then
166          * force linemode back on.
167          */
168         if (alwayslinemode && linemode && !uselinemode) {
169                 uselinemode = 1;
170                 tty_setlinemode(uselinemode);
171         }
172
173         if (uselinemode) {
174                 /*
175          * Check for state of BINARY options.
176                  *
177                  * We only need to do the binary dance if we are actually going
178                  * to use linemode.  As this confuses some telnet clients
179                  * that don't support linemode, and doesn't gain us
180                  * anything, we don't do it unless we're doing linemode.
181                  * -Crh (henrich@msu.edu)
182          */
183
184         if (tty_isbinaryin()) {
185                 if (his_want_state_is_wont(TELOPT_BINARY))
186                         send_do(TELOPT_BINARY, 1);
187         } else {
188                 if (his_want_state_is_will(TELOPT_BINARY))
189                         send_dont(TELOPT_BINARY, 1);
190         }
191
192         if (tty_isbinaryout()) {
193                 if (my_want_state_is_wont(TELOPT_BINARY))
194                         send_will(TELOPT_BINARY, 1);
195         } else {
196                 if (my_want_state_is_will(TELOPT_BINARY))
197                         send_wont(TELOPT_BINARY, 1);
198         }
199         }
200
201 #ifdef  ENCRYPTION
202         /*
203          * If the terminal is not echoing, but editing is enabled,
204          * something like password input is going to happen, so
205          * if we the other side is not currently sending encrypted
206          * data, ask the other side to start encrypting.
207          */
208         if (his_state_is_will(TELOPT_ENCRYPT)) {
209                 static int enc_passwd = 0;
210                 if (uselinemode && !tty_isecho() && tty_isediting()
211                     && (enc_passwd == 0) && !decrypt_input) {
212                         encrypt_send_request_start();
213                         enc_passwd = 1;
214                 } else if (enc_passwd) {
215                         encrypt_send_request_end();
216                         enc_passwd = 0;
217                 }
218         }
219 #endif  /* ENCRYPTION */
220
221         /*
222          * Do echo mode handling as soon as we know what the
223          * linemode is going to be.
224          * If the pty has echo turned off, then tell the client that
225          * the server will echo.  If echo is on, then the server
226          * will echo if in character mode, but in linemode the
227          * client should do local echoing.  The state machine will
228          * not send anything if it is unnecessary, so don't worry
229          * about that here.
230          *
231          * If we need to send the WILL ECHO (because echo is off),
232          * then delay that until after we have changed the MODE.
233          * This way, when the user is turning off both editing
234          * and echo, the client will get editing turned off first.
235          * This keeps the client from going into encryption mode
236          * and then right back out if it is doing auto-encryption
237          * when passwords are being typed.
238          */
239         if (uselinemode) {
240                 if (tty_isecho())
241                         send_wont(TELOPT_ECHO, 1);
242                 else
243                         need_will_echo = 1;
244 #ifdef  KLUDGELINEMODE
245                 if (lmodetype == KLUDGE_OK)
246                         lmodetype = KLUDGE_LINEMODE;
247 #endif
248         }
249
250         /*
251          * If linemode is being turned off, send appropriate
252          * command and then we're all done.
253          */
254          if (!uselinemode && linemode) {
255 # ifdef KLUDGELINEMODE
256                 if (lmodetype == REAL_LINEMODE) {
257 # endif /* KLUDGELINEMODE */
258                         send_dont(TELOPT_LINEMODE, 1);
259 # ifdef KLUDGELINEMODE
260                 } else if (lmodetype == KLUDGE_LINEMODE)
261                         send_will(TELOPT_SGA, 1);
262 # endif /* KLUDGELINEMODE */
263                 send_will(TELOPT_ECHO, 1);
264                 linemode = uselinemode;
265                 goto done;
266         }
267
268 # ifdef KLUDGELINEMODE
269         /*
270          * If using real linemode check edit modes for possible later use.
271          * If we are in kludge linemode, do the SGA negotiation.
272          */
273         if (lmodetype == REAL_LINEMODE) {
274 # endif /* KLUDGELINEMODE */
275                 useeditmode = 0;
276                 if (tty_isediting())
277                         useeditmode |= MODE_EDIT;
278                 if (tty_istrapsig())
279                         useeditmode |= MODE_TRAPSIG;
280                 if (tty_issofttab())
281                         useeditmode |= MODE_SOFT_TAB;
282                 if (tty_islitecho())
283                         useeditmode |= MODE_LIT_ECHO;
284 # ifdef KLUDGELINEMODE
285         } else if (lmodetype == KLUDGE_LINEMODE) {
286                 if (tty_isediting() && uselinemode)
287                         send_wont(TELOPT_SGA, 1);
288                 else
289                         send_will(TELOPT_SGA, 1);
290         }
291 # endif /* KLUDGELINEMODE */
292
293         /*
294          * Negotiate linemode on if pty state has changed to turn it on.
295          * Send appropriate command and send along edit mode, then all done.
296          */
297         if (uselinemode && !linemode) {
298 # ifdef KLUDGELINEMODE
299                 if (lmodetype == KLUDGE_LINEMODE) {
300                         send_wont(TELOPT_SGA, 1);
301                 } else if (lmodetype == REAL_LINEMODE) {
302 # endif /* KLUDGELINEMODE */
303                         send_do(TELOPT_LINEMODE, 1);
304                         /* send along edit modes */
305                         (void) sprintf(nfrontp, "%c%c%c%c%c%c%c", IAC, SB,
306                                 TELOPT_LINEMODE, LM_MODE, useeditmode,
307                                 IAC, SE);
308                         nfrontp += 7;
309                         editmode = useeditmode;
310 # ifdef KLUDGELINEMODE
311                 }
312 # endif /* KLUDGELINEMODE */
313                 linemode = uselinemode;
314                 goto done;
315         }
316
317 # ifdef KLUDGELINEMODE
318         /*
319          * None of what follows is of any value if not using
320          * real linemode.
321          */
322         if (lmodetype < REAL_LINEMODE)
323                 goto done;
324 # endif /* KLUDGELINEMODE */
325
326         if (linemode && his_state_is_will(TELOPT_LINEMODE)) {
327                 /*
328                  * If edit mode changed, send edit mode.
329                  */
330                  if (useeditmode != editmode) {
331                         /*
332                          * Send along appropriate edit mode mask.
333                          */
334                         (void) sprintf(nfrontp, "%c%c%c%c%c%c%c", IAC, SB,
335                                 TELOPT_LINEMODE, LM_MODE, useeditmode,
336                                 IAC, SE);
337                         nfrontp += 7;
338                         editmode = useeditmode;
339                 }
340
341
342                 /*
343                  * Check for changes to special characters in use.
344                  */
345                 start_slc(0);
346                 check_slc();
347                 (void) end_slc(0);
348         }
349
350 done:
351         if (need_will_echo)
352                 send_will(TELOPT_ECHO, 1);
353         /*
354          * Some things should be deferred until after the pty state has
355          * been set by the local process.  Do those things that have been
356          * deferred now.  This only happens once.
357          */
358         if (_terminit == 0) {
359                 _terminit = 1;
360                 defer_terminit();
361         }
362
363         netflush();
364         set_termbuf();
365         return;
366
367 }  /* end of localstat */
368 #endif  /* LINEMODE */
369
370 /*
371  * flowstat
372  *
373  * Check for changes to flow control
374  */
375         void
376 flowstat()
377 {
378         if (his_state_is_will(TELOPT_LFLOW)) {
379                 if (tty_flowmode() != flowmode) {
380                         flowmode = tty_flowmode();
381                         (void) sprintf(nfrontp, "%c%c%c%c%c%c",
382                                         IAC, SB, TELOPT_LFLOW,
383                                         flowmode ? LFLOW_ON : LFLOW_OFF,
384                                         IAC, SE);
385                         nfrontp += 6;
386                 }
387                 if (tty_restartany() != restartany) {
388                         restartany = tty_restartany();
389                         (void) sprintf(nfrontp, "%c%c%c%c%c%c",
390                                         IAC, SB, TELOPT_LFLOW,
391                                         restartany ? LFLOW_RESTART_ANY
392                                                    : LFLOW_RESTART_XON,
393                                         IAC, SE);
394                         nfrontp += 6;
395                 }
396         }
397 }
398
399 /*
400  * clientstat
401  *
402  * Process linemode related requests from the client.
403  * Client can request a change to only one of linemode, editmode or slc's
404  * at a time, and if using kludge linemode, then only linemode may be
405  * affected.
406  */
407         void
408 clientstat(code, parm1, parm2)
409         register int code, parm1, parm2;
410 {
411         void netflush();
412
413         /*
414          * Get a copy of terminal characteristics.
415          */
416         init_termbuf();
417
418         /*
419          * Process request from client. code tells what it is.
420          */
421         switch (code) {
422 #ifdef  LINEMODE
423         case TELOPT_LINEMODE:
424                 /*
425                  * Don't do anything unless client is asking us to change
426                  * modes.
427                  */
428                 uselinemode = (parm1 == WILL);
429                 if (uselinemode != linemode) {
430 # ifdef KLUDGELINEMODE
431                         /*
432                          * If using kludge linemode, make sure that
433                          * we can do what the client asks.
434                          * We can not turn off linemode if alwayslinemode
435                          * and the ICANON bit is set.
436                          */
437                         if (lmodetype == KLUDGE_LINEMODE) {
438                                 if (alwayslinemode && tty_isediting()) {
439                                         uselinemode = 1;
440                                 }
441                         }
442
443                         /*
444                          * Quit now if we can't do it.
445                          */
446                         if (uselinemode == linemode)
447                                 return;
448
449                         /*
450                          * If using real linemode and linemode is being
451                          * turned on, send along the edit mode mask.
452                          */
453                         if (lmodetype == REAL_LINEMODE && uselinemode)
454 # else  /* KLUDGELINEMODE */
455                         if (uselinemode)
456 # endif /* KLUDGELINEMODE */
457                         {
458                                 useeditmode = 0;
459                                 if (tty_isediting())
460                                         useeditmode |= MODE_EDIT;
461                                 if (tty_istrapsig)
462                                         useeditmode |= MODE_TRAPSIG;
463                                 if (tty_issofttab())
464                                         useeditmode |= MODE_SOFT_TAB;
465                                 if (tty_islitecho())
466                                         useeditmode |= MODE_LIT_ECHO;
467                                 (void) sprintf(nfrontp, "%c%c%c%c%c%c%c", IAC,
468                                         SB, TELOPT_LINEMODE, LM_MODE,
469                                                         useeditmode, IAC, SE);
470                                 nfrontp += 7;
471                                 editmode = useeditmode;
472                         }
473
474
475                         tty_setlinemode(uselinemode);
476
477                         linemode = uselinemode;
478
479                         if (!linemode)
480                                 send_will(TELOPT_ECHO, 1);
481                 }
482                 break;
483
484         case LM_MODE:
485             {
486                 register int ack, changed;
487
488                 /*
489                  * Client has sent along a mode mask.  If it agrees with
490                  * what we are currently doing, ignore it; if not, it could
491                  * be viewed as a request to change.  Note that the server
492                  * will change to the modes in an ack if it is different from
493                  * what we currently have, but we will not ack the ack.
494                  */
495                  useeditmode &= MODE_MASK;
496                  ack = (useeditmode & MODE_ACK);
497                  useeditmode &= ~MODE_ACK;
498
499                  if ((changed = (useeditmode ^ editmode))) {
500                         /*
501                          * This check is for a timing problem.  If the
502                          * state of the tty has changed (due to the user
503                          * application) we need to process that info
504                          * before we write in the state contained in the
505                          * ack!!!  This gets out the new MODE request,
506                          * and when the ack to that command comes back
507                          * we'll set it and be in the right mode.
508                          */
509                         if (ack)
510                                 localstat();
511                         if (changed & MODE_EDIT)
512                                 tty_setedit(useeditmode & MODE_EDIT);
513
514                         if (changed & MODE_TRAPSIG)
515                                 tty_setsig(useeditmode & MODE_TRAPSIG);
516
517                         if (changed & MODE_SOFT_TAB)
518                                 tty_setsofttab(useeditmode & MODE_SOFT_TAB);
519
520                         if (changed & MODE_LIT_ECHO)
521                                 tty_setlitecho(useeditmode & MODE_LIT_ECHO);
522
523                         set_termbuf();
524
525                         if (!ack) {
526                                 (void) sprintf(nfrontp, "%c%c%c%c%c%c%c", IAC,
527                                         SB, TELOPT_LINEMODE, LM_MODE,
528                                         useeditmode|MODE_ACK,
529                                         IAC, SE);
530                                 nfrontp += 7;
531                         }
532
533                         editmode = useeditmode;
534                 }
535
536                 break;
537
538             }  /* end of case LM_MODE */
539 #endif  /* LINEMODE */
540
541         case TELOPT_NAWS:
542 #ifdef  TIOCSWINSZ
543             {
544                 struct winsize ws;
545
546                 def_col = parm1;
547                 def_row = parm2;
548 #ifdef  LINEMODE
549                 /*
550                  * Defer changing window size until after terminal is
551                  * initialized.
552                  */
553                 if (terminit() == 0)
554                         return;
555 #endif  /* LINEMODE */
556
557                 /*
558                  * Change window size as requested by client.
559                  */
560
561                 ws.ws_col = parm1;
562                 ws.ws_row = parm2;
563                 (void) ioctl(pty, TIOCSWINSZ, (char *)&ws);
564             }
565 #endif  /* TIOCSWINSZ */
566
567                 break;
568
569         case TELOPT_TSPEED:
570             {
571                 def_tspeed = parm1;
572                 def_rspeed = parm2;
573 #ifdef  LINEMODE
574                 /*
575                  * Defer changing the terminal speed.
576                  */
577                 if (terminit() == 0)
578                         return;
579 #endif  /* LINEMODE */
580                 /*
581                  * Change terminal speed as requested by client.
582                  * We set the receive speed first, so that if we can't
583                  * store seperate receive and transmit speeds, the transmit
584                  * speed will take precedence.
585                  */
586                 tty_rspeed(parm2);
587                 tty_tspeed(parm1);
588                 set_termbuf();
589
590                 break;
591
592             }  /* end of case TELOPT_TSPEED */
593
594         default:
595                 /* What? */
596                 break;
597         }  /* end of switch */
598
599 #if     defined(CRAY2) && defined(UNICOS5)
600         /*
601          * Just in case of the likely event that we changed the pty state.
602          */
603         rcv_ioctl();
604 #endif  /* defined(CRAY2) && defined(UNICOS5) */
605
606         netflush();
607
608 }  /* end of clientstat */
609
610 #if     defined(CRAY2) && defined(UNICOS5)
611         void
612 termstat()
613 {
614         needtermstat = 1;
615 }
616
617         void
618 _termstat()
619 {
620         needtermstat = 0;
621         init_termbuf();
622         localstat();
623         rcv_ioctl();
624 }
625 #endif  /* defined(CRAY2) && defined(UNICOS5) */
626
627 #ifdef  LINEMODE
628 /*
629  * defer_terminit
630  *
631  * Some things should not be done until after the login process has started
632  * and all the pty modes are set to what they are supposed to be.  This
633  * function is called when the pty state has been processed for the first time.
634  * It calls other functions that do things that were deferred in each module.
635  */
636         void
637 defer_terminit()
638 {
639
640         /*
641          * local stuff that got deferred.
642          */
643         if (def_tspeed != -1) {
644                 clientstat(TELOPT_TSPEED, def_tspeed, def_rspeed);
645                 def_tspeed = def_rspeed = 0;
646         }
647
648 #ifdef  TIOCSWINSZ
649         if (def_col || def_row) {
650                 struct winsize ws;
651
652                 memset((char *)&ws, 0, sizeof(ws));
653                 ws.ws_col = def_col;
654                 ws.ws_row = def_row;
655                 (void) ioctl(pty, TIOCSWINSZ, (char *)&ws);
656         }
657 #endif
658
659         /*
660          * The only other module that currently defers anything.
661          */
662         deferslc();
663
664 }  /* end of defer_terminit */
665
666 /*
667  * terminit
668  *
669  * Returns true if the pty state has been processed yet.
670  */
671         int
672 terminit()
673 {
674         return(_terminit);
675
676 }  /* end of terminit */
677 #endif  /* LINEMODE */