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