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
29 * @(#)state.c 8.5 (Berkeley) 5/30/95
30 * $FreeBSD: src/crypto/telnet/telnetd/state.c,v 1.4.2.3 2002/04/13 10:59:08 markm Exp $
36 #include <libtelnet/auth.h>
39 #include <libtelnet/encrypt.h>
42 static int envvarok(char *);
44 unsigned char doopt[] = { IAC, DO, '%', 'c', 0 };
45 unsigned char dont[] = { IAC, DONT, '%', 'c', 0 };
46 unsigned char will[] = { IAC, WILL, '%', 'c', 0 };
47 unsigned char wont[] = { IAC, WONT, '%', 'c', 0 };
51 * Buffer for sub-options, and macros
52 * for suboptions buffer manipulations
54 unsigned char subbuffer[512], *subpointer= subbuffer, *subend= subbuffer;
56 #define SB_CLEAR() subpointer = subbuffer
57 #define SB_TERM() { subend = subpointer; SB_CLEAR(); }
58 #define SB_ACCUM(c) if (subpointer < (subbuffer+sizeof subbuffer)) { \
59 *subpointer++ = (c); \
61 #define SB_GET() ((*subpointer++)&0xff)
62 #define SB_EOF() (subpointer >= subend)
63 #define SB_LEN() (subend - subpointer)
66 unsigned char *subsave;
67 #define SB_SAVE() subsave = subpointer;
68 #define SB_RESTORE() subpointer = subsave;
75 #define TS_DATA 0 /* base state */
76 #define TS_IAC 1 /* look for double IAC's */
77 #define TS_CR 2 /* CR-LF ->'s CR */
78 #define TS_SB 3 /* throw away begin's... */
79 #define TS_SE 4 /* ...end's (suboption negotiation) */
80 #define TS_WILL 5 /* will option negotiation */
81 #define TS_WONT 6 /* wont " */
82 #define TS_DO 7 /* do " */
83 #define TS_DONT 8 /* dont " */
85 static void doclientstat(void);
91 static int state = TS_DATA;
94 if ((&ptyobuf[BUFSIZ] - pfrontp) < 2)
96 c = *netip++ & 0377, ncc--;
99 c = (*decrypt_input)(c);
100 #endif /* ENCRYPTION */
105 /* Strip off \n or \0 after a \r */
106 if ((c == 0) || (c == '\n')) {
117 * We now map \r\n ==> \r for pragmatic reasons.
118 * Many client implementations send \r\n when
119 * the user hits the CarriageReturn key.
121 * We USED to map \r\n ==> \n, since \r\n says
122 * that we want to be in column 1 of the next
123 * printable line, and \n is the standard
124 * unix way of saying that (\r is only good
125 * if CRMOD is set, which it normally is).
127 if ((c == '\r') && his_state_is_wont(TELOPT_BINARY)) {
131 nc = (*decrypt_input)(nc & 0xff);
132 #endif /* ENCRYPTION */
135 * If we are operating in linemode,
136 * convert to local end-of-line.
138 if (linemode && (ncc > 0) && (('\n' == nc) ||
139 ((0 == nc) && tty_iscrnl())) ) {
147 (void)(*decrypt_input)(-1);
148 #endif /* ENCRYPTION */
159 * Send the process on the pty side an
160 * interrupt. Do this with a NULL or
161 * interrupt char; depending on the tty mode.
165 printoption("td: recv IAC", c));
171 printoption("td: recv IAC", c));
180 printoption("td: recv IAC", c));
190 printoption("td: recv IAC", c));
191 ptyflush(); /* half-hearted */
194 if (slctab[SLC_AO].sptr &&
195 *slctab[SLC_AO].sptr != (cc_t)(_POSIX_VDISABLE)) {
197 (unsigned char)*slctab[SLC_AO].sptr;
200 netclear(); /* clear buffer back */
201 output_data("%c%c", IAC, DM);
202 neturg = nfrontp-1; /* off by one XXX */
204 printoption("td: send IAC", DM));
209 * Erase Character and
218 printoption("td: recv IAC", c));
219 ptyflush(); /* half-hearted */
222 ch = *slctab[SLC_EC].sptr;
224 ch = *slctab[SLC_EL].sptr;
225 if (ch != (cc_t)(_POSIX_VDISABLE))
226 *pfrontp++ = (unsigned char)ch;
231 * Check for urgent data...
235 printoption("td: recv IAC", c));
236 SYNCHing = stilloob(net);
242 * Begin option subnegotiation...
265 if (his_state_is_will(TELOPT_EOR))
270 * Handle RFC 10xx Telnet linemode option additions
271 * to command stream (EOF, SUSP, ABORT).
304 * bad form of suboption negotiation.
305 * handle it in such a way as to avoid
306 * damage to local state. Parse
307 * suboption buffer found so far,
308 * then treat remaining stream as
309 * another command sequence.
312 /* for DIAGNOSTICS */
325 /* for DIAGNOSTICS */
331 suboption(); /* handle sub-option */
357 syslog(LOG_ERR, "panic state=%d", state);
358 printf("telnetd: panic state=%d\n", state);
362 } /* end of telrcv */
365 * The will/wont/do/dont state machines are based on Dave Borman's
366 * Telnet option processing state machine.
368 * These correspond to the following states:
369 * my_state = the last negotiated state
370 * want_state = what I want the state to go to
371 * want_resp = how many requests I have sent
372 * All state defaults are negative, and resp defaults to 0.
374 * When initiating a request to change state to new_state:
376 * if ((want_resp == 0 && new_state == my_state) || want_state == new_state) {
379 * want_state = new_state;
384 * When receiving new_state:
388 * if (want_resp && (new_state == my_state))
391 * if ((want_resp == 0) && (new_state != want_state)) {
392 * if (ok_to_switch_to new_state)
393 * want_state = new_state;
398 * my_state = new_state;
400 * Note that new_state is implied in these functions by the function itself.
401 * will and do imply positive new_state, wont and dont imply negative.
403 * Finally, there is one catch. If we send a negative response to a
404 * positive request, my_state will be the positive while want_state will
405 * remain negative. my_state will revert to negative when the negative
406 * acknowlegment arrives from the peer. Thus, my_state generally tells
407 * us not only the last negotiated state, but also tells us what the peer
408 * wants to be doing as well. It is important to understand this difference
409 * as we may wish to be processing data streams based on our desired state
410 * (want_state) or based on what the peer thinks the state is (my_state).
412 * This all works fine because if the peer sends a positive request, the data
413 * that we receive prior to negative acknowlegment will probably be affected
414 * by the positive state, and we can process it as such (if we can; if we
415 * can't then it really doesn't matter). If it is that important, then the
416 * peer probably should be buffering until this option state negotiation
421 send_do(int option, int init)
424 if ((do_dont_resp[option] == 0 && his_state_is_will(option)) ||
425 his_want_state_is_will(option))
428 * Special case for TELOPT_TM: We send a DO, but pretend
429 * that we sent a DONT, so that we can send more DOs if
432 if (option == TELOPT_TM)
433 set_his_want_state_wont(option);
435 set_his_want_state_will(option);
436 do_dont_resp[option]++;
438 output_data((const char *)doopt, option);
440 DIAG(TD_OPTIONS, printoption("td: send do", option));
444 willoption(int option)
447 void (*func)(void) = NULL;
450 * process input from peer.
453 DIAG(TD_OPTIONS, printoption("td: recv will", option));
455 if (do_dont_resp[option]) {
456 do_dont_resp[option]--;
457 if (do_dont_resp[option] && his_state_is_will(option))
458 do_dont_resp[option]--;
460 if (do_dont_resp[option] == 0) {
461 if (his_want_state_is_wont(option)) {
473 * See comments below for more info.
475 not42 = 0; /* looks like a 4.2 system */
479 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
481 * This telnetd implementation does not really
482 * support timing marks, it just uses them to
483 * support the kludge linemode stuff. If we
484 * receive a will or wont TM in response to our
485 * do TM request that may have been sent to
486 * determine kludge linemode support, process
487 * it, otherwise TM should get a negative
491 * Handle the linemode kludge stuff.
492 * If we are not currently supporting any
493 * linemode at all, then we assume that this
494 * is the client telling us to use kludge
495 * linemode in response to our query. Set the
496 * linemode type that is to be supported, note
497 * that the client wishes to use linemode, and
498 * eat the will TM as though it never arrived.
500 if (lmodetype < KLUDGE_LINEMODE) {
501 lmodetype = KLUDGE_LINEMODE;
502 clientstat(TELOPT_LINEMODE, WILL, 0);
503 send_wont(TELOPT_SGA, 1);
504 } else if (lmodetype == NO_AUTOKLUDGE) {
505 lmodetype = KLUDGE_OK;
507 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
509 * We never respond to a WILL TM, and
510 * we leave the state WONT.
516 * If we are going to support flow control
517 * option, then don't worry peer that we can't
518 * change the flow control characters.
520 slctab[SLC_XON].defset.flag &= ~SLC_LEVELBITS;
521 slctab[SLC_XON].defset.flag |= SLC_DEFAULT;
522 slctab[SLC_XOFF].defset.flag &= ~SLC_LEVELBITS;
523 slctab[SLC_XOFF].defset.flag |= SLC_DEFAULT;
529 case TELOPT_XDISPLOC:
530 case TELOPT_NEW_ENVIRON:
531 case TELOPT_OLD_ENVIRON:
536 case TELOPT_LINEMODE:
537 # ifdef KLUDGELINEMODE
539 * Note client's desire to use linemode.
541 lmodetype = REAL_LINEMODE;
542 # endif /* KLUDGELINEMODE */
546 #endif /* LINEMODE */
548 #ifdef AUTHENTICATION
549 case TELOPT_AUTHENTICATION:
557 func = encrypt_send_support;
560 #endif /* ENCRYPTION */
566 set_his_want_state_will(option);
569 do_dont_resp[option]++;
570 send_dont(option, 0);
574 * Option processing that should happen when
575 * we receive conformation of a change in
576 * state that we had requested.
580 not42 = 0; /* looks like a 4.2 system */
582 * Egads, he responded "WILL ECHO". Turn
585 send_dont(option, 1);
587 * "WILL ECHO". Kludge upon kludge!
588 * A 4.2 client is now echoing user input at
589 * the tty. This is probably undesireable and
590 * it should be stopped. The client will
591 * respond WONT TM to the DO TM that we send to
592 * check for kludge linemode. When the WONT TM
593 * arrives, linemode will be turned off and a
594 * change propogated to the pty. This change
595 * will cause us to process the new pty state
596 * in localstat(), which will notice that
597 * linemode is off and send a WILL ECHO
598 * so that we are properly in character mode and
603 case TELOPT_LINEMODE:
604 # ifdef KLUDGELINEMODE
606 * Note client's desire to use linemode.
608 lmodetype = REAL_LINEMODE;
609 # endif /* KLUDGELINEMODE */
612 #endif /* LINEMODE */
614 #ifdef AUTHENTICATION
615 case TELOPT_AUTHENTICATION:
622 func = encrypt_send_support;
624 #endif /* ENCRYPTION */
631 set_his_state_will(option);
634 } /* end of willoption */
637 send_dont(int option, int init)
640 if ((do_dont_resp[option] == 0 && his_state_is_wont(option)) ||
641 his_want_state_is_wont(option))
643 set_his_want_state_wont(option);
644 do_dont_resp[option]++;
646 output_data((const char *)dont, option);
648 DIAG(TD_OPTIONS, printoption("td: send dont", option));
652 wontoption(int option)
655 * Process client input.
658 DIAG(TD_OPTIONS, printoption("td: recv wont", option));
660 if (do_dont_resp[option]) {
661 do_dont_resp[option]--;
662 if (do_dont_resp[option] && his_state_is_wont(option))
663 do_dont_resp[option]--;
665 if (do_dont_resp[option] == 0) {
666 if (his_want_state_is_will(option)) {
667 /* it is always ok to change to negative state */
670 not42 = 1; /* doesn't seem to be a 4.2 system */
680 case TELOPT_LINEMODE:
681 # ifdef KLUDGELINEMODE
683 * If real linemode is supported, then client is
684 * asking to turn linemode off.
686 if (lmodetype != REAL_LINEMODE)
688 lmodetype = KLUDGE_LINEMODE;
689 # endif /* KLUDGELINEMODE */
690 clientstat(TELOPT_LINEMODE, WONT, 0);
692 #endif /* LINEMODE */
696 * If we get a WONT TM, and had sent a DO TM,
697 * don't respond with a DONT TM, just leave it
698 * as is. Short circut the state machine to
701 set_his_want_state_wont(TELOPT_TM);
706 * If we are not going to support flow control
707 * option, then let peer know that we can't
708 * change the flow control characters.
710 slctab[SLC_XON].defset.flag &= ~SLC_LEVELBITS;
711 slctab[SLC_XON].defset.flag |= SLC_CANTCHANGE;
712 slctab[SLC_XOFF].defset.flag &= ~SLC_LEVELBITS;
713 slctab[SLC_XOFF].defset.flag |= SLC_CANTCHANGE;
716 #ifdef AUTHENTICATION
717 case TELOPT_AUTHENTICATION:
718 auth_finished(0, AUTH_REJECT);
723 * For options that we might spin waiting for
724 * sub-negotiation, if the client turns off the
725 * option rather than responding to the request,
726 * we have to treat it here as if we got a response
727 * to the sub-negotiation, (by updating the timers)
728 * so that we'll break out of the loop.
731 settimer(ttypesubopt);
735 settimer(tspeedsubopt);
738 case TELOPT_XDISPLOC:
739 settimer(xdisplocsubopt);
742 case TELOPT_OLD_ENVIRON:
743 settimer(oenvironsubopt);
746 case TELOPT_NEW_ENVIRON:
747 settimer(environsubopt);
753 set_his_want_state_wont(option);
754 if (his_state_is_will(option))
755 send_dont(option, 0);
759 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
760 if (lmodetype < NO_AUTOKLUDGE) {
761 lmodetype = NO_LINEMODE;
762 clientstat(TELOPT_LINEMODE, WONT, 0);
763 send_will(TELOPT_SGA, 1);
764 send_will(TELOPT_ECHO, 1);
766 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
769 #ifdef AUTHENTICATION
770 case TELOPT_AUTHENTICATION:
771 auth_finished(0, AUTH_REJECT);
779 set_his_state_wont(option);
781 } /* end of wontoption */
784 send_will(int option, int init)
787 if ((will_wont_resp[option] == 0 && my_state_is_will(option))||
788 my_want_state_is_will(option))
790 set_my_want_state_will(option);
791 will_wont_resp[option]++;
793 output_data((const char *)will, option);
795 DIAG(TD_OPTIONS, printoption("td: send will", option));
798 #if !defined(LINEMODE) || !defined(KLUDGELINEMODE)
800 * When we get a DONT SGA, we will try once to turn it
801 * back on. If the other side responds DONT SGA, we
802 * leave it at that. This is so that when we talk to
803 * clients that understand KLUDGELINEMODE but not LINEMODE,
804 * we'll keep them in char-at-a-time mode.
815 * Process client input.
818 DIAG(TD_OPTIONS, printoption("td: recv do", option));
820 if (will_wont_resp[option]) {
821 will_wont_resp[option]--;
822 if (will_wont_resp[option] && my_state_is_will(option))
823 will_wont_resp[option]--;
825 if ((will_wont_resp[option] == 0) && (my_want_state_is_wont(option))) {
829 # ifdef KLUDGELINEMODE
830 if (lmodetype == NO_LINEMODE)
832 if (his_state_is_wont(TELOPT_LINEMODE))
851 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
853 * If kludge linemode is in use, then we must
854 * process an incoming do SGA for linemode
857 if (lmodetype == KLUDGE_LINEMODE) {
859 * Receipt of "do SGA" in kludge
860 * linemode is the peer asking us to
861 * turn off linemode. Make note of
864 clientstat(TELOPT_LINEMODE, WONT, 0);
866 * If linemode did not get turned off
867 * then don't tell peer that we did.
868 * Breaking here forces a wont SGA to
876 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
886 * Special case for TM. We send a WILL, but
887 * pretend we sent a WONT.
889 send_will(option, 0);
890 set_my_want_state_wont(option);
891 set_my_state_wont(option);
896 * When we get a LOGOUT option, respond
897 * with a WILL LOGOUT, make sure that
898 * it gets written out to the network,
899 * and then just go away...
901 set_my_want_state_will(TELOPT_LOGOUT);
902 send_will(TELOPT_LOGOUT, 0);
903 set_my_state_will(TELOPT_LOGOUT);
913 #endif /* ENCRYPTION */
914 case TELOPT_LINEMODE:
919 case TELOPT_XDISPLOC:
920 #ifdef TELOPT_ENVIRON
921 case TELOPT_NEW_ENVIRON:
923 case TELOPT_OLD_ENVIRON:
928 set_my_want_state_will(option);
929 send_will(option, 0);
931 will_wont_resp[option]++;
932 send_wont(option, 0);
935 set_my_state_will(option);
937 } /* end of dooption */
940 send_wont(int option, int init)
943 if ((will_wont_resp[option] == 0 && my_state_is_wont(option)) ||
944 my_want_state_is_wont(option))
946 set_my_want_state_wont(option);
947 will_wont_resp[option]++;
949 output_data((const char *)wont, option);
951 DIAG(TD_OPTIONS, printoption("td: send wont", option));
955 dontoption(int option)
958 * Process client input.
962 DIAG(TD_OPTIONS, printoption("td: recv dont", option));
964 if (will_wont_resp[option]) {
965 will_wont_resp[option]--;
966 if (will_wont_resp[option] && my_state_is_wont(option))
967 will_wont_resp[option]--;
969 if ((will_wont_resp[option] == 0) && (my_want_state_is_will(option))) {
977 case TELOPT_ECHO: /* we should stop echoing */
979 # ifdef KLUDGELINEMODE
980 if ((lmodetype != REAL_LINEMODE) &&
981 (lmodetype != KLUDGE_LINEMODE))
983 if (his_state_is_wont(TELOPT_LINEMODE))
994 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
996 * If kludge linemode is in use, then we
997 * must process an incoming do SGA for
1000 if ((lmodetype == KLUDGE_LINEMODE) ||
1001 (lmodetype == KLUDGE_OK)) {
1003 * The client is asking us to turn
1006 lmodetype = KLUDGE_LINEMODE;
1007 clientstat(TELOPT_LINEMODE, WILL, 0);
1009 * If we did not turn line mode on,
1010 * then what do we say? Will SGA?
1011 * This violates design of telnet.
1012 * Gross. Very Gross.
1017 set_my_want_state_wont(option);
1018 if (my_state_is_will(option))
1019 send_wont(option, 0);
1020 set_my_state_wont(option);
1021 if (turn_on_sga ^= 1)
1022 send_will(option, 1);
1024 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
1030 set_my_want_state_wont(option);
1031 if (my_state_is_will(option))
1032 send_wont(option, 0);
1034 set_my_state_wont(option);
1036 } /* end of dontoption */
1040 int env_ovalue = -1;
1041 #else /* ENV_HACK */
1042 # define env_ovar OLD_ENV_VAR
1043 # define env_ovalue OLD_ENV_VALUE
1044 #endif /* ENV_HACK */
1046 /* envvarok(char*) */
1047 /* check that variable is safe to pass to login or shell */
1049 envvarok(char *varp)
1052 if (strcmp(varp, "TERMCAP") && /* to prevent a security hole */
1053 strcmp(varp, "TERMINFO") && /* with tgetent */
1054 strcmp(varp, "TERMPATH") &&
1055 strcmp(varp, "HOME") && /* to prevent the tegetent bug */
1056 strncmp(varp, "LD_", strlen("LD_")) && /* most systems */
1057 strncmp(varp, "_RLD_", strlen("_RLD_")) && /* IRIX */
1058 strcmp(varp, "LIBPATH") && /* AIX */
1059 strcmp(varp, "ENV") &&
1060 strcmp(varp, "BASH_ENV") &&
1061 strcmp(varp, "IFS") &&
1062 strncmp(varp, "KRB5", strlen("KRB5")) && /* Krb5 */
1064 * The above case is a catch-all for now. Here are some of
1065 * the specific ones we must avoid passing, at least until
1066 * we can prove it can be done safely. Keep this list
1067 * around un case someone wants to remove the catch-all.
1069 strcmp(varp, "KRB5_CONFIG") && /* Krb5 */
1070 strcmp(varp, "KRB5CCNAME") && /* Krb5 */
1071 strcmp(varp, "KRB5_KTNAME") && /* Krb5 */
1072 strcmp(varp, "KRBTKFILE") && /* Krb4 */
1073 strcmp(varp, "KRB_CONF") && /* CNS 4 */
1074 strcmp(varp, "KRB_REALMS") && /* CNS 4 */
1075 strcmp(varp, "RESOLV_HOST_CONF")) /* Linux */
1078 syslog(LOG_INFO, "Rejected the attempt to modify the "
1079 "environment variable \"%s\"", varp);
1087 * Look at the sub-option buffer, and try to be helpful to the other
1090 * Currently we recognize:
1102 DIAG(TD_OPTIONS, {netflush(); printsub('<', subpointer, SB_LEN()+2);});
1106 case TELOPT_TSPEED: {
1109 if (his_state_is_wont(TELOPT_TSPEED)) /* Ignore if option disabled */
1112 settimer(tspeedsubopt);
1114 if (SB_EOF() || SB_GET() != TELQUAL_IS)
1117 xspeed = atoi((char *)subpointer);
1119 while (SB_GET() != ',' && !SB_EOF());
1123 rspeed = atoi((char *)subpointer);
1124 clientstat(TELOPT_TSPEED, xspeed, rspeed);
1128 } /* end of case TELOPT_TSPEED */
1130 case TELOPT_TTYPE: { /* Yaaaay! */
1131 static char terminalname[TERMINAL_TYPE_SIZE];
1133 if (his_state_is_wont(TELOPT_TTYPE)) /* Ignore if option disabled */
1135 settimer(ttypesubopt);
1137 if (SB_EOF() || SB_GET() != TELQUAL_IS) {
1138 return; /* ??? XXX but, this is the most robust */
1141 terminaltype = terminalname;
1143 while ((terminaltype < (terminalname + sizeof terminalname-1)) &&
1151 *terminaltype++ = c; /* accumulate name */
1154 terminaltype = terminalname;
1156 } /* end of case TELOPT_TTYPE */
1159 int xwinsize, ywinsize;
1161 if (his_state_is_wont(TELOPT_NAWS)) /* Ignore if option disabled */
1166 xwinsize = SB_GET() << 8;
1169 xwinsize |= SB_GET();
1172 ywinsize = SB_GET() << 8;
1175 ywinsize |= SB_GET();
1176 clientstat(TELOPT_NAWS, xwinsize, ywinsize);
1180 } /* end of case TELOPT_NAWS */
1183 case TELOPT_LINEMODE: {
1186 if (his_state_is_wont(TELOPT_LINEMODE)) /* Ignore if option disabled */
1189 * Process linemode suboptions.
1192 break; /* garbage was sent */
1193 request = SB_GET(); /* get will/wont */
1196 break; /* another garbage check */
1198 if (request == LM_SLC) { /* SLC is not preceeded by WILL or WONT */
1200 * Process suboption buffer of slc's
1203 do_opt_slc(subpointer, subend - subpointer);
1206 } else if (request == LM_MODE) {
1209 useeditmode = SB_GET(); /* get mode flag */
1210 clientstat(LM_MODE, 0, 0);
1216 switch (SB_GET()) { /* what suboption? */
1217 case LM_FORWARDMASK:
1219 * According to spec, only server can send request for
1220 * forwardmask, and client can only return a positive response.
1221 * So don't worry about it.
1228 } /* end of case TELOPT_LINEMODE */
1230 case TELOPT_STATUS: {
1238 if (my_state_is_will(TELOPT_STATUS))
1249 } /* end of case TELOPT_STATUS */
1251 case TELOPT_XDISPLOC: {
1252 if (SB_EOF() || SB_GET() != TELQUAL_IS)
1254 settimer(xdisplocsubopt);
1255 subpointer[SB_LEN()] = '\0';
1256 if (setenv("DISPLAY", (char *)subpointer, 1) == -1)
1257 syslog(LOG_ERR, "setenv: cannot set DISPLAY=%s: %m", (char *)subpointer);
1259 } /* end of case TELOPT_XDISPLOC */
1261 #ifdef TELOPT_NEW_ENVIRON
1262 case TELOPT_NEW_ENVIRON:
1264 case TELOPT_OLD_ENVIRON: {
1266 char *cp, *varp, *valp;
1271 if (c == TELQUAL_IS) {
1272 if (subchar == TELOPT_OLD_ENVIRON)
1273 settimer(oenvironsubopt);
1275 settimer(environsubopt);
1276 } else if (c != TELQUAL_INFO) {
1280 #ifdef TELOPT_NEW_ENVIRON
1281 if (subchar == TELOPT_NEW_ENVIRON) {
1284 if ((c == NEW_ENV_VAR) || (c == ENV_USERVAR))
1292 * We only want to do this if we haven't already decided
1293 * whether or not the other side has its VALUE and VAR
1297 int last = -1; /* invalid value */
1299 int got_var = 0, got_value = 0, got_uservar = 0;
1302 * The other side might have its VALUE and VAR values
1303 * reversed. To be interoperable, we need to determine
1304 * which way it is. If the first recognized character
1305 * is a VAR or VALUE, then that will tell us what
1306 * type of client it is. If the fist recognized
1307 * character is a USERVAR, then we continue scanning
1308 * the suboption looking for two consecutive
1309 * VAR or VALUE fields. We should not get two
1310 * consecutive VALUE fields, so finding two
1311 * consecutive VALUE or VAR fields will tell us
1312 * what the client is.
1319 if (last < 0 || last == OLD_ENV_VAR
1320 || (empty && (last == OLD_ENV_VALUE)))
1326 if (last < 0 || last == OLD_ENV_VALUE
1327 || (empty && (last == OLD_ENV_VAR)))
1328 goto env_ovar_wrong;
1330 last = OLD_ENV_VALUE;
1333 /* count strings of USERVAR as one */
1334 if (last != ENV_USERVAR)
1337 if (last == OLD_ENV_VALUE)
1339 if (last == OLD_ENV_VAR)
1340 goto env_ovar_wrong;
1355 if (last == OLD_ENV_VALUE)
1357 if (last == OLD_ENV_VAR)
1358 goto env_ovar_wrong;
1361 * Ok, the first thing was a USERVAR, and there
1362 * are not two consecutive VAR or VALUE commands,
1363 * and none of the VAR or VALUE commands are empty.
1364 * If the client has sent us a well-formed option,
1365 * then the number of VALUEs received should always
1366 * be less than or equal to the number of VARs and
1367 * USERVARs received.
1369 * If we got exactly as many VALUEs as VARs and
1370 * USERVARs, the client has the same definitions.
1372 * If we got exactly as many VARs as VALUEs and
1373 * USERVARS, the client has reversed definitions.
1375 if (got_uservar + got_var == got_value) {
1377 env_ovar = OLD_ENV_VAR;
1378 env_ovalue = OLD_ENV_VALUE;
1379 } else if (got_uservar + got_value == got_var) {
1381 env_ovar = OLD_ENV_VALUE;
1382 env_ovalue = OLD_ENV_VAR;
1384 output_data("ENVIRON VALUE and VAR are reversed!\r\n"));
1393 if ((c == env_ovar) || (c == ENV_USERVAR))
1401 cp = varp = (char *)subpointer;
1406 if (subchar == TELOPT_OLD_ENVIRON) {
1409 else if (c == env_ovalue)
1416 cp = valp = (char *)subpointer;
1422 if (envvarok(varp)) {
1424 if (setenv(varp, valp, 1) == -1)
1425 syslog(LOG_ERR, "setenv: cannot set %s=%s: %m", varp, valp);
1430 cp = varp = (char *)subpointer;
1445 if (envvarok(varp)) {
1447 if (setenv(varp, valp, 1) == -1)
1448 syslog(LOG_ERR, "setenv: cannot set %s=%s: %m", varp, valp);
1454 } /* end of case TELOPT_NEW_ENVIRON */
1455 #ifdef AUTHENTICATION
1456 case TELOPT_AUTHENTICATION:
1463 * These are sent by us and cannot be sent by
1468 auth_is(subpointer, SB_LEN());
1471 auth_name(subpointer, SB_LEN());
1477 case TELOPT_ENCRYPT:
1481 case ENCRYPT_SUPPORT:
1482 encrypt_support(subpointer, SB_LEN());
1485 encrypt_is(subpointer, SB_LEN());
1488 encrypt_reply(subpointer, SB_LEN());
1491 encrypt_start(subpointer, SB_LEN());
1496 case ENCRYPT_REQSTART:
1497 encrypt_request_start(subpointer, SB_LEN());
1499 case ENCRYPT_REQEND:
1501 * We can always send an REQEND so that we cannot
1502 * get stuck encrypting. We should only get this
1503 * if we have been able to get in the correct mode
1506 encrypt_request_end();
1508 case ENCRYPT_ENC_KEYID:
1509 encrypt_enc_keyid(subpointer, SB_LEN());
1511 case ENCRYPT_DEC_KEYID:
1512 encrypt_dec_keyid(subpointer, SB_LEN());
1518 #endif /* ENCRYPTION */
1522 } /* end of switch */
1524 } /* end of suboption */
1529 clientstat(TELOPT_LINEMODE, WILL, 0);
1532 #define ADD(c) *ncp++ = c
1533 #define ADD_DATA(c) { *ncp++ = c; if (c == SE || c == IAC) *ncp++ = c; }
1537 unsigned char statusbuf[256];
1543 netflush(); /* get rid of anything waiting to go out */
1551 * We check the want_state rather than the current state,
1552 * because if we received a DO/WILL for an option that we
1553 * don't support, and the other side didn't send a DONT/WONT
1554 * in response to our WONT/DONT, then the "state" will be
1555 * WILL/DO, and the "want_state" will be WONT/DONT. We
1556 * need to go by the latter.
1558 for (i = 0; i < (unsigned char)NTELOPTS; i++) {
1559 if (my_want_state_is_will(i)) {
1565 if (his_want_state_is_will(i)) {
1573 if (his_want_state_is_will(TELOPT_LFLOW)) {
1583 if (restartany >= 0) {
1587 ADD(LFLOW_RESTART_ANY);
1589 ADD(LFLOW_RESTART_XON);
1596 if (his_want_state_is_will(TELOPT_LINEMODE)) {
1597 unsigned char *cp, *cpe;
1601 ADD(TELOPT_LINEMODE);
1607 ADD(TELOPT_LINEMODE);
1612 for (cpe = cp + len; cp < cpe; cp++)
1616 #endif /* LINEMODE */
1621 output_datalen(statusbuf, ncp - statusbuf);
1622 netflush(); /* Send it on its way */
1625 {printsub('>', statusbuf, ncp - statusbuf); netflush();});
1629 * This function appends data to nfrontp and advances nfrontp.
1630 * Returns the number of characters written altogether (the
1631 * buffer may have been flushed in the process).
1635 output_data(const char *format, ...)
1641 va_start(args, format);
1642 if ((len = vasprintf(&buf, format, args)) == -1) {
1646 output_datalen(buf, len);
1653 output_datalen(const char *buf, int len)
1655 int remaining, copied;
1657 remaining = BUFSIZ - (nfrontp - netobuf);
1659 /* Free up enough space if the room is too low*/
1660 if ((len > BUFSIZ ? BUFSIZ : len) > remaining) {
1662 remaining = BUFSIZ - (nfrontp - netobuf);
1665 /* Copy out as much as will fit */
1666 copied = remaining > len ? len : remaining;
1667 memmove(nfrontp, buf, copied);
1670 remaining -= copied;