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";
38 static const char rcsid[] =
43 #if defined(AUTHENTICATION)
44 #include <libtelnet/auth.h>
46 #if defined(ENCRYPTION)
47 #include <libtelnet/encrypt.h>
50 unsigned char doopt[] = { IAC, DO, '%', 'c', 0 };
51 unsigned char dont[] = { IAC, DONT, '%', 'c', 0 };
52 unsigned char will[] = { IAC, WILL, '%', 'c', 0 };
53 unsigned char wont[] = { IAC, WONT, '%', 'c', 0 };
57 * Buffer for sub-options, and macros
58 * for suboptions buffer manipulations
60 unsigned char subbuffer[512], *subpointer= subbuffer, *subend= subbuffer;
62 #define SB_CLEAR() subpointer = subbuffer
63 #define SB_TERM() { subend = subpointer; SB_CLEAR(); }
64 #define SB_ACCUM(c) if (subpointer < (subbuffer+sizeof subbuffer)) { \
65 *subpointer++ = (c); \
67 #define SB_GET() ((*subpointer++)&0xff)
68 #define SB_EOF() (subpointer >= subend)
69 #define SB_LEN() (subend - subpointer)
72 unsigned char *subsave;
73 #define SB_SAVE() subsave = subpointer;
74 #define SB_RESTORE() subpointer = subsave;
81 #define TS_DATA 0 /* base state */
82 #define TS_IAC 1 /* look for double IAC's */
83 #define TS_CR 2 /* CR-LF ->'s CR */
84 #define TS_SB 3 /* throw away begin's... */
85 #define TS_SE 4 /* ...end's (suboption negotiation) */
86 #define TS_WILL 5 /* will option negotiation */
87 #define TS_WONT 6 /* wont " */
88 #define TS_DO 7 /* do " */
89 #define TS_DONT 8 /* dont " */
95 static int state = TS_DATA;
96 #if defined(CRAY2) && defined(UNICOS5)
97 char *opfrontp = pfrontp;
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 */
210 neturg = nfrontp-1; /* off by one XXX */
212 printoption("td: send IAC", DM));
217 * Erase Character and
226 printoption("td: recv IAC", c));
227 ptyflush(); /* half-hearted */
230 ch = *slctab[SLC_EC].sptr;
232 ch = *slctab[SLC_EL].sptr;
233 if (ch != (cc_t)(_POSIX_VDISABLE))
234 *pfrontp++ = (unsigned char)ch;
239 * Check for urgent data...
243 printoption("td: recv IAC", c));
244 SYNCHing = stilloob(net);
250 * Begin option subnegotiation...
273 if (his_state_is_will(TELOPT_EOR))
278 * Handle RFC 10xx Telnet linemode option additions
279 * to command stream (EOF, SUSP, ABORT).
312 * bad form of suboption negotiation.
313 * handle it in such a way as to avoid
314 * damage to local state. Parse
315 * suboption buffer found so far,
316 * then treat remaining stream as
317 * another command sequence.
320 /* for DIAGNOSTICS */
333 /* for DIAGNOSTICS */
339 suboption(); /* handle sub-option */
365 syslog(LOG_ERR, "panic state=%d", state);
366 printf("telnetd: panic state=%d\n", state);
370 #if defined(CRAY2) && defined(UNICOS5)
372 char xptyobuf[BUFSIZ+NETSLOP];
375 int n = pfrontp - opfrontp, oc;
376 memmove(xptyobuf, opfrontp, n);
378 pfrontp += term_input(xptyobuf, pfrontp, n, BUFSIZ+NETSLOP,
380 for (cp = xbuf2; oc > 0; --oc)
381 if ((*nfrontp++ = *cp++) == IAC)
384 #endif /* defined(CRAY2) && defined(UNICOS5) */
385 } /* end of telrcv */
388 * The will/wont/do/dont state machines are based on Dave Borman's
389 * Telnet option processing state machine.
391 * These correspond to the following states:
392 * my_state = the last negotiated state
393 * want_state = what I want the state to go to
394 * want_resp = how many requests I have sent
395 * All state defaults are negative, and resp defaults to 0.
397 * When initiating a request to change state to new_state:
399 * if ((want_resp == 0 && new_state == my_state) || want_state == new_state) {
402 * want_state = new_state;
407 * When receiving new_state:
411 * if (want_resp && (new_state == my_state))
414 * if ((want_resp == 0) && (new_state != want_state)) {
415 * if (ok_to_switch_to new_state)
416 * want_state = new_state;
421 * my_state = new_state;
423 * Note that new_state is implied in these functions by the function itself.
424 * will and do imply positive new_state, wont and dont imply negative.
426 * Finally, there is one catch. If we send a negative response to a
427 * positive request, my_state will be the positive while want_state will
428 * remain negative. my_state will revert to negative when the negative
429 * acknowlegment arrives from the peer. Thus, my_state generally tells
430 * us not only the last negotiated state, but also tells us what the peer
431 * wants to be doing as well. It is important to understand this difference
432 * as we may wish to be processing data streams based on our desired state
433 * (want_state) or based on what the peer thinks the state is (my_state).
435 * This all works fine because if the peer sends a positive request, the data
436 * that we receive prior to negative acknowlegment will probably be affected
437 * by the positive state, and we can process it as such (if we can; if we
438 * can't then it really doesn't matter). If it is that important, then the
439 * peer probably should be buffering until this option state negotiation
444 send_do(option, init)
448 if ((do_dont_resp[option] == 0 && his_state_is_will(option)) ||
449 his_want_state_is_will(option))
452 * Special case for TELOPT_TM: We send a DO, but pretend
453 * that we sent a DONT, so that we can send more DOs if
456 if (option == TELOPT_TM)
457 set_his_want_state_wont(option);
459 set_his_want_state_will(option);
460 do_dont_resp[option]++;
462 (void) sprintf(nfrontp, (char *)doopt, option);
463 nfrontp += sizeof (dont) - 2;
465 DIAG(TD_OPTIONS, printoption("td: send do", option));
468 #ifdef AUTHENTICATION
469 extern void auth_request();
472 extern void doclientstat();
475 extern void encrypt_send_support();
476 #endif /* ENCRYPTION */
486 * process input from peer.
489 DIAG(TD_OPTIONS, printoption("td: recv will", option));
491 if (do_dont_resp[option]) {
492 do_dont_resp[option]--;
493 if (do_dont_resp[option] && his_state_is_will(option))
494 do_dont_resp[option]--;
496 if (do_dont_resp[option] == 0) {
497 if (his_want_state_is_wont(option)) {
509 * See comments below for more info.
511 not42 = 0; /* looks like a 4.2 system */
515 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
517 * This telnetd implementation does not really
518 * support timing marks, it just uses them to
519 * support the kludge linemode stuff. If we
520 * receive a will or wont TM in response to our
521 * do TM request that may have been sent to
522 * determine kludge linemode support, process
523 * it, otherwise TM should get a negative
527 * Handle the linemode kludge stuff.
528 * If we are not currently supporting any
529 * linemode at all, then we assume that this
530 * is the client telling us to use kludge
531 * linemode in response to our query. Set the
532 * linemode type that is to be supported, note
533 * that the client wishes to use linemode, and
534 * eat the will TM as though it never arrived.
536 if (lmodetype < KLUDGE_LINEMODE) {
537 lmodetype = KLUDGE_LINEMODE;
538 clientstat(TELOPT_LINEMODE, WILL, 0);
539 send_wont(TELOPT_SGA, 1);
540 } else if (lmodetype == NO_AUTOKLUDGE) {
541 lmodetype = KLUDGE_OK;
543 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
545 * We never respond to a WILL TM, and
546 * we leave the state WONT.
552 * If we are going to support flow control
553 * option, then don't worry peer that we can't
554 * change the flow control characters.
556 slctab[SLC_XON].defset.flag &= ~SLC_LEVELBITS;
557 slctab[SLC_XON].defset.flag |= SLC_DEFAULT;
558 slctab[SLC_XOFF].defset.flag &= ~SLC_LEVELBITS;
559 slctab[SLC_XOFF].defset.flag |= SLC_DEFAULT;
564 case TELOPT_XDISPLOC:
565 case TELOPT_NEW_ENVIRON:
566 case TELOPT_OLD_ENVIRON:
571 case TELOPT_LINEMODE:
572 # ifdef KLUDGELINEMODE
574 * Note client's desire to use linemode.
576 lmodetype = REAL_LINEMODE;
577 # endif /* KLUDGELINEMODE */
581 #endif /* LINEMODE */
583 #ifdef AUTHENTICATION
584 case TELOPT_AUTHENTICATION:
592 func = encrypt_send_support;
595 #endif /* ENCRYPTION */
601 set_his_want_state_will(option);
604 do_dont_resp[option]++;
605 send_dont(option, 0);
609 * Option processing that should happen when
610 * we receive conformation of a change in
611 * state that we had requested.
615 not42 = 0; /* looks like a 4.2 system */
617 * Egads, he responded "WILL ECHO". Turn
620 send_dont(option, 1);
622 * "WILL ECHO". Kludge upon kludge!
623 * A 4.2 client is now echoing user input at
624 * the tty. This is probably undesireable and
625 * it should be stopped. The client will
626 * respond WONT TM to the DO TM that we send to
627 * check for kludge linemode. When the WONT TM
628 * arrives, linemode will be turned off and a
629 * change propogated to the pty. This change
630 * will cause us to process the new pty state
631 * in localstat(), which will notice that
632 * linemode is off and send a WILL ECHO
633 * so that we are properly in character mode and
638 case TELOPT_LINEMODE:
639 # ifdef KLUDGELINEMODE
641 * Note client's desire to use linemode.
643 lmodetype = REAL_LINEMODE;
644 # endif /* KLUDGELINEMODE */
647 #endif /* LINEMODE */
649 #ifdef AUTHENTICATION
650 case TELOPT_AUTHENTICATION:
657 func = encrypt_send_support;
659 #endif /* ENCRYPTION */
666 set_his_state_will(option);
669 } /* end of willoption */
672 send_dont(option, init)
676 if ((do_dont_resp[option] == 0 && his_state_is_wont(option)) ||
677 his_want_state_is_wont(option))
679 set_his_want_state_wont(option);
680 do_dont_resp[option]++;
682 (void) sprintf(nfrontp, (char *)dont, option);
683 nfrontp += sizeof (doopt) - 2;
685 DIAG(TD_OPTIONS, printoption("td: send dont", option));
693 * Process client input.
696 DIAG(TD_OPTIONS, printoption("td: recv wont", option));
698 if (do_dont_resp[option]) {
699 do_dont_resp[option]--;
700 if (do_dont_resp[option] && his_state_is_wont(option))
701 do_dont_resp[option]--;
703 if (do_dont_resp[option] == 0) {
704 if (his_want_state_is_will(option)) {
705 /* it is always ok to change to negative state */
708 not42 = 1; /* doesn't seem to be a 4.2 system */
718 case TELOPT_LINEMODE:
719 # ifdef KLUDGELINEMODE
721 * If real linemode is supported, then client is
722 * asking to turn linemode off.
724 if (lmodetype != REAL_LINEMODE)
726 # endif /* KLUDGELINEMODE */
727 clientstat(TELOPT_LINEMODE, WONT, 0);
729 #endif /* LINEMODE */
733 * If we get a WONT TM, and had sent a DO TM,
734 * don't respond with a DONT TM, just leave it
735 * as is. Short circut the state machine to
738 set_his_want_state_wont(TELOPT_TM);
743 * If we are not going to support flow control
744 * option, then let peer know that we can't
745 * change the flow control characters.
747 slctab[SLC_XON].defset.flag &= ~SLC_LEVELBITS;
748 slctab[SLC_XON].defset.flag |= SLC_CANTCHANGE;
749 slctab[SLC_XOFF].defset.flag &= ~SLC_LEVELBITS;
750 slctab[SLC_XOFF].defset.flag |= SLC_CANTCHANGE;
753 #if defined(AUTHENTICATION)
754 case TELOPT_AUTHENTICATION:
755 auth_finished(0, AUTH_REJECT);
760 * For options that we might spin waiting for
761 * sub-negotiation, if the client turns off the
762 * option rather than responding to the request,
763 * we have to treat it here as if we got a response
764 * to the sub-negotiation, (by updating the timers)
765 * so that we'll break out of the loop.
768 settimer(ttypesubopt);
772 settimer(tspeedsubopt);
775 case TELOPT_XDISPLOC:
776 settimer(xdisplocsubopt);
779 case TELOPT_OLD_ENVIRON:
780 settimer(oenvironsubopt);
783 case TELOPT_NEW_ENVIRON:
784 settimer(environsubopt);
790 set_his_want_state_wont(option);
791 if (his_state_is_will(option))
792 send_dont(option, 0);
796 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
797 if (lmodetype < NO_AUTOKLUDGE) {
798 lmodetype = NO_LINEMODE;
799 clientstat(TELOPT_LINEMODE, WONT, 0);
800 send_will(TELOPT_SGA, 1);
801 send_will(TELOPT_ECHO, 1);
803 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
806 #if defined(AUTHENTICATION)
807 case TELOPT_AUTHENTICATION:
808 auth_finished(0, AUTH_REJECT);
816 set_his_state_wont(option);
818 } /* end of wontoption */
821 send_will(option, init)
825 if ((will_wont_resp[option] == 0 && my_state_is_will(option))||
826 my_want_state_is_will(option))
828 set_my_want_state_will(option);
829 will_wont_resp[option]++;
831 (void) sprintf(nfrontp, (char *)will, option);
832 nfrontp += sizeof (doopt) - 2;
834 DIAG(TD_OPTIONS, printoption("td: send will", option));
837 #if !defined(LINEMODE) || !defined(KLUDGELINEMODE)
839 * When we get a DONT SGA, we will try once to turn it
840 * back on. If the other side responds DONT SGA, we
841 * leave it at that. This is so that when we talk to
842 * clients that understand KLUDGELINEMODE but not LINEMODE,
843 * we'll keep them in char-at-a-time mode.
855 * Process client input.
858 DIAG(TD_OPTIONS, printoption("td: recv do", option));
860 if (will_wont_resp[option]) {
861 will_wont_resp[option]--;
862 if (will_wont_resp[option] && my_state_is_will(option))
863 will_wont_resp[option]--;
865 if ((will_wont_resp[option] == 0) && (my_want_state_is_wont(option))) {
869 # ifdef KLUDGELINEMODE
870 if (lmodetype == NO_LINEMODE)
872 if (his_state_is_wont(TELOPT_LINEMODE))
891 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
893 * If kludge linemode is in use, then we must
894 * process an incoming do SGA for linemode
897 if (lmodetype == KLUDGE_LINEMODE) {
899 * Receipt of "do SGA" in kludge
900 * linemode is the peer asking us to
901 * turn off linemode. Make note of
904 clientstat(TELOPT_LINEMODE, WONT, 0);
906 * If linemode did not get turned off
907 * then don't tell peer that we did.
908 * Breaking here forces a wont SGA to
916 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
926 * Special case for TM. We send a WILL, but
927 * pretend we sent a WONT.
929 send_will(option, 0);
930 set_my_want_state_wont(option);
931 set_my_state_wont(option);
936 * When we get a LOGOUT option, respond
937 * with a WILL LOGOUT, make sure that
938 * it gets written out to the network,
939 * and then just go away...
941 set_my_want_state_will(TELOPT_LOGOUT);
942 send_will(TELOPT_LOGOUT, 0);
943 set_my_state_will(TELOPT_LOGOUT);
953 #endif /* ENCRYPTION */
954 case TELOPT_LINEMODE:
959 case TELOPT_XDISPLOC:
960 #ifdef TELOPT_ENVIRON
961 case TELOPT_NEW_ENVIRON:
963 case TELOPT_OLD_ENVIRON:
968 set_my_want_state_will(option);
969 send_will(option, 0);
971 will_wont_resp[option]++;
972 send_wont(option, 0);
975 set_my_state_will(option);
977 } /* end of dooption */
980 send_wont(option, init)
984 if ((will_wont_resp[option] == 0 && my_state_is_wont(option)) ||
985 my_want_state_is_wont(option))
987 set_my_want_state_wont(option);
988 will_wont_resp[option]++;
990 (void) sprintf(nfrontp, (char *)wont, option);
991 nfrontp += sizeof (wont) - 2;
993 DIAG(TD_OPTIONS, printoption("td: send wont", option));
1001 * Process client input.
1005 DIAG(TD_OPTIONS, printoption("td: recv dont", option));
1007 if (will_wont_resp[option]) {
1008 will_wont_resp[option]--;
1009 if (will_wont_resp[option] && my_state_is_wont(option))
1010 will_wont_resp[option]--;
1012 if ((will_wont_resp[option] == 0) && (my_want_state_is_will(option))) {
1020 case TELOPT_ECHO: /* we should stop echoing */
1022 # ifdef KLUDGELINEMODE
1023 if ((lmodetype != REAL_LINEMODE) &&
1024 (lmodetype != KLUDGE_LINEMODE))
1026 if (his_state_is_wont(TELOPT_LINEMODE))
1037 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
1039 * If kludge linemode is in use, then we
1040 * must process an incoming do SGA for
1041 * linemode purposes.
1043 if ((lmodetype == KLUDGE_LINEMODE) ||
1044 (lmodetype == KLUDGE_OK)) {
1046 * The client is asking us to turn
1049 lmodetype = KLUDGE_LINEMODE;
1050 clientstat(TELOPT_LINEMODE, WILL, 0);
1052 * If we did not turn line mode on,
1053 * then what do we say? Will SGA?
1054 * This violates design of telnet.
1055 * Gross. Very Gross.
1060 set_my_want_state_wont(option);
1061 if (my_state_is_will(option))
1062 send_wont(option, 0);
1063 set_my_state_wont(option);
1064 if (turn_on_sga ^= 1)
1065 send_will(option, 1);
1067 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
1073 set_my_want_state_wont(option);
1074 if (my_state_is_will(option))
1075 send_wont(option, 0);
1077 set_my_state_wont(option);
1079 } /* end of dontoption */
1083 int env_ovalue = -1;
1084 #else /* ENV_HACK */
1085 # define env_ovar OLD_ENV_VAR
1086 # define env_ovalue OLD_ENV_VALUE
1087 #endif /* ENV_HACK */
1092 * Look at the sub-option buffer, and try to be helpful to the other
1095 * Currently we recognize:
1105 register int subchar;
1107 DIAG(TD_OPTIONS, {netflush(); printsub('<', subpointer, SB_LEN()+2);});
1111 case TELOPT_TSPEED: {
1112 register int xspeed, rspeed;
1114 if (his_state_is_wont(TELOPT_TSPEED)) /* Ignore if option disabled */
1117 settimer(tspeedsubopt);
1119 if (SB_EOF() || SB_GET() != TELQUAL_IS)
1122 xspeed = atoi((char *)subpointer);
1124 while (SB_GET() != ',' && !SB_EOF());
1128 rspeed = atoi((char *)subpointer);
1129 clientstat(TELOPT_TSPEED, xspeed, rspeed);
1133 } /* end of case TELOPT_TSPEED */
1135 case TELOPT_TTYPE: { /* Yaaaay! */
1136 static char terminalname[41];
1138 if (his_state_is_wont(TELOPT_TTYPE)) /* Ignore if option disabled */
1140 settimer(ttypesubopt);
1142 if (SB_EOF() || SB_GET() != TELQUAL_IS) {
1143 return; /* ??? XXX but, this is the most robust */
1146 terminaltype = terminalname;
1148 while ((terminaltype < (terminalname + sizeof terminalname-1)) &&
1156 *terminaltype++ = c; /* accumulate name */
1159 terminaltype = terminalname;
1161 } /* end of case TELOPT_TTYPE */
1164 register int xwinsize, ywinsize;
1166 if (his_state_is_wont(TELOPT_NAWS)) /* Ignore if option disabled */
1171 xwinsize = SB_GET() << 8;
1174 xwinsize |= SB_GET();
1177 ywinsize = SB_GET() << 8;
1180 ywinsize |= SB_GET();
1181 clientstat(TELOPT_NAWS, xwinsize, ywinsize);
1185 } /* end of case TELOPT_NAWS */
1188 case TELOPT_LINEMODE: {
1189 register int request;
1191 if (his_state_is_wont(TELOPT_LINEMODE)) /* Ignore if option disabled */
1194 * Process linemode suboptions.
1197 break; /* garbage was sent */
1198 request = SB_GET(); /* get will/wont */
1201 break; /* another garbage check */
1203 if (request == LM_SLC) { /* SLC is not preceeded by WILL or WONT */
1205 * Process suboption buffer of slc's
1208 do_opt_slc(subpointer, subend - subpointer);
1211 } else if (request == LM_MODE) {
1214 useeditmode = SB_GET(); /* get mode flag */
1215 clientstat(LM_MODE, 0, 0);
1221 switch (SB_GET()) { /* what suboption? */
1222 case LM_FORWARDMASK:
1224 * According to spec, only server can send request for
1225 * forwardmask, and client can only return a positive response.
1226 * So don't worry about it.
1233 } /* end of case TELOPT_LINEMODE */
1235 case TELOPT_STATUS: {
1243 if (my_state_is_will(TELOPT_STATUS))
1254 } /* end of case TELOPT_STATUS */
1256 case TELOPT_XDISPLOC: {
1257 if (SB_EOF() || SB_GET() != TELQUAL_IS)
1259 settimer(xdisplocsubopt);
1260 subpointer[SB_LEN()] = '\0';
1261 (void)setenv("DISPLAY", (char *)subpointer, 1);
1263 } /* end of case TELOPT_XDISPLOC */
1265 #ifdef TELOPT_NEW_ENVIRON
1266 case TELOPT_NEW_ENVIRON:
1268 case TELOPT_OLD_ENVIRON: {
1270 register char *cp, *varp, *valp;
1275 if (c == TELQUAL_IS) {
1276 if (subchar == TELOPT_OLD_ENVIRON)
1277 settimer(oenvironsubopt);
1279 settimer(environsubopt);
1280 } else if (c != TELQUAL_INFO) {
1284 #ifdef TELOPT_NEW_ENVIRON
1285 if (subchar == TELOPT_NEW_ENVIRON) {
1288 if ((c == NEW_ENV_VAR) || (c == ENV_USERVAR))
1296 * We only want to do this if we haven't already decided
1297 * whether or not the other side has its VALUE and VAR
1301 register int last = -1; /* invalid value */
1303 int got_var = 0, got_value = 0, got_uservar = 0;
1306 * The other side might have its VALUE and VAR values
1307 * reversed. To be interoperable, we need to determine
1308 * which way it is. If the first recognized character
1309 * is a VAR or VALUE, then that will tell us what
1310 * type of client it is. If the fist recognized
1311 * character is a USERVAR, then we continue scanning
1312 * the suboption looking for two consecutive
1313 * VAR or VALUE fields. We should not get two
1314 * consecutive VALUE fields, so finding two
1315 * consecutive VALUE or VAR fields will tell us
1316 * what the client is.
1323 if (last < 0 || last == OLD_ENV_VAR
1324 || (empty && (last == OLD_ENV_VALUE)))
1330 if (last < 0 || last == OLD_ENV_VALUE
1331 || (empty && (last == OLD_ENV_VAR)))
1332 goto env_ovar_wrong;
1334 last = OLD_ENV_VALUE;
1337 /* count strings of USERVAR as one */
1338 if (last != ENV_USERVAR)
1341 if (last == OLD_ENV_VALUE)
1343 if (last == OLD_ENV_VAR)
1344 goto env_ovar_wrong;
1359 if (last == OLD_ENV_VALUE)
1361 if (last == OLD_ENV_VAR)
1362 goto env_ovar_wrong;
1365 * Ok, the first thing was a USERVAR, and there
1366 * are not two consecutive VAR or VALUE commands,
1367 * and none of the VAR or VALUE commands are empty.
1368 * If the client has sent us a well-formed option,
1369 * then the number of VALUEs received should always
1370 * be less than or equal to the number of VARs and
1371 * USERVARs received.
1373 * If we got exactly as many VALUEs as VARs and
1374 * USERVARs, the client has the same definitions.
1376 * If we got exactly as many VARs as VALUEs and
1377 * USERVARS, the client has reversed definitions.
1379 if (got_uservar + got_var == got_value) {
1381 env_ovar = OLD_ENV_VAR;
1382 env_ovalue = OLD_ENV_VALUE;
1383 } else if (got_uservar + got_value == got_var) {
1385 env_ovar = OLD_ENV_VALUE;
1386 env_ovalue = OLD_ENV_VAR;
1387 DIAG(TD_OPTIONS, {sprintf(nfrontp,
1388 "ENVIRON VALUE and VAR are reversed!\r\n");
1389 nfrontp += strlen(nfrontp);});
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;
1428 (void)setenv(varp, valp, 1);
1431 cp = varp = (char *)subpointer;
1447 (void)setenv(varp, valp, 1);
1451 } /* end of case TELOPT_NEW_ENVIRON */
1452 #if defined(AUTHENTICATION)
1453 case TELOPT_AUTHENTICATION:
1460 * These are sent by us and cannot be sent by
1465 auth_is(subpointer, SB_LEN());
1468 auth_name(subpointer, SB_LEN());
1474 case TELOPT_ENCRYPT:
1478 case ENCRYPT_SUPPORT:
1479 encrypt_support(subpointer, SB_LEN());
1482 encrypt_is(subpointer, SB_LEN());
1485 encrypt_reply(subpointer, SB_LEN());
1488 encrypt_start(subpointer, SB_LEN());
1493 case ENCRYPT_REQSTART:
1494 encrypt_request_start(subpointer, SB_LEN());
1496 case ENCRYPT_REQEND:
1498 * We can always send an REQEND so that we cannot
1499 * get stuck encrypting. We should only get this
1500 * if we have been able to get in the correct mode
1503 encrypt_request_end();
1505 case ENCRYPT_ENC_KEYID:
1506 encrypt_enc_keyid(subpointer, SB_LEN());
1508 case ENCRYPT_DEC_KEYID:
1509 encrypt_dec_keyid(subpointer, SB_LEN());
1515 #endif /* ENCRYPTION */
1519 } /* end of switch */
1521 } /* end of suboption */
1526 clientstat(TELOPT_LINEMODE, WILL, 0);
1529 #define ADD(c) *ncp++ = c
1530 #define ADD_DATA(c) { *ncp++ = c; if (c == SE || c == IAC) *ncp++ = c; }
1534 unsigned char statusbuf[256];
1535 register unsigned char *ncp;
1536 register unsigned char i;
1540 netflush(); /* get rid of anything waiting to go out */
1548 * We check the want_state rather than the current state,
1549 * because if we received a DO/WILL for an option that we
1550 * don't support, and the other side didn't send a DONT/WONT
1551 * in response to our WONT/DONT, then the "state" will be
1552 * WILL/DO, and the "want_state" will be WONT/DONT. We
1553 * need to go by the latter.
1555 for (i = 0; i < (unsigned char)NTELOPTS; i++) {
1556 if (my_want_state_is_will(i)) {
1560 if (his_want_state_is_will(i)) {
1566 if (his_want_state_is_will(TELOPT_LFLOW)) {
1576 if (restartany >= 0) {
1580 ADD(LFLOW_RESTART_ANY);
1582 ADD(LFLOW_RESTART_XON);
1589 if (his_want_state_is_will(TELOPT_LINEMODE)) {
1590 unsigned char *cp, *cpe;
1594 ADD(TELOPT_LINEMODE);
1600 ADD(TELOPT_LINEMODE);
1605 for (cpe = cp + len; cp < cpe; cp++)
1609 #endif /* LINEMODE */
1614 writenet(statusbuf, ncp - statusbuf);
1615 netflush(); /* Send it on its way */
1618 {printsub('>', statusbuf, ncp - statusbuf); netflush();});