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