]> git.saurik.com Git - apple/network_cmds.git/blob - telnetd.tproj/termstat.c
network_cmds-245.19.tar.gz
[apple/network_cmds.git] / telnetd.tproj / 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: src/libexec/telnetd/termstat.c,v 1.11 2001/07/20 15:14:03 ru Exp $";
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 int need_will_echo = 0;
140
141 #if defined(CRAY2) && defined(UNICOS5)
142 /*
143 * Keep track of that ol' CR/NL mapping while we're in the
144 * neighborhood.
145 */
146 newmap = tty_isnewmap();
147 #endif /* defined(CRAY2) && defined(UNICOS5) */
148
149 /*
150 * Check for changes to flow control if client supports it.
151 */
152 flowstat();
153
154 /*
155 * Check linemode on/off state
156 */
157 uselinemode = tty_linemode();
158
159 /*
160 * If alwayslinemode is on, and pty is changing to turn it off, then
161 * force linemode back on.
162 */
163 if (alwayslinemode && linemode && !uselinemode) {
164 uselinemode = 1;
165 tty_setlinemode(uselinemode);
166 }
167
168 if (uselinemode) {
169
170 /*
171 * Check for state of BINARY options.
172 *
173 * We only need to do the binary dance if we are actually going
174 * to use linemode. As this confuses some telnet clients that dont
175 * support linemode, and doesnt gain us anything, we dont do it
176 * unless we're doing linemode. -Crh (henrich@msu.edu)
177 */
178
179 if (tty_isbinaryin()) {
180 if (his_want_state_is_wont(TELOPT_BINARY))
181 send_do(TELOPT_BINARY, 1);
182 } else {
183 if (his_want_state_is_will(TELOPT_BINARY))
184 send_dont(TELOPT_BINARY, 1);
185 }
186
187 if (tty_isbinaryout()) {
188 if (my_want_state_is_wont(TELOPT_BINARY))
189 send_will(TELOPT_BINARY, 1);
190 } else {
191 if (my_want_state_is_will(TELOPT_BINARY))
192 send_wont(TELOPT_BINARY, 1);
193 }
194
195 }
196
197 /*
198 * Do echo mode handling as soon as we know what the
199 * linemode is going to be.
200 * If the pty has echo turned off, then tell the client that
201 * the server will echo. If echo is on, then the server
202 * will echo if in character mode, but in linemode the
203 * client should do local echoing. The state machine will
204 * not send anything if it is unnecessary, so don't worry
205 * about that here.
206 *
207 * If we need to send the WILL ECHO (because echo is off),
208 * then delay that until after we have changed the MODE.
209 * This way, when the user is turning off both editing
210 * and echo, the client will get editing turned off first.
211 * This keeps the client from going into encryption mode
212 * and then right back out if it is doing auto-encryption
213 * when passwords are being typed.
214 */
215 if (uselinemode) {
216 if (tty_isecho())
217 send_wont(TELOPT_ECHO, 1);
218 else
219 need_will_echo = 1;
220 #ifdef KLUDGELINEMODE
221 if (lmodetype == KLUDGE_OK)
222 lmodetype = KLUDGE_LINEMODE;
223 #endif
224 }
225
226 /*
227 * If linemode is being turned off, send appropriate
228 * command and then we're all done.
229 */
230 if (!uselinemode && linemode) {
231 # ifdef KLUDGELINEMODE
232 if (lmodetype == REAL_LINEMODE) {
233 # endif /* KLUDGELINEMODE */
234 send_dont(TELOPT_LINEMODE, 1);
235 # ifdef KLUDGELINEMODE
236 } else if (lmodetype == KLUDGE_LINEMODE)
237 send_will(TELOPT_SGA, 1);
238 # endif /* KLUDGELINEMODE */
239 send_will(TELOPT_ECHO, 1);
240 linemode = uselinemode;
241 goto done;
242 }
243
244 # ifdef KLUDGELINEMODE
245 /*
246 * If using real linemode check edit modes for possible later use.
247 * If we are in kludge linemode, do the SGA negotiation.
248 */
249 if (lmodetype == REAL_LINEMODE) {
250 # endif /* KLUDGELINEMODE */
251 useeditmode = 0;
252 if (tty_isediting())
253 useeditmode |= MODE_EDIT;
254 if (tty_istrapsig())
255 useeditmode |= MODE_TRAPSIG;
256 if (tty_issofttab())
257 useeditmode |= MODE_SOFT_TAB;
258 if (tty_islitecho())
259 useeditmode |= MODE_LIT_ECHO;
260 # ifdef KLUDGELINEMODE
261 } else if (lmodetype == KLUDGE_LINEMODE) {
262 if (tty_isediting() && uselinemode)
263 send_wont(TELOPT_SGA, 1);
264 else
265 send_will(TELOPT_SGA, 1);
266 }
267 # endif /* KLUDGELINEMODE */
268
269 /*
270 * Negotiate linemode on if pty state has changed to turn it on.
271 * Send appropriate command and send along edit mode, then all done.
272 */
273 if (uselinemode && !linemode) {
274 # ifdef KLUDGELINEMODE
275 if (lmodetype == KLUDGE_LINEMODE) {
276 send_wont(TELOPT_SGA, 1);
277 } else if (lmodetype == REAL_LINEMODE) {
278 # endif /* KLUDGELINEMODE */
279 send_do(TELOPT_LINEMODE, 1);
280 /* send along edit modes */
281 output_data("%c%c%c%c%c%c%c", IAC, SB,
282 TELOPT_LINEMODE, LM_MODE, useeditmode,
283 IAC, SE);
284 editmode = useeditmode;
285 # ifdef KLUDGELINEMODE
286 }
287 # endif /* KLUDGELINEMODE */
288 linemode = uselinemode;
289 goto done;
290 }
291
292 # ifdef KLUDGELINEMODE
293 /*
294 * None of what follows is of any value if not using
295 * real linemode.
296 */
297 if (lmodetype < REAL_LINEMODE)
298 goto done;
299 # endif /* KLUDGELINEMODE */
300
301 if (linemode && his_state_is_will(TELOPT_LINEMODE)) {
302 /*
303 * If edit mode changed, send edit mode.
304 */
305 if (useeditmode != editmode) {
306 /*
307 * Send along appropriate edit mode mask.
308 */
309 output_data("%c%c%c%c%c%c%c", IAC, SB,
310 TELOPT_LINEMODE, LM_MODE, useeditmode,
311 IAC, SE);
312 editmode = useeditmode;
313 }
314
315
316 /*
317 * Check for changes to special characters in use.
318 */
319 start_slc(0);
320 check_slc();
321 (void) end_slc(0);
322 }
323
324 done:
325 if (need_will_echo)
326 send_will(TELOPT_ECHO, 1);
327 /*
328 * Some things should be deferred until after the pty state has
329 * been set by the local process. Do those things that have been
330 * deferred now. This only happens once.
331 */
332 if (_terminit == 0) {
333 _terminit = 1;
334 defer_terminit();
335 }
336
337 netflush();
338 set_termbuf();
339 return;
340
341 } /* end of localstat */
342 #endif /* LINEMODE */
343
344 /*
345 * flowstat
346 *
347 * Check for changes to flow control
348 */
349 void
350 flowstat()
351 {
352 if (his_state_is_will(TELOPT_LFLOW)) {
353 if (tty_flowmode() != flowmode) {
354 flowmode = tty_flowmode();
355 output_data("%c%c%c%c%c%c",
356 IAC, SB, TELOPT_LFLOW,
357 flowmode ? LFLOW_ON : LFLOW_OFF,
358 IAC, SE);
359 }
360 if (tty_restartany() != restartany) {
361 restartany = tty_restartany();
362 output_data("%c%c%c%c%c%c",
363 IAC, SB, TELOPT_LFLOW,
364 restartany ? LFLOW_RESTART_ANY
365 : LFLOW_RESTART_XON,
366 IAC, SE);
367 }
368 }
369 }
370
371 /*
372 * clientstat
373 *
374 * Process linemode related requests from the client.
375 * Client can request a change to only one of linemode, editmode or slc's
376 * at a time, and if using kludge linemode, then only linemode may be
377 * affected.
378 */
379 void
380 clientstat(code, parm1, parm2)
381 register int code, parm1, parm2;
382 {
383
384 /*
385 * Get a copy of terminal characteristics.
386 */
387 init_termbuf();
388
389 /*
390 * Process request from client. code tells what it is.
391 */
392 switch (code) {
393 #ifdef LINEMODE
394 case TELOPT_LINEMODE:
395 /*
396 * Don't do anything unless client is asking us to change
397 * modes.
398 */
399 uselinemode = (parm1 == WILL);
400 if (uselinemode != linemode) {
401 # ifdef KLUDGELINEMODE
402 /*
403 * If using kludge linemode, make sure that
404 * we can do what the client asks.
405 * We can not turn off linemode if alwayslinemode
406 * and the ICANON bit is set.
407 */
408 if (lmodetype == KLUDGE_LINEMODE) {
409 if (alwayslinemode && tty_isediting()) {
410 uselinemode = 1;
411 }
412 }
413
414 /*
415 * Quit now if we can't do it.
416 */
417 if (uselinemode == linemode)
418 return;
419
420 /*
421 * If using real linemode and linemode is being
422 * turned on, send along the edit mode mask.
423 */
424 if (lmodetype == REAL_LINEMODE && uselinemode)
425 # else /* KLUDGELINEMODE */
426 if (uselinemode)
427 # endif /* KLUDGELINEMODE */
428 {
429 useeditmode = 0;
430 if (tty_isediting())
431 useeditmode |= MODE_EDIT;
432 if (tty_istrapsig)
433 useeditmode |= MODE_TRAPSIG;
434 if (tty_issofttab())
435 useeditmode |= MODE_SOFT_TAB;
436 if (tty_islitecho())
437 useeditmode |= MODE_LIT_ECHO;
438 output_data("%c%c%c%c%c%c%c", IAC,
439 SB, TELOPT_LINEMODE, LM_MODE,
440 useeditmode, IAC, SE);
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 output_data("%c%c%c%c%c%c%c", IAC,
497 SB, TELOPT_LINEMODE, LM_MODE,
498 useeditmode|MODE_ACK,
499 IAC, SE);
500 }
501
502 editmode = useeditmode;
503 }
504
505 break;
506
507 } /* end of case LM_MODE */
508 #endif /* LINEMODE */
509
510 case TELOPT_NAWS:
511 #ifdef TIOCSWINSZ
512 {
513 struct winsize ws;
514
515 def_col = parm1;
516 def_row = parm2;
517 #ifdef LINEMODE
518 /*
519 * Defer changing window size until after terminal is
520 * initialized.
521 */
522 if (terminit() == 0)
523 return;
524 #endif /* LINEMODE */
525
526 /*
527 * Change window size as requested by client.
528 */
529
530 ws.ws_col = parm1;
531 ws.ws_row = parm2;
532 (void) ioctl(pty, TIOCSWINSZ, (char *)&ws);
533 }
534 #endif /* TIOCSWINSZ */
535
536 break;
537
538 case TELOPT_TSPEED:
539 {
540 def_tspeed = parm1;
541 def_rspeed = parm2;
542 #ifdef LINEMODE
543 /*
544 * Defer changing the terminal speed.
545 */
546 if (terminit() == 0)
547 return;
548 #endif /* LINEMODE */
549 /*
550 * Change terminal speed as requested by client.
551 * We set the receive speed first, so that if we can't
552 * store separate receive and transmit speeds, the transmit
553 * speed will take precedence.
554 */
555 tty_rspeed(parm2);
556 tty_tspeed(parm1);
557 set_termbuf();
558
559 break;
560
561 } /* end of case TELOPT_TSPEED */
562
563 default:
564 /* What? */
565 break;
566 } /* end of switch */
567
568 #if defined(CRAY2) && defined(UNICOS5)
569 /*
570 * Just in case of the likely event that we changed the pty state.
571 */
572 rcv_ioctl();
573 #endif /* defined(CRAY2) && defined(UNICOS5) */
574
575 netflush();
576
577 } /* end of clientstat */
578
579 #if defined(CRAY2) && defined(UNICOS5)
580 void
581 termstat()
582 {
583 needtermstat = 1;
584 }
585
586 void
587 _termstat()
588 {
589 needtermstat = 0;
590 init_termbuf();
591 localstat();
592 rcv_ioctl();
593 }
594 #endif /* defined(CRAY2) && defined(UNICOS5) */
595
596 #ifdef LINEMODE
597 /*
598 * defer_terminit
599 *
600 * Some things should not be done until after the login process has started
601 * and all the pty modes are set to what they are supposed to be. This
602 * function is called when the pty state has been processed for the first time.
603 * It calls other functions that do things that were deferred in each module.
604 */
605 void
606 defer_terminit()
607 {
608
609 /*
610 * local stuff that got deferred.
611 */
612 if (def_tspeed != -1) {
613 clientstat(TELOPT_TSPEED, def_tspeed, def_rspeed);
614 def_tspeed = def_rspeed = 0;
615 }
616
617 #ifdef TIOCSWINSZ
618 if (def_col || def_row) {
619 struct winsize ws;
620
621 bzero((char *)&ws, sizeof(ws));
622 ws.ws_col = def_col;
623 ws.ws_row = def_row;
624 (void) ioctl(pty, TIOCSWINSZ, (char *)&ws);
625 }
626 #endif
627
628 /*
629 * The only other module that currently defers anything.
630 */
631 deferslc();
632
633 } /* end of defer_terminit */
634
635 /*
636 * terminit
637 *
638 * Returns true if the pty state has been processed yet.
639 */
640 int
641 terminit()
642 {
643 return(_terminit);
644
645 } /* end of terminit */
646 #endif /* LINEMODE */