OSDN Git Service

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