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
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
33 * @(#)state.c 8.5 (Berkeley) 5/30/95
34 * $FreeBSD: src/libexec/telnetd/state.c,v 1.9.2.4 2002/04/13 11:07:12 markm Exp $
35 * $DragonFly: src/libexec/telnetd/state.c,v 1.3 2006/01/12 13:43:10 corecode Exp $
41 static int envvarok(char *);
43 unsigned char doopt
[] = { IAC
, DO
, '%', 'c', 0 };
44 unsigned char dont
[] = { IAC
, DONT
, '%', 'c', 0 };
45 unsigned char will
[] = { IAC
, WILL
, '%', 'c', 0 };
46 unsigned char wont
[] = { IAC
, WONT
, '%', 'c', 0 };
50 * Buffer for sub-options, and macros
51 * for suboptions buffer manipulations
53 unsigned char subbuffer
[512], *subpointer
= subbuffer
, *subend
= subbuffer
;
55 #define SB_CLEAR() subpointer = subbuffer
56 #define SB_TERM() { subend = subpointer; SB_CLEAR(); }
57 #define SB_ACCUM(c) if (subpointer < (subbuffer+sizeof subbuffer)) { \
58 *subpointer++ = (c); \
60 #define SB_GET() ((*subpointer++)&0xff)
61 #define SB_EOF() (subpointer >= subend)
62 #define SB_LEN() (subend - subpointer)
65 unsigned char *subsave
;
66 #define SB_SAVE() subsave = subpointer;
67 #define SB_RESTORE() subpointer = subsave;
74 #define TS_DATA 0 /* base state */
75 #define TS_IAC 1 /* look for double IAC's */
76 #define TS_CR 2 /* CR-LF ->'s CR */
77 #define TS_SB 3 /* throw away begin's... */
78 #define TS_SE 4 /* ...end's (suboption negotiation) */
79 #define TS_WILL 5 /* will option negotiation */
80 #define TS_WONT 6 /* wont " */
81 #define TS_DO 7 /* do " */
82 #define TS_DONT 8 /* dont " */
84 static void doclientstat(void);
90 static int state
= TS_DATA
;
93 if ((&ptyobuf
[BUFSIZ
] - pfrontp
) < 2)
95 c
= *netip
++ & 0377, ncc
--;
100 /* Strip off \n or \0 after a \r */
101 if ((c
== 0) || (c
== '\n')) {
112 * We now map \r\n ==> \r for pragmatic reasons.
113 * Many client implementations send \r\n when
114 * the user hits the CarriageReturn key.
116 * We USED to map \r\n ==> \n, since \r\n says
117 * that we want to be in column 1 of the next
118 * printable line, and \n is the standard
119 * unix way of saying that (\r is only good
120 * if CRMOD is set, which it normally is).
122 if ((c
== '\r') && his_state_is_wont(TELOPT_BINARY
)) {
126 * If we are operating in linemode,
127 * convert to local end-of-line.
129 if (linemode
&& (ncc
> 0) && (('\n' == nc
) ||
130 ((0 == nc
) && tty_iscrnl())) ) {
146 * Send the process on the pty side an
147 * interrupt. Do this with a NULL or
148 * interrupt char; depending on the tty mode.
152 printoption("td: recv IAC", c
));
158 printoption("td: recv IAC", c
));
167 printoption("td: recv IAC", c
));
177 printoption("td: recv IAC", c
));
178 ptyflush(); /* half-hearted */
181 if (slctab
[SLC_AO
].sptr
&&
182 *slctab
[SLC_AO
].sptr
!= (cc_t
)(_POSIX_VDISABLE
)) {
184 (unsigned char)*slctab
[SLC_AO
].sptr
;
187 netclear(); /* clear buffer back */
188 output_data("%c%c", IAC
, DM
);
189 neturg
= nfrontp
-1; /* off by one XXX */
191 printoption("td: send IAC", DM
));
196 * Erase Character and
205 printoption("td: recv IAC", c
));
206 ptyflush(); /* half-hearted */
209 ch
= *slctab
[SLC_EC
].sptr
;
211 ch
= *slctab
[SLC_EL
].sptr
;
212 if (ch
!= (cc_t
)(_POSIX_VDISABLE
))
213 *pfrontp
++ = (unsigned char)ch
;
218 * Check for urgent data...
222 printoption("td: recv IAC", c
));
223 SYNCHing
= stilloob(net
);
229 * Begin option subnegotiation...
252 if (his_state_is_will(TELOPT_EOR
))
257 * Handle RFC 10xx Telnet linemode option additions
258 * to command stream (EOF, SUSP, ABORT).
291 * bad form of suboption negotiation.
292 * handle it in such a way as to avoid
293 * damage to local state. Parse
294 * suboption buffer found so far,
295 * then treat remaining stream as
296 * another command sequence.
299 /* for DIAGNOSTICS */
312 /* for DIAGNOSTICS */
318 suboption(); /* handle sub-option */
344 syslog(LOG_ERR
, "panic state=%d", state
);
345 printf("telnetd: panic state=%d\n", state
);
349 } /* end of telrcv */
352 * The will/wont/do/dont state machines are based on Dave Borman's
353 * Telnet option processing state machine.
355 * These correspond to the following states:
356 * my_state = the last negotiated state
357 * want_state = what I want the state to go to
358 * want_resp = how many requests I have sent
359 * All state defaults are negative, and resp defaults to 0.
361 * When initiating a request to change state to new_state:
363 * if ((want_resp == 0 && new_state == my_state) || want_state == new_state) {
366 * want_state = new_state;
371 * When receiving new_state:
375 * if (want_resp && (new_state == my_state))
378 * if ((want_resp == 0) && (new_state != want_state)) {
379 * if (ok_to_switch_to new_state)
380 * want_state = new_state;
385 * my_state = new_state;
387 * Note that new_state is implied in these functions by the function itself.
388 * will and do imply positive new_state, wont and dont imply negative.
390 * Finally, there is one catch. If we send a negative response to a
391 * positive request, my_state will be the positive while want_state will
392 * remain negative. my_state will revert to negative when the negative
393 * acknowlegment arrives from the peer. Thus, my_state generally tells
394 * us not only the last negotiated state, but also tells us what the peer
395 * wants to be doing as well. It is important to understand this difference
396 * as we may wish to be processing data streams based on our desired state
397 * (want_state) or based on what the peer thinks the state is (my_state).
399 * This all works fine because if the peer sends a positive request, the data
400 * that we receive prior to negative acknowlegment will probably be affected
401 * by the positive state, and we can process it as such (if we can; if we
402 * can't then it really doesn't matter). If it is that important, then the
403 * peer probably should be buffering until this option state negotiation
408 send_do(int option
, int init
)
411 if ((do_dont_resp
[option
] == 0 && his_state_is_will(option
)) ||
412 his_want_state_is_will(option
))
415 * Special case for TELOPT_TM: We send a DO, but pretend
416 * that we sent a DONT, so that we can send more DOs if
419 if (option
== TELOPT_TM
)
420 set_his_want_state_wont(option
);
422 set_his_want_state_will(option
);
423 do_dont_resp
[option
]++;
425 output_data((const char *)doopt
, option
);
427 DIAG(TD_OPTIONS
, printoption("td: send do", option
));
431 willoption(int option
)
434 void (*func
)(void) = 0;
437 * process input from peer.
440 DIAG(TD_OPTIONS
, printoption("td: recv will", option
));
442 if (do_dont_resp
[option
]) {
443 do_dont_resp
[option
]--;
444 if (do_dont_resp
[option
] && his_state_is_will(option
))
445 do_dont_resp
[option
]--;
447 if (do_dont_resp
[option
] == 0) {
448 if (his_want_state_is_wont(option
)) {
460 * See comments below for more info.
462 not42
= 0; /* looks like a 4.2 system */
466 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
468 * This telnetd implementation does not really
469 * support timing marks, it just uses them to
470 * support the kludge linemode stuff. If we
471 * receive a will or wont TM in response to our
472 * do TM request that may have been sent to
473 * determine kludge linemode support, process
474 * it, otherwise TM should get a negative
478 * Handle the linemode kludge stuff.
479 * If we are not currently supporting any
480 * linemode at all, then we assume that this
481 * is the client telling us to use kludge
482 * linemode in response to our query. Set the
483 * linemode type that is to be supported, note
484 * that the client wishes to use linemode, and
485 * eat the will TM as though it never arrived.
487 if (lmodetype
< KLUDGE_LINEMODE
) {
488 lmodetype
= KLUDGE_LINEMODE
;
489 clientstat(TELOPT_LINEMODE
, WILL
, 0);
490 send_wont(TELOPT_SGA
, 1);
491 } else if (lmodetype
== NO_AUTOKLUDGE
) {
492 lmodetype
= KLUDGE_OK
;
494 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
496 * We never respond to a WILL TM, and
497 * we leave the state WONT.
503 * If we are going to support flow control
504 * option, then don't worry peer that we can't
505 * change the flow control characters.
507 slctab
[SLC_XON
].defset
.flag
&= ~SLC_LEVELBITS
;
508 slctab
[SLC_XON
].defset
.flag
|= SLC_DEFAULT
;
509 slctab
[SLC_XOFF
].defset
.flag
&= ~SLC_LEVELBITS
;
510 slctab
[SLC_XOFF
].defset
.flag
|= SLC_DEFAULT
;
515 case TELOPT_XDISPLOC
:
516 case TELOPT_NEW_ENVIRON
:
517 case TELOPT_OLD_ENVIRON
:
522 case TELOPT_LINEMODE
:
523 # ifdef KLUDGELINEMODE
525 * Note client's desire to use linemode.
527 lmodetype
= REAL_LINEMODE
;
528 # endif /* KLUDGELINEMODE */
532 #endif /* LINEMODE */
540 set_his_want_state_will(option
);
543 do_dont_resp
[option
]++;
544 send_dont(option
, 0);
548 * Option processing that should happen when
549 * we receive conformation of a change in
550 * state that we had requested.
554 not42
= 0; /* looks like a 4.2 system */
556 * Egads, he responded "WILL ECHO". Turn
559 send_dont(option
, 1);
561 * "WILL ECHO". Kludge upon kludge!
562 * A 4.2 client is now echoing user input at
563 * the tty. This is probably undesireable and
564 * it should be stopped. The client will
565 * respond WONT TM to the DO TM that we send to
566 * check for kludge linemode. When the WONT TM
567 * arrives, linemode will be turned off and a
568 * change propogated to the pty. This change
569 * will cause us to process the new pty state
570 * in localstat(), which will notice that
571 * linemode is off and send a WILL ECHO
572 * so that we are properly in character mode and
577 case TELOPT_LINEMODE
:
578 # ifdef KLUDGELINEMODE
580 * Note client's desire to use linemode.
582 lmodetype
= REAL_LINEMODE
;
583 # endif /* KLUDGELINEMODE */
586 #endif /* LINEMODE */
595 set_his_state_will(option
);
598 } /* end of willoption */
601 send_dont(int option
, int init
)
604 if ((do_dont_resp
[option
] == 0 && his_state_is_wont(option
)) ||
605 his_want_state_is_wont(option
))
607 set_his_want_state_wont(option
);
608 do_dont_resp
[option
]++;
610 output_data((const char *)dont
, option
);
612 DIAG(TD_OPTIONS
, printoption("td: send dont", option
));
616 wontoption(int option
)
619 * Process client input.
622 DIAG(TD_OPTIONS
, printoption("td: recv wont", option
));
624 if (do_dont_resp
[option
]) {
625 do_dont_resp
[option
]--;
626 if (do_dont_resp
[option
] && his_state_is_wont(option
))
627 do_dont_resp
[option
]--;
629 if (do_dont_resp
[option
] == 0) {
630 if (his_want_state_is_will(option
)) {
631 /* it is always ok to change to negative state */
634 not42
= 1; /* doesn't seem to be a 4.2 system */
644 case TELOPT_LINEMODE
:
645 # ifdef KLUDGELINEMODE
647 * If real linemode is supported, then client is
648 * asking to turn linemode off.
650 if (lmodetype
!= REAL_LINEMODE
)
652 lmodetype
= KLUDGE_LINEMODE
;
653 # endif /* KLUDGELINEMODE */
654 clientstat(TELOPT_LINEMODE
, WONT
, 0);
656 #endif /* LINEMODE */
660 * If we get a WONT TM, and had sent a DO TM,
661 * don't respond with a DONT TM, just leave it
662 * as is. Short circut the state machine to
665 set_his_want_state_wont(TELOPT_TM
);
670 * If we are not going to support flow control
671 * option, then let peer know that we can't
672 * change the flow control characters.
674 slctab
[SLC_XON
].defset
.flag
&= ~SLC_LEVELBITS
;
675 slctab
[SLC_XON
].defset
.flag
|= SLC_CANTCHANGE
;
676 slctab
[SLC_XOFF
].defset
.flag
&= ~SLC_LEVELBITS
;
677 slctab
[SLC_XOFF
].defset
.flag
|= SLC_CANTCHANGE
;
682 * For options that we might spin waiting for
683 * sub-negotiation, if the client turns off the
684 * option rather than responding to the request,
685 * we have to treat it here as if we got a response
686 * to the sub-negotiation, (by updating the timers)
687 * so that we'll break out of the loop.
690 settimer(ttypesubopt
);
694 settimer(tspeedsubopt
);
697 case TELOPT_XDISPLOC
:
698 settimer(xdisplocsubopt
);
701 case TELOPT_OLD_ENVIRON
:
702 settimer(oenvironsubopt
);
705 case TELOPT_NEW_ENVIRON
:
706 settimer(environsubopt
);
712 set_his_want_state_wont(option
);
713 if (his_state_is_will(option
))
714 send_dont(option
, 0);
718 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
719 if (lmodetype
< NO_AUTOKLUDGE
) {
720 lmodetype
= NO_LINEMODE
;
721 clientstat(TELOPT_LINEMODE
, WONT
, 0);
722 send_will(TELOPT_SGA
, 1);
723 send_will(TELOPT_ECHO
, 1);
725 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
733 set_his_state_wont(option
);
735 } /* end of wontoption */
738 send_will(int option
, int init
)
741 if ((will_wont_resp
[option
] == 0 && my_state_is_will(option
))||
742 my_want_state_is_will(option
))
744 set_my_want_state_will(option
);
745 will_wont_resp
[option
]++;
747 output_data((const char *)will
, option
);
749 DIAG(TD_OPTIONS
, printoption("td: send will", option
));
752 #if !defined(LINEMODE) || !defined(KLUDGELINEMODE)
754 * When we get a DONT SGA, we will try once to turn it
755 * back on. If the other side responds DONT SGA, we
756 * leave it at that. This is so that when we talk to
757 * clients that understand KLUDGELINEMODE but not LINEMODE,
758 * we'll keep them in char-at-a-time mode.
769 * Process client input.
772 DIAG(TD_OPTIONS
, printoption("td: recv do", option
));
774 if (will_wont_resp
[option
]) {
775 will_wont_resp
[option
]--;
776 if (will_wont_resp
[option
] && my_state_is_will(option
))
777 will_wont_resp
[option
]--;
779 if ((will_wont_resp
[option
] == 0) && (my_want_state_is_wont(option
))) {
783 # ifdef KLUDGELINEMODE
784 if (lmodetype
== NO_LINEMODE
)
786 if (his_state_is_wont(TELOPT_LINEMODE
))
805 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
807 * If kludge linemode is in use, then we must
808 * process an incoming do SGA for linemode
811 if (lmodetype
== KLUDGE_LINEMODE
) {
813 * Receipt of "do SGA" in kludge
814 * linemode is the peer asking us to
815 * turn off linemode. Make note of
818 clientstat(TELOPT_LINEMODE
, WONT
, 0);
820 * If linemode did not get turned off
821 * then don't tell peer that we did.
822 * Breaking here forces a wont SGA to
830 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
840 * Special case for TM. We send a WILL, but
841 * pretend we sent a WONT.
843 send_will(option
, 0);
844 set_my_want_state_wont(option
);
845 set_my_state_wont(option
);
850 * When we get a LOGOUT option, respond
851 * with a WILL LOGOUT, make sure that
852 * it gets written out to the network,
853 * and then just go away...
855 set_my_want_state_will(TELOPT_LOGOUT
);
856 send_will(TELOPT_LOGOUT
, 0);
857 set_my_state_will(TELOPT_LOGOUT
);
863 case TELOPT_LINEMODE
:
868 case TELOPT_XDISPLOC
:
869 #ifdef TELOPT_ENVIRON
870 case TELOPT_NEW_ENVIRON
:
872 case TELOPT_OLD_ENVIRON
:
877 set_my_want_state_will(option
);
878 send_will(option
, 0);
880 will_wont_resp
[option
]++;
881 send_wont(option
, 0);
884 set_my_state_will(option
);
886 } /* end of dooption */
889 send_wont(int option
, int init
)
892 if ((will_wont_resp
[option
] == 0 && my_state_is_wont(option
)) ||
893 my_want_state_is_wont(option
))
895 set_my_want_state_wont(option
);
896 will_wont_resp
[option
]++;
898 output_data((const char *)wont
, option
);
900 DIAG(TD_OPTIONS
, printoption("td: send wont", option
));
904 dontoption(int option
)
907 * Process client input.
911 DIAG(TD_OPTIONS
, printoption("td: recv dont", option
));
913 if (will_wont_resp
[option
]) {
914 will_wont_resp
[option
]--;
915 if (will_wont_resp
[option
] && my_state_is_wont(option
))
916 will_wont_resp
[option
]--;
918 if ((will_wont_resp
[option
] == 0) && (my_want_state_is_will(option
))) {
926 case TELOPT_ECHO
: /* we should stop echoing */
928 # ifdef KLUDGELINEMODE
929 if ((lmodetype
!= REAL_LINEMODE
) &&
930 (lmodetype
!= KLUDGE_LINEMODE
))
932 if (his_state_is_wont(TELOPT_LINEMODE
))
943 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
945 * If kludge linemode is in use, then we
946 * must process an incoming do SGA for
949 if ((lmodetype
== KLUDGE_LINEMODE
) ||
950 (lmodetype
== KLUDGE_OK
)) {
952 * The client is asking us to turn
955 lmodetype
= KLUDGE_LINEMODE
;
956 clientstat(TELOPT_LINEMODE
, WILL
, 0);
958 * If we did not turn line mode on,
959 * then what do we say? Will SGA?
960 * This violates design of telnet.
966 set_my_want_state_wont(option
);
967 if (my_state_is_will(option
))
968 send_wont(option
, 0);
969 set_my_state_wont(option
);
970 if (turn_on_sga
^= 1)
971 send_will(option
, 1);
973 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
979 set_my_want_state_wont(option
);
980 if (my_state_is_will(option
))
981 send_wont(option
, 0);
983 set_my_state_wont(option
);
985 } /* end of dontoption */
991 # define env_ovar OLD_ENV_VAR
992 # define env_ovalue OLD_ENV_VALUE
993 #endif /* ENV_HACK */
995 /* envvarok(char*) */
996 /* check that variable is safe to pass to login or shell */
1001 if (strcmp(varp
, "TERMCAP") && /* to prevent a security hole */
1002 strcmp(varp
, "TERMINFO") && /* with tgetent */
1003 strcmp(varp
, "TERMPATH") &&
1004 strcmp(varp
, "HOME") && /* to prevent the tegetent bug */
1005 strncmp(varp
, "LD_", strlen("LD_")) && /* most systems */
1006 strncmp(varp
, "_RLD_", strlen("_RLD_")) && /* IRIX */
1007 strcmp(varp
, "LIBPATH") && /* AIX */
1008 strcmp(varp
, "ENV") &&
1009 strcmp(varp
, "BASH_ENV") &&
1010 strcmp(varp
, "IFS") &&
1011 strncmp(varp
, "KRB5", strlen("KRB5")) && /* Krb5 */
1013 * The above case is a catch-all for now. Here are some of
1014 * the specific ones we must avoid passing, at least until
1015 * we can prove it can be done safely. Keep this list
1016 * around un case someone wants to remove the catch-all.
1018 strcmp(varp
, "KRB5_CONFIG") && /* Krb5 */
1019 strcmp(varp
, "KRB5CCNAME") && /* Krb5 */
1020 strcmp(varp
, "KRB5_KTNAME") && /* Krb5 */
1021 strcmp(varp
, "KRBTKFILE") && /* Krb4 */
1022 strcmp(varp
, "KRB_CONF") && /* CNS 4 */
1023 strcmp(varp
, "KRB_REALMS") && /* CNS 4 */
1024 strcmp(varp
, "RESOLV_HOST_CONF")) /* Linux */
1027 syslog(LOG_INFO
, "Rejected the attempt to modify the "
1028 "environment variable \"%s\"", varp
);
1036 * Look at the sub-option buffer, and try to be helpful to the other
1039 * Currently we recognize:
1051 DIAG(TD_OPTIONS
, {netflush(); printsub('<', subpointer
, SB_LEN()+2);});
1055 case TELOPT_TSPEED
: {
1058 if (his_state_is_wont(TELOPT_TSPEED
)) /* Ignore if option disabled */
1061 settimer(tspeedsubopt
);
1063 if (SB_EOF() || SB_GET() != TELQUAL_IS
)
1066 xspeed
= atoi((char *)subpointer
);
1068 while (SB_GET() != ',' && !SB_EOF());
1072 rspeed
= atoi((char *)subpointer
);
1073 clientstat(TELOPT_TSPEED
, xspeed
, rspeed
);
1077 } /* end of case TELOPT_TSPEED */
1079 case TELOPT_TTYPE
: { /* Yaaaay! */
1080 static char terminalname
[41];
1082 if (his_state_is_wont(TELOPT_TTYPE
)) /* Ignore if option disabled */
1084 settimer(ttypesubopt
);
1086 if (SB_EOF() || SB_GET() != TELQUAL_IS
) {
1087 return; /* ??? XXX but, this is the most robust */
1090 terminaltype
= terminalname
;
1092 while ((terminaltype
< (terminalname
+ sizeof terminalname
-1)) &&
1100 *terminaltype
++ = c
; /* accumulate name */
1103 terminaltype
= terminalname
;
1105 } /* end of case TELOPT_TTYPE */
1108 int xwinsize
, ywinsize
;
1110 if (his_state_is_wont(TELOPT_NAWS
)) /* Ignore if option disabled */
1115 xwinsize
= SB_GET() << 8;
1118 xwinsize
|= SB_GET();
1121 ywinsize
= SB_GET() << 8;
1124 ywinsize
|= SB_GET();
1125 clientstat(TELOPT_NAWS
, xwinsize
, ywinsize
);
1129 } /* end of case TELOPT_NAWS */
1132 case TELOPT_LINEMODE
: {
1135 if (his_state_is_wont(TELOPT_LINEMODE
)) /* Ignore if option disabled */
1138 * Process linemode suboptions.
1141 break; /* garbage was sent */
1142 request
= SB_GET(); /* get will/wont */
1145 break; /* another garbage check */
1147 if (request
== LM_SLC
) { /* SLC is not preceeded by WILL or WONT */
1149 * Process suboption buffer of slc's
1152 do_opt_slc(subpointer
, subend
- subpointer
);
1155 } else if (request
== LM_MODE
) {
1158 useeditmode
= SB_GET(); /* get mode flag */
1159 clientstat(LM_MODE
, 0, 0);
1165 switch (SB_GET()) { /* what suboption? */
1166 case LM_FORWARDMASK
:
1168 * According to spec, only server can send request for
1169 * forwardmask, and client can only return a positive response.
1170 * So don't worry about it.
1177 } /* end of case TELOPT_LINEMODE */
1179 case TELOPT_STATUS
: {
1187 if (my_state_is_will(TELOPT_STATUS
))
1198 } /* end of case TELOPT_STATUS */
1200 case TELOPT_XDISPLOC
: {
1201 if (SB_EOF() || SB_GET() != TELQUAL_IS
)
1203 settimer(xdisplocsubopt
);
1204 subpointer
[SB_LEN()] = '\0';
1205 if (setenv("DISPLAY", (char *)subpointer
, 1) == -1)
1206 syslog(LOG_ERR
, "setenv: cannot set DISPLAY=%s: %m", (char *)subpointer
);
1208 } /* end of case TELOPT_XDISPLOC */
1210 #ifdef TELOPT_NEW_ENVIRON
1211 case TELOPT_NEW_ENVIRON
:
1213 case TELOPT_OLD_ENVIRON
: {
1215 char *cp
, *varp
, *valp
;
1220 if (c
== TELQUAL_IS
) {
1221 if (subchar
== TELOPT_OLD_ENVIRON
)
1222 settimer(oenvironsubopt
);
1224 settimer(environsubopt
);
1225 } else if (c
!= TELQUAL_INFO
) {
1229 #ifdef TELOPT_NEW_ENVIRON
1230 if (subchar
== TELOPT_NEW_ENVIRON
) {
1233 if ((c
== NEW_ENV_VAR
) || (c
== ENV_USERVAR
))
1241 * We only want to do this if we haven't already decided
1242 * whether or not the other side has its VALUE and VAR
1246 int last
= -1; /* invalid value */
1248 int got_var
= 0, got_value
= 0, got_uservar
= 0;
1251 * The other side might have its VALUE and VAR values
1252 * reversed. To be interoperable, we need to determine
1253 * which way it is. If the first recognized character
1254 * is a VAR or VALUE, then that will tell us what
1255 * type of client it is. If the fist recognized
1256 * character is a USERVAR, then we continue scanning
1257 * the suboption looking for two consecutive
1258 * VAR or VALUE fields. We should not get two
1259 * consecutive VALUE fields, so finding two
1260 * consecutive VALUE or VAR fields will tell us
1261 * what the client is.
1268 if (last
< 0 || last
== OLD_ENV_VAR
1269 || (empty
&& (last
== OLD_ENV_VALUE
)))
1275 if (last
< 0 || last
== OLD_ENV_VALUE
1276 || (empty
&& (last
== OLD_ENV_VAR
)))
1277 goto env_ovar_wrong
;
1279 last
= OLD_ENV_VALUE
;
1282 /* count strings of USERVAR as one */
1283 if (last
!= ENV_USERVAR
)
1286 if (last
== OLD_ENV_VALUE
)
1288 if (last
== OLD_ENV_VAR
)
1289 goto env_ovar_wrong
;
1304 if (last
== OLD_ENV_VALUE
)
1306 if (last
== OLD_ENV_VAR
)
1307 goto env_ovar_wrong
;
1310 * Ok, the first thing was a USERVAR, and there
1311 * are not two consecutive VAR or VALUE commands,
1312 * and none of the VAR or VALUE commands are empty.
1313 * If the client has sent us a well-formed option,
1314 * then the number of VALUEs received should always
1315 * be less than or equal to the number of VARs and
1316 * USERVARs received.
1318 * If we got exactly as many VALUEs as VARs and
1319 * USERVARs, the client has the same definitions.
1321 * If we got exactly as many VARs as VALUEs and
1322 * USERVARS, the client has reversed definitions.
1324 if (got_uservar
+ got_var
== got_value
) {
1326 env_ovar
= OLD_ENV_VAR
;
1327 env_ovalue
= OLD_ENV_VALUE
;
1328 } else if (got_uservar
+ got_value
== got_var
) {
1330 env_ovar
= OLD_ENV_VALUE
;
1331 env_ovalue
= OLD_ENV_VAR
;
1333 output_data("ENVIRON VALUE and VAR are reversed!\r\n"));
1342 if ((c
== env_ovar
) || (c
== ENV_USERVAR
))
1350 cp
= varp
= (char *)subpointer
;
1355 if (subchar
== TELOPT_OLD_ENVIRON
) {
1358 else if (c
== env_ovalue
)
1365 cp
= valp
= (char *)subpointer
;
1371 if (envvarok(varp
)) {
1373 if (setenv(varp
, valp
, 1) == -1)
1374 syslog(LOG_ERR
, "setenv: cannot set %s=%s: %m", varp
, valp
);
1379 cp
= varp
= (char *)subpointer
;
1394 if (envvarok(varp
)) {
1396 if (setenv(varp
, valp
, 1) == -1)
1397 syslog(LOG_ERR
, "setenv: cannot set %s=%s: %m", varp
, valp
);
1403 } /* end of case TELOPT_NEW_ENVIRON */
1407 } /* end of switch */
1409 } /* end of suboption */
1414 clientstat(TELOPT_LINEMODE
, WILL
, 0);
1417 #define ADD(c) *ncp++ = c
1418 #define ADD_DATA(c) { *ncp++ = c; if (c == SE || c == IAC) *ncp++ = c; }
1422 unsigned char statusbuf
[256];
1428 netflush(); /* get rid of anything waiting to go out */
1436 * We check the want_state rather than the current state,
1437 * because if we received a DO/WILL for an option that we
1438 * don't support, and the other side didn't send a DONT/WONT
1439 * in response to our WONT/DONT, then the "state" will be
1440 * WILL/DO, and the "want_state" will be WONT/DONT. We
1441 * need to go by the latter.
1443 for (i
= 0; i
< (unsigned char)NTELOPTS
; i
++) {
1444 if (my_want_state_is_will(i
)) {
1450 if (his_want_state_is_will(i
)) {
1458 if (his_want_state_is_will(TELOPT_LFLOW
)) {
1468 if (restartany
>= 0) {
1472 ADD(LFLOW_RESTART_ANY
);
1474 ADD(LFLOW_RESTART_XON
);
1481 if (his_want_state_is_will(TELOPT_LINEMODE
)) {
1482 unsigned char *cp
, *cpe
;
1486 ADD(TELOPT_LINEMODE
);
1492 ADD(TELOPT_LINEMODE
);
1497 for (cpe
= cp
+ len
; cp
< cpe
; cp
++)
1501 #endif /* LINEMODE */
1506 output_datalen(statusbuf
, ncp
- statusbuf
);
1507 netflush(); /* Send it on its way */
1510 {printsub('>', statusbuf
, ncp
- statusbuf
); netflush();});
1514 * This function appends data to nfrontp and advances nfrontp.
1515 * Returns the number of characters written altogether (the
1516 * buffer may have been flushed in the process).
1520 output_data(const char *format
, ...)
1526 va_start(args
, format
);
1527 if ((len
= vasprintf(&buf
, format
, args
)) == -1)
1529 output_datalen(buf
, len
);
1536 output_datalen(const char *buf
, int len
)
1538 int remaining
, copied
;
1540 remaining
= BUFSIZ
- (nfrontp
- netobuf
);
1542 /* Free up enough space if the room is too low*/
1543 if ((len
> BUFSIZ
? BUFSIZ
: len
) > remaining
) {
1545 remaining
= BUFSIZ
- (nfrontp
- netobuf
);
1548 /* Copy out as much as will fit */
1549 copied
= remaining
> len
? len
: remaining
;
1550 memmove(nfrontp
, buf
, copied
);
1553 remaining
-= copied
;