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
36 static const char sccsid
[] = "@(#)state.c 8.5 (Berkeley) 5/30/95";
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD: src/contrib/telnet/telnetd/state.c,v 1.14 2003/05/04 02:54:49 obrien Exp $");
45 #include <libtelnet/auth.h>
48 #include <libtelnet/encrypt.h>
51 unsigned char doopt
[] = { IAC
, DO
, '%', 'c', 0 };
52 unsigned char dont
[] = { IAC
, DONT
, '%', 'c', 0 };
53 unsigned char will
[] = { IAC
, WILL
, '%', 'c', 0 };
54 unsigned char wont
[] = { IAC
, WONT
, '%', 'c', 0 };
58 * Buffer for sub-options, and macros
59 * for suboptions buffer manipulations
61 unsigned char subbuffer
[512], *subpointer
= subbuffer
, *subend
= subbuffer
;
63 #define SB_CLEAR() subpointer = subbuffer
64 #define SB_TERM() { subend = subpointer; SB_CLEAR(); }
65 #define SB_ACCUM(c) if (subpointer < (subbuffer+sizeof subbuffer)) { \
66 *subpointer++ = (c); \
68 #define SB_GET() ((*subpointer++)&0xff)
69 #define SB_EOF() (subpointer >= subend)
70 #define SB_LEN() (subend - subpointer)
73 unsigned char *subsave
;
74 #define SB_SAVE() subsave = subpointer;
75 #define SB_RESTORE() subpointer = subsave;
82 #define TS_DATA 0 /* base state */
83 #define TS_IAC 1 /* look for double IAC's */
84 #define TS_CR 2 /* CR-LF ->'s CR */
85 #define TS_SB 3 /* throw away begin's... */
86 #define TS_SE 4 /* ...end's (suboption negotiation) */
87 #define TS_WILL 5 /* will option negotiation */
88 #define TS_WONT 6 /* wont " */
89 #define TS_DO 7 /* do " */
90 #define TS_DONT 8 /* dont " */
92 static void doclientstat(void);
98 static int state
= TS_DATA
;
101 if ((&ptyobuf
[BUFSIZ
] - pfrontp
) < 2)
103 c
= *netip
++ & 0377, ncc
--;
106 c
= (*decrypt_input
)(c
);
107 #endif /* ENCRYPTION */
112 /* Strip off \n or \0 after a \r */
113 if ((c
== 0) || (c
== '\n')) {
124 * We now map \r\n ==> \r for pragmatic reasons.
125 * Many client implementations send \r\n when
126 * the user hits the CarriageReturn key.
128 * We USED to map \r\n ==> \n, since \r\n says
129 * that we want to be in column 1 of the next
130 * printable line, and \n is the standard
131 * unix way of saying that (\r is only good
132 * if CRMOD is set, which it normally is).
134 if ((c
== '\r') && his_state_is_wont(TELOPT_BINARY
)) {
138 nc
= (*decrypt_input
)(nc
& 0xff);
139 #endif /* ENCRYPTION */
142 * If we are operating in linemode,
143 * convert to local end-of-line.
145 if (linemode
&& (ncc
> 0) && (('\n' == nc
) ||
146 ((0 == nc
) && tty_iscrnl())) ) {
154 (void)(*decrypt_input
)(-1);
155 #endif /* ENCRYPTION */
166 * Send the process on the pty side an
167 * interrupt. Do this with a NULL or
168 * interrupt char; depending on the tty mode.
172 printoption("td: recv IAC", c
));
178 printoption("td: recv IAC", c
));
187 printoption("td: recv IAC", c
));
197 printoption("td: recv IAC", c
));
198 ptyflush(); /* half-hearted */
201 if (slctab
[SLC_AO
].sptr
&&
202 *slctab
[SLC_AO
].sptr
!= (cc_t
)(_POSIX_VDISABLE
)) {
204 (unsigned char)*slctab
[SLC_AO
].sptr
;
207 netclear(); /* clear buffer back */
208 output_data("%c%c", IAC
, DM
);
209 neturg
= nfrontp
-1; /* off by one XXX */
211 printoption("td: send IAC", DM
));
216 * Erase Character and
225 printoption("td: recv IAC", c
));
226 ptyflush(); /* half-hearted */
229 ch
= *slctab
[SLC_EC
].sptr
;
231 ch
= *slctab
[SLC_EL
].sptr
;
232 if (ch
!= (cc_t
)(_POSIX_VDISABLE
))
233 *pfrontp
++ = (unsigned char)ch
;
238 * Check for urgent data...
242 printoption("td: recv IAC", c
));
243 SYNCHing
= stilloob(net
);
249 * Begin option subnegotiation...
272 if (his_state_is_will(TELOPT_EOR
))
277 * Handle RFC 10xx Telnet linemode option additions
278 * to command stream (EOF, SUSP, ABORT).
311 * bad form of suboption negotiation.
312 * handle it in such a way as to avoid
313 * damage to local state. Parse
314 * suboption buffer found so far,
315 * then treat remaining stream as
316 * another command sequence.
319 /* for DIAGNOSTICS */
332 /* for DIAGNOSTICS */
338 suboption(); /* handle sub-option */
364 syslog(LOG_ERR
, "panic state=%d", state
);
365 printf("telnetd: panic state=%d\n", state
);
369 } /* end of telrcv */
372 * The will/wont/do/dont state machines are based on Dave Borman's
373 * Telnet option processing state machine.
375 * These correspond to the following states:
376 * my_state = the last negotiated state
377 * want_state = what I want the state to go to
378 * want_resp = how many requests I have sent
379 * All state defaults are negative, and resp defaults to 0.
381 * When initiating a request to change state to new_state:
383 * if ((want_resp == 0 && new_state == my_state) || want_state == new_state) {
386 * want_state = new_state;
391 * When receiving new_state:
395 * if (want_resp && (new_state == my_state))
398 * if ((want_resp == 0) && (new_state != want_state)) {
399 * if (ok_to_switch_to new_state)
400 * want_state = new_state;
405 * my_state = new_state;
407 * Note that new_state is implied in these functions by the function itself.
408 * will and do imply positive new_state, wont and dont imply negative.
410 * Finally, there is one catch. If we send a negative response to a
411 * positive request, my_state will be the positive while want_state will
412 * remain negative. my_state will revert to negative when the negative
413 * acknowlegment arrives from the peer. Thus, my_state generally tells
414 * us not only the last negotiated state, but also tells us what the peer
415 * wants to be doing as well. It is important to understand this difference
416 * as we may wish to be processing data streams based on our desired state
417 * (want_state) or based on what the peer thinks the state is (my_state).
419 * This all works fine because if the peer sends a positive request, the data
420 * that we receive prior to negative acknowlegment will probably be affected
421 * by the positive state, and we can process it as such (if we can; if we
422 * can't then it really doesn't matter). If it is that important, then the
423 * peer probably should be buffering until this option state negotiation
428 send_do(int option
, int init
)
431 if ((do_dont_resp
[option
] == 0 && his_state_is_will(option
)) ||
432 his_want_state_is_will(option
))
435 * Special case for TELOPT_TM: We send a DO, but pretend
436 * that we sent a DONT, so that we can send more DOs if
439 if (option
== TELOPT_TM
)
440 set_his_want_state_wont(option
);
442 set_his_want_state_will(option
);
443 do_dont_resp
[option
]++;
445 output_data((const char *)doopt
, option
);
447 DIAG(TD_OPTIONS
, printoption("td: send do", option
));
451 willoption(int option
)
454 void (*func
)(void) = 0;
457 * process input from peer.
460 DIAG(TD_OPTIONS
, printoption("td: recv will", option
));
462 if (do_dont_resp
[option
]) {
463 do_dont_resp
[option
]--;
464 if (do_dont_resp
[option
] && his_state_is_will(option
))
465 do_dont_resp
[option
]--;
467 if (do_dont_resp
[option
] == 0) {
468 if (his_want_state_is_wont(option
)) {
480 * See comments below for more info.
482 not42
= 0; /* looks like a 4.2 system */
486 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
488 * This telnetd implementation does not really
489 * support timing marks, it just uses them to
490 * support the kludge linemode stuff. If we
491 * receive a will or wont TM in response to our
492 * do TM request that may have been sent to
493 * determine kludge linemode support, process
494 * it, otherwise TM should get a negative
498 * Handle the linemode kludge stuff.
499 * If we are not currently supporting any
500 * linemode at all, then we assume that this
501 * is the client telling us to use kludge
502 * linemode in response to our query. Set the
503 * linemode type that is to be supported, note
504 * that the client wishes to use linemode, and
505 * eat the will TM as though it never arrived.
507 if (lmodetype
< KLUDGE_LINEMODE
) {
508 lmodetype
= KLUDGE_LINEMODE
;
509 clientstat(TELOPT_LINEMODE
, WILL
, 0);
510 send_wont(TELOPT_SGA
, 1);
511 } else if (lmodetype
== NO_AUTOKLUDGE
) {
512 lmodetype
= KLUDGE_OK
;
514 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
516 * We never respond to a WILL TM, and
517 * we leave the state WONT.
523 * If we are going to support flow control
524 * option, then don't worry peer that we can't
525 * change the flow control characters.
527 slctab
[SLC_XON
].defset
.flag
&= ~SLC_LEVELBITS
;
528 slctab
[SLC_XON
].defset
.flag
|= SLC_DEFAULT
;
529 slctab
[SLC_XOFF
].defset
.flag
&= ~SLC_LEVELBITS
;
530 slctab
[SLC_XOFF
].defset
.flag
|= SLC_DEFAULT
;
535 case TELOPT_XDISPLOC
:
536 case TELOPT_NEW_ENVIRON
:
537 case TELOPT_OLD_ENVIRON
:
542 case TELOPT_LINEMODE
:
543 # ifdef KLUDGELINEMODE
545 * Note client's desire to use linemode.
547 lmodetype
= REAL_LINEMODE
;
548 # endif /* KLUDGELINEMODE */
552 #endif /* LINEMODE */
554 #ifdef AUTHENTICATION
555 case TELOPT_AUTHENTICATION
:
563 func
= encrypt_send_support
;
566 #endif /* ENCRYPTION */
572 set_his_want_state_will(option
);
575 do_dont_resp
[option
]++;
576 send_dont(option
, 0);
580 * Option processing that should happen when
581 * we receive conformation of a change in
582 * state that we had requested.
586 not42
= 0; /* looks like a 4.2 system */
588 * Egads, he responded "WILL ECHO". Turn
591 send_dont(option
, 1);
593 * "WILL ECHO". Kludge upon kludge!
594 * A 4.2 client is now echoing user input at
595 * the tty. This is probably undesireable and
596 * it should be stopped. The client will
597 * respond WONT TM to the DO TM that we send to
598 * check for kludge linemode. When the WONT TM
599 * arrives, linemode will be turned off and a
600 * change propogated to the pty. This change
601 * will cause us to process the new pty state
602 * in localstat(), which will notice that
603 * linemode is off and send a WILL ECHO
604 * so that we are properly in character mode and
609 case TELOPT_LINEMODE
:
610 # ifdef KLUDGELINEMODE
612 * Note client's desire to use linemode.
614 lmodetype
= REAL_LINEMODE
;
615 # endif /* KLUDGELINEMODE */
618 #endif /* LINEMODE */
620 #ifdef AUTHENTICATION
621 case TELOPT_AUTHENTICATION
:
628 func
= encrypt_send_support
;
630 #endif /* ENCRYPTION */
637 set_his_state_will(option
);
640 } /* end of willoption */
643 send_dont(int option
, int init
)
646 if ((do_dont_resp
[option
] == 0 && his_state_is_wont(option
)) ||
647 his_want_state_is_wont(option
))
649 set_his_want_state_wont(option
);
650 do_dont_resp
[option
]++;
652 output_data((const char *)dont
, option
);
654 DIAG(TD_OPTIONS
, printoption("td: send dont", option
));
658 wontoption(int option
)
661 * Process client input.
664 DIAG(TD_OPTIONS
, printoption("td: recv wont", option
));
666 if (do_dont_resp
[option
]) {
667 do_dont_resp
[option
]--;
668 if (do_dont_resp
[option
] && his_state_is_wont(option
))
669 do_dont_resp
[option
]--;
671 if (do_dont_resp
[option
] == 0) {
672 if (his_want_state_is_will(option
)) {
673 /* it is always ok to change to negative state */
676 not42
= 1; /* doesn't seem to be a 4.2 system */
686 case TELOPT_LINEMODE
:
687 # ifdef KLUDGELINEMODE
689 * If real linemode is supported, then client is
690 * asking to turn linemode off.
692 if (lmodetype
!= REAL_LINEMODE
)
694 lmodetype
= KLUDGE_LINEMODE
;
695 # endif /* KLUDGELINEMODE */
696 clientstat(TELOPT_LINEMODE
, WONT
, 0);
698 #endif /* LINEMODE */
702 * If we get a WONT TM, and had sent a DO TM,
703 * don't respond with a DONT TM, just leave it
704 * as is. Short circut the state machine to
707 set_his_want_state_wont(TELOPT_TM
);
712 * If we are not going to support flow control
713 * option, then let peer know that we can't
714 * change the flow control characters.
716 slctab
[SLC_XON
].defset
.flag
&= ~SLC_LEVELBITS
;
717 slctab
[SLC_XON
].defset
.flag
|= SLC_CANTCHANGE
;
718 slctab
[SLC_XOFF
].defset
.flag
&= ~SLC_LEVELBITS
;
719 slctab
[SLC_XOFF
].defset
.flag
|= SLC_CANTCHANGE
;
722 #ifdef AUTHENTICATION
723 case TELOPT_AUTHENTICATION
:
724 auth_finished(0, AUTH_REJECT
);
729 * For options that we might spin waiting for
730 * sub-negotiation, if the client turns off the
731 * option rather than responding to the request,
732 * we have to treat it here as if we got a response
733 * to the sub-negotiation, (by updating the timers)
734 * so that we'll break out of the loop.
737 settimer(ttypesubopt
);
741 settimer(tspeedsubopt
);
744 case TELOPT_XDISPLOC
:
745 settimer(xdisplocsubopt
);
748 case TELOPT_OLD_ENVIRON
:
749 settimer(oenvironsubopt
);
752 case TELOPT_NEW_ENVIRON
:
753 settimer(environsubopt
);
759 set_his_want_state_wont(option
);
760 if (his_state_is_will(option
))
761 send_dont(option
, 0);
765 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
766 if (lmodetype
< NO_AUTOKLUDGE
) {
767 lmodetype
= NO_LINEMODE
;
768 clientstat(TELOPT_LINEMODE
, WONT
, 0);
769 send_will(TELOPT_SGA
, 1);
770 send_will(TELOPT_ECHO
, 1);
772 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
775 #ifdef AUTHENTICATION
776 case TELOPT_AUTHENTICATION
:
777 auth_finished(0, AUTH_REJECT
);
785 set_his_state_wont(option
);
787 } /* end of wontoption */
790 send_will(int option
, int init
)
793 if ((will_wont_resp
[option
] == 0 && my_state_is_will(option
))||
794 my_want_state_is_will(option
))
796 set_my_want_state_will(option
);
797 will_wont_resp
[option
]++;
799 output_data((const char *)will
, option
);
801 DIAG(TD_OPTIONS
, printoption("td: send will", option
));
804 #if !defined(LINEMODE) || !defined(KLUDGELINEMODE)
806 * When we get a DONT SGA, we will try once to turn it
807 * back on. If the other side responds DONT SGA, we
808 * leave it at that. This is so that when we talk to
809 * clients that understand KLUDGELINEMODE but not LINEMODE,
810 * we'll keep them in char-at-a-time mode.
821 * Process client input.
824 DIAG(TD_OPTIONS
, printoption("td: recv do", option
));
826 if (will_wont_resp
[option
]) {
827 will_wont_resp
[option
]--;
828 if (will_wont_resp
[option
] && my_state_is_will(option
))
829 will_wont_resp
[option
]--;
831 if ((will_wont_resp
[option
] == 0) && (my_want_state_is_wont(option
))) {
835 # ifdef KLUDGELINEMODE
836 if (lmodetype
== NO_LINEMODE
)
838 if (his_state_is_wont(TELOPT_LINEMODE
))
857 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
859 * If kludge linemode is in use, then we must
860 * process an incoming do SGA for linemode
863 if (lmodetype
== KLUDGE_LINEMODE
) {
865 * Receipt of "do SGA" in kludge
866 * linemode is the peer asking us to
867 * turn off linemode. Make note of
870 clientstat(TELOPT_LINEMODE
, WONT
, 0);
872 * If linemode did not get turned off
873 * then don't tell peer that we did.
874 * Breaking here forces a wont SGA to
882 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
892 * Special case for TM. We send a WILL, but
893 * pretend we sent a WONT.
895 send_will(option
, 0);
896 set_my_want_state_wont(option
);
897 set_my_state_wont(option
);
902 * When we get a LOGOUT option, respond
903 * with a WILL LOGOUT, make sure that
904 * it gets written out to the network,
905 * and then just go away...
907 set_my_want_state_will(TELOPT_LOGOUT
);
908 send_will(TELOPT_LOGOUT
, 0);
909 set_my_state_will(TELOPT_LOGOUT
);
919 #endif /* ENCRYPTION */
920 case TELOPT_LINEMODE
:
925 case TELOPT_XDISPLOC
:
926 #ifdef TELOPT_ENVIRON
927 case TELOPT_NEW_ENVIRON
:
929 case TELOPT_OLD_ENVIRON
:
934 set_my_want_state_will(option
);
935 send_will(option
, 0);
937 will_wont_resp
[option
]++;
938 send_wont(option
, 0);
941 set_my_state_will(option
);
943 } /* end of dooption */
946 send_wont(int option
, int init
)
949 if ((will_wont_resp
[option
] == 0 && my_state_is_wont(option
)) ||
950 my_want_state_is_wont(option
))
952 set_my_want_state_wont(option
);
953 will_wont_resp
[option
]++;
955 output_data((const char *)wont
, option
);
957 DIAG(TD_OPTIONS
, printoption("td: send wont", option
));
961 dontoption(int option
)
964 * Process client input.
968 DIAG(TD_OPTIONS
, printoption("td: recv dont", option
));
970 if (will_wont_resp
[option
]) {
971 will_wont_resp
[option
]--;
972 if (will_wont_resp
[option
] && my_state_is_wont(option
))
973 will_wont_resp
[option
]--;
975 if ((will_wont_resp
[option
] == 0) && (my_want_state_is_will(option
))) {
983 case TELOPT_ECHO
: /* we should stop echoing */
985 # ifdef KLUDGELINEMODE
986 if ((lmodetype
!= REAL_LINEMODE
) &&
987 (lmodetype
!= KLUDGE_LINEMODE
))
989 if (his_state_is_wont(TELOPT_LINEMODE
))
1000 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
1002 * If kludge linemode is in use, then we
1003 * must process an incoming do SGA for
1004 * linemode purposes.
1006 if ((lmodetype
== KLUDGE_LINEMODE
) ||
1007 (lmodetype
== KLUDGE_OK
)) {
1009 * The client is asking us to turn
1012 lmodetype
= KLUDGE_LINEMODE
;
1013 clientstat(TELOPT_LINEMODE
, WILL
, 0);
1015 * If we did not turn line mode on,
1016 * then what do we say? Will SGA?
1017 * This violates design of telnet.
1018 * Gross. Very Gross.
1023 set_my_want_state_wont(option
);
1024 if (my_state_is_will(option
))
1025 send_wont(option
, 0);
1026 set_my_state_wont(option
);
1027 if (turn_on_sga
^= 1)
1028 send_will(option
, 1);
1030 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
1036 set_my_want_state_wont(option
);
1037 if (my_state_is_will(option
))
1038 send_wont(option
, 0);
1040 set_my_state_wont(option
);
1042 } /* end of dontoption */
1046 int env_ovalue
= -1;
1047 #else /* ENV_HACK */
1048 # define env_ovar OLD_ENV_VAR
1049 # define env_ovalue OLD_ENV_VALUE
1050 #endif /* ENV_HACK */
1055 * Look at the sub-option buffer, and try to be helpful to the other
1058 * Currently we recognize:
1070 DIAG(TD_OPTIONS
, {netflush(); printsub('<', subpointer
, SB_LEN()+2);});
1074 case TELOPT_TSPEED
: {
1077 if (his_state_is_wont(TELOPT_TSPEED
)) /* Ignore if option disabled */
1080 settimer(tspeedsubopt
);
1082 if (SB_EOF() || SB_GET() != TELQUAL_IS
)
1085 xspeed
= atoi((char *)subpointer
);
1087 while (SB_GET() != ',' && !SB_EOF());
1091 rspeed
= atoi((char *)subpointer
);
1092 clientstat(TELOPT_TSPEED
, xspeed
, rspeed
);
1096 } /* end of case TELOPT_TSPEED */
1098 case TELOPT_TTYPE
: { /* Yaaaay! */
1099 static char terminalname
[41];
1101 if (his_state_is_wont(TELOPT_TTYPE
)) /* Ignore if option disabled */
1103 settimer(ttypesubopt
);
1105 if (SB_EOF() || SB_GET() != TELQUAL_IS
) {
1106 return; /* ??? XXX but, this is the most robust */
1109 terminaltype
= terminalname
;
1111 while ((terminaltype
< (terminalname
+ sizeof terminalname
-1)) &&
1119 *terminaltype
++ = c
; /* accumulate name */
1122 terminaltype
= terminalname
;
1124 } /* end of case TELOPT_TTYPE */
1127 int xwinsize
, ywinsize
;
1129 if (his_state_is_wont(TELOPT_NAWS
)) /* Ignore if option disabled */
1134 xwinsize
= SB_GET() << 8;
1137 xwinsize
|= SB_GET();
1140 ywinsize
= SB_GET() << 8;
1143 ywinsize
|= SB_GET();
1144 clientstat(TELOPT_NAWS
, xwinsize
, ywinsize
);
1148 } /* end of case TELOPT_NAWS */
1151 case TELOPT_LINEMODE
: {
1154 if (his_state_is_wont(TELOPT_LINEMODE
)) /* Ignore if option disabled */
1157 * Process linemode suboptions.
1160 break; /* garbage was sent */
1161 request
= SB_GET(); /* get will/wont */
1164 break; /* another garbage check */
1166 if (request
== LM_SLC
) { /* SLC is not preceeded by WILL or WONT */
1168 * Process suboption buffer of slc's
1171 do_opt_slc(subpointer
, subend
- subpointer
);
1174 } else if (request
== LM_MODE
) {
1177 useeditmode
= SB_GET(); /* get mode flag */
1178 clientstat(LM_MODE
, 0, 0);
1184 switch (SB_GET()) { /* what suboption? */
1185 case LM_FORWARDMASK
:
1187 * According to spec, only server can send request for
1188 * forwardmask, and client can only return a positive response.
1189 * So don't worry about it.
1196 } /* end of case TELOPT_LINEMODE */
1198 case TELOPT_STATUS
: {
1206 if (my_state_is_will(TELOPT_STATUS
))
1217 } /* end of case TELOPT_STATUS */
1219 case TELOPT_XDISPLOC
: {
1220 if (SB_EOF() || SB_GET() != TELQUAL_IS
)
1222 settimer(xdisplocsubopt
);
1223 subpointer
[SB_LEN()] = '\0';
1224 (void)setenv("DISPLAY", (char *)subpointer
, 1);
1226 } /* end of case TELOPT_XDISPLOC */
1228 #ifdef TELOPT_NEW_ENVIRON
1229 case TELOPT_NEW_ENVIRON
:
1231 case TELOPT_OLD_ENVIRON
: {
1233 char *cp
, *varp
, *valp
;
1238 if (c
== TELQUAL_IS
) {
1239 if (subchar
== TELOPT_OLD_ENVIRON
)
1240 settimer(oenvironsubopt
);
1242 settimer(environsubopt
);
1243 } else if (c
!= TELQUAL_INFO
) {
1247 #ifdef TELOPT_NEW_ENVIRON
1248 if (subchar
== TELOPT_NEW_ENVIRON
) {
1251 if ((c
== NEW_ENV_VAR
) || (c
== ENV_USERVAR
))
1259 * We only want to do this if we haven't already decided
1260 * whether or not the other side has its VALUE and VAR
1264 int last
= -1; /* invalid value */
1266 int got_var
= 0, got_value
= 0, got_uservar
= 0;
1269 * The other side might have its VALUE and VAR values
1270 * reversed. To be interoperable, we need to determine
1271 * which way it is. If the first recognized character
1272 * is a VAR or VALUE, then that will tell us what
1273 * type of client it is. If the fist recognized
1274 * character is a USERVAR, then we continue scanning
1275 * the suboption looking for two consecutive
1276 * VAR or VALUE fields. We should not get two
1277 * consecutive VALUE fields, so finding two
1278 * consecutive VALUE or VAR fields will tell us
1279 * what the client is.
1286 if (last
< 0 || last
== OLD_ENV_VAR
1287 || (empty
&& (last
== OLD_ENV_VALUE
)))
1293 if (last
< 0 || last
== OLD_ENV_VALUE
1294 || (empty
&& (last
== OLD_ENV_VAR
)))
1295 goto env_ovar_wrong
;
1297 last
= OLD_ENV_VALUE
;
1300 /* count strings of USERVAR as one */
1301 if (last
!= ENV_USERVAR
)
1304 if (last
== OLD_ENV_VALUE
)
1306 if (last
== OLD_ENV_VAR
)
1307 goto env_ovar_wrong
;
1322 if (last
== OLD_ENV_VALUE
)
1324 if (last
== OLD_ENV_VAR
)
1325 goto env_ovar_wrong
;
1328 * Ok, the first thing was a USERVAR, and there
1329 * are not two consecutive VAR or VALUE commands,
1330 * and none of the VAR or VALUE commands are empty.
1331 * If the client has sent us a well-formed option,
1332 * then the number of VALUEs received should always
1333 * be less than or equal to the number of VARs and
1334 * USERVARs received.
1336 * If we got exactly as many VALUEs as VARs and
1337 * USERVARs, the client has the same definitions.
1339 * If we got exactly as many VARs as VALUEs and
1340 * USERVARS, the client has reversed definitions.
1342 if (got_uservar
+ got_var
== got_value
) {
1344 env_ovar
= OLD_ENV_VAR
;
1345 env_ovalue
= OLD_ENV_VALUE
;
1346 } else if (got_uservar
+ got_value
== got_var
) {
1348 env_ovar
= OLD_ENV_VALUE
;
1349 env_ovalue
= OLD_ENV_VAR
;
1351 output_data("ENVIRON VALUE and VAR are reversed!\r\n"));
1360 if ((c
== env_ovar
) || (c
== ENV_USERVAR
))
1368 cp
= varp
= (char *)subpointer
;
1373 if (subchar
== TELOPT_OLD_ENVIRON
) {
1376 else if (c
== env_ovalue
)
1383 cp
= valp
= (char *)subpointer
;
1390 (void)setenv(varp
, valp
, 1);
1393 cp
= varp
= (char *)subpointer
;
1409 (void)setenv(varp
, valp
, 1);
1413 } /* end of case TELOPT_NEW_ENVIRON */
1414 #ifdef AUTHENTICATION
1415 case TELOPT_AUTHENTICATION
:
1422 * These are sent by us and cannot be sent by
1427 auth_is(subpointer
, SB_LEN());
1430 auth_name(subpointer
, SB_LEN());
1436 case TELOPT_ENCRYPT
:
1440 case ENCRYPT_SUPPORT
:
1441 encrypt_support(subpointer
, SB_LEN());
1444 encrypt_is(subpointer
, SB_LEN());
1447 encrypt_reply(subpointer
, SB_LEN());
1450 encrypt_start(subpointer
, SB_LEN());
1455 case ENCRYPT_REQSTART
:
1456 encrypt_request_start(subpointer
, SB_LEN());
1458 case ENCRYPT_REQEND
:
1460 * We can always send an REQEND so that we cannot
1461 * get stuck encrypting. We should only get this
1462 * if we have been able to get in the correct mode
1465 encrypt_request_end();
1467 case ENCRYPT_ENC_KEYID
:
1468 encrypt_enc_keyid(subpointer
, SB_LEN());
1470 case ENCRYPT_DEC_KEYID
:
1471 encrypt_dec_keyid(subpointer
, SB_LEN());
1477 #endif /* ENCRYPTION */
1481 } /* end of switch */
1483 } /* end of suboption */
1488 clientstat(TELOPT_LINEMODE
, WILL
, 0);
1491 #define ADD(c) *ncp++ = c
1492 #define ADD_DATA(c) { *ncp++ = c; if (c == SE || c == IAC) *ncp++ = c; }
1496 unsigned char statusbuf
[256];
1502 netflush(); /* get rid of anything waiting to go out */
1510 * We check the want_state rather than the current state,
1511 * because if we received a DO/WILL for an option that we
1512 * don't support, and the other side didn't send a DONT/WONT
1513 * in response to our WONT/DONT, then the "state" will be
1514 * WILL/DO, and the "want_state" will be WONT/DONT. We
1515 * need to go by the latter.
1517 for (i
= 0; i
< (unsigned char)NTELOPTS
; i
++) {
1518 if (my_want_state_is_will(i
)) {
1524 if (his_want_state_is_will(i
)) {
1532 if (his_want_state_is_will(TELOPT_LFLOW
)) {
1542 if (restartany
>= 0) {
1546 ADD(LFLOW_RESTART_ANY
);
1548 ADD(LFLOW_RESTART_XON
);
1555 if (his_want_state_is_will(TELOPT_LINEMODE
)) {
1556 unsigned char *cp
, *cpe
;
1560 ADD(TELOPT_LINEMODE
);
1566 ADD(TELOPT_LINEMODE
);
1571 for (cpe
= cp
+ len
; cp
< cpe
; cp
++)
1575 #endif /* LINEMODE */
1580 output_datalen(statusbuf
, ncp
- statusbuf
);
1581 netflush(); /* Send it on its way */
1584 {printsub('>', statusbuf
, ncp
- statusbuf
); netflush();});
1588 * This function appends data to nfrontp and advances nfrontp.
1589 * Returns the number of characters written altogether (the
1590 * buffer may have been flushed in the process).
1594 output_data(const char *format
, ...)
1600 va_start(args
, format
);
1601 if ((len
= vasprintf(&buf
, format
, args
)) == -1)
1603 output_datalen(buf
, len
);
1610 output_datalen(const char *buf
, int len
)
1612 int remaining
, copied
;
1614 remaining
= BUFSIZ
- (nfrontp
- netobuf
);
1616 /* Free up enough space if the room is too low*/
1617 if ((len
> BUFSIZ
? BUFSIZ
: len
) > remaining
) {
1619 remaining
= BUFSIZ
- (nfrontp
- netobuf
);
1622 /* Copy out as much as will fit */
1623 copied
= remaining
> len
? len
: remaining
;
1624 memmove(nfrontp
, buf
, copied
);
1627 remaining
-= copied
;