1 /* $NetBSD: state.c,v 1.29 2012/01/09 16:36:48 christos 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.29 2012/01/09 16:36:48 christos Exp $");
46 static int envvarok(char *);
51 * Buffer for sub-options, and macros
52 * for suboptions buffer manipulations
54 unsigned char subbuffer
[4096], *subpointer
= subbuffer
, *subend
= subbuffer
;
56 #define SB_CLEAR() subpointer = subbuffer
57 #define SB_TERM() { subend = subpointer; SB_CLEAR(); }
58 #define SB_ACCUM(c) if (subpointer < (subbuffer+sizeof subbuffer)) { \
59 *subpointer++ = (c); \
61 #define SB_GET() ((*subpointer++)&0xff)
62 #define SB_EOF() (subpointer >= subend)
63 #define SB_LEN() (subend - subpointer)
66 unsigned char *subsave
;
67 #define SB_SAVE() subsave = subpointer;
68 #define SB_RESTORE() subpointer = subsave;
75 #define TS_DATA 0 /* base state */
76 #define TS_IAC 1 /* look for double IAC's */
77 #define TS_CR 2 /* CR-LF ->'s CR */
78 #define TS_SB 3 /* throw away begin's... */
79 #define TS_SE 4 /* ...end's (suboption negotiation) */
80 #define TS_WILL 5 /* will option negotiation */
81 #define TS_WONT 6 /* wont " */
82 #define TS_DO 7 /* do " */
83 #define TS_DONT 8 /* dont " */
89 static int state
= TS_DATA
;
92 if ((&ptyobuf
[BUFSIZ
] - pfrontp
) < 2)
94 c
= *netip
++ & 0377, ncc
--;
97 c
= (*decrypt_input
)(c
);
98 #endif /* ENCRYPTION */
103 /* Strip off \n or \0 after a \r */
104 if ((c
== 0) || (c
== '\n')) {
115 * We now map \r\n ==> \r for pragmatic reasons.
116 * Many client implementations send \r\n when
117 * the user hits the CarriageReturn key.
119 * We USED to map \r\n ==> \n, since \r\n says
120 * that we want to be in column 1 of the next
121 * printable line, and \n is the standard
122 * unix way of saying that (\r is only good
123 * if CRMOD is set, which it normally is).
125 if ((c
== '\r') && his_state_is_wont(TELOPT_BINARY
)) {
128 #endif /* !__minix */
131 nc
= (*decrypt_input
)(nc
& 0xff);
132 #endif /* ENCRYPTION */
135 * If we are operating in linemode,
136 * convert to local end-of-line.
138 if (linemode
&& (ncc
> 0) && (('\n' == nc
) ||
139 ((0 == nc
) && tty_iscrnl())) ) {
147 (void)(*decrypt_input
)(-1);
148 #endif /* ENCRYPTION */
159 * Send the process on the pty side an
160 * interrupt. Do this with a NULL or
161 * interrupt char; depending on the tty mode.
165 printoption("td: recv IAC", c
));
171 printoption("td: recv IAC", c
));
180 printoption("td: recv IAC", c
));
190 printoption("td: recv IAC", c
));
191 ptyflush(); /* half-hearted */
194 if (slctab
[SLC_AO
].sptr
&&
195 *slctab
[SLC_AO
].sptr
!= (cc_t
)(_POSIX_VDISABLE
)) {
197 (unsigned char)*slctab
[SLC_AO
].sptr
;
200 netclear(); /* clear buffer back */
201 output_data("%c%c", IAC
, DM
);
202 neturg
= nfrontp
- 1; /* off by one XXX */
204 printoption("td: send IAC", DM
));
209 * Erase Character and
218 printoption("td: recv IAC", c
));
219 ptyflush(); /* half-hearted */
222 ch
= *slctab
[SLC_EC
].sptr
;
224 ch
= *slctab
[SLC_EL
].sptr
;
225 if (ch
!= (cc_t
)(_POSIX_VDISABLE
))
226 *pfrontp
++ = (unsigned char)ch
;
231 * Check for urgent data...
235 printoption("td: recv IAC", c
));
236 SYNCHing
= stilloob(net
);
242 * Begin option subnegotiation...
265 if (his_state_is_will(TELOPT_EOR
))
270 * Handle RFC 10xx Telnet linemode option additions
271 * to command stream (EOF, SUSP, ABORT).
304 * bad form of suboption negotiation.
305 * handle it in such a way as to avoid
306 * damage to local state. Parse
307 * suboption buffer found so far,
308 * then treat remaining stream as
309 * another command sequence.
312 /* for DIAGNOSTICS */
325 /* for DIAGNOSTICS */
331 suboption(); /* handle sub-option */
357 syslog(LOG_ERR
, "panic state=%d", state
);
358 printf("telnetd: panic state=%d\n", state
);
362 } /* end of telrcv */
365 * The will/wont/do/dont state machines are based on Dave Borman's
366 * Telnet option processing state machine.
368 * These correspond to the following states:
369 * my_state = the last negotiated state
370 * want_state = what I want the state to go to
371 * want_resp = how many requests I have sent
372 * All state defaults are negative, and resp defaults to 0.
374 * When initiating a request to change state to new_state:
376 * if ((want_resp == 0 && new_state == my_state) || want_state == new_state) {
379 * want_state = new_state;
384 * When receiving new_state:
388 * if (want_resp && (new_state == my_state))
391 * if ((want_resp == 0) && (new_state != want_state)) {
392 * if (ok_to_switch_to new_state)
393 * want_state = new_state;
398 * my_state = new_state;
400 * Note that new_state is implied in these functions by the function itself.
401 * will and do imply positive new_state, wont and dont imply negative.
403 * Finally, there is one catch. If we send a negative response to a
404 * positive request, my_state will be the positive while want_state will
405 * remain negative. my_state will revert to negative when the negative
406 * acknowlegment arrives from the peer. Thus, my_state generally tells
407 * us not only the last negotiated state, but also tells us what the peer
408 * wants to be doing as well. It is important to understand this difference
409 * as we may wish to be processing data streams based on our desired state
410 * (want_state) or based on what the peer thinks the state is (my_state).
412 * This all works fine because if the peer sends a positive request, the data
413 * that we receive prior to negative acknowlegment will probably be affected
414 * by the positive state, and we can process it as such (if we can; if we
415 * can't then it really doesn't matter). If it is that important, then the
416 * peer probably should be buffering until this option state negotiation
421 send_do(int option
, int init
)
424 if ((do_dont_resp
[option
] == 0 && his_state_is_will(option
)) ||
425 his_want_state_is_will(option
))
428 * Special case for TELOPT_TM: We send a DO, but pretend
429 * that we sent a DONT, so that we can send more DOs if
432 if (option
== TELOPT_TM
)
433 set_his_want_state_wont(option
);
435 set_his_want_state_will(option
);
436 do_dont_resp
[option
]++;
438 (void) output_data("%c%c%c", IAC
, DO
, option
);
440 DIAG(TD_OPTIONS
, printoption("td: send do", option
));
444 extern void doclientstat(void);
447 #ifdef AUTHENTICATION
448 extern void auth_request(void); /* libtelnet */
451 extern void encrypt_send_support(void);
452 #endif /* ENCRYPTION */
456 willoption(int option
)
459 void (*func
)(void) = 0;
462 * process input from peer.
465 DIAG(TD_OPTIONS
, printoption("td: recv will", option
));
467 if (do_dont_resp
[option
]) {
468 do_dont_resp
[option
]--;
469 if (do_dont_resp
[option
] && his_state_is_will(option
))
470 do_dont_resp
[option
]--;
472 if (do_dont_resp
[option
] == 0) {
473 if (his_want_state_is_wont(option
)) {
485 * See comments below for more info.
487 not42
= 0; /* looks like a 4.2 system */
491 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
493 * This telnetd implementation does not really
494 * support timing marks, it just uses them to
495 * support the kludge linemode stuff. If we
496 * receive a will or wont TM in response to our
497 * do TM request that may have been sent to
498 * determine kludge linemode support, process
499 * it, otherwise TM should get a negative
503 * Handle the linemode kludge stuff.
504 * If we are not currently supporting any
505 * linemode at all, then we assume that this
506 * is the client telling us to use kludge
507 * linemode in response to our query. Set the
508 * linemode type that is to be supported, note
509 * that the client wishes to use linemode, and
510 * eat the will TM as though it never arrived.
512 if (lmodetype
< KLUDGE_LINEMODE
) {
513 lmodetype
= KLUDGE_LINEMODE
;
514 clientstat(TELOPT_LINEMODE
, WILL
, 0);
515 send_wont(TELOPT_SGA
, 1);
516 } else if (lmodetype
== NO_AUTOKLUDGE
) {
517 lmodetype
= KLUDGE_OK
;
519 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
521 * We never respond to a WILL TM, and
522 * we leave the state WONT.
528 * If we are going to support flow control
529 * option, then don't worry peer that we can't
530 * change the flow control characters.
532 slctab
[SLC_XON
].defset
.flag
&= ~SLC_LEVELBITS
;
533 slctab
[SLC_XON
].defset
.flag
|= SLC_DEFAULT
;
534 slctab
[SLC_XOFF
].defset
.flag
&= ~SLC_LEVELBITS
;
535 slctab
[SLC_XOFF
].defset
.flag
|= SLC_DEFAULT
;
540 case TELOPT_XDISPLOC
:
541 case TELOPT_NEW_ENVIRON
:
542 case TELOPT_OLD_ENVIRON
:
547 case TELOPT_LINEMODE
:
548 # ifdef KLUDGELINEMODE
550 * Note client's desire to use linemode.
552 lmodetype
= REAL_LINEMODE
;
553 # endif /* KLUDGELINEMODE */
557 #endif /* LINEMODE */
559 #ifdef AUTHENTICATION
560 case TELOPT_AUTHENTICATION
:
568 func
= encrypt_send_support
;
571 #endif /* ENCRYPTION */
577 set_his_want_state_will(option
);
580 do_dont_resp
[option
]++;
581 send_dont(option
, 0);
585 * Option processing that should happen when
586 * we receive conformation of a change in
587 * state that we had requested.
591 not42
= 0; /* looks like a 4.2 system */
593 * Egads, he responded "WILL ECHO". Turn
596 send_dont(option
, 1);
598 * "WILL ECHO". Kludge upon kludge!
599 * A 4.2 client is now echoing user input at
600 * the tty. This is probably undesireable and
601 * it should be stopped. The client will
602 * respond WONT TM to the DO TM that we send to
603 * check for kludge linemode. When the WONT TM
604 * arrives, linemode will be turned off and a
605 * change propogated to the pty. This change
606 * will cause us to process the new pty state
607 * in localstat(), which will notice that
608 * linemode is off and send a WILL ECHO
609 * so that we are properly in character mode and
614 case TELOPT_LINEMODE
:
615 # ifdef KLUDGELINEMODE
617 * Note client's desire to use linemode.
619 lmodetype
= REAL_LINEMODE
;
620 # endif /* KLUDGELINEMODE */
623 #endif /* LINEMODE */
625 #ifdef AUTHENTICATION
626 case TELOPT_AUTHENTICATION
:
633 func
= encrypt_send_support
;
635 #endif /* ENCRYPTION */
643 set_his_state_will(option
);
646 } /* end of willoption */
649 send_dont(int option
, int init
)
652 if ((do_dont_resp
[option
] == 0 && his_state_is_wont(option
)) ||
653 his_want_state_is_wont(option
))
655 set_his_want_state_wont(option
);
656 do_dont_resp
[option
]++;
658 (void) output_data("%c%c%c", IAC
, DONT
, option
);
660 DIAG(TD_OPTIONS
, printoption("td: send dont", option
));
664 wontoption(int option
)
667 * Process client input.
670 DIAG(TD_OPTIONS
, printoption("td: recv wont", option
));
672 if (do_dont_resp
[option
]) {
673 do_dont_resp
[option
]--;
674 if (do_dont_resp
[option
] && his_state_is_wont(option
))
675 do_dont_resp
[option
]--;
677 if (do_dont_resp
[option
] == 0) {
678 if (his_want_state_is_will(option
)) {
679 /* it is always ok to change to negative state */
682 not42
= 1; /* doesn't seem to be a 4.2 system */
692 case TELOPT_LINEMODE
:
693 # ifdef KLUDGELINEMODE
695 * If real linemode is supported, then client is
696 * asking to turn linemode off.
698 if (lmodetype
!= REAL_LINEMODE
)
700 /* XXX double-check this --thorpej */
701 lmodetype
= KLUDGE_LINEMODE
;
702 # endif /* KLUDGELINEMODE */
703 clientstat(TELOPT_LINEMODE
, WONT
, 0);
705 #endif /* LINEMODE */
709 * If we get a WONT TM, and had sent a DO TM,
710 * don't respond with a DONT TM, just leave it
711 * as is. Short circut the state machine to
714 set_his_want_state_wont(TELOPT_TM
);
719 * If we are not going to support flow control
720 * option, then let peer know that we can't
721 * change the flow control characters.
723 slctab
[SLC_XON
].defset
.flag
&= ~SLC_LEVELBITS
;
724 slctab
[SLC_XON
].defset
.flag
|= SLC_CANTCHANGE
;
725 slctab
[SLC_XOFF
].defset
.flag
&= ~SLC_LEVELBITS
;
726 slctab
[SLC_XOFF
].defset
.flag
|= SLC_CANTCHANGE
;
729 #ifdef AUTHENTICATION
730 case TELOPT_AUTHENTICATION
:
731 auth_finished(0, AUTH_REJECT
);
736 * For options that we might spin waiting for
737 * sub-negotiation, if the client turns off the
738 * option rather than responding to the request,
739 * we have to treat it here as if we got a response
740 * to the sub-negotiation, (by updating the timers)
741 * so that we'll break out of the loop.
744 settimer(ttypesubopt
);
748 settimer(tspeedsubopt
);
751 case TELOPT_XDISPLOC
:
752 settimer(xdisplocsubopt
);
755 case TELOPT_OLD_ENVIRON
:
756 settimer(oenvironsubopt
);
759 case TELOPT_NEW_ENVIRON
:
760 settimer(environsubopt
);
766 set_his_want_state_wont(option
);
767 if (his_state_is_will(option
))
768 send_dont(option
, 0);
772 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
773 if (lmodetype
< NO_AUTOKLUDGE
) {
774 lmodetype
= NO_LINEMODE
;
775 clientstat(TELOPT_LINEMODE
, WONT
, 0);
776 send_will(TELOPT_SGA
, 1);
777 send_will(TELOPT_ECHO
, 1);
779 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
782 #ifdef AUTHENTICATION
783 case TELOPT_AUTHENTICATION
:
784 auth_finished(0, AUTH_REJECT
);
792 set_his_state_wont(option
);
794 } /* end of wontoption */
797 send_will(int option
, int init
)
800 if ((will_wont_resp
[option
] == 0 && my_state_is_will(option
))||
801 my_want_state_is_will(option
))
803 set_my_want_state_will(option
);
804 will_wont_resp
[option
]++;
806 (void) output_data("%c%c%c", IAC
, WILL
, option
);
808 DIAG(TD_OPTIONS
, printoption("td: send will", option
));
811 #if !defined(LINEMODE) || !defined(KLUDGELINEMODE)
813 * When we get a DONT SGA, we will try once to turn it
814 * back on. If the other side responds DONT SGA, we
815 * leave it at that. This is so that when we talk to
816 * clients that understand KLUDGELINEMODE but not LINEMODE,
817 * we'll keep them in char-at-a-time mode.
828 * Process client input.
831 DIAG(TD_OPTIONS
, printoption("td: recv do", option
));
833 if (will_wont_resp
[option
]) {
834 will_wont_resp
[option
]--;
835 if (will_wont_resp
[option
] && my_state_is_will(option
))
836 will_wont_resp
[option
]--;
838 if ((will_wont_resp
[option
] == 0) && (my_want_state_is_wont(option
))) {
842 # ifdef KLUDGELINEMODE
843 if (lmodetype
== NO_LINEMODE
)
845 if (his_state_is_wont(TELOPT_LINEMODE
))
864 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
866 * If kludge linemode is in use, then we must
867 * process an incoming do SGA for linemode
870 if (lmodetype
== KLUDGE_LINEMODE
) {
872 * Receipt of "do SGA" in kludge
873 * linemode is the peer asking us to
874 * turn off linemode. Make note of
877 clientstat(TELOPT_LINEMODE
, WONT
, 0);
879 * If linemode did not get turned off
880 * then don't tell peer that we did.
881 * Breaking here forces a wont SGA to
889 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
899 * Special case for TM. We send a WILL, but
900 * pretend we sent a WONT.
902 send_will(option
, 0);
903 set_my_want_state_wont(option
);
904 set_my_state_wont(option
);
909 * When we get a LOGOUT option, respond
910 * with a WILL LOGOUT, make sure that
911 * it gets written out to the network,
912 * and then just go away...
914 set_my_want_state_will(TELOPT_LOGOUT
);
915 send_will(TELOPT_LOGOUT
, 0);
916 set_my_state_will(TELOPT_LOGOUT
);
926 #endif /* ENCRYPTION */
928 case TELOPT_LINEMODE
:
933 case TELOPT_XDISPLOC
:
934 case TELOPT_OLD_ENVIRON
:
939 set_my_want_state_will(option
);
940 send_will(option
, 0);
942 will_wont_resp
[option
]++;
943 send_wont(option
, 0);
946 set_my_state_will(option
);
948 } /* end of dooption */
951 send_wont(int option
, int init
)
954 if ((will_wont_resp
[option
] == 0 && my_state_is_wont(option
)) ||
955 my_want_state_is_wont(option
))
957 set_my_want_state_wont(option
);
958 will_wont_resp
[option
]++;
960 (void) output_data("%c%c%c", IAC
, WONT
, option
);
962 DIAG(TD_OPTIONS
, printoption("td: send wont", option
));
966 dontoption(int option
)
969 * Process client input.
973 DIAG(TD_OPTIONS
, printoption("td: recv dont", option
));
975 if (will_wont_resp
[option
]) {
976 will_wont_resp
[option
]--;
977 if (will_wont_resp
[option
] && my_state_is_wont(option
))
978 will_wont_resp
[option
]--;
980 if ((will_wont_resp
[option
] == 0) && (my_want_state_is_will(option
))) {
988 case TELOPT_ECHO
: /* we should stop echoing */
990 # ifdef KLUDGELINEMODE
991 if ((lmodetype
!= REAL_LINEMODE
) &&
992 (lmodetype
!= KLUDGE_LINEMODE
))
994 if (his_state_is_wont(TELOPT_LINEMODE
))
1005 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
1007 * If kludge linemode is in use, then we
1008 * must process an incoming do SGA for
1009 * linemode purposes.
1011 if ((lmodetype
== KLUDGE_LINEMODE
) ||
1012 (lmodetype
== KLUDGE_OK
)) {
1014 * The client is asking us to turn
1017 lmodetype
= KLUDGE_LINEMODE
;
1018 clientstat(TELOPT_LINEMODE
, WILL
, 0);
1020 * If we did not turn line mode on,
1021 * then what do we say? Will SGA?
1022 * This violates design of telnet.
1023 * Gross. Very Gross.
1028 set_my_want_state_wont(option
);
1029 if (my_state_is_will(option
))
1030 send_wont(option
, 0);
1031 set_my_state_wont(option
);
1032 if (turn_on_sga
^= 1)
1033 send_will(option
, 1);
1035 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
1041 set_my_want_state_wont(option
);
1042 if (my_state_is_will(option
))
1043 send_wont(option
, 0);
1045 set_my_state_wont(option
);
1047 } /* end of dontoption */
1051 int env_ovalue
= -1;
1052 #else /* ENV_HACK */
1053 # define env_ovar OLD_ENV_VAR
1054 # define env_ovalue OLD_ENV_VALUE
1055 #endif /* ENV_HACK */
1057 /* envvarok(char*) */
1058 /* check that variable is safe to pass to login or shell */
1060 envvarok(char *varp
)
1063 if (strcmp(varp
, "TERMCAP") && /* to prevent a security hole */
1064 strcmp(varp
, "TERMINFO") && /* with tgetent */
1065 strcmp(varp
, "TERMPATH") &&
1066 strcmp(varp
, "HOME") && /* to prevent the tegetent bug */
1067 strncmp(varp
, "LD_", strlen("LD_")) && /* most systems */
1068 strncmp(varp
, "_RLD_", strlen("_RLD_")) && /* IRIX */
1069 strcmp(varp
, "LIBPATH") && /* AIX */
1070 strcmp(varp
, "ENV") &&
1071 strcmp(varp
, "BASH_ENV") &&
1072 strcmp(varp
, "IFS") &&
1073 strncmp(varp
, "KRB5", strlen("KRB5")) && /* Krb5 */
1075 * The above case is a catch-all for now. Here are some of
1076 * the specific ones we must avoid passing, at least until
1077 * we can prove it can be done safely. Keep this list
1078 * around un case someone wants to remove the catch-all.
1080 strcmp(varp
, "KRB5_CONFIG") && /* Krb5 */
1081 strcmp(varp
, "KRB5CCNAME") && /* Krb5 */
1082 strcmp(varp
, "KRB5_KTNAME") && /* Krb5 */
1083 strcmp(varp
, "KRBTKFILE") && /* Krb4 */
1084 strcmp(varp
, "KRB_CONF") && /* CNS 4 */
1085 strcmp(varp
, "KRB_REALMS") && /* CNS 4 */
1086 strcmp(varp
, "RESOLV_HOST_CONF")) /* Linux */
1089 syslog(LOG_INFO
, "Rejected the attempt to modify the "
1090 "environment variable \"%s\"", varp
);
1098 * Look at the sub-option buffer, and try to be helpful to the other
1101 * Currently we recognize:
1113 DIAG(TD_OPTIONS
, {netflush(); printsub('<', subpointer
, SB_LEN()+2);});
1117 case TELOPT_TSPEED
: {
1120 if (his_state_is_wont(TELOPT_TSPEED
)) /* Ignore if option disabled */
1123 settimer(tspeedsubopt
);
1125 if (SB_EOF() || SB_GET() != TELQUAL_IS
)
1128 xspeed
= atoi((char *)subpointer
);
1130 while (SB_GET() != ',' && !SB_EOF());
1134 rspeed
= atoi((char *)subpointer
);
1135 clientstat(TELOPT_TSPEED
, xspeed
, rspeed
);
1139 } /* end of case TELOPT_TSPEED */
1141 case TELOPT_TTYPE
: { /* Yaaaay! */
1144 if (his_state_is_wont(TELOPT_TTYPE
)) /* Ignore if option disabled */
1146 settimer(ttypesubopt
);
1148 if (SB_EOF() || SB_GET() != TELQUAL_IS
) {
1149 return; /* ??? XXX but, this is the most robust */
1154 while ((p
< (terminaltype
+ sizeof terminaltype
-1)) &&
1162 *p
++ = c
; /* accumulate name */
1166 } /* end of case TELOPT_TTYPE */
1169 int xwinsize
, ywinsize
;
1171 if (his_state_is_wont(TELOPT_NAWS
)) /* Ignore if option disabled */
1176 xwinsize
= SB_GET() << 8;
1179 xwinsize
|= SB_GET();
1182 ywinsize
= SB_GET() << 8;
1185 ywinsize
|= SB_GET();
1186 clientstat(TELOPT_NAWS
, xwinsize
, ywinsize
);
1190 } /* end of case TELOPT_NAWS */
1193 case TELOPT_LINEMODE
: {
1196 if (his_state_is_wont(TELOPT_LINEMODE
)) /* Ignore if option disabled */
1199 * Process linemode suboptions.
1202 break; /* garbage was sent */
1203 request
= SB_GET(); /* get will/wont */
1206 break; /* another garbage check */
1208 if (request
== LM_SLC
) { /* SLC is not preceded by WILL or WONT */
1210 * Process suboption buffer of slc's
1213 do_opt_slc(subpointer
, SB_LEN());
1216 } else if (request
== LM_MODE
) {
1219 useeditmode
= SB_GET(); /* get mode flag */
1220 clientstat(LM_MODE
, 0, 0);
1226 switch (SB_GET()) { /* what suboption? */
1227 case LM_FORWARDMASK
:
1229 * According to spec, only server can send request for
1230 * forwardmask, and client can only return a positive response.
1231 * So don't worry about it.
1238 } /* end of case TELOPT_LINEMODE */
1240 case TELOPT_STATUS
: {
1248 if (my_state_is_will(TELOPT_STATUS
))
1259 } /* end of case TELOPT_STATUS */
1261 case TELOPT_XDISPLOC
: {
1262 if (SB_EOF() || SB_GET() != TELQUAL_IS
)
1264 settimer(xdisplocsubopt
);
1265 subpointer
[SB_LEN()] = '\0';
1266 (void)setenv("DISPLAY", (char *)subpointer
, 1);
1268 } /* end of case TELOPT_XDISPLOC */
1270 case TELOPT_NEW_ENVIRON
:
1271 case TELOPT_OLD_ENVIRON
: {
1273 char *cp
, *varp
, *valp
;
1278 if (c
== TELQUAL_IS
) {
1279 if (subchar
== TELOPT_OLD_ENVIRON
)
1280 settimer(oenvironsubopt
);
1282 settimer(environsubopt
);
1283 } else if (c
!= TELQUAL_INFO
) {
1287 if (subchar
== TELOPT_NEW_ENVIRON
) {
1290 if ((c
== NEW_ENV_VAR
) || (c
== ENV_USERVAR
))
1297 * We only want to do this if we haven't already decided
1298 * whether or not the other side has its VALUE and VAR
1302 int last
= -1; /* invalid value */
1304 int got_var
= 0, got_value
= 0, got_uservar
= 0;
1307 * The other side might have its VALUE and VAR values
1308 * reversed. To be interoperable, we need to determine
1309 * which way it is. If the first recognized character
1310 * is a VAR or VALUE, then that will tell us what
1311 * type of client it is. If the fist recognized
1312 * character is a USERVAR, then we continue scanning
1313 * the suboption looking for two consecutive
1314 * VAR or VALUE fields. We should not get two
1315 * consecutive VALUE fields, so finding two
1316 * consecutive VALUE or VAR fields will tell us
1317 * what the client is.
1324 if (last
< 0 || last
== OLD_ENV_VAR
1325 || (empty
&& (last
== OLD_ENV_VALUE
)))
1331 if (last
< 0 || last
== OLD_ENV_VALUE
1332 || (empty
&& (last
== OLD_ENV_VAR
)))
1333 goto env_ovar_wrong
;
1335 last
= OLD_ENV_VALUE
;
1338 /* count strings of USERVAR as one */
1339 if (last
!= ENV_USERVAR
)
1342 if (last
== OLD_ENV_VALUE
)
1344 if (last
== OLD_ENV_VAR
)
1345 goto env_ovar_wrong
;
1360 if (last
== OLD_ENV_VALUE
)
1362 if (last
== OLD_ENV_VAR
)
1363 goto env_ovar_wrong
;
1366 * Ok, the first thing was a USERVAR, and there
1367 * are not two consecutive VAR or VALUE commands,
1368 * and none of the VAR or VALUE commands are empty.
1369 * If the client has sent us a well-formed option,
1370 * then the number of VALUEs received should always
1371 * be less than or equal to the number of VARs and
1372 * USERVARs received.
1374 * If we got exactly as many VALUEs as VARs and
1375 * USERVARs, the client has the same definitions.
1377 * If we got exactly as many VARs as VALUEs and
1378 * USERVARS, the client has reversed definitions.
1380 if (got_uservar
+ got_var
== got_value
) {
1382 env_ovar
= OLD_ENV_VAR
;
1383 env_ovalue
= OLD_ENV_VALUE
;
1384 } else if (got_uservar
+ got_value
== got_var
) {
1386 env_ovar
= OLD_ENV_VALUE
;
1387 env_ovalue
= OLD_ENV_VAR
;
1388 DIAG(TD_OPTIONS
, {output_data(
1389 "ENVIRON VALUE and VAR are reversed!\r\n");});
1398 if ((c
== env_ovar
) || (c
== ENV_USERVAR
))
1406 cp
= varp
= (char *)subpointer
;
1411 if (subchar
== TELOPT_OLD_ENVIRON
) {
1414 else if (c
== env_ovalue
)
1421 cp
= valp
= (char *)subpointer
;
1427 if (envvarok(varp
)) {
1429 (void)setenv(varp
, valp
, 1);
1433 cp
= varp
= (char *)subpointer
;
1448 if (envvarok(varp
)) {
1450 (void)setenv(varp
, valp
, 1);
1455 } /* end of case TELOPT_NEW_ENVIRON */
1456 #ifdef AUTHENTICATION
1457 case TELOPT_AUTHENTICATION
:
1464 * These are sent by us and cannot be sent by
1469 auth_is(subpointer
, SB_LEN());
1472 auth_name(subpointer
, SB_LEN());
1478 case TELOPT_ENCRYPT
:
1482 case ENCRYPT_SUPPORT
:
1483 encrypt_support(subpointer
, SB_LEN());
1486 encrypt_is(subpointer
, SB_LEN());
1489 encrypt_reply(subpointer
, SB_LEN());
1492 encrypt_start(subpointer
, SB_LEN());
1497 case ENCRYPT_REQSTART
:
1498 encrypt_request_start(subpointer
, SB_LEN());
1500 case ENCRYPT_REQEND
:
1502 * We can always send an REQEND so that we cannot
1503 * get stuck encrypting. We should only get this
1504 * if we have been able to get in the correct mode
1507 encrypt_request_end();
1509 case ENCRYPT_ENC_KEYID
:
1510 encrypt_enc_keyid(subpointer
, SB_LEN());
1512 case ENCRYPT_DEC_KEYID
:
1513 encrypt_dec_keyid(subpointer
, SB_LEN());
1519 #endif /* ENCRYPTION */
1523 } /* end of switch */
1525 } /* end of suboption */
1531 clientstat(TELOPT_LINEMODE
, WILL
, 0);
1533 #endif /* LINEMODE */
1545 #define ADD_DATA(c) \
1547 ADD(c); if (c == SE || c == IAC) ADD(c); \
1550 unsigned char statusbuf
[256];
1556 ep
= statusbuf
+ sizeof(statusbuf
);
1558 netflush(); /* get rid of anything waiting to go out */
1566 * We check the want_state rather than the current state,
1567 * because if we received a DO/WILL for an option that we
1568 * don't support, and the other side didn't send a DONT/WONT
1569 * in response to our WONT/DONT, then the "state" will be
1570 * WILL/DO, and the "want_state" will be WONT/DONT. We
1571 * need to go by the latter.
1573 for (i
= 0; i
< (unsigned char)NTELOPTS
; i
++) {
1574 if (my_want_state_is_will(i
)) {
1578 if (his_want_state_is_will(i
)) {
1584 if (his_want_state_is_will(TELOPT_LFLOW
)) {
1594 if (restartany
>= 0) {
1598 ADD(LFLOW_RESTART_ANY
);
1600 ADD(LFLOW_RESTART_XON
);
1607 if (his_want_state_is_will(TELOPT_LINEMODE
)) {
1608 unsigned char *cp
, *cpe
;
1612 ADD(TELOPT_LINEMODE
);
1618 ADD(TELOPT_LINEMODE
);
1623 for (cpe
= cp
+ len
; cp
< cpe
; cp
++)
1627 #endif /* LINEMODE */
1632 writenet(statusbuf
, ncp
- statusbuf
);
1633 netflush(); /* Send it on its way */
1636 {printsub('>', statusbuf
, ncp
- statusbuf
); netflush();});
1647 output_data(const char *format
, ...)
1650 size_t remaining
, ret
;
1652 va_start(args
, format
);
1653 remaining
= BUFSIZ
- (nfrontp
- netobuf
);
1654 /* try a netflush() if the room is too low */
1655 if (strlen(format
) > remaining
|| BUFSIZ
/ 4 > remaining
) {
1657 remaining
= BUFSIZ
- (nfrontp
- netobuf
);
1659 ret
= vsnprintf(nfrontp
, remaining
, format
, args
);
1660 nfrontp
+= ((ret
< remaining
- 1) ? ret
: remaining
- 1);
1666 output_datalen(const char *buf
, size_t l
)
1670 remaining
= BUFSIZ
- (nfrontp
- netobuf
);
1671 if (remaining
< l
) {
1673 remaining
= BUFSIZ
- (nfrontp
- netobuf
);
1677 memmove(nfrontp
, buf
, l
);