1 /* $NetBSD: state.c,v 1.26 2005/06/21 22:29:53 lha Exp $ */
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
35 static char sccsid
[] = "@(#)state.c 8.5 (Berkeley) 5/30/95";
37 __RCSID("$NetBSD: state.c,v 1.26 2005/06/21 22:29:53 lha Exp $");
46 static int envvarok(char *);
48 unsigned const char doopt
[] = { IAC
, DO
, '%', 'c', 0 };
49 unsigned const char dont
[] = { IAC
, DONT
, '%', 'c', 0 };
50 unsigned const char will
[] = { IAC
, WILL
, '%', 'c', 0 };
51 unsigned const char wont
[] = { IAC
, WONT
, '%', 'c', 0 };
55 * Buffer for sub-options, and macros
56 * for suboptions buffer manipulations
58 unsigned char subbuffer
[4096], *subpointer
= subbuffer
, *subend
= subbuffer
;
60 #define SB_CLEAR() subpointer = subbuffer
61 #define SB_TERM() { subend = subpointer; SB_CLEAR(); }
62 #define SB_ACCUM(c) if (subpointer < (subbuffer+sizeof subbuffer)) { \
63 *subpointer++ = (c); \
65 #define SB_GET() ((*subpointer++)&0xff)
66 #define SB_EOF() (subpointer >= subend)
67 #define SB_LEN() (subend - subpointer)
70 unsigned char *subsave
;
71 #define SB_SAVE() subsave = subpointer;
72 #define SB_RESTORE() subpointer = subsave;
79 #define TS_DATA 0 /* base state */
80 #define TS_IAC 1 /* look for double IAC's */
81 #define TS_CR 2 /* CR-LF ->'s CR */
82 #define TS_SB 3 /* throw away begin's... */
83 #define TS_SE 4 /* ...end's (suboption negotiation) */
84 #define TS_WILL 5 /* will option negotiation */
85 #define TS_WONT 6 /* wont " */
86 #define TS_DO 7 /* do " */
87 #define TS_DONT 8 /* dont " */
93 static int state
= TS_DATA
;
96 if ((&ptyobuf
[BUFSIZ
] - pfrontp
) < 2)
98 c
= *netip
++ & 0377, ncc
--;
101 c
= (*decrypt_input
)(c
);
102 #endif /* ENCRYPTION */
107 /* Strip off \n or \0 after a \r */
108 if ((c
== 0) || (c
== '\n')) {
119 * We now map \r\n ==> \r for pragmatic reasons.
120 * Many client implementations send \r\n when
121 * the user hits the CarriageReturn key.
123 * We USED to map \r\n ==> \n, since \r\n says
124 * that we want to be in column 1 of the next
125 * printable line, and \n is the standard
126 * unix way of saying that (\r is only good
127 * if CRMOD is set, which it normally is).
129 if ((c
== '\r') && his_state_is_wont(TELOPT_BINARY
)) {
133 nc
= (*decrypt_input
)(nc
& 0xff);
134 #endif /* ENCRYPTION */
137 * If we are operating in linemode,
138 * convert to local end-of-line.
140 if (linemode
&& (ncc
> 0) && (('\n' == nc
) ||
141 ((0 == nc
) && tty_iscrnl())) ) {
149 (void)(*decrypt_input
)(-1);
150 #endif /* ENCRYPTION */
161 * Send the process on the pty side an
162 * interrupt. Do this with a NULL or
163 * interrupt char; depending on the tty mode.
167 printoption("td: recv IAC", c
));
173 printoption("td: recv IAC", c
));
182 printoption("td: recv IAC", c
));
192 printoption("td: recv IAC", c
));
193 ptyflush(); /* half-hearted */
196 if (slctab
[SLC_AO
].sptr
&&
197 *slctab
[SLC_AO
].sptr
!= (cc_t
)(_POSIX_VDISABLE
)) {
199 (unsigned char)*slctab
[SLC_AO
].sptr
;
202 netclear(); /* clear buffer back */
203 output_data("%c%c", IAC
, DM
);
204 neturg
= nfrontp
- 1; /* off by one XXX */
206 printoption("td: send IAC", DM
));
211 * Erase Character and
220 printoption("td: recv IAC", c
));
221 ptyflush(); /* half-hearted */
224 ch
= *slctab
[SLC_EC
].sptr
;
226 ch
= *slctab
[SLC_EL
].sptr
;
227 if (ch
!= (cc_t
)(_POSIX_VDISABLE
))
228 *pfrontp
++ = (unsigned char)ch
;
233 * Check for urgent data...
237 printoption("td: recv IAC", c
));
238 SYNCHing
= stilloob(net
);
244 * Begin option subnegotiation...
267 if (his_state_is_will(TELOPT_EOR
))
272 * Handle RFC 10xx Telnet linemode option additions
273 * to command stream (EOF, SUSP, ABORT).
306 * bad form of suboption negotiation.
307 * handle it in such a way as to avoid
308 * damage to local state. Parse
309 * suboption buffer found so far,
310 * then treat remaining stream as
311 * another command sequence.
314 /* for DIAGNOSTICS */
327 /* for DIAGNOSTICS */
333 suboption(); /* handle sub-option */
359 syslog(LOG_ERR
, "panic state=%d", state
);
360 printf("telnetd: panic state=%d\n", state
);
364 } /* end of telrcv */
367 * The will/wont/do/dont state machines are based on Dave Borman's
368 * Telnet option processing state machine.
370 * These correspond to the following states:
371 * my_state = the last negotiated state
372 * want_state = what I want the state to go to
373 * want_resp = how many requests I have sent
374 * All state defaults are negative, and resp defaults to 0.
376 * When initiating a request to change state to new_state:
378 * if ((want_resp == 0 && new_state == my_state) || want_state == new_state) {
381 * want_state = new_state;
386 * When receiving new_state:
390 * if (want_resp && (new_state == my_state))
393 * if ((want_resp == 0) && (new_state != want_state)) {
394 * if (ok_to_switch_to new_state)
395 * want_state = new_state;
400 * my_state = new_state;
402 * Note that new_state is implied in these functions by the function itself.
403 * will and do imply positive new_state, wont and dont imply negative.
405 * Finally, there is one catch. If we send a negative response to a
406 * positive request, my_state will be the positive while want_state will
407 * remain negative. my_state will revert to negative when the negative
408 * acknowlegment arrives from the peer. Thus, my_state generally tells
409 * us not only the last negotiated state, but also tells us what the peer
410 * wants to be doing as well. It is important to understand this difference
411 * as we may wish to be processing data streams based on our desired state
412 * (want_state) or based on what the peer thinks the state is (my_state).
414 * This all works fine because if the peer sends a positive request, the data
415 * that we receive prior to negative acknowlegment will probably be affected
416 * by the positive state, and we can process it as such (if we can; if we
417 * can't then it really doesn't matter). If it is that important, then the
418 * peer probably should be buffering until this option state negotiation
423 send_do(int option
, int init
)
426 if ((do_dont_resp
[option
] == 0 && his_state_is_will(option
)) ||
427 his_want_state_is_will(option
))
430 * Special case for TELOPT_TM: We send a DO, but pretend
431 * that we sent a DONT, so that we can send more DOs if
434 if (option
== TELOPT_TM
)
435 set_his_want_state_wont(option
);
437 set_his_want_state_will(option
);
438 do_dont_resp
[option
]++;
440 (void) output_data((const char *)doopt
, option
);
442 DIAG(TD_OPTIONS
, printoption("td: send do", option
));
446 extern void doclientstat(void);
449 #ifdef AUTHENTICATION
450 extern void auth_request(void); /* libtelnet */
453 extern void encrypt_send_support(void);
454 #endif /* ENCRYPTION */
458 willoption(int option
)
461 void (*func
)(void) = 0;
464 * process input from peer.
467 DIAG(TD_OPTIONS
, printoption("td: recv will", option
));
469 if (do_dont_resp
[option
]) {
470 do_dont_resp
[option
]--;
471 if (do_dont_resp
[option
] && his_state_is_will(option
))
472 do_dont_resp
[option
]--;
474 if (do_dont_resp
[option
] == 0) {
475 if (his_want_state_is_wont(option
)) {
487 * See comments below for more info.
489 not42
= 0; /* looks like a 4.2 system */
493 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
495 * This telnetd implementation does not really
496 * support timing marks, it just uses them to
497 * support the kludge linemode stuff. If we
498 * receive a will or wont TM in response to our
499 * do TM request that may have been sent to
500 * determine kludge linemode support, process
501 * it, otherwise TM should get a negative
505 * Handle the linemode kludge stuff.
506 * If we are not currently supporting any
507 * linemode at all, then we assume that this
508 * is the client telling us to use kludge
509 * linemode in response to our query. Set the
510 * linemode type that is to be supported, note
511 * that the client wishes to use linemode, and
512 * eat the will TM as though it never arrived.
514 if (lmodetype
< KLUDGE_LINEMODE
) {
515 lmodetype
= KLUDGE_LINEMODE
;
516 clientstat(TELOPT_LINEMODE
, WILL
, 0);
517 send_wont(TELOPT_SGA
, 1);
518 } else if (lmodetype
== NO_AUTOKLUDGE
) {
519 lmodetype
= KLUDGE_OK
;
521 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
523 * We never respond to a WILL TM, and
524 * we leave the state WONT.
530 * If we are going to support flow control
531 * option, then don't worry peer that we can't
532 * change the flow control characters.
534 slctab
[SLC_XON
].defset
.flag
&= ~SLC_LEVELBITS
;
535 slctab
[SLC_XON
].defset
.flag
|= SLC_DEFAULT
;
536 slctab
[SLC_XOFF
].defset
.flag
&= ~SLC_LEVELBITS
;
537 slctab
[SLC_XOFF
].defset
.flag
|= SLC_DEFAULT
;
542 case TELOPT_XDISPLOC
:
543 case TELOPT_NEW_ENVIRON
:
544 case TELOPT_OLD_ENVIRON
:
549 case TELOPT_LINEMODE
:
550 # ifdef KLUDGELINEMODE
552 * Note client's desire to use linemode.
554 lmodetype
= REAL_LINEMODE
;
555 # endif /* KLUDGELINEMODE */
559 #endif /* LINEMODE */
561 #ifdef AUTHENTICATION
562 case TELOPT_AUTHENTICATION
:
570 func
= encrypt_send_support
;
573 #endif /* ENCRYPTION */
579 set_his_want_state_will(option
);
582 do_dont_resp
[option
]++;
583 send_dont(option
, 0);
587 * Option processing that should happen when
588 * we receive conformation of a change in
589 * state that we had requested.
593 not42
= 0; /* looks like a 4.2 system */
595 * Egads, he responded "WILL ECHO". Turn
598 send_dont(option
, 1);
600 * "WILL ECHO". Kludge upon kludge!
601 * A 4.2 client is now echoing user input at
602 * the tty. This is probably undesireable and
603 * it should be stopped. The client will
604 * respond WONT TM to the DO TM that we send to
605 * check for kludge linemode. When the WONT TM
606 * arrives, linemode will be turned off and a
607 * change propogated to the pty. This change
608 * will cause us to process the new pty state
609 * in localstat(), which will notice that
610 * linemode is off and send a WILL ECHO
611 * so that we are properly in character mode and
616 case TELOPT_LINEMODE
:
617 # ifdef KLUDGELINEMODE
619 * Note client's desire to use linemode.
621 lmodetype
= REAL_LINEMODE
;
622 # endif /* KLUDGELINEMODE */
625 #endif /* LINEMODE */
627 #ifdef AUTHENTICATION
628 case TELOPT_AUTHENTICATION
:
635 func
= encrypt_send_support
;
637 #endif /* ENCRYPTION */
645 set_his_state_will(option
);
648 } /* end of willoption */
651 send_dont(int option
, int init
)
654 if ((do_dont_resp
[option
] == 0 && his_state_is_wont(option
)) ||
655 his_want_state_is_wont(option
))
657 set_his_want_state_wont(option
);
658 do_dont_resp
[option
]++;
660 (void) output_data((const char *)dont
, option
);
662 DIAG(TD_OPTIONS
, printoption("td: send dont", option
));
666 wontoption(int option
)
669 * Process client input.
672 DIAG(TD_OPTIONS
, printoption("td: recv wont", option
));
674 if (do_dont_resp
[option
]) {
675 do_dont_resp
[option
]--;
676 if (do_dont_resp
[option
] && his_state_is_wont(option
))
677 do_dont_resp
[option
]--;
679 if (do_dont_resp
[option
] == 0) {
680 if (his_want_state_is_will(option
)) {
681 /* it is always ok to change to negative state */
684 not42
= 1; /* doesn't seem to be a 4.2 system */
694 case TELOPT_LINEMODE
:
695 # ifdef KLUDGELINEMODE
697 * If real linemode is supported, then client is
698 * asking to turn linemode off.
700 if (lmodetype
!= REAL_LINEMODE
)
702 /* XXX double-check this --thorpej */
703 lmodetype
= KLUDGE_LINEMODE
;
704 # endif /* KLUDGELINEMODE */
705 clientstat(TELOPT_LINEMODE
, WONT
, 0);
707 #endif /* LINEMODE */
711 * If we get a WONT TM, and had sent a DO TM,
712 * don't respond with a DONT TM, just leave it
713 * as is. Short circut the state machine to
716 set_his_want_state_wont(TELOPT_TM
);
721 * If we are not going to support flow control
722 * option, then let peer know that we can't
723 * change the flow control characters.
725 slctab
[SLC_XON
].defset
.flag
&= ~SLC_LEVELBITS
;
726 slctab
[SLC_XON
].defset
.flag
|= SLC_CANTCHANGE
;
727 slctab
[SLC_XOFF
].defset
.flag
&= ~SLC_LEVELBITS
;
728 slctab
[SLC_XOFF
].defset
.flag
|= SLC_CANTCHANGE
;
731 #ifdef AUTHENTICATION
732 case TELOPT_AUTHENTICATION
:
733 auth_finished(0, AUTH_REJECT
);
738 * For options that we might spin waiting for
739 * sub-negotiation, if the client turns off the
740 * option rather than responding to the request,
741 * we have to treat it here as if we got a response
742 * to the sub-negotiation, (by updating the timers)
743 * so that we'll break out of the loop.
746 settimer(ttypesubopt
);
750 settimer(tspeedsubopt
);
753 case TELOPT_XDISPLOC
:
754 settimer(xdisplocsubopt
);
757 case TELOPT_OLD_ENVIRON
:
758 settimer(oenvironsubopt
);
761 case TELOPT_NEW_ENVIRON
:
762 settimer(environsubopt
);
768 set_his_want_state_wont(option
);
769 if (his_state_is_will(option
))
770 send_dont(option
, 0);
774 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
775 if (lmodetype
< NO_AUTOKLUDGE
) {
776 lmodetype
= NO_LINEMODE
;
777 clientstat(TELOPT_LINEMODE
, WONT
, 0);
778 send_will(TELOPT_SGA
, 1);
779 send_will(TELOPT_ECHO
, 1);
781 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
784 #ifdef AUTHENTICATION
785 case TELOPT_AUTHENTICATION
:
786 auth_finished(0, AUTH_REJECT
);
794 set_his_state_wont(option
);
796 } /* end of wontoption */
799 send_will(int option
, int init
)
802 if ((will_wont_resp
[option
] == 0 && my_state_is_will(option
))||
803 my_want_state_is_will(option
))
805 set_my_want_state_will(option
);
806 will_wont_resp
[option
]++;
808 (void) output_data((const char *)will
, option
);
810 DIAG(TD_OPTIONS
, printoption("td: send will", option
));
813 #if !defined(LINEMODE) || !defined(KLUDGELINEMODE)
815 * When we get a DONT SGA, we will try once to turn it
816 * back on. If the other side responds DONT SGA, we
817 * leave it at that. This is so that when we talk to
818 * clients that understand KLUDGELINEMODE but not LINEMODE,
819 * we'll keep them in char-at-a-time mode.
830 * Process client input.
833 DIAG(TD_OPTIONS
, printoption("td: recv do", option
));
835 if (will_wont_resp
[option
]) {
836 will_wont_resp
[option
]--;
837 if (will_wont_resp
[option
] && my_state_is_will(option
))
838 will_wont_resp
[option
]--;
840 if ((will_wont_resp
[option
] == 0) && (my_want_state_is_wont(option
))) {
844 # ifdef KLUDGELINEMODE
845 if (lmodetype
== NO_LINEMODE
)
847 if (his_state_is_wont(TELOPT_LINEMODE
))
866 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
868 * If kludge linemode is in use, then we must
869 * process an incoming do SGA for linemode
872 if (lmodetype
== KLUDGE_LINEMODE
) {
874 * Receipt of "do SGA" in kludge
875 * linemode is the peer asking us to
876 * turn off linemode. Make note of
879 clientstat(TELOPT_LINEMODE
, WONT
, 0);
881 * If linemode did not get turned off
882 * then don't tell peer that we did.
883 * Breaking here forces a wont SGA to
891 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
901 * Special case for TM. We send a WILL, but
902 * pretend we sent a WONT.
904 send_will(option
, 0);
905 set_my_want_state_wont(option
);
906 set_my_state_wont(option
);
911 * When we get a LOGOUT option, respond
912 * with a WILL LOGOUT, make sure that
913 * it gets written out to the network,
914 * and then just go away...
916 set_my_want_state_will(TELOPT_LOGOUT
);
917 send_will(TELOPT_LOGOUT
, 0);
918 set_my_state_will(TELOPT_LOGOUT
);
928 #endif /* ENCRYPTION */
930 case TELOPT_LINEMODE
:
935 case TELOPT_XDISPLOC
:
936 case TELOPT_OLD_ENVIRON
:
941 set_my_want_state_will(option
);
942 send_will(option
, 0);
944 will_wont_resp
[option
]++;
945 send_wont(option
, 0);
948 set_my_state_will(option
);
950 } /* end of dooption */
953 send_wont(int option
, int init
)
956 if ((will_wont_resp
[option
] == 0 && my_state_is_wont(option
)) ||
957 my_want_state_is_wont(option
))
959 set_my_want_state_wont(option
);
960 will_wont_resp
[option
]++;
962 (void) output_data((const char *)wont
, option
);
964 DIAG(TD_OPTIONS
, printoption("td: send wont", option
));
968 dontoption(int option
)
971 * Process client input.
975 DIAG(TD_OPTIONS
, printoption("td: recv dont", option
));
977 if (will_wont_resp
[option
]) {
978 will_wont_resp
[option
]--;
979 if (will_wont_resp
[option
] && my_state_is_wont(option
))
980 will_wont_resp
[option
]--;
982 if ((will_wont_resp
[option
] == 0) && (my_want_state_is_will(option
))) {
990 case TELOPT_ECHO
: /* we should stop echoing */
992 # ifdef KLUDGELINEMODE
993 if ((lmodetype
!= REAL_LINEMODE
) &&
994 (lmodetype
!= KLUDGE_LINEMODE
))
996 if (his_state_is_wont(TELOPT_LINEMODE
))
1007 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
1009 * If kludge linemode is in use, then we
1010 * must process an incoming do SGA for
1011 * linemode purposes.
1013 if ((lmodetype
== KLUDGE_LINEMODE
) ||
1014 (lmodetype
== KLUDGE_OK
)) {
1016 * The client is asking us to turn
1019 lmodetype
= KLUDGE_LINEMODE
;
1020 clientstat(TELOPT_LINEMODE
, WILL
, 0);
1022 * If we did not turn line mode on,
1023 * then what do we say? Will SGA?
1024 * This violates design of telnet.
1025 * Gross. Very Gross.
1030 set_my_want_state_wont(option
);
1031 if (my_state_is_will(option
))
1032 send_wont(option
, 0);
1033 set_my_state_wont(option
);
1034 if (turn_on_sga
^= 1)
1035 send_will(option
, 1);
1037 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
1043 set_my_want_state_wont(option
);
1044 if (my_state_is_will(option
))
1045 send_wont(option
, 0);
1047 set_my_state_wont(option
);
1049 } /* end of dontoption */
1053 int env_ovalue
= -1;
1054 #else /* ENV_HACK */
1055 # define env_ovar OLD_ENV_VAR
1056 # define env_ovalue OLD_ENV_VALUE
1057 #endif /* ENV_HACK */
1059 /* envvarok(char*) */
1060 /* check that variable is safe to pass to login or shell */
1062 envvarok(char *varp
)
1065 if (strcmp(varp
, "TERMCAP") && /* to prevent a security hole */
1066 strcmp(varp
, "TERMINFO") && /* with tgetent */
1067 strcmp(varp
, "TERMPATH") &&
1068 strcmp(varp
, "HOME") && /* to prevent the tegetent bug */
1069 strncmp(varp
, "LD_", strlen("LD_")) && /* most systems */
1070 strncmp(varp
, "_RLD_", strlen("_RLD_")) && /* IRIX */
1071 strcmp(varp
, "LIBPATH") && /* AIX */
1072 strcmp(varp
, "ENV") &&
1073 strcmp(varp
, "BASH_ENV") &&
1074 strcmp(varp
, "IFS") &&
1075 strncmp(varp
, "KRB5", strlen("KRB5")) && /* Krb5 */
1077 * The above case is a catch-all for now. Here are some of
1078 * the specific ones we must avoid passing, at least until
1079 * we can prove it can be done safely. Keep this list
1080 * around un case someone wants to remove the catch-all.
1082 strcmp(varp
, "KRB5_CONFIG") && /* Krb5 */
1083 strcmp(varp
, "KRB5CCNAME") && /* Krb5 */
1084 strcmp(varp
, "KRB5_KTNAME") && /* Krb5 */
1085 strcmp(varp
, "KRBTKFILE") && /* Krb4 */
1086 strcmp(varp
, "KRB_CONF") && /* CNS 4 */
1087 strcmp(varp
, "KRB_REALMS") && /* CNS 4 */
1088 strcmp(varp
, "RESOLV_HOST_CONF")) /* Linux */
1091 syslog(LOG_INFO
, "Rejected the attempt to modify the "
1092 "environment variable \"%s\"", varp
);
1100 * Look at the sub-option buffer, and try to be helpful to the other
1103 * Currently we recognize:
1115 DIAG(TD_OPTIONS
, {netflush(); printsub('<', subpointer
, SB_LEN()+2);});
1119 case TELOPT_TSPEED
: {
1122 if (his_state_is_wont(TELOPT_TSPEED
)) /* Ignore if option disabled */
1125 settimer(tspeedsubopt
);
1127 if (SB_EOF() || SB_GET() != TELQUAL_IS
)
1130 xspeed
= atoi((char *)subpointer
);
1132 while (SB_GET() != ',' && !SB_EOF());
1136 rspeed
= atoi((char *)subpointer
);
1137 clientstat(TELOPT_TSPEED
, xspeed
, rspeed
);
1141 } /* end of case TELOPT_TSPEED */
1143 case TELOPT_TTYPE
: { /* Yaaaay! */
1146 if (his_state_is_wont(TELOPT_TTYPE
)) /* Ignore if option disabled */
1148 settimer(ttypesubopt
);
1150 if (SB_EOF() || SB_GET() != TELQUAL_IS
) {
1151 return; /* ??? XXX but, this is the most robust */
1156 while ((p
< (terminaltype
+ sizeof terminaltype
-1)) &&
1164 *p
++ = c
; /* accumulate name */
1168 } /* end of case TELOPT_TTYPE */
1171 int xwinsize
, ywinsize
;
1173 if (his_state_is_wont(TELOPT_NAWS
)) /* Ignore if option disabled */
1178 xwinsize
= SB_GET() << 8;
1181 xwinsize
|= SB_GET();
1184 ywinsize
= SB_GET() << 8;
1187 ywinsize
|= SB_GET();
1188 clientstat(TELOPT_NAWS
, xwinsize
, ywinsize
);
1192 } /* end of case TELOPT_NAWS */
1195 case TELOPT_LINEMODE
: {
1198 if (his_state_is_wont(TELOPT_LINEMODE
)) /* Ignore if option disabled */
1201 * Process linemode suboptions.
1204 break; /* garbage was sent */
1205 request
= SB_GET(); /* get will/wont */
1208 break; /* another garbage check */
1210 if (request
== LM_SLC
) { /* SLC is not preceded by WILL or WONT */
1212 * Process suboption buffer of slc's
1215 do_opt_slc(subpointer
, subend
- subpointer
);
1218 } else if (request
== LM_MODE
) {
1221 useeditmode
= SB_GET(); /* get mode flag */
1222 clientstat(LM_MODE
, 0, 0);
1228 switch (SB_GET()) { /* what suboption? */
1229 case LM_FORWARDMASK
:
1231 * According to spec, only server can send request for
1232 * forwardmask, and client can only return a positive response.
1233 * So don't worry about it.
1240 } /* end of case TELOPT_LINEMODE */
1242 case TELOPT_STATUS
: {
1250 if (my_state_is_will(TELOPT_STATUS
))
1261 } /* end of case TELOPT_STATUS */
1263 case TELOPT_XDISPLOC
: {
1264 if (SB_EOF() || SB_GET() != TELQUAL_IS
)
1266 settimer(xdisplocsubopt
);
1267 subpointer
[SB_LEN()] = '\0';
1268 (void)setenv("DISPLAY", (char *)subpointer
, 1);
1270 } /* end of case TELOPT_XDISPLOC */
1272 case TELOPT_NEW_ENVIRON
:
1273 case TELOPT_OLD_ENVIRON
: {
1275 char *cp
, *varp
, *valp
;
1280 if (c
== TELQUAL_IS
) {
1281 if (subchar
== TELOPT_OLD_ENVIRON
)
1282 settimer(oenvironsubopt
);
1284 settimer(environsubopt
);
1285 } else if (c
!= TELQUAL_INFO
) {
1289 if (subchar
== TELOPT_NEW_ENVIRON
) {
1292 if ((c
== NEW_ENV_VAR
) || (c
== ENV_USERVAR
))
1299 * We only want to do this if we haven't already decided
1300 * whether or not the other side has its VALUE and VAR
1304 int last
= -1; /* invalid value */
1306 int got_var
= 0, got_value
= 0, got_uservar
= 0;
1309 * The other side might have its VALUE and VAR values
1310 * reversed. To be interoperable, we need to determine
1311 * which way it is. If the first recognized character
1312 * is a VAR or VALUE, then that will tell us what
1313 * type of client it is. If the fist recognized
1314 * character is a USERVAR, then we continue scanning
1315 * the suboption looking for two consecutive
1316 * VAR or VALUE fields. We should not get two
1317 * consecutive VALUE fields, so finding two
1318 * consecutive VALUE or VAR fields will tell us
1319 * what the client is.
1326 if (last
< 0 || last
== OLD_ENV_VAR
1327 || (empty
&& (last
== OLD_ENV_VALUE
)))
1333 if (last
< 0 || last
== OLD_ENV_VALUE
1334 || (empty
&& (last
== OLD_ENV_VAR
)))
1335 goto env_ovar_wrong
;
1337 last
= OLD_ENV_VALUE
;
1340 /* count strings of USERVAR as one */
1341 if (last
!= ENV_USERVAR
)
1344 if (last
== OLD_ENV_VALUE
)
1346 if (last
== OLD_ENV_VAR
)
1347 goto env_ovar_wrong
;
1362 if (last
== OLD_ENV_VALUE
)
1364 if (last
== OLD_ENV_VAR
)
1365 goto env_ovar_wrong
;
1368 * Ok, the first thing was a USERVAR, and there
1369 * are not two consecutive VAR or VALUE commands,
1370 * and none of the VAR or VALUE commands are empty.
1371 * If the client has sent us a well-formed option,
1372 * then the number of VALUEs received should always
1373 * be less than or equal to the number of VARs and
1374 * USERVARs received.
1376 * If we got exactly as many VALUEs as VARs and
1377 * USERVARs, the client has the same definitions.
1379 * If we got exactly as many VARs as VALUEs and
1380 * USERVARS, the client has reversed definitions.
1382 if (got_uservar
+ got_var
== got_value
) {
1384 env_ovar
= OLD_ENV_VAR
;
1385 env_ovalue
= OLD_ENV_VALUE
;
1386 } else if (got_uservar
+ got_value
== got_var
) {
1388 env_ovar
= OLD_ENV_VALUE
;
1389 env_ovalue
= OLD_ENV_VAR
;
1390 DIAG(TD_OPTIONS
, {output_data(
1391 "ENVIRON VALUE and VAR are reversed!\r\n");});
1400 if ((c
== env_ovar
) || (c
== ENV_USERVAR
))
1408 cp
= varp
= (char *)subpointer
;
1413 if (subchar
== TELOPT_OLD_ENVIRON
) {
1416 else if (c
== env_ovalue
)
1423 cp
= valp
= (char *)subpointer
;
1429 if (envvarok(varp
)) {
1431 (void)setenv(varp
, valp
, 1);
1435 cp
= varp
= (char *)subpointer
;
1450 if (envvarok(varp
)) {
1452 (void)setenv(varp
, valp
, 1);
1457 } /* end of case TELOPT_NEW_ENVIRON */
1458 #ifdef AUTHENTICATION
1459 case TELOPT_AUTHENTICATION
:
1466 * These are sent by us and cannot be sent by
1471 auth_is(subpointer
, SB_LEN());
1474 auth_name(subpointer
, SB_LEN());
1480 case TELOPT_ENCRYPT
:
1484 case ENCRYPT_SUPPORT
:
1485 encrypt_support(subpointer
, SB_LEN());
1488 encrypt_is(subpointer
, SB_LEN());
1491 encrypt_reply(subpointer
, SB_LEN());
1494 encrypt_start(subpointer
, SB_LEN());
1499 case ENCRYPT_REQSTART
:
1500 encrypt_request_start(subpointer
, SB_LEN());
1502 case ENCRYPT_REQEND
:
1504 * We can always send an REQEND so that we cannot
1505 * get stuck encrypting. We should only get this
1506 * if we have been able to get in the correct mode
1509 encrypt_request_end();
1511 case ENCRYPT_ENC_KEYID
:
1512 encrypt_enc_keyid(subpointer
, SB_LEN());
1514 case ENCRYPT_DEC_KEYID
:
1515 encrypt_dec_keyid(subpointer
, SB_LEN());
1521 #endif /* ENCRYPTION */
1525 } /* end of switch */
1527 } /* end of suboption */
1533 clientstat(TELOPT_LINEMODE
, WILL
, 0);
1535 #endif /* LINEMODE */
1547 #define ADD_DATA(c) \
1549 ADD(c); if (c == SE || c == IAC) ADD(c); \
1552 unsigned char statusbuf
[256];
1558 ep
= statusbuf
+ sizeof(statusbuf
);
1560 netflush(); /* get rid of anything waiting to go out */
1568 * We check the want_state rather than the current state,
1569 * because if we received a DO/WILL for an option that we
1570 * don't support, and the other side didn't send a DONT/WONT
1571 * in response to our WONT/DONT, then the "state" will be
1572 * WILL/DO, and the "want_state" will be WONT/DONT. We
1573 * need to go by the latter.
1575 for (i
= 0; i
< (unsigned char)NTELOPTS
; i
++) {
1576 if (my_want_state_is_will(i
)) {
1580 if (his_want_state_is_will(i
)) {
1586 if (his_want_state_is_will(TELOPT_LFLOW
)) {
1596 if (restartany
>= 0) {
1600 ADD(LFLOW_RESTART_ANY
);
1602 ADD(LFLOW_RESTART_XON
);
1609 if (his_want_state_is_will(TELOPT_LINEMODE
)) {
1610 unsigned char *cp
, *cpe
;
1614 ADD(TELOPT_LINEMODE
);
1620 ADD(TELOPT_LINEMODE
);
1625 for (cpe
= cp
+ len
; cp
< cpe
; cp
++)
1629 #endif /* LINEMODE */
1634 writenet(statusbuf
, ncp
- statusbuf
);
1635 netflush(); /* Send it on its way */
1638 {printsub('>', statusbuf
, ncp
- statusbuf
); netflush();});
1649 output_data(const char *format
, ...)
1652 size_t remaining
, ret
;
1654 va_start(args
, format
);
1655 remaining
= BUFSIZ
- (nfrontp
- netobuf
);
1656 /* try a netflush() if the room is too low */
1657 if (strlen(format
) > remaining
|| BUFSIZ
/ 4 > remaining
) {
1659 remaining
= BUFSIZ
- (nfrontp
- netobuf
);
1661 ret
= vsnprintf(nfrontp
, remaining
, format
, args
);
1662 nfrontp
+= ((ret
< remaining
- 1) ? ret
: remaining
- 1);
1668 output_datalen(const char *buf
, size_t l
)
1672 remaining
= BUFSIZ
- (nfrontp
- netobuf
);
1673 if (remaining
< l
) {
1675 remaining
= BUFSIZ
- (nfrontp
- netobuf
);
1679 memmove(nfrontp
, buf
, l
);