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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 static const char sccsid[] = "@(#)state.c 8.5 (Berkeley) 5/30/95";
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
41 #include <libtelnet/auth.h>
44 #include <libtelnet/encrypt.h>
47 unsigned char doopt[] = { IAC, DO, '%', 'c', 0 };
48 unsigned char dont[] = { IAC, DONT, '%', 'c', 0 };
49 unsigned char will[] = { IAC, WILL, '%', 'c', 0 };
50 unsigned char wont[] = { IAC, WONT, '%', 'c', 0 };
54 * Buffer for sub-options, and macros
55 * for suboptions buffer manipulations
57 unsigned char subbuffer[512], *subpointer= subbuffer, *subend= subbuffer;
59 #define SB_CLEAR() subpointer = subbuffer
60 #define SB_TERM() { subend = subpointer; SB_CLEAR(); }
61 #define SB_ACCUM(c) if (subpointer < (subbuffer+sizeof subbuffer)) { \
62 *subpointer++ = (c); \
64 #define SB_GET() ((*subpointer++)&0xff)
65 #define SB_EOF() (subpointer >= subend)
66 #define SB_LEN() (subend - subpointer)
69 unsigned char *subsave;
70 #define SB_SAVE() subsave = subpointer;
71 #define SB_RESTORE() subpointer = subsave;
78 #define TS_DATA 0 /* base state */
79 #define TS_IAC 1 /* look for double IAC's */
80 #define TS_CR 2 /* CR-LF ->'s CR */
81 #define TS_SB 3 /* throw away begin's... */
82 #define TS_SE 4 /* ...end's (suboption negotiation) */
83 #define TS_WILL 5 /* will option negotiation */
84 #define TS_WONT 6 /* wont " */
85 #define TS_DO 7 /* do " */
86 #define TS_DONT 8 /* dont " */
88 static void doclientstat(void);
94 static int state = TS_DATA;
97 if ((&ptyobuf[BUFSIZ] - pfrontp) < 2)
99 c = *netip++ & 0377, ncc--;
102 c = (*decrypt_input)(c);
103 #endif /* ENCRYPTION */
108 /* Strip off \n or \0 after a \r */
109 if ((c == 0) || (c == '\n')) {
120 * We now map \r\n ==> \r for pragmatic reasons.
121 * Many client implementations send \r\n when
122 * the user hits the CarriageReturn key.
124 * We USED to map \r\n ==> \n, since \r\n says
125 * that we want to be in column 1 of the next
126 * printable line, and \n is the standard
127 * unix way of saying that (\r is only good
128 * if CRMOD is set, which it normally is).
130 if ((c == '\r') && his_state_is_wont(TELOPT_BINARY)) {
134 nc = (*decrypt_input)(nc & 0xff);
135 #endif /* ENCRYPTION */
138 * If we are operating in linemode,
139 * convert to local end-of-line.
141 if (linemode && (ncc > 0) && (('\n' == nc) ||
142 ((0 == nc) && tty_iscrnl())) ) {
150 (void)(*decrypt_input)(-1);
151 #endif /* ENCRYPTION */
162 * Send the process on the pty side an
163 * interrupt. Do this with a NULL or
164 * interrupt char; depending on the tty mode.
168 printoption("td: recv IAC", c));
174 printoption("td: recv IAC", c));
183 printoption("td: recv IAC", c));
193 printoption("td: recv IAC", c));
194 ptyflush(); /* half-hearted */
197 if (slctab[SLC_AO].sptr &&
198 *slctab[SLC_AO].sptr != (cc_t)(_POSIX_VDISABLE)) {
200 (unsigned char)*slctab[SLC_AO].sptr;
203 netclear(); /* clear buffer back */
204 output_data("%c%c", IAC, DM);
205 neturg = nfrontp-1; /* off by one XXX */
207 printoption("td: send IAC", DM));
212 * Erase Character and
221 printoption("td: recv IAC", c));
222 ptyflush(); /* half-hearted */
225 ch = *slctab[SLC_EC].sptr;
227 ch = *slctab[SLC_EL].sptr;
228 if (ch != (cc_t)(_POSIX_VDISABLE))
229 *pfrontp++ = (unsigned char)ch;
234 * Check for urgent data...
238 printoption("td: recv IAC", c));
239 SYNCHing = stilloob(net);
245 * Begin option subnegotiation...
268 if (his_state_is_will(TELOPT_EOR))
273 * Handle RFC 10xx Telnet linemode option additions
274 * to command stream (EOF, SUSP, ABORT).
307 * bad form of suboption negotiation.
308 * handle it in such a way as to avoid
309 * damage to local state. Parse
310 * suboption buffer found so far,
311 * then treat remaining stream as
312 * another command sequence.
315 /* for DIAGNOSTICS */
328 /* for DIAGNOSTICS */
334 suboption(); /* handle sub-option */
360 syslog(LOG_ERR, "panic state=%d", state);
361 printf("telnetd: panic state=%d\n", state);
365 } /* end of telrcv */
368 * The will/wont/do/dont state machines are based on Dave Borman's
369 * Telnet option processing state machine.
371 * These correspond to the following states:
372 * my_state = the last negotiated state
373 * want_state = what I want the state to go to
374 * want_resp = how many requests I have sent
375 * All state defaults are negative, and resp defaults to 0.
377 * When initiating a request to change state to new_state:
379 * if ((want_resp == 0 && new_state == my_state) || want_state == new_state) {
382 * want_state = new_state;
387 * When receiving new_state:
391 * if (want_resp && (new_state == my_state))
394 * if ((want_resp == 0) && (new_state != want_state)) {
395 * if (ok_to_switch_to new_state)
396 * want_state = new_state;
401 * my_state = new_state;
403 * Note that new_state is implied in these functions by the function itself.
404 * will and do imply positive new_state, wont and dont imply negative.
406 * Finally, there is one catch. If we send a negative response to a
407 * positive request, my_state will be the positive while want_state will
408 * remain negative. my_state will revert to negative when the negative
409 * acknowlegment arrives from the peer. Thus, my_state generally tells
410 * us not only the last negotiated state, but also tells us what the peer
411 * wants to be doing as well. It is important to understand this difference
412 * as we may wish to be processing data streams based on our desired state
413 * (want_state) or based on what the peer thinks the state is (my_state).
415 * This all works fine because if the peer sends a positive request, the data
416 * that we receive prior to negative acknowlegment will probably be affected
417 * by the positive state, and we can process it as such (if we can; if we
418 * can't then it really doesn't matter). If it is that important, then the
419 * peer probably should be buffering until this option state negotiation
424 send_do(int option, int init)
427 if ((do_dont_resp[option] == 0 && his_state_is_will(option)) ||
428 his_want_state_is_will(option))
431 * Special case for TELOPT_TM: We send a DO, but pretend
432 * that we sent a DONT, so that we can send more DOs if
435 if (option == TELOPT_TM)
436 set_his_want_state_wont(option);
438 set_his_want_state_will(option);
439 do_dont_resp[option]++;
441 output_data((const char *)doopt, option);
443 DIAG(TD_OPTIONS, printoption("td: send do", option));
447 willoption(int option)
450 void (*func)(void) = 0;
453 * process input from peer.
456 DIAG(TD_OPTIONS, printoption("td: recv will", option));
458 if (do_dont_resp[option]) {
459 do_dont_resp[option]--;
460 if (do_dont_resp[option] && his_state_is_will(option))
461 do_dont_resp[option]--;
463 if (do_dont_resp[option] == 0) {
464 if (his_want_state_is_wont(option)) {
476 * See comments below for more info.
478 not42 = 0; /* looks like a 4.2 system */
482 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
484 * This telnetd implementation does not really
485 * support timing marks, it just uses them to
486 * support the kludge linemode stuff. If we
487 * receive a will or wont TM in response to our
488 * do TM request that may have been sent to
489 * determine kludge linemode support, process
490 * it, otherwise TM should get a negative
494 * Handle the linemode kludge stuff.
495 * If we are not currently supporting any
496 * linemode at all, then we assume that this
497 * is the client telling us to use kludge
498 * linemode in response to our query. Set the
499 * linemode type that is to be supported, note
500 * that the client wishes to use linemode, and
501 * eat the will TM as though it never arrived.
503 if (lmodetype < KLUDGE_LINEMODE) {
504 lmodetype = KLUDGE_LINEMODE;
505 clientstat(TELOPT_LINEMODE, WILL, 0);
506 send_wont(TELOPT_SGA, 1);
507 } else if (lmodetype == NO_AUTOKLUDGE) {
508 lmodetype = KLUDGE_OK;
510 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
512 * We never respond to a WILL TM, and
513 * we leave the state WONT.
519 * If we are going to support flow control
520 * option, then don't worry peer that we can't
521 * change the flow control characters.
523 slctab[SLC_XON].defset.flag &= ~SLC_LEVELBITS;
524 slctab[SLC_XON].defset.flag |= SLC_DEFAULT;
525 slctab[SLC_XOFF].defset.flag &= ~SLC_LEVELBITS;
526 slctab[SLC_XOFF].defset.flag |= SLC_DEFAULT;
531 case TELOPT_XDISPLOC:
532 case TELOPT_NEW_ENVIRON:
533 case TELOPT_OLD_ENVIRON:
538 case TELOPT_LINEMODE:
539 # ifdef KLUDGELINEMODE
541 * Note client's desire to use linemode.
543 lmodetype = REAL_LINEMODE;
544 # endif /* KLUDGELINEMODE */
548 #endif /* LINEMODE */
550 #ifdef AUTHENTICATION
551 case TELOPT_AUTHENTICATION:
552 if (auth_level >= 0) {
561 func = encrypt_send_support;
564 #endif /* ENCRYPTION */
570 set_his_want_state_will(option);
573 do_dont_resp[option]++;
574 send_dont(option, 0);
578 * Option processing that should happen when
579 * we receive conformation of a change in
580 * state that we had requested.
584 not42 = 0; /* looks like a 4.2 system */
586 * Egads, he responded "WILL ECHO". Turn
589 send_dont(option, 1);
591 * "WILL ECHO". Kludge upon kludge!
592 * A 4.2 client is now echoing user input at
593 * the tty. This is probably undesireable and
594 * it should be stopped. The client will
595 * respond WONT TM to the DO TM that we send to
596 * check for kludge linemode. When the WONT TM
597 * arrives, linemode will be turned off and a
598 * change propogated to the pty. This change
599 * will cause us to process the new pty state
600 * in localstat(), which will notice that
601 * linemode is off and send a WILL ECHO
602 * so that we are properly in character mode and
607 case TELOPT_LINEMODE:
608 # ifdef KLUDGELINEMODE
610 * Note client's desire to use linemode.
612 lmodetype = REAL_LINEMODE;
613 # endif /* KLUDGELINEMODE */
616 #endif /* LINEMODE */
618 #ifdef AUTHENTICATION
619 case TELOPT_AUTHENTICATION:
626 func = encrypt_send_support;
628 #endif /* ENCRYPTION */
635 set_his_state_will(option);
638 } /* end of willoption */
641 send_dont(int option, int init)
644 if ((do_dont_resp[option] == 0 && his_state_is_wont(option)) ||
645 his_want_state_is_wont(option))
647 set_his_want_state_wont(option);
648 do_dont_resp[option]++;
650 output_data((const char *)dont, option);
652 DIAG(TD_OPTIONS, printoption("td: send dont", option));
656 wontoption(int option)
659 * Process client input.
662 DIAG(TD_OPTIONS, printoption("td: recv wont", option));
664 if (do_dont_resp[option]) {
665 do_dont_resp[option]--;
666 if (do_dont_resp[option] && his_state_is_wont(option))
667 do_dont_resp[option]--;
669 if (do_dont_resp[option] == 0) {
670 if (his_want_state_is_will(option)) {
671 /* it is always ok to change to negative state */
674 not42 = 1; /* doesn't seem to be a 4.2 system */
684 case TELOPT_LINEMODE:
685 # ifdef KLUDGELINEMODE
687 * If real linemode is supported, then client is
688 * asking to turn linemode off.
690 if (lmodetype != REAL_LINEMODE)
692 lmodetype = KLUDGE_LINEMODE;
693 # endif /* KLUDGELINEMODE */
694 clientstat(TELOPT_LINEMODE, WONT, 0);
696 #endif /* LINEMODE */
700 * If we get a WONT TM, and had sent a DO TM,
701 * don't respond with a DONT TM, just leave it
702 * as is. Short circut the state machine to
705 set_his_want_state_wont(TELOPT_TM);
710 * If we are not going to support flow control
711 * option, then let peer know that we can't
712 * change the flow control characters.
714 slctab[SLC_XON].defset.flag &= ~SLC_LEVELBITS;
715 slctab[SLC_XON].defset.flag |= SLC_CANTCHANGE;
716 slctab[SLC_XOFF].defset.flag &= ~SLC_LEVELBITS;
717 slctab[SLC_XOFF].defset.flag |= SLC_CANTCHANGE;
720 #ifdef AUTHENTICATION
721 case TELOPT_AUTHENTICATION:
722 auth_finished(0, AUTH_REJECT);
727 * For options that we might spin waiting for
728 * sub-negotiation, if the client turns off the
729 * option rather than responding to the request,
730 * we have to treat it here as if we got a response
731 * to the sub-negotiation, (by updating the timers)
732 * so that we'll break out of the loop.
735 settimer(ttypesubopt);
739 settimer(tspeedsubopt);
742 case TELOPT_XDISPLOC:
743 settimer(xdisplocsubopt);
746 case TELOPT_OLD_ENVIRON:
747 settimer(oenvironsubopt);
750 case TELOPT_NEW_ENVIRON:
751 settimer(environsubopt);
757 set_his_want_state_wont(option);
758 if (his_state_is_will(option))
759 send_dont(option, 0);
763 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
764 if (lmodetype < NO_AUTOKLUDGE) {
765 lmodetype = NO_LINEMODE;
766 clientstat(TELOPT_LINEMODE, WONT, 0);
767 send_will(TELOPT_SGA, 1);
768 send_will(TELOPT_ECHO, 1);
770 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
773 #ifdef AUTHENTICATION
774 case TELOPT_AUTHENTICATION:
775 auth_finished(0, AUTH_REJECT);
783 set_his_state_wont(option);
785 } /* end of wontoption */
788 send_will(int option, int init)
791 if ((will_wont_resp[option] == 0 && my_state_is_will(option))||
792 my_want_state_is_will(option))
794 set_my_want_state_will(option);
795 will_wont_resp[option]++;
797 output_data((const char *)will, option);
799 DIAG(TD_OPTIONS, printoption("td: send will", option));
802 #if !defined(LINEMODE) || !defined(KLUDGELINEMODE)
804 * When we get a DONT SGA, we will try once to turn it
805 * back on. If the other side responds DONT SGA, we
806 * leave it at that. This is so that when we talk to
807 * clients that understand KLUDGELINEMODE but not LINEMODE,
808 * we'll keep them in char-at-a-time mode.
819 * Process client input.
822 DIAG(TD_OPTIONS, printoption("td: recv do", option));
824 if (will_wont_resp[option]) {
825 will_wont_resp[option]--;
826 if (will_wont_resp[option] && my_state_is_will(option))
827 will_wont_resp[option]--;
829 if ((will_wont_resp[option] == 0) && (my_want_state_is_wont(option))) {
833 # ifdef KLUDGELINEMODE
834 if (lmodetype == NO_LINEMODE)
836 if (his_state_is_wont(TELOPT_LINEMODE))
855 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
857 * If kludge linemode is in use, then we must
858 * process an incoming do SGA for linemode
861 if (lmodetype == KLUDGE_LINEMODE) {
863 * Receipt of "do SGA" in kludge
864 * linemode is the peer asking us to
865 * turn off linemode. Make note of
868 clientstat(TELOPT_LINEMODE, WONT, 0);
870 * If linemode did not get turned off
871 * then don't tell peer that we did.
872 * Breaking here forces a wont SGA to
880 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
890 * Special case for TM. We send a WILL, but
891 * pretend we sent a WONT.
893 send_will(option, 0);
894 set_my_want_state_wont(option);
895 set_my_state_wont(option);
900 * When we get a LOGOUT option, respond
901 * with a WILL LOGOUT, make sure that
902 * it gets written out to the network,
903 * and then just go away...
905 set_my_want_state_will(TELOPT_LOGOUT);
906 send_will(TELOPT_LOGOUT, 0);
907 set_my_state_will(TELOPT_LOGOUT);
917 #endif /* ENCRYPTION */
918 case TELOPT_LINEMODE:
923 case TELOPT_XDISPLOC:
924 #ifdef TELOPT_ENVIRON
925 case TELOPT_NEW_ENVIRON:
927 case TELOPT_OLD_ENVIRON:
932 set_my_want_state_will(option);
933 send_will(option, 0);
935 will_wont_resp[option]++;
936 send_wont(option, 0);
939 set_my_state_will(option);
941 } /* end of dooption */
944 send_wont(int option, int init)
947 if ((will_wont_resp[option] == 0 && my_state_is_wont(option)) ||
948 my_want_state_is_wont(option))
950 set_my_want_state_wont(option);
951 will_wont_resp[option]++;
953 output_data((const char *)wont, option);
955 DIAG(TD_OPTIONS, printoption("td: send wont", option));
959 dontoption(int option)
962 * Process client input.
966 DIAG(TD_OPTIONS, printoption("td: recv dont", option));
968 if (will_wont_resp[option]) {
969 will_wont_resp[option]--;
970 if (will_wont_resp[option] && my_state_is_wont(option))
971 will_wont_resp[option]--;
973 if ((will_wont_resp[option] == 0) && (my_want_state_is_will(option))) {
981 case TELOPT_ECHO: /* we should stop echoing */
983 # ifdef KLUDGELINEMODE
984 if ((lmodetype != REAL_LINEMODE) &&
985 (lmodetype != KLUDGE_LINEMODE))
987 if (his_state_is_wont(TELOPT_LINEMODE))
998 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
1000 * If kludge linemode is in use, then we
1001 * must process an incoming do SGA for
1002 * linemode purposes.
1004 if ((lmodetype == KLUDGE_LINEMODE) ||
1005 (lmodetype == KLUDGE_OK)) {
1007 * The client is asking us to turn
1010 lmodetype = KLUDGE_LINEMODE;
1011 clientstat(TELOPT_LINEMODE, WILL, 0);
1013 * If we did not turn line mode on,
1014 * then what do we say? Will SGA?
1015 * This violates design of telnet.
1016 * Gross. Very Gross.
1021 set_my_want_state_wont(option);
1022 if (my_state_is_will(option))
1023 send_wont(option, 0);
1024 set_my_state_wont(option);
1025 if (turn_on_sga ^= 1)
1026 send_will(option, 1);
1028 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
1034 set_my_want_state_wont(option);
1035 if (my_state_is_will(option))
1036 send_wont(option, 0);
1038 set_my_state_wont(option);
1040 } /* end of dontoption */
1044 int env_ovalue = -1;
1045 #else /* ENV_HACK */
1046 # define env_ovar OLD_ENV_VAR
1047 # define env_ovalue OLD_ENV_VALUE
1048 #endif /* ENV_HACK */
1053 * Look at the sub-option buffer, and try to be helpful to the other
1056 * Currently we recognize:
1068 DIAG(TD_OPTIONS, {netflush(); printsub('<', subpointer, SB_LEN()+2);});
1072 case TELOPT_TSPEED: {
1075 if (his_state_is_wont(TELOPT_TSPEED)) /* Ignore if option disabled */
1078 settimer(tspeedsubopt);
1080 if (SB_EOF() || SB_GET() != TELQUAL_IS)
1083 xspeed = atoi((char *)subpointer);
1085 while (SB_GET() != ',' && !SB_EOF());
1089 rspeed = atoi((char *)subpointer);
1090 clientstat(TELOPT_TSPEED, xspeed, rspeed);
1094 } /* end of case TELOPT_TSPEED */
1096 case TELOPT_TTYPE: { /* Yaaaay! */
1097 static char terminalname[41];
1099 if (his_state_is_wont(TELOPT_TTYPE)) /* Ignore if option disabled */
1101 settimer(ttypesubopt);
1103 if (SB_EOF() || SB_GET() != TELQUAL_IS) {
1104 return; /* ??? XXX but, this is the most robust */
1107 terminaltype = terminalname;
1109 while ((terminaltype < (terminalname + sizeof terminalname-1)) &&
1117 *terminaltype++ = c; /* accumulate name */
1120 terminaltype = terminalname;
1122 } /* end of case TELOPT_TTYPE */
1125 int xwinsize, ywinsize;
1127 if (his_state_is_wont(TELOPT_NAWS)) /* Ignore if option disabled */
1132 xwinsize = SB_GET() << 8;
1135 xwinsize |= SB_GET();
1138 ywinsize = SB_GET() << 8;
1141 ywinsize |= SB_GET();
1142 clientstat(TELOPT_NAWS, xwinsize, ywinsize);
1146 } /* end of case TELOPT_NAWS */
1149 case TELOPT_LINEMODE: {
1152 if (his_state_is_wont(TELOPT_LINEMODE)) /* Ignore if option disabled */
1155 * Process linemode suboptions.
1158 break; /* garbage was sent */
1159 request = SB_GET(); /* get will/wont */
1162 break; /* another garbage check */
1164 if (request == LM_SLC) { /* SLC is not preceeded by WILL or WONT */
1166 * Process suboption buffer of slc's
1169 do_opt_slc(subpointer, subend - subpointer);
1172 } else if (request == LM_MODE) {
1175 useeditmode = SB_GET(); /* get mode flag */
1176 clientstat(LM_MODE, 0, 0);
1182 switch (SB_GET()) { /* what suboption? */
1183 case LM_FORWARDMASK:
1185 * According to spec, only server can send request for
1186 * forwardmask, and client can only return a positive response.
1187 * So don't worry about it.
1194 } /* end of case TELOPT_LINEMODE */
1196 case TELOPT_STATUS: {
1204 if (my_state_is_will(TELOPT_STATUS))
1215 } /* end of case TELOPT_STATUS */
1217 case TELOPT_XDISPLOC: {
1218 if (SB_EOF() || SB_GET() != TELQUAL_IS)
1220 settimer(xdisplocsubopt);
1221 subpointer[SB_LEN()] = '\0';
1222 (void)setenv("DISPLAY", (char *)subpointer, 1);
1224 } /* end of case TELOPT_XDISPLOC */
1226 #ifdef TELOPT_NEW_ENVIRON
1227 case TELOPT_NEW_ENVIRON:
1229 case TELOPT_OLD_ENVIRON: {
1231 char *cp, *varp, *valp;
1236 if (c == TELQUAL_IS) {
1237 if (subchar == TELOPT_OLD_ENVIRON)
1238 settimer(oenvironsubopt);
1240 settimer(environsubopt);
1241 } else if (c != TELQUAL_INFO) {
1245 #ifdef TELOPT_NEW_ENVIRON
1246 if (subchar == TELOPT_NEW_ENVIRON) {
1249 if ((c == NEW_ENV_VAR) || (c == ENV_USERVAR))
1257 * We only want to do this if we haven't already decided
1258 * whether or not the other side has its VALUE and VAR
1262 int last = -1; /* invalid value */
1264 int got_var = 0, got_value = 0, got_uservar = 0;
1267 * The other side might have its VALUE and VAR values
1268 * reversed. To be interoperable, we need to determine
1269 * which way it is. If the first recognized character
1270 * is a VAR or VALUE, then that will tell us what
1271 * type of client it is. If the fist recognized
1272 * character is a USERVAR, then we continue scanning
1273 * the suboption looking for two consecutive
1274 * VAR or VALUE fields. We should not get two
1275 * consecutive VALUE fields, so finding two
1276 * consecutive VALUE or VAR fields will tell us
1277 * what the client is.
1284 if (last < 0 || last == OLD_ENV_VAR
1285 || (empty && (last == OLD_ENV_VALUE)))
1291 if (last < 0 || last == OLD_ENV_VALUE
1292 || (empty && (last == OLD_ENV_VAR)))
1293 goto env_ovar_wrong;
1295 last = OLD_ENV_VALUE;
1298 /* count strings of USERVAR as one */
1299 if (last != ENV_USERVAR)
1302 if (last == OLD_ENV_VALUE)
1304 if (last == OLD_ENV_VAR)
1305 goto env_ovar_wrong;
1320 if (last == OLD_ENV_VALUE)
1322 if (last == OLD_ENV_VAR)
1323 goto env_ovar_wrong;
1326 * Ok, the first thing was a USERVAR, and there
1327 * are not two consecutive VAR or VALUE commands,
1328 * and none of the VAR or VALUE commands are empty.
1329 * If the client has sent us a well-formed option,
1330 * then the number of VALUEs received should always
1331 * be less than or equal to the number of VARs and
1332 * USERVARs received.
1334 * If we got exactly as many VALUEs as VARs and
1335 * USERVARs, the client has the same definitions.
1337 * If we got exactly as many VARs as VALUEs and
1338 * USERVARS, the client has reversed definitions.
1340 if (got_uservar + got_var == got_value) {
1342 env_ovar = OLD_ENV_VAR;
1343 env_ovalue = OLD_ENV_VALUE;
1344 } else if (got_uservar + got_value == got_var) {
1346 env_ovar = OLD_ENV_VALUE;
1347 env_ovalue = OLD_ENV_VAR;
1349 output_data("ENVIRON VALUE and VAR are reversed!\r\n"));
1358 if ((c == env_ovar) || (c == ENV_USERVAR))
1366 cp = varp = (char *)subpointer;
1371 if (subchar == TELOPT_OLD_ENVIRON) {
1374 else if (c == env_ovalue)
1381 cp = valp = (char *)subpointer;
1388 (void)setenv(varp, valp, 1);
1391 cp = varp = (char *)subpointer;
1407 (void)setenv(varp, valp, 1);
1411 } /* end of case TELOPT_NEW_ENVIRON */
1412 #ifdef AUTHENTICATION
1413 case TELOPT_AUTHENTICATION:
1420 * These are sent by us and cannot be sent by
1425 auth_is(subpointer, SB_LEN());
1428 auth_name(subpointer, SB_LEN());
1434 case TELOPT_ENCRYPT:
1438 case ENCRYPT_SUPPORT:
1439 encrypt_support(subpointer, SB_LEN());
1442 encrypt_is(subpointer, SB_LEN());
1445 encrypt_reply(subpointer, SB_LEN());
1448 encrypt_start(subpointer, SB_LEN());
1453 case ENCRYPT_REQSTART:
1454 encrypt_request_start(subpointer, SB_LEN());
1456 case ENCRYPT_REQEND:
1458 * We can always send an REQEND so that we cannot
1459 * get stuck encrypting. We should only get this
1460 * if we have been able to get in the correct mode
1463 encrypt_request_end();
1465 case ENCRYPT_ENC_KEYID:
1466 encrypt_enc_keyid(subpointer, SB_LEN());
1468 case ENCRYPT_DEC_KEYID:
1469 encrypt_dec_keyid(subpointer, SB_LEN());
1475 #endif /* ENCRYPTION */
1479 } /* end of switch */
1481 } /* end of suboption */
1486 clientstat(TELOPT_LINEMODE, WILL, 0);
1489 #define ADD(c) *ncp++ = c
1490 #define ADD_DATA(c) { *ncp++ = c; if (c == SE || c == IAC) *ncp++ = c; }
1494 unsigned char statusbuf[256];
1500 netflush(); /* get rid of anything waiting to go out */
1508 * We check the want_state rather than the current state,
1509 * because if we received a DO/WILL for an option that we
1510 * don't support, and the other side didn't send a DONT/WONT
1511 * in response to our WONT/DONT, then the "state" will be
1512 * WILL/DO, and the "want_state" will be WONT/DONT. We
1513 * need to go by the latter.
1515 for (i = 0; i < (unsigned char)NTELOPTS; i++) {
1516 if (my_want_state_is_will(i)) {
1522 if (his_want_state_is_will(i)) {
1530 if (his_want_state_is_will(TELOPT_LFLOW)) {
1540 if (restartany >= 0) {
1544 ADD(LFLOW_RESTART_ANY);
1546 ADD(LFLOW_RESTART_XON);
1553 if (his_want_state_is_will(TELOPT_LINEMODE)) {
1554 unsigned char *cp, *cpe;
1558 ADD(TELOPT_LINEMODE);
1564 ADD(TELOPT_LINEMODE);
1569 for (cpe = cp + len; cp < cpe; cp++)
1573 #endif /* LINEMODE */
1578 output_datalen(statusbuf, ncp - statusbuf);
1579 netflush(); /* Send it on its way */
1582 {printsub('>', statusbuf, ncp - statusbuf); netflush();});
1586 * This function appends data to nfrontp and advances nfrontp.
1587 * Returns the number of characters written altogether (the
1588 * buffer may have been flushed in the process).
1592 output_data(const char *format, ...)
1598 va_start(args, format);
1599 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;