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
34 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD: src/libexec/telnetd/state.c,v 1.9.2.4 2002/04/13 11:07:12 markm Exp $");
39 static const char sccsid[] = "@(#)state.c 8.5 (Berkeley) 5/30/95";
45 unsigned char doopt[] = { IAC, DO, '%', 'c', 0 };
46 unsigned char dont[] = { IAC, DONT, '%', 'c', 0 };
47 unsigned char will[] = { IAC, WILL, '%', 'c', 0 };
48 unsigned char wont[] = { IAC, WONT, '%', 'c', 0 };
52 * Buffer for sub-options, and macros
53 * for suboptions buffer manipulations
55 unsigned char subbuffer[512], *subpointer= subbuffer, *subend= subbuffer;
57 #define SB_CLEAR() subpointer = subbuffer
58 #define SB_TERM() { subend = subpointer; SB_CLEAR(); }
59 #define SB_ACCUM(c) if (subpointer < (subbuffer+sizeof subbuffer)) { \
60 *subpointer++ = (c); \
62 #define SB_GET() ((*subpointer++)&0xff)
63 #define SB_EOF() (subpointer >= subend)
64 #define SB_LEN() (subend - subpointer)
67 unsigned char *subsave;
68 #define SB_SAVE() subsave = subpointer;
69 #define SB_RESTORE() subpointer = subsave;
76 #define TS_DATA 0 /* base state */
77 #define TS_IAC 1 /* look for double IAC's */
78 #define TS_CR 2 /* CR-LF ->'s CR */
79 #define TS_SB 3 /* throw away begin's... */
80 #define TS_SE 4 /* ...end's (suboption negotiation) */
81 #define TS_WILL 5 /* will option negotiation */
82 #define TS_WONT 6 /* wont " */
83 #define TS_DO 7 /* do " */
84 #define TS_DONT 8 /* dont " */
86 static void doclientstat(void);
92 static int state = TS_DATA;
95 if ((&ptyobuf[BUFSIZ] - pfrontp) < 2)
97 c = *netip++ & 0377, ncc--;
102 /* Strip off \n or \0 after a \r */
103 if ((c == 0) || (c == '\n')) {
114 * We now map \r\n ==> \r for pragmatic reasons.
115 * Many client implementations send \r\n when
116 * the user hits the CarriageReturn key.
118 * We USED to map \r\n ==> \n, since \r\n says
119 * that we want to be in column 1 of the next
120 * printable line, and \n is the standard
121 * unix way of saying that (\r is only good
122 * if CRMOD is set, which it normally is).
124 if ((c == '\r') && his_state_is_wont(TELOPT_BINARY)) {
128 * If we are operating in linemode,
129 * convert to local end-of-line.
131 if (linemode && (ncc > 0) && (('\n' == nc) ||
132 ((0 == nc) && tty_iscrnl())) ) {
148 * Send the process on the pty side an
149 * interrupt. Do this with a NULL or
150 * interrupt char; depending on the tty mode.
154 printoption("td: recv IAC", c));
160 printoption("td: recv IAC", c));
169 printoption("td: recv IAC", c));
179 printoption("td: recv IAC", c));
180 ptyflush(); /* half-hearted */
183 if (slctab[SLC_AO].sptr &&
184 *slctab[SLC_AO].sptr != (cc_t)(_POSIX_VDISABLE)) {
186 (unsigned char)*slctab[SLC_AO].sptr;
189 netclear(); /* clear buffer back */
190 output_data("%c%c", IAC, DM);
191 neturg = nfrontp-1; /* off by one XXX */
193 printoption("td: send IAC", DM));
198 * Erase Character and
207 printoption("td: recv IAC", c));
208 ptyflush(); /* half-hearted */
211 ch = *slctab[SLC_EC].sptr;
213 ch = *slctab[SLC_EL].sptr;
214 if (ch != (cc_t)(_POSIX_VDISABLE))
215 *pfrontp++ = (unsigned char)ch;
220 * Check for urgent data...
224 printoption("td: recv IAC", c));
225 SYNCHing = stilloob(net);
231 * Begin option subnegotiation...
254 if (his_state_is_will(TELOPT_EOR))
259 * Handle RFC 10xx Telnet linemode option additions
260 * to command stream (EOF, SUSP, ABORT).
293 * bad form of suboption negotiation.
294 * handle it in such a way as to avoid
295 * damage to local state. Parse
296 * suboption buffer found so far,
297 * then treat remaining stream as
298 * another command sequence.
301 /* for DIAGNOSTICS */
314 /* for DIAGNOSTICS */
320 suboption(); /* handle sub-option */
346 syslog(LOG_ERR, "panic state=%d", state);
347 printf("telnetd: panic state=%d\n", state);
351 } /* end of telrcv */
354 * The will/wont/do/dont state machines are based on Dave Borman's
355 * Telnet option processing state machine.
357 * These correspond to the following states:
358 * my_state = the last negotiated state
359 * want_state = what I want the state to go to
360 * want_resp = how many requests I have sent
361 * All state defaults are negative, and resp defaults to 0.
363 * When initiating a request to change state to new_state:
365 * if ((want_resp == 0 && new_state == my_state) || want_state == new_state) {
368 * want_state = new_state;
373 * When receiving new_state:
377 * if (want_resp && (new_state == my_state))
380 * if ((want_resp == 0) && (new_state != want_state)) {
381 * if (ok_to_switch_to new_state)
382 * want_state = new_state;
387 * my_state = new_state;
389 * Note that new_state is implied in these functions by the function itself.
390 * will and do imply positive new_state, wont and dont imply negative.
392 * Finally, there is one catch. If we send a negative response to a
393 * positive request, my_state will be the positive while want_state will
394 * remain negative. my_state will revert to negative when the negative
395 * acknowlegment arrives from the peer. Thus, my_state generally tells
396 * us not only the last negotiated state, but also tells us what the peer
397 * wants to be doing as well. It is important to understand this difference
398 * as we may wish to be processing data streams based on our desired state
399 * (want_state) or based on what the peer thinks the state is (my_state).
401 * This all works fine because if the peer sends a positive request, the data
402 * that we receive prior to negative acknowlegment will probably be affected
403 * by the positive state, and we can process it as such (if we can; if we
404 * can't then it really doesn't matter). If it is that important, then the
405 * peer probably should be buffering until this option state negotiation
410 send_do(int option, int init)
413 if ((do_dont_resp[option] == 0 && his_state_is_will(option)) ||
414 his_want_state_is_will(option))
417 * Special case for TELOPT_TM: We send a DO, but pretend
418 * that we sent a DONT, so that we can send more DOs if
421 if (option == TELOPT_TM)
422 set_his_want_state_wont(option);
424 set_his_want_state_will(option);
425 do_dont_resp[option]++;
427 output_data((const char *)doopt, option);
429 DIAG(TD_OPTIONS, printoption("td: send do", option));
433 willoption(int option)
436 void (*func)(void) = 0;
439 * process input from peer.
442 DIAG(TD_OPTIONS, printoption("td: recv will", option));
444 if (do_dont_resp[option]) {
445 do_dont_resp[option]--;
446 if (do_dont_resp[option] && his_state_is_will(option))
447 do_dont_resp[option]--;
449 if (do_dont_resp[option] == 0) {
450 if (his_want_state_is_wont(option)) {
462 * See comments below for more info.
464 not42 = 0; /* looks like a 4.2 system */
468 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
470 * This telnetd implementation does not really
471 * support timing marks, it just uses them to
472 * support the kludge linemode stuff. If we
473 * receive a will or wont TM in response to our
474 * do TM request that may have been sent to
475 * determine kludge linemode support, process
476 * it, otherwise TM should get a negative
480 * Handle the linemode kludge stuff.
481 * If we are not currently supporting any
482 * linemode at all, then we assume that this
483 * is the client telling us to use kludge
484 * linemode in response to our query. Set the
485 * linemode type that is to be supported, note
486 * that the client wishes to use linemode, and
487 * eat the will TM as though it never arrived.
489 if (lmodetype < KLUDGE_LINEMODE) {
490 lmodetype = KLUDGE_LINEMODE;
491 clientstat(TELOPT_LINEMODE, WILL, 0);
492 send_wont(TELOPT_SGA, 1);
493 } else if (lmodetype == NO_AUTOKLUDGE) {
494 lmodetype = KLUDGE_OK;
496 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
498 * We never respond to a WILL TM, and
499 * we leave the state WONT.
505 * If we are going to support flow control
506 * option, then don't worry peer that we can't
507 * change the flow control characters.
509 slctab[SLC_XON].defset.flag &= ~SLC_LEVELBITS;
510 slctab[SLC_XON].defset.flag |= SLC_DEFAULT;
511 slctab[SLC_XOFF].defset.flag &= ~SLC_LEVELBITS;
512 slctab[SLC_XOFF].defset.flag |= SLC_DEFAULT;
517 case TELOPT_XDISPLOC:
518 case TELOPT_NEW_ENVIRON:
519 case TELOPT_OLD_ENVIRON:
524 case TELOPT_LINEMODE:
525 # ifdef KLUDGELINEMODE
527 * Note client's desire to use linemode.
529 lmodetype = REAL_LINEMODE;
530 # endif /* KLUDGELINEMODE */
534 #endif /* LINEMODE */
542 set_his_want_state_will(option);
545 do_dont_resp[option]++;
546 send_dont(option, 0);
550 * Option processing that should happen when
551 * we receive conformation of a change in
552 * state that we had requested.
556 not42 = 0; /* looks like a 4.2 system */
558 * Egads, he responded "WILL ECHO". Turn
561 send_dont(option, 1);
563 * "WILL ECHO". Kludge upon kludge!
564 * A 4.2 client is now echoing user input at
565 * the tty. This is probably undesireable and
566 * it should be stopped. The client will
567 * respond WONT TM to the DO TM that we send to
568 * check for kludge linemode. When the WONT TM
569 * arrives, linemode will be turned off and a
570 * change propogated to the pty. This change
571 * will cause us to process the new pty state
572 * in localstat(), which will notice that
573 * linemode is off and send a WILL ECHO
574 * so that we are properly in character mode and
579 case TELOPT_LINEMODE:
580 # ifdef KLUDGELINEMODE
582 * Note client's desire to use linemode.
584 lmodetype = REAL_LINEMODE;
585 # endif /* KLUDGELINEMODE */
588 #endif /* LINEMODE */
597 set_his_state_will(option);
600 } /* end of willoption */
603 send_dont(int option, int init)
606 if ((do_dont_resp[option] == 0 && his_state_is_wont(option)) ||
607 his_want_state_is_wont(option))
609 set_his_want_state_wont(option);
610 do_dont_resp[option]++;
612 output_data((const char *)dont, option);
614 DIAG(TD_OPTIONS, printoption("td: send dont", option));
618 wontoption(int option)
621 * Process client input.
624 DIAG(TD_OPTIONS, printoption("td: recv wont", option));
626 if (do_dont_resp[option]) {
627 do_dont_resp[option]--;
628 if (do_dont_resp[option] && his_state_is_wont(option))
629 do_dont_resp[option]--;
631 if (do_dont_resp[option] == 0) {
632 if (his_want_state_is_will(option)) {
633 /* it is always ok to change to negative state */
636 not42 = 1; /* doesn't seem to be a 4.2 system */
646 case TELOPT_LINEMODE:
647 # ifdef KLUDGELINEMODE
649 * If real linemode is supported, then client is
650 * asking to turn linemode off.
652 if (lmodetype != REAL_LINEMODE)
654 lmodetype = KLUDGE_LINEMODE;
655 # endif /* KLUDGELINEMODE */
656 clientstat(TELOPT_LINEMODE, WONT, 0);
658 #endif /* LINEMODE */
662 * If we get a WONT TM, and had sent a DO TM,
663 * don't respond with a DONT TM, just leave it
664 * as is. Short circut the state machine to
667 set_his_want_state_wont(TELOPT_TM);
672 * If we are not going to support flow control
673 * option, then let peer know that we can't
674 * change the flow control characters.
676 slctab[SLC_XON].defset.flag &= ~SLC_LEVELBITS;
677 slctab[SLC_XON].defset.flag |= SLC_CANTCHANGE;
678 slctab[SLC_XOFF].defset.flag &= ~SLC_LEVELBITS;
679 slctab[SLC_XOFF].defset.flag |= SLC_CANTCHANGE;
684 * For options that we might spin waiting for
685 * sub-negotiation, if the client turns off the
686 * option rather than responding to the request,
687 * we have to treat it here as if we got a response
688 * to the sub-negotiation, (by updating the timers)
689 * so that we'll break out of the loop.
692 settimer(ttypesubopt);
696 settimer(tspeedsubopt);
699 case TELOPT_XDISPLOC:
700 settimer(xdisplocsubopt);
703 case TELOPT_OLD_ENVIRON:
704 settimer(oenvironsubopt);
707 case TELOPT_NEW_ENVIRON:
708 settimer(environsubopt);
714 set_his_want_state_wont(option);
715 if (his_state_is_will(option))
716 send_dont(option, 0);
720 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
721 if (lmodetype < NO_AUTOKLUDGE) {
722 lmodetype = NO_LINEMODE;
723 clientstat(TELOPT_LINEMODE, WONT, 0);
724 send_will(TELOPT_SGA, 1);
725 send_will(TELOPT_ECHO, 1);
727 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
735 set_his_state_wont(option);
737 } /* end of wontoption */
740 send_will(int option, int init)
743 if ((will_wont_resp[option] == 0 && my_state_is_will(option))||
744 my_want_state_is_will(option))
746 set_my_want_state_will(option);
747 will_wont_resp[option]++;
749 output_data((const char *)will, option);
751 DIAG(TD_OPTIONS, printoption("td: send will", option));
754 #if !defined(LINEMODE) || !defined(KLUDGELINEMODE)
756 * When we get a DONT SGA, we will try once to turn it
757 * back on. If the other side responds DONT SGA, we
758 * leave it at that. This is so that when we talk to
759 * clients that understand KLUDGELINEMODE but not LINEMODE,
760 * we'll keep them in char-at-a-time mode.
771 * Process client input.
774 DIAG(TD_OPTIONS, printoption("td: recv do", option));
776 if (will_wont_resp[option]) {
777 will_wont_resp[option]--;
778 if (will_wont_resp[option] && my_state_is_will(option))
779 will_wont_resp[option]--;
781 if ((will_wont_resp[option] == 0) && (my_want_state_is_wont(option))) {
785 # ifdef KLUDGELINEMODE
786 if (lmodetype == NO_LINEMODE)
788 if (his_state_is_wont(TELOPT_LINEMODE))
807 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
809 * If kludge linemode is in use, then we must
810 * process an incoming do SGA for linemode
813 if (lmodetype == KLUDGE_LINEMODE) {
815 * Receipt of "do SGA" in kludge
816 * linemode is the peer asking us to
817 * turn off linemode. Make note of
820 clientstat(TELOPT_LINEMODE, WONT, 0);
822 * If linemode did not get turned off
823 * then don't tell peer that we did.
824 * Breaking here forces a wont SGA to
832 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
842 * Special case for TM. We send a WILL, but
843 * pretend we sent a WONT.
845 send_will(option, 0);
846 set_my_want_state_wont(option);
847 set_my_state_wont(option);
852 * When we get a LOGOUT option, respond
853 * with a WILL LOGOUT, make sure that
854 * it gets written out to the network,
855 * and then just go away...
857 set_my_want_state_will(TELOPT_LOGOUT);
858 send_will(TELOPT_LOGOUT, 0);
859 set_my_state_will(TELOPT_LOGOUT);
865 case TELOPT_LINEMODE:
870 case TELOPT_XDISPLOC:
871 #ifdef TELOPT_ENVIRON
872 case TELOPT_NEW_ENVIRON:
874 case TELOPT_OLD_ENVIRON:
879 set_my_want_state_will(option);
880 send_will(option, 0);
882 will_wont_resp[option]++;
883 send_wont(option, 0);
886 set_my_state_will(option);
888 } /* end of dooption */
891 send_wont(int option, int init)
894 if ((will_wont_resp[option] == 0 && my_state_is_wont(option)) ||
895 my_want_state_is_wont(option))
897 set_my_want_state_wont(option);
898 will_wont_resp[option]++;
900 output_data((const char *)wont, option);
902 DIAG(TD_OPTIONS, printoption("td: send wont", option));
906 dontoption(int option)
909 * Process client input.
913 DIAG(TD_OPTIONS, printoption("td: recv dont", option));
915 if (will_wont_resp[option]) {
916 will_wont_resp[option]--;
917 if (will_wont_resp[option] && my_state_is_wont(option))
918 will_wont_resp[option]--;
920 if ((will_wont_resp[option] == 0) && (my_want_state_is_will(option))) {
928 case TELOPT_ECHO: /* we should stop echoing */
930 # ifdef KLUDGELINEMODE
931 if ((lmodetype != REAL_LINEMODE) &&
932 (lmodetype != KLUDGE_LINEMODE))
934 if (his_state_is_wont(TELOPT_LINEMODE))
945 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
947 * If kludge linemode is in use, then we
948 * must process an incoming do SGA for
951 if ((lmodetype == KLUDGE_LINEMODE) ||
952 (lmodetype == KLUDGE_OK)) {
954 * The client is asking us to turn
957 lmodetype = KLUDGE_LINEMODE;
958 clientstat(TELOPT_LINEMODE, WILL, 0);
960 * If we did not turn line mode on,
961 * then what do we say? Will SGA?
962 * This violates design of telnet.
968 set_my_want_state_wont(option);
969 if (my_state_is_will(option))
970 send_wont(option, 0);
971 set_my_state_wont(option);
972 if (turn_on_sga ^= 1)
973 send_will(option, 1);
975 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
981 set_my_want_state_wont(option);
982 if (my_state_is_will(option))
983 send_wont(option, 0);
985 set_my_state_wont(option);
987 } /* end of dontoption */
993 # define env_ovar OLD_ENV_VAR
994 # define env_ovalue OLD_ENV_VALUE
995 #endif /* ENV_HACK */
1000 * Look at the sub-option buffer, and try to be helpful to the other
1003 * Currently we recognize:
1015 DIAG(TD_OPTIONS, {netflush(); printsub('<', subpointer, SB_LEN()+2);});
1019 case TELOPT_TSPEED: {
1022 if (his_state_is_wont(TELOPT_TSPEED)) /* Ignore if option disabled */
1025 settimer(tspeedsubopt);
1027 if (SB_EOF() || SB_GET() != TELQUAL_IS)
1030 xspeed = atoi((char *)subpointer);
1032 while (SB_GET() != ',' && !SB_EOF());
1036 rspeed = atoi((char *)subpointer);
1037 clientstat(TELOPT_TSPEED, xspeed, rspeed);
1041 } /* end of case TELOPT_TSPEED */
1043 case TELOPT_TTYPE: { /* Yaaaay! */
1044 static char terminalname[41];
1046 if (his_state_is_wont(TELOPT_TTYPE)) /* Ignore if option disabled */
1048 settimer(ttypesubopt);
1050 if (SB_EOF() || SB_GET() != TELQUAL_IS) {
1051 return; /* ??? XXX but, this is the most robust */
1054 terminaltype = terminalname;
1056 while ((terminaltype < (terminalname + sizeof terminalname-1)) &&
1064 *terminaltype++ = c; /* accumulate name */
1067 terminaltype = terminalname;
1069 } /* end of case TELOPT_TTYPE */
1072 int xwinsize, ywinsize;
1074 if (his_state_is_wont(TELOPT_NAWS)) /* Ignore if option disabled */
1079 xwinsize = SB_GET() << 8;
1082 xwinsize |= SB_GET();
1085 ywinsize = SB_GET() << 8;
1088 ywinsize |= SB_GET();
1089 clientstat(TELOPT_NAWS, xwinsize, ywinsize);
1093 } /* end of case TELOPT_NAWS */
1096 case TELOPT_LINEMODE: {
1099 if (his_state_is_wont(TELOPT_LINEMODE)) /* Ignore if option disabled */
1102 * Process linemode suboptions.
1105 break; /* garbage was sent */
1106 request = SB_GET(); /* get will/wont */
1109 break; /* another garbage check */
1111 if (request == LM_SLC) { /* SLC is not preceeded by WILL or WONT */
1113 * Process suboption buffer of slc's
1116 do_opt_slc(subpointer, subend - subpointer);
1119 } else if (request == LM_MODE) {
1122 useeditmode = SB_GET(); /* get mode flag */
1123 clientstat(LM_MODE, 0, 0);
1129 switch (SB_GET()) { /* what suboption? */
1130 case LM_FORWARDMASK:
1132 * According to spec, only server can send request for
1133 * forwardmask, and client can only return a positive response.
1134 * So don't worry about it.
1141 } /* end of case TELOPT_LINEMODE */
1143 case TELOPT_STATUS: {
1151 if (my_state_is_will(TELOPT_STATUS))
1162 } /* end of case TELOPT_STATUS */
1164 case TELOPT_XDISPLOC: {
1165 if (SB_EOF() || SB_GET() != TELQUAL_IS)
1167 settimer(xdisplocsubopt);
1168 subpointer[SB_LEN()] = '\0';
1169 (void)setenv("DISPLAY", (char *)subpointer, 1);
1171 } /* end of case TELOPT_XDISPLOC */
1173 #ifdef TELOPT_NEW_ENVIRON
1174 case TELOPT_NEW_ENVIRON:
1176 case TELOPT_OLD_ENVIRON: {
1178 char *cp, *varp, *valp;
1183 if (c == TELQUAL_IS) {
1184 if (subchar == TELOPT_OLD_ENVIRON)
1185 settimer(oenvironsubopt);
1187 settimer(environsubopt);
1188 } else if (c != TELQUAL_INFO) {
1192 #ifdef TELOPT_NEW_ENVIRON
1193 if (subchar == TELOPT_NEW_ENVIRON) {
1196 if ((c == NEW_ENV_VAR) || (c == ENV_USERVAR))
1204 * We only want to do this if we haven't already decided
1205 * whether or not the other side has its VALUE and VAR
1209 int last = -1; /* invalid value */
1211 int got_var = 0, got_value = 0, got_uservar = 0;
1214 * The other side might have its VALUE and VAR values
1215 * reversed. To be interoperable, we need to determine
1216 * which way it is. If the first recognized character
1217 * is a VAR or VALUE, then that will tell us what
1218 * type of client it is. If the fist recognized
1219 * character is a USERVAR, then we continue scanning
1220 * the suboption looking for two consecutive
1221 * VAR or VALUE fields. We should not get two
1222 * consecutive VALUE fields, so finding two
1223 * consecutive VALUE or VAR fields will tell us
1224 * what the client is.
1231 if (last < 0 || last == OLD_ENV_VAR
1232 || (empty && (last == OLD_ENV_VALUE)))
1238 if (last < 0 || last == OLD_ENV_VALUE
1239 || (empty && (last == OLD_ENV_VAR)))
1240 goto env_ovar_wrong;
1242 last = OLD_ENV_VALUE;
1245 /* count strings of USERVAR as one */
1246 if (last != ENV_USERVAR)
1249 if (last == OLD_ENV_VALUE)
1251 if (last == OLD_ENV_VAR)
1252 goto env_ovar_wrong;
1267 if (last == OLD_ENV_VALUE)
1269 if (last == OLD_ENV_VAR)
1270 goto env_ovar_wrong;
1273 * Ok, the first thing was a USERVAR, and there
1274 * are not two consecutive VAR or VALUE commands,
1275 * and none of the VAR or VALUE commands are empty.
1276 * If the client has sent us a well-formed option,
1277 * then the number of VALUEs received should always
1278 * be less than or equal to the number of VARs and
1279 * USERVARs received.
1281 * If we got exactly as many VALUEs as VARs and
1282 * USERVARs, the client has the same definitions.
1284 * If we got exactly as many VARs as VALUEs and
1285 * USERVARS, the client has reversed definitions.
1287 if (got_uservar + got_var == got_value) {
1289 env_ovar = OLD_ENV_VAR;
1290 env_ovalue = OLD_ENV_VALUE;
1291 } else if (got_uservar + got_value == got_var) {
1293 env_ovar = OLD_ENV_VALUE;
1294 env_ovalue = OLD_ENV_VAR;
1296 output_data("ENVIRON VALUE and VAR are reversed!\r\n"));
1305 if ((c == env_ovar) || (c == ENV_USERVAR))
1313 cp = varp = (char *)subpointer;
1318 if (subchar == TELOPT_OLD_ENVIRON) {
1321 else if (c == env_ovalue)
1328 cp = valp = (char *)subpointer;
1335 (void)setenv(varp, valp, 1);
1338 cp = varp = (char *)subpointer;
1354 (void)setenv(varp, valp, 1);
1358 } /* end of case TELOPT_NEW_ENVIRON */
1362 } /* end of switch */
1364 } /* end of suboption */
1369 clientstat(TELOPT_LINEMODE, WILL, 0);
1372 #define ADD(c) *ncp++ = c
1373 #define ADD_DATA(c) { *ncp++ = c; if (c == SE || c == IAC) *ncp++ = c; }
1377 unsigned char statusbuf[256];
1383 netflush(); /* get rid of anything waiting to go out */
1391 * We check the want_state rather than the current state,
1392 * because if we received a DO/WILL for an option that we
1393 * don't support, and the other side didn't send a DONT/WONT
1394 * in response to our WONT/DONT, then the "state" will be
1395 * WILL/DO, and the "want_state" will be WONT/DONT. We
1396 * need to go by the latter.
1398 for (i = 0; i < (unsigned char)NTELOPTS; i++) {
1399 if (my_want_state_is_will(i)) {
1405 if (his_want_state_is_will(i)) {
1413 if (his_want_state_is_will(TELOPT_LFLOW)) {
1423 if (restartany >= 0) {
1427 ADD(LFLOW_RESTART_ANY);
1429 ADD(LFLOW_RESTART_XON);
1436 if (his_want_state_is_will(TELOPT_LINEMODE)) {
1437 unsigned char *cp, *cpe;
1441 ADD(TELOPT_LINEMODE);
1447 ADD(TELOPT_LINEMODE);
1452 for (cpe = cp + len; cp < cpe; cp++)
1456 #endif /* LINEMODE */
1461 output_datalen(statusbuf, ncp - statusbuf);
1462 netflush(); /* Send it on its way */
1465 {printsub('>', statusbuf, ncp - statusbuf); netflush();});
1469 * This function appends data to nfrontp and advances nfrontp.
1470 * Returns the number of characters written altogether (the
1471 * buffer may have been flushed in the process).
1475 output_data(const char *format, ...)
1481 va_start(args, format);
1482 if ((len = vasprintf(&buf, format, args)) == -1)
1484 output_datalen(buf, len);
1491 output_datalen(const char *buf, int len)
1493 int remaining, copied;
1495 remaining = BUFSIZ - (nfrontp - netobuf);
1497 /* Free up enough space if the room is too low*/
1498 if ((len > BUFSIZ ? BUFSIZ : len) > remaining) {
1500 remaining = BUFSIZ - (nfrontp - netobuf);
1503 /* Copy out as much as will fit */
1504 copied = remaining > len ? len : remaining;
1505 memmove(nfrontp, buf, copied);
1508 remaining -= copied;