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