priv: Introduce and use PRIV_VFS_REVOKE
[dragonfly.git] / libexec / telnetd / termstat.c
blob85adfeef2170446cfb032ae8b260b4ebfb15eb61
1 /*
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
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.
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.
33 * @(#)termstat.c 8.2 (Berkeley) 5/30/95
34 * $FreeBSD: src/crypto/telnet/telnetd/termstat.c,v 1.4.2.4 2002/04/13 10:59:09 markm Exp $
35 * $DragonFly: src/crypto/telnet/telnetd/termstat.c,v 1.2 2003/06/17 04:24:37 dillon Exp $
38 #include "telnetd.h"
40 #ifdef ENCRYPTION
41 #include <libtelnet/encrypt.h>
42 #endif
45 * local variables
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 */
55 #ifdef LINEMODE
57 * localstat
59 * This function handles all management of linemode.
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.
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.
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
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.
87 * editmode, useeditmode : like linemode/uselinemode, but
88 * these contain the edit mode states (edit and trapsig).
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.
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.
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.
132 void
133 localstat(void)
135 int need_will_echo = 0;
138 * Check for changes to flow control if client supports it.
140 flowstat();
143 * Check linemode on/off state
145 uselinemode = tty_linemode();
148 * If alwayslinemode is on, and pty is changing to turn it off, then
149 * force linemode back on.
151 if (alwayslinemode && linemode && !uselinemode) {
152 uselinemode = 1;
153 tty_setlinemode(uselinemode);
156 if (uselinemode) {
158 * Check for state of BINARY options.
160 * We only need to do the binary dance if we are actually going
161 * to use linemode. As this confuses some telnet clients
162 * that don't support linemode, and doesn't gain us
163 * anything, we don't do it unless we're doing linemode.
164 * -Crh (henrich@msu.edu)
167 if (tty_isbinaryin()) {
168 if (his_want_state_is_wont(TELOPT_BINARY))
169 send_do(TELOPT_BINARY, 1);
170 } else {
171 if (his_want_state_is_will(TELOPT_BINARY))
172 send_dont(TELOPT_BINARY, 1);
175 if (tty_isbinaryout()) {
176 if (my_want_state_is_wont(TELOPT_BINARY))
177 send_will(TELOPT_BINARY, 1);
178 } else {
179 if (my_want_state_is_will(TELOPT_BINARY))
180 send_wont(TELOPT_BINARY, 1);
184 #ifdef ENCRYPTION
186 * If the terminal is not echoing, but editing is enabled,
187 * something like password input is going to happen, so
188 * if we the other side is not currently sending encrypted
189 * data, ask the other side to start encrypting.
191 if (his_state_is_will(TELOPT_ENCRYPT)) {
192 static int enc_passwd = 0;
193 if (uselinemode && !tty_isecho() && tty_isediting()
194 && (enc_passwd == 0) && !decrypt_input) {
195 encrypt_send_request_start();
196 enc_passwd = 1;
197 } else if (enc_passwd) {
198 encrypt_send_request_end();
199 enc_passwd = 0;
202 #endif /* ENCRYPTION */
205 * Do echo mode handling as soon as we know what the
206 * linemode is going to be.
207 * If the pty has echo turned off, then tell the client that
208 * the server will echo. If echo is on, then the server
209 * will echo if in character mode, but in linemode the
210 * client should do local echoing. The state machine will
211 * not send anything if it is unnecessary, so don't worry
212 * about that here.
214 * If we need to send the WILL ECHO (because echo is off),
215 * then delay that until after we have changed the MODE.
216 * This way, when the user is turning off both editing
217 * and echo, the client will get editing turned off first.
218 * This keeps the client from going into encryption mode
219 * and then right back out if it is doing auto-encryption
220 * when passwords are being typed.
222 if (uselinemode) {
223 if (tty_isecho())
224 send_wont(TELOPT_ECHO, 1);
225 else
226 need_will_echo = 1;
227 #ifdef KLUDGELINEMODE
228 if (lmodetype == KLUDGE_OK)
229 lmodetype = KLUDGE_LINEMODE;
230 #endif
234 * If linemode is being turned off, send appropriate
235 * command and then we're all done.
237 if (!uselinemode && linemode) {
238 # ifdef KLUDGELINEMODE
239 if (lmodetype == REAL_LINEMODE) {
240 # endif /* KLUDGELINEMODE */
241 send_dont(TELOPT_LINEMODE, 1);
242 # ifdef KLUDGELINEMODE
243 } else if (lmodetype == KLUDGE_LINEMODE)
244 send_will(TELOPT_SGA, 1);
245 # endif /* KLUDGELINEMODE */
246 send_will(TELOPT_ECHO, 1);
247 linemode = uselinemode;
248 goto done;
251 # ifdef KLUDGELINEMODE
253 * If using real linemode check edit modes for possible later use.
254 * If we are in kludge linemode, do the SGA negotiation.
256 if (lmodetype == REAL_LINEMODE) {
257 # endif /* KLUDGELINEMODE */
258 useeditmode = 0;
259 if (tty_isediting())
260 useeditmode |= MODE_EDIT;
261 if (tty_istrapsig())
262 useeditmode |= MODE_TRAPSIG;
263 if (tty_issofttab())
264 useeditmode |= MODE_SOFT_TAB;
265 if (tty_islitecho())
266 useeditmode |= MODE_LIT_ECHO;
267 # ifdef KLUDGELINEMODE
268 } else if (lmodetype == KLUDGE_LINEMODE) {
269 if (tty_isediting() && uselinemode)
270 send_wont(TELOPT_SGA, 1);
271 else
272 send_will(TELOPT_SGA, 1);
274 # endif /* KLUDGELINEMODE */
277 * Negotiate linemode on if pty state has changed to turn it on.
278 * Send appropriate command and send along edit mode, then all done.
280 if (uselinemode && !linemode) {
281 # ifdef KLUDGELINEMODE
282 if (lmodetype == KLUDGE_LINEMODE) {
283 send_wont(TELOPT_SGA, 1);
284 } else if (lmodetype == REAL_LINEMODE) {
285 # endif /* KLUDGELINEMODE */
286 send_do(TELOPT_LINEMODE, 1);
287 /* send along edit modes */
288 output_data("%c%c%c%c%c%c%c", IAC, SB,
289 TELOPT_LINEMODE, LM_MODE, useeditmode,
290 IAC, SE);
291 editmode = useeditmode;
292 # ifdef KLUDGELINEMODE
294 # endif /* KLUDGELINEMODE */
295 linemode = uselinemode;
296 goto done;
299 # ifdef KLUDGELINEMODE
301 * None of what follows is of any value if not using
302 * real linemode.
304 if (lmodetype < REAL_LINEMODE)
305 goto done;
306 # endif /* KLUDGELINEMODE */
308 if (linemode && his_state_is_will(TELOPT_LINEMODE)) {
310 * If edit mode changed, send edit mode.
312 if (useeditmode != editmode) {
314 * Send along appropriate edit mode mask.
316 output_data("%c%c%c%c%c%c%c", IAC, SB,
317 TELOPT_LINEMODE, LM_MODE, useeditmode,
318 IAC, SE);
319 editmode = useeditmode;
324 * Check for changes to special characters in use.
326 start_slc(0);
327 check_slc();
328 (void) end_slc(0);
331 done:
332 if (need_will_echo)
333 send_will(TELOPT_ECHO, 1);
335 * Some things should be deferred until after the pty state has
336 * been set by the local process. Do those things that have been
337 * deferred now. This only happens once.
339 if (_terminit == 0) {
340 _terminit = 1;
341 defer_terminit();
344 netflush();
345 set_termbuf();
346 return;
348 } /* end of localstat */
349 #endif /* LINEMODE */
352 * flowstat
354 * Check for changes to flow control
356 void
357 flowstat(void)
359 if (his_state_is_will(TELOPT_LFLOW)) {
360 if (tty_flowmode() != flowmode) {
361 flowmode = tty_flowmode();
362 output_data("%c%c%c%c%c%c",
363 IAC, SB, TELOPT_LFLOW,
364 flowmode ? LFLOW_ON : LFLOW_OFF,
365 IAC, SE);
367 if (tty_restartany() != restartany) {
368 restartany = tty_restartany();
369 output_data("%c%c%c%c%c%c",
370 IAC, SB, TELOPT_LFLOW,
371 restartany ? LFLOW_RESTART_ANY
372 : LFLOW_RESTART_XON,
373 IAC, SE);
379 * clientstat
381 * Process linemode related requests from the client.
382 * Client can request a change to only one of linemode, editmode or slc's
383 * at a time, and if using kludge linemode, then only linemode may be
384 * affected.
386 void
387 clientstat(int code, int parm1, int parm2)
391 * Get a copy of terminal characteristics.
393 init_termbuf();
396 * Process request from client. code tells what it is.
398 switch (code) {
399 #ifdef LINEMODE
400 case TELOPT_LINEMODE:
402 * Don't do anything unless client is asking us to change
403 * modes.
405 uselinemode = (parm1 == WILL);
406 if (uselinemode != linemode) {
407 # ifdef KLUDGELINEMODE
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.
414 if (lmodetype == KLUDGE_LINEMODE) {
415 if (alwayslinemode && tty_isediting()) {
416 uselinemode = 1;
421 * Quit now if we can't do it.
423 if (uselinemode == linemode)
424 return;
427 * If using real linemode and linemode is being
428 * turned on, send along the edit mode mask.
430 if (lmodetype == REAL_LINEMODE && uselinemode)
431 # else /* KLUDGELINEMODE */
432 if (uselinemode)
433 # endif /* KLUDGELINEMODE */
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 output_data("%c%c%c%c%c%c%c", IAC,
445 SB, TELOPT_LINEMODE, LM_MODE,
446 useeditmode, IAC, SE);
447 editmode = useeditmode;
451 tty_setlinemode(uselinemode);
453 linemode = uselinemode;
455 if (!linemode)
456 send_will(TELOPT_ECHO, 1);
458 break;
460 case LM_MODE:
462 int ack, changed;
465 * Client has sent along a mode mask. If it agrees with
466 * what we are currently doing, ignore it; if not, it could
467 * be viewed as a request to change. Note that the server
468 * will change to the modes in an ack if it is different from
469 * what we currently have, but we will not ack the ack.
471 useeditmode &= MODE_MASK;
472 ack = (useeditmode & MODE_ACK);
473 useeditmode &= ~MODE_ACK;
475 if ((changed = (useeditmode ^ editmode))) {
477 * This check is for a timing problem. If the
478 * state of the tty has changed (due to the user
479 * application) we need to process that info
480 * before we write in the state contained in the
481 * ack!!! This gets out the new MODE request,
482 * and when the ack to that command comes back
483 * we'll set it and be in the right mode.
485 if (ack)
486 localstat();
487 if (changed & MODE_EDIT)
488 tty_setedit(useeditmode & MODE_EDIT);
490 if (changed & MODE_TRAPSIG)
491 tty_setsig(useeditmode & MODE_TRAPSIG);
493 if (changed & MODE_SOFT_TAB)
494 tty_setsofttab(useeditmode & MODE_SOFT_TAB);
496 if (changed & MODE_LIT_ECHO)
497 tty_setlitecho(useeditmode & MODE_LIT_ECHO);
499 set_termbuf();
501 if (!ack) {
502 output_data("%c%c%c%c%c%c%c", IAC,
503 SB, TELOPT_LINEMODE, LM_MODE,
504 useeditmode|MODE_ACK,
505 IAC, SE);
508 editmode = useeditmode;
511 break;
513 } /* end of case LM_MODE */
514 #endif /* LINEMODE */
516 case TELOPT_NAWS:
517 #ifdef TIOCSWINSZ
519 struct winsize ws;
521 def_col = parm1;
522 def_row = parm2;
523 #ifdef LINEMODE
525 * Defer changing window size until after terminal is
526 * initialized.
528 if (terminit() == 0)
529 return;
530 #endif /* LINEMODE */
533 * Change window size as requested by client.
536 ws.ws_col = parm1;
537 ws.ws_row = parm2;
538 (void) ioctl(pty, TIOCSWINSZ, (char *)&ws);
540 #endif /* TIOCSWINSZ */
542 break;
544 case TELOPT_TSPEED:
546 def_tspeed = parm1;
547 def_rspeed = parm2;
548 #ifdef LINEMODE
550 * Defer changing the terminal speed.
552 if (terminit() == 0)
553 return;
554 #endif /* LINEMODE */
556 * Change terminal speed as requested by client.
557 * We set the receive speed first, so that if we can't
558 * store separate receive and transmit speeds, the transmit
559 * speed will take precedence.
561 tty_rspeed(parm2);
562 tty_tspeed(parm1);
563 set_termbuf();
565 break;
567 } /* end of case TELOPT_TSPEED */
569 default:
570 /* What? */
571 break;
572 } /* end of switch */
574 netflush();
576 } /* end of clientstat */
578 #ifdef LINEMODE
580 * defer_terminit
582 * Some things should not be done until after the login process has started
583 * and all the pty modes are set to what they are supposed to be. This
584 * function is called when the pty state has been processed for the first time.
585 * It calls other functions that do things that were deferred in each module.
587 void
588 defer_terminit(void)
592 * local stuff that got deferred.
594 if (def_tspeed != -1) {
595 clientstat(TELOPT_TSPEED, def_tspeed, def_rspeed);
596 def_tspeed = def_rspeed = 0;
599 #ifdef TIOCSWINSZ
600 if (def_col || def_row) {
601 struct winsize ws;
603 memset((char *)&ws, 0, sizeof(ws));
604 ws.ws_col = def_col;
605 ws.ws_row = def_row;
606 (void) ioctl(pty, TIOCSWINSZ, (char *)&ws);
608 #endif
611 * The only other module that currently defers anything.
613 deferslc();
615 } /* end of defer_terminit */
618 * terminit
620 * Returns true if the pty state has been processed yet.
623 terminit(void)
625 return(_terminit);
627 } /* end of terminit */
628 #endif /* LINEMODE */